| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the lxpgw/logger. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) lichunqiang <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace lxpgw\logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\base\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\base\Object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use yii\helpers\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Pubu.im adapter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * ~~~~ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * $pubu = new Pubu(['remote' => 'http://service.pubu.im/dsad']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * $pubu->send('This is test'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * ~~~ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @version 0.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @author lichunqaing <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class Pubu extends Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string The target remote url send message to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public $remote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |         if (is_null($this->remote)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |             throw new InvalidConfigException('$remote must be set.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param null|string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array       $attachments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |     public function send($text = null, $attachments = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $context = stream_context_create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'http' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |                 'method' => 'POST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |                 'header' => 'Content-Type:application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |                 'content' => Json::encode($this->getPayload($text, $attachments)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         return file_get_contents($this->remote, false, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param array  $attachments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 74 | 2 |  |     private function getPayload($text, $attachments) | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 76 | 2 |  |         if ($text == null) { | 
            
                                                        
            
                                    
            
            
                | 77 | 1 |  |             $text = __CLASS__ . '::' . date('Y-m-d H:i'); | 
            
                                                        
            
                                    
            
            
                | 78 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         $payload = [ | 
            
                                                        
            
                                    
            
            
                | 80 | 2 |  |             'text' => $text, | 
            
                                                        
            
                                    
            
            
                | 81 | 2 |  |             'attachments' => $attachments, | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             //'displayUser' => [ | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |             //    'name' => 'FUckaer', | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |             //    'avatarUrl' => 'https://dn-facecdn.qbox.me/assets/1602030042/img/logo-sm.png' | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |             //] | 
            
                                                        
            
                                    
            
            
                | 86 | 2 |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 88 | 2 |  |         return $payload; | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |