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