Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
transport.php
1<?php
3
12
13if (!defined('REST_MARKETPLACE_URL'))
14{
15 define('REST_MARKETPLACE_URL', '');
16}
17
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;
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}
static getCurrent()
Definition context.php:241
static isModuleInstalled($moduleName)
call($method, $fields=array())
getServiceUrl(string $type=self::SERVICE_TYPE_APP)