| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * T3Bot. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Frank Nägler <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @link http://www.t3bot.de | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @link http://wiki.typo3.org/T3Bot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace T3Bot\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use T3Bot\Slack\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class WebHookController. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class WebHookController extends AbstractHookController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $colorMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'info' => Message\Attachment::COLOR_INFO, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'ok' => Message\Attachment::COLOR_GOOD, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'warning' => Message\Attachment::COLOR_WARNING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'danger' => Message\Attachment::COLOR_DANGER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'notice' => Message\Attachment::COLOR_NOTICE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * public method to start processing the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Input example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *   "securityToken": "a valid security token", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      *   "color": [info|ok|warning|danger|notice|#HEXCODE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      *   "title": "Title of the message" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      *   "text": "Text of the message" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 9 |  |     public function process($hook, $input = 'php://input') | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 9 |  |         $entityBody = file_get_contents($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 9 |  |         $json = json_decode($entityBody); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 9 |  |         $hookConfiguration = $this->configuration['webhook'][$hook] ?? []; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 9 |  |         if (empty($hookConfiguration) || $hookConfiguration['securityToken'] !== $json->securityToken) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 2 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 7 |  |         $this->sendMessage($json, $hookConfiguration); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param \stdClass $json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param array $hookConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 7 |  |     protected function sendMessage(\stdClass $json, array $hookConfiguration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 7 |  |         $color = $this->colorMap[$json->color] ?? $json->color; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 7 |  |         $message = new Message(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 7 |  |         $message->setText(' '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 7 |  |         $attachment = new Message\Attachment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 7 |  |         $attachment->setColor($color); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 7 |  |         $attachment->setTitle($json->title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 7 |  |         $attachment->setText($json->text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 7 |  |         $attachment->setFallback($json->text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 7 |  |         $message->addAttachment($attachment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 7 |  |         if (is_array($hookConfiguration['channels'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 7 |  |             foreach ($hookConfiguration['channels'] as $channel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 7 |  |                 $this->postToSlack($message, $channel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |