Passed
Push — dev ( add4e9...02a5b7 )
by Greg
02:28
created

HttpHeader::createData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 17
rs 9.9332
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);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $header_status does not seem to be defined for all execution paths leading up to this point.
Loading history...
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);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $no_slash does not seem to be defined for all execution paths leading up to this point.
Loading history...
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
            // standard port
144
            $data_variation[0] = 'http://' . $domain; // insecure, no-www
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data_variation was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data_variation = array(); before regardless.
Loading history...
145
            $data_variation[1] = 'http://www.' . $domain; // insecure, www
146
            $data_variation[2] = 'https://' . $domain; // secure, no-www
147
            $data_variation[3] = 'https://www.' . $domain; // secure, www
148
            // port 443
149
            $data_variation[4] = 'http://' . $domain . ':443'; // insecure, no-www
150
            $data_variation[5] = 'http://www.' . $domain . ':443'; // insecure, www
151
            $data_variation[6] = 'https://' . $domain . ':443'; // secure, no-www
152
            $data_variation[7] = 'https://www.' . $domain . ':443'; // secure, www
153
154
            $data_result = explode(" -> ", $http_header_data);
155
156
            $count = 0;
157
158
            foreach ($data_result as $value) {
159
160
                $data_result_formatted[$count] = $value;
161
                $count++;
162
163
            }
164
165
            $all_same_domain = !array_diff($data_result_formatted, $data_variation);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data_result_formatted seems to be defined by a foreach iteration on line 158. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
166
167
            if ($all_same_domain === true) {
168
169
                $final_http_header_status = 'Live Site (200)';
170
171
            }
172
173
            unset($data_variation);
174
            unset($data_result);
175
            unset($data_result_formatted);
176
177
        }
178
179
        return $final_http_header_status;
180
181
    }
182
183
}
184