These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * PHP version 5.4 |
||
4 | * |
||
5 | * @category GLICER |
||
6 | * @package GlLinkChecker |
||
7 | * @author Emmanuel ROECKER |
||
8 | * @author Rym BOUCHAGOUR |
||
9 | * @copyright 2015 GLICER |
||
10 | * @license MIT |
||
11 | * @link http://dev.glicer.com/ |
||
12 | * |
||
13 | * Created : 10/03/15 |
||
14 | * File : GlLinkCheckerError.php |
||
15 | * |
||
16 | */ |
||
17 | |||
18 | namespace GlLinkChecker; |
||
19 | |||
20 | use GuzzleHttp\Client; |
||
21 | use GuzzleHttp\Exception\ClientException; |
||
22 | use GuzzleHttp\Exception\RequestException; |
||
23 | use GuzzleHttp\Ring\Exception\RingException; |
||
24 | |||
25 | /** |
||
26 | * Class GlLinkCheckerUrl |
||
27 | * @package GLLinkChecker |
||
28 | */ |
||
29 | class GlLinkCheckerError |
||
30 | { |
||
31 | /** |
||
32 | * @var \GuzzleHttp\Client $client |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $link; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $url; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $files; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $statuscode = 0; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $isAbsolute = true; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $isExist = true; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $isLowerCase = true; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $isNotEndSlash = true; |
||
76 | |||
77 | /** |
||
78 | * Client $client |
||
79 | * string $link |
||
80 | * array $files |
||
81 | */ |
||
82 | public function __construct(Client $client, $link, $files) |
||
83 | { |
||
84 | $url = parse_url($link); |
||
85 | if ($url === false) { |
||
86 | throw new \Exception("Cannot parse link : " . $link); |
||
87 | } |
||
88 | |||
89 | $this->client = $client; |
||
90 | $this->url = $url; |
||
91 | $this->link = $link; |
||
92 | $this->files = $files; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | View Code Duplication | private function checkexisthead() |
|
99 | { |
||
100 | try { |
||
101 | $response = $this->client->head($this->link); |
||
102 | $this->statuscode = $response->getStatusCode(); |
||
103 | $this->isExist = (($this->statuscode == 200) || ($this->statuscode == 204)); |
||
104 | |||
105 | return $this->isExist; |
||
106 | } catch (ClientException $e) { |
||
107 | $this->statuscode = $e->getCode(); |
||
108 | } catch (RequestException $e) { |
||
109 | |||
110 | } catch (RingException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
111 | |||
112 | } |
||
113 | |||
114 | $this->isExist = false; |
||
115 | |||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | View Code Duplication | private function checkexistget() |
|
123 | { |
||
124 | try { |
||
125 | $response = $this->client->get($this->link); |
||
126 | $this->statuscode = $response->getStatusCode(); |
||
127 | $this->isExist = (($this->statuscode == 200) || ($this->statuscode == 204)); |
||
128 | |||
129 | return $this->isExist; |
||
130 | } catch (ClientException $e) { |
||
131 | $this->statuscode = $e->getCode(); |
||
132 | } catch (RequestException $e) { |
||
133 | } catch (RingException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
134 | } |
||
135 | |||
136 | $this->isExist = false; |
||
137 | |||
138 | return false; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * |
||
143 | */ |
||
144 | private function checkexist() |
||
145 | { |
||
146 | if ($this->checkexisthead()) { |
||
147 | return true; |
||
148 | } |
||
149 | if ($this->checkexistget()) { |
||
150 | return true; |
||
151 | } |
||
152 | |||
153 | return false; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | private function checkendslash() |
||
160 | { |
||
161 | if (substr($this->link, -1) == '/') { |
||
162 | $this->isNotEndSlash = true; |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
167 | if (isset($this->url['path']) && (strlen($this->url['path']) > 0)) { |
||
168 | $extension = pathinfo($this->url['path'], PATHINFO_EXTENSION); |
||
169 | if (isset($extension) && (strlen($extension) > 0)) { |
||
170 | $this->isNotEndSlash = true; |
||
171 | |||
172 | return true; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $this->isNotEndSlash = false; |
||
177 | |||
178 | return false; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return bool |
||
183 | */ |
||
184 | private function checkabsolute() |
||
185 | { |
||
186 | View Code Duplication | if (isset($this->url['host']) && (strlen($this->url['host']) > 0)) { |
|
187 | $this->isAbsolute = true; |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | View Code Duplication | if (isset($this->url['path']) && (strpos($this->url['path'], "/") === 0)) { |
|
192 | $this->isAbsolute = true; |
||
193 | |||
194 | return true; |
||
195 | } |
||
196 | $this->isAbsolute = false; |
||
197 | |||
198 | return false; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return bool |
||
203 | */ |
||
204 | private function checklowercase() |
||
205 | { |
||
206 | $this->isLowerCase = ($this->link === strtolower($this->link)); |
||
207 | |||
208 | return $this->isLowerCase; |
||
209 | } |
||
210 | |||
211 | |||
212 | /** |
||
213 | * @param array|null $internalurls |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function isInternal($internalurls) |
||
218 | { |
||
219 | if (!isset($internalurls)) { |
||
220 | return true; |
||
221 | } |
||
222 | |||
223 | if (!isset($this->url['host']) || (strlen($this->url['host']) <= 0)) { |
||
224 | return true; |
||
225 | } |
||
226 | |||
227 | foreach ($internalurls as $internalurl) { |
||
228 | if (strpos($this->link, $internalurl) === 0) { |
||
229 | return true; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return false; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param array $list |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function check(array $list) |
||
242 | { |
||
243 | $result = true; |
||
244 | foreach ($list as $element) { |
||
245 | $element = "check" . trim(strtolower($element)); |
||
246 | $result &= $this->$element(); |
||
247 | } |
||
248 | |||
249 | return $result; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getLink() |
||
256 | { |
||
257 | return $this->link; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return array |
||
262 | */ |
||
263 | public function getFiles() |
||
264 | { |
||
265 | return $this->files; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @return int |
||
270 | */ |
||
271 | public function getStatusCode() |
||
272 | { |
||
273 | return $this->statuscode; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getErrorMessages() |
||
280 | { |
||
281 | $message = []; |
||
282 | |||
283 | if (!($this->isAbsolute)) { |
||
284 | $message[] = "Must be absolute (Sample : /article/index.html)"; |
||
285 | } |
||
286 | |||
287 | if (!($this->isLowerCase)) { |
||
288 | $message[] = "Must be in lowercase (Sample : http://www.example.com/index.html)"; |
||
289 | } |
||
290 | |||
291 | if (!($this->isExist)) { |
||
292 | $message[] = "Must exist (Http get error)"; |
||
293 | } |
||
294 | |||
295 | if (!($this->isNotEndSlash)) { |
||
296 | $message[] = "Must have a slash at the end (Sample : http://www.example.com/)"; |
||
297 | } |
||
298 | |||
299 | return $message; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getErrorArray() { |
||
306 | $error = []; |
||
307 | |||
308 | $error['absolute'] = $this->isAbsolute; |
||
309 | $error['lowercase'] = $this->isLowerCase; |
||
310 | $error['exist'] = $this->isExist; |
||
311 | $error['notendslash'] = $this->isNotEndSlash; |
||
312 | |||
313 | return $error; |
||
314 | } |
||
315 | } |
||
316 |