Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
repo.php
1<?php
3
4use \Bitrix\Landing\Manager;
5use \Bitrix\Main\Localization\Loc;
6use \Bitrix\Rest\Marketplace\Client;
7use \Bitrix\Rest\PlacementTable;
8use \Bitrix\Landing\Placement;
9use \Bitrix\Landing\Block as BlockCore;
10use \Bitrix\Landing\Repo as RepoCore;
11use \Bitrix\Landing\PublicActionResult;
12use \Bitrix\Landing\Node\StyleImg;
13
14Loc::loadMessages(__FILE__);
15
16class Repo
17{
24 public static function checkContent($content, $splitter = '#SANITIZE#')
25 {
26 $result = new PublicActionResult();
27 $content = Manager::sanitize(
28 $content,
29 $bad,
30 $splitter
31 );
32 $result->setResult(array(
33 'is_bad' => $bad,
34 'content' => $content
35 ));
36 return $result;
37 }
38
46 public static function register(string $code, array $fields, array $manifest = []): PublicActionResult
47 {
48 $result = new PublicActionResult();
49 $error = new \Bitrix\Landing\Error;
50
51 // unset not allowed keys
52 $notAllowed = array('callbacks');
53 foreach ($notAllowed as $key)
54 {
55 if (isset($manifest[$key]))
56 {
57 unset($manifest[$key]);
58 }
59 }
60
61 if (!is_array($fields))
62 {
63 $fields = array();
64 }
65
66 $check = false;
67 $fields['XML_ID'] = trim($code);
68
69 // check intersect item of nodes and styles for background type
70 if (is_array($manifest['nodes'] ?? null))
71 {
72 foreach ($manifest['nodes'] as $selector => $manifestItem)
73 {
74 $styleItem = null;
75
76 if (isset($manifest['style'][$selector]))
77 {
78 $styleItem = $manifest['style'][$selector];
79 }
80 if (isset($manifest['style']['nodes'][$selector]))
81 {
82 $styleItem = $manifest['style']['nodes'][$selector];
83 }
84
85 if ($styleItem['type'] ?? null)
86 {
87 if (!empty(array_intersect((array)$styleItem['type'], StyleImg::STYLES_WITH_IMAGE)))
88 {
89 $error->addError(
90 'MANIFEST_INTERSECT_IMG',
91 Loc::getMessage('LANDING_APP_MANIFEST_INTERSECT_IMG', ['#selector#' => $selector])
92 );
93 $result->setError($error);
94 return $result;
95 }
96 }
97 }
98 }
99
100 if (isset($fields['CONTENT']))
101 {
102 // sanitize content
103 $fields['CONTENT'] = Manager::sanitize(
104 $fields['CONTENT'],
105 $bad
106 );
107 if ($bad)
108 {
109 $error->addError(
110 'CONTENT_IS_BAD',
111 Loc::getMessage('LANDING_APP_CONTENT_IS_BAD')
112 );
113 $result->setError($error);
114 return $result;
115 }
116 // sanitize card's content
117 if (
118 isset($manifest['cards']) &&
119 is_array($manifest['cards'])
120 )
121 {
122 foreach ($manifest['cards'] as $cardCode => &$card)
123 {
124 if (
125 isset($card['presets']) &&
126 is_array($card['presets'])
127 )
128 {
129 foreach ($card['presets'] as $presetCode => &$preset)
130 {
131 foreach (['html', 'name', 'values'] as $code)
132 {
133 if (isset($preset[$code]))
134 {
135 $preset[$code] = Manager::sanitize(
136 $preset[$code],
137 $bad
138 );
139 if ($bad)
140 {
141 $error->addError(
142 'PRESET_CONTENT_IS_BAD',
144 'LANDING_APP_PRESET_CONTENT_IS_BAD',
145 array(
146 '#preset#' => $presetCode,
147 '#card#' => $cardCode
148 ))
149 );
150 $result->setError($error);
151 return $result;
152 }
153 }
154 }
155 }
156 unset($preset);
157 }
158 }
159 unset($card);
160 }
161 }
162
163 $fields['MANIFEST'] = serialize($manifest);
164
165 // set app code
167 {
168 $fields['APP_CODE'] = $app['CODE'];
169 }
170
171 // check unique
172 if ($fields['XML_ID'])
173 {
174 $check = RepoCore::getList(array(
175 'select' => array(
176 'ID'
177 ),
178 'filter' =>
179 isset($fields['APP_CODE'])
180 ? array(
181 '=XML_ID' => $fields['XML_ID'],
182 '=APP_CODE' => $fields['APP_CODE']
183 )
184 : array(
185 '=XML_ID' => $fields['XML_ID']
186 )
187 ))->fetch();
188 }
189
190 // register (add / update)
191 if ($check)
192 {
193 $res = RepoCore::update($check['ID'], $fields);
194 }
195 else
196 {
197 $res = RepoCore::add($fields);
198 }
199 if ($res->isSuccess())
200 {
201 if (
202 isset($fields['RESET']) &&
203 $fields['RESET'] == 'Y'
204 )
205 {
206 \Bitrix\Landing\Update\Block::register(
207 'repo_' . $res->getId()
208 );
209 }
210 $result->setResult($res->getId());
211 }
212 else
213 {
214 $error->addFromResult($res);
215 $result->setError($error);
216 }
217
218 return $result;
219 }
220
226 public static function unregister($code)
227 {
228 $result = new PublicActionResult();
229 $error = new \Bitrix\Landing\Error;
230
231 $result->setResult(false);
232
233 if (!is_string($code))
234 {
235 return $result;
236 }
237
238 // search and delete
239 if ($code)
240 {
241 // set app code
242 $app = \Bitrix\Landing\PublicAction::restApplication();
243
244 $row = RepoCore::getList(array(
245 'select' => array(
246 'ID'
247 ),
248 'filter' =>
249 isset($app['CODE'])
250 ? array(
251 '=XML_ID' => $code,
252 '=APP_CODE' => $app['CODE']
253 )
254 : array(
255 '=XML_ID' => $code
256 )
257 ))->fetch();
258 if ($row)
259 {
260 // delete all sush blocks from landings
261 $codeToDelete = array();
262 $res = RepoCore::getList(array(
263 'select' => array(
264 'ID'
265 ),
266 'filter' =>
267 isset($app['CODE'])
268 ? array(
269 '=XML_ID' => $code,
270 '=APP_CODE' => $app['CODE']
271 )
272 : array(
273 '=XML_ID' => $code
274 )
275 ));
276 while ($rowRepo = $res->fetch())
277 {
278 $codeToDelete[] = 'repo_' . $rowRepo['ID'];
279 }
280 if (!empty($codeToDelete))
281 {
282 BlockCore::deleteByCode($codeToDelete);
283 }
284 // delete block from repo
285 $res = RepoCore::delete($row['ID']);
286 if ($res->isSuccess())
287 {
288 $result->setResult(true);
289 }
290 else
291 {
292 $error->addFromResult($res);
293 }
294 }
295 }
296
297 $result->setError($error);
298
299 return $result;
300 }
301
307 public static function getAppInfo($code)
308 {
309 $result = new PublicActionResult();
310 $error = new \Bitrix\Landing\Error;
311 $app = array();
312
313 if (!is_string($code))
314 {
315 return $result;
316 }
317
318 if ($appLocal = RepoCore::getAppByCode($code))
319 {
320 $app = array(
321 'CODE' => $appLocal['CODE'],
322 'NAME' => $appLocal['APP_NAME'],
323 'DATE_FINISH' => (string)$appLocal['DATE_FINISH'],
324 'PAYMENT_ALLOW' => $appLocal['PAYMENT_ALLOW'],
325 'ICON' => '',
326 'PRICE' => array(),
327 'UPDATES' => 0
328 );
329 if (\Bitrix\Main\Loader::includeModule('rest'))
330 {
331 $appRemote = Client::getApp($code);
332 if (isset($appRemote['ITEMS']))
333 {
334 $data = $appRemote['ITEMS'];
335 if (isset($data['ICON']))
336 {
337 $app['ICON'] = $data['ICON'];
338 }
339 if (isset($data['PRICE']) && !empty($data['PRICE']))
340 {
341 $app['PRICE'] = $data['PRICE'];
342 }
343 }
344 $updates = Client::getUpdates(array(
345 $code => $appLocal['VERSION']
346 ));
347 if (
348 isset($updates['ITEMS'][0]['VERSIONS']) &&
349 is_array($updates['ITEMS'][0]['VERSIONS'])
350 )
351 {
352 $app['UPDATES'] = count($updates['ITEMS'][0]['VERSIONS']);
353 }
354 }
355 $result->setResult($app);
356 }
357
358 if (empty($app))
359 {
360 $error->addError(
361 'NOT_FOUND',
362 Loc::getMessage('LANDING_APP_NOT_FOUND')
363 );
364 }
365
366 $result->setError($error);
367
368 return $result;
369 }
370
376 public static function bind(array $fields)
377 {
378 $result = new PublicActionResult();
379 $error = new \Bitrix\Landing\Error;
380 \trimArr($fields);
381
383 {
384 $fields['APP_ID'] = $app['ID'];
385 }
386
387 $res = Placement::getList(array(
388 'select' => array(
389 'ID'
390 ),
391 'filter' => array(
392 'APP_ID' => isset($fields['APP_ID'])
393 ? $fields['APP_ID']
394 : false,
395 'PLACEMENT' => isset($fields['PLACEMENT'])
396 ? $fields['PLACEMENT']
397 : false,
398 'PLACEMENT_HANDLER' => isset($fields['PLACEMENT_HANDLER'])
399 ? $fields['PLACEMENT_HANDLER']
400 : false
401 )
402 ));
403 // add, if not exist
404 if (!$res->fetch())
405 {
406 if (\Bitrix\Main\Loader::includeModule('rest'))
407 {
408 // first try add in the local table
409 $resLocal = Placement::add($fields);
410 if ($resLocal->isSuccess())
411 {
412 // then add in the rest table
413 $resRest = PlacementTable::add(
414 $fields
415 );
416 if ($resRest->isSuccess())
417 {
418 $result->setResult(true);
419 }
420 else
421 {
422 $error->addFromResult($resRest);
423 Placement::delete($resLocal->getId());
424 }
425 }
426 else
427 {
428 $error->addFromResult($resLocal);
429 }
430 }
431 }
432 else
433 {
434 $error->addError(
435 'PLACEMENT_EXIST',
436 Loc::getMessage('LANDING_APP_PLACEMENT_EXIST')
437 );
438 }
439
440 $result->setError($error);
441
442 return $result;
443 }
444
451 public static function unbind($code, $handler = null)
452 {
453 $result = new PublicActionResult();
454 $error = new \Bitrix\Landing\Error;
455
456 if (!is_string($code))
457 {
458 return $result;
459 }
460
461 $code = trim($code);
462 $wasDeleted = false;
463
465 {
466 $fields['APP_ID'] = $app['ID'];
467 }
468 if (
469 !isset($fields['APP_ID']) ||
470 !$fields['APP_ID']
471 )
472 {
473 return $result;
474 }
475
476 // common ORM params
477 $params = [
478 'select' => [
479 'ID'
480 ],
481 'filter' => [
482 'APP_ID' => $fields['APP_ID'],
483 '=PLACEMENT' => $code
484 ]
485 ];
486 if ($handler)
487 {
488 $params['filter']['=PLACEMENT_HANDLER'] = trim($handler);
489 }
490
491 // at first, delete local binds
492 $res = Placement::getList($params);
493 while ($row = $res->fetch())
494 {
495 $wasDeleted = true;
496 Placement::delete($row['ID']);
497 }
498 unset($res, $row);
499
500 // then delete from rest placements
501 if (\Bitrix\Main\Loader::includeModule('rest'))
502 {
503 $res = PlacementTable::getList($params);
504 while ($row = $res->fetch())
505 {
506 PlacementTable::delete($row['ID']);
507 }
508 unset($res, $row);
509 }
510
511 // make answer
512 if ($wasDeleted)
513 {
514 $result->setResult(true);
515 }
516 else
517 {
518 $error->addError(
519 'PLACEMENT_NO_EXIST',
520 Loc::getMessage('LANDING_APP_PLACEMENT_NO_EXIST')
521 );
522 $result->setError($error);
523 }
524
525 return $result;
526 }
527
533 public static function getList(array $params = array())
534 {
535 $result = new PublicActionResult();
536 $params = $result->sanitizeKeys($params);
537
538 if (!is_array($params))
539 {
540 $params = array();
541 }
542 if (
543 !isset($params['filter']) ||
544 !is_array($params['filter'])
545 )
546 {
547 $params['filter'] = array();
548 }
549 // set app code
551 {
552 $params['filter']['APP_CODE'] = $app['CODE'];
553 }
554 else
555 {
556 $params['filter']['APP_CODE'] = false;
557 }
558
559 $data = array();
560 $res = RepoCore::getList($params);
561 while ($row = $res->fetch())
562 {
563 if (isset($row['DATE_CREATE']))
564 {
565 $row['DATE_CREATE'] = (string) $row['DATE_CREATE'];
566 }
567 if (isset($row['DATE_MODIFY']))
568 {
569 $row['DATE_MODIFY'] = (string) $row['DATE_MODIFY'];
570 }
571 $row['MANIFEST'] = unserialize($row['MANIFEST'], ['allowed_classes' => false]);
572 $data[] = $row;
573 }
574 $result->setResult($data);
575
576 return $result;
577 }
578}
static sanitize($value, &$bad=false, $splitter=' ')
Definition manager.php:1255
static getList(array $params=array())
Definition repo.php:533
static bind(array $fields)
Definition repo.php:376
static unbind($code, $handler=null)
Definition repo.php:451
static checkContent($content, $splitter='#SANITIZE#')
Definition repo.php:24
static update($id, $fields=array())
Definition repo.php:37
static getAppByCode($code)
Definition repo.php:293
static add($fields)
Definition repo.php:19
static delete($id)
Definition repo.php:54
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getUpdates($codeList)
Definition client.php:93
static getApp($code, $version=false, $checkHash=false, $installHash=false)
Definition client.php:278