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