00001 <?php 00002 00003 class HTMLPurifier_DefinitionCache_Serializer extends 00004 HTMLPurifier_DefinitionCache 00005 { 00006 00007 public function add($def, $config) { 00008 if (!$this->checkDefType($def)) return; 00009 $file = $this->generateFilePath($config); 00010 if (file_exists($file)) return false; 00011 if (!$this->_prepareDir($config)) return false; 00012 return $this->_write($file, serialize($def)); 00013 } 00014 00015 public function set($def, $config) { 00016 if (!$this->checkDefType($def)) return; 00017 $file = $this->generateFilePath($config); 00018 if (!$this->_prepareDir($config)) return false; 00019 return $this->_write($file, serialize($def)); 00020 } 00021 00022 public function replace($def, $config) { 00023 if (!$this->checkDefType($def)) return; 00024 $file = $this->generateFilePath($config); 00025 if (!file_exists($file)) return false; 00026 if (!$this->_prepareDir($config)) return false; 00027 return $this->_write($file, serialize($def)); 00028 } 00029 00030 public function get($config) { 00031 $file = $this->generateFilePath($config); 00032 if (!file_exists($file)) return false; 00033 return unserialize(file_get_contents($file)); 00034 } 00035 00036 public function remove($config) { 00037 $file = $this->generateFilePath($config); 00038 if (!file_exists($file)) return false; 00039 return unlink($file); 00040 } 00041 00042 public function flush($config) { 00043 if (!$this->_prepareDir($config)) return false; 00044 $dir = $this->generateDirectoryPath($config); 00045 $dh = opendir($dir); 00046 while (false !== ($filename = readdir($dh))) { 00047 if (empty($filename)) continue; 00048 if ($filename[0] === '.') continue; 00049 unlink($dir . '/' . $filename); 00050 } 00051 } 00052 00053 public function cleanup($config) { 00054 if (!$this->_prepareDir($config)) return false; 00055 $dir = $this->generateDirectoryPath($config); 00056 $dh = opendir($dir); 00057 while (false !== ($filename = readdir($dh))) { 00058 if (empty($filename)) continue; 00059 if ($filename[0] === '.') continue; 00060 $key = substr($filename, 0, strlen($filename) - 4); 00061 if ($this->isOld($key, $config)) unlink($dir . '/' . $filename); 00062 } 00063 } 00064 00070 public function generateFilePath($config) { 00071 $key = $this->generateKey($config); 00072 return $this->generateDirectoryPath($config) . '/' . $key . '.ser'; 00073 } 00074 00080 public function generateDirectoryPath($config) { 00081 $base = $this->generateBaseDirectoryPath($config); 00082 return $base . '/' . $this->type; 00083 } 00084 00090 public function generateBaseDirectoryPath($config) { 00091 $base = $config->get('Cache.SerializerPath'); 00092 $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base; 00093 return $base; 00094 } 00095 00102 private function _write($file, $data) { 00103 return file_put_contents($file, $data); 00104 } 00105 00110 private function _prepareDir($config) { 00111 $directory = $this->generateDirectoryPath($config); 00112 if (!is_dir($directory)) { 00113 $base = $this->generateBaseDirectoryPath($config); 00114 if (!is_dir($base)) { 00115 trigger_error('Base directory '.$base.' does not exist, 00116 please create or change using %Cache.SerializerPath', 00117 E_USER_WARNING); 00118 return false; 00119 } elseif (!$this->_testPermissions($base)) { 00120 return false; 00121 } 00122 $old = umask(0022); // disable group and world writes 00123 mkdir($directory); 00124 umask($old); 00125 } elseif (!$this->_testPermissions($directory)) { 00126 return false; 00127 } 00128 return true; 00129 } 00130 00135 private function _testPermissions($dir) { 00136 // early abort, if it is writable, everything is hunky-dory 00137 if (is_writable($dir)) return true; 00138 if (!is_dir($dir)) { 00139 // generally, you'll want to handle this beforehand 00140 // so a more specific error message can be given 00141 trigger_error('Directory '.$dir.' does not exist', 00142 E_USER_WARNING); 00143 return false; 00144 } 00145 if (function_exists('posix_getuid')) { 00146 // POSIX system, we can give more specific advice 00147 if (fileowner($dir) === posix_getuid()) { 00148 // we can chmod it ourselves 00149 chmod($dir, 0755); 00150 return true; 00151 } elseif (filegroup($dir) === posix_getgid()) { 00152 $chmod = '775'; 00153 } else { 00154 // PHP's probably running as nobody, so we'll 00155 // need to give global permissions 00156 $chmod = '777'; 00157 } 00158 trigger_error('Directory '.$dir.' not writable, '. 00159 'please chmod to ' . $chmod, 00160 E_USER_WARNING); 00161 } else { 00162 // generic error message 00163 trigger_error('Directory '.$dir.' not writable, '. 00164 'please alter file permissions', 00165 E_USER_WARNING); 00166 } 00167 return false; 00168 } 00169 00170 } 00171 00172 // vim: et sw=4 sts=4
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
