Browse Source

- Huge commit to add the new Sessionx management

- Clean lot of old code
pull/16/head
Jaussoin Timothée 12 years ago
parent
commit
89318072a1
  1. 12
      app/assets/js/movim_rpc.js
  2. 54
      app/assets/js/movim_session.js
  3. 23
      app/models/sessionx/Sessionx.php
  4. 142
      app/models/sessionx/SessionxDAO.php
  5. 6
      app/widgets/Chat/Chat.php
  6. 29
      app/widgets/Feed/Feed.php
  7. 83
      app/widgets/Login/Login.php
  8. 2
      app/widgets/Node/node.tpl
  9. 9
      app/widgets/Poller/poller.js
  10. 6
      app/widgets/Presence/Presence.php
  11. 13
      app/widgets/System/System.php
  12. 2
      app/widgets/System/system.tpl
  13. 7
      bootstrap.php
  14. 2
      manifest.webapp
  15. 8
      system/RPC.php
  16. 114
      system/Sessionx.php
  17. 25
      system/User.php
  18. 4
      system/controllers/BaseController.php

12
app/assets/js/movim_rpc.js

@ -80,15 +80,7 @@ function MovimRPC()
{
this.params.push(param);
};
/**
* Set the global session
*/
this.set_session = function()
{
this.session = Session.getSession();
};
/**
* Sets all movim call parameters at once.
*/
@ -96,7 +88,6 @@ function MovimRPC()
{
this.set_widget(widget);
this.set_func(func);
this.set_session();
this.params = params;
};
@ -132,7 +123,6 @@ function MovimRPC()
this.widget = '';
this.func = '';
this.params = [];
this.session = null;
}
/**

54
app/assets/js/movim_session.js

@ -1,54 +0,0 @@
/**
* Movim Session class.
*
* Implement an additional security to prevent same rid and id in the
* XMPP requests during the session
*/
var Session = {
session: null,
init: function() {
this.session = localStorage.getItem('session');
if(this.session == "null" || this.session == null) {
this.clear();
localStorage.setItem('session', JSON.stringify(this.session));
} else {
this.session = JSON.parse(this.session);
}
},
reset: function() {
localStorage.setItem('session', null);
},
check: function() {
if(SESSION_RID != null && SESSION_RID > this.session.rid)
this.session.rid = SESSION_RID;
if(SESSION_ID != null && SESSION_ID > this.session.id)
this.session.id = SESSION_ID;
},
clear: function() {
this.session = {
'id' : 0,
'rid' : 0
};
localStorage.setItem('session', JSON.stringify(this.session));
},
getSession: function() {
console.log('Increase '+this.session.rid);
this.init();
this.check();
this.session.id = this.session.id+1;
this.session.rid = this.session.rid+1;
localStorage.setItem('session', JSON.stringify(this.session));
return this.session;
}
}
Session.init();

23
app/models/sessionx/Sessionx.php

@ -5,6 +5,7 @@ namespace modl;
class Sessionx extends ModlModel {
public $session;
public $user;
public $password;
public $ressource;
public $rid;
public $sid;
@ -24,12 +25,14 @@ class Sessionx extends ModlModel {
{"type":"string", "size":128, "mandatory":true, "key":true },
"user" :
{"type":"string", "size":64 },
"password" :
{"type":"string", "size":64 },
"ressource" :
{"type":"string", "size":64 },
"rid" :
{"type":"int", "size":8, "mandatory":true },
"sid" :
{"type":"int", "size":8 },
{"type":"string", "size":64 },
"id" :
{"type":"int", "size":8, "mandatory":true },
"url" :
@ -51,21 +54,3 @@ class Sessionx extends ModlModel {
parent::__construct();
}
}
/* $session = array(
'rid' => 1,
'sid' => 0,
'id' => 0,
'url' => $serverconfig['boshUrl'],
'port'=> 5222,
'host'=> $host,
'domain' => $domain,
'ressource' => 'moxl'.substr(md5(date('c')), 3, 6),
'user' => $user,
'password' => $element['pass'],
'proxyenabled' => $serverconfig['proxyEnabled'],
'proxyurl' => $serverconfig['proxyURL'],
'proxyport' => $serverconfig['proxyPort'],
'proxyuser' => $serverconfig['proxyUser'],
'proxypass' => $serverconfig['proxyPass']);*/

142
app/models/sessionx/SessionxDAO.php

@ -7,6 +7,7 @@ class SessionxDAO extends ModlSQL {
$this->_sql = '
update sessionx
set user = :user,
password = :password,
ressource = :ressource,
rid = :rid,
sid = :sid,
@ -25,6 +26,7 @@ class SessionxDAO extends ModlSQL {
array(
'session' => $s->session,
'user' => $s->user,
'password' => $s->password,
'ressource' => $s->ressource,
'rid' => $s->rid,
'sid' => $s->sid,
@ -46,6 +48,7 @@ class SessionxDAO extends ModlSQL {
insert into sessionx
(session,
user,
password,
ressource,
rid,
sid,
@ -60,6 +63,7 @@ class SessionxDAO extends ModlSQL {
values
(:session,
:user,
:password,
:ressource,
:rid,
:sid,
@ -77,6 +81,7 @@ class SessionxDAO extends ModlSQL {
array(
'session' => $s->session,
'user' => $s->user,
'password' => $s->password,
'ressource' => $s->ressource,
'rid' => $s->rid,
'sid' => $s->sid,
@ -95,6 +100,41 @@ class SessionxDAO extends ModlSQL {
}
}
function update($session, $key, $value) {
$this->_sql = '
update sessionx
set
'.$key.' = :'.$key.'
where
session = :session';
$this->prepare(
'Sessionx',
array(
'session' => $session,
$key => $value
)
);
$this->run('Sessionx');
}
function get($session) {
$this->_sql = '
select * from sessionx
where
session = :session';
$this->prepare(
'Sessionx',
array(
'session' => $session
)
);
return $this->run('Sessionx', 'item');
}
function getId($session) {
$this->_sql = '
select id from sessionx
@ -182,106 +222,4 @@ class SessionxDAO extends ModlSQL {
return $this->run('Sessionx');
}
/*function set($session, $container, $name, $value, $timestamp) {
$timestamp = date(DATE_ISO8601, $timestamp);
$this->_sql = '
update session
set value = :value,
timestamp = :timestamp
where session = :session
and container = :container
and name = :name';
$this->prepare(
'Session',
array(
'session' => $session,
'container' => $container,
'name' => $name,
'value' => $value,
'timestamp' => $timestamp
)
);
$this->run('Session');
if(!$this->_effective) {
$this->_sql = '
insert into session
(name, value, session, container, timestamp)
values (:name, :value, :session, :container, :timestamp)';
$this->prepare(
'Session',
array(
'session' => $session,
'container' => $container,
'name' => $name,
'value' => $value,
'timestamp' => $timestamp
)
);
return $this->run('Session');
}
}
function get($session, $container, $name) {
$this->_sql = '
select * from session
where
session = :session
and container = :container
and name = :name';
$this->prepare(
'Session',
array(
'session' => $session,
'container' => $container,
'name' => $name
)
);
return $this->run('Session', 'item');
}
function delete($session, $container, $name) {
$this->_sql = '
delete from session
where
session = :session
and container = :container
and name = :name';
$this->prepare(
'Session',
array(
'session' => $session,
'container' => $container,
'name' => $name
)
);
return $this->run('Session');
}
function deleteContainer($session, $container) {
$this->_sql = '
delete from session
where
session = :session
and container = :container';
$this->prepare(
'Session',
array(
'session' => $session,
'container' => $container,
)
);
return $this->run('Session');
}*/
}

6
app/widgets/Chat/Chat.php

@ -246,14 +246,14 @@ class Chat extends WidgetBase
$m->jidto = echapJid($to);
$m->jidfrom = $this->user->getLogin();
global $session;
$session = \Sessionx::start();
$m->type = 'chat';
$m->ressource = $session['ressource'];
$m->ressource = $session->ressource;
if($muc) {
$m->type = 'groupchat';
$m->ressource = $session['user'];
$m->ressource = $session->user;
$m->jidfrom = $to;
}

29
app/widgets/Feed/Feed.php

@ -5,29 +5,6 @@ class Feed extends WidgetCommon {
function WidgetLoad()
{
$sess = Session::start(APP_NAME);
var_dump($sess->get('session'));
$s = Sessionx::start();
// We get the Server Configuration
$serverconfig = \system\Conf::getServerConf();
$s->url = $serverconfig['boshUrl'];
$s->port = 5222;
$s->host = 'movim.eu';
$s->domain = 'pod.mov.im';
$s->user = 'edhelas';
$s->ressource = 'moxl'.substr(md5(date('c')), 3, 6);
$s->init();
var_dump($s->getRid());
var_dump($s->getRid());
var_dump($s->getRid());
var_dump($s->getId());
var_dump($s->getId());
var_dump($s);
var_dump($_COOKIE);
$this->addcss('feed.css');
$this->registerEvent('opt_post', 'onStream');
@ -115,14 +92,14 @@ class Feed extends WidgetCommon {
function prepareHead() {
$html = '';
global $session;
$session = \Sessionx::start();
if($session['config']['config'] == false) {
if($session->config['config'] == false) {
$html .=
'<div class="message warning" style="margin: 1.5em;">'.
t("Your server doesn't support post publication, you can only read contact's feeds").
'</div>';
} elseif(!isset($session['config']['feed'])) {
} elseif(!isset($session->config['feed'])) {
$html .= '
<div id="feednotifs">
<div class="message info">'.

83
app/widgets/Login/Login.php

@ -21,7 +21,7 @@ if (!defined('DOCUMENT_ROOT')) die('Access denied');
class Login extends WidgetBase {
function WidgetLoad()
{
{
$this->addcss('login.css');
$this->addjs('login.js');
$this->registerEvent('config', 'onConfig');
@ -195,6 +195,31 @@ class Login extends WidgetBase {
function ajaxLogin($element)
{
/*
$sess = Session::start(APP_NAME);
var_dump($sess->get('session'));
$s = Sessionx::start();
// We get the Server Configuration
$serverconfig = \system\Conf::getServerConf();
$s->url = $serverconfig['boshUrl'];
$s->port = 5222;
$s->host = 'movim.eu';
$s->domain = 'pod.mov.im';
$s->user = 'edhelas';
$s->ressource = 'moxl'.substr(md5(date('c')), 3, 6);
$s->init();
var_dump($s->getRid());
var_dump($s->getRid());
var_dump($s->getRid());
var_dump($s->getId());
var_dump($s->getId());
var_dump($s);
var_dump($_COOKIE);
*/
// We get the Server Configuration
$serverconfig = \system\Conf::getServerConf();
@ -239,50 +264,54 @@ class Login extends WidgetBase {
$domain = $dns[0]['target'];
else {
$domain = $host;
//$warning = 'dnsdomain';
}
$this->displayWarning($warning);
global $session;
/*global $session;
/*if($s != false) {
$session = $sess->get('session');
}
else {*/
$session = array(
'rid' => 1,
'sid' => 0,
'id' => 0,
'url' => $serverconfig['boshUrl'],
'port'=> 5222,
'host'=> $host,
'domain' => $domain,
'ressource' => 'moxl'.substr(md5(date('c')), 3, 6),
$session = array(
'rid' => 1,
'sid' => 0,
'id' => 0,
'url' => $serverconfig['boshUrl'],
'port'=> 5222,
'host'=> $host,
'domain' => $domain,
'ressource' => 'moxl'.substr(md5(date('c')), 3, 6),
'user' => $user,
'password' => $element['pass'],
'user' => $user,
'password' => $element['pass'],
'proxyenabled' => $serverconfig['proxyEnabled'],
'proxyurl' => $serverconfig['proxyURL'],
'proxyport' => $serverconfig['proxyPort'],
'proxyuser' => $serverconfig['proxyUser'],
'proxypass' => $serverconfig['proxyPass']);
//}
'proxyenabled' => $serverconfig['proxyEnabled'],
'proxyurl' => $serverconfig['proxyURL'],
'proxyport' => $serverconfig['proxyPort'],
'proxyuser' => $serverconfig['proxyUser'],
'proxypass' => $serverconfig['proxyPass']);
$sess = Session::start(APP_NAME);
$sess->set('session', $session);
$sess->set('session', $session);*/
// We create a new session or clear the old one
$s = Sessionx::start();
$s->init($user, $element['pass'], $host, $domain);
//global $session;
//$session = $s->get();
//\movim_log($session);
// We save the loaded widgets list in the database
$wrapper = WidgetWrapper::getInstance(false);
$sess = Session::start(APP_NAME);
$sess->set('registered_events', $wrapper->register_events());
// BOSH + XMPP connexion test
$warning = moxl\login();
if($warning != 'OK') {
//$this->displayWarning($warning);
RPC::call('movim_redirect', Route::urlize('login', $warning));
RPC::commit();
} else {

2
app/widgets/Node/node.tpl

@ -5,5 +5,5 @@
</div>
<script type="text/javascript">
{$getaffiliations}
<!--{$getmetadata}-->
{$getmetadata}
</script>

9
app/widgets/Poller/poller.js

@ -47,14 +47,7 @@ function movim_poll()
}
};
var poll = {
session: Session.getSession(true)
};
//console.log(JSON.stringify(poll));
poller.send(JSON.stringify(poll));
//poller.send();
poller.send();
}
function halt_poll()

6
app/widgets/Presence/Presence.php

@ -78,8 +78,7 @@ class Presence extends WidgetBase
{
$p = new moxl\PresenceUnavaiable();
$p->request();
//$user = new User();
//$user->desauth();
RPC::call('movim_redirect', Route::urlize('disconnect'));
RPC::commit();
}
@ -92,7 +91,7 @@ class Presence extends WidgetBase
global $session;
$pd = new \modl\PresenceDAO();
$p = $pd->getPresence($this->user->getLogin(), $session['ressource']);
$p = $pd->getPresence($this->user->getLogin(), $session->ressource);
if($p)
$html = '
@ -120,7 +119,6 @@ class Presence extends WidgetBase
<a onclick="'.$this->genCallAjax('ajaxLogout').'; movim_toggle_class(\'#logoutlist\', \'show\');" class="disconnect">'.t('Disconnect').'</a>
</div>
';
//href="'.Route::urlize('disconnect').'"
return $html;
}

13
app/widgets/System/System.php

@ -33,18 +33,5 @@ class System extends WidgetBase {
'timezone' => $conf['timezone']
);
$this->view->assign('server_conf', json_encode($public_conf));
$sess = \Session::start(APP_NAME);
$session = $sess->get('session');
if(isset($session['rid']))
$this->view->assign('session_rid', $session['rid']);
else
$this->view->assign('session_rid', 'null');
if(isset($session['id']))
$this->view->assign('session_id', $session['id']);
else
$this->view->assign('session_id', 'null');
}
}

2
app/widgets/System/system.tpl

@ -4,6 +4,4 @@
var PAGE_KEY_URI = '{$page_key_uri}';
var FAIL_SAFE = '{$fail_safe}';
var SERVER_CONF = {$server_conf};
var SESSION_RID = {$session_rid};
var SESSION_ID = {$session_id};
</script>

7
bootstrap.php

@ -324,10 +324,11 @@ class Bootstrap {
}
private function startingSession() {
global $session;
$s = \Sessionx::start();
//$s->load();
// Starting session.
$sess = Session::start(APP_NAME);
$session = $sess->get('session');
//$sess = Session::start(APP_NAME);
//$session = $sess->get('session');
//$this->user = new User;

2
manifest.webapp

@ -1,5 +1,5 @@
{
"version": "0.7beta6",
"version": "0.7.2",
"name": "Movim",
"description": "Kick Ass Social Network, decentralized and fully based on XMPP ",
"icons": {

8
system/RPC.php

@ -96,14 +96,6 @@ class RPC
$request = json_decode($json);
// We force the rid and id session number from the browser
/*
if(isset($request->session->rid)
&& isset($request->session->id)) {
global $session;
$session['rid'] = $request->session->rid;
$session['id'] = $request->session->id;
}*/
if(isset($_GET['do']) && $_GET['do'] == 'poll') {
moxl\ping();
} else {

114
system/Sessionx.php

@ -17,7 +17,7 @@
* See COPYING for licensing information.
*/
class Sessionx {
protected static $_sid = null;
protected static $_sessionid = null;
protected static $_instance;
private $_max_age = 86400; // 24hour
private $_timestamp;
@ -25,15 +25,18 @@ class Sessionx {
private $_rid;
private $_id;
public $user;
public $ressource;
public $sessionid;
public $url;
public $port;
public $host;
public $domain;
public $active = false;
public $config;
private $_currentid;
private $_user;
private $_password;
private $_ressource;
private $_sid;
private $_url;
private $_port;
private $_host;
private $_domain;
private $_active = false;
private $_config;
/*
* Session generation and handling part
*/
@ -41,9 +44,9 @@ class Sessionx {
protected function __construct()
{
// Does the database exist?
if(self::$_sid == null) {
if(self::$_sessionid == null) {
if(isset($_COOKIE['MOVIM_SESSION_ID'])) {
self::$_sid = $_COOKIE['MOVIM_SESSION_ID'];
self::$_sessionid = $_COOKIE['MOVIM_SESSION_ID'];
} else {
$this->regenerate();
}
@ -61,8 +64,8 @@ class Sessionx {
$hash.= $hash_chars[$r];
}
self::$_sid = $hash;
setcookie('MOVIM_SESSION_ID', self::$_sid, time() + $this->_max_age);
self::$_sessionid = $hash;
setcookie('MOVIM_SESSION_ID', self::$_sessionid, time() + $this->_max_age);
}
public static function start()
@ -77,54 +80,81 @@ class Sessionx {
/*
* Session management part
*/
private function inject() {
$s = new modl\Sessionx();
$s->session = self::$_sid;
$s->user = $this->user;
$s->ressource = $this->ressource;
$s->session = self::$_sessionid;
$s->user = $this->_user;
$s->password = $this->_password;
$s->ressource = $this->_ressource;
$s->rid = $this->_rid;
$s->sid = $this->sessionid;
$s->sid = $this->_sid;
$s->id = $this->_id;
$s->url = $this->url;
$s->port = $this->port;
$s->host = $this->host;
$s->domain = $this->domain;
$s->config = $this->config;
$s->active = $this->active;
$s->url = $this->_url;
$s->port = $this->_port;
$s->host = $this->_host;
$s->domain = $this->_domain;
$s->config = serialize($this->_config);
$s->active = $this->_active;
$s->timestamp = $this->_timestamp;
return $s;
}
public function init() {
public function init($user, $pass, $host, $domain) {
$serverconfig = \system\Conf::getServerConf();
$this->_url = $serverconfig['boshUrl'];
$this->_port = 5222;
$this->_host = $host;
$this->_domain = $domain;
$this->_user = $user;
$this->_password = $pass;
$this->_ressource = 'moxl'.substr(md5(date('c')), 3, 6);
$this->_rid = 0;
$this->_id = 0;
$sd = new modl\SessionxDAO();
$s = $this->inject();
$sd->init($s);
}
public function save() {
public function __get($key) {
$sd = new modl\SessionxDAO();
$session = $sd->get(self::$_sessionid);
}
if($key == 'rid') {
$sd = new modl\SessionxDAO();
$this->_rid = $sd->getRid(self::$_sessionid);
return $this->_rid;
}
elseif($key == 'id') {
$sd = new modl\SessionxDAO();
$this->_id = $sd->getId(self::$_sessionid);
return $this->_id;
} else {
$sd = new modl\SessionxDAO();
$session = $sd->get(self::$_sessionid);
if(isset($session->config))
$session->config = unserialize($session->config);
public function destroy() {
$sd = new modl\SessionxDAO();
$sd->delete(self::$_sid);
if($key == 'currentid')
$key = 'id';
return $session->$key;
}
}
/*
* rid and id specific getter, theses getter autoincrement each
* time the value in the database
*/
public function getId() {
public function __set($key, $value) {
if($key == 'config')
$value = serialize($value);
$sd = new modl\SessionxDAO();
$this->_id = $sd->getId(self::$_sid);
return $this->_id;
$sd->update(self::$_sessionid, $key, $value);
}
public function getRid() {
public function destroy() {
$sd = new modl\SessionxDAO();
$this->_rid = $sd->getRid(self::$_sid);
return $this->_rid;
$sd->delete(self::$_sessionid);
}
}

25
system/User.php

@ -25,11 +25,12 @@ class User {
function __construct()
{
if($this->isLogged()) {
global $session;
$this->username = $session['user'].'@'.$session['host'];
$session = \Sessionx::start();
$this->username = $session->user.'@'.$session->host;
if(isset($session['config']))
$this->config = $session['config'];
if($session->config)
$this->config = $session->config;
$this->sizelimit = (int)\system\Conf::getServerConfElement('sizeLimit');
@ -84,10 +85,10 @@ class User {
function isLogged()
{
// User is not logged in if both the session vars and the members are unset.
global $session;
$session = \Sessionx::start();
if(isset($session['on']) && $session['on'])
return $session['on'];
if($session->active)
return $session->active;
else
return false;
}
@ -96,6 +97,9 @@ class User {
{
$pd = new modl\PresenceDAO();
$pd->clearPresence($this->username);
$s = \Sessionx::start();
$s->destroy();
$sess = Session::start(APP_NAME);
Session::dispose(APP_NAME);
@ -113,11 +117,8 @@ class User {
function setConfig(array $config)
{
global $session;
$session['config'] = $config;
$sess = Session::start(APP_NAME);
$sess->set('session', $session);
$session = \Sessionx::start();
$session->config = $config;
}
function getConfig($key = false)

4
system/controllers/BaseController.php

@ -13,7 +13,7 @@ class BaseController {
$this->page->addScript('movim_utils.js');
$this->page->addScript('movim_base.js');
$this->page->addScript('movim_tpl.js');
$this->page->addScript('movim_session.js');
//$this->page->addScript('movim_session.js');
$this->page->addScript('movim_rpc.js');
}
@ -22,7 +22,7 @@ class BaseController {
* Loads up the language, either from the User or default.
*/
function load_language() {
$user = new user();
$user = new User();
if($user->isLogged()) {
try{
$lang = $user->getConfig('language');

Loading…
Cancel
Save