1 | <?php |
||
39 | class Guard |
||
40 | { |
||
41 | /** |
||
42 | * Empty string. |
||
43 | */ |
||
44 | const SUCCESS_EMPTY_RESPONSE = 'success'; |
||
45 | |||
46 | const TEXT_MSG = 2; |
||
47 | const IMAGE_MSG = 4; |
||
48 | const VOICE_MSG = 8; |
||
49 | const VIDEO_MSG = 16; |
||
50 | const SHORT_VIDEO_MSG = 32; |
||
51 | const LOCATION_MSG = 64; |
||
52 | const LINK_MSG = 128; |
||
53 | const EVENT_MSG = 1048576; |
||
54 | const ALL_MSG = 1048830; |
||
55 | |||
56 | /** |
||
57 | * @var Request |
||
58 | */ |
||
59 | protected $request; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $token; |
||
65 | |||
66 | /** |
||
67 | * @var Encryptor |
||
68 | */ |
||
69 | protected $encryptor; |
||
70 | |||
71 | /** |
||
72 | * @var string|callable |
||
73 | */ |
||
74 | protected $messageHandler; |
||
75 | |||
76 | /** |
||
77 | * @var int |
||
78 | */ |
||
79 | protected $messageFilter; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $messageTypeMapping = [ |
||
85 | 'text' => 2, |
||
86 | 'image' => 4, |
||
87 | 'voice' => 8, |
||
88 | 'video' => 16, |
||
89 | 'shortvideo' => 32, |
||
90 | 'location' => 64, |
||
91 | 'link' => 128, |
||
92 | 'event' => 1048576, |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $debug = false; |
||
99 | |||
100 | /** |
||
101 | * Constructor. |
||
102 | * |
||
103 | * @param string $token |
||
104 | * @param Request $request |
||
105 | */ |
||
106 | 7 | public function __construct($token, Request $request = null) |
|
111 | |||
112 | /** |
||
113 | * Enable/Disable debug mode. |
||
114 | * |
||
115 | * @param bool $debug |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function debug($debug = true) |
||
125 | |||
126 | /** |
||
127 | * Handle and return response. |
||
128 | * |
||
129 | * @return Response |
||
130 | * |
||
131 | * @throws BadRequestException |
||
132 | */ |
||
133 | 6 | public function serve() |
|
159 | |||
160 | /** |
||
161 | * Validation request params. |
||
162 | * |
||
163 | * @param string $token |
||
164 | * |
||
165 | * @throws FaultException |
||
166 | */ |
||
167 | 6 | public function validate($token) |
|
179 | |||
180 | /** |
||
181 | * Add a event listener. |
||
182 | * |
||
183 | * @param callable $callback |
||
184 | * @param int $option |
||
185 | * |
||
186 | * @return Guard |
||
187 | * |
||
188 | * @throws InvalidArgumentException |
||
189 | */ |
||
190 | 6 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
|
201 | |||
202 | /** |
||
203 | * Return the message listener. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 1 | public function getMessageHandler() |
|
211 | |||
212 | /** |
||
213 | * Set Encryptor. |
||
214 | * |
||
215 | * @param Encryptor $encryptor |
||
216 | * |
||
217 | * @return Guard |
||
218 | */ |
||
219 | 1 | public function setEncryptor(Encryptor $encryptor) |
|
225 | |||
226 | /** |
||
227 | * Return the encryptor instance. |
||
228 | * |
||
229 | * @return Encryptor |
||
230 | */ |
||
231 | public function getEncryptor() |
||
235 | |||
236 | /** |
||
237 | * Build response. |
||
238 | * |
||
239 | * @param $to |
||
240 | * @param $from |
||
241 | * @param mixed $message |
||
242 | * |
||
243 | * @return string |
||
244 | * |
||
245 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
246 | */ |
||
247 | 6 | protected function buildResponse($to, $from, $message) |
|
278 | |||
279 | /** |
||
280 | * Whether response is message. |
||
281 | * |
||
282 | * @param mixed $message |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 4 | protected function isMessage($message) |
|
300 | |||
301 | /** |
||
302 | * Handle request. |
||
303 | * |
||
304 | * @return array |
||
305 | * |
||
306 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
307 | * @throws \EasyWeChat\Server\BadRequestException |
||
308 | */ |
||
309 | 6 | protected function handleRequest() |
|
325 | |||
326 | /** |
||
327 | * Handle message. |
||
328 | * |
||
329 | * @param array $message |
||
330 | * |
||
331 | * @return mixed |
||
332 | */ |
||
333 | 6 | protected function handleMessage($message) |
|
357 | |||
358 | /** |
||
359 | * Build reply XML. |
||
360 | * |
||
361 | * @param string $to |
||
362 | * @param string $from |
||
363 | * @param AbstractMessage $message |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | 4 | protected function buildReply($to, $from, $message) |
|
380 | |||
381 | /** |
||
382 | * Get signature. |
||
383 | * |
||
384 | * @param array $request |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | 6 | protected function signature($request) |
|
394 | |||
395 | /** |
||
396 | * Parse message array from raw php input. |
||
397 | * |
||
398 | * @param string|resource $content |
||
399 | * |
||
400 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
401 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
402 | * |
||
403 | * @return array |
||
404 | */ |
||
405 | 6 | protected function parseMessageFromRequest($content) |
|
426 | |||
427 | /** |
||
428 | * Check the request message safe mode. |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | 6 | private function isSafeMode() |
|
436 | } |
||
437 |