public function masquerade() { // don't use $member->logIn() because it triggers tracking and breaks remember me tokens, etc. $sessionData = Session::get_all(); Session::clear_all(); Session::set("loggedInAs", $this->getOwner()->ID); Session::set('Masquerade.Old', $sessionData); return $this->getOwner(); }
function testGetAllElements() { Session::clear_all(); // Remove all session that might've been set by the test harness Session::set('Test', 'Test'); Session::set('Test-2', 'Test-2'); $session = Session::get_all(); $this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2')); }
function testClearAllElements() { Session::set('Test', 'Test'); Session::set('Test-1', 'Test-1'); Session::clear_all(); // should session get return null? The array key should probably be // unset from the data array $this->assertEquals(Session::get('Test'), ''); $this->assertEquals(Session::get('Test-1'), ''); }
public function logout($redirect = true) { if (Session::get('Masquerade.Old.loggedInAs')) { $oldSession = Session::get('Masquerade.Old'); Session::clear_all(); foreach ($oldSession as $name => $val) { Session::set($name, $val); } if ($redirect && !$this->getResponse()->isFinished()) { $this->redirectBack(); } } else { parent::logout($redirect); } }
/** * Determine if the cache should be enabled for the current request * * @param string $url * @return boolean */ protected function enabled($url) { // Master override if (!self::config()->enabled) { return false; } // No GET params other than cache relevant config is passed (e.g. "?stage=Stage"), // which would mean that we have to bypass the cache if (count(array_diff(array_keys($_GET), array('url')))) { return false; } // Request is not POST (which would have to be handled dynamically) if ($_POST) { return false; } // Check url doesn't hit opt out filter $optOutURL = self::config()->optOutURL; if (!empty($optOutURL) && preg_match($optOutURL, $url)) { return false; } // Check url hits the opt in filter $optInURL = self::config()->optInURL; if (!empty($optInURL) && !preg_match($optInURL, $url)) { return false; } // Check ajax filter if (!self::config()->enableAjax && Director::is_ajax()) { return false; } // If displaying form errors then don't display cached result if (!isset($_SESSION)) { Session::start(); } Session::clear_all(); // Forces the session to be regenerated from $_SESSION foreach (Session::get_all() as $field => $data) { // Check for session details in the form FormInfo.{$FormName}.errors if ($field === 'FormInfo' && $data != null) { foreach ($data as $formData) { if (isset($formData['errors'])) { return false; } } } } // OK! return true; }
/** * @deprecated 2.5 Use Session::clear_all() */ public static function clearAll() { user_error('Session::clearAll() is deprecated. Please use Session::clear_all() instead.', E_USER_NOTICE); return Session::clear_all(); }
/** * Activate caching on a given url * * @param string $url */ public function run($url) { // First make sure we have session if (!isset($_SESSION)) { Session::start(); } // Forces the session to be regenerated from $_SESSION Session::clear_all(); // This prevents a new user's security token from being regenerated incorrectly $_SESSION['SecurityID'] = SecurityToken::getSecurityID(); // Get cache and cache details $responseHeader = self::config()->responseHeader; $cache = $this->getCache(); $cacheKey = $this->getCacheKey($url); // Check if caching should be short circuted $enabled = $this->enabled($url); $this->extend('updateEnabled', $enabled); if (!$enabled) { if ($responseHeader) { header("{$responseHeader}: skipped"); } $this->yieldControl(); return; } // Check if cached value can be returned $cachedValue = $cache->load($cacheKey); if ($this->presentCachedResult($cachedValue)) { return; } // Run this page, caching output and capturing data if ($responseHeader) { header("{$responseHeader}: miss at " . @date('r')); } ob_start(); $this->yieldControl(); $headers = headers_list(); $result = ob_get_flush(); $responseCode = http_response_code(); // Skip blank copy unless redirecting $locationHeaderMatches = preg_grep('/^Location/i', $headers); if (empty($result) && empty($locationHeaderMatches)) { return; } // Skip excluded status codes $optInResponseCodes = self::config()->optInResponseCodes; $optOutResponseCodes = self::config()->optOutResponseCodes; if (is_array($optInResponseCodes) && !in_array($responseCode, $optInResponseCodes)) { return; } if (is_array($optOutResponseCodes) && in_array($responseCode, $optInResponseCodes)) { return; } // Check if any headers match the specified rules forbidding caching if (!$this->headersAllowCaching($headers)) { return; } // Include any "X-Header" sent with this request. This is necessary to // ensure that additional CSS, JS, and other files are retained $saveHeaders = $this->getCacheableHeaders($headers); // Save data along with sent headers $this->cacheResult($cache, $result, $saveHeaders, $cacheKey, $responseCode); }
/** * Log the currently logged in user out * * @param bool $redirect Redirect the user back to where they came. * - If it's false, the code calling logout() is * responsible for sending the user where-ever * they should go. */ public function logout($redirect = true) { $member = Member::currentUser(); if ($member) { $member->logOut(); } Session::clear_all(); if ($redirect) { Director::redirectBack(); } }
/** * Activate caching on a given url * * @param string $url */ public function run($url) { // First make sure we have session if (!isset($_SESSION)) { Session::start(); } Session::clear_all(); // Forces the session to be regenerated from $_SESSION // Get cache and cache details $responseHeader = self::config()->responseHeader; $cache = $this->getCache(); $cacheKey = $this->getCacheKey($url); // Check if caching should be short circuted $enabled = $this->enabled($url); $this->extend('updateEnabled', $enabled); if (!$enabled) { if ($responseHeader) { header("{$responseHeader}: skipped"); } $this->yieldControl(); return; } // Check if cached value can be returned $cachedValue = $cache->load($cacheKey); if ($this->presentCachedResult($cachedValue)) { return; } // Run this page, caching output and capturing data if ($responseHeader) { header("{$responseHeader}: miss at " . @date('r')); } ob_start(); $this->yieldControl(); $headers = headers_list(); $result = ob_get_flush(); // Skip blank copy if (empty($result)) { return; } // Check if any headers match the specified rules forbidding caching if (!$this->headersAllowCaching($headers)) { return; } // Include any "X-Header" sent with this request. This is necessary to // ensure that additional CSS, JS, and other files are retained $saveHeaders = $this->getCacheableHeaders($headers); // Save data along with sent headers $this->cacheResult($cache, $result, $saveHeaders, $cacheKey); }