File.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (https://phpids.org)
  8. *
  9. * PHPIDS is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * PHPIDS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5.1.6+
  23. *
  24. * @category Security
  25. * @package PHPIDS
  26. * @author Mario Heiderich <mario.heiderich@gmail.com>
  27. * @author Christian Matthies <ch0012@gmail.com>
  28. * @author Lars Strojny <lars@strojny.net>
  29. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  30. * @link http://php-ids.org/
  31. */
  32. require_once 'IDS/Caching/Interface.php';
  33. /**
  34. * File caching wrapper
  35. *
  36. * This class inhabits functionality to get and set cache via a static flatfile.
  37. *
  38. * @category Security
  39. * @package PHPIDS
  40. * @author Christian Matthies <ch0012@gmail.com>
  41. * @author Mario Heiderich <mario.heiderich@gmail.com>
  42. * @author Lars Strojny <lars@strojny.net>
  43. * @copyright 2007-2009 The PHPIDS Group
  44. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  45. * @version Release: $Id:File.php 517 2007-09-15 15:04:13Z mario $
  46. * @link http://php-ids.org/
  47. * @since Version 0.4
  48. */
  49. class IDS_Caching_File implements IDS_Caching_Interface
  50. {
  51. /**
  52. * Caching type
  53. *
  54. * @var string
  55. */
  56. private $type = null;
  57. /**
  58. * Cache configuration
  59. *
  60. * @var array
  61. */
  62. private $config = null;
  63. /**
  64. * Path to cache file
  65. *
  66. * @var string
  67. */
  68. private $path = null;
  69. /**
  70. * Holds an instance of this class
  71. *
  72. * @var object
  73. */
  74. private static $cachingInstance = null;
  75. /**
  76. * Constructor
  77. *
  78. * @param string $type caching type
  79. * @param object $init the IDS_Init object
  80. *
  81. * @return void
  82. */
  83. public function __construct($type, $init)
  84. {
  85. $this->type = $type;
  86. $this->config = $init->config['Caching'];
  87. $this->path = $init->getBasePath() . $this->config['path'];
  88. if (file_exists($this->path) && !is_writable($this->path)) {
  89. throw new Exception('Make sure all files in ' .
  90. htmlspecialchars($this->path, ENT_QUOTES, 'UTF-8') .
  91. 'are writeable!');
  92. }
  93. }
  94. /**
  95. * Returns an instance of this class
  96. *
  97. * @param string $type caching type
  98. * @param object $init the IDS_Init object
  99. *
  100. * @return object $this
  101. */
  102. public static function getInstance($type, $init)
  103. {
  104. if (!self::$cachingInstance) {
  105. self::$cachingInstance = new IDS_Caching_File($type, $init);
  106. }
  107. return self::$cachingInstance;
  108. }
  109. /**
  110. * Writes cache data into the file
  111. *
  112. * @param array $data the cache data
  113. *
  114. * @throws Exception if cache file couldn't be created
  115. * @return object $this
  116. */
  117. public function setCache(array $data)
  118. {
  119. if (!is_writable(preg_replace('/[\/][^\/]+\.[^\/]++$/', null,
  120. $this->path))) {
  121. throw new Exception('Temp directory ' .
  122. htmlspecialchars($this->path, ENT_QUOTES, 'UTF-8') .
  123. ' seems not writable');
  124. }
  125. if ((!file_exists($this->path) || (time()-filectime($this->path)) >
  126. $this->config['expiration_time'])) {
  127. $handle = @fopen($this->path, 'w+');
  128. $serialized = @serialize($data);
  129. if (!$handle) {
  130. throw new Exception("Cache file couldn't be created");
  131. }
  132. if (!$serialized) {
  133. throw new Exception("Cache data couldn't be serialized");
  134. }
  135. fwrite($handle, $serialized);
  136. fclose($handle);
  137. }
  138. return $this;
  139. }
  140. /**
  141. * Returns the cached data
  142. *
  143. * Note that this method returns false if either type or file cache is
  144. * not set
  145. *
  146. * @return mixed cache data or false
  147. */
  148. public function getCache()
  149. {
  150. // make sure filters are parsed again if cache expired
  151. if (file_exists($this->path) && (time()-filectime($this->path)) <
  152. $this->config['expiration_time']) {
  153. $data = unserialize(file_get_contents($this->path));
  154. return $data;
  155. }
  156. return false;
  157. }
  158. }
  159. /**
  160. * Local variables:
  161. * tab-width: 4
  162. * c-basic-offset: 4
  163. * End:
  164. * vim600: sw=4 ts=4 expandtab
  165. */