-
Notifications
You must be signed in to change notification settings - Fork 5
/
parsemx.php
2701 lines (2349 loc) · 88.3 KB
/
parsemx.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ParseMX - fast web data retrieval functions
*
* @author: MaxD - max@bukrek.net
* @version: 1.08
*/
$replace_file = 'replace.txt';
// ======================== MySQL
// Connect and work with MySQL databases with less code possible. Databases are considered UTF-8.
// If database doesn't exist, it is created automatically
$q_database = "parsemx";
$q_user = "root";
$q_password = "root";
$q_server = "localhost";
$q_port = 3306;
// Execute query, returns array of result rows. Connects to database, if not connected.
// If result rows consist of only one column, you will get just an array of this column values.
function qq($query) {
global $q_connection;
if (!$q_connection) q_connect();
$result = $q_connection->query($query);
if (!$q_connection->errno) {
if ($result instanceof \mysqli_result) {
$data = array();
while ($row = $result->fetch_assoc()) {
if (count($row)==1) $row = reset($row);
$data[] = $row;
}
$result->close();
re($data);
return $data;
} else {
return re(true);
}
} else {
if ($q_connection->errno == 2006) {
q_disconnect();
$data = qq($query);
re($data);
return $data;
}
xwarn("SQL Error: ".$q_connection->error);
xnotice("<font color='grey'>SQL Query:</font> ".$query);
die;
}
}
// Execute query, returns first row from result. Connects to database, if not connected.
// If result row consist of only one column, you will get just value of this column.
function q($query) {
$r = qq($query);
if (is_array($r)) $r = reset($r);
else $r = false;
re($r); return $r;
}
// Escapes text and adds '' to it.
function q_escape($text) {
global $q_connection;
if (!$q_connection) return "'".str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $text)."'";
return "'".$q_connection->real_escape_string($text)."'";
}
// Returns number of last query affected rows
function q_affected() {
global $q_connection;
return re($q_connection->affected_rows);
}
// Returns id of the entity inserted by last query
function q_last_id() {
global $q_connection;
return re($q_connection->insert_id);
}
// q_connect and q_disconnect are not meant be called directly, for internal use
function q_connect() {
global $q_connection;
if ($q_connection) return;
global $q_database, $q_user, $q_password, $q_server, $q_port;
$q_connection = new mysqli($q_server, $q_user, $q_password, $q_database, $q_port);
if ($q_connection->connect_errno) {
if ($q_connection->connect_errno == 1049) {
// Database doesn't exists, trying to create.
xnotice("Creating database <b>$q_database</b>...");
$q_connection = new mysqli($q_server, $q_user, $q_password, false, $q_port);
q("CREATE DATABASE `$q_database` CHARACTER SET utf8 COLLATE utf8_general_ci");
q("USE `$q_database`");
} else die;
}
$q_connection->set_charset("utf8");
q("SET SQL_MODE = ''");
re();
}
function q_disconnect() {
global $q_connection;
if (!$q_connection) return;
$q_connection->close();
$q_connection = null;
re();
}
// ======================== UTF-8 String Functions
// TODO: Fallback when MB_STRING not installed
function upcase($text) {
$text = mb_strtoupper($text, 'UTF-8');
re($text); return $text;
}
function lowcase($text) {
$text = mb_strtolower($text, 'UTF-8');
re($text); return $text;
}
// This functions are equivalents of regular PHP functions
function stripos8($haystack, $needle, $offset = 0) {
return mb_stripos($haystack, $needle, $offset, "UTF-8");
}
function strlen8($text) {
return mb_strlen($text, 'UTF-8');
}
function substr8($string, $start, $length) {
return mb_substr($string, $start, $length, 'UTF-8');
}
// ======================== Script Execution Control
// Indicates that your script is live and gives it another 5 minutes (by default) to execute.
// Call it from some long-running cycles
function script_live() {
static $last_live;
if ($last_live>time()-60) return;
$last_live = time();
global $mx_check_script_duplicate_name, $mx_script_timeout_mins;
if (!$mx_check_script_duplicate_name) $mx_script_timeout_mins = 5;
else mx_config_set(g('mx_check_script_duplicate_name') . "_runtime", time());
set_time_limit(g('mx_script_timeout_mins')*60-20);
}
// If another instance of your script is already working, this Function will finish current script.
// Call it at the beginning of your script, if you are invoking it with CRON.
function script_check_duplicate($name = false, $timeout_mins = 5 ) {
$timeout = $timeout_mins * 60;
if (!$name) $name = "parsemx";
$pid = mx_config_get($name . "_pid");
if ($pid)
if (!posix_getsid($pid)) $pid = false;
if ($pid and $timeout) {
$time = mx_config_get($name . "_runtime");
if ($time < time() - $timeout) {
// Kill old copy
posix_kill($pid,9);
$pid = false;
}
}
if ($pid) die("Script _$name already working, exiting.");
mx_config_set($name . "_pid", getmypid());
if ($timeout) mx_config_set($name . "_runtime", time());
global $mx_check_script_duplicate_name, $mx_script_timeout_mins;
$mx_check_script_duplicate_name = $name;
$mx_script_timeout_mins = $timeout_mins;
ignore_user_abort(true);
}
// ======================== Config Values Storage
$mx_config_file = 'config.mx';
// Set config value, $value may be any type of variable
function mx_config_set($key, $value = false) {
if ($value===false) $value = 0;
elseif ($value===true) $value = 1;
if (is_string($value)) $value = '"'.$value.'"';
elseif (!is_numeric($value)) $value = 'unserialize("'.serialize($value).'")';
global $mx_config_file;
$lines = @file($mx_config_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$found = false;
if ($lines)
foreach ($lines as $i=>$line) {
$name = inside("$", "=",$line);
if ($name!=$key) continue;
$found = true;
if ($value) $lines[$i] = '$'.$name.' = '.$value.';';
else unset($lines[$i]);
}
if (!$found and $value) $lines[] = '$'.$key.' = '.$value.';';
file_put_contents($mx_config_file, implode("\n",$lines));
re();
}
// Get config value
function mx_config_get($key) {
global $mx_config_file;
$lines = @file($mx_config_file, FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
if ($lines)
foreach ($lines as $i=>$line) {
$name = inside("$", "=",$line);
if ($name!=$key) continue;
$value = inside("=","",$line);
if (substr($value,-1)==";") $value = substr($value,0,-1);
if (substr($value,0,13)=='unserialize("') $value = unserialize(substr($value,13,-2));
elseif (substr($value,0,1)=='"') $value = substr($value,1,-1);
re($value); return $value;
}
re(false); return false;
}
// ======================== Hash Cache
$hash_cache_folder = "cache";
$hash_cache_maxtime = 2 /* Hours */ * 60 * 60;
define('hash_cache_delimeter','@#$hash@@*%');
function save_hash_cache($key, $data) {
global $hash_cache_folder;
if (!file_exists($hash_cache_folder)) mkdir($hash_cache_folder);
$free_space = @disk_free_space($hash_cache_folder);
if ($free_space>1 && $free_space<500*1024*1024) return; // Do not save cache if free space is less then 500 Mb
$hash = md5($key);
$folder = $hash_cache_folder."/".substr($hash,0,3);
$file = $folder."/c".substr($hash,3);
if (!file_exists($folder)) mkdir($folder);
file_put_contents($file,$key.hash_cache_delimeter.$data);
}
function load_hash_cache($key) {
global $hash_cache_folder, $hash_cache_maxtime;
if (!file_exists($hash_cache_folder)) mkdir($hash_cache_folder);
$hash = md5($key);
$folder = $hash_cache_folder."/".substr($hash,0,3);
$file = $folder."/c".substr($hash,3);
if (!file_exists($folder)) mkdir($folder);
$time = microtime(true);
if (filemtime($folder)<$time-10*60) {
// Lets delete old files
$time -= $hash_cache_maxtime;
foreach (glob($folder."/c*") as $ofile)
if (filemtime($ofile) < $time) @unlink($ofile);
touch($folder);
}
if (!file_exists($file)) return false;
$data = file_get_contents($file);
$p = strpos($data,hash_cache_delimeter);
$key2 = substr($data,0,$p);
if ($key!==$key2) return false;
$data = substr($data,$p+strlen(hash_cache_delimeter));
return $data;
}
function clear_hash_cache() {
global $hash_cache_folder;
foreach (glob($hash_cache_folder."/*") as $folder) {
foreach (glob($folder . "/c*") as $ofile) @unlink($ofile);
rmdir($folder);
}
}
// ======================== MX_HTTP
$http_proxies_file = 'proxies.mx'; // File to save proxies list
$http_cookies_file = 'cookies.txt';
$http_curl_timeout = 20;
// Global result vars
$http_code = 200; // Last HTTP operation code
$http_html = "";
$http_user_agent = false; // FALSE = Google bot TRUE = latest Chrome
$http_headers = array();
$http_cookies = false;
$http_referer = false;
$http_url_base = false;
$http_auth = false;
$http_proxy = false;
$http_content_type = false;
$http_encoding = false;
$mx_http_debug_proxies = false;
$http_use_proxies = false;
$http_cache = false;
// Get URL contents and return it
function http_get($url)
{
global $http_code, $http_ohtml, $http_html;
// Local file code
$http_code = 200;
if (!$url) { re(false); return false; }
if (strpos($url,'://')===false) { // Local file specified
if (file_exists($url)) {
$http_html = file_get_contents($url);
$http_ohtml = $http_html;
set_source($http_html);
re($http_html); return $http_html;
}
$http_code = 404;
re(false); return false;
}
$url = if_inside('', '#', $url);
// Retry and cache code
if (substr($url,0,1)!="%") {
if (g('http_cache')) {
$data = load_hash_cache($url);
if ($data) {
$http_code = 200;
$http_ohtml = $data;
$http_html = flatty_html($data);
global $http_url_base;
$base = inside("href=","",inside("<base", ">", $http_html));
if ($base) $base = substr($base,1,-1);
else $base = $url;
$base = if_inside("", "?",$base);
if ($p=strrpos($base, '/')) $base = substr($base, 0, $p);
if ($base) $http_url_base = $base."/";
xlog("Fetching <a rel='nofollow' href='$url' target='_blank'>$url</a> (cache)");
if (g('debug_nesting_level') and ini_get('display_errors')) { // Write result to output
mx_debug_fetch_result($http_html);
}
set_source($http_html);
re($http_html); return $http_html;
}
}
$n=3;
if (strpos($url,'yandex.ru')) $n=10;
for ($i=0;$i<$n;$i++) {
$http_html = http_get("%".$url);
if ($http_html || $http_code==404) { re($http_html); return $http_html; }
}
re(false); return false;
}
$url = substr($url,1); // Remove % marker
// Proxy code
global $http_proxy, $http_proxies_file;
if (g('http_use_proxies') and !$http_proxy)
if (! ($http_proxy = mx_config_get("proxy")) ) {
clearstatcache();
if (!file_exists($http_proxies_file) or (filemtime($http_proxies_file)<microtime(true)-24*60*60)) {
xlog('Receiving fresh proxies list...');
$proxies = @http_curl("http://parsemx.com/getmxp.php","url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",$http_proxies_file);
if ($proxies) $proxies = file_get_contents($http_proxies_file);
if ($proxies) xsuccess("Received ".round(strlen($proxies)/6). " proxies.");
}
$proxies = file_get_contents($http_proxies_file);
$n = (int) (strlen($proxies)/6);
// Testproxy URL
$proxy_url = $url;
if (strpos($url,'aliexpress.com')) $proxy_url = 'http://www.aliexpress.com/activities/';
elseif (strpos($url,'translate.yandex.net/api/v1')) $proxy_url = 'http://translate.yandex.net/api/v1/tr.json';
elseif ($p=strpos($url,'/',8)) $proxy_url=substr($url,0,$p+1);
while (!$http_proxy) {
xlog("Checking proxies...");
// Get 20 proxies
$xproxies = array();
for ($i=0;$i<20;$i++) {
$p = rand(0,$n-1)*6;
$proxy = ord($proxies[$p]).".".ord($proxies[$p+1]).".".ord($proxies[$p+2]).".".ord($proxies[$p+3]).":".(ord($proxies[$p+4])+ord($proxies[$p+5])*256);
$xproxies[] = $proxy;
}
$http_proxy = testproxies($xproxies,$proxy_url);
if ($http_proxy) {
xlog("Using proxy: ".$http_proxy);
mx_config_set("proxy", $http_proxy);
break;
}
}
}
$http_html = http_curl($url);
$http_ohtml = $http_html;
if (!$http_html) { re(false); return false; }
if (g('http_cache')) {
save_hash_cache($url,$http_html);
}
$http_html=flatty_html($http_html);
set_source($http_html);
re($http_html); return $http_html;
}
function http_post($url, $data) {
global $http_html, $http_ohtml;
$http_html = http_curl($url,$data);
$http_ohtml = $http_html;
if (!$http_html) { re(false); return false; }
$http_html=flatty_html($http_html);
set_source($http_html);
re($http_html); return $http_html;
}
function http_ajax($url, $data) {
global $http_html, $http_ohtml, $http_headers;
$save_headers = $http_headers;
if (empty($http_headers["X-Requested-With"]))
$http_headers["X-Requested-With"] = "XMLHttpRequest";
$http_html = http_curl($url,$data);
$http_headers = $save_headers;
$http_ohtml = $http_html;
if (!$http_html) { re(false); return false; }
if ($http_html[0] == "{" || $http_html[0] == "[")
return re(json_decode($http_html));
$http_html=flatty_html($http_html);
set_source($http_html);
re($http_html); return $http_html;
}
// Fetch file into folder and return its name if successful. $access_path is resulting path addition
function http_get_file($url, $save_path= '.', $access_path = false)
{
if (substr($save_path,-1)!="/") {
$path_parts = pathinfo($save_path);
$save_path = $path_parts['dirname'] . '/';
$name = $path_parts['filename'];
$ext = @$path_parts['extension'];
} else {
$name = false;
$ext = false;
}
if (!file_exists($save_path)) mkdir($save_path, 0777, true);
if (!$name) {
$l = parse_url($url);
$name = stristr($l['path'], "/");
$path_parts = pathinfo($name);
$name = $path_parts['filename'];
$ext = $path_parts['extension'];
}
if (!$name) $name = 'file';
if (!$ext) $ext = 'jpg';
$name = translit($name);
$file = $save_path."file".random(10000000).".tmp";
if (g('http_cache')) {
global $http_code, $http_content_type;
$data = load_hash_cache($url);
if ($data) {
$http_code = 200;
$data = explode(hash_cache_delimeter,$data);
$http_content_type = $data[0];
file_put_contents($file, $data[1]);
xlog("Fetching <a rel='nofollow' href='$url' target='_blank'>$url</a> (cache)");
if (g('debug_nesting_level') and ini_get('display_errors')) { // Write result to output
mx_debug_fetch_result($url);
}
} else {
http_curl($url, '', $file);
if (file_exists($file))
save_hash_cache($url,$http_content_type.hash_cache_delimeter.file_get_contents($file));
}
} else http_curl($url, '', $file);
if (!file_exists($file)) { re(false); return false; }
$cext = if_inside("",";", inside("/","",g('http_content_type')));
if ($cext) $ext = $cext;
$add = ''; $count = 2;
while (file_exists($file2 = $save_path.$name.$add.'.'.$ext))
$add = '-'.$count++;
rename($file,$file2);
if ($access_path===false) $access_path = $save_path;
$r = $access_path . $name . $add . '.' . $ext;
re($r); return $r;
}
function domain($url, $top = false)
{
$url = strtolower(trim($url));
if (strpos($url, '://') !== false) {
$url = parse_url($url);
@$url = $url['host'];
}
if ($url[strlen($url) - 1] == '/')
$url = substr($url, 0, -1);
if (substr($url, 0, 4) == "www.")
$url = substr($url, 4);
if ($top) { // Get top domain
$pos = strrpos($url,".");
if ($pos!==false && $pos==strlen($url)-3) {
$pos2 = strrpos($url,".", $pos-strlen($url)-1);
if ($pos-$pos2<5) $pos = $pos2;
}
if ($pos!==false) {
$pos = strrpos($url, ".", $pos-strlen($url)-1);
if ($pos!==false) $url = substr($url, $pos+1);
}
}
re($url); return $url;
}
$mx_http_code_messages = array(
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
306 => "(Unused)",
307 => "Temporary Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested Range Not Satisfiable",
417 => "Expectation Failed",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
555 => "Download interrupted");
$mx_http_code_messages_groups = array(
1 => "Informational",
2 => "Successful",
3 => "Redirection",
4 => "Client Error",
5 => "Server Error",
);
$http_user_agents = array(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.7 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.7",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)"
);
function http_code_message($http_code = false) {
global $mx_http_code_messages, $mx_http_code_messages_groups;
if (!$http_code) $http_code = g('http_code');
$group = (int) ($http_code / 100);
if (empty($mx_http_code_messages_groups[$group])) $r = 'Unknown';
elseif (empty($mx_http_code_messages[$http_code])) return $r = $mx_http_code_messages_groups[$group];
else $r = $mx_http_code_messages_groups[$group].': '.$mx_http_code_messages[$http_code];
re($r); return $r;
}
function http_getq($url) { // Quiet get
$display = ini_get('display_errors');
$log = ini_get('log_errors');
ini_set('display_errors',0);
ini_set('log_errors',0);
$res = http_get($url);
ini_set('display_errors',$display);
ini_set('log_errors',$log);
return $res;
}
function flatty_html($http_html) {
// TAB
$http_html=str_replace(chr(9)," ",$http_html);
// ---- Flattening
$http_html=replace("<!-*->","",$http_html);
$http_html = preg_replace('/^\s+|\n|\r|\s+$/m', ' ', $http_html);
// No double spaces
$double=true;
while ($double) $http_html=str_replace(" "," ",$http_html,$double);
$http_html = preg_replace('/>\s*</', '><', $http_html);
$http_html = preg_replace('/\s*>/', '>', $http_html);
$http_html = preg_replace('/<\s*/', '<', $http_html);
if (g('auto_replace')) $http_html = replace("","",$http_html);
re($http_html); return $http_html;
}
// Test an array of proxies with MultiCURL
function testproxies($proxies,$url) {
$timeout = 10;
$chs=array();
$mh = curl_multi_init();
foreach ($proxies as $proxy) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, "https://www.google.com/search?q=new+goods+for+family&oq=new+goods+for+family&aqs=chrome.0.69i59.6188j0&sourceid=chrome&ie=UTF-8");
curl_multi_add_handle($mh,$ch);
$chs[]=$ch;
}
$active = 0;
$start_time = microtime(true);
do {
if (microtime(true)-$start_time>$timeout) break;
$status = curl_multi_exec($mh, $active);
$info = curl_multi_info_read($mh);
if (false !== $info) {
$i = array_search($info['handle'],$chs);
$proxy = $proxies[$i];
$res = curl_getinfo($info['handle']);
if (g('mx_http_debug_proxies'))
xlog("Proxy: $proxy Result: <b>".$res['http_code']."</b>");
if (($res['http_code']==200) or ($res['http_code']==302) or ($res['http_code']==404)) {
// Test for blocked message
$h=curl_multi_getcontent($info['handle']);
if (g('mx_http_debug_proxies')) xlog("Content: ".htmlentities($h));
if (find("detected unusual traffic",$h)) $h ="";
$h=inside("<title>","</title>",$h);
if ($h=='Ой!') $h='';
if (strpos(strtoupper($h),'BLOCK')!==false) $h='';
if (strpos(strtoupper($h),'CONFIGURATION')!==false) $h='';
if (strpos($h,'403')!==false) $h='';
$h2 = iconv('Windows-1251', 'UTF-8//IGNORE', $h);
if ($h or strpos($url,'banggood.com')) return $proxy;
}
}
sleep(0.1);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
foreach ($chs as $ch) curl_close($ch);
return false;
}
function mx_url_path_encode_callback($matches) {
return urlencode($matches[0]);
}
function url_path_encode($url) { // Prepare URL for CURL fetch
$url = str_replace('&', '&', $url); $url = str_replace('&', '&', $url);
$url = str_replace(array(' '), array('%20'), $url);
$chars = '$-_.+!*\'(),{}|\\^~[]`<>#%";/?:@&=';
$pattern = '~[^a-z0-9' . preg_quote($chars, '~') . ']+~iu';
$url = preg_replace_callback($pattern, 'mx_url_path_encode_callback', $url);
re($url); return $url;
}
// Use CURL to get URL [ used by get() ] If file is specified, that contents are written to the file (and nothing returned).
function http_curl($url, $post = false, $file = false)
{
$url = url_path_encode($url);
xlog("Fetching <a rel='nofollow' href='$url' target='_blank'>$url</a>...");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_ENCODING , "");
$user_agent = g('http_user_agent');
if (!$user_agent) $user_agent = 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)';
elseif ($user_agent===true) $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36';
elseif ($user_agent==="random")
$user_agent = random($GLOBALS['http_user_agents']);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
global $http_headers;
if ($http_headers) {
$hheader = array();
foreach ($http_headers as $hname=>$hval)
$hheader[] = $hname.': '.$hval;
curl_setopt($ch, CURLOPT_HTTPHEADER, $hheader);
}
curl_setopt($ch, CURLOPT_TIMEOUT, g('http_curl_timeout'));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, g('http_curl_timeout'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
global $http_cookies;
if (($http_cookies!==false) and ($http_cookies!==NULL)) curl_setopt($ch, CURLOPT_COOKIE, $http_cookies);
else {
curl_setopt($ch, CURLOPT_COOKIEFILE, g('http_cookies_file'));
curl_setopt($ch, CURLOPT_COOKIEJAR, g('http_cookies_file'));
}
if (g('http_referer')) curl_setopt($ch, CURLOPT_REFERER, g('http_referer'));
else if (g('http_url_base')) curl_setopt($ch, CURLOPT_REFERER, g('http_url_base'));
$f = false;
if ($file) { //save output to file
$f = fopen($file, 'w');
curl_setopt($ch, CURLOPT_FILE, $f);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if (g('http_auth')) curl_setopt($ch, CURLOPT_USERPWD, g('http_auth'));
global $http_proxy;
if ($http_proxy) curl_setopt($ch, CURLOPT_PROXY, $http_proxy);
$http_html = curl_redir_exec($ch,$file,$f);
if ($file) @fclose($f);
if (@!filesize($file)) @unlink($file);
global $http_code, $http_content_type;
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$http_content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
if ($http_code==200 && curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD) > curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD)) {
$http_code = 555;
if ($file) @unlink($file);
}
curl_close($ch);
if (($http_code != 200) and ($http_code != 404) and ($http_code != 410)) {
$http_proxy = false;
mx_config_set("proxy",false);
}
if ($http_code != 200 ) xnotice("HTTP Result ".$http_code." - ". http_code_message());
if ($http_html) {
// Detect charset
$encoding = g('http_encoding');
if (!$encoding) $encoding = inside("charset=","", $http_content_type);
if (!$encoding) {
preg_match('/charset\s*="?[\w-]*/i',$http_html,$find);
if (!$find) preg_match('/encoding\s*="?[\w-]*/i',$http_html,$find);
if ($find) {
$find=$find[0];
$find = trim(substr($find,strpos($find,'=')+1));
if (substr($find,0,1)=='"') $find = substr($find,1);
$encoding = $find;
}
if (!$encoding) $encoding = "UTF-8";
}
if (strtoupper($encoding)!="UTF-8") dmsg('Converting from encoding: '.$encoding);
$http_html = @iconv($encoding, 'UTF-8//IGNORE', $http_html);
$http_html = str_replace($encoding,'UTF-8',$http_html);
}
if (!$file) {
global $http_url_base;
$base = inside("href=", "", inside("<base", ">", $http_html));
if ($base) $base = substr($base, 1, -1);
else $base = $url;
$base = if_inside("", "?", $base);
if ($p = strrpos($base, '/')) $base = substr($base, 0, $p);
if ($base)
$http_url_base = $base . "/";
}
if (g('debug_nesting_level') and ini_get('display_errors')) { // Write result to output
if (!$file) mx_debug_fetch_result($http_html);
else mx_debug_fetch_result($url);
}
if ($file and file_exists($file)) { re(true); return true; }
re($http_html); return $http_html;
}
function mx_debug_fetch_result($http_html) {
static $id;
if (!trim($http_html)) return;
$id++;
$m = "<a style='background-color:yellow' href='javascript: document.getElementById(\"fetch$id\").hidden = ! document.getElementById(\"fetch$id\").hidden'>
[ Fetch Result ]</a>
<div id='fetch$id' hidden='hidden'>";
if (substr($http_html,0,7)=="http://")
$m .= "<img src='$http_html' />";
else $m .= "<code>" . debug_highlight(htmlspecialchars($http_html)) . "</code>";
$m .= "</div><br/>";
echo $m;
}
// Exec CURL with redirects helper
function curl_header_callback($curl_handler, $header_line) {
global $mx_curl_save_header;
$mx_curl_save_header .= $header_line;
return strlen($header_line);
}
function curl_redir_exec($curl_handler, $file_name = false, $file_handler = false)
{
static $curl_loops = 0;
static $curl_max_loops = 20;
if ($curl_loops++>$curl_max_loops) {
$curl_loops = 0;
return false;
}
global $mx_curl_save_header;
$mx_curl_save_header = '';
curl_setopt($curl_handler, CURLOPT_HEADERFUNCTION, 'curl_header_callback');
$data = curl_exec($curl_handler);
$header = $mx_curl_save_header;
$http_code = curl_getinfo($curl_handler, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302)
{
if ($file_name) {
fclose($file_handler);
unlink($file_name);
$file_handler = fopen($file_name, 'w');
curl_setopt($curl_handler, CURLOPT_FILE, $file_handler);
}
$matches = array();
$header = str_replace('-Location', '', $header);
preg_match('/Location:(.*?)\n/', $header, $matches);
if (empty($matches[1]))
{
//couldn't process the url to redirect to
$curl_loops = 0;
return $data;
}
$new_url = trim($matches[1]);
if ($new_url) $new_url = url($new_url);
curl_setopt($curl_handler, CURLOPT_URL, $new_url);
dmsg("Redirected to <a rel='nofollow' target='_blank' href='$new_url'>$new_url</a>...");
if (strpos($new_url,'showcaptcha')) {
global $http_proxy, $http_code;
$http_proxy = false;
mx_config_set("proxy",false);
$http_code = 503;
return false;
}
return curl_redir_exec($curl_handler,$file_name,$file_handler);
} else {
$curl_loops=0;
return $data;
}
}
// ======================== Simple Data Retrieval
// Find the text inside the $start and $end.
// If $start="" then start is start of content.
// If $end="" then end is end of content.
function inside($start, $end="", $source = false)
{
if ($source === false) { global $http_html; $source = &$http_html; } // Use set_source() default
$r = '';
if ($start) $s = stripos($source, $start);
else $s=0;
if ($s !== false) {
$s += strlen($start);
if ($end) $e = stripos($source, $end, $s);
else $e = strlen($source);
if ($e !== false)
$r = trim(substr($source, $s, $e - $s));
}
re($r); return $r;
}
// Find inside, if not found - return original string
function if_inside ($start, $end="", $source) {
$res = inside ($start,$end,$source);
if ($res) { re($res); return $res; }
else { re($source); return $source; }
}
// Find the texts inside the $start and $end
function insides($start, $end="", $source = false)
{
if ($source === false) { global $http_html; $source = &$http_html; } // Use set_source() default
$r = array(); $s=0;
if (is_array($source)) {
foreach ($source as $line)
if ($start and $end) array_push($r,insides($start,$end,$line));
else {
$i = inside($start,$end,$line);
if ($i) $r[] = $i;
}
re($r); return $r;
}
while ( ($s = strpos($source, $start,$s) ) !== false ) {
$s += strlen($start);