forked from hedii/php-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.php
620 lines (476 loc) · 13.5 KB
/
Crawler.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
set_time_limit(86400); // 24 hours
ignore_user_abort(true);
require_once ('MysqliDb.php');
/**
* Crawler class
*/
class Crawler {
/**
* db
*
* @var mixed
* @access private
*/
private $db;
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
// database connection
$this->db = new MysqliDb ('localhost', 'root', 'root', 'php-crawler');
}
/**
* crawl_urls function.
*
* @access public
* @param mixed $url
* @return void
*/
public function crawl_urls($url) {
// insert the first url in the database
$this->insert_url($url);
// loop through urls while there are non visited urls in the database
while ($this->there_are_non_visited_urls_in_database() === true) {
// get the first url that has not been not visited yet
$url = $this->get_one_non_visited_url();
// find urls on this url
$this->find_url($url);
}
}
/**
* crawl_emails function.
*
* @access public
* @return void
*/
public function crawl_emails() {
// loop through urls while there are non crawled for email urls in the database
while ($this->there_are_non_crawled_for_email_urls_in_database() === true) {
// get (int)x urls that has not been crawled for emails yet
$url = $this->get_x_non_crawled_for_email_url();
// find emails on this url
$this->find_email($url);
}
}
/**
* find_url function.
*
* @access private
* @param mixed $url
* @return void
*/
private function find_url($url) {
// set up ob for displaying results in real time
ini_set('output_buffering', 'off');
while (@ob_end_flush());
ini_set('implicit_flush', true);
ob_implicit_flush(true);
ob_start();
// initialize curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ignore hhtp headers
curl_setopt($ch, CURLOPT_HEADER, 0);
// maximum time the request is allowed to take
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// execute curl
$datas = curl_exec($ch);
// show result on the browser
echo '<strong class="text-green">VISITED URL: </strong>' . $url . str_pad("", 4096, " ") . " <br>";
ob_flush();
flush();
// update the first url to visited (we have data from it)
$this->update_url($url);
// close curl session
curl_close($ch);
// find urls on the datas variable
$pattern = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
preg_match_all($pattern, $datas, $urls);
// for every url find on the datas variable
foreach ($urls[0] as $url) {
// we want only urls that are equal ore shorter than 1000 characteres
if (strlen($url) <= 1000) {
// we do not want to crawl file urls
if (
$this->string_ends_with($url, '.jpg') === false &&
$this->string_ends_with($url, '.png') === false &&
$this->string_ends_with($url, '.gif') === false &&
$this->string_ends_with($url, '.tiff') === false &&
$this->string_ends_with($url, '.bmp') === false &&
$this->string_ends_with($url, '.pdf') === false &&
$this->string_ends_with($url, '.svg') === false &&
$this->string_ends_with($url, '.fla') === false &&
$this->string_ends_with($url, '.swf') === false &&
$this->string_ends_with($url, '.css') === false &&
$this->string_ends_with($url, '.js') === false
) {
// we don't want to store already stored urls on the database
if ($this->url_exists_in_database($url) === false) {
// we don'nt want to store anchors like http://example.com/index.php/#comment-123456
if ($this->string_contains($url, '/#') === false) {
// send url to the database
$this->insert_url($url);
}
}
}
}
}
}
/**
* find_email function.
*
* @access private
* @param mixed $url
* @return void
*/
private function find_email($url) {
// set up ob for displaying results in real time
ini_set('output_buffering', 'off');
while (@ob_end_flush());
ini_set('implicit_flush', true);
ob_implicit_flush(true);
ob_start();
// if there are several urls, we want to run curl multi
if (is_array($url)) {
// the number of urls
$count = count($url);
for ($i = 0; $i < $count; $i++) {
${'ch' . $i} = curl_init($url[$i]['url']);
curl_setopt(${'ch' . $i}, CURLOPT_RETURNTRANSFER, true);
curl_setopt(${'ch' . $i}, CURLOPT_HEADER, 0);
curl_setopt(${'ch' . $i}, CURLOPT_TIMEOUT, 5);
}
$mh = curl_multi_init();
for ($i = 0; $i < $count; $i++) {
curl_multi_add_handle($mh, ${'ch' . $i});
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
for ($i = 0; $i < $count; $i++) {
// get the content on the url
${'data' . $i} = curl_multi_getcontent(${'ch' . $i});
// show the url to the brawser
echo '<strong class="text-green">CRAWLED URL: </strong>' . $url[$i]['url'] . str_pad("", 4096, " ") . " <br>";
ob_flush();
flush();
// update url to visited on the database
$this->update_crawled_for_email_url($url[$i]['url']);
// find emails on the data from the url
$pattern = '#\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b#i';
preg_match_all($pattern, ${'data' . $i}, $emails);
foreach ($emails[0] as $email) {
// we want only emails that are equal ore shorter than 255 characteres
if (strlen($email) <= 255) {
// we do not want @2x retina images or weird things
if (
$this->string_ends_with($email, '.jpg') === false &&
$this->string_ends_with($email, '.png') === false &&
$this->string_ends_with($email, '.gif') === false &&
$this->string_ends_with($email, '.tiff') === false &&
$this->string_ends_with($email, '.bmp') === false &&
$this->string_ends_with($email, '.pdf') === false &&
$this->string_ends_with($email, '.svg') === false &&
$this->string_ends_with($email, '.fla') === false &&
$this->string_ends_with($email, '.swf') === false &&
$this->string_ends_with($email, '.css') === false &&
$this->string_ends_with($email, '.js') === false
) {
// we don't want to store already stored emails on the database
if ($this->email_exists_in_database($email) === false) {
// send email to the database
$this->insert_email($email);
// show found email on the browser
echo '<strong class="text-red">EMAIL FOUND: </strong>' . $email;
echo str_pad("", 4096, " ");
echo " <br>";
}
}
}
}
}
} else {
// url is a single url, initialize curl (not curl multi)
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ignore hhtp headers
curl_setopt($ch, CURLOPT_HEADER, 0);
// execute curl
$datas = curl_exec($ch);
// show result on the browser
echo '<strong class="text-green">CRAWLED URL: </strong>' . $url . str_pad("", 4096, " ") . " <br>";
ob_flush();
flush();
// update the first url to visited (we have data from it)
$this->update_crawled_for_email_url($url);
// close curl session
curl_close($ch);
// find emails on the datas variable
$pattern = '#\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b#i';
preg_match_all($pattern, $datas, $emails);
// for every email find on the datas variable
foreach ($emails[0] as $email) {
// we do not want @2x retina images or weird things
if (
$this->ends_with($email, '.jpg') === false &&
$this->ends_with($email, '.png') === false &&
$this->ends_with($email, '.gif') === false &&
$this->ends_with($email, '.tiff') === false &&
$this->ends_with($email, '.bmp') === false &&
$this->ends_with($email, '.pdf') === false &&
$this->ends_with($email, '.svg') === false &&
$this->ends_with($email, '.fla') === false &&
$this->ends_with($email, '.swf') === false &&
$this->ends_with($email, '.css') === false &&
$this->ends_with($email, '.js') === false
) {
// we don't want to store already stored emails on the database
if ($this->email_exists_in_database($email) === false) {
// send email to the database
$this->insert_email($email);
// show found email on the browser
echo '<strong class="text-red">EMAIL FOUND: </strong>' . $email;
echo str_pad("",2048," ");
echo " <br>";
}
}
}
}
ob_flush();
flush();
}
/**
* insert_url function.
*
* @access private
* @param mixed $url
* @return bool
*/
private function insert_url($url) {
$data = array(
'url' => $url,
'date' => date('Y-m-j H:i:s'),
);
if ($this->db->insert('urls', $data)) {
return true;
}
//echo 'insert failed: ' . $db->getLastError();
return false;
}
/**
* insert_email function.
*
* @access private
* @param mixed $email
* @return bool
*/
private function insert_email($email) {
$data = array(
'email' => $email,
'date' => date('Y-m-j H:i:s'),
);
if ($this->db->insert('emails', $data)) {
return true;
}
//echo 'insert failed: ' . $db->getLastError();
return false;
}
/**
* update_url function.
*
* @access private
* @param mixed $url
* @return bool
*/
private function update_url($url) {
$data = array(
'visited' => '1'
);
$this->db->where('url', $url);
if ($this->db->update('urls', $data)) {
return true;
}
//echo 'update failed: ' . $db->getLastError();
return false;
}
/**
* update_crawled_for_email_url function.
*
* @access private
* @param mixed $url
* @return bool
*/
private function update_crawled_for_email_url($url) {
$data = array(
'email_visited' => '1'
);
$this->db->where('url', $url);
if ($this->db->update('urls', $data)) {
return true;
}
//echo 'update failed: ' . $db->getLastError();
return false;
}
/**
* url_exists_in_database function.
*
* @access private
* @param mixed $url
* @return bool
*/
private function url_exists_in_database($url) {
$this->db->where('url', $url);
$count = $this->db->getValue('urls', 'count(*)');
if ($count > 0) {
return true;
}
return false;
}
/**
* email_exists_in_database function.
*
* @access private
* @param mixed $email
* @return bool
*/
private function email_exists_in_database($email) {
$this->db->where('email', $email);
$count = $this->db->getValue('emails', 'count(*)');
if ($count > 0) {
return true;
}
return false;
}
/**
* there_are_non_visited_urls_in_database function.
*
* @access private
* @return bool
*/
private function there_are_non_visited_urls_in_database() {
$this->db->where('visited', '0');
$count = $this->db->getValue('urls', 'count(*)');
if ($count > 0) {
return true;
}
return false;
}
/**
* there_are_non_crawled_for_email_urls_in_database function.
*
* @access private
* @return bool
*/
private function there_are_non_crawled_for_email_urls_in_database() {
$this->db->where('email_visited', '0');
$count = $this->db->getValue('urls', 'count(*)');
if ($count > 0) {
return true;
}
return false;
}
/**
* get_one_non_visited_url function.
*
* @access private
* @return string
*/
private function get_one_non_visited_url() {
$this->db->where('visited', '0');
$result = $this->db->getOne('urls');
return $result['url'];
}
/**
* get_one_non_crawled_for_email_url function.
*
* @access private
* @return string
*/
private function get_one_non_crawled_for_email_url() {
$this->db->where('email_visited', '0');
$result = $this->db->getOne('urls');
return $result['url'];
}
/**
* get_x_non_crawled_for_email_url function.
*
* @access private
* @param int $number (default: 2)
* @return array
*/
private function get_x_non_crawled_for_email_url($number = 2) {
$this->db->where('email_visited', '0');
$result = $this->db->get('urls', $number);
return $result;
}
/**
* string_ends_with function.
*
* @access private
* @param mixed $haystack
* @param mixed $needle
* @return bool
*/
private function string_ends_with($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
/**
* string_contains function.
*
* @access private
* @param mixed $haystack
* @param mixed $needle
* @return bool
*/
private function string_contains($haystack, $needle) {
if (strpos($haystack, $needle) !== false) {
return true;
}
return false;
}
/**
* get_total_url function.
*
* @access public
* @return float
*/
public function get_total_url() {
return $this->db->rawQuery('SELECT COUNT(id) FROM urls')[0]['COUNT(id)'];
}
/**
* get_visited_url function.
*
* @access public
* @return float
*/
public function get_visited_url() {
return $this->db->rawQuery('SELECT COUNT(id) FROM urls WHERE visited=1')[0]['COUNT(id)'];
}
/**
* get_total_email function.
*
* @access public
* @return float
*/
public function get_total_email() {
return $this->db->rawQuery('SELECT COUNT(id) FROM emails')[0]['COUNT(id)'];
}
/**
* get_crawled_for_email_url function.
*
* @access public
* @return float
*/
public function get_crawled_for_email_url() {
return $this->db->rawQuery('SELECT COUNT(id) FROM urls WHERE email_visited=1')[0]['COUNT(id)'];
}
}