PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ezyang/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php

https://gitlab.com/afzalpotenza/YII_salon
PHP | 291 lines | 195 code | 15 blank | 81 comment | 32 complexity | 6ac88a85bf094aecd0d27921f6660488 MD5 | raw file
  1. <?php
  2. class HTMLPurifier_DefinitionCache_Serializer extends HTMLPurifier_DefinitionCache
  3. {
  4. /**
  5. * @param HTMLPurifier_Definition $def
  6. * @param HTMLPurifier_Config $config
  7. * @return int|bool
  8. */
  9. public function add($def, $config)
  10. {
  11. if (!$this->checkDefType($def)) {
  12. return;
  13. }
  14. $file = $this->generateFilePath($config);
  15. if (file_exists($file)) {
  16. return false;
  17. }
  18. if (!$this->_prepareDir($config)) {
  19. return false;
  20. }
  21. return $this->_write($file, serialize($def), $config);
  22. }
  23. /**
  24. * @param HTMLPurifier_Definition $def
  25. * @param HTMLPurifier_Config $config
  26. * @return int|bool
  27. */
  28. public function set($def, $config)
  29. {
  30. if (!$this->checkDefType($def)) {
  31. return;
  32. }
  33. $file = $this->generateFilePath($config);
  34. if (!$this->_prepareDir($config)) {
  35. return false;
  36. }
  37. return $this->_write($file, serialize($def), $config);
  38. }
  39. /**
  40. * @param HTMLPurifier_Definition $def
  41. * @param HTMLPurifier_Config $config
  42. * @return int|bool
  43. */
  44. public function replace($def, $config)
  45. {
  46. if (!$this->checkDefType($def)) {
  47. return;
  48. }
  49. $file = $this->generateFilePath($config);
  50. if (!file_exists($file)) {
  51. return false;
  52. }
  53. if (!$this->_prepareDir($config)) {
  54. return false;
  55. }
  56. return $this->_write($file, serialize($def), $config);
  57. }
  58. /**
  59. * @param HTMLPurifier_Config $config
  60. * @return bool|HTMLPurifier_Config
  61. */
  62. public function get($config)
  63. {
  64. $file = $this->generateFilePath($config);
  65. if (!file_exists($file)) {
  66. return false;
  67. }
  68. return unserialize(file_get_contents($file));
  69. }
  70. /**
  71. * @param HTMLPurifier_Config $config
  72. * @return bool
  73. */
  74. public function remove($config)
  75. {
  76. $file = $this->generateFilePath($config);
  77. if (!file_exists($file)) {
  78. return false;
  79. }
  80. return unlink($file);
  81. }
  82. /**
  83. * @param HTMLPurifier_Config $config
  84. * @return bool
  85. */
  86. public function flush($config)
  87. {
  88. if (!$this->_prepareDir($config)) {
  89. return false;
  90. }
  91. $dir = $this->generateDirectoryPath($config);
  92. $dh = opendir($dir);
  93. while (false !== ($filename = readdir($dh))) {
  94. if (empty($filename)) {
  95. continue;
  96. }
  97. if ($filename[0] === '.') {
  98. continue;
  99. }
  100. unlink($dir . '/' . $filename);
  101. }
  102. }
  103. /**
  104. * @param HTMLPurifier_Config $config
  105. * @return bool
  106. */
  107. public function cleanup($config)
  108. {
  109. if (!$this->_prepareDir($config)) {
  110. return false;
  111. }
  112. $dir = $this->generateDirectoryPath($config);
  113. $dh = opendir($dir);
  114. while (false !== ($filename = readdir($dh))) {
  115. if (empty($filename)) {
  116. continue;
  117. }
  118. if ($filename[0] === '.') {
  119. continue;
  120. }
  121. $key = substr($filename, 0, strlen($filename) - 4);
  122. if ($this->isOld($key, $config)) {
  123. unlink($dir . '/' . $filename);
  124. }
  125. }
  126. }
  127. /**
  128. * Generates the file path to the serial file corresponding to
  129. * the configuration and definition name
  130. * @param HTMLPurifier_Config $config
  131. * @return string
  132. * @todo Make protected
  133. */
  134. public function generateFilePath($config)
  135. {
  136. $key = $this->generateKey($config);
  137. return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
  138. }
  139. /**
  140. * Generates the path to the directory contain this cache's serial files
  141. * @param HTMLPurifier_Config $config
  142. * @return string
  143. * @note No trailing slash
  144. * @todo Make protected
  145. */
  146. public function generateDirectoryPath($config)
  147. {
  148. $base = $this->generateBaseDirectoryPath($config);
  149. return $base . '/' . $this->type;
  150. }
  151. /**
  152. * Generates path to base directory that contains all definition type
  153. * serials
  154. * @param HTMLPurifier_Config $config
  155. * @return mixed|string
  156. * @todo Make protected
  157. */
  158. public function generateBaseDirectoryPath($config)
  159. {
  160. $base = $config->get('Cache.SerializerPath');
  161. $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
  162. return $base;
  163. }
  164. /**
  165. * Convenience wrapper function for file_put_contents
  166. * @param string $file File name to write to
  167. * @param string $data Data to write into file
  168. * @param HTMLPurifier_Config $config
  169. * @return int|bool Number of bytes written if success, or false if failure.
  170. */
  171. private function _write($file, $data, $config)
  172. {
  173. $result = file_put_contents($file, $data);
  174. if ($result !== false) {
  175. // set permissions of the new file (no execute)
  176. $chmod = $config->get('Cache.SerializerPermissions');
  177. if (!$chmod) {
  178. $chmod = 0644; // invalid config or simpletest
  179. }
  180. $chmod = $chmod & 0666;
  181. chmod($file, $chmod);
  182. }
  183. return $result;
  184. }
  185. /**
  186. * Prepares the directory that this type stores the serials in
  187. * @param HTMLPurifier_Config $config
  188. * @return bool True if successful
  189. */
  190. private function _prepareDir($config)
  191. {
  192. $directory = $this->generateDirectoryPath($config);
  193. $chmod = $config->get('Cache.SerializerPermissions');
  194. if (!$chmod) {
  195. $chmod = 0755; // invalid config or simpletest
  196. }
  197. if (!is_dir($directory)) {
  198. $base = $this->generateBaseDirectoryPath($config);
  199. if (!is_dir($base)) {
  200. trigger_error(
  201. 'Base directory ' . $base . ' does not exist,
  202. please create or change using %Cache.SerializerPath',
  203. E_USER_WARNING
  204. );
  205. return false;
  206. } elseif (!$this->_testPermissions($base, $chmod)) {
  207. return false;
  208. }
  209. mkdir($directory, $chmod);
  210. if (!$this->_testPermissions($directory, $chmod)) {
  211. trigger_error(
  212. 'Base directory ' . $base . ' does not exist,
  213. please create or change using %Cache.SerializerPath',
  214. E_USER_WARNING
  215. );
  216. return false;
  217. }
  218. } elseif (!$this->_testPermissions($directory, $chmod)) {
  219. return false;
  220. }
  221. return true;
  222. }
  223. /**
  224. * Tests permissions on a directory and throws out friendly
  225. * error messages and attempts to chmod it itself if possible
  226. * @param string $dir Directory path
  227. * @param int $chmod Permissions
  228. * @return bool True if directory is writable
  229. */
  230. private function _testPermissions($dir, $chmod)
  231. {
  232. // early abort, if it is writable, everything is hunky-dory
  233. if (is_writable($dir)) {
  234. return true;
  235. }
  236. if (!is_dir($dir)) {
  237. // generally, you'll want to handle this beforehand
  238. // so a more specific error message can be given
  239. trigger_error(
  240. 'Directory ' . $dir . ' does not exist',
  241. E_USER_WARNING
  242. );
  243. return false;
  244. }
  245. if (function_exists('posix_getuid')) {
  246. // POSIX system, we can give more specific advice
  247. if (fileowner($dir) === posix_getuid()) {
  248. // we can chmod it ourselves
  249. $chmod = $chmod | 0700;
  250. if (chmod($dir, $chmod)) {
  251. return true;
  252. }
  253. } elseif (filegroup($dir) === posix_getgid()) {
  254. $chmod = $chmod | 0070;
  255. } else {
  256. // PHP's probably running as nobody, so we'll
  257. // need to give global permissions
  258. $chmod = $chmod | 0777;
  259. }
  260. trigger_error(
  261. 'Directory ' . $dir . ' not writable, ' .
  262. 'please chmod to ' . decoct($chmod),
  263. E_USER_WARNING
  264. );
  265. } else {
  266. // generic error message
  267. trigger_error(
  268. 'Directory ' . $dir . ' not writable, ' .
  269. 'please alter file permissions',
  270. E_USER_WARNING
  271. );
  272. }
  273. return false;
  274. }
  275. }
  276. // vim: et sw=4 sts=4