jfusion /administrator/components/com_jfusion/models/model.curl.php

Language PHP Lines 1524
MD5 Hash 1f1274789a89827fbdc0311925f6a70c Estimated Cost $29,832 (why?)
Repository http://jfusion.googlecode.com/svn/trunk/ View Raw File
   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
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
<?php

/**
 * curl login model
 *
 * PHP version 5
 *
 * @category  JFusion
 * @package   Models
 * @author    Henk Wevers <henk@wevers.net>
 * @copyright 2008 - 2011  JFusion - Henk Wevers. All rights reserved.
 * @license   http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link      http://www.jfusion.org
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

/**
 * HTML Form Parser
 * This will extract all forms and his elements in an
 * big assoc Array.
 * Many modifications and bug repairs by Henk Wevers
 *
 * @category  JFusion
 * @package   Models
 * @author    Peter Valicek <Sonny2@gmx.DE>
 * @copyright 2004 Peter Valicek Peter Valicek <Sonny2@gmx.DE>: GPL-2
 * @license   http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link      http://www.jfusion.org
 */
class JFusionCurlHtmlFormParser
{

	var $html_data = '';
	var $_return = array();
	var $_counter = '';
	var $button_counter = '';
	var $_unique_id = '';
	/**
	 * html form parser
	 *
	 * @param string $html_data the actual html string
	 *
	 * @return array html elements
	 */
	function JFusionCurlHtmlFormParser($html_data)
	{
		if (is_array($html_data)) {
			$this->html_data = join('', $html_data);
		} else {
			$this->html_data = $html_data;
		}
		$this->_return = array();
		$this->_counter = 0;
		$this->button_counter = 0;
		$this->_unique_id = md5(time());
	}

	/**
	 * Parses the forms
	 *
	 * @return string nothing
	 */
	function parseForms()
	{
		if (preg_match_all("/<form.*>.+<\/form>/isU", $this->html_data, $forms)) {
			foreach ($forms[0] as $form) {
				$this->button_counter = 0;

				//form details
				preg_match("/<form.*?name=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $form_name);
				if ($form_name) {
					$this->_return[$this->_counter]['form_data']['name'] = preg_replace("/[\"'<>]/", "", $form_name[1]);
				}
				preg_match("/<form.*?action=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/i", $form, $action);
				if ($action) {
					$this->_return[$this->_counter]['form_data']['action'] = preg_replace("/[\"'<>]/", "", $action[1]);
				}
				preg_match("/<form.*?method=[\"']?([\w\s]*)[\"']?[\s>]/i", $form, $method);
				if ($method) {
					$this->_return[$this->_counter]['form_data']['method'] = preg_replace("/[\"'<>]/", "", $method[1]);
				}
				preg_match("/<form.*?enctype=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/i", $form, $enctype);
				if ($enctype) {
					$this->_return[$this->_counter]['form_data']['enctype'] = preg_replace("/[\"'<>]/", "", $enctype[1]);
				}
				preg_match("/<form.*?id=[\"']?([\w\s-]*)[\"']?[\s>]/i", $form, $id);
				if ($id) {
					$this->_return[$this->_counter]['form_data']['id'] = preg_replace("/[\"'<>]/", "", $id[1]);
				}

				// form elements: input type = hidden
				if (preg_match_all("/<input[^<>]+type=[\"']hidden[\"'][^<>]*>/iU", $form, $hiddens)) {
					foreach ($hiddens[0] as $hidden) {
						$this->_return[$this->_counter]['form_elements'][$this->_getName($hidden)] = array(
                            'type'  =>  'hidden',
                            'value'  =>  $this->_getValue($hidden)
						);
					}
				}

				// form elements: input type = text
				if (preg_match_all("/<input[^<>]+type=[\"']text[\"'][^<>]*>/iU", $form, $texts)) {
					foreach ($texts[0] as $text) {
						$this->_return[$this->_counter]['form_elements'][$this->_getName($text)] = array(
                            'type'  => 'text',
                            'value'  =>  $this->_getValue($text),
                            'id'  =>  $this->_getId($text),
                            'class'  =>  $this->_getClass($text)
						);
					}
				}

				// form elements: input type = password
				if (preg_match_all("/<input[^<>]+type=[\"']password[\"'][^<>]*>/iU", $form, $passwords)) {
					foreach ($passwords[0] as $password) {
						$this->_return[$this->_counter]['form_elements'][$this->_getName($password)] = array(
                            'type'  =>  'password',
                            'value'  =>  $this->_getValue($password)
						);
					}
				}

				// form elements: textarea
				if (preg_match_all("/<textarea.*>.*<\/textarea>/isU", $form, $textareas)) {
					foreach ($textareas[0] as $textarea) {
						preg_match("/<textarea.*>(.*)<\/textarea>/isU", $textarea, $textarea_value);
						$this->_return[$this->_counter]['form_elements'][$this->_getName($textarea)] = array(
                            'type'  =>  'textarea',
                            'value'  =>  $textarea_value[1]
						);
					}
				}

				// form elements: input type = checkbox
				if (preg_match_all("/<input[^<>]+type=[\"']checkbox[\"'][^<>]*>/iU", $form, $checkboxes)) {
					foreach ($checkboxes[0] as $checkbox) {
						if (preg_match("/checked/i", $checkbox)) {
							$this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] = array(
                                'type'  =>  'checkbox',
                                'value'  =>  'on'
							);
						} else {
							$this->_return[$this->_counter]['form_elements'][$this->_getName($checkbox)] = array(
                                'type'  =>  'checkbox',
                                'value'  =>  ''
							);
						}
					}
				}

				// form elements: input type = radio
				if (preg_match_all("/<input[^<>]+type=[\"']radio[\"'][^<>]*>/iU", $form, $radios)) {
					foreach ($radios[0] as $radio) {
						if (preg_match("/checked/i", $radio)) {
							$this->_return[$this->_counter]['form_elements'][$this->_getName($radio)] = array(
                                'type'  =>  'radio',
                                'value'  =>  $this->_getValue($radio)
							);
						}
					}
				}

				// form elements: input type = submit
				if (preg_match_all("/<input[^<>]+type=[\"']submit[\"'][^<>]*>/iU", $form, $submits)) {
					foreach ($submits[0] as $submit) {
						$this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
                            'type'  => 'submit',
                            'name'  => $this->_getName($submit),
                            'value'  => $this->_getValue($submit)
						);
						$this->button_counter++;
					}
				}

				// form elements: input type = button
				if (preg_match_all("/<input[^<>]+type=[\"']button[\"'][^<>]*>/iU", $form, $buttons)) {
					foreach ($buttons[0] as $button) {
						$this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
                            'type'  => 'button',
                            'name'  => $this->_getName($button),
                            'value'  => $this->_getValue($button)
						);
						$this->button_counter++;
					}
				}

				// form elements: input type = reset
				if (preg_match_all("/<input[^<>]+type=[\"']reset[\"'][^<>]*>/iU", $form, $resets)) {
					foreach ($resets[0] as $reset) {
						$this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
                            'type'  => 'reset',
                            'name'  => $this->_getName($reset),
                            'value'  => $this->_getValue($reset)
						);
						$this->button_counter++;
					}
				}

				// form elements: input type = image
				if (preg_match_all("/<input[^<>]+type=[\"']image[\"'][^<>]*>/iU", $form, $images)) {
					foreach ($images[0] as $image) {
						$this->_return[$this->_counter]['buttons'][$this->button_counter] = array(
                            'type'  => 'image',
                            'name'  => $this->_getName($image),
                            'value'  => $this->_getValue($image)
						);
						$this->button_counter++;
					}
				}

				// input type=select entries
				// Here I have to go on step around to grep at first all select names and then
				// the content. Seems not to work in an other way
				if (preg_match_all("/<select.*>.+<\/select>/isU", $form, $selects)) {
					foreach ($selects[0] as $select) {
						if (preg_match_all("/<option.*>.+<\/option>/isU", $select, $all_options)) {
							foreach ($all_options[0] as $option) {
								if (preg_match("/selected/i", $option)) {
									if (preg_match("/value=[\"'](.*)[\"']\s/iU", $option, $option_value)) {
										$option_value = $option_value[1];
										$found_selected = 1;
									} else {
										preg_match("/<option.*>(.*)<\/option>/isU", $option, $option_value);
										$option_value = $option_value[1];
										$found_selected = 1;
									}
								}
							}
							if (!isset($found_selected)) {
								if (preg_match("/value=[\"'](.*)[\"']/iU", $all_options[0][0], $option_value)) {
									$option_value = $option_value[1];
								} else {
									preg_match("/<option>(.*)<\/option>/iU", $all_options[0][0], $option_value);
									$option_value = $option_value[1];
								}
							} else {
								unset($found_selected);
							}
							$this->_return[$this->_counter]['form_elements'][$this->_getName($select)] = array(
                                'type'  => 'select',
                                'value'  => trim($option_value)
							);
						}
					}
				}

				# form elements: input type = --not defined--
				if ( preg_match_all("/<input[^<>]+name=[\"'](.*)[\"'][^<>]*>/iU", $form, $inputs)) {
					foreach ( $inputs[0] as $input ) {
						if ( !preg_match("/type=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $input) ) {
							if ( !isset($this->_return[$this->_counter]['form_elements'][$this->_getName($input)]) ) {
								$this->_return[$this->_counter]['form_elements'][$this->_getName($input)] =
								array(
                                    'type'  => 'text',
                                    'value'  =>  $this->_getValue($input),
                                    'id'  =>  $this->_getId($input),
                                    'class'  =>  $this->_getClass($input)
								);

							}
						}
					}
				}

				// Update the form counter if we have more then 1 form in the HTML table
				$this->_counter++;
			}
		}
		return $this->_return;
	}

	/**
	 * gets the name
	 *
	 * @param string $string string
	 *
	 * @return string something
	 */
	function _getName($string)
	{
		if (preg_match("/name=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
			//preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match)) { -- did not work as expected
			$val_match = trim($match[1]);
			$val_match = trim($val_match, "\"\'");
			unset($string);
			return trim($val_match, '"');
		}
	}

	/**
	 * gets the value
	 *
	 * @param string $string string
	 *
	 * @return string something
	 */
	function _getValue($string)
	{
		if (preg_match("/value=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
			$val_match = trim($match[1]);
			$val_match = trim($val_match, "\"\'");
			unset($string);
			return $val_match;
		}
	}

	/**
	 * gets the id
	 *
	 * @param string $string string
	 *
	 * @return string something
	 */
	function _getId($string)
	{
		if (preg_match("/id=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
			//preg_match("/name=[\"']?([\w\s]*)[\"']?[\s>]/i", $string, $match)) { -- did not work as expected
			$val_match = trim($match[1]);
			$val_match = trim($val_match, "\"\'");
			unset($string);
			return $val_match;
		}
	}

	/**
	 * gets the class
	 *
	 * @param string $string string
	 *
	 * @return string something
	 */
	function _getClass($string)
	{
		if (preg_match("/class=(\"([^\"]*)\"|'([^']*)'|[^>\s]*)([^>]*)?>/is", $string, $match)) {
			$val_match = trim($match[1]);
			$val_match = trim($val_match, "\"\'");
			unset($string);
			return $val_match;
		}
	}
}

/**
 * Singleton static only class that creates instances for each specific JFusion plugin.
 *
 * @category  JFusion
 * @package   Models
 * @author    Henk Wevers <henk@wevers.net>
 * @copyright 2008 JFusion - Henk Wevers. All rights reserved.
 * @license   http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link      http://www.jfusion.org
 */
class JFusionCurl
{
	/**
	 * NOTE: The routines buildcookie and imlodeCookies are identical to the ones in model.cookie.
	 * They are duplicated here because I want this file selfcontained so it can be used by the DSSO routines in
	 * the integrations. All you need to do there is to overload the JFusionCurl:: _ function to translate strings
	 **/

	/**
	 * Translate function, mimics the php gettext (alias _) function
	 *
	 * Do not overload when used within Joomla, the function simply calls Jtext::_
	 * When you use the JFusionCurl class outside Joomla, f.i. as part of an DSSO extension in an integration
	 * then you have to overload this function to provide the translated strings probably using native code
	 *
	 * @param string  $string The string to translate
	 * @param boolean $jsSafe Make the result javascript safe
	 *
	 * @return string The translation of the string
	 **/
	public function _($string, $jsSafe = false)
	{
		return JText::_($string, $jsSafe);
	}

	/**
	 * Retrieve the cookies as a string cookiename=cookievalue; or as an array
	 *
	 * @param string $type the type
	 *
	 * @return string or array
	 */
	public function buildCookie($type = 'string')
	{
		switch ($type) {
			case 'array':
				return $_COOKIE;
				break;
			case 'string':
			default:
				return JFusionCurl::implodeCookies($_COOKIE, ';');
				break;
		}
		return false;
	}

	/**
	 * Can implode an array of any dimension
	 * Uses a few basic rules for implosion:
	 *        1. Replace all instances of delimeters in strings by '/' followed by delimeter
	 *        2. 2 Delimeters in between keys
	 *        3. 3 Delimeters in between key and value
	 *        4. 4 Delimeters in between key-value pairs
	 *
	 * @param array  $array     array
	 * @param string $delimeter delemeter
	 * @param string $keyssofar keyssofar
	 *
	 * @return string imploded cookies
	 */
	function implodeCookies($array, $delimeter, $keyssofar = '')
	{
		$output = '';
		foreach ($array as $key => $value) {
			if (!is_array($value)) {
				if ($keyssofar) {
					$pair = $keyssofar . '[' . $key . ']=' . urlencode($value) . $delimeter;
				} else {
					$pair = $key . '=' . urlencode($value) . $delimeter;
				}
				if ($output != '') {
					$output .= ' ';
				}
				$output .= $pair;
			} else {
				if ($output != '') {
					$output .= ' ';
				}
				$output .= self::implodeCookies($value, $delimeter, $key . $keyssofar);
			}
		}
		return $output;
	}


	/**
	 * curl redir exec
	 *
	 * @param string $ch ch
	 *
	 * @return string something
	 */
	function curl_redir_exec($ch)
	{
		static $curl_loops = 0;
		static $curl_max_loops = 20;
		if ($curl_loops++ >= $curl_max_loops) {
			$curl_loops = 0;
			return false;
		}

		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$data = curl_exec($ch);
		$lastdata = $data;
		$data = str_replace("\r", '', $data);
		list($header, $data) = explode("\n\n", $data, 2);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ($http_code == 301 || $http_code == 302) {
			$matches = array();
			preg_match('/Location:(.*?)\n/', $header, $matches);
			$url = @parse_url(trim(array_pop($matches)));
			if (!$url) {
				//couldn't process the url to redirect to
				$curl_loops = 0;
				return $data;
			}
			$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
			/*      if (!$url['scheme'])
			 $url['scheme'] = $last_url['scheme'];
			if (!$url['host'])
			$url['host'] = $last_url['host'];
			if (!$url['path'])
			$url['path'] = $last_url['path'];
			*/      $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
			curl_setopt($ch, CURLOPT_URL, $new_url);
			return JFusionCurl::curl_redir_exec($ch);
		} else {
			$curl_loops=0;
			return $lastdata;
		}
	}

	/**
	 * function read_header
	 * Basic  code was found on Svetlozar Petrovs website http://svetlozar.net/page/free-code.html.
	 * The code is free to use and similar code can be found on other places on the net.
	 *
	 * @param string $ch     ch
	 * @param string $string string
	 *
	 * @return string something
	 */
	function read_header($ch, $string)
	{
		global $location;
		global $cookiearr;
		global $ch;
		global $cookies_to_set;
		global $cookies_to_set_index;

		$length = strlen($string);
		if (!strncmp($string, "Location:", 9)) {
			$location = trim(substr($string, 9, -1));
		}
		if (!strncmp($string, "Set-Cookie:", 11)) {
			header($string, false);
			$cookiestr = trim(substr($string, 11, -1));
			$cookie = explode(';', $cookiestr);
			$cookies_to_set[$cookies_to_set_index] = $cookie;
			$cookies_to_set_index++;
			$cookie = explode('=', $cookie[0]);
			$cookiename = trim(array_shift($cookie));
			$cookiearr[$cookiename] = trim(implode('=', $cookie));
		}

		$cookie = "";
		if (!empty($cookiearr) && (trim($string) == "")) {
			foreach ($cookiearr as $key=>$value) {
				$cookie .= "$key=$value; ";
			}
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		return $length;
	}

	/**
	 * function parseURL
	 * out[0] = full url
	 * out[1] = scheme or '' if no scheme was found
	 * out[2] = username or '' if no auth username was found
	 * out[3] = password or '' if no auth password was found
	 * out[4] = domain name or '' if no domain name was found
	 * out[5] = port number or '' if no port number was found
	 * out[6] = path or '' if no path was found
	 * out[7] = query or '' if no query was found
	 * out[8] = fragment or '' if no fragment was found
	 *
	 * @param string $url url
	 *
	 * @return array output
	 */
	function parseUrl($url)
	{
		$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
		$r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
		preg_match($r, $url, $out);
		return $out;
	}

	/**
	 * parses cookies
	 *
	 * @param array $cookielines cookies
	 *
	 * @return array parsed cookies
	 */
	function parsecookies($cookielines)
	{
		$line=array();
		$cookies=array();
		foreach ($cookielines as $line) {
			$cdata = array();
			$data = array();
			foreach ($line as $data) {
				$cinfo = explode('=', $data);
				$cinfo[0] = trim($cinfo[0]);
				if (!isset($cinfo[1])) {
					$cinfo[1]='';
				}
				if (strcasecmp($cinfo[0], 'expires') == 0) {
					$cinfo[1] = strtotime($cinfo[1]);
				}
				if (strcasecmp($cinfo[0], 'secure') == 0) {
					$cinfo[1] = "true";
				}
				if (strcasecmp($cinfo[0], 'httponly') == 0) {
					$cinfo[1] = "true";
				}
				if (in_array(strtolower($cinfo[0]), array('domain', 'expires', 'path', 'secure', 'comment', 'httponly'))) {
					$cdata[trim($cinfo[0])] = $cinfo[1];
				} else {
					$cdata['value']['key'] = $cinfo[0];
					$cdata['value']['value'] = $cinfo[1];
				}
			}
			$cookies[] = $cdata;
		}
		return $cookies;
	}

	/**
	 * Adds a cookie to the php header
	 *
	 * @param string $name            cookie name
	 * @param string $value           cookie value
	 * @param int    $expires         cookie expiry time
	 * @param string $cookiepath      cookie path
	 * @param string $cookiedomain    cookie domain
	 * @param string $secure          secure
	 * @param string $httponly        is the cookie http only
	 * @param string $crossdomain_url cross domain url
	 *
	 * @return string nothing
	 */
	function addCookie($name, $value='', $expires=0, $cookiepath='', $cookiedomain='', $secure=0, $httponly=0, $crossdomain_url='')
	{

		// Versions of PHP prior to 5.2 do not support HttpOnly cookies
		// IE is buggy when specifying a blank domain so set the cookie manually
		// solve the empty cookiedomain IE problem by specifying a domain in the plugin's parameters. <------
		if (version_compare(phpversion(), "5.2.0", ">=")) {
			setcookie($name, $value, $expires, $cookiepath, $cookiedomain, $secure, $httponly);
		} else {
			setcookie($name, $value, $expires, $cookiepath, $cookiedomain, $secure);
		}
		if ($crossdomain_url) {
			$jc = JFusionFactory::getCookies();
			$jc->addCookie($crossdomain_url, $name, $value, $expires, $cookiepath, $cookiedomain, $secure, $httponly);
		}
	}

	/**
	 * sets my cookies
	 *
	 * @param string $status           cookie name
	 * @param string $mycookies_to_set cookie value
	 * @param string $cookiedomain     cookie domain
	 * @param string $cookiepath       cookie path
	 * @param string $expires          expires
	 * @param string $secure           secure
	 * @param string $httponly         is the cookie http only
	 * @param string $crossdomain_url  cross domain url
	 *
	 * @return string nothing
	 */
	function setmycookies($status, $mycookies_to_set, $cookiedomain, $cookiepath, $expires=0, $secure=0, $httponly=1, $crossdomain_url='')
	{
		$cookies=array();
		$cookies=JFusionCurl::parsecookies($mycookies_to_set);
		foreach ($cookies as $cookie) {
			$name="";
			$value="";
			if ($expires == 0) {
				$expires_time=0;
			} else {
				$expires_time=time()+$expires;
			}
			if (isset($cookie['value']['key'])) {
				$name= $cookie['value']['key'];
			}
			if (isset($cookie['value']['value'])) {
				$value=$cookie['value']['value'];
			}
			if (isset($cookie['expires'])) {
				$expires_time=$cookie['expires'];
			}
			if (!$cookiepath) {
				if (isset($cookie['path'])) {
					$cookiepath=$cookie['path'];
				}
			}
			if (!$cookiedomain) {
				if (isset($cookie['domain'])) {
					$cookiedomain=$cookie['domain'];
				}
			}
			JFusionCurl::addCookie($name, urldecode($value), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);

			if (($expires_time) == 0) {
				$expires_time='Session_cookie';
			} else {
				$expires_time=date('d-m-Y H:i:s', $expires_time);
			}
			$status['debug'][] = JFusionCurl::_('CREATED') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($value) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
			if ($name=='MOODLEID_') {
				$status['cURL']['moodle'] = urldecode($value);
			}
		}
		return $status;
	}

	/**
	 * delete my cookies
	 *
	 * @param string $status           cookie name
	 * @param string $mycookies_to_set cookie value
	 * @param string $cookiedomain     cookie domain
	 * @param string $cookiepath       cookie path
	 * @param string $leavealone       leavealone
	 * @param string $secure           secure
	 * @param string $httponly         is the cookie http only
	 * @param string $crossdomain_url  cross domain url
	 *
	 * @return string nothing
	 */
	function deletemycookies($status, $mycookies_to_set, $cookiedomain, $cookiepath, $leavealone, $secure=0, $httponly=1, $crossdomain_url='')
	{
		$cookies=array();
		$cookies=JFusionCurl::parsecookies($mycookies_to_set);
		// leavealone keys/values while deleting
		// the $leavealone is an array of key=value that controls cookiedeletion
		// key = value
		// if key is an existing cookiename then that cookie will be affected depending on the value
		// if value = '>' then the 'name' cookies with an expiration date/time > now() will not be deleted
		// if value = '0' then  the 'name' cookies will never be deleted at all
		// if name is a string than the cookie with that name will be affected
		// if name = '0' then all cookies will be affected according to the value
		// thus
		// MOODLEID_=> keeps the cookie with the name MOODLEID_ if expirationtime lies after now()
		// 0=> will keep all cookies that are not sessioncookies
		// 0=0 will keep all cookies

		if ($leavealone) {
			$leavealonearr = array();
			$lines = array();
			$line=array();
			$lines = explode(',', $leavealone);
			$i = 0;

			foreach ($lines as $line) {
				$cinfo = explode('=', $line);
				$leavealonearr[$i]['name']  = $cinfo[0];
				$leavealonearr[$i]['value'] = $cinfo[1];
				$i++;
			}
		}

		foreach ($cookies as $cookie) {
			// check if we schould leave the cookie alone
			$leaveit = false;
			if ($leavealone) {
				for ($i=0;$i<count($leavealonearr);$i++) {
					if (isset($cookie['value']['key'])) {
						if (($cookie['value']['key']== $leavealonearr[$i]['name']) || ($leavealonearr[$i]['name']=='0')) {
							if (($leavealonearr[$i]['value'] == '0')||($cookie['expires'] > time())) {
								$leaveit = true;
							}
						}
					}
				}
			}
			$name="";
			$value="";
			if (isset($cookie['value']['key'])) {
				$name= $cookie['value']['key'];
			}
			if (isset($cookie['expires'])) {
				$expires_time=$cookie['expires'];
			}
			if (!$cookiepath) {
				if (isset($cookie['path'])) {
					$cookiepath=$cookie['path'];
				}
			}
			if (!$cookiedomain) {
				if (isset($cookie['domain'])) {
					$cookiedomain=$cookie['domain'];
				}
			}
			if ($name=='MOODLEID_') {
				$status['cURL']['moodle'] = urldecode($cookie['value']['value']);
			}
			if (!$leaveit) {
				$expires_time=time()-30*60;
				$value = '';
				JFusionCurl::addCookie($name, urldecode($value), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);
				if (($expires_time) == 0) {
					$expires_time='Session_cookie';
				} else {
					$expires_time=date('d-m-Y H:i:s', $expires_time);
				}
				$status['debug'][] = JFusionCurl::_('DELETED') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($value) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
			} else {
				JFusionCurl::addCookie($name, urldecode($cookie['value']['value']), $expires_time, $cookiepath, $cookiedomain, $secure, $httponly, $crossdomain_url);
				if (($expires_time) == 0) {
					$expires_time='Session_cookie';
				} else {
					$expires_time=date('d-m-Y H:i:s', $expires_time);
				}
				$status['debug'][] = JFusionCurl::_('LEFT_ALONE') . ' ' . JFusionCurl::_('COOKIE') . ': ' . JFusionCurl::_('NAME') . '=' . $name . ', ' . JFusionCurl::_('VALUE') . '=' . urldecode($cookie['value']['value']) .', ' .JFusionCurl::_('EXPIRES') . '=' .$expires_time .', ' . JFusionCurl::_('COOKIE_PATH') . '=' . $cookiepath . ', ' . JFusionCurl::_('COOKIE_DOMAIN') . '=' . $cookiedomain. ', '.JFusionCurl::_('COOKIE_SECURE') . '=' .$secure. ', '.JFusionCurl::_('COOKIE_HTTPONLY') . '=' .$httponly;
			}
		}
		return $status;
	}

	/**
	 * function ReadPage
	 * This function will read a page of an integration
	 * Caller should make sure that the Curl extension is loaded
	 * @param array $curl_options curl options
	 *
	 * @return string page read
	 */

	function ReadPage($curl_options, &$status, $curlinit=true)
	{
		global $ch;
		global $cookiearr;
		global $cookies_to_set;
		global $cookies_to_set_index;

		$cookies_to_set=array();
		$cookies_to_set_index=0;
		$open_basedir = ini_get('open_basedir');
		$safe_mode = ini_get('safe_mode');




		// read the page
		if ($curlinit) {
			$ch = curl_init();
		}
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_URL, $curl_options['post_url']);
		curl_setopt($ch, CURLOPT_REFERER, "");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
		if (empty($open_basedir) && empty($safe_mode)) {
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		}
		curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
		if (empty($curl_options['brute_force'])){
			curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());
		}

		if (!empty($curl_options['httpauth'])) {
			curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");

			switch ($curl_options['httpauth']) {
				case "basic":
					$curl_options['httpauth'] = CURLAUTH_BASIC;
					break;
				case "gssnegotiate":
					$curl_options['httpauth'] = CURLAUTH_GSSNEGOTIATE;
					break;
				case "digest":
					$curl_options['httpauth'] = CURLAUTH_DIGEST;
					break;
				case "ntlm":
					$curl_options['httpauth'] = CURLAUTH_NTLM;
					break;
				case "anysafe":
					$curl_options['httpauth'] = CURLAUTH_ANYSAFE;
					break;
				case "any":
				default:
					$curl_options['httpauth'] = CURLAUTH_ANY;
			}

			curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
		}

		if (empty($open_basedir) && empty($safe_mode)) {
			$remotedata = curl_exec($ch);
		} else {
			$remotedata= JFusionCurl::curl_redir_exec($ch);
		}
		if ($curl_options['debug']) {
			$status['cURL']['data'][]= $remotedata;
			$status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
		}
		if (curl_error($ch)) {
			$status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
			curl_close($ch);
			return null;
		}

		if ($curl_options['integrationtype'] ==1) {
			curl_close($ch);
		}
		return $remotedata;
	}

	/**
	 * function RemoteLogin
	 * Smart function to programatically login to an JFusion integration
	 * Will determine what to post (including, optionally, hidden form inputs) and what cookies to set.
	 * Will then login.
	 * In addition to username and password the function only needs an URL to a page with a loginform
	 * and the ID of the loginform.
	 * Including button information and hidden input posts is optionally
	 *
	 * 29-07-2011 Modified to handle logout as well when the loginform is used for logout
	 * just call as login and add $curl_options['logout'] = '1'
	 *
	 * @param array $curl_options curl options
	 *
	 * @return string something
	 */
	function RemoteLogin($curl_options)
	{
		global $ch;
		global $cookiearr;
		global $cookies_to_set;
		global $cookies_to_set_index;
		// extra lines for passing curl options to other routines, like ambrasubs payment processor
		// we are using the super global $_SESSION to pass data in $_SESSION[$var]
		$var = 'curl_options';
		if(!array_key_exists($var,$_SESSION)) $_SESSION[$var]='';
		$_SESSION[$var]=$curl_options;
		$GLOBALS[$var]=&$_SESSION[$var];
		// end extra lines
		$status = array();
		$tmpurl = array();
		$overridearr = array();
		$newhidden = array();
		$lines = array();
		$line=array();
		$cookies_to_set=array();
		$status['debug']=array();
		$status['error']=array();
		$status['cURL']=array();
		$status['cURL']['moodle']='';
		$status['cURL']['data']= array();
		$cookies_to_set_index=0;
		$open_basedir = ini_get('open_basedir');
		$safe_mode = ini_get('safe_mode');

		// check parameters and set defaults
		if (!isset($curl_options['integrationtype'])) {
			$curl_options['integrationtype'] = 1;
		}
		if (!isset($curl_options['relpath'])) {
			$curl_options['relpath'] = false;
		}
		if (!isset($curl_options['hidden'])) {
			$curl_options['hidden'] = false;
		}
		if (!isset($curl_options['buttons'])) {
			$curl_options['buttons'] = false;
		}
		if (!isset($curl_options['override'])) {
			$curl_options['override'] = null;
		}
		if (!isset($curl_options['cookiedomain'])) {
			$curl_options['cookiedomain'] = '';
		}
		if (!isset($curl_options['cookiepath'])) {
			$curl_options['cookiepath'] = '';
		}
		if (!isset($curl_options['expires'])) {
			$curl_options['expires'] = 1800;
		}
		if (!isset($curl_options['input_username_id'])) {
			$curl_options['input_username_id'] = '';
		}
		if (!isset($curl_options['input_password_id'])) {
			$curl_options['input_password_id'] = '';
		}
		if (!isset($curl_options['secure'])) {
			$curl_options['secure'] = 0;
		}
		if (!isset($curl_options['httponly'])) {
			$curl_options['httponly'] = 0;
		}
		if (!isset($curl_options['verifyhost'])) {
			$curl_options['verifyhost'] = 1;
		}
		if (!isset($curl_options['crossdomain_url'])) {
			$curl_options['crossdomain_url'] = '';
		}
		if (!isset($curl_options['debug'])) {
			$curl_options['debug'] = false;
		}

		// find out if we have a SSL enabled website
		if (strpos($curl_options['post_url'], 'https://') === false) {
			$ssl_string = 'http://';
		} else {
			$ssl_string = 'https://';
		}

		// check if curl extension is loaded
		if (!isset($curl_options['post_url']) || !isset($curl_options['formid'])) {
			$status['error'][] = JFusionCurl::_('CURL_FATAL');
			return $status;
		}
		if (!extension_loaded('curl')) {
			$status['error'][] = JFusionCurl::_('CURL_NOTINSTALLED');
			return $status;
		}
		$status['debug'][] = JFusionCurl::_('CURL_POST_URL_1')." ".$curl_options['post_url'];
		$remotedata = JFusionCurl::ReadPage($curl_options, $status,true);
		if (!empty($status['error'])) {
			return $status;
		}
		$status['debug'][] = JFusionCurl::_('CURL_PHASE_1');
		$status1=JFusionCurl::setmycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
		$status = array_merge($status,$status1);
		//find out if we have the form with the name/id specified
		$parser = new JFusionCurlHtmlFormParser($remotedata);
		$result = $parser->parseForms();
		$frmcount = count($result);
		$myfrm = -1;
		$i = 0;
		do {
			if (isset($result[$i]['form_data']['name'])) {
				if ($result[$i]['form_data']['name']==$curl_options['formid']) {
					$myfrm = $i;
					break;
				}
			}
			if (isset($result[$i]['form_data']['id'])) {
				if ($result[$i]['form_data']['id']==$curl_options['formid']) {
					$myfrm = $i;
					break;
				}
			}
			if (isset($result[$i]['form_data']['action'])) {
				if ($result[$i]['form_data']['action']==$curl_options['formid']) {
					$myfrm = $i;
					break;
				}
			}
			$i +=1;
		} while ($i<$frmcount);

		if ($myfrm == -1) {
			$helpthem = '';
			if ($frmcount >0) {
				$i = 0;
				$helpthem = 'I found';
				do {
					if (isset($result[$i]['form_data']['id'])) {
						$helpthem = $helpthem.' -- Name='.$result[$i]['form_data']['name'].' &ID='.$result[$i]['form_data']['id'];
					}
					$i +=1;
				} while ($i<$frmcount);
			}
			$status['error'][] = JFusionCurl::_('CURL_NO_LOGINFORM')." ".$helpthem;
			return $status;
		}
		$status['debug'][] = JFusionCurl::_('CURL_VALID_FORM');


		// by now we have the specified  login/logout form, lets get the data needed to login/logout
		// we went to all this trouble to get to the hidden input entries.
		// The stuff is there to enhance security and is, yes, hidden
		$form_action = htmlspecialchars_decode($result[$myfrm]['form_data']['action']);
		$form_method = $result[$myfrm]['form_data']['method'];
		$elements_keys = array_keys($result[$myfrm]['form_elements']);
		$elements_values = array_values($result[$myfrm]['form_elements']);
		$elements_count  = count($result[$myfrm]['form_elements']);

		// override keys/values from hidden inputs
		// the $override is an array of keys/values that override existing keys/values

		if (empty($curl_options['logout'])){

			if ($curl_options['override']) {
				$lines = explode(',', $curl_options['override']);
				foreach ($lines as $line) {
					$cinfo = explode('=', $line);
					$overridearr[$cinfo[0]]['value'] = $cinfo[1];
					$overridearr[$cinfo[0]]['type'] = 'hidden';
				}
				$newhidden= array_merge($result[$myfrm]['form_elements'], $overridearr);
				$elements_keys = array_keys($newhidden);
				$elements_values = array_values($newhidden);
				$elements_count  = count($newhidden);
			}
		}
		// now construct the action parameter

		// we have 4 possible options:
		// case 0 Form action is without httpo.. and relpath = 0 , special case
		// case 1 Form action is without http.. and relpath = 1 , just construct the action
		// case 2 form_action is a full url, eg http..... and relpath = 0 This is easy, we do nothing at all
		// case 3 form_action is a full url, eg http..... and relpath = 1 special case

		$rel = (int)($curl_options['relpath']);
		//      if (substr($form_action,0,strlen($ssl_string))== $ssl_string) $hashttp = 2; else $hashttp = 0;
		if (substr($form_action, 0, strlen('http'))== 'http') {
			$hashttp = 2;
		} else {
			$hashttp = 0;
		}

		switch($rel+$hashttp) {
			case 0:
				//add a / in front of form_action
				if (substr($form_action, 0, 1) != "/") {
					$form_action = '/'.$form_action;
				}
				// we need to correct various situations like
				// relative url from basedir, relative url from postdir etc
				$tmpurl   = JFusionCurl::parseUrl($curl_options['post_url']);
				$pathinfo1  = pathinfo($form_action);
				$pathinfo = pathinfo($tmpurl[6]);
				//$status['debug'][] = 'post_url   : '.print_r($curl_options['post_url'],true);
				//$status['debug'][] = 'tmpurl     : '.print_r($tmpurl,true);
				//$status['debug'][] = 'form_action: '.print_r($form_action,true);
				//$status['debug'][] = 'pathinfo1  : '.print_r($pathinfo1,true);
				//$status['debug'][] = 'pathinfo   : '.print_r($pathinfo,true);
				if ($pathinfo['dirname'] == $pathinfo1['dirname']) {
					$pathinfo['dirname']='';
				} //prevent double directory

				// replace windows DS bt unix DS
				$pathinfo['dirname'] = str_replace("\\", "/", $pathinfo['dirname']);
				// get rid of the trailing /  in dir
				rtrim($pathinfo['dirname'], '/');
				$port = !empty($tmpurl[5]) ? ":".$tmpurl[5] : '';
				$form_action = $ssl_string.$tmpurl[4].$port.$pathinfo['dirname'].$form_action;
				//$status['debug'][] = 'form_action_final: '.print_r($form_action,true);
				break;
			case 1:
				//add a / in front of form_action
				if (substr($form_action, 0, 1) != "/") {
					$form_action = '/'.$form_action;
				}
				$curl_options['post_url']=rtrim($curl_options['post_url'], '/');
				$form_action = $curl_options['post_url'].$form_action;
				break;
			case 2:
				//do nothing at all
				break;
			case 3:
				// reserved, maybe something pops up, then we use this
				break;
		}

		if (empty($curl_options['logout'])){
			$input_username_name="";
			for ($i = 0; $i <= $elements_count-1; $i++) {
				if ($curl_options['input_username_id']) {
					if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_username_id'])) {
						$input_username_name=$elements_keys[$i];
						break;
					}
				}
				if ($input_username_name == "") {
					if (strpos(strtolower($elements_keys[$i]), 'user')!==false) {
						$input_username_name=$elements_keys[$i];
					}
					if (strpos(strtolower($elements_keys[$i]), 'name')!==false) {
						$input_username_name=$elements_keys[$i];
					}
				}
			}


			if ($input_username_name == "") {
				$status['error'][] = JFusionCurl::_('CURL_NO_NAMEFIELD');
				return $status;
			}

			$input_password_name = "";
			for ($i = 0; $i <= $elements_count-1; $i++) {
				if ($curl_options['input_password_id']) {
					if (strtolower($elements_keys[$i]) == strtolower($curl_options['input_password_id'])) {
						$input_password_name=$elements_keys[$i];
						break;
					}
				}
				if (strpos(strtolower($elements_keys[$i]), 'pass')!==false) {
					$input_password_name=$elements_keys[$i];
				}
			}

			if ($input_password_name=="") {
				$status['error'][] = JFusionCurl::_('CURL_NO_PASSWORDFIELD');
				return $status;
			}
			$status['debug'][] = JFusionCurl::_('CURL_VALID_USERNAME');
		}
		// we now set the submit parameters. These are:
		// all form_elements name=value combinations with value != '' and type hidden
		$strParameters="";
		if ($curl_options['hidden']) {
			for ($i = 0; $i <= $elements_count-1; $i++) {
				if (($elements_values[$i] ['value'] != '')&& ($elements_values[$i] ['type'] == 'hidden')) {
					$strParameters .= '&'.$elements_keys[$i].'='.urlencode($elements_values[$i] ['value']);
				}
			}
		}

		// code for buttons submitted by Daniel Baur
		if ($curl_options['buttons']) {
			if (isset($result[$myfrm] ['buttons'][0]['type'])) {
				if ($result[$myfrm] ['buttons'][0]['type'] =='submit') {
					if ($result[$myfrm]['buttons'][0]['name']) {
						$strParameters .= '&'.$result[$myfrm]['buttons'][0]['name'].'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
					} else {
						$strParameters .= '&'.'submit'.'='.urlencode($result[$myfrm] ['buttons'][0]['value']);
					}
				}
			}
		}

		// extra post parameter to avoid endless loop when more then one jFusion is installed
		if (isset($curl_options['jnodeid'])) {
			$strParameters .= '&jnodeid='.urlencode($curl_options['jnodeid']);
		}

		// extra post parameter to signal a host calling
		if (isset($curl_options['jhost'])) {
			$strParameters .= '&jhost=true';
		}

		if (empty($curl_options['logout'])){
			$post_params = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=".urlencode($curl_options['password']);
			$post_params_debug = $input_username_name."=".urlencode($curl_options['username'])."&".$input_password_name."=xxxxxx";
			$status['debug'][] = JFusionCurl::_('CURL_STARTING_LOGIN')." ".$form_action." parameters= ".$post_params_debug.$strParameters;
		} else {
			$post_params = '';
			$status['debug'][] = JFusionCurl::_('CURL_STARTING_LOGOUT')." ".$form_action." parameters= ".$strParameters;
		}



		// finally submit the login/logout form:
		if ($curl_options['integrationtype'] == 1) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
			curl_setopt($ch, CURLOPT_REFERER, "");
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
			curl_setopt($ch, CURLOPT_FAILONERROR, 1);
			curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
			if (empty($curl_options['brute_force'])){
				curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());
			}
			curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
		}
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($ch, CURLOPT_URL, $form_action);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params.$strParameters);
		if (!empty($curl_options['httpauth'])) {
			curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");
			curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
		}

		$remotedata = curl_exec($ch);
		if ($curl_options['debug']) {
			$status['cURL']['data'][]= $remotedata;
			$status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
		}
		if (curl_error($ch)) {
			$status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
			curl_close($ch);
			return $status;
		}
		curl_close($ch);

		//we have to set the cookies now

		if (empty($curl_options['logout'])){
			$status['debug'][] = JFusionCurl::_('CURL_LOGIN_FINISHED');
			$status=JFusionCurl::setmycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
		} else {
			$status['debug'][] = JFusionCurl::_('CURL_LOGOUT_FINISHED');
			$status=JFusionCurl::deletemycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
		}
		$cookies_to_set_index = 0;
		return $status;
	}

	/**
	 * RemoteLogout
	 *
	 * @param array $curl_options curl options
	 *
	 * @return string something
	 */
	function RemoteLogout($curl_options)
	{
		$status=array();
		global $ch;
		global $cookiearr;
		global $cookies_to_set;
		global $cookies_to_set_index;
		$tmpurl = array();
		$cookies_to_set=array();
		$cookies_to_set_index=0;
		$status['debug']=array();
		$status['error']=array();
		$status['cURL']=array();
		$status['cURL']['moodle']='';
		$status['cURL']['data']= array();

		// check parameters and set defaults
		if (!isset($curl_options['post_url'])) {
			$status['error'][]= 'Fatal programming error : no post_url!';
			return $status;
		}
		if (!isset($curl_options['cookiedomain'])) {
			$curl_options['cookiedomain'] = '';
		}
		if (!isset($curl_options['cookiepath'])) {
			$curl_options['cookiepath'] = '';
		}
		if (!isset($curl_options['leavealone'])) {
			$curl_options['leavealone'] = null;
		}
		if (!isset($curl_options['secure'])) {
			$curl_options['secure'] = 0;
		}
		if (!isset($curl_options['httponly'])) {
			$curl_options['httponly'] = 0;
		}
		if (!isset($curl_options['verifyhost'])) {
			$curl_options['verifyhost'] = 1;
		}
		if (!isset($curl_options['crossdomain_url'])) {
			$curl_options['crossdomain_url'] = '';
		}
		if (!isset($curl_options['debug'])) {
			$curl_options['debug'] = false;
		}

		// prevent usererror by not supplying trailing backslash.
		// make sure that when parameters are sent we do not add a backslash
		if (strpos($curl_options['post_url'], '?') === false) {
			if (!(substr($curl_options['post_url'], -1) == "/")) {
				$curl_options['post_url'] = $curl_options['post_url']."/";
			}
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_REFERER, "");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
		curl_setopt($ch, CURLOPT_URL, $curl_options['post_url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server

		if (!empty($curl_options['httpauth'])) {
			curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");

			switch ($curl_options['httpauth']) {
				case "basic":
					$curl_options['httpauth'] = CURLAUTH_BASIC;
					break;
				case "gssnegotiate":
					$curl_options['httpauth'] = CURLAUTH_GSSNEGOTIATE;
					break;
				case "digest":
					$curl_options['httpauth'] = CURLAUTH_DIGEST;
					break;
				case "ntlm":
					$curl_options['httpauth'] = CURLAUTH_NTLM;
					break;
				case "anysafe":
					$curl_options['httpauth'] = CURLAUTH_ANYSAFE;
					break;
				case "any":
				default:
					$curl_options['httpauth'] = CURLAUTH_ANY;
			}

			curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
		}

		$remotedata = curl_exec($ch);
		if ($curl_options['debug']) {
			$status['cURL']['data'][]= $remotedata;
			$status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
		}
		if (curl_error($ch)) {
			$status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
			curl_close($ch);
			return $status;
		}
		curl_close($ch);

		//we have to delete the cookies now
		$status=JFusionCurl::deletemycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['leavealone'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
		$cookies_to_set_index = 0;
		return $status;
	}

	/**
	 * remote logout url
	 *
	 * @param array $curl_options curl options
	 *
	 * @return string something
	 */
	function RemoteLogoutUrl($curl_options)
	{
		$status=array();
		global $ch;
		global $cookiearr;
		global $cookies_to_set;
		global $cookies_to_set_index;
		$tmpurl = array();
		$cookies_to_set=array();
		$cookies_to_set_index=0;
		$status['debug']=array();
		$status['error']=array();
		$status['cURL']=array();
		$status['cURL']['moodle']='';
		$status['cURL']['data']= array();
		$open_basedir = ini_get('open_basedir');
		$safe_mode = ini_get('safe_mode');

		// check parameters and set defaults
		if (!isset($curl_options['post_url'])) {
			$status['error'][]= 'Fatal programming error : no post_url!';
			return $status;
		}
		if (!isset($curl_options['cookiedomain'])) {
			$curl_options['cookiedomain'] = '';
		}
		if (!isset($curl_options['cookiepath'])) {
			$curl_options['cookiepath'] = '';
		}
		if (!isset($curl_options['leavealone'])) {
			$curl_options['leavealone'] = null;
		}
		if (!isset($curl_options['secure'])) {
			$curl_options['secure'] = 0;
		}
		if (!isset($curl_options['httponly'])) {
			$curl_options['httponly'] = 0;
		}
		if (!isset($curl_options['verifyhost'])) {
			$curl_options['verifyhost'] = 1;
		}
		if (!isset($curl_options['crossdomain_url'])) {
			$curl_options['crossdomain_url'] = '';
		}
		if (!isset($curl_options['debug'])) {
			$curl_options['debug'] = false;
		}
		if (!isset($curl_options['postfields'])) {
			$curl_options['postfields'] = "";
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_REFERER, "");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $curl_options['verifyhost']);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
		curl_setopt($ch, CURLOPT_URL, $curl_options['post_url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());
		curl_setopt($ch, CURLOPT_VERBOSE, $curl_options['debug']); // Display communication with server
		if (empty($open_basedir) && empty($safe_mode)) {
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		}

		if (isset($curl_options['jnodeid'])) {
			if ($curl_options['postfields']) {
				$curl_options['postfields']=$curl_options['postfields'].'&jnodeid='.$curl_options['jnodeid'];
			} else {
				$curl_options['postfields']= 'jnodeid='.$curl_options['jnodeid'];
			}
		}

		if ($curl_options['postfields']){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_options['postfields']);
		}
		curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('JFusionCurl','read_header'));
		curl_setopt($ch, CURLOPT_COOKIE, JFusionCurl::buildCookie());

		if (!empty($curl_options['httpauth'])) {
			curl_setopt($ch, CURLOPT_USERPWD, "{$curl_options['httpauth_username']}:{$curl_options['httpauth_password']}");

			switch ($curl_options['httpauth']) {
				case "basic":
					$curl_options['httpauth'] = CURLAUTH_BASIC;
					break;
				case "gssnegotiate":
					$curl_options['httpauth'] = CURLAUTH_GSSNEGOTIATE;
					break;
				case "digest":
					$curl_options['httpauth'] = CURLAUTH_DIGEST;
					break;
				case "ntlm":
					$curl_options['httpauth'] = CURLAUTH_NTLM;
					break;
				case "anysafe":
					$curl_options['httpauth'] = CURLAUTH_ANYSAFE;
					break;
				case "any":
				default:
					$curl_options['httpauth'] = CURLAUTH_ANY;
			}

			curl_setopt($ch, CURLOPT_HTTPAUTH, $curl_options['httpauth']);
		}

		if (empty($open_basedir) && empty($safe_mode)) {
			$remotedata = curl_exec($ch);
		} else {
			$remotedata= JFusionCurl::curl_redir_exec($ch);
		}
		if ($curl_options['debug']) {
			$status['cURL']['data'][]= $remotedata;
			$status['debug'][]='CURL_INFO'.': '.print_r(curl_getinfo($ch), true);
		}
		$status['debug'][]= JText::_('CURL_LOGOUT_URL').': '.  $curl_options['post_url'];
		if (curl_error($ch)) {
			$status['error'][] = JFusionCurl::_('CURL_ERROR_MSG').": ".curl_error($ch);
			curl_close($ch);
			return $status;
		}
		curl_close($ch);
		$status=JFusionCurl::setmycookies($status, $cookies_to_set, $curl_options['cookiedomain'], $curl_options['cookiepath'], $curl_options['expires'], $curl_options['secure'], $curl_options['httponly'], $curl_options['crossdomain_url']);
		$cookies_to_set_index = 0;
		return $status;
	}
}
?>
Back to Top