| * +-----------------------------------------------------------------------------+ * * @category XML * @package XML_RPC2 * @author Sergio Carvalho * @copyright 2004-2006 Sergio Carvalho * @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 'XML/RPC2/Exception.php'; require_once 'XML/RPC2/Backend.php'; require_once 'XML/RPC2/Server/Input.php'; // }}} /** * XML_RPC2_Server is the frontend class for exposing PHP functions via XML-RPC. * * Exporting a programatic interface via XML-RPC using XML_RPC2 is exceedingly easy: * * The first step is to assemble all methods you wish to export into a class. You may either * create a (abstract) class with exportable methods as static, or use an existing instance * of an object. * * You'll then need to document the methods using PHPDocumentor tags. XML_RPC2 will use the * documentation for server introspection. You'll get something like this: * * * class ExampleServer { * /** * * hello says hello * * * * @param string Name * * @return string Greetings * {@*} * public static function hello($name) { * return "Hello $name"; * } * } * * * Now, instantiate the server, using the Factory method to select a backend and a call handler for you: * * require_once 'XML/RPC2/Server.php'; * $server = XML_RPC2_Server::create('ExampleServer'); * $server->handleCall(); * * * This will create a server exporting all of the 'ExampleServer' class' methods. If you wish to export * instance methods as well, pass an object instance to the factory instead: * * require_once 'XML/RPC2/Server.php'; * $server = XML_RPC2_Server::create(new ExampleServer()); * $server->handleCall(); * * * @category XML * @package XML_RPC2 * @author Sergio Carvalho * @copyright 2004-2006 Sergio Carvalho * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @link http://pear.php.net/package/XML_RPC2 */ abstract class XML_RPC2_Server { // {{{ properties /** * callHandler field * * The call handler is responsible for executing the server exported methods * * @var mixed */ protected $callHandler = null; /** * prefix field * * @var string */ protected $prefix = ''; /** * encoding field * * TODO : work on encoding for this backend * * @var string */ protected $encoding = 'utf-8'; /** * display html documentation of xmlrpc exported methods when there is no post datas * * @var boolean */ protected $autoDocument = true; /** * display external links at the end of autodocumented page * * @var boolean */ protected $autoDocumentExternalLinks = true; /** * signature checking flag * * if set to true, the server will check the method signature before * calling the corresponding php method * * @var boolean */ protected $signatureChecking = true; /** * input handler * * Implementation of XML_RPC2_Server_Input that feeds this server with input * * @var XML_RPC2_Server_Input */ protected $input; // }}} // {{{ constructor /** * Create a new XML-RPC Server. * * @param object $callHandler the call handler will receive a method call for each remote call received. * @param array associative array of options */ protected function __construct($callHandler, $options = array()) { $this->callHandler = $callHandler; if ((isset($options['prefix'])) && (is_string($options['prefix']))) { $this->prefix = $options['prefix']; } if ((isset($options['encoding'])) && (is_string($options['encoding']))) { $this->encoding = $options['encoding']; } if ((isset($options['autoDocument'])) && (is_bool($options['autoDocument']))) { $this->autoDocument = $options['autoDocument']; } if ((isset($options['autoDocumentExternalLinks'])) && (is_bool($options['autoDocumentExternalLinks']))) { $this->autoDocumentExternalLinks = $options['autoDocumentExternalLinks']; } if ((isset($options['signatureChecking'])) && (is_bool($options['signatureChecking']))) { $this->signatureChecking = $options['signatureChecking']; } if (!isset($options['input'])) $options['input'] = 'XML_RPC2_Server_Input_RawPostData'; if (is_string($options['input'])) { $inputDir = strtr($options['input'], array('_' => DIRECTORY_SEPARATOR)) . '.php'; require_once($inputDir); $inputClass = $options['input']; $options['input'] = new $inputClass(); } if ($options['input'] instanceof XML_RPC2_Server_Input) { $this->input = $options['input']; } else { throw new XML_RPC2_ConfigException('Invalid value for "input" option. It must be either a XML_RPC2_Server_Input subclass name or XML_RPC2_Server_Input subclass instance'); } } // }}} // {{{ create() /** * Factory method to select a backend and return a new XML_RPC2_Server based on the backend * * @param mixed $callTarget either a class name or an object instance. * @param array associative array of options * @return object a server class instance */ public static function create($callTarget, $options = array()) { if (isset($options['backend'])) { XML_RPC2_Backend::setBackend($options['backend']); } if (isset($options['prefix'])) { $prefix = $options['prefix']; } else { $prefix = ''; } $backend = XML_RPC2_Backend::getServerClassname(); // Find callHandler class if (!isset($options['callHandler'])) { if (is_object($callTarget)) { // Delegate calls to instance methods require_once 'XML/RPC2/Server/CallHandler/Instance.php'; $callHandler = new XML_RPC2_Server_CallHandler_Instance($callTarget, $prefix); } else { // Delegate calls to static class methods require_once 'XML/RPC2/Server/CallHandler/Class.php'; $callHandler = new XML_RPC2_Server_CallHandler_Class($callTarget, $prefix); } } else { $callHandler = $options['callHandler']; } return new $backend($callHandler, $options); } // }}} // {{{ handleCall() /** * Receive the XML-RPC request, decode the HTTP payload, delegate execution to the call handler, and output the encoded call handler response. * */ public abstract function handleCall(); // }}} // {{{ errorToException() /** * Transform an error into an exception * * @param int $errno error number * @param string $errstr error string * @param string $errfile error file * @param int $errline error line */ public static function errorToException($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_WARNING: case E_NOTICE: case E_USER_WARNING: case E_USER_NOTICE: case E_STRICT: // Silence warnings // TODO Logging should occur here break; default: throw new Exception('Classic error reported "' . $errstr . '" on ' . $errfile . ':' . $errline); } } // }}} // {{{ autoDocument() /* autoDocument {{{ */ /** * autoDocument. Produce an HTML page from the result of server introspection * * @return string HTML document describing this server */ public function autoDocument() /* }}} */ { print "\n"; print "\n"; print " \n"; print " \n"; print " Available XMLRPC methods for this server\n"; print " \n"; print " \n"; print " \n"; print "

Available XMLRPC methods for this server

\n"; print "

Index

\n"; print " \n"; print "

Details

\n"; foreach ($this->callHandler->getMethods() as $method) { print "
\n"; $method->autoDocument(); print "

(return to index)

\n"; print "
\n"; } if (!($this->autoDocumentExternalLinks)) { print '

Powered by PEAR/XML_RPC2       Valid XHTML 1.0 Strict       Valid CSS!

' . "\n"; } print " \n"; print "\n"; } // }}} // {{{ 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; } // }}} } ?>