5 use PhpXmlRpc\Helper\Charset;
9 public static $xmlrpcI4 = "i4";
10 public static $xmlrpcInt = "int";
11 public static $xmlrpcBoolean = "boolean";
12 public static $xmlrpcDouble = "double";
13 public static $xmlrpcString = "string";
14 public static $xmlrpcDateTime = "dateTime.iso8601";
15 public static $xmlrpcBase64 = "base64";
16 public static $xmlrpcArray = "array";
17 public static $xmlrpcStruct = "struct";
18 public static $xmlrpcValue = "undefined";
19 public static $xmlrpcNull = "null";
21 public static $xmlrpcTypes = array(
27 "dateTime.iso8601" => 1,
34 /// @todo: do these need to be public?
37 public $_php_class = null;
40 * Build an xmlrpc value
43 * @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed
45 public function __construct($val = -1, $type = '')
47 // optimization creep - do not call addXX, do it all inline.
48 // downside: booleans will not be coerced anymore
49 if ($val !== -1 || $type != '') {
50 // optimization creep: inlined all work done by constructor
54 $this->me['string'] = $val;
61 case 'dateTime.iso8601':
65 $this->me[$type] = $val;
69 $this->me['array'] = $val;
73 $this->me['struct'] = $val;
76 error_log("XML-RPC: " . __METHOD__ . ": not a known type ($type)");
83 if(static::$xmlrpcTypes[$type]==1)
85 $this->addScalar($val,$type);
87 elseif(static::$xmlrpcTypes[$type]==2)
89 $this->addArray($val);
91 elseif(static::$xmlrpcTypes[$type]==3)
93 $this->addStruct($val);
99 * Add a single php value to an (unitialized) xmlrpc value.
102 * @param string $type
104 * @return int 1 or 0 on failure
106 public function addScalar($val, $type = 'string')
109 if (isset(static::$xmlrpcTypes[$type])) {
110 $typeOf = static::$xmlrpcTypes[$type];
114 error_log("XML-RPC: " . __METHOD__ . ": not a scalar type ($type)");
119 // coerce booleans into correct values
120 // NB: we should either do it for datetimes, integers and doubles, too,
121 // or just plain remove this check, implemented on booleans only...
122 if ($type == static::$xmlrpcBoolean) {
123 if (strcasecmp($val, 'true') == 0 || $val == 1 || ($val == true && strcasecmp($val, 'false'))) {
130 switch ($this->mytype) {
132 error_log('XML-RPC: ' . __METHOD__ . ': scalar xmlrpc value can have only one value');
136 error_log('XML-RPC: ' . __METHOD__ . ': cannot add anonymous scalar to struct xmlrpc value');
140 // we're adding a scalar value to an array here
141 $this->me['array'][] = new Value($val, $type);
145 // a scalar, so set the value and remember we're scalar
146 $this->me[$type] = $val;
147 $this->mytype = $typeOf;
154 * Add an array of xmlrpc values objects to an xmlrpc value.
156 * @param Value[] $values
158 * @return int 1 or 0 on failure
160 * @todo add some checking for $values to be an array of xmlrpc values?
162 public function addArray($values)
164 if ($this->mytype == 0) {
165 $this->mytype = static::$xmlrpcTypes['array'];
166 $this->me['array'] = $values;
169 } elseif ($this->mytype == 2) {
170 // we're adding to an array here
171 $this->me['array'] = array_merge($this->me['array'], $values);
175 error_log('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
182 * Add an array of named xmlrpc value objects to an xmlrpc value.
184 * @param Value[] $values
186 * @return int 1 or 0 on failure
188 * @todo add some checking for $values to be an array?
190 public function addStruct($values)
192 if ($this->mytype == 0) {
193 $this->mytype = static::$xmlrpcTypes['struct'];
194 $this->me['struct'] = $values;
197 } elseif ($this->mytype == 3) {
198 // we're adding to a struct here
199 $this->me['struct'] = array_merge($this->me['struct'], $values);
203 error_log('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
210 * Returns a string containing "struct", "array" or "scalar" describing the base type of the value.
214 public function kindOf()
216 switch ($this->mytype) {
231 protected function serializedata($typ, $val, $charsetEncoding = '')
235 if (!isset(static::$xmlrpcTypes[$typ])) {
239 switch (static::$xmlrpcTypes[$typ]) {
242 case static::$xmlrpcBase64:
243 $rs .= "<${typ}>" . base64_encode($val) . "</${typ}>";
245 case static::$xmlrpcBoolean:
246 $rs .= "<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
248 case static::$xmlrpcString:
249 // G. Giunta 2005/2/13: do NOT use htmlentities, since
250 // it will produce named html entities, which are invalid xml
251 $rs .= "<${typ}>" . Charset::instance()->encodeEntities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</${typ}>";
253 case static::$xmlrpcInt:
254 case static::$xmlrpcI4:
255 $rs .= "<${typ}>" . (int)$val . "</${typ}>";
257 case static::$xmlrpcDouble:
258 // avoid using standard conversion of float to string because it is locale-dependent,
259 // and also because the xmlrpc spec forbids exponential notation.
260 // sprintf('%F') could be most likely ok but it fails eg. on 2e-14.
261 // The code below tries its best at keeping max precision while avoiding exp notation,
262 // but there is of course no limit in the number of decimal places to be used...
263 $rs .= "<${typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, 128, '.', '')) . "</${typ}>";
265 case static::$xmlrpcDateTime:
266 if (is_string($val)) {
267 $rs .= "<${typ}>${val}</${typ}>";
268 } elseif (is_a($val, 'DateTime')) {
269 $rs .= "<${typ}>" . $val->format('Ymd\TH:i:s') . "</${typ}>";
270 } elseif (is_int($val)) {
271 $rs .= "<${typ}>" . strftime("%Y%m%dT%H:%M:%S", $val) . "</${typ}>";
273 // not really a good idea here: but what shall we output anyway? left for backward compat...
274 $rs .= "<${typ}>${val}</${typ}>";
277 case static::$xmlrpcNull:
278 if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
285 // no standard type value should arrive here, but provide a possibility
286 // for xmlrpc values of unknown type...
287 $rs .= "<${typ}>${val}</${typ}>";
292 if ($this->_php_class) {
293 $rs .= '<struct php_class="' . $this->_php_class . "\">\n";
297 $charsetEncoder = Charset::instance();
298 foreach ($val as $key2 => $val2) {
299 $rs .= '<member><name>' . $charsetEncoder->encodeEntities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</name>\n";
300 //$rs.=$this->serializeval($val2);
301 $rs .= $val2->serialize($charsetEncoding);
302 $rs .= "</member>\n";
308 $rs .= "<array>\n<data>\n";
309 foreach ($val as $element) {
310 //$rs.=$this->serializeval($val[$i]);
311 $rs .= $element->serialize($charsetEncoding);
313 $rs .= "</data>\n</array>";
323 * Returns xml representation of the value. XML prologue not included.
325 * @param string $charsetEncoding the charset to be used for serialization. if null, US-ASCII is assumed
329 public function serialize($charsetEncoding = '')
331 // add check? slower, but helps to avoid recursion in serializing broken xmlrpc values...
332 //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
335 list($typ, $val) = each($this->me);
337 return '<value>' . $this->serializedata($typ, $val, $charsetEncoding) . "</value>\n";
342 * Checks whether a struct member with a given name is present.
343 * Works only on xmlrpc values of type struct.
345 * @param string $key the name of the struct member to be looked up
349 public function structmemexists($key)
351 return array_key_exists($key, $this->me['struct']);
355 * Returns the value of a given struct member (an xmlrpc value object in itself).
356 * Will raise a php warning if struct member of given name does not exist.
358 * @param string $key the name of the struct member to be looked up
362 public function structmem($key)
364 return $this->me['struct'][$key];
368 * Reset internal pointer for xmlrpc values of type struct.
370 public function structreset()
372 reset($this->me['struct']);
376 * Return next member element for xmlrpc values of type struct.
380 public function structeach()
382 return each($this->me['struct']);
386 * Returns the value of a scalar xmlrpc value.
390 public function scalarval()
393 list(, $b) = each($this->me);
399 * Returns the type of the xmlrpc value.
400 * For integers, 'int' is always returned in place of 'i4'.
404 public function scalartyp()
407 list($a,) = each($this->me);
408 if ($a == static::$xmlrpcI4) {
409 $a = static::$xmlrpcInt;
416 * Returns the m-th member of an xmlrpc value of struct type.
418 * @param integer $key the index of the value to be retrieved (zero based)
422 public function arraymem($key)
424 return $this->me['array'][$key];
428 * Returns the number of members in an xmlrpc value of array type.
432 public function arraysize()
434 return count($this->me['array']);
438 * Returns the number of members in an xmlrpc value of struct type.
442 public function structsize()
444 return count($this->me['struct']);