1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Nkey/LinkCheck package |
4
|
|
|
* |
5
|
|
|
* @package Nkey\LinkCheck |
6
|
|
|
*/ |
7
|
|
|
namespace Nkey\LinkCheck; |
8
|
|
|
|
9
|
|
|
use Generics\Client\HttpClientFactory; |
10
|
|
|
use Generics\Socket\Url; |
11
|
|
|
use Generics\Streams\StandardOutputStream; |
12
|
|
|
use Generics\Streams\HttpStream; |
13
|
|
|
use Generics\Util\UrlParser; |
14
|
|
|
use Generics\Streams\OutputStream; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class provides the link checking functionality. |
18
|
|
|
* |
19
|
|
|
* @author Maik Greubel <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class LinkCheckProvider |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var HttpStream |
27
|
|
|
*/ |
28
|
|
|
private $httpClient; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var OutputStream |
33
|
|
|
*/ |
34
|
|
|
private $output; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var Url |
39
|
|
|
*/ |
40
|
|
|
private $url; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new instance of LinkCheckProvider |
44
|
|
|
* |
45
|
|
|
* @param Url $url |
46
|
|
|
* The url to check |
47
|
|
|
*/ |
48
|
1 |
|
public function __construct(Url $url) |
49
|
|
|
{ |
50
|
1 |
|
$this->httpClient = HttpClientFactory::get($url); |
51
|
1 |
|
$this->output = new StandardOutputStream(); |
52
|
1 |
|
$this->url = $url; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Override the standard output stream using a custom output stream instance |
57
|
|
|
* |
58
|
|
|
* @param OutputStream $stream |
59
|
|
|
*/ |
60
|
1 |
|
public function setOutput(OutputStream $stream) |
61
|
|
|
{ |
62
|
1 |
|
$this->output = $stream; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Perform check |
67
|
|
|
* - Retrieve the contents of url |
68
|
|
|
* - Extract all anchor links |
69
|
|
|
* - Check all anchor links for existance (response code < 400) |
70
|
|
|
* |
71
|
|
|
* @param array $options |
72
|
|
|
* The options for check ('recursive' => perform a recursive check of all same-site-links) |
73
|
|
|
*/ |
74
|
1 |
|
public function check(array $options = array()) |
75
|
|
|
{ |
76
|
1 |
|
$this->httpClient->request('GET'); |
77
|
|
|
|
78
|
1 |
|
if ($this->httpClient->getResponseCode() != 200) { |
79
|
|
|
$this->output->write("Invalid response code " . $this->httpClient->getResponseCode()); |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$response = ""; |
84
|
1 |
|
$size = $this->httpClient->getPayload()->count(); |
85
|
1 |
|
if (isset($this->httpClient->getHeaders()['Content-Length'])) { |
86
|
1 |
|
$size = $this->httpClient->getHeaders()['Content-Length']; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
while ($this->httpClient->getPayload()->ready()) { |
90
|
1 |
|
$response .= $this->httpClient->getPayload()->read($size); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$this->extractAndCheckUrls($response, $options); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
private function extractAndCheckUrls(string $response, array $options) |
97
|
|
|
{ |
98
|
1 |
|
$matches = []; |
99
|
|
|
|
100
|
1 |
|
if (preg_match_all('#<a href="([^\"]*)">#', $response, $matches)) { |
101
|
1 |
|
array_shift($matches); |
102
|
1 |
|
$matches = $matches[0]; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
if (count($matches) == 0) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
foreach ($matches as $match) { |
110
|
1 |
|
if (substr($match, 0, 4) != 'http') { |
111
|
1 |
|
$match = sprintf("%s://%s:%d%s", $this->url->getScheme(), $this->url->getAddress(), $this->url->getPort(), $match); |
112
|
|
|
} |
113
|
1 |
|
$url = UrlParser::parseUrl($match); |
114
|
|
|
|
115
|
1 |
|
$this->output->write($url); |
116
|
1 |
|
if ($url->getAddress() == $this->url->getAddress()) { |
117
|
1 |
|
$this->checkSameSiteurl($url, $options); |
118
|
|
|
} |
119
|
1 |
|
$this->output->write("\n"); |
120
|
|
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
private function checkSameSiteUrl(Url $url, array $options) |
124
|
|
|
{ |
125
|
1 |
|
if (isset($options['recursive']) && $options['recursive'] == true) { |
126
|
|
|
$subProvider = new LinkCheckProvider($url); |
127
|
|
|
$subProvider->check($options); |
128
|
|
|
} else { |
129
|
1 |
|
$http = HttpClientFactory::get($url); |
130
|
1 |
|
$http->setTimeout(10); |
131
|
1 |
|
$http->request('HEAD'); |
132
|
1 |
|
if ($http->getResponseCode() < 400) { |
133
|
1 |
|
$this->output->write(" OK"); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |