2002-01-02 15:32:22 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Session management *
|
2002-01-02 15:32:22 +01:00
|
|
|
* This file written by Dan Kuykendall <seek3r@phpgroupware.org> *
|
|
|
|
* and Joseph Engo <jengo@phpgroupware.org> *
|
2003-05-18 21:16:58 +02:00
|
|
|
* and Ralf Becker <ralfbecker@outdoor-training.de> *
|
2002-01-02 15:32:22 +01:00
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall *
|
|
|
|
* -------------------------------------------------------------------------*
|
2004-08-09 15:46:03 +02:00
|
|
|
* This library is part of the eGroupWare API *
|
2004-05-05 14:06:13 +02:00
|
|
|
* http://www.egroupware.org/api *
|
2002-01-02 15:32:22 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-08-28 16:31:11 +02:00
|
|
|
class sessions extends sessions_
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
2004-11-03 22:37:01 +01:00
|
|
|
function sessions($domain_names=null)
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
2004-11-03 22:37:01 +01:00
|
|
|
$this->sessions_($domain_names);
|
2003-10-11 15:26:01 +02:00
|
|
|
//controls the time out for php4 sessions - skwashd 18-May-2003
|
2003-05-18 21:16:58 +02:00
|
|
|
ini_set('session.gc_maxlifetime', $GLOBALS['phpgw_info']['server']['sessions_timeout']);
|
2003-10-25 13:37:36 +02:00
|
|
|
session_name('sessionid');
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
2003-10-25 13:37:36 +02:00
|
|
|
|
2003-10-21 12:46:33 +02:00
|
|
|
function read_session()
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
2003-12-29 22:10:46 +01:00
|
|
|
if (!$this->sessionid)
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2003-10-21 12:46:33 +02:00
|
|
|
session_id($this->sessionid);
|
2002-01-02 15:32:22 +01:00
|
|
|
session_start();
|
2005-02-18 21:28:06 +01:00
|
|
|
return $_SESSION['egw'];
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
|
2003-05-18 21:16:58 +02:00
|
|
|
function set_cookie_params($domain)
|
2002-09-12 04:27:58 +02:00
|
|
|
{
|
2003-05-18 21:16:58 +02:00
|
|
|
session_set_cookie_params(0,'/',$domain);
|
2002-09-12 04:27:58 +02:00
|
|
|
}
|
|
|
|
|
2003-10-21 12:46:33 +02:00
|
|
|
function new_session_id()
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
|
|
|
session_start();
|
|
|
|
|
2003-10-21 12:46:33 +02:00
|
|
|
return session_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
function register_session($login,$user_ip,$now,$session_flags)
|
|
|
|
{
|
|
|
|
// session_start() is now called in new_session_id() !!!
|
2005-02-18 21:28:06 +01:00
|
|
|
$_SESSION['egw']['session_id'] = $this->sessionid;
|
|
|
|
$_SESSION['egw']['session_lid'] = $login;
|
|
|
|
$_SESSION['egw']['session_ip'] = $user_ip;
|
|
|
|
$_SESSION['egw']['session_logintime'] = $now;
|
|
|
|
$_SESSION['egw']['session_dla'] = $now;
|
|
|
|
$_SESSION['egw']['session_action'] = $_SERVER['PHP_SELF'];
|
|
|
|
$_SESSION['egw']['session_flags'] = $session_flags;
|
2003-05-18 21:16:58 +02:00
|
|
|
// we need the install-id to differ between serveral installs shareing one tmp-dir
|
2005-02-18 21:28:06 +01:00
|
|
|
$_SESSION['egw']['session_install_id'] = $GLOBALS['phpgw_info']['server']['install_id'];
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will update the DateLastActive column, so the login does not expire
|
|
|
|
function update_dla()
|
|
|
|
{
|
2003-05-18 21:16:58 +02:00
|
|
|
if (@isset($_GET['menuaction']))
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
2003-05-18 21:16:58 +02:00
|
|
|
$action = $_GET['menuaction'];
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-18 21:16:58 +02:00
|
|
|
$action = $_SERVER['PHP_SELF'];
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
|
2003-10-25 13:37:36 +02:00
|
|
|
// This way XML-RPC users aren't always listed as
|
|
|
|
// xmlrpc.php
|
|
|
|
if ($this->xmlrpc_method_called)
|
|
|
|
{
|
|
|
|
$action = $this->xmlrpc_method_called;
|
|
|
|
}
|
|
|
|
|
2005-02-18 21:28:06 +01:00
|
|
|
$_SESSION['egw']['session_dla'] = time();
|
|
|
|
$_SESSION['egw']['session_action'] = $action;
|
2002-01-02 15:32:22 +01:00
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroy($sessionid, $kp3)
|
|
|
|
{
|
2004-08-27 15:17:25 +02:00
|
|
|
if (!$sessionid && $kp3)
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2003-05-18 21:16:58 +02:00
|
|
|
$this->log_access($this->sessionid); // log logout-time
|
2002-01-02 15:32:22 +01:00
|
|
|
|
|
|
|
// Only do the following, if where working with the current user
|
|
|
|
if ($sessionid == $GLOBALS['phpgw_info']['user']['sessionid'])
|
|
|
|
{
|
2003-03-21 01:47:18 +01:00
|
|
|
session_unset();
|
2004-08-27 15:17:25 +02:00
|
|
|
//echo "<p>sessions_php4::destroy: session_destroy() returned ".(session_destroy() ? 'True' : 'False')."</p>\n";
|
2004-09-13 09:46:36 +02:00
|
|
|
@session_destroy();
|
2004-03-06 21:35:23 +01:00
|
|
|
if ($GLOBALS['phpgw_info']['server']['usecookies'])
|
2003-10-25 13:37:36 +02:00
|
|
|
{
|
|
|
|
$this->phpgw_setcookie(session_name());
|
|
|
|
}
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
|
|
|
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";
|
2003-10-25 13:37:36 +02:00
|
|
|
@unlink($sessions[$sessionid]['php_session_file']);
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************\
|
|
|
|
* Functions for appsession data and session cache *
|
|
|
|
\*************************************************************************/
|
|
|
|
function delete_cache($accountid='')
|
|
|
|
{
|
|
|
|
$account_id = get_account_id($accountid,$this->account_id);
|
|
|
|
|
2005-02-18 21:28:06 +01:00
|
|
|
$_SESSION['egw']['app_sessions']['phpgwapi']['phpgw_info_cache'] = '';
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
2002-08-12 02:59:34 +02:00
|
|
|
|
2002-01-02 15:32:22 +01:00
|
|
|
function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
|
|
|
|
{
|
|
|
|
if (! $appname)
|
|
|
|
{
|
|
|
|
$appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
|
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2002-01-02 15:32:22 +01:00
|
|
|
/* This allows the user to put '' as the value. */
|
|
|
|
if ($data == '##NOTHING##')
|
|
|
|
{
|
|
|
|
/* do not decrypt and return if no data (decrypt returning garbage) */
|
2005-02-18 21:28:06 +01:00
|
|
|
if($_SESSION['egw']['app_sessions'][$appname][$location])
|
2002-01-02 15:32:22 +01:00
|
|
|
{
|
2005-02-18 21:28:06 +01:00
|
|
|
return $GLOBALS['phpgw']->crypto->decrypt($_SESSION['egw']['app_sessions'][$appname][$location]);
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
2005-02-18 21:28:06 +01:00
|
|
|
return false;
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
2005-02-18 21:28:06 +01:00
|
|
|
$_SESSION['egw']['app_sessions'][$appname][$location] = $GLOBALS['phpgw']->crypto->encrypt($data);
|
|
|
|
|
|
|
|
return $data;
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
function session_sort($a,$b)
|
|
|
|
{
|
|
|
|
$sign = strcasecmp($GLOBALS['phpgw']->session->sort_order,'ASC') ? 1 : -1;
|
|
|
|
|
2003-12-13 16:03:23 +01:00
|
|
|
return strcasecmp(
|
|
|
|
$a[$GLOBALS['phpgw']->session->sort_by],
|
|
|
|
$b[$GLOBALS['phpgw']->session->sort_by]
|
|
|
|
) * $sign;
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
/*!
|
|
|
|
@function list_sessions
|
|
|
|
@abstract get list of normal / non-anonymous sessions
|
2003-05-18 21:16:58 +02:00
|
|
|
@note The data form the session-files get cached in the app_session phpgwapi/php4_session_cache
|
2003-03-21 01:47:18 +01:00
|
|
|
@author ralfbecker
|
|
|
|
*/
|
|
|
|
function list_sessions($start,$order,$sort,$all_no_sort = False)
|
|
|
|
{
|
|
|
|
//echo "<p>session_php4::list_sessions($start,'$order','$sort',$all)</p>\n";
|
2003-05-18 21:16:58 +02:00
|
|
|
$session_cache = $this->appsession('php4_session_cache','phpgwapi');
|
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
$values = array();
|
|
|
|
$maxmatchs = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
|
2003-10-02 18:58:46 +02:00
|
|
|
$dir = @opendir($path = ini_get('session.save_path'));
|
|
|
|
if (!$dir) // eg. openbasedir restrictions
|
|
|
|
{
|
|
|
|
return $values;
|
|
|
|
}
|
2003-03-21 01:47:18 +01:00
|
|
|
while ($file = readdir($dir))
|
|
|
|
{
|
2005-02-18 21:28:06 +01:00
|
|
|
if (substr($file,0,5) != 'sess_' || $session_cache[$file] === false)
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-18 21:16:58 +02:00
|
|
|
if (isset($session_cache[$file])) // use copy from cache
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
2003-05-18 21:16:58 +02:00
|
|
|
if (!$all_no_sort) // we need the up-to-date data --> unset and reread it
|
|
|
|
{
|
|
|
|
unset($session_cache[$file]);
|
|
|
|
}
|
2005-02-18 21:28:06 +01:00
|
|
|
$session = $session_cache[$file];
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
2003-05-18 21:16:58 +02:00
|
|
|
if (!isset($session_cache[$file])) // not in cache, read and cache it
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
2003-10-25 13:37:36 +02:00
|
|
|
if (!is_readable($path. '/' . $file))
|
|
|
|
{
|
2005-02-18 21:28:06 +01:00
|
|
|
$session_cache[$file] = false; // dont try reading it again
|
2003-10-25 13:37:36 +02:00
|
|
|
continue; // happens if webserver runs multiple user-ids
|
|
|
|
}
|
2004-08-27 15:17:25 +02:00
|
|
|
$session = '';
|
|
|
|
if (($fd = fopen ($path . '/' . $file,'r')))
|
|
|
|
{
|
|
|
|
$session = ($size = filesize ($path . '/' . $file)) ? fread ($fd, $size) : 0;
|
|
|
|
fclose ($fd);
|
|
|
|
}
|
2005-02-18 21:28:06 +01:00
|
|
|
if (substr($session,0,4) != 'egw|')
|
2003-05-18 21:16:58 +02:00
|
|
|
{
|
2005-02-18 21:28:06 +01:00
|
|
|
$session_cache[$file] = false; // dont try reading it again
|
2003-05-18 21:16:58 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-02-18 21:28:06 +01:00
|
|
|
$session = unserialize(substr($session,4));
|
|
|
|
unset($session['app_sessions']); // not needed, saves memory
|
2003-05-18 21:16:58 +02:00
|
|
|
$session_cache[$file] = $session;
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
if($session['session_flags'] == 'A' || !$session['session_id'] ||
|
|
|
|
$session['session_install_id'] != $GLOBALS['phpgw_info']['server']['install_id'])
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
2005-02-18 21:28:06 +01:00
|
|
|
$session_cache[$file] = false; // dont try reading it again
|
2003-05-18 21:16:58 +02:00
|
|
|
continue; // no anonymous sessions or other domains or installations
|
2003-03-21 01:47:18 +01:00
|
|
|
}
|
|
|
|
//echo "file='$file'=<pre>"; print_r($session); echo "</pre>";
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
$session['php_session_file'] = $path . '/' . $file;
|
|
|
|
$values[$session['session_id']] = $session;
|
|
|
|
}
|
|
|
|
closedir($dir);
|
2003-12-13 16:03:23 +01:00
|
|
|
|
|
|
|
if(!$all_no_sort)
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
|
|
|
$GLOBALS['phpgw']->session->sort_by = $sort;
|
|
|
|
$GLOBALS['phpgw']->session->sort_order = $order;
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
uasort($values,array('sessions','session_sort'));
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
$i = 0;
|
2003-12-13 16:03:23 +01:00
|
|
|
$start = (int)$start;
|
2003-03-21 01:47:18 +01:00
|
|
|
foreach($values as $id => $data)
|
|
|
|
{
|
2003-12-13 16:03:23 +01:00
|
|
|
if($i < $start || $i > $start+$maxmatchs)
|
2003-03-21 01:47:18 +01:00
|
|
|
{
|
|
|
|
unset($values[$id]);
|
|
|
|
}
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
reset($values);
|
|
|
|
}
|
2003-05-18 21:16:58 +02:00
|
|
|
$this->appsession('php4_session_cache','phpgwapi',$session_cache);
|
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
return $values;
|
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2003-03-21 01:47:18 +01:00
|
|
|
/*!
|
|
|
|
@function total
|
|
|
|
@abstract get number of normal / non-anonymous sessions
|
|
|
|
@author ralfbecker
|
|
|
|
*/
|
|
|
|
function total()
|
|
|
|
{
|
|
|
|
return count($this->list_sessions(0,'','',True));
|
|
|
|
}
|
2002-01-02 15:32:22 +01:00
|
|
|
}
|