1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Cache\Adapter\Filesystem\FilesystemCachePool; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Ivory\HttpAdapter\CurlHttpAdapter; |
8
|
|
|
use League\Flysystem\Adapter\Local; |
9
|
|
|
use League\Flysystem\Filesystem; |
10
|
|
|
use phm\HttpWebdriverClient\Http\Client\Chrome\ChromeClient; |
11
|
|
|
use phm\HttpWebdriverClient\Http\Client\Decorator\CacheDecorator; |
12
|
|
|
use phm\HttpWebdriverClient\Http\Client\Guzzle\GuzzleClient; |
13
|
|
|
use phm\HttpWebdriverClient\Http\Client\HttpClient; |
14
|
|
|
use phmLabs\Components\Annovent\Dispatcher; |
15
|
|
|
use PhmLabs\Components\Init\Init; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use whm\Smoke\Config\Configuration; |
20
|
|
|
use whm\Smoke\Extensions\SmokeHttpClient\CacheAware; |
21
|
|
|
use whm\Smoke\Scanner\Scanner; |
22
|
|
|
use whm\Smoke\Yaml\EnvAwareYaml; |
23
|
|
|
|
24
|
|
|
class SmokeCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var OutputInterface |
28
|
|
|
*/ |
29
|
|
|
protected $output; |
30
|
|
|
protected $eventDispatcher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Configuration |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
protected function init(InputInterface $input, OutputInterface $output, $url = null) |
38
|
|
|
{ |
39
|
|
|
if ($input->hasOption('bootstrap') && !is_null($input->getOption('bootstrap'))) { |
40
|
|
|
include $input->getOption('bootstrap'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->output = $output; |
44
|
|
|
$this->eventDispatcher = new Dispatcher(); |
45
|
|
|
|
46
|
|
|
Init::registerGlobalParameter('_eventDispatcher', $this->eventDispatcher); |
47
|
|
|
Init::registerGlobalParameter('_output', $output); |
48
|
|
|
|
49
|
|
|
$this->writeSmokeCredentials($url); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This function creates the credentials header for the command line. |
54
|
|
|
* |
55
|
|
|
* @param null $url |
56
|
|
|
*/ |
57
|
|
|
protected function writeSmokeCredentials($url = null) |
58
|
|
|
{ |
59
|
|
|
$this->output->writeln("\n Smoke " . SMOKE_VERSION . " by Nils Langner\n"); |
60
|
|
|
|
61
|
|
|
if ($url) { |
62
|
|
|
$this->output->writeln(' <info>Scanning ' . $url . "</info>\n"); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function scan() |
67
|
|
|
{ |
68
|
|
|
$client = $this->config->getClient(); |
69
|
|
|
|
70
|
|
|
$scanner = new Scanner($this->config->getRules(), |
71
|
|
|
$client, |
72
|
|
|
$this->eventDispatcher, |
73
|
|
|
$this->config->getExtension('_ResponseRetriever')->getRetriever()); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$scanner->scan(); |
77
|
|
|
} catch (\Exception $e) { |
78
|
|
|
if (method_exists($client, 'close')) { |
79
|
|
|
$client->close(); |
80
|
|
|
} |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (method_exists($client, 'close')) { |
85
|
|
|
$client->close(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $scanner->getStatus(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns an array representing the configuration. |
93
|
|
|
* |
94
|
|
|
* @param $configFile |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function getConfigArray($configFile, $mandatory = false) |
99
|
|
|
{ |
100
|
|
|
$configArray = array(); |
101
|
|
|
|
102
|
|
|
if ($configFile) { |
103
|
|
|
if (strpos($configFile, 'http://') === 0 || strpos($configFile, 'https://') === 0) { |
104
|
|
|
$curlClient = new Client(); |
105
|
|
|
$fileContent = (string)$curlClient->get($configFile)->getBody(); |
106
|
|
|
} else { |
107
|
|
|
if (file_exists($configFile)) { |
108
|
|
|
$fileContent = file_get_contents($configFile); |
109
|
|
|
} else { |
110
|
|
|
throw new \RuntimeException("Config file was not found ('" . $configFile . "')."); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$configArray = EnvAwareYaml::parse($fileContent); |
114
|
|
|
} else { |
115
|
|
|
if ($mandatory) { |
116
|
|
|
throw new \RuntimeException('Config file was not defined.'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $configArray; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|