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 function __construct($val=-1, $type='') {
44 /// @todo: optimization creep - do not call addXX, do it all inline.
45 /// downside: booleans will not be coerced anymore
46 if($val!==-1 || $type!='')
48 // optimization creep: inlined all work done by constructor
53 $this->me['string']=$val;
60 case 'dateTime.iso8601':
64 $this->me[$type]=$val;
68 $this->me['array']=$val;
72 $this->me['struct']=$val;
75 error_log("XML-RPC: ".__METHOD__.": not a known type ($type)");
81 if(static::$xmlrpcTypes[$type]==1)
83 $this->addScalar($val,$type);
85 elseif(static::$xmlrpcTypes[$type]==2)
87 $this->addArray($val);
89 elseif(static::$xmlrpcTypes[$type]==3)
91 $this->addStruct($val);
97 * Add a single php value to an (unitialized) xmlrpcval
100 * @return int 1 or 0 on failure
102 function addScalar($val, $type='string')
105 if(isset(static::$xmlrpcTypes[$type])) {
106 $typeof = static::$xmlrpcTypes[$type];
111 error_log("XML-RPC: ".__METHOD__.": not a scalar type ($type)");
115 // coerce booleans into correct values
116 // NB: we should either do it for datetimes, integers and doubles, too,
117 // or just plain remove this check, implemented on booleans only...
118 if($type==static::$xmlrpcBoolean)
120 if(strcasecmp($val,'true')==0 || $val==1 || ($val==true && strcasecmp($val,'false')))
130 switch($this->mytype)
133 error_log('XML-RPC: '.__METHOD__.': scalar xmlrpcval can have only one value');
136 error_log('XML-RPC: '.__METHOD__.': cannot add anonymous scalar to struct xmlrpcval');
139 // we're adding a scalar value to an array here
140 //$ar=$this->me['array'];
141 //$ar[]=new Value($val, $type);
142 //$this->me['array']=$ar;
143 // Faster (?) avoid all the costly array-copy-by-val done here...
144 $this->me['array'][]=new Value($val, $type);
147 // a scalar, so set the value and remember we're scalar
148 $this->me[$type]=$val;
149 $this->mytype=$typeof;
155 * Add an array of xmlrpcval objects to an xmlrpcval
157 * @return int 1 or 0 on failure
159 * @todo add some checking for $vals to be an array of xmlrpcvals?
161 public function addArray($vals)
165 $this->mytype=static::$xmlrpcTypes['array'];
166 $this->me['array']=$vals;
169 elseif($this->mytype==2)
171 // we're adding to an array here
172 $this->me['array'] = array_merge($this->me['array'], $vals);
177 error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']');
183 * Add an array of named xmlrpcval objects to an xmlrpcval
185 * @return int 1 or 0 on failure
187 * @todo add some checking for $vals to be an array?
189 public function addStruct($vals)
193 $this->mytype=static::$xmlrpcTypes['struct'];
194 $this->me['struct']=$vals;
197 elseif($this->mytype==3)
199 // we're adding to a struct here
200 $this->me['struct'] = array_merge($this->me['struct'], $vals);
205 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
214 public function kindOf()
216 switch($this->mytype)
232 private function serializedata($typ, $val, $charset_encoding='')
236 if(!isset(static::$xmlrpcTypes[$typ])) {
240 switch(static::$xmlrpcTypes[$typ])
245 case static::$xmlrpcBase64:
246 $rs.="<${typ}>" . base64_encode($val) . "</${typ}>";
248 case static::$xmlrpcBoolean:
249 $rs.="<${typ}>" . ($val ? '1' : '0') . "</${typ}>";
251 case static::$xmlrpcString:
252 // G. Giunta 2005/2/13: do NOT use htmlentities, since
253 // it will produce named html entities, which are invalid xml
254 $rs.="<${typ}>" . Charset::instance()->encode_entities($val, PhpXmlRpc::$xmlrpc_internalencoding, $charset_encoding). "</${typ}>";
256 case static::$xmlrpcInt:
257 case static::$xmlrpcI4:
258 $rs.="<${typ}>".(int)$val."</${typ}>";
260 case static::$xmlrpcDouble:
261 // avoid using standard conversion of float to string because it is locale-dependent,
262 // and also because the xmlrpc spec forbids exponential notation.
263 // sprintf('%F') could be most likely ok but it fails eg. on 2e-14.
264 // The code below tries its best at keeping max precision while avoiding exp notation,
265 // but there is of course no limit in the number of decimal places to be used...
266 $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>";
268 case static::$xmlrpcDateTime:
271 $rs.="<${typ}>${val}</${typ}>";
273 else if(is_a($val, 'DateTime'))
275 $rs.="<${typ}>".$val->format('Ymd\TH:i:s')."</${typ}>";
277 else if(is_int($val))
279 $rs.="<${typ}>".strftime("%Y%m%dT%H:%M:%S", $val)."</${typ}>";
283 // not really a good idea here: but what shall we output anyway? left for backward compat...
284 $rs.="<${typ}>${val}</${typ}>";
287 case static::$xmlrpcNull:
288 if (PhpXmlRpc::$xmlrpc_null_apache_encoding)
298 // no standard type value should arrive here, but provide a possibility
299 // for xmlrpcvals of unknown type...
300 $rs.="<${typ}>${val}</${typ}>";
305 if ($this->_php_class)
307 $rs.='<struct php_class="' . $this->_php_class . "\">\n";
313 $charsetEncoder = Charset::instance();
314 foreach($val as $key2 => $val2)
316 $rs.='<member><name>'.$charsetEncoder->encode_entities($key2, PhpXmlRpc::$xmlrpc_internalencoding, $charset_encoding)."</name>\n";
317 //$rs.=$this->serializeval($val2);
318 $rs.=$val2->serialize($charset_encoding);
325 $rs.="<array>\n<data>\n";
326 foreach($val as $element)
328 //$rs.=$this->serializeval($val[$i]);
329 $rs.=$element->serialize($charset_encoding);
331 $rs.="</data>\n</array>";
340 * Returns xml representation of the value. XML prologue not included
341 * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed
344 public function serialize($charset_encoding='')
346 // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...
347 //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
350 list($typ, $val) = each($this->me);
351 return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n";
356 function serializeval($o)
358 // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...
359 //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))
363 list($typ, $val) = each($ar);
364 return '<value>' . $this->serializedata($typ, $val) . "</value>\n";
369 * Checks whether a struct member with a given name is present.
370 * Works only on xmlrpcvals of type struct.
371 * @param string $m the name of the struct member to be looked up
374 public function structmemexists($m)
376 return array_key_exists($m, $this->me['struct']);
380 * Returns the value of a given struct member (an xmlrpcval object in itself).
381 * Will raise a php warning if struct member of given name does not exist
382 * @param string $m the name of the struct member to be looked up
385 public function structmem($m)
387 return $this->me['struct'][$m];
391 * Reset internal pointer for xmlrpcvals of type struct.
393 public function structreset()
395 reset($this->me['struct']);
399 * Return next member element for xmlrpcvals of type struct.
402 public function structeach()
404 return each($this->me['struct']);
407 // DEPRECATED! this code looks like it is very fragile and has not been fixed
408 // for a long long time. Shall we remove it for 2.0?
413 list($a,$b)=each($this->me);
414 // contributed by I Sofer, 2001-03-24
415 // add support for nested arrays to scalarval
416 // i've created a new method here, so as to
417 // preserve back compatibility
422 while(list($id,$cont) = @each($b))
424 $b[$id] = $cont->scalarval();
428 // add support for structures directly encoding php objects
431 $t = get_object_vars($b);
433 while(list($id,$cont) = @each($t))
435 $t[$id] = $cont->scalarval();
438 while(list($id,$cont) = @each($t))
448 * Returns the value of a scalar xmlrpcval
451 public function scalarval()
454 list(,$b)=each($this->me);
459 * Returns the type of the xmlrpcval.
460 * For integers, 'int' is always returned in place of 'i4'
463 public function scalartyp()
466 list($a,)=each($this->me);
467 if($a==static::xmlrpcI4)
469 $a=static::xmlrpcInt;
475 * Returns the m-th member of an xmlrpcval of struct type
476 * @param integer $m the index of the value to be retrieved (zero based)
479 public function arraymem($m)
481 return $this->me['array'][$m];
485 * Returns the number of members in an xmlrpcval of array type
488 public function arraysize()
490 return count($this->me['array']);
494 * Returns the number of members in an xmlrpcval of struct type
497 public function structsize()
499 return count($this->me['struct']);