phpldapadmin/lib/session_functions.php

179 lines
5.6 KiB
PHP
Raw Normal View History

2009-06-30 09:22:30 +00:00
<?php
// $Header$
2009-06-30 09:22:30 +00:00
/**
* A collection of functions to handle sessions.
*
2009-06-30 09:22:30 +00:00
* @author The phpLDAPadmin development team
* @package phpLDAPadmin
* @subpackage Session
2009-06-30 09:22:30 +00:00
*/
/** The session ID that this application will use for all sessions */
define('APP_SESSION_ID',md5(app_name()));
2009-06-30 09:22:30 +00:00
/** Enables session paranoia, which causes SIDs to change each page load (EXPERIMENTAL!) */
define('app_session_id_paranoid', false);
2009-06-30 09:22:30 +00:00
/** Flag to indicate whether the session has already been initialized (this constant gets stored in $_SESSION) */
define('app_session_id_init', 'app_initialized');
2009-06-30 09:22:30 +00:00
/** The minimum first char value IP in hex for IP hashing. */
define('app_session_id_ip_min', 8);
2009-06-30 09:22:30 +00:00
/** The maximum first char value of the IP in hex for IP hashing. */
define('app_session_id_ses_max', 36);
2009-06-30 09:22:30 +00:00
/**
* Creates a new session id, which includes an IP hash.
*
* @return string the new session ID string
*/
function app_session_get_id() {
2009-06-30 09:22:30 +00:00
$id_md5 = md5(rand(1,1000000));
$ip_md5 = md5($_SERVER['REMOTE_ADDR']);
$id_hex = hexdec($id_md5[0]) + 1;
$ip_hex = hexdec($ip_md5[0]);
if ($ip_hex <= app_session_id_ip_min)
$ip_len = app_session_id_ip_min;
2009-06-30 09:22:30 +00:00
else
$ip_len = $ip_hex - 1;
2009-06-30 09:29:51 +00:00
$new_id = substr($id_md5, 0, $id_hex) .
2009-06-30 09:22:30 +00:00
substr($ip_md5, $ip_hex, $ip_len) .
substr($id_md5, $id_hex, app_session_id_ses_max - ($id_hex + $ip_len));
2009-06-30 09:29:51 +00:00
2009-06-30 09:22:30 +00:00
return $new_id;
}
/**
* Checks if the session belongs to an IP
*
* @return boolean True, if the session is valid
2009-06-30 09:22:30 +00:00
*/
function app_session_verify_id() {
2009-06-30 09:22:30 +00:00
$check_id = session_id();
$ip_md5 = md5($_SERVER['REMOTE_ADDR']);
$id_hex = hexdec($check_id[0]) + 1;
$ip_hex = hexdec($ip_md5[0]);
if ($ip_hex <= app_session_id_ip_min)
$ip_len = app_session_id_ip_min;
2009-06-30 09:22:30 +00:00
else
$ip_len = $ip_hex - 1;
2009-06-30 09:29:51 +00:00
2009-06-30 09:22:30 +00:00
$ip_ses = substr($check_id, $id_hex, $ip_len);
$ip_ver = substr($ip_md5, $ip_hex, $ip_len);
return ($ip_ses == $ip_ver);
}
function app_session_param() {
2009-06-30 10:46:00 +00:00
/* If cookies were disabled, build the url parameter for the session id.
2009-06-30 11:51:50 +00:00
* It will be append to the url to be redirect */
2009-06-30 10:46:00 +00:00
return (SID != '') ? sprintf('&%s=%s',session_name(),session_id()) : '';
}
2009-06-30 09:22:30 +00:00
/**
* The only function which should be called by a user
*
* @see common.php
* @see APP_SESSION_ID
* @return boolean Returns true if the session was started the first time
2009-06-30 09:22:30 +00:00
*/
function app_session_start() {
$sysmsg = null;
# If we have a sysmsg before our session has started, then preserve it.
if (isset($_SESSION['sysmsg']))
$sysmsg = $_SESSION['sysmsg'];
2009-06-30 11:46:44 +00:00
2009-06-30 10:46:00 +00:00
/* If session.auto_start is on in the server's PHP configuration (php.ini), then
* we will have problems loading our schema cache since the session will have started
* prior to loading the SchemaItem (and descedants) class. Destroy the auto-started
* session to prevent this problem.
*/
if (ini_get('session.auto_start'))
@session_destroy();
# Do we already have a session?
if (@session_id())
return;
2009-06-30 10:46:00 +00:00
@session_name(APP_SESSION_ID);
2009-06-30 10:46:00 +00:00
@session_start();
# Do we have a valid session?
$is_initialized = is_array($_SESSION) && array_key_exists(app_session_id_init,$_SESSION);
2009-06-30 10:46:00 +00:00
if (! $is_initialized) {
if (app_session_id_paranoid) {
2009-06-30 10:46:00 +00:00
ini_set('session.use_trans_sid',0);
2009-06-30 09:22:30 +00:00
@session_destroy();
@session_id(app_session_get_id());
2009-06-30 09:22:30 +00:00
@session_start();
2009-06-30 10:46:00 +00:00
ini_set('session.use_trans_sid',1);
2009-06-30 09:22:30 +00:00
}
2009-06-30 10:46:00 +00:00
$_SESSION[app_session_id_init]['name'] = app_name();
$_SESSION[app_session_id_init]['version'] = app_version();
$_SESSION[app_session_id_init]['config'] = filemtime(CONFDIR.'config.php');
2009-06-30 09:22:30 +00:00
}
2009-06-30 10:46:00 +00:00
@header('Cache-control: private'); // IE 6 Fix
2009-06-30 09:22:30 +00:00
if (app_session_id_paranoid && ! app_session_verify_id())
2009-06-30 11:52:55 +00:00
error('Session inconsistent or session timeout','error','index.php');
2009-06-30 09:22:30 +00:00
2009-06-30 11:46:44 +00:00
# Check we have the correct version of the SESSION cache
if (isset($_SESSION['cache']) || isset($_SESSION[app_session_id_init])) {
if (! is_array($_SESSION[app_session_id_init])) $_SESSION[app_session_id_init] = array();
2009-06-30 11:46:44 +00:00
if (! isset($_SESSION[app_session_id_init]['version']) || ! isset($_SESSION[app_session_id_init]['config']) || ! isset($_SESSION[app_session_id_init]['name'])
|| $_SESSION[app_session_id_init]['name'] !== app_name()
|| $_SESSION[app_session_id_init]['version'] !== app_version()
|| $_SESSION[app_session_id_init]['config'] != filemtime(CONFDIR.'config.php')) {
2009-06-30 11:51:50 +00:00
$_SESSION[app_session_id_init]['name'] = app_name();
$_SESSION[app_session_id_init]['version'] = app_version();
$_SESSION[app_session_id_init]['config'] = filemtime(CONFDIR.'config.php');
2009-06-30 11:46:44 +00:00
unset($_SESSION['cache']);
unset($_SESSION[APPCONFIG]);
# Our configuration information has changed, so we'll redirect to index.php to get it reloaded again.
system_message(array(
'title'=>_('Configuration cache stale.'),
'body'=>_('Your configuration has been automatically refreshed.'),
'type'=>'info','special'=>true));
2009-06-30 11:46:44 +00:00
$config_file = CONFDIR.'config.php';
$config = check_config($config_file);
if (! $config)
debug_dump_backtrace('config is empty?',1);
2009-06-30 11:46:44 +00:00
} else {
# Sanity check, specially when upgrading from a previous release.
if (isset($_SESSION['cache']))
foreach (array_keys($_SESSION['cache']) as $id)
if (isset($_SESSION['cache'][$id]['tree']['null']) && ! is_object($_SESSION['cache'][$id]['tree']['null']))
unset($_SESSION['cache'][$id]);
}
}
# If we came via index.php, then set our $config.
if (! isset($_SESSION[APPCONFIG]) && isset($config))
$_SESSION[APPCONFIG] = $config;
# Restore our sysmsg's if there were any.
if ($sysmsg) {
if (! isset($_SESSION['sysmsg']) || ! is_array($_SESSION['sysmsg']))
$_SESSION['sysmsg'] = array();
$_SESSION['sysmsg'] = array_merge($_SESSION['sysmsg'],$sysmsg);
}
2009-06-30 09:22:30 +00:00
}
/**
* Stops the current session.
*/
function app_session_close() {
2009-06-30 11:51:50 +00:00
@session_write_close();
2009-06-30 09:22:30 +00:00
}
?>