2009-06-30 08:07:14 +00:00
< ? php
2009-07-01 06:09:17 +00:00
// $Header$
2009-06-30 08:07:14 +00:00
2009-06-30 09:29:51 +00:00
/**
2009-06-30 11:46:44 +00:00
* Contains code to be executed at the top of each application page .
2009-06-30 08:07:14 +00:00
* include this file at the top of every PHP file .
2009-06-30 09:29:51 +00:00
*
2009-06-30 11:46:44 +00:00
* This file will " pre-initialise " an application environment so that any PHP file will have a consistent
* environment with other application PHP files .
2009-06-30 09:29:51 +00:00
*
* This code WILL NOT check that all required functions are usable / readable , etc . This process has
2009-06-30 11:46:44 +00:00
* been moved to index . php ( which really is only called once when a browser hits the application for the first time ) .
2009-06-30 09:29:51 +00:00
*
* The list of ADDITIONAL function files is now defined in functions . php .
*
2009-07-01 06:09:17 +00:00
* @ author The phpLDAPadmin development team
2009-06-30 09:29:51 +00:00
* @ package phpLDAPadmin
2009-06-30 08:07:14 +00:00
*/
2009-07-01 06:09:17 +00:00
/**
* @ package phpLDAPadmin
* @ subpackage Functions
*/
/* Initialize the app array . The app array is initialised each invocation of a PLA script and therefore
has no state between invocations .*/
$app = array ();
/** The index we will store our config in $_SESSION */
2009-06-30 11:50:46 +00:00
if ( ! defined ( 'APPCONFIG' ))
define ( 'APPCONFIG' , 'plaConfig' );
2009-06-30 11:46:44 +00:00
/**
* Catch any scripts that are called directly .
* If they are called directly , then they should be routed back through index . php
*/
$app [ 'direct_scripts' ] = array ( 'cmd.php' , 'index.php' ,
'view_jpeg_photo.php' , 'entry_chooser.php' ,
2009-06-30 11:52:55 +00:00
'password_checker.php' , 'download_binary_attr.php' ,
'unserialize.php'
);
2009-06-30 11:46:44 +00:00
2009-07-01 06:09:17 +00:00
# Which script was invoked.
$app [ 'script_running' ] = $_SERVER [ 'SCRIPT_NAME' ];
2009-06-30 11:46:44 +00:00
foreach ( $app [ 'direct_scripts' ] as $script ) {
2009-07-01 06:09:17 +00:00
$app [ 'scriptOK' ] = false ;
2009-06-30 10:46:00 +00:00
2009-07-01 06:09:17 +00:00
if ( preg_match ( '/' . $script . '$/' , $app [ 'script_running' ])) {
$app [ 'scriptOK' ] = true ;
2009-06-30 10:46:00 +00:00
break ;
}
}
2009-07-01 06:09:17 +00:00
# Anything in the tools dir or cron dir can be executed directly.
if (( ! $app [ 'scriptOK' ] && preg_match ( '/^\/[cron|tools]/' , $app [ 'script_running' ])) || ! isset ( $_SERVER [ 'SERVER_SOFTWARE' ]))
$app [ 'scriptOK' ] = true ;
2009-06-30 11:50:46 +00:00
2009-07-01 06:09:17 +00:00
if ( ! $app [ 'scriptOK' ]) {
2009-06-30 11:46:44 +00:00
if ( isset ( $_REQUEST [ 'server_id' ]))
2009-06-30 10:46:00 +00:00
header ( sprintf ( 'Location: index.php?server_id=%s' , $_REQUEST [ 'server_id' ]));
else
header ( 'Location: index.php' );
die ();
}
2009-07-01 06:09:17 +00:00
/**
* All commands are disabled in read - only unless specified here
*/
$app [ 'readwrite_cmds' ] = array (
'collapse' , 'draw_tree_node' , 'expand' ,
'compare_form' , 'compare' ,
'download_binary_attr' , 'view_jpeg_photo' ,
'entry_chooser' ,
'export_form' , 'export' ,
'login_form' , 'login' , 'logout' ,
'monitor' ,
'password_checker' ,
'purge_cache' ,
'refresh' , 'schema' , 'query_engine' , 'server_info' , 'show_cache' , 'template_engine' ,
'welcome'
);
2009-06-30 10:46:00 +00:00
/**
2009-06-30 11:46:44 +00:00
* Timer stopwatch , used to instrument the application
2009-06-30 10:46:00 +00:00
*/
if ( ! function_exists ( 'stopwatch' )) {
function stopwatch () {
static $mt_previous = 0 ;
list ( $usec , $sec ) = explode ( ' ' , microtime ());
$mt_current = ( float ) $usec + ( float ) $sec ;
if ( ! $mt_previous ) {
$mt_previous = $mt_current ;
return 0 ;
} else {
$mt_diff = ( $mt_current - $mt_previous );
$mt_previous = $mt_current ;
return sprintf ( '%.5f' , $mt_diff );
}
}
2009-06-30 11:46:44 +00:00
# For compatability - if common has been sourced a second time, then return to the calling script.
2009-06-30 10:46:00 +00:00
} else {
return ;
}
2009-06-30 11:46:44 +00:00
# Set the defualt time zone, if it isnt set in php.ini
if ( function_exists ( 'date_default_timezone_set' ) && ! ini_get ( 'date.timezone' ))
2009-06-30 10:46:00 +00:00
date_default_timezone_set ( 'UTC' );
2009-06-30 11:46:44 +00:00
# If we are called from index.php, LIBDIR will be set, all other calls to common.php dont need to set it.
2009-06-30 10:46:00 +00:00
if ( ! defined ( 'LIBDIR' ))
define ( 'LIBDIR' , '../lib/' );
2009-06-30 08:09:20 +00:00
2009-06-30 09:29:51 +00:00
# For PHP5 backward/forward compatibility
if ( ! defined ( 'E_STRICT' ))
define ( 'E_STRICT' , 2048 );
2009-06-30 08:09:20 +00:00
2009-06-30 10:26:08 +00:00
# General functions needed to proceed.
2009-06-30 08:10:17 +00:00
ob_start ();
2009-06-30 09:29:51 +00:00
require_once realpath ( LIBDIR . 'functions.php' );
2009-06-30 11:46:44 +00:00
if ( ob_get_level ())
ob_end_clean ();
2009-06-30 08:09:20 +00:00
2009-06-30 11:46:44 +00:00
/**
* Turn on all notices and warnings . This helps us write cleaner code ( we hope at least )
2009-06-30 10:46:00 +00:00
* Our custom error handler receives all error notices that pass the error_reporting ()
* level set above .
*/
2009-06-30 11:46:44 +00:00
# Call our custom defined error handler, if it is defined in functions.php
2009-07-01 06:09:17 +00:00
if ( function_exists ( 'app_error_handler' ))
set_error_handler ( 'app_error_handler' );
2009-06-30 11:46:44 +00:00
2009-06-30 09:40:37 +00:00
# Disable error reporting until all our required functions are loaded.
error_reporting ( 0 );
2009-06-30 08:09:20 +00:00
2009-06-30 11:46:44 +00:00
/**
* functions . php should have defined our $app [ 'function_files' ] array , listing all our
2009-06-30 09:29:51 +00:00
* required functions ( order IS important ) .
* index . php should have checked they exist and are usable - we ' ll assume that the user
* has been via index . php , and fixed any problems already .
*/
ob_start ();
2009-06-30 11:46:44 +00:00
if ( isset ( $app [ 'function_files' ]) && is_array ( $app [ 'function_files' ]))
2009-07-01 06:09:17 +00:00
foreach ( $app [ 'function_files' ] as $script )
require_once realpath ( $script );
2009-06-30 09:22:30 +00:00
2009-06-30 10:46:00 +00:00
# Now read in config_default.php
2009-06-30 09:29:51 +00:00
require_once realpath ( LIBDIR . 'config_default.php' );
2009-06-30 11:46:44 +00:00
if ( ob_get_level ())
ob_end_clean ();
2009-06-30 08:09:20 +00:00
2009-06-30 09:40:37 +00:00
# We are now ready for error reporting.
2009-06-30 10:46:00 +00:00
error_reporting ( E_ALL );
2009-06-30 09:40:37 +00:00
2009-06-30 11:46:44 +00:00
# Start our session.
2009-07-01 06:09:17 +00:00
app_session_start ();
2009-06-30 10:46:00 +00:00
2009-07-08 06:14:50 +00:00
# See if we have a session, we can then get our theme out
$app [ 'theme' ] = 'default' ;
if ( isset ( $_SESSION [ APPCONFIG ]))
2009-08-12 13:53:14 +00:00
if ( is_dir ( realpath ( sprintf ( 'images/%s' , $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'theme' ))))
2009-07-08 06:14:50 +00:00
&& is_file ( realpath ( sprintf ( 'css/%s/%s' , $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'theme' ), $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'stylesheet' )))))
2009-08-12 13:53:14 +00:00
$app [ 'theme' ] = $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'theme' );
2009-07-08 06:14:50 +00:00
define ( 'CSSDIR' , sprintf ( 'css/%s' , $app [ 'theme' ]));
define ( 'IMGDIR' , sprintf ( 'images/%s' , $app [ 'theme' ]));
2009-06-30 11:52:55 +00:00
# Initialise the hooks
2009-08-12 13:53:14 +00:00
if ( file_exists ( LIBDIR . 'hooks.php' ))
require_once LIBDIR . 'hooks.php' ;
2009-06-30 11:52:55 +00:00
2009-06-30 11:46:44 +00:00
# If we get here, and $_SESSION[APPCONFIG] is not set, then redirect the user to the index.
2009-07-01 06:09:17 +00:00
if ( isset ( $_SERVER [ 'SERVER_SOFTWARE' ]) && ! isset ( $_SESSION [ APPCONFIG ])) {
if ( $_SERVER [ 'QUERY_STRING' ])
header ( sprintf ( 'Location: index.php?URI=%s' , base64_encode ( $_SERVER [ 'QUERY_STRING' ])));
else
header ( 'Location: index.php' );
2009-06-30 10:46:00 +00:00
die ();
} else {
2009-06-30 11:52:55 +00:00
# SF Bug #1903987
if ( ! method_exists ( $_SESSION [ APPCONFIG ], 'CheckCustom' ))
error ( 'Unknown situation, $_SESSION[APPCONFIG] exists, but method CheckCustom() does not' , 'error' , null , true , true );
2009-06-30 10:46:00 +00:00
# Check our custom variables.
2009-07-01 06:09:17 +00:00
# @todo Change this so that we dont process a cached session.
2009-06-30 11:46:44 +00:00
$_SESSION [ APPCONFIG ] -> CheckCustom ();
2009-06-30 10:46:00 +00:00
}
2009-06-30 11:52:55 +00:00
# Check for safe mode.
if ( ini_get ( 'safe_mode' ) && ! get_request ( 'cmd' , 'GET' ))
system_message ( array (
'title' => _ ( 'PHP Safe Mode' ),
2009-07-01 06:09:17 +00:00
'body' => _ ( 'You have PHP Safe Mode enabled. This application may work unexpectedly in Safe Mode.' ),
2009-06-30 11:52:55 +00:00
'type' => 'info' ));
2009-06-30 11:51:50 +00:00
# Set our timezone, if it is specified in config.php
2009-07-01 06:09:17 +00:00
if ( $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'timezone' ))
date_default_timezone_set ( $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'timezone' ));
2009-06-30 11:51:50 +00:00
2009-06-30 10:46:00 +00:00
# If we are here, $_SESSION is set - so enabled DEBUGing if it has been configured.
2009-07-01 06:09:17 +00:00
if (( $_SESSION [ APPCONFIG ] -> getValue ( 'debug' , 'syslog' ) || $_SESSION [ APPCONFIG ] -> getValue ( 'debug' , 'file' ))
&& $_SESSION [ APPCONFIG ] -> getValue ( 'debug' , 'level' ))
2009-06-30 10:46:00 +00:00
define ( 'DEBUG_ENABLED' , 1 );
else
define ( 'DEBUG_ENABLED' , 0 );
if ( DEBUG_ENABLED )
2009-08-19 03:39:37 +00:00
debug_log ( 'Application (%s) initialised and starting with (%s).' , 1 , 0 , __FILE__ , __LINE__ , __METHOD__ ,
2009-07-01 06:09:17 +00:00
app_version (), $_REQUEST );
2009-06-30 10:46:00 +00:00
# Set our PHP timelimit.
2009-07-01 06:09:17 +00:00
if ( $_SESSION [ APPCONFIG ] -> getValue ( 'session' , 'timelimit' ) && ! ini_get ( 'safe_mode' ))
set_time_limit ( $_SESSION [ APPCONFIG ] -> getValue ( 'session' , 'timelimit' ));
2009-06-30 10:46:00 +00:00
# If debug mode is set, increase the time_limit, since we probably need it.
2009-07-01 06:09:17 +00:00
if ( DEBUG_ENABLED && $_SESSION [ APPCONFIG ] -> getValue ( 'session' , 'timelimit' ) && ! ini_get ( 'safe_mode' ))
set_time_limit ( $_SESSION [ APPCONFIG ] -> getValue ( 'session' , 'timelimit' ) * 5 );
2009-06-30 08:10:17 +00:00
2009-06-30 11:46:44 +00:00
/**
2009-06-30 09:29:51 +00:00
* Language configuration . Auto or specified ?
* Shall we attempt to auto - determine the language ?
*/
2009-07-01 06:09:17 +00:00
# If we are in safe mode, and LANG is not in the allowed vars, display an error.
if ( ini_get ( 'safe_mode' ) && ! in_array ( 'LANG' , explode ( ',' , ini_get ( 'safe_mode_allowed_env_vars' ))))
error ( 'You are running in SAFE_MODE, but LANG is not in the safe_mode_allowed_env_vars. Please add LANG to safe_mode_allowed_env_vars' , 'error' , true , false );
$app [ 'language' ] = $_SESSION [ APPCONFIG ] -> getValue ( 'appearance' , 'language' );
2009-06-30 09:40:37 +00:00
2009-07-01 06:09:17 +00:00
if ( $app [ 'language' ] == 'auto' ) {
2009-06-30 11:46:44 +00:00
2009-06-30 09:29:51 +00:00
# Make sure their browser correctly reports language. If not, skip this.
2009-06-30 10:26:08 +00:00
if ( isset ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ])) {
# Get the languages which are spetcified in the HTTP header
2009-07-01 06:09:17 +00:00
$app [ 'lang_http' ] = preg_split ( '/[;,]+/' , $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ]);
foreach ( $app [ 'lang_http' ] as $key => $value ) {
2009-06-30 10:26:08 +00:00
if ( substr ( $value , 0 , 2 ) == 'q=' ) {
2009-07-01 06:09:17 +00:00
unset ( $app [ 'lang_http' ][ $key ]);
2009-06-30 10:26:08 +00:00
continue ;
}
$value = preg_split ( '/[-]+/' , $value );
if ( sizeof ( $value ) == 2 )
2009-07-01 06:09:17 +00:00
$app [ 'lang_http' ][ $key ] = strtolower ( $value [ 0 ]) . '_' . strtoupper ( $value [ 1 ]);
2009-06-30 10:26:08 +00:00
else
2009-07-01 06:09:17 +00:00
$app [ 'lang_http' ][ $key ] = auto_lang ( strtolower ( $value [ 0 ]));
2009-06-30 08:09:20 +00:00
}
2009-06-30 09:29:51 +00:00
2009-07-01 06:09:17 +00:00
$app [ 'lang_http' ] = array_unique ( $app [ 'lang_http' ]);
2009-06-30 10:26:08 +00:00
2009-07-01 06:09:17 +00:00
foreach ( $app [ 'lang_http' ] as $lang ) {
$app [ 'language_dir' ] = LANGDIR . $lang ;
2009-06-30 10:26:08 +00:00
2009-07-01 06:09:17 +00:00
if (( substr ( $lang , 0 , 2 ) == 'en' ) ||
( file_exists ( $app [ 'language_dir' ]) && is_readable ( $app [ 'language_dir' ]))) {
2009-06-30 10:26:08 +00:00
# Set language
2009-07-01 06:09:17 +00:00
putenv ( 'LANG=' . $lang ); # e.g. LANG=de_DE
$lang .= '.UTF-8' ;
setlocale ( LC_ALL , $lang ); # set LC_ALL to de_DE
2009-06-30 10:26:08 +00:00
bindtextdomain ( 'messages' , LANGDIR );
bind_textdomain_codeset ( 'messages' , 'UTF-8' );
textdomain ( 'messages' );
header ( 'Content-type: text/html; charset=UTF-8' , true );
2009-06-30 09:29:51 +00:00
break ;
2009-06-30 08:09:20 +00:00
}
}
2009-07-01 06:09:17 +00:00
#todo Generate an error if language doesnt exist.
2009-06-30 08:09:20 +00:00
}
2009-06-30 09:29:51 +00:00
} else {
2009-06-30 10:26:08 +00:00
# Grab the language file configured in config.php
2009-07-01 06:09:17 +00:00
#todo Generate an error if language doesnt exist.
if ( $app [ 'language' ] != null ) {
if ( strcmp ( $app [ 'language' ], 'english' ) == 0 )
$app [ 'language' ] = 'en_GB' ;
2009-06-30 10:26:08 +00:00
# Set language
2009-07-01 06:09:17 +00:00
putenv ( 'LANG=' . $app [ 'language' ]); # e.g. LANG=de_DE
$app [ 'language' ] .= '.UTF-8' ;
setlocale ( LC_ALL , $app [ 'language' ]); # set LC_ALL to de_DE
2009-06-30 10:26:08 +00:00
bindtextdomain ( 'messages' , LANGDIR );
bind_textdomain_codeset ( 'messages' , 'UTF-8' );
textdomain ( 'messages' );
2009-06-30 10:46:00 +00:00
header ( 'Content-type: text/html; charset=UTF-8' , true );
2009-06-30 09:29:51 +00:00
}
2009-06-30 08:09:20 +00:00
}
2009-06-30 08:07:14 +00:00
2009-06-30 11:46:44 +00:00
/**
2009-06-30 09:29:51 +00:00
* Strip slashes from GET , POST , and COOKIE variables if this
* PHP install is configured to automatically addslashes ()
*/
if ( get_magic_quotes_gpc () && ( ! isset ( $slashes_stripped ) || ! $slashes_stripped )) {
array_stripslashes ( $_REQUEST );
2009-06-30 08:07:14 +00:00
array_stripslashes ( $_GET );
2009-06-30 08:09:20 +00:00
array_stripslashes ( $_POST );
array_stripslashes ( $_COOKIE );
2009-06-30 08:07:14 +00:00
$slashes_stripped = true ;
}
2009-07-01 06:09:17 +00:00
# Create our application repository variable.
$app [ 'server' ] = $_SESSION [ APPCONFIG ] -> getServer ( get_request ( 'server_id' , 'REQUEST' ));
2009-06-30 10:26:08 +00:00
2009-06-30 11:46:44 +00:00
/**
* Look / evaluate our timeout
*/
2009-07-01 06:09:17 +00:00
if ( ! $app [ 'server' ] -> isSessionValid ()) {
system_message ( array (
'title' => _ ( 'Session Timed Out' ),
'body' => sprintf ( '%s %s %s' ,
_ ( 'Your Session timed out after' ), $app [ 'server' ] -> getValue ( 'login' , 'timeout' ),
_ ( 'min. of inactivity. You have been automatically logged out.' )),
'type' => 'info' ), sprintf ( 'index.php?server_id=%s&refresh=SID_%s' , $app [ 'server' ] -> getIndex (), $app [ 'server' ] -> getIndex ()));
2009-06-30 10:26:08 +00:00
2009-07-01 06:09:17 +00:00
die ();
2009-06-30 11:46:44 +00:00
}
2009-06-30 10:46:00 +00:00
2009-07-01 06:09:17 +00:00
# If syslog is enabled, we need to include the supporting file.
if ( $_SESSION [ APPCONFIG ] -> getValue ( 'debug' , 'syslog' ))
require LIBDIR . 'syslog.php' ;
2009-06-30 10:26:08 +00:00
/**
2009-06-30 10:46:00 +00:00
* At this point we have read all our additional function PHP files and our configuration .
2009-06-30 11:46:44 +00:00
* If we are using hooks , run the session_init hook .
2009-06-30 10:26:08 +00:00
*/
2009-06-30 11:46:44 +00:00
if ( function_exists ( 'run_hook' ))
run_hook ( 'post_session_init' , array ());
2009-06-30 08:07:14 +00:00
?>