Browse Source

Added session-management class.

pull/5/head
Etenil 15 years ago
parent
commit
3174d55cd1
  1. 4
      init.php
  2. 7
      system/Controller/ControllerMain.php
  3. 4
      system/Jabber.php
  4. 13
      system/RPC.php
  5. 148
      system/Session.php
  6. 31
      system/User.php
  7. 10
      system/Widget/WidgetWrapper.php

4
init.php

@ -1,10 +1,8 @@
<?php
ob_start();
session_commit();
session_start();
define('APP_NAME', 'movim');
define('BASE_PATH', dirname(__FILE__) . '/');
define('LIB_PATH',BASE_PATH.'system/');
define('PROPERTIES_PATH',BASE_PATH.'page/properties/');

7
system/Controller/ControllerMain.php

@ -57,15 +57,15 @@ class ControllerMain extends ControllerBase
if(isset($_GET['f']) && $_GET['f'] != "" ) {
$this->page->setTitle(t('%s - Welcome to Movim', APP_TITLE));
$this->page->menuAddLink($this->page->theme_img('img/home_icon.png', 'home_icon').t('Home'), '?q=mainPage');
$cachevcard = Cache::c('vcard'.$_GET['f']);
if(isset($cachevcard['vCardFN']) || isset($cachevcard['vCardFamily']))
if(isset($cachevcard['vCardFN']) || isset($cachevcard['vCardFamily']))
$this->page->menuAddLink($cachevcard['vCardFN'] ." ".$cachevcard['vCardFamily'], false, true);
elseif(isset($cachevcard['vCardNickname']))
$this->page->menuAddLink($cachevcard['vCardNickname'], false, true);
else
$this->page->menuAddLink($_GET['f'], false, true);
$this->page->menuAddLink(t('Configuration'), '?q=config');
$content = new TplPageBuilder($user);
@ -135,7 +135,6 @@ class ControllerMain extends ControllerBase
'</form>'.
'</div>');
echo $this->page->build('page.tpl');
session_commit();
}
function disconnect()

4
system/Jabber.php

@ -43,7 +43,9 @@ class Jabber
$userConf = Conf::getUserConf($jid);
$serverConf = Conf::getServerConf();
unset($_SESSION['jid']);
$sess = Session::start(APP_NAME);
$sess->remove('jid'); // ???
$this->jaxl = new JAXL(array(
// User Configuration

13
system/RPC.php

@ -3,7 +3,7 @@
/**
* @file RPC.php
* This file is part of PROJECT.
*
*
* @brief Description
*
* @author Etenil <etenil@etenilsrealm.nl>
@ -12,7 +12,7 @@
* @date 20 February 2011
*
* Copyright (C)2011 Etenil
*
*
* All rights reserved.
*/
@ -29,7 +29,7 @@ class RPC
$args = func_get_args();
array_shift($args);
$funcall = array(
'func' => $funcname,
'params' => $args,
@ -63,13 +63,13 @@ class RPC
foreach(self::$funcalls as $funcall) {
println('<funcall name="%s">', $funcall['func']);
if(is_array($funcall['params'])) {
foreach($funcall['params'] as $param) {
println('<param>%s</param>', $param);
}
}
println('</funcall>');
}
println('</movimcontainer>');
@ -83,13 +83,10 @@ class RPC
*/
public function handle()
{
//session_commit();
if(isset($_GET['do']) && $_GET['do'] == 'poll') {
$user = new User();
$xmppSession = Jabber::getInstance($user->getLogin());
session_commit();
$xmppSession->pingServer();
session_commit();
} else {
$xml = file_get_contents('php://input');
$request = simplexml_load_string($xml);

148
system/Session.php

@ -0,0 +1,148 @@
<?php
/**
* @file Session.php
* This file is part of MOVIM.
*
* @brief Class that manages session variables with minimal lock time.
*
* @author Guillaume Pasquet <etenil@etenilsrealm.nl>
*
* Yes, this class is again a singleton. But this is justified by the fact that
* there can only be one active session that is locked down.
*
* @version 1.0
* @date 26 April 2011
*
* Copyright (C)2011 MOVIM
*
* See COPYING for licensing information.
*/
class Session
{
protected static $instances = array();
protected $session;
protected $sid;
protected $s_name;
protected $nosave = false;
/**
* Loads and immediately closes the session variables for the namespace
* $name.
*/
protected function __construct($name)
{
$this->s_name = $name;
$this->load();
}
/**
* Gets an instance of Session.
*/
public static function start($name)
{
if(!isset(self::$instances[$name])) {
self::$instances[$name] = new self($name);
}
return self::$instances[$name];
}
/**
* Commits the session upon destruction.
*/
public function __destruct()
{
if(!$this->nosave) {
$this->commit();
}
}
/**
* Loads data from the session.
*/
protected function load()
{
session_start();
$this->sid = session_id();
$this->session = unserialize(base64_decode($_SESSION[$this->s_name]));
session_commit();
}
/**
* Gets a session variable. Returns false if doesn't exist.
*/
public function get($varname)
{
if(isset($this->session[$varname])) {
return $this->session[$varname];
} else {
return false;
}
}
/**
* Sets a session variable. Returns $value.
*/
public function set($varname, $value)
{
$this->session[$varname] = $value;
return $value;
}
/**
* Deletes a variable from the session.
*/
public function remove($varname)
{
if(isset($this->session[$varname])) {
unset($this->session[$varname]);
return true;
} else {
return false;
}
}
/**
* Instance alias of the destroy function.
*/
public function dispose()
{
$this->nosave = true;
self::destroy($this->s_name());
}
/**
* Deletes the whole namespace.
*/
public static function destroy($name)
{
session_start();
session_unset($name);
session_commit();
unset(self::$instances[$name]);
}
/**
* Forces write of session. Call this once you have written data that needs
* sharing.
*/
public function commit()
{
session_start();
$_SESSION[$this->s_name] = base64_encode(serialize($this->session));
session_commit();
}
/**
* Cancels all changes on the session (dangerous).
*/
public function rollback()
{
$this->load();
}
}
?>

31
system/User.php

@ -1,4 +1,4 @@
<?php
<?php
/**
* \class User
@ -19,8 +19,9 @@ class User {
function __construct()
{
if($this->isLogged()) {
$this->username = $_SESSION['login'];
$this->password = $_SESSION['pass'];
$sess = Session::start(APP_NAME);
$this->username = $sess->get('login');
$this->password = $sess->get('pass');
$this->xmppSession = Jabber::getInstance($this->username);
}
@ -38,8 +39,8 @@ class User {
function isLogged()
{
// User is not logged in if both the session vars and the members are unset.
return (($this->username != '' && $this->password != '')
|| (isset($_SESSION['login']) && ($_SESSION['login'] != '')));
$sess = Session::start(APP_NAME);
return (($this->username != '' && $this->password != '') || $sess->get('login'));
}
function authenticate($login,$pass)
@ -50,14 +51,15 @@ class User {
Conf::createUserConf($login, $pass);
$data = Conf::getUserData($login);
}
$this->xmppSession = Jabber::getInstance($login);
$this->xmppSession->login($login, $pass);
// Careful guys, md5 is _not_ secure. SHA1 recommended here.
if(sha1($pass) == $data['pass']) {
$_SESSION['login'] = $login;
$_SESSION['pass'] = $pass;
$sess = Session::start(APP_NAME);
$sess->set('login', $login);
$sess->set('pass', $pass);
$this->username = $login;
$this->password = $pass;
@ -70,19 +72,22 @@ class User {
return $e->getMessage();
}
}
function desauth()
{
unset($_SESSION);
session_destroy();
$sess = Session::start(APP_NAME);
$sess->remove('login');
$sess->remove('pass');
$sess->remove('jid');
$sess->dispose();
}
function getLogin()
{
return $this->username;
}
function getPass()
{
return $this->password;

10
system/Widget/WidgetWrapper.php

@ -39,9 +39,10 @@ class WidgetWrapper
private function __construct($register)
{
$this->register_widgets = $register;
if(isset($_SESSION['loaded_widgets'])
&& is_array($_SESSION['loaded_widgets'])) {
$this->loaded_widgets_old = $_SESSION['loaded_widgets'];
$sess = Session::start(APP_NAME);
$widgets = $sess->get('loaded_widgets');
if(is_array($widgets)) {
$this->loaded_widgets_old = $widgets;
}
}
@ -59,7 +60,8 @@ class WidgetWrapper
function __destruct()
{
if($this->register_widgets) {
$_SESSION['loaded_widgets'] = $this->loaded_widgets;
$sess = Session::start(APP_NAME);
$sess->set('loaded_widgets', $this->loaded_widgets);
}
}

Loading…
Cancel
Save