PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php

https://bitbucket.org/ngmares/moodle
PHP | 191 lines | 131 code | 16 blank | 44 comment | 31 complexity | 8d944f52f789e0e2a0ffc8b2e71fc9f6 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  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. trigger_error('Base directory '.$base.' does not exist,
  119. please create or change using %Cache.SerializerPath',
  120. E_USER_WARNING);
  121. return false;
  122. } elseif (!$this->_testPermissions($base, $chmod)) {
  123. return false;
  124. }
  125. $old = umask(0000);
  126. mkdir($directory, $chmod);
  127. umask($old);
  128. } elseif (!$this->_testPermissions($directory, $chmod)) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * Tests permissions on a directory and throws out friendly
  135. * error messages and attempts to chmod it itself if possible
  136. * @param $dir Directory path
  137. * @param $chmod Permissions
  138. * @return True if directory writable
  139. */
  140. private function _testPermissions($dir, $chmod) {
  141. // early abort, if it is writable, everything is hunky-dory
  142. if (is_writable($dir)) return true;
  143. if (!is_dir($dir)) {
  144. // generally, you'll want to handle this beforehand
  145. // so a more specific error message can be given
  146. trigger_error('Directory '.$dir.' does not exist',
  147. E_USER_WARNING);
  148. return false;
  149. }
  150. if (function_exists('posix_getuid')) {
  151. // POSIX system, we can give more specific advice
  152. if (fileowner($dir) === posix_getuid()) {
  153. // we can chmod it ourselves
  154. $chmod = $chmod | 0700;
  155. if (chmod($dir, $chmod)) return true;
  156. } elseif (filegroup($dir) === posix_getgid()) {
  157. $chmod = $chmod | 0070;
  158. } else {
  159. // PHP's probably running as nobody, so we'll
  160. // need to give global permissions
  161. $chmod = $chmod | 0777;
  162. }
  163. trigger_error('Directory '.$dir.' not writable, '.
  164. 'please chmod to ' . decoct($chmod),
  165. E_USER_WARNING);
  166. } else {
  167. // generic error message
  168. trigger_error('Directory '.$dir.' not writable, '.
  169. 'please alter file permissions',
  170. E_USER_WARNING);
  171. }
  172. return false;
  173. }
  174. }
  175. // vim: et sw=4 sts=4