10 defined(
'JPATH_PLATFORM') or die;
25 public static $_handler = array();
40 public function __construct($options)
44 $this->_options = array(
45 'cachebase' => $conf->get(
'cache_path', JPATH_CACHE),
46 'lifetime' => (int) $conf->get(
'cachetime'),
47 'language' => $conf->get(
'language',
'en-GB'),
48 'storage' => $conf->get(
'cache_handler',
''),
49 'defaultgroup' =>
'default',
53 'caching' => ($conf->get(
'caching') >= 1) ?
true :
false);
56 foreach ($options as $option => $value)
58 if (isset($options[$option]) && $options[$option] !==
'')
60 $this->_options[$option] = $options[$option];
64 if (empty($this->_options[
'storage']))
66 $this->_options[
'caching'] =
false;
80 public static function getInstance($type =
'output', $options = array())
92 public static function getStores()
97 $iterator =
new DirectoryIterator(__DIR__ .
'/storage');
99 foreach ($iterator as $file)
101 $fileName = $file->getFilename();
106 || substr($fileName, strrpos($fileName,
'.') + 1) !=
'php'
107 || $fileName ==
'helper.php')
113 $class = str_ireplace(
'.php',
'',
'JCacheStorage' . ucfirst(trim($fileName)));
116 if (!class_exists($class))
122 if ($class::isSupported())
125 $handlers[] = str_ireplace(
'.php',
'', $fileName);
141 public function setCaching($enabled)
143 $this->_options[
'caching'] = $enabled;
153 public function getCaching()
155 return $this->_options[
'caching'];
167 public function setLifeTime($lt)
169 $this->_options[
'lifetime'] = $lt;
182 public function get($id, $group = null)
185 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
188 $handler = $this->_getStorage();
189 if (!($handler instanceof Exception) && $this->_options[
'caching'])
191 return $handler->get($id, $group, $this->_options[
'checkTime']);
203 public function getAll()
206 $handler = $this->_getStorage();
207 if (!($handler instanceof Exception) && $this->_options[
'caching'])
209 return $handler->getAll();
225 public function store($data, $id, $group = null)
228 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
231 $handler = $this->_getStorage();
232 if (!($handler instanceof Exception) && $this->_options[
'caching'])
234 $handler->_lifetime = $this->_options[
'lifetime'];
235 return $handler->store($id, $group, $data);
250 public function remove($id, $group = null)
253 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
256 $handler = $this->_getStorage();
257 if (!($handler instanceof Exception))
259 return $handler->remove($id, $group);
277 public function clean($group = null, $mode =
'group')
280 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
283 $handler = $this->_getStorage();
284 if (!($handler instanceof Exception))
286 return $handler->clean($group, $mode);
301 $handler = $this->_getStorage();
302 if (!($handler instanceof Exception))
304 return $handler->gc();
320 public function lock($id, $group = null, $locktime = null)
322 $returning =
new stdClass;
323 $returning->locklooped =
false;
326 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
329 $locktime = ($locktime) ? $locktime : $this->_options[
'locktime'];
333 $handler = $this->_getStorage();
334 if (!($handler instanceof Exception) && $this->_options[
'locking'] ==
true && $this->_options[
'caching'] ==
true)
336 $locked = $handler->lock($id, $group, $locktime);
337 if ($locked !==
false)
344 $curentlifetime = $this->_options[
'lifetime'];
347 $this->_options[
'lifetime'] = $locktime;
349 $looptime = $locktime * 10;
350 $id2 = $id .
'_lock';
352 if ($this->_options[
'locking'] ==
true && $this->_options[
'caching'] ==
true)
354 $data_lock = $this->
get($id2, $group);
360 $returning->locked =
false;
363 if ($data_lock !==
false)
369 while ($data_lock !==
false)
372 if ($lock_counter > $looptime)
374 $returning->locked =
false;
375 $returning->locklooped =
true;
380 $data_lock = $this->
get($id2, $group);
385 if ($this->_options[
'locking'] ==
true && $this->_options[
'caching'] ==
true)
387 $returning->locked = $this->store(1, $id2, $group);
391 $this->_options[
'lifetime'] = $curentlifetime;
406 public function unlock($id, $group = null)
411 $group = ($group) ? $group : $this->_options[
'defaultgroup'];
414 $handler = $this->_getStorage();
415 if (!($handler instanceof Exception) && $this->_options[
'caching'])
417 $unlocked = $handler->unlock($id, $group);
418 if ($unlocked !==
false)
425 if ($this->_options[
'caching'])
427 $unlock = $this->
remove($id .
'_lock', $group);
440 public function &_getStorage()
442 $hash = md5(serialize($this->_options));
444 if (isset(self::$_handler[$hash]))
446 return self::$_handler[$hash];
450 return self::$_handler[$hash];
463 public static function getWorkarounds($data, $options = array())
470 if (isset($options[
'mergehead']) && $options[
'mergehead'] == 1 && isset($data[
'head']) && !empty($data[
'head']))
472 $document->mergeHeadData($data[
'head']);
474 elseif (isset($data[
'head']) && method_exists($document,
'setHeadData'))
476 $document->setHeadData($data[
'head']);
480 if (isset($data[
'pathway']) && is_array($data[
'pathway']))
483 $pathway = $app->getPathWay();
484 $pathway->setPathway($data[
'pathway']);
489 if (isset($data[
'module']) && is_array($data[
'module']))
492 foreach ($data[
'module'] as $name => $contents)
494 $document->setBuffer($contents,
'module', $name);
499 if (isset($data[
'headers']) && $data[
'headers'])
501 foreach($data[
'headers'] as $header)
503 $app->setHeader($header[
'name'], $header[
'value']);
509 if (isset($data[
'body']))
512 $search =
'#<input type="hidden" name="[0-9a-f]{32}" value="1" />#';
513 $replacement =
'<input type="hidden" name="' . $token .
'" value="1" />';
515 $data[
'body'] = preg_replace($search, $replacement, $data[
'body']);
516 $body = $data[
'body'];
533 public static function setWorkarounds($data, $options = array())
542 if (isset($options[
'nopathway']))
544 $loptions[
'nopathway'] = $options[
'nopathway'];
547 if (isset($options[
'nohead']))
549 $loptions[
'nohead'] = $options[
'nohead'];
552 if (isset($options[
'nomodules']))
554 $loptions[
'nomodules'] = $options[
'nomodules'];
557 if (isset($options[
'modulemode']))
559 $loptions[
'modulemode'] = $options[
'modulemode'];
565 if ($loptions[
'nomodules'] != 1)
568 $buffer1 = $document->getBuffer();
569 if (!is_array($buffer1))
575 if (!isset($buffer1[
'module']) || !is_array($buffer1[
'module']))
577 $buffer1[
'module'] = array();
582 $cached[
'body'] = $data;
585 if ($loptions[
'nohead'] != 1 && method_exists($document,
'getHeadData'))
588 if ($loptions[
'modulemode'] == 1)
590 $headnow = $document->getHeadData();
591 $unset = array(
'title',
'description',
'link',
'links',
'metaTags');
593 foreach ($unset as $un)
595 unset($headnow[$un]);
596 unset($options[
'headerbefore'][$un]);
599 $cached[
'head'] = array();
602 foreach ($headnow as $now => $value)
604 if (isset($options[
'headerbefore'][$now]))
607 $nowvalue = array_map(
'serialize', $headnow[$now]);
608 $beforevalue = array_map(
'serialize', $options[
'headerbefore'][$now]);
609 $newvalue = array_diff_assoc($nowvalue, $beforevalue);
610 $newvalue = array_map(
'unserialize', $newvalue);
614 $newvalue = $headnow[$now];
617 if (!empty($newvalue))
619 $cached[
'head'][$now] = $newvalue;
626 $cached[
'head'] = $document->getHeadData();
631 if ($app->isSite() && $loptions[
'nopathway'] != 1)
633 $pathway = $app->getPathWay();
634 $cached[
'pathway'] = isset($data[
'pathway']) ? $data[
'pathway'] : $pathway->getPathway();
637 if ($loptions[
'nomodules'] != 1)
641 $buffer2 = $document->getBuffer();
642 if (!is_array($buffer2))
648 if (!isset($buffer2[
'module']) || !is_array($buffer2[
'module']))
650 $buffer2[
'module'] = array();
654 $cached[
'module'] = array_diff_assoc($buffer2[
'module'], $buffer1[
'module']);
658 if (isset($options[
'headers']) && $options[
'headers'])
660 $cached[
'headers'] = $app->getHeaders();
673 public static function makeId()
678 if (!empty($app->registeredurlparams))
680 $registeredurlparams = $app->registeredurlparams;
684 $registeredurlparams->format =
'WORD';
685 $registeredurlparams->option =
'WORD';
686 $registeredurlparams->view =
'WORD';
687 $registeredurlparams->layout =
'WORD';
688 $registeredurlparams->tpl =
'CMD';
689 $registeredurlparams->id =
'INT';
691 $safeuriaddon =
new stdClass;
693 foreach ($registeredurlparams as $key => $value)
695 $safeuriaddon->$key = $app->input->get($key, null, $value);
698 return md5(serialize($safeuriaddon));
711 public static function addIncludePath($path =
'')
719 if (!empty($path) && !in_array($path, $paths))
721 jimport(
'joomla.filesystem.path');