public function __construct($calling_from_hook = FALSE)
{
$this->EE =& get_instance();
// load dependencies - make sure the package path is available in case the class is being called statically
$this->EE->load->add_package_path(PATH_THIRD . 'stash/', TRUE);
$this->EE->lang->loadfile('stash');
$this->EE->load->model('stash_model');
// default site id
$this->site_id = $this->EE->config->item('site_id');
// config defaults
$this->path = $this->EE->config->item('stash_file_basepath') ? $this->EE->config->item('stash_file_basepath') : APPPATH . 'stash/';
$this->file_sync = $this->_get_boolean_config_item('stash_file_sync', FALSE);
// default = FALSE
$this->stash_cookie = $this->EE->config->item('stash_cookie') ? $this->EE->config->item('stash_cookie') : 'stashid';
$this->stash_cookie_expire = $this->EE->config->item('stash_cookie_expire') ? $this->EE->config->item('stash_cookie_expire') : 0;
$this->stash_cookie_enabled = $this->_get_boolean_config_item('stash_cookie_enabled');
// default = TRUE
$this->default_scope = $this->EE->config->item('stash_default_scope') ? $this->EE->config->item('stash_default_scope') : 'user';
$this->default_refresh = $this->EE->config->item('stash_default_refresh') ? $this->EE->config->item('stash_default_refresh') : 0;
// minutes
$this->limit_bots = $this->_get_boolean_config_item('stash_limit_bots', FALSE);
// default = FALSE
// cache pruning can cache stampede mitigation defaults
$this->prune = $this->_get_boolean_config_item('stash_prune_enabled');
// default = TRUE
$this->prune_probability = $this->EE->config->item('stash_prune_probability') ? $this->EE->config->item('stash_prune_probability') : 0.4;
// percent
$this->invalidation_period = $this->EE->config->item('stash_invalidation_period') ? $this->EE->config->item('stash_invalidation_period') : 0;
// seconds
// permitted file extensions for Stash embeds
$this->file_extensions = $this->EE->config->item('stash_file_extensions') ? (array) $this->EE->config->item('stash_file_extensions') : array('html', 'md', 'css', 'js', 'rss', 'xml');
// Support {if var1 IN (var2) }...{/if} style conditionals in Stash templates / tagdata?
$this->parse_if_in = $this->EE->config->item('stash_parse_if_in') ? $this->EE->config->item('stash_parse_if_in') : FALSE;
// include query string when using the @URI context (full page caching)?
$this->include_query_str = $this->EE->config->item('stash_query_strings') ? $this->EE->config->item('stash_query_strings') : FALSE;
// initialise tag parameters
if (FALSE === $calling_from_hook) {
$this->init();
}
// fetch the stash session id
if ($this->stash_cookie_enabled) {
if (!isset($this->EE->session->cache['stash']['_session_id'])) {
// do we have a stash cookie?
if ($cookie_data = $this->_get_stash_cookie()) {
// YES - restore session
$this->EE->session->cache['stash']['_session_id'] = $cookie_data['id'];
// shall we prune expired variables?
if ($this->prune) {
// probability that pruning occurs
$prune_chance = 100 / $this->prune_probability;
// trigger pruning every 1 chance out of $prune_chance
if (mt_rand(0, $prune_chance - 1) === 0) {
// prune variables with expiry date older than right now
$this->EE->stash_model->prune_keys();
}
}
} else {
if ($this->limit_bots) {
// Is the user a human? Legitimate bots don't set cookies so will end up here every page load
// Humans who accept cookies only get checked when the cookie is first set
self::$_is_human = $this->_is_bot() ? FALSE : TRUE;
}
// NO - let's generate a unique id
$unique_id = $this->EE->functions->random();
// add to stash array
$this->EE->session->cache['stash']['_session_id'] = $unique_id;
// create a cookie; store the creation date in the cookie itself
$this->_set_stash_cookie($unique_id);
}
}
// create a reference to the session id
$this->_session_id =& $this->EE->session->cache['stash']['_session_id'];
} else {
$this->_session_id = '_global';
}
}