*/ define('DSX', DIRECTORY_SEPARATOR); define('FISHPIG_BOLT', true); define('FISHPIG_BOLT_DEBUG', true); define('FISHPIG_BOLT_PARAM_REFRESH', '___refresh'); if (FISHPIG_BOLT_DEBUG) { error_reporting(E_ALL); ini_set('display_errors', 1); } /** * Main Bolt class file * */ class Fishpig_Bolt { /** * Cache for config file * * @static array */ static protected $_config = null; /** * Cache for session file * * @static array */ static protected $_session = null; /** * Cache ID for current request * * @static array */ static protected $_cacheId = null; /** * Cache for the processed request uri * * @static string */ static protected $_requestUri = null; /** * System parameters that can be ignored * This is merged with the custom list from the config * * @static array */ static protected $_excludeParams = array( '___store', '___from_store', 'isAjax', FISHPIG_BOLT_PARAM_REFRESH, ); /** * A list of URI patterns that should not be cached * This is merged with config * * @static array */ static protected $_excludeUris = array(); /** * Flag that determines whether the store is being changed * * @static bool */ static protected $_isChangingStore = false; /** * Main method * * @return void */ static public function strike() { try { self::getSession(); if (self::getConfig() !== false && self::validateConfig()) { if (self::getRequestUri() !== false) { if (self::canLoadCachedRequest()) { if (($html = Fishpig_Bolt_Cache::getCachedPage(self::getCacheId())) !== false) { // Hole punch $html if enabled Fishpig_Bolt_Hole_Punch::punch($html); // Send response to the browser self::sendResponse($html); } } } } } catch (Exception $e) { if (FISHPIG_BOLT_DEBUG) { echo sprintf('
%s', $e->getMessage(), $e->getTraceAsString()); exit; } } include(dirname(__FILE__) . DSX . 'index.php'); } /** * Retrieve a config value * * @param string|null $key = null * @return mixed */ static public function getConfig($key = null) { if (is_null(self::$_config)) { // Initialise the config file self::$_config = array(); if (($config = self::getFile(self::getDir('app' . DSX . 'etc' . DSX . 'bolt.config'))) !== false) { self::$_config = (array)unserialize($config); } else { return false; } // Check for Admin URL if (preg_match('/^(' . basename(__FILE__) . '\/' . self::$_config['admin_route'] . '(\/|\z)|' . self::$_config['admin_route'] . '(\/|\z))/', trim($_SERVER['REQUEST_URI'], '/'))) { return false; } if (isset($_GET['___store'])) { self::$_isChangingStore = true; } if (isset($_GET['___store']) && ($store = self::getStoreByCode($_GET['___store'])) !== false) { self::$_isChangingStore = $_GET['___store'] !== self::getSession('core/store_code'); // Changing store via the query string } else if (is_array(($store = self::getStoreByCode(self::getSession('core/store_code'))))) { // Loaded store via the session } else { // Load through environment variables or just load default $code = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : ''; $type = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store'; if (!$code && isset(self::$_config['websites'][self::$_config['default_website_id']])) { $code = self::$_config['default_website_code']; $type = 'website'; } $store = false; if ($type === 'store') { $store = self::getStoreByCode($code); } else if ($type === 'website') { $store = self::getStoreByWebsiteCode($code); } } if (!is_array($store)) { throw new Exception('Unable to load store'); } unset(self::$_config['websites']); self::$_config = array_merge(self::$_config, (array)$store); } return is_null($key) ? self::$_config : self::getArrayValue(self::$_config, $key); } /** * Ensure the config details are valid * If not we cannot run * * @return bool */ static public function validateConfig() { if ((int)self::getConfig('use_cache') !== 1) { return false; } else if (!Fishpig_Bolt_Cache::setCachePath(self::getDir(self::getConfig('cache_file/path')))) { return false; } return true; } /** * Retrieve the session data * * @return array * @todo Add ability to load session from the database */ static public function getSession($key = null) { if (is_null(self::$_session)) { self::$_session = array(); $sessionFile = rtrim(self::getDir('var' . DSX . 'session'), DSX) . DSX . 'sess_' . (isset($_COOKIE['frontend']) ? $_COOKIE['frontend'] : session_id()); if (($data = self::getFile($sessionFile)) === false) { return null; } $session = array(); $split = preg_split('/([a-z\_]{1,}\|*)\|/', $data,-1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $len = count($split); for ($i = 0; $i < $len; $i++, $i++) { $session[$split[$i]] = @unserialize($split[$i+1]); } self::$_session = $session; } return is_null($key) ? self::$_session : self::getArrayValue(self::$_session, $key); } /** * Determine whether to cache the current request * * @return bool */ static public function canCacheRequest() { return self::getRequestUri() !== false && !self::isHttpPostRequest() && !self::hasCartItems() && !self::isCustomerLoggedIn() && !self::hasMessages(); } /** * Determine whether to cache the current request * * @return bool */ static public function canLoadCachedRequest() { return !(isset($_GET[FISHPIG_BOLT_PARAM_REFRESH]) && $_GET[FISHPIG_BOLT_PARAM_REFRESH] === 'bolt') && !self::isHttpPostRequest() && (Fishpig_Bolt_Hole_Punch::isEnabled() || (!self::hasCartItems() && !self::isCustomerLoggedIn())) && !self::hasMessages() && !self::$_isChangingStore; } /** * Send a response to the browser * * @param string $html * @return void */ static public function sendResponse($html) { header('Content-Type: text/html; charset=utf8'); header("Pragma: no-cache"); header("Cache-Control: no-cache, must-revalidate, no-store, post-check=0, pre-check=0"); echo str_replace('