-
Notifications
You must be signed in to change notification settings - Fork 5
/
GlW3CValidator.php
277 lines (245 loc) · 7.58 KB
/
GlW3CValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
*
* PHP version 5.4
*
* @category GLICER
* @package GlValidator
* @author Emmanuel ROECKER
* @author Rym BOUCHAGOUR
* @copyright 2015 GLICER
* @license MIT
* @link http://dev.glicer.com/
*
* Created : 19/02/15
* File : GlW3CValidator.php
*
*/
namespace GlValidator;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use GuzzleHttp\Client;
use GuzzleHttp\Post\PostFile;
use GlHtml\GlHtml;
/**
* Class GlW3CValidator
* @package GlValidator
*/
class GlW3CValidator
{
const MAX_RETRY = 3;
private $types = [
'html' => [
'validator' => '/',
'selector' => '.success',
'resulttag' => '#results',
'field' => 'file',
'css' => [
'/style.css'
]
],
'css' => [
'validator' => '/validator',
'selector' => '#congrats',
'resulttag' => '#results_container',
'field' => 'file',
'css' => [
'/style/base.css',
'/style/results.css'
]
]
];
/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
private $fs;
/**
* @var string
*/
private $resultrootdir;
/**
* @param string $resultrootdir
* @param string $urlHtmlValidator
* @param string $urlCssValidator
*/
public function __construct(
$resultrootdir,
$urlHtmlValidator = "https://validator.w3.org/nu",
$urlCssValidator = "http://jigsaw.w3.org/css-validator"
) {
$this->fs = new Filesystem();
$this->resultrootdir = $resultrootdir;
$this->types['html']['w3curl'] = $urlHtmlValidator;
$this->types['css']['w3curl'] = $urlCssValidator;
if (!($this->fs->exists($resultrootdir))) {
$this->fs->mkdir($resultrootdir);
}
}
/**
* @param string $w3curl
* @param string $validator
* @param string $selector
* @param string $field
* @param string $htmltag
* @param string $file
* @param string $title
* @param array $csslist
*
* @return string
*/
private function sendToW3C($w3curl, $validator, $selector, $field, $htmltag, $file, $title, $csslist)
{
$client = new Client();
$request = $client->createRequest('POST', $w3curl . $validator, ['exceptions' => false]);
$postBody = $request->getBody();
$postBody->addFile(
new PostFile($field, fopen(
$file,
'r'
))
);
$retry = self::MAX_RETRY;
$response = null;
while ($retry--) {
$response = $client->send($request);
if ($response->getStatusCode() == 200) {
break;
}
}
$html = $response->getBody()->getContents();
$html = new GlHtml($html);
if ($html->get($selector)) {
return null;
}
$html->delete('head style');
$style = '<style type="text/css" media="all">';
foreach ($csslist as $css) {
$style .= '@import url("' . $w3curl . $css . '");';
}
$style .= '</style>';
$html->get("head")[0]->add($style);
$stats = $html->get("p.stats");
if (isset($stats) && count($stats) > 0) {
$stats[0]->delete();
}
$head = $html->get("head")[0]->getHtml();
$resulttag = $html->get($htmltag);
if (count($resulttag) <= 0) {
$result = '<p class="failure">There were errors.</p>';
} else {
$result = $resulttag[0]->getHtml();
}
$view = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">' .
$head . "</head><body><h2>$title</h2>" .
$result . "</body></html>";
return $view;
}
/**
* @param SplFileInfo $fileinfo
*
* @throws \Exception
* @return string
*/
private function validateFile(SplFileInfo $fileinfo)
{
$ext = $fileinfo->getExtension();
$title = strtr($fileinfo->getRelativePathname(), ["\\" => "/"]);
$view = $this->sendToW3C(
$this->types[$ext]['w3curl'],
$this->types[$ext]['validator'],
$this->types[$ext]['selector'],
$this->types[$ext]['field'],
$this->types[$ext]['resulttag'],
strtr($fileinfo->getRealPath(), ["\\" => "/"]),
$title,
$this->types[$ext]['css']
);
if ($view === null) {
return null;
}
$filedir = $this->resultrootdir . '/' . strtr($fileinfo->getRelativepath(), ["\\" => "/"]);
if (!$this->fs->exists($filedir)) {
$this->fs->mkdir($filedir);
}
$resultname = $filedir . "/w3c_" . $ext . "_" . $fileinfo->getBaseName($ext) . 'html';
file_put_contents($resultname, $view);
return $resultname;
}
/**
* @param array $files
* @param array $types
* @param callable $callback
*
* @throws \Exception
* @return array
*/
public function validate(array $files, array $types, callable $callback)
{
$filter = '/\.(' . implode('|', $types) . ')$/';
$results = [];
foreach ($files as $file) {
if ($file instanceof Finder) {
$this->validateFinder($file, $filter, $callback, $results);
} else {
if (is_string($file)) {
$this->validateDirect($file, $filter, $callback, $results);
} else {
throw new \Exception('Must be a string or a finder');
}
}
}
return $results;
}
/**
* @param Finder $files
* @param string $filter
* @param callable $callback
* @param array $result
*/
private function validateFinder(Finder $files, $filter, callable $callback, array &$result)
{
$files->name($filter);
/**
* @var SplFileInfo $file
*/
foreach ($files as $file) {
$callback($file);
$result[strtr($file->getRelativePath() . '/' . $file->getFilename(), ["\\" => "/"])] = $this->validateFile(
$file
);
}
}
/**
* @param string $file
* @param string $filter
* @param callable $callback
* @param array $result
*/
private function validateDirect($file, $filter, callable $callback, array &$result)
{
if (is_dir($file)) {
$finder = new Finder();
$finder->files()->in($file)->name($filter);
/**
* @var SplFileInfo $finderfile
*/
foreach ($finder as $finderfile) {
$callback($finderfile);
$result[strtr(
$finderfile->getRelativePath() . '/' . $finderfile->getFilename(),
["\\" => "/"]
)] = $this->validateFile($finderfile);
}
} else {
if (preg_match($filter, $file)) {
$finderfile = new SplFileInfo($file, "", "");
$callback($finderfile);
$result[strtr(
$finderfile->getRelativePath() . '/' . $finderfile->getFilename(),
["\\" => "/"]
)] = $this->validateFile($finderfile);
}
}
}
}