Elgg  Version 2.3
ElggFileCache.php
Go to the documentation of this file.
1 <?php
9 class ElggFileCache extends \ElggCache {
19  public 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("Cache path set to nothing!");
26  }
27  }
28 
37  protected function createFile($filename, $rw = "rb") {
38  // Create a filename matrix
39  $matrix = "";
40  $depth = strlen($filename);
41  if ($depth > 5) {
42  $depth = 5;
43  }
44 
45  // Create full path
46  $path = $this->getVariable("cache_path") . $matrix;
47  if (!is_dir($path)) {
48  mkdir($path, 0700, true);
49  }
50 
51  // Open the file
52  if ((!file_exists($path . $filename)) && ($rw == "rb")) {
53  return false;
54  }
55 
56  return fopen($path . $filename, $rw);
57  }
58 
59  // @codingStandardsIgnoreStart
68  protected function sanitise_filename($key) {
69  return $this->sanitizeFilename($key);
70  }
71  // @codingStandardsIgnoreEnd
72 
80  protected function sanitizeFilename($key) {
81  // handles all keys in use by core
82  if (preg_match('~^[a-zA-Z0-9\-_\.]{1,250}$~', $key)) {
83  return $key;
84  }
85 
86  $key = md5($key);
87 
88  // prevent collision with un-hashed keys
89  $key = "=" . $key;
90 
91  return $key;
92  }
93 
102  public function save($key, $data) {
103  $f = $this->createFile($this->sanitizeFilename($key), "wb");
104  if ($f) {
105  $result = fwrite($f, $data);
106  fclose($f);
107 
108  return $result;
109  }
110 
111  return false;
112  }
113 
123  public function load($key, $offset = 0, $limit = null) {
124  $f = $this->createFile($this->sanitizeFilename($key));
125  if ($f) {
126  if (!$limit) {
127  $limit = -1;
128  }
129 
130  $data = stream_get_contents($f, $limit, $offset);
131 
132  fclose($f);
133 
134  return $data;
135  }
136 
137  return false;
138  }
139 
147  public function delete($key) {
148  $dir = $this->getVariable("cache_path");
149 
150  if (file_exists($dir . $key)) {
151  return unlink($dir . $key);
152  }
153  return true;
154  }
155 
161  public function clear() {
162  $dir = $this->getVariable("cache_path");
163 
164  $exclude = array(".", "..");
165 
166  $files = scandir($dir);
167  if (!$files) {
168  return;
169  }
170 
171  foreach ($files as $f) {
172  if (!in_array($f, $exclude)) {
173  unlink($dir . $f);
174  }
175  }
176  }
177 
183  public function __destruct() {
184  // @todo Check size and age, clean up accordingly
185  $size = 0;
186  $dir = $this->getVariable("cache_path");
187 
188  // Short circuit if both size and age are unlimited
189  if (($this->getVariable("max_age") == 0) && ($this->getVariable("max_size") == 0)) {
190  return;
191  }
192 
193  $exclude = array(".", "..");
194 
195  $files = scandir($dir);
196  if (!$files) {
197  throw new \IOException($dir . " is not a directory.");
198  }
199 
200  // Perform cleanup
201  foreach ($files as $f) {
202  if (!in_array($f, $exclude)) {
203  $stat = stat($dir . $f);
204 
205  // Add size
206  $size .= $stat['size'];
207 
208  // Is this older than my maximum date?
209  if (($this->getVariable("max_age") > 0) && (time() - $stat['mtime'] > $this->getVariable("max_age"))) {
210  unlink($dir . $f);
211  }
212 
213  // @todo Size
214  }
215  }
216  }
217 }
createFile($filename, $rw="rb")
Create and return a handle to a file.
__destruct()
Preform cleanup and invalidates cache upon object destruction.
$data
Definition: opendd.php:13
$path
Definition: details.php:88
if(!$count) $offset
Definition: pagination.php:26
$limit
Definition: userpicker.php:38
$key
Definition: summary.php:34
load($key, $offset=0, $limit=null)
Load a key.
sanitizeFilename($key)
Create a sanitised filename for the file.
save($key, $data)
Save a key.
__construct($cache_path, $max_age=0, $max_size=0)
Set the Elgg cache.
$size
Definition: default.php:20
elgg subtext time
clear()
Delete all files in the directory of this file cache.
$filename
setVariable($variable, $value)
Set a cache variable.
Definition: ElggCache.php:32
sanitise_filename($key)
Create a sanitised filename for the file.
foreach($resources as $id=> $href) if(!empty($resources_html)) $files
Definition: details.php:141
getVariable($variable)
Get variables for this cache.
Definition: ElggCache.php:47