Passed
Push — master ( d046f6...1686c2 )
by Greg
01:43
created

HttpHeader::getDomainApex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace GJClasses;
3
4
class HttpHeader
5
{
6
    public $log;
7
8
    public function __construct()
9
    {
10
        $this->log = new Log('class.httpheader');
11
    }
12
13
    public function process($domain)
14
    {
15
        $headers = $this->retrieve($domain);
16
        list($status, $data, $final_destination, $final_destination_apex) = $this->processRules($domain, $headers);
17
        return array($status, $data, $final_destination, $final_destination_apex);
18
    }
19
20
    public function retrieve($domain)
21
    {
22
        stream_context_set_default(
23
            array(
24
                'http' => array(
25
                    'timeout' => 8,
26
                    'method' => "GET",
27
                    'header' => "Accept-language: en\r\n" .
28
                        "Cookie: LANGUAGE=en;DEFLANG=en;"
29
                )
30
            )
31
        );
32
33
        return @get_headers('http://' . $domain);
34
    }
35
36
    public function processRules($domain, $headers)
37
    {
38
        if ($headers['0'] == 'HTTP/1.0 200 OK' || $headers['0'] == 'HTTP/1.1 200 OK') {
39
40
            $final_header_status = 'Live Site (200)';
41
            $header_data = 'n/a';
42
            $final_destination = 'http://' . $domain;
43
            $final_destination_apex = $domain;
44
45
        } elseif ($headers['0'] == 'HTTP/1.0 301 Moved Permanently' || $headers['0'] == 'HTTP/1.1 301 Moved Permanently') {
46
47
            list($header_data, $final_destination, $final_destination_apex, $count) = $this->createData($domain, $headers);
48
49
            if ($count === 1) {
50
                $header_status = 'Redirect, Permanent (301)';
51
            } elseif ($count > 1) {
52
                $header_status = 'Redirect, Permanent (301) [Multiple Redirects]';
53
            }
54
55
            $final_header_status = $this->checkSameRedirects($domain, $header_status, $header_data);
56
57
        } elseif ($headers['0'] == 'HTTP/1.0 302 Found' || $headers['0'] == 'HTTP/1.1 302 Found' || $headers['0'] == 'HTTP/1.1 302 Moved Temporarily') {
58
59
            list($header_data, $final_destination, $final_destination_apex, $count) = $this->createData($domain, $headers);
60
61
            if ($count === 1) {
62
                $header_status = 'Redirect, Temporary (302)';
63
            } elseif ($count > 1) {
64
                $header_status = 'Redirect, Temporary (302) [Multiple Redirects]';
65
            }
66
67
            $final_header_status = $this->checkSameRedirects($domain, $header_status, $header_data);
68
69
        } elseif ($headers['0'] == 'HTTP/1.0 400 Bad Request') {
70
71
            $final_header_status = 'Bad Request (400)';
72
            $header_data = 'n/a';
73
            $final_destination = 'n/a';
74
            $final_destination_apex = 'n/a';
75
76
        } elseif ($headers['0'] == 'HTTP/1.1 403 Forbidden') {
77
78
            $final_header_status = 'Forbidden (403)';
79
            $header_data = 'n/a';
80
            $final_destination = 'n/a';
81
            $final_destination_apex = 'n/a';
82
83
        } elseif ($headers['0'] == 'HTTP/1.0 404 Not Found' || $headers['0'] == 'HTTP/1.1 404 Not Found') {
84
85
            $final_header_status = 'Not Found (404)';
86
            $header_data = 'n/a';
87
            $final_destination = 'n/a';
88
            $final_destination_apex = 'n/a';
89
90
        } elseif ($headers['0'] == 'HTTP/1.1 463' || $headers['0'] == 'HTTP/1.1 463 ') {
91
92
            $final_header_status = 'Unspecified Error (463)';
93
            $header_data = 'n/a';
94
            $final_destination = 'n/a';
95
            $final_destination_apex = 'n/a';
96
97
        } elseif ($headers['0'] == 'HTTP/1.0 500 Internal Server Error' || $headers['0'] == 'HTTP/1.1 500 Internal Server Error') {
98
99
            $final_header_status = 'Internal Server Error (500)';
100
            $header_data = 'n/a';
101
            $final_destination = 'n/a';
102
            $final_destination_apex = 'n/a';
103
104
        } elseif ($headers['0'] == 'HTTP/1.0 503 Service Unavailable' || $headers['0'] == 'HTTP/1.1 503 Service Unavailable' || $headers['0'] == 'HTTP/1.1 503 Service Temporarily Unavailable') {
105
106
            $final_header_status = 'Service Temporarily Unavailable (503)';
107
            $header_data = 'n/a';
108
            $final_destination = 'n/a';
109
            $final_destination_apex = 'n/a';
110
111
        } else {
112
113
            if (!$headers['0'] || $headers['0'] == '') {
114
115
                $final_header_status = 'Error Retrieving Headers';
116
117
            } else {
118
119
                $final_header_status = $headers['0'];
120
121
            }
122
123
            $header_data = 'n/a';
124
            $final_destination = 'n/a';
125
            $final_destination_apex = 'n/a';
126
127
        }
128
129
        return array($final_header_status, $header_data, $final_destination, $final_destination_apex);
130
    }
131
132
    public function createData($domain, $headers)
133
    {
134
        $count = 0;
135
136
        $header_data = 'http://' . $domain . ' -> ';
137
138
        foreach ($headers as $value) {
139
            if (preg_match("/^Location:/", $value)) {
140
                $no_slash = rtrim($value, '/');
141
                $header_data .= substr($no_slash, 10) . " -> ";
142
                $count++;
143
            }
144
        }
145
        $header_data = substr($header_data, 0, -4);
146
        $final_destination = substr($no_slash, 10);
147
        $final_destination_apex = $this->getDomainApex($final_destination);
148
149
        return array($header_data, $final_destination, $final_destination_apex, $count);
150
151
    }
152
153
    public function getDomainApex($domain)
154
    {
155
        return ltrim(parse_url($domain, PHP_URL_HOST), 'www.');
156
    }
157
158
    public function checkSameRedirects($domain, $http_header_status, $http_header_data)
159
    {
160
        // check to see if the redirect chain is all for the same domain, and if so update it to 'Live Site (200)'
161
        $final_http_header_status = $http_header_status;
162
163
        if ($http_header_status === 'Redirect, Permanent (301)' || $http_header_status ===
164
            'Redirect, Permanent (301) [Multiple Redirects]' || $http_header_status === 'Redirect, Temporary (302)' ||
165
            $http_header_status === 'Redirect, Temporary (302) [Multiple Redirects]') {
166
167
            $header_data_array = explode(" -> ", $http_header_data);
168
169
            // standard port
170
            $url_variations = array();
171
            $url_variations[0] = 'http://' . $domain; // insecure, no-www
172
            $url_variations[1] = 'http://www.' . $domain; // insecure, www
173
            $url_variations[2] = 'https://' . $domain; // secure, no-www
174
            $url_variations[3] = 'https://www.' . $domain; // secure, www
175
            // port 443
176
            $url_variations[4] = 'http://' . $domain . ':443'; // insecure, no-www
177
            $url_variations[5] = 'http://www.' . $domain . ':443'; // insecure, www
178
            $url_variations[6] = 'https://' . $domain . ':443'; // secure, no-www
179
            $url_variations[7] = 'https://www.' . $domain . ':443'; // secure, www
180
181
            $all_same_domain = !array_diff($header_data_array, $url_variations);
182
183
            if ($all_same_domain === true) {
184
185
                $final_http_header_status = 'Live Site (200) [With Redirects]';
186
187
            }
188
189
        }
190
191
        return $final_http_header_status;
192
193
    }
194
195
}
196