1 | <?php |
||
14 | class SensiolabHelper |
||
15 | { |
||
16 | |||
17 | protected $guzzle; |
||
18 | |||
19 | protected $command; |
||
20 | |||
21 | protected $tableVulnerabilities = []; |
||
22 | |||
23 | /** |
||
24 | * SensiolabHelper constructor. |
||
25 | * @param Client $objguzzle |
||
26 | * @param Command $objcommand |
||
27 | */ |
||
28 | 12 | public function __construct(Client $objguzzle, Command $objcommand) |
|
33 | |||
34 | /** |
||
35 | * |
||
36 | * Send Request to sensiolab and return array of sensiolab vulnerabilities. |
||
37 | * Empty array if here is no vulnerabilities. |
||
38 | * |
||
39 | * @param $fileLock path to composer.lock file. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | 10 | public function getSensiolabVulnerabilties($fileLock) |
|
95 | |||
96 | /** |
||
97 | * @param $name |
||
98 | * @param $vulnerability |
||
99 | * @return array |
||
100 | */ |
||
101 | 4 | public function parseVulnerability($name, $vulnerability) |
|
102 | { |
||
103 | $data = [ |
||
104 | 4 | 'name' => $name, |
|
105 | 4 | 'version' => $vulnerability['version'], |
|
106 | 4 | 'advisories' => array_values($vulnerability['advisories']) |
|
107 | 4 | ]; |
|
108 | 4 | unset($this->tableVulnerabilities); |
|
109 | 4 | foreach ($data['advisories'] as $key2 => $advisory) { |
|
110 | $data2 = [ |
||
111 | 4 | 'title' => $advisory['title'], |
|
112 | 4 | 'link' => $advisory['link'], |
|
113 | 4 | 'cve' => $advisory['cve'] |
|
114 | 4 | ]; |
|
115 | |||
116 | $dataTable = [ |
||
117 | 4 | 'name' => $data['name'], |
|
118 | 4 | 'version' => $data['version'], |
|
119 | 4 | 'advisories' => $data2["title"] |
|
120 | 4 | ]; |
|
121 | |||
122 | 4 | $this->addVerboseLog($data['name'] . " " . $data['version'] . " " . $data2["title"], true); |
|
123 | 4 | $this->tableVulnerabilities[] = $dataTable; |
|
124 | 4 | } |
|
125 | |||
126 | 4 | return $this->tableVulnerabilities; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param $key |
||
131 | * @param $vulnerability |
||
132 | * @param $tuttoOk |
||
133 | * @return array |
||
134 | */ |
||
135 | 2 | public function checkResponse($key, $vulnerability, $tuttoOk) |
|
136 | { |
||
137 | 2 | $tableVulnerabilities = array(); |
|
138 | |||
139 | 2 | foreach ($this->parseVulnerability($key, $vulnerability) as $vul) { |
|
140 | 2 | $tableVulnerabilities[] = array_merge($vul, array('isOk' => $tuttoOk)); |
|
141 | 2 | } |
|
142 | |||
143 | 2 | return $tableVulnerabilities; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param $msg |
||
148 | * @param bool|false $error |
||
149 | */ |
||
150 | 12 | private function addVerboseLog($msg, $error = false) |
|
151 | { |
||
152 | 12 | $verbose = $this->command->option('verbose'); |
|
153 | 12 | if ($verbose) { |
|
154 | 10 | if ($error) { |
|
155 | 2 | $this->command->error($msg); |
|
156 | 2 | } else { |
|
157 | 8 | $this->command->line($msg); |
|
158 | } |
||
159 | 10 | } |
|
160 | 12 | } |
|
161 | |||
162 | /** |
||
163 | * @param \Psr\Http\Message\MessageInterface $message |
||
164 | * |
||
165 | */ |
||
166 | 6 | private function printMessage(\Psr\Http\Message\MessageInterface $message) |
|
167 | { |
||
168 | 6 | $type = ''; |
|
169 | 6 | if (is_a($message, '\Psr\Http\Message\RequestInterface')) { |
|
170 | 6 | $type = 'REQUEST'; |
|
171 | 6 | } else if (is_a($message, '\Psr\Http\Message\ResponseInterface')) { |
|
172 | 6 | $type = 'RESPONSE'; |
|
173 | 6 | } |
|
174 | 6 | $this->command->info("$type:"); |
|
175 | 6 | $headers = ''; |
|
176 | 6 | foreach ($message->getHeaders() as $name => $values) { |
|
177 | $headers .= $name . ': ' . implode(', ', $values) . "\r\n"; |
||
178 | 6 | } |
|
179 | 6 | $this->command->comment($headers); |
|
180 | 6 | if ($type == 'REQUEST') { |
|
181 | 6 | $this->command->comment($message->getBody()); |
|
182 | 6 | } else if ($type == 'RESPONSE') { |
|
183 | 6 | $this->command->comment($message->getBody()->getContents()); |
|
184 | 6 | } |
|
185 | 6 | } |
|
186 | |||
187 | |||
188 | /** |
||
189 | * Get the color tag for the given status code. |
||
190 | * |
||
191 | * @param string $code |
||
192 | * |
||
193 | * @return string |
||
194 | * |
||
195 | * @see https://github.com/spatie/http-status-check/blob/master/src/CrawlLogger.php#L96 |
||
196 | */ |
||
197 | 6 | protected function getColorTagForStatusCode($code) |
|
207 | } |
||
208 |