1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
application.php
См. документацию.
1<?php
2
4
19use CRestUtil;
20
21Loc::loadMessages(__FILE__);
22
24{
29 protected static ?int $contextUserId = null;
30
31 public static function setContextUserId(int $id): void
32 {
33 self::$contextUserId = $id;
34 }
35
36 public static function install($code, $version = false, $checkHash = false, $installHash = false, $from = null) : array
37 {
38 $result = [];
39
40 if (!OAuthService::getEngine()->isRegistered())
41 {
42 try
43 {
45 OAuthService::getEngine()->getClient()->getApplicationList();
46 }
47 catch(SystemException $e)
48 {
49 $result = [
50 'error' => $e->getCode(),
51 'errorDescription' => $e->getMessage(),
52 ];
53 }
54 }
55
56 if (OAuthService::getEngine()->isRegistered())
57 {
58 $version = !empty($version) ? $version : false;
59
60 $result = [
61 'error' => 'INSTALL_ERROR',
62 'errorDescription' => Loc::getMessage('RMP_INSTALL_ERROR'),
63 ];
64
65 $appDetailInfo = false;
66 if ($code <> '')
67 {
68 if (!empty($checkHash) && !empty($installHash))
69 {
70 $appDetailInfo = Client::getInstall($code, $version, $checkHash, $installHash);
71 }
72 else
73 {
74 $appDetailInfo = Client::getInstall($code, $version);
75 }
76
77 if ($appDetailInfo)
78 {
79 $appDetailInfo = $appDetailInfo['ITEMS'];
80 }
81 }
82
83 if (
84 $appDetailInfo
85 && !Access::canInstallApp($appDetailInfo)
86 )
87 {
88 $result = [
89 'error' => 'ACTION_ACCESS_DENIED',
90 'errorDescription' => Loc::getMessage('RMP_ERROR_ACCESS_DENIED'),
91 'helperCode' => Access::getHelperCode(Access::ACTION_INSTALL, Access::ENTITY_TYPE_APP, $appDetailInfo)
92 ];
93 }
94 elseif ($appDetailInfo)
95 {
96 if (CRestUtil::canInstallApplication($appDetailInfo, self::$contextUserId))
97 {
98 $queryFields = [
99 'CLIENT_ID' => $appDetailInfo['APP_CODE'],
100 'VERSION' => $appDetailInfo['VER'],
101 'BY_SUBSCRIPTION' => $appDetailInfo['BY_SUBSCRIPTION'] ?? 'N',
102 ];
103
104 if (!empty($checkHash) && !empty($installHash))
105 {
106 $queryFields['CHECK_HASH'] = $checkHash;
107 $queryFields['INSTALL_HASH'] = $installHash;
108 }
109
110 $installResult = OAuthService::getEngine()->getClient()->installApplication($queryFields);
111 if (isset($installResult['error']) && $installResult['error'] === 'verification_needed')
112 {
114 {
115 OAuthService::getEngine()->getClient()->getApplicationList();
116 $installResult = OAuthService::getEngine()->getClient()->installApplication($queryFields);
117 }
118 elseif (
119 defined('ADMIN_SECTION')
120 && $appDetailInfo['TYPE'] === 'C'
121 && $appDetailInfo['MODE'] === 'S'
122 && mb_strpos($appDetailInfo['CODE'], 'bitrix.') === 0
123 )
124 {
125 $installResult = [
126 'result' => [
127 'client_id' => $appDetailInfo['APP_CODE'],
128 'version' => (int)$appDetailInfo['VER'],
129 'status' => AppTable::STATUS_FREE,
130 'scope' => array_keys($appDetailInfo['RIGHTS']),
131 ],
132 ];
133 }
134 }
135
136 if (isset($installResult['error']) && $installResult['error'])
137 {
138 $result['error'] = $installResult['error'];
139 $result['errorDescription'] = $installResult['error_description'];
140 }
141 elseif ($installResult['result'])
142 {
143 $appFields = [
144 'CLIENT_ID' => $installResult['result']['client_id'],
145 'CODE' => $appDetailInfo['CODE'],
146 'ACTIVE' => AppTable::ACTIVE,
147 'INSTALLED' => ($appDetailInfo['OPEN_API'] === 'Y' || empty($appDetailInfo['INSTALL_URL']))
150 'URL' => $appDetailInfo['URL'],
151 'URL_DEMO' => $appDetailInfo['DEMO_URL'],
152 'URL_INSTALL' => $appDetailInfo['INSTALL_URL'],
153 'URL_SETTINGS' => $appDetailInfo['SETTINGS_URL'],
154 'VERSION' => $installResult['result']['version'],
155 'SCOPE' => implode(',', $installResult['result']['scope']),
156 'STATUS' => $installResult['result']['status'],
157 'SHARED_KEY' => $appDetailInfo['SHARED_KEY'],
158 'CLIENT_SECRET' => '',
159 'APP_NAME' => $appDetailInfo['NAME'],
160 'MOBILE' => $appDetailInfo['BXMOBILE'] === 'Y' ? AppTable::ACTIVE : AppTable::INACTIVE,
161 'USER_INSTALL' => CRestUtil::appCanBeInstalledByUser($appDetailInfo) ? AppTable::ACTIVE : AppTable::INACTIVE,
162 ];
163
164 if (
165 $appFields['STATUS'] === AppTable::STATUS_TRIAL
166 || $appFields['STATUS'] === AppTable::STATUS_PAID
167 )
168 {
169 $appFields['DATE_FINISH'] = DateTime::createFromTimestamp($installResult['result']['date_finish']);
170 }
171 else
172 {
173 $appFields['DATE_FINISH'] = '';
174 }
175
176 //Configuration app
177 if (
178 (
179 $appDetailInfo['TYPE'] === AppTable::TYPE_CONFIGURATION
180 && $appDetailInfo['MODE'] !== AppTable::MODE_SITE
181 )
182 || $appDetailInfo['TYPE'] === AppTable::TYPE_BIC_DASHBOARD
183 )
184 {
185 $appFields['INSTALLED'] = AppTable::NOT_INSTALLED;
186 }
187
188 $existingApp = AppTable::getByClientId($appFields['CLIENT_ID']);
189 if ($existingApp)
190 {
191 $addResult = AppTable::update($existingApp['ID'], $appFields);
192 }
193 else
194 {
195 $addResult = AppTable::add($appFields);
196 }
197
198 if ($addResult->isSuccess())
199 {
200 $appId = $addResult->getId();
201
202 if ($existingApp)
203 {
205 }
206 else
207 {
209 }
210
211 if ($appFields['INSTALLED'] === AppTable::INSTALLED)
212 {
214 }
215
216 if (!CRestUtil::isAdmin(self::$contextUserId))
217 {
218 CRestUtil::notifyInstall($appFields);
219 }
220
221 if (isset($appDetailInfo['MENU_TITLE']) && is_array($appDetailInfo['MENU_TITLE']))
222 {
223 foreach ($appDetailInfo['MENU_TITLE'] as $lang => $langName)
224 {
225 $appLangFields = array(
226 'APP_ID' => $appId,
227 'LANGUAGE_ID' => $lang,
228 'MENU_NAME' => $langName
229 );
230
231 $appLangUpdateFields = array(
232 'MENU_NAME' => $langName
233 );
234
236 $queries = $connection->getSqlHelper()->prepareMerge(
238 [
239 'APP_ID',
240 'LANGUAGE_ID'
241 ],
242 $appLangFields,
243 $appLangUpdateFields
244 );
245
246 foreach($queries as $query)
247 {
248 $connection->queryExecute($query);
249 }
250 }
251 }
252
253 if ($appDetailInfo['OPEN_API'] === 'Y' && !empty($appFields['URL_INSTALL']))
254 {
255 // checkCallback is already called inside checkFields
256 $result = EventTable::add(
257 [
258 'APP_ID' => $appId,
259 'EVENT_NAME' => 'ONAPPINSTALL',
260 'EVENT_HANDLER' => $appFields['URL_INSTALL'],
261 ]
262 );
263 if ($result->isSuccess())
264 {
265 Sender::bind('rest', 'OnRestAppInstall');
266 }
267 }
268
269 AppTable::install($appId);
270
271 $redirect = false;
272 $open = false;
273 $sliderUrl = false;
274 if (
275 $appDetailInfo['TYPE'] !== AppTable::TYPE_CONFIGURATION
276 && $appDetailInfo['TYPE'] !== AppTable::TYPE_BIC_DASHBOARD
277 )
278 {
279 $uriString = CRestUtil::getApplicationPage($appId);
280 $uri = new Uri($uriString);
281 $ver = (int) $version;
282 $uri->addParams(
283 [
284 'ver' => $ver,
285 'check_hash' => $checkHash,
286 'install_hash' => $installHash
287 ]
288 );
289 $redirect = $uri->getUri();
290 $open = $appDetailInfo['OPEN_API'] !== 'Y';
291 }
292 else
293 {
294 if ((int)$appDetailInfo['IMPORT_ZIP_ID'] > 0)
295 {
296 $url = Url::getConfigurationImportZipUrl((int)$appDetailInfo['IMPORT_ZIP_ID']);
297 }
298 else
299 {
300 $url = Url::getConfigurationImportAppUrl($appDetailInfo['CODE']);
301 }
302
303 $uri = new Uri($url);
304 if (!empty($checkHash) && !empty($installHash))
305 {
306 $uri->addParams(
307 [
308 'check_hash' => $checkHash,
309 'install_hash' => $installHash,
310 ]
311 );
312 }
313 $sliderUrl = $uri->getUri();
314 }
315
316 $result = [
317 'success' => 1,
318 'id' => $appId,
319 'open' => $open,
320 'installed' => AppTable::isInstalled($appId),
321 'redirect' => $redirect,
322 'openSlider' => $sliderUrl,
323 'canShowForm' => $appDetailInfo['OPEN_API'] === 'Y' && !empty($appFields['URL_SETTINGS'])
324 ];
325
327 'finishInstall',
328 $code,
329 $from ?? 'index'
330 );
331 }
332 else
333 {
334 $result['errorDescription'] = implode('<br />', $addResult->getErrorMessages());
335 }
336 }
337 }
338 elseif (
339 !empty($appInfo['HOLD_INSTALL_BY_TRIAL'])
340 && $appInfo['HOLD_INSTALL_BY_TRIAL'] === 'Y'
342 )
343 {
344 $result = ['error' => Loc::getMessage('RMP_TRIAL_HOLD_INSTALL')];
345 }
346 else
347 {
348 $result = [
349 'error' => 'ACCESS_DENIED',
350 'errorDescription' => Loc::getMessage('RMP_ACCESS_DENIED'),
351 ];
352 }
353 }
354 else
355 {
356 $result = [
357 'error' => 'APPLICATION_NOT_FOUND',
358 'errorDescription' => Loc::getMessage('RMP_NOT_FOUND'),
359 ];
360 }
361 }
362 elseif (!$result['error'])
363 {
364 $result = [
365 'error' => 'OAUTH_REGISTER',
366 'errorDescription' => Loc::getMessage('RMP_INSTALL_ERROR'),
367 ];
368 }
369
370 if (isset($result['error']) && $result['error'])
371 {
372 if ($result['error'] === 'SUBSCRIPTION_REQUIRED')
373 {
374 $result['errorDescription'] = Loc::getMessage('RMP_ERROR_SUBSCRIPTION_REQUIRED');
375 }
376 elseif ($result['error'] === 'verification_needed')
377 {
378 $result['errorDescription'] = Loc::getMessage('RMP_ERROR_VERIFICATION_NEEDED');
379 }
380 }
381
382 return $result;
383 }
384
385 public static function uninstall($code, bool $clean = false, $from = null) : array
386 {
387 if (CRestUtil::isAdmin(self::$contextUserId))
388 {
389 $res = AppTable::getList(
390 [
391 'filter' => [
392 '=CODE' => $code,
393 '!=STATUS' => AppTable::STATUS_LOCAL,
394 ],
395 ]
396 );
397
398 $appInfo = $res->fetch();
399 if ($appInfo)
400 {
401 $checkResult = AppTable::checkUninstallAvailability($appInfo['ID'], $clean);
402 if (
403 $checkResult->isEmpty()
404 && AppTable::canUninstallByType($appInfo['CODE'], $appInfo['VERSION'])
405 )
406 {
407 AppTable::uninstall($appInfo['ID'], $clean);
408
409 $appFields = [
410 'ACTIVE' => 'N',
411 'INSTALLED' => 'N',
412 ];
413
414 AppTable::update($appInfo['ID'], $appFields);
415
417
419 'finishUninstall',
420 $appInfo['CODE'],
421 $from ?? 'index'
422 );
423
424 $result = ['success' => 1];
425 }
426 else
427 {
428 $errorMessage = [];
429 foreach ($checkResult as $error)
430 {
431 $errorMessage[] = $error->getMessage();
432 }
433
434 $result = ['error' => implode(PHP_EOL, $errorMessage)];
435 $appType = AppTable::getAppType($appInfo['CODE']);
436 if (
437 $checkResult->isEmpty()
438 && ($appType === AppTable::TYPE_CONFIGURATION || $appType === AppTable::TYPE_BIC_DASHBOARD)
439 )
440 {
441 $result = [
443 $appInfo['CODE']
444 ),
445 ];
446 }
447 }
448 }
449 else
450 {
451 $result = ['error' => Loc::getMessage('RMP_NOT_FOUND')];
452 }
453 }
454 else
455 {
456 $result = ['error' => Loc::getMessage('RMP_ACCESS_DENIED')];
457 }
458
459 return $result;
460 }
461
462 public static function reinstall($id) : array
463 {
464 $result = [];
465 if (CRestUtil::isAdmin(self::$contextUserId))
466 {
467 $appInfo = AppTable::getByClientId($id);
468 if (
469 !Access::isAvailable($id)
470 || !Access::isAvailableCount(Access::ENTITY_TYPE_APP, $id)
471 )
472 {
473 $result = [
474 'error' => Loc::getMessage('RMP_ERROR_ACCESS_DENIED'),
475 'helperCode' => Access::getHelperCode(Access::ACTION_INSTALL, Access::ENTITY_TYPE_APP, $appInfo)
476 ];
477 }
478 elseif ($appInfo && $appInfo['STATUS'] === AppTable::STATUS_LOCAL)
479 {
480 // delete user application params to trigger installation event
481 \CUserOptions::DeleteOption('app_options', 'params_' . $appInfo['CLIENT_ID'] . '_' . $appInfo['VERSION']);
482
483 if (empty($appInfo['MENU_NAME']) && empty($appInfo['MENU_NAME_DEFAULT']))
484 {
485 AppTable::install($appInfo['ID']);
486 $result = ['success' => 1];
487 }
488 elseif (!empty($appInfo['URL_INSTALL']))
489 {
490 $appFields = [
491 'INSTALLED' => 'N',
492 ];
493
494 AppTable::update($appInfo['ID'], $appFields);
495
496 $result = [
497 'success' => 1,
498 'redirect' => CRestUtil::getApplicationPage($appInfo['ID']),
499 ];
500 }
501 }
502 else
503 {
504 $result = ['error' => Loc::getMessage('RMP_NOT_FOUND')];
505 }
506 }
507 else
508 {
509 $result = ['error' => Loc::getMessage('RMP_ACCESS_DENIED')];
510 }
511
512 return $result;
513 }
514
515 public static function setRights($appId, $rights) : array
516 {
517 $result = [];
518 // todo: maybe can add self::$contextUser to isAdmin check
519 if (CRestUtil::isAdmin())
520 {
521 if ($appId > 0)
522 {
523 $appInfo = AppTable::getByClientId($appId);
524 if ($appInfo['CODE'])
525 {
527 'setAppRight',
528 $appInfo['CODE'],
529 $appInfo['CODE']
530 );
531 }
534 $result = ['success' => 1];
535 }
536 }
537 else
538 {
539 $result = ['error' => Loc::getMessage('RMP_ACCESS_DENIED')];
540 }
541
542 return $result;
543 }
544
545 public static function getRights($appId)
546 {
547 // todo: maybe can add self::$contextUser to isAdmin check
548 if (CRestUtil::isAdmin())
549 {
550 if ($appId > 0)
551 {
553 }
554 else
555 {
556 $result = 0;
557 }
558 }
559 else
560 {
561 $result = ['error' => Loc::getMessage('RMP_ACCESS_DENIED')];
562 }
563
564 return $result;
565 }
566}
$connection
Определения actionsdefinitions.php:38
static getConnection($name="")
Определения application.php:638
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
Определения uri.php:17
static logToFile($action, $tag='', $label='', $actionType='')
Определения analytic.php:20
static getTableName()
Определения applang.php:40
const ACTION_TYPE_UNINSTALL
Определения applog.php:40
const ACTION_TYPE_UPDATE
Определения applog.php:39
const ACTION_TYPE_ADD
Определения applog.php:37
static log($appId, $action)
Определения applog.php:90
const ACTION_TYPE_INSTALL
Определения applog.php:38
Определения app.php:68
static isInstalled(int $appId)
Определения app.php:486
const STATUS_PAID
Определения app.php:86
static canUninstallByType($code, $version=false)
Определения app.php:1271
static getAppType($code, $version=false)
Определения app.php:1293
static install($appId)
Определения app.php:497
const INSTALLED
Определения app.php:71
static getByClientId($clientId)
Определения app.php:967
const TYPE_CONFIGURATION
Определения app.php:78
const TYPE_BIC_DASHBOARD
Определения app.php:80
const STATUS_TRIAL
Определения app.php:88
const STATUS_LOCAL
Определения app.php:84
static uninstall($appId, $clean=0)
Определения app.php:551
const ACTIVE
Определения app.php:69
static getAccess($appId)
Определения app.php:896
const MODE_SITE
Определения app.php:82
const NOT_INSTALLED
Определения app.php:72
const STATUS_FREE
Определения app.php:85
static setAccess($appId, $newRights=array())
Определения app.php:929
const INACTIVE
Определения app.php:70
Определения event.php:38
static install($code, $version=false, $checkHash=false, $installHash=false, $from=null)
Определения application.php:36
static setRights($appId, $rights)
Определения application.php:515
static reinstall($id)
Определения application.php:462
static setContextUserId(int $id)
Определения application.php:31
static getRights($appId)
Определения application.php:545
static uninstall($code, bool $clean=false, $from=null)
Определения application.php:385
static int $contextUserId
Определения application.php:29
static isSubscriptionDemo()
Определения client.php:620
static getInstall($code, $version=false, $checkHash=false, $installHash=false)
Определения client.php:425
static getConfigurationImportAppUrl($code=null)
Определения url.php:537
static getConfigurationImportRollbackUrl($appCode)
Определения url.php:542
static getConfigurationImportZipUrl($zipId, $from='')
Определения url.php:547
static getEngine()
Определения oauthservice.php:49
static register()
Определения oauthservice.php:59
static clearHandlerCache()
Определения placement.php:380
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
if(!defined('SITE_ID')) $lang
Определения include.php:91
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$error
Определения subscription_card_product.php:20
$rights
Определения options.php:4
$url
Определения iframe.php:7