home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / RPC2 / Server.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.1 KB  |  312 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{ 
  6.  
  7. /**
  8. * +-----------------------------------------------------------------------------+
  9. * | Copyright (c) 2004-2006 Sergio Gonalves Carvalho                                |
  10. * +-----------------------------------------------------------------------------+
  11. * | This file is part of XML_RPC2.                                              |
  12. * |                                                                             |
  13. * | XML_RPC2 is free software; you can redistribute it and/or modify            |
  14. * | it under the terms of the GNU Lesser General Public License as published by |
  15. * | the Free Software Foundation; either version 2.1 of the License, or         |
  16. * | (at your option) any later version.                                         |
  17. * |                                                                             |
  18. * | XML_RPC2 is distributed in the hope that it will be useful,                 |
  19. * | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  20. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  21. * | GNU Lesser General Public License for more details.                         |
  22. * |                                                                             |
  23. * | You should have received a copy of the GNU Lesser General Public License    |
  24. * | along with XML_RPC2; if not, write to the Free Software                     |
  25. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  26. * | 02111-1307 USA                                                              |
  27. * +-----------------------------------------------------------------------------+
  28. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
  29. * +-----------------------------------------------------------------------------+
  30. *
  31. * @category   XML
  32. * @package    XML_RPC2
  33. * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  34. * @copyright  2004-2006 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: Server.php,v 1.12 2006/12/02 18:09:49 sergiosgc Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Exception.php';
  44. require_once 'XML/RPC2/Backend.php';
  45. // }}}
  46.  
  47.  
  48. /**
  49.  * XML_RPC2_Server is the frontend class for exposing PHP functions via XML-RPC. 
  50.  *
  51.  * Exporting a programatic interface via XML-RPC using XML_RPC2 is exceedingly easy:
  52.  *
  53.  * The first step is to assemble all methods you wish to export into a class. You may either
  54.  * create a (abstract) class with exportable methods as static, or use an existing instance
  55.  * of an object.
  56.  *
  57.  * You'll then need to document the methods using PHPDocumentor tags. XML_RPC2 will use the
  58.  * documentation for server introspection. You'll get something like this:
  59.  *
  60.  * <code>
  61.  * class ExampleServer {
  62.  *     /**
  63.  *      * hello says hello
  64.  *      *
  65.  *      * @param string  Name
  66.  *      * @return string Greetings
  67.  *      {@*}
  68.  *     public static function hello($name) 
  69.     {
  70.  *         return "Hello $name";
  71.  *     }
  72.  * }
  73.  * </code>
  74.  *
  75.  * Now, instantiate the server, using the Factory method to select a backend and a call handler for you:
  76.  * <code>
  77.  * require_once 'XML/RPC2/Server.php';
  78.  * $server = XML_RPC2_Server::create('ExampleServer');
  79.  * $server->handleCall();
  80.  * </code>
  81.  *
  82.  * This will create a server exporting all of the 'ExampleServer' class' methods. If you wish to export
  83.  * instance methods as well, pass an object instance to the factory instead:
  84.  * <code>
  85.  * require_once 'XML/RPC2/Server.php';
  86.  * $server = XML_RPC2_Server::create(new ExampleServer());
  87.  * $server->handleCall();
  88.  * </code>
  89.  *
  90.  * @category   XML
  91.  * @package    XML_RPC2
  92.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  93.  * @copyright  2004-2006 Sergio Carvalho
  94.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  95.  * @link       http://pear.php.net/package/XML_RPC2
  96.  */
  97. abstract class XML_RPC2_Server 
  98. {
  99.  
  100.     // {{{ properties
  101.     
  102.     /**
  103.      * callHandler field
  104.      *
  105.      * The call handler is responsible for executing the server exported methods
  106.      *
  107.      * @var mixed
  108.      */
  109.     protected $callHandler = null;
  110.        
  111.     /**
  112.      * prefix field
  113.      *
  114.      * @var string
  115.      */
  116.     protected $prefix = '';
  117.     
  118.     /**
  119.      * encoding field
  120.      *
  121.      * TODO : work on encoding for this backend
  122.      *
  123.      * @var string
  124.      */
  125.     protected $encoding = 'iso-8859-1';
  126.     
  127.     /** 
  128.      * display html documentation of xmlrpc exported methods when there is no post datas
  129.      *
  130.      * @var boolean
  131.      */
  132.     protected $autoDocument = true;
  133.     
  134.     /**
  135.      * display external links at the end of autodocumented page
  136.      *
  137.      * @var boolean
  138.      */
  139.     protected $autoDocumentExternalLinks = true;
  140.     
  141.     /**
  142.      * signature checking flag
  143.      * 
  144.      * if set to true, the server will check the method signature before
  145.      * calling the corresponding php method
  146.      * 
  147.      * @var boolean
  148.      */
  149.     protected $signatureChecking = true;
  150.       
  151.     // }}}
  152.     // {{{ constructor
  153.     
  154.     /**
  155.      * Create a new XML-RPC Server. 
  156.      *
  157.      * @param object $callHandler the call handler will receive a method call for each remote call received.
  158.      * @param array associative array of options
  159.      */
  160.     protected function __construct($callHandler, $options = array())
  161.     {
  162.         $this->callHandler = $callHandler;
  163.         if ((isset($options['prefix'])) && (is_string($options['prefix']))) {
  164.             $this->prefix = $options['prefix'];
  165.         }
  166.         if ((isset($options['encoding'])) && (is_string($options['encoding']))) {
  167.             $this->encoding = $options['encoding'];
  168.         }
  169.         if ((isset($options['autoDocument'])) && (is_bool($options['autoDocument']))) {
  170.             $this->autoDocument = $options['autoDocument'];
  171.         }
  172.         if ((isset($options['autoDocumentExternalLinks'])) && (is_bool($options['autoDocumentExternalLinks']))) {
  173.             $this->autoDocumentExternalLinks = $options['autoDocumentExternalLinks'];
  174.         }
  175.         if ((isset($options['signatureChecking'])) && (is_bool($options['signatureChecking']))) {
  176.             $this->signatureChecking = $options['signatureChecking'];
  177.         }
  178.     }
  179.     
  180.     // }}}
  181.     // {{{ create()
  182.     
  183.     /**
  184.      * Factory method to select a backend and return a new XML_RPC2_Server based on the backend
  185.      *
  186.      * @param mixed $callTarget either a class name or an object instance. 
  187.      * @param array associative array of options
  188.      * @return object a server class instance
  189.      */
  190.     public static function create($callTarget, $options = array())
  191.     {        
  192.         if (isset($options['backend'])) {
  193.             XML_RPC2_Backend::setBackend($options['backend']);
  194.         }
  195.         if (isset($options['prefix'])) {
  196.             $prefix = $options['prefix'];
  197.         } else {
  198.             $prefix = '';
  199.         }
  200.         $backend = XML_RPC2_Backend::getServerClassname();
  201.         // Find callHandler class
  202.         if (!isset($options['callHandler'])) {
  203.             if (is_object($callTarget)) { // Delegate calls to instance methods
  204.                 require_once 'XML/RPC2/Server/CallHandler/Instance.php';
  205.                 $callHandler = new XML_RPC2_Server_CallHandler_Instance($callTarget, $prefix);
  206.             } else { // Delegate calls to static class methods
  207.                 require_once 'XML/RPC2/Server/CallHandler/Class.php';
  208.                 $callHandler = new XML_RPC2_Server_CallHandler_Class($callTarget, $prefix);
  209.             }
  210.         } else {
  211.             $callHandler = $options['callHandler'];
  212.         }
  213.         return new $backend($callHandler, $options);
  214.     }
  215.     
  216.     // }}}
  217.     // {{{ handleCall()
  218.     
  219.     /**
  220.      * Receive the XML-RPC request, decode the HTTP payload, delegate execution to the call handler, and output the encoded call handler response.
  221.      *
  222.      */
  223.     public abstract function handleCall();
  224.     
  225.     // }}}
  226.     // {{{ errorToException()
  227.     
  228.     /**
  229.      * Transform an error into an exception
  230.      *
  231.      * @param int $errno error number
  232.      * @param string $errstr error string
  233.      * @param string $errfile error file
  234.      * @param int $errline error line
  235.      */
  236.     public static function errorToException($errno, $errstr, $errfile, $errline)
  237.     {
  238.         switch ($errno) {
  239.             case E_WARNING:
  240.             case E_NOTICE:
  241.             case E_USER_WARNING:
  242.             case E_USER_NOTICE:
  243.             case E_STRICT:
  244.                 // Silence warnings
  245.                 // TODO Logging should occur here
  246.                 break;
  247.             default:
  248.                 throw new Exception('Classic error reported "' . $errstr . '" on ' . $errfile . ':' . $errline);
  249.         }
  250.     }
  251.     
  252.     // }}}
  253.     // {{{ autoDocument()
  254.     /*     autoDocument {{{ */
  255.     /**
  256.      *     autoDocument. Produce an HTML page from the result of server introspection
  257.      *
  258.      * @return string HTML document describing this server
  259.      */
  260.     public function autoDocument()
  261.     /* }}} */
  262.     {
  263.         print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  264.         print "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n";
  265.         print "  <head>\n";
  266.         print "    <meta http-equiv=\"Content-Type\" content=\"text/HTML; charset=" . $this->encoding . "\"  />\n";
  267.         print "    <title>Available XMLRPC methods for this server</title>\n";
  268.         print "    <style type=\"text/css\">\n";
  269.         print "      li,p { font-size: 10pt; font-family: Arial,Helvetia,sans-serif; }\n";
  270.         print "      a:link { background-color: white; color: blue; text-decoration: underline; font-weight: bold; }\n";
  271.         print "      a:visited { background-color: white; color: blue; text-decoration: underline; font-weight: bold; }\n";
  272.         print "      table { border-collapse:collapse; width: 100% }\n";
  273.         print "      table,td { padding: 5px; border: 1px solid black; }\n";
  274.         print "      div.bloc { border: 1px dashed gray; padding: 10px; margin-bottom: 20px; }\n";
  275.         print "      div.description { border: 1px solid black; padding: 10px; }\n";
  276.         print "      span.type { background-color: white; color: gray; font-weight: normal; }\n";
  277.         print "      span.paratype { background-color: white; color: gray; font-weight: normal; }\n";
  278.         print "      span.name { background-color: white; color: #660000; }\n";
  279.         print "      span.paraname { background-color: white; color: #336600; }\n";
  280.         print "      img { border: 0px; }\n";
  281.         print "      li { font-size: 12pt; }\n";
  282.         print "    </style>\n";
  283.         print "  </head>\n";
  284.         print "  <body>\n";
  285.         print "    <h1>Available XMLRPC methods for this server</h1>\n";
  286.         print "    <h2><a name=\"index\">Index</a></h2>\n";
  287.         print "    <ul>\n";
  288.         foreach ($this->callHandler->getMethods() as $method) {
  289.             $name = $method->getName();
  290.             $id = md5($name);
  291.             $signature = $method->getHTMLSignature();
  292.             print "      <li><a href=\"#$id\">$name()</a></li>\n";
  293.         }
  294.         print "    </ul>\n";
  295.         print "    <h2>Details</h2>\n";
  296.         foreach ($this->callHandler->getMethods() as $method) {
  297.             print "    <div class=\"bloc\">\n";   
  298.             $method->autoDocument();
  299.             print "      <p>(return to <a href=\"#index\">index</a>)</p>\n";
  300.             print "    </div>\n";
  301.         }
  302.         if (!($this->autoDocumentExternalLinks)) {
  303.             print '    <p><a href="http://pear.php.net/packages/XML_RPC2"><img src="http://pear.php.net/gifs/pear-power.png" alt="Powered by PEAR/XML_RPC2" height="31" width="88" /></a>       <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>       <a href="http://jigsaw.w3.org/css-validator/"><img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" /></a></p>' . "\n";
  304.         }
  305.         print "  </body>\n";
  306.         print "</html>\n";
  307.     }    
  308.     // }}}
  309. }
  310.  
  311. ?>
  312.