17 require_once SQ_INCLUDE_PATH.
'/asset.inc';
18 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
19 require_once SQ_FUDGE_PATH.
'/db_extras/db_extras.inc';
47 public $global_cache = FALSE;
58 $this->_ser_attrs = TRUE;
59 parent::__construct($assetid);
61 $storage_type = $this->
attr(
'cache_storage_type');
74 $storage_type = $this->
attr(
'cache_storage_type');
91 require_once SQ_CORE_PACKAGE_PATH.
'/system/system_asset_fns.inc';
92 if (!system_asset_fns_create_pre_check($this)) {
95 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
96 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
98 if ($linkid = parent::create($link)) {
99 if (!system_asset_fns_create_cleanup($this)) {
105 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
107 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
110 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
126 return $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($this->
type(),
'name');
182 if (basename($_SERVER[
'PHP_SELF']) == SQ_CONF_RECACHE_SUFFIX) {
190 $perm_key = $this->
_getPermKey($assetid, $cache_key);
192 if (empty($perm_key))
return FALSE;
194 if ($use_url && !$this->global_cache) {
195 $url = current_url();
197 if (isset($_SERVER[
'QUERY_STRING']) && !empty($_SERVER[
'QUERY_STRING'])) {
203 $cache_key = md5($cache_key.$assetid);
205 return $this->_storage->read($cache_key, $perm_key, $url, $assetid);
227 function saveToCache($assetid, $asset_type, $cache_key, $contents, $use_url=TRUE, $expiry_override=
'')
229 if (!$this->
cacheEnabled($asset_type, $assetid))
return FALSE;
230 $perm_key = $this->
_getPermKey($assetid, $cache_key);
232 if (empty($perm_key))
return FALSE;
234 if ($use_url && !$this->global_cache) {
235 $url = current_url();
237 if (isset($_SERVER[
'QUERY_STRING']) && !empty($_SERVER[
'QUERY_STRING'])) {
243 $cache_key = md5($cache_key.$assetid);
246 if (is_int($expiry_override) && ($expiry_override >= 0) && ($expiry_override + $now > $now)) {
247 $expiry = $expiry_override + $now;
249 $expiry = $now + $this->
getExpiry($asset_type, $assetid);
251 return $this->_storage->store($cache_key, $perm_key, $url, $assetid, $contents, $expiry);
272 parse_str($_SERVER[
'QUERY_STRING'], $query_vars);
273 $mute_list = array_intersect(array_keys($query_vars), Array(
'SQ_BACKEND_PAGE',
'SQ_ACTION',
'SQ_DESIGN_NAME',
'SQ_CONTEXT_NAME'));
274 foreach ($mute_list as $name) {
275 unset($query_vars[$name]);
278 if (!empty($query_vars)) {
280 foreach ($query_vars as $key => $val) {
281 if (is_array($val)) {
282 foreach($val as $mkey => $mval) {
283 $new_query_str .= $key.
'['.$mkey.
']='.$mval.
'&';
286 $new_query_str .= $key.
'='.$val.
'&';
289 $url .=
'?'.rtrim($new_query_str,
'&');
307 if ($this->_storage->clear($assetids) === FALSE) {
311 foreach ($assetids as $assetid) {
312 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
313 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_matrix_cache_cleared', $asset, NULL);
314 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
333 return $this->_storage->getFilePaths($assetids);
348 if (is_object($this->_storage)) {
349 return $this->_storage->canClearByAssetid();
365 $this->_storage->clearAll();
380 return $this->_storage->getAllFilePaths($option);
400 $current_user = $GLOBALS[
'SQ_SYSTEM']->user;
401 if (is_null($current_user) || ($current_user instanceof
Public_User)) {
403 if ($this->
attr(
'public_caching')) {
404 if (is_null($current_user)) {
405 $perm_key =
'public';
409 $groups = array_values($current_user->getUserGroups());
411 $perm_key =
'public'.implode(
'|',$groups);
415 if (empty($cache_key)) {
418 if ($this->
attr(
'permission_caching')) {
419 $groups = array_keys($current_user->getGroups());
420 $userids = Array((
string) $current_user->id) + $groups;
426 'assetid' => $assetid,
427 'userids' => $userids,
431 throw new Exception(
'Unable to get maximum permission level of asset ID #'.$assetid.
' due to database error: '.$e->getMessage());
434 switch ($permission) {
435 case SQ_PERMISSION_READ :
436 $perm_key .=
':read';
438 case SQ_PERMISSION_WRITE :
439 $perm_key .=
':write';
441 case SQ_PERMISSION_ADMIN :
442 $perm_key .=
':admin';
449 if ($this->
attr(
'group_caching')) {
450 $groups = array_values($current_user->getUserGroups());
452 $perm_key = implode(
'|', $groups);
456 $perm_key = (empty($perm_key)) ?
'' : md5($perm_key);
472 $assetid = trim($assetid);
475 $bucketsize = $this->
attr(
'num_cache_dirs');
476 $len = strlen($assetid);
477 for ($i = 0; $i < $len; $i++) {
478 if (!is_numeric($assetid{$i})) {
479 $hash += ord($assetid{$i});
481 $hash += (int) $assetid{$i};
484 $hash = $hash % $bucketsize;
486 while (strlen($hash) != strlen($bucketsize)) {
508 if (SQ_ROLLBACK_VIEW)
return FALSE;
509 if (basename($_SERVER[
'PHP_SELF']) == SQ_CONF_NOCACHE_SUFFIX) {
512 if (array_get_index($_GET,
'SQ_ACTION') ==
'diff') {
515 if(defined(
'SQ_IN_PERFORMANCE_TIMING') && isset($_SESSION[
'SQ_PERFORMANCE_SETTING'][
'caching'])) {
516 if($_SESSION[
'SQ_PERFORMANCE_SETTING'][
'caching'] ===
'Off')
return FALSE;
520 $current_url = current_url(FALSE, TRUE);
521 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
522 $current_root_url =
'';
523 foreach ($root_urls as $url) {
524 if (0 === strpos($current_url, $url)) {
525 if (strlen($url) > strlen($current_root_url)) {
526 $current_root_url = $url;
530 if (in_array($current_root_url, $this->
attr(
'non_cachable_urls'))) {
539 if ($specific !== FALSE) {
543 return $this->
attr(
'enabled');
558 if (SQ_ROLLBACK_VIEW)
return FALSE;
559 if (basename($_SERVER[
'PHP_SELF']) == SQ_CONF_NOCACHE_SUFFIX) {
562 if (array_get_index($_GET,
'SQ_ACTION') ==
'diff') {
565 if(defined(
'SQ_IN_PERFORMANCE_TIMING'))
return FALSE;
568 $current_url = current_url(FALSE, TRUE);
569 $root_urls = explode(
"\n", SQ_CONF_SYSTEM_ROOT_URLS);
570 $current_root_url =
'';
571 foreach ($root_urls as $url) {
572 if (0 === strpos($current_url, $url)) {
573 if (strlen($url) > strlen($current_root_url)) {
574 $current_root_url = $url;
579 if (in_array($current_root_url, $this->
attr(
'non_cacheable_header_urls'))) {
586 if ($specific !== FALSE) {
590 $is_public = $GLOBALS[
'SQ_SYSTEM']->user instanceof
Public_User;
591 if (!$is_public && in_array($current_root_url, $this->
attr(
'user_cacheable_header_urls'))) {
595 return $is_public && SQ_CONF_SEND_CACHEABLE_HEADER;
615 if ($specific !== FALSE) {
619 return $this->
attr(
'expiry');
639 if ($specific !== FALSE) {
643 return $this->
attr(
'browser_cache_expiry');
663 $type_codes = $this->
attr(
'type_codes');
664 if (isset($type_codes[$type_code][$value])) {
665 return $type_codes[$type_code][$value];
668 $root_nodes = $this->
attr(
'root_nodes');
672 if (empty($assetid) && !empty($root_nodes)) {
674 }
else if (!empty($root_nodes)) {
676 if (isset($root_nodes[$assetid][$value])) {
677 return $root_nodes[$assetid][$value];
681 if(count($root_nodes) < 10) {
682 $matched_roots = $GLOBALS[
'SQ_SYSTEM']->am->isUnderRootNodes($assetid, array_keys($root_nodes), TRUE);
685 $matched_roots = array_keys($GLOBALS[
'SQ_SYSTEM']->am->getParents($assetid));
687 foreach ($matched_roots as $rootid) {
689 if (isset($root_nodes[$rootid][
'all_underneath']) && $root_nodes[$rootid][
'all_underneath']) {
691 if (isset($root_nodes[$rootid][$value])) {
692 return $root_nodes[$rootid][$value];
714 return $this->_storage;
728 $asset_type =
'cache_storage_'.strtolower($storage_type);
730 if ($GLOBALS[
'SQ_SYSTEM']->am->installed($asset_type) === FALSE) {
731 trigger_error(
'Unable to find "'.$storage_type.
'" storage type, reverting to default', E_USER_WARNING);
732 $asset_type =
'cache_storage_default';
735 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($asset_type);
737 if (eval(
'return '.$asset_type.
'::isAvailable();') === FALSE) {
738 trigger_error(
'The cache storage type "'.$storage_type.
'" cannot be used under current conditions, reverting to default', E_USER_WARNING);
739 $asset_type =
'cache_storage_default';
740 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($asset_type);
743 $this->_storage =
new $asset_type();
767 return $this->_storage->clearCachedAssetsByUrl($url);
783 $success = parent::setAttrValue($name, $value);
786 if ($name ===
'cache_storage_type' && $success) {
804 $protocol_setting = $this->
attr(
'cacheale_header_protocol');
805 if ($protocol_setting !=
'both') {
806 return current_protocol() ==
'http' ? $protocol_setting ==
'http' : $protocol_setting ==
'https';
823 return current_protocol() ==
'http' ? $this->
attr(
'cache_control_http') : $this->
attr(
'cache_control_https');