[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/domit/ -> dom_xmlrpc_builder.php (source)

   1  <?php
   2  /**
   3  * dom_xmlrpc_builder is a utility for building string representations of XML-RPC documents
   4  * @package dom-xmlrpc
   5  * @copyright (C) 2004 John Heinstein. All rights reserved
   6  * @license http://www.gnu.org/copyleft/lesser.html LGPL License
   7  * @author John Heinstein <johnkarl@nbnet.nb.ca>
   8  * @link http://www.engageinteractive.com/dom_xmlrpc/ DOM XML-RPC Home Page
   9  * DOM XML-RPC is Free Software
  10  **/
  11  
  12  if (!defined('DOM_XMLRPC_INCLUDE_PATH')) {
  13      define('DOM_XMLRPC_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  14  }
  15  
  16  require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_constants.php');
  17  
  18  /**
  19  * A utility for building string representations of XML-RPC documents
  20  *
  21  * @package dom-xmlrpc
  22  * @author John Heinstein <johnkarl@nbnet.nb.ca>
  23  */
  24  class dom_xmlrpc_builder {
  25      /** @var string The method type of the XML-RPC data, e.g., DOM_XMLRPC_TYPE_METHODRESPONSE */
  26      var $methodType;
  27      /** @var string The XML string representing the method params */
  28      var $params = "";
  29      /** @var string The type of object marshalling to be used, if this option is enabled */
  30      var $objectMarshalling = DOM_XMLRPC_OBJECT_MARSHALLING_ANONYMOUS;
  31  
  32      /**
  33      * Invokes the ability to pass PHP objects in addition to basic native types such as int and string
  34      * @param string The type of object marshalling to be invoked: either anonymous, named, or serialized
  35      */
  36  	function setObjectMarshalling($type) {
  37          $type = strtolower($type);
  38  
  39          switch ($type) {
  40              case DOM_XMLRPC_OBJECT_MARSHALLING_ANONYMOUS: //default
  41              case DOM_XMLRPC_OBJECT_MARSHALLING_NAMED:
  42              case DOM_XMLRPC_OBJECT_MARSHALLING_SERIALIZED:
  43                  $this->objectMarshalling = $type;
  44                  break;
  45              default:
  46                  XMLRPC_Client_Exception::raiseException(XMLRPC_CLIENT_RESPONSE_TYPE_ERR,
  47                                      ('Invalid object marshalling type: ' . $type));
  48          }
  49      } //setObjectMarshalling
  50  
  51      /**
  52      * Creates an XML-RPC representation of a scalar value (e.g. string, double, boolean)
  53      * @param mixed The value to be converted to XML-RPC notation
  54      * @param string The type of the value (will be autodetected if omitted)
  55      * @return string An XML-RPC representation of the value
  56      */
  57  	function createScalar($value, $type = '') {
  58          if ($type == '') {
  59              $type =  dom_xmlrpc_utilities::getScalarTypeFromValue($value);
  60          }
  61  
  62          switch ($type) {
  63              case DOM_XMLRPC_TYPE_STRING:
  64                  if ($this->methodType == DOM_XMLRPC_TYPE_METHODRESPONSE) {
  65                      //htmlencode the string, because
  66                      //DOM XML-RPC server has decoded it
  67                      $value = htmlentities($value, ENT_QUOTES);
  68                  }
  69                  break;
  70              case DOM_XMLRPC_TYPE_DOUBLE:
  71                  //ensure trailing .0 if it looks like an int
  72                  $value = '' . $value;
  73                  if (strpos($value, '.') === false) {
  74                      $value .= '.0';
  75                  }
  76                  break;
  77              case DOM_XMLRPC_TYPE_BOOLEAN:
  78                  if (is_bool($value)) {
  79                      $value = ($value ? '1' : '0');
  80                  }
  81                  break;
  82              case DOM_XMLRPC_TYPE_BASE64:
  83                  if (is_object($value)) {
  84                      $value = $value->getEncoded();
  85                  }
  86                  break;
  87              case DOM_XMLRPC_TYPE_DATETIME:
  88                  if (is_object($value)) {
  89                      $value = $value->getDateTime_iso();
  90                  }
  91                  break;
  92          }
  93  
  94          return ("<value><$type>$value</$type></value>");
  95      } //createScalar
  96  
  97      /**
  98      * Generates an XML-RPC param from the scalar and adds it to the method params
  99      * @param mixed The value to be converted to XML-RPC notation
 100      * @param string The type of the value (will be autodetected if omitted)
 101      */
 102  	function addScalar($value, $type = '') {
 103          $this->params .= "\n\t\t<param>" . $this->createScalar($value, $type)  . '</param>';
 104      } //addScalar
 105  
 106      /**
 107      * Creates an XML-RPC representation of a PHP array
 108      * @param array The array to be converted to XML-RPC notation
 109      * @return string An XML-RPC representation of the array
 110      */
 111  	function createArray(&$myArray) {
 112          $data = '<value><array><data>';
 113  
 114          foreach ($myArray as $key => $value) {
 115              $currDataItem =& $myArray[$key];
 116              $currType = dom_xmlrpc_utilities::getTypeFromValue($currDataItem);
 117  
 118              $data .= $this->create($currDataItem, $currType);
 119          }
 120  
 121          $data .= '</data></array></value>';
 122  
 123          return $data;
 124      } //createArray
 125  
 126      /**
 127      * Generates an XML-RPC param from the array and adds it to the method params
 128      * @param array The array to be converted to XML-RPC notation
 129      */
 130  	function addArray($myArray) {
 131          $this->addArrayByRef($myArray);
 132      } //addArray
 133  
 134      /**
 135      * Generates an XML-RPC param from the array reference and adds it to the method params
 136      * @param array The array reference to be converted to XML-RPC notation
 137      */
 138  	function addArrayByRef(&$myArray) { //can call with reference if deep copy is required
 139          $this->params .= "\n\t\t<param>" . $this->createArray($myArray)  . '</param>';
 140      } //addArrayByRef
 141  
 142      /**
 143      * Creates an XML-RPC representation of a PHP object
 144      * @param object The object to be converted to XML-RPC notation
 145      * @return string An XML-RPC representation of the object
 146      */
 147  	function createObject(&$myObject) {
 148          require_once ('dom_xmlrpc_object.php');
 149  
 150          if (get_class($myObject) == 'dom_xmlrpc_object') { //wrapper for xmlrpc object
 151              $myObject =& $myObject->getObject(); //grab embedded object
 152          }
 153  
 154          switch($this->objectMarshalling){
 155              case DOM_XMLRPC_OBJECT_MARSHALLING_ANONYMOUS:
 156                  //generic struct, one member for each
 157                  //object property, object type is discarded
 158                  $data = '<value><struct>';
 159  
 160                  foreach ($myObject as $key => $value) {
 161                      $currValue =& $myObject->$key;
 162                      $currType = dom_xmlrpc_utilities::getTypeFromValue($currValue);
 163  
 164                      $data .= $this->createMember($key, $currValue, $currType);
 165                  }
 166  
 167                  $data .= '</struct></value>';
 168                  break;
 169  
 170              case DOM_XMLRPC_OBJECT_MARSHALLING_NAMED:
 171                  //struct with one member for each
 172                  //object property, object type is defined
 173                  //by an additional member (first in list) named
 174                  //"__phpobject__" whose value is a string
 175                  //containing the class type of the object
 176                  $data = '<value><struct>';
 177                  $data .= $this->createMember(DOM_XMLRPC_PHPOBJECT, get_class($myObject), DOM_XMLRPC_TYPE_STRING);
 178  
 179                  foreach ($myObject as $key => $value) {
 180                      $currValue =& $myObject->$key;
 181                      $currType = dom_xmlrpc_utilities::getTypeFromValue($currValue);
 182  
 183                      $data .= $this->createMember($key, $currValue, $currType);
 184                  }
 185  
 186                  $data .= '</struct></value>';
 187                  break;
 188  
 189              case DOM_XMLRPC_OBJECT_MARSHALLING_SERIALIZED:
 190                  //serialized object, one member of type base64
 191                  //which is the serialized object, base64 encoded
 192                  require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_base64.php');
 193                  $data = '<value><struct>';
 194                  $data .= $this->createMember(DOM_XMLRPC_PHPOBJECT, get_class($myObject), DOM_XMLRPC_TYPE_STRING);
 195  
 196                  $serialized =& serialize($myObject);
 197                  $currValue =& $this->createBase64($serialized);
 198                  $data .= $this->createMember(DOM_XMLRPC_SERIALIZED, $currValue, DOM_XMLRPC_TYPE_BASE64);
 199  
 200                  $data .= '</struct></value>';
 201                  break;
 202          } // switch
 203  
 204          return $data;
 205      } //createObject
 206  
 207      /**
 208      * Generates an XML-RPC param from the object and adds it to the method params
 209      * @param object The object to be converted to XML-RPC notation
 210      */
 211  	function addObject($myObject) {
 212          $this->addObjectByRef($myObject);
 213      }//addObject
 214  
 215      /**
 216      * Generates an XML-RPC param from the object reference and adds it to the method params
 217      * @param object The object reference to be converted to XML-RPC notation
 218      */
 219  	function addObjectByRef(&$myObject) { //can call with reference if deep copy is required
 220          $this->params .= "\n\t\t<param>" . $this->createObject($myObject)  . '</param>';
 221      }//addObjectByRef
 222  
 223      /**
 224      * Creates an XML-RPC representation of a struct
 225      * @param mixed The struct to be converted to XML-RPC notation
 226      * @return string An XML-RPC representation of the struct
 227      */
 228  	function createStruct(&$myStruct) {
 229          require_once ('dom_xmlrpc_object.php');
 230          $className = get_class($myStruct);
 231  
 232          //if struct is explicitly cast as an xmlrpc object
 233          if ($className == 'dom_xmlrpc_object') {
 234              $myObject =& $myStruct->getObject(); //grab embedded object
 235              return $this->createObject($myObject);
 236          }
 237          else {
 238              //wrapper for numeric indexed structs
 239              //that are meant to be string indexed
 240              if ($className == 'dom_xmlrpc_struct') {
 241                  $myStruct =& $myStruct->getStruct(); //grab embedded array
 242              }
 243  
 244              $isArrayNotObj = is_array($myStruct);
 245  
 246              //if struct is an object and isn't anonymous
 247              if ($isArrayNotObj && ($this->objectMarshalling != DOM_XMLRPC_OBJECT_MARSHALLING_ANONYMOUS)) {
 248                  return $this->createObject($myStruct);
 249              }
 250              else {
 251                  $data = '<value><struct>';
 252  
 253                   foreach ($myStruct as $key => $value) {
 254                       $isArrayNotObj ? ($currValue =& $myStruct[$key]) : ($currValue =& $value);
 255                       $currType = dom_xmlrpc_utilities::getTypeFromValue($currValue);
 256  
 257                       $data .= $this->createMember($key, $currValue, $currType);
 258                   }
 259  
 260                   $data .= '</struct></value>';
 261                   return $data;
 262              }
 263          }
 264      } //createStruct
 265  
 266      /**
 267      * Generates an XML-RPC param from the struct and adds it to the method params
 268      * @param mixed The struct to be converted to XML-RPC notation
 269      */
 270  	function addStruct($myStruct) {
 271          $this->addStructByRef($myStruct);
 272      } //addStruct
 273  
 274      /**
 275      * Generates an XML-RPC param from the struct reference and adds it to the method params
 276      * @param mixed The struct reference to be converted to XML-RPC notation
 277      */
 278  	function addStructByRef(&$myStruct) { //can call with reference if deep copy is required
 279          $this->params .= "\n\t\t<param>" . $this->createStruct($myStruct)  . '</param>';
 280      } //addStructRef
 281  
 282      /**
 283      * Creates an XML-RPC representation of a member
 284      * @param mixed The member to be converted to XML-RPC notation
 285      * @return string An XML-RPC representation of the member
 286      */
 287  	function createMember($name, &$value, $type) {
 288          $data = '<member><name>' . $name . '</name>';
 289  
 290          if ($type == '') {
 291              $type =  dom_xmlrpc_utilities::getScalarTypeFromValue($value);
 292          }
 293  
 294          $data .= $this->create($value, $type) . '</member>';
 295  
 296          return $data;
 297      } //createMember
 298  
 299      /**
 300      * Generates an XML-RPC param from the value and adds it to the method params
 301      * @param mixed The value to be converted to XML-RPC notation
 302      */
 303  	function create(&$value, $type = '') {
 304          $data = '';
 305  
 306          if ($type == '') {
 307              require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_utilities.php');
 308              $type = dom_xmlrpc_utilities::getTypeFromValue($value);
 309          }
 310  
 311          switch ($type) {
 312              case DOM_XMLRPC_TYPE_STRING:
 313              case DOM_XMLRPC_TYPE_INT:
 314              case DOM_XMLRPC_TYPE_I4:
 315              case DOM_XMLRPC_TYPE_DOUBLE:
 316              case DOM_XMLRPC_TYPE_BOOLEAN:
 317              case DOM_XMLRPC_TYPE_BASE64:
 318              case DOM_XMLRPC_TYPE_DATETIME:
 319                  $data .= $this->createScalar($value, $type);
 320                  break;
 321              case DOM_XMLRPC_TYPE_STRUCT:
 322                  $data .= $this->createStruct($value, $type);
 323                  break;
 324              case DOM_XMLRPC_TYPE_ARRAY:
 325                  $data .= $this->createArray($value, $type);
 326                  break;
 327          }
 328  
 329          return $data;
 330      } //create
 331  
 332      /**
 333      * Generates an XML-RPC param from the unspecified value
 334      * @param mixed The value to be converted to XML-RPC notation
 335      */
 336  	function add($value, $type = '') {
 337          $this->addByRef($value, $type);
 338      } //add
 339  
 340      /**
 341      * Generates an XML-RPC param from the unspecified reference
 342      * @param mixed The reference to be converted to XML-RPC notation
 343      */
 344  	function addByRef(&$value, $type = '') { //can call with reference if deep copy is required
 345          $this->params .= "\n\t\t<param>" . $this->create($value, $type)  . '</param>';
 346      } //addByRef
 347  
 348      /**
 349      * Generates an XML-RPC param from list of values
 350      */
 351  	function addList() {
 352          $total = func_num_args();
 353  
 354          for ($i = 0; $i < $total; $i++) {
 355              $this->add(func_get_arg($i));
 356          }
 357      } //addList
 358  
 359      /**
 360      * Creates a dom_xmlrpc_base64 object from the binary data
 361      * @param mixed The binary data
 362      * @return object The base64 encoded data
 363      */
 364      function &createBase64($binaryData) {
 365          require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_base64.php');
 366  
 367          $base64 = new dom_xmlrpc_base64();
 368          $base64->fromBinary($binaryData);
 369          return $base64;
 370      } //createBase64
 371  
 372      /**
 373      * Creates a dom_xmlrpc_base64 object from the file
 374      * @param string The file path
 375      * @return object The base64 encoded data
 376      */
 377      function &createBase64FromFile($fileName) {
 378          require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_base64.php');
 379  
 380          $base64 = new dom_xmlrpc_base64();
 381          $base64->fromFile($fileName);
 382          return $base64;
 383      } //createBase64FromFile
 384  
 385      /**
 386      * Creates a dom_xmlrpc_datetime_iso8601 object from the PHP time
 387      * @param string The time
 388      * @return object The dom_xmlrpc_datetime_iso8601 object
 389      */
 390      function &createDateTimeISO($time) {
 391          require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_datetime_iso8601.php');
 392  
 393          $isoDateTime = new dom_xmlrpc_datetime_iso8601($time);
 394  
 395          return $isoDateTime;
 396      } //createDateTimeISO
 397  
 398  } //dom_xmlrpc_builder
 399  ?>


Généré le : Wed Nov 21 14:43:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics