Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
transport.php
1
<?php
2
namespace
Bitrix\Rest\Marketplace
;
3
4
use
Bitrix\Main\ArgumentException
;
5
use
Bitrix\Main\Context
;
6
use
Bitrix\Main\Loader
;
7
use
Bitrix\Main\ModuleManager
;
8
use
Bitrix\Main\Text\Encoding
;
9
use
Bitrix\Main\Config\Option
;
10
use
Bitrix\Main\Web\HttpClient
;
11
use
Bitrix\Main\Web\Json
;
12
13
if
(!defined(
'REST_MARKETPLACE_URL'
))
14
{
15
define(
'REST_MARKETPLACE_URL'
,
''
);
16
}
17
18
class
Transport
19
{
21
const
SERVICE_URL
= REST_MARKETPLACE_URL;
22
protected
const
VERSION
= 1;
23
24
protected
string
$serviceDomain
=
''
;
25
private
const
DEFAULT_SERVICE_REGION =
'en'
;
26
private
const
SERVICE_DOMAIN_LIST = [
27
'en'
=>
'https://util.bitrixsoft.com/'
,
28
'ru'
=>
'https://util.1c-bitrix.ru/'
,
29
'kz'
=>
'https://util.1c-bitrix.kz/'
,
30
'by'
=>
'https://util.1c-bitrix.by/'
,
31
'ua'
=>
'https://util.bitrix.ua/'
,
32
];
33
public
const
SERVICE_TYPE_APP
=
'APP'
;
34
public
const
SERVICE_TYPE_COUPON
=
'COUPON'
;
35
private
const
SERVICE_URN_LIST = [
36
self::SERVICE_TYPE_APP =>
'b24/apps.php'
,
37
self::SERVICE_TYPE_COUPON =>
'b24/b24_coupon.php'
,
38
];
39
40
const
SOCKET_TIMEOUT
= 10;
41
const
STREAM_TIMEOUT
= 10;
42
43
const
METHOD_GET_LAST
=
'get_last'
;
44
const
METHOD_GET_DEV
=
'get_dev'
;
45
const
METHOD_GET_BEST
=
'get_best'
;
46
const
METHOD_GET_SALE_OUT
=
'get_sale_out'
;
47
const
METHOD_GET_BUY
=
'get_buy'
;
48
const
METHOD_GET_UPDATES
=
'get_updates'
;
49
const
METHOD_GET_IMMUNE
=
'get_immune'
;
50
const
METHOD_GET_CATEGORIES
=
'get_categories'
;
51
const
METHOD_GET_CATEGORY
=
'get_category'
;
52
const
METHOD_GET_TAG
=
'get_tag'
;
53
const
METHOD_GET_APP
=
'get_app'
;
54
const
METHOD_GET_APP_PUBLIC
=
'get_app_public'
;
55
const
METHOD_GET_INSTALL
=
'get_app_install'
;
56
const
METHOD_SET_INSTALL
=
'is_installed'
;
57
const
METHOD_SEARCH_APP
=
'search_app'
;
58
const
METHOD_FILTER_APP
=
'search_app_adv'
;
59
const
METHOD_GET_SITE_LIST
=
'sites_list'
;
60
const
METHOD_GET_SITE_ITEM
=
'sites_item'
;
61
const
METHOD_GET_COLLECTIONS
=
'get_collections'
;
62
const
METHOD_GET_FULL_COLLECTION
=
'get_full_collection'
;
63
const
METHOD_GET_SLIDER
=
'get_slider'
;
64
const
METHOD_GET_CATEGORIES_V2
=
'get_categories_v2'
;
65
const
METHOD_MARKET_APP
=
'market_app'
;
66
const
METHOD_TOTAL_APPS
=
'total_apps'
;
67
const
METHOD_GET_REVIEWS
=
'get_reviews'
;
68
const
METHOD_ADD_REVIEW
=
'add_review'
;
69
70
protected
static
$instance
=
null
;
71
77
public
static
function
instance
()
78
{
79
if
(static::$instance ==
null
)
80
{
81
static::$instance =
new
self
();
82
}
83
84
return
static::$instance;
85
}
86
87
88
public
function
__construct
()
89
{
90
if
(Loader::includeModule(
'bitrix24'
))
91
{
92
$region = \CBitrix24::getLicensePrefix();
93
}
94
else
95
{
96
$region = Option::get(
'main'
,
'~PARAM_CLIENT_LANG'
, LANGUAGE_ID);
97
}
98
$this->serviceDomain = self::SERVICE_DOMAIN_LIST[$region] ?? self::SERVICE_DOMAIN_LIST[self::DEFAULT_SERVICE_REGION];
99
}
100
107
public
function
getServiceUrl
(
string
$type = self::SERVICE_TYPE_APP): string
108
{
109
if
($type === self::SERVICE_TYPE_APP && !empty(self::SERVICE_URL))
110
{
111
return
self::SERVICE_URL
;
112
}
113
114
return
self::SERVICE_URN_LIST[$type] ? $this->serviceDomain . self::SERVICE_URN_LIST[$type] :
''
;
115
}
116
117
public
function
call
($method, $fields = array())
118
{
119
$query = $this->
prepareQuery
($method, $fields);
120
121
$httpClient =
new
HttpClient
(array(
122
'socketTimeout'
=> static::SOCKET_TIMEOUT,
123
'streamTimeout'
=> static::STREAM_TIMEOUT,
124
));
125
126
$response = $httpClient->post($this->
getServiceUrl
(), $query);
127
128
return
$this->
prepareAnswer
($response);
129
}
130
131
public
function
batch
($actions)
132
{
133
$query = array();
134
135
foreach
($actions as $key => $batch)
136
{
137
if
(!isset($batch[1]))
138
{
139
$batch[1] = [];
140
}
141
$query[$key] = $this->
prepareQuery
($batch[0], $batch[1]);
142
}
143
144
$query = array(
'batch'
=> $query);
145
146
$httpClient =
new
HttpClient
();
147
$response = $httpClient->post($this->
getServiceUrl
(), $query);
148
149
return
$this->
prepareAnswer
($response);
150
}
151
152
protected
function
prepareQuery
($method, $fields)
153
{
154
if
(!is_array($fields))
155
{
156
$fields = array();
157
}
158
159
$fields[
'action'
] = $method;
160
if
(
Client::isSubscriptionAccess
())
161
{
162
$fields[
'queryVersion'
] = static::VERSION;
163
}
164
$fields[
'lang'
] = LANGUAGE_ID;
165
$fields[
'bsm'
] =
ModuleManager::isModuleInstalled
(
'intranet'
) ?
'0'
:
'1'
;
166
167
if
(Loader::includeModule(
'bitrix24'
) && defined(
'BX24_HOST_NAME'
))
168
{
169
$fields[
'tariff'
] = \CBitrix24::getLicensePrefix();
170
$fields[
'host_name'
] = BX24_HOST_NAME;
171
}
172
else
173
{
174
$request =
Context::getCurrent
()->getRequest();
175
$fields[
'host_name'
] = $request->getHttpHost();
176
@include($_SERVER[
'DOCUMENT_ROOT'
] .
'/bitrix/license_key.php'
);
177
$fields[
'license_key'
] = ($LICENSE_KEY ==
'DEMO'
) ?
'DEMO'
: md5(
'BITRIX'
. $LICENSE_KEY .
'LICENCE'
);
178
}
179
180
return
Encoding::convertEncoding($fields, LANG_CHARSET,
'utf-8'
);
181
}
182
183
protected
function
prepareAnswer
($response)
184
{
185
$responseData =
false
;
186
if
($response && $response <>
''
)
187
{
188
try
189
{
190
$responseData = Json::decode($response);
191
}
192
catch
(
ArgumentException
$e)
193
{
194
$responseData =
false
;
195
}
196
}
197
return
is_array($responseData) ? $responseData :
false
;
198
}
199
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\ModuleManager
Definition
modulemanager.php:5
Bitrix\Main\ModuleManager\isModuleInstalled
static isModuleInstalled($moduleName)
Definition
modulemanager.php:67
Bitrix\Main\Text\Encoding
Definition
encoding.php:8
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Rest\Marketplace\Client\isSubscriptionAccess
static isSubscriptionAccess()
Definition
client.php:626
Bitrix\Rest\Marketplace\Transport
Definition
transport.php:19
Bitrix\Rest\Marketplace\Transport\METHOD_GET_BUY
const METHOD_GET_BUY
Definition
transport.php:47
Bitrix\Rest\Marketplace\Transport\prepareQuery
prepareQuery($method, $fields)
Definition
transport.php:152
Bitrix\Rest\Marketplace\Transport\__construct
__construct()
Definition
transport.php:88
Bitrix\Rest\Marketplace\Transport\instance
static instance()
Definition
transport.php:77
Bitrix\Rest\Marketplace\Transport\METHOD_GET_INSTALL
const METHOD_GET_INSTALL
Definition
transport.php:55
Bitrix\Rest\Marketplace\Transport\METHOD_GET_IMMUNE
const METHOD_GET_IMMUNE
Definition
transport.php:49
Bitrix\Rest\Marketplace\Transport\METHOD_GET_LAST
const METHOD_GET_LAST
Definition
transport.php:43
Bitrix\Rest\Marketplace\Transport\METHOD_GET_DEV
const METHOD_GET_DEV
Definition
transport.php:44
Bitrix\Rest\Marketplace\Transport\METHOD_GET_COLLECTIONS
const METHOD_GET_COLLECTIONS
Definition
transport.php:61
Bitrix\Rest\Marketplace\Transport\METHOD_GET_SLIDER
const METHOD_GET_SLIDER
Definition
transport.php:63
Bitrix\Rest\Marketplace\Transport\METHOD_SEARCH_APP
const METHOD_SEARCH_APP
Definition
transport.php:57
Bitrix\Rest\Marketplace\Transport\METHOD_GET_APP
const METHOD_GET_APP
Definition
transport.php:53
Bitrix\Rest\Marketplace\Transport\METHOD_GET_SITE_LIST
const METHOD_GET_SITE_LIST
Definition
transport.php:59
Bitrix\Rest\Marketplace\Transport\METHOD_GET_FULL_COLLECTION
const METHOD_GET_FULL_COLLECTION
Definition
transport.php:62
Bitrix\Rest\Marketplace\Transport\STREAM_TIMEOUT
const STREAM_TIMEOUT
Definition
transport.php:41
Bitrix\Rest\Marketplace\Transport\METHOD_SET_INSTALL
const METHOD_SET_INSTALL
Definition
transport.php:56
Bitrix\Rest\Marketplace\Transport\prepareAnswer
prepareAnswer($response)
Definition
transport.php:183
Bitrix\Rest\Marketplace\Transport\METHOD_GET_BEST
const METHOD_GET_BEST
Definition
transport.php:45
Bitrix\Rest\Marketplace\Transport\METHOD_GET_SALE_OUT
const METHOD_GET_SALE_OUT
Definition
transport.php:46
Bitrix\Rest\Marketplace\Transport\METHOD_FILTER_APP
const METHOD_FILTER_APP
Definition
transport.php:58
Bitrix\Rest\Marketplace\Transport\METHOD_GET_CATEGORIES_V2
const METHOD_GET_CATEGORIES_V2
Definition
transport.php:64
Bitrix\Rest\Marketplace\Transport\METHOD_GET_CATEGORY
const METHOD_GET_CATEGORY
Definition
transport.php:51
Bitrix\Rest\Marketplace\Transport\METHOD_TOTAL_APPS
const METHOD_TOTAL_APPS
Definition
transport.php:66
Bitrix\Rest\Marketplace\Transport\SERVICE_TYPE_APP
const SERVICE_TYPE_APP
Definition
transport.php:33
Bitrix\Rest\Marketplace\Transport\METHOD_GET_APP_PUBLIC
const METHOD_GET_APP_PUBLIC
Definition
transport.php:54
Bitrix\Rest\Marketplace\Transport\METHOD_GET_UPDATES
const METHOD_GET_UPDATES
Definition
transport.php:48
Bitrix\Rest\Marketplace\Transport\SERVICE_TYPE_COUPON
const SERVICE_TYPE_COUPON
Definition
transport.php:34
Bitrix\Rest\Marketplace\Transport\SOCKET_TIMEOUT
const SOCKET_TIMEOUT
Definition
transport.php:40
Bitrix\Rest\Marketplace\Transport\METHOD_ADD_REVIEW
const METHOD_ADD_REVIEW
Definition
transport.php:68
Bitrix\Rest\Marketplace\Transport\METHOD_GET_SITE_ITEM
const METHOD_GET_SITE_ITEM
Definition
transport.php:60
Bitrix\Rest\Marketplace\Transport\SERVICE_URL
const SERVICE_URL
Definition
transport.php:21
Bitrix\Rest\Marketplace\Transport\METHOD_GET_TAG
const METHOD_GET_TAG
Definition
transport.php:52
Bitrix\Rest\Marketplace\Transport\call
call($method, $fields=array())
Definition
transport.php:117
Bitrix\Rest\Marketplace\Transport\batch
batch($actions)
Definition
transport.php:131
Bitrix\Rest\Marketplace\Transport\METHOD_GET_REVIEWS
const METHOD_GET_REVIEWS
Definition
transport.php:67
Bitrix\Rest\Marketplace\Transport\$serviceDomain
string $serviceDomain
Definition
transport.php:24
Bitrix\Rest\Marketplace\Transport\METHOD_MARKET_APP
const METHOD_MARKET_APP
Definition
transport.php:65
Bitrix\Rest\Marketplace\Transport\getServiceUrl
getServiceUrl(string $type=self::SERVICE_TYPE_APP)
Definition
transport.php:107
Bitrix\Rest\Marketplace\Transport\$instance
static $instance
Definition
transport.php:70
Bitrix\Rest\Marketplace\Transport\METHOD_GET_CATEGORIES
const METHOD_GET_CATEGORIES
Definition
transport.php:50
Bitrix\Rest\Marketplace\Transport\VERSION
const VERSION
Definition
transport.php:22
Bitrix\Main\Context
Definition
culture.php:9
Bitrix\Rest\Marketplace
Definition
application.php:3
modules
rest
lib
marketplace
transport.php
Создано системой
1.10.0