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: does these need to be public?
37 public $_php_class = null;
41 * @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed
43 public function __construct($val = -1, $type = '')
45 /// @todo: optimization creep - do not call addXX, do it all inline.
46 /// downside: booleans will not be coerced anymore
47 if ($val !== -1 || $type != '') {
48 // optimization creep: inlined all work done by constructor
52 $this->me['string'] = $val;
59 case 'dateTime.iso8601':
63 $this->me[$type] = $val;
67 $this->me['array'] = $val;
71 $this->me['struct'] = $val;
74 error_log("XML-RPC: " . __METHOD__ . ": not a known type ($type)");
80 if(static::$xmlrpcTypes[$type]==1)
82 $this->addScalar($val,$type);
84 elseif(static::$xmlrpcTypes[$type]==2)
86 $this->addArray($val);
88 elseif(static::$xmlrpcTypes[$type]==3)
90 $this->addStruct($val);
96 * Add a single php value to an (unitialized) xmlrpcval.
101 * @return int 1 or 0 on failure
103 public function addScalar($val, $type = 'string')
106 if (isset(static::$xmlrpcTypes[$type])) {
107 $typeof = static::$xmlrpcTypes[$type];
111 error_log("XML-RPC: " . __METHOD__ . ": not a scalar type ($type)");
116 // coerce booleans into correct values
117 // NB: we should either do it for datetimes, integers and doubles, too,
118 // or just plain remove this check, implemented on booleans only...
119 if ($type == static::$xmlrpcBoolean) {
120 if (strcasecmp($val, 'true') == 0 || $val == 1 || ($val == true && strcasecmp($val, 'false'))) {
127 switch ($this->mytype) {
129 error_log('XML-RPC: ' . __METHOD__ . ': scalar xmlrpc value can have only one value');
133 error_log('XML-RPC: ' . __METHOD__ . ': cannot add anonymous scalar to struct xmlrpc value');
137 // we're adding a scalar value to an array here
138 //$ar=$this->me['array'];
139 //$ar[]=new Value($val, $type);
140 //$this->me['array']=$ar;
141 // Faster (?) avoid all the costly array-copy-by-val done here...
142 $this->me['array'][] = new Value($val, $type);
146 // a scalar, so set the value and remember we're scalar
147 $this->me[$type] = $val;
148 $this->mytype = $typeof;
155 * Add an array of xmlrpcval objects to an xmlrpcval.
157 * @param Value[] $vals
159 * @return int 1 or 0 on failure
161 * @todo add some checking for $vals to be an array of xmlrpcvals?
163 public function addArray($vals)
165 if ($this->mytype == 0) {
166 $this->mytype = static::$xmlrpcTypes['array'];
167 $this->me['array'] = $vals;
170 } elseif ($this->mytype == 2) {
171 // we're adding to an array here
172 $this->me['array'] = array_merge($this->me['array'], $vals);
176 error_log('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
183 * Add an array of named xmlrpcval objects to an xmlrpcval.
185 * @param Value[] $vals
187 * @return int 1 or 0 on failure
189 * @todo add some checking for $vals to be an array?
191 public function addStruct($vals)
193 if ($this->mytype == 0) {
194 $this->mytype = static::$xmlrpcTypes['struct'];
195 $this->me['struct'] = $vals;
198 } elseif ($this->mytype == 3) {
199 // we're adding to a struct here
200 $this->me['struct'] = array_merge($this->me['struct'], $vals);
204 error_log('XML-RPC: ' . __METHOD__ . ': already initialized as a [' . $this->kindOf() . ']');
211 * Returns a string containing "struct", "array" or "scalar" describing the base type of the value.
215 public function kindOf()
217 switch ($this->mytype) {
232 protected function serializedata($typ, $val, $charset_encoding = '')
236 if (!isset(static::$xmlrpcTypes[$typ])) {
240 switch (static::$xmlrpcTypes[$typ]) {
243 case static::$xmlrpcBase64:
244 $rs .= "<${typ}>" . base64_encode($val) . "</${typ}>";
246 case static::$xmlrpcBoolean:
247 $rs .= "<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
249 case static::$xmlrpcString:
250 // G. Giunta 2005/2/13: do NOT use htmlentities, since
251 // it will produce named html entities, which are invalid xml
252 $rs .= "<${typ}>" . Charset::instance()->encode_entities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charset_encoding) . "</${typ}>";
254 case static::$xmlrpcInt:
255 case static::$xmlrpcI4:
256 $rs .= "<${typ}>" . (int)$val . "</${typ}>";
258 case static::$xmlrpcDouble:
259 // avoid using standard conversion of float to string because it is locale-dependent,
260 // and also because the xmlrpc spec forbids exponential notation.
261 // sprintf('%F') could be most likely ok but it fails eg. on 2e-14.
262 // The code below tries its best at keeping max precision while avoiding exp notation,
263 // but there is of course no limit in the number of decimal places to be used...
264 $rs .= "<${typ}>" . preg_replace('/\\.?0+$/', '', number_format((double)$val, 128, '.', '')) . "</${typ}>";
266 case static::$xmlrpcDateTime:
267 if (is_string($val)) {
268 $rs .= "<${typ}>${val}</${typ}>";
269 } elseif (is_a($val, 'DateTime')) {
270 $rs .= "<${typ}>" . $val->format('Ymd\TH:i:s') . "</${typ}>";
271 } elseif (is_int($val)) {
272 $rs .= "<${typ}>" . strftime("%Y%m%dT%H:%M:%S", $val) . "</${typ}>";
274 // not really a good idea here: but what shall we output anyway? left for backward compat...
275 $rs .= "<${typ}>${val}</${typ}>";
278 case static::$xmlrpcNull:
279 if (PhpXmlRpc::$xmlrpc_null_apache_encoding) {
286 // no standard type value should arrive here, but provide a possibility
287 // for xmlrpcvals of unknown type...
288 $rs .= "<${typ}>${val}</${typ}>";
293 if ($this->_php_class) {
294 $rs .= '<struct php_class="' . $this->_php_class . "\">\n";
298 $charsetEncoder = Charset::instance();
299 foreach ($val as $key2 => $val2) {
300 $rs .= '<member><name>' . $charsetEncoder->encode_entities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charset_encoding) . "</name>\n";
301 //$rs.=$this->serializeval($val2);
302 $rs .= $val2->serialize($charset_encoding);
303 $rs .= "</member>\n";
309 $rs .= "<array>\n<data>\n";
310 foreach ($val as $element) {
311 //$rs.=$this->serializeval($val[$i]);
312 $rs .= $element->serialize($charset_encoding);
314 $rs .= "</data>\n</array>";
324 * Returns xml representation of the value. XML prologue not included.
326 * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed
330 public function serialize($charset_encoding = '')
332 // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...
333 //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
336 list($typ, $val) = each($this->me);
338 return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n";
343 * Checks whether a struct member with a given name is present.
344 * Works only on xmlrpcvals of type struct.
346 * @param string $m the name of the struct member to be looked up
350 public function structmemexists($m)
352 return array_key_exists($m, $this->me['struct']);
356 * Returns the value of a given struct member (an xmlrpcval object in itself).
357 * Will raise a php warning if struct member of given name does not exist.
359 * @param string $m the name of the struct member to be looked up
363 public function structmem($m)
365 return $this->me['struct'][$m];
369 * Reset internal pointer for xmlrpcvals of type struct.
371 public function structreset()
373 reset($this->me['struct']);
377 * Return next member element for xmlrpcvals of type struct.
381 public function structeach()
383 return each($this->me['struct']);
387 * Returns the value of a scalar xmlrpcval.
391 public function scalarval()
394 list(, $b) = each($this->me);
400 * Returns the type of the xmlrpcval.
401 * For integers, 'int' is always returned in place of 'i4'.
405 public function scalartyp()
408 list($a,) = each($this->me);
409 if ($a == static::$xmlrpcI4) {
410 $a = static::$xmlrpcInt;
417 * Returns the m-th member of an xmlrpcval of struct type.
419 * @param integer $m the index of the value to be retrieved (zero based)
423 public function arraymem($m)
425 return $this->me['array'][$m];
429 * Returns the number of members in an xmlrpcval of array type.
433 public function arraysize()
435 return count($this->me['array']);
439 * Returns the number of members in an xmlrpcval of struct type.
443 public function structsize()
445 return count($this->me['struct']);