Projet

Général

Profil

Télécharger (58,9 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / xmlrpc_client.inc @ 96d91e4a

1
<?php
2

    
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4

    
5
/**
6
 * PHP implementation of the XML-RPC protocol
7
 *
8
 * This is a PEAR-ified version of Useful inc's XML-RPC for PHP.
9
 * It has support for HTTP transport, proxies and authentication.
10
 *
11
 * PHP versions 4 and 5
12
 *
13
 * @category   Web Services
14
 * @package    XML_RPC
15
 * @author     Edd Dumbill <edd@usefulinc.com>
16
 * @author     Stig Bakken <stig@php.net>
17
 * @author     Martin Jansen <mj@php.net>
18
 * @author     Daniel Convissor <danielc@php.net>
19
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
20
 * @license    http://www.php.net/license/3_01.txt  PHP License
21
 * @version    SVN: $Id: RPC.php 300961 2010-07-03 02:17:34Z danielc $
22
 * @link       http://pear.php.net/package/XML_RPC
23
 */
24

    
25

    
26
if (!function_exists('xml_parser_create')) {
27
    include_once 'PEAR.inc';
28
    PEAR::loadExtension('xml');
29
}
30

    
31
/**#@+
32
 * Error constants
33
 */
34
/**
35
 * Parameter values don't match parameter types
36
 */
37
define('XML_RPC_ERROR_INVALID_TYPE', 101);
38
/**
39
 * Parameter declared to be numeric but the values are not
40
 */
41
define('XML_RPC_ERROR_NON_NUMERIC_FOUND', 102);
42
/**
43
 * Communication error
44
 */
45
define('XML_RPC_ERROR_CONNECTION_FAILED', 103);
46
/**
47
 * The array or struct has already been started
48
 */
49
define('XML_RPC_ERROR_ALREADY_INITIALIZED', 104);
50
/**
51
 * Incorrect parameters submitted
52
 */
53
define('XML_RPC_ERROR_INCORRECT_PARAMS', 105);
54
/**
55
 * Programming error by developer
56
 */
57
define('XML_RPC_ERROR_PROGRAMMING', 106);
58
/**#@-*/
59

    
60

    
61
/**
62
 * Data types
63
 * @global string $GLOBALS['XML_RPC_I4']
64
 */
65
$GLOBALS['XML_RPC_I4'] = 'i4';
66

    
67
/**
68
 * Data types
69
 * @global string $GLOBALS['XML_RPC_Int']
70
 */
71
$GLOBALS['XML_RPC_Int'] = 'int';
72

    
73
/**
74
 * Data types
75
 * @global string $GLOBALS['XML_RPC_Boolean']
76
 */
77
$GLOBALS['XML_RPC_Boolean'] = 'boolean';
78

    
79
/**
80
 * Data types
81
 * @global string $GLOBALS['XML_RPC_Double']
82
 */
83
$GLOBALS['XML_RPC_Double'] = 'double';
84

    
85
/**
86
 * Data types
87
 * @global string $GLOBALS['XML_RPC_String']
88
 */
89
$GLOBALS['XML_RPC_String'] = 'string';
90

    
91
/**
92
 * Data types
93
 * @global string $GLOBALS['XML_RPC_DateTime']
94
 */
95
$GLOBALS['XML_RPC_DateTime'] = 'dateTime.iso8601';
96

    
97
/**
98
 * Data types
99
 * @global string $GLOBALS['XML_RPC_Base64']
100
 */
101
$GLOBALS['XML_RPC_Base64'] = 'base64';
102

    
103
/**
104
 * Data types
105
 * @global string $GLOBALS['XML_RPC_Array']
106
 */
107
$GLOBALS['XML_RPC_Array'] = 'array';
108

    
109
/**
110
 * Data types
111
 * @global string $GLOBALS['XML_RPC_Struct']
112
 */
113
$GLOBALS['XML_RPC_Struct'] = 'struct';
114

    
115

    
116
/**
117
 * Data type meta-types
118
 * @global array $GLOBALS['XML_RPC_Types']
119
 */
120
$GLOBALS['XML_RPC_Types'] = array(
121
    $GLOBALS['XML_RPC_I4']       => 1,
122
    $GLOBALS['XML_RPC_Int']      => 1,
123
    $GLOBALS['XML_RPC_Boolean']  => 1,
124
    $GLOBALS['XML_RPC_String']   => 1,
125
    $GLOBALS['XML_RPC_Double']   => 1,
126
    $GLOBALS['XML_RPC_DateTime'] => 1,
127
    $GLOBALS['XML_RPC_Base64']   => 1,
128
    $GLOBALS['XML_RPC_Array']    => 2,
129
    $GLOBALS['XML_RPC_Struct']   => 3,
130
);
131

    
132

    
133
/**
134
 * Error message numbers
135
 * @global array $GLOBALS['XML_RPC_err']
136
 */
137
$GLOBALS['XML_RPC_err'] = array(
138
    'unknown_method'      => 1,
139
    'invalid_return'      => 2,
140
    'incorrect_params'    => 3,
141
    'introspect_unknown'  => 4,
142
    'http_error'          => 5,
143
    'not_response_object' => 6,
144
    'invalid_request'     => 7,
145
);
146

    
147
/**
148
 * Error message strings
149
 * @global array $GLOBALS['XML_RPC_str']
150
 */
151
$GLOBALS['XML_RPC_str'] = array(
152
    'unknown_method'      => gettext("Unknown method"),
153
    'invalid_return'      => gettext("Invalid return payload: enable debugging to examine incoming payload"),
154
    'incorrect_params'    => gettext("Incorrect parameters passed to method"),
155
    'introspect_unknown'  => gettext("Can't introspect: method unknown"),
156
    'http_error'          => gettext("Didn't receive 200 OK from remote server."),
157
    'not_response_object' => gettext("The requested method didn't return an XML_RPC_Response object."),
158
    'invalid_request'     => gettext("Invalid request payload"),
159
);
160

    
161

    
162
/**
163
 * Default XML encoding (ISO-8859-1, UTF-8 or US-ASCII)
164
 * @global string $GLOBALS['XML_RPC_defencoding']
165
 */
166
$GLOBALS['XML_RPC_defencoding'] = 'UTF-8';
167

    
168
/**
169
 * User error codes start at 800
170
 * @global int $GLOBALS['XML_RPC_erruser']
171
 */
172
$GLOBALS['XML_RPC_erruser'] = 800;
173

    
174
/**
175
 * XML parse error codes start at 100
176
 * @global int $GLOBALS['XML_RPC_errxml']
177
 */
178
$GLOBALS['XML_RPC_errxml'] = 100;
179

    
180

    
181
/**
182
 * Compose backslashes for escaping regexp
183
 * @global string $GLOBALS['XML_RPC_backslash']
184
 */
185
$GLOBALS['XML_RPC_backslash'] = chr(92) . chr(92);
186

    
187

    
188
/**
189
 * Should we automatically base64 encode strings that contain characters
190
 * which can cause PHP's SAX-based XML parser to break?
191
 * @global boolean $GLOBALS['XML_RPC_auto_base64']
192
 */
193
$GLOBALS['XML_RPC_auto_base64'] = true;
194

    
195

    
196
/**
197
 * Valid parents of XML elements
198
 * @global array $GLOBALS['XML_RPC_valid_parents']
199
 */
200
$GLOBALS['XML_RPC_valid_parents'] = array(
201
    'BOOLEAN' => array('VALUE'),
202
    'I4' => array('VALUE'),
203
    'INT' => array('VALUE'),
204
    'STRING' => array('VALUE'),
205
    'DOUBLE' => array('VALUE'),
206
    'DATETIME.ISO8601' => array('VALUE'),
207
    'BASE64' => array('VALUE'),
208
    'ARRAY' => array('VALUE'),
209
    'STRUCT' => array('VALUE'),
210
    'PARAM' => array('PARAMS'),
211
    'METHODNAME' => array('METHODCALL'),
212
    'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
213
    'MEMBER' => array('STRUCT'),
214
    'NAME' => array('MEMBER'),
215
    'DATA' => array('ARRAY'),
216
    'FAULT' => array('METHODRESPONSE'),
217
    'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'),
218
);
219

    
220

    
221
/**
222
 * Stores state during parsing
223
 *
224
 * quick explanation of components:
225
 *   + ac     = accumulates values
226
 *   + qt     = decides if quotes are needed for evaluation
227
 *   + cm     = denotes struct or array (comma needed)
228
 *   + isf    = indicates a fault
229
 *   + lv     = indicates "looking for a value": implements the logic
230
 *               to allow values with no types to be strings
231
 *   + params = stores parameters in method calls
232
 *   + method = stores method name
233
 *
234
 * @global array $GLOBALS['XML_RPC_xh']
235
 */
236
$GLOBALS['XML_RPC_xh'] = array();
237

    
238

    
239
/**
240
 * Start element handler for the XML parser
241
 *
242
 * @return void
243
 */
244
function XML_RPC_se($parser_resource, $name, $attrs)
245
{
246
    global $XML_RPC_xh, $XML_RPC_valid_parents;
247

    
248
    $parser = (int) $parser_resource;
249

    
250
    // if invalid xmlrpc already detected, skip all processing
251
    if ($XML_RPC_xh[$parser]['isf'] >= 2) {
252
        return;
253
    }
254

    
255
    // check for correct element nesting
256
    // top level element can only be of 2 types
257
    if (count($XML_RPC_xh[$parser]['stack']) == 0) {
258
        if ($name != 'METHODRESPONSE' && $name != 'METHODCALL') {
259
            $XML_RPC_xh[$parser]['isf'] = 2;
260
            $XML_RPC_xh[$parser]['isf_reason'] = gettext('missing top level xmlrpc element');
261
            return;
262
        }
263
    } else {
264
        // not top level element: see if parent is OK
265
        if (!in_array($XML_RPC_xh[$parser]['stack'][0], $XML_RPC_valid_parents[$name])) {
266
            $name = preg_replace('@[^a-zA-Z0-9._-]@', '', $name);
267
            $XML_RPC_xh[$parser]['isf'] = 2;
268
            $XML_RPC_xh[$parser]['isf_reason'] = sprintf(gettext('xmlrpc element %1$s cannot be child of %2$s'), $name, $XML_RPC_xh[$parser]['stack'][0]);
269
            return;
270
        }
271
    }
272

    
273
    switch ($name) {
274
    case 'STRUCT':
275
        $XML_RPC_xh[$parser]['cm']++;
276

    
277
        // turn quoting off
278
        $XML_RPC_xh[$parser]['qt'] = 0;
279

    
280
        $cur_val = array();
281
        $cur_val['value'] = array();
282
        $cur_val['members'] = 1;
283
        array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
284
        break;
285

    
286
    case 'ARRAY':
287
        $XML_RPC_xh[$parser]['cm']++;
288

    
289
        // turn quoting off
290
        $XML_RPC_xh[$parser]['qt'] = 0;
291

    
292
        $cur_val = array();
293
        $cur_val['value'] = array();
294
        $cur_val['members'] = 0;
295
        array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
296
        break;
297

    
298
    case 'NAME':
299
        $XML_RPC_xh[$parser]['ac'] = '';
300
        break;
301

    
302
    case 'FAULT':
303
        $XML_RPC_xh[$parser]['isf'] = 1;
304
        break;
305

    
306
    case 'PARAM':
307
        $XML_RPC_xh[$parser]['valuestack'] = array();
308
        break;
309

    
310
    case 'VALUE':
311
        $XML_RPC_xh[$parser]['lv'] = 1;
312
        $XML_RPC_xh[$parser]['vt'] = $GLOBALS['XML_RPC_String'];
313
        $XML_RPC_xh[$parser]['ac'] = '';
314
        $XML_RPC_xh[$parser]['qt'] = 0;
315
        // look for a value: if this is still 1 by the
316
        // time we reach the first data segment then the type is string
317
        // by implication and we need to add in a quote
318
        break;
319

    
320
    case 'I4':
321
    case 'INT':
322
    case 'STRING':
323
    case 'BOOLEAN':
324
    case 'DOUBLE':
325
    case 'DATETIME.ISO8601':
326
    case 'BASE64':
327
        $XML_RPC_xh[$parser]['ac'] = ''; // reset the accumulator
328

    
329
        if ($name == 'DATETIME.ISO8601' || $name == 'STRING') {
330
            $XML_RPC_xh[$parser]['qt'] = 1;
331

    
332
            if ($name == 'DATETIME.ISO8601') {
333
                $XML_RPC_xh[$parser]['vt'] = $GLOBALS['XML_RPC_DateTime'];
334
            }
335

    
336
        } elseif ($name == 'BASE64') {
337
            $XML_RPC_xh[$parser]['qt'] = 2;
338
        } else {
339
            // No quoting is required here -- but
340
            // at the end of the element we must check
341
            // for data format errors.
342
            $XML_RPC_xh[$parser]['qt'] = 0;
343
        }
344
        break;
345

    
346
    case 'MEMBER':
347
        $XML_RPC_xh[$parser]['ac'] = '';
348
        break;
349

    
350
    case 'DATA':
351
    case 'METHODCALL':
352
    case 'METHODNAME':
353
    case 'METHODRESPONSE':
354
    case 'PARAMS':
355
        // valid elements that add little to processing
356
        break;
357
    }
358

    
359

    
360
    // Save current element to stack
361
    array_unshift($XML_RPC_xh[$parser]['stack'], $name);
362

    
363
    if ($name != 'VALUE') {
364
        $XML_RPC_xh[$parser]['lv'] = 0;
365
    }
366
}
367

    
368
/**
369
 * End element handler for the XML parser
370
 *
371
 * @return void
372
 */
373
function XML_RPC_ee($parser_resource, $name)
374
{
375
    global $XML_RPC_xh;
376

    
377
    $parser = (int) $parser_resource;
378

    
379
    if ($XML_RPC_xh[$parser]['isf'] >= 2) {
380
        return;
381
    }
382

    
383
    // push this element from stack
384
    // NB: if XML validates, correct opening/closing is guaranteed and
385
    // we do not have to check for $name == $curr_elem.
386
    // we also checked for proper nesting at start of elements...
387
    $curr_elem = array_shift($XML_RPC_xh[$parser]['stack']);
388

    
389
    switch ($name) {
390
    case 'STRUCT':
391
    case 'ARRAY':
392
    $cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
393
    $XML_RPC_xh[$parser]['value'] = $cur_val['value'];
394
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
395
        $XML_RPC_xh[$parser]['cm']--;
396
        break;
397

    
398
    case 'NAME':
399
    $XML_RPC_xh[$parser]['valuestack'][0]['name'] = $XML_RPC_xh[$parser]['ac'];
400
        break;
401

    
402
    case 'BOOLEAN':
403
        // special case here: we translate boolean 1 or 0 into PHP
404
        // constants true or false
405
        if ($XML_RPC_xh[$parser]['ac'] == '1') {
406
            $XML_RPC_xh[$parser]['ac'] = 'true';
407
        } else {
408
            $XML_RPC_xh[$parser]['ac'] = 'false';
409
        }
410

    
411
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
412
        // Drop through intentionally.
413

    
414
    case 'I4':
415
    case 'INT':
416
    case 'STRING':
417
    case 'DOUBLE':
418
    case 'DATETIME.ISO8601':
419
    case 'BASE64':
420
        if ($XML_RPC_xh[$parser]['qt'] == 1) {
421
            // we use double quotes rather than single so backslashification works OK
422
            $XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
423
        } elseif ($XML_RPC_xh[$parser]['qt'] == 2) {
424
            $XML_RPC_xh[$parser]['value'] = base64_decode($XML_RPC_xh[$parser]['ac']);
425
        } elseif ($name == 'BOOLEAN') {
426
            $XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
427
        } else {
428
            // we have an I4, INT or a DOUBLE
429
            // we must check that only 0123456789-.<space> are characters here
430
            if (!preg_match("@^[+-]?[0123456789 \t\.]+$@", $XML_RPC_xh[$parser]['ac'])) {
431
                XML_RPC_Base::raiseError(gettext('Non-numeric value received in INT or DOUBLE'),
432
                                         XML_RPC_ERROR_NON_NUMERIC_FOUND);
433
                $XML_RPC_xh[$parser]['value'] = XML_RPC_ERROR_NON_NUMERIC_FOUND;
434
            } else {
435
                // it's ok, add it on
436
                $XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
437
            }
438
        }
439

    
440
        $XML_RPC_xh[$parser]['ac'] = '';
441
        $XML_RPC_xh[$parser]['qt'] = 0;
442
        $XML_RPC_xh[$parser]['lv'] = 3; // indicate we've found a value
443
        break;
444

    
445
    case 'VALUE':
446
        if ($XML_RPC_xh[$parser]['vt'] == $GLOBALS['XML_RPC_String']) {
447
            if (strlen($XML_RPC_xh[$parser]['ac']) > 0) {
448
                $XML_RPC_xh[$parser]['value'] = $XML_RPC_xh[$parser]['ac'];
449
            } elseif ($XML_RPC_xh[$parser]['lv'] == 1) {
450
                // The <value> element was empty.
451
                $XML_RPC_xh[$parser]['value'] = '';
452
            }
453
        }
454

    
455
        $temp = new XML_RPC_Value($XML_RPC_xh[$parser]['value'], $XML_RPC_xh[$parser]['vt']);
456

    
457
        $cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
458
        if (is_array($cur_val)) {
459
            if ($cur_val['members']==0) {
460
                $cur_val['value'][] = $temp;
461
            } else {
462
                $XML_RPC_xh[$parser]['value'] = $temp;
463
            }
464
            array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
465
        } else {
466
            $XML_RPC_xh[$parser]['value'] = $temp;
467
        }
468
        break;
469

    
470
    case 'MEMBER':
471
        $XML_RPC_xh[$parser]['ac'] = '';
472
        $XML_RPC_xh[$parser]['qt'] = 0;
473

    
474
        $cur_val = array_shift($XML_RPC_xh[$parser]['valuestack']);
475
        if (is_array($cur_val)) {
476
            if ($cur_val['members']==1) {
477
                $cur_val['value'][$cur_val['name']] = $XML_RPC_xh[$parser]['value'];
478
            }
479
            array_unshift($XML_RPC_xh[$parser]['valuestack'], $cur_val);
480
        }
481
        break;
482

    
483
    case 'DATA':
484
        $XML_RPC_xh[$parser]['ac'] = '';
485
        $XML_RPC_xh[$parser]['qt'] = 0;
486
        break;
487

    
488
    case 'PARAM':
489
        $XML_RPC_xh[$parser]['params'][] = $XML_RPC_xh[$parser]['value'];
490
        break;
491

    
492
    case 'METHODNAME':
493
    case 'RPCMETHODNAME':
494
        $XML_RPC_xh[$parser]['method'] = preg_replace("@^[\n\r\t ]+@", '',
495
                                                      $XML_RPC_xh[$parser]['ac']);
496
        break;
497
    }
498

    
499
    // if it's a valid type name, set the type
500
    if (isset($GLOBALS['XML_RPC_Types'][strtolower($name)])) {
501
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
502
    }
503
}
504

    
505
/**
506
 * Character data handler for the XML parser
507
 *
508
 * @return void
509
 */
510
function XML_RPC_cd($parser_resource, $data)
511
{
512
    global $XML_RPC_xh, $XML_RPC_backslash;
513

    
514
    $parser = (int) $parser_resource;
515

    
516
    if ($XML_RPC_xh[$parser]['lv'] != 3) {
517
        // "lookforvalue==3" means that we've found an entire value
518
        // and should discard any further character data
519

    
520
        if ($XML_RPC_xh[$parser]['lv'] == 1) {
521
            // if we've found text and we're just in a <value> then
522
            // turn quoting on, as this will be a string
523
            $XML_RPC_xh[$parser]['qt'] = 1;
524
            // and say we've found a value
525
            $XML_RPC_xh[$parser]['lv'] = 2;
526
        }
527

    
528
        // replace characters that eval would
529
        // do special things with
530
        if (!isset($XML_RPC_xh[$parser]['ac'])) {
531
            $XML_RPC_xh[$parser]['ac'] = '';
532
        }
533
        $XML_RPC_xh[$parser]['ac'] .= $data;
534
    }
535
}
536

    
537
/**
538
 * The common methods and properties for all of the XML_RPC classes
539
 *
540
 * @category   Web Services
541
 * @package    XML_RPC
542
 * @author     Edd Dumbill <edd@usefulinc.com>
543
 * @author     Stig Bakken <stig@php.net>
544
 * @author     Martin Jansen <mj@php.net>
545
 * @author     Daniel Convissor <danielc@php.net>
546
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
547
 * @license    http://www.php.net/license/3_01.txt  PHP License
548
 * @version    Release: @package_version@
549
 * @link       http://pear.php.net/package/XML_RPC
550
 */
551
class XML_RPC_Base {
552

    
553
    /**
554
     * PEAR Error handling
555
     *
556
     * @return object  PEAR_Error object
557
     */
558
    function raiseError($msg, $code)
559
    {
560
        include_once 'PEAR.inc';
561
        if (is_object(@$this)) {
562
	    log_error(get_class($this) . ': ' . $msg . " {$code}");
563
            return PEAR::raiseError(get_class($this) . ': ' . $msg, $code);
564
        } else {
565
	    log_error("XML_RPC: " . ': ' . $msg . " {$code}");
566
            return PEAR::raiseError('XML_RPC: ' . $msg, $code);
567
        }
568
    }
569

    
570
    /**
571
     * Tell whether something is a PEAR_Error object
572
     *
573
     * @param mixed $value  the item to check
574
     *
575
     * @return bool  whether $value is a PEAR_Error object or not
576
     *
577
     * @access public
578
     */
579
    function isError($value)
580
    {
581
        return is_a($value, 'PEAR_Error');
582
    }
583
}
584

    
585
/**
586
 * The methods and properties for submitting XML RPC requests
587
 *
588
 * @category   Web Services
589
 * @package    XML_RPC
590
 * @author     Edd Dumbill <edd@usefulinc.com>
591
 * @author     Stig Bakken <stig@php.net>
592
 * @author     Martin Jansen <mj@php.net>
593
 * @author     Daniel Convissor <danielc@php.net>
594
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
595
 * @license    http://www.php.net/license/3_01.txt  PHP License
596
 * @version    Release: @package_version@
597
 * @link       http://pear.php.net/package/XML_RPC
598
 */
599
class XML_RPC_Client extends XML_RPC_Base {
600

    
601
    /**
602
     * The path and name of the RPC server script you want the request to go to
603
     * @var string
604
     */
605
    var $path = '';
606

    
607
    /**
608
     * The name of the remote server to connect to
609
     * @var string
610
     */
611
    var $server = '';
612

    
613
    /**
614
     * The protocol to use in contacting the remote server
615
     * @var string
616
     */
617
    var $protocol = 'http://';
618

    
619
    /**
620
     * The port for connecting to the remote server
621
     *
622
     * The default is 80 for http:// connections
623
     * and 443 for https:// and ssl:// connections.
624
     *
625
     * @var integer
626
     */
627
    var $port = 80;
628

    
629
    /**
630
     * A user name for accessing the RPC server
631
     * @var string
632
     * @see XML_RPC_Client::setCredentials()
633
     */
634
    var $username = '';
635

    
636
    /**
637
     * A password for accessing the RPC server
638
     * @var string
639
     * @see XML_RPC_Client::setCredentials()
640
     */
641
    var $password = '';
642

    
643
    /**
644
     * The name of the proxy server to use, if any
645
     * @var string
646
     */
647
    var $proxy = '';
648

    
649
    /**
650
     * The protocol to use in contacting the proxy server, if any
651
     * @var string
652
     */
653
    var $proxy_protocol = 'http://';
654

    
655
    /**
656
     * The port for connecting to the proxy server
657
     *
658
     * The default is 8080 for http:// connections
659
     * and 443 for https:// and ssl:// connections.
660
     *
661
     * @var integer
662
     */
663
    var $proxy_port = 8080;
664

    
665
    /**
666
     * A user name for accessing the proxy server
667
     * @var string
668
     */
669
    var $proxy_user = '';
670

    
671
    /**
672
     * A password for accessing the proxy server
673
     * @var string
674
     */
675
    var $proxy_pass = '';
676

    
677
    /**
678
     * The error number, if any
679
     * @var integer
680
     */
681
    var $errno = 0;
682

    
683
    /**
684
     * The error message, if any
685
     * @var string
686
     */
687
    var $errstr = '';
688

    
689
    /**
690
     * The current debug mode (1 = on, 0 = off)
691
     * @var integer
692
     */
693
    var $debug = 0;
694

    
695
    /**
696
     * The HTTP headers for the current request.
697
     * @var string
698
     */
699
    var $headers = '';
700

    
701

    
702
    /**
703
     * Sets the object's properties
704
     *
705
     * @param string  $path        the path and name of the RPC server script
706
     *                              you want the request to go to
707
     * @param string  $server      the URL of the remote server to connect to.
708
     *                              If this parameter doesn't specify a
709
     *                              protocol and $port is 443, ssl:// is
710
     *                              assumed.
711
     * @param integer $port        a port for connecting to the remote server.
712
     *                              Defaults to 80 for http:// connections and
713
     *                              443 for https:// and ssl:// connections.
714
     * @param string  $proxy       the URL of the proxy server to use, if any.
715
     *                              If this parameter doesn't specify a
716
     *                              protocol and $port is 443, ssl:// is
717
     *                              assumed.
718
     * @param integer $proxy_port  a port for connecting to the remote server.
719
     *                              Defaults to 8080 for http:// connections and
720
     *                              443 for https:// and ssl:// connections.
721
     * @param string  $proxy_user  a user name for accessing the proxy server
722
     * @param string  $proxy_pass  a password for accessing the proxy server
723
     *
724
     * @return void
725
     */
726
    function XML_RPC_Client($path, $server, $port = 0,
727
                            $proxy = '', $proxy_port = 0,
728
                            $proxy_user = '', $proxy_pass = '')
729
    {
730
        $this->path       = $path;
731
        $this->proxy_user = $proxy_user;
732
        $this->proxy_pass = $proxy_pass;
733

    
734
        preg_match('@^(http://|https://|ssl://)?(.*)$@', $server, $match);
735
        if ($match[1] == '') {
736
            if ($port == 443) {
737
                $this->server   = $match[2];
738
                $this->protocol = 'ssl://';
739
                $this->port     = 443;
740
            } else {
741
                $this->server = $match[2];
742
                if ($port) {
743
                    $this->port = $port;
744
                }
745
            }
746
        } elseif ($match[1] == 'http://') {
747
            $this->server = $match[2];
748
            if ($port) {
749
                $this->port = $port;
750
            }
751
        } else {
752
            $this->server   = $match[2];
753
            $this->protocol = 'ssl://';
754
            if ($port) {
755
                $this->port = $port;
756
            } else {
757
                $this->port = 443;
758
            }
759
        }
760

    
761
        if ($proxy) {
762
            preg_match('@^(http://|https://|ssl://)?(.*)$@', $proxy, $match);
763
            if ($match[1] == '') {
764
                if ($proxy_port == 443) {
765
                    $this->proxy          = $match[2];
766
                    $this->proxy_protocol = 'ssl://';
767
                    $this->proxy_port     = 443;
768
                } else {
769
                    $this->proxy = $match[2];
770
                    if ($proxy_port) {
771
                        $this->proxy_port = $proxy_port;
772
                    }
773
                }
774
            } elseif ($match[1] == 'http://') {
775
                $this->proxy = $match[2];
776
                if ($proxy_port) {
777
                    $this->proxy_port = $proxy_port;
778
                }
779
            } else {
780
                $this->proxy          = $match[2];
781
                $this->proxy_protocol = 'ssl://';
782
                if ($proxy_port) {
783
                    $this->proxy_port = $proxy_port;
784
                } else {
785
                    $this->proxy_port = 443;
786
                }
787
            }
788
        }
789
    }
790

    
791
    /**
792
     * Change the current debug mode
793
     *
794
     * @param int $in  where 1 = on, 0 = off
795
     *
796
     * @return void
797
     */
798
    function setDebug($in)
799
    {
800
        if ($in) {
801
            $this->debug = 1;
802
        } else {
803
            $this->debug = 0;
804
        }
805
    }
806

    
807
    /**
808
     * Sets whether strings that contain characters which may cause PHP's
809
     * SAX-based XML parser to break should be automatically base64 encoded
810
     *
811
     * This is is a workaround for systems that don't have PHP's mbstring
812
     * extension available.
813
     *
814
     * @param int $in  where 1 = on, 0 = off
815
     *
816
     * @return void
817
     */
818
    function setAutoBase64($in)
819
    {
820
        if ($in) {
821
            $GLOBALS['XML_RPC_auto_base64'] = true;
822
        } else {
823
            $GLOBALS['XML_RPC_auto_base64'] = false;
824
        }
825
    }
826

    
827
    /**
828
     * Set username and password properties for connecting to the RPC server
829
     *
830
     * @param string $u  the user name
831
     * @param string $p  the password
832
     *
833
     * @return void
834
     *
835
     * @see XML_RPC_Client::$username, XML_RPC_Client::$password
836
     */
837
    function setCredentials($u, $p)
838
    {
839
        $this->username = $u;
840
        $this->password = $p;
841
    }
842

    
843
    /**
844
     * Transmit the RPC request via HTTP 1.0 protocol
845
     *
846
     * @param object $msg       the XML_RPC_Message object
847
     * @param int    $timeout   how many seconds to wait for the request
848
     *
849
     * @return object  an XML_RPC_Response object.  0 is returned if any
850
     *                  problems happen.
851
     *
852
     * @see XML_RPC_Message, XML_RPC_Client::XML_RPC_Client(),
853
     *      XML_RPC_Client::setCredentials()
854
     */
855
    function send($msg, $timeout = 0)
856
    {
857
        if (!is_a($msg, 'XML_RPC_Message')) {
858
            $this->errstr = sprintf(
859
                gettext(
860
                    "send()'s %s parameter must be an XML_RPC_Message object."
861
                ), $msg);
862
            $this->raiseError($this->errstr, XML_RPC_ERROR_PROGRAMMING);
863
            return 0;
864
        }
865
        $msg->debug = $this->debug;
866
        return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
867
                                        $timeout, $this->username,
868
                                        $this->password);
869
    }
870

    
871
    /**
872
     * Transmit the RPC request via HTTP 1.0 protocol
873
     *
874
     * Requests should be sent using XML_RPC_Client send() rather than
875
     * calling this method directly.
876
     *
877
     * @param object $msg       the XML_RPC_Message object
878
     * @param string $server    the server to send the request to
879
     * @param int    $port      the server port send the request to
880
     * @param int    $timeout   how many seconds to wait for the request
881
     *                           before giving up
882
     * @param string $username  a user name for accessing the RPC server
883
     * @param string $password  a password for accessing the RPC server
884
     *
885
     * @return object  an XML_RPC_Response object.  0 is returned if any
886
     *                  problems happen.
887
     *
888
     * @access protected
889
     * @see XML_RPC_Client::send()
890
     */
891
    function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
892
                               $username = '', $password = '')
893
    {
894
        // Pre-emptive BC hacks for fools calling sendPayloadHTTP10() directly
895
        if ($username != $this->username) {
896
            $this->setCredentials($username, $password);
897
        }
898

    
899
        // Only create the payload if it was not created previously
900
        if (empty($msg->payload)) {
901
            $msg->createPayload();
902
        }
903
        $this->createHeaders($msg);
904

    
905
        $op  = $this->headers . "\r\n\r\n";
906
        $op .= $msg->payload;
907

    
908
        if ($this->debug) {
909
            print "\n<pre>---SENT---\n";
910
            print $op;
911
            print "\n---END---</pre>\n";
912
        }
913

    
914
        /*
915
         * If we're using a proxy open a socket to the proxy server
916
         * instead to the xml-rpc server
917
         */
918
        if ($this->proxy) {
919
            if ($this->proxy_protocol == 'http://') {
920
                $protocol = '';
921
            } else {
922
                $protocol = $this->proxy_protocol;
923
            }
924
            if ($timeout > 0) {
925
                $fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
926
                                 $this->errno, $this->errstr, $timeout);
927
            } else {
928
                $fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
929
                                 $this->errno, $this->errstr);
930
            }
931
        } else {
932
            if ($this->protocol == 'http://') {
933
                $protocol = '';
934
            } else {
935
                $protocol = $this->protocol;
936
            }
937
            if ($timeout > 0) {
938
                $fp = @fsockopen($protocol . $server, $port,
939
                                 $this->errno, $this->errstr, $timeout);
940
            } else {
941
                $fp = @fsockopen($protocol . $server, $port,
942
                                 $this->errno, $this->errstr);
943
            }
944
        }
945

    
946
        /*
947
         * Just raising the error without returning it is strange,
948
         * but keep it here for backwards compatibility.
949
         */
950
        if (!$fp && $this->proxy) {
951
            $this->raiseError(sprintf(gettext('Connection to proxy server 
952
                              %1$s:%2$s failed. %3$s')
953
                              ,$this->proxy,$this->proxy_port,$this->errstr),
954
                              XML_RPC_ERROR_CONNECTION_FAILED);
955
            return 0;
956
        } elseif (!$fp) {
957
            $this->raiseError(sprintf(gettext('Connection to RPC server 
958
                              %1$s:%2$s failed. %3$s')
959
                              ,$server,$port,$this->errstr),
960
                              XML_RPC_ERROR_CONNECTION_FAILED);
961
            return 0;
962
        }
963

    
964
        if ($timeout) {
965
            /*
966
             * Using socket_set_timeout() because stream_set_timeout()
967
             * was introduced in 4.3.0, but we need to support 4.2.0.
968
             */
969
            socket_set_timeout($fp, $timeout);
970
        }
971

    
972
        if (!fputs($fp, $op, strlen($op))) {
973
            $this->errstr = 'Write error';
974
            return 0;
975
        }
976
        $resp = $msg->parseResponseFile($fp);
977

    
978
        $meta = socket_get_status($fp);
979
        if ($meta['timed_out']) {
980
            fclose($fp);
981
            $this->errstr = 'RPC server did not send response before timeout.';
982
            $this->raiseError($this->errstr, XML_RPC_ERROR_CONNECTION_FAILED);
983
            return 0;
984
        }
985

    
986
        fclose($fp);
987
        return $resp;
988
    }
989

    
990
    /**
991
     * Determines the HTTP headers and puts it in the $headers property
992
     *
993
     * @param object $msg       the XML_RPC_Message object
994
     *
995
     * @return boolean  TRUE if okay, FALSE if the message payload isn't set.
996
     *
997
     * @access protected
998
     */
999
    function createHeaders($msg)
1000
    {
1001
        if (empty($msg->payload)) {
1002
            return false;
1003
        }
1004
        if ($this->proxy) {
1005
            $this->headers = 'POST ' . $this->protocol . $this->server;
1006
            if ($this->proxy_port) {
1007
                $this->headers .= ':' . $this->port;
1008
            }
1009
        } else {
1010
           $this->headers = 'POST ';
1011
        }
1012
        $this->headers .= $this->path. " HTTP/1.0\r\n";
1013

    
1014
        $this->headers .= "User-Agent: PEAR XML_RPC\r\n";
1015
        $this->headers .= 'Host: ' . $this->server . "\r\n";
1016

    
1017
        if ($this->proxy && $this->proxy_user) {
1018
            $this->headers .= 'Proxy-Authorization: Basic '
1019
                     . base64_encode("$this->proxy_user:$this->proxy_pass")
1020
                     . "\r\n";
1021
        }
1022

    
1023
        // thanks to Grant Rauscher <grant7@firstworld.net> for this
1024
        if ($this->username) {
1025
            $this->headers .= 'Authorization: Basic '
1026
                     . base64_encode("$this->username:$this->password")
1027
                     . "\r\n";
1028
        }
1029

    
1030
        $this->headers .= "Content-Type: text/xml\r\n";
1031
        $this->headers .= 'Content-Length: ' . strlen($msg->payload);
1032
        return true;
1033
    }
1034
}
1035

    
1036
/**
1037
 * The methods and properties for interpreting responses to XML RPC requests
1038
 *
1039
 * @category   Web Services
1040
 * @package    XML_RPC
1041
 * @author     Edd Dumbill <edd@usefulinc.com>
1042
 * @author     Stig Bakken <stig@php.net>
1043
 * @author     Martin Jansen <mj@php.net>
1044
 * @author     Daniel Convissor <danielc@php.net>
1045
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
1046
 * @license    http://www.php.net/license/3_01.txt  PHP License
1047
 * @version    Release: @package_version@
1048
 * @link       http://pear.php.net/package/XML_RPC
1049
 */
1050
class XML_RPC_Response extends XML_RPC_Base
1051
{
1052
    var $xv;
1053
    var $fn;
1054
    var $fs;
1055
    var $hdrs;
1056

    
1057
    /**
1058
     * @return void
1059
     */
1060
    function XML_RPC_Response($val, $fcode = 0, $fstr = '')
1061
    {
1062
        if ($fcode != 0) {
1063
            $this->fn = $fcode;
1064
            $this->fs = htmlspecialchars($fstr);
1065
        } else {
1066
            $this->xv = $val;
1067
        }
1068
    }
1069

    
1070
    /**
1071
     * @return int  the error code
1072
     */
1073
    function faultCode()
1074
    {
1075
        if (isset($this->fn)) {
1076
            return $this->fn;
1077
        } else {
1078
            return 0;
1079
        }
1080
    }
1081

    
1082
    /**
1083
     * @return string  the error string
1084
     */
1085
    function faultString()
1086
    {
1087
        return $this->fs;
1088
    }
1089

    
1090
    /**
1091
     * @return mixed  the value
1092
     */
1093
    function value()
1094
    {
1095
        return $this->xv;
1096
    }
1097

    
1098
    /**
1099
     * @return string  the error message in XML format
1100
     */
1101
    function serialize()
1102
    {
1103
        $rs = "<methodResponse>\n";
1104
        if ($this->fn) {
1105
            $rs .= "<fault>
1106
  <value>
1107
    <struct>
1108
      <member>
1109
        <name>faultCode</name>
1110
        <value><int>" . $this->fn . "</int></value>
1111
      </member>
1112
      <member>
1113
        <name>faultString</name>
1114
        <value><string>" . $this->fs . "</string></value>
1115
      </member>
1116
    </struct>
1117
  </value>
1118
</fault>";
1119
        } else {
1120
            $rs .= "<params>\n<param>\n" . $this->xv->serialize() .
1121
        "</param>\n</params>";
1122
        }
1123
        $rs .= "\n</methodResponse>";
1124
        return $rs;
1125
    }
1126
}
1127

    
1128
/**
1129
 * The methods and properties for composing XML RPC messages
1130
 *
1131
 * @category   Web Services
1132
 * @package    XML_RPC
1133
 * @author     Edd Dumbill <edd@usefulinc.com>
1134
 * @author     Stig Bakken <stig@php.net>
1135
 * @author     Martin Jansen <mj@php.net>
1136
 * @author     Daniel Convissor <danielc@php.net>
1137
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
1138
 * @license    http://www.php.net/license/3_01.txt  PHP License
1139
 * @version    Release: @package_version@
1140
 * @link       http://pear.php.net/package/XML_RPC
1141
 */
1142
class XML_RPC_Message extends XML_RPC_Base
1143
{
1144
    /**
1145
     * Should the payload's content be passed through mb_convert_encoding()?
1146
     *
1147
     * @see XML_RPC_Message::setConvertPayloadEncoding()
1148
     * @since Property available since Release 1.5.1
1149
     * @var boolean
1150
     */
1151
    var $convert_payload_encoding = false;
1152

    
1153
    /**
1154
     * The current debug mode (1 = on, 0 = off)
1155
     * @var integer
1156
     */
1157
    var $debug = 0;
1158

    
1159
    /**
1160
     * The encoding to be used for outgoing messages
1161
     *
1162
     * Defaults to the value of <var>$GLOBALS['XML_RPC_defencoding']</var>
1163
     *
1164
     * @var string
1165
     * @see XML_RPC_Message::setSendEncoding(),
1166
     *      $GLOBALS['XML_RPC_defencoding'], XML_RPC_Message::xml_header()
1167
     */
1168
    var $send_encoding = '';
1169

    
1170
    /**
1171
     * The method presently being evaluated
1172
     * @var string
1173
     */
1174
    var $methodname = '';
1175

    
1176
    /**
1177
     * @var array
1178
     */
1179
    var $params = array();
1180

    
1181
    /**
1182
     * The XML message being generated
1183
     * @var string
1184
     */
1185
    var $payload = '';
1186

    
1187
    /**
1188
     * Should extra line breaks be removed from the payload?
1189
     * @since Property available since Release 1.4.6
1190
     * @var boolean
1191
     */
1192
    var $remove_extra_lines = true;
1193

    
1194
    /**
1195
     * The XML response from the remote server
1196
     * @since Property available since Release 1.4.6
1197
     * @var string
1198
     */
1199
    var $response_payload = '';
1200

    
1201

    
1202
    /**
1203
     * @return void
1204
     */
1205
    function XML_RPC_Message($meth, $pars = 0)
1206
    {
1207
        $this->methodname = $meth;
1208
        if (is_array($pars) && sizeof($pars) > 0) {
1209
            for ($i = 0; $i < sizeof($pars); $i++) {
1210
                $this->addParam($pars[$i]);
1211
            }
1212
        }
1213
    }
1214

    
1215
    /**
1216
     * Produces the XML declaration including the encoding attribute
1217
     *
1218
     * The encoding is determined by this class' <var>$send_encoding</var>
1219
     * property.  If the <var>$send_encoding</var> property is not set, use
1220
     * <var>$GLOBALS['XML_RPC_defencoding']</var>.
1221
     *
1222
     * @return string  the XML declaration and <methodCall> element
1223
     *
1224
     * @see XML_RPC_Message::setSendEncoding(),
1225
     *      XML_RPC_Message::$send_encoding, $GLOBALS['XML_RPC_defencoding']
1226
     */
1227
    function xml_header()
1228
    {
1229
        global $XML_RPC_defencoding;
1230

    
1231
        if (!$this->send_encoding) {
1232
            $this->send_encoding = $XML_RPC_defencoding;
1233
        }
1234
        return '<?xml version="1.0" encoding="' . $this->send_encoding . '"?>'
1235
               . "\n<methodCall>\n";
1236
    }
1237

    
1238
    /**
1239
     * @return string  the closing </methodCall> tag
1240
     */
1241
    function xml_footer()
1242
    {
1243
        return "</methodCall>\n";
1244
    }
1245

    
1246
    /**
1247
     * Fills the XML_RPC_Message::$payload property
1248
     *
1249
     * Part of the process makes sure all line endings are in DOS format
1250
     * (CRLF), which is probably required by specifications.
1251
     *
1252
     * If XML_RPC_Message::setConvertPayloadEncoding() was set to true,
1253
     * the payload gets passed through mb_convert_encoding()
1254
     * to ensure the payload matches the encoding set in the
1255
     * XML declaration.  The encoding type can be manually set via
1256
     * XML_RPC_Message::setSendEncoding().
1257
     *
1258
     * @return void
1259
     *
1260
     * @uses XML_RPC_Message::xml_header(), XML_RPC_Message::xml_footer()
1261
     * @see XML_RPC_Message::setSendEncoding(), $GLOBALS['XML_RPC_defencoding'],
1262
     *      XML_RPC_Message::setConvertPayloadEncoding()
1263
     */
1264
    function createPayload()
1265
    {
1266
        $this->payload = $this->xml_header();
1267
        $this->payload .= '<methodName>' . $this->methodname . "</methodName>\n";
1268
        $this->payload .= "<params>\n";
1269
        for ($i = 0; $i < sizeof($this->params); $i++) {
1270
            $p = $this->params[$i];
1271
            $this->payload .= "<param>\n" . $p->serialize() . "</param>\n";
1272
        }
1273
        $this->payload .= "</params>\n";
1274
        $this->payload .= $this->xml_footer();
1275
        if ($this->remove_extra_lines) {
1276
            $this->payload = preg_replace("@[\r\n]+@", "\r\n", $this->payload);
1277
        } else {
1278
            $this->payload = preg_replace("@\r\n|\n|\r|\n\r@", "\r\n", $this->payload);
1279
        }
1280
        if ($this->convert_payload_encoding) {
1281
            $this->payload = mb_convert_encoding($this->payload, $this->send_encoding);
1282
        }
1283
    }
1284

    
1285
    /**
1286
     * @return string  the name of the method
1287
     */
1288
    function method($meth = '')
1289
    {
1290
        if ($meth != '') {
1291
            $this->methodname = $meth;
1292
        }
1293
        return $this->methodname;
1294
    }
1295

    
1296
    /**
1297
     * @return string  the payload
1298
     */
1299
    function serialize()
1300
    {
1301
        $this->createPayload();
1302
        return $this->payload;
1303
    }
1304

    
1305
    /**
1306
     * @return void
1307
     */
1308
    function addParam($par)
1309
    {
1310
        $this->params[] = $par;
1311
    }
1312

    
1313
    /**
1314
     * Obtains an XML_RPC_Value object for the given parameter
1315
     *
1316
     * @param int $i  the index number of the parameter to obtain
1317
     *
1318
     * @return object  the XML_RPC_Value object.
1319
     *                  If the parameter doesn't exist, an XML_RPC_Response object.
1320
     *
1321
     * @since Returns XML_RPC_Response object on error since Release 1.3.0
1322
     */
1323
    function getParam($i)
1324
    {
1325
        global $XML_RPC_err, $XML_RPC_str;
1326

    
1327
        if (isset($this->params[$i])) {
1328
            return $this->params[$i];
1329
        } else {
1330
            $this->raiseError(gettext('The submitted request did not contain this parameter'),
1331
                              XML_RPC_ERROR_INCORRECT_PARAMS);
1332
            return new XML_RPC_Response(0, $XML_RPC_err['incorrect_params'],
1333
                                        $XML_RPC_str['incorrect_params']);
1334
        }
1335
    }
1336

    
1337
    /**
1338
     * @return int  the number of parameters
1339
     */
1340
    function getNumParams()
1341
    {
1342
        return sizeof($this->params);
1343
    }
1344

    
1345
    /**
1346
     * Sets whether the payload's content gets passed through
1347
     * mb_convert_encoding()
1348
     *
1349
     * Returns PEAR_ERROR object if mb_convert_encoding() isn't available.
1350
     *
1351
     * @param int $in  where 1 = on, 0 = off
1352
     *
1353
     * @return void
1354
     *
1355
     * @see XML_RPC_Message::setSendEncoding()
1356
     * @since Method available since Release 1.5.1
1357
     */
1358
    function setConvertPayloadEncoding($in)
1359
    {
1360
        if ($in && !function_exists('mb_convert_encoding')) {
1361
            return $this->raiseError(gettext('mb_convert_encoding() is not available'),
1362
                              XML_RPC_ERROR_PROGRAMMING);
1363
        }
1364
        $this->convert_payload_encoding = $in;
1365
    }
1366

    
1367
    /**
1368
     * Sets the XML declaration's encoding attribute
1369
     *
1370
     * @param string $type  the encoding type (ISO-8859-1, UTF-8 or US-ASCII)
1371
     *
1372
     * @return void
1373
     *
1374
     * @see XML_RPC_Message::setConvertPayloadEncoding(), XML_RPC_Message::xml_header()
1375
     * @since Method available since Release 1.2.0
1376
     */
1377
    function setSendEncoding($type)
1378
    {
1379
        $this->send_encoding = $type;
1380
    }
1381

    
1382
    /**
1383
     * Determine the XML's encoding via the encoding attribute
1384
     * in the XML declaration
1385
     *
1386
     * If the encoding parameter is not set or is not ISO-8859-1, UTF-8
1387
     * or US-ASCII, $XML_RPC_defencoding will be returned.
1388
     *
1389
     * @param string $data  the XML that will be parsed
1390
     *
1391
     * @return string  the encoding to be used
1392
     *
1393
     * @link   http://php.net/xml_parser_create
1394
     * @since  Method available since Release 1.2.0
1395
     */
1396
    function getEncoding($data)
1397
    {
1398
        global $XML_RPC_defencoding;
1399

    
1400
        if (preg_match('@<\?xml[^>]*\s*encoding\s*=\s*[\'"]([^"\']*)[\'"]@',
1401
                       $data, $match))
1402
        {
1403
            $match[1] = trim(strtoupper($match[1]));
1404
            switch ($match[1]) {
1405
                case 'ISO-8859-1':
1406
                case 'UTF-8':
1407
                case 'US-ASCII':
1408
                    return $match[1];
1409
                    break;
1410

    
1411
                default:
1412
                    return $XML_RPC_defencoding;
1413
            }
1414
        } else {
1415
            return $XML_RPC_defencoding;
1416
        }
1417
    }
1418

    
1419
    /**
1420
     * @return object  a new XML_RPC_Response object
1421
     */
1422
    function parseResponseFile($fp)
1423
    {
1424
        $ipd = '';
1425
        while ($data = @fread($fp, 8192)) {
1426
            $ipd .= $data;
1427
        }
1428
        return $this->parseResponse($ipd);
1429
    }
1430

    
1431
    /**
1432
     * @return object  a new XML_RPC_Response object
1433
     */
1434
    function parseResponse($data = '')
1435
    {
1436
        global $XML_RPC_xh, $XML_RPC_err, $XML_RPC_str, $XML_RPC_defencoding;
1437

    
1438
        $encoding = $this->getEncoding($data);
1439
        $parser_resource = xml_parser_create($encoding);
1440
        $parser = (int) $parser_resource;
1441

    
1442
        $XML_RPC_xh = array();
1443
        $XML_RPC_xh[$parser] = array();
1444

    
1445
        $XML_RPC_xh[$parser]['cm'] = 0;
1446
        $XML_RPC_xh[$parser]['isf'] = 0;
1447
        $XML_RPC_xh[$parser]['ac'] = '';
1448
        $XML_RPC_xh[$parser]['qt'] = '';
1449
        $XML_RPC_xh[$parser]['stack'] = array();
1450
        $XML_RPC_xh[$parser]['valuestack'] = array();
1451

    
1452
        xml_parser_set_option($parser_resource, XML_OPTION_CASE_FOLDING, true);
1453
        xml_set_element_handler($parser_resource, 'XML_RPC_se', 'XML_RPC_ee');
1454
        xml_set_character_data_handler($parser_resource, 'XML_RPC_cd');
1455

    
1456
        $hdrfnd = 0;
1457
        if ($this->debug) {
1458
            print "\n<pre>---GOT---\n";
1459
            print isset($_SERVER['SERVER_PROTOCOL']) ? htmlspecialchars($data) : $data;
1460
            print "\n---END---</pre>\n";
1461
        }
1462

    
1463
        // See if response is a 200 or a 100 then a 200, else raise error.
1464
        // But only do this if we're using the HTTP protocol.
1465
        if (preg_match('@^HTTP@', $data) &&
1466
            !preg_match('@^HTTP/[0-9\.]+ 200 @', $data) &&
1467
            !preg_match('@^HTTP/[0-9\.]+ 10[0-9]([A-Z ]+)?[\r\n]+HTTP/[0-9\.]+ 200@', $data))
1468
        {
1469
                $errstr = substr($data, 0, strpos($data, "\n") - 1);
1470
                error_log(sprintf(gettext("HTTP error, got response: %s"),$errstr));
1471
                $r = new XML_RPC_Response(0, $XML_RPC_err['http_error'],
1472
                                          $XML_RPC_str['http_error'] . ' (' .
1473
                                          $errstr . ')');
1474
                xml_parser_free($parser_resource);
1475
                return $r;
1476
        }
1477

    
1478
        // gotta get rid of headers here
1479
        if (!$hdrfnd && ($brpos = strpos($data,"\r\n\r\n"))) {
1480
            $XML_RPC_xh[$parser]['ha'] = substr($data, 0, $brpos);
1481
            $data = substr($data, $brpos + 4);
1482
            $hdrfnd = 1;
1483
        }
1484

    
1485
        /*
1486
         * be tolerant of junk after methodResponse
1487
         * (e.g. javascript automatically inserted by free hosts)
1488
         * thanks to Luca Mariano <luca.mariano@email.it>
1489
         */
1490
        $data = substr($data, 0, strpos($data, "</methodResponse>") + 17);
1491
        $this->response_payload = $data;
1492

    
1493
        if (!xml_parse($parser_resource, $data, sizeof($data))) {
1494
            // thanks to Peter Kocks <peter.kocks@baygate.com>
1495
            if (xml_get_current_line_number($parser_resource) == 1) {
1496
		/* We already error on this in the GUI, no need to log it and cause a PHP error. */
1497
		//$errstr = gettext("XML error at line 1, check URL");
1498
            } else {
1499
                $errstr = sprintf('XML error: %s at line %d',
1500
                                  xml_error_string(xml_get_error_code($parser_resource)),
1501
                                  xml_get_current_line_number($parser_resource));
1502
            }
1503
		if (!empty($errstr))
1504
			error_log($errstr);
1505
            $r = new XML_RPC_Response(0, $XML_RPC_err['invalid_return'],
1506
                                      $XML_RPC_str['invalid_return']);
1507
            xml_parser_free($parser_resource);
1508
            return $r;
1509
        }
1510

    
1511
        xml_parser_free($parser_resource);
1512

    
1513
        if ($this->debug) {
1514
            print "\n<pre>---PARSED---\n";
1515
            var_dump($XML_RPC_xh[$parser]['value']);
1516
            print "---END---</pre>\n";
1517
        }
1518

    
1519
        if ($XML_RPC_xh[$parser]['isf'] > 1) {
1520
            $r = new XML_RPC_Response(0, $XML_RPC_err['invalid_return'],
1521
                                      $XML_RPC_str['invalid_return'].' '.$XML_RPC_xh[$parser]['isf_reason']);
1522
        } elseif (!is_object($XML_RPC_xh[$parser]['value'])) {
1523
            // then something odd has happened
1524
            // and it's time to generate a client side error
1525
            // indicating something odd went on
1526
            $r = new XML_RPC_Response(0, $XML_RPC_err['invalid_return'],
1527
                                      $XML_RPC_str['invalid_return']);
1528
        } else {
1529
            $v = $XML_RPC_xh[$parser]['value'];
1530
            if ($XML_RPC_xh[$parser]['isf']) {
1531
                $f = $v->structmem('faultCode');
1532
                $fs = $v->structmem('faultString');
1533
                $r = new XML_RPC_Response($v, $f->scalarval(),
1534
                                          $fs->scalarval());
1535
            } else {
1536
                $r = new XML_RPC_Response($v);
1537
            }
1538
        }
1539
        $r->hdrs = preg_split("@\r?\n@", $XML_RPC_xh[$parser]['ha'][1]);
1540
        return $r;
1541
    }
1542
}
1543

    
1544
/**
1545
 * The methods and properties that represent data in XML RPC format
1546
 *
1547
 * @category   Web Services
1548
 * @package    XML_RPC
1549
 * @author     Edd Dumbill <edd@usefulinc.com>
1550
 * @author     Stig Bakken <stig@php.net>
1551
 * @author     Martin Jansen <mj@php.net>
1552
 * @author     Daniel Convissor <danielc@php.net>
1553
 * @copyright  1999-2001 Edd Dumbill, 2001-2010 The PHP Group
1554
 * @license    http://www.php.net/license/3_01.txt  PHP License
1555
 * @version    Release: @package_version@
1556
 * @link       http://pear.php.net/package/XML_RPC
1557
 */
1558
class XML_RPC_Value extends XML_RPC_Base
1559
{
1560
    var $me = array();
1561
    var $mytype = 0;
1562

    
1563
    /**
1564
     * @return void
1565
     */
1566
    function XML_RPC_Value($val = -1, $type = '')
1567
    {
1568
        $this->me = array();
1569
        $this->mytype = 0;
1570
        if ($val != -1 || $type != '') {
1571
            if ($type == '') {
1572
                $type = 'string';
1573
            }
1574
            if (!array_key_exists($type, $GLOBALS['XML_RPC_Types'])) {
1575
                // XXX
1576
                // need some way to report this error
1577
            } elseif ($GLOBALS['XML_RPC_Types'][$type] == 1) {
1578
                $this->addScalar($val, $type);
1579
            } elseif ($GLOBALS['XML_RPC_Types'][$type] == 2) {
1580
                $this->addArray($val);
1581
            } elseif ($GLOBALS['XML_RPC_Types'][$type] == 3) {
1582
                $this->addStruct($val);
1583
            }
1584
        }
1585
    }
1586

    
1587
    /**
1588
     * @return int  returns 1 if successful or 0 if there are problems
1589
     */
1590
    function addScalar($val, $type = 'string')
1591
    {
1592
        if ($this->mytype == 1) {
1593
            $this->raiseError(gettext('Scalar can have only one value'),
1594
                              XML_RPC_ERROR_INVALID_TYPE);
1595
            return 0;
1596
        }
1597
        $typeof = $GLOBALS['XML_RPC_Types'][$type];
1598
        if ($typeof != 1) {
1599
            $this->raiseError(
1600
                sprintf(gettext("Not a scalar type (%s)"), $typeof),
1601
                XML_RPC_ERROR_INVALID_TYPE);
1602
            return 0;
1603
        }
1604

    
1605
        if ($type == $GLOBALS['XML_RPC_Boolean']) {
1606
            if (strcasecmp($val, 'true') == 0
1607
                || $val == 1
1608
                || ($val == true && strcasecmp($val, 'false')))
1609
            {
1610
                $val = 1;
1611
            } else {
1612
                $val = 0;
1613
            }
1614
        }
1615

    
1616
        if ($this->mytype == 2) {
1617
            // we're adding to an array here
1618
            $ar = $this->me['array'];
1619
            $ar[] = new XML_RPC_Value($val, $type);
1620
            $this->me['array'] = $ar;
1621
        } else {
1622
            // a scalar, so set the value and remember we're scalar
1623
            $this->me[$type] = $val;
1624
            $this->mytype = $typeof;
1625
        }
1626
        return 1;
1627
    }
1628

    
1629
    /**
1630
     * @return int  returns 1 if successful or 0 if there are problems
1631
     */
1632
    function addArray($vals)
1633
    {
1634
        if ($this->mytype != 0) {
1635
            $this->raiseError(
1636
                    sprintf(gettext('Already initialized as a [%s]'), $this->kindOf()),
1637
                    XML_RPC_ERROR_ALREADY_INITIALIZED);
1638
            return 0;
1639
        }
1640
        $this->mytype = $GLOBALS['XML_RPC_Types']['array'];
1641
        $this->me['array'] = $vals;
1642
        return 1;
1643
    }
1644

    
1645
    /**
1646
     * @return int  returns 1 if successful or 0 if there are problems
1647
     */
1648
    function addStruct($vals)
1649
    {
1650
        if ($this->mytype != 0) {
1651
            $this->raiseError(
1652
                    sprintf(gettext('Already initialized as a [%s]'), $this->kindOf()),
1653
                    XML_RPC_ERROR_ALREADY_INITIALIZED);
1654
            return 0;
1655
        }
1656
        $this->mytype = $GLOBALS['XML_RPC_Types']['struct'];
1657
        $this->me['struct'] = $vals;
1658
        return 1;
1659
    }
1660

    
1661
    /**
1662
     * @return void
1663
     */
1664
    function dump($ar)
1665
    {
1666
        reset($ar);
1667
        foreach ($ar as $key => $val) {
1668
            echo "$key => $val<br />";
1669
            if ($key == 'array') {
1670
                foreach ($val as $key2 => $val2) {
1671
                    echo "-- $key2 => $val2<br />";
1672
                }
1673
            }
1674
        }
1675
    }
1676

    
1677
    /**
1678
     * @return string  the data type of the current value
1679
     */
1680
    function kindOf()
1681
    {
1682
        switch ($this->mytype) {
1683
        case 3:
1684
            return 'struct';
1685

    
1686
        case 2:
1687
            return 'array';
1688

    
1689
        case 1:
1690
            return 'scalar';
1691

    
1692
        default:
1693
            return 'undef';
1694
        }
1695
    }
1696

    
1697
    /**
1698
     * @return string  the data in XML format
1699
     */
1700
    function serializedata($typ, $val)
1701
    {
1702
        $rs = '';
1703
        if (!array_key_exists($typ, $GLOBALS['XML_RPC_Types'])) {
1704
            // XXX
1705
            // need some way to report this error
1706
            return;
1707
        }
1708
        switch ($GLOBALS['XML_RPC_Types'][$typ]) {
1709
        case 3:
1710
            // struct
1711
            $rs .= "<struct>\n";
1712
            reset($val);
1713
            foreach ($val as $key2 => $val2) {
1714
                $rs .= "<member><name>" . htmlspecialchars($key2) . "</name>\n";
1715
                $rs .= $this->serializeval($val2);
1716
                $rs .= "</member>\n";
1717
            }
1718
            $rs .= '</struct>';
1719
            break;
1720

    
1721
        case 2:
1722
            // array
1723
            $rs .= "<array>\n<data>\n";
1724
            foreach ($val as $value) {
1725
                $rs .= $this->serializeval($value);
1726
            }
1727
            $rs .= "</data>\n</array>";
1728
            break;
1729

    
1730
        case 1:
1731
            switch ($typ) {
1732
            case $GLOBALS['XML_RPC_Base64']:
1733
                $rs .= "<${typ}>" . base64_encode($val) . "</${typ}>";
1734
                break;
1735
            case $GLOBALS['XML_RPC_Boolean']:
1736
                $rs .= "<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
1737
                break;
1738
            case $GLOBALS['XML_RPC_String']:
1739
                $rs .= "<${typ}>" . htmlspecialchars($val). "</${typ}>";
1740
                break;
1741
            default:
1742
                $rs .= "<${typ}>${val}</${typ}>";
1743
            }
1744
        }
1745
        return $rs;
1746
    }
1747

    
1748
    /**
1749
     * @return string  the data in XML format
1750
     */
1751
    function serialize()
1752
    {
1753
        return $this->serializeval($this);
1754
    }
1755

    
1756
    /**
1757
     * @return string  the data in XML format
1758
     */
1759
    function serializeval($o)
1760
    {
1761
        if (!is_object($o) || empty($o->me) || !is_array($o->me)) {
1762
            return '';
1763
        }
1764
        $ar = $o->me;
1765
        reset($ar);
1766
        list($typ, $val) = each($ar);
1767
        return '<value>' .  $this->serializedata($typ, $val) .  "</value>\n";
1768
    }
1769

    
1770
    /**
1771
     * @return mixed  the contents of the element requested
1772
     */
1773
    function structmem($m)
1774
    {
1775
        return $this->me['struct'][$m];
1776
    }
1777

    
1778
    /**
1779
     * @return void
1780
     */
1781
    function structreset()
1782
    {
1783
        reset($this->me['struct']);
1784
    }
1785

    
1786
    /**
1787
     * @return  the key/value pair of the struct's current element
1788
     */
1789
    function structeach()
1790
    {
1791
        return each($this->me['struct']);
1792
    }
1793

    
1794
    /**
1795
     * @return mixed  the current value
1796
     */
1797
    function getval()
1798
    {
1799
        // UNSTABLE
1800

    
1801
        reset($this->me);
1802
        $b = current($this->me);
1803

    
1804
        // contributed by I Sofer, 2001-03-24
1805
        // add support for nested arrays to scalarval
1806
        // i've created a new method here, so as to
1807
        // preserve back compatibility
1808

    
1809
        if (is_array($b)) {
1810
            foreach ($b as $id => $cont) {
1811
                $b[$id] = $cont->scalarval();
1812
            }
1813
        }
1814

    
1815
        // add support for structures directly encoding php objects
1816
        if (is_object($b)) {
1817
            $t = get_object_vars($b);
1818
            foreach ($t as $id => $cont) {
1819
                $t[$id] = $cont->scalarval();
1820
            }
1821
            foreach ($t as $id => $cont) {
1822
                $b->$id = $cont;
1823
            }
1824
        }
1825

    
1826
        // end contrib
1827
        return $b;
1828
    }
1829

    
1830
    /**
1831
     * @return mixed  the current element's scalar value.  If the value is
1832
     *                 not scalar, FALSE is returned.
1833
     */
1834
    function scalarval()
1835
    {
1836
        reset($this->me);
1837
        $v = current($this->me);
1838
        if (!is_scalar($v)) {
1839
            $v = false;
1840
        }
1841
        return $v;
1842
    }
1843

    
1844
    /**
1845
     * @return string
1846
     */
1847
    function scalartyp()
1848
    {
1849
        reset($this->me);
1850
        $a = key($this->me);
1851
        if ($a == $GLOBALS['XML_RPC_I4']) {
1852
            $a = $GLOBALS['XML_RPC_Int'];
1853
        }
1854
        return $a;
1855
    }
1856

    
1857
    /**
1858
     * @return mixed  the struct's current element
1859
     */
1860
    function arraymem($m)
1861
    {
1862
        return $this->me['array'][$m];
1863
    }
1864

    
1865
    /**
1866
     * @return int  the number of elements in the array
1867
     */
1868
    function arraysize()
1869
    {
1870
        reset($this->me);
1871
        list($a, $b) = each($this->me);
1872
        return sizeof($b);
1873
    }
1874

    
1875
    /**
1876
     * Determines if the item submitted is an XML_RPC_Value object
1877
     *
1878
     * @param mixed $val  the variable to be evaluated
1879
     *
1880
     * @return bool  TRUE if the item is an XML_RPC_Value object
1881
     *
1882
     * @static
1883
     * @since Method available since Release 1.3.0
1884
     */
1885
    function isValue($val)
1886
    {
1887
        return (strtolower(get_class($val)) == 'xml_rpc_value');
1888
    }
1889
}
1890

    
1891
/**
1892
 * Return an ISO8601 encoded string
1893
 *
1894
 * While timezones ought to be supported, the XML-RPC spec says:
1895
 *
1896
 * "Don't assume a timezone. It should be specified by the server in its
1897
 * documentation what assumptions it makes about timezones."
1898
 *
1899
 * This routine always assumes localtime unless $utc is set to 1, in which
1900
 * case UTC is assumed and an adjustment for locale is made when encoding.
1901
 *
1902
 * @return string  the formatted date
1903
 */
1904
function XML_RPC_iso8601_encode($timet, $utc = 0)
1905
{
1906
    if (!$utc) {
1907
        $t = strftime('%Y%m%dT%H:%M:%S', $timet);
1908
    } else {
1909
        if (function_exists('gmstrftime')) {
1910
            // gmstrftime doesn't exist in some versions
1911
            // of PHP
1912
            $t = gmstrftime('%Y%m%dT%H:%M:%S', $timet);
1913
        } else {
1914
            $t = strftime('%Y%m%dT%H:%M:%S', $timet - date('Z'));
1915
        }
1916
    }
1917
    return $t;
1918
}
1919

    
1920
/**
1921
 * Convert a datetime string into a Unix timestamp
1922
 *
1923
 * While timezones ought to be supported, the XML-RPC spec says:
1924
 *
1925
 * "Don't assume a timezone. It should be specified by the server in its
1926
 * documentation what assumptions it makes about timezones."
1927
 *
1928
 * This routine always assumes localtime unless $utc is set to 1, in which
1929
 * case UTC is assumed and an adjustment for locale is made when encoding.
1930
 *
1931
 * @return int  the unix timestamp of the date submitted
1932
 */
1933
function XML_RPC_iso8601_decode($idate, $utc = 0)
1934
{
1935
    $t = 0;
1936
    if (preg_match('@([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})@', $idate, $regs)) {
1937
        if ($utc) {
1938
            $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1939
        } else {
1940
            $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1941
        }
1942
    }
1943
    return $t;
1944
}
1945

    
1946
/**
1947
 * Converts an XML_RPC_Value object into native PHP types
1948
 *
1949
 * @param object $XML_RPC_val  the XML_RPC_Value object to decode
1950
 *
1951
 * @return mixed  the PHP values
1952
 */
1953
function XML_RPC_decode($XML_RPC_val)
1954
{
1955
    $kind = $XML_RPC_val->kindOf();
1956

    
1957
    if ($kind == 'scalar') {
1958
        return $XML_RPC_val->scalarval();
1959

    
1960
    } elseif ($kind == 'array') {
1961
        $size = $XML_RPC_val->arraysize();
1962
        $arr = array();
1963
        for ($i = 0; $i < $size; $i++) {
1964
            $arr[] = XML_RPC_decode($XML_RPC_val->arraymem($i));
1965
        }
1966
        return $arr;
1967

    
1968
    } elseif ($kind == 'struct') {
1969
        $XML_RPC_val->structreset();
1970
        $arr = array();
1971
        while (list($key, $value) = $XML_RPC_val->structeach()) {
1972
            $arr[$key] = XML_RPC_decode($value);
1973
        }
1974
        return $arr;
1975
    }
1976
}
1977

    
1978
/**
1979
 * Converts native PHP types into an XML_RPC_Value object
1980
 *
1981
 * @param mixed $php_val  the PHP value or variable you want encoded
1982
 *
1983
 * @return object  the XML_RPC_Value object
1984
 */
1985
function XML_RPC_encode($php_val)
1986
{
1987
    $type = gettype($php_val);
1988
    $XML_RPC_val = new XML_RPC_Value;
1989

    
1990
    switch ($type) {
1991
    case 'array':
1992
        if (empty($php_val)) {
1993
            $XML_RPC_val->addArray($php_val);
1994
            break;
1995
        }
1996
        $tmp = array_diff(array_keys($php_val), range(0, count($php_val)-1));
1997
        if (empty($tmp)) {
1998
           $arr = array();
1999
           foreach ($php_val as $k => $v) {
2000
               $arr[$k] = XML_RPC_encode($v);
2001
           }
2002
           $XML_RPC_val->addArray($arr);
2003
           break;
2004
        }
2005
        // fall though if it's not an enumerated array
2006

    
2007
    case 'object':
2008
        $arr = array();
2009
        foreach ($php_val as $k => $v) {
2010
            $arr[$k] = XML_RPC_encode($v);
2011
        }
2012
        $XML_RPC_val->addStruct($arr);
2013
        break;
2014

    
2015
    case 'integer':
2016
        $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_Int']);
2017
        break;
2018

    
2019
    case 'double':
2020
        $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_Double']);
2021
        break;
2022

    
2023
    case 'string':
2024
    case 'NULL':
2025
        if (preg_match('@^[0-9]{8}\T{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}$@', $php_val)) {
2026
            $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_DateTime']);
2027
        } elseif ($GLOBALS['XML_RPC_auto_base64']
2028
                  && preg_match("@[^ -~\t\r\n]@", $php_val))
2029
        {
2030
            // Characters other than alpha-numeric, punctuation, SP, TAB,
2031
            // LF and CR break the XML parser, encode value via Base 64.
2032
            $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_Base64']);
2033
        } else {
2034
            $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_String']);
2035
        }
2036
        break;
2037

    
2038
    case 'boolean':
2039
        // Add support for encoding/decoding of booleans, since they
2040
        // are supported in PHP
2041
        // by <G_Giunta_2001-02-29>
2042
        $XML_RPC_val->addScalar($php_val, $GLOBALS['XML_RPC_Boolean']);
2043
        break;
2044

    
2045
    case 'unknown type':
2046
    default:
2047
        $XML_RPC_val = false;
2048
    }
2049
    return $XML_RPC_val;
2050
}
2051

    
2052
/*
2053
 * Local variables:
2054
 * tab-width: 4
2055
 * c-basic-offset: 4
2056
 * c-hanging-comment-ender-p: nil
2057
 * End:
2058
 */
2059

    
2060
?>
(65-65/67)