Passed
Push — dev ( 02a5b7...e90dba )
by Greg
01:45
created

HttpHeader::checkSameRedirects()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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