This repository was archived by the owner on Jan 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathxmlrpc_client.inc
1954 lines (1751 loc) · 55.3 KB
/
xmlrpc_client.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* PHP implementation of the XML-RPC protocol
*
* This is a PEAR-ified version of Useful inc's XML-RPC for PHP.
* It has support for HTTP transport, proxies and authentication.
*
* PHP versions 4 and 5
*
* LICENSE: License is granted to use or modify this software
* ("XML-RPC for PHP") for commercial or non-commercial use provided the
* copyright of the author is preserved in any distributed or derivative work.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Web Services
* @package XML_RPC
* @author Edd Dumbill <[email protected]>
* @author Stig Bakken <[email protected]>
* @author Martin Jansen <[email protected]>
* @author Daniel Convissor <[email protected]>
* @copyright 1999-2001 Edd Dumbill, 2001-2005 The PHP Group
* @version CVS: $Id: xmlrpc_client.inc,v 1.7 2005/09/20 18:02:23 colin Exp $
* @link http://pear.php.net/package/XML_RPC
*/
if (!function_exists('xml_parser_create')) {
PEAR::loadExtension('xml');
}
/**#@+
* Error constants
*/
/**
* Parameter values don't match parameter types
*/
define('XML_RPC_ERROR_INVALID_TYPE', 101);
/**
* Parameter declared to be numeric but the values are not
*/
define('XML_RPC_ERROR_NON_NUMERIC_FOUND', 102);
/**
* Communication error
*/
define('XML_RPC_ERROR_CONNECTION_FAILED', 103);
/**
* The array or struct has already been started
*/
define('XML_RPC_ERROR_ALREADY_INITIALIZED', 104);
/**
* Incorrect parameters submitted
*/
define('XML_RPC_ERROR_INCORRECT_PARAMS', 105);
/**
* Programming error by developer
*/
define('XML_RPC_ERROR_PROGRAMMING', 106);
/**#@-*/
/**
* Data types
* @global string $GLOBALS['XML_RPC_I4']
*/
$GLOBALS['XML_RPC_I4'] = 'i4';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Int']
*/
$GLOBALS['XML_RPC_Int'] = 'int';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Boolean']
*/
$GLOBALS['XML_RPC_Boolean'] = 'boolean';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Double']
*/
$GLOBALS['XML_RPC_Double'] = 'double';
/**
* Data types
* @global string $GLOBALS['XML_RPC_String']
*/
$GLOBALS['XML_RPC_String'] = 'string';
/**
* Data types
* @global string $GLOBALS['XML_RPC_DateTime']
*/
$GLOBALS['XML_RPC_DateTime'] = 'dateTime.iso8601';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Base64']
*/
$GLOBALS['XML_RPC_Base64'] = 'base64';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Array']
*/
$GLOBALS['XML_RPC_Array'] = 'array';
/**
* Data types
* @global string $GLOBALS['XML_RPC_Struct']
*/
$GLOBALS['XML_RPC_Struct'] = 'struct';
/**
* Data type meta-types
* @global array $GLOBALS['XML_RPC_Types']
*/
$GLOBALS['XML_RPC_Types'] = array(
$GLOBALS['XML_RPC_I4'] => 1,
$GLOBALS['XML_RPC_Int'] => 1,
$GLOBALS['XML_RPC_Boolean'] => 1,
$GLOBALS['XML_RPC_String'] => 1,
$GLOBALS['XML_RPC_Double'] => 1,
$GLOBALS['XML_RPC_DateTime'] => 1,
$GLOBALS['XML_RPC_Base64'] => 1,
$GLOBALS['XML_RPC_Array'] => 2,
$GLOBALS['XML_RPC_Struct'] => 3,
);
/**
* Error message numbers
* @global array $GLOBALS['XML_RPC_err']
*/
$GLOBALS['XML_RPC_err'] = array(
'unknown_method' => 1,
'invalid_return' => 2,
'incorrect_params' => 3,
'introspect_unknown' => 4,
'http_error' => 5,
'not_response_object' => 6,
'invalid_request' => 7,
);
/**
* Error message strings
* @global array $GLOBALS['XML_RPC_str']
*/
$GLOBALS['XML_RPC_str'] = array(
'unknown_method' => 'Unknown method',
'invalid_return' => 'Invalid return payload: enable debugging to examine incoming payload',
'incorrect_params' => 'Incorrect parameters passed to method',
'introspect_unknown' => 'Can\'t introspect: method unknown',
'http_error' => 'Didn\'t receive 200 OK from remote server.',
'not_response_object' => 'The requested method didn\'t return an XML_RPC_Response object.',
'invalid_request' => 'Invalid request payload',
);
/**
* Default XML encoding (ISO-8859-1, UTF-8 or US-ASCII)
* @global string $GLOBALS['XML_RPC_defencoding']
*/
$GLOBALS['XML_RPC_defencoding'] = 'UTF-8';
/**
* User error codes start at 800
* @global int $GLOBALS['XML_RPC_erruser']
*/
$GLOBALS['XML_RPC_erruser'] = 800;
/**
* XML parse error codes start at 100
* @global int $GLOBALS['XML_RPC_errxml']
*/
$GLOBALS['XML_RPC_errxml'] = 100;
/**
* Compose backslashes for escaping regexp
* @global string $GLOBALS['XML_RPC_backslash']
*/
$GLOBALS['XML_RPC_backslash'] = chr(92) . chr(92);
/**
* Valid parents of XML elements
* @global array $GLOBALS['XML_RPC_valid_parents']
*/
$GLOBALS['XML_RPC_valid_parents'] = array(
'BOOLEAN' => array('VALUE'),
'I4' => array('VALUE'),
'INT' => array('VALUE'),
'STRING' => array('VALUE'),
'DOUBLE' => array('VALUE'),
'DATETIME.ISO8601' => array('VALUE'),
'BASE64' => array('VALUE'),
'ARRAY' => array('VALUE'),
'STRUCT' => array('VALUE'),
'PARAM' => array('PARAMS'),
'METHODNAME' => array('METHODCALL'),
'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
'MEMBER' => array('STRUCT'),
'NAME' => array('MEMBER'),
'DATA' => array('ARRAY'),
'FAULT' => array('METHODRESPONSE'),
'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'),
);
/**
* Stores state during parsing
*
* quick explanation of components:
* + ac = accumulates values
* + qt = decides if quotes are needed for evaluation
* + cm = denotes struct or array (comma needed)
* + isf = indicates a fault
* + lv = indicates "looking for a value": implements the logic
* to allow values with no types to be strings
* + params = stores parameters in method calls
* + method = stores method name
*
* @global array $GLOBALS['XML_RPC_xh']
*/
$GLOBALS['XML_RPC_xh'] = array();
/**
* Start element handler for the XML parser
*
* @return void
*/
function XML_RPC_se($parser_resource, $name, $attrs)
{
global $XML_RPC_xh, $XML_RPC_DateTime, $XML_RPC_String, $XML_RPC_valid_parents;
$parser = (int) $parser_resource;
// if invalid xmlrpc already detected, skip all processing
if ($XML_RPC_xh[$parser]['isf'] >= 2) {
return;
}
// check for correct element nesting
// top level element can only be of 2 types
if (count($XML_RPC_xh[$parser]['stack']) == 0) {
if ($name != 'METHODRESPONSE' && $name != 'METHODCALL') {
$XML_RPC_xh[$parser]['isf'] = 2;
$XML_RPC_xh[$parser]['isf_reason'] = 'missing top level xmlrpc element';
return;
}
} else {
// not top level element: see if parent is OK
if (!in_array($XML_RPC_xh[$parser]['stack'][0], $XML_RPC_valid_parents[$name])) {
$name = preg_replace('[^a-zA-Z0-9._-]', '', $name);
$XML_RPC_xh[$parser]['isf'] = 2;
$XML_RPC_xh[$parser]['isf_reason'] = "xmlrpc element $name cannot be child of {$XML_RPC_xh[$parser]['stack'][0]}";
return;
}
}
switch ($name) {
case 'STRUCT':
$XML_RPC_xh[$parser]['cm']++;
// turn quoting off
$XML_RPC_xh[$parser]['qt'] = 0;
$cur_val = array();
$cur_val['value'] = array();
$cur_val['members'] = 1;
array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
break;
case 'ARRAY':
$XML_RPC_xh[$parser]['cm']++;
// turn quoting off
$XML_RPC_xh[$parser]['qt'] = 0;
$cur_val = array();
$cur_val['value'] = array();
$cur_val['members'] = 0;
array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
break;
case 'NAME':
$XML_RPC_xh[$parser]['ac'] = '';
break;
case 'FAULT':
$XML_RPC_xh[$parser]['isf'] = 1;
break;
case 'PARAM':
$XML_RPC_xh[$parser]['valuestack'] = array();
break;
case 'VALUE':
$XML_RPC_xh[$parser]['lv'] = 1;
$XML_RPC_xh[$parser]['vt'] = $XML_RPC_String;
$XML_RPC_xh[$parser]['ac'] = '';
$XML_RPC_xh[$parser]['qt'] = 0;
// look for a value: if this is still 1 by the
// time we reach the first data segment then the type is string
// by implication and we need to add in a quote
break;
case 'I4':
case 'INT':
case 'STRING':
case 'BOOLEAN':
case 'DOUBLE':
case 'DATETIME.ISO8601':
case 'BASE64':
$XML_RPC_xh[$parser]['ac'] = ''; // reset the accumulator
if ($name == 'DATETIME.ISO8601' || $name == 'STRING') {
$XML_RPC_xh[$parser]['qt'] = 1;
if ($name == 'DATETIME.ISO8601') {
$XML_RPC_xh[$parser]['vt'] = $XML_RPC_DateTime;
}
} elseif ($name == 'BASE64') {
$XML_RPC_xh[$parser]['qt'] = 2;
} else {
// No quoting is required here -- but
// at the end of the element we must check
// for data format errors.
$XML_RPC_xh[$parser]['qt'] = 0;
}
break;
case 'MEMBER':
$XML_RPC_xh[$parser]['ac'] = '';
break;
case 'DATA':
case 'METHODCALL':
case 'METHODNAME':
case 'METHODRESPONSE':
case 'PARAMS':
// valid elements that add little to processing
break;
}
// Save current element to stack
array_unshift($XML_RPC_xh[$parser]['stack'], $name);
if ($name != 'VALUE') {
$XML_RPC_xh[$parser]['lv'] = 0;
}
}
/**
* End element handler for the XML parser
*
* @return void
*/
function XML_RPC_ee($parser_resource, $name)
{
global $XML_RPC_xh, $XML_RPC_Types, $XML_RPC_String;
$parser = (int) $parser_resource;
if ($XML_RPC_xh[$parser]['isf'] >= 2) {
return;
}
// push this element from stack
// NB: if XML validates, correct opening/closing is guaranteed and
// we do not have to check for $name == $curr_elem.
// we also checked for proper nesting at start of elements...
$curr_elem = array_shift($XML_RPC_xh[$parser]['stack']);
switch ($name) {
case 'STRUCT':
case 'ARRAY':
$cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
$XML_RPC_xh[$parser]['value'] = $cur_val['value'];
$XML_RPC_xh[$parser]['vt'] = strtolower($name);
$XML_RPC_xh[$parser]['cm']--;
break;
case 'NAME':
$XML_RPC_xh[$parser]['valuestack'][0]['name'] = $XML_RPC_xh[$parser]['ac'];
break;
case 'BOOLEAN':
// special case here: we translate boolean 1 or 0 into PHP
// constants true or false
if ($XML_RPC_xh[$parser]['ac'] == '1') {
$XML_RPC_xh[$parser]['ac'] = 'true';
} else {
$XML_RPC_xh[$parser]['ac'] = 'false';
}
$XML_RPC_xh[$parser]['vt'] = strtolower($name);
// Drop through intentionally.
case 'I4':
case 'INT':
case 'STRING':
case 'DOUBLE':
case 'DATETIME.ISO8601':
case 'BASE64':
if ($XML_RPC_xh[$parser]['qt'] == 1) {
// we use double quotes rather than single so backslashification works OK
$XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
} elseif ($XML_RPC_xh[$parser]['qt'] == 2) {
$XML_RPC_xh[$parser]['value'] = base64_decode($XML_RPC_xh[$parser]['ac']);
} elseif ($name == 'BOOLEAN') {
$XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
} else {
// we have an I4, INT or a DOUBLE
// we must check that only 0123456789-.<space> are characters here
if (!ereg("^[+-]?[0123456789 \t\.]+$", $XML_RPC_xh[$parser]['ac'])) {
XML_RPC_Base::raiseError('Non-numeric value received in INT or DOUBLE',
XML_RPC_ERROR_NON_NUMERIC_FOUND);
$XML_RPC_xh[$parser]['value'] = XML_RPC_ERROR_NON_NUMERIC_FOUND;
} else {
// it's ok, add it on
$XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
}
}
$XML_RPC_xh[$parser]['ac'] = '';
$XML_RPC_xh[$parser]['qt'] = 0;
$XML_RPC_xh[$parser]['lv'] = 3; // indicate we've found a value
break;
case 'VALUE':
// deal with a string value
if (strlen($XML_RPC_xh[$parser]['ac']) > 0 &&
$XML_RPC_xh[$parser]['vt'] == $XML_RPC_String) {
$XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
} elseif ($XML_RPC_xh[$parser]['lv'] == 1) {
// The <value> element was empty.
$XML_RPC_xh[$parser]['value'] = '';
}
$temp = new XML_RPC_Value($XML_RPC_xh[$parser]['value'], $XML_RPC_xh[$parser]['vt']);
$cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
if (is_array($cur_val)) {
if ($cur_val['members']==0) {
$cur_val['value'][] = $temp;
} else {
$XML_RPC_xh[$parser]['value'] = $temp;
}
array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
} else {
$XML_RPC_xh[$parser]['value'] = $temp;
}
break;
case 'MEMBER':
$XML_RPC_xh[$parser]['ac'] = '';
$XML_RPC_xh[$parser]['qt'] = 0;
$cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
if (is_array($cur_val)) {
if ($cur_val['members']==1) {
$cur_val['value'][$cur_val['name']] = $XML_RPC_xh[$parser]['value'];
}
array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
}
break;
case 'DATA':
$XML_RPC_xh[$parser]['ac'] = '';
$XML_RPC_xh[$parser]['qt'] = 0;
break;
case 'PARAM':
$XML_RPC_xh[$parser]['params'][] = $XML_RPC_xh[$parser]['value'];
break;
case 'METHODNAME':
case 'RPCMETHODNAME':
$XML_RPC_xh[$parser]['method'] = ereg_replace("^[\n\r\t ]+", '',
$XML_RPC_xh[$parser]['ac']);
break;
}
// if it's a valid type name, set the type
if (isset($XML_RPC_Types[strtolower($name)])) {
$XML_RPC_xh[$parser]['vt'] = strtolower($name);
}
}
/**
* Character data handler for the XML parser
*
* @return void
*/
function XML_RPC_cd($parser_resource, $data)
{
global $XML_RPC_xh, $XML_RPC_backslash;
$parser = (int) $parser_resource;
if ($XML_RPC_xh[$parser]['lv'] != 3) {
// "lookforvalue==3" means that we've found an entire value
// and should discard any further character data
if ($XML_RPC_xh[$parser]['lv'] == 1) {
// if we've found text and we're just in a <value> then
// turn quoting on, as this will be a string
$XML_RPC_xh[$parser]['qt'] = 1;
// and say we've found a value
$XML_RPC_xh[$parser]['lv'] = 2;
}
// replace characters that eval would
// do special things with
if (!isset($XML_RPC_xh[$parser]['ac'])) {
$XML_RPC_xh[$parser]['ac'] = '';
}
$XML_RPC_xh[$parser]['ac'] .= $data;
}
}
/**
* The common methods and properties for all of the XML_RPC classes
*
* @category Web Services
* @package XML_RPC
* @author Edd Dumbill <[email protected]>
* @author Stig Bakken <[email protected]>
* @author Martin Jansen <[email protected]>
* @author Daniel Convissor <[email protected]>
* @copyright 1999-2001 Edd Dumbill, 2001-2005 The PHP Group
* @version Release: 1.4.2
* @link http://pear.php.net/package/XML_RPC
*/
class XML_RPC_Base {
/**
* PEAR Error handling
*
* @return object PEAR_Error object
*/
function raiseError($msg, $code)
{
include_once 'PEAR.php';
if (is_object(@$this)) {
return PEAR::raiseError(get_class($this) . ': ' . $msg, $code);
} else {
return PEAR::raiseError('XML_RPC: ' . $msg, $code);
}
}
/**
* Tell whether something is a PEAR_Error object
*
* @param mixed $value the item to check
*
* @return bool whether $value is a PEAR_Error object or not
*
* @access public
*/
function isError($value)
{
return is_a($value, 'PEAR_Error');
}
}
/**
* The methods and properties for submitting XML RPC requests
*
* @category Web Services
* @package XML_RPC
* @author Edd Dumbill <[email protected]>
* @author Stig Bakken <[email protected]>
* @author Martin Jansen <[email protected]>
* @author Daniel Convissor <[email protected]>
* @copyright 1999-2001 Edd Dumbill, 2001-2005 The PHP Group
* @version Release: 1.4.2
* @link http://pear.php.net/package/XML_RPC
*/
class XML_RPC_Client extends XML_RPC_Base {
/**
* The path and name of the RPC server script you want the request to go to
* @var string
*/
var $path = '';
/**
* The name of the remote server to connect to
* @var string
*/
var $server = '';
/**
* The protocol to use in contacting the remote server
* @var string
*/
var $protocol = 'http://';
/**
* The port for connecting to the remote server
*
* The default is 80 for http:// connections
* and 443 for https:// and ssl:// connections.
*
* @var integer
*/
var $port = 80;
/**
* A user name for accessing the RPC server
* @var string
* @see XML_RPC_Client::setCredentials()
*/
var $username = '';
/**
* A password for accessing the RPC server
* @var string
* @see XML_RPC_Client::setCredentials()
*/
var $password = '';
/**
* The name of the proxy server to use, if any
* @var string
*/
var $proxy = '';
/**
* The protocol to use in contacting the proxy server, if any
* @var string
*/
var $proxy_protocol = 'http://';
/**
* The port for connecting to the proxy server
*
* The default is 8080 for http:// connections
* and 443 for https:// and ssl:// connections.
*
* @var integer
*/
var $proxy_port = 8080;
/**
* A user name for accessing the proxy server
* @var string
*/
var $proxy_user = '';
/**
* A password for accessing the proxy server
* @var string
*/
var $proxy_pass = '';
/**
* The error number, if any
* @var integer
*/
var $errno = 0;
/**
* The error message, if any
* @var string
*/
var $errstr = '';
/**
* The current debug mode (1 = on, 0 = off)
* @var integer
*/
var $debug = 0;
/**
* The HTTP headers for the current request.
* @var string
*/
var $headers = '';
/**
* Sets the object's properties
*
* @param string $path the path and name of the RPC server script
* you want the request to go to
* @param string $server the URL of the remote server to connect to.
* If this parameter doesn't specify a
* protocol and $port is 443, ssl:// is
* assumed.
* @param integer $port a port for connecting to the remote server.
* Defaults to 80 for http:// connections and
* 443 for https:// and ssl:// connections.
* @param string $proxy the URL of the proxy server to use, if any.
* If this parameter doesn't specify a
* protocol and $port is 443, ssl:// is
* assumed.
* @param integer $proxy_port a port for connecting to the remote server.
* Defaults to 8080 for http:// connections and
* 443 for https:// and ssl:// connections.
* @param string $proxy_user a user name for accessing the proxy server
* @param string $proxy_pass a password for accessing the proxy server
*
* @return void
*/
function XML_RPC_Client($path, $server, $port = 0,
$proxy = '', $proxy_port = 0,
$proxy_user = '', $proxy_pass = '')
{
$this->path = $path;
$this->proxy_user = $proxy_user;
$this->proxy_pass = $proxy_pass;
preg_match('@^(http://|https://|ssl://)?(.*)$@', $server, $match);
if ($match[1] == '') {
if ($port == 443) {
$this->server = $match[2];
$this->protocol = 'ssl://';
$this->port = 443;
} else {
$this->server = $match[2];
if ($port) {
$this->port = $port;
}
}
} elseif ($match[1] == 'http://') {
$this->server = $match[2];
if ($port) {
$this->port = $port;
}
} else {
$this->server = $match[2];
$this->protocol = 'ssl://';
if ($port) {
$this->port = $port;
} else {
$this->port = 443;
}
}
if ($proxy) {
preg_match('@^(http://|https://|ssl://)?(.*)$@', $proxy, $match);
if ($match[1] == '') {
if ($proxy_port == 443) {
$this->proxy = $match[2];
$this->proxy_protocol = 'ssl://';
$this->proxy_port = 443;
} else {
$this->proxy = $match[2];
if ($proxy_port) {
$this->proxy_port = $proxy_port;
}
}
} elseif ($match[1] == 'http://') {
$this->proxy = $match[2];
if ($proxy_port) {
$this->proxy_port = $proxy_port;
}
} else {
$this->proxy = $match[2];
$this->proxy_protocol = 'ssl://';
if ($proxy_port) {
$this->proxy_port = $proxy_port;
} else {
$this->proxy_port = 443;
}
}
}
}
/**
* Change the current debug mode
*
* @param int $in where 1 = on, 0 = off
*
* @return void
*/
function setDebug($in)
{
if ($in) {
$this->debug = 1;
} else {
$this->debug = 0;
}
}
/**
* Set username and password properties for connecting to the RPC server
*
* @param string $u the user name
* @param string $p the password
*
* @return void
*
* @see XML_RPC_Client::$username, XML_RPC_Client::$password
*/
function setCredentials($u, $p)
{
$this->username = $u;
$this->password = $p;
}
/**
* Transmit the RPC request via HTTP 1.0 protocol
*
* @param object $msg the XML_RPC_Message object
* @param int $timeout how many seconds to wait for the request
*
* @return object an XML_RPC_Response object. 0 is returned if any
* problems happen.
*
* @see XML_RPC_Message, XML_RPC_Client::XML_RPC_Client(),
* XML_RPC_Client::setCredentials()
*/
function send($msg, $timeout = 0)
{
if (!is_a($msg, 'XML_RPC_Message')) {
$this->errstr = 'send()\'s $msg parameter must be an'
. ' XML_RPC_Message object.';
$this->raiseError($this->errstr, XML_RPC_ERROR_PROGRAMMING);
return 0;
}
$msg->debug = $this->debug;
return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
$timeout, $this->username,
$this->password);
}
/**
* Transmit the RPC request via HTTP 1.0 protocol
*
* Requests should be sent using XML_RPC_Client send() rather than
* calling this method directly.
*
* @param object $msg the XML_RPC_Message object
* @param string $server the server to send the request to
* @param int $port the server port send the request to
* @param int $timeout how many seconds to wait for the request
* before giving up
* @param string $username a user name for accessing the RPC server
* @param string $password a password for accessing the RPC server
*
* @return object an XML_RPC_Response object. 0 is returned if any
* problems happen.
*
* @access protected
* @see XML_RPC_Client::send()
*/
function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
$username = '', $password = '')
{
/*
* If we're using a proxy open a socket to the proxy server
* instead to the xml-rpc server
*/
if ($this->proxy) {
if ($this->proxy_protocol == 'http://') {
$protocol = '';
} else {
$protocol = $this->proxy_protocol;
}
if ($timeout > 0) {
$fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
$this->errno, $this->errstr, $timeout);
} else {
$fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
$this->errno, $this->errstr);
}
} else {
if ($this->protocol == 'http://') {
$protocol = '';
} else {
$protocol = $this->protocol;
}
if ($timeout > 0) {
$fp = @fsockopen($protocol . $server, $port,
$this->errno, $this->errstr, $timeout);
} else {
$fp = @fsockopen($protocol . $server, $port,
$this->errno, $this->errstr);
}
}
/*
* Just raising the error without returning it is strange,
* but keep it here for backwards compatibility.
*/
if (!$fp && $this->proxy) {
$this->raiseError('Connection to proxy server '
. $this->proxy . ':' . $this->proxy_port
. ' failed. ' . $this->errstr,
XML_RPC_ERROR_CONNECTION_FAILED);
return 0;
} elseif (!$fp) {
$this->raiseError('Connection to RPC server '
. $server . ':' . $port
. ' failed. ' . $this->errstr,
XML_RPC_ERROR_CONNECTION_FAILED);
return 0;
}
if ($timeout) {
/*
* Using socket_set_timeout() because stream_set_timeout()
* was introduced in 4.3.0, but we need to support 4.2.0.
*/
socket_set_timeout($fp, $timeout);
}
// Pre-emptive BC hacks for fools calling sendPayloadHTTP10() directly
if ($username != $this->username) {
$this->setCredentials($username, $password);
}
// Only create the payload if it was not created previously
if (empty($msg->payload)) {
$msg->createPayload();
}
$this->createHeaders($msg);
$op = $this->headers . "\r\n\r\n";
$op .= $msg->payload;
if (!fputs($fp, $op, strlen($op))) {
$this->errstr = 'Write error';
return 0;
}
$resp = $msg->parseResponseFile($fp);
$meta = socket_get_status($fp);
if ($meta['timed_out']) {
fclose($fp);
$this->errstr = 'RPC server did not send response before timeout.';
$this->raiseError($this->errstr, XML_RPC_ERROR_CONNECTION_FAILED);
return 0;
}
fclose($fp);
return $resp;
}
/**
* Determines the HTTP headers and puts it in the $headers property
*
* @param object $msg the XML_RPC_Message object
*
* @return boolean TRUE if okay, FALSE if the message payload isn't set.
*
* @access protected
*/
function createHeaders($msg)
{
if (empty($msg->payload)) {
return false;
}
if ($this->proxy) {
$this->headers = 'POST ' . $this->protocol . $this->server;
if ($this->proxy_port) {
$this->headers .= ':' . $this->port;
}
} else {
$this->headers = 'POST ';
}
$this->headers .= $this->path. " HTTP/1.0\r\n";
$this->headers .= "User-Agent: PEAR XML_RPC\r\n";
$this->headers .= 'Host: ' . $this->server . "\r\n";
if ($this->proxy && $this->proxy_user) {
$this->headers .= 'Proxy-Authorization: Basic '
. base64_encode("$this->proxy_user:$this->proxy_pass")
. "\r\n";
}
// thanks to Grant Rauscher <[email protected]> for this
if ($this->username) {
$this->headers .= 'Authorization: Basic '
. base64_encode("$this->username:$this->password")
. "\r\n";
}
$this->headers .= "Content-Type: text/xml\r\n";