Session, some fixes in PagerView and ErrorView #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@126 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
@ -29,11 +29,13 @@ class ErrorAction extends Action
|
|||||||
if ($this->exception instanceof Error404Exception) {
|
if ($this->exception instanceof Error404Exception) {
|
||||||
$this->template = 404;
|
$this->template = 404;
|
||||||
}
|
}
|
||||||
|
$this->logError();
|
||||||
|
$this->sendHTTPCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTemplate()
|
protected function getTemplate()
|
||||||
{
|
{
|
||||||
return '/static/' . $this->template;
|
return '/actions/' . $this->template;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function sendHttpCode()
|
protected function sendHttpCode()
|
||||||
@ -79,11 +81,4 @@ class ErrorAction extends Action
|
|||||||
error_log($message);
|
error_log($message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch()
|
|
||||||
{
|
|
||||||
$this->logError();
|
|
||||||
$this->sendHTTPCode();
|
|
||||||
return $this->view->fetch($this->getTemplate());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -32,6 +32,7 @@ class FrontController
|
|||||||
private function __construct()
|
private function __construct()
|
||||||
{
|
{
|
||||||
ErrorHandler::init();
|
ErrorHandler::init();
|
||||||
|
Session::start();
|
||||||
if (DEBUG == true) {
|
if (DEBUG == true) {
|
||||||
Profiler::getInstance()->start();
|
Profiler::getInstance()->start();
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,12 @@ class PagerAction extends Action
|
|||||||
|
|
||||||
protected function execute()
|
protected function execute()
|
||||||
{
|
{
|
||||||
$page = (int) Env::Get('p');
|
$this->last_page = ceil($this->count / $this->limit);
|
||||||
$this->last_page = ceil($this->count/$this->limit);
|
if (Env::Get('p') == 'last') {
|
||||||
|
$page = $this->last_page;
|
||||||
|
} else {
|
||||||
|
$page = (int) Env::Get('p');
|
||||||
|
}
|
||||||
$this->page = ($page <= $this->last_page && $page > 0) ? $page : 1;
|
$this->page = ($page <= $this->last_page && $page > 0) ? $page : 1;
|
||||||
$this->offset = $this->limit * ($this->page - 1);
|
$this->offset = $this->limit * ($this->page - 1);
|
||||||
}
|
}
|
||||||
|
@ -64,28 +64,6 @@ class Env
|
|||||||
return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
|
return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function Session($var, $default = false)
|
|
||||||
{
|
|
||||||
return isset($_SESSION[$var]) ? $_SESSION[$var] : $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
static public function setSession($var, $value)
|
|
||||||
{
|
|
||||||
$_SESSION[$var] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unsets session var
|
|
||||||
*
|
|
||||||
* @param string $var
|
|
||||||
*/
|
|
||||||
static public function unsetSession($var)
|
|
||||||
{
|
|
||||||
if (isset($_SESSION[$var])) {
|
|
||||||
unset($_SESSION[$var]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static public function setCookie($var, $value, $time = 0, $path = '/')
|
static public function setCookie($var, $value, $time = 0, $path = '/')
|
||||||
{
|
{
|
||||||
return setcookie($var, $value, $time, $path);
|
return setcookie($var, $value, $time, $path);
|
||||||
@ -120,5 +98,4 @@ class Env
|
|||||||
{
|
{
|
||||||
self::$params = self::$params + $params;
|
self::$params = self::$params + $params;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
203
session/Session.php
Normal file
203
session/Session.php
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
|
* @link http://netmonsters.ru
|
||||||
|
* @package Majestic
|
||||||
|
* @subpackage session
|
||||||
|
* @since 2010-03-14
|
||||||
|
* @version SVN: $Id$
|
||||||
|
* @filesource $URL$
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Session
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Default number of seconds the session will be remembered
|
||||||
|
* for when asked to be remembered
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private static $remember_time = 1209600; // 2 weeks
|
||||||
|
|
||||||
|
private static $started = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Avoid new
|
||||||
|
*/
|
||||||
|
private function __construct(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Avoid cloning
|
||||||
|
*/
|
||||||
|
private function __clone(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a member of the $_SESSION
|
||||||
|
*
|
||||||
|
* If no $key is passed, returns the entire $_SESSION array.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $default Default value to use if key not found
|
||||||
|
* @return mixed Returns null if key does not exist
|
||||||
|
*/
|
||||||
|
public static function get($key = null, $default = null)
|
||||||
|
{
|
||||||
|
if (null === $key) {
|
||||||
|
return $_SESSION;
|
||||||
|
}
|
||||||
|
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set $_SESSION values
|
||||||
|
*
|
||||||
|
* @param string|array $spec
|
||||||
|
* @param null|mixed $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function set($spec, $value = null)
|
||||||
|
{
|
||||||
|
if (is_array($spec)) {
|
||||||
|
foreach ($spec as $key => $value) {
|
||||||
|
self::set($key, $value);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$_SESSION[(string) $spec] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete Session value
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public static function del($key)
|
||||||
|
{
|
||||||
|
if (isset($_SESSION[$key])) {
|
||||||
|
unset($_SESSION[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remember() - Write a persistent cookie that expires after a number
|
||||||
|
* of seconds in the future. If no number of seconds is specified,
|
||||||
|
* then this defaults to self::$rememberMeSeconds.
|
||||||
|
*
|
||||||
|
* @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function remember($seconds = null)
|
||||||
|
{
|
||||||
|
$seconds = (int) $seconds;
|
||||||
|
$seconds = ($seconds > 0) ? $seconds : self::$remember_time;
|
||||||
|
self::rememberUntil($seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* forget() - Write a volatile session cookie, removing any persistent
|
||||||
|
* cookie that may have existed. The session would end upon,
|
||||||
|
* for example, termination of a web browser program.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function forget()
|
||||||
|
{
|
||||||
|
self::rememberUntil();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rememberUntil() - This method does the work of changing the state of the session
|
||||||
|
* cookie and making sure that it gets resent to the browser via regenerateId()
|
||||||
|
*
|
||||||
|
* @param int $seconds
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function rememberUntil($seconds = 0)
|
||||||
|
{
|
||||||
|
$params = session_get_cookie_params();
|
||||||
|
session_set_cookie_params(
|
||||||
|
$seconds,
|
||||||
|
$params['path'],
|
||||||
|
$params['domain'],
|
||||||
|
$params['secure']
|
||||||
|
);
|
||||||
|
self::regenerateId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* regenerateId() - Regenerate the session id. Best practice is to call this after
|
||||||
|
* session is started. If called prior to session starting, session id will be regenerated
|
||||||
|
* at start time.
|
||||||
|
*
|
||||||
|
* @throws Zend_Session_Exception
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function regenerateId()
|
||||||
|
{
|
||||||
|
if (self::$started) {
|
||||||
|
session_regenerate_id(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isExists()
|
||||||
|
{
|
||||||
|
if (ini_get('session.use_cookies') == '1' && isset($COOKIE[session_name()])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function start()
|
||||||
|
{
|
||||||
|
self::$started = true;
|
||||||
|
session_start();
|
||||||
|
self::regenerateId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getId() - get the current session id
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getId()
|
||||||
|
{
|
||||||
|
return session_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* destroy() - This is used to destroy session data, and optionally,
|
||||||
|
* the session cookie itself
|
||||||
|
*
|
||||||
|
* @param bool $remove_cookie - OPTIONAL remove session id cookie,
|
||||||
|
* defaults to true (remove cookie)
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function destroy($remove_cookie = true)
|
||||||
|
{
|
||||||
|
session_destroy();
|
||||||
|
if ($remove_cookie) {
|
||||||
|
self::expireSessionCookie();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function expireSessionCookie()
|
||||||
|
{
|
||||||
|
if (isset($COOKIE[session_name()])) {
|
||||||
|
$params = session_get_cookie_params();
|
||||||
|
setcookie(
|
||||||
|
session_name(),
|
||||||
|
false,
|
||||||
|
0,
|
||||||
|
$params['path'],
|
||||||
|
$params['domain'],
|
||||||
|
$params['secure']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user