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