PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/vjCommentPlugin/lib/tools/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 172 lines | 118 code | 16 blank | 38 comment | 27 complexity | d63b0aeacf255afb75982a14f1dd0daf MD5 | raw file
Possible License(s): LGPL-2.1
  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));
  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));
  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));
  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. * @return Number of bytes written if success, or false if failure.
  89. */
  90. private function _write($file, $data) {
  91. return file_put_contents($file, $data);
  92. }
  93. /**
  94. * Prepares the directory that this type stores the serials in
  95. * @return True if successful
  96. */
  97. private function _prepareDir($config) {
  98. $directory = $this->generateDirectoryPath($config);
  99. if (!is_dir($directory)) {
  100. $base = $this->generateBaseDirectoryPath($config);
  101. if (!is_dir($base)) {
  102. trigger_error('Base directory '.$base.' does not exist,
  103. please create or change using %Cache.SerializerPath',
  104. E_USER_WARNING);
  105. return false;
  106. } elseif (!$this->_testPermissions($base)) {
  107. return false;
  108. }
  109. $old = umask(0022); // disable group and world writes
  110. mkdir($directory);
  111. umask($old);
  112. } elseif (!$this->_testPermissions($directory)) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. /**
  118. * Tests permissions on a directory and throws out friendly
  119. * error messages and attempts to chmod it itself if possible
  120. */
  121. private function _testPermissions($dir) {
  122. // early abort, if it is writable, everything is hunky-dory
  123. if (is_writable($dir)) return true;
  124. if (!is_dir($dir)) {
  125. // generally, you'll want to handle this beforehand
  126. // so a more specific error message can be given
  127. trigger_error('Directory '.$dir.' does not exist',
  128. E_USER_WARNING);
  129. return false;
  130. }
  131. if (function_exists('posix_getuid')) {
  132. // POSIX system, we can give more specific advice
  133. if (fileowner($dir) === posix_getuid()) {
  134. // we can chmod it ourselves
  135. chmod($dir, 0755);
  136. return true;
  137. } elseif (filegroup($dir) === posix_getgid()) {
  138. $chmod = '775';
  139. } else {
  140. // PHP's probably running as nobody, so we'll
  141. // need to give global permissions
  142. $chmod = '777';
  143. }
  144. trigger_error('Directory '.$dir.' not writable, '.
  145. 'please chmod to ' . $chmod,
  146. E_USER_WARNING);
  147. } else {
  148. // generic error message
  149. trigger_error('Directory '.$dir.' not writable, '.
  150. 'please alter file permissions',
  151. E_USER_WARNING);
  152. }
  153. return false;
  154. }
  155. }
  156. // vim: et sw=4 sts=4