Issues (2407)

administration/controller/tool/log.php (7 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ControllerToolLog extends \Divine\Engine\Core\Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    private $error = array();
26
27
    public function index()
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
29
        $this->load->language('tool/log');
30
31
        $this->document->setTitle($this->language->get('heading_title'));
32
33
        $data['heading_title'] = $this->language->get('heading_title');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
34
35
        $data['text_list'] = $this->language->get('text_list');
36
        $data['text_confirm'] = $this->language->get('text_confirm');
37
38
        $data['button_download'] = $this->language->get('button_download');
39
        $data['button_clear'] = $this->language->get('button_clear');
40
41
        if (isset($this->session->data['error'])) {
42
            $data['error_warning'] = $this->session->data['error'];
43
44
            unset($this->session->data['error']);
45
        } elseif (isset($this->error['warning'])) {
46
            $data['error_warning'] = $this->error['warning'];
47
        } else {
48
            $data['error_warning'] = '';
49
        }
50
51
        if (isset($this->session->data['success'])) {
52
            $data['success'] = $this->session->data['success'];
53
54
            unset($this->session->data['success']);
55
        } else {
56
            $data['success'] = '';
57
        }
58
59
        $data['breadcrumbs'] = array();
60
61
        $data['breadcrumbs'][] = array(
62
            'text' => $this->language->get('text_home'),
63
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
64
        );
65
66
        $data['breadcrumbs'][] = array(
67
            'text' => $this->language->get('heading_title'),
68
            'href' => $this->url->link('tool/log', 'token=' . $this->session->data['token'], true)
69
        );
70
71
        $data['download'] = $this->url->link('tool/log/download', 'token=' . $this->session->data['token'], true);
72
        $data['clear'] = $this->url->link('tool/log/clear', 'token=' . $this->session->data['token'], true);
73
74
        $data['log'] = '';
75
76
        $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/logs/' . $this->config->get('config_error_filename');
77
78
        if (file_exists($file)) {
79
            $size = filesize($file);
80
81
            if ($size >= 5242880) {
82
                $suffix = array(
83
                    'B',
84
                    'KB',
85
                    'MB',
86
                    'GB',
87
                    'TB',
88
                    'PB',
89
                    'EB',
90
                    'ZB',
91
                    'YB'
92
                );
93
94
                $i = 0;
95
96
                while (($size / 1024) > 1) {
97
                    $size = $size / 1024;
98
                    $i++;
99
                }
100
101
                $data['error_warning'] = sprintf($this->language->get('error_warning'), basename($file), round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i]);
0 ignored issues
show
substr($size, 0, strpos($size, '.') + 4) of type string is incompatible with the type double expected by parameter $val of round(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
                $data['error_warning'] = sprintf($this->language->get('error_warning'), basename($file), round(/** @scrutinizer ignore-type */ substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i]);
Loading history...
102
            } else {
103
                $data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
0 ignored issues
show
FILE_USE_INCLUDE_PATH of type integer is incompatible with the type boolean expected by parameter $use_include_path of file_get_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                $data['log'] = file_get_contents($file, /** @scrutinizer ignore-type */ FILE_USE_INCLUDE_PATH, null);
Loading history...
104
            }
105
        }
106
107
        $data['header'] = $this->load->controller('common/header');
108
        $data['column'] = $this->load->controller('common/column_left');
109
        $data['footer'] = $this->load->controller('common/footer');
110
111
        $this->response->setOutput($this->load->view('tool/log', $data));
112
    }
113
114
    public function download()
115
    {
116
        $this->load->language('tool/log');
117
118
        $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/logs/' . $this->config->get('config_error_filename');
119
120
        if (file_exists($file) && filesize($file) > 0) {
121
            $this->response->addheader('Pragma: public');
122
            $this->response->addheader('Expires: 0');
123
            $this->response->addheader('Content-Description: File Transfer');
124
            $this->response->addheader('Content-Type: application/octet-stream');
125
            $this->response->addheader('Content-Disposition: attachment; filename="' . $this->config->get('config_name') . '_' . date('Y-m-d_H-i-s', time()) . '_error.log"');
126
            $this->response->addheader('Content-Transfer-Encoding: binary');
127
128
            $this->response->setOutput(file_get_contents($file, FILE_USE_INCLUDE_PATH, null));
0 ignored issues
show
FILE_USE_INCLUDE_PATH of type integer is incompatible with the type boolean expected by parameter $use_include_path of file_get_contents(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
            $this->response->setOutput(file_get_contents($file, /** @scrutinizer ignore-type */ FILE_USE_INCLUDE_PATH, null));
Loading history...
129
        } else {
130
            $this->session->data['error'] = sprintf($this->language->get('error_warning'), basename($file), '0B');
131
132
            $this->response->redirect($this->url->link('tool/log', 'token=' . $this->session->data['token'], true));
133
        }
134
    }
135
136
    public function clear()
137
    {
138
        $this->load->language('tool/log');
139
140
        if (!$this->user->hasPermission('modify', 'tool/log')) {
141
            $this->session->data['error'] = $this->language->get('error_permission');
142
        } else {
143
            $file = $_SERVER['DOCUMENT_ROOT'] . '/storage/logs/' . $this->config->get('config_error_filename');
144
145
            $handle = fopen($file, 'w+');
146
147
            fclose($handle);
0 ignored issues
show
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
148
149
            $this->session->data['success'] = $this->language->get('text_success');
150
        }
151
152
        $this->response->redirect($this->url->link('tool/log', 'token=' . $this->session->data['token'], true));
153
    }
154
}
155