Passed
Push — master ( 1b743f...79d769 )
by Nicolaas
02:00
created

W3cValidateApi   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 80
c 1
b 1
f 0
dl 0
loc 138
rs 10
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 66 5
A makePostVars() 0 7 3
A setUri() 0 3 1
B W3Validate() 0 28 7
A setFragment() 0 4 1
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Api;
4
5
/*
6
   Author:	Jamie Telin ([email protected]), currently at employed Zebramedia.se
7
8
   Scriptname: W3C Validation Api v1.0 (W3C Markup Validation Service)
9
10
*/
11
12
class W3cValidateApi
13
{
14
    private $baseURL = 'http://validator.w3.org/check';
15
16
    private $output = 'soap12';
17
18
    private $uri = '';
19
20
    private $fragment = '';
21
22
    private $postVars = [];
23
24
    private $validResult = false;
25
26
    private $errorCount = 0;
27
28
    private $showErrors = true;
29
30
    private $errorList = [];
31
32
    public function W3Validate($uri = '', $fragment = '')
33
    {
34
        if ($uri) {
35
            $this->setUri($uri);
36
        } elseif ($fragment) {
37
            $this->setFragment($fragment);
38
        }
39
        $this->validate();
40
        if ($this->validResult) {
41
            $type = 'PASS';
42
            $color1 = '#00CC00';
43
        } else {
44
            $type = 'FAIL';
45
            $color1 = '#FF3300';
46
        }
47
        $errorDescription = '';
48
        if ($this->errorCount) {
49
            $errorDescription = ' - ' . $this->errorCount . 'errors: ';
50
            if ($this->showErrors) {
51
                if (count($this->errorList)) {
52
                    $errorDescription .= '<ul style="display: none;"><li>' . implode('</li><li>', $this->errorList) . '</li></ul>';
53
                }
54
            } else {
55
                $errorDescription .= '<a href="' . $this->baseURL . '?uri=' . urlencode($uri) . '">check</a>';
56
            }
57
        }
58
59
        return '<div style="background:' . $color1 . ';"><a href="#" class="showMoreClick">' . $type . '</a></strong>' . $errorDescription . '</div>';
60
    }
61
62
    private function makePostVars()
63
    {
64
        $this->postVars['output'] = $this->output;
65
        if ($this->fragment) {
66
            $this->postVars['fragment'] = $this->fragment;
67
        } elseif ($this->uri) {
68
            $this->postVars['uri'] = $this->uri;
69
        }
70
    }
71
72
    private function setUri($uri)
73
    {
74
        $this->uri = $uri;
75
    }
76
77
    private function setFragment($fragment)
78
    {
79
        $fragment = preg_replace('/\s+/', ' ', $fragment);
80
        $this->fragment = $fragment;
81
    }
82
83
84
    private function validate()
85
    {
86
        sleep(1);
87
88
        $this->makePostVars();
89
90
        $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
91
        $options = [
92
            CURLOPT_CUSTOMREQUEST => 'POST',        //set request type post or get
93
            CURLOPT_POST => 1,            //set to GET
94
            CURLOPT_USERAGENT => $user_agent, //"test from www.sunnysideup.co.nz",//$user_agent, //set user agent
95
            CURLOPT_COOKIEFILE => 'cookie.txt', //set cookie file
96
            CURLOPT_COOKIEJAR => 'cookie.txt', //set cookie jar
97
            CURLOPT_RETURNTRANSFER => true,     // return web page
98
            CURLOPT_HEADER => false,    // don't return headers
99
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
100
            CURLOPT_ENCODING => '',       // handle all encodings
101
            CURLOPT_AUTOREFERER => true,     // set referer on redirect
102
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
103
            CURLOPT_TIMEOUT => 120,      // timeout on response
104
            CURLOPT_MAXREDIRS => 10,       // stop after 10 redirects
105
            CURLOPT_POSTFIELDS => $this->postVars,
106
            CURLOPT_URL => $this->baseURL,
107
        ];
108
        $httphttpCode = '000';
0 ignored issues
show
Unused Code introduced by
The assignment to $httphttpCode is dead and can be removed.
Loading history...
109
110
        // Initialize the curl session
111
        $ch = curl_init();
112
        if($ch) {
0 ignored issues
show
introduced by
$ch is of type false|resource, thus it always evaluated to false.
Loading history...
113
            curl_setopt_array($ch, $options);
114
            // Execute the session and capture the response
115
            $out = curl_exec($ch);
116
117
            //$err               = curl_errno( $ch );
118
            //$errmsg            = curl_error( $ch );
119
            //$header            = curl_getinfo( $ch );
120
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
121
            if ($httpCode === 200) {
122
                $doc = simplexml_load_string($out);
123
                $doc->registerXPathNamespace('m', 'http://www.w3.org/2005/10/markup-validator');
124
125
                //valid ??
126
                $nodes = $doc->xpath('//m:markupvalidationresponse/m:validity');
127
                $this->validResult = strval($nodes[0]) === 'true' ? true : false;
128
129
                //error count ??
130
                $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorcount');
131
                $this->errorCount = strval($nodes[0]);
132
                //errors
133
                $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorlist/m:error');
134
                foreach ($nodes as $node) {
135
                    //line
136
                    $nodes = $node->xpath('m:line');
137
                    $line = strval($nodes[0]);
138
                    //col
139
                    $nodes = $node->xpath('m:col');
140
                    $col = strval($nodes[0]);
141
                    //message
142
                    $nodes = $node->xpath('m:message');
143
                    $message = strval($nodes[0]);
144
                    $this->errorList[] = $message . "(${line},${col})";
145
                }
146
            }
147
        }
148
149
        return $httpCode;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $httpCode does not seem to be defined for all execution paths leading up to this point.
Loading history...
150
    }
151
}
152