This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
functions_index.php
681 lines (463 loc) · 14.8 KB
/
functions_index.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?php
/**
* Indexing Functions
*/
//error_reporting(E_ALL ^E_NOTICE);
/**
* crawl a single page ($url) and return the links
* we skip - external and relative links and
* - excluded urls
* - images, scripts, stylesheets etc.
*/
function fc_crawler($id='') {
global $fc_base_url;
global $exclude_urls;
$allowed_extensions = array('.html','.htm','.php');
$links = array();
if($id == '') {
$url = '';
} else {
/* get url bei id */
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT page_url FROM pages WHERE page_id = :pid LIMIT 1";
$sth = $dbh->prepare($sql);
$sth->bindParam(':pid', $id, PDO::PARAM_STR);
$sth->execute();
$item = $sth->fetch(PDO::FETCH_ASSOC);
$sth = null;
$dbh = null;
$url = $item['page_url'];
}
if(substr($url, 0,1) == '/') {
$url = substr($url, 1,strlen($url));
}
$check_page = $fc_base_url.$url;
if(isset($_POST['start_index']) && $_POST['start_index'] != '') {
$check_page = $_POST['start_index'];
/* we are indexing our own pages only */
$check_len = strlen($fc_base_url);
if(substr($check_page, 0,$check_len) != $fc_base_url) {
return;
}
}
$get_html = fc_loadSourceCode($check_page);
$links = fc_get_links($get_html);
foreach($links as $link) {
$href = $link['url'];
/* we accept absolute urls only */
if(substr("$href",0,1) !== '/') {
continue;
}
/* if the last char issn't '/', check if we have an allowed extension */
if(substr("$href",-1) !== '/') {
$skip = TRUE;
foreach($allowed_extensions as $extension) {
$extension_length = strlen($extension);
$url_extension = substr($href, -$extension_length);
if(in_array($url_extension, $allowed_extensions)) {
$skip = FALSE;
}
}
if($skip === TRUE) {
continue;
}
}
/* skip the excluded urls */
$skip_exclude = false;
foreach($exclude_urls as $ex) {
if(stristr($href, $ex['item_url']) !== FALSE) {
$skip_exclude = true;
break;
}
}
if($skip_exclude === true) {
continue;
}
fc_add_url($href);
}
}
/**
* get the sourcecode from $url
*/
function fc_loadSourceCode($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'flatCoreBot/1.0 (+https://flatcore.org)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code>=200 && $http_code<300) {
return $data;
} else {
return false;
}
}
/**
* get al the links from $html
*/
function fc_get_links($html) {
$xml = new DOMDocument();
$xml->loadHTML($html,LIBXML_NOERROR);
$links = array();
foreach($xml->getElementsByTagName('a') as $link) {
$links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}
return $links;
}
/**
* Collect SEO relevant data
*/
function fc_get_html_data($html) {
$data = array();
$h1 = array();
$h2 = array();
$h3 = array();
$d = new DOMDocument();
$d->loadHTML($html,LIBXML_NOERROR);
foreach($d->getElementsByTagName('h1') as $item){
$h1[] = $item->textContent;
}
foreach($d->getElementsByTagName('h2') as $item){
$h2[] = $item->textContent;
}
foreach($d->getElementsByTagName('h3') as $item){
$h3[] = $item->textContent;
}
foreach($d->getElementsByTagName('a') as $item){
$l = $item->textContent;
$links[$l]['href'] = $item->attributes->getNamedItem('href')->nodeValue;
$links[$l]['title'] = $item->attributes->getNamedItem('title')->nodeValue;
}
foreach($d->getElementsByTagName('img') as $image){
$i = $image->attributes->getNamedItem('src')->nodeValue;
$imgs[$i]['alt'] = $image->attributes->getNamedItem('alt')->nodeValue;
$imgs[$i]['title'] = $image->attributes->getNamedItem('title')->nodeValue;
}
foreach($d->getElementsByTagName('meta') as $meta) {
if($meta->getAttribute('property')=='og:image'){
$data['page_thumbnail'] = $meta->getAttribute('content');
}
}
$img_str = '';
foreach($imgs as $k => $v) {
$img_str .= $k.'<|>'.$v['alt'].'<|>'.$v['title'].'<|-|>';
}
$data['img_str'] = substr($img_str, 0,-5);
$link_str = '';
foreach($links as $k => $v) {
$link_str .= $k.'<|>'.$v['title'].'<|>'.$v['href'].'<|-|>';
}
$data['link_str'] = substr($link_str, 0,-5);
$data['h1_str'] = implode("<|>", $h1);
$data['h2_str'] = implode("<|>", $h2);
$data['h3_str'] = implode("<|>", $h3);
return $data;
}
/**
* save cleaned page content
*/
function fc_update_page_index($id) {
global $fc_base_url;
global $exclude_items;
global $exclude_urls;
$time = time();
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "select page_url from pages where page_id = :id";
$sth = $dbh->prepare($sql);
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$sth->execute();
$item = $sth->fetch(PDO::FETCH_ASSOC);
$sth = null;
$dbh = null;
$url = $item['page_url'];
if(substr($url, 0,1) == '/') {
$url = substr($url, 1,strlen($url));
}
foreach($exclude_urls as $u) {
if($u['item_url'] == "/$url") {
echo '<div class="alert alert-danger">removed from index: '.$url.' (page defined as excluded)</div>';
fc_delete_url("/$url");
return;
}
}
$check_page = $fc_base_url.$url;
$get_html = fc_loadSourceCode($check_page);
if($get_html === false) {
/* this page isn't available anymore */
echo '<div class="alert alert-danger">removed from index: '.$url.' (page is not available)</div>';
fc_delete_url("/$url");
return;
}
$html_data = fc_get_html_data($get_html);
/* extract page infos */
preg_match("/<title>(.+)<\/title>/siU", $get_html, $matches);
$page_title = $matches[1];
$k = "<meta\s+name=['\"]??keywords['\"]??\s+content=['\"]??(.+)['\"]??\s*\/?>";
preg_match("/$k/siU", $get_html, $matches);
$page_keywords = $matches[1];
$d = "<meta\s+name=['\"]??description['\"]??\s+content=['\"]??(.+)['\"]??\s*\/?>";
preg_match("/$d/siU", $get_html, $matches);
$page_description = $matches[1];
/* cleaning */
$get_html = str_replace("\n\r"," ",$get_html);
$get_html = str_replace("\n"," ",$get_html);
$get_html = str_replace("\r"," ",$get_html);
$get_html = str_replace("/>"," />",$get_html);
foreach($exclude_items as $ex_item) {
$el = $ex_item['item_element']; // the element
$att = $ex_item['item_attributes']; // the .class or #id
$clean_att = substr($att, 1); // the attribute without "#" or "."
if(substr($att, 0, 1) == '.') {
$pattern = '~<'.$el.'([^>]*)(class\\s*=\\s*["\']'.$clean_att.'["\'])([^>]*)>(.*?)</'.$el.'>~si';
} else if(substr($att, 0, 1) == '#') {
$pattern = '~<'.$el.'([^>]*)(id\\s*=\\s*["\']'.$clean_att.'["\'])([^>]*)>(.*?)</'.$el.'>~si';
} else {
continue;
}
$get_html = preg_replace($pattern, '', $get_html);
}
$get_html = preg_replace("/<( )*script([^>])*>/i", "<script>", $get_html);
$get_html = preg_replace("/<script[^>]*>[\s\S]*?<\/script>/i", "", $get_html);
$get_html = preg_replace("/<style[^>]*>[\s\S]*?<\/style>/i", "", $get_html);
$get_html = preg_replace("/<iframe[^>]*>[\s\S]*?<\/iframe>/i", "", $get_html);
$get_html = preg_replace("/<head[^>]*>[\s\S]*?<\/head>/i", "", $get_html);
$get_html = preg_replace("/<meta([^>])*\/>/i", "", $get_html);
$get_html = preg_replace("/<td[^>]*>/", " ", $get_html);
$get_html = preg_replace("/<br[^>]*>/", " ", $get_html);
$get_html = preg_replace("/<[^>]*>/", "", $get_html);
$get_html = str_replace(" "," ",$get_html);
while(strpos($get_html,"\t")!==false) $get_html = preg_replace("/\t/", " ", $get_html);
while(strpos($get_html,' ')!==false) $get_html = preg_replace("/ /", " ", $get_html);
$get_html = trim($get_html);
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "UPDATE pages SET
page_content = :page_content,
page_title = :page_title, page_description = :page_description,
page_thumbnail = :page_thumbnail, page_keywords = :page_keywords, indexed_time = :indexed_time,
page_h1 = :page_h1, page_h2 = :page_h2, page_h3 = :page_h3,
page_images = :page_images, page_links = :page_links
WHERE page_id = :page_id";
$sth = $dbh->prepare($sql);
$sth->bindParam(':page_content', $get_html, PDO::PARAM_STR);
$sth->bindParam(':page_title', $page_title, PDO::PARAM_STR);
$sth->bindParam(':page_description', $page_description, PDO::PARAM_STR);
$sth->bindParam(':page_keywords', $page_keywords, PDO::PARAM_STR);
$sth->bindParam(':page_thumbnail', $html_data['page_thumbnail'], PDO::PARAM_STR);
$sth->bindParam(':page_id', $id, PDO::PARAM_STR);
$sth->bindParam(':page_h1', $html_data['h1_str'], PDO::PARAM_STR);
$sth->bindParam(':page_h2', $html_data['h2_str'], PDO::PARAM_STR);
$sth->bindParam(':page_h3', $html_data['h3_str'], PDO::PARAM_STR);
$sth->bindParam(':page_images', $html_data['img_str'], PDO::PARAM_STR);
$sth->bindParam(':page_links', $html_data['link_str'], PDO::PARAM_STR);
$sth->bindParam(':indexed_time', $time, PDO::PARAM_STR);
$cnt_changes = $sth->execute();
$dbh = null;
$end_time = time();
$return['duration'] = $end_time-$time;
$return['url'] = $url;
return $return;
}
/**
* update the page index of the (num) oldest pages
*/
function fc_update_bulk_page_index($num=5) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT page_id FROM pages ORDER BY indexed_time ASC LIMIT $num";
$items = $dbh->query($sql);
$items = $items->fetchAll(PDO::FETCH_ASSOC);
$dbh = null;
foreach($items as $item) {
$update = fc_update_page_index($item['page_id']);
}
}
/**
* crawl for links in the (num) oldest pages
*/
function fc_crawler_bulk($num=5) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT page_id FROM pages ORDER BY indexed_time ASC LIMIT $num";
$items = $dbh->query($sql);
$items = $items->fetchAll(PDO::FETCH_ASSOC);
$dbh = null;
foreach($items as $item) {
$update = fc_crawler($item['page_id']);
}
}
/**
* update the page index
* this function is called by pages.edit.php if you save a plublic or ghost page
* if page is in index, update contents
* if not, add as new entry
*/
function fc_update_or_insert_index($permalink) {
$url = '/'.$permalink;
$dbh = new PDO("sqlite:".INDEX_DB);
$sql_check = "select page_id from pages where page_url = :url LIMIT 1";
$sth = $dbh->prepare($sql_check);
$sth->bindParam(':url', $url, PDO::PARAM_STR);
$sth->execute();
$entry = $sth->fetch(PDO::FETCH_ASSOC);
$sth = null;
$dbh = null;
if($entry['page_id'] == '') {
/* we have a new entry */
} else {
/* update entry */
$update = fc_update_page_index($entry['page_id']);
}
}
/**
* Add URL to list
* check for existing entries
*/
function fc_add_url($url) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql_check = "select count(1) from pages where page_url = :url";
$sth = $dbh->prepare($sql_check);
$sth->bindParam(':url', $url, PDO::PARAM_STR);
$sth->execute();
$cnt_entries = $sth->fetch(PDO::FETCH_NUM);
if($cnt_entries[0] < 1) {
echo '<div class="alert alert-success">Add to index: '.$url.'</div>';
}
$page_id = md5($url);
$sql_insert = "INSERT INTO pages (
docid, page_id, page_url
) VALUES (
NULL, :page_id, :url ) ";
$std = $dbh->prepare($sql_insert);
if($cnt_entries[0] < 1) {
$url = htmlentities($url);
$std->bindParam(':url', $url, PDO::PARAM_STR);
$std->bindParam(':page_id', $page_id, PDO::PARAM_STR);
$add = $std->execute();
}
$dbh = null;
}
/**
* delete page by id from pages table
*/
function fc_remove_page_from_index($id) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "DELETE FROM pages WHERE page_id = :id";
$sth = $dbh->prepare($sql);
$sth->bindParam(':id', $id, PDO::PARAM_STR);
$cnt_changes = $sth->execute();
$dbh = null;
}
/**
* delete URL from pages table
*/
function fc_delete_url($url) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "DELETE FROM pages WHERE page_url = :page_url";
$sth = $dbh->prepare($sql);
$sth->bindParam(':page_url', $url, PDO::PARAM_STR);
$cnt_changes = $sth->execute();
$dbh = null;
}
/**
* get entries from pages table
*/
function fc_get_indexed_pages() {
$dbh = new PDO("sqlite:".INDEX_DB);
$limit = (int) $_SESSION['limit_index_items'];
if($limit == 0) {
$sql = "SELECT * FROM pages ORDER BY indexed_time ASC";
} else {
$sql = "SELECT * FROM pages ORDER BY indexed_time ASC LIMIT 0,$limit";
}
$items = $dbh->query($sql);
$items = $items->fetchAll(PDO::FETCH_ASSOC);
$dbh = null;
return $items;
}
/**
* get the number of entries from pages table
* return int
*/
function fc_get_nbr_of_indexed_pages() {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT COUNT(*) AS nbr FROM pages";
$items = $dbh->query($sql);
$items = $items->fetch(PDO::FETCH_ASSOC);
$dbh = null;
return (int) $items['nbr'];
}
/**
* delete from excludes (elements | url) by id
*/
function fc_delete_excludes($id) {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "DELETE FROM excludes WHERE item_id = :item_id";
$sth = $dbh->prepare($sql);
$sth->bindParam(':item_id', $id, PDO::PARAM_INT);
$cnt_changes = $sth->execute();
$dbh = null;
}
/**
* write exclude elements
* elements -> div, span, footer, aside ...
* attribute -> id or class name f.e. #footer #sidebar
*/
function fc_write_exclude_elements($element,$attribute) {
$element = strip_tags($element);
$attribute = strip_tags($attribute);
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "INSERT INTO excludes (
item_id, item_element, item_attributes
) VALUES (
NULL, :item_element, :item_attributes ) ";
$sth = $dbh->prepare($sql);
$sth->bindParam(':item_element', $element, PDO::PARAM_STR);
$sth->bindParam(':item_attributes', $attribute, PDO::PARAM_STR);
$cnt_changes = $sth->execute();
$dbh = null;
}
/**
* get exclude elements
*/
function fc_get_exclude_elements() {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT * FROM excludes WHERE item_url IS NULL";
$items = $dbh->query($sql);
$items = $items->fetchAll(PDO::FETCH_ASSOC);
$dbh = null;
return $items;
}
/**
* write exclude urls
*/
function fc_write_exclude_url($url) {
$url = strip_tags($url);
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "INSERT INTO excludes (
item_id , item_url
) VALUES (
NULL, :item_url ) ";
$sth = $dbh->prepare($sql);
$sth->bindParam(':item_url', $url, PDO::PARAM_STR);
$cnt_changes = $sth->execute();
$dbh = null;
}
/**
* get exclude urls
*/
function fc_get_exclude_urls() {
$dbh = new PDO("sqlite:".INDEX_DB);
$sql = "SELECT * FROM excludes WHERE item_url IS NOT NULL";
$items = $dbh->query($sql);
$items = $items->fetchAll(PDO::FETCH_ASSOC);
$dbh = null;
return $items;
}
?>