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 / Backend / Php / Value / Base64.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.4 KB  |  136 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 Goncalves 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: Base64.php,v 1.4 2006/01/22 01:57:00 fab 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/Value/Scalar.php';
  45. // }}}
  46.  
  47. /**
  48.  * XML_RPC base64 value class. Instances of this class represent base64-encoded string scalars in XML_RPC
  49.  * 
  50.  * To work on a compatible way with the xmlrpcext backend, we introduce a particular "nativeValue" which is
  51.  * a standard class (stdclass) with two public properties :
  52.  * scalar => the string (non encoded)
  53.  * xmlrpc_type => 'base64'
  54.  * 
  55.  * The constructor can be called with a "classic" string or with a such object 
  56.  * 
  57.  * @category   XML
  58.  * @package    XML_RPC2
  59.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  60.  * @copyright  2004-2006 Sergio Carvalho
  61.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  62.  * @link       http://pear.php.net/package/XML_RPC2
  63.  */
  64. class XML_RPC2_Backend_Php_Value_Base64 extends XML_RPC2_Backend_Php_Value
  65. {
  66.     
  67.     // {{{ constructor
  68.  
  69.     /**
  70.      * Constructor. Will build a new XML_RPC2_Backend_Php_Value_Base64 with the given value
  71.      *
  72.      * This class handles encoding-decoding internally. Do not provide the
  73.      * native string base64-encoded
  74.      * 
  75.      * @param mixed String $nativeValue to be transmited base64-encoded or "stdclass native value"  
  76.      */
  77.     public function __construct($nativeValue) 
  78.     {
  79.         if ((is_object($nativeValue)) &&(strtolower(get_class($nativeValue)) == 'stdclass') && (isset($nativeValue->xmlrpc_type))) {
  80.             $scalar = $nativeValue->scalar;
  81.         } else {
  82.             if (!is_string($nativeValue)) {
  83.                 throw new XML_RPC2_InvalidTypeException(sprintf('Cannot create XML_RPC2_Backend_Php_Value_Base64 from type \'%s\'.', gettype($nativeValue)));
  84.            }
  85.             $scalar = $nativeValue;
  86.         }
  87.         $tmp              = new stdclass();
  88.         $tmp->scalar      = $scalar;
  89.         $tmp->xmlrpc_type = 'base64';
  90.         $this->setNativeValue($tmp);
  91.     }
  92.     
  93.     // }}}
  94.     // {{{ encode()
  95.     
  96.     /**
  97.      * Encode the instance into XML, for transport
  98.      * 
  99.      * @return string The encoded XML-RPC value,
  100.      */
  101.     public function encode() 
  102.     {
  103.         $native = $this->getNativeValue();
  104.         return '<base64>' . base64_encode($native->scalar) . '</base64>';
  105.     }
  106.     
  107.     // }}}
  108.     // {{{ decode()
  109.     
  110.     /**
  111.      * Decode transport XML and set the instance value accordingly
  112.      *
  113.      * @param mixed $xml The encoded XML-RPC value,
  114.      */
  115.     public static function decode($xml) 
  116.     {
  117.         // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
  118.         // xpath is used both in an element and in one of its children
  119.         $xml = simplexml_load_string($xml->asXML());
  120.         $value = $xml->xpath('/value/base64/text()');
  121.         if (!array_key_exists(0, $value)) {
  122.             $value = $xml->xpath('/value/text()');
  123.         }
  124.         // Emulate xmlrpcext results (to be able to switch from a backend to another)
  125.         $result = new stdclass();
  126.         $result->scalar = base64_decode($value[0]);
  127.         $result->xmlrpc_type = 'base64';
  128.         return $result;
  129.     }
  130.     
  131.     // }}}
  132.     
  133. }
  134.  
  135. ?>
  136.