Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Trial.php
1<?php
2
4
11use CRestUtil;
12
13class Trial
14{
15 public static function isAvailable(): bool
16 {
18 }
19
20 public static function activate(): array
21 {
22 if (!self::isAvailable())
23 {
24 return self::getError(Loc::getMessage('REST_MARKET_ACTIVATE_DEMO_NOT_AVAILABLE'));
25 }
26
27 if (!CRestUtil::isAdmin())
28 {
29 return self::getError(Loc::getMessage('REST_MARKET_ACTIVATE_DEMO_ACCESS_DENIED'));
30 }
31
32
33 if (!OAuthService::getEngine()->isRegistered())
34 {
35 try
36 {
38 }
39 catch (SystemException $e)
40 {
41 return self::getError(
42 Loc::getMessage('REST_MARKET_CONFIG_ACTIVATE_ERROR'),
43 $e->getMessage(),
44 $e->getCode()
45 );
46 }
47 }
48
49 try
50 {
51 OAuthService::getEngine()->getClient()->getApplicationList();
52 }
53 catch (SystemException $e)
54 {
55 return self::getError(
56 Loc::getMessage('REST_MARKET_CONFIG_ACTIVATE_ERROR'),
57 $e->getMessage(),
58 4
59 );
60 }
61
62
63 if (!OAuthService::getEngine()->isRegistered())
64 {
65 return self::getError(
66 Loc::getMessage('REST_MARKET_CONFIG_ACTIVATE_ERROR'),
67 '',
68 1
69 );
70 }
71
72 $loadedBitrix24 = Loader::includeModule('bitrix24');
73 $queryFields = $loadedBitrix24 ? self::getB24Fields() : self::getCPFields();
74
75 if (empty($queryFields))
76 {
77 return [];
78 }
79
80 $httpClient = new HttpClient();
81 $response = $httpClient->post('https://www.1c-bitrix.ru/buy_tmp/b24_coupon.php', $queryFields);
82 if (!$response)
83 {
84 return [];
85 }
86
87 $result = [
88 'result' => true,
89 ];
90 if (mb_strpos($response, 'OK') === false)
91 {
92 $result = self::getError(
93 Loc::getMessage('REST_MARKET_CONFIG_ACTIVATE_ERROR'),
94 '',
95 2
96 );
97 }
98
99 if (!$loadedBitrix24)
100 {
101 require_once($_SERVER['DOCUMENT_ROOT']
102 . '/bitrix/modules/main/classes/general/update_client.php');
103 $errorMessage = '';
104 \CUpdateClient::GetUpdatesList($errorMessage, LANG);
105 }
106
107 return $result;
108 }
109
110 private static function getB24Fields(): array
111 {
112 $server = Context::getCurrent()->getServer();
113
114 $queryFields = [
115 'DEMO' => 'subscription',
116 'SITE' => (defined('BX24_HOST_NAME')) ? BX24_HOST_NAME : $server->getHttpHost(),
117 ];
118
119 if (function_exists('bx_sign'))
120 {
121 $queryFields['hash'] = bx_sign(md5(implode('|', $queryFields)));
122 }
123
124 return $queryFields;
125 }
126
127 private static function getCPFields(): array
128 {
129 $queryFields = [];
130
131 $LicenseKeyHash = \Bitrix\Main\Application::getInstance()->getLicense()->getHashLicenseKey();
132
133 if (!is_null($LicenseKeyHash))
134 {
135 $queryFields = [
136 'DEMO' => 'subscription',
137 'SITE' => 'cp',
138 'key' => $LicenseKeyHash,
139 'hash' => md5('cp' . '|' . 'subscription' . '|' . $LicenseKeyHash),
140 ];
141 }
142
143 return $queryFields;
144 }
145
146 private static function getError(string $message, string $description = '', $code = null): array
147 {
148 $error = ['error' => $message];
149
150 if ($description !== '')
151 {
152 $error['error_description'] = $description;
153 }
154
155 if (!is_null($code))
156 {
157 $error['error_code'] = $code;
158 }
159
160 return $error;
161 }
162}
static getCurrent()
Definition context.php:241
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29