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

Source for file PHP.php

Documentation is available at PHP.php

  1. <?php
  2. // $Id$
  3. /**
  4.  * PHP Serializer
  5.  *
  6.  * @category   HTML
  7.  * @package    AJAX
  8.  * @author     Arpad Ray <arpad@php.net>
  9.  * @copyright  2005 Arpad Ray
  10.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  11.  * @version    Release: 0.5.4
  12.  * @link       http://pear.php.net/package/HTML_AJAX
  13.  */
  14. {    
  15.     function serialize($input
  16.     {
  17.         return serialize($input);
  18.     }
  19.  
  20.     /**
  21.      * Unserializes the given string
  22.      *
  23.      * Triggers an error if a class is found which is not
  24.      * in the provided array of allowed class names.
  25.      *
  26.      * @param   string  $input 
  27.      *   the serialized string to process
  28.      * @param   array   $allowedClasses 
  29.      *   an array of class names to check objects against
  30.      *   before instantion
  31.      * @return  mixed 
  32.      *   the unserialized variable on success, or false on
  33.      *   failure. If this method fails it will also trigger
  34.      *   a warning.
  35.      */
  36.     function unserialize($input$allowedClasses
  37.     {
  38.         if (version_compare(PHP_VERSION'4.3.10''<')
  39.              || (substr(PHP_VERSION01== '5' && version_compare(PHP_VERSION'5.0.3''<'))) {
  40.             trigger_error('Unsafe version of PHP for native unserialization');
  41.             return false;
  42.         }
  43.         $classes $this->_getSerializedClassNames($input);
  44.         if ($classes === false{
  45.             trigger_error('Invalidly serialized string');
  46.             return false;
  47.         }
  48.         $diff array_diff($classes$allowedClasses);
  49.         if (!empty($diff)) {
  50.             trigger_error('Class(es) not allowed to be serialized');
  51.             return false;
  52.         }
  53.         return unserialize($input);
  54.     }
  55.     
  56.     /**
  57.      * Extract class names from serialized string
  58.      *
  59.      * Adapted from code by Harry Fuecks
  60.      *
  61.      * @param   string  $string 
  62.      *   the serialized string to process
  63.      * @return  mixed 
  64.      *   an array of class names found, or false if the input
  65.      *   is invalidly formed
  66.      */
  67.     function _getSerializedClassNames($string{
  68.         // Strip any string representations (which might contain object syntax)
  69.         while (($pos strpos($string's:')) !== false{
  70.             $pos2 strpos($string':'$pos + 2);
  71.             if ($pos2 === false{
  72.                 // invalidly serialized string
  73.                 return false;    
  74.             }
  75.             $end $pos + 2 + substr($string$pos + 2$pos2+ 1;
  76.             $string substr($string0$possubstr($string$end);
  77.         }
  78.         
  79.         // Pull out the class names
  80.         preg_match_all('/O:[0-9]+:"(.*)"/U'$string$matches);
  81.         
  82.         // Make sure names are unique (same object serialized twice)
  83.         return array_unique($matches[1]);
  84.     }
  85. }
  86. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  87. ?>

Documentation generated on Fri, 04 Apr 2008 18:30:22 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.