Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
Trial.php
1
<?php
2
3
namespace
Bitrix\Rest\Marketplace
;
4
5
use
Bitrix\Main\Context
;
6
use
Bitrix\Main\Loader
;
7
use
Bitrix\Main\Localization\Loc
;
8
use
Bitrix\Main\SystemException
;
9
use
Bitrix\Main\Web\HttpClient
;
10
use
Bitrix\Rest\OAuthService
;
11
use CRestUtil;
12
13
class
Trial
14
{
15
public
static
function
isAvailable
(): bool
16
{
17
return
Client::isSubscriptionDemoAvailable
() &&
Client::getSubscriptionFinalDate
() ===
null
;
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
{
37
OAuthService::register
();
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
}
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Rest\Marketplace\Client\isSubscriptionDemoAvailable
static isSubscriptionDemoAvailable()
Definition
client.php:659
Bitrix\Rest\Marketplace\Client\getSubscriptionFinalDate
static getSubscriptionFinalDate()
Definition
client.php:570
Bitrix\Rest\Marketplace\Trial
Definition
Trial.php:14
Bitrix\Rest\Marketplace\Trial\isAvailable
static isAvailable()
Definition
Trial.php:15
Bitrix\Rest\Marketplace\Trial\activate
static activate()
Definition
Trial.php:20
Bitrix\Rest\OAuthService
Definition
oauthservice.php:35
Bitrix\Rest\OAuthService\getEngine
static getEngine()
Definition
oauthservice.php:46
Bitrix\Rest\OAuthService\register
static register()
Definition
oauthservice.php:56
Bitrix\Main\Context
Definition
culture.php:9
Bitrix\Rest\Marketplace
Definition
application.php:3
modules
rest
lib
marketplace
Trial.php
Создано системой
1.10.0