/usr/share/php/XML/RPC2/CachedServer.php is in php-xml-rpc2 1.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | <?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
/**
* +-----------------------------------------------------------------------------+
* | Copyright (c) 2004 Sérgio Gonçalves Carvalho |
* +-----------------------------------------------------------------------------+
* | This file is part of XML_RPC2. |
* | |
* | XML_RPC2 is free software; you can redistribute it and/or modify |
* | it under the terms of the GNU Lesser General Public License as published by |
* | the Free Software Foundation; either version 2.1 of the License, or |
* | (at your option) any later version. |
* | |
* | XML_RPC2 is distributed in the hope that it will be useful, |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* | GNU Lesser General Public License for more details. |
* | |
* | You should have received a copy of the GNU Lesser General Public License |
* | along with XML_RPC2; if not, write to the Free Software |
* | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
* | 02111-1307 USA |
* +-----------------------------------------------------------------------------+
* | Author: Sérgio Carvalho <sergio.carvalho@portugalmail.com> |
* +-----------------------------------------------------------------------------+
*
* @category XML
* @package XML_RPC2
* @author Fabien MARTY <fab@php.net>
* @copyright 2005-2006 Fabien MARTY
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id$
* @link http://pear.php.net/package/XML_RPC2
*/
// }}}
// dependencies {{{
require_once('Cache/Lite.php');
// }}}
/**
* XML_RPC "cached server" class.
*
* @category XML
* @package XML_RPC2
* @author Fabien MARTY <fab@php.net>
* @copyright 2005-2006 Fabien MARTY
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear.php.net/package/XML_RPC2
*/
class XML_RPC2_CachedServer {
// {{{ properties
/**
* cache by default
*
* @var boolean
*/
private $_cacheByDefault = true;
/**
* Cache_Lite object
*
* @var object
*/
private $_cacheObject = null;
/**
* XML_RPC2_Server object (if needed, dynamically built)
*
* @var object
*/
private $_serverObject = null;
/**
* Default cache group for XML_RPC server caching
*
* @var string
*/
private $_defaultCacheGroup = 'xml_rpc2_server';
/**
* callHandler field
*
* The call handler is responsible for executing the server exported methods
*
* @var mixed
*/
private $_callHandler = null;
/**
* either a class name or an object instance
*
* @var mixed
*/
private $_callTarget = '';
/**
* methods prefix
*
* @var string
*/
private $_prefix = '';
/**
* XML_RPC2_Server options
*
* @var array
*/
private $_options = array();
/**
* "cache debug" flag (for debugging the caching process)
*
* @var boolean
*/
private $_cacheDebug = false;
/**
* encoding
*
* @var string
*/
private $_encoding = 'utf-8';
// }}}
// {{{ setCacheOptions()
/**
* Set options for the caching process
*
* See Cache_Lite constructor for options
* Specific options are 'cachedMethods', 'notCachedMethods', 'cacheByDefault', 'defaultCacheGroup'
* See corresponding properties for more informations
*
* @param array $array
*/
private function _setCacheOptions($array)
{
if (isset($array['defaultCacheGroup'])) {
$this->_defaultCacheGroup = $array['defaultCacheGroup'];
unset($array['defaultCacheGroup']); // this is a "non standard" option for Cache_Lite
}
if (isset($array['cacheByDefault'])) {
$this->_cacheByDefault = $array['cacheByDefault'];
unset($array['CacheByDefault']); // this is a "non standard" option for Cache_Lite
}
$array['automaticSerialization'] = false; // datas are already serialized in this class
if (!isset($array['lifetime'])) {
$array['lifetime'] = 3600; // we need a default lifetime
}
$this->_cacheOptions = $array;
$this->_cacheObject = new Cache_Lite($this->_cacheOptions);
}
// }}}
// {{{ constructor
/**
* Constructor
*
* @param object $callHandler the call handler will receive a method call for each remote call received.
*/
protected function __construct($callTarget, $options = array())
{
if (isset($options['cacheOptions'])) {
$cacheOptions = $options['cacheOptions'];
$this->_setCacheOptions($cacheOptions);
unset($options['cacheOptions']);
}
if (isset($options['cacheDebug'])) {
$this->_cacheDebug = $options['cacheDebug'];
unset($options['cacheDebug']); // 'cacheDebug' is not a standard option for XML/RPC2/Server
}
$this->_options = $options;
$this->_callTarget = $callTarget;
if (isset($this->_options['encoding'])) {
$this->_encoding = $this->_options['encoding'];
}
if (isset($this->_options['prefix'])) {
$this->_prefix = $this->_options['prefix'];
}
}
// }}}
// {{{ create()
/**
* "Emulated Factory" method to get the same API than XML_RPC2_Server class
*
* Here, simply returns a new instance of XML_RPC2_CachedServer class
*
* @param mixed $callTarget either a class name or an object instance.
* @param array $options associative array of options
* @return object a server class instance
*/
public static function create($callTarget, $options = array())
{
return new XML_RPC2_CachedServer($callTarget, $options);
}
// }}}
// {{{ handleCall()
/**
* handle XML_RPC calls
*
*/
public function handleCall()
{
$response = $this->getResponse();
$encoding = 'utf-8';
if (isset($this->_options['encoding'])) {
$encoding = $this->_options['encoding'];
}
header('Content-type: text/xml; charset=' . $encoding);
header('Content-length: ' . $this->getContentLength($response));
print $response;
}
/**
* get the XML response of the XMLRPC server
*
* @return string the XML response
*/
public function getResponse()
{
if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
$methodName = $this->_parseMethodName($GLOBALS['HTTP_RAW_POST_DATA']);
} else {
$methodName = null;
}
$weCache = $this->_cacheByDefault;
$lifetime = $this->_cacheOptions['lifetime'];
if ($this->_cacheDebug) {
if ($weCache) {
print "CACHE DEBUG : default values => weCache=true, lifetime=$lifetime\n";
} else {
print "CACHE DEBUG : default values => weCache=false, lifetime=$lifetime\n";
}
}
if ($methodName) {
// work on reflection API to search for @xmlrpc.caching tags into PHPDOC comments
list($weCache, $lifetime) = $this->_reflectionWork($methodName);
if ($this->_cacheDebug) {
if ($weCache) {
print "CACHE DEBUG : phpdoc comments => weCache=true, lifetime=$lifetime\n";
} else {
print "CACHE DEBUG : phpdoc comments => weCache=false, lifetime=$lifetime\n";
}
}
}
if (($weCache) and ($lifetime!=-1)) {
if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
$cacheId = $this->_makeCacheId($GLOBALS['HTTP_RAW_POST_DATA']);
} else {
$cacheId = 'norawpostdata';
}
$this->_cacheObject = new Cache_Lite($this->_cacheOptions);
$this->_cacheObject->setLifetime($lifetime);
if ($data = $this->_cacheObject->get($cacheId, $this->_defaultCacheGroup)) {
// cache id hit
if ($this->_cacheDebug) {
print "CACHE DEBUG : cache is hit !\n";
}
} else {
// cache is not hit
if ($this->_cacheDebug) {
print "CACHE DEBUG : cache is not hit !\n";
}
$data = $this->_workWithoutCache();
$this->_cacheObject->save($data);
}
} else {
if ($this->_cacheDebug) {
print "CACHE DEBUG : we don't cache !\n";
}
$data = $this->_workWithoutCache();
}
return $data;
}
// }}}
// {{{ _reflectionWork()
/**
* Work on reflection API to search for @xmlrpc.caching tags into PHPDOC comments
*
* @param string $methodName method name
* @return array array((boolean) weCache, (int) lifetime) => parameters to use for caching
*/
private function _reflectionWork($methodName) {
$weCache = $this->_cacheByDefault;
$lifetime = $this->_cacheOptions['lifetime'];
if (is_string($this->_callTarget)) {
$className = strtolower($this->_callTarget);
} else {
$className = get_class($this->_callTarget);
}
$class = new ReflectionClass($className);
$method = $class->getMethod($methodName);
$docs = explode("\n", $method->getDocComment());
foreach ($docs as $i => $doc) {
$doc = trim($doc, " \r\t/*");
$res = preg_match('/@xmlrpc.caching ([+-]{0,1}[a-zA-Z0-9]*)/', $doc, $results); // TODO : better/faster regexp ?
if ($res>0) {
$value = $results[1];
if (($value=='yes') or ($value=='true') or ($value=='on')) {
$weCache = true;
} else if (($value=='no') or ($value=='false') or ($value=='off')) {
$weCache = false;
} else {
$lifetime = (int) $value;
if ($lifetime==-1) {
$weCache = false;
} else {
$weCache = true;
}
}
}
}
return array($weCache, $lifetime);
}
// }}}
// {{{ _parseMethodName()
/**
* Parse the method name from the raw XMLRPC client request
*
* NB : the prefix is removed from the method name
*
* @param string $request raw XMLRPC client request
* @return string method name
*/
private function _parseMethodName($request)
{
// TODO : change for "simplexml"
$res = preg_match('/<methodName>' . $this->_prefix . '([a-zA-Z0-9\.,\/]*)<\/methodName>/', $request, $results);
if ($res>0) {
return $results[1];
}
return false;
}
// }}}
// {{{ _workWithoutCache()
/**
* Do the real stuff if no cache available
*
* @return string the response of the real XML/RPC2 server
*/
private function _workWithoutCache()
{
require_once('XML/RPC2/Server.php');
$this->_serverObject = XML_RPC2_Server::create($this->_callTarget, $this->_options);
return $this->_serverObject->getResponse();
}
// }}}
// {{{ _makeCacheId()
/**
* make a cache id depending on the raw xmlrpc client request but depending on "environnement" setting too
*
* @param string $raw_request
* @return string cache id
*/
private function _makeCacheId($raw_request)
{
return md5($raw_request . serialize($this->_options));
}
// }}}
// {{{ clean()
/**
* Clean all the cache
*/
public function clean()
{
$this->_cacheObject->clean($this->_defaultCacheGroup, 'ingroup');
}
// }}}
// {{{ getContentLength()
/**
* Gets the content legth of a serialized XML-RPC message in bytes
*
* @param string $content the serialized XML-RPC message.
*
* @return integer the content length in bytes.
*/
protected function getContentLength($content)
{
if (extension_loaded('mbstring') && (ini_get('mbstring.func_overload') & 2) == 2) {
$length = mb_strlen($content, '8bit');
} else {
$length = strlen((binary)$content);
}
return $length;
}
// }}}
}
?>
|