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 | 18 | 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 | 16 | public function getSensiolabVulnerabilties($fileLock) |
|
44 | { |
||
45 | 16 | $this->addVerboseLog('Send request to sensiolab: <info>' . $fileLock . '</info>'); |
|
46 | |||
47 | 16 | $debug = false;//set to true to log into console output |
|
48 | $headers = [ |
||
49 | //OPTIONS |
||
50 | 'allow_redirects' => [ |
||
51 | 16 | 'max' => 3, // allow at most 10 redirects. |
|
52 | 16 | 'strict' => true, // use "strict" RFC compliant redirects. |
|
53 | 16 | 'referer' => true, // add a Referer header |
|
54 | 16 | 'protocols' => ['http', 'https'], // only allow http and https URLs |
|
55 | 'track_redirects' => false |
||
56 | 16 | ], |
|
57 | 16 | 'connect_timeout' => 20,//Use 0 to wait connection indefinitely |
|
58 | 16 | 'timeout' => 30, //Use 0 to wait response indefinitely |
|
59 | 16 | 'debug' => $debug, |
|
60 | //HEADERS |
||
61 | 'headers' => [ |
||
62 | 'Accept' => 'application/json' |
||
63 | 16 | ], |
|
64 | //UPLOAD FORM FILE |
||
65 | 'multipart' => [ |
||
66 | [ |
||
67 | 16 | 'name' => 'lock', |
|
68 | 16 | 'contents' => fopen($fileLock, 'r') |
|
69 | 8 | ] |
|
70 | 8 | ] |
|
71 | 8 | ]; |
|
72 | 8 | $response = null; |
|
73 | |||
74 | try { |
||
75 | 8 | $iResponse = $this->guzzle->request('POST', 'https://security.sensiolabs.org/check_lock', $headers); |
|
76 | 8 | $responseBody = $iResponse->getBody()->getContents(); |
|
77 | 8 | $response = json_decode($responseBody, true); |
|
78 | 8 | } catch (\GuzzleHttp\Exception\ClientException $e) { |
|
79 | $this->command->error("ClientException!\nMessage: " . $e->getMessage()); |
||
80 | $colorTag = $this->getColorTagForStatusCode($e->getResponse()->getStatusCode()); |
||
81 | $this->command->line("HTTP StatusCode: <{$colorTag}>" . $e->getResponse()->getStatusCode() . "<{$colorTag}>"); |
||
82 | $this->printMessage($e->getResponse()); |
||
83 | $this->printMessage($e->getRequest()); |
||
84 | } catch (\GuzzleHttp\Exception\RequestException $e) { |
||
85 | $this->command->error("RequestException!\nMessage: " . $e->getMessage()); |
||
86 | $this->printMessage($e->getRequest()); |
||
87 | if ($e->hasResponse()) { |
||
88 | $colorTag = $this->getColorTagForStatusCode($e->getResponse()->getStatusCode()); |
||
89 | $this->command->line("HTTP StatusCode: <{$colorTag}>" . $e->getResponse()->getStatusCode() . "<{$colorTag}>"); |
||
90 | $this->printMessage($e->getResponse()); |
||
91 | } |
||
92 | } |
||
93 | 8 | return $response; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param $name |
||
98 | * @param $vulnerability |
||
99 | * @return array |
||
100 | */ |
||
101 | 8 | public function parseVulnerability($name, $vulnerability) |
|
128 | |||
129 | /** |
||
130 | * @param $key |
||
131 | * @param $vulnerability |
||
132 | * @param $tuttoOk |
||
133 | * @return array |
||
134 | */ |
||
135 | 6 | public function checkResponse($key, $vulnerability, $tuttoOk) |
|
145 | |||
146 | /** |
||
147 | * @param $msg |
||
148 | * @param bool|false $error |
||
149 | */ |
||
150 | 18 | private function addVerboseLog($msg, $error = false) |
|
161 | |||
162 | /** |
||
163 | * @param \Psr\Http\Message\MessageInterface $message |
||
164 | * |
||
165 | */ |
||
166 | private function printMessage(\Psr\Http\Message\MessageInterface $message) |
||
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 | protected function getColorTagForStatusCode($code) |
||
207 | } |
||
208 |