Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
xscan.php
1<?php
2
4
9
10class Xscan extends Controller
11{
12 public static function getFilter()
13 {
14 $filterOptions = new \Bitrix\Main\UI\Filter\Options('report_filter');
15 $filters = $filterOptions->getFilter();
16
17 $filter = [];
18 foreach ($filters as $k => $v)
19 {
20 if (!$v)
21 {
22 continue;
23 }
24
25 switch ($k)
26 {
27 case 'mtime_from':
28 $filter['>=mtime'] = $v;
29 break;
30 case 'mtime_to':
31 $filter['<=mtime'] = $v;
32 break;
33 case 'ctime_from':
34 $filter['>=ctime'] = $v;
35 break;
36 case 'ctime_to':
37 $filter['<=ctime'] = $v;
38 break;
39 case 'tags':
40 foreach ($v as $t)
41 {
42 $filter[] = ['%tags' => $t];
43 }
44 break;
45
46 case 'preset':
47 switch ($v){
48 case 'a':
49 $filter[] = ['%src' => '/bitrix/admin'];
50 break;
51 case 'm':
52 $filter[] = ['%src' => '/bitrix/modules'];
53 break;
54 case 'c':
55 $filter[] = ['%src' => '/bitrix/components'];
56 break;
57 case '!m':
58 $filter[] = ['!%src' => '/bitrix/modules'];
59 break;
60 case 'pop':
61 $filter[] = ['LOGIC' => 'OR',
62 ['%src' => '/prolog_after.php'], ['%src' => '/index.php'],
63 ['%src' => '/content.php'], ['%src' => '/main.php'], ['%src' => '/spread.php'],
64 ['%src' => '/bx_root.php'], ['%src' => '/.access.php'], ['%src' => '/radio.php']
65 ];
66 break;
67 }
68 break;
69
70 case 'FIND':
71 if (strpos($v, '!') === 0)
72 {
73 $v = ltrim($v, '!');
74 $filter[] = ['LOGIC' => 'AND', ['!%src' => $v], ['!%message' => $v]];
75 }
76 else
77 {
78 $filter[] = ['LOGIC' => 'OR', ['%src' => $v], ['%message' => $v]];
79 }
80 break;
81 }
82
83 }
84
85 return $filter;
86 }
87
88
89 protected function processBeforeAction(Action $action): bool
90 {
91 ini_set('display_errors', '0');
92 Loc::loadMessages(__FILE__);
93
94 if (!Controller::getCurrentUser()->isAdmin())
95 {
96 return false;
97 }
98
99 return parent::processBeforeAction($action);
100 }
101
102 public function prisonAction(string $file)
103 {
104 $file = '/' . trim($file, '/');
105
106 if (!$file || !file_exists($file))
107 {
108 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_FILE_NOT_FOUND") . htmlspecialcharsbx($file), 'red');
109 }
110 else
111 {
112 $new_f = preg_replace('#\.php[578]?$#i', '.ph_', $file);
113 if (rename($file, $new_f))
114 {
115 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_RENAMED") . htmlspecialcharsbx($new_f));
116 }
117 else
118 {
119 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_ERR_RENAME") . htmlspecialcharsbx($file), 'red');
120 }
121 }
122
123 return $msg;
124 }
125
126 public function releaseAction(string $file)
127 {
128 $file = '/' . trim($file, '/');
129
130 if (!$file || !file_exists($file))
131 {
132 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_FILE_NOT_FOUND") . htmlspecialcharsbx($file), 'red');
133 }
134 else
135 {
136 $new_f = preg_replace('#\.ph_$#', '.php', $file);
137 if (rename($file, $new_f))
138 {
139 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_RENAMED") . htmlspecialcharsbx($new_f));
140 }
141 else
142 {
143 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_ERR_RENAME") . htmlspecialcharsbx($file), 'red');
144 }
145 }
146
147 return $msg;
148 }
149
150 public function hideAction(string $file)
151 {
152 $file = '/' . trim($file, '/');
153 $msg = '';
154
155 $ent = XScanResultTable::getList(['select' => ['id'], 'filter' => ['src' => $file]])->fetch();
156
157 if ($ent)
158 {
159 XScanResultTable::delete($ent['id']);
160 $msg = \CBitrixXscan::ShowMsg(Loc::getMessage("BITRIX_XSCAN_HIDED") . htmlspecialcharsbx($file));
161 }
162
163 return $msg;
164 }
165
166 public function hideFilesAction(array $files, string $all='false')
167 {
168 \Bitrix\Main\Type\Collection::normalizeArrayValuesByInt($files);
169
170 $filter = $all === 'true' ? self::getFilter(): ['@id' => $files];
171
173
174 return '';
175 }
176
177 public function addErrorAction(string $file)
178 {
179 $file = '/' . trim($file, '/');
180
181 if ($file)
182 {
183 XScanResultTable::add(['type' => 'file', 'src' => $file, 'message' => 'error', 'score' => 0.5]);
184 }
185
186 return '';
187 }
188
189 public function scanAction(string $start_path, string $break_point = '', string $clean = 'N', int $progress = 0, int $total = 0)
190 {
191 $start_path = $start_path ? $start_path : $_SERVER['DOCUMENT_ROOT'];
192 $start_path = rtrim($start_path, '/');
193
194 $scaner = new \CBitrixXscan($progress, $total);
195 $scaner->skip_path = $break_point;
196
197 $session = \Bitrix\Main\Application::getInstance()->getSession();
198
199 if (!is_dir($start_path))
200 {
201 $msg = Loc::getMessage("BITRIX_XSCAN_NACALQNYY_PUTQ_NE_NA");
202 return ['error' => $msg];
203 }
204
205 if ($clean == 'Y')
206 {
207 $session['xscan_page'] = 1;
208 $session->save();
209
210 $scaner->clean();
211 $scaner->CheckEvents();
212 $scaner->CheckAgents();
213 $scaner->Search($start_path, 'count');
214 }
215 else
216 {
217 $session->save();
218 }
219
220 $scaner->Search($start_path);
221 $scaner->SavetoDB();
222
223
224 $prc = $scaner->total == 0 ? min(75, (int)($scaner->progress / 1000)) : (int)($scaner->progress * 100 / $scaner->total);
225
226 return [
227 'progress' => $scaner->progress,
228 'total' => $scaner->total,
229 'break_point' => $scaner->break_point,
230 'prc' => $prc,
231 ];
232 }
233
234 public function findHtaccessAction(string $break_point = '')
235 {
236 $localStorage = \Bitrix\Main\Application::getInstance()->getLocalSession('xscan_htaccess');
237
238 if (!$break_point)
239 {
240 $localStorage->clear();
241 $localStorage->set('timestamp', time());
242 $localStorage->set('status', 'pending');
243 }
244
245 $path = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
246
247 $searcher = new \CBitrixXscanHtaccess();
248 $searcher->skip_path = $break_point;
249 $searcher->Search($path);
250
251 $files = $localStorage['files'] ?? [];
252
253 if($searcher->result)
254 {
255 $files = array_merge($files, $searcher->result);
256 $localStorage->set('files', $files);
257 }
258
259 if (!$searcher->break_point)
260 {
261 $localStorage->set('status', 'done');
262 }
263
264
265 return [
266 'count' => count($files),
267 'break_point' => $searcher->break_point
268 ];
269
270 }
271}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
findHtaccessAction(string $break_point='')
Definition xscan.php:234
processBeforeAction(Action $action)
Definition xscan.php:89
scanAction(string $start_path, string $break_point='', string $clean='N', int $progress=0, int $total=0)
Definition xscan.php:189
hideFilesAction(array $files, string $all='false')
Definition xscan.php:166