ripcurl /releases/ripcurl-0.6.0/ripcurl.class.php

Language PHP Lines 1399
MD5 Hash 178370c55e16f7d90b4aaf25a0d9b08d Estimated Cost $25,374 (why?)
Repository https://bitbucket.org/linuxdream/ripcurl 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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
<?php
//RipCURL class 0.6 - SD Linux Services
//This class was written by Brandon Ching of SD Linux Services
//It is licensed under the GPL 2 and requires PHP5 and CURL 7


class ripCurl{
	//Class definitions
	//Define the default directory for ripWrite. Must include trailing "/". Please turn safe_mode off or make the proper UID/GID changes
	//to the php.ini file or in the ripWrite function below. The directory should be located under the web root directory for links to work
	//or use a directory alias in httpd.conf.
	const WRITEDIRECTORY = "/tmp/";
	
	//Set class variables
	//curl handler
	private $ch;
	//Returned value of curl_exec. This value should never be directly written to. It is meant to be used by class methods only.
	//It always holds the current HTML of the last page to be fetched/processed.
	private $rawHtml = '';
	//Value of the last POST fields
	private $postData = '';
	//Error variable
	private $error = '';
	//Total HTTP errors for class instance
	private $httpErrors = 0;
	//Count for ripWrite
	private $writeCount = 0;
	//Count for ripGetlinks
	private $linksCount = 0;
	//Total redirects for class instance
	private $totalRedirects = 0;
	//Total time for class existance
	private $totalTime = 0;
	//Total size of information transfered
	private $totalSize = 0;
	//Average speed of all transfers
	private $averageSpeed = 0;
	//Average time per transfer
	private $averageTime = 0;
	//Total number of connections for class existance
	private $totalConnections = 0;
	//Last ripRun status code
	private $lastStatusCode = null;
	//Initialize cookiejar directory. Filename gets set in the constructor
	private $cookiejar = "/tmp/";
	//Form action of last getFormElements()
	private $formAction = "";
	//Form method of last getFormElements()
	private $formMethod = "post";
	//Maintains internal track of all links in getLinks()
	private $currentLinks = "";
	//Last URL in ripRun();
	private $lastUrl = "";
	
	//Accessor methods
	public function getRawHtml(){
		return $this->rawHtml;
	}
	
	public function getLastUrl(){
		return $this->lastUrl;
	}
	
	public function getPostData(){
		return $this->postData;
	}
	
	public function getHttpErrors(){
		return $this->httpErrors;
	}
	
	public function getWriteCount(){
		return $this->writeCount;
	}
	
	public function getLinksCount(){
		return $this->linksCount;
	}
	
	public function zeroLinksCount(){
		if($this->linksCount = 0){
			return true;
		}else{
			$this->error = "zeroLinksCount: Could not reset linksCount variable.";
			return false;
		}
	}
	
	public function getTotalRedirects(){
		return $this->totalRedirects;
	}
	
	public function getTotalTime(){
		return $this->totalTime;
	}
	
	public function getTotalSize(){
		return $this->totalSize;
	}
	
	public function getAverageSpeed(){
		return $this->averageSpeed;
	}
	
	public function getAverageTime(){
		return $this->averageTime;
	}
	
	public function getTotalConnections(){
		return $this->totalConnections;
	}
	
	public function getCookieJar(){
		return $this->cookiejar;
	}

	public function getError(){
		return $this->error;
	}
	
	public function getLastStatus(){
		return $this->lastStatus;
	}
	
	public function setCookieJar($o){
		if(is_writeable($o) && $this->cookiejar = $o){
			$this->cookieJar($o);
			$this->cookieFile($o);
			return true;
		}else{
			$this->error = "setCookieJar: Cookiejar could not be set";
			return false;
		}
	}
	
	public function getFormAction(){
		return $this->formAction;
	}
	
	public function getFormMethod(){
		return $this->formMethod;
	}
	
	public function getCurrentLinks(){
		return $this->currentLinks;
	}

	public function clearCurrentLinks(){
		if($this->currentLinks = array()){
			return true;
		}else{
			$this->error = "clearCurrentLinks: Could not clear currentLinks array";
			return false;
		}
	}
	
	public function setFormMethod($o){
		$this->formMethod = $o;
	}

	//Constructor method. Designed to set standard defaults for pulling sites into a variable
	public function ripCurl($multi = null){
			$this->cookiejar = $this->cookiejar . md5(date('l dS \of F Y h:i:s A')+rand(5, 50)) . ".cookiejar.txt";
			$this->ch = curl_init();
			curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($this->ch, CURLOPT_AUTOREFERER,1);
			curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION,1);
			curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST,0);
			curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER,0);
			curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT,30);
			curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookiejar);
			curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookiejar);
	}
	
	//Class methods for setting curlopt variables
	public function autoReferer($o = 1){
		curl_setopt($this->ch, CURLOPT_AUTOREFERER, $o);
	}
	
	public function cookieSession($o = 1){
		curl_setopt($this->ch, CURLOPT_COOKIESESSION, $o);
	}
	
	public function failOnError($o = 1){
		curl_setopt($this->ch, CURLOPT_FAILONERROR, $o);
	}
	
	public function followLocation($o = 1){
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $o);
	}
	
	public function forbidReuse($o = 1){
		curl_setopt($this->ch, CURLOPT_FORBID_REUSE, $o);
	}
	
	public function freshConnect($o = 1){
		curl_setopt($this->ch, CURLOPT_FRESH_CONNECT, $o);
	}
	
	public function showHeader($o = 1){
		curl_setopt($this->ch, CURLOPT_HEADER, $o);
	}
	
	public function httpGet($o = 1){
		curl_setopt($this->ch, CURLOPT_HTTPGET, $o);
	}
	
	public function mute($o = 1){
		curl_setopt($this->ch, CURLOPT_MUTE, $o);
	}
	
	public function noSignal($o = 1){
		curl_setopt($this->ch, CURLOPT_NOSIGNAL, $o);
	}
	
	public function post($o = 1){
		curl_setopt($this->ch, CURLOPT_POST, $o);
	}
	
	public function put($o = 1){
		curl_setopt($this->ch, CURLOPT_PUT, $o);
	}
	
	public function returnTransfer($o = 1){
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, $o);
	}
	
	public function sslVerifyPeer($o = 1){
		curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, $o);
	}
	
	public function sslVerifyHost($o = 1){
		curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, $o);
	}
	
	public function verbose($o = 1){
		curl_setopt($this->ch, CURLOPT_VERBOSE, $o);
	}
	
	public function bufferSize($o){
		curl_setopt($this->ch, CURLOPT_BUFFERSIZE, $o);
	}
	
	public function connectTimeout($o){
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $o);
	}
	
	public function dnsCacheTimeout($o){
		curl_setopt($this->ch, CURLOPT_DNS_CACHE_TIMEOUT, $o);
	}
	
	public function httpVersion($o){
		curl_setopt($this->ch, CURLOPT_HTTP_VERSION, $o);
	}
	
	public function httpAuth($o){
		curl_setopt($this->ch, CURLOPT_HTTPAUTH, $o);
	}
	
	public function maxConnects($o){
		curl_setopt($this->ch, CURLOPT_MAXCONNECTS, $o);
	}
	
	public function port($o){
		curl_setopt($this->ch, CURLOPT_PORT, $o);
	}
	
	public function maxRedirs($o){
		curl_setopt($this->ch, CURLOPT_MAXREDIRS, $o);
	}
	
	public function sslVersion($o){
		curl_setopt($this->ch, CURLOPT_SSLVERSION, $o);
	}
	
	public function timeOut($o){
		curl_setopt($this->ch, CURLOPT_TIMEOUT, $o);
	}
	
	public function cookie($o){
		curl_setopt($this->ch, CURLOPT_COOKIE, $o);
	}
	
	public function cookieJar($o){
		curl_setopt($this->ch, CURLOPT_COOKIEJAR, $o);
	}
	
	public function cookieFile($o){
		curl_setopt($this->ch, CURLOPT_COOKIEFILE, $o);
	}
	
	public function encoding($o){
		curl_setopt($this->ch, CURLOPT_ENCODING, $o);
	}
	
	public function postFields($o){
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, $o);
	}
	
	public function referer($o){
		curl_setopt($this->ch, CURLOPT_REFERER, $o);
	}
	
	public function url($o){
		curl_setopt($this->ch, CURLOPT_URL, $o);
	}
	
	public function userAgent($o){
		switch ($o){
			case 'ie6':
				$o = 'Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)';
				break;
			case 'ie7':
				$o = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)';
				break;
			case 'ff':
				$o = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6';
				break;
			case 'google':
				$o = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
				break;
			case 'msn':
				$o = 'msnbot/1.0 (+http://search.msn.com/msnbot.htm)';
				break;
			case 'yahoo':
				$o = 'Mozilla/5.0 (compatible; Yahoo! Slurp;http://help.yahoo.com/help/us/ysearch/slurp)';
				break;
		}
		curl_setopt($this->ch, CURLOPT_USERAGENT, $o);
	}
	
	//Username and password in uname:pass format
	public function userPwd($o){
		curl_setopt($this->ch, CURLOPT_USERPWD, $o);
	}
	
	public function httpHeader($o){
		curl_setopt($this->ch, CURLOPT_HTTPHEADER, $o);
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	// Begin major class methods
	//
	/////////////////////////////////////////////////////////////////////////////////////
	//Used to display a summary of the entire ripCurl session. 
	//
	//If a parameter is specified, it will return only that key value.
	//Otherwise, return an array
	//
	public function getInfo($o = null){
		if(!is_null($o)){
			$info = curl_getinfo($this->ch);
			$value = $info[$o];
			return $value;
		} else {
			$info = curl_getinfo($this->ch);
			return $info;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Get contents of cookiefile
	//
	//Returns an associative array of cookie parts
	//
	public function getCookieJarContents($cookiefile = null){
		if(is_null($cookiefile)){
			$cookiefile = $this->cookiejar;
		}

		if(!is_readable($cookiefile)){
			$this->error = "ripGetCookieContents: Cookie file is not readable";
			return false;
		}

		$contents = file_get_contents($cookiefile);
		preg_match_all('|.*\t.*|', $contents, $dough);
		$cookies = array();
		for($i = 0; $i < count($dough[0]); $i++){
			$c = explode("\t", $dough[0][$i]);
			$cookies[$i]['host'] = $c[0];
			$cookies[$i]['secure'] = $c[1];
			$cookies[$i]['path'] = $c[2];
			$cookies[$i]['httpOnly'] = $c[3];
			$cookies[$i]['expire'] = $c[4];
			$cookies[$i]['name'] = $c[5];
			$cookies[$i]['value'] = $c[6];
		}
		return $cookies;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Sets a cookie accroding to Netscape cookie files.
	//
	//Returns TRUE on successfull cookie write
	//
	public function writeCookie($cookie){
		if(!is_array($cookie) || count($cookie) != 7){
			$this->error = "setCookie: Argument must be an array with host, secure, path, httpOnly, expire, name, and value";
			return false;
		}
		$cookiefile = $this->cookiejar;
		
		if(!is_writable($cookiefile)){
			$this->error = "Cookie file is not writeable";
			return false;
		}

		$fileh = fopen($cookiefile, 'a+');
		$cookieString = implode("\t", $cookie);
		if(fwrite($fileh, $cookieString) === FALSE){
			$this->error = "setCookie: Could not write cookie to file";
			return false;
		}
		fclose($fileh);
		return true;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Pulls all HTML between the $start and $end and returns it.
	//
	//Returns array of captured content
	//
	public function ripInBetween($start, $end, $greedy = null, $html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		if(is_null($greedy)){
			$needle="|$start" . "(.*?)" . "$end|is";
		}else{//Do greedy search
			$needle="|$start" . "(.*)" . "$end|is";
		}
		preg_match_all($needle, $html, $result, PREG_PATTERN_ORDER);
		return $result[1];
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Search and replace function.
	//
	//Returns value of $html after replacement. Also sets replaced value to $rawHtml
	//
	public function sandr($search, $replace, $html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		
		$html = preg_replace("|$search|is", $replace, $html);
		$this->rawHtml = $html;
		return $html;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Removes all javascript from the passed html
	//
	//Returns clean HTML. Also sets value to $rawHtml
	//
	public function ripJS($html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		$clean = preg_replace('|<script.*?/script>|is', '', $html);
		$this->rawHtml = $clean;
		return $clean;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Removes all style sheets and <style> contents.
	//
	//Returns clean HTML. Also sets value to $rawHtml
	//
	public function ripStyles($html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		$clean = preg_replace('|<style.*?/style>|is', '', $html);
		$this->rawHtml = $clean;
		return $clean;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Method to get all links in a page. If the $id is specified, returned links must 
	//contain $id. Link counts are automatically tracked in the $this->linksCount class 
	//variable
	//
	//Returns array of all links found with $id specified
	//
	public function getLinks($id = null, $html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		if(is_null($id)){
			$id = ".*";
		}
	 $links = array();
	 $pattern = '/<A\s*HREF=[\"\']?([^\"\'>]*)[\"\']?[^>]*>(.*?)<\/A>/is';
	 $idpattern = '|' . $id . '|';
	 if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
   		if(!is_null($id)){
	   		foreach($matches as $match){ 
	   			if(preg_match($idpattern, $match[1])){
						array_push($links, $match[1]);
	   			}
	   		}
   		} else {
			$links = $matches;
		}
   }
   
   $links = array_unique($links);
   $this->linksCount = $this->linksCount + count($links);
   
   $this->currentLinks = $links;

   return $links;
	}
	
	

	/////////////////////////////////////////////////////////////////////////////////////
	//Like getLinks method but for links with javascript in the href portion
	//
	//Returns array of all links found with $id
	//
	public function getWeirdLinks($id = null, $html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		if(is_null($id)){
			$id = ".*";
		}
		
		$return = array();
		$doc = new DOMDocument();
		@$doc->loadHTML($html);
		$tags = $doc->getElementsByTagName('a');
		foreach($tags as $tag){
			$href = $tag->getAttribute('href');
			if(preg_match("|$id|s", $href)){
				$return[] = $href;
			}
		}
		
		$return = array_unique($return);		
		$this->currentLinks = $return;
   	
		return $return;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Search for string within $html. Set $caseSensitive to true for case sensitive search
	//
	//Returns true if $needle found within 
	//
	public function printLinks($links = null){
		if(is_null($links)){
			$links = $this->currentLinks;
		}
		
		if(!is_array($links) || count($links) < 1){
			$this->error = 'printLinks: $links is not an array or has no elements.';
			return false;
		}
		
		foreach($links as $link){
			echo "<a href=$link>Link</a><br />\n";
		}
		return true;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Search for string within $html. Set $caseSensitive to true for case sensitive search
	//
	//Returns true if $needle found within 
	//
	public function inData($needle, $caseSensitive = null, $html = null){
		if(is_null($html)){
			$html = $this->getRawHtml();
		}
		
		if(is_null($caseSensitive)){
			$search = "|$needle|is";
		}else{
			$search = "|$needle|s";
		}
		
		if(preg_match($search, $html)){
			return true;
		}else{
			return false;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Deletes specified directory and its contents.
	//
	//Returns TRUE if directory and contents were successfully deleted
	//
	public function clearDir($dir){
		//remove leading / if it's there
		if (substr($dir, 0, 1) == '/'){
			preg_match('|/(.*)|', $dir, $match);
			$dir=$match[1];
		}
		$ogdir = $dir;
		$dir = self::WRITEDIRECTORY . $dir;
		
		if ($handle = @opendir("$dir")) {
			while (false !== ($item = readdir($handle))) {
				if ($item != "." && $item != "..") {
					if(!@unlink("$dir/$item")){
			 			if(is_dir("$dir/$item")){
							$this->clearDir("$ogdir/$item");
						}
        	}
				}
     	}

     	closedir($handle);
   		}else{
   			$this->error = "clearDir: Could not open $dir for delete";
   			return false;
   		}
		
		if(rmdir($dir)){
			return true;
		} else {
			$this->error = "clearDir: There was a problem clearing $dir.";
			return false;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Writes the value of $html to $dir directory. If $html is not specified, it uses 
	//$this->rawHtml. If safe_mode is on in php.ini, directories must be same UID/GID 
	//(if relaxed to GID) as the sctipt calling this function. If a filename is specified, 
	//it will write to that file, otherwise, it will create a unique filename in the dir 
	//specified. Filename must have .extension
	//
	//Returns TRUE if write was successfull
	//
	public function write($dir, $html = null){
		//If no data was passed, assign the class variable
		if(is_null($html)){
			$html = $this->rawHtml;
		}
		//remove leading / if it's there
		if (substr($dir, 0, 1) == '/'){
			preg_match('|/(.*)|', $dir, $match);
		  $dir=$match[1];
		}

		$dir = self::WRITEDIRECTORY . $dir;
		$path_info = pathinfo($dir);
		if(!strstr($path_info['basename'], '.')){
			if (substr($dir, -1, 1) != '/'){
				$dir=$dir . "/";
			}
			//Check if dir already exists
			if(!is_dir($dir)){
				//file is not specified, create directory
				if(exec('mkdir -p ' . $dir)){
					$this->error = "write: Creation of $dir was unsuccessfull";
					return false;
				}
			}
		}else{
			//file was specified, create path upto filename
			if(!is_dir($path_info['dirname'])){
				if(exec('mkdir -p '. $path_info['dirname'])){
					$this->error = "write: Creation of $dir was unsuccessfull";
					return false;
				}
			}
		}
		//If directory was specified, cd to it and enable random filenames.
		if(is_dir($dir)){
			chdir($dir);
			$randomName = 1;
		}else{
			//Dir not specified, just cd into dirname to create file. 
			chdir($path_info['dirname']);
		}
																																											
		//Assures a unique filename and no duplication
		//Check for specified file, if not, then create unique filename
		if($randomName){
			$hash = md5($html) . ".html";
			$abspath = $dir . $hash;
			$file = fopen($abspath, "w");
		}else{
			$file = fopen($dir, 'w');
		}
		
		if(fwrite($file, $html)){
			$this->writeCount++;
		} else {
			$this->error = "write: There was an error writing to $abspath.";
			return false;
		}
		fclose($file);

		return true;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Writes $image to $image_name. $image_name will be appended to WRITEDIRECTORY constant.
	//$image can be actual binary image or image URL
	//
	//Returns true if image wrote to disk successfully
	//
	public function writeImage($image, $img_name){
		if(empty($image)){
			$this->error = "writeImage: There is no image to write.";
			return false;
		}
		
		//remove leading / if it's there
		if (substr($img_name, 0, 1) == '/'){
			preg_match('|/(.*)|', $img_name, $match);
		  $img_name=$match[1];
		}

		$img_name = self::WRITEDIRECTORY . $img_name;
		$path_info = pathinfo($img_name);
		//file was specified, create path upto filename
		if(!is_dir($path_info['dirname'])){
			if(exec('mkdir -p '. $path_info['dirname'])){
				$this->error = "write: Creation of $img_name was unsuccessfull";
				return false;
			}
		}
		
		//Allows for either actual binary image file or web address
		if(preg_match('/^[http:|https:|ftp:]/i', $image)){
			$image = $this->ripRun($image);
		}
		
		$file = fopen($img_name, "w");
		if(fwrite($file, $image)){
			return true;
		} else {
			$this->error = "writeImage: Could not write image";
			return false;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Removes any alert or confirm javascript popups.
	//
	//Returns clean HTML. Also sets $rawHtml to clean value.
	public function ripJSPopups($html = null){
		if(is_null($html)){
			$html = $this->rawHtml;
		}
		
		$clean = preg_replace('/alert\(.*?\)[;]/', '', $html);
		$realclean = preg_replace('/confirm\(.*?\)[;]/', '', $clean);
		$this->rawHtml = $realclean;
		return $this->rawHtml;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Makes all ralative links absolute with the provided $basrUrl. This function is usually
	//only used by ripRun() when specified in argument 2.
	//
	//Returns clean HTML. Also sets $rawHtml to cleaned value
	//
	public function fixLinks($baseUrl, $html = null){
    if(is_null($html)){
			$html = $this->rawHtml;
		}
		
		$tagAttributes=array(
      'table'=>'background',
      'td'=>'background',
      'tr'=>'background',
      'th'=>'background',
      'body'=>'background',
      'a'=>'href',
      'link'=>'href',
      'area'=>'href',
      'form'=>'action',
      'script'=>'src',
      'img'=>'src',
      'input'=>'src',
      'iframe'=>'src',
			'frame'=>'src',
      'embed'=>'src');

    
    //Get hostname for relative URL's
    $host = parse_url($baseUrl);
    $host = $host['scheme'] . "://" . $host['host'];
    
    if(preg_match('/<base(?:.*?)href=["\']?([^\'"\s]*)[\'"\s]?/is', $html, $base)){
    	$baseUrl = $base[1];
    	$host = $baseUrl;
    }
        
    // Append a trailing slash to the url if it doesn't exist
    if (substr($baseUrl, -1, 1) !='/'){
      $baseUrl.='/';
    }
		
    //Since we process $html through DOM, it replaces HTML special chars with ?
    //Anyone know of a better solution, changing the DOM character encoding does not work.
    $encodingValues = array(
    												'&nbsp;'=>'s%s%',
    												'&copy;'=>'c%c%',
    												'&sect;'=>'se%se%',
    												'&amp;'=>'a%a%',
    												'&cent;'=>'ce%ce%',
    												'&pound;'=>'p%p%',
    												'&reg;'=>'r%r%',
    												'&yen;'=>'y%y%',
    												'&deg;'=>'d%d%',
    												'&laquo;'=>'l%l%',
    												'&quot;'=>'q%q%',
    												'&lt;'=>'lt%lt%',
    												'&gt;'=>'g%g%',
    												'&trade;'=>'t%t%',
    												'&raquo;'=>'ra%ra%'
    												);
    												
    foreach($encodingValues as $code=>$replace){
    	$html = preg_replace("|$code|i", $replace, $html);
    }
    	
    $doc = new DOMDocument();
  	@$doc->loadHTML($html);
  	foreach($tagAttributes as $tag=>$attribute){
	  	$tagType = $doc->getElementsByTagName($tag);
	  	foreach($tagType as $link){
	  		//Link is relative
		  	$url = $link->getAttribute($attribute);
		  	if(substr($url, 0, 1) == "/"){
		  		if(substr($url, 1, 2) == ".."){
		  			//Leave as if relative since /../needs to maintain its current path
		  			$link->setAttribute($attribute, $baseUrl . $link->getAttribute($attribute));
		  		}
		  		$link->setAttribute($attribute, $host . $link->getAttribute($attribute));
		  	}elseif(preg_match('/^https?:|ftp:|javascript:/', $url)){
		  		//Don't need to change anything
		  		continue;
		  	}else{
		  		$link->setAttribute($attribute, $baseUrl . $link->getAttribute($attribute));
		  	}
	  	}
  	}
  	$html = $doc->saveHTML();
  	
  	foreach($encodingValues as $code=>$replace){
    	$html = preg_replace("|$replace|i", $code, $html);
    }
  	
    //Need to catch the @import styles
    if(preg_match('|<style.*?@import.*?</style>|is', $html)){
    	$data = $this->ripInBetween('<style.*?>', '</style>', null, $html);
    	foreach($data as $link){
    		preg_match_all('$[\(|\'|"](.*?)[\)|\'|"]$s', $link, $matches);
    		$tail = $matches[1][0];
    		if(!preg_match('|^https?:|is', $tail)){
    			if(!substr($tail, 0, 1 == "/")){
    				$html = $this->sandr($tail, $baseUrl . $tail, $html);
    			}
    		}
    	}
    }
    
  	$this->rawHtml = $html;
    return $html;
  }

 
  /////////////////////////////////////////////////////////////////////////////////////
	//Find all links to other files...inside body, img, iframe, etc...
	//
	//Returns array of all possible external links found
	//
	function getAllLinks($html = null){
		if(is_null($html)){
			$html = $this->rawHtml;
		}

		$links = array();
		
	 $tagAttributes=array(
	  'table'=>'background',
		'td'=>'background',
		'tr'=>'background',
		'th'=>'background',
		'body'=>'background',
		'a'=>'href',
		'link'=>'href',
		'area'=>'href',
		'form'=>'action',
		'script'=>'src',
		'img'=>'src',
		'iframe'=>'src',
		'input'=>'src',
		'frame'=>'src',
		'embed'=>'src');
		
		// Single, double, and no quotes are both supported
		foreach($tagAttributes as $tag=>$attribute){
			$pattern="/<$tag([^>]*) $attribute=[\"']?([^\"' ]*)[\"']?/is";
			preg_match_all($pattern, $html, $matches);
			$links = array_merge($links, $matches[2]);
		}

		return $links;
	}

	
	/////////////////////////////////////////////////////////////////////////////////////
	//Finds all hidden form elements within specified $formName. If empty, it searches 
	//the entire document.
	//
	//Returns asociative array (element name=>element value) of found elements and their 
	//current values
	//
	public function getHiddenFormElements($formName = null, $html = null){
		if(is_null($html)){
			$html = $this->rawHtml;
		}
			
		$elementsArray = array();
		
		$page = new DOMDocument();
		$page->preserveWhiteSpace=false;
		@$page->loadHTML($html);
		
		//Look for the form that matches given name
		$forms = $page->getElementsByTagName('form');
		foreach($forms as $form){
			if(is_null($formName) || ($form->getAttribute('name') == $formName)){
				$currentForm = $form;
			}
		}
		
		$inputElements = $currentForm->getElementsByTagName('input');
		foreach($inputElements as $element){
			$name = $element->getAttribute('name');
			$value = $element->getAttribute('value');
			$type = $element->getAttribute('type');
			
			if(strtolower($type) == 'hidden'){
				if(array_key_exists($name, $elementsArray)){
					if(is_array($elementsArray[$name])){
						$elementsArray[$name][] = $value;
					}else{
						$elementsArray[$name] = array($elementsArray[$name], $value);
					}
				}else{
					$elementsArray[$name] = $value;
				}
			}
		}
				
		return $elementsArray;
	}


	/////////////////////////////////////////////////////////////////////////////////////
	//Finds all form elements within specified $formName. If empty, it searches the entire 
	//document.
	//
	//Returns asociative array (element name=>element value) of found elements and their 
	//current values
	//
	public function getFormElements($formName = null, $html = null){
		if(is_null($html)){
			$html = $this->rawHtml;
		}
		
		$elementsArray = array();
		
		$page = new DOMDocument();
		$page->preserveWhiteSpace=false;
		@$page->loadHTML($html);
		
		//Look for the form that matches given name
		$forms = $page->getElementsByTagName('form');
		foreach($forms as $form){
			if(is_null($formName) || ($form->getAttribute('name') == $formName)){
				$currentForm = $form;
			}
		}
		
		if(!is_object($currentForm)){
			$this->error = "getFormElements: HTML contains no forms.";
			return false;
		}
		
		//Set form info class variables
		$this->formAction = $currentForm->getAttribute('action');
		$this->formMethod = $currentForm->getAttribute('method');
		
		//Process for input elements
		$inputElements = $currentForm->getElementsByTagName('input');
		foreach($inputElements as $element){
			$name = $element->getAttribute('name');
			$value = $element->getAttribute('value');
			$type = $element->getAttribute('type');
			
			switch (strtolower($type)){
				case 'radio':
					if(!$element->attributes->getNamedItem('checked')){
						continue 2;
					}
					break;
				case 'checkbox':
					if(!$element->attributes->getNamedItem('checked')){
						continue 2;
					}
					break;
				case 'submit';
					continue 2;
					break;
				case 'button';
					continue 2;
					break;
				default:
					
			}
			
			if(array_key_exists($name, $elementsArray)){
				if(is_array($elementsArray[$name])){
					$elementsArray[$name][] = $value;
				}else{
					$elementsArray[$name] = array($elementsArray[$name], $value);
				}
			}else{
				$elementsArray[$name] = $value;
			}
			
		}
		
		//Process for select options
		$selectElements = $currentForm->getElementsByTagName('select');
		foreach($selectElements as $element){
			$name = $element->getAttribute('name');
			$options = $element->getElementsByTagName('option');
			foreach($options as $option){
				$value = $option->getAttribute('value');
				$optionValue = $option->getAttribute('value');
				if($option->attributes->getNamedItem('selected')){
					if(array_key_exists($name, $elementsArray)){
						if(is_array($elementsArray[$name])){
							$elementsArray[$name][] = $value;
						}else{
							$elementsArray[$name] = array($elementsArray[$name], $value);
						}
					}else{
						$elementsArray[$name] = $value;
					}
				}
			}
		}
		
		//Process for textarea
		$textElements = $currentForm->getElementsByTagName('textarea');
		foreach($textElements as $element){
			$name = $element->getAttribute('name');
			$value = $element->textContent;
			if(array_key_exists($name, $elementsArray)){
				if(is_array($elementsArray[$name])){
					$elementsArray[$name][] = $value;
				}else{
					$elementsArray[$name] = array($elementsArray[$name], $value);
				}
			}else{
				$elementsArray[$name] = $value;
			}
		}
		
		return $elementsArray;
	}

	
	/////////////////////////////////////////////////////////////////////////////////////
  //Gets form information
  //
  //Returns array of form information
  //
  public function getFormInfo($formName = null, $html = null){
    if(is_null($html)){
      $html = $this->rawHtml;
    }
    
		$page = new DOMDocument();
		$page->preserveWhiteSpace=false;
		@$page->loadHTML($html);
		
		//Look for the form that matches given name
		$forms = $page->getElementsByTagName('form');
		foreach($forms as $form){
			if(is_null($formName) || ($form->getAttribute('name') == $formName)){
				$currentForm = $form;
			}
		}
		
		//Set form info class variables
		$this->formAction = $currentForm->getAttribute('action');
		$return['action'] = $this->formAction;
		$this->formMethod = $currentForm->getAttribute('method');
		$return['method'] = $this->formMethod;

    return $return;

  }

	
	/////////////////////////////////////////////////////////////////////////////////////
  //Some general statistics for the current ripCurl session. This is a cumulative total
  //of all ripRun()'s.
  //
  //Returns HTML of totals.
  //
	public function getStats(){
		return $stats = "<br /><br />
											Total Connections: $this->totalConnections<br />
											Total Time: $this->totalTime<br />
											Total Download Size: $this->totalSize<br />
											Average Connection Speed: $this->averageSpeed<br />
											Total Redirects: $this->totalRedirects<br />
											Average Time per Connection: $this->averageTime<br />";
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Assigns POST data and enables POST settings in the CURL object. Primarily used 
	//from 3rd argument of ripRun().
	//
	//Returns true if settings set and data ready for ripRun().
	//
	public function postRequest($data){
		if(empty($data)){
			$this->error = "postRequest: No post data specified";
			return false;
		}
		
		$this->post();
		$string = "";
		if(!is_array($data)){
			$this->postFields($data);
			$this->postData = $string;
		} else {
			foreach($data as $k=>$v){
				if(is_array($v)){
					foreach($v as $subval){
						$string .= urlencode($k) . "=" . urlencode($subval) . "&";
					}
				}else{
					$string .= urlencode($k) . "=" . urlencode($v) . "&";
				}
			}
			$string = rtrim($string, '&');
			$this->postFields($string);
			$this->postData = $string;
			return true;
			}
		}

		
	/////////////////////////////////////////////////////////////////////////////////////
	//Sets up data for get requests
	//	
	//Returns query string
	public function getRequest($data){
		$string = "?";
		if(!is_array($data)){
			return $data;
		} else {
			foreach($data as $k=>$v){
				if(is_array($v)){
					foreach($v as $subval){
						$string .= urlencode($k) . "=" . urlencode($subval) . "&";
					}
				}else{
					$string .= urlencode($k) . "=" . urlencode($v) . "&";
				}
			}
			$string = rtrim($string, '&');
			return $string;
		}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Remove all HTML comments
	//
	//Returns HTML stripped of comments.
	//
	public function stripComments($html = null){
		if(is_null($html)){
      $html = $this->rawHtml;
    }
    $html = $this->sandr('<!--.*?-->', '', $html);
    $this->rawHtml = $html;
    return $html;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Retrieve 'href' value for a given link value
	//
	//Returns array of matching values
	//
	public function getNamedLinks($id, $html = null){
		if(is_null($html)){
      $html = $this->rawHtml;
    }
    
		$return = array();
		$doc = new DOMDocument();
		@$doc->loadHTML($html);
		$tags = $doc->getElementsByTagName('a');
		foreach($tags as $tag){
			$href = $tag->getAttribute('href');
			$link = $tag->nodeValue;
			if(preg_match("|$id|s", $link)){
				$return[] = $href;
			}
		}
		unset($doc);
		$return = array_unique($return);
		return $return;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Main ripCurl execution method. $url is the URL to pull from. if not set, it is taken 
	//from $this->ripUrl. $fixlinks is boolean option to automatically fix relative links. 
	//$postdata can be array or urlencoded string. Result page is both returned and set 
	//in $this->rawHtml.
	//
	//Returns HTML content retrieved from URL query.
	//
	public function ripRun($url, $fixLinks = null, $postdata = null){
				
		$this->url($url);
		
		if(!is_null($postdata)){
			if(strtolower($this->formMethod) == 'get'){
				$url = $url . $this->getRequest($postdata);
				$this->url($url);
			}else{
				$this->url($url);
				$this->postRequest($postdata);
			}
		}
		
		$html = curl_exec($this->ch);
		
		$this->rawHtml = $html;
		$this->lastUrl = $url;
		//$this->lastUrl = curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL);
		
		if($fixLinks){
			$parse = parse_url($url);
			if(preg_match('|.*?/([^/]*\..*?[^/]*)|is', $parse['path'], $file)){
				$parse['path'] = preg_replace('|(.*?/)([^/]*\..*?[^/]*)|is', '$1', $parse['path']);
			}
			$host = $parse['scheme'] . "://" . $parse['host'] . $parse['path'];
			//$host = $path;
			$this->fixLinks($host, $this->rawHtml);
		}
		
		//Stats tracking
		$this->totalConnections = $this->totalConnections + 1;
		$this->totalTime = $this->totalTime + $this->getInfo('total_time');
		$this->totalSize = $this->totalSize + $this->getInfo('size_download');
		$this->averageSpeed = ($this->averageSpeed + $this->getInfo('speed_download')) / $this->totalConnections;
		$redir = $this->getInfo('redirect_time');
		
		if(!empty($redir)){
			$this->totalRedirects = $this->totalRedirects + 1;
		}
		$this->averageTime = $this->totalTime / $this->totalConnections;

		$this->lastStatus = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

		return $this->getRawHtml();
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//XML methods
	/////////////////////////////////////////////////////////////////////////////////////
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Parses XML file or http address where the output is valid XML
	//
	//Returns array
	//
	public function getXMLArray($filename){
		if(!is_file($filename)){
			$this->error = "getXMLArray: Bad filename $filename";
			return false;
		}
		
		$contents = file_get_contents($filename);
		
		if(!$this->inData('^<\?[.*?]xml version=[.*?]\?>', null, $contents)){
			$contents = '<?xml version="1.0"?>' . $contents;
		}
		
		$xml= new DOMDocument();
		$xml->preserveWhiteSpace=false;
		$xml->loadXML($contents);
		$result = $this->xml2array($xml);

		return $result;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Creates array of XML file or existing XML array and pull out only the desired named
	//fields.
	//
	//Returns array of named fields
	//
	public function getXMLElements($filename, $elementName){
		if(is_file($filename) && is_readable($filename)){
			$data = $this->getXMLArray($filename);
		}elseif(is_array($filename)){
			$data = $filename;
		}else{
			$this->error = "getXMLElements: Bad filename or array given";
			return false;
		}
		
		foreach($data as $k=>$v){
			if(is_array($v)){
				$this->getXMLElements($v, $elementName);
			}elseif($k == $elementName){
				$return[] = $v;
			}
		}
		return $return;
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//Helper function for XML parsing
	//
	//Returns XML structure as array
	// 
	private function xml2array($n){
		$return=array();
		foreach($n->childNodes as $nc){
			if($nc->hasChildNodes()){
				if($n->firstChild->nodeName== $n->lastChild->nodeName&&$n->childNodes->length>1){
					$return[$nc->nodeName][]=$this->xml2array($nc);
				}else{
					$return[$nc->nodeName]=$this->xml2array($nc);
				}
			}else{
					$return=$nc->nodeValue;
				}
			}
		return $return;
	}	
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	//This section is to layout future development ideas and projects
	//
	//Expansion ideas:
	//Expand image features with GD lib
	//Build XML parsing methods
	//Include validation checks for XML, HTML, CSS, etc.
	//Include limited JavaScript parsing capabilities
	//Build pagination methods with callback functionality
	//
	/////////////////////////////////////////////////////////////////////////////////////
	
}


?>
Back to Top