~ubuntu-branches/ubuntu/trusty/phpldapadmin/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/common.php

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2010-05-10 06:15:32 UTC
  • mfrom: (1.1.9 upstream) (3.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100510061532-s4m6ytom0eb7oam1
Tags: 1.2.0.5-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Merged call to dh_install to install debian/additional-templates/*
  - added groupOfNames.xml
  - Adds php_value memory_limit 32M to the apache.conf.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/common.php,v 1.80.2.17 2008/12/13 08:57:09 wurley Exp $
3
 
 
4
2
/**
5
3
 * Contains code to be executed at the top of each application page.
6
4
 * include this file at the top of every PHP file.
13
11
 *
14
12
 * The list of ADDITIONAL function files is now defined in functions.php.
15
13
 *
16
 
 * @package phpLDAPadmin
17
 
 */
18
 
 
19
 
# The index we will store our config in $_SESSION
 
14
 * @author The phpLDAPadmin development team
 
15
 * @package phpLDAPadmin
 
16
 */
 
17
 
 
18
/**
 
19
 * @package phpLDAPadmin
 
20
 * @subpackage Functions
 
21
 */
 
22
 
 
23
/* Initialize the app array. The app array is initialised each invocation of a PLA script and therefore
 
24
   has no state between invocations.*/
 
25
$app = array();
 
26
 
 
27
/** The index we will store our config in $_SESSION */
20
28
if (! defined('APPCONFIG'))
21
29
        define('APPCONFIG','plaConfig');
22
30
 
30
38
        'unserialize.php'
31
39
        );
32
40
 
 
41
# Which script was invoked.
 
42
$app['script_running'] = $_SERVER['SCRIPT_NAME'];
 
43
 
33
44
foreach ($app['direct_scripts'] as $script) {
34
 
        $scriptOK = false;
 
45
        $app['scriptOK'] = false;
35
46
 
36
 
        if (preg_match('/'.$script.'$/',$_SERVER['SCRIPT_NAME'])) {
37
 
                $scriptOK = true;
 
47
        if (preg_match('/'.$script.'$/',$app['script_running'])) {
 
48
                $app['scriptOK'] = true;
38
49
                break;
39
50
        }
40
51
}
41
52
 
42
 
# Anything in the tools dir can be executed directly.
43
 
if (! $scriptOK && preg_match('/^\/tools/',$_SERVER['SCRIPT_NAME']))
44
 
        $scriptOK = true;
 
53
# Anything in the tools dir or cron dir can be executed directly.
 
54
if ((! $app['scriptOK'] && preg_match('/^\/[cron|tools]/',$app['script_running'])) || ! isset($_SERVER['SERVER_SOFTWARE']))
 
55
        $app['scriptOK'] = true;
45
56
 
46
 
if (! $scriptOK) {
 
57
if (! $app['scriptOK']) {
47
58
        if (isset($_REQUEST['server_id']))
48
59
                header(sprintf('Location: index.php?server_id=%s',$_REQUEST['server_id']));
49
60
        else
52
63
}
53
64
 
54
65
/**
 
66
 * All commands are disabled in read-only unless specified here
 
67
 */
 
68
$app['readwrite_cmds'] = array(
 
69
        'collapse','draw_tree_node','expand',
 
70
        'compare_form','compare',
 
71
        'download_binary_attr','view_jpeg_photo',
 
72
        'entry_chooser',
 
73
        'export_form','export',
 
74
        'login_form','login','logout',
 
75
        'monitor',
 
76
        'password_checker',
 
77
        'purge_cache',
 
78
        'refresh','schema','query_engine','server_info','show_cache','template_engine',
 
79
        'welcome'
 
80
        );
 
81
 
 
82
/**
55
83
 * Timer stopwatch, used to instrument the application
56
84
 */
57
85
if (! function_exists('stopwatch')) {
81
109
if (function_exists('date_default_timezone_set') && ! ini_get('date.timezone'))
82
110
        date_default_timezone_set('UTC');
83
111
 
84
 
# Start out instrumentation
85
 
$timer = stopwatch();
86
 
 
87
112
# If we are called from index.php, LIBDIR will be set, all other calls to common.php dont need to set it.
88
113
if (! defined('LIBDIR'))
89
114
        define('LIBDIR','../lib/');
105
130
 */
106
131
 
107
132
# Call our custom defined error handler, if it is defined in functions.php
108
 
if (function_exists('pla_error_handler'))
109
 
        set_error_handler('pla_error_handler');
 
133
if (function_exists('app_error_handler'))
 
134
        set_error_handler('app_error_handler');
110
135
 
111
136
# Disable error reporting until all our required functions are loaded.
112
137
error_reporting(0);
119
144
 */
120
145
ob_start();
121
146
if (isset($app['function_files']) && is_array($app['function_files']))
122
 
        foreach ($app['function_files'] as $file_name) {
123
 
                require_once realpath ($file_name);
124
 
        }
 
147
        foreach ($app['function_files'] as $script)
 
148
                require_once realpath($script);
125
149
 
126
150
# Now read in config_default.php
127
151
require_once realpath(LIBDIR.'config_default.php');
132
156
error_reporting(E_ALL);
133
157
 
134
158
# Start our session.
135
 
pla_session_start();
 
159
app_session_start();
 
160
 
 
161
# See if we have a session, we can then get our theme out
 
162
$app['theme'] = 'default';
 
163
if (isset($_SESSION[APPCONFIG]))
 
164
        if (is_dir(realpath(sprintf('images/%s',$_SESSION[APPCONFIG]->getValue('appearance','theme'))))
 
165
                && is_file(realpath(sprintf('css/%s/%s',$_SESSION[APPCONFIG]->getValue('appearance','theme'),$_SESSION[APPCONFIG]->getValue('appearance','stylesheet')))))
 
166
 
 
167
                $app['theme'] = $_SESSION[APPCONFIG]->getValue('appearance','theme');
 
168
 
 
169
define('CSSDIR',sprintf('css/%s',$app['theme']));
 
170
define('IMGDIR',sprintf('images/%s',$app['theme']));
136
171
 
137
172
# Initialise the hooks
138
 
require_once LIBDIR.'hooks.php';
 
173
if (file_exists(LIBDIR.'hooks.php'))
 
174
        require_once LIBDIR.'hooks.php';
139
175
 
140
176
# If we get here, and $_SESSION[APPCONFIG] is not set, then redirect the user to the index.
141
 
if (! isset($_SESSION[APPCONFIG])) {
142
 
        header(sprintf('Location: index.php?URI=%s',base64_encode($_SERVER['QUERY_STRING'])));
 
177
if (isset($_SERVER['SERVER_SOFTWARE']) && ! isset($_SESSION[APPCONFIG])) {
 
178
        if ($_SERVER['QUERY_STRING'])
 
179
                header(sprintf('Location: index.php?URI=%s',base64_encode($_SERVER['QUERY_STRING'])));
 
180
        else
 
181
                header('Location: index.php');
 
182
 
143
183
        die();
144
184
 
145
185
} else {
148
188
                error('Unknown situation, $_SESSION[APPCONFIG] exists, but method CheckCustom() does not','error',null,true,true);
149
189
 
150
190
        # Check our custom variables.
151
 
        # @todo: Change this so that we dont process a cached session.
 
191
        # @todo Change this so that we dont process a cached session.
152
192
        $_SESSION[APPCONFIG]->CheckCustom();
153
193
}
154
194
 
156
196
if (ini_get('safe_mode') && ! get_request('cmd','GET'))
157
197
        system_message(array(
158
198
        'title'=>_('PHP Safe Mode'),
159
 
        'body'=>_('You have PHP Safe Mode enabled. PLA may work unexpectedly in Safe Mode.'),
 
199
        'body'=>_('You have PHP Safe Mode enabled. This application may work unexpectedly in Safe Mode.'),
160
200
        'type'=>'info'));
161
201
 
162
202
# Set our timezone, if it is specified in config.php
163
 
if ($_SESSION[APPCONFIG]->GetValue('appearance','timezone'))
164
 
        date_default_timezone_set($_SESSION[APPCONFIG]->GetValue('appearance','timezone'));
 
203
if ($_SESSION[APPCONFIG]->getValue('appearance','timezone'))
 
204
        date_default_timezone_set($_SESSION[APPCONFIG]->getValue('appearance','timezone'));
165
205
 
166
206
# If we are here, $_SESSION is set - so enabled DEBUGing if it has been configured.
167
 
if (($_SESSION[APPCONFIG]->GetValue('debug','syslog') || $_SESSION[APPCONFIG]->GetValue('debug','file'))
168
 
        && $_SESSION[APPCONFIG]->GetValue('debug','level'))
 
207
if (($_SESSION[APPCONFIG]->getValue('debug','syslog') || $_SESSION[APPCONFIG]->getValue('debug','file'))
 
208
        && $_SESSION[APPCONFIG]->getValue('debug','level'))
169
209
        define('DEBUG_ENABLED',1);
170
210
else
171
211
        define('DEBUG_ENABLED',0);
172
212
 
173
213
if (DEBUG_ENABLED)
174
 
        debug_log('Application (%s) initialised and starting with (%s).',1,__FILE__,__LINE__,__METHOD__,
175
 
                pla_version(),$_REQUEST);
 
214
        debug_log('Application (%s) initialised and starting with (%s).',1,0,__FILE__,__LINE__,__METHOD__,
 
215
                app_version(),$_REQUEST);
176
216
 
177
217
# Set our PHP timelimit.
178
 
if ($_SESSION[APPCONFIG]->GetValue('session','timelimit'))
179
 
        @set_time_limit($_SESSION[APPCONFIG]->GetValue('session','timelimit'));
 
218
if ($_SESSION[APPCONFIG]->getValue('session','timelimit') && ! ini_get('safe_mode'))
 
219
        set_time_limit($_SESSION[APPCONFIG]->getValue('session','timelimit'));
180
220
 
181
221
# If debug mode is set, increase the time_limit, since we probably need it.
182
 
if (DEBUG_ENABLED && $_SESSION[APPCONFIG]->GetValue('session','timelimit'))
183
 
        @set_time_limit($_SESSION[APPCONFIG]->GetValue('session','timelimit') * 5);
 
222
if (DEBUG_ENABLED && $_SESSION[APPCONFIG]->getValue('session','timelimit') && ! ini_get('safe_mode'))
 
223
        set_time_limit($_SESSION[APPCONFIG]->getValue('session','timelimit') * 5);
184
224
 
185
225
/**
186
226
 * Language configuration. Auto or specified?
187
227
 * Shall we attempt to auto-determine the language?
188
228
 */
189
 
$language = $_SESSION[APPCONFIG]->GetValue('appearance','language');
190
 
 
191
 
if ($language == 'auto') {
 
229
# If we are in safe mode, and LANG is not in the allowed vars, display an error.
 
230
if (ini_get('safe_mode') && ! in_array('LANG',explode(',',ini_get('safe_mode_allowed_env_vars'))))
 
231
        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);
 
232
 
 
233
$app['language'] = $_SESSION[APPCONFIG]->getValue('appearance','language');
 
234
 
 
235
if ($app['language'] == 'auto') {
192
236
 
193
237
        # Make sure their browser correctly reports language. If not, skip this.
194
238
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
195
239
 
196
240
                # Get the languages which are spetcified in the HTTP header
197
 
                $HTTP_LANGS = preg_split ('/[;,]+/',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
198
 
                foreach ($HTTP_LANGS as $key => $value) {
 
241
                $app['lang_http'] = preg_split ('/[;,]+/',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
 
242
                foreach ($app['lang_http'] as $key => $value) {
199
243
                        if (substr($value,0,2) == 'q=') {
200
 
                                unset($HTTP_LANGS[$key]);
 
244
                                unset($app['lang_http'][$key]);
201
245
                                continue;
202
246
                        }
203
247
 
204
248
                        $value = preg_split('/[-]+/',$value);
205
249
                        if (sizeof($value) == 2)
206
 
                                $HTTP_LANGS[$key] = strtolower($value[0]).'_'.strtoupper($value[1]);
 
250
                                $app['lang_http'][$key] = strtolower($value[0]).'_'.strtoupper($value[1]);
207
251
                        else
208
 
                                $HTTP_LANGS[$key] = auto_lang(strtolower($value[0]));
 
252
                                $app['lang_http'][$key] = auto_lang(strtolower($value[0]));
209
253
                }
210
254
 
211
 
                $HTTP_LANGS = array_unique($HTTP_LANGS);
212
 
 
213
 
                foreach ($HTTP_LANGS as $HTTP_LANG) {
214
 
                        $language_dir = LANGDIR.$HTTP_LANG;
215
 
 
216
 
                        if ((substr($HTTP_LANG,0,2) == 'en') ||
217
 
                                (file_exists($language_dir) && is_readable($language_dir))) {
 
255
                $app['lang_http'] = array_unique($app['lang_http']);
 
256
 
 
257
                foreach ($app['lang_http'] as $lang) {
 
258
                        $app['language_dir'] = LANGDIR.$lang;
 
259
 
 
260
                        if ((substr($lang,0,2) == 'en') ||
 
261
                                (file_exists($app['language_dir']) && is_readable($app['language_dir']))) {
218
262
 
219
263
                                # Set language
220
 
                                @putenv('LANG='.$HTTP_LANG); # e.g. LANG=de_DE
221
 
                                $HTTP_LANG .= '.UTF-8';
222
 
                                setlocale(LC_ALL,$HTTP_LANG); # set LC_ALL to de_DE
 
264
                                putenv('LANG='.$lang); # e.g. LANG=de_DE
 
265
                                $lang .= '.UTF-8';
 
266
                                setlocale(LC_ALL,$lang); # set LC_ALL to de_DE
223
267
                                bindtextdomain('messages',LANGDIR);
224
268
                                bind_textdomain_codeset('messages','UTF-8');
225
269
                                textdomain('messages');
227
271
                                break;
228
272
                        }
229
273
                }
230
 
                #todo: Generate an error if language doesnt exist.
 
274
                #todo Generate an error if language doesnt exist.
231
275
        }
232
276
 
233
277
} else {
234
278
        # Grab the language file configured in config.php
235
 
        #todo: Generate an error if language doesnt exist.
236
 
        if ($language != null) {
237
 
                if (strcmp($language,'english') == 0)
238
 
                        $language = 'en_GB';
 
279
        #todo Generate an error if language doesnt exist.
 
280
        if ($app['language'] != null) {
 
281
                if (strcmp($app['language'],'english') == 0)
 
282
                        $app['language'] = 'en_GB';
239
283
 
240
284
                # Set language
241
 
                @putenv('LANG='.$language); # e.g. LANG=de_DE
242
 
                $language .= '.UTF-8';
243
 
                setlocale(LC_ALL,$language); # set LC_ALL to de_DE
 
285
                putenv('LANG='.$app['language']); # e.g. LANG=de_DE
 
286
                $app['language'] .= '.UTF-8';
 
287
                setlocale(LC_ALL,$app['language']); # set LC_ALL to de_DE
244
288
                bindtextdomain('messages',LANGDIR);
245
289
                bind_textdomain_codeset('messages','UTF-8');
246
290
                textdomain('messages');
260
304
        $slashes_stripped = true;
261
305
}
262
306
 
263
 
/**
264
 
 * Create our application repository variable.
265
 
 */
266
 
if (isset($_REQUEST['server_id'])) {
267
 
        $ldapserver = $_SESSION[APPCONFIG]->ldapservers->Instance($_REQUEST['server_id']);
268
 
} else {
269
 
        if (isset($_SESSION[APPCONFIG]->ldapservers) && is_object($_SESSION[APPCONFIG]->ldapservers))
270
 
                $ldapserver = $_SESSION[APPCONFIG]->ldapservers->Instance(null);
271
 
}
 
307
# Create our application repository variable.
 
308
$app['server'] = $_SESSION[APPCONFIG]->getServer(get_request('server_id','REQUEST'));
272
309
 
273
310
/**
274
311
 * Look/evaluate our timeout
275
312
 */
276
 
if (isset($ldapserver) && is_object($ldapserver) && method_exists($ldapserver,'haveAuthInfo')) {
277
 
        if ($ldapserver->haveAuthInfo() && isset($ldapserver->auth_type) && ! in_array($ldapserver->auth_type,array('config','http'))) {
278
 
                /**
279
 
                 * If time out value has been reached:
280
 
                 * - log out user
281
 
                 * - put $server_id in array of recently timed out servers
282
 
                 */
283
 
                if (function_exists('session_timed_out') && session_timed_out($ldapserver)) {
284
 
 
285
 
                        # If $session_timeout not defined, use ( session_cache_expire() - 1 )
286
 
                        $session_timeout = $ldapserver->session_timeout ? $ldapserver->session_timeout : session_cache_expire()-1;
287
 
 
288
 
                        system_message(array(
289
 
                                'title'=>_('Session Timed Out'),
290
 
                                'body'=>sprintf('%s %s %s',
291
 
                                        _('Your Session timed out after'),$session_timeout,
292
 
                                        _('min. of inactivity. You have been automatically logged out.')),
293
 
                                'type'=>'info'),'index.php');
294
 
                        die();
295
 
                }
296
 
        }
297
 
 
298
 
        # Update $_SESSION['activity'] for timeout and automatic logout feature
299
 
        if ($ldapserver->haveAuthInfo() && function_exists('set_lastactivity'))
300
 
                set_lastactivity($ldapserver);
 
313
if (! $app['server']->isSessionValid()) {
 
314
        system_message(array(
 
315
                'title'=>_('Session Timed Out'),
 
316
                'body'=>sprintf('%s %s %s',
 
317
                        _('Your Session timed out after'),$app['server']->getValue('login','timeout'),
 
318
                        _('min. of inactivity. You have been automatically logged out.')),
 
319
                'type'=>'info'),sprintf('index.php?server_id=%s&refresh=SID_%s',$app['server']->getIndex(),$app['server']->getIndex()));
 
320
 
 
321
        die();
301
322
}
302
323
 
 
324
# If syslog is enabled, we need to include the supporting file.
 
325
if ($_SESSION[APPCONFIG]->getValue('debug','syslog'))
 
326
        require LIBDIR.'syslog.php';
 
327
 
303
328
/**
304
329
 * At this point we have read all our additional function PHP files and our configuration.
305
330
 * If we are using hooks, run the session_init hook.