adm/index.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 02:49:04 +0400
changeset 19 6d3ca1857104
parent 0 ceef6d64c21d
permissions -rw-r--r--
RU translate fix
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package acp
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
*/
indvdum@0
    13
define('IN_PHPBB', true);
indvdum@0
    14
define('ADMIN_START', true);
indvdum@0
    15
define('NEED_SID', true);
indvdum@0
    16
indvdum@0
    17
// Include files
indvdum@0
    18
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
indvdum@0
    19
$phpEx = substr(strrchr(__FILE__, '.'), 1);
indvdum@0
    20
require($phpbb_root_path . 'common.' . $phpEx);
indvdum@0
    21
require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
indvdum@0
    22
require($phpbb_root_path . 'includes/functions_module.' . $phpEx);
indvdum@0
    23
indvdum@0
    24
// Start session management
indvdum@0
    25
$user->session_begin();
indvdum@0
    26
$auth->acl($user->data);
indvdum@0
    27
$user->setup('acp/common');
indvdum@0
    28
// End session management
indvdum@0
    29
indvdum@0
    30
// Have they authenticated (again) as an admin for this session?
indvdum@0
    31
if (!isset($user->data['session_admin']) || !$user->data['session_admin'])
indvdum@0
    32
{
indvdum@0
    33
	login_box('', $user->lang['LOGIN_ADMIN_CONFIRM'], $user->lang['LOGIN_ADMIN_SUCCESS'], true, false);
indvdum@0
    34
}
indvdum@0
    35
indvdum@0
    36
// Is user any type of admin? No, then stop here, each script needs to
indvdum@0
    37
// check specific permissions but this is a catchall
indvdum@0
    38
if (!$auth->acl_get('a_'))
indvdum@0
    39
{
indvdum@0
    40
	trigger_error('NO_ADMIN');
indvdum@0
    41
}
indvdum@0
    42
indvdum@0
    43
// We define the admin variables now, because the user is now able to use the admin related features...
indvdum@0
    44
define('IN_ADMIN', true);
indvdum@0
    45
$phpbb_admin_path = (defined('PHPBB_ADMIN_PATH')) ? PHPBB_ADMIN_PATH : './';
indvdum@0
    46
indvdum@0
    47
// Some oft used variables
indvdum@0
    48
$safe_mode		= (@ini_get('safe_mode') == '1' || strtolower(@ini_get('safe_mode')) === 'on') ? true : false;
indvdum@0
    49
$file_uploads	= (@ini_get('file_uploads') == '1' || strtolower(@ini_get('file_uploads')) === 'on') ? true : false;
indvdum@0
    50
$module_id		= request_var('i', '');
indvdum@0
    51
$mode			= request_var('mode', '');
indvdum@0
    52
indvdum@0
    53
// Set custom template for admin area
indvdum@0
    54
$template->set_custom_template($phpbb_admin_path . 'style', 'admin');
indvdum@0
    55
$template->assign_var('T_TEMPLATE_PATH', $phpbb_admin_path . 'style');
indvdum@0
    56
indvdum@0
    57
// the acp template is never stored in the database
indvdum@0
    58
$user->theme['template_storedb'] = false;
indvdum@0
    59
indvdum@0
    60
// Instantiate new module
indvdum@0
    61
$module = new p_master();
indvdum@0
    62
indvdum@0
    63
// Instantiate module system and generate list of available modules
indvdum@0
    64
$module->list_modules('acp');
indvdum@0
    65
indvdum@0
    66
// Select the active module
indvdum@0
    67
$module->set_active($module_id, $mode);
indvdum@0
    68
indvdum@0
    69
// Assign data to the template engine for the list of modules
indvdum@0
    70
// We do this before loading the active module for correct menu display in trigger_error
indvdum@0
    71
$module->assign_tpl_vars(append_sid("{$phpbb_admin_path}index.$phpEx"));
indvdum@0
    72
indvdum@0
    73
// Load and execute the relevant module
indvdum@0
    74
$module->load_active();
indvdum@0
    75
indvdum@0
    76
// Generate the page
indvdum@0
    77
adm_page_header($module->get_page_title());
indvdum@0
    78
indvdum@0
    79
$template->set_filenames(array(
indvdum@0
    80
	'body' => $module->get_tpl_name(),
indvdum@0
    81
));
indvdum@0
    82
indvdum@0
    83
adm_page_footer();
indvdum@0
    84
indvdum@0
    85
/**
indvdum@0
    86
* Header for acp pages
indvdum@0
    87
*/
indvdum@0
    88
function adm_page_header($page_title)
indvdum@0
    89
{
indvdum@0
    90
	global $config, $db, $user, $template;
indvdum@0
    91
	global $phpbb_root_path, $phpbb_admin_path, $phpEx, $SID, $_SID;
indvdum@0
    92
indvdum@0
    93
	if (defined('HEADER_INC'))
indvdum@0
    94
	{
indvdum@0
    95
		return;
indvdum@0
    96
	}
indvdum@0
    97
indvdum@0
    98
	define('HEADER_INC', true);
indvdum@0
    99
indvdum@0
   100
	// gzip_compression
indvdum@0
   101
	if ($config['gzip_compress'])
indvdum@0
   102
	{
indvdum@0
   103
		if (@extension_loaded('zlib') && !headers_sent())
indvdum@0
   104
		{
indvdum@0
   105
			ob_start('ob_gzhandler');
indvdum@0
   106
		}
indvdum@0
   107
	}
indvdum@0
   108
indvdum@0
   109
	$template->assign_vars(array(
indvdum@0
   110
		'PAGE_TITLE'			=> $page_title,
indvdum@0
   111
		'USERNAME'				=> $user->data['username'],
indvdum@0
   112
indvdum@0
   113
		'SID'					=> $SID,
indvdum@0
   114
		'_SID'					=> $_SID,
indvdum@0
   115
		'SESSION_ID'			=> $user->session_id,
indvdum@0
   116
		'ROOT_PATH'				=> $phpbb_admin_path,
indvdum@0
   117
indvdum@0
   118
		'U_LOGOUT'				=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=logout'),
indvdum@0
   119
		'U_ADM_LOGOUT'			=> append_sid("{$phpbb_admin_path}index.$phpEx", 'action=admlogout'),
indvdum@0
   120
		'U_ADM_INDEX'			=> append_sid("{$phpbb_admin_path}index.$phpEx"),
indvdum@0
   121
		'U_INDEX'				=> append_sid("{$phpbb_root_path}index.$phpEx"),
indvdum@0
   122
indvdum@0
   123
		'T_IMAGES_PATH'			=> "{$phpbb_root_path}images/",
indvdum@0
   124
		'T_SMILIES_PATH'		=> "{$phpbb_root_path}{$config['smilies_path']}/",
indvdum@0
   125
		'T_AVATAR_PATH'			=> "{$phpbb_root_path}{$config['avatar_path']}/",
indvdum@0
   126
		'T_AVATAR_GALLERY_PATH'	=> "{$phpbb_root_path}{$config['avatar_gallery_path']}/",
indvdum@0
   127
		'T_ICONS_PATH'			=> "{$phpbb_root_path}{$config['icons_path']}/",
indvdum@0
   128
		'T_RANKS_PATH'			=> "{$phpbb_root_path}{$config['ranks_path']}/",
indvdum@0
   129
		'T_UPLOAD_PATH'			=> "{$phpbb_root_path}{$config['upload_path']}/",
indvdum@0
   130
indvdum@0
   131
		'ICON_MOVE_UP'				=> '<img src="' . $phpbb_admin_path . 'images/icon_up.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
indvdum@0
   132
		'ICON_MOVE_UP_DISABLED'		=> '<img src="' . $phpbb_admin_path . 'images/icon_up_disabled.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
indvdum@0
   133
		'ICON_MOVE_DOWN'			=> '<img src="' . $phpbb_admin_path . 'images/icon_down.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
indvdum@0
   134
		'ICON_MOVE_DOWN_DISABLED'	=> '<img src="' . $phpbb_admin_path . 'images/icon_down_disabled.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
indvdum@0
   135
		'ICON_EDIT'					=> '<img src="' . $phpbb_admin_path . 'images/icon_edit.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
indvdum@0
   136
		'ICON_EDIT_DISABLED'		=> '<img src="' . $phpbb_admin_path . 'images/icon_edit_disabled.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
indvdum@0
   137
		'ICON_DELETE'				=> '<img src="' . $phpbb_admin_path . 'images/icon_delete.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
indvdum@0
   138
		'ICON_DELETE_DISABLED'		=> '<img src="' . $phpbb_admin_path . 'images/icon_delete_disabled.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
indvdum@0
   139
		'ICON_SYNC'					=> '<img src="' . $phpbb_admin_path . 'images/icon_sync.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
indvdum@0
   140
		'ICON_SYNC_DISABLED'		=> '<img src="' . $phpbb_admin_path . 'images/icon_sync_disabled.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
indvdum@0
   141
indvdum@0
   142
		'S_USER_LANG'			=> $user->lang['USER_LANG'],
indvdum@0
   143
		'S_CONTENT_DIRECTION'	=> $user->lang['DIRECTION'],
indvdum@0
   144
		'S_CONTENT_ENCODING'	=> 'UTF-8',
indvdum@0
   145
		'S_CONTENT_FLOW_BEGIN'	=> ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right',
indvdum@0
   146
		'S_CONTENT_FLOW_END'	=> ($user->lang['DIRECTION'] == 'ltr') ? 'right' : 'left',
indvdum@0
   147
	));
indvdum@0
   148
indvdum@0
   149
	// application/xhtml+xml not used because of IE
indvdum@0
   150
	header('Content-type: text/html; charset=UTF-8');
indvdum@0
   151
indvdum@0
   152
	header('Cache-Control: private, no-cache="set-cookie"');
indvdum@0
   153
	header('Expires: 0');
indvdum@0
   154
	header('Pragma: no-cache');
indvdum@0
   155
indvdum@0
   156
	return;
indvdum@0
   157
}
indvdum@0
   158
indvdum@0
   159
/**
indvdum@0
   160
* Page footer for acp pages
indvdum@0
   161
*/
indvdum@0
   162
function adm_page_footer($copyright_html = true)
indvdum@0
   163
{
indvdum@0
   164
	global $db, $config, $template, $user, $auth, $cache;
indvdum@0
   165
	global $starttime, $phpbb_root_path, $phpbb_admin_path, $phpEx;
indvdum@0
   166
indvdum@0
   167
	// Output page creation time
indvdum@0
   168
	if (defined('DEBUG'))
indvdum@0
   169
	{
indvdum@0
   170
		$mtime = explode(' ', microtime());
indvdum@0
   171
		$totaltime = $mtime[0] + $mtime[1] - $starttime;
indvdum@0
   172
indvdum@0
   173
		if (!empty($_REQUEST['explain']) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report'))
indvdum@0
   174
		{
indvdum@0
   175
			$db->sql_report('display');
indvdum@0
   176
		}
indvdum@0
   177
indvdum@0
   178
		$debug_output = sprintf('Time : %.3fs | ' . $db->sql_num_queries() . ' Queries | GZIP : ' . (($config['gzip_compress']) ? 'On' : 'Off') . (($user->load) ? ' | Load : ' . $user->load : ''), $totaltime);
indvdum@0
   179
indvdum@0
   180
		if ($auth->acl_get('a_') && defined('DEBUG_EXTRA'))
indvdum@0
   181
		{
indvdum@0
   182
			if (function_exists('memory_get_usage'))
indvdum@0
   183
			{
indvdum@0
   184
				if ($memory_usage = memory_get_usage())
indvdum@0
   185
				{
indvdum@0
   186
					global $base_memory_usage;
indvdum@0
   187
					$memory_usage -= $base_memory_usage;
indvdum@0
   188
					$memory_usage = get_formatted_filesize($memory_usage);
indvdum@0
   189
indvdum@0
   190
					$debug_output .= ' | Memory Usage: ' . $memory_usage;
indvdum@0
   191
				}
indvdum@0
   192
			}
indvdum@0
   193
indvdum@0
   194
			$debug_output .= ' | <a href="' . build_url() . '&amp;explain=1">Explain</a>';
indvdum@0
   195
		}
indvdum@0
   196
	}
indvdum@0
   197
indvdum@0
   198
	$template->assign_vars(array(
indvdum@0
   199
		'DEBUG_OUTPUT'		=> (defined('DEBUG')) ? $debug_output : '',
indvdum@0
   200
		'TRANSLATION_INFO'	=> (!empty($user->lang['TRANSLATION_INFO'])) ? $user->lang['TRANSLATION_INFO'] : '',
indvdum@0
   201
		'S_COPYRIGHT_HTML'	=> $copyright_html,
indvdum@0
   202
		'VERSION'			=> $config['version'])
indvdum@0
   203
	);
indvdum@0
   204
indvdum@0
   205
	$template->display('body');
indvdum@0
   206
indvdum@0
   207
	garbage_collection();
indvdum@0
   208
	exit_handler();
indvdum@0
   209
}
indvdum@0
   210
indvdum@0
   211
/**
indvdum@0
   212
* Generate back link for acp pages
indvdum@0
   213
*/
indvdum@0
   214
function adm_back_link($u_action)
indvdum@0
   215
{
indvdum@0
   216
	global $user;
indvdum@0
   217
	return '<br /><br /><a href="' . $u_action . '">&laquo; ' . $user->lang['BACK_TO_PREV'] . '</a>';
indvdum@0
   218
}
indvdum@0
   219
indvdum@0
   220
/**
indvdum@0
   221
* Build select field options in acp pages
indvdum@0
   222
*/
indvdum@0
   223
function build_select($option_ary, $option_default = false)
indvdum@0
   224
{
indvdum@0
   225
	global $user;
indvdum@0
   226
indvdum@0
   227
	$html = '';
indvdum@0
   228
	foreach ($option_ary as $value => $title)
indvdum@0
   229
	{
indvdum@0
   230
		$selected = ($option_default !== false && $value == $option_default) ? ' selected="selected"' : '';
indvdum@0
   231
		$html .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$title] . '</option>';
indvdum@0
   232
	}
indvdum@0
   233
indvdum@0
   234
	return $html;
indvdum@0
   235
}
indvdum@0
   236
indvdum@0
   237
/**
indvdum@0
   238
* Build radio fields in acp pages
indvdum@0
   239
*/
indvdum@0
   240
function h_radio($name, $input_ary, $input_default = false, $id = false, $key = false, $separator = '')
indvdum@0
   241
{
indvdum@0
   242
	global $user;
indvdum@0
   243
indvdum@0
   244
	$html = '';
indvdum@0
   245
	$id_assigned = false;
indvdum@0
   246
	foreach ($input_ary as $value => $title)
indvdum@0
   247
	{
indvdum@0
   248
		$selected = ($input_default !== false && $value == $input_default) ? ' checked="checked"' : '';
indvdum@0
   249
		$html .= '<label><input type="radio" name="' . $name . '"' . (($id && !$id_assigned) ? ' id="' . $id . '"' : '') . ' value="' . $value . '"' . $selected . (($key) ? ' accesskey="' . $key . '"' : '') . ' class="radio" /> ' . $user->lang[$title] . '</label>' . $separator;
indvdum@0
   250
		$id_assigned = true;
indvdum@0
   251
	}
indvdum@0
   252
indvdum@0
   253
	return $html;
indvdum@0
   254
}
indvdum@0
   255
indvdum@0
   256
/**
indvdum@0
   257
* Build configuration template for acp configuration pages
indvdum@0
   258
*/
indvdum@0
   259
function build_cfg_template($tpl_type, $key, &$new, $config_key, $vars)
indvdum@0
   260
{
indvdum@0
   261
	global $user, $module;
indvdum@0
   262
indvdum@0
   263
	$tpl = '';
indvdum@0
   264
	$name = 'config[' . $config_key . ']';
indvdum@0
   265
indvdum@0
   266
	// Make sure there is no notice printed out for non-existent config options (we simply set them)
indvdum@0
   267
	if (!isset($new[$config_key]))
indvdum@0
   268
	{
indvdum@0
   269
		$new[$config_key] = '';
indvdum@0
   270
	}
indvdum@0
   271
indvdum@0
   272
	switch ($tpl_type[0])
indvdum@0
   273
	{
indvdum@0
   274
		case 'text':
indvdum@0
   275
		case 'password':
indvdum@0
   276
			$size = (int) $tpl_type[1];
indvdum@0
   277
			$maxlength = (int) $tpl_type[2];
indvdum@0
   278
indvdum@0
   279
			$tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new[$config_key] . '"' . (($tpl_type[0] === 'password') ?  ' autocomplete="off"' : '') . ' />';
indvdum@0
   280
		break;
indvdum@0
   281
indvdum@0
   282
		case 'dimension':
indvdum@0
   283
			$size = (int) $tpl_type[1];
indvdum@0
   284
			$maxlength = (int) $tpl_type[2];
indvdum@0
   285
indvdum@0
   286
			$tpl = '<input id="' . $key . '" type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_width]" value="' . $new[$config_key . '_width'] . '" /> x <input type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_height]" value="' . $new[$config_key . '_height'] . '" />';
indvdum@0
   287
		break;
indvdum@0
   288
indvdum@0
   289
		case 'textarea':
indvdum@0
   290
			$rows = (int) $tpl_type[1];
indvdum@0
   291
			$cols = (int) $tpl_type[2];
indvdum@0
   292
indvdum@0
   293
			$tpl = '<textarea id="' . $key . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $new[$config_key] . '</textarea>';
indvdum@0
   294
		break;
indvdum@0
   295
indvdum@0
   296
		case 'radio':
indvdum@0
   297
			$key_yes	= ($new[$config_key]) ? ' checked="checked"' : '';
indvdum@0
   298
			$key_no		= (!$new[$config_key]) ? ' checked="checked"' : '';
indvdum@0
   299
indvdum@0
   300
			$tpl_type_cond = explode('_', $tpl_type[1]);
indvdum@0
   301
			$type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
indvdum@0
   302
indvdum@0
   303
			$tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $user->lang['NO'] : $user->lang['DISABLED']) . '</label>';
indvdum@0
   304
			$tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $user->lang['YES'] : $user->lang['ENABLED']) . '</label>';
indvdum@0
   305
indvdum@0
   306
			$tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes;
indvdum@0
   307
		break;
indvdum@0
   308
indvdum@0
   309
		case 'select':
indvdum@0
   310
		case 'custom':
indvdum@0
   311
indvdum@0
   312
			$return = '';
indvdum@0
   313
indvdum@0
   314
			if (isset($vars['method']))
indvdum@0
   315
			{
indvdum@0
   316
				$call = array($module->module, $vars['method']);
indvdum@0
   317
			}
indvdum@0
   318
			else if (isset($vars['function']))
indvdum@0
   319
			{
indvdum@0
   320
				$call = $vars['function'];
indvdum@0
   321
			}
indvdum@0
   322
			else
indvdum@0
   323
			{
indvdum@0
   324
				break;
indvdum@0
   325
			}
indvdum@0
   326
indvdum@0
   327
			if (isset($vars['params']))
indvdum@0
   328
			{
indvdum@0
   329
				$args = array();
indvdum@0
   330
				foreach ($vars['params'] as $value)
indvdum@0
   331
				{
indvdum@0
   332
					switch ($value)
indvdum@0
   333
					{
indvdum@0
   334
						case '{CONFIG_VALUE}':
indvdum@0
   335
							$value = $new[$config_key];
indvdum@0
   336
						break;
indvdum@0
   337
indvdum@0
   338
						case '{KEY}':
indvdum@0
   339
							$value = $key;
indvdum@0
   340
						break;
indvdum@0
   341
					}
indvdum@0
   342
indvdum@0
   343
					$args[] = $value;
indvdum@0
   344
				}
indvdum@0
   345
			}
indvdum@0
   346
			else
indvdum@0
   347
			{
indvdum@0
   348
				$args = array($new[$config_key], $key);
indvdum@0
   349
			}
indvdum@0
   350
indvdum@0
   351
			$return = call_user_func_array($call, $args);
indvdum@0
   352
indvdum@0
   353
			if ($tpl_type[0] == 'select')
indvdum@0
   354
			{
indvdum@0
   355
				$tpl = '<select id="' . $key . '" name="' . $name . '">' . $return . '</select>';
indvdum@0
   356
			}
indvdum@0
   357
			else
indvdum@0
   358
			{
indvdum@0
   359
				$tpl = $return;
indvdum@0
   360
			}
indvdum@0
   361
indvdum@0
   362
		break;
indvdum@0
   363
indvdum@0
   364
		default:
indvdum@0
   365
		break;
indvdum@0
   366
	}
indvdum@0
   367
indvdum@0
   368
	if (isset($vars['append']))
indvdum@0
   369
	{
indvdum@0
   370
		$tpl .= $vars['append'];
indvdum@0
   371
	}
indvdum@0
   372
indvdum@0
   373
	return $tpl;
indvdum@0
   374
}
indvdum@0
   375
indvdum@0
   376
/**
indvdum@0
   377
* Going through a config array and validate values, writing errors to $error. The validation method  accepts parameters separated by ':' for string and int.
indvdum@0
   378
* The first parameter defines the type to be used, the second the lower bound and the third the upper bound. Only the type is required.
indvdum@0
   379
*/
indvdum@0
   380
function validate_config_vars($config_vars, &$cfg_array, &$error)
indvdum@0
   381
{
indvdum@0
   382
	global $phpbb_root_path, $user;
indvdum@0
   383
	$type	= 0;
indvdum@0
   384
	$min	= 1;
indvdum@0
   385
	$max	= 2;
indvdum@0
   386
indvdum@0
   387
	foreach ($config_vars as $config_name => $config_definition)
indvdum@0
   388
	{
indvdum@0
   389
		if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
indvdum@0
   390
		{
indvdum@0
   391
			continue;
indvdum@0
   392
		}
indvdum@0
   393
indvdum@0
   394
		if (!isset($config_definition['validate']))
indvdum@0
   395
		{
indvdum@0
   396
			continue;
indvdum@0
   397
		}
indvdum@0
   398
indvdum@0
   399
		$validator = explode(':', $config_definition['validate']);
indvdum@0
   400
indvdum@0
   401
		// Validate a bit. ;) (0 = type, 1 = min, 2= max)
indvdum@0
   402
		switch ($validator[$type])
indvdum@0
   403
		{
indvdum@0
   404
			case 'string':
indvdum@0
   405
				$length = utf8_strlen($cfg_array[$config_name]);
indvdum@0
   406
indvdum@0
   407
				// the column is a VARCHAR
indvdum@0
   408
				$validator[$max] = (isset($validator[$max])) ? min(255, $validator[$max]) : 255;
indvdum@0
   409
indvdum@0
   410
				if (isset($validator[$min]) && $length < $validator[$min])
indvdum@0
   411
				{
indvdum@0
   412
					$error[] = sprintf($user->lang['SETTING_TOO_SHORT'], $user->lang[$config_definition['lang']], $validator[$min]);
indvdum@0
   413
				}
indvdum@0
   414
				else if (isset($validator[$max]) && $length > $validator[2])
indvdum@0
   415
				{
indvdum@0
   416
					$error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$config_definition['lang']], $validator[$max]);
indvdum@0
   417
				}
indvdum@0
   418
			break;
indvdum@0
   419
indvdum@0
   420
			case 'bool':
indvdum@0
   421
				$cfg_array[$config_name] = ($cfg_array[$config_name]) ? 1 : 0;
indvdum@0
   422
			break;
indvdum@0
   423
indvdum@0
   424
			case 'int':
indvdum@0
   425
				$cfg_array[$config_name] = (int) $cfg_array[$config_name];
indvdum@0
   426
indvdum@0
   427
				if (isset($validator[$min]) && $cfg_array[$config_name] < $validator[$min])
indvdum@0
   428
				{
indvdum@0
   429
					$error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], $validator[$min]);
indvdum@0
   430
				}
indvdum@0
   431
				else if (isset($validator[$max]) && $cfg_array[$config_name] > $validator[$max])
indvdum@0
   432
				{
indvdum@0
   433
					$error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$config_definition['lang']], $validator[$max]);
indvdum@0
   434
				}
indvdum@0
   435
indvdum@0
   436
				if (strpos($config_name, '_max') !== false)
indvdum@0
   437
				{
indvdum@0
   438
					// Min/max pairs of settings should ensure that min <= max
indvdum@0
   439
					// Replace _max with _min to find the name of the minimum
indvdum@0
   440
					// corresponding configuration variable
indvdum@0
   441
					$min_name = str_replace('_max', '_min', $config_name);
indvdum@0
   442
indvdum@0
   443
					if (isset($cfg_array[$min_name]) && is_numeric($cfg_array[$min_name]) && $cfg_array[$config_name] < $cfg_array[$min_name])
indvdum@0
   444
					{
indvdum@0
   445
						// A minimum value exists and the maximum value is less than it
indvdum@0
   446
						$error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], (int) $cfg_array[$min_name]);
indvdum@0
   447
					}
indvdum@0
   448
				}
indvdum@0
   449
			break;
indvdum@0
   450
indvdum@0
   451
			// Absolute path
indvdum@0
   452
			case 'script_path':
indvdum@0
   453
				if (!$cfg_array[$config_name])
indvdum@0
   454
				{
indvdum@0
   455
					break;
indvdum@0
   456
				}
indvdum@0
   457
indvdum@0
   458
				$destination = str_replace('\\', '/', $cfg_array[$config_name]);
indvdum@0
   459
indvdum@0
   460
				if ($destination !== '/')
indvdum@0
   461
				{
indvdum@0
   462
					// Adjust destination path (no trailing slash)
indvdum@0
   463
					if (substr($destination, -1, 1) == '/')
indvdum@0
   464
					{
indvdum@0
   465
						$destination = substr($destination, 0, -1);
indvdum@0
   466
					}
indvdum@0
   467
indvdum@0
   468
					$destination = str_replace(array('../', './'), '', $destination);
indvdum@0
   469
indvdum@0
   470
					if ($destination[0] != '/')
indvdum@0
   471
					{
indvdum@0
   472
						$destination = '/' . $destination;
indvdum@0
   473
					}
indvdum@0
   474
				}
indvdum@0
   475
indvdum@0
   476
				$cfg_array[$config_name] = trim($destination);
indvdum@0
   477
indvdum@0
   478
			break;
indvdum@0
   479
indvdum@0
   480
			// Absolute path
indvdum@0
   481
			case 'lang':
indvdum@0
   482
				if (!$cfg_array[$config_name])
indvdum@0
   483
				{
indvdum@0
   484
					break;
indvdum@0
   485
				}
indvdum@0
   486
indvdum@0
   487
				$cfg_array[$config_name] = basename($cfg_array[$config_name]);
indvdum@0
   488
indvdum@0
   489
				if (!file_exists($phpbb_root_path . 'language/' . $cfg_array[$config_name] . '/'))
indvdum@0
   490
				{
indvdum@0
   491
					$error[] = $user->lang['WRONG_DATA_LANG'];
indvdum@0
   492
				}
indvdum@0
   493
			break;
indvdum@0
   494
indvdum@0
   495
			// Relative path (appended $phpbb_root_path)
indvdum@0
   496
			case 'rpath':
indvdum@0
   497
			case 'rwpath':
indvdum@0
   498
				if (!$cfg_array[$config_name])
indvdum@0
   499
				{
indvdum@0
   500
					break;
indvdum@0
   501
				}
indvdum@0
   502
indvdum@0
   503
				$destination = $cfg_array[$config_name];
indvdum@0
   504
indvdum@0
   505
				// Adjust destination path (no trailing slash)
indvdum@0
   506
				if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
indvdum@0
   507
				{
indvdum@0
   508
					$destination = substr($destination, 0, -1);
indvdum@0
   509
				}
indvdum@0
   510
indvdum@0
   511
				$destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
indvdum@0
   512
				if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
indvdum@0
   513
				{
indvdum@0
   514
					$destination = '';
indvdum@0
   515
				}
indvdum@0
   516
indvdum@0
   517
				$cfg_array[$config_name] = trim($destination);
indvdum@0
   518
indvdum@0
   519
			// Path being relative (still prefixed by phpbb_root_path), but with the ability to escape the root dir...
indvdum@0
   520
			case 'path':
indvdum@0
   521
			case 'wpath':
indvdum@0
   522
indvdum@0
   523
				if (!$cfg_array[$config_name])
indvdum@0
   524
				{
indvdum@0
   525
					break;
indvdum@0
   526
				}
indvdum@0
   527
indvdum@0
   528
				$cfg_array[$config_name] = trim($cfg_array[$config_name]);
indvdum@0
   529
indvdum@0
   530
				// Make sure no NUL byte is present...
indvdum@0
   531
				if (strpos($cfg_array[$config_name], "\0") !== false || strpos($cfg_array[$config_name], '%00') !== false)
indvdum@0
   532
				{
indvdum@0
   533
					$cfg_array[$config_name] = '';
indvdum@0
   534
					break;
indvdum@0
   535
				}
indvdum@0
   536
indvdum@0
   537
				if (!file_exists($phpbb_root_path . $cfg_array[$config_name]))
indvdum@0
   538
				{
indvdum@0
   539
					$error[] = sprintf($user->lang['DIRECTORY_DOES_NOT_EXIST'], $cfg_array[$config_name]);
indvdum@0
   540
				}
indvdum@0
   541
indvdum@0
   542
				if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !is_dir($phpbb_root_path . $cfg_array[$config_name]))
indvdum@0
   543
				{
indvdum@0
   544
					$error[] = sprintf($user->lang['DIRECTORY_NOT_DIR'], $cfg_array[$config_name]);
indvdum@0
   545
				}
indvdum@0
   546
indvdum@0
   547
				// Check if the path is writable
indvdum@0
   548
				if ($config_definition['validate'] == 'wpath' || $config_definition['validate'] == 'rwpath')
indvdum@0
   549
				{
indvdum@0
   550
					if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !phpbb_is_writable($phpbb_root_path . $cfg_array[$config_name]))
indvdum@0
   551
					{
indvdum@0
   552
						$error[] = sprintf($user->lang['DIRECTORY_NOT_WRITABLE'], $cfg_array[$config_name]);
indvdum@0
   553
					}
indvdum@0
   554
				}
indvdum@0
   555
indvdum@0
   556
			break;
indvdum@0
   557
		}
indvdum@0
   558
	}
indvdum@0
   559
indvdum@0
   560
	return;
indvdum@0
   561
}
indvdum@0
   562
indvdum@0
   563
/**
indvdum@0
   564
* Checks whatever or not a variable is OK for use in the Database
indvdum@0
   565
* param mixed $value_ary An array of the form array(array('lang' => ..., 'value' => ..., 'column_type' =>))'
indvdum@0
   566
* param mixed $error The error array
indvdum@0
   567
*/
indvdum@0
   568
function validate_range($value_ary, &$error)
indvdum@0
   569
{
indvdum@0
   570
	global $user;
indvdum@0
   571
indvdum@0
   572
	$column_types = array(
indvdum@0
   573
		'BOOL'	=> array('php_type' => 'int', 		'min' => 0, 				'max' => 1),
indvdum@0
   574
		'USINT'	=> array('php_type' => 'int',		'min' => 0, 				'max' => 65535),
indvdum@0
   575
		'UINT'	=> array('php_type' => 'int', 		'min' => 0, 				'max' => (int) 0x7fffffff),
indvdum@0
   576
		// Do not use (int) 0x80000000 - it evaluates to different
indvdum@0
   577
		// values on 32-bit and 64-bit systems.
indvdum@0
   578
		// Apparently -2147483648 is a float on 32-bit systems,
indvdum@0
   579
		// despite fitting in an int, thus explicit cast is needed.
indvdum@0
   580
		'INT'	=> array('php_type' => 'int', 		'min' => (int) -2147483648,	'max' => (int) 0x7fffffff),
indvdum@0
   581
		'TINT'	=> array('php_type' => 'int',		'min' => -128,				'max' => 127),
indvdum@0
   582
indvdum@0
   583
		'VCHAR'	=> array('php_type' => 'string', 	'min' => 0, 				'max' => 255),
indvdum@0
   584
	);
indvdum@0
   585
	foreach ($value_ary as $value)
indvdum@0
   586
	{
indvdum@0
   587
		$column = explode(':', $value['column_type']);
indvdum@0
   588
		$max = $min = 0;
indvdum@0
   589
		$type = 0;
indvdum@0
   590
		if (!isset($column_types[$column[0]]))
indvdum@0
   591
		{
indvdum@0
   592
			continue;
indvdum@0
   593
		}
indvdum@0
   594
		else
indvdum@0
   595
		{
indvdum@0
   596
			$type = $column_types[$column[0]];
indvdum@0
   597
		}
indvdum@0
   598
indvdum@0
   599
		switch ($type['php_type'])
indvdum@0
   600
		{
indvdum@0
   601
			case 'string' :
indvdum@0
   602
				$max = (isset($column[1])) ? min($column[1],$type['max']) : $type['max'];
indvdum@0
   603
				if (utf8_strlen($value['value']) > $max)
indvdum@0
   604
				{
indvdum@0
   605
					$error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$value['lang']], $max);
indvdum@0
   606
				}
indvdum@0
   607
			break;
indvdum@0
   608
indvdum@0
   609
			case 'int':
indvdum@0
   610
				$min = (isset($column[1])) ? max($column[1],$type['min']) : $type['min'];
indvdum@0
   611
				$max = (isset($column[2])) ? min($column[2],$type['max']) : $type['max'];
indvdum@0
   612
				if ($value['value'] < $min)
indvdum@0
   613
				{
indvdum@0
   614
					$error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$value['lang']], $min);
indvdum@0
   615
				}
indvdum@0
   616
				else if ($value['value'] > $max)
indvdum@0
   617
				{
indvdum@0
   618
					$error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$value['lang']], $max);
indvdum@0
   619
				}
indvdum@0
   620
			break;
indvdum@0
   621
		}
indvdum@0
   622
	}
indvdum@0
   623
}
indvdum@0
   624
indvdum@0
   625
?>