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

W3cValidateApi::W3cValidateApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
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 get_headers_from_curl_response($response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public function get_headers_from_curl_response(/** @scrutinizer ignore-unused */ $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        return $header;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $header seems to be never defined.
Loading history...
35
    }
36
37
    public function W3Validate($uri = '', $fragment = '')
38
    {
39
        if ($uri) {
40
            $this->setUri($uri);
41
        } elseif ($fragment) {
42
            $this->setFragment($fragment);
43
        }
44
        $this->validate();
45
        if ($this->validResult) {
46
            $type = 'PASS';
47
            $color1 = '#00CC00';
48
        } else {
49
            $type = 'FAIL';
50
            $color1 = '#FF3300';
51
        }
52
        $errorDescription = '';
53
        if ($this->errorCount) {
54
            $errorDescription = ' - ' . $this->errorCount . 'errors: ';
55
            if ($this->showErrors) {
56
                if (count($this->errorList)) {
57
                    $errorDescription .= '<ul style="display: none;"><li>' . implode('</li><li>', $this->errorList) . '</li></ul>';
58
                }
59
            } else {
60
                $errorDescription .= '<a href="' . $this->baseURL . '?uri=' . urlencode($uri) . '">check</a>';
61
            }
62
        }
63
64
        return '<div style="background:' . $color1 . ';"><a href="#" class="showMoreClick">' . $type . '</a></strong>' . $errorDescription . '</div>';
65
    }
66
67
    private function makePostVars()
68
    {
69
        $this->postVars['output'] = $this->output;
70
        if ($this->fragment) {
71
            $this->postVars['fragment'] = $this->fragment;
72
        } elseif ($this->uri) {
73
            $this->postVars['uri'] = $this->uri;
74
        }
75
    }
76
77
    private function setUri($uri)
78
    {
79
        $this->uri = $uri;
80
    }
81
82
    private function setFragment($fragment)
83
    {
84
        $fragment = preg_replace('/\s+/', ' ', $fragment);
85
        $this->fragment = $fragment;
86
    }
87
88
    private function validate()
89
    {
90
        sleep(1);
91
92
        $this->makePostVars();
93
94
        $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
95
        $options = [
96
            CURLOPT_CUSTOMREQUEST => 'POST',        //set request type post or get
97
            CURLOPT_POST => 1,            //set to GET
98
            CURLOPT_USERAGENT => $user_agent, //"test from www.sunnysideup.co.nz",//$user_agent, //set user agent
99
            CURLOPT_COOKIEFILE => 'cookie.txt', //set cookie file
100
            CURLOPT_COOKIEJAR => 'cookie.txt', //set cookie jar
101
            CURLOPT_RETURNTRANSFER => true,     // return web page
102
            CURLOPT_HEADER => false,    // don't return headers
103
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
104
            CURLOPT_ENCODING => '',       // handle all encodings
105
            CURLOPT_AUTOREFERER => true,     // set referer on redirect
106
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
107
            CURLOPT_TIMEOUT => 120,      // timeout on response
108
            CURLOPT_MAXREDIRS => 10,       // stop after 10 redirects
109
            CURLOPT_POSTFIELDS => $this->postVars,
110
            CURLOPT_URL => $this->baseURL,
111
        ];
112
        // Initialize the curl session
113
        $ch = curl_init();
114
        curl_setopt_array($ch, $options);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, $options);
Loading history...
115
        // Execute the session and capture the response
116
        $out = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        $out = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
117
118
        //$err               = curl_errno( $ch );
119
        //$errmsg            = curl_error( $ch );
120
        //$header            = curl_getinfo( $ch );
121
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        $httpCode = curl_getinfo(/** @scrutinizer ignore-type */ $ch, CURLINFO_HTTP_CODE);
Loading history...
122
        if ($httpCode === 200) {
123
            $doc = simplexml_load_string($out);
124
            $doc->registerXPathNamespace('m', 'http://www.w3.org/2005/10/markup-validator');
125
126
            //valid ??
127
            $nodes = $doc->xpath('//m:markupvalidationresponse/m:validity');
128
            $this->validResult = strval($nodes[0]) === 'true' ? true : false;
129
130
            //error count ??
131
            $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorcount');
132
            $this->errorCount = strval($nodes[0]);
133
            //errors
134
            $nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorlist/m:error');
135
            foreach ($nodes as $node) {
136
                //line
137
                $nodes = $node->xpath('m:line');
138
                $line = strval($nodes[0]);
139
                //col
140
                $nodes = $node->xpath('m:col');
141
                $col = strval($nodes[0]);
142
                //message
143
                $nodes = $node->xpath('m:message');
144
                $message = strval($nodes[0]);
145
                $this->errorList[] = $message . "(${line},${col})";
146
            }
147
        }
148
        return $httpCode;
149
    }
150
}
151