Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
router.php
1<?php
3
4
12
13
14final class Router
15{
16 const LOG_DIR = '/bitrix/modules/sale/lib/exchange/integration/log';
17 const LOG_PATH = 'router_request.log';
18
19 protected $fields;
20
21 public function __construct()
22 {
23 $this->fields = new \Bitrix\Sale\Internals\Fields(
24 $this->requestJsonDecode(
26 ->getRequest()
27 ->toArray()));
28
29 $this->log($this->fields->getValues());
30 }
31
32 public function check()
33 {
34 return $this->checkToken($this->fields->get('AUTH_ID'), $this->fields->get('REFRESH_ID'));
35 }
36
37 protected function checkToken($accessToken, $refreshToken)
38 {
39 $r = new Result();
40
41 $restClient = new Rest\Client\Base([
42 "accessToken" => $accessToken,
43 "refreshToken" => $refreshToken,
44 "endPoint" => Settings::getOAuthRestUrl()
45 ]);
46
47 try
48 {
49 $response = $restClient->call("app.info"); //echo '<pre>';print_r($response);die;
50 }
51 catch (\Exception $exception)
52 {
53 return $r->addError(new Error(Loc::getMessage("SALE_ROUTER_INTERNAL_SERVER_ERROR", ["#ERROR#"=>$exception->getMessage()])));
54 }
55
56 if (isset($response["error"]))
57 {
58
59 return $r->addError(new Error(Loc::getMessage("SALE_ROUTER_INTERNAL_SERVER_ERROR_AUHORIZATION",
60 ['#DESCRIPTION#'=>$response["error_description"], '#ERROR#'=>$response["error"]])
61 ));
62 }
63 else if (!isset($response["result"]))
64 {
65 return $r->addError(new Error(Loc::getMessage("SALE_ROUTER_INTERNAL_SERVER_ERROR_WRONG_RESPONSE")));
66 }
67
68 $token = Token::getToken([], (new App\IntegrationB24())->getCode());
69
70 if(is_null($token))
71 {
72 return $r->addError(new Error(Loc::getMessage("SALE_ROUTER_INTERNAL_SERVER_ERROR_TOKEN_IS_NULL")));
73 }
74
75 if($token->getPortalId() !== $response['result']['install']['member_id'])
76 {
77 return $r->addError(new Error(Loc::getMessage("SALE_ROUTER_INTERNAL_SERVER_ERROR_TOKEN_IS_COMPROMISED")));
78 }
79
80 return $r;
81 }
82
83 public function redirect()
84 {
85 $placementType = $this->getPlacement();
86
87 if(Placement\Type::isDefined(
88 Placement\Type::resolveId($placementType)))
89 {
90 $placement = Placement\Factory::create(Placement\Type::resolveId($placementType), $this->fields->getValues());
91
92 $link = Admin\Factory::create($placement->getModeType());
93
94 if($placement->getModeType() == Admin\ModeType::APP_LAYOUT_TYPE)
95 {
96 $type = $placement->getType();
97 if($type == Placement\Type::DEFAULT_TOOLBAR)
98 {
99 $entityTypeId = $this->resolveTypeId($placement->getEntityTypeId());
100 //TODO: fabric
101 if($entityTypeId == EntityType::ORDER)
102 {
103 $orderId = $this->getBySourceEntity(
105 $placement->getEntityTypeId(), $placement->getEntityId());
106
107 if($orderId>0)
108 {
109 $orderId = Order::load($orderId) ? $orderId
110 :0;
111 }
112
113 $dealId = $orderId>0 ? $this->getByDestinationEntity(
114 EntityType::ORDER, $orderId,
115 CRM\EntityType::DEAL, '')
116 :0;
117
118 if($orderId>0 && $dealId>0)
119 {
120 $link
121 ->setPageByType(Admin\Registry::SALE_ORDER_VIEW)
122 ->setField('entityId', $dealId)
123 ->setField('entityTypeId', CRM\EntityType::DEAL)
124 ->setField('ID', $orderId)
125 ->setFilterParams(false)
126 ->fill()
127 ->redirect();
128 }
129 else
130 {
131 $title = Loc::getMessage("SALE_ROUTER_INTERNAL_ERROR_TITLE");
132
133 if($orderId == 0)
134 {
135 $message = Loc::getMessage("SALE_ROUTER_ORDER_NOT_FOUND", ['#PLACEMENT_ENTITY_ID#'=>$placement->getEntityId()]);
136 $link
137 ->setPage('/bitrix/services/sale/b24integration/500/rest-app-warning.php')
138 ->setField('message', urlencode($message))
139 ->setField('title', $title)
140 ->redirect();
141 }
142
143 $message = Loc::getMessage("SALE_ROUTER_INTERNAL_ERROR");
144 $link
145 ->setPage('/bitrix/services/sale/b24integration/500/rest-app-warning.php')
146 ->setField('message', urlencode($message))
147 ->setField('title', $title)
148 ->redirect();
149 die;
150 }
151 }
152 }
153 elseif($type == Placement\Type::DEAL_DETAIL_TOOLBAR)
154 {
155 if($placement->getTypeHandler() == HandlerType::ORDER_NEW)
156 {
157 $link
158 ->setPageByType(Admin\Registry::SALE_ORDER_CREATE)
159 ->setField('entityId', $placement->getEntityId())
160 ->setField('entityTypeId', $placement->getEntityTypeId())
161 ->setFilterParams(false)
162 ->setField('SITE', SITE_ID)
163 ->setField('HANDLER', HandlerType::ORDER_NEW)
164 ->fill()
165 ->redirect();
166 }
167 elseif ($placement->getTypeHandler() == HandlerType::ORDER_REGISTRY)
168 {
169 $link
170 ->setPageByType(Admin\Registry::SALE_ORDER)
171 ->setField('entityId', $placement->getEntityId())
172 ->setField('entityTypeId', $placement->getEntityTypeId())
173 ->setFilterParams('&set_filter=Y&filter_is_sync_b24=N')
174 ->fill()
175 ->redirect();
176 }
177 }
178 }
179 }
180
181 $link = Admin\Factory::create(Admin\ModeType::APP_LAYOUT_TYPE);
182 $title = Loc::getMessage("SALE_ROUTER_INTERNAL_ERROR_TITLE");
183 $message = Loc::getMessage("SALE_ROUTER_PAGE_NOT_FOUND");
184 $link
185 ->setPage('/bitrix/services/sale/b24integration/500/rest-app-warning.php')
186 ->setField('message', urlencode($message))
187 ->setField('title', $title)
188 ->redirect();
189 }
190
191 protected function getPlacement()
192 {
193 return $this->fields->get('PLACEMENT');
194 }
195
196 protected function resolveTypeId($typeId)
197 {
198 if($typeId == CRM\EntityType::ACTIVITY)
199 {
200 return EntityType::ORDER;
201 }
202 else
203 {
205 }
206 }
207
208 protected function getBySourceEntity($sourceEntityTypeId='', $sourceEntityId='', $destinationEntityTypeId='', $destinationEntityId='')
209 {
210 $relation = Relation\Relation::getByEntity(
211 $sourceEntityTypeId, $sourceEntityId,
212 $destinationEntityTypeId, $destinationEntityId);
213
214 return isset($relation['SRC_ENTITY_ID'])? $relation['SRC_ENTITY_ID']:0;
215 }
216
217 protected function getByDestinationEntity($sourceEntityTypeId='', $sourceEntityId='', $destinationEntityTypeId='', $destinationEntityId='')
218 {
219 $relation = Relation\Relation::getByEntity(
220 $sourceEntityTypeId, $sourceEntityId,
221 $destinationEntityTypeId, $destinationEntityId);
222
223 return isset($relation['DST_ENTITY_ID'])? $relation['DST_ENTITY_ID']:0;
224 }
225
226 protected function requestJsonDecode($request)
227 {
228 $request['PLACEMENT_OPTIONS'] = isset($request['PLACEMENT_OPTIONS'])? json_decode($request['PLACEMENT_OPTIONS'], true):[];
229
230 return $request;
231 }
232
237 protected function log($params)
238 {
239 if($this->isOnLog() == false)
240 {
241 return;
242 }
243
244 $dir = $_SERVER['DOCUMENT_ROOT'].static::LOG_DIR;
245 if(is_dir($dir) || @mkdir($dir, BX_DIR_PERMISSIONS))
246 {
247 $f = fopen($dir.'/'.static::LOG_PATH, "a+");
248 fwrite($f, print_r($params, true));
249 fclose($f);
250 }
251 }
252
253 protected function isOnLog()
254 {
255 return \Bitrix\Main\Config\Option::get("sale", "log_integration_b24_router_request", 'N') == 'Y';
256 }
257}
static getCurrent()
Definition context.php:241
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getByDestinationEntity($sourceEntityTypeId='', $sourceEntityId='', $destinationEntityTypeId='', $destinationEntityId='')
Definition router.php:217
checkToken($accessToken, $refreshToken)
Definition router.php:37
getBySourceEntity($sourceEntityTypeId='', $sourceEntityId='', $destinationEntityTypeId='', $destinationEntityId='')
Definition router.php:208
static getToken(array $fields, $guid=null)
Definition token.php:12