Project

General

Profile

Download (48.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
/* $Id$ */
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
 * LICENSE: License is granted to use or modify this software
14
 * ("XML-RPC for PHP") for commercial or non-commercial use provided the
15
 * copyright of the author is preserved in any distributed or derivative work.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESSED OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 * @category   Web Services
29
 * @package    XML_RPC
30
 * @author     Edd Dumbill <edd@usefulinc.com>
31
 * @author     Stig Bakken <stig@php.net>
32
 * @author     Martin Jansen <mj@php.net>
33
 * @author     Daniel Convissor <danielc@php.net>
34
 * @copyright  1999-2001 Edd Dumbill
35
 * @version    CVS: $Id$
36
 * @link       http://pear.php.net/package/XML_RPC
37
 */
38

    
39

    
40
if (!function_exists('xml_parser_create')) {
41
    // Win 32 fix. From: "Leo West" <lwest@imaginet.fr>
42
    if ($WINDIR) {
43
        dl('php_xml.dll');
44
    } else {
45
        dl('xml.so');
46
    }
47
}
48

    
49
/**#@+
50
 * Error constants
51
 */
52
define('XML_RPC_ERROR_INVALID_TYPE',        101);
53
define('XML_RPC_ERROR_NON_NUMERIC_FOUND',   102);
54
define('XML_RPC_ERROR_CONNECTION_FAILED',   103);
55
define('XML_RPC_ERROR_ALREADY_INITIALIZED', 104);
56
/**#@-*/
57

    
58

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

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

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

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

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

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

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

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

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

    
113

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

    
130

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

    
143
/**
144
 * Error message strings
145
 * @global array $GLOBALS['XML_RPC_str']
146
 */
147
$GLOBALS['XML_RPC_str'] = array(
148
    'unknown_method'      => 'Unknown method',
149
    'invalid_return'      => 'Invalid return payload: enable debugging to examine incoming payload',
150
    'incorrect_params'    => 'Incorrect parameters passed to method',
151
    'introspect_unknown'  => 'Can\'t introspect: method unknown',
152
    'http_error'          => 'Didn\'t receive 200 OK from remote server.',
153
);
154

    
155

    
156
/**
157
 * Default XML encoding (ISO-8859-1, UTF-8 or US-ASCII)
158
 * @global string $GLOBALS['XML_RPC_defencoding']
159
 */
160
$GLOBALS['XML_RPC_defencoding'] = 'UTF-8';
161

    
162
/**
163
 * User error codes start at 800
164
 * @global int $GLOBALS['XML_RPC_erruser']
165
 */
166
$GLOBALS['XML_RPC_erruser'] = 800;
167

    
168
/**
169
 * XML parse error codes start at 100
170
 * @global int $GLOBALS['XML_RPC_errxml']
171
 */
172
$GLOBALS['XML_RPC_errxml'] = 100;
173

    
174

    
175
/**
176
 * Compose backslashes for escaping regexp
177
 * @global string $GLOBALS['XML_RPC_backslash']
178
 */
179
$GLOBALS['XML_RPC_backslash'] = chr(92) . chr(92);
180

    
181

    
182
/**
183
 * Stores state during parsing
184
 *
185
 * quick explanation of components:
186
 *   + st     = builds up a string for evaluation
187
 *   + ac     = accumulates values
188
 *   + qt     = decides if quotes are needed for evaluation
189
 *   + cm     = denotes struct or array (comma needed)
190
 *   + isf    = indicates a fault
191
 *   + lv     = indicates "looking for a value": implements the logic
192
 *               to allow values with no types to be strings
193
 *   + params = stores parameters in method calls
194
 *   + method = stores method name
195
 *
196
 * @global array $GLOBALS['XML_RPC_xh']
197
 */
198
$GLOBALS['XML_RPC_xh'] = array();
199

    
200

    
201
/**
202
 * Start element handler for the XML parser
203
 *
204
 * @return void
205
 */
206
function XML_RPC_se($parser_resource, $name, $attrs)
207
{
208
    global $XML_RPC_xh, $XML_RPC_DateTime, $XML_RPC_String;
209
    $parser = (int) $parser_resource;
210

    
211
    switch ($name) {
212
    case 'STRUCT':
213
    case 'ARRAY':
214
        $XML_RPC_xh[$parser]['st'] .= 'array(';
215
        $XML_RPC_xh[$parser]['cm']++;
216
        // this last line turns quoting off
217
        // this means if we get an empty array we'll
218
        // simply get a bit of whitespace in the eval
219
        $XML_RPC_xh[$parser]['qt'] = 0;
220
        break;
221

    
222
    case 'NAME':
223
        $XML_RPC_xh[$parser]['st'] .= "'";
224
        $XML_RPC_xh[$parser]['ac'] = '';
225
        break;
226

    
227
    case 'FAULT':
228
        $XML_RPC_xh[$parser]['isf'] = 1;
229
        break;
230

    
231
    case 'PARAM':
232
        $XML_RPC_xh[$parser]['st'] = '';
233
        break;
234

    
235
    case 'VALUE':
236
        $XML_RPC_xh[$parser]['st'] .= 'new XML_RPC_Value(';
237
        $XML_RPC_xh[$parser]['lv'] = 1;
238
        $XML_RPC_xh[$parser]['vt'] = $XML_RPC_String;
239
        $XML_RPC_xh[$parser]['ac'] = '';
240
        $XML_RPC_xh[$parser]['qt'] = 0;
241
        // look for a value: if this is still 1 by the
242
        // time we reach the first data segment then the type is string
243
        // by implication and we need to add in a quote
244
        break;
245

    
246
    case 'I4':
247
    case 'INT':
248
    case 'STRING':
249
    case 'BOOLEAN':
250
    case 'DOUBLE':
251
    case 'DATETIME.ISO8601':
252
    case 'BASE64':
253
        $XML_RPC_xh[$parser]['ac'] = ''; // reset the accumulator
254

    
255
        if ($name == 'DATETIME.ISO8601' || $name == 'STRING') {
256
            $XML_RPC_xh[$parser]['qt'] = 1;
257

    
258
            if ($name == 'DATETIME.ISO8601') {
259
                $XML_RPC_xh[$parser]['vt'] = $XML_RPC_DateTime;
260
            }
261

    
262
        } elseif ($name == 'BASE64') {
263
            $XML_RPC_xh[$parser]['qt'] = 2;
264
        } else {
265
            // No quoting is required here -- but
266
            // at the end of the element we must check
267
            // for data format errors.
268
            $XML_RPC_xh[$parser]['qt'] = 0;
269
        }
270
        break;
271

    
272
    case 'MEMBER':
273
        $XML_RPC_xh[$parser]['ac'] = '';
274
    }
275

    
276
    if ($name != 'VALUE') {
277
        $XML_RPC_xh[$parser]['lv'] = 0;
278
    }
279
}
280

    
281
/**
282
 * End element handler for the XML parser
283
 *
284
 * @return void
285
 */
286
function XML_RPC_ee($parser_resource, $name)
287
{
288
    global $XML_RPC_xh, $XML_RPC_Types, $XML_RPC_String;
289
    $parser = (int) $parser_resource;
290

    
291
    switch ($name) {
292
    case 'STRUCT':
293
    case 'ARRAY':
294
        if ($XML_RPC_xh[$parser]['cm']
295
            && substr($XML_RPC_xh[$parser]['st'], -1) == ',')
296
        {
297
            $XML_RPC_xh[$parser]['st'] = substr($XML_RPC_xh[$parser]['st'], 0, -1);
298
        }
299

    
300
        $XML_RPC_xh[$parser]['st'] .= ')';
301
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
302
        $XML_RPC_xh[$parser]['cm']--;
303
        break;
304

    
305
    case 'NAME':
306
        $XML_RPC_xh[$parser]['st'] .= $XML_RPC_xh[$parser]['ac'] . "' => ";
307
        break;
308

    
309
    case 'BOOLEAN':
310
        // special case here: we translate boolean 1 or 0 into PHP
311
        // constants true or false
312
        if ($XML_RPC_xh[$parser]['ac'] == '1') {
313
            $XML_RPC_xh[$parser]['ac'] = 'true';
314
        } else {
315
            $XML_RPC_xh[$parser]['ac'] = 'false';
316
        }
317

    
318
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
319
        // Drop through intentionally.
320

    
321
    case 'I4':
322
    case 'INT':
323
    case 'STRING':
324
    case 'DOUBLE':
325
    case 'DATETIME.ISO8601':
326
    case 'BASE64':
327
        if ($XML_RPC_xh[$parser]['qt'] == 1) {
328
            // we use double quotes rather than single so backslashification works OK
329
            $XML_RPC_xh[$parser]['st'] .= '"' . $XML_RPC_xh[$parser]['ac'] . '"';
330
        } elseif ($XML_RPC_xh[$parser]['qt'] == 2) {
331
            $XML_RPC_xh[$parser]['st'] .= "base64_decode('"
332
                                        . $XML_RPC_xh[$parser]['ac'] . "')";
333
        } elseif ($name == 'BOOLEAN') {
334
            $XML_RPC_xh[$parser]['st'] .= $XML_RPC_xh[$parser]['ac'];
335
        } else {
336
            // we have an I4, INT or a DOUBLE
337
            // we must check that only 0123456789-.<space> are characters here
338
            if (!ereg("^[+-]?[0123456789 \t\.]+$", $XML_RPC_xh[$parser]['ac'])) {
339
                XML_RPC_Base::raiseError('Non-numeric value received in INT or DOUBLE',
340
                                         XML_RPC_ERROR_NON_NUMERIC_FOUND);
341
                $XML_RPC_xh[$parser]['st'] .= 'XML_RPC_ERROR_NON_NUMERIC_FOUND';
342
            } else {
343
                // it's ok, add it on
344
                $XML_RPC_xh[$parser]['st'] .= $XML_RPC_xh[$parser]['ac'];
345
            }
346
        }
347

    
348
        $XML_RPC_xh[$parser]['ac'] = '';
349
        $XML_RPC_xh[$parser]['qt'] = 0;
350
        $XML_RPC_xh[$parser]['lv'] = 3; // indicate we've found a value
351
        break;
352

    
353
    case 'VALUE':
354
        // deal with a string value
355
        if (strlen($XML_RPC_xh[$parser]['ac']) > 0 &&
356
            $XML_RPC_xh[$parser]['vt'] == $XML_RPC_String) {
357

    
358
            $XML_RPC_xh[$parser]['st'] .= '"' . $XML_RPC_xh[$parser]['ac'] . '"';
359
        }
360

    
361
        // This if () detects if no scalar was inside <VALUE></VALUE>
362
        // and pads an empty "".
363
        if ($XML_RPC_xh[$parser]['st'][strlen($XML_RPC_xh[$parser]['st'])-1] == '(') {
364
            $XML_RPC_xh[$parser]['st'] .= '""';
365
        }
366
        $XML_RPC_xh[$parser]['st'] .= ", '" . $XML_RPC_xh[$parser]['vt'] . "')";
367
        if ($XML_RPC_xh[$parser]['cm']) {
368
            $XML_RPC_xh[$parser]['st'] .= ',';
369
        }
370
        break;
371

    
372
    case 'MEMBER':
373
        $XML_RPC_xh[$parser]['ac'] = '';
374
        $XML_RPC_xh[$parser]['qt'] = 0;
375
        break;
376

    
377
    case 'DATA':
378
        $XML_RPC_xh[$parser]['ac'] = '';
379
        $XML_RPC_xh[$parser]['qt'] = 0;
380
        break;
381

    
382
    case 'PARAM':
383
        $XML_RPC_xh[$parser]['params'][] = $XML_RPC_xh[$parser]['st'];
384
        break;
385

    
386
    case 'METHODNAME':
387
        $XML_RPC_xh[$parser]['method'] = ereg_replace("^[\n\r\t ]+", '',
388
                                                      $XML_RPC_xh[$parser]['ac']);
389
        break;
390

    
391
    case 'BOOLEAN':
392
        // special case here: we translate boolean 1 or 0 into PHP
393
        // constants true or false
394
        if ($XML_RPC_xh[$parser]['ac'] == '1') {
395
            $XML_RPC_xh[$parser]['ac'] = 'true';
396
        } else {
397
            $XML_RPC_xh[$parser]['ac'] = 'false';
398
        }
399

    
400
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
401
    }
402

    
403
    // if it's a valid type name, set the type
404
    if (isset($XML_RPC_Types[strtolower($name)])) {
405
        $XML_RPC_xh[$parser]['vt'] = strtolower($name);
406
    }
407
}
408

    
409
/**
410
 * Character data handler for the XML parser
411
 *
412
 * @return void
413
 */
414
function XML_RPC_cd($parser_resource, $data)
415
{
416
    global $XML_RPC_xh, $XML_RPC_backslash;
417
    $parser = (int) $parser_resource;
418

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

    
423
        if ($XML_RPC_xh[$parser]['lv'] == 1) {
424
            // if we've found text and we're just in a <value> then
425
            // turn quoting on, as this will be a string
426
            $XML_RPC_xh[$parser]['qt'] = 1;
427
            // and say we've found a value
428
            $XML_RPC_xh[$parser]['lv'] = 2;
429
        }
430

    
431
        // replace characters that eval would
432
        // do special things with
433
        if (!isset($XML_RPC_xh[$parser]['ac'])) {
434
            $XML_RPC_xh[$parser]['ac'] = '';
435
        }
436
        $XML_RPC_xh[$parser]['ac'] .= str_replace('$', '\$',
437
            str_replace('"', '\"', str_replace(chr(92),
438
            $XML_RPC_backslash, $data)));
439
    }
440
}
441

    
442
/**
443
 * Base class
444
 *
445
 * This class provides common functions for all of the XML_RPC classes.
446
 *
447
 * @category   Web Services
448
 * @package    XML_RPC
449
 * @author     Edd Dumbill <edd@usefulinc.com>
450
 * @author     Stig Bakken <stig@php.net>
451
 * @author     Martin Jansen <mj@php.net>
452
 * @copyright  1999-2001 Edd Dumbill
453
 * @version    Release: @package_version@
454
 * @link       http://pear.php.net/package/XML_RPC
455
 */
456
class XML_RPC_Base {
457

    
458
    /**
459
     * PEAR Error handling
460
     *
461
     * @return object  PEAR_Error object
462
     */
463
    function raiseError($msg, $code)
464
    {
465
        include_once 'PEAR.php';
466
        if (is_object(@$this)) {
467
            return PEAR::raiseError(get_class($this) . ': ' . $msg, $code);
468
        } else {
469
            return PEAR::raiseError('XML_RPC: ' . $msg, $code);
470
        }
471
    }
472

    
473
    /**
474
     * Tell whether something is a PEAR_Error object
475
     *
476
     * @param mixed $value  the item to check
477
     *
478
     * @return bool  whether $value is a PEAR_Error object or not
479
     *
480
     * @access public
481
     */
482
    function isError($value)
483
    {
484
        return is_a($value, 'PEAR_Error');
485
    }
486
}
487

    
488
/**
489
 *
490
 *
491
 * @category   Web Services
492
 * @package    XML_RPC
493
 * @author     Edd Dumbill <edd@usefulinc.com>
494
 * @author     Stig Bakken <stig@php.net>
495
 * @author     Martin Jansen <mj@php.net>
496
 * @author     Daniel Convissor <danielc@php.net>
497
 * @copyright  1999-2001 Edd Dumbill
498
 * @version    Release: @package_version@
499
 * @link       http://pear.php.net/package/XML_RPC
500
 */
501
class XML_RPC_Client extends XML_RPC_Base {
502

    
503
    /**
504
     * The path and name of the RPC server script you want the request to go to
505
     * @var string
506
     */
507
    var $path = '';
508

    
509
    /**
510
     * The name of the remote server to connect to
511
     * @var string
512
     */
513
    var $server = '';
514

    
515
    /**
516
     * The protocol to use in contacting the remote server
517
     * @var string
518
     */
519
    var $protocol = 'http://';
520

    
521
    /**
522
     * The port for connecting to the remote server
523
     *
524
     * The default is 80 for http:// connections
525
     * and 443 for https:// and ssl:// connections.
526
     *
527
     * @var integer
528
     */
529
    var $port = 80;
530

    
531
    /**
532
     * A user name for accessing the RPC server
533
     * @var string
534
     * @see XML_RPC_Client::setCredentials()
535
     */
536
    var $username = '';
537

    
538
    /**
539
     * A password for accessing the RPC server
540
     * @var string
541
     * @see XML_RPC_Client::setCredentials()
542
     */
543
    var $password = '';
544

    
545
    /**
546
     * The name of the proxy server to use, if any
547
     * @var string
548
     */
549
    var $proxy = '';
550

    
551
    /**
552
     * The protocol to use in contacting the proxy server, if any
553
     * @var string
554
     */
555
    var $proxy_protocol = 'http://';
556

    
557
    /**
558
     * The port for connecting to the proxy server
559
     *
560
     * The default is 8080 for http:// connections
561
     * and 443 for https:// and ssl:// connections.
562
     *
563
     * @var integer
564
     */
565
    var $proxy_port = 8080;
566

    
567
    /**
568
     * A user name for accessing the proxy server
569
     * @var string
570
     */
571
    var $proxy_user = '';
572

    
573
    /**
574
     * A password for accessing the proxy server
575
     * @var string
576
     */
577
    var $proxy_pass = '';
578

    
579
    /**
580
     * The error number, if any
581
     * @var integer
582
     */
583
    var $errno = 0;
584

    
585
    /**
586
     * The error message, if any
587
     * @var string
588
     */
589
    var $errstring = '';
590

    
591
    /**
592
     * The current debug mode (1 = on, 0 = off)
593
     * @var integer
594
     */
595
    var $debug = 0;
596

    
597

    
598
    /**
599
     * Sets the object's properties
600
     *
601
     * @param string  $path        the path and name of the RPC server script
602
     *                              you want the request to go to
603
     * @param string  $server      the URL of the remote server to connect to.
604
     *                              If this parameter doesn't specify a
605
     *                              protocol and $port is 443, ssl:// is
606
     *                              assumed.
607
     * @param integer $port        a port for connecting to the remote server.
608
     *                              Defaults to 80 for http:// connections and
609
     *                              443 for https:// and ssl:// connections.
610
     * @param string  $proxy       the URL of the proxy server to use, if any.
611
     *                              If this parameter doesn't specify a
612
     *                              protocol and $port is 443, ssl:// is
613
     *                              assumed.
614
     * @param integer $proxy_port  a port for connecting to the remote server.
615
     *                              Defaults to 8080 for http:// connections and
616
     *                              443 for https:// and ssl:// connections.
617
     * @param string  $proxy_user  a user name for accessing the proxy server
618
     * @param string  $proxy_pass  a password for accessing the proxy server
619
     *
620
     * @return void
621
     */
622
    function XML_RPC_Client($path, $server, $port = 0,
623
                            $proxy = '', $proxy_port = 0,
624
                            $proxy_user = '', $proxy_pass = '')
625
    {
626
        $this->path       = $path;
627
        $this->proxy_user = $proxy_user;
628
        $this->proxy_pass = $proxy_pass;
629

    
630
        preg_match('@^(http://|https://|ssl://)?(.*)$@', $server, $match);
631
        if ($match[1] == '') {
632
            if ($port == 443) {
633
                $this->server   = $match[2];
634
                $this->protocol = 'ssl://';
635
                $this->port     = 443;
636
            } else {
637
                $this->server = $match[2];
638
                if ($port) {
639
                    $this->port = $port;
640
                }
641
            }
642
        } elseif ($match[1] == 'http://') {
643
            $this->server = $match[2];
644
            if ($port) {
645
                $this->port = $port;
646
            }
647
        } else {
648
            $this->server   = $match[2];
649
            $this->protocol = 'ssl://';
650
            if ($port) {
651
                $this->port = $port;
652
            } else {
653
                $this->port = 443;
654
            }
655
        }
656

    
657
        if ($proxy) {
658
            preg_match('@^(http://|https://|ssl://)?(.*)$@', $proxy, $match);
659
            if ($match[1] == '') {
660
                if ($proxy_port == 443) {
661
                    $this->proxy          = $match[2];
662
                    $this->proxy_protocol = 'ssl://';
663
                    $this->proxy_port     = 443;
664
                } else {
665
                    $this->proxy = $match[2];
666
                    if ($proxy_port) {
667
                        $this->proxy_port = $proxy_port;
668
                    }
669
                }
670
            } elseif ($match[1] == 'http://') {
671
                $this->proxy = $match[2];
672
                if ($proxy_port) {
673
                    $this->proxy_port = $proxy_port;
674
                }
675
            } else {
676
                $this->proxy          = $match[2];
677
                $this->proxy_protocol = 'ssl://';
678
                if ($proxy_port) {
679
                    $this->proxy_port = $proxy_port;
680
                } else {
681
                    $this->proxy_port = 443;
682
                }
683
            }
684
        }
685
    }
686

    
687
    /**
688
     * Change the current debug mode
689
     *
690
     * @param int $in  where 1 = on, 0 = off
691
     *
692
     * @return void
693
     */
694
    function setDebug($in)
695
    {
696
        if ($in) {
697
            $this->debug = 1;
698
        } else {
699
            $this->debug = 0;
700
        }
701
    }
702

    
703
    /**
704
     * Set username and password properties for connecting to the RPC server
705
     *
706
     * @param string $u  the user name
707
     * @param string $p  the password
708
     *
709
     * @return void
710
     *
711
     * @see XML_RPC_Client::$username, XML_RPC_Client::$password
712
     */
713
    function setCredentials($u, $p)
714
    {
715
        $this->username = $u;
716
        $this->password = $p;
717
    }
718

    
719
    /**
720
     * Transmit the RPC request via HTTP 1.0 protocol
721
     *
722
     * @param object $msg       the XML_RPC_Message object
723
     * @param int    $timeout   how many seconds to wait for the request
724
     *
725
     * @return object  an XML_RPC_Response object.  0 is returned if any
726
     *                  problems happen.
727
     *
728
     * @see XML_RPC_Message, XML_RPC_Client::XML_RPC_Client(),
729
     *      XML_RPC_Client::setCredentials()
730
     */
731
    function send($msg, $timeout = 0)
732
    {
733
        $msg->debug = $this->debug;
734
        return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
735
                                        $timeout, $this->username,
736
                                        $this->password);
737
    }
738

    
739
    /**
740
     * Transmit the RPC request via HTTP 1.0 protocol
741
     *
742
     * Requests should be sent using XML_RPC_Client send() rather than
743
     * calling this method directly.
744
     *
745
     * @param object $msg       the XML_RPC_Message object
746
     * @param string $server    the server to send the request to
747
     * @param int    $port      the server port send the request to
748
     * @param int    $timeout   how many seconds to wait for the request
749
     *                           before giving up
750
     * @param string $username  a user name for accessing the RPC server
751
     * @param string $password  a password for accessing the RPC server
752
     *
753
     * @return object  an XML_RPC_Response object.  0 is returned if any
754
     *                  problems happen.
755
     *
756
     * @see XML_RPC_Client::send()
757
     */
758
    function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
759
                               $username = '', $password = '')
760
    {
761
        /*
762
         * If we're using a proxy open a socket to the proxy server
763
         * instead to the xml-rpc server
764
         */
765
        if ($this->proxy) {
766
            if ($this->proxy_protocol == 'http://') {
767
                $protocol = '';
768
            } else {
769
                $protocol = $this->proxy_protocol;
770
            }
771
            if ($timeout > 0) {
772
                $fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
773
                                 $this->errno, $this->errstr, $timeout);
774
            } else {
775
                $fp = @fsockopen($protocol . $this->proxy, $this->proxy_port,
776
                                 $this->errno, $this->errstr);
777
            }
778
        } else {
779
            if ($this->protocol == 'http://') {
780
                $protocol = '';
781
            } else {
782
                $protocol = $this->protocol;
783
            }
784
            if ($timeout > 0) {
785
                $fp = @fsockopen($protocol . $server, $port,
786
                                 $this->errno, $this->errstr, $timeout);
787
            } else {
788
                $fp = @fsockopen($protocol . $server, $port,
789
                                 $this->errno, $this->errstr);
790
            }
791
        }
792

    
793
        /*
794
         * Just raising the error without returning it is strange,
795
         * but keep it here for backwards compatibility.
796
         */
797
        if (!$fp && $this->proxy) {
798
            $this->raiseError('Connection to proxy server '
799
                              . $this->proxy . ':' . $this->proxy_port
800
                              . ' failed. ' . $this->errstr,
801
                              XML_RPC_ERROR_CONNECTION_FAILED);
802
            return 0;
803
        } elseif (!$fp) {
804
            $this->raiseError('Connection to RPC server '
805
                              . $server . ':' . $port
806
                              . ' failed. ' . $this->errstr,
807
                              XML_RPC_ERROR_CONNECTION_FAILED);
808
            return 0;
809
        }
810

    
811
        // Only create the payload if it was not created previously
812
        if (empty($msg->payload)) {
813
            $msg->createPayload();
814
        }
815

    
816
        // thanks to Grant Rauscher <grant7@firstworld.net> for this
817
        $credentials = '';
818
        if ($username != '') {
819
            $credentials = 'Authorization: Basic ' .
820
                base64_encode($username . ':' . $password) . "\r\n";
821
        }
822

    
823
        if ($this->proxy) {
824
            $op = 'POST ' . $this->protocol . $server;
825
            if ($this->proxy_port) {
826
                $op .= ':' . $this->port;
827
            }
828
        } else {
829
           $op = 'POST ';
830
        }
831

    
832
        $op .= $this->path. " HTTP/1.0\r\n" .
833
               "User-Agent: PEAR XML_RPC\r\n" .
834
               'Host: ' . $server . "\r\n";
835
        if ($this->proxy && $this->proxy_user != '') {
836
            $op .= 'Proxy-Authorization: Basic ' .
837
                base64_encode($this->proxy_user . ':' . $this->proxy_pass) .
838
                "\r\n";
839
        }
840
        $op .= $credentials .
841
               "Content-Type: text/xml\r\n" .
842
               'Content-Length: ' . strlen($msg->payload) . "\r\n\r\n" .
843
               $msg->payload;
844

    
845
        if (!fputs($fp, $op, strlen($op))) {
846
            $this->errstr = 'Write error';
847
            return 0;
848
        }
849
        $resp = $msg->parseResponseFile($fp);
850
        fclose($fp);
851
        return $resp;
852
    }
853
}
854

    
855
/**
856
 *
857
 *
858
 * @category   Web Services
859
 * @package    XML_RPC
860
 * @author     Edd Dumbill <edd@usefulinc.com>
861
 * @author     Stig Bakken <stig@php.net>
862
 * @author     Martin Jansen <mj@php.net>
863
 * @copyright  1999-2001 Edd Dumbill
864
 * @version    Release: @package_version@
865
 * @link       http://pear.php.net/package/XML_RPC
866
 */
867
class XML_RPC_Response extends XML_RPC_Base
868
{
869
    var $xv;
870
    var $fn;
871
    var $fs;
872
    var $hdrs;
873

    
874
    /**
875
     * @return void
876
     */
877
    function XML_RPC_Response($val, $fcode = 0, $fstr = '')
878
    {
879
        if ($fcode != 0) {
880
            $this->fn = $fcode;
881
            $this->fs = htmlspecialchars($fstr);
882
        } else {
883
            $this->xv = $val;
884
        }
885
    }
886

    
887
    /**
888
     * @return int  the error code
889
     */
890
    function faultCode()
891
    {
892
        if (isset($this->fn)) {
893
            return $this->fn;
894
        } else {
895
            return 0;
896
        }
897
    }
898

    
899
    /**
900
     * @return string  the error string
901
     */
902
    function faultString()
903
    {
904
        return $this->fs;
905
    }
906

    
907
    /**
908
     * @return mixed  the value
909
     */
910
    function value()
911
    {
912
        return $this->xv;
913
    }
914

    
915
    /**
916
     * @return string  the error message in XML format
917
     */
918
    function serialize()
919
    {
920
        $rs = "<methodResponse>\n";
921
        if ($this->fn) {
922
            $rs .= "<fault>
923
  <value>
924
    <struct>
925
      <member>
926
        <name>faultCode</name>
927
        <value><int>" . $this->fn . "</int></value>
928
      </member>
929
      <member>
930
        <name>faultString</name>
931
        <value><string>" . $this->fs . "</string></value>
932
      </member>
933
    </struct>
934
  </value>
935
</fault>";
936
        } else {
937
            $rs .= "<params>\n<param>\n" . $this->xv->serialize() .
938
        "</param>\n</params>";
939
        }
940
        $rs .= "\n</methodResponse>";
941
        return $rs;
942
    }
943
}
944

    
945
/**
946
 *
947
 *
948
 * @category   Web Services
949
 * @package    XML_RPC
950
 * @author     Edd Dumbill <edd@usefulinc.com>
951
 * @author     Stig Bakken <stig@php.net>
952
 * @author     Martin Jansen <mj@php.net>
953
 * @author     Daniel Convissor <danielc@php.net>
954
 * @copyright  1999-2001 Edd Dumbill
955
 * @version    Release: @package_version@
956
 * @link       http://pear.php.net/package/XML_RPC
957
 */
958
class XML_RPC_Message extends XML_RPC_Base
959
{
960
    /**
961
     * The current debug mode (1 = on, 0 = off)
962
     * @var integer
963
     */
964
    var $debug = 0;
965

    
966
    /**
967
     * The encoding to be used for outgoing messages
968
     *
969
     * Defaults to the value of <var>$GLOBALS['XML_RPC_defencoding']</var>
970
     *
971
     * @var string
972
     * @see XML_RPC_Message::setSendEncoding(),
973
     *      $GLOBALS['XML_RPC_defencoding'], XML_RPC_Message::xml_header()
974
     */
975
    var $send_encoding = '';
976

    
977
    /**
978
     * The method presently being evaluated
979
     * @var string
980
     */
981
    var $methodname = '';
982

    
983
    /**
984
     * @var array
985
     */
986
    var $params = array();
987

    
988
    /**
989
     * The XML message being generated
990
     * @var string
991
     */
992
    var $payload = '';
993

    
994
    /**
995
     * @return void
996
     */
997
    function XML_RPC_Message($meth, $pars = 0)
998
    {
999
        $this->methodname = $meth;
1000
        if (is_array($pars) && sizeof($pars) > 0) {
1001
            for ($i = 0; $i < sizeof($pars); $i++) {
1002
                $this->addParam($pars[$i]);
1003
            }
1004
        }
1005
    }
1006

    
1007
    /**
1008
     * Produces the XML declaration including the encoding attribute
1009
     *
1010
     * The encoding is determined by this class' <var>$send_encoding</var>
1011
     * property.  If the <var>$send_encoding</var> property is not set, use
1012
     * <var>$GLOBALS['XML_RPC_defencoding']</var>.
1013
     *
1014
     * @return string  the XML declaration and <methodCall> element
1015
     *
1016
     * @see XML_RPC_Message::setSendEncoding(),
1017
     *      XML_RPC_Message::$send_encoding, $GLOBALS['XML_RPC_defencoding']
1018
     */
1019
    function xml_header()
1020
    {
1021
        global $XML_RPC_defencoding;
1022
        if (!$this->send_encoding) {
1023
            $this->send_encoding = $XML_RPC_defencoding;
1024
        }
1025
        return '<?xml version="1.0" encoding="' . $this->send_encoding . '"?>'
1026
               . "\n<methodCall>\n";
1027
    }
1028

    
1029
    /**
1030
     * @return string  the closing </methodCall> tag
1031
     */
1032
    function xml_footer()
1033
    {
1034
        return "</methodCall>\n";
1035
    }
1036

    
1037
    /**
1038
     * @return void
1039
     *
1040
     * @uses XML_RPC_Message::xml_header(), XML_RPC_Message::xml_footer()
1041
     */
1042
    function createPayload()
1043
    {
1044
        $this->payload = $this->xml_header();
1045
        $this->payload .= '<methodName>' . $this->methodname . "</methodName>\n";
1046
        $this->payload .= "<params>\n";
1047
        for ($i = 0; $i < sizeof($this->params); $i++) {
1048
            $p = $this->params[$i];
1049
            $this->payload .= "<param>\n" . $p->serialize() . "</param>\n";
1050
        }
1051
        $this->payload .= "</params>\n";
1052
        $this->payload .= $this->xml_footer();
1053
        $this->payload = ereg_replace("[\r\n]+", "\r\n", $this->payload);
1054
    }
1055

    
1056
    /**
1057
     * @return string  the name of the method
1058
     */
1059
    function method($meth = '')
1060
    {
1061
        if ($meth != '') {
1062
            $this->methodname = $meth;
1063
        }
1064
        return $this->methodname;
1065
    }
1066

    
1067
    /**
1068
     * @return string  the payload
1069
     */
1070
    function serialize()
1071
    {
1072
        $this->createPayload();
1073
        return $this->payload;
1074
    }
1075

    
1076
    /**
1077
     * @return void
1078
     */
1079
    function addParam($par)
1080
    {
1081
        $this->params[] = $par;
1082
    }
1083

    
1084
    /**
1085
     * @return void
1086
     */
1087
    function getParam($i)
1088
    {
1089
        return $this->params[$i];
1090
    }
1091

    
1092
    /**
1093
     * @return int  the number of parameters
1094
     */
1095
    function getNumParams()
1096
    {
1097
        return sizeof($this->params);
1098
    }
1099

    
1100
    /**
1101
     * Sets the XML declaration's encoding attribute
1102
     *
1103
     * @param string $type  the encoding type (ISO-8859-1, UTF-8 or US-ASCII)
1104
     *
1105
     * @return void
1106
     *
1107
     * @see XML_RPC_Message::$send_encoding, XML_RPC_Message::xml_header()
1108
     * @since Method available since Release 1.2.0
1109
     */
1110
    function setSendEncoding($type)
1111
    {
1112
        $this->send_encoding = $type;
1113
    }
1114

    
1115
    /**
1116
     * Determine the XML's encoding via the encoding attribute
1117
     * in the XML declaration
1118
     *
1119
     * If the encoding parameter is not set or is not ISO-8859-1, UTF-8
1120
     * or US-ASCII, $XML_RPC_defencoding will be returned.
1121
     *
1122
     * @param string $data  the XML that will be parsed
1123
     *
1124
     * @return string  the encoding to be used
1125
     *
1126
     * @link   http://php.net/xml_parser_create
1127
     * @since  Method available since Release 1.2.0
1128
     */
1129
    function getEncoding($data)
1130
    {
1131
        global $XML_RPC_defencoding;
1132

    
1133
        if (preg_match('/<\?xml[^>]*\s*encoding\s*=\s*[\'"]([^"\']*)[\'"]/i',
1134
                       $data, $match))
1135
        {
1136
            $match[1] = trim(strtoupper($match[1]));
1137
            switch ($match[1]) {
1138
                case 'ISO-8859-1':
1139
                case 'UTF-8':
1140
                case 'US-ASCII':
1141
                    return $match[1];
1142
                    break;
1143

    
1144
                default:
1145
                    return $XML_RPC_defencoding;
1146
            }
1147
        } else {
1148
            return $XML_RPC_defencoding;
1149
        }
1150
    }
1151

    
1152
    /**
1153
     * @return object  a new XML_RPC_Response object
1154
     */
1155
    function parseResponseFile($fp)
1156
    {
1157
        $ipd = '';
1158
        while ($data = @fread($fp, 8192)) {
1159
            $ipd .= $data;
1160
        }
1161
        return $this->parseResponse($ipd);
1162
    }
1163

    
1164
    /**
1165
     * @return object  a new XML_RPC_Response object
1166
     */
1167
    function parseResponse($data = '')
1168
    {
1169
        global $XML_RPC_xh, $XML_RPC_err, $XML_RPC_str, $XML_RPC_defencoding;
1170

    
1171
        $encoding = $this->getEncoding($data);
1172
        $parser_resource = xml_parser_create($encoding);
1173
        $parser = (int) $parser_resource;
1174

    
1175
        $XML_RPC_xh[$parser] = array();
1176

    
1177
        $XML_RPC_xh[$parser]['st'] = '';
1178
        $XML_RPC_xh[$parser]['cm'] = 0;
1179
        $XML_RPC_xh[$parser]['isf'] = 0;
1180
        $XML_RPC_xh[$parser]['ac'] = '';
1181
        $XML_RPC_xh[$parser]['qt'] = '';
1182

    
1183
        xml_parser_set_option($parser_resource, XML_OPTION_CASE_FOLDING, true);
1184
        xml_set_element_handler($parser_resource, 'XML_RPC_se', 'XML_RPC_ee');
1185
        xml_set_character_data_handler($parser_resource, 'XML_RPC_cd');
1186

    
1187
        $hdrfnd = 0;
1188
        if ($this->debug) {
1189
            print "<PRE>---GOT---\n";
1190
            print isset($_SERVER['SERVER_PROTOCOL']) ? htmlspecialchars($data) : $data;
1191
            print "\n---END---\n</PRE>";
1192
        }
1193

    
1194
        // see if we got an HTTP 200 OK, else bomb
1195
        // but only do this if we're using the HTTP protocol.
1196
        if (ereg('^HTTP', $data) &&
1197
            !ereg('^HTTP/[0-9\.]+ 200 ', $data)) {
1198
                $errstr = substr($data, 0, strpos($data, "\n") - 1);
1199
                error_log('HTTP error, got response: ' . $errstr);
1200
                $r = new XML_RPC_Response(0, $XML_RPC_err['http_error'],
1201
                                          $XML_RPC_str['http_error'] . ' (' .
1202
                                          $errstr . ')');
1203
                xml_parser_free($parser_resource);
1204
                return $r;
1205
        }
1206
        // gotta get rid of headers here
1207

    
1208

    
1209
        if ((!$hdrfnd) && ($brpos = strpos($data,"\r\n\r\n"))) {
1210
            $XML_RPC_xh[$parser]['ha'] = substr($data, 0, $brpos);
1211
            $data = substr($data, $brpos + 4);
1212
            $hdrfnd = 1;
1213
        }
1214

    
1215
        /*
1216
         * be tolerant of junk after methodResponse
1217
         * (e.g. javascript automatically inserted by free hosts)
1218
         * thanks to Luca Mariano <luca.mariano@email.it>
1219
         */
1220
        $data = substr($data, 0, strpos($data, "</methodResponse>") + 17);
1221

    
1222
        if (!xml_parse($parser_resource, $data, sizeof($data))) {
1223
            // thanks to Peter Kocks <peter.kocks@baygate.com>
1224
            if ((xml_get_current_line_number($parser_resource)) == 1) {
1225
                $errstr = 'XML error at line 1, check URL';
1226
            } else {
1227
                $errstr = sprintf('XML error: %s at line %d',
1228
                                  xml_error_string(xml_get_error_code($parser_resource)),
1229
                                  xml_get_current_line_number($parser_resource));
1230
            }
1231
            error_log($errstr);
1232
            $r = new XML_RPC_Response(0, $XML_RPC_err['invalid_return'],
1233
                                      $XML_RPC_str['invalid_return']);
1234
            xml_parser_free($parser_resource);
1235
            return $r;
1236
        }
1237
        xml_parser_free($parser_resource);
1238
        if ($this->debug) {
1239
            print '<PRE>---EVALING---[' .
1240
            strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" .
1241
            htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>";
1242
        }
1243
        if (strlen($XML_RPC_xh[$parser]['st']) == 0) {
1244
            // then something odd has happened
1245
            // and it's time to generate a client side error
1246
            // indicating something odd went on
1247
            $r = new XML_RPC_Response(0, $XML_RPC_err['invalid_return'],
1248
                                      $XML_RPC_str['invalid_return']);
1249
        } else {
1250
            eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;');
1251
            if ($XML_RPC_xh[$parser]['isf']) {
1252
                $f = $v->structmem('faultCode');
1253
                $fs = $v->structmem('faultString');
1254
                $r = new XML_RPC_Response($v, $f->scalarval(),
1255
                                          $fs->scalarval());
1256
            } else {
1257
                $r = new XML_RPC_Response($v);
1258
            }
1259
        }
1260
        $r->hdrs = split("\r?\n", $XML_RPC_xh[$parser]['ha'][1]);
1261
        return $r;
1262
    }
1263
}
1264

    
1265
/**
1266
 *
1267
 *
1268
 * @category   Web Services
1269
 * @package    XML_RPC
1270
 * @author     Edd Dumbill <edd@usefulinc.com>
1271
 * @author     Stig Bakken <stig@php.net>
1272
 * @author     Martin Jansen <mj@php.net>
1273
 * @copyright  1999-2001 Edd Dumbill
1274
 * @version    Release: @package_version@
1275
 * @link       http://pear.php.net/package/XML_RPC
1276
 */
1277
class XML_RPC_Value extends XML_RPC_Base
1278
{
1279
    var $me = array();
1280
    var $mytype = 0;
1281

    
1282
    /**
1283
     * @return void
1284
     */
1285
    function XML_RPC_Value($val = -1, $type = '')
1286
    {
1287
        global $XML_RPC_Types;
1288
        $this->me = array();
1289
        $this->mytype = 0;
1290
        if ($val != -1 || $type != '') {
1291
            if ($type == '') {
1292
                $type = 'string';
1293
            }
1294
            if (!array_key_exists($type, $XML_RPC_Types)) {
1295
                // XXX
1296
                // need some way to report this error
1297
            } elseif ($XML_RPC_Types[$type] == 1) {
1298
                $this->addScalar($val, $type);
1299
            } elseif ($XML_RPC_Types[$type] == 2) {
1300
                $this->addArray($val);
1301
            } elseif ($XML_RPC_Types[$type] == 3) {
1302
                $this->addStruct($val);
1303
            }
1304
        }
1305
    }
1306

    
1307
    /**
1308
     * @return int  returns 1 if successful or 0 if there are problems
1309
     */
1310
    function addScalar($val, $type = 'string')
1311
    {
1312
        global $XML_RPC_Types, $XML_RPC_Boolean;
1313

    
1314
        if ($this->mytype == 1) {
1315
            $this->raiseError('Scalar can have only one value',
1316
                              XML_RPC_ERROR_INVALID_TYPE);
1317
            return 0;
1318
        }
1319
        $typeof = $XML_RPC_Types[$type];
1320
        if ($typeof != 1) {
1321
            $this->raiseError("Not a scalar type (${typeof})",
1322
                              XML_RPC_ERROR_INVALID_TYPE);
1323
            return 0;
1324
        }
1325

    
1326
        if ($type == $XML_RPC_Boolean) {
1327
            if (strcasecmp($val, 'true') == 0
1328
                || $val == 1
1329
                || ($val == true && strcasecmp($val, 'false')))
1330
            {
1331
                $val = 1;
1332
            } else {
1333
                $val = 0;
1334
            }
1335
        }
1336

    
1337
        if ($this->mytype == 2) {
1338
            // we're adding to an array here
1339
            $ar = $this->me['array'];
1340
            $ar[] = new XML_RPC_Value($val, $type);
1341
            $this->me['array'] = $ar;
1342
        } else {
1343
            // a scalar, so set the value and remember we're scalar
1344
            $this->me[$type] = $val;
1345
            $this->mytype = $typeof;
1346
        }
1347
        return 1;
1348
    }
1349

    
1350
    /**
1351
     * @return int  returns 1 if successful or 0 if there are problems
1352
     */
1353
    function addArray($vals)
1354
    {
1355
        global $XML_RPC_Types;
1356
        if ($this->mytype != 0) {
1357
            $this->raiseError(
1358
                    'Already initialized as a [' . $this->kindOf() . ']',
1359
                    XML_RPC_ERROR_ALREADY_INITIALIZED);
1360
            return 0;
1361
        }
1362
        $this->mytype = $XML_RPC_Types['array'];
1363
        $this->me['array'] = $vals;
1364
        return 1;
1365
    }
1366

    
1367
    /**
1368
     * @return int  returns 1 if successful or 0 if there are problems
1369
     */
1370
    function addStruct($vals)
1371
    {
1372
        global $XML_RPC_Types;
1373
        if ($this->mytype != 0) {
1374
            $this->raiseError(
1375
                    'Already initialized as a [' . $this->kindOf() . ']',
1376
                    XML_RPC_ERROR_ALREADY_INITIALIZED);
1377
            return 0;
1378
        }
1379
        $this->mytype = $XML_RPC_Types['struct'];
1380
        $this->me['struct'] = $vals;
1381
        return 1;
1382
    }
1383

    
1384
    /**
1385
     * @return void
1386
     */
1387
    function dump($ar)
1388
    {
1389
        reset($ar);
1390
        while (list($key, $val) = each($ar)) {
1391
            echo "$key => $val<br>";
1392
            if ($key == 'array') {
1393
                while (list($key2, $val2) = each($val)) {
1394
                    echo "-- $key2 => $val2<br>";
1395
                }
1396
            }
1397
        }
1398
    }
1399

    
1400
    /**
1401
     * @return string  the data type of the current value
1402
     */
1403
    function kindOf()
1404
    {
1405
        switch ($this->mytype) {
1406
        case 3:
1407
            return 'struct';
1408

    
1409
        case 2:
1410
            return 'array';
1411

    
1412
        case 1:
1413
            return 'scalar';
1414

    
1415
        default:
1416
            return 'undef';
1417
        }
1418
    }
1419

    
1420
    /**
1421
     * @return string  the data in XML format
1422
     */
1423
    function serializedata($typ, $val)
1424
    {
1425
        $rs = '';
1426
        global $XML_RPC_Types, $XML_RPC_Base64, $XML_RPC_String, $XML_RPC_Boolean;
1427
        if (!array_key_exists($typ, $XML_RPC_Types)) {
1428
            // XXX
1429
            // need some way to report this error
1430
            return;
1431
        }
1432
        switch ($XML_RPC_Types[$typ]) {
1433
        case 3:
1434
            // struct
1435
            $rs .= "<struct>\n";
1436
            reset($val);
1437
            while (list($key2, $val2) = each($val)) {
1438
                $rs .= "<member><name>${key2}</name>\n";
1439
                $rs .= $this->serializeval($val2);
1440
                $rs .= "</member>\n";
1441
            }
1442
            $rs .= '</struct>';
1443
            break;
1444

    
1445
        case 2:
1446
            // array
1447
            $rs .= "<array>\n<data>\n";
1448
            for ($i = 0; $i < sizeof($val); $i++) {
1449
                $rs .= $this->serializeval($val[$i]);
1450
            }
1451
            $rs .= "</data>\n</array>";
1452
            break;
1453

    
1454
        case 1:
1455
            switch ($typ) {
1456
            case $XML_RPC_Base64:
1457
                $rs .= "<${typ}>" . base64_encode($val) . "</${typ}>";
1458
                break;
1459
            case $XML_RPC_Boolean:
1460
                $rs .= "<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
1461
                break;
1462
            case $XML_RPC_String:
1463
		if(is_array($val)) $val = 'array';
1464
                $rs .= "<${typ}>" . htmlspecialchars($val). "</${typ}>";
1465
                break;
1466
            default:
1467
                $rs .= "<${typ}>${val}</${typ}>";
1468
            }
1469
        }
1470
        return $rs;
1471
    }
1472

    
1473
    /**
1474
     * @return string  the data in XML format
1475
     */
1476
    function serialize()
1477
    {
1478
        return $this->serializeval($this);
1479
    }
1480

    
1481
    /**
1482
     * @return string  the data in XML format
1483
     */
1484
    function serializeval($o)
1485
    {
1486
        $rs = '';
1487
        $ar = $o->me;
1488
        reset($ar);
1489
        list($typ, $val) = each($ar);
1490
        $rs .= '<value>';
1491
        $rs .= $this->serializedata($typ, $val);
1492
        $rs .= "</value>\n";
1493
        return $rs;
1494
    }
1495

    
1496
    /**
1497
     * @return mixed  the contents of the element requested
1498
     */
1499
    function structmem($m)
1500
    {
1501
        return $this->me['struct'][$m];
1502
    }
1503

    
1504
    /**
1505
     * @return void
1506
     */
1507
    function structreset()
1508
    {
1509
        reset($this->me['struct']);
1510
    }
1511

    
1512
    /**
1513
     * @return  the key/value pair of the struct's current element
1514
     */
1515
    function structeach()
1516
    {
1517
        return each($this->me['struct']);
1518
    }
1519

    
1520
    /**
1521
     * @return mixed  the current value
1522
     */
1523
    function getval() {
1524
        // UNSTABLE
1525
        global $XML_RPC_BOOLEAN, $XML_RPC_Base64;
1526

    
1527
        reset($this->me);
1528
        list($a, $b) = each($this->me);
1529

    
1530
        // contributed by I Sofer, 2001-03-24
1531
        // add support for nested arrays to scalarval
1532
        // i've created a new method here, so as to
1533
        // preserve back compatibility
1534

    
1535
        if (is_array($b)) {
1536
            foreach ($b as $id => $cont) {
1537
                $b[$id] = $cont->scalarval();
1538
            }
1539
        }
1540

    
1541
        // add support for structures directly encoding php objects
1542
        if (is_object($b)) {
1543
            $t = get_object_vars($b);
1544
            foreach ($t as $id => $cont) {
1545
                $t[$id] = $cont->scalarval();
1546
            }
1547
            foreach ($t as $id => $cont) {
1548
                eval('$b->'.$id.' = $cont;');
1549
            }
1550
        }
1551

    
1552
        // end contrib
1553
        return $b;
1554
    }
1555

    
1556
    /**
1557
     * @return mixed
1558
     */
1559
    function scalarval()
1560
    {
1561
        global $XML_RPC_Boolean, $XML_RPC_Base64;
1562
        reset($this->me);
1563
        list($a, $b) = each($this->me);
1564
        return $b;
1565
    }
1566

    
1567
    /**
1568
     * @return string
1569
     */
1570
    function scalartyp()
1571
    {
1572
        global $XML_RPC_I4, $XML_RPC_Int;
1573
        reset($this->me);
1574
        list($a, $b) = each($this->me);
1575
        if ($a == $XML_RPC_I4) {
1576
            $a = $XML_RPC_Int;
1577
        }
1578
        return $a;
1579
    }
1580

    
1581
    /**
1582
     * @return mixed  the struct's current element
1583
     */
1584
    function arraymem($m)
1585
    {
1586
        return $this->me['array'][$m];
1587
    }
1588

    
1589
    /**
1590
     * @return int  the number of elements in the array
1591
     */
1592
    function arraysize()
1593
    {
1594
        reset($this->me);
1595
        list($a, $b) = each($this->me);
1596
        return sizeof($b);
1597
    }
1598
}
1599

    
1600
/**
1601
 * Return an ISO8601 encoded string
1602
 *
1603
 * While timezones ought to be supported, the XML-RPC spec says:
1604
 *
1605
 * "Don't assume a timezone. It should be specified by the server in its
1606
 * documentation what assumptions it makes about timezones."
1607
 *
1608
 * This routine always assumes localtime unless $utc is set to 1, in which
1609
 * case UTC is assumed and an adjustment for locale is made when encoding.
1610
 *
1611
 * @return string  the formatted date
1612
 */
1613
function XML_RPC_iso8601_encode($timet, $utc = 0) {
1614
    if (!$utc) {
1615
        $t = strftime('%Y%m%dT%H:%M:%S', $timet);
1616
    } else {
1617
        if (function_exists('gmstrftime')) {
1618
            // gmstrftime doesn't exist in some versions
1619
            // of PHP
1620
            $t = gmstrftime('%Y%m%dT%H:%M:%S', $timet);
1621
        } else {
1622
            $t = strftime('%Y%m%dT%H:%M:%S', $timet - date('Z'));
1623
        }
1624
    }
1625
    return $t;
1626
}
1627

    
1628
/**
1629
 * Convert a datetime string into a Unix timestamp
1630
 *
1631
 * While timezones ought to be supported, the XML-RPC spec says:
1632
 *
1633
 * "Don't assume a timezone. It should be specified by the server in its
1634
 * documentation what assumptions it makes about timezones."
1635
 *
1636
 * This routine always assumes localtime unless $utc is set to 1, in which
1637
 * case UTC is assumed and an adjustment for locale is made when encoding.
1638
 *
1639
 * @return int  the unix timestamp of the date submitted
1640
 */
1641
function XML_RPC_iso8601_decode($idate, $utc = 0) {
1642
    $t = 0;
1643
    if (ereg('([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})', $idate, $regs)) {
1644
        if ($utc) {
1645
            $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1646
        } else {
1647
            $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1648
        }
1649
    }
1650
    return $t;
1651
}
1652

    
1653
/**
1654
 * Takes a message in PHP XML_RPC object format and translates it into
1655
 * native PHP types
1656
 *
1657
 * @return mixed
1658
 *
1659
 * @author Dan Libby <dan@libby.com>
1660
 */
1661
function XML_RPC_decode($XML_RPC_val)
1662
{
1663
    $kind = $XML_RPC_val->kindOf();
1664

    
1665
    if ($kind == 'scalar') {
1666
        return $XML_RPC_val->scalarval();
1667

    
1668
    } elseif ($kind == 'array') {
1669
        $size = $XML_RPC_val->arraysize();
1670
        $arr = array();
1671
        for ($i = 0; $i < $size; $i++) {
1672
            $arr[] = XML_RPC_decode($XML_RPC_val->arraymem($i));
1673
        }
1674
        return $arr;
1675

    
1676
    } elseif ($kind == 'struct') {
1677
        $XML_RPC_val->structreset();
1678
        $arr = array();
1679
        while (list($key, $value) = $XML_RPC_val->structeach()) {
1680
            $arr[$key] = XML_RPC_decode($value);
1681
        }
1682
        return $arr;
1683
    }
1684
}
1685

    
1686
/**
1687
 * Takes native php types and encodes them into XML_RPC PHP object format
1688
 *
1689
 * Feature creep -- could support more types via optional type argument.
1690
 *
1691
 * @return string
1692
 *
1693
 * @author Dan Libby <dan@libby.com>
1694
 */
1695
function XML_RPC_encode($php_val) {
1696
    global $XML_RPC_Boolean, $XML_RPC_Int, $XML_RPC_Double, $XML_RPC_String,
1697
            $XML_RPC_Array, $XML_RPC_Struct;
1698

    
1699
    $type = gettype($php_val);
1700
    $XML_RPC_val = new XML_RPC_Value;
1701

    
1702
    switch ($type) {
1703
    case 'array':
1704
        if (empty($php_val)) {
1705
            $XML_RPC_val->addArray($php_val);
1706
            break;
1707
        }
1708
        $tmp = array_diff(array_keys($php_val), range(0, count($php_val)-1));
1709
        if (empty($tmp)) {
1710
           $arr = array();
1711
           foreach ($php_val as $k => $v) {
1712
               $arr[$k] = XML_RPC_encode($v);
1713
           }
1714
           $XML_RPC_val->addArray($arr);
1715
           break;
1716
        }
1717
        // fall though if it's not an enumerated array
1718

    
1719
    case 'object':
1720
        $arr = array();
1721
        foreach ($php_val as $k => $v) {
1722
            $arr[$k] = XML_RPC_encode($v);
1723
        }
1724
        $XML_RPC_val->addStruct($arr);
1725
        break;
1726

    
1727
    case 'integer':
1728
        $XML_RPC_val->addScalar($php_val, $XML_RPC_Int);
1729
        break;
1730

    
1731
    case 'double':
1732
        $XML_RPC_val->addScalar($php_val, $XML_RPC_Double);
1733
        break;
1734

    
1735
    case 'string':
1736
    case 'NULL':
1737
        $XML_RPC_val->addScalar($php_val, $XML_RPC_String);
1738
        break;
1739

    
1740
    case 'boolean':
1741
        // Add support for encoding/decoding of booleans, since they
1742
        // are supported in PHP
1743
        // by <G_Giunta_2001-02-29>
1744
        $XML_RPC_val->addScalar($php_val, $XML_RPC_Boolean);
1745
        break;
1746

    
1747
    case 'unknown type':
1748
    default:
1749
        $XML_RPC_val = false;
1750
    }
1751
    return $XML_RPC_val;
1752
}
1753

    
1754
/*
1755
 * Local variables:
1756
 * tab-width: 4
1757
 * c-basic-offset: 4
1758
 * c-hanging-comment-ender-p: nil
1759
 * End:
1760
 */
1761

    
1762
?>
(16-16/17)