|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\PBXCoreREST\Lib\SysLogs; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Core\System\Processes; |
|
23
|
|
|
use MikoPBX\Core\System\System; |
|
24
|
|
|
use MikoPBX\Core\System\Util; |
|
25
|
|
|
use MikoPBX\PBXCoreREST\Lib\PBXApiResult; |
|
26
|
|
|
use Phalcon\Di; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets partially filtered log file strings. |
|
30
|
|
|
* |
|
31
|
|
|
* @package MikoPBX\PBXCoreREST\Lib\SysLogs |
|
32
|
|
|
*/ |
|
33
|
|
|
class GetLogFromFileAction extends \Phalcon\Di\Injectable |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Gets partially filtered log file strings. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $data An array containing the following parameters: |
|
39
|
|
|
* - filename (string): The name of the log file. |
|
40
|
|
|
* - filter (string): The filter string. |
|
41
|
|
|
* - lines (int): The number of lines to return. |
|
42
|
|
|
* - offset (int): The number of lines to skip. |
|
43
|
|
|
* |
|
44
|
|
|
* @return PBXApiResult An object containing the result of the API call. |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function main(array $data): PBXApiResult |
|
47
|
|
|
{ |
|
48
|
|
|
$filename = (string)($data['filename'] ?? ''); |
|
49
|
|
|
$filter = (string)($data['filter'] ?? ''); |
|
50
|
|
|
$lines = (int)($data['lines'] ?? ''); |
|
51
|
|
|
$offset = (int)($data['offset'] ?? ''); |
|
52
|
|
|
|
|
53
|
|
|
$res = new PBXApiResult(); |
|
54
|
|
|
$res->processor = __METHOD__; |
|
55
|
|
|
$filename = System::getLogDir() . '/' . $filename; |
|
56
|
|
|
if (!file_exists($filename)) { |
|
57
|
|
|
$res->success = false; |
|
58
|
|
|
$res->messages[] = 'No access to the file ' . $filename; |
|
59
|
|
|
} else { |
|
60
|
|
|
$res->success = true; |
|
61
|
|
|
$head = Util::which('head'); |
|
62
|
|
|
$grep = '/bin/grep'; |
|
63
|
|
|
if (!is_executable($grep)) { |
|
64
|
|
|
$grep = Util::which('grep'); |
|
65
|
|
|
} |
|
66
|
|
|
$tail = Util::which('tail'); |
|
67
|
|
|
$filter = escapeshellarg($filter); |
|
68
|
|
|
$linesPlusOffset = $lines + $offset; |
|
69
|
|
|
|
|
70
|
|
|
$di = Di::getDefault(); |
|
71
|
|
|
$dirsConfig = $di->getShared('config'); |
|
72
|
|
|
$cacheDir = $dirsConfig->path('www.downloadCacheDir'); |
|
73
|
|
|
if (!file_exists($cacheDir)) { |
|
74
|
|
|
Util::mwMkdir($cacheDir, true); |
|
75
|
|
|
} |
|
76
|
|
|
$filenameTmp = $cacheDir . '/' . __FUNCTION__ . '_' . time() . '.log'; |
|
77
|
|
|
if (empty($filter)) { |
|
78
|
|
|
$cmd = "{$tail} -n {$linesPlusOffset} {$filename}"; |
|
79
|
|
|
} else { |
|
80
|
|
|
$cmd = "{$grep} --text -h -e " . str_replace('&', "' -e '", $filter) . " -F {$filename} | $tail -n {$linesPlusOffset}"; |
|
81
|
|
|
} |
|
82
|
|
|
if ($offset > 0) { |
|
83
|
|
|
$cmd .= " | {$head} -n {$lines}"; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$sedPath = Util::which('sed'); |
|
87
|
|
|
$cmd .= ' | ' . $sedPath . ' -E \'s/\\\\([tnrfvb]|040)/ /g\''; |
|
88
|
|
|
$cmd .= " > $filenameTmp"; |
|
89
|
|
|
|
|
90
|
|
|
Processes::mwExec("$cmd; chown www:www $filenameTmp"); |
|
91
|
|
|
$res->data['cmd'] = $cmd; |
|
92
|
|
|
$res->data['filename'] = $filenameTmp; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $res; |
|
96
|
|
|
} |
|
97
|
|
|
} |