2011-01-11 08:02:45 +00:00
< ? php
/**
2011-01-14 19:40:12 +00:00
* Implementation of a simple session management .
*
2013-02-14 11:10:53 +00:00
* SeedDMS uses its own simple session management , storing sessions
2011-01-14 19:40:12 +00:00
* into the database . A session holds the currently logged in user ,
* the theme and the language .
2011-01-11 08:02:45 +00:00
*
* @ category DMS
2013-02-14 11:10:53 +00:00
* @ package SeedDMS
2011-01-11 08:02:45 +00:00
* @ license GPL 2
* @ version @ version @
* @ author Uwe Steinmann < uwe @ steinmann . cx >
* @ copyright 2011 Uwe Steinmann
* @ version Release : @ package_version @
*/
/**
2011-01-14 19:40:12 +00:00
* Class to represent a session
*
* This class provides some very basic methods to load , save and delete
* sessions . It does not set or retrieve a cockie . This is up to the
* application . The class basically provides access to the session database
* table .
2011-01-11 08:02:45 +00:00
*
* @ category DMS
2013-02-14 11:10:53 +00:00
* @ package SeedDMS
2011-01-11 08:02:45 +00:00
* @ author Markus Westphal , Malcolm Cowe , Uwe Steinmann < uwe @ steinmann . cx >
* @ copyright 2011 Uwe Steinmann
* @ version Release : @ package_version @
*/
2013-02-14 11:10:53 +00:00
class SeedDMS_Session {
2011-01-11 08:02:45 +00:00
/**
* @ var object $db reference to database object . This must be an instance
2013-02-14 11:10:53 +00:00
* of { @ link SeedDMS_Core_DatabaseAccess } .
2011-01-11 08:02:45 +00:00
* @ access protected
*/
protected $db ;
/**
* @ var array $data session data
* @ access protected
*/
protected $data ;
/**
* @ var string $id session id
* @ access protected
*/
protected $id ;
/**
* Create a new instance of the session handler
*
* @ param object $db object to access the underlying database
2013-02-14 11:10:53 +00:00
* @ return object instance of SeedDMS_Session
2011-01-11 08:02:45 +00:00
*/
function __construct ( $db ) { /* {{{ */
$this -> db = $db ;
2012-08-29 20:42:33 +00:00
$this -> id = false ;
2011-01-11 08:02:45 +00:00
} /* }}} */
/**
2011-01-14 19:40:12 +00:00
* Load session by its id from database
2011-01-11 08:02:45 +00:00
*
* @ param string $id id of session
* @ return boolean true if successful otherwise false
*/
function load ( $id ) { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " SELECT * FROM `tblSessions` WHERE `id` = " . $this -> db -> qstr ( $id );
2011-01-11 08:02:45 +00:00
$resArr = $this -> db -> getResultArray ( $queryStr );
if ( is_bool ( $resArr ) && $resArr == false )
return false ;
if ( count ( $resArr ) == 0 )
return false ;
2012-08-29 20:42:33 +00:00
$this -> id = $id ;
2013-05-18 20:29:45 +00:00
$this -> data = array ( 'userid' => $resArr [ 0 ][ 'userID' ], 'theme' => $resArr [ 0 ][ 'theme' ], 'lang' => $resArr [ 0 ][ 'language' ], 'id' => $resArr [ 0 ][ 'id' ], 'lastaccess' => $resArr [ 0 ][ 'lastAccess' ], 'su' => $resArr [ 0 ][ 'su' ]);
2013-01-30 19:58:36 +00:00
if ( $resArr [ 0 ][ 'clipboard' ])
$this -> data [ 'clipboard' ] = json_decode ( $resArr [ 0 ][ 'clipboard' ], true );
else
$this -> data [ 'clipboard' ] = array ( 'docs' => array (), 'folders' => array ());
2013-05-21 06:36:11 +00:00
if ( $resArr [ 0 ][ 'splashmsg' ])
$this -> data [ 'splashmsg' ] = json_decode ( $resArr [ 0 ][ 'splashmsg' ], true );
2013-05-18 20:52:43 +00:00
else
2013-05-21 06:36:11 +00:00
$this -> data [ 'splashmsg' ] = array ();
2011-01-11 08:02:45 +00:00
return $resArr [ 0 ];
} /* }}} */
/**
2011-01-14 19:40:12 +00:00
* Create a new session and saving the given data into the database
2011-01-11 08:02:45 +00:00
*
* @ param array $data data saved in session ( the only fields supported
2013-04-19 06:44:04 +00:00
* are userid , theme , language , su )
2011-01-11 08:02:45 +00:00
* @ return string / boolean id of session of false in case of an error
*/
function create ( $data ) { /* {{{ */
2017-12-30 11:41:07 +00:00
$id = " " . rand () . '-' . microtime () . '-' . rand () . " " ;
2011-01-11 08:02:45 +00:00
$id = md5 ( $id );
2013-02-27 08:08:16 +00:00
$lastaccess = time ();
2017-02-12 07:07:14 +00:00
$queryStr = " INSERT INTO `tblSessions` (`id`, `userID`, `lastAccess`, `theme`, `language`, `su`) " .
2013-04-22 08:26:48 +00:00
" VALUES (' " . $id . " ', " . $data [ 'userid' ] . " , " . $lastaccess . " , ' " . $data [ 'theme' ] . " ', ' " . $data [ 'lang' ] . " ', 0) " ;
2011-01-11 08:02:45 +00:00
if ( ! $this -> db -> getResult ( $queryStr )) {
return false ;
}
$this -> id = $id ;
$this -> data = $data ;
2013-01-24 08:04:14 +00:00
$this -> data [ 'id' ] = $id ;
$this -> data [ 'lastaccess' ] = $lastaccess ;
2013-05-02 06:27:59 +00:00
$this -> data [ 'su' ] = 0 ;
2013-01-30 19:58:36 +00:00
$this -> data [ 'clipboard' ] = array ( 'docs' => array (), 'folders' => array ());
2013-05-18 20:29:45 +00:00
$this -> data [ 'clipboard' ] = array ( 'type' => '' , 'msg' => '' );
2013-05-21 06:36:11 +00:00
$this -> data [ 'splashmsg' ] = array ();
2011-01-11 08:02:45 +00:00
return $id ;
} /* }}} */
2014-05-22 04:35:12 +00:00
/**
* Update last access time of session
*
* This function should be called , when the last access time of the
* session must be updated . This should be done at least after login ,
* but can also be done at any other time . Sessions that are never
* updated will be deleted when deleteByTime () is called and the session
* life time has exceeded the cookie life time or 1 week .
*
* @ param string $id id of session
* @ return boolean true if successful otherwise false
*/
function updateAccess ( $id ) { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `lastAccess` = " . time () . " WHERE `id` = " . $this -> db -> qstr ( $id );
2014-05-22 04:35:12 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
return true ;
} /* }}} */
2011-01-11 08:02:45 +00:00
/**
2011-01-14 19:40:12 +00:00
* Delete sessions older than a given time from the database
2011-01-11 08:02:45 +00:00
*
* @ param integer $sec maximum number of seconds a session may live
* @ return boolean true if successful otherwise false
*/
2011-01-14 19:40:12 +00:00
function deleteByTime ( $sec ) { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " DELETE FROM `tblSessions` WHERE " . time () . " - `lastAccess` > " . $sec ;
2011-01-11 08:02:45 +00:00
if ( ! $this -> db -> getResult ( $queryStr )) {
return false ;
}
return true ;
2011-01-14 19:40:12 +00:00
} /* }}} */
2011-01-11 08:02:45 +00:00
/**
* Delete session by its id
*
* @ param string $id id of session
* @ return boolean true if successful otherwise false
*/
function delete ( $id ) { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " DELETE FROM `tblSessions` WHERE `id` = " . $this -> db -> qstr ( $id );
2011-01-11 08:02:45 +00:00
if ( ! $this -> db -> getResult ( $queryStr )) {
return false ;
}
2012-08-29 20:42:33 +00:00
$this -> id = false ;
2011-01-11 08:02:45 +00:00
return true ;
} /* }}} */
2012-08-29 20:42:33 +00:00
/**
* Get session id
*
* @ return string session id
*/
function getId () { /* {{{ */
return $this -> id ;
} /* }}} */
2013-01-24 08:04:14 +00:00
2017-11-21 07:33:57 +00:00
/**
* Get user id of session
*
* @ return integer user id
*/
function getUser () { /* {{{ */
return $this -> data [ 'userid' ];
} /* }}} */
2013-02-06 17:33:11 +00:00
/**
* Set user of session
*
* @ param integer $userid id of user
*/
function setUser ( $userid ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `userID` = " . $this -> db -> qstr ( $userid ) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-02-06 17:33:11 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
$this -> data [ 'userid' ] = $userid ;
}
return true ;
} /* }}} */
2013-01-24 08:04:14 +00:00
/**
* Set language of session
*
2013-01-30 19:58:36 +00:00
* @ param string $lang language
2013-01-24 08:04:14 +00:00
*/
function setLanguage ( $lang ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `language` = " . $this -> db -> qstr ( $lang ) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-01-24 08:04:14 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
$this -> data [ 'lang' ] = $lang ;
}
return true ;
} /* }}} */
/**
2013-01-30 19:58:36 +00:00
* Get language of session
2013-01-24 08:04:14 +00:00
*
2013-01-30 19:58:36 +00:00
* @ return string language
2013-01-24 08:04:14 +00:00
*/
function getLanguage () { /* {{{ */
return $this -> data [ 'lang' ];
} /* }}} */
2013-01-30 19:58:36 +00:00
2013-04-19 06:44:04 +00:00
/**
* Substitute user of session
*
* @ param integer $su user id
*/
function setSu ( $su ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `su` = " . ( int ) $su . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-04-19 06:44:04 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
$this -> data [ 'su' ] = ( int ) $su ;
}
return true ;
} /* }}} */
/**
* Reset substitute user of session
*
* @ param integer $su user id
*/
function resetSu () { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `su` = 0 WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-04-19 06:44:04 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
$this -> data [ 'su' ] = 0 ;
}
return true ;
} /* }}} */
/**
* Get substituted user id of session
*
* @ return integer substituted user id
*/
function getSu () { /* {{{ */
return $this -> data [ 'su' ];
} /* }}} */
2013-01-30 19:58:36 +00:00
/**
* Set clipboard of session
*
* @ param array $clipboard list of folders and documents
*/
function setClipboard ( $clipboard ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `clipboard` = " . $this -> db -> qstr ( json_encode ( $clipboard )) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-01-30 19:58:36 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
$this -> data [ 'clipboard' ] = $clipboard ;
}
return true ;
} /* }}} */
/**
* Get clipboard of session
*
* @ return array list of clipboard entries
*/
function getClipboard () { /* {{{ */
return ( array ) $this -> data [ 'clipboard' ];
} /* }}} */
/**
* Add to clipboard of session
*
* @ param object $object Document or folder
*/
function addToClipboard ( $object ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2020-05-18 16:29:19 +00:00
$dms = $object -> getDMS ();
2019-08-08 07:08:27 +00:00
if ( $object -> isType ( 'document' )) {
2013-01-30 19:58:36 +00:00
if ( ! in_array ( $object -> getID (), $this -> data [ 'clipboard' ][ 'docs' ]))
array_push ( $this -> data [ 'clipboard' ][ 'docs' ], $object -> getID ());
2019-08-08 07:08:27 +00:00
} elseif ( $object -> isType ( 'folder' )) {
2013-01-30 19:58:36 +00:00
if ( ! in_array ( $object -> getID (), $this -> data [ 'clipboard' ][ 'folders' ]))
array_push ( $this -> data [ 'clipboard' ][ 'folders' ], $object -> getID ());
}
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `clipboard` = " . $this -> db -> qstr ( json_encode ( $this -> data [ 'clipboard' ])) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-01-30 19:58:36 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
}
return true ;
} /* }}} */
/**
* Remove from clipboard
*
* @ param object $object Document or folder to remove
*/
function removeFromClipboard ( $object ) { /* {{{ */
/* id is only set if load() was called before */
if ( $this -> id ) {
2020-05-18 16:29:19 +00:00
$dms = $object -> getDMS ();
2019-08-08 07:08:27 +00:00
if ( $object -> isType ( 'document' )) {
2013-01-30 19:58:36 +00:00
$key = array_search ( $object -> getID (), $this -> data [ 'clipboard' ][ 'docs' ]);
if ( $key !== false )
unset ( $this -> data [ 'clipboard' ][ 'docs' ][ $key ]);
2019-08-08 07:08:27 +00:00
} elseif ( $object -> isType ( 'folder' )) {
2013-01-30 19:58:36 +00:00
$key = array_search ( $object -> getID (), $this -> data [ 'clipboard' ][ 'folders' ]);
if ( $key !== false )
unset ( $this -> data [ 'clipboard' ][ 'folders' ][ $key ]);
}
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `clipboard` = " . $this -> db -> qstr ( json_encode ( $this -> data [ 'clipboard' ])) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-01-30 19:58:36 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
}
return true ;
} /* }}} */
2013-06-13 21:14:47 +00:00
/**
* Clear clipboard
*
*/
function clearClipboard () { /* {{{ */
$this -> data [ 'clipboard' ][ 'docs' ] = array ();
$this -> data [ 'clipboard' ][ 'folders' ] = array ();
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `clipboard` = " . $this -> db -> qstr ( json_encode ( $this -> data [ 'clipboard' ])) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-06-13 21:14:47 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
return true ;
} /* }}} */
2013-05-18 20:29:45 +00:00
/**
2013-05-21 06:36:11 +00:00
* Set splash message of session
2013-05-18 20:29:45 +00:00
*
* @ param array $msg contains 'typ' and 'msg'
*/
2013-05-21 06:36:11 +00:00
function setSplashMsg ( $msg ) { /* {{{ */
2013-05-18 20:29:45 +00:00
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `splashmsg` = " . $this -> db -> qstr ( json_encode ( $msg )) . " WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-05-18 20:29:45 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
2013-05-21 06:36:11 +00:00
$this -> data [ 'splashmsg' ] = $msg ;
2013-05-18 20:29:45 +00:00
}
return true ;
} /* }}} */
2013-05-18 20:52:43 +00:00
/**
2013-05-21 06:36:11 +00:00
* Set splash message of session
2013-05-18 20:52:43 +00:00
*
* @ param array $msg contains 'typ' and 'msg'
*/
2013-05-21 06:36:11 +00:00
function clearSplashMsg () { /* {{{ */
2013-05-18 20:52:43 +00:00
/* id is only set if load() was called before */
if ( $this -> id ) {
2017-02-12 07:07:14 +00:00
$queryStr = " UPDATE `tblSessions` SET `splashmsg` = '' WHERE `id` = " . $this -> db -> qstr ( $this -> id );
2013-05-18 20:52:43 +00:00
if ( ! $this -> db -> getResult ( $queryStr ))
return false ;
2013-05-21 06:36:11 +00:00
$this -> data [ 'splashmsg' ] = '' ;
2013-05-18 20:52:43 +00:00
}
return true ;
} /* }}} */
2013-05-18 20:29:45 +00:00
/**
2013-05-21 06:36:11 +00:00
* Get splash message of session
2013-05-18 20:29:45 +00:00
*
2013-05-21 06:36:11 +00:00
* @ return array last splash message
2013-05-18 20:29:45 +00:00
*/
2013-05-21 06:36:11 +00:00
function getSplashMsg () { /* {{{ */
return ( array ) $this -> data [ 'splashmsg' ];
2013-05-18 20:29:45 +00:00
} /* }}} */
2014-05-22 04:35:12 +00:00
/**
* Get timestamp of last access
*
* @ return int last access time
*/
function getLastAccess () { /* {{{ */
return ( int ) $this -> data [ 'lastaccess' ];
} /* }}} */
}
/**
* Class for managing sessions
*
* This class is for retrieving sessions .
*
* @ category DMS
* @ package SeedDMS
* @ author Uwe Steinmann < uwe @ steinmann . cx >
* @ copyright 2014 Uwe Steinmann
* @ version Release : @ package_version @
*/
class SeedDMS_SessionMgr {
/**
* @ var object $db reference to database object . This must be an instance
* of { @ link SeedDMS_Core_DatabaseAccess } .
* @ access protected
*/
protected $db ;
/**
* Create a new instance of the session manager
*
* @ param object $db object to access the underlying database
* @ return object instance of SeedDMS_SessionMgr
*/
function __construct ( $db ) { /* {{{ */
$this -> db = $db ;
} /* }}} */
/**
* Create a new session and saving the given data into the database
*
* @ param array $data data saved in session ( the only fields supported
* are userid , theme , language , su )
* @ return string / boolean id of session of false in case of an error
*/
function create ( $data ) { /* {{{ */
$id = " " . rand () . time () . rand () . " " ;
$id = md5 ( $id );
$lastaccess = time ();
2017-02-12 07:07:14 +00:00
$queryStr = " INSERT INTO `tblSessions` (`id`, `userID`, `lastAccess`, `theme`, `language`, `su`) " .
2014-05-22 04:35:12 +00:00
" VALUES (' " . $id . " ', " . $data [ 'userid' ] . " , " . $lastaccess . " , ' " . $data [ 'theme' ] . " ', ' " . $data [ 'lang' ] . " ', 0) " ;
if ( ! $this -> db -> getResult ( $queryStr )) {
return false ;
}
return $id ;
} /* }}} */
/**
* Get list of all active sessions
*
* @ return array list of sessions
*/
function getAllSessions () { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " SELECT * FROM `tblSessions` " ;
2014-05-22 04:35:12 +00:00
$resArr = $this -> db -> getResultArray ( $queryStr );
if ( is_bool ( $resArr ) && $resArr == false )
return false ;
$sessions = array ();
foreach ( $resArr as $rec ) {
$session = new SeedDMS_Session ( $this -> db );
$session -> load ( $rec [ 'id' ]);
$sessions [] = $session ;
}
return $sessions ;
} /* }}} */
/**
* Get list of active sessions for a given user
*
* @ return array list of sessions
*/
function getUserSessions ( $user ) { /* {{{ */
2017-02-12 07:07:14 +00:00
$queryStr = " SELECT * FROM `tblSessions` WHERE `userID`= " . $user -> getID ();
2014-05-22 04:35:12 +00:00
$resArr = $this -> db -> getResultArray ( $queryStr );
if ( is_bool ( $resArr ) && $resArr == false )
return false ;
$sessions = array ();
foreach ( $resArr as $rec ) {
$session = new SeedDMS_Session ( $this -> db );
$session -> load ( $rec [ 'id' ]);
$sessions [] = $session ;
}
return $sessions ;
} /* }}} */
2017-03-24 14:54:52 +00:00
/**
* Get list of active sessions with a given time
*
* @ return array list of sessions
*/
function getLastAccessedSessions ( $datetime ) { /* {{{ */
if ( ! $ts = makeTsFromLongDate ( $datetime ))
return false ;
2017-10-27 17:42:56 +00:00
$queryStr = " SELECT a.* FROM `tblSessions` AS a LEFT OUTER JOIN `tblSessions` AS b ON a.`userID`=b.`userID` AND a.`lastAccess`< b.`lastAccess` WHERE b.`userID` IS NULL AND a.`lastAccess` >= " . $ts ;
2017-03-24 14:54:52 +00:00
$resArr = $this -> db -> getResultArray ( $queryStr );
if ( is_bool ( $resArr ) && $resArr == false )
return false ;
$sessions = array ();
foreach ( $resArr as $rec ) {
$session = new SeedDMS_Session ( $this -> db );
$session -> load ( $rec [ 'id' ]);
$sessions [] = $session ;
}
return $sessions ;
} /* }}} */
2011-01-11 08:02:45 +00:00
}