* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Fre e Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILIT Y or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 5 9 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * * * @package OXSessionD */ /** * * OXSessionD - Abstraction of the OpenExchange session daemon access * * The code of this class is heavily derivated from the * OXtensions::OXSession perl module at the openexchange wiki. * */ class OXSessionD { /** * The hostname of the session daemon * * @access private * @var string */ var $host; /** * The port of the session daemon * * @access private * @var string */ var $port; /** * The connected socket. * * @access private * @var string */ var $socket; /** * The session id * * @access public * @var string */ var $sid; /** * Constructor * * The constructor opens a connection to the OpenExchange session * daemon and returns an OXsession object. * * @access public * @param string $host * @param string $port * @return object */ function OXSessionD ($host,$port) { $this->host = $host; $this->port = $port; } /** * Class method which returns a list of sessions * * This method returns an array with session data arrays. * * @access public * @return bool */ function getSessions() { $query = 'getsessions: '.base64_encode('authdata'); $data = OXSessionD::_queryDaemon($query); foreach ($data as $line) { // format "sessionID uid\x01pass\x01\lang\x01clientip\x01host" list($sid,$str) = preg_split('/\s+/',$line); $str = base64_decode($str); list($uid,$pass,$lang,$clientip,$host) = preg_split('/\x01/',$str); $slist[$sid] = new OXSession($uid,$pass,$lang,$clientip,$host,$sid); } return $slist; } /** * Class method to check if the user exists * * @access public * @param string $uid * @return bool */ function checkUser($uid) { $data = OXSessionD::_queryDaemon('checkuser: '.$uid); if ($data) { return TRUE; } else { return FALSE; } } /** * Class method forces the session daemon to log session data * * @access public * @return bool */ function report($uid) { $data = OXSessionD::_queryDaemon('report:'); if ($data) { return TRUE; } else { return FALSE; } } /** * Performs an authentication request and starts a session * * This method performs an authentication request. If successful * an openexchange session will be established. A php session with * the same session id with the openexchange session will be * initiated too. * * The parameters are: * * uid - user id * passwd - password (cleartext) * lang - language * remoteip - ip address of the clients host * hostname - name of the virtual host * sid - session id * * @access public * @param string $login * @param string $passwd * @param string $remoteip * @param string $hostname * @param string $lang (default DE) * @return string $sessionid */ function login($login,$passwd,$remoteip,$hostname,$lang='DE') { $query = base64_encode($login."\x01". $passwd."\x01". $lang."\x01". $remoteip."\x01". $hostname); $query = preg_replace('/\n/','',$query); session_start(); $sid = session_id(); $timestamp = time(); $resp = $this->_queryDaemon("add: $timestamp 3600 $sid $query"); if ($resp) { $this->_setOXCookie($sid); return new OXSession($login,$passwd,$lang,$remoteip,$hostname,$sid); } else { $this->_destroy_php_session($sid); return FALSE; } } /** * Loads the data of a certain session. * * Returns an array with authentication information of the session * assigned to the given session id. The following keys will be * set: * uid - user id * passwd - cleartext password * lang - language * localip - local ip * host - host * * @access public * @param string $sessionID * @return array $auth */ function getSession ($sessionID) { $data = $this->_queryDaemon("getauth: $sessionID"); if ($data) { $line = base64_decode($data[0]); list($uid,$pass,$lang,$localip,$host) = preg_split('/\x01/',$line); return new OXSession($uid,$pass,$lang,$clientip,$host,$this->sid); } else { return FALSE; } } /** * Reset the timeout of the OX session with the given session id * * @access public * @param string $sid * @return bool */ function ping($sid) { $data = $this->_queryDaemon('ping: '.$sid); if ($data) { return TRUE; } else { return FALSE; } } /** * Removes the current session (logoff) * * This method sets some http headers. Thus you MUST NOT call this * method after you print (echo) something to STDOUT. * * @access public * @access string $sid * @return bool */ function logoff($sid) { $data = $this->_queryDaemon('clear: '.$sid); if ($data) { $this->_destroy_ox_session($sid); $this->_destroy_php_session($sid); return TRUE; } else { return FALSE; } } /** * Destroys the current php session * * @access private * @return object $this */ function _destroy_php_session() { session_unset(); session_destroy(); // set the expiration time of the cookie to 0 setcookie($sid, '', 0); return $this; } /** * Destroys the current ox session * * @access private * @param string $sid * @return object $this */ function _destroy_ox_session($sid) { $name = 'open-xchange-session-'.$sid; // set the expiration time of the cookie to 0 setcookie($name, '', 0); return $this; } /** * Sets the openexchange session cookie * * @access private * @param string sid * @return string cookie-name */ function _setOXCookie ($sid) { $name = 'open-xchange-session-'.$sid; setcookie($name,$sid,0,'/'); return $name; } /** * Queries the session daemon * * @access private * @param string $query * @return array */ function _queryDaemon ($query) { $retary = array(); $query .= "\0"; $errno = ''; $errstr = ''; // The session daemon allows only one request per connection, // thus we close the sockete immediately. $socket = fsockopen($this->host, $this->port, $errno, $errstr, 10); if (!$socket) { error_log("Cannot open socket: $errstr ($errno)."); exit(1); } fwrite($socket, $query); while (!feof($socket)) { $line = fgets($socket, 1024); if (ereg('ERROR',$line)) { fclose($socket); return FALSE; } array_push($retary,$line); } fclose($socket); return $retary; } } /************************************************************************/ /************************************************************************/ /** * * Copyright (c) 2005 Martin Werthmoeller * */ /** * @package OXSession */ /** * * OXSession - OpenExchange session class * */ class OXSession { /** * The login name. * * @access public * @var string */ var $login; /** * The cleartext password. * * @access public * @var string */ var $password; /** * The configured language. * * @access public * @var string */ var $language; /** * The ip address of the client. * * @access public * @var string */ var $clientip; /** * The hostname of the openexchange server. * * @access public * @var string */ var $hostname; /** * The session id of the current session * * @access public * @var string */ var $sid; /** * The URI of the openexchange webinterface * * @access public * @var string */ var $weburi; /** * Constructor * * @access public * @param string $login * @param string $password * @param string $language * @param string $clientip * @param string $hostname * @param string $sid * @return object */ function OXSession ($login,$password,$language,$clientip,$hostname,$sid) { $this->login = $login; $this->password = $password; $this->language = $language; $this->clientip = $clientip; $this->hostname = $hostname; $this->sid = $sid; $this->weburi = 'http://'. $hostname . '/servlet/intranet?SITE=beforeAuth&sessionID=' . $this->sid; } } ?>