Login chamilo using symfony login_check path #2322
parent
ea398cc4c8
commit
ec47aaf5ce
@ -1,144 +1,165 @@ |
|||||||
<?php |
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Session\Session; |
use Chamilo\CoreBundle\Framework\Container; |
||||||
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Chamilo session (i.e. the session that maintains the connection open after usr login) |
|
||||||
* |
|
||||||
* Usage: |
|
||||||
* |
|
||||||
* |
|
||||||
* use ChamiloSession as Session; |
|
||||||
* |
|
||||||
* Session::read('name'); |
|
||||||
* |
|
||||||
* Or |
|
||||||
* |
|
||||||
* Chamilo::session()->... |
|
||||||
* session()->... |
|
||||||
* |
|
||||||
* @license see /license.txt |
|
||||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
|
||||||
*/ |
|
||||||
/** |
|
||||||
* @todo use session symfony component |
|
||||||
* @todo replace all $_SESSION calls with this class. |
* @todo replace all $_SESSION calls with this class. |
||||||
* @todo remove System\Session class |
|
||||||
* ChamiloSession class definition |
|
||||||
*/ |
*/ |
||||||
class ChamiloSession extends System\Session |
class ChamiloSession implements \ArrayAccess |
||||||
{ |
{ |
||||||
const NAME = 'ch_sid'; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* Generate new session instance |
* @param string $variable |
||||||
* @return ChamiloSession |
* @param null $default |
||||||
|
* @return mixed|null |
||||||
*/ |
*/ |
||||||
public static function instance() |
public static function read($variable, $default = null) |
||||||
{ |
{ |
||||||
static $result = null; |
$session = Container::getSession(); |
||||||
|
$result = null; |
||||||
|
if (isset($session)) { |
||||||
|
$result = $session->get($variable); |
||||||
|
} |
||||||
|
|
||||||
|
// Check if the value exists in the $_SESSION array |
||||||
if (empty($result)) { |
if (empty($result)) { |
||||||
$result = new ChamiloSession(); |
if (isset($_SESSION[$variable])) { |
||||||
|
return $_SESSION[$variable]; |
||||||
|
} |
||||||
|
return $default; |
||||||
|
} else { |
||||||
|
return $result; |
||||||
} |
} |
||||||
return $result; |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Returns the session lifetime |
* @param string $variable |
||||||
* @return int The session lifetime as defined in the config file, in seconds |
* @param mixed $value |
||||||
*/ |
*/ |
||||||
public static function session_lifetime() |
public static function write($variable, $value) |
||||||
{ |
{ |
||||||
global $_configuration; |
//$_SESSION[$variable] = $value; |
||||||
return $_configuration['session_lifetime']; |
$session = Container::getSession(); |
||||||
|
// Writing the session in 2 instances because |
||||||
|
$_SESSION[$variable] = $value; |
||||||
|
$session->set($variable, $value); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Starts the Chamilo session. |
* @param string $variable |
||||||
|
*/ |
||||||
|
public static function erase($variable) |
||||||
|
{ |
||||||
|
$variable = (string) $variable; |
||||||
|
$session = Container::getSession(); |
||||||
|
$session->remove($variable); |
||||||
|
|
||||||
|
if (isset($GLOBALS[$variable])) { |
||||||
|
unset($GLOBALS[$variable]); |
||||||
|
} |
||||||
|
if (isset($_SESSION[$variable])) { |
||||||
|
unset($_SESSION[$variable]); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns true if session has variable set up, false otherwise. |
||||||
* |
* |
||||||
* The default lifetime for session is set here. It is not possible to have it |
* @param string $variable |
||||||
* as a database setting as it is used before the database connection has been made. |
|
||||||
* It is taken from the configuration file, and if it doesn't exist there, it is set |
|
||||||
* to 360000 seconds |
|
||||||
* |
* |
||||||
* @author Olivier Brouckaert |
* @return bool |
||||||
* @param string variable - the variable name to save into the session |
|
||||||
* @return void |
|
||||||
*/ |
*/ |
||||||
public static function start($already_installed = true) |
public static function has($variable) |
||||||
{ |
{ |
||||||
/* |
return isset($_SESSION[$variable]); |
||||||
* Prevent Session fixation bug fixes |
} |
||||||
* See http://support.chamilo.org/issues/3600 |
|
||||||
* http://php.net/manual/en/session.configuration.php |
|
||||||
* @todo use session_set_cookie_params with some custom admin parameters |
|
||||||
*/ |
|
||||||
|
|
||||||
//session.cookie_lifetime |
|
||||||
//the session ID is only accepted from a cookie |
|
||||||
ini_set('session.use_only_cookies', 1); |
|
||||||
|
|
||||||
//HTTPS only if possible |
|
||||||
//ini_set('session.cookie_secure', 1); |
|
||||||
//session ID in the cookie is only readable by the server |
|
||||||
ini_set('session.cookie_httponly', 1); |
|
||||||
|
|
||||||
//Use entropy file |
|
||||||
//session.entropy_file |
|
||||||
//ini_set('session.entropy_length', 128); |
|
||||||
//Do not include the identifier in the URL, and not to read the URL for |
|
||||||
// identifiers. |
|
||||||
ini_set('session.use_trans_sid', 0); |
|
||||||
|
|
||||||
session_name(self::NAME); |
|
||||||
session_start(); |
|
||||||
$session = self::instance(); |
|
||||||
if ($already_installed) { |
|
||||||
if (!isset($session['checkChamiloURL'])) { |
|
||||||
$session['checkChamiloURL'] = api_get_path(WEB_PATH); |
|
||||||
} elseif ($session['checkChamiloURL'] != api_get_path(WEB_PATH)) { |
|
||||||
self::clear(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// If the session time has expired, refresh the starttime value, |
/** |
||||||
// so we're starting to count down from a later time |
* Clear |
||||||
if (self::has('starttime') && $session->is_expired()) { |
*/ |
||||||
self::destroy(); |
public static function clear() |
||||||
} else { |
{ |
||||||
//error_log('Time not expired, extend session for a bit more'); |
$session = Container::getSession(); |
||||||
self::write('starttime', time()); |
$session->clear(); |
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Session start time: that is the last time the user loaded a page (before this time) |
* Destroy |
||||||
* @return int timestamp |
*/ |
||||||
|
public static function destroy() |
||||||
|
{ |
||||||
|
$session = Container::getSession(); |
||||||
|
$session->invalidate(); |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* ArrayAccess |
||||||
*/ |
*/ |
||||||
public function start_time() |
public function offsetExists($offset) |
||||||
{ |
{ |
||||||
return self::read('starttime'); |
return isset($_SESSION[$offset]); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Session end time: when the session expires. This is made of the last page |
* It it exists returns the value stored at the specified offset. |
||||||
* load time + a number of seconds |
* If offset does not exists returns null. Do not trigger a warning. |
||||||
* @return int UNIX timestamp (server's timezone) |
* |
||||||
|
* @param string $offset |
||||||
|
* @return any |
||||||
|
*/ |
||||||
|
public function offsetGet($offset) |
||||||
|
{ |
||||||
|
return self::read($offset); |
||||||
|
} |
||||||
|
|
||||||
|
public function offsetSet($offset, $value) |
||||||
|
{ |
||||||
|
self::write($offset, $value); |
||||||
|
} |
||||||
|
|
||||||
|
public function offsetUnset($offset) |
||||||
|
{ |
||||||
|
unset($_SESSION[$offset]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $name |
||||||
|
*/ |
||||||
|
public function __unset($name) |
||||||
|
{ |
||||||
|
unset($_SESSION[$name]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $name |
||||||
|
* @return bool |
||||||
*/ |
*/ |
||||||
public function end_time() |
public function __isset($name) |
||||||
{ |
{ |
||||||
$start_time = $this->start_time(); |
return self::has($name); |
||||||
$lifetime = self::session_lifetime(); |
|
||||||
return $start_time + $lifetime; |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Returns whether the session is expired |
* It it exists returns the value stored at the specified offset. |
||||||
* @return bool True if the session is expired, false if it is still valid |
* If offset does not exists returns null. Do not trigger a warning. |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* |
||||||
|
* @return mixed |
||||||
|
* |
||||||
|
*/ |
||||||
|
public function __get($name) |
||||||
|
{ |
||||||
|
return self::read($name); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @param string $name |
||||||
|
* @param mixed $value |
||||||
*/ |
*/ |
||||||
public function is_expired() |
public function __set($name, $value) |
||||||
{ |
{ |
||||||
return $this->end_time() < time(); |
self::write($name, $value); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue