1: | <?php
2: | namespace Opencart\Admin\Controller\Tool;
3: | /**
4: | * Class Log
5: | *
6: | * @package Opencart\Admin\Controller\Tool
7: | */
8: | class Log extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('tool/log');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | $data['breadcrumbs'] = [];
20: |
21: | $data['breadcrumbs'][] = [
22: | 'text' => $this->language->get('text_home'),
23: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
24: | ];
25: |
26: | $data['breadcrumbs'][] = [
27: | 'text' => $this->language->get('heading_title'),
28: | 'href' => $this->url->link('tool/log', 'user_token=' . $this->session->data['user_token'])
29: | ];
30: |
31: | if (isset($this->session->data['error'])) {
32: | $data['error_warning'] = $this->session->data['error'];
33: |
34: | unset($this->session->data['error']);
35: | } else {
36: | $data['error_warning'] = '';
37: | }
38: |
39: | $file = DIR_LOGS . $this->config->get('config_error_filename');
40: |
41: | if (!is_file($file)) {
42: | file_put_contents($file, '', FILE_APPEND);
43: | }
44: |
45: | $data['log'] = [];
46: |
47: | $files = glob(DIR_LOGS . '*.log');
48: |
49: | foreach ($files as $file) {
50: | $error = '';
51: |
52: | $filename = basename($file);
53: |
54: | $size = filesize($file);
55: |
56: | if ($size >= 3145728) {
57: | $suffix = [
58: | 'B',
59: | 'KB',
60: | 'MB',
61: | 'GB',
62: | 'TB',
63: | 'PB',
64: | 'EB',
65: | 'ZB',
66: | 'YB'
67: | ];
68: |
69: | $i = 0;
70: |
71: | while (($size / 1024) > 1) {
72: | $size /= 1024;
73: | $i++;
74: | }
75: |
76: | $error = sprintf($this->language->get('error_size'), $filename, round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i]);
77: | }
78: |
79: | $handle = fopen($file, 'r+');
80: |
81: | $data['logs'][] = [
82: | 'name' => $filename,
83: | 'output' => fread($handle, 3145728),
84: | 'download' => $this->url->link('tool/log.download', 'user_token=' . $this->session->data['user_token'] . '&filename=' . $filename),
85: | 'clear' => $this->url->link('tool/log.clear', 'user_token=' . $this->session->data['user_token'] . '&filename=' . $filename),
86: | 'error' => $error
87: | ];
88: |
89: | fclose($handle);
90: | }
91: |
92: | $data['user_token'] = $this->session->data['user_token'];
93: |
94: | $data['header'] = $this->load->controller('common/header');
95: | $data['column_left'] = $this->load->controller('common/column_left');
96: | $data['footer'] = $this->load->controller('common/footer');
97: |
98: | $this->response->setOutput($this->load->view('tool/log', $data));
99: | }
100: |
101: | /**
102: | * Download
103: | *
104: | * @return void
105: | */
106: | public function download(): void {
107: | $this->load->language('tool/log');
108: |
109: | if (isset($this->request->get['filename'])) {
110: | $filename = (string)basename(html_entity_decode($this->request->get['filename'], ENT_QUOTES, 'UTF-8'));
111: | } else {
112: | $filename = '';
113: | }
114: |
115: | $file = DIR_LOGS . $filename;
116: |
117: | if (!is_file($file)) {
118: | $this->session->data['error'] = sprintf($this->language->get('error_file'), $filename);
119: |
120: | $this->response->redirect($this->url->link('tool/log', 'user_token=' . $this->session->data['user_token'], true));
121: | }
122: |
123: | if (!filesize($file)) {
124: | $this->session->data['error'] = sprintf($this->language->get('error_empty'), $filename);
125: |
126: | $this->response->redirect($this->url->link('tool/log', 'user_token=' . $this->session->data['user_token'], true));
127: | }
128: |
129: | $this->response->addheader('Pragma: public');
130: | $this->response->addheader('Expires: 0');
131: | $this->response->addheader('Content-Description: File Transfer');
132: | $this->response->addheader('Content-Type: application/octet-stream');
133: | $this->response->addheader('Content-Disposition: attachment; filename="' . $this->config->get('config_name') . '_' . date('Y-m-d_H-i-s', time()) . '_error.log"');
134: | $this->response->addheader('Content-Transfer-Encoding: binary');
135: |
136: | $this->response->setOutput(file_get_contents($file, true, null));
137: | }
138: |
139: | /**
140: | * Clear
141: | *
142: | * @return void
143: | */
144: | public function clear(): void {
145: | $this->load->language('tool/log');
146: |
147: | if (isset($this->request->get['filename'])) {
148: | $filename = (string)basename(html_entity_decode($this->request->get['filename'], ENT_QUOTES, 'UTF-8'));
149: | } else {
150: | $filename = '';
151: | }
152: |
153: | $json = [];
154: |
155: | if (!$this->user->hasPermission('modify', 'tool/log')) {
156: | $json['error'] = $this->language->get('error_permission');
157: | }
158: |
159: | $file = DIR_LOGS . $filename;
160: |
161: | if (!is_file($file)) {
162: | $json['error'] = sprintf($this->language->get('error_file'), $filename);
163: | }
164: |
165: | if (!$json) {
166: | $handle = fopen($file, 'w+');
167: |
168: | fclose($handle);
169: |
170: | $json['success'] = $this->language->get('text_success');
171: | }
172: |
173: | $this->response->addHeader('Content-Type: application/json');
174: | $this->response->setOutput(json_encode($json));
175: | }
176: | }
177: |

OpenCart API API documentation generated by ApiGen dev-master