| [ Index ] |
|
Code source de Joomla 1.0.13 |
1 <?php 2 /** 3 * dom_xmlrpc_client provides basic XML-RPC client functionality 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 //must change these to match the XML-RPC error spec 17 /** invalid method response type error */ 18 define('XMLRPC_CLIENT_RESPONSE_TYPE_ERR', 1); 19 /** malformed XML response error */ 20 define('XMLRPC_CLIENT_MALFORMED_XML_ERR', 2); 21 22 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_methodcall.php'); 23 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_constants.php'); 24 require_once (DOM_XMLRPC_INCLUDE_PATH . 'php_http_client_generic.php'); 25 26 /** 27 * Provides basic XML-RPC client functionality 28 * 29 * @package dom-xmlrpc 30 * @author John Heinstein <johnkarl@nbnet.nb.ca> 31 */ 32 class dom_xmlrpc_client extends php_http_client_generic { 33 /** @var string The method response type requested by the client */ 34 var $responseType = DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT; 35 /** @var boolean True if multiple method calls are to be made in a single request */ 36 var $isMultiCall = false; 37 38 /** 39 * XML-RPC Client constructor 40 * @param string The client connection host name, with or without its protocol prefix 41 * @param string The client connection path, not including the host name 42 * @param int The port to establish the client connection on 43 * @param string A proxy connection host name, with or without its protocol prefix 44 * @param int The timeout value for the client connection 45 */ 46 function dom_xmlrpc_client ($host = '', $path = '/', $port = 80, $proxy = '', $timeout = 0) { 47 if ($proxy != '') { 48 $host = $proxy; 49 } 50 51 $this->php_http_client_generic($host, $path, $port, $timeout); 52 $this->setHeaders(); 53 } //dom_xmlrpc_client 54 55 /** 56 * Sets the headers for the client connection 57 */ 58 function setHeaders() { 59 $this->setHeader('Content-Type', 'text/xml'); 60 $this->setHeader('Host', $this->connection->host); 61 $this->setHeader('User-Agent', 'DOM XML-RPC Client/0.1'); 62 $this->setHeader('Connection', 'close'); 63 } //setHeaders 64 65 /** 66 * Determines whether message is multicall 67 * @param mixed The message 68 * @return mixed The evaluated message 69 */ 70 function evaluateMessage(&$message) { 71 if (!is_string($message)) { 72 if ($message->methodName == 'system.multicall') $this->isMultiCall = true; 73 return $message->toXML(); 74 } 75 else { 76 if (strpos($message, 'system.multicall') !== false) $this->isMultiCall = true; 77 } 78 79 return $message; 80 } //evaluateMessage 81 82 /** 83 * Sends data through the client connection 84 * @param string The message to be sent 85 * @return string The http response 86 */ 87 function &send(&$message) { 88 if (!$this->isConnected()) { 89 $this->connect(); 90 } 91 92 $message = $this->evaluateMessage($message); 93 $this->setHeader('Content-Length', strlen($message)); 94 95 $response =& parent::send($message); 96 97 return $this->formatResponse($response->getResponse()); 98 } //send 99 100 /** 101 * Sends data through the client connection and disconnects 102 * @param string The message to be sent 103 * @return string The http response 104 */ 105 function &sendAndDisconnect($message) { 106 $response =& $this->send($message); 107 $this->disconnect(); 108 return $response; 109 } //send 110 111 /** 112 * Returns the message response, formatted according to the specified response type 113 * @param string The unformatted response 114 * @return string The formatted response 115 */ 116 function &formatResponse($response) { 117 switch ($this->responseType) { 118 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT: 119 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT_LITE: 120 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMXML: 121 return $this->returnAsXML($response); 122 break; 123 124 case DOM_XMLRPC_RESPONSE_TYPE_ARRAY: 125 return $this->returnAsArray($response); 126 break; 127 128 case DOM_XMLRPC_RESPONSE_TYPE_STRING: 129 return $response; 130 break; 131 } 132 } //formatResponse 133 134 /** 135 * Returns the message response as an XML document of the specified type 136 * @param string The unformatted response 137 * @return string An XML document representing the method response 138 */ 139 function &returnAsXML($response) { 140 switch ($this->responseType) { 141 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT: 142 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_domit_parser.php' ); 143 $xmlrpcDoc = new dom_xmlrpc_domit_document(); 144 $success = $xmlrpcDoc->parseXML($response, false); 145 break; 146 147 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT_LITE: 148 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_domit_lite_parser.php' ); 149 $xmlrpcDoc = new dom_xmlrpc_domit_lite_document(); 150 $success = $xmlrpcDoc->parseXML($response, false); 151 break; 152 153 case DOM_XMLRPC_RESPONSE_TYPE_XML_XML_DOMXML: 154 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_domxml_parser.php' ); 155 $xmlrpcDoc = new dom_xmlrpc_domxml_document(); 156 $success = $xmlrpcDoc->parseXML($response); 157 break; 158 } 159 160 if ($success) { 161 return $xmlrpcDoc; 162 } 163 164 XMLRPC_Client_Exception::raiseException(XMLRPC_CLIENT_MALFORMED_XML_ERR, 165 ("Malformed xml returned: \n $response")); 166 } //returnAsXML 167 168 /** 169 * Returns the message response as a PHP array 170 * @param string The unformatted response 171 * @return string A PHP array representation of the method response 172 */ 173 function &returnAsArray($response) { 174 require_once (DOM_XMLRPC_INCLUDE_PATH . 'dom_xmlrpc_array_parser.php'); 175 176 $arrayParser = new dom_xmlrpc_array_parser(); 177 178 if ($arrayParser->parseXML($response, false)) { 179 return $arrayParser->getArrayDocument(); 180 } 181 else { 182 XMLRPC_Client_Exception::raiseException(XMLRPC_CLIENT_MALFORMED_XML_ERR, 183 ("Malformed xml returned: \n $response")); 184 } 185 } //returnAsArray 186 187 /** 188 * Converts PHP arrays to stdclass objects 189 * @param array The PHP array 190 * @return object A stdclass object 191 */ 192 function &arraysToObjects(&$myArray) { 193 foreach ($myArray as $key => $value) { 194 $currItem =& $myArray[$key]; 195 196 if (is_array($currItem)) { 197 $currItem =& $this->arraysToObjects($currItem); 198 } 199 } 200 201 if (dom_xmlrpc_utilities::isAssociativeArray($myArray)) { 202 $obj = new stdclass(); 203 204 foreach ($myArray as $key => $value) { 205 $obj->$key =& $myArray[$value]; 206 } 207 208 return $obj; 209 } 210 else { 211 return $myArray; 212 } 213 } //arraysToObjects 214 215 /** 216 * Sets the method response type to the specified type 217 * @param array The requested method response type 218 */ 219 function setResponseType($type) { 220 $type = strtolower($type); 221 222 switch ($type) { 223 case DOM_XMLRPC_RESPONSE_TYPE_ARRAY: 224 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT: 225 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMIT_LITE: 226 case DOM_XMLRPC_RESPONSE_TYPE_XML_DOMXML: 227 case DOM_XMLRPC_RESPONSE_TYPE_STRING: 228 $this->responseType = $type; 229 break; 230 default: 231 XMLRPC_Client_Exception::raiseException(XMLRPC_CLIENT_RESPONSE_TYPE_ERR, 232 ('Invalid response type: ' . $type)); 233 } 234 } //setResponseType 235 236 /** 237 * Returns the current method response type 238 * @return string The current method response type 239 */ 240 function getResponseType() { 241 return $this->responseType; 242 } //getResponseType 243 } //dom_xmlrpc_client 244 245 246 /** 247 * An XML-RPC Client excpetion class 248 * 249 * @package dom-xmlrpc 250 * @author John Heinstein <johnkarl@nbnet.nb.ca> 251 */ 252 class XMLRPC_Client_Exception { 253 254 /** 255 * Raises the specified exception 256 * @param int The error number 257 * @param int The error string 258 */ 259 function raiseException($errorNum, $errorString) { 260 $errorMessage = $errorNum . "\n " . $errorString; 261 262 if ((!isset($GLOBALS['DOMIT_XMLRPC_ERROR_FORMATTING_HTML'])) || 263 ($GLOBALS['DOMIT_XMLRPC_ERROR_FORMATTING_HTML'] == true)) { 264 $errorMessage = "<p><pre>" . $errorMessage . "</pre></p>"; 265 } 266 267 die($errorMessage); 268 } //raiseException 269 } //XMLRPC_Client_Exception 270 271 272 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Wed Nov 21 14:43:32 2007 | par Balluche grâce à PHPXref 0.7 |
|