im-5r /inc/minibots.class.php

Language PHP Lines 1135
MD5 Hash da82edcd052157a160e52813b6ae8353 Estimated Cost $22,469 (why?)
Repository https://bitbucket.org/tamvominh/im-5r.git View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
<?
/* ---------------------------------------------------------- */
/* minibots.class.php Ver.1.9g                                */
/* ---------------------------------------------------------- */
/* Mini Bots class is a small php class that allows you to    */
/* use some free web seriveces online to retrive usefull data */
/* and infos. This version includes:                          */
/* smtp validation, check spelling, meteo, exchange rates,    */
/* shorten urls, and geo referencing with IP address and more */
/* Feel free to use in your applications, but link my blog:   */
/* http://www.barattalo.it                                    */
/* Giulio Pons                                                */
/* ---------------------------------------------------------- */

Class Minibots
{
	private $file_size = 0;
	private $max_file_size = 5000;
	private $file_downloaded = "";

	public function __construct () {

	}

	public function getIP() {
		$ip="";
		if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
		else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
		else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
		else $ip = "";
		return $ip;
	}


	private function dayadd($days,$date=null , $format="d/m/Y"){
		// add days to a date
		return date($format,strtotime($days." days",strtotime( $date ? $date : date($format) )));
	}


	private function attr($s,$attrname) {
		//return html attribute
		preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
		if (count($x)>=3) return isset($x[2][0]) ? $x[2][0] : "";
		return "";
	}

	private function makeabsolute($url,$link) {
		$p = parse_url($url);
		if (strpos( $link,"http://")===0 ) return $link;
		if($p['scheme']."://".$p['host']==$url && $link[0]!="/" && $link!=$url) return $p['scheme']."://".$p['host']."/".$link;
		if (strpos( $link, "/")===0) return "http://".$p['host'].$link;
		return str_replace(substr(strrchr($url, "/"), 1),"",$url).$link;
	}

	function on_curl_header($ch, $header) {	// to handle file size check and prevent downloading too much
		$trimmed = rtrim($header);
		if (preg_match('/^Content-Length: (\d+)$/i', $trimmed, $matches)) {
			$file_size = (float)$matches[1];
			if ($file_size > $this->max_file_size) {
				// stop if bigger
				return -1;
			}
		}
		return strlen($header);
	}

	function on_curl_write($ch, $data) {	// to handle file size check and prevent downloading too much
		$bytes = strlen($data);
		$this->file_size += $bytes;
		$this->file_downloaded .= $data;
		if ($this->file_size > $this->max_file_size) {
			// stop if bigger
			return -1;
		}
		return $bytes;
	}


	private function getRemoteFileSize($url) {
		if (substr($url,0,4)=='http') {
			$x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
			if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; }
			else { $x = $x['content-length']; }
		}
		else { $x = @filesize($url); }
		return $x;
	}


	public function doSpelling($q) {
		// (thanks to google)
		// grab google page with search
		$web_page = file_get_contents( "http://www.google.it/search?q=" . urlencode($q) );
		// put anchors tag in an array
		preg_match_all('#<a([^>]*)?>(.*)</a>#Us', $web_page, $a_array);
		for($j=0;$j<count($a_array[0]);$j++) {
			// find link with spell suggestion and return it
			if(stristr($a_array[0][$j],"class=spell")) return strip_tags($a_array[0][$j]);
		}
		return $q;	//if no results returns the q value
	}


	public function doExchangeRate($m,$d) {
		// (thanks to bank of italy)
		// grab exchange rates
		$dar = explode("-" , $this->dayadd(-1,$d,"Y-m-d") );
		$web_page = file_get_contents( "http://uif.bancaditalia.it/UICFEWebroot/QueryOneDateAllCur?lang=en&rate=0&initDay=".$dar[2]."&initMonth=".$dar[1]."&initYear=".$dar[0]."&refCur=euro&R1=csv");
		// parse csv results
		$lines = explode("\n",$web_page);
		for($j=0;$j<count($lines);$j++) {
			$fields = explode(",",$lines[$j]);
			if ($fields[2]==$m) return $fields[4];
		}
		return "";
	}


	public function doMeteo($q,$thedate="") {
		//(thanks to google)
		if (!$thedate) $date = date("Y-m-d");	//today
			else $date = $thedate;
		if ($date>$this->dayadd(3,date("Y-m-d"),"Y-m-d"))return "";
		// grab google page with meteo query
		$web_page = file_get_contents( "http://www.google.it/search?q=meteo+" . urlencode($q) );
		//parse to find data
		preg_match_all('#<div class=e>(.*)</table>#Us', $web_page, $m);
		if (count($m)>0) {

			$p = array();
			preg_match_all('#<img([^>]*)?>#Us', $m[0][0], $img);
			for ($i=0;$i<count($img[0]);$i++) {
				$tag = str_replace("src=\"/","src=\"http://www.google.it/",$img[0][$i]);
				$p[$i]["date"]=$this->dayadd($i,date("Y-m-d"),"Y-m-d");
				$p[$i]["title"] = $this->attr($tag,"title");
				$p[$i]["img"] = $this->attr($tag,"src");
			}
			preg_match_all('#<nobr>(.*)</nobr>#Uis', $m[0][0], $nobr);
			for ($i=0;$i<count($nobr[1]);$i++) {
				$temp= explode("|",$nobr[1][$i]);
				$p[$i]["min"] = trim($temp[1]) ;
				$p[$i]["max"] = trim($temp[0]) ;
			}
			return (!$thedate?$p:$p[$date]);
		}
		return array();
	}


	public function doShortURL($ToConvert) {
		//(thanks to tinyurl.com)
		$short_url= file_get_contents('http://tinyurl.com/api-create.php?url=' . $ToConvert);
		return $short_url;
	}

	public function doShortURLDecode($url) {
		if (!function_exists("curl_init")) die("doShortURLDecode needs CURL module, please install CURL on your php.");
		$ch = @curl_init($url);
		@curl_setopt($ch, CURLOPT_HEADER, TRUE);
		@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
		@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$response = @curl_exec($ch);
		preg_match('/Location: (.*)\n/', $response, $a);
		if (!isset($a[1])) return $url;
		return $a[1];
	}

	public function checkMp3($url) {
		if (!function_exists("curl_init")) die("getHttpResponseCode needs CURL module, please install CURL on your php.");
		$a = parse_url($url);
		if(checkdnsrr(str_replace("www.","",$a['host']),"A") || checkdnsrr(str_replace("www.","",$a['host']))) {
			$ch = @curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_NOBODY, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 15);
			$results = explode("\n", trim(curl_exec($ch)));
			$mime = "";
			foreach($results as $line) {
				if (strtok($line, ':') == 'Content-Type') {
					$parts = explode(":", $line);
					$mime = trim($parts[1]);
				}
			}
			return $mime=="audio/mpeg";
		} else {
			return false;
		}
	}

	private function getHttpResponseCode($url) {
		if (!function_exists("curl_init")) die("getHttpResponseCode needs CURL module, please install CURL on your php.");
		// 404 not found, 403 forbidden...
		// for a full list: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
		$ch = @curl_init($url);
		@curl_setopt($ch, CURLOPT_HEADER, TRUE);
		@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
		@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$status = array();
		preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($ch) , $status);
		return isset($status[1]) ? $status[1] : null;
	}

	//like file_exists, but for remote urls.
	public function url_exists($url) {
		return ($this->getHttpResponseCode($url) == 200);
	}

	public function doGeoIp($ip="") {
		//(thanks to geoiptool)
		// -----------------------------------------------------------------------------------
		// better use this free api: http://ipinfodb.com/ip_query.php?ip=62.149.150.92
		// that returns clean xml code.
		if (!$ip) $ip = $this->getIP();
		$ar = array();
		$web_page = file_get_contents( "http://www.geoiptool.com/en/?IP=".$ip );
		preg_match_all('#<table([^>]*)tbl_style([^>]*)?>(.*)</table>#Us', $web_page, $t_array);
		for($j=0;$j<count($t_array[0]);$j++) {
			//find table with data
			if (stristr($t_array[0][$j],"IP Address")) {
				//parse data
				$table = $t_array[0][$j];
				preg_match_all('#<tr([^>]*)?>(.*)</tr>#Us', $table, $tr_array);
				for($i=0;$i<count($tr_array[0]);$i++) {
					$tar = explode(":", strip_tags ( $tr_array[0][$i] ) );
					$ar[ trim($tar[0]) ] = trim($tar[1]);
				}
			}
		}
		return $ar;
	}

	function doSMTPValidation($email, $probe_address="", $debug=false) {
		# --------------------------------
		# function to validate email address
		# through a smtp connection with the
		# mail server. returns an true when ok
		# or an array (msg, error code) when fails.
		# --------------------------------
		$output = "";
		# --------------------------------
		# Check syntax with regular expression
		# --------------------------------
		if (!$probe_address) $probe_address = $_SERVER["SERVER_ADMIN"];
		if (preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $email, $matches)) {
			$user = $matches[1];
			$domain = $matches[2];
			# --------------------------------
			# Check availability of DNS MX records
			# --------------------------------
			if (function_exists('checkdnsrr')) {
				# --------------------------------
				# Construct array of available mailservers
				# --------------------------------
				if(getmxrr($domain, $mxhosts, $mxweight)) {
					for($i=0;$i<count($mxhosts);$i++){
						$mxs[$mxhosts[$i]] = $mxweight[$i];
					}
					asort($mxs);
					$mailers = array_keys($mxs);
				} elseif(checkdnsrr($domain, 'A')) {
					$mailers[0] = gethostbyname($domain);
				} else {
					$mailers=array();
				}
				$total = count($mailers);
				# --------------------------------
				# Query each mailserver
				# --------------------------------
				if($total > 0) {
					# --------------------------------
					# Check if mailers accept mail
					# --------------------------------
					for($n=0; $n < $total; $n++) {
						# --------------------------------
						# Check if socket can be opened
						# --------------------------------
						if($debug) { $output .= "Checking server $mailers[$n]...\n";}
						$connect_timeout = 2;
						$errno = 0;
						$errstr = 0;
						# --------------------------------
						# controllo probe address
						# --------------------------------
						if (preg_match('/^([a-zA-Z0-9\._\+-]+)\@((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,7}|[0-9]{1,3})(\]?))$/', $probe_address,$fakematches)) {
							$probe_domain = str_replace("@","",strstr($probe_address, '@'));

							# --------------------------------
							# Try to open up socket
							# --------------------------------
							if($sock = @fsockopen($mailers[$n], 25, $errno , $errstr, $connect_timeout)) {
								$response = fgets($sock);
								if($debug) {$output .= "Opening up socket to $mailers[$n]... Success!\n";}
								stream_set_timeout($sock, 5);
								$meta = stream_get_meta_data($sock);
								if($debug) { $output .= "$mailers[$n] replied: $response\n";}
								# --------------------------------
								# Be sure to set this correctly!
								# --------------------------------
								$cmds = array(
									"HELO $probe_domain",
									"MAIL FROM: <$probe_address>",
									"RCPT TO: <$email>",
									"QUIT",
								);
								# --------------------------------
								# Hard error on connect -> break out
								# --------------------------------
								if(!$meta['timed_out'] && !preg_match('/^2\d\d[ -]/', $response)) {
									$codice = trim(substr(trim($response),0,3));
									if ($codice=="421") {
										//421 #4.4.5 Too many connections to this host.
										$error = $response;
										break;
									} else {
										if($response=="" || $codice=="") {
											//c'è stato un errore ma la situazione è poco chiara
											$codice = "0";
										}
										$error = "Error: $mailers[$n] said: $response\n";
										break;
									}
									break;
								}
								foreach($cmds as $cmd) {
									$before = microtime(true);
									fputs($sock, "$cmd\r\n");
									$response = fgets($sock, 4096);
									$t = 1000*(microtime(true)-$before);
									if($debug) {$output .= "$cmd\n$response" . "(" . sprintf('%.2f', $t) . " ms)\n";}
									if(!$meta['timed_out'] && preg_match('/^5\d\d[ -]/', $response)) {
										$codice = trim(substr(trim($response),0,3));
										if ($codice<>"552") {
											$error = "Unverified address: $mailers[$n] said: $response";
											break 2;
										} else {
											$error = $response;
											break 2;
										}
										# --------------------------------
										// il 554 e il 552 sono quota
										// 554 Recipient address rejected: mailbox overquota
										// 552 RCPT TO: Mailbox disk quota exceeded
										# --------------------------------
									}
								}
								fclose($sock);
								if($debug) { $output .= "Succesful communication with $mailers[$n], no hard errors, assuming OK\n";}
								break;
							} elseif($n == $total-1) {
								$error = "None of the mailservers listed for $domain could be contacted";
								$codice = "0";
							}
						} else {
							$error = "Il probe_address non è una mail valida.";
						}
					}
				} elseif($total <= 0) {
					$error = "No usable DNS records found for domain '$domain'";
				}
			}
		} else {
			$error = 'Address syntax not correct';
		}
		if($debug) {
			print nl2br(htmlentities($output));
		}
		if(!isset($codice)) {$codice="n.a.";}
		if(isset($error)) return array($error,$codice); else return true;
	}

	public function getUrlInfo($url,$maximages=5,$maxkbimg=10) {
		if (!function_exists("curl_init")) die("getUrlInfo needs CURL module, please install CURL on your php.");
		// this bot retrieves info about a url:
		// keywords, title, description,favicon and images
		//
		$url = $this->makeabsolute($url, $this->doShortURLDecode($url));
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);       // Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);     // return into a variable
		curl_setopt($ch, CURLOPT_PORT, 80);             //Set the port number
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);          // times out after 15s
		if($maximages==0) {
			// if you don't want images from html
			// use only first 5 kb to reduce band used and time
			$this->max_file_size = 5000;
			curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'on_curl_header'));
			curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'on_curl_write'));
		}
		$web_page = curl_exec($ch);
		if(strlen($web_page) <= 1 && $maximages==0) {
			$web_page = $this->file_downloaded;
		}

		//$web_page = file_get_contents($url);
		$data['keywords']="";
		$data['description']="";
		$data['title']="";
		$data['favicon']="";
		$data['images']=array();
		$data['thumbsite']="http://open.thumbshots.org/image.pxf?url=".urlencode($url);
		//search title
		preg_match_all('#<title([^>]*)?>(.*)</title>#Uis', $web_page, $title_array);
		$data['title'] = $title_array[2][0];
		//search keywords and description
		preg_match_all('#<meta([^>]*)(.*)>#Uis', $web_page, $meta_array);
		//print_r($meta_array);
		for($i=0;$i<count($meta_array[0]);$i++) {
			if (strtolower($this->attr($meta_array[0][$i],"name"))=='description') $data['description'] = $this->attr($meta_array[0][$i],"content");
			if (strtolower($this->attr($meta_array[0][$i],"name"))=='keywords') $data['keywords'] = $this->attr($meta_array[0][$i],"content");
		}
		//search favicon
		preg_match_all('#<link([^>]*)(.*)>#Uis', $web_page, $link_array);
		for($i=0;$i<count($link_array[0]);$i++) {
			if (strtolower($this->attr($link_array[0][$i],"rel"))=='shortcut icon') $data['favicon'] = $this->makeabsolute($url,$this->attr($link_array[0][$i],"href"));
		}
		// search images big enough
		preg_match_all('#<img([^>]*)(.*)/?>#Uis', $web_page, $imgs_array);
		$imgs = array();
		for($i=0;$i<count($imgs_array[0]);$i++) {
			if ($src = $this->attr($imgs_array[0][$i],"src")) {
				$src = $this->makeabsolute($url,$src);
				if(!in_array($src,$imgs) && $this->getRemoteFileSize($src)>$maxkbimg*1000) array_push($imgs,$src);
			}
			if (count($imgs)>$maximages-1) break;
		}
		$data['images']=$imgs;

		return $data;
	}

	public function getUrlInfoFast($url,$maxbytes=4096) {
		if (!function_exists("curl_init")) die("getUrlInfo needs CURL module, please install CURL on your php.");
		$url = $this->makeabsolute($url, $this->doShortURLDecode($url));
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, 0);       // Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);     // return into a variable
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);          // times out after 15s
		$this->max_file_size = $maxbytes;
		//curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'on_curl_header'));
		curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'on_curl_write'));
		$web_page = curl_exec($ch);
		if(strlen($web_page) <= 1) $web_page = $this->file_downloaded;
		$data['d']="";
		$data['t']="";
		$data['f']="";
		$data['e']=""; // per mettere automaticamente embed di player video youtube e mp3
		$data['g']=""; // trigger per visualizzare embed
		//search title
		preg_match_all('#<title([^>]*)?>(.*)</title>#Uis', $web_page, $title_array);
		$data['t'] = isset($title_array[2][0]) ? trim(preg_replace('/ +/', ' ', $title_array[2][0])) : "senza titolo";
		//search keywords and description
		preg_match_all('#<meta([^>]*)(.*)>#Uis', $web_page, $meta_array);
		//print_r($meta_array);
		for($i=0;$i<count($meta_array[0]);$i++) if (strtolower($this->attr($meta_array[0][$i],"name"))=='description') $data['d'] = $this->attr($meta_array[0][$i],"content");
		//search favicon
		preg_match_all('#<link([^>]*)(.*)>#Uis', $web_page, $link_array);
		for($i=0;$i<count($link_array[0]);$i++) {
			if (strtolower($this->attr($link_array[0][$i],"rel"))=='shortcut icon') $data['f'] = $this->makeabsolute($url,$this->attr($link_array[0][$i],"href"));
		}
		$trigger = "<img src='http://open.thumbshots.org/image.pxf?url=".urlencode($url)."' />";
		$embed = "";

		//http://www.youtube.com/v/Md1E_Rg4MGQ&hl=en&fs=1&
		//http://www.youtube.com/watch?v=Md1E_Rg4MGQ&feature=aso
		preg_match_all('/^http:\/\/www.youtube.com\/(v\/|watch\?v=)([^&]*)(.*)$/', $url, $yarr);
		if(isset($yarr[2][0])) {
			$trigger = "<img src='http://img.youtube.com/vi/".$yarr[2][0]."/1.jpg'/>";
			$embed = $this->resizeEmbed(
			'<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'.$yarr[2][0].'?fs=1&amp;hl=it_IT"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$yarr[2][0].'?fs=1&amp;hl=it_IT" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>', 400);
		}

		//http://vimeo.com/17116744
		preg_match_all('/^http:\/\/vimeo.com\/([0-9]*)$/', $url, $varr);
		if(isset($varr[1][0])) {
			$trigger = "<img src='".$this->getVimeoInfo($varr[1][0],"thumbnail_small")."'/>";
			$embed = $this->resizeEmbed(
			'<iframe src="http://player.vimeo.com/video/'.$varr[1][0].'" width="400" height="225" frameborder="0"></iframe>' , 400);
		}

		$data["e"] = $embed;
		$data["g"] = $trigger;
		return $data;
	}

	//
	// copy remote file to server
	private function copyFile($url,$filename){
		$file = fopen ($url, "rb");
		if (!$file) return false; else {
			$fc = fopen($filename, "wb");
			while (!feof ($file)) {
				$line = fread ($file, 1028);
				fwrite($fc,$line);
			}
			fclose($fc);
			return true;
		}
	}

	//
	// save a url to a local pdf using pdfmyurl.com service
	public function url2pdf($url,$pdffilename) {
		return $this->copyFile("http://pdfmyurl.com?url=".urlencode( str_replace("http://","",$url) ), $pdffilename);
	}

	//
	// save a url to a local jpg thumb using open.thumbshots.com service
	public function url2thumb($url,$thumbfilename) {
		return $this->copyFile("http://open.thumbshots.org/image.pxf?url=".urlencode( $url ), $thumbfilename);
	}

	//
	// return text from a url
	// thanks to php.net
	public function webpage2txt($url) {
		if (!function_exists("curl_init")) die("webpage2txt needs CURL module, please install CURL on your php.");
		$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; it; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";

		$ch = curl_init();    // initialize curl handle
		curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
		curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
		curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s

		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

		$document = curl_exec($ch);

		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
			'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
			'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
			'@<![\s\S]*?[ \t\n\r]*>@',         // Strip multi-line comments including CDATA
			'/\s{2,}/',
		);

		$text = preg_replace($search, " ", html_entity_decode($document));

		$pat[0] = "/^\s+/";
		$pat[2] = "/\s+\$/";
		$rep[0] = "";
		$rep[2] = " ";

		$text = preg_replace($pat, $rep, trim($text));

		return $text;
	}

  function remove_html_tags($str) {
    $str = preg_replace(
      array(// Remove invisible content
        '@<head[^>]*?>.*?</head>@siu',
        '@<script[^>]*?>.*?</script>@siu',
        '@<noscript[^>]*?.*?</noscript>@siu',
      ),
      "", //replace above with nothing
      $str );
    return $str;
  }

  // OK
	public function twitterSetStatus($user, $pwd, $status, $proxy_obj) {
		if (!function_exists("curl_init")) die("twitterSetStatus needs CURL module, please install CURL on your php.");
		$ch = curl_init();

		// -------------------------------------------------------
		// get login form and parse it
    $cookie_file = "." . DIRECTORY_SEPARATOR . "cookies" . DIRECTORY_SEPARATOR . "my_cookies.txt";
    if (file_exists($cookie_file)) {
      unlink($cookie_file);
    }

		curl_setopt($ch, CURLOPT_URL, "https://mobile.twitter.com/session/new");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 ");
    if ($proxy_obj) {
      curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
      curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
      curl_setopt($ch, CURLOPT_PROXY, $proxy_obj['proxy']);
      curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_obj['port']);
    }

		$page = curl_exec($ch);
    $page = $this->remove_html_tags($page);
    // echo "Login page: $page <br/>";

		// $page = stristr($page, "<div id>");
		preg_match("/form action=\"(.*?)\"/", $page, $action);
    $regex = "/<input.+?name=\"authenticity_token\".+?value=\"(\w+)\"/";
		preg_match($regex, $page, $authenticity_token);
    // echo "Auth token: $authenticity_token[1]";

		// -------------------------------------------------------
		// make login and get home page
		$strpost = "authenticity_token=".urlencode($authenticity_token[1])."&username=".urlencode($user)."&password=".urlencode($pwd);
		curl_setopt($ch, CURLOPT_URL, $action[1]);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost);
		$page = curl_exec($ch);
    $page = $this->remove_html_tags($page);
    // echo "home page: $page";
		// check if login was ok

		curl_setopt($ch, CURLOPT_URL, "https://mobile.twitter.com/compose/tweet");
		// curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost);
		$page = curl_exec($ch);
    $page = $this->remove_html_tags($page);
    // echo $page;

    preg_match("/\<div class=\"warning\"\>(.*?)\<\/div\>/", $page, $warning);
    if (isset($warning[1])) {
      echo $warning[1];
      return false;
    }

    // $page = stristr($page,'<table class="tweettable">');
    preg_match("/form action=\"(.*?)\"/", $page, $action);
    preg_match($regex, $page, $authenticity_token);

		// -------------------------------------------------------
		// send status update
    $strpost = "authenticity_token=".urlencode($authenticity_token[1]);
    $tweet['display_coordinates']='';
    $tweet['in_reply_to_status_id']='';
    $tweet['lat']='';
    $tweet['long']='';
    $tweet['place_id']='';
    $tweet['text']=$status;
    $ar = array("authenticity_token" => $authenticity_token[1], "tweet"=>$tweet);
    $data = http_build_query($ar);
    curl_setopt($ch, CURLOPT_URL, "https://mobile.twitter.com/");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $page = curl_exec($ch);
    $page = $this->remove_html_tags($page);
    // echo $page;

		// return $page;
    if (Util::http_code($ch) == 200) {
      return true;
    } else {
      return false;
    }
	}

	//
	// get twitter infos from nickname
	// and get avatar url, parse data from page
	public function twitterInfo($nick) {
		if (!function_exists("curl_init")) die("twitterInfo needs CURL module, please install CURL on your php.");
		$ch = curl_init();    // initialize curl handle
		curl_setopt($ch, CURLOPT_URL, "http://twitter.com/$nick"); // set url to post to
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);		// Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);	// allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);		// return into a variable
		curl_setopt($ch, CURLOPT_PORT, 80);				// Set the port number
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);			// times out after 15s
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		$document = curl_exec($ch);
		preg_match_all('#<div class="stats">(.*)</div>#Uis', $document, $stats);
		preg_match_all('#<span[^>]*?>(.*)</span>#Uis', $stats[1][0], $spans);
		$o = array();
		for ($i=0;$i<count($spans[0]);$i++) {
			if ($this->attr($spans[0][$i],"id")=="following_count") $o['following'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="follower_count") $o['follower'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="lists_count") $o['lists'] = $spans[1][$i];
		}
		$o['avatar'] = "";
		preg_match_all('#<img [^>]*?>#Uis', $document, $t);
		for ($i=0;$i<count($t[0]);$i++) if ($this->attr($t[0][$i],"id")=="profile-image") $o['avatar'] = $this->attr($t[0][$i],"src");
		return $o;
	}

	//
	// get twitter infos from nickname
	// and get avatar url, parse data from api xml response
  // OK
	public function twitterInfoApi($nick) {
		if (!function_exists("curl_init")) die("twitterInfoApi needs CURL module, please install CURL on your php.");
		$ch = curl_init();    // initialize curl handle
		curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/users/show.xml?screen_name=$nick"); // set url to post to
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		$obj = simplexml_load_string( curl_exec($ch) );

    // echo "<pre>";
    // var_dump($obj);
    // echo "</pre>";

		if(is_object($obj)) {
			return array(
					"id" => (string)$obj->id,
					"name" => (string)$obj->name,
          "screen_name" => (string)$obj->screen_name,
					"description" => (string)$obj->description,
					"avatar" => (string)$obj->profile_image_url,
					"followers" => (string)$obj->followers_count,
					"following" => (string)$obj->friends_count,
					"favourites_count" => (string)$obj->favourites_count,
          "statuses_count" => (string)$obj->statuses_count,
					"status" => date("Y-m-d H:i:s", strtotime( (string)$obj->status->created_at ))." - ".(string)$obj->status->text
				);
		} else {
			return null;
		}
	}

	//
	// update twitter status (old function, no longer valid, with basic authentication)
	private function twitterSetStatus__OLD__($user,$pwd,$status) {
		if (!function_exists("curl_init")) die("twitterSetStatus needs CURL module, please install CURL on your php.");
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/update.xml");
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'status=' . urlencode($status));
		curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		$document = curl_exec($ch);
		if ($document) return true; else return false;
	}

	//
	// elenco aggiornamenti di status
	public function twitterGetStatusList($nick) {
		if (!function_exists("curl_init")) die("twitterGetStatusList needs CURL module, please install CURL on your php.");
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=$nick");
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		$obj = simplexml_load_string( curl_exec($ch) );
		$a= array();
		if ($obj) {
			for ($i=0;$i<count($obj->status);$i++) {
				$s = date("Y-m-d H:i:s", strtotime( (string)$obj->status[$i]->created_at ))." - ".(string)$obj->status[$i]->text;
				array_push($a,$s);
			}
		}
		return $a;
	}

	//
	// change Facebook status with curl
	// Thanks to Alste (curl stuff inspired by nexdot.net/blog)
	// This function executes the status update if $pagina == "home.php".
	// If you provide a different fan page url, this function will post
	// on the page wall (if the user can).
	private function setFacebookStatusOrPostToWall($status, $login_email, $login_pass, $pagina = "home.php", $debug=false) {
		if (!function_exists("curl_init")) die("setFacebookStatusOrPostToWall needs CURL module, please install CURL on your php.");
		//CURL stuff
		//This executes the login procedure
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=' . urlencode("Log in"));
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//make sure you put a popular web browser here (signature for your web browser can be retrieved with 'echo $_SERVER['HTTP_USER_AGENT'];'
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
		curl_exec($ch);

		// post status or on a fan page
		curl_setopt($ch, CURLOPT_POST, 0);
		curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/'.$pagina);
		//echo 'http://m.facebook.com/'.$pagina;
		//die;
		$page = curl_exec($ch);

		if($pagina == "home.php" ) {
			// update status
			if ($debug) {
				//show information regarding the request
				print_r(curl_getinfo($ch));
				echo curl_errno($ch) . '-' . curl_error($ch);
				echo "<hr>";
				echo (htmlspecialchars($page));
				echo "<br><br>Your Facebook status seems to have been updated.";
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			//this gets the post_form_id value
			preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
			preg_match("/input type=\"hidden\" name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $fb_dtsg);
			preg_match("/input type=\"hidden\" name=\"charset_test\" value=\"(.*?)\"/", $page, $charset_test);
			preg_match("/input type=\"submit\" class=\"button\" name=\"update\" value=\"(.*?)\"/", $page, $update);
			//we'll also need the exact name of the form processor page
			//preg_match("/form action=\"(.*?)\"/", $page, $form_num);
			//sometimes doesn't work so we search the correct form action to use
			//since there could be more than one form in the page.
			preg_match_all("#<form([^>]*)>(.*)</form>#Ui", $page, $form_ar);
			for($i=0;$i<count($form_ar[0]);$i++) if(stristr($form_ar[0][$i],"post_form_id")) preg_match("/form action=\"(.*?)\"/", $page, $form_num);


			$strpost = 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode($update[1]) . '&charset_test=' . urlencode($charset_test[1]) . '&fb_dtsg=' . urlencode($fb_dtsg[1]);
			if($debug) {
				echo "Parameters sent: ".$strpost."<hr>";
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );

			//set url to form processor page
			curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
			curl_exec($ch);
			if ($debug) {
				//show information regarding the request
				print_r(curl_getinfo($ch));
				echo curl_errno($ch) . '-' . curl_error($ch);
				echo "<br><br>Your Facebook status seems to have been updated.";
			}
			//close the connection
			curl_close($ch);
		} else {
			// post on facebook page
			if ($debug) {
				//show information regarding the request
				print_r(curl_getinfo($ch));
				echo curl_errno($ch) . '-' . curl_error($ch);
				echo "<hr>";
				echo (htmlspecialchars($page));
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			//this gets the post_form_id value
			preg_match("/form action=\"(.*?)\" method=\"post\"/", $page, $action);
			preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
			preg_match("/input type=\"hidden\" name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $fb_dtsg);
			preg_match("/input type=\"hidden\" name=\"charset_test\" value=\"(.*?)\"/", $page, $charset_test);
			preg_match("/input type=\"submit\" class=\"button\" name=\"post\" value=\"(.*?)\"/", $page, $post);
			//we'll also need the exact name of the form processor page
			preg_match("/form action=\"(.*?)\"/", $page, $form_num);

			$strpost = 'post_form_id=' . $form_id[1] . '&message=' . urlencode($status) . '&post=' . urlencode($post[1]) . '&charset_test=' . urlencode($charset_test[1]) . '&fb_dtsg=' . urlencode($fb_dtsg[1]);
			if($debug) {
				echo "Parameters sent: ".$strpost."<hr>";
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );

			//set url to form processor page
			curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $action[1]);
			curl_exec($ch);
			if ($debug) {
				//show information regarding the request
				print_r(curl_getinfo($ch));
				echo curl_errno($ch) . '-' . curl_error($ch);
				echo "<br><br>Your Facebook page wall have been updated.";
			}
			//close the connection
			curl_close($ch);

		}


	}

	public function setFacebookStatus($status, $login_email, $login_pass, $debug=false) {
		if (!function_exists("curl_init")) die("setFacebookStatus needs CURL module, please install CURL on your php.");
		$this->setFacebookStatusOrPostToWall($status, $login_email, $login_pass, "home.php", $debug);
	}

	public function postToFacebookPage($msg, $pagina, $login_email, $login_pass, $debug=false) {
		if (!function_exists("curl_init")) die("postToFacebookPage needs CURL module, please install CURL on your php.");
		//http://www.facebook.com/#!/pages/Favignana/38995680998?ref=ts
		// $pagina = "/pages/Favignana/38995680998";
		$this->setFacebookStatusOrPostToWall($msg, $login_email, $login_pass, $pagina, $debug);
	}


	//
	// get list of images from google images
	public function googleGetImages($k) {
		$url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y";
		$web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));

		$tieni = stristr($web_page,"dyn.setResults(");
		$tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
		$tieni = str_replace("[]","",$tieni);
		$m = preg_split("/[\[\]]/",$tieni);
		$x = array();
		for($i=0;$i<count($m);$i++) {
			$m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
			$m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
			$m[$i] = preg_replace("/^\"/i","",$m[$i]);
			$m[$i] = preg_replace("/^,/i","",$m[$i]);
			if ($m[$i]!="") array_push($x,$m[$i]);
		}
		return $x;
	}

	//
	// get users video from youtube
	public function youtubeGetVideos($user) {
		// you can retrieve thumbs from here: http://www.reelseo.com/youtube-thumbnail-image/
		// youtube thumbs here: http://img.youtube.com/vi/####VIDEOID#####/1.jpg
		$web_page = file_get_contents( "http://m.youtube.com/".urlencode($user) );
		preg_match_all('#<a (.*)</a>#Uis', $web_page, $links);

		$x = array();
		for ($i=0;$i<count($links[1]);$i++) {
			if (stristr($links[1][$i],"/watch")) {
				$m = preg_split("/[&=\?]/", str_replace("&amp;", "&", $this->attr($links[1][$i],"href")));
				for ($j=0;$j<count($m);$j++) if ($m[$j]=="v") { array_push($x,$m[$j+1]); break; }
			}
		}
		return $x;
	}

	private function decodeFlickrUsername($n) {
		$s = @file_get_contents("http://www.flickr.com/photos/".$n);
		preg_match_all('#<a([^>]*)?>(.*)</a>#Us', $s, $a_array);
		for($i=0;$i<count($a_array[0]);$i++) {
			//echo htmlspecialchars($a_array[0][$i])."<hr>";
			if(stristr($a_array[0][$i],"http://api.flickr.com/services/feeds")) {
				$m = preg_split("/[&=\?]/", $this->attr($a_array[0][$i],"href"));
				for ($j=0;$j<count($m);$j++) if ($m[$j]=="id") return $m[$j+1];
			}
		}
		return "";

	}
	public function parseFlickrFeed($user,$n) {
		$id = $this->decodeFlickrUsername($user);
		if(!$id) return array();
		// $id = 16664181@N00
		$url = "http://api.flickr.com/services/feeds/photos_public.gne?id={$id}&lang=it-it&format=rss_200";
		$s = file_get_contents($url);
		preg_match_all('#<item>(.*)</item>#Us', $s, $items);
		$ar = array();
		for($i=0;$i<count($items[1]);$i++) {
			if($i>=$n) return $out;
			$item = $items[1][$i];
			preg_match_all('#<link>(.*)</link>#Us', $item, $temp);
			$link = $temp[1][0];
			preg_match_all('#<title>(.*)</title>#Us', $item, $temp);
			$title = $temp[1][0];
			preg_match_all('#<media:thumbnail([^>]*)>#Us', $item, $temp);
			$thumb = $this->attr($temp[0][0],"url");
			$ar['images'][$i] = $thumb;
			$ar['link'][$i] = $link;
			$ar['title'][$i] = $title;
		}
		return $ar;
	}

	public function pushMeTo($widgeturl,$text,$signature) {
		if (!function_exists("curl_init")) die("pushMeTo needs CURL module, please install CURL on your php.");
		//$widgeturl = "http://pushme.to/q/widget/export/?hash=51ff0b6e3c1ce3a7a7e473198e1b6d9a";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $widgeturl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
		$page = curl_exec($ch);
		//this gets the post_form_id value
		preg_match("/form action=\"(.*?)\"/", $page, $form_action);
		preg_match("/textarea name=\"(.*?)\"/", $page, $message_field);
		preg_match("/input type=\"text\" name=\"(.*?)\"/", $page, $signature_field);

		$ch = curl_init();
		$strpost = $message_field[1].'=' . urlencode($text) . '&'.$signature_field[1].'=' . urlencode($signature);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );
		curl_setopt($ch, CURLOPT_URL, $form_action[1]);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
		$page = curl_exec($ch);
	}

	public function googleSuggestKeywords($k) {
		if (!function_exists("curl_init")) die("googleSuggestKeywords needs CURL module, please install CURL on your php.");
		// Get all the related keywords from Google Suggest (JUST ONE WORD ALLOWED)
		// http://www.labnol.org/internet/tutorial-create-bot-for-gtalk-yahoo-messenger/4354/
		$k = explode(" ",$k); $k = $k[0];
		$u = "http://google.com/complete/search?output=toolbar&q=" . $k;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $u);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$xml = simplexml_load_string(curl_exec($ch));
		curl_close($ch);
		// Parse the keywords
		$result = $xml->xpath('//@data');
		$ar = array();
		while (list($key, $value) = each($result)) $ar[] = (string)$value;
		return $ar;
	}

	// from an address to a couple (lat,long) coordinates
	public function getLatLong($address){
		if (!is_string($address))die("All Addresses must be passed as a string");
		$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
		$_result = false;
		if($_result = file_get_contents($_url)) {
			if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
			preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
			$_coords['lat'] = $_match[1];
			$_coords['long'] = $_match[2];
		}
		return $_coords;
	}


	public function wikiDefinition($s) {
		//http://it.wikipedia.org/w/api.php?action=opensearch&search=subsonica&format=xml&limit=1
		$url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
		curl_setopt($ch, CURLOPT_POST, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, false);   // Include head as needed
		curl_setopt($ch, CURLOPT_NOBODY, FALSE);        // Return body
		curl_setopt($ch, CURLOPT_VERBOSE, FALSE);           // Minimize logs
		curl_setopt($ch, CURLOPT_REFERER, "");            // Referer value
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
		curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");   // Webbot name
		$page = curl_exec($ch);
		$xml = simplexml_load_string($page);
		if((string)$xml->Section->Item->Description) {
			return array((string)$xml->Section->Item->Text, (string)$xml->Section->Item->Description, (string)$xml->Section->Item->Url);
		} else {
			return "";
		}
	}

	public function myspaceConcerts($user) {
		$ch = curl_init("http://www.myspace.com/".$user."/shows");
		curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
		curl_setopt($ch, CURLOPT_POST, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, false);   // Include head as needed
		curl_setopt($ch, CURLOPT_NOBODY, FALSE);        // Return body
		curl_setopt($ch, CURLOPT_VERBOSE, FALSE);           // Minimize logs
		curl_setopt($ch, CURLOPT_REFERER, "");            // Referer value
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
		curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
		$page = curl_exec($ch);
		preg_match_all("#<a class=\"userLink\" href=\"/".$user."\">(.*)</a>#Us", $page, $a);
		$band = trim(strip_tags($a[1][0]));
		// months array is in italian because of from my web servers pages returns in italian
		// probably you have to change this array
		$months = array("gen"=>"01","feb"=>"02","mar"=>"03","apr"=>"04","mag"=>"05","giu"=>"06","lug"=>"07","ago"=>"08","set"=>"09","ott"=>"10","nov"=>"11","dic"=>"12");
		$out = array();
		$c=0;
		$li = preg_split("/<li class=\"moduleItem event( odd| even)?( first| last)? vevent\" ?>/i",$page);
		for($i=0;$i<count($li);$i++) {
			if(stristr($li[$i],"<div class=\"entryDate\">")) {
				//echo strip_tags($li[$i])."<hr>";
				//<span class="month"> ott</span>
				preg_match_all("#<span class=\"month\">(.*)</span>#Us", $li[$i], $temp);
				$month = $months[strip_tags(trim($temp[1][0]))];
				preg_match_all("#<span class=\"day\">(.*)</span>#Us", $li[$i], $temp);
				$day = str_pad( strip_tags(trim($temp[1][0])), 2, "0", STR_PAD_LEFT);
				$year = date("Y");
				$data = $year."-".$month."-".$day;
				if($data<date("Y-m-d")) { $data = (date("Y")+1)."-".$month."-".$day; }
				preg_match_all("#<h4>(.*)</h4>#Us", $li[$i], $temp);
				$posto = strip_tags(trim($temp[1][0]));
				preg_match_all("#<span class=\"locality\">(.*)</span>#Us", $li[$i], $temp);
				$citta = strip_tags(trim($temp[1][0]));
				preg_match_all("#<span class=\"region\">(.*)</span>#Us", $li[$i], $temp);
				$region = strip_tags(trim($temp[1][0]));
				preg_match_all("#<span class=\"country-name\">(.*)</span>#Us", $li[$i], $temp);
				$stato = strip_tags(trim($temp[1][0]));
				$out[$c]["band"] = $band;
				$out[$c]["date"] = $data;
				//$out[$c]["time"] = ""; not parsed
				$out[$c]["venue"] = $posto;
				//$out[$c]["url"] = ""; not parsed
				$out[$c]["where"] = $citta.",".$region.",".$stato;
				$c++;
			}
		}
		return $out;
	}

	public function getVimeoInfo($id, $info = 'thumbnail_medium') {
		// http://www.soapboxdave.com/2010/04/getting-the-vimeo-thumbnail/
		if (!function_exists('curl_init')) die('CURL is not installed!');
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php");
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$output = unserialize(curl_exec($ch));
		$output = $output[0][$info];
		curl_close($ch);
		return $output;
	}

	// resize video embed and iframes
	public function resizeEmbed($video,$new_width='') {
		preg_match("/width=\"([^\"]*)\"/i",$video,$w); $w = (integer)$w[1];
		preg_match("/height=\"([^\"]*)\"/i",$video,$h); $h = (integer)$h[1];
			if (!$new_width) $new_width = $w;
		$w2 = $new_width;
		$ratio = (float)($w2/$w);
		$h2 = (integer)($h * $ratio);
		$video = str_replace("width=\"$w\"","width=\"$w2\"",$video);
		$video = str_replace("height=\"$h\"","height=\"$h2\"",$video);
		//return array("embed"=>$video,"w"=>$w2,"h"=>$h2,"w0"=>$w,"h0"=>$h);
		return $video;
	}



}

?>
Back to Top