|
HTMLPurifier 4.4.0
|
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), $config); 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), $config); 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), $config); 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 00103 private function _write($file, $data, $config) { 00104 $result = file_put_contents($file, $data); 00105 if ($result !== false) { 00106 // set permissions of the new file (no execute) 00107 $chmod = $config->get('Cache.SerializerPermissions'); 00108 if (!$chmod) { 00109 $chmod = 0644; // invalid config or simpletest 00110 } 00111 $chmod = $chmod & 0666; 00112 chmod($file, $chmod); 00113 } 00114 return $result; 00115 } 00116 00122 private function _prepareDir($config) { 00123 $directory = $this->generateDirectoryPath($config); 00124 $chmod = $config->get('Cache.SerializerPermissions'); 00125 if (!$chmod) { 00126 $chmod = 0755; // invalid config or simpletest 00127 } 00128 if (!is_dir($directory)) { 00129 $base = $this->generateBaseDirectoryPath($config); 00130 if (!is_dir($base)) { 00131 trigger_error('Base directory '.$base.' does not exist, 00132 please create or change using %Cache.SerializerPath', 00133 E_USER_WARNING); 00134 return false; 00135 } elseif (!$this->_testPermissions($base, $chmod)) { 00136 return false; 00137 } 00138 $old = umask(0000); 00139 mkdir($directory, $chmod); 00140 umask($old); 00141 } elseif (!$this->_testPermissions($directory, $chmod)) { 00142 return false; 00143 } 00144 return true; 00145 } 00146 00154 private function _testPermissions($dir, $chmod) { 00155 // early abort, if it is writable, everything is hunky-dory 00156 if (is_writable($dir)) return true; 00157 if (!is_dir($dir)) { 00158 // generally, you'll want to handle this beforehand 00159 // so a more specific error message can be given 00160 trigger_error('Directory '.$dir.' does not exist', 00161 E_USER_WARNING); 00162 return false; 00163 } 00164 if (function_exists('posix_getuid')) { 00165 // POSIX system, we can give more specific advice 00166 if (fileowner($dir) === posix_getuid()) { 00167 // we can chmod it ourselves 00168 $chmod = $chmod | 0700; 00169 if (chmod($dir, $chmod)) return true; 00170 } elseif (filegroup($dir) === posix_getgid()) { 00171 $chmod = $chmod | 0070; 00172 } else { 00173 // PHP's probably running as nobody, so we'll 00174 // need to give global permissions 00175 $chmod = $chmod | 0777; 00176 } 00177 trigger_error('Directory '.$dir.' not writable, '. 00178 'please chmod to ' . decoct($chmod), 00179 E_USER_WARNING); 00180 } else { 00181 // generic error message 00182 trigger_error('Directory '.$dir.' not writable, '. 00183 'please alter file permissions', 00184 E_USER_WARNING); 00185 } 00186 return false; 00187 } 00188 00189 } 00190 00191 // vim: et sw=4 sts=4