Serializer.php 6.7 KB

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