phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] phpgwapi/inc/sessions class.sessions.inc.php cl...


From: Dave Hall
Subject: [Phpgroupware-cvs] phpgwapi/inc/sessions class.sessions.inc.php cl...
Date: Sat, 09 Sep 2006 11:35:41 +0000

CVSROOT:        /cvsroot/phpgwapi
Module name:    phpgwapi
Changes by:     Dave Hall <skwashd>     06/09/09 11:35:41

Added files:
        inc/sessions   : class.sessions.inc.php 
                         class.sessions_db.inc.php 
                         class.sessions_php.inc.php 

Log message:
        switch to new sessions classes, rename php4 to php and use object 
facotry

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/phpgwapi/inc/sessions/class.sessions.inc.php?cvsroot=phpgwapi&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/phpgwapi/inc/sessions/class.sessions_db.inc.php?cvsroot=phpgwapi&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/phpgwapi/inc/sessions/class.sessions_php.inc.php?cvsroot=phpgwapi&rev=1.1

Patches:
Index: class.sessions.inc.php
===================================================================
RCS file: class.sessions.inc.php
diff -N class.sessions.inc.php
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ class.sessions.inc.php      9 Sep 2006 11:35:41 -0000       1.1
@@ -0,0 +1,1311 @@
+<?php 
+       /**
+       * Session management
+       * @author NetUSE AG Boris Erdmann, Kristian Koehntopp
+       * @author Dan Kuykendall <address@hidden>
+       * @author Joseph Engo <address@hidden>
+       * @author Ralf Becker <address@hidden>
+       * @copyright Copyright (C) 1998-2000 NetUSE AG Boris Erdmann, Kristian 
Koehntopp
+       * @copyright Portions Copyright (C) 2000-2004 Free Software Foundation, 
Inc. http://www.fsf.org/
+       * @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General 
Public License
+       * @package phpgwapi
+       * @subpackage accounts
+       * @version $Id: class.sessions.inc.php,v 1.1 2006/09/09 11:35:41 
skwashd Exp $
+       * @link http://www.sanisoft.com/phplib/manual/session.php
+       */
+
+       /**
+       * Session Management
+       * 
+       * This allows phpGroupWare to use php4 or database sessions 
+       *
+       * @package phpgwapi
+       * @subpackage accounts
+       * @abstract
+       */
+       class sessions
+       {
+               /**
+               * @var string current user login
+               */
+               var $login;
+               
+               /**
+               * @var sting current user password
+               */
+               var $passwd;
+               
+               /**
+               * @var int current user db/ldap account id
+               */
+               var $account_id;
+               
+               /**
+               * @var string current user account login id - ie address@hidden
+               */
+               var $account_lid;
+               
+               /**
+               * @var string previous page call id - repost prevention
+               */
+               var $history_id;
+
+               /**
+               * @var string domain for current user
+               */
+               var $account_domain;
+               
+               /**
+               * @var session type flag, A - anonymous session, N - None, 
normal session
+               */
+               var $session_flags;
+               
+               /**
+               * @var string current user session id
+               */
+               var $sessionid;
+               
+               /**
+               * @var string not sure what this does, but it is important :)
+               */
+               var $kp3;
+               
+               /**
+               * @var string encryption key?
+               */
+               var $key;
+               
+               /**
+               * @var string iv == ivegotnoidea ;) (skwashd)
+               */
+               var $iv;
+               
+               /**
+               * @var session data
+               */
+               var $data;
+               
+               /**
+               * @var object holder for the database object
+               */
+               var $db;
+
+               /**
+               * @var array publicly available methods
+               */
+               var $public_functions = array
+               (
+                       'list_methods' => true,
+                       'update_dla'   => true,
+                       'list'         => true,
+                       'total'        => true
+               );
+
+               /**
+               * @var string domain for cookies
+               */
+               var $cookie_domain;
+               
+               /**
+               * @var name of XML-RPC/SOAP method called
+               */
+               var $xmlrpc_method_called;
+               
+               /**
+               * Constructor just loads up some defaults from cookies
+               */
+               function sessions()
+               {
+                       $this->db =& $GLOBALS['phpgw']->db;
+                       $this->sessionid = 
get_var('sessionid',array('GET','COOKIE'));
+                       $this->kp3       = get_var('kp3',array('GET','COOKIE'));
+                       
+                       /* Create the crypto object */
+                       $GLOBALS['phpgw']->crypto = 
createObject('phpgwapi.crypto');
+                       $this->phpgw_set_cookiedomain();
+                       
+                       // verfiy and if necessary create and save our config 
settings
+                       //
+                       $save_rep = False;
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['max_access_log_age']))
+                       {
+                               
$GLOBALS['phpgw_info']['server']['max_access_log_age'] = 90;// default 90 days
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['block_time']))
+                       {
+                               $GLOBALS['phpgw_info']['server']['block_time'] 
= 30;// default 30min
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['num_unsuccessful_id']))
+                       {
+                               
$GLOBALS['phpgw_info']['server']['num_unsuccessful_id'] = 3;// default 3 trys 
per id
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']))
+                       {
+                               // default same as for id
+                               
$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']  
+                                       = 
$GLOBALS['phpgw_info']['server']['num_unsuccessful_id'];
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['install_id']))
+                       {
+                               $GLOBALS['phpgw_info']['server']['install_id']
+                                       = 
md5($GLOBALS['phpgw']->common->randomstring(15));
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['sessions_timeout']))
+                       {
+                               
$GLOBALS['phpgw_info']['server']['sessions_timeout'] = 14400;
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['sessions_app_timeout']))
+                       {
+                               
$GLOBALS['phpgw_info']['server']['sessions_app_timeout'] = 86400;
+                               $save_rep = True;
+                       }
+                       if 
(!isset($GLOBALS['phpgw_info']['server']['max_history']))
+                       {
+                               $GLOBALS['phpgw_info']['server']['max_history'] 
= 20;
+                               $save_rep = True;
+                       }
+                       if ($save_rep)
+                       {
+                               $config = 
createObject('phpgwapi.config','phpgwapi');
+                               $config->read_repository();
+                               
$config->value('max_access_log_age',$GLOBALS['phpgw_info']['server']['max_access_log_age']);
+                               
$config->value('block_time',$GLOBALS['phpgw_info']['server']['block_time']);
+                               
$config->value('num_unsuccessful_id',$GLOBALS['phpgw_info']['server']['num_unsuccessful_id']);
+                               
$config->value('num_unsuccessful_ip',$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']);
+                               
$config->value('install_id',$GLOBALS['phpgw_info']['server']['install_id']);
+                               
$config->value('sessions_timeout',$GLOBALS['phpgw_info']['server']['sessions_timeout']);
+                               
$config->value('sessions_app_timeout',$GLOBALS['phpgw_info']['server']['sessions_app_timeout']);
+                               $config->value('max_history', 
$GLOBALS['phpgw_info']['server']['max_history'] );
+                               $config->save_repository();
+                               unset($config);
+                       }
+               }
+
+               /**
+               * Introspection for XML-RPC/SOAP
+               * Diabled - why??
+               *
+               * @param string $_type tpye of introspection being sought
+               * @return array available methods and args
+               */
+               function DONTlist_methods($_type)
+               {
+                       if (is_array($_type))
+                       {
+                               $_type = $_type['type'];
+                       }
+                       
+                       switch($_type)
+                       {
+                               case 'xmlrpc':
+                                       $xml_functions = array(
+                                               'list_methods' => array(
+                                                       'function'  => 
'list_methods',
+                                                       'signature' => 
array(array(xmlrpcStruct,xmlrpcString)),
+                                                       'docstring' => 
lang('Read this list of methods.')
+                                               ),
+                                               'update_dla' => array(
+                                                       'function'  => 
'update_dla',
+                                                       'signature' => 
array(array(xmlrpcBoolean)),
+                                                       'docstring' => 
lang('Returns an array of todo items')
+                                               )
+                                       );
+                                       return $xml_functions;
+                                       break;
+                               case 'soap':
+                                       return $this->soap_functions;
+                                       break;
+                               default:
+                                       return array();
+                                       break;
+                       }
+               }
+
+               /**
+               * Check to see if a session is still current and valid
+               *
+               * @param string $sessionid session id to be verfied
+               * @param string $kp3 ?? to be verified
+               * @return bool is the session valid?
+               */
+               
+               function verify($sessionid='',$kp3='')
+               {
+                       if(empty($sessionid) || !$sessionid)
+                       {
+                               $sessionid = 
get_var('sessionid',array('GET','COOKIE'));
+                               $kp3       = 
get_var('kp3',array('GET','COOKIE'));
+                       }
+                       
+                       $this->sessionid = $sessionid;
+                       $this->kp3       = $kp3;
+                       
+                       $session = $this->read_session($sessionid);
+                       //echo "<pre>session::verify(id='$sessionid'): \n" . 
print_r($session, true) . "</pre>\n";
+                       
+                       if ( !isset($session['session_dla']) 
+                               || $session['session_dla'] <= (time() - 
$GLOBALS['phpgw_info']['server']['sessions_timeout']))
+                       {
+                               $this->clean_sessions();
+                               return False;
+                       }
+
+                       $this->session_flags = $session['session_flags'];
+
+                       $lid_data = explode('@', $session['session_lid']);
+                       $this->account_lid = $lid_data[0];
+
+                       if ( isset($lid_data[1]) )
+                       {
+                               $this->account_domain = $lid_data[1];
+                       }
+                       else
+                       {
+                               $this->account_domain = 
$GLOBALS['phpgw_info']['server']['default_domain'];
+                       }
+                       unset($lid_data);
+
+                       $GLOBALS['phpgw_info']['user']['kp3'] = $this->kp3;
+
+                       $this->update_dla();
+                       $this->account_id = 
$GLOBALS['phpgw']->accounts->name2id($this->account_lid);
+                       if (!$this->account_id)
+                       {
+                               return false;
+                       }
+
+                       $GLOBALS['phpgw_info']['user']['account_id'] = 
$this->account_id;
+
+                       /* init the crypto object before appsession call below 
*/
+                       $this->key = md5($this->kp3 . $this->sessionid . 
$GLOBALS['phpgw_info']['server']['encryptkey']);
+                       $this->iv  = 
$GLOBALS['phpgw_info']['server']['mcrypt_iv'];
+                       
$GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
+
+                       
$this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
+                       
+                       if ($this->user['expires'] != -1 && 
$this->user['expires'] < time())
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-VerifySession, 
account loginid %1 is expired',
+                                               'p1'   => $this->account_lid,
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+                               if(is_object($GLOBALS['phpgw']->crypto))
+                               {
+                                       $GLOBALS['phpgw']->crypto->cleanup();
+                                       unset($GLOBALS['phpgw']->crypto);
+                               }
+                               return False;
+                       }
+
+                       $GLOBALS['phpgw_info']['user']  = $this->user;
+                       $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
+
+                       $GLOBALS['phpgw_info']['user']['session_ip'] = 
$session['session_ip'];
+                       $GLOBALS['phpgw_info']['user']['passwd']     = 
base64_decode($this->appsession('password','phpgwapi'));
+
+                       if ($this->account_domain != 
$GLOBALS['phpgw_info']['user']['domain'])
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-VerifySession, the 
domains %1 and %2 don\'t match',
+                                               'p1'   => $userid_array[1],
+                                               'p2'   => 
$GLOBALS['phpgw_info']['user']['domain'],
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+                               if(is_object($GLOBALS['phpgw']->crypto))
+                               {
+                                       $GLOBALS['phpgw']->crypto->cleanup();
+                                       unset($GLOBALS['phpgw']->crypto);
+                               }
+                               return False;
+                       }
+
+                       if 
(@$GLOBALS['phpgw_info']['server']['sessions_checkip'])
+                       {
+                               if (PHP_OS != 'Windows' && (! 
$GLOBALS['phpgw_info']['user']['session_ip'] || 
$GLOBALS['phpgw_info']['user']['session_ip'] != $this->getuser_ip()))
+                               {
+                                       if(is_object($GLOBALS['phpgw']->log))
+                                       {
+                                               // This needs some better 
wording
+                                               
$GLOBALS['phpgw']->log->message(array(
+                                                       'text' => 
'W-VerifySession, IP %1 doesn\'t match IP %2 in session table',
+                                                       'p1'   => 
$this->getuser_ip(),
+                                                       'p2'   => 
$GLOBALS['phpgw_info']['user']['session_ip'],
+                                                       'line' => __LINE__,
+                                                       'file' => __FILE__
+                                               ));
+                                               
$GLOBALS['phpgw']->log->commit();
+                                       }
+                                       if(is_object($GLOBALS['phpgw']->crypto))
+                                       {
+                                               
$GLOBALS['phpgw']->crypto->cleanup();
+                                               
unset($GLOBALS['phpgw']->crypto);
+                                       }
+                                       return False;
+                               }
+                       }
+
+                       $GLOBALS['phpgw']->acl->acl($this->account_id);
+                       
$GLOBALS['phpgw']->accounts->accounts($this->account_id);
+                       
$GLOBALS['phpgw']->preferences->preferences($this->account_id);
+                       
$GLOBALS['phpgw']->applications->applications($this->account_id);
+
+                       if (! $this->account_lid)
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       // This needs some better wording
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-VerifySession, 
account_id is empty',
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+                               if(is_object($GLOBALS['phpgw']->crypto))
+                               {
+                                       $GLOBALS['phpgw']->crypto->cleanup();
+                                       unset($GLOBALS['phpgw']->crypto);
+                               }
+                               //echo 'DEBUG: Sessions: account_id is 
empty!<br>'."\n";
+                               return False;
+                       }
+                       else
+                       {
+                               return True;
+                       }
+               }
+
+               /**
+               * Functions for creating and verifying the session
+               */
+               
+               /**
+               * Get the ip address of current users
+               *
+               * @return string ip address
+               */
+               function getuser_ip()
+               {
+                       return (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? 
$_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
+               }
+
+               /**
+               * Set the domain used for cookies
+               *
+               * @return string domain
+               */
+               function phpgw_set_cookiedomain()
+               {
+                       $dom = $_SERVER['HTTP_HOST'];
+                       if (preg_match("/^(.*):(.*)$/",$dom,$arr))
+                       {
+                               $dom = $arr[1];
+                       }
+                       $parts = explode('.',$dom);
+                       if (count($parts) > 2)
+                       {
+                               if (!ereg('[0-9]+',$parts[1]))
+                               {
+                                       for($i=1;$i<count($parts);$i++)
+                                       {
+                                               $this->cookie_domain .= 
'.'.$parts[$i];
+                                       }
+                               }
+                               else
+                               {
+                                       $this->cookie_domain = '';
+                               }
+                       }
+                       else
+                       {
+                               $this->cookie_domain = '';
+                       }
+                       print_debug('COOKIE_DOMAIN',$this->cookie_domain,'api');
+                       
+                       $this->set_cookie_params($this->cookie_domain); // for 
php4 sessions necessary
+               }
+
+               /**
+               * Set a cookie
+               *
+               * @param string $cookiename name of cookie to be set
+               * @param string $cookievalue value to be used, if unset cookie 
is cleared (optional)
+               * @param int $cookietime when cookie should expire, 0 for 
session only (optional)
+               */
+               function 
phpgw_setcookie($cookiename,$cookievalue='',$cookietime=0)
+               {
+                       if (!$this->cookie_domain)
+                       {
+                               $this->phpgw_set_cookiedomain();
+                       }
+                       
setcookie($cookiename,$cookievalue,$cookietime,'/',$this->cookie_domain); 
+               }
+
+               /**
+               * Create a new session
+               *
+               * @param string $login user login
+               * @param string $passwd user password
+               * @param string $passwd_type type of password being used, ie 
plaintext, md5, sha1
+               * @return string session id
+               */
+               function create($login,$passwd = '',$passwd_type = '')
+               {
+                       if (is_array($login))
+                       {
+                               $this->login       = $login['login'];
+                               $this->passwd      = $login['passwd'];
+                               $this->passwd_type = $login['passwd_type'];
+                               $login             = $this->login;
+                       }
+                       else
+                       {
+                               $this->login       = $login;
+                               $this->passwd      = $passwd;
+                               $this->passwd_type = $passwd_type;
+                       }
+
+                       $this->clean_sessions();
+                       $now = time();
+
+                       if (strstr($login,'@') === False)
+                       {
+                               $this->account_domain = 
$GLOBALS['phpgw_info']['server']['default_domain'];
+                               $this->account_lid = $login;
+                       }
+                       else
+                       {
+                               list($this->account_lid, $this->account_domain) 
= explode('@', $login);
+                       }
+
+                       //echo "<p>session::create(login='$login'): 
lid='$this->account_lid', domain='$this->account_domain'</p>\n";
+                       $user_ip = $this->getuser_ip();
+
+                       $blocked = false;
+                       if ( ($blocked = $this->login_blocked($login, 
$_SERVER['REMOTE_ADDR'])) // too many unsuccessful attempts
+                               || ( 
isset($GLOBALS['phpgw_info']['server']['global_denied_users'][$this->account_lid])
 && $GLOBALS['phpgw_info']['server']['global_denied_users'][$this->account_lid] 
)
+                               || 
!$GLOBALS['phpgw']->auth->authenticate($this->account_lid, $this->passwd, 
$this->passwd_type) 
+                               || 
$GLOBALS['phpgw']->accounts->get_type($this->account_lid) == 'g')
+                       {
+                               $this->reason = $blocked ? 'blocked, too many 
attempts' : 'bad login or password';
+                               $this->cd_reason = $blocked ? 99 : 5;
+                               
+                               
$this->log_access($this->reason,$login,$user_ip,0);     // log unsuccessfull 
login
+                               return False;
+                       }
+
+                       if 
((!$GLOBALS['phpgw']->accounts->exists($this->account_lid)) && 
$GLOBALS['phpgw_info']['server']['auto_create_acct'] == True)
+                       {
+                               $this->account_id = 
$GLOBALS['phpgw']->accounts->auto_add($this->account_lid, $passwd);
+                       }
+                       else
+                       {
+                               $this->account_id = 
$GLOBALS['phpgw']->accounts->name2id($this->account_lid);
+                       }
+                       $GLOBALS['phpgw_info']['user']['account_id'] = 
$this->account_id;
+                       
$GLOBALS['phpgw']->accounts->accounts($this->account_id);
+                       $this->sessionid = 
md5($GLOBALS['phpgw']->common->randomstring(15));
+                       $this->kp3       = 
md5($GLOBALS['phpgw']->common->randomstring(15));
+
+                       if ( 
isset($GLOBALS['phpgw_info']['server']['usecookies'])
+                               && 
$GLOBALS['phpgw_info']['server']['usecookies'] )
+                       {
+                               
$this->phpgw_setcookie('sessionid',$this->sessionid);
+                               $this->phpgw_setcookie('kp3',$this->kp3);
+                               
$this->phpgw_setcookie('domain',$this->account_domain);
+                       }
+
+                       if ( ( 
isset($GLOBALS['phpgw_info']['server']['usecookies']) && 
$GLOBALS['phpgw_info']['server']['usecookies'] )
+                               || isset($_COOKIE['last_loginid']))
+                       { 
+                               $this->phpgw_setcookie('last_loginid', 
$this->account_lid ,$now+1209600); /* For 2 weeks */
+                               
$this->phpgw_setcookie('last_domain',$this->account_domain,$now+1209600);
+                       }
+                       
unset($GLOBALS['phpgw_info']['server']['default_domain']); /* we kill this for 
security reasons */
+
+                       /* init the crypto object */
+                       $this->key = md5($this->kp3 . $this->sessionid . 
$GLOBALS['phpgw_info']['server']['encryptkey']);
+                       $this->iv  = 
$GLOBALS['phpgw_info']['server']['mcrypt_iv'];
+                       
$GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
+
+                       $this->read_repositories(False);
+                       if ($this->user['expires'] != -1 && 
$this->user['expires'] < time())
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-LoginFailure, 
account loginid %1 is expired',
+                                               'p1'   => $this->account_lid,
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+
+                               return False;
+                       }
+
+                       $GLOBALS['phpgw_info']['user']  = $this->user;
+                       $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
+
+                       
$this->appsession('password','phpgwapi',base64_encode($this->passwd));
+                       if 
($GLOBALS['phpgw']->acl->check('anonymous',1,'phpgwapi'))
+                       {
+                               $session_flags = 'A';
+                       }
+                       else
+                       {
+                               $session_flags = 'N';
+                       }
+
+                       $GLOBALS['phpgw']->db->transaction_begin();
+                       
$this->register_session($login,$user_ip,$now,$session_flags);
+                       
$this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
+                       
$this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
+                       
$GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
+                       $GLOBALS['phpgw']->db->transaction_commit();
+                       
+                       //if (!$this->sessionid) echo 
"<p>session::create(login='$login') = '$this->sessionid': 
lid='$this->account_lid', domain='$this->account_domain'</p>\n";
+
+                       return $this->sessionid;
+               }
+
+               /**
+               * Write or update (for logout) the access_log
+               *
+               * @param string $sessionid id of session or 0 for unsuccessful 
logins
+               * @param string $login account_lid (evtl. with domain) or '' 
for settion the logout-time
+               * @param string $user_ip ip to log
+               * @param int $account_id numerical account_id
+               */
+               function 
log_access($sessionid,$login='',$user_ip='',$account_id='')
+               {
+                       $now = time();
+
+                       if ($login != '')
+                       {
+                               $GLOBALS['phpgw']->db->query('INSERT INTO 
phpgw_access_log(sessionid,loginid,ip,li,lo,account_id)'.
+                                       " VALUES ('" . $sessionid . "','" . 
$this->db->db_addslashes($login). "','" . 
+                                       $this->db->db_addslashes($user_ip) . 
"',$now,0,".intval($account_id).")",__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $GLOBALS['phpgw']->db->query("UPDATE 
phpgw_access_log SET lo=" . $now . " WHERE sessionid='" .
+                                       $sessionid . "'",__LINE__,__FILE__);
+                       }
+                       if 
($GLOBALS['phpgw_info']['server']['max_access_log_age'])
+                       {
+                               $max_age = $now - 
$GLOBALS['phpgw_info']['server']['max_access_log_age'] * 24 * 60 * 60;
+                               
+                               $GLOBALS['phpgw']->db->query("DELETE FROM 
phpgw_access_log WHERE li < $max_age");
+                       }
+               }
+
+               /**
+               * Protect against brute force attacks, block login if too many 
unsuccessful login attmepts
+               *
+               * @param string $login account_lid (evtl. with domain)
+               * @param string $ip the ip that made the request
+               * @returns bool login blocked?
+               */
+               function login_blocked($login, $ip)
+               {
+                       $blocked = false;
+                       $block_time = time() - 
$GLOBALS['phpgw_info']['server']['block_time'] * 60;
+                       
+                       $ip = $this->db->db_addslashes($ip);
+                       $this->db->query("SELECT count(*) FROM phpgw_access_log 
WHERE account_id=0 AND ip='$ip' AND li > $block_time",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       if (($false_ip = $this->db->f(0)) > 
$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip'])
+                       {
+                               //echo "<p>login_blocked: ip='$ip' 
".$this->db->f(0)." tries 
(".$GLOBALS['phpgw_info']['server']['num_unsuccessful_ip']." max.) since 
".date('Y/m/d H:i',$block_time)."</p>\n";
+                               $blocked = True;
+                       }
+                       $login = $this->db->db_addslashes($login);
+                       $this->db->query("SELECT count(*) FROM phpgw_access_log 
WHERE account_id=0 AND (loginid='$login' OR loginid LIKE 'address@hidden') AND 
li > $block_time",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       if (($false_id = $this->db->f(0)) > 
$GLOBALS['phpgw_info']['server']['num_unsuccessful_id'])
+                       {
+                               //echo "<p>login_blocked: login='$login' 
".$this->db->f(0)." tries 
(".$GLOBALS['phpgw_info']['server']['num_unsuccessful_id']." max.) since 
".date('Y/m/d H:i',$block_time)."</p>\n";
+                               $blocked = True;
+                       }
+                       if ($blocked && 
isset($GLOBALS['phpgw_info']['server']['admin_mails']) && 
$GLOBALS['phpgw_info']['server']['admin_mails']  &&
+                               // max. one mail each 5mins
+                               
$GLOBALS['phpgw_info']['server']['login_blocked_mail_time'] < time()-5*60)
+                       {
+                               // notify admin(s) via email
+                               $from    = 
'phpGroupWare@'.$GLOBALS['phpgw_info']['server']['mail_suffix'];
+                               $subject = lang("phpGroupWare: login blocked 
for user '%1', IP %2",$login,$ip);
+                               $body    = lang("Too many unsuccessful attempts 
to login: %1 for the user '%2', %3 for the IP %4", $false_id, $login, 
$false_ip, $ip);
+                               
+                               if(!is_object($GLOBALS['phpgw']->send))
+                               {
+                                       $GLOBALS['phpgw']->send = 
createObject('phpgwapi.send');
+                               }
+                               $subject = 
$GLOBALS['phpgw']->send->encode_subject($subject);
+                               $admin_mails = 
explode(',',$GLOBALS['phpgw_info']['server']['admin_mails']);
+                               foreach($admin_mails as $to)
+                               {
+                                       
$GLOBALS['phpgw']->send->msg('email',$to,$subject,$body,'','','',$from,$from);
+                               }
+                               // save time of mail, to not send to many mails
+                               $config = 
createObject('phpgwapi.config','phpgwapi');
+                               $config->read_repository();
+                               
$config->value('login_blocked_mail_time',time());
+                               $config->save_repository();
+                       }
+                       return $blocked;
+               }
+
+               /**
+               * Verfy a peer server access request
+               * 
+               * @param string $sessionid session id to verfiy
+               * @param string $kp3 ??
+               * @return bool verfied?
+               */
+               function verify_server($sessionid, $kp3)
+               {
+                       $GLOBALS['phpgw']->interserver = 
createObject('phpgwapi.interserver');
+                       $this->sessionid = $sessionid;
+                       $this->kp3       = $kp3;
+
+                       $session = $this->read_session($this->sessionid);
+                       $this->session_flags = $session['session_flags'];
+
+                       list($this->account_lid,$this->account_domain) = 
explode('@', $session['session_lid']);
+                       
+                       if ($this->account_domain == '')
+                       {
+                               $this->account_domain = 
$GLOBALS['phpgw_info']['server']['default_domain'];
+                       }
+
+                       $GLOBALS['phpgw_info']['user']['kp3'] = $this->kp3;
+                       $phpgw_info_flags = $GLOBALS['phpgw_info']['flags'];
+
+                       $GLOBALS['phpgw_info']['flags'] = $phpgw_info_flags;
+                       
+                       $this->update_dla();
+                       $this->account_id = 
$GLOBALS['phpgw']->interserver->name2id($this->account_lid);
+
+                       if (!$this->account_id)
+                       {
+                               return False;
+                       }
+
+                       $GLOBALS['phpgw_info']['user']['account_id'] = 
$this->account_id;
+                       
+                       
$this->read_repositories(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info']);
+
+                       /* init the crypto object before appsession call below 
*/
+                       $this->key = md5($this->kp3 . $this->sessionid . 
$GLOBALS['phpgw_info']['server']['encryptkey']);
+                       $this->iv  = 
$GLOBALS['phpgw_info']['server']['mcrypt_iv'];
+                       
$GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
+
+                       $GLOBALS['phpgw_info']['user']  = $this->user;
+                       $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
+
+                       $GLOBALS['phpgw_info']['user']['session_ip'] = 
$session['session_ip'];
+                       $GLOBALS['phpgw_info']['user']['passwd'] = 
base64_decode($this->appsession('password','phpgwapi'));
+
+                       if ($userid_array[1] != 
$GLOBALS['phpgw_info']['user']['domain'])
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-VerifySession, the 
domains %1 and %2 don\t match',
+                                               'p1'   => $userid_array[1],
+                                               'p2'   => 
$GLOBALS['phpgw_info']['user']['domain'],
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+
+                               if(is_object($GLOBALS['phpgw']->crypto))
+                               {
+                                       $GLOBALS['phpgw']->crypto->cleanup();
+                                       unset($GLOBALS['phpgw']->crypto);
+                               }
+                               return False;
+                       }
+
+                       if 
(@$GLOBALS['phpgw_info']['server']['sessions_checkip'])
+                       {
+                               if (PHP_OS != 'Windows' && (! 
$GLOBALS['phpgw_info']['user']['session_ip'] || 
$GLOBALS['phpgw_info']['user']['session_ip'] != $this->getuser_ip()))
+                               {
+                                       if(is_object($GLOBALS['phpgw']->log))
+                                       {
+                                               // This needs some better 
wording
+                                               
$GLOBALS['phpgw']->log->message(array(
+                                                       'text' => 
'W-VerifySession, IP %1 doesn\'t match IP %2 in session table',
+                                                       'p1'   => 
$this->getuser_ip(),
+                                                       'p2'   => 
$GLOBALS['phpgw_info']['user']['session_ip'],
+                                                       'line' => __LINE__,
+                                                       'file' => __FILE__
+                                               ));
+                                               
$GLOBALS['phpgw']->log->commit();
+                                       }
+
+                                       if(is_object($GLOBALS['phpgw']->crypto))
+                                       {
+                                               
$GLOBALS['phpgw']->crypto->cleanup();
+                                               
unset($GLOBALS['phpgw']->crypto);
+                                       }
+                                       return False;
+                               }
+                       }
+
+                       $GLOBALS['phpgw']->acl->acl($this->account_id);
+                       
$GLOBALS['phpgw']->accounts->accounts($this->account_id);
+                       
$GLOBALS['phpgw']->preferences->preferences($this->account_id);
+                       
$GLOBALS['phpgw']->applications->applications($this->account_id);
+
+                       if (! $this->account_lid)
+                       {
+                               if(is_object($GLOBALS['phpgw']->log))
+                               {
+                                       // This needs some better wording
+                                       $GLOBALS['phpgw']->log->message(array(
+                                               'text' => 'W-VerifySession, 
account_id is empty',
+                                               'line' => __LINE__,
+                                               'file' => __FILE__
+                                       ));
+                                       $GLOBALS['phpgw']->log->commit();
+                               }
+
+                               if(is_object($GLOBALS['phpgw']->crypto))
+                               {
+                                       $GLOBALS['phpgw']->crypto->cleanup();
+                                       unset($GLOBALS['phpgw']->crypto);
+                               }
+                               return False;
+                       }
+                       else
+                       {
+                               return True;
+                       }
+               }
+
+               /**
+               * Validate a peer server login request
+               *
+               * @param string $login login name
+               * @param string $password password
+               * @return bool login ok?
+               */
+               function create_server($login,$passwd)
+               {
+                       $GLOBALS['phpgw']->interserver = 
createObject('phpgwapi.interserver');
+                       $this->login  = $login;
+                       $this->passwd = $passwd;
+                       $this->clean_sessions();
+                       $login_array = explode('@', $login);
+                       $this->account_lid = $login_array[0];
+                       $now = time();
+
+                       if ($login_array[1] != '')
+                       {
+                               $this->account_domain = $login_array[1];
+                       }
+                       else
+                       {
+                               $this->account_domain = 
$GLOBALS['phpgw_info']['server']['default_domain'];
+                       }
+
+                       $serverdata = array(
+                               'server_name' => $this->account_domain,
+                               'username'    => $this->account_lid,
+                               'password'    => $passwd
+                       );
+                       if (!$GLOBALS['phpgw']->interserver->auth($serverdata))
+                       {
+                               return False;
+                               exit;
+                       }
+
+                       if 
(!$GLOBALS['phpgw']->interserver->exists($this->account_lid))
+                       {
+                               $this->account_id = 
$GLOBALS['phpgw']->interserver->name2id($this->account_lid);
+                       }
+                       $GLOBALS['phpgw_info']['user']['account_id'] = 
$this->account_id;
+                       $GLOBALS['phpgw']->interserver->serverid = 
$this->account_id;
+
+                       $this->sessionid = 
md5($GLOBALS['phpgw']->common->randomstring(10));
+                       $this->kp3       = 
md5($GLOBALS['phpgw']->common->randomstring(15));
+
+                       /* re-init the crypto object */
+                       $this->key = md5($this->kp3 . $this->sessionid . 
$GLOBALS['phpgw_info']['server']['encryptkey']);
+                       $this->iv  = 
$GLOBALS['phpgw_info']['server']['mcrypt_iv'];
+                       
$GLOBALS['phpgw']->crypto->init(array($this->key,$this->iv));
+
+                       //$this->read_repositories(False);
+
+                       $GLOBALS['phpgw_info']['user']  = $this->user;
+                       $GLOBALS['phpgw_info']['hooks'] = $this->hooks;
+
+                       
$this->appsession('password','phpgwapi',base64_encode($this->passwd));
+                       $session_flags = 'S';
+
+                       $user_ip = $this->getuser_ip();
+
+                       $GLOBALS['phpgw']->db->transaction_begin();
+                       
$this->register_session($login,$user_ip,$now,$session_flags);
+
+                       
$this->log_access($this->sessionid,$login,$user_ip,$this->account_id);
+
+                       
$this->appsession('account_previous_login','phpgwapi',$GLOBALS['phpgw']->auth->previous_login);
+                       
$GLOBALS['phpgw']->auth->update_lastlogin($this->account_id,$user_ip);
+                       $GLOBALS['phpgw']->db->transaction_commit();
+
+                       return array($this->sessionid,$this->kp3);
+               }
+
+               /**
+               * Functions for appsession data and session cache
+               */
+               
+               /**
+               * Is this also useless?? (skwashd)
+               */
+               function read_repositories($cached='',$write_cache=True)
+               {
+                       $GLOBALS['phpgw']->acl->acl($this->account_id);
+                       
$GLOBALS['phpgw']->accounts->accounts($this->account_id);
+                       
$GLOBALS['phpgw']->preferences->preferences($this->account_id);
+                       
$GLOBALS['phpgw']->applications->applications($this->account_id);
+                       
+                       if(@$cached)
+                       {
+                               $this->user = 
$this->appsession('phpgw_info_cache','phpgwapi');
+                               if(!empty($this->user))
+                               {
+                                       $GLOBALS['phpgw']->preferences->data = 
$this->user['preferences'];
+                                       if 
(!isset($GLOBALS['phpgw_info']['apps']) || 
!is_array($GLOBALS['phpgw_info']['apps']))
+                                       {
+                                               
$GLOBALS['phpgw']->applications->read_installed_apps();
+                                       }
+                               }
+                               else
+                               {
+                                       $this->setup_cache($write_cache);
+                               }
+                       }
+                       else
+                       {
+                               $this->setup_cache($write_cache);
+                       }
+                       $this->hooks = $GLOBALS['phpgw']->hooks->read();
+               }
+
+               /**
+               * Is this also useless?? (skwashd)
+               */
+               function setup_cache($write_cache=True)
+               {
+                       $this->user                = 
$GLOBALS['phpgw']->accounts->read_repository();
+                       $this->user['acl']         = 
$GLOBALS['phpgw']->acl->read_repository();
+                       $this->user['preferences'] = 
$GLOBALS['phpgw']->preferences->read_repository();
+                       $this->user['apps']        = 
$GLOBALS['phpgw']->applications->read_repository();
+                       //@reset($this->data['user']['apps']);
+
+                       $this->user['domain']      = $this->account_domain;
+                       $this->user['sessionid']   = $this->sessionid;
+                       $this->user['kp3']         = $this->kp3;
+                       $this->user['session_ip']  = $this->getuser_ip();
+                       $this->user['session_lid'] = 
$this->account_lid.'@'.$this->account_domain;
+                       $this->user['account_id']  = $this->account_id;
+                       $this->user['account_lid'] = $this->account_lid;
+                       $this->user['userid']      = $this->account_lid;
+                       $this->user['passwd']      = @$this->passwd;
+                       
if(@$GLOBALS['phpgw_info']['server']['cache_phpgw_info'] && $write_cache)
+                       {
+                               $this->delete_cache();
+                               
$this->appsession('phpgw_info_cache','phpgwapi',$this->user);
+                       }
+               }
+
+               
+               /**
+               * This looks to be useless
+               * This will capture everything in the $GLOBALS['phpgw_info'] 
including server info,
+               * and store it in appsessions.  This is really incompatible 
with any type of restoring
+               * from appsession as the saved user info is really in ['user'] 
rather than the root of
+               * the structure, which is what this class likes.
+               */
+               function save_repositories()
+               {
+                       $phpgw_info_temp = $GLOBALS['phpgw_info'];
+                       $phpgw_info_temp['user']['kp3'] = '';
+                       $phpgw_info_temp['flags'] = array();
+                       
+                       if 
($GLOBALS['phpgw_info']['server']['cache_phpgw_info'])
+                       {
+                               
$this->appsession('phpgw_info_cache','phpgwapi',$phpgw_info_temp);
+                       }
+               }
+       
+               function restore()
+               {
+                       $sessionData = $this->appsession('sessiondata');
+                       
+                       if (!empty($sessionData) && is_array($sessionData))
+                       {
+                               foreach($sessionData as $key => $value)
+                               {
+                                       global $$key;
+                                       $$key = $value;
+                                       $this->variableNames[$key] = 
'registered';
+                                       // echo 'restored: '.$key.', ' . $value 
. '<br>';
+                               }
+                       }
+               }
+
+               /**
+               * Save the current values of all registered variables
+               */
+               function save()
+               {
+                       if (is_array($this->variableNames))
+                       {
+                               reset($this->variableNames);
+                               while(list($key, $value) = 
each($this->variableNames))
+                               {
+                                       if ($value == 'registered')
+                                       {
+                                               global $$key;
+                                               $sessionData[$key] = $$key;
+                                       }
+                               }
+                               
$this->appsession('sessiondata','',$sessionData);
+                       }
+               }
+
+               /**
+               * Create a list a variable names, which data needs to be 
restored
+               *
+               * @param string $_variableName name of variable to be registered
+               */
+               function register($_variableName)
+               {
+                       $this->variableNames[$_variableName]='registered';
+                       #print 'registered '.$_variableName.'<br>';
+               }
+
+               /**
+               * Mark variable as unregistered
+               *
+               * @param string $_variableName name of variable to deregister
+               */
+               function unregister($_variableName)
+               {
+                       $this->variableNames[$_variableName]='unregistered';
+                       #print 'unregistered '.$_variableName.'<br>';
+               }
+
+               /**
+               * Check if we have a variable registred already
+               *
+               * @param string $_variableName name of variable to check
+               * @return bool was the variable found?
+               */
+               function is_registered($_variableName)
+               {
+                       if ($this->variableNames[$_variableName] == 
'registered')
+                       {
+                               return True;
+                       }
+                       else
+                       {
+                               return False;
+                       }
+               }
+               
+               /**
+               * Additional tracking of user actions - prevents reposts/use of 
back button
+               *
+               * @author skwashd
+               * @return string current history id
+               */
+               function generate_click_history()
+               {
+                       if(!isset($this->history_id))
+                       {
+                               $this->history_id = md5($this->login . time());
+                               $history = $this->appsession('history', 
'phpgwapi');
+                               
+                               if(count($history) >= 
$GLOBALS['phpgw_info']['server']['max_history'])
+                               {
+                                       array_shift($history);
+                                       $this->appsession('history', 
'phpgwapi', $history);
+                               }
+                       }
+                       return $this->history_id;
+               }
+               
+               /**
+               * Detects if the page has already been called before - good for 
forms
+               *
+               * @author skwashd
+               * @param bool $diplay_error when implemented will use the 
generic error handling code
+               * @return True if called previously, else False - call ok
+               */
+               function is_repost($display_error = False)
+               {
+                       $history = $this->appsession($location = 'history', 
$appname = 'phpgwapi');
+                       if(isset($history[$_GET['click_history']]))
+                       {
+                               if($display_error)
+                               {
+                                       
$GLOBALS['phpgw']->redirect_link('/error.php', 'type=repost');//more on this 
later :)
+                               }
+                               else
+                               {
+                                       return True; //handled by the app
+                               }
+                       }
+                       else
+                       {
+                               $history[$_GET['click_history']] = True;
+                               $this->appsession($location = 'history', 
$appname = 'phpgwapi', $history);
+                               return False;
+                       }
+               }
+
+               /**
+               * Generate a url which supports url or cookies based sessions
+               *
+               * @param string $url a url relative to the phpgroupware install 
root
+               * @param array $extravars query string arguements
+               * @param bool $redirect is this for a redirect link ?
+               * @return string generated url
+               */
+               function link($url, $extravars = array(), $redirect=false)
+               {
+                       $term = '&amp;'; //W3C Compliant in markup
+                       if ( $redirect )
+                       {
+                               $term = '&'; // RFC Compliant for 
Header('Location: ...
+                       }
+                       
+                       /* first we process the $url to build the full 
scriptname */
+                       $full_scriptname = True;
+
+                       $url_firstchar = substr($url ,0,1);
+                       if ($url_firstchar == '/' && 
$GLOBALS['phpgw_info']['server']['webserver_url'] == '/')
+                       {
+                               $full_scriptname = False;
+                       }
+
+                       if ($url_firstchar != '/')
+                       {
+                               $app = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                               if ($app != 'home' && $app != 'login' && $app 
!= 'logout')
+                               {
+                                       $url = $app.'/'.$url;
+                               }
+                       }
+                       
+                       if($full_scriptname)
+                       {
+                               $webserver_url_count = 
strlen($GLOBALS['phpgw_info']['server']['webserver_url'])-1;
+                               
if(substr($GLOBALS['phpgw_info']['server']['webserver_url'] 
,$webserver_url_count,1) != '/' && $url_firstchar != '/')
+                               {
+                                       $url = 
$GLOBALS['phpgw_info']['server']['webserver_url'] .'/'. $url;
+                               }
+                               else
+                               {
+                                       $url = 
$GLOBALS['phpgw_info']['server']['webserver_url'] . $url;
+                               }
+                       }
+
+                       
if(isset($GLOBALS['phpgw_info']['server']['enforce_ssl']) && 
$GLOBALS['phpgw_info']['server']['enforce_ssl'])
+                       {
+                               if(substr($url ,0,4) != 'http')
+                               {
+                                       $url = 
'https://'.$GLOBALS['phpgw_info']['server']['hostname'].$url;
+                               }
+                               else
+                               {
+                                       $url = str_replace ( 'http:', 'https:', 
$url);
+                               }
+                       }
+
+                       /*
+                               If an app sends the extrvars as a string we 
covert the extrvars into an array for proper processing
+                               This also helps prevent any duplicate values in 
the query string.
+                       */
+                       if (!is_array($extravars) && $extravars != '')
+                       {
+                               echo 'String used for extravar in 
sessions::link(url, extravar) call, please use an array!<br><pre>' . 
print_r(debug_backtrace(), true) . '</pre>';
+                               $vars = explode('&', $extravars);
+                               foreach( $vars as $v )
+                               {
+                                       $b = split('=', $v);
+                                       $new_extravars[$b[0]] = $b[1];
+                               }
+                               unset($extravars);
+                               $extravars = $new_extravars;
+                               unset($new_extravars);
+                       }
+
+                       /* if using frames we make sure there is a framepart */
+                       if(defined('PHPGW_USE_FRAMES') && PHPGW_USE_FRAMES)
+                       {
+                               if (!isset($extravars['framepart']))
+                               {
+                                       $extravars['framepart'] = 'body';
+                               }
+                       }
+                       
+                       /* add session params if not using cookies */
+                       if ( 
!isset($GLOBALS['phpgw_info']['server']['usecookies']) 
+                               || 
!$GLOBALS['phpgw_info']['server']['usecookies'])
+                       {
+                               $extravars = array_merge($extravars, 
$this->_get_session_vars());
+                       }
+                       
+                       //used for repost prevention
+                       $extravars['click_history'] = 
$this->generate_click_history();
+
+                       if (is_array($extravars)) //we have something to append
+                       {
+                               return "{$url}?" . http_build_query($extravars, 
null, $term);
+                       }
+                       return $url;
+               }
+               
+               /**
+               * The remaining methods are abstract - as they are unique for 
each session handler
+               */
+
+               
+               /**
+               * Load user's session information
+               *
+               * @param string $sessionid user's session id string
+               * @return mixed the session data
+               */
+               function read_session($sessionid)
+               {}
+
+               /**
+               * Remove stale sessions out of the database
+               */
+               function clean_sessions()
+               {}
+
+               /**
+               * Set paramaters for cookies - only implemented in PHP4 sessions
+               *
+               * @param string $domain domain name to use in cookie
+               */
+               function set_cookie_params($domain)
+               {}
+
+               /**
+               * Create a new session
+               *
+               * @param string $login user login
+               * @param string $user_ip users ip address
+               * @param int $now time now as a unix timestamp
+               * @param string $session_flags A = Anonymous, N = Normal
+               */
+               function register_session($login,$user_ip,$now,$session_flags)
+               {}
+
+               /**
+               * Update the date last active info for the session, so the 
login does not expire
+               *
+               * @return bool did it suceed?
+               */
+               function update_dla()
+               {}
+
+               /**
+               * Terminate a session
+               *
+               * @param string $sessionid the id of the session to be 
terminated
+               * @param string $kp3 - NOT SURE
+               * @return bool did it suceed?
+               */
+               function destroy($sessionid, $kp3)
+               {}
+
+               /**
+               * Functions for appsession data and session cache
+               */
+
+               /**
+               * Delete all data from the session cache for a user
+               * 
+               * @param int $accountid user account id, defaults to current 
user (optional)
+               */
+               function delete_cache($accountid='')
+               {}
+
+               /**
+               * Stores or retrieves information from the sessions cache
+               * 
+               * @param string $location identifier for data
+               * @param string $appname name of app which is responsbile for 
the data
+               * @param mixed $data data to be stored, if left blank data is 
retreived (optional)
+               * @return mixed data from cache, only returned if $data arg is 
not used 
+               */
+               function appsession($location = 'default', $appname = '', $data 
= '##NOTHING##')
+               {}
+
+               /**
+               * Get list of normal / non-anonymous sessions
+               * Note: The data from the session-files get cached in the 
+               *       app_session phpgwapi/php4_session_cache
+               *
+               * @author ralfbecker
+               * @param int $start session to start at
+               * @param string $order field to sort on
+               * @param string $sort sort order
+               * @param bool $all_no_sort list all with out sorting (optional) 
default False
+               * @return array info for all current sessions  
+               */
+               function list_sessions($start,$order,$sort,$all_no_sort = False)
+               {}
+               
+               /**
+               * Get the number of normal / non-anonymous sessions
+               * 
+               * @author ralfbecker
+               * @return int number of sessions
+               */
+               function total()
+               {
+                       return 0;
+               }
+
+               /**
+               * Get the list of session variables used for non cookie based 
sessions
+               *
+               * @access private
+               * @return array the variables which are specific to this 
session type
+               */
+               function _get_session_vars()
+               {
+                       return array();
+               }
+       }

Index: class.sessions_db.inc.php
===================================================================
RCS file: class.sessions_db.inc.php
diff -N class.sessions_db.inc.php
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ class.sessions_db.inc.php   9 Sep 2006 11:35:41 -0000       1.1
@@ -0,0 +1,231 @@
+<?php
+       /**
+       * Session management - Database storage
+       * @author NetUSE AG Boris Erdmann, Kristian Koehntopp
+       * @author Dan Kuykendall <address@hidden>
+       * @author Joseph Engo <address@hidden>
+       * @author Ralf Becker <address@hidden>
+       * @copyright Copyright (C) 1998-2000 NetUSE AG Boris Erdmann, Kristian 
Koehntopp
+       * @copyright Portions Copyright (C) 2000-2004 Free Software Foundation, 
Inc. http://www.fsf.org/
+       * @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General 
Public License
+       * @package phpgwapi
+       * @subpackage accounts
+       * @version $Id: class.sessions_db.inc.php,v 1.1 2006/09/09 11:35:41 
skwashd Exp $
+       * @link http://www.sanisoft.com/phplib/manual/session.php
+       */
+
+       /**
+       * Session management based on database
+       * 
+       * @package phpgwapi
+       * @subpackage sessions
+       */
+       class sessions_db extends sessions
+       {
+               function sessions_db()
+               {
+                       parent::sessions();
+               }
+               
+               function read_session($sessionid)
+               {
+                       $this->db->query("SELECT * FROM phpgw_sessions WHERE 
session_id='" . $this->sessionid . "'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       
+                       return $this->db->resultSet->fetchRow();
+               }
+
+               // This will remove stale sessions out of the database
+               function clean_sessions()
+               {
+                       // If you plan on using the cron apps, please remove 
the following lines.
+                       // I am going to make this a config option durring 
0.9.11, instead of an application (jengo)
+
+                       $GLOBALS['phpgw']->db->query("DELETE FROM 
phpgw_sessions WHERE session_dla <= '" . (time() - 
$GLOBALS['phpgw_info']['server']['sessions_timeout'])
+                               . "' AND session_flags 
!='A'",__LINE__,__FILE__);
+
+                       // This is set a little higher, we don't want to kill 
session data for anonymous sessions.
+                       $GLOBALS['phpgw']->db->query("DELETE FROM 
phpgw_app_sessions WHERE session_dla <= '" . (time() - 
$GLOBALS['phpgw_info']['server']['sessions_timeout'])
+                               . "'",__LINE__,__FILE__);
+               }
+
+               function register_session($login,$user_ip,$now,$session_flags)
+               {
+                       $GLOBALS['phpgw']->db->query("INSERT INTO 
phpgw_sessions VALUES ('" . $this->sessionid
+                               . "','".$login."','" . $user_ip . "','"
+                               . $now . "','" . $now . "','" . 
$_SERVER['PHP_SELF'] . "','" . $session_flags
+                               . "')",__LINE__,__FILE__);
+               }
+
+               // This will update the DateLastActive column, so the login 
does not expire
+               function update_dla()
+               {
+                       $menu_action = 
get_var('menuaction',Array('GET','POST'));
+                       $action = $menu_action ? $menu_action : 
$_SERVER['PHP_SELF'];  
+
+                       // This way XML-RPC users aren't always listed as
+                       // xmlrpc.php
+                       if ($this->xmlrpc_method_called)
+                       {
+                               $action = $this->xmlrpc_method_called;
+                       }
+
+                       $GLOBALS['phpgw']->db->query("UPDATE phpgw_sessions SET 
session_dla='" . time() . "', session_action='$action' "
+                               . "WHERE session_id='" . 
$this->sessionid."'",__LINE__,__FILE__);
+
+                       $GLOBALS['phpgw']->db->query("UPDATE phpgw_app_sessions 
SET session_dla='" . time() . "' "
+                               . "WHERE sessionid='" . 
$this->sessionid."'",__LINE__,__FILE__);
+                       return True;
+               }
+
+               function destroy($sessionid, $kp3)
+               {
+                       if (! $sessionid && $kp3)
+                       {
+                               return False;
+                       }
+
+                       $GLOBALS['phpgw']->db->transaction_begin();
+                       $GLOBALS['phpgw']->db->query("DELETE FROM 
phpgw_sessions WHERE session_id='"
+                               . $sessionid . "'",__LINE__,__FILE__);
+                       $GLOBALS['phpgw']->db->query("DELETE FROM 
phpgw_app_sessions WHERE sessionid='"
+                               . $sessionid . "'",__LINE__,__FILE__);
+                       $this->log_access($this->sessionid);    // log 
logout-time
+
+                       // Only do the following, if where working with the 
current user
+                       if ($sessionid == 
$GLOBALS['phpgw_info']['user']['sessionid'])
+                       {
+                               $this->clean_sessions();
+                       }
+                       $GLOBALS['phpgw']->db->transaction_commit();
+
+                       return True;
+               }
+
+               
/*************************************************************************\
+               * Functions for appsession data and session cache               
          *
+               
\*************************************************************************/
+
+               function delete_cache($accountid='')
+               {
+                       $account_id = 
get_account_id($accountid,$this->account_id);
+
+                       $query = "DELETE FROM phpgw_app_sessions WHERE loginid 
= '".$account_id."'"
+                               ." AND app = 'phpgwapi' AND location = 
'phpgw_info_cache'";
+
+                       $GLOBALS['phpgw']->db->query($query);
+               }
+
+               function appsession($location = 'default', $appname = '', $data 
= '##NOTHING##')
+               {
+                       if (! $appname)
+                       {
+                               $appname = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       }
+                       
+                       /* This allows the user to put '' as the value. */
+                       if ($data == '##NOTHING##')
+                       {
+                               $query = "SELECT content FROM 
phpgw_app_sessions WHERE"
+                                       ." sessionid='".$this->sessionid."' AND 
loginid='".$this->account_id."'"
+                                       ." AND app = '".$appname."' AND 
location='".$location."'";
+       
+                               
$GLOBALS['phpgw']->db->query($query,__LINE__,__FILE__);
+                               $GLOBALS['phpgw']->db->next_record();
+
+                               // I added these into seperate steps for easier 
debugging
+                               $data = $GLOBALS['phpgw']->db->f('content');
+                               // Changed by Skeeter 2001 Mar 04 0400Z
+                               // This was not properly decoding structures 
saved into session data properly
+//                             $data = 
$GLOBALS['phpgw']->common->decrypt($data);
+//                             return stripslashes($data);
+                               // Changed by milosch 2001 Dec 20
+                               // do not stripslashes here unless this proves 
to be a problem.
+                               // Changed by milosch 2001 Dec 25
+                               /* do not decrypt and return if no data 
(decrypt returning garbage) */
+                               if($data)
+                               {
+                                       $data = 
$GLOBALS['phpgw']->crypto->decrypt($data);
+//                                     echo 'appsession returning: '; 
_debug_array($data);
+                                       return $data;
+                               }
+                       }
+                       else
+                       {
+                               $GLOBALS['phpgw']->db->query("SELECT content 
FROM phpgw_app_sessions WHERE "
+                                       . "sessionid = '".$this->sessionid."' 
AND loginid = '".$this->account_id."'"
+                                       . " AND app = '".$appname."' AND 
location = '".$location."'",__LINE__,__FILE__);
+
+                               $encrypteddata = 
$GLOBALS['phpgw']->crypto->encrypt($data);
+                               $encrypteddata = 
$GLOBALS['phpgw']->db->db_addslashes($encrypteddata);
+
+                               if ($GLOBALS['phpgw']->db->num_rows()==0)
+                               {
+                                       $GLOBALS['phpgw']->db->query("INSERT 
INTO phpgw_app_sessions (sessionid,loginid,app,location,content,session_dla) "
+                                               . "VALUES 
('".$this->sessionid."','".$this->account_id."','".$appname
+                                               . 
"','".$location."','".$encrypteddata."','" . time() . "')",__LINE__,__FILE__);
+                               }
+                               else
+                               {
+                                       $GLOBALS['phpgw']->db->query("UPDATE 
phpgw_app_sessions SET content='".$encrypteddata."'"
+                                               . "WHERE sessionid = 
'".$this->sessionid."'"
+                                               . "AND loginid = 
'".$this->account_id."' AND app = '".$appname."'"
+                                               . "AND location = 
'".$location."'",__LINE__,__FILE__);
+                               }
+                               return $data;
+                       }
+               }
+
+               function list_sessions($start, $order = 'ASC', $sort = 
'session_lid', $all_no_sort = false)
+               {
+                       $SQL_sort  =  strlen($sort) ? 'ORDER BY '.$sort : '';
+                       $SQL_sort .= (strlen($sort) && strlen($order)) ? ' 
'.$order : '' ;
+                       
+                       $this->db->limit_query('SELECT * FROM phpgw_sessions'
+                                       . " WHERE session_flags != 'A' 
$SQL_sort", $start, __LINE__, __FILE__);
+
+                       $values = array();
+                               
+                       while ($this->db->next_record())
+                       {
+                               $values[] = array(
+                                       'session_id'        => 
$this->db->f('session_id'),
+                                       'session_lid'       => 
$this->db->f('session_lid'),
+                                       'session_ip'        => 
$this->db->f('session_ip'),
+                                       'session_logintime' => 
$this->db->f('session_logintime'),
+                                       'session_action'    => 
$this->db->f('session_action'),
+                                       'session_dla'       => 
$this->db->f('session_dla')
+                               );
+                       }
+                       return $values;
+               }
+               
+               /**
+                * get number of normal / non-anonymous sessions
+               *
+                */
+               function total()
+               {
+                       $this->db->query("select count(*) from phpgw_sessions 
where session_flags != 'A'",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       return $this->db->f(0);
+               }
+
+               /**
+               * Get the list of session variables used for non cookie based 
sessions
+               *
+               * @access private
+               * @return array the variables which are specific to this 
session type
+               */
+               function _get_session_vars()
+               {
+                       return array
+                       (
+                               'sessionid'     => $this->sessionid,
+                               'kp3'           => $this->kp3,
+                               'domain'        => $this->account_domain
+                       );
+               }
+       }
+?>

Index: class.sessions_php.inc.php
===================================================================
RCS file: class.sessions_php.inc.php
diff -N class.sessions_php.inc.php
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ class.sessions_php.inc.php  9 Sep 2006 11:35:41 -0000       1.1
@@ -0,0 +1,310 @@
+<?php
+       /**
+       * Session management - Native php handler
+       * @author Dan Kuykendall <address@hidden>
+       * @author Joseph Engo <address@hidden>
+       * @author Ralf Becker <address@hidden>
+       * @copyright Copyright (C) 2000-2004 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General 
Public License
+       * @package phpgwapi
+       * @subpackage sessions
+       * @version $Id: class.sessions_php.inc.php,v 1.1 2006/09/09 11:35:41 
skwashd Exp $
+       */
+
+       /**
+       * Track the name of the php session variable used in GPC for non cookie 
based sessions
+       */
+       define('PHPGW_PHPSESSID', ini_get('session.name'));
+
+       /**
+       * Session management - Native php handler
+       * 
+       * @package phpgwapi
+       * @subpackage sessions
+       */
+       class sessions_php extends sessions
+       {
+               function sessions_php()
+               {
+                       //Call abstract class constructor
+                       parent::sessions();
+                       
+                       //respect the config option for cookies
+                       
ini_set('session.use_cookies',address@hidden'phpgw_info']['server']['usecookies']);
+                       //don't rewrite URL, as we have to do it in link - why? 
cos it is buggy otherwise
+                       ini_set('url_rewriter.tags', '');
+                       
+                       //controls the time out for php sessions
+                       ini_set('session.gc_maxlifetime', 
$GLOBALS['phpgw_info']['server']['sessions_timeout']);
+               }
+               
+               function read_session($sessionid)
+               {
+                       if($sessionid)
+                       {
+                               session_id($sessionid);
+                       }
+                       session_start();
+                       if ( isset($_SESSION['phpgw_session']) && 
is_array($_SESSION['phpgw_session']) )
+                       {
+                               $GLOBALS['phpgw_session'] = 
$_SESSION['phpgw_session'];
+                               return $GLOBALS['phpgw_session'];
+                       }
+                       return array();
+               }
+
+               function set_cookie_params($domain)
+               {
+                       session_set_cookie_params(0,'/',$domain);
+               }
+
+               function register_session($login,$user_ip,$now,$session_flags)
+               {
+                       if(isset($this->sessionid))
+                       {
+                               session_id($this->sessionid);
+                       }
+
+                       if ( !strlen(session_id() ) )
+                       {
+                               session_start();
+                       }
+
+                       $GLOBALS['phpgw_session']['session_id'] = 
$this->sessionid;
+                       $GLOBALS['phpgw_session']['session_lid'] = $login;
+                       $GLOBALS['phpgw_session']['session_ip'] = $user_ip;
+                       $GLOBALS['phpgw_session']['session_logintime'] = $now;
+                       $GLOBALS['phpgw_session']['session_dla'] = $now;
+                       $GLOBALS['phpgw_session']['session_action'] = 
$_SERVER['PHP_SELF'];
+                       $GLOBALS['phpgw_session']['session_flags'] = 
$session_flags;
+                       // we need the install-id to differ between serveral 
installs shareing one tmp-dir
+                       $GLOBALS['phpgw_session']['session_install_id'] = 
$GLOBALS['phpgw_info']['server']['install_id'];
+
+                       session_register('phpgw_session');
+                       $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
+               }
+
+               // This will update the DateLastActive column, so the login 
does not expire
+               function update_dla()
+               {
+                       if (@isset($GLOBALS['phpgw_info']['menuaction']))
+                       {
+                               $action = $GLOBALS['phpgw_info']['menuaction'];
+                       }
+                       else
+                       {
+                               $action = $_SERVER['PHP_SELF'];
+                       }
+
+                       $GLOBALS['phpgw_session']['session_dla'] = time();
+                       $GLOBALS['phpgw_session']['session_action'] = $action;
+               
+                       session_register('phpgw_session');
+                       $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
+
+                       return True;
+               }
+
+               function destroy($sessionid, $kp3)
+               {
+                       if (! $sessionid && $kp3)
+                       {
+                               return False;
+                       }
+
+                       $this->log_access($this->sessionid);    // log 
logout-time
+
+                       // Only do the following, if where working with the 
current user
+                       if ($sessionid == 
$GLOBALS['phpgw_info']['user']['sessionid'])
+                       {
+                               $this->clean_sessions();
+                               session_unset();
+                               session_destroy();
+                               $this->phpgw_setcookie(session_name());
+                       }
+                       else
+                       {
+                               $sessions = $this->list_sessions(0,'','',True);
+                               
+                               if (isset($sessions[$sessionid]))
+                               {
+                                       //echo 
"<p>session_php4::destroy($session_id): 
unlink('".$sessions[$sessionid]['php_session_file'].")</p>\n";
+                                       
unlink($sessions[$sessionid]['php_session_file']);
+                               }
+                       }
+
+                       return True;
+               }
+
+               
/*************************************************************************\
+               * Functions for appsession data and session cache               
          *
+               
\*************************************************************************/
+               function delete_cache($accountid='')
+               {
+                       $account_id = 
get_account_id($accountid,$this->account_id);
+
+                       
$GLOBALS['phpgw_session']['phpgw_app_sessions']['phpgwapi']['phpgw_info_cache'] 
= '';
+       
+                       session_register('phpgw_session');
+                       $_SESSION['phpgw_session'] = $GLOBALS['phpgw_session'];
+               }
+
+               function appsession($location = 'default', $appname = '', $data 
= '##NOTHING##')
+               {
+                       if (! $appname)
+                       {
+                               $appname = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       }
+                       
+                       /* This allows the user to put '' as the value. */
+                       if ($data == '##NOTHING##')
+                       {
+                               
+                               if ( 
isset($GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'])
 )
+                               {
+                                       // I added these into seperate steps 
for easier debugging
+                                       $data = 
$GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'];
+
+                                       /* do not decrypt and return if no data 
(decrypt returning garbage) */
+                                       if($data)
+                                       {
+                                               $data = 
$GLOBALS['phpgw']->crypto->decrypt($data);
+                                               //echo "appsession returning: 
location='$location',app='$appname',data=$data"; _debug_array($data);
+                                               return $data;
+                                       }
+                               }
+                               return '';
+                       }
+                       else
+                       {
+                               $encrypteddata = 
$GLOBALS['phpgw']->crypto->encrypt($data);
+                               
$GLOBALS['phpgw_session']['phpgw_app_sessions'][$appname][$location]['content'] 
= $encrypteddata;
+                               session_register('phpgw_session');
+                               $_SESSION['phpgw_session'] = 
$GLOBALS['phpgw_session'];
+                               return $data;
+                       }
+               }
+
+               function session_sort($a,$b)
+               {
+                       $sign = 
strcasecmp($GLOBALS['phpgw']->session->sort_order,'ASC') ? 1 : -1;
+
+                       return 
strcasecmp($a[$GLOBALS['phpgw']->session->sort_by],
+                                                         
$b[$GLOBALS['phpgw']->session->sort_by]) * $sign;
+               }
+               
+               /**
+                * get list of normal / non-anonymous sessions
+               *
+                * The data form the session-files get cached in the 
app_session phpgwapi/php4_session_cache
+                * @author ralfbecker
+                */
+               function list_sessions($start,$order,$sort,$all_no_sort = False)
+               {
+                       //echo 
"<p>session_php4::list_sessions($start,'$order','$sort',$all)</p>\n";
+                       $session_cache = 
$this->appsession('php4_session_cache','phpgwapi');
+
+                       $values = array();
+                       $maxmatchs = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       $dir = @opendir($path = ini_get('session.save_path'));
+                       while ($dir && $file = readdir($dir))
+                       {
+                               if (substr($file,0,5) != 'sess_')
+                               {
+                                       continue;
+                               }
+                               if (isset($session_cache[$file]))       // use 
copy from cache
+                               {
+                                       $session = $session_cache[$file];
+
+                                       if ($session['session_flags'] == 'A' || 
!$session['session_id'] ||
+                                               $session['session_install_id'] 
!= $GLOBALS['phpgw_info']['server']['install_id'])
+                                       {
+                                               continue;       // no anonymous 
sessions or other domains or installations
+                                       }
+                                       if (!$all_no_sort)      // we need the 
up-to-date data --> unset and reread it
+                                       {
+                                               unset($session_cache[$file]);
+                                       }
+                               }
+                               if (!isset($session_cache[$file]))      // not 
in cache, read and cache it
+                               {
+                 $fd = fopen ($path . '/' . $file,'r');
+                 $fs = filesize ($path . '/' . $file);
+                 // handle filesize 0 because php recently warns if fread is 
used on 0byte files 
+                 if ($fs > 0) {
+                       $session = fread ($fd, filesize ($path . '/' . $file));
+                 } else {
+                       $session = '';
+                 }
+                 fclose ($fd);
+                                       if (substr($session,0,14) != 
'phpgw_session|')
+                                       {
+                                               continue;
+                                       }
+                                       $session = 
unserialize(substr($session,14));
+                                       unset($session['phpgw_app_sessions']);  
// not needed, saves memory
+                                       $session_cache[$file] = $session;
+                               }
+                               if ($session['session_flags'] == 'A' || 
!$session['session_id'] ||
+                                       $session['session_install_id'] != 
$GLOBALS['phpgw_info']['server']['install_id'])
+                               {
+                                       continue;       // no anonymous 
sessions or other domains or installations
+                               }
+                               //echo "file='$file'=<pre>"; print_r($session); 
echo "</pre>"; 
+                               
+                               $session['php_session_file'] = $path . '/' . 
$file;
+                               $values[$session['session_id']] = $session;
+                       }
+                       @closedir($dir);
+                       
+                       if (!$all_no_sort)
+                       {
+                               $GLOBALS['phpgw']->session->sort_by = $sort;
+                               $GLOBALS['phpgw']->session->sort_order = $order;
+                       
+                               
uasort($values,array('sessions','session_sort'));
+                               
+                               $i = 0;
+                               $start = intval($start);
+                               foreach($values as $id => $data)
+                               {
+                                       if ($i < $start || $i > 
$start+$maxmatchs)
+                                       {
+                                               unset($values[$id]);
+                                       }
+                                       ++$i;
+                               }
+                               reset($values);
+                       }
+                       
$this->appsession('php4_session_cache','phpgwapi',$session_cache);
+
+                       return $values;
+               }
+               
+               /**
+                * get number of normal / non-anonymous sessions
+               *
+                * @author ralfbecker
+                */
+               function total()
+               {
+                       return count($this->list_sessions(0,'','',True));
+               }
+
+               /**
+               * Get the list of session variables used for non cookie based 
sessions
+               *
+               * @access private
+               * @return array the variables which are specific to this 
session type
+               */
+               function _get_session_vars()
+               {
+                       return array
+                       (
+                               'sessionid'             => $this->sessionid,
+                               'kp3'                   => $this->kp3,
+                               'domain'                => $this->account_domain
+                       );
+               }
+       }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]