This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SimaLand\API\Rest; |
||
4 | |||
5 | use GuzzleHttp\ClientInterface; |
||
6 | use GuzzleHttp\RequestOptions; |
||
7 | use GuzzleHttp\Psr7\Response; |
||
8 | use SimaLand\API\BaseObject; |
||
9 | |||
10 | /** |
||
11 | * SimaLand клиент. |
||
12 | * |
||
13 | * @link https://www.sima-land.ru/api/v3/help/ |
||
14 | */ |
||
15 | class Client extends BaseObject |
||
16 | { |
||
17 | /** |
||
18 | * Базовый url API sima-land.ru. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | public $baseUrl = 'https://www.sima-land.ru/api/v3'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | public $login; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public $password; |
||
33 | |||
34 | /** |
||
35 | * Путь до токена. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $tokenPath; |
||
40 | |||
41 | /** |
||
42 | * @var \GuzzleHttp\ClientInterface |
||
43 | */ |
||
44 | private $httpClient; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $token; |
||
50 | |||
51 | /** |
||
52 | * Базовые опции http запроса к API |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private $options = [ |
||
57 | 'http_errors' => false, |
||
58 | 'headers' => [ |
||
59 | 'User-Agent' => 'Sima-land api-php-client/3.0', |
||
60 | 'Content-Type' => 'application/json', |
||
61 | ], |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @param array $options |
||
66 | * @throws \Exception |
||
67 | */ |
||
68 | 120 | public function __construct(array $options = []) |
|
69 | { |
||
70 | 120 | if (!isset($options['login'])) { |
|
71 | 3 | throw new \Exception('Login can`t be empty'); |
|
72 | } |
||
73 | 117 | if (!isset($options['password'])) { |
|
74 | 3 | throw new \Exception('Password can`t be empty'); |
|
75 | } |
||
76 | 114 | parent::__construct($options); |
|
77 | 114 | } |
|
78 | |||
79 | /** |
||
80 | * Получить http клиент. |
||
81 | * |
||
82 | * @return \GuzzleHttp\ClientInterface |
||
83 | */ |
||
84 | 72 | public function getHttpClient() |
|
85 | { |
||
86 | 72 | if (is_null($this->httpClient)) { |
|
87 | 6 | $this->httpClient = new \GuzzleHttp\Client(); |
|
88 | 2 | } |
|
89 | 72 | return $this->httpClient; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Установить http клиент. |
||
94 | * |
||
95 | * @param \GuzzleHttp\ClientInterface $httpClient |
||
96 | * @return Client |
||
97 | */ |
||
98 | 102 | public function setHttpClient(ClientInterface $httpClient) |
|
99 | { |
||
100 | 102 | $this->httpClient = $httpClient; |
|
101 | 102 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Групповой запрос к API. |
||
106 | * |
||
107 | * @param array $requests |
||
108 | * @return Response[] |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | 69 | public function batchQuery(array $requests) |
|
112 | { |
||
113 | 69 | $client = $this->getHttpClient(); |
|
114 | 69 | $promises = []; |
|
115 | 69 | foreach ($requests as $name => $request) { |
|
116 | 69 | if (!($request instanceof Request)) { |
|
117 | 3 | throw new \Exception('Request must be implement "\SimaLand\API\Rest\Request"'); |
|
118 | } |
||
119 | 66 | $url = $this->createUrl($request->entity); |
|
120 | 66 | $this->getLogger()->info( |
|
121 | 66 | "Send request {$url}", |
|
122 | [ |
||
123 | 66 | 'getParams' => $request->getParams, |
|
124 | 66 | 'postParams' => $request->postParams |
|
125 | 22 | ] |
|
126 | 22 | ); |
|
127 | 66 | $promises[$name] = $client->requestAsync( |
|
128 | 66 | $request->method, |
|
129 | 66 | $url, |
|
130 | 66 | $this->getOptions($request) |
|
131 | 20 | ); |
|
132 | 20 | } |
|
133 | /** @var \GuzzleHttp\Psr7\Response[] $responses */ |
||
134 | 60 | $responses = \GuzzleHttp\Promise\unwrap($promises); |
|
135 | 57 | foreach ($responses as $key => $response) { |
|
136 | 57 | if ($response->getStatusCode() == 401) { |
|
137 | 3 | $this->deleteToken(); |
|
138 | 39 | return $this->batchQuery($requests); |
|
139 | } |
||
140 | 19 | } |
|
141 | 57 | return $responses; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Запрос к API. |
||
146 | * |
||
147 | * @param string $method |
||
148 | * @param string $entity |
||
149 | * @param array $getParams |
||
150 | * @param array $postParams |
||
151 | * @return Response |
||
152 | * @throws \Exception |
||
153 | */ |
||
154 | 12 | public function query($method, $entity, array $getParams = [], array $postParams = []) |
|
155 | { |
||
156 | 12 | $response = $this->batchQuery([ |
|
157 | 12 | new Request([ |
|
158 | 12 | 'entity' => $entity, |
|
159 | 12 | 'method' => $method, |
|
160 | 12 | 'getParams' => $getParams, |
|
161 | 8 | 'postParams' => $postParams |
|
162 | 4 | ]) |
|
163 | 4 | ]); |
|
164 | 6 | return reset($response); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
165 | } |
||
166 | |||
167 | /** |
||
168 | * GET запрос к API. |
||
169 | * |
||
170 | * @param string $entity |
||
171 | * @param array $getParams |
||
172 | * @return Response |
||
173 | */ |
||
174 | 12 | public function get($entity, array $getParams = []) |
|
175 | { |
||
176 | 12 | return $this->query('GET', $entity, $getParams); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Удалить файл с токеном. |
||
181 | * |
||
182 | * @return Client |
||
183 | * @throws \Exception |
||
184 | */ |
||
185 | 6 | public function deleteToken() |
|
186 | { |
||
187 | 6 | $this->token = null; |
|
188 | 6 | $filename = $this->getTokenFilename(); |
|
189 | 6 | if (file_exists($filename)) { |
|
190 | 6 | unlink($filename); |
|
191 | 2 | } |
|
192 | 6 | return $this; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Получить опции для http клиента. |
||
197 | * |
||
198 | * @param Request|null $request |
||
199 | * @return array |
||
200 | */ |
||
201 | 69 | public function getOptions(Request $request = null) |
|
202 | { |
||
203 | 69 | $options = []; |
|
204 | 69 | if (!is_null($request)) { |
|
205 | 69 | if (!empty($request->getParams)) { |
|
206 | 33 | $options[RequestOptions::QUERY] = $request->getParams; |
|
207 | 11 | } |
|
208 | 69 | if (!empty($request->postParams)) { |
|
209 | 3 | $options[RequestOptions::JSON] = $request->postParams; |
|
210 | 1 | } |
|
211 | 23 | } |
|
212 | 69 | $options = array_merge($this->options, $options); |
|
213 | 69 | $options['headers']['Authorization'] = 'Bearer ' . $this->getToken(); |
|
214 | 63 | return $options; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Аутентификация пользователя. |
||
219 | * |
||
220 | * @throws \Exception |
||
221 | */ |
||
222 | 6 | private function auth() |
|
223 | { |
||
224 | 6 | $client = $this->getHttpClient(); |
|
225 | 6 | $options = $this->options; |
|
226 | 6 | $options['headers']['Authorization'] = 'Basic ' . base64_encode($this->login . ":" . $this->password); |
|
227 | 6 | $response = $client->request('GET', $this->createUrl('auth'), $options); |
|
228 | 6 | if ($response->getStatusCode() != 200) { |
|
229 | 3 | throw new \Exception($response->getReasonPhrase(), $response->getStatusCode()); |
|
230 | } |
||
231 | 3 | $response->getStatusCode(); |
|
232 | 3 | $body = json_decode($response->getBody(), true); |
|
233 | 3 | $this->token = $body['jwt']; |
|
234 | 3 | file_put_contents($this->getTokenFilename(), $body['jwt']); |
|
235 | 3 | } |
|
236 | |||
237 | /** |
||
238 | * Создания url к сущности. |
||
239 | * |
||
240 | * @param string $entity |
||
241 | * @return string |
||
242 | */ |
||
243 | 66 | private function createUrl($entity) |
|
244 | { |
||
245 | 66 | $url = $this->baseUrl; |
|
246 | 66 | $urlLen = strlen($url); |
|
247 | 66 | $entityLen = strlen($entity); |
|
248 | 66 | if ($url[$urlLen - 1] != '/' && $entity[0] != '/') { |
|
249 | 66 | $url .= "/"; |
|
250 | 22 | } |
|
251 | 66 | if ($entity[$entityLen - 1] != '/') { |
|
252 | 66 | $entity .= "/"; |
|
253 | 22 | } |
|
254 | 66 | return $url . $entity; |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * Получить полный путь до токена. |
||
259 | * |
||
260 | * @return string |
||
261 | * @throws \Exception |
||
262 | */ |
||
263 | 72 | private function getTokenFilename() |
|
264 | { |
||
265 | 72 | if (is_null($this->tokenPath)) { |
|
266 | 3 | $this->tokenPath = sys_get_temp_dir(); |
|
267 | 1 | } |
|
268 | 72 | if (!file_exists($this->tokenPath)) { |
|
269 | 3 | throw new \Exception("Path {$this->tokenPath} not found"); |
|
270 | } |
||
271 | 69 | if (substr($this->tokenPath, -1) != '/') { |
|
272 | 69 | $this->tokenPath .= '/'; |
|
273 | 23 | } |
|
274 | 69 | return $this->tokenPath . 'token.txt'; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Получить токен. |
||
279 | * |
||
280 | * @return string |
||
281 | * @throws \Exception |
||
282 | */ |
||
283 | 69 | private function getToken() |
|
284 | { |
||
285 | 69 | if (is_null($this->token)) { |
|
286 | 69 | $filename = $this->getTokenFilename(); |
|
287 | 66 | if (file_exists($filename)) { |
|
288 | 63 | $this->token = file_get_contents($filename); |
|
289 | 21 | } else { |
|
290 | 6 | $this->auth(); |
|
291 | } |
||
292 | 21 | } |
|
293 | 63 | return $this->token; |
|
294 | } |
||
295 | } |
||
296 |