-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkchecker.php
313 lines (242 loc) · 8.66 KB
/
linkchecker.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
//Autor: Marco Kleine-Albers
/**
* checking links of a sitemap for broken links, see config.php
*
* @var array $sites
*/
require 'config.php';
$results_dir = dirname(__FILE__) . '/results/results.html';
//get the content of the xml file
$urlContent = file_get_contents($sites['config']['sitemap']);
unset($sites['config']);
$providers = $sites;
//the website links we check
$links = extract_xmlsitemap_links($urlContent);
//save already checked links for more efficientcy
$checked_links = [];
$link_count = count($links);
foreach ($links as $url_key => $url) {
//get the website link html code
$webseite_data = check_url($url);
$header = $webseite_data['headers']['http_code'];
$website_code = $webseite_data['data'];
//extract all links form that websites html code
$website_links = extract_html_links($website_code);
//@todo 302 redirects on amazon redirect wrong short urls to amazon.com? no?
//check for all providers, see config.php
foreach ($providers as $provider_name => $provider_data) {
$urls_pattern = implode('|', $provider_data['urls']);
//extract provider_urls
$provider_links = extract_provider_links($website_links, $provider_data['urls']);
//now we have all provider links and can loop these for checking
foreach ($provider_links as $key => $provider_link) {
//only check a provider link ONCE on a run!
if (!in_array($provider_link, $checked_links)) {
//add to checked list
$checked_links[] = $provider_link;
//check the links of provider
$provider = "$provider_name";
//we check if a product is not avaliable no more. We use the STRING from config.php for that
$na_regex = '/' . $provider_data['not_avaliable'] . '/m';
$error_regex = '/' . $provider_data['not_found_string'] . '/m';
//now call the providers link/url!
$provider_website_data = check_url($provider_link);
//debug error page 404
//$provider_website_data = check_url('https://www.amazon.de/gp/product/B08LKC334PPWM');
//debug not avaliable
//$provider_website_data = check_url('https://www.amazon.de/dp/B00AJWBAB2');
//debug broken shortlink
//$provider_website_data = check_url('https://amzn.to/3q13NPc');
$provider_header = $provider_website_data['headers']['http_code'];
$provider_code = $provider_website_data['data'];
$provider_location = $provider_website_data['headers']['url'];
//@todo wrong shortlinks automatically redirect to the amazon.com homepage...detect that
//@todo if we have no povider date we are banned for a certain time?
//only check on product pages containing xy. Example amazon has /dp/ in url for product pages
//header: location
if(array_search_partial($provider_data['product_page_url_string'], $provider_location) !== false ) {
$provider = "$provider product-page";
//$test = preg_match($na_regex, $provider_code, $matches, PREG_OFFSET_CAPTURE, 0);
if (preg_match($na_regex, $provider_code, $matches, PREG_OFFSET_CAPTURE, 0) == 1) {
$provider = "$provider not-avaliable ";
}
//$test = preg_match($error_regex, $provider_code, $matches, PREG_OFFSET_CAPTURE, 0);
if ( (preg_match($error_regex, $provider_code, $matches, PREG_OFFSET_CAPTURE, 0)) == 1 || $provider_header == 404) {
$provider = "$provider error ";
}
}
else {
$provider = "$provider non-product-page";
}
}
else {
$provider = 'already checked skipped ';
}
$items['url_checks'][$url_key][] = ['url' => $url, 'provider_url' => $provider_link, 'header' => $header, 'provider' => $provider];
//sleep after every link checked. Avoid hammering/throtteling bans
//need to sleep on a link we already checked
if($provider == 'already checked skipped ') {
$rand = 0;
}
else {
$rand = rand(5, 10);
}
$link_nr = $url_key+1;
$message = "
Process link number $link_nr of $link_count pages
Processed url: $url
Processed provider link: $provider_link
Result: $provider
Sleep $rand seconds
--------------------------
";
echo $message;
//full log
file_put_contents('full_log.txt', $message, FILE_APPEND);
sleep($rand);
}
}
}
$stop=1;
echo "Finished processing all urls:\r\n";
//create html code from results array
$html_log = generate_html_log($items);
//write log as html
//delete file if already exists because we arite an ew log every time
file_put_contents($results_dir, $html_log);
/**
* @param $arr
* @param $keyword
*
* Helper: Check a string if values of an array are present in teh string
*
* @return int|string
*/
function array_search_partial($arr, $haystack) {
$result = false;
foreach($arr as $index => $string) {
if (strpos($haystack, $string) !== FALSE) {
$result = true;
}
}
return $result;
}
/**
* extract links for an url schema
*
* @param $link_array
* @param $provider_schema
*/
function extract_provider_links($link_array, $provider_schema) {
$provider_links = [];
foreach ($link_array as $key => $link) {
foreach ($provider_schema as $pkey => $plink) {
if (strpos($link, $plink) !== FALSE) {
$provider_links[] = $link;
}
}
}
return $provider_links;
}
/**
* extract all links from a sitemap.xml
*
* @param $urlContent 'content of xml file'
*
* @return array
*/
function extract_xmlsitemap_links($urlContent) {
$urls = [];
if ($urlContent !== FALSE) {
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = FALSE;
$DomDocument->loadXML($urlContent);
$DomNodeList = $DomDocument->getElementsByTagName('url');
foreach ($DomNodeList as $url) {
$locs = $url->getElementsByTagName('loc');
$loc = $locs->item(0)->nodeValue;
$urls[] = $loc;
}
//save the urls we will check.
file_put_contents('url_log.txt', implode(PHP_EOL, $urls));
}
return $urls;
}
/**
* extract all links from a website
*/
function extract_html_links($urlContent) {
$urls = [];
if ($urlContent !== FALSE) {
$dom = new DOMDocument;
@$dom->loadHTML($urlContent);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$urls[] = $link->getAttribute('href');
}
}
return $urls;
}
/**
* @param $url
*
* @return mixed
*
* check an url, get header code
*/
function check_url($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING , "gzip"); //page may be gzipped? most servers do
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
$result['data'] = $data;
$result['headers'] = $headers;
return $result;
}
/**
* create html code for logfile from url checked items array
*
* @param $data
*
* @return string
*/
function generate_html_log($data) {
$html = '<link rel="stylesheet" href="style.css">';
$html .= '<link rel="stylesheet" href="../bootstrap/css/bootstrap.css">';
$html .= '<script src="../js/jquery-3.5.1.min.js"></script>';
$html .= "<script src='../js/custom.js'></script>";
$html .= "<button type='button' class='btn btn-primary hide-skipped'>Remove skipped</button>";
$html .= "<button type='button' class='btn btn-primary hide-200'>Hide 200</button>";
$html .= "<button type='button' class='btn btn-primary hide-301'>Hide 301</button>";
$html .= "<table class='table table-striped table-bordered table-hover table-sm'>
<thead>
<tr>
<th>Result Nr.</th>
<th>Titel</th>
<th>URL Check</th></tr>
</thead>
<tbody>";
$check_nr = 1;
foreach ($data['url_checks'] as $key => $items) {
$html .= "<tr>";
$html .= "<td>" . $check_nr . "</td>";
$check_nr++;
$html .= "<td><a href='" . $items[0]['url'] . "'>" . $items[0]['url'] . "</a></td>";
$html .= "<td><ul>";
foreach ($items as $ukey => $urldata) {
$classes = $urldata['provider'] . ' header-' . $urldata['header'];
$html .= "<li class='$classes'><a href='" . $urldata['provider_url'] . "' target='_blank'>" . $urldata['provider_url'] . "</a></li>";
}
$html .= "</ul></td>";
$html .= "</tr>";
}
$html .= "</tbody>";
$html .= "</table>";
return $html;
}