]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Logs/View.php
Merge pull request #11003 from annando/fix-api
[friendica.git] / src / Module / Admin / Logs / View.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin\Logs;
23
24 use Friendica\DI;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Theme;
27 use Friendica\Module\BaseAdmin;
28 use Psr\Log\LogLevel;
29
30 class View extends BaseAdmin
31 {
32         const LIMIT = 500;
33
34         public function content(): string
35         {
36                 parent::content();
37
38                 $t = Renderer::getMarkupTemplate('admin/logs/view.tpl');
39                 DI::page()->registerFooterScript(Theme::getPathForFile('js/module/admin/logs/view.js'));
40
41                 $f     = DI::config()->get('system', 'logfile');
42                 $data  = null;
43                 $error = null;
44
45                 $search = $_GET['q'] ?? '';
46
47                 $filters_valid_values = [
48                         'level' => [
49                                 '',
50                                 LogLevel::CRITICAL,
51                                 LogLevel::ERROR,
52                                 LogLevel::WARNING,
53                                 LogLevel::NOTICE,
54                                 LogLevel::INFO,
55                                 LogLevel::DEBUG,
56                         ],
57                         'context' => ['', 'index', 'worker'],
58                 ];
59                 $filters = [
60                         'level'   => $_GET['level'] ?? '',
61                         'context' => $_GET['context'] ?? '',
62                 ];
63                 foreach ($filters as $k => $v) {
64                         if ($v == '' || !in_array($v, $filters_valid_values[$k])) {
65                                 unset($filters[$k]);
66                         }
67                 }
68
69                 if (!file_exists($f)) {
70                         $error = DI::l10n()->t('Error trying to open <strong>%1$s</strong> log file.<br/>Check to see if file %1$s exist and is readable.', $f);
71                 } else {
72                         try {
73                                 $data = DI::parsedLogIterator()
74                                                 ->open($f)
75                                                 ->withLimit(self::LIMIT)
76                                                 ->withFilters($filters)
77                                                 ->withSearch($search);
78                         } catch (\Exception $e) {
79                                 $error = DI::l10n()->t('Couldn\'t open <strong>%1$s</strong> log file.<br/>Check to see if file %1$s is readable.', $f);
80                         }
81                 }
82                 return Renderer::replaceMacros($t, [
83                         '$title' => DI::l10n()->t('Administration'),
84                         '$page'  => DI::l10n()->t('View Logs'),
85                         '$l10n'  => [
86                                 'Search'                => DI::l10n()->t('Search'),
87                                 'Search_in_logs'        => DI::l10n()->t('Search in logs'),
88                                 'Show_all'              => DI::l10n()->t('Show all'),
89                                 'Date'                  => DI::l10n()->t('Date'),
90                                 'Level'                 => DI::l10n()->t('Level'),
91                                 'Context'               => DI::l10n()->t('Context'),
92                                 'Message'               => DI::l10n()->t('Message'),
93                                 'ALL'                   => DI::l10n()->t('ALL'),
94                                 'View_details'          => DI::l10n()->t('View details'),
95                                 'Click_to_view_details' => DI::l10n()->t('Click to view details'),
96                                 'Event_details'         => DI::l10n()->t('Event details'),
97                                 'Data'                  => DI::l10n()->t('Data'),
98                                 'Source'                => DI::l10n()->t('Source'),
99                                 'File'                  => DI::l10n()->t('File'),
100                                 'Line'                  => DI::l10n()->t('Line'),
101                                 'Function'              => DI::l10n()->t('Function'),
102                                 'UID'                   => DI::l10n()->t('UID'),
103                                 'Process_ID'            => DI::l10n()->t('Process ID'),
104                                 'Close'                 => DI::l10n()->t('Close'),
105                         ],
106                         '$data'          => $data,
107                         '$q'             => $search,
108                         '$filters'       => $filters,
109                         '$filtersvalues' => $filters_valid_values,
110                         '$error'         => $error,
111                         '$logname'       => DI::config()->get('system', 'logfile'),
112                 ]);
113         }
114 }