1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12  13  14  15  16  17  18 
 19 class JCacheStorageCachelite extends JCacheStorage
 20 {
 21      22  23  24  25  26 
 27     protected static $CacheLiteInstance = null;
 28 
 29      30  31  32  33  34 
 35     protected $_root;
 36 
 37      38  39  40  41  42  43 
 44     public function __construct($options = array())
 45     {
 46         parent::__construct($options);
 47 
 48         $this->_root = $options['cachebase'];
 49 
 50         $cloptions = array(
 51             'cacheDir'                => $this->_root . '/',
 52             'lifeTime'                => $this->_lifetime,
 53             'fileLocking'             => $this->_locking,
 54             'automaticCleaningFactor' => isset($options['autoclean']) ? $options['autoclean'] : 200,
 55             'fileNameProtection'      => false,
 56             'hashedDirectoryLevel'    => 0,
 57             'caching'                 => $options['caching'],
 58         );
 59 
 60         if (static::$CacheLiteInstance === null)
 61         {
 62             $this->initCache($cloptions);
 63         }
 64     }
 65 
 66      67  68  69  70  71  72  73  74 
 75     protected function initCache($cloptions)
 76     {
 77         if (!class_exists('Cache_Lite'))
 78         {
 79             require_once 'Cache/Lite.php';
 80         }
 81 
 82         static::$CacheLiteInstance = new Cache_Lite($cloptions);
 83 
 84         return static::$CacheLiteInstance;
 85     }
 86 
 87      88  89  90  91  92  93  94  95  96 
 97     public function contains($id, $group)
 98     {
 99         return $this->get($id, $group) !== false;
100     }
101 
102     103 104 105 106 107 108 109 110 111 112 
113     public function get($id, $group, $checkTime = true)
114     {
115         static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
116 
117         
118         $this->_getCacheId($id, $group);
119 
120         return static::$CacheLiteInstance->get($this->rawname, $group);
121     }
122 
123     124 125 126 127 128 129 
130     public function getAll()
131     {
132         $path    = $this->_root;
133         $folders = new DirectoryIterator($path);
134         $data    = array();
135 
136         foreach ($folders as $folder)
137         {
138             if (!$folder->isDir() || $folder->isDot())
139             {
140                 continue;
141             }
142 
143             $foldername = $folder->getFilename();
144 
145             $files = new DirectoryIterator($path . '/' . $foldername);
146             $item  = new JCacheStorageHelper($foldername);
147 
148             foreach ($files as $file)
149             {
150                 if (!$file->isFile())
151                 {
152                     continue;
153                 }
154 
155                 $filename = $file->getFilename();
156 
157                 $item->updateSize(filesize($path . '/' . $foldername . '/' . $filename));
158             }
159 
160             $data[$foldername] = $item;
161         }
162 
163         return $data;
164     }
165 
166     167 168 169 170 171 172 173 174 175 176 
177     public function store($id, $group, $data)
178     {
179         $dir = $this->_root . '/' . $group;
180 
181         
182         if (!is_dir($dir))
183         {
184             
185             $indexFile = $dir . '/index.html';
186             @mkdir($dir) && file_put_contents($indexFile, '<!DOCTYPE html><title></title>');
187         }
188 
189         
190         if (!is_dir($dir))
191         {
192             return false;
193         }
194 
195         static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
196 
197         
198         $this->_getCacheId($id, $group);
199 
200         return static::$CacheLiteInstance->save($data, $this->rawname, $group);
201     }
202 
203     204 205 206 207 208 209 210 211 212 
213     public function remove($id, $group)
214     {
215         static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
216 
217         
218         $this->_getCacheId($id, $group);
219 
220         return static::$CacheLiteInstance->remove($this->rawname, $group);
221     }
222 
223     224 225 226 227 228 229 230 231 232 233 234 235 
236     public function clean($group, $mode = null)
237     {
238         jimport('joomla.filesystem.folder');
239         jimport('joomla.filesystem.file');
240 
241         switch ($mode)
242         {
243             case 'notgroup':
244                 $clmode  = 'notingroup';
245                 $success = static::$CacheLiteInstance->clean($group, $clmode);
246                 break;
247 
248             case 'group':
249                 if (is_dir($this->_root . '/' . $group))
250                 {
251                     $clmode = $group;
252                     static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
253                     $success = static::$CacheLiteInstance->clean($group, $clmode);
254 
255                     
256                     $folders = JFolder::folders($this->_root . '/' . $group, '.', false, true, array(), array());
257 
258                     foreach ($folders as $folder)
259                     {
260                         if (is_link($folder))
261                         {
262                             if (JFile::delete($folder) !== true)
263                             {
264                                 return false;
265                             }
266                         }
267                         elseif (JFolder::delete($folder) !== true)
268                         {
269                             return false;
270                         }
271                     }
272                 }
273                 else
274                 {
275                     $success = true;
276                 }
277 
278                 break;
279 
280             default:
281                 if (is_dir($this->_root . '/' . $group))
282                 {
283                     $clmode = $group;
284                     static::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
285                     $success = static::$CacheLiteInstance->clean($group, $clmode);
286                 }
287                 else
288                 {
289                     $success = true;
290                 }
291 
292                 break;
293         }
294 
295         return $success;
296     }
297 
298     299 300 301 302 303 304 
305     public function gc()
306     {
307         $result = true;
308         static::$CacheLiteInstance->setOption('automaticCleaningFactor', 1);
309         static::$CacheLiteInstance->setOption('hashedDirectoryLevel', 1);
310         $success1 = static::$CacheLiteInstance->_cleanDir($this->_root . '/', false, 'old');
311 
312         if (!($dh = opendir($this->_root . '/')))
313         {
314             return false;
315         }
316 
317         while ($file = readdir($dh))
318         {
319             if (($file != '.') && ($file != '..') && ($file != '.svn'))
320             {
321                 $file2 = $this->_root . '/' . $file;
322 
323                 if (is_dir($file2))
324                 {
325                     $result = ($result && (static::$CacheLiteInstance->_cleanDir($file2 . '/', false, 'old')));
326                 }
327             }
328         }
329 
330         $success = ($success1 && $result);
331 
332         return $success;
333     }
334 
335     336 337 338 339 340 341 
342     public static function isSupported()
343     {
344         @include_once 'Cache/Lite.php';
345 
346         return class_exists('Cache_Lite');
347     }
348 }
349