Passed
Push — master ( 0fe5f5...c76c09 )
by Nils
02:59
created

BadHeaderCollector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B collect() 0 37 10
A setClient() 0 3 1
A setInventory() 0 3 1
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Website;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Exception\ServerException;
7
use Startwind\Inventorio\Collector\BasicCollector;
8
use Startwind\Inventorio\Collector\ClientAwareCollector;
9
use Startwind\Inventorio\Collector\InventoryAwareCollector;
10
use Startwind\Inventorio\Util\Client;
11
use Startwind\Inventorio\Util\WebsiteUtil;
12
13
class BadHeaderCollector extends BasicCollector implements InventoryAwareCollector, ClientAwareCollector
14
{
15
    protected string $identifier = "WebsiteBadHeader";
16
17
    private array $inventory;
18
    private Client $client;
19
20
    public function setClient(Client $client): void
21
    {
22
        $this->client = $client;
23
    }
24
25
    public function setInventory(array $inventory): void
26
    {
27
        $this->inventory = $inventory;
28
    }
29
30
    public function collect(): array
31
    {
32
        $domains = WebsiteUtil::extractDomains($this->inventory);
33
34
        $badHeaders = [];
35
36
        foreach ($domains as $domain) {
37
            try {
38
                $url = $domain;
39
                if (!str_starts_with($url, 'http')) $url = 'https://' . $url;
40
                $response = $this->client->get($url);
41
            } catch (ClientException $e) {
42
                $response = $e->getResponse();
43
            } catch (ServerException $e) {
44
                $response = $e->getResponse();
45
            } catch (\Exception $e) {
46
                continue;
47
            }
48
49
            $headers = $response->getHeaders();
50
51
            if (array_key_exists('Server', $headers)) {
52
                if (str_contains($headers['Server'][0], 'Apache/')) {
53
                    $badHeaders[$domain]['apache_version'] = $headers['Server'][0];
54
                }
55
            }
56
57
            if (array_key_exists('X-Powered-By', $headers)) {
58
                if (str_contains($headers['X-Powered-By'][0], 'PHP/')) {
59
                    $badHeaders[$domain]['php_version'] = $headers['X-Powered-By'][0];
60
                }
61
            }
62
        }
63
64
        var_dump($badHeaders);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($badHeaders) looks like debug code. Are you sure you do not want to remove it?
Loading history...
65
66
        return $badHeaders;
67
    }
68
}
69