public function __construct() { parent::__construct(); if (!($this->caches[$this->cache_prefix] = $this->cache->get($this->cache_prefix))) { $query = $this->get_many_by('active', 1); foreach ((array) $query as $row) { $this->caches[$this->cache_prefix][$row->match] = $row->url; } $this->cache->save($this->cache_prefix, $this->caches[$this->cache_prefix], get_ttl()); } }
protected function _firstaccess() { switch ($this->mode) { case 'database': if (!($value = ci()->cache->get($this->database_table))) { $this->database_resource = ci()->load->database($this->database_group, true); $dbc = $this->database_resource->get_where($this->database_table, ['id' => 1]); $dbr = $dbc->row(); $value = $dbr->value; ci()->cache->save($this->database_table, $value, get_ttl()); } $this->appvar = json_decode($value, true); break; case 'file': if (file_exists($this->appvarpath) == true) { $this->appvar = (include $this->appvarpath); } break; } }
public function show($options) { if (in_array($options['widget'], $this->force_token) || $this->token_required) { if ($options['wkey'] !== $this->token) { $this->_throw_error('Token Validation Failed'); } } /* remove the wkey because it changes with each page request and we already tested it */ unset($options['wkey']); /* are they asking for a user? if so we insert it here so it's on the cache key */ if ($options['user'] == 'true') { $options['user'] = isset(ci()->user->id) ? ci()->user->id : 0; } /* are they asking for a role? if so we insert it here so it's on the cache key */ if ($options['role'] == 'true') { $options['role'] = isset(ci()->user->role_id) ? ci()->user->role_id : 0; } /* In code are we forcing a user? if so we insert it here so it's on the cache key */ if (in_array($options['widget'], $this->force_user)) { $options['user'] = $this->force_user[$options['widget']]; } /* In code are we forcing a role? if so we insert it here so it's on the cache key */ if (in_array($options['widget'], $this->force_role)) { $options['role'] = $this->force_role[$options['widget']]; } /* In code are we forcing a cache? if so we insert it here so it's on the cache key */ if (in_array($options['widget'], $this->force_cache)) { $options['cache'] = $this->force_cache[$options['widget']]; } /* ok now we can figure out the cache key */ $cache_name = 'widget_' . md5(json_encode($options)); /* was this cached or has it expired? */ if (!($output = ci()->cache->get($cache_name))) { /* add a validation rule just for widgets */ ci()->validate->attach('widget_command', function ($validate, $field, $options) { /* make sure we only have 2 parts ie 1 colon */ if (count(explode(':', $field)) !== 2) { return false; } return (bool) preg_match("#^[0-9A-Za-z\\/:_]+\$#", $field); }); /* validate the library and class */ if (!ci()->input->is_valid('required|max_length[64]|widget_command', $options['widget'], false)) { $this->_throw_error('Unknown Widget'); } /* verification passed - split off the library and method */ list($class, $method) = explode(':', $options['widget'], 2); /* add widget to the begining of the class name */ $classname = 'Widget_' . basename($class); /* Let PHP try to autoload it through any available autoloaders (including Composer and user's custom autoloaders). If we don't find it, then assume it's a CI library that we can reach. */ if (class_exists($classname)) { $obj = new $classname(); } else { $classfile = trim(dirname($class) . '/' . $classname, '/.'); if (!ci()->load->exists($classfile)) { $this->_throw_error('Widget Not Found'); } ci()->load->library($classfile); $classname = strtolower($classname); $obj =& ci()->{$classname}; } if (!method_exists($obj, $method)) { /* failed - can't find the method */ $this->_throw_error('Widget Method Not Found'); } /* Call the class with our parameters */ $output = $obj->{$method}($options); /* cache length - use parameter cache="0" for no cache */ $cache_ttl = isset($options['cache']) ? (int) $options['cache'] : get_ttl(false); /* cache it */ if ($cache_ttl > 0) { ci()->cache->save($cache_name, $output, $cache_ttl + mt_rand(1, 120)); } } return $output; }