forked from rne1223/Joomla_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpiderTree.php
592 lines (516 loc) · 18.5 KB
/
SpiderTree.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<?php
class SpiderTree
{
/**
* Settings in order to modify curl
*/
public $ch; // will initialize curl handle in __construct
public $host; // contains the Host name eg: http://google.com -> host=google.com
public $parser;
public $strRootLink; // Passed link by the user
public $display = 0; // Flag to display errors
public $display_error = 0; // Flag to display errors
public $booMax = 0; // Flag to check if the max was reached
public $gotMenu = 0; // Flag to check if the menu has been retrived
public $intFetch = 6; // How many handles should be process at a time
public $intDepth = 0; // tracks depth as spider crawls
public $intMaxDepth = 2; // max number of pages to crawl
public $Tree = array(); // holds links to crawl
public $arrIDs = array('menu'=>'pbutts','content'=>'prg-cont'); // Hold selected IDs in order to search
public $arrUrlExt = array('','org','php','html','htm','com','edu');
public $arrScraped = array(); // holds links found so they won't be added again
public $arrLinksFound = array(); // holds links found so they won't be added again
public $arrErrors = array();
public $VAR_CURLOPT_FAILONERROR = 0; // if HTTP code > 300 still returns the page
public $VAR_CURLOPT_FOLLOWLOCATION = 1; // allow redirects
public $VAR_CURLOPT_RETURNTRANSFER = 1; // will return the page in a variable
public $VAR_CURLOPT_USERAGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
/*
* Constructor
* Sets the time and initiates curl
* @strInpuLink - input link from the user
*/
function __construct($strInputLink)
{
unset($this->arrLinksFound, $this->arrTree); // Clear the arrays
ob_start(); // to output faster
$this->parser = new DOMDocument();
$this->host = parse_url($strInputLink,PHP_URL_HOST); //contains our current host
$this->ch = curl_multi_init();
$this->strRootLink = $strInputLink;
$this->intDepth = 0;
$this->Tree = array($this->strRootLink);
$this->BuildTree($this->Tree);
}
function __destruct()
{
curl_multi_close($this->ch);
ob_end_flush();
}
/**
* Builds a tree like structure with one parent and multiple childs
*
* @arrInput -> passed in array holding parent links
*/
function BuildTree($arrInput)
{
if($this->intMaxDepth-1 < $this->intDepth)
{
$this->booMax = 1;
return;
}
if(empty($arrInput))
{
echo "<i><b>DONE</b></i>";
return;
}
if(!is_array($arrInput))
{
echo "<b>Error: input not allowed</b><br>";
return;
}
$arrData = $this->Pipe($arrInput);
$childs = array();
// Get the links to search
foreach ($arrData as $page)
{
$childs = array_merge($childs,$page['links']);
// $cont[] = array('url'=>$page['url'],'title'=>$page['title'],'html'=>$page['html']);
echo $page['url'].'<br>';
echo $page['html'];
echo '++++++++++++++++++<br>';
}
if(!empty($arrData))
$this->arrTree[] = $arrData;
// Clear some memory
unset($arrInput,$arrData);
++$this->intDepth;
$this->BuildTree($childs);
}
/**
* Gets child links inside an html page
*
* @Urls Array - links to obtain child links from
* @returns Array
*/
function Pipe($Urls)
{
$fetchedData = array();
$handles = array();
$max = count($Urls);
$len = $this->intFetch;
if($max < $len)
{
$handles = $this->addHandles($this->ch,$Urls);
$this->execHandles();
$fetchedData = array_merge($fetchedData,$this->getPages($handles));
}
else
{
for($start=0; $start < $max; $start+=$len)
{
$handles = $this->addHandles($this->ch,array_slice($Urls,$start,$len));
$this->execHandles();
$fetchedData = array_merge($fetchedData,$this->getPages($handles));
}
}
unset($max,$len,$handles,$Urls);
return $fetchedData;
}
/**
* This function retrives data from multiple pages
*
* @param Array Curl handles to retrive data from the web
* @return Array HTML pages with their data
**/
function getPages($arrHandles)
{
$pages = $output = array();
$html = $errortext = $url = '';
$error = -1;
$http_code = 0;
// Get pages from the web and process them
foreach($arrHandles as $handle)
{
$error = curl_errno($handle);
$errortext = curl_error($handle);
// If there weren't error retriving the page
if($error == 0)
{
$html = curl_multi_getcontent($handle);
$url = curl_getinfo($handle,CURLINFO_EFFECTIVE_URL);
$http_code = curl_getinfo($handle,CURLINFO_HTTP_CODE);
// If the status isn't 200 means that the page doesn't exist
if($http_code == 200)
{
// If it can be scrape then pull all links,images,html ect from url
if($this->accExt($url) && !$this->scraped($url))
{
$this->arrScraped[] = $url;
// Remove everything except the selected ID
if(!$this->gotMenu)
{
$page = $this->getPage($this->arrIDs['menu'],$html,$url);
$this->gotMenu = 1;
}
else
$page = $this->getPage($this->arrIDs['content'],$html,$url);
if(!empty($page))
$pages[] = $page; // Get pages from the page
} // If it can't be scrape but it already exist like a .pdf then just pull the info
else if(!$this->accExt($url) && $this->exist($url) && !$this->scraped($url))
{
$this->arrScraped[] = $url;
if($this->display)
{
echo "<pre> Parent: ====> $url<br></pre>";
ob_flush();
flush();
usleep(50000);
}
}
}
else
{
if($this->display)
{
echo "<pre><b>Error: $http_code </b> ====> <u>$url</u> doesn't exist<br></pre>";
ob_flush();
flush();
usleep(50000);
}
$this->arrErrors[$url] = "Error: $http_code ====> $url doesn't exist";
}
}
else
{
if($this->display )
{
echo "<pre><b>Error: $error</b> ====> <u>$url</u> <b>$errortext</b><br></pre>";
ob_flush();
flush();
usleep(50000);
}
$this->arrErrors[$url] = "Error: $error ====> $url $errortext";
}
// In order to clear memory
curl_multi_remove_handle($this->ch,$handle);
curl_close($handle);
}
unset($arrHandles);
return $pages;
}
/**
* This function retrives the data
*
* @return array
**/
function getPage($id,$HTML,$url)
{
$title = $this->getTitle($url);
if(empty($title))
$title = 'Home';
$html = $this->getHtmlById($id,$HTML);
if($this->display)
{
echo "<pre> Parent:$title ====> $url<br>";
ob_flush();
flush();
usleep(50000);
}
$Page = array();
$Page['url'] = $url;
$Page['title'] = $title;
$Page['html'] = $html;
$Page['links'] = $this->getLinks($html,$url,'a');
if($this->display)
{
echo "</pre>";
ob_flush();
flush();
usleep(50000);
}
return $Page;
}
/**
* Gets links of a certain tag
*
* @return array with
*/
function getLinks($HTML,$ParentUrl,$tag)
{
@$this->parser->loadHTML($HTML);
$arrLinks = array();
$path = $href = $host = '';
foreach($this->parser->getElementsByTagName($tag) as $link)
{
$host = parse_url($path,PHP_URL_HOST);
$ParentUrl = rtrim($ParentUrl,'/');
if ($tag == 'a')
{
$path = rtrim($link->getAttribute('href'),'/');
}
else if ($tag == 'img')
{
$path = rtrim($link->getAttribute('src'),'/');
}
// If scraping from another host
if (strpos($path,'http') !== false && $this->host != $host)
{
continue;
} // If it has our host don't do any string manipulation
else if (strpos($path,'http') !== false && $this->host == $host)
{
continue;
}
// Clean link from '#','?',javascript and other stuff
if (!empty($path))
$cleanHref = $this->cleanLink($path);
if (strpos($ParentUrl,"\n") !== false || strpos($path,"\n") !== false)
{
continue;
}
// If after being process nothing gets returned
if (!empty($cleanHref))
{
if ($cleanHref[0] != '/')
{
// Parent url can't contain extensions
// such as http://example.com/index.php
if(strpos($ParentUrl,'php') !== false)
$ParentUrl = dirname($ParentUrl);
$href = $ParentUrl.'/'.$cleanHref;
}
else
$href = $this->strRootLink.$cleanHref;
if (strpos($href,'..') !== false)
{
// We remove the root so it doesn't mess with the double back slashes from http://
$href = str_replace($this->strRootLink,'',$href);
$href = $this->realPath($href);
// We add it back to make it a comple url
$href = $this->strRootLink.$href;
}
if (!$this->exist($href))
{
if ($this->display)
{
echo "\tChild: ====> $href\n";
ob_flush();
flush();
usleep(50000);
}
$arrLinks[] = $href;
$this->arrLinksFound[] = $href;
}
}
}
return $arrLinks;
}
/**
* Clean links from unwanted stuff
*
* @return String if there is something to return Void otherwise
**/
function cleanLink($strLink)
{
if(strpos($strLink,' ') !==false)
$strLink = str_replace(' ','%20',$strLink);
// docs found in the menu -> this means that is under /student_orgs
// a hack for rop.mercedlearn.org's menu
if($this->intDepth == 0 && strpos($strLink,'docs') !== false &&
strpos($strLink,'for_teachers') === false)
{
$strLink = '/student_orgs/'.$strLink;
}
// Remove '#' '?' 'javascript:void()'
if( strpos($strLink,'#') !== false ||
strpos($strLink,'?') !== false ||
strpos($strLink,'javascript') !== false ||
strpos($strLink,'@') !== false ||
strpos($strLink,'index.php') !== false)
{
$strLink = dirname($strLink);
}
if(strlen($strLink) > 1)
return $strLink;
}
/**
* Returns html as an string
*
* @param id Id that we want to get the html from
* @return string Html inside the id
**/
function getHtmlById($id,$HTML)
{
$this->parser = new DOMDocument;
@$this->parser->loadHTML($HTML);
$html = $this->parser->saveXML($this->parser->getElementById($id));
$html = $this->cleanHtml($html);
return $html;
}
function cleanHtml($HTML)
{
@$this->parser->loadHTML($HTML);
$xpath = new DOMXPath($this->parser);
$query = "//div[@id='searchbar'] | //img[@class='page-header']/..";
$oldnodes = $xpath->query($query);
foreach($oldnodes as $node)
{
$node->parentNode->removeChild($node);
}
return $this->parser->saveXML();
}
/**
* This function retrives the title for an html page
*
* @param String HTML page
* @return String title of html page
**/
function getTitle($url)
{
$title = str_replace($this->strRootLink,'',$url);
$title = str_replace('.php','',$title); // remove .php
$title = str_replace('%20','_',$title); // replace with whites spaces
$title = str_replace('/',' ',$title);
$title = ucwords($title);
return $title;
}
/**
* Verifies a link
*
* @returns bool -> true if is acceptable false otherwise
*/
function accExt($strLink)
{
$found = false; // Flag to check for extensions
if(empty($strLink))
return $found;
$strExt = pathinfo($strLink,PATHINFO_EXTENSION);
if(!empty($this->arrUrlExt))
{
$search = array_flip($this->arrUrlExt);
if(isset($search[$strExt]))
return !$found; // return true if extension found
else
return $found;
}
else
return $found;
}
/**
* Converts a complicated path into the real path
*
* @param String Path that needs to be converted
* @return String Returns real path
*/
function realPath($path)
{
$out=array();
foreach(explode('/', $path) as $i=>$fold)
{
if ($fold=='' || $fold=='.')
continue;
if ($fold=='..' && $i>0 && end($out)!='..')
array_pop($out);
else
$out[]= $fold;
}
return ($path{0}=='/'?'/':'').join('/', $out);
}
/**
* Checks if a link already exist
*
* @return bool True if exist False otherwise
**/
function exist($strLink)
{
$found = false;
if(empty($strLink))
return $found;
if(!empty($this->arrLinksFound))
{
$search = array_flip($this->arrLinksFound);
if(isset($search[$strLink]))
return !$found;
else
return $found;
}
else
return $found;
}
/**
* Checks if a link already exist
*
* @return bool True if exist False otherwise
**/
function scraped($strLink)
{
$found = false;
if(empty($strLink))
return $found;
if(!empty($this->arrScraped))
{
$search = array_flip($this->arrScraped);
if(isset($search[$strLink]))
return !$found;
else
return $found;
}
else
return $found;
}
/**
* Create cUrl multi handles
*
* @param reference curlHandle is a reference to the multi_init
* @param array arrUrl is an array containing the urls that need handle
* @returns arrays of handles
*/
function addHandles(&$curlHandle,$arrUrl)
{
$arrHandles = array();
foreach($arrUrl as $url)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_FAILONERROR, $this->VAR_CURLOPT_FAILONERROR);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, $this->VAR_CURLOPT_FOLLOWLOCATION);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, $this->VAR_CURLOPT_RETURNTRANSFER);
if(strlen($this->VAR_CURLOPT_USERAGENT)>0)
curl_setopt($handle, CURLOPT_USERAGENT, $this->VAR_CURLOPT_USERAGENT);
curl_multi_add_handle($curlHandle,$handle);
$arrHandles[] = $handle;
}
return $arrHandles;
}
/**
* Executes handles
*
* @return void
**/
function execHandles()
{
// Processes each of the handles from $arrHandles
do {
$mrc = curl_multi_exec($this->ch,$active); // fetch pages in parallel
// usleep(rand(1,2)*1000); // reduce heavy load on the server, wait from 1 to 2 sec
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active and $mrc == CURLM_OK)
{
// wait for network
if (curl_multi_select($this->ch) != -1)
{
// pull in any new data, or at least handle timeouts
do {
$mrc = curl_multi_exec($this->ch, $active);
// usleep(rand(1,2)*1000); // reduce heavy load on the server, wait from 1 to 2 sec
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// In Case there was an error while cUrl was fetching pages
if ($mrc != CURLM_OK)
echo "<b>Error: Curl multi read error $mrc<b><br>";
}
}
?>