[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Horde_RPC:: class provides a set of server and client methods for 4 * RPC communication. 5 * 6 * TODO: 7 * - Introspection documentation and method signatures. 8 * 9 * EXAMPLE: 10 * <code> 11 * $response = Horde_RPC::request('xmlrpc', 12 * 'http://localhost:80/horde/rpc.php' 13 * 'contacts.search', 14 * array(array('jan'), array('localsql'), 15 * array('name', 'email')), 16 * array('user' => Auth::getAuth(), 17 * 'pass' => Auth::getCredential('password'))); 18 * </code> 19 * 20 * $Horde: framework/RPC/RPC.php,v 1.7.10.8 2006/01/01 21:28:33 jan Exp $ 21 * 22 * Copyright 2002-2006 Jan Schneider <jan@horde.org> 23 * 24 * See the enclosed file COPYING for license information (LGPL). If you 25 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 26 * 27 * @author Jan Schneider <jan@horde.org> 28 * @since Horde 3.0 29 * @package Horde_RPC 30 */ 31 class Horde_RPC { 32 33 /** 34 * Whether we need an authorized user or not. 35 * 36 * @access protected 37 * @var boolean 38 */ 39 var $_authorize = true; 40 41 /** 42 * RPC server constructor 43 * 44 * @access private 45 * @return object An RPC server instance. 46 */ 47 function Horde_RPC() 48 { 49 register_shutdown_function(array($this, 'shutdown')); 50 } 51 52 /** 53 * Cleans up the RPC server. 54 * 55 * @abstract 56 */ 57 function shutdown() 58 { 59 } 60 61 /** 62 * Check authentication. Different backends may handle 63 * authentication in different ways. The base class implementation 64 * checks for HTTP Authentication against the Horde auth setup. 65 * 66 * @return boolean Returns true if authentication is successful. 67 * Should send appropriate "not authorized" headers 68 * or other response codes/body if auth fails, 69 * and take care of exiting. 70 */ 71 function authorize() 72 { 73 if (!$this->_authorize) { 74 return true; 75 } 76 77 $auth = &Auth::singleton($GLOBALS['conf']['auth']['driver']); 78 79 if (isset($_SERVER['PHP_AUTH_USER'])) { 80 $user = $_SERVER['PHP_AUTH_USER']; 81 $pass = $_SERVER['PHP_AUTH_PW']; 82 } 83 84 if (!isset($user) 85 || !$auth->authenticate($user, array('password' => $pass))) { 86 header('WWW-Authenticate: Basic realm="Horde RPC"'); 87 header('HTTP/1.0 401 Unauthorized'); 88 echo '401 Unauthorized'; 89 exit; 90 } 91 92 return true; 93 } 94 95 /** 96 * Get the request body input. Different RPC backends can override 97 * this to return an open stream to php://stdin, for instance - 98 * whatever is easiest to handle in the getResponse() method. 99 * 100 * The base class implementation looks for $HTTP_RAW_POST_DATA and 101 * returns that if it's available; otherwise, it returns the 102 * contents of php://stdin. 103 * 104 * @return mixed The input - a string (default), a filehandle, etc. 105 */ 106 function getInput() 107 { 108 if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) { 109 return $GLOBALS['HTTP_RAW_POST_DATA']; 110 } else { 111 return implode("\r\n", file('php://input')); 112 } 113 } 114 115 /** 116 * Sends an RPC request to the server and returns the result. 117 * 118 * @param string The raw request string. 119 * 120 * @return string The XML encoded response from the server. 121 */ 122 function getResponse($request) 123 { 124 return _("not implemented"); 125 } 126 127 /** 128 * Get the Content-Type of the response. 129 * 130 * @return string The MIME Content-Type of the RPC response. 131 */ 132 function getResponseContentType() 133 { 134 return 'text/xml'; 135 } 136 137 /** 138 * Builds an RPC request and sends it to the RPC server. 139 * 140 * This statically called method is actually the RPC client. 141 * 142 * @param string $driver The protocol driver to use. Currently 'soap' 143 * and 'xmlrpc' are available. 144 * @param string $url The path to the RPC server on the called host. 145 * @param string $method The method to call. 146 * @param array $params A hash containing any necessary parameters for 147 * the method call. 148 * @param $options Associative array of parameters depending on 149 * the selected protocol driver. 150 * 151 * @return mixed The returned result from the method or a PEAR 152 * error object on failure. 153 */ 154 function request($driver, $url, $method, $params = null, $options = array()) 155 { 156 if (is_array($driver)) { 157 list($app, $driver) = $driver; 158 } 159 160 $driver = basename($driver); 161 162 if (!empty($app)) { 163 require_once $app . '/lib/RPC/' . $driver . '.php'; 164 } elseif (@file_exists(dirname(__FILE__) . '/RPC/' . $driver . '.php')) { 165 require_once dirname(__FILE__) . '/RPC/' . $driver . '.php'; 166 } else { 167 @include_once 'Horde/RPC/' . $driver . '.php'; 168 } 169 $class = 'Horde_RPC_' . $driver; 170 if (class_exists($class)) { 171 return call_user_func(array($class, 'request'), $url, $method, $params, $options); 172 } else { 173 require_once 'PEAR.php'; 174 return PEAR::raiseError('Class definition of ' . $class . ' not found.'); 175 } 176 } 177 178 /** 179 * Attempts to return a concrete RPC server instance based on 180 * $driver. 181 * 182 * @param mixed $driver The type of concrete RPC subclass to return. If 183 * $driver is an array, then we will look in 184 * $driver[0]/lib/RPC/ for the subclass 185 * implementation named $driver[1].php. 186 * @param array $params A hash containing any additional configuration or 187 * connection parameters a subclass might need. 188 * 189 * @return Horde_RPC The newly created concrete Horde_RPC server instance, 190 * or a PEAR_Error on an error. 191 */ 192 function &factory($driver, $params = null) 193 { 194 if (is_array($driver)) { 195 list($app, $driver) = $driver; 196 } 197 198 $driver = basename($driver); 199 200 if (!empty($app)) { 201 require_once $app . '/lib/RPC/' . $driver . '.php'; 202 } elseif (@file_exists(dirname(__FILE__) . '/RPC/' . $driver . '.php')) { 203 require_once dirname(__FILE__) . '/RPC/' . $driver . '.php'; 204 } else { 205 @include_once 'Horde/RPC/' . $driver . '.php'; 206 } 207 $class = 'Horde_RPC_' . $driver; 208 if (class_exists($class)) { 209 $rpc = &new $class($params); 210 } else { 211 require_once 'PEAR.php'; 212 $rpc = PEAR::raiseError('Class definition of ' . $class . ' not found.'); 213 } 214 215 return $rpc; 216 } 217 218 /** 219 * Attempts to return a reference to a concrete RPC server 220 * instance based on $driver. It will only create a new instance 221 * if no RPC server instance with the same parameters currently 222 * exists. 223 * 224 * This should be used if multiple RPC servers (and thus, multiple RPC 225 * server instances) are required. 226 * 227 * This method must be invoked as: $var = &Horde_RPC::singleton() 228 * 229 * @param string $driver The type of concrete RPC subclass to return. 230 * @param array $params A hash containing any additional configuration or 231 * connection parameters a subclass might need. 232 * 233 * @return Horde_RPC The concrete Horde_RPC server reference, or a 234 * PEAR_Error on an error. 235 */ 236 function &singleton($driver, $params = null) 237 { 238 static $instances; 239 240 if (!isset($instances)) { 241 $instances = array(); 242 } 243 244 $signature = serialize(array($driver, $params)); 245 if (!array_key_exists($signature, $instances)) { 246 $instances[$signature] = &Horde_RPC::factory($driver, $params); 247 } 248 249 return $instances[$signature]; 250 } 251 252 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |