1: <?php
2: 3: 4: 5: 6: 7: 8:
9: class ElggFileCache extends ElggCache {
10: 11: 12: 13: 14: 15: 16: 17: 18:
19: function __construct($cache_path, $max_age = 0, $max_size = 0) {
20: $this->setVariable("cache_path", $cache_path);
21: $this->setVariable("max_age", $max_age);
22: $this->setVariable("max_size", $max_size);
23:
24: if ($cache_path == "") {
25: throw new ConfigurationException(elgg_echo('ConfigurationException:NoCachePath'));
26: }
27: }
28:
29:
30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: protected function create_file($filename, $rw = "rb") {
41: elgg_deprecated_notice('ElggFileCache::create_file() is deprecated by ::createFile()', 1.8);
42:
43: return $this->createFile($filename, $rw);
44: }
45:
46:
47: 48: 49: 50: 51: 52: 53: 54:
55: protected function createFile($filename, $rw = "rb") {
56:
57: $matrix = "";
58: $depth = strlen($filename);
59: if ($depth > 5) {
60: $depth = 5;
61: }
62:
63:
64: $path = $this->getVariable("cache_path") . $matrix;
65: if (!is_dir($path)) {
66: mkdir($path, 0700, true);
67: }
68:
69:
70: if ((!file_exists($path . $filename)) && ($rw == "rb")) {
71: return false;
72: }
73:
74: return fopen($path . $filename, $rw);
75: }
76:
77:
78: 79: 80: 81: 82: 83: 84: 85: 86:
87: protected function sanitise_filename($filename) {
88:
89:
90: return $filename;
91: }
92:
93:
94: 95: 96: 97: 98: 99: 100:
101: protected function sanitizeFilename($filename) {
102:
103:
104: return $filename;
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114:
115: public function save($key, $data) {
116: $f = $this->createFile($this->sanitizeFilename($key), "wb");
117: if ($f) {
118: $result = fwrite($f, $data);
119: fclose($f);
120:
121: return $result;
122: }
123:
124: return false;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135:
136: public function load($key, $offset = 0, $limit = null) {
137: $f = $this->createFile($this->sanitizeFilename($key));
138: if ($f) {
139: if (!$limit) {
140: $limit = -1;
141: }
142:
143: $data = stream_get_contents($f, $limit, $offset);
144:
145: fclose($f);
146:
147: return $data;
148: }
149:
150: return false;
151: }
152:
153: 154: 155: 156: 157: 158: 159:
160: public function delete($key) {
161: $dir = $this->getVariable("cache_path");
162:
163: if (file_exists($dir . $key)) {
164: return unlink($dir . $key);
165: }
166: return TRUE;
167: }
168:
169: 170: 171: 172: 173:
174: public function clear() {
175: $dir = $this->getVariable("cache_path");
176:
177: $exclude = array(".", "..");
178:
179: $files = scandir($dir);
180: if (!$files) {
181: return;
182: }
183:
184: foreach ($files as $f) {
185: if (!in_array($f, $exclude)) {
186: unlink($dir . $f);
187: }
188: }
189: }
190:
191: 192: 193: 194: 195:
196: public function __destruct() {
197:
198: $size = 0;
199: $dir = $this->getVariable("cache_path");
200:
201:
202: if (($this->getVariable("max_age") == 0) && ($this->getVariable("max_size") == 0)) {
203: return;
204: }
205:
206: $exclude = array(".", "..");
207:
208: $files = scandir($dir);
209: if (!$files) {
210: throw new IOException(elgg_echo('IOException:NotDirectory', array($dir)));
211: }
212:
213:
214: foreach ($files as $f) {
215: if (!in_array($f, $exclude)) {
216: $stat = stat($dir . $f);
217:
218:
219: $size .= $stat['size'];
220:
221:
222: if (($this->getVariable("max_age") > 0) && (time() - $stat['mtime'] > $this->getVariable("max_age"))) {
223: unlink($dir . $f);
224: }
225:
226:
227: }
228: }
229: }
230: }
231: