6bcb8b459fb341e3514c7ad1053e7b740ed386b3
[plcapi.git] / src / Value.php
1 <?php
2
3 namespace PhpXmlRpc;
4
5 use PhpXmlRpc\Helper\Charset;
6 use PhpXmlRpc\Helper\Logger;
7
8 /**
9  * This class enables the creation of values for XML-RPC, by encapsulating plain php values.
10  */
11 class Value implements \Countable, \IteratorAggregate, \ArrayAccess
12 {
13     public static $xmlrpcI4 = "i4";
14     public static $xmlrpcI8 = "i8";
15     public static $xmlrpcInt = "int";
16     public static $xmlrpcBoolean = "boolean";
17     public static $xmlrpcDouble = "double";
18     public static $xmlrpcString = "string";
19     public static $xmlrpcDateTime = "dateTime.iso8601";
20     public static $xmlrpcBase64 = "base64";
21     public static $xmlrpcArray = "array";
22     public static $xmlrpcStruct = "struct";
23     public static $xmlrpcValue = "undefined";
24     public static $xmlrpcNull = "null";
25
26     public static $xmlrpcTypes = array(
27         "i4" => 1,
28         "i8" => 1,
29         "int" => 1,
30         "boolean" => 1,
31         "double" => 1,
32         "string" => 1,
33         "dateTime.iso8601" => 1,
34         "base64" => 1,
35         "array" => 2,
36         "struct" => 3,
37         "null" => 1,
38     );
39
40     /// @todo: do these need to be public?
41     /** @var Value[]|mixed */
42     public $me = array();
43     /**
44      * @var int $mytype
45      * @internal
46      */
47     public $mytype = 0;
48     /** @var string|null $_php_class */
49     public $_php_class = null;
50
51     /**
52      * Build an xmlrpc value.
53      *
54      * When no value or type is passed in, the value is left uninitialized, and the value can be added later.
55      *
56      * @param mixed $val if passing in an array, all array elements should be PhpXmlRpc\Value themselves
57      * @param string $type any valid xmlrpc type name (lowercase): i4, int, boolean, string, double, dateTime.iso8601,
58      *                     base64, array, struct, null.
59      *                     If null, 'string' is assumed.
60      *                     You should refer to http://www.xmlrpc.com/spec for more information on what each of these mean.
61      */
62     public function __construct($val = -1, $type = '')
63     {
64         // optimization creep - do not call addXX, do it all inline.
65         // downside: booleans will not be coerced anymore
66         if ($val !== -1 || $type != '') {
67             switch ($type) {
68                 case '':
69                     $this->mytype = 1;
70                     $this->me['string'] = $val;
71                     break;
72                 case 'i4':
73                 case 'i8':
74                 case 'int':
75                 case 'double':
76                 case 'string':
77                 case 'boolean':
78                 case 'dateTime.iso8601':
79                 case 'base64':
80                 case 'null':
81                     $this->mytype = 1;
82                     $this->me[$type] = $val;
83                     break;
84                 case 'array':
85                     $this->mytype = 2;
86                     $this->me['array'] = $val;
87                     break;
88                 case 'struct':
89                     $this->mytype = 3;
90                     $this->me['struct'] = $val;
91                     break;
92                 default:
93                     Logger::instance()->errorLog("XML-RPC: " . __METHOD__ . ": not a known type ($type)");
94             }
95         }
96     }
97
98     /**
99      * Add a single php value to an xmlrpc value.
100      *
101      * If the xmlrpc value is an array, the php value is added as its last element.
102      * If the xmlrpc value is empty (uninitialized), this method makes it a scalar value, and sets that value.
103      * Fails if the xmlrpc value is not an array and already initialized.
104      *
105      * @param mixed $val
106      * @param string $type allowed values: i4, i8, int, boolean, string, double, dateTime.iso8601, base64, null.
107      *
108      * @return int 1 or 0 on failure
109      */
110     public function addScalar($val, $type = 'string')
111     {
112         $typeOf = null;
113         if (isset(static::$xmlrpcTypes[$type])) {
114             $typeOf = static::$xmlrpcTypes[$type];
115         }
116
117         if ($typeOf !== 1) {
118             Logger::instance()->errorLog("XML-RPC: " . __METHOD__ . ": not a scalar type ($type)");
119             return 0;
120         }
121
122         // coerce booleans into correct values
123         // NB: we should either do it for datetimes, integers, i8 and doubles, too,
124         // or just plain remove this check, implemented on booleans only...
125         if ($type == static::$xmlrpcBoolean) {
126             if (strcasecmp($val, 'true') == 0 || $val == 1 || ($val == true && strcasecmp($val, 'false'))) {
127                 $val = true;
128             } else {
129                 $val = false;
130             }
131         }
132
133         switch ($this->mytype) {
134             case 1:
135                 Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': scalar xmlrpc value can have only one value');
136                 return 0;
137             case 3:
138                 Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': cannot add anonymous scalar to struct xmlrpc value');
139                 return 0;
140             case 2:
141                 // we're adding a scalar value to an array here
142                 $this->me['array'][] = new Value($val, $type);
143
144                 return 1;
145             default:
146                 // a scalar, so set the value and remember we're scalar
147                 $this->me[$type] = $val;
148                 $this->mytype = $typeOf;
149
150                 return 1;
151         }
152     }
153
154     /**
155      * Add an array of xmlrpc value objects to an xmlrpc value.
156      *
157      * If the xmlrpc value is an array, the elements are appended to the existing ones.
158      * If the xmlrpc value is empty (uninitialized), this method makes it an array value, and sets that value.
159      * Fails otherwise.
160      *
161      * @param Value[] $values
162      *
163      * @return int 1 or 0 on failure
164      *
165      * @todo add some checking for $values to be an array of xmlrpc values?
166      */
167     public function addArray($values)
168     {
169         if ($this->mytype == 0) {
170             $this->mytype = static::$xmlrpcTypes['array'];
171             $this->me['array'] = $values;
172
173             return 1;
174         } elseif ($this->mytype == 2) {
175             // we're adding to an array here
176             $this->me['array'] = array_merge($this->me['array'], $values);
177
178             return 1;
179         } else {
180             Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
181             return 0;
182         }
183     }
184
185     /**
186      * Merges an array of named xmlrpc value objects into an xmlrpc value.
187      *
188      * If the xmlrpc value is a struct, the elements are merged with the existing ones (overwriting existing ones).
189      * If the xmlrpc value is empty (uninitialized), this method makes it a struct value, and sets that value.
190      * Fails otherwise.
191      *
192      * @param Value[] $values
193      *
194      * @return int 1 or 0 on failure
195      *
196      * @todo add some checking for $values to be an array?
197      */
198     public function addStruct($values)
199     {
200         if ($this->mytype == 0) {
201             $this->mytype = static::$xmlrpcTypes['struct'];
202             $this->me['struct'] = $values;
203
204             return 1;
205         } elseif ($this->mytype == 3) {
206             // we're adding to a struct here
207             $this->me['struct'] = array_merge($this->me['struct'], $values);
208
209             return 1;
210         } else {
211             Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
212             return 0;
213         }
214     }
215
216     /**
217      * Returns a string containing either "struct", "array", "scalar" or "undef", describing the base type of the value.
218      *
219      * @return string
220      */
221     public function kindOf()
222     {
223         switch ($this->mytype) {
224             case 3:
225                 return 'struct';
226             case 2:
227                 return 'array';
228             case 1:
229                 return 'scalar';
230             default:
231                 return 'undef';
232         }
233     }
234
235     protected function serializedata($typ, $val, $charsetEncoding = '')
236     {
237         $rs = '';
238
239         if (!isset(static::$xmlrpcTypes[$typ])) {
240             return $rs;
241         }
242
243         switch (static::$xmlrpcTypes[$typ]) {
244             case 1:
245                 switch ($typ) {
246                     case static::$xmlrpcBase64:
247                         $rs .= "<${typ}>" . base64_encode($val) . "</${typ}>";
248                         break;
249                     case static::$xmlrpcBoolean:
250                         $rs .= "<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
251                         break;
252                     case static::$xmlrpcString:
253                         // Do NOT use htmlentities, since it will produce named html entities, which are invalid xml
254                         $rs .= "<${typ}>" . Charset::instance()->encodeEntities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</${typ}>";
255                         break;
256                     case static::$xmlrpcInt:
257                     case static::$xmlrpcI4:
258                     case static::$xmlrpcI8:
259                         $rs .= "<${typ}>" . (int)$val . "</${typ}>";
260                         break;
261                     case static::$xmlrpcDouble:
262                         // avoid using standard conversion of float to string because it is locale-dependent,
263                         // and also because the xmlrpc spec forbids exponential notation.
264                         // sprintf('%F') could be most likely ok but it fails eg. on 2e-14.
265                         // The code below tries its best at keeping max precision while avoiding exp notation,
266                         // but there is of course no limit in the number of decimal places to be used...
267                         $rs .= "<${typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, PhpXmlRpc::$xmlpc_double_precision, '.', '')) . "</${typ}>";
268                         break;
269                     case static::$xmlrpcDateTime:
270                         if (is_string($val)) {
271                             $rs .= "<${typ}>${val}</${typ}>";
272                         } elseif (is_a($val, 'DateTime')) {
273                             $rs .= "<${typ}>" . $val->format('Ymd\TH:i:s') . "</${typ}>";
274                         } elseif (is_int($val)) {
275                             $rs .= "<${typ}>" . strftime("%Y%m%dT%H:%M:%S", $val) . "</${typ}>";
276                         } else {
277                             // not really a good idea here: but what shall we output anyway? left for backward compat...
278                             $rs .= "<${typ}>${val}</${typ}>";
279                         }
280                         break;
281                     case static::$xmlrpcNull:
282                         if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
283                             $rs .= "<ex:nil/>";
284                         } else {
285                             $rs .= "<nil/>";
286                         }
287                         break;
288                     default:
289                         // no standard type value should arrive here, but provide a possibility
290                         // for xmlrpc values of unknown type...
291                         $rs .= "<${typ}>${val}</${typ}>";
292                 }
293                 break;
294             case 3:
295                 // struct
296                 if ($this->_php_class) {
297                     $rs .= '<struct php_class="' . $this->_php_class . "\">\n";
298                 } else {
299                     $rs .= "<struct>\n";
300                 }
301                 $charsetEncoder = Charset::instance();
302                 /** @var Value $val2 */
303                 foreach ($val as $key2 => $val2) {
304                     $rs .= '<member><name>' . $charsetEncoder->encodeEntities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</name>\n";
305                     //$rs.=$this->serializeval($val2);
306                     $rs .= $val2->serialize($charsetEncoding);
307                     $rs .= "</member>\n";
308                 }
309                 $rs .= '</struct>';
310                 break;
311             case 2:
312                 // array
313                 $rs .= "<array>\n<data>\n";
314                 /** @var Value $element */
315                 foreach ($val as $element) {
316                     //$rs.=$this->serializeval($val[$i]);
317                     $rs .= $element->serialize($charsetEncoding);
318                 }
319                 $rs .= "</data>\n</array>";
320                 break;
321             default:
322                 break;
323         }
324
325         return $rs;
326     }
327
328     /**
329      * Returns the xml representation of the value. XML prologue not included.
330      *
331      * @param string $charsetEncoding the charset to be used for serialization. if null, US-ASCII is assumed
332      *
333      * @return string
334      */
335     public function serialize($charsetEncoding = '')
336     {
337         $val = reset($this->me);
338         $typ = key($this->me);
339
340         return '<value>' . $this->serializedata($typ, $val, $charsetEncoding) . "</value>\n";
341     }
342
343     /**
344      * Checks whether a struct member with a given name is present.
345      *
346      * Works only on xmlrpc values of type struct.
347      *
348      * @param string $key the name of the struct member to be looked up
349      *
350      * @return boolean
351      *
352      * @deprecated use array access, e.g. isset($val[$key])
353      */
354     public function structmemexists($key)
355     {
356         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
357
358         return array_key_exists($key, $this->me['struct']);
359     }
360
361     /**
362      * Returns the value of a given struct member (an xmlrpc value object in itself).
363      * Will raise a php warning if struct member of given name does not exist.
364      *
365      * @param string $key the name of the struct member to be looked up
366      *
367      * @return Value
368      *
369      * @deprecated use array access, e.g. $val[$key]
370      */
371     public function structmem($key)
372     {
373         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
374
375         return $this->me['struct'][$key];
376     }
377
378     /**
379      * Reset internal pointer for xmlrpc values of type struct.
380      * @deprecated iterate directly over the object using foreach instead
381      */
382     public function structreset()
383     {
384         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
385
386         reset($this->me['struct']);
387     }
388
389     /**
390      * Return next member element for xmlrpc values of type struct.
391      *
392      * @return Value
393      * @throws \Error starting with php 8.0, this function should not be used, as it will always throw
394      *
395      * @deprecated iterate directly over the object using foreach instead
396      */
397     public function structeach()
398     {
399         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
400
401         return @each($this->me['struct']);
402     }
403
404     /**
405      * Returns the value of a scalar xmlrpc value (base 64 decoding is automatically handled here)
406      *
407      * @return mixed
408      */
409     public function scalarval()
410     {
411         $b = reset($this->me);
412
413         return $b;
414     }
415
416     /**
417      * Returns the type of the xmlrpc value.
418      *
419      * For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and returned as such
420      *
421      * @return string
422      */
423     public function scalartyp()
424     {
425         reset($this->me);
426         $a = key($this->me);
427         if ($a == static::$xmlrpcI4) {
428             $a = static::$xmlrpcInt;
429         }
430
431         return $a;
432     }
433
434     /**
435      * Returns the m-th member of an xmlrpc value of array type.
436      *
437      * @param integer $key the index of the value to be retrieved (zero based)
438      *
439      * @return Value
440      *
441      * @deprecated use array access, e.g. $val[$key]
442      */
443     public function arraymem($key)
444     {
445         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
446
447         return $this->me['array'][$key];
448     }
449
450     /**
451      * Returns the number of members in an xmlrpc value of array type.
452      *
453      * @return integer
454      *
455      * @deprecated use count() instead
456      */
457     public function arraysize()
458     {
459         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
460
461         return count($this->me['array']);
462     }
463
464     /**
465      * Returns the number of members in an xmlrpc value of struct type.
466      *
467      * @return integer
468      *
469      * @deprecated use count() instead
470      */
471     public function structsize()
472     {
473         //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
474
475         return count($this->me['struct']);
476     }
477
478     /**
479      * Returns the number of members in an xmlrpc value:
480      * - 0 for uninitialized values
481      * - 1 for scalar values
482      * - the number of elements for struct and array values
483      *
484      * @return integer
485      */
486     public function count()
487     {
488         switch ($this->mytype) {
489             case 3:
490                 return count($this->me['struct']);
491             case 2:
492                 return count($this->me['array']);
493             case 1:
494                 return 1;
495             default:
496                 return 0;
497         }
498     }
499
500     /**
501      * Implements the IteratorAggregate interface
502      *
503      * @return \ArrayIterator
504      */
505     public function getIterator()
506     {
507         switch ($this->mytype) {
508             case 3:
509                 return new \ArrayIterator($this->me['struct']);
510             case 2:
511                 return new \ArrayIterator($this->me['array']);
512             case 1:
513                 return new \ArrayIterator($this->me);
514             default:
515                 return new \ArrayIterator();
516         }
517     }
518
519     /**
520      * @internal required to be public to implement an Interface
521      * @param mixed $offset
522      * @param mixed $value
523      * @throws \Exception
524      */
525     public function offsetSet($offset, $value)
526     {
527         switch ($this->mytype) {
528             case 3:
529                 if (!($value instanceof \PhpXmlRpc\Value)) {
530                     throw new \Exception('It is only possible to add Value objects to an XML-RPC Struct');
531                 }
532                 if (is_null($offset)) {
533                     // disallow struct members with empty names
534                     throw new \Exception('It is not possible to add anonymous members to an XML-RPC Struct');
535                 } else {
536                     $this->me['struct'][$offset] = $value;
537                 }
538                 return;
539             case 2:
540                 if (!($value instanceof \PhpXmlRpc\Value)) {
541                     throw new \Exception('It is only possible to add Value objects to an XML-RPC Array');
542                 }
543                 if (is_null($offset)) {
544                     $this->me['array'][] = $value;
545                 } else {
546                     // nb: we are not checking that $offset is above the existing array range...
547                     $this->me['array'][$offset] = $value;
548                 }
549                 return;
550             case 1:
551 // todo: handle i4 vs int
552                 reset($this->me);
553                 $type = key($this->me);
554                 if ($type != $offset) {
555                     throw new \Exception('');
556                 }
557                 $this->me[$type] = $value;
558                 return;
559             default:
560                 // it would be nice to allow empty values to be be turned into non-empty ones this way, but we miss info to do so
561                 throw new \Exception("XML-RPC Value is of type 'undef' and its value can not be set using array index");
562         }
563     }
564
565     /**
566      * @internal required to be public to implement an Interface
567      * @param mixed $offset
568      * @return bool
569      */
570     public function offsetExists($offset)
571     {
572         switch ($this->mytype) {
573             case 3:
574                 return isset($this->me['struct'][$offset]);
575             case 2:
576                 return isset($this->me['array'][$offset]);
577             case 1:
578 // todo: handle i4 vs int
579                 return $offset == $this->scalartyp();
580             default:
581                 return false;
582         }
583     }
584
585     /**
586      * @internal required to be public to implement an Interface
587      * @param mixed $offset
588      * @throws \Exception
589      */
590     public function offsetUnset($offset)
591     {
592         switch ($this->mytype) {
593             case 3:
594                 unset($this->me['struct'][$offset]);
595                 return;
596             case 2:
597                 unset($this->me['array'][$offset]);
598                 return;
599             case 1:
600                 // can not remove value from a scalar
601                 throw new \Exception("XML-RPC Value is of type 'scalar' and its value can not be unset using array index");
602             default:
603                 throw new \Exception("XML-RPC Value is of type 'undef' and its value can not be unset using array index");
604         }
605     }
606
607     /**
608      * @internal required to be public to implement an Interface
609      * @param mixed $offset
610      * @return mixed|Value|null
611      * @throws \Exception
612      */
613     public function offsetGet($offset)
614     {
615         switch ($this->mytype) {
616             case 3:
617                 return isset($this->me['struct'][$offset]) ? $this->me['struct'][$offset] : null;
618             case 2:
619                 return isset($this->me['array'][$offset]) ? $this->me['array'][$offset] : null;
620             case 1:
621 // on bad type: null or exception?
622                 $value = reset($this->me);
623                 $type = key($this->me);
624                 return $type == $offset ? $value : null;
625             default:
626 // return null or exception?
627                 throw new \Exception("XML-RPC Value is of type 'undef' and can not be accessed using array index");
628         }
629     }
630 }