| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Mage Scan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @category  MageScan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package   MageScan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author    Steve Robbins <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright 2015 Steve Robbins | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link      https://github.com/steverobbins/magescan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace MageScan\Command\Scan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use MageScan\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use MageScan\Url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Console\Formatter\OutputFormatterStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Console\Helper\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Abstract scan command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @category  MageScan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @package   MageScan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @author    Steve Robbins <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @copyright 2015 Steve Robbins | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @link      https://github.com/steverobbins/magescan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | abstract class AbstractCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Input object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var \Symfony\Component\Console\Input\InputInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Output object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var \Symfony\Component\Console\Output\OutputInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Cached request object with desired secure flag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @var \MageScan\Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Configure command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             ->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 'url', | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 InputArgument::REQUIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 'The URL of the Magento application' | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                 'insecure', | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 'k', | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                 'Don\'t validate SSL certificate if URL is https' | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 'format', | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 null, | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 InputOption::VALUE_REQUIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 'Specify output format (default, json)', | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 'default' | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Initialize command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param InputInterface  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     protected function initialize(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->input   = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->output  = $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $url = new Url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->request = new Request( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $url->clean($input->getArgument('url')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->input->getOption('insecure') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $style = new OutputFormatterStyle('white', 'blue', ['bold']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->output->getFormatter()->setStyle('header', $style); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Output information in the correct format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string       $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array|string $messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     protected function out($title, $messages = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $format = $this->input->getOption('format'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $method = 'outputFormat' . ucfirst($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (!method_exists($this, $method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 'Format "' . $format . '" is not supported' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->$method($title, $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Output in default format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param string       $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param array|string $messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     protected function outputFormatDefault($title, $messages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->writeHeader($title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if (!is_array($messages)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return $this->output->writeln($messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         foreach ($messages as $message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             switch (isset($message['type']) ? $message['type'] : false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 case 'table': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     $tableHelper = new Table($this->output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                     $tableHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                         ->setHeaders($message['data'][0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                         ->setRows($message['data'][1]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                         ->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     $this->output->writeln(is_array($message) ? $message['data'] : $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * Output in json format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param string       $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param array|string $messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     protected function outputFormatJson($title, $messages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $json = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             'name'     => $title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             'results'  => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             'messages' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         if (!is_array($messages)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $json['messages'][] = strip_tags($messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             foreach ($messages as $message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 switch (isset($message['type']) ? $message['type'] : false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                     case 'table': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                         $headers = $message['data'][0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                         array_map('strtolower', $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                         foreach ($message['data'][1] as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                             foreach ($headers as $key => $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                                 $result[$name] = strip_tags($row[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                             $json['results'][] = $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                     default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                         $json['messages'][] = strip_tags(is_array($message) ? $message['data'] : $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $this->output->write(json_encode($json), false, OutputInterface::OUTPUT_RAW); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * Write a header block | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @param string $style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     protected function writeHeader($text, $style = 'bg=blue;fg=white') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         $this->output->writeln([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             $this->getHelperSet()->get('formatter') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |                 ->formatBlock($text, $style, true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 215 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 216 |  |  |  |