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 | * Request instance. |
||
58 | * |
||
59 | * @var Request |
||
60 | */ |
||
61 | protected $request; |
||
62 | |||
63 | /** |
||
64 | * Encryptor instance. |
||
65 | * |
||
66 | * @var Encryptor |
||
67 | */ |
||
68 | protected $encryptor; |
||
69 | |||
70 | /** |
||
71 | * Message listener. |
||
72 | * |
||
73 | * @var string|callable |
||
74 | */ |
||
75 | protected $messageHandler; |
||
76 | |||
77 | /** |
||
78 | * Message type filter. |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | protected $messageFilter; |
||
83 | |||
84 | /** |
||
85 | * Message type mapping. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $messageTypeMapping = [ |
||
90 | 'text' => 2, |
||
91 | 'image' => 4, |
||
92 | 'voice' => 8, |
||
93 | 'video' => 16, |
||
94 | 'shortvideo' => 32, |
||
95 | 'location' => 64, |
||
96 | 'link' => 128, |
||
97 | 'event' => 1048576, |
||
98 | ]; |
||
99 | |||
100 | /** |
||
101 | * Debug mode. |
||
102 | * |
||
103 | * @var bool |
||
104 | */ |
||
105 | protected $debug = false; |
||
106 | |||
107 | /** |
||
108 | * Constructor. |
||
109 | * |
||
110 | * @param Request $request |
||
111 | */ |
||
112 | 7 | public function __construct(Request $request = null) |
|
116 | |||
117 | /** |
||
118 | * Enable/Disable debug mode. |
||
119 | * |
||
120 | * @param bool $debug |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function debug($debug = true) |
||
130 | |||
131 | /** |
||
132 | * Handle and return response. |
||
133 | * |
||
134 | * @return Response |
||
135 | * |
||
136 | * @throws BadRequestException |
||
137 | */ |
||
138 | 6 | public function serve() |
|
162 | |||
163 | /** |
||
164 | * Validation request params. |
||
165 | * |
||
166 | * @param string $token |
||
167 | * |
||
168 | * @throws FaultException |
||
169 | */ |
||
170 | public function validate($token) |
||
182 | |||
183 | /** |
||
184 | * Add a event listener. |
||
185 | * |
||
186 | * @param callable $callback |
||
187 | * @param int $option |
||
188 | * |
||
189 | * @return Guard |
||
190 | * |
||
191 | * @throws InvalidArgumentException |
||
192 | */ |
||
193 | 6 | public function setMessageHandler($callback = null, $option = self::ALL_MSG) |
|
204 | |||
205 | /** |
||
206 | * Return the message listener. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 1 | public function getMessageHandler() |
|
214 | |||
215 | /** |
||
216 | * Set Encryptor. |
||
217 | * |
||
218 | * @param Encryptor $encryptor |
||
219 | * |
||
220 | * @return Guard |
||
221 | */ |
||
222 | 1 | public function setEncryptor(Encryptor $encryptor) |
|
228 | |||
229 | /** |
||
230 | * Return the encryptor instance. |
||
231 | * |
||
232 | * @return Encryptor |
||
233 | */ |
||
234 | public function getEncryptor() |
||
238 | |||
239 | /** |
||
240 | * Build response. |
||
241 | * |
||
242 | * @param $to |
||
243 | * @param $from |
||
244 | * @param mixed $message |
||
245 | * |
||
246 | * @return string |
||
247 | * |
||
248 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
249 | */ |
||
250 | 6 | protected function buildResponse($to, $from, $message) |
|
281 | |||
282 | /** |
||
283 | * Whether response is message. |
||
284 | * |
||
285 | * @param mixed $message |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 4 | protected function isMessage($message) |
|
303 | |||
304 | /** |
||
305 | * Handle request. |
||
306 | * |
||
307 | * @return array |
||
308 | * |
||
309 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
310 | * @throws \EasyWeChat\Server\BadRequestException |
||
311 | */ |
||
312 | 6 | protected function handleRequest() |
|
328 | |||
329 | /** |
||
330 | * Handle message. |
||
331 | * |
||
332 | * @param array $message |
||
333 | * |
||
334 | * @return mixed |
||
335 | */ |
||
336 | 6 | protected function handleMessage($message) |
|
360 | |||
361 | /** |
||
362 | * Build reply XML. |
||
363 | * |
||
364 | * @param string $to |
||
365 | * @param string $from |
||
366 | * @param AbstractMessage $message |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | 4 | protected function buildReply($to, $from, $message) |
|
383 | |||
384 | /** |
||
385 | * Get signature. |
||
386 | * |
||
387 | * @param array $request |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | protected function signature($request) |
||
397 | |||
398 | /** |
||
399 | * Parse message array from raw php input. |
||
400 | * |
||
401 | * @param string|resource $content |
||
402 | * |
||
403 | * @throws \EasyWeChat\Core\Exceptions\RuntimeException |
||
404 | * @throws \EasyWeChat\Encryption\EncryptionException |
||
405 | * |
||
406 | * @return array |
||
407 | */ |
||
408 | 6 | protected function parseMessageFromRequest($content) |
|
429 | |||
430 | /** |
||
431 | * Check the request message safe mode. |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | 6 | private function isSafeMode() |
|
439 | } |
||
440 |