|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00024 require_once 'Zend/XmlRpc/Value.php'; 00025 00029 require_once 'Zend/XmlRpc/Fault.php'; 00030 00048 class Zend_XmlRpc_Request 00049 { 00054 protected $_encoding = 'UTF-8'; 00055 00060 protected $_method; 00061 00066 protected $_xml; 00067 00072 protected $_params = array(); 00073 00078 protected $_fault = null; 00079 00084 protected $_types = array(); 00085 00090 protected $_xmlRpcParams = array(); 00091 00098 public function __construct($method = null, $params = null) 00099 { 00100 if ($method !== null) { 00101 $this->setMethod($method); 00102 } 00103 00104 if ($params !== null) { 00105 $this->setParams($params); 00106 } 00107 } 00108 00109 00116 public function setEncoding($encoding) 00117 { 00118 $this->_encoding = $encoding; 00119 Zend_XmlRpc_Value::setEncoding($encoding); 00120 return $this; 00121 } 00122 00128 public function getEncoding() 00129 { 00130 return $this->_encoding; 00131 } 00132 00139 public function setMethod($method) 00140 { 00141 if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) { 00142 $this->_fault = new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")'); 00143 $this->_fault->setEncoding($this->getEncoding()); 00144 return false; 00145 } 00146 00147 $this->_method = $method; 00148 return true; 00149 } 00150 00156 public function getMethod() 00157 { 00158 return $this->_method; 00159 } 00160 00171 public function addParam($value, $type = null) 00172 { 00173 $this->_params[] = $value; 00174 if (null === $type) { 00175 // Detect type if not provided explicitly 00176 if ($value instanceof Zend_XmlRpc_Value) { 00177 $type = $value->getType(); 00178 } else { 00179 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value); 00180 $type = $xmlRpcValue->getType(); 00181 } 00182 } 00183 $this->_types[] = $type; 00184 $this->_xmlRpcParams[] = array('value' => $value, 'type' => $type); 00185 } 00186 00208 public function setParams() 00209 { 00210 $argc = func_num_args(); 00211 $argv = func_get_args(); 00212 if (0 == $argc) { 00213 return; 00214 } 00215 00216 if ((1 == $argc) && is_array($argv[0])) { 00217 $params = array(); 00218 $types = array(); 00219 $wellFormed = true; 00220 foreach ($argv[0] as $arg) { 00221 if (!is_array($arg) || !isset($arg['value'])) { 00222 $wellFormed = false; 00223 break; 00224 } 00225 $params[] = $arg['value']; 00226 00227 if (!isset($arg['type'])) { 00228 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']); 00229 $arg['type'] = $xmlRpcValue->getType(); 00230 } 00231 $types[] = $arg['type']; 00232 } 00233 if ($wellFormed) { 00234 $this->_xmlRpcParams = $argv[0]; 00235 $this->_params = $params; 00236 $this->_types = $types; 00237 } else { 00238 $this->_params = $argv[0]; 00239 $this->_types = array(); 00240 $xmlRpcParams = array(); 00241 foreach ($argv[0] as $arg) { 00242 if ($arg instanceof Zend_XmlRpc_Value) { 00243 $type = $arg->getType(); 00244 } else { 00245 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg); 00246 $type = $xmlRpcValue->getType(); 00247 } 00248 $xmlRpcParams[] = array('value' => $arg, 'type' => $type); 00249 $this->_types[] = $type; 00250 } 00251 $this->_xmlRpcParams = $xmlRpcParams; 00252 } 00253 return; 00254 } 00255 00256 $this->_params = $argv; 00257 $this->_types = array(); 00258 $xmlRpcParams = array(); 00259 foreach ($argv as $arg) { 00260 if ($arg instanceof Zend_XmlRpc_Value) { 00261 $type = $arg->getType(); 00262 } else { 00263 $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg); 00264 $type = $xmlRpcValue->getType(); 00265 } 00266 $xmlRpcParams[] = array('value' => $arg, 'type' => $type); 00267 $this->_types[] = $type; 00268 } 00269 $this->_xmlRpcParams = $xmlRpcParams; 00270 } 00271 00277 public function getParams() 00278 { 00279 return $this->_params; 00280 } 00281 00287 public function getTypes() 00288 { 00289 return $this->_types; 00290 } 00291 00298 public function loadXml($request) 00299 { 00300 if (!is_string($request)) { 00301 $this->_fault = new Zend_XmlRpc_Fault(635); 00302 $this->_fault->setEncoding($this->getEncoding()); 00303 return false; 00304 } 00305 00306 try { 00307 $xml = new SimpleXMLElement($request); 00308 } catch (Exception $e) { 00309 // Not valid XML 00310 $this->_fault = new Zend_XmlRpc_Fault(631); 00311 $this->_fault->setEncoding($this->getEncoding()); 00312 return false; 00313 } 00314 00315 // Check for method name 00316 if (empty($xml->methodName)) { 00317 // Missing method name 00318 $this->_fault = new Zend_XmlRpc_Fault(632); 00319 $this->_fault->setEncoding($this->getEncoding()); 00320 return false; 00321 } 00322 00323 $this->_method = (string) $xml->methodName; 00324 00325 // Check for parameters 00326 if (!empty($xml->params)) { 00327 $types = array(); 00328 $argv = array(); 00329 foreach ($xml->params->children() as $param) { 00330 if (!isset($param->value)) { 00331 $this->_fault = new Zend_XmlRpc_Fault(633); 00332 $this->_fault->setEncoding($this->getEncoding()); 00333 return false; 00334 } 00335 00336 try { 00337 $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING); 00338 $types[] = $param->getType(); 00339 $argv[] = $param->getValue(); 00340 } catch (Exception $e) { 00341 $this->_fault = new Zend_XmlRpc_Fault(636); 00342 $this->_fault->setEncoding($this->getEncoding()); 00343 return false; 00344 } 00345 } 00346 00347 $this->_types = $types; 00348 $this->_params = $argv; 00349 } 00350 00351 $this->_xml = $request; 00352 00353 return true; 00354 } 00355 00362 public function isFault() 00363 { 00364 return $this->_fault instanceof Zend_XmlRpc_Fault; 00365 } 00366 00372 public function getFault() 00373 { 00374 return $this->_fault; 00375 } 00376 00382 protected function _getXmlRpcParams() 00383 { 00384 $params = array(); 00385 if (is_array($this->_xmlRpcParams)) { 00386 foreach ($this->_xmlRpcParams as $param) { 00387 $value = $param['value']; 00388 $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE; 00389 00390 if (!$value instanceof Zend_XmlRpc_Value) { 00391 $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type); 00392 } 00393 $params[] = $value; 00394 } 00395 } 00396 00397 return $params; 00398 } 00399 00405 public function saveXml() 00406 { 00407 $args = $this->_getXmlRpcParams(); 00408 $method = $this->getMethod(); 00409 00410 $generator = Zend_XmlRpc_Value::getGenerator(); 00411 $generator->openElement('methodCall') 00412 ->openElement('methodName', $method) 00413 ->closeElement('methodName'); 00414 00415 if (is_array($args) && count($args)) { 00416 $generator->openElement('params'); 00417 00418 foreach ($args as $arg) { 00419 $generator->openElement('param'); 00420 $arg->generateXml(); 00421 $generator->closeElement('param'); 00422 } 00423 $generator->closeElement('params'); 00424 } 00425 $generator->closeElement('methodCall'); 00426 00427 return $generator->flush(); 00428 } 00429 00435 public function __toString() 00436 { 00437 return $this->saveXML(); 00438 } 00439 }