Source for file Serializer.php

Documentation is available at Serializer.php

  1. <?php
  2.  
  3. {
  4.     
  5.     public function add($def$config{
  6.         if (!$this->checkDefType($def)) return;
  7.         $file $this->generateFilePath($config);
  8.         if (file_exists($file)) return false;
  9.         if (!$this->_prepareDir($config)) return false;
  10.         return $this->_write($fileserialize($def));
  11.     }
  12.     
  13.     public function set($def$config{
  14.         if (!$this->checkDefType($def)) return;
  15.         $file $this->generateFilePath($config);
  16.         if (!$this->_prepareDir($config)) return false;
  17.         return $this->_write($fileserialize($def));
  18.     }
  19.     
  20.     public function replace($def$config{
  21.         if (!$this->checkDefType($def)) return;
  22.         $file $this->generateFilePath($config);
  23.         if (!file_exists($file)) return false;
  24.         if (!$this->_prepareDir($config)) return false;
  25.         return $this->_write($fileserialize($def));
  26.     }
  27.     
  28.     public function get($config{
  29.         $file $this->generateFilePath($config);
  30.         if (!file_exists($file)) return false;
  31.         return unserialize(file_get_contents($file));
  32.     }
  33.     
  34.     public function remove($config{
  35.         $file $this->generateFilePath($config);
  36.         if (!file_exists($file)) return false;
  37.         return unlink($file);
  38.     }
  39.     
  40.     public function flush($config{
  41.         if (!$this->_prepareDir($config)) return false;
  42.         $dir $this->generateDirectoryPath($config);
  43.         $dh  opendir($dir);
  44.         while (false !== ($filename readdir($dh))) {
  45.             if (empty($filename)) continue;
  46.             if ($filename[0=== '.'continue;
  47.             unlink($dir '/' $filename);
  48.         }
  49.     }
  50.     
  51.     public function cleanup($config{
  52.         if (!$this->_prepareDir($config)) return false;
  53.         $dir $this->generateDirectoryPath($config);
  54.         $dh  opendir($dir);
  55.         while (false !== ($filename readdir($dh))) {
  56.             if (empty($filename)) continue;
  57.             if ($filename[0=== '.'continue;
  58.             $key substr($filename0strlen($filename4);
  59.             if ($this->isOld($key$config)) unlink($dir '/' $filename);
  60.         }
  61.     }
  62.     
  63.     /**
  64.      * Generates the file path to the serial file corresponding to
  65.      * the configuration and definition name
  66.      * @todo Make protected
  67.      */
  68.     public function generateFilePath($config{
  69.         $key $this->generateKey($config);
  70.         return $this->generateDirectoryPath($config'/' $key '.ser';
  71.     }
  72.     
  73.     /**
  74.      * Generates the path to the directory contain this cache's serial files
  75.      * @note No trailing slash
  76.      * @todo Make protected
  77.      */
  78.     public function generateDirectoryPath($config{
  79.         $base $this->generateBaseDirectoryPath($config);
  80.         return $base '/' $this->type;
  81.     }
  82.     
  83.     /**
  84.      * Generates path to base directory that contains all definition type
  85.      * serials
  86.      * @todo Make protected
  87.      */
  88.     public function generateBaseDirectoryPath($config{
  89.         $base $config->get('Cache''SerializerPath');
  90.         $base is_null($baseHTMLPURIFIER_PREFIX '/HTMLPurifier/DefinitionCache/Serializer' $base;
  91.         return $base;
  92.     }
  93.     
  94.     /**
  95.      * Convenience wrapper function for file_put_contents
  96.      * @param $file File name to write to
  97.      * @param $data Data to write into file
  98.      * @return Number of bytes written if success, or false if failure.
  99.      */
  100.     private function _write($file$data{
  101.         return file_put_contents($file$data);
  102.     }
  103.     
  104.     /**
  105.      * Prepares the directory that this type stores the serials in
  106.      * @return True if successful
  107.      */
  108.     private function _prepareDir($config{
  109.         $directory $this->generateDirectoryPath($config);
  110.         if (!is_dir($directory)) {
  111.             $base $this->generateBaseDirectoryPath($config);
  112.             if (!is_dir($base)) {
  113.                 trigger_error('Base directory '.$base.' does not exist,
  114.                     please create or change using %Cache.SerializerPath',
  115.                     E_USER_ERROR);
  116.                 return false;
  117.             elseif (!$this->_testPermissions($base)) {
  118.                 return false;
  119.             }
  120.             $old umask(0022)// disable group and world writes
  121.             mkdir($directory);
  122.             umask($old);
  123.         elseif (!$this->_testPermissions($directory)) {
  124.             return false;
  125.         }
  126.         return true;
  127.     }
  128.     
  129.     /**
  130.      * Tests permissions on a directory and throws out friendly
  131.      * error messages and attempts to chmod it itself if possible
  132.      */
  133.     private function _testPermissions($dir{
  134.         // early abort, if it is writable, everything is hunky-dory
  135.         if (is_writable($dir)) return true;
  136.         if (!is_dir($dir)) {
  137.             // generally, you'll want to handle this beforehand
  138.             // so a more specific error message can be given
  139.             trigger_error('Directory '.$dir.' does not exist',
  140.                 E_USER_ERROR);
  141.             return false;
  142.         }
  143.         if (function_exists('posix_getuid')) {
  144.             // POSIX system, we can give more specific advice
  145.             if (fileowner($dir=== posix_getuid()) {
  146.                 // we can chmod it ourselves
  147.                 chmod($dir0755);
  148.                 return true;
  149.             elseif (filegroup($dir=== posix_getgid()) {
  150.                 $chmod '775';
  151.             else {
  152.                 // PHP's probably running as nobody, so we'll
  153.                 // need to give global permissions
  154.                 $chmod '777';
  155.             }
  156.             trigger_error('Directory '.$dir.' not writable, '.
  157.                 'please chmod to ' $chmod,
  158.                 E_USER_ERROR);
  159.         else {
  160.             // generic error message
  161.             trigger_error('Directory '.$dir.' not writable, '.
  162.                 'please alter file permissions',
  163.                 E_USER_ERROR);
  164.         }
  165.         return false;
  166.     }
  167.     
  168. }

Documentation generated on Thu, 19 Jun 2008 18:50:19 -0400 by phpDocumentor 1.4.2