nanoserv
[ class tree: nanoserv ] [ index: nanoserv ] [ all elements ]

Source for file NS_XML_RPC_Service_Handler.php

Documentation is available at NS_XML_RPC_Service_Handler.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * nanoserv handlers - XML-RPC server
  6.  * 
  7.  * Copyright (C) 2004-2010 Vincent Negrier aka. sIX <six@aegis-corp.org>
  8.  * 
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  * 
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with this library; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
  22.  *
  23.  * @package nanoserv
  24.  * @subpackage Handlers
  25.  */
  26.  
  27. /**
  28.  * Require the HTTP server
  29.  */
  30. require_once "nanoserv-compat/handlers/NS_HTTP_Service_Handler.php";
  31.  
  32.  
  33. /**
  34.  * XML-RPC Service handler class
  35.  *
  36.  * @package nanoserv
  37.  * @subpackage Handlers
  38.  */
  39.  
  40.     /**
  41.      * Request URL
  42.      * @var string 
  43.      */
  44.     protected $request_url = "";
  45.     
  46.     /**
  47.      * Convert a PHP variable to XML string representation
  48.      *
  49.      * @param string $var 
  50.      * @return string 
  51.      */
  52.     static protected function Variable_To_XML_String($var{
  53.  
  54.         $ret "<value>";
  55.  
  56.         if (is_int($var)) {
  57.  
  58.             $ret .= "<i4>".$var."</i4>";
  59.  
  60.         else if (is_bool($var)) {
  61.  
  62.             $ret .= "<boolean>".(int)$var."</boolean>";
  63.  
  64.         else if (is_string($var)) {
  65.  
  66.             if (htmlentities($var!= $var{
  67.  
  68.                 $ret .= "<base64>".base64_encode($var)."</base64>";
  69.             
  70.             else {
  71.             
  72.                 $ret .= "<string>".$var."</string>";
  73.  
  74.             }
  75.  
  76.         else if (is_float($var)) {
  77.  
  78.             $ret .= "<double>".$var."</double>";
  79.  
  80.         else if (is_array($var)) {
  81.  
  82.             if (self::Is_Assoc($var)) {
  83.  
  84.                 $ret .= "<struct>";
  85.                 
  86.                 foreach ($var as $k=>$v{
  87.  
  88.                     $ret .= "<member>";
  89.                     $ret .= "<name>".$k."</name>";
  90.                     $ret .= self::Variable_To_XML_String($v);
  91.                     $ret .= "</member>";
  92.                 
  93.                 }
  94.  
  95.                 $ret .= "</struct>";
  96.             
  97.             else {
  98.  
  99.                 $ret .= "<array><data>";
  100.                 
  101.                 foreach ($var as $v$ret .= self::Variable_To_XML_String($v);
  102.             
  103.                 $ret .= "</data></array>";
  104.             
  105.             }
  106.         
  107.         }
  108.         
  109.         $ret .= "</value>";
  110.     
  111.         return $ret;
  112.     
  113.     }
  114.     
  115.     /**
  116.      * Checks if given array is associative
  117.      *
  118.      * @param array $arr 
  119.      * @return bool 
  120.      */
  121.     static private function Is_Assoc($arr{
  122.  
  123.         return is_array($arr&& array_keys($arr!== range(0sizeof($arr1);
  124.     
  125.     }
  126.     
  127.     /**
  128.      * Convert a XMLRPC value stored in a SimpleXmlElement object to php variable
  129.      *
  130.      * @param SimpleXmlElement $xml 
  131.      * @return mixed 
  132.      */
  133.     static protected function XML_Value_To_Variable(SimpleXmlElement $xml{
  134.  
  135.         foreach ($xml as $type => $xvaluebreak;
  136.             
  137.         if (isset($type)) {
  138.         
  139.             $value = (string)$xvalue;
  140.  
  141.         else {
  142.  
  143.             $type "string";
  144.             $value = (string)$xml;
  145.  
  146.         }
  147.         
  148.         switch (strtoupper($type)) {
  149.  
  150.             case "I4":
  151.             case "INT":
  152.             $value = (int)$value;
  153.             break;
  154.  
  155.             case "BOOLEAN":
  156.             $value = (bool)$value;
  157.             break;
  158.  
  159.             case "DOUBLE":
  160.             $value = (float)$value;
  161.             break;
  162.  
  163.             case "BASE64":
  164.             $value base64_decode($value);
  165.             break;
  166.  
  167.             case "DATETIME.ISO8601":
  168.             $value strtotime($value);
  169.             break;
  170.             
  171.             case "STRUCT":
  172.             case "ARRAY":
  173.             $value self::XML_Struct_To_Array($xvalue);
  174.             break;
  175.             
  176.             case "STRING":
  177.             default:
  178.         
  179.         }
  180.  
  181.         return $value;
  182.     
  183.     }
  184.     
  185.     /**
  186.      * Convert a XMLRPC struct or array stored in a SimpleXmlElement object to php array
  187.      *
  188.      * @param SimpleXmlElement $xml 
  189.      * @return array 
  190.      */
  191.     static protected function XML_Struct_To_Array(SimpleXmlElement $xml{
  192.  
  193.         $ret array();
  194.  
  195.         foreach ($xml as $xtype=>$xelem{
  196.  
  197.             switch (strtoupper($xtype)) {
  198.  
  199.                 case "MEMBER":
  200.                 
  201.                 $mname $mval false;
  202.                 
  203.                 foreach ($xelem as $mprop=>$xval{
  204.  
  205.                     switch (strtoupper($mprop)) {
  206.  
  207.                         case "NAME":
  208.                         $mname = (string)$xval;
  209.                         break;
  210.  
  211.                         case "VALUE":
  212.                         $mval self::XML_Value_To_Variable($xval);
  213.                         break;
  214.                     
  215.                     }
  216.  
  217.                 }
  218.  
  219.                 $ret[$mname$mval;
  220.                 
  221.                 break;
  222.  
  223.                 case "DATA":
  224.                 foreach ($xelem as $xval$ret[self::XML_Value_To_Variable($xval);
  225.                 break;
  226.             
  227.             }
  228.  
  229.         }
  230.         
  231.         return $ret;
  232.     
  233.     }
  234.     
  235.     /**
  236.      * Convert XMLRPC method call params stored in a SimpleXmlElement object to a php array
  237.      *
  238.      * @param SimpleXmlElement $xml 
  239.      * @return array 
  240.      */
  241.     static protected function XML_Params_To_Array(SimpleXmlElement $xml{
  242.  
  243.         $ret array();
  244.         
  245.         foreach ($xml as $topname=>$xparam{
  246.  
  247.             if (strtoupper($topname!= "PARAM"continue;
  248.  
  249.             foreach ($xparam as $xvalue$ret[self::XML_Value_To_Variable($xvalue);
  250.         
  251.         }
  252.     
  253.         return $ret;
  254.     
  255.     }
  256.     
  257.     /**
  258.      * Add XMLRPC response envelope
  259.      *
  260.      * @param string $xml_result 
  261.      * @return string 
  262.      */
  263.     static protected function XML_Add_MethodResponse_Envelope($xml_result{
  264.  
  265.         return "<methodResponse><params><param>{$xml_result}</param></params></methodResponse>";
  266.  
  267.     }
  268.     
  269.     static protected function XML_Add_Fault_Envelope(Exception $e{
  270.  
  271.         return "<methodResponse><fault><value><struct><member><name>faultCode</name><value><int>" $e->getCode("</int></value></member><member><name>faultString</name><value><string>" $e->getMessage("</string></value></member></struct></value></fault></methodResponse>";
  272.     
  273.     }
  274.     
  275.     final public function on_Request($url{
  276.  
  277.         $this->request_url = $url;
  278.         
  279.         $xreq @simplexml_load_string($this->request_content);
  280.         
  281.         if ($xreq === false{
  282.  
  283.             $this->Set_Response_Status(400);
  284.             return "";
  285.         
  286.         }
  287.  
  288.         foreach ($xreq as $name => $xtopelem{
  289.  
  290.             switch (strtoupper($name)) {
  291.  
  292.                 case "METHODNAME":
  293.                 $method = (string)$xtopelem;
  294.                 break;
  295.  
  296.                 case "PARAMS":
  297.                 $params $xtopelem;
  298.                 break;
  299.             
  300.             }
  301.             
  302.         
  303.         }
  304.         
  305.         $this->Set_Content_Type("text/xml");
  306.  
  307.         try {
  308.         
  309.             return self::XML_Add_MethodResponse_Envelope(self::Variable_To_XML_String($this->on_Call($methodisset($paramsself::XML_Params_To_Array($paramsNULL)));
  310.  
  311.         catch (Exception $e{
  312.  
  313.             return self::XML_Add_Fault_Envelope($e);
  314.         
  315.         }
  316.     
  317.     }
  318.  
  319.     /**
  320.      * Event called on XML-RPC method call
  321.      *
  322.      * The value returned by on_Call() will be sent back as the XMLRPC method call response
  323.      *
  324.      * @param string $method 
  325.      * @param array $args 
  326.      * @return mixed 
  327.      */
  328.     abstract public function on_Call($method$args);
  329.  
  330. }
  331.  
  332. ?>

Documentation generated on Wed, 30 Nov 2011 22:03:26 +0100 by phpDocumentor 1.4.3