36 public static function install($code, $version =
false, $checkHash =
false, $installHash =
false, $from =
null) : array
40 if (!OAuthService::getEngine()->isRegistered())
44 OAuthService::register();
45 OAuthService::getEngine()->getClient()->getApplicationList();
50 'error' => $e->getCode(),
51 'errorDescription' => $e->getMessage(),
56 if (OAuthService::getEngine()->isRegistered())
58 $version = !empty($version) ? $version :
false;
61 'error' =>
'INSTALL_ERROR',
65 $appDetailInfo =
false;
68 if (!empty($checkHash) && !empty($installHash))
70 $appDetailInfo = Client::getInstall($code, $version, $checkHash, $installHash);
74 $appDetailInfo = Client::getInstall($code, $version);
79 $appDetailInfo = $appDetailInfo[
'ITEMS'];
86 !Access::isAvailable($code)
87 || !Access::isAvailableCount(Access::ENTITY_TYPE_APP, $code)
92 'error' =>
'ACTION_ACCESS_DENIED',
94 'helperCode' => Access::getHelperCode(Access::ACTION_INSTALL, Access::ENTITY_TYPE_APP, $appDetailInfo)
97 elseif ($appDetailInfo)
99 if (CRestUtil::canInstallApplication($appDetailInfo, self::$contextUserId))
102 'CLIENT_ID' => $appDetailInfo[
'APP_CODE'],
103 'VERSION' => $appDetailInfo[
'VER'],
104 'BY_SUBSCRIPTION' => $appDetailInfo[
'BY_SUBSCRIPTION'] ??
'N',
107 if (!empty($checkHash) && !empty($installHash))
109 $queryFields[
'CHECK_HASH'] = $checkHash;
110 $queryFields[
'INSTALL_HASH'] = $installHash;
113 $installResult = OAuthService::getEngine()->getClient()->installApplication($queryFields);
114 if (isset($installResult[
'error']) && $installResult[
'error'] ===
'verification_needed')
118 OAuthService::getEngine()->getClient()->getApplicationList();
119 $installResult = OAuthService::getEngine()->getClient()->installApplication($queryFields);
122 defined(
'ADMIN_SECTION')
123 && $appDetailInfo[
'TYPE'] ===
'C'
124 && $appDetailInfo[
'MODE'] ===
'S'
125 && mb_strpos($appDetailInfo[
'CODE'],
'bitrix.') === 0
130 'client_id' => $appDetailInfo[
'APP_CODE'],
131 'version' => (int)$appDetailInfo[
'VER'],
132 'status' => AppTable::STATUS_FREE,
133 'scope' => array_keys($appDetailInfo[
'RIGHTS']),
139 if (isset($installResult[
'error']) && $installResult[
'error'])
141 $result[
'error'] = $installResult[
'error'];
142 $result[
'errorDescription'] = $installResult[
'error_description'];
144 elseif ($installResult[
'result'])
147 'CLIENT_ID' => $installResult[
'result'][
'client_id'],
148 'CODE' => $appDetailInfo[
'CODE'],
149 'ACTIVE' => AppTable::ACTIVE,
150 'INSTALLED' => ($appDetailInfo[
'OPEN_API'] ===
'Y' || empty($appDetailInfo[
'INSTALL_URL']))
151 ? AppTable::INSTALLED
152 : AppTable::NOT_INSTALLED,
153 'URL' => $appDetailInfo[
'URL'],
154 'URL_DEMO' => $appDetailInfo[
'DEMO_URL'],
155 'URL_INSTALL' => $appDetailInfo[
'INSTALL_URL'],
156 'VERSION' => $installResult[
'result'][
'version'],
157 'SCOPE' => implode(
',', $installResult[
'result'][
'scope']),
158 'STATUS' => $installResult[
'result'][
'status'],
159 'SHARED_KEY' => $appDetailInfo[
'SHARED_KEY'],
160 'CLIENT_SECRET' =>
'',
161 'APP_NAME' => $appDetailInfo[
'NAME'],
162 'MOBILE' => $appDetailInfo[
'BXMOBILE'] ===
'Y' ? AppTable::ACTIVE : AppTable::INACTIVE,
163 'USER_INSTALL' => CRestUtil::appCanBeInstalledByUser($appDetailInfo) ? AppTable::ACTIVE : AppTable::INACTIVE,
167 $appFields[
'STATUS'] === AppTable::STATUS_TRIAL
168 || $appFields[
'STATUS'] === AppTable::STATUS_PAID
175 $appFields[
'DATE_FINISH'] =
'';
180 $appDetailInfo[
'TYPE'] === AppTable::TYPE_CONFIGURATION
181 && $appDetailInfo[
'MODE'] !== AppTable::MODE_SITE
184 $appFields[
'INSTALLED'] = AppTable::NOT_INSTALLED;
187 $existingApp = AppTable::getByClientId($appFields[
'CLIENT_ID']);
190 $addResult = AppTable::update($existingApp[
'ID'], $appFields);
194 $addResult = AppTable::add($appFields);
197 if ($addResult->isSuccess())
199 $appId = $addResult->getId();
203 AppLogTable::log($appId, AppLogTable::ACTION_TYPE_UPDATE);
207 AppLogTable::log($appId, AppLogTable::ACTION_TYPE_ADD);
210 if ($appFields[
'INSTALLED'] === AppTable::INSTALLED)
212 AppLogTable::log($appId, AppLogTable::ACTION_TYPE_INSTALL);
215 if (!CRestUtil::isAdmin(self::$contextUserId))
217 CRestUtil::notifyInstall($appFields);
220 if (isset($appDetailInfo[
'MENU_TITLE']) && is_array($appDetailInfo[
'MENU_TITLE']))
222 foreach ($appDetailInfo[
'MENU_TITLE'] as $lang => $langName)
224 $appLangFields = array(
226 'LANGUAGE_ID' => $lang,
227 'MENU_NAME' => $langName
230 $appLangUpdateFields = array(
231 'MENU_NAME' => $langName
234 $connection = Main\Application::getConnection();
235 $queries = $connection->getSqlHelper()->prepareMerge(
236 AppLangTable::getTableName(),
245 foreach($queries as $query)
247 $connection->queryExecute($query);
252 if ($appDetailInfo[
'OPEN_API'] ===
'Y' && !empty($appFields[
'URL_INSTALL']))
255 $result = EventTable::add(
258 'EVENT_NAME' =>
'ONAPPINSTALL',
259 'EVENT_HANDLER' => $appFields[
'URL_INSTALL'],
262 if ($result->isSuccess())
264 Sender::bind(
'rest',
'OnRestAppInstall');
268 AppTable::install($appId);
273 if ($appDetailInfo[
'TYPE'] !== AppTable::TYPE_CONFIGURATION)
275 $uriString = CRestUtil::getApplicationPage($appId);
276 $uri =
new Uri($uriString);
277 $ver = (int) $version;
281 'check_hash' => $checkHash,
282 'install_hash' => $installHash
285 $redirect = $uri->getUri();
286 $open = $appDetailInfo[
'OPEN_API'] !==
'Y';
290 if ((
int)$appDetailInfo[
'IMPORT_ZIP_ID'] > 0)
292 $url = Url::getConfigurationImportZipUrl((
int)$appDetailInfo[
'IMPORT_ZIP_ID']);
296 $url = Url::getConfigurationImportAppUrl($appDetailInfo[
'CODE']);
299 $uri =
new Uri($url);
300 if (!empty($checkHash) && !empty($installHash))
304 'check_hash' => $checkHash,
305 'install_hash' => $installHash,
309 $sliderUrl = $uri->getUri();
316 'installed' => $appFields[
'INSTALLED'] ===
'Y',
317 'redirect' => $redirect,
318 'openSlider' => $sliderUrl,
329 $result[
'errorDescription'] = implode(
'<br />', $addResult->getErrorMessages());
334 !empty($appInfo[
'HOLD_INSTALL_BY_TRIAL'])
335 && $appInfo[
'HOLD_INSTALL_BY_TRIAL'] ===
'Y'
336 && Client::isSubscriptionDemo()
344 'error' =>
'ACCESS_DENIED',
352 'error' =>
'APPLICATION_NOT_FOUND',
357 elseif (!$result[
'error'])
360 'error' =>
'OAUTH_REGISTER',
365 if (isset($result[
'error']) && $result[
'error'])
367 if ($result[
'error'] ===
'SUBSCRIPTION_REQUIRED')
369 $result[
'errorDescription'] =
Loc::getMessage(
'RMP_ERROR_SUBSCRIPTION_REQUIRED');
371 elseif ($result[
'error'] ===
'verification_needed')
373 $result[
'errorDescription'] =
Loc::getMessage(
'RMP_ERROR_VERIFICATION_NEEDED');
380 public static function uninstall($code,
bool $clean =
false, $from =
null) : array
382 if (CRestUtil::isAdmin(self::$contextUserId))
384 $res = AppTable::getList(
388 '!=STATUS' => AppTable::STATUS_LOCAL,
393 $appInfo = $res->fetch();
396 $checkResult = AppTable::checkUninstallAvailability($appInfo[
'ID'], $clean);
398 $checkResult->isEmpty()
399 && AppTable::canUninstallByType($appInfo[
'CODE'], $appInfo[
'VERSION'])
402 AppTable::uninstall($appInfo[
'ID'], $clean);
409 AppTable::update($appInfo[
'ID'], $appFields);
411 AppLogTable::log($appInfo[
'ID'], AppLogTable::ACTION_TYPE_UNINSTALL);
419 $result = [
'success' => 1];
424 foreach ($checkResult as $error)
426 $errorMessage .= $error->getMessage() .
"\n";
429 $result = [
'error' => $errorMessage];
431 $checkResult->isEmpty()
432 && AppTable::getAppType($appInfo[
'CODE']) == AppTable::TYPE_CONFIGURATION
436 'sliderUrl' => \Bitrix\Rest\Marketplace\Url::getConfigurationImportRollbackUrl(
459 if (CRestUtil::isAdmin(self::$contextUserId))
461 $appInfo = AppTable::getByClientId($id);
463 !Access::isAvailable($id)
464 || !Access::isAvailableCount(Access::ENTITY_TYPE_APP, $id)
469 'helperCode' => Access::getHelperCode(Access::ACTION_INSTALL, Access::ENTITY_TYPE_APP, $appInfo)
472 elseif ($appInfo && $appInfo[
'STATUS'] === AppTable::STATUS_LOCAL)
474 if (empty($appInfo[
'MENU_NAME']) && empty($appInfo[
'MENU_NAME_DEFAULT']))
476 AppTable::install($appInfo[
'ID']);
477 $result = [
'success' => 1];
479 elseif (!empty($appInfo[
'URL_INSTALL']))
485 AppTable::update($appInfo[
'ID'], $appFields);
489 'redirect' => CRestUtil::getApplicationPage($appInfo[
'ID']),