Chris@76: $txt['displayedValue'])), Chris@76: Note that just saying array('first', 'second') will put 0 in the SQL for 'first'. Chris@76: Chris@76: * A password input box. Used for passwords, no less! Chris@76: ie. array('password', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'), Chris@76: Chris@76: * A permission - for picking groups who have a permission. Chris@76: ie. array('permissions', 'manage_groups'), Chris@76: Chris@76: * A BBC selection box. Chris@76: ie. array('bbc', 'sig_bbc'), Chris@76: Chris@76: For each option: Chris@76: type (see above), variable name, size/possible values. Chris@76: OR make type '' for an empty string for a horizontal rule. Chris@76: SET preinput - to put some HTML prior to the input box. Chris@76: SET postinput - to put some HTML following the input box. Chris@76: SET invalid - to mark the data as invalid. Chris@76: PLUS You can override label and help parameters by forcing their keys in the array, for example: Chris@76: array('text', 'invalidlabel', 3, 'label' => 'Actual Label') */ Chris@76: Chris@76: // This is the main pass through function, it creates tabs and the like. Chris@76: function ModifySettings() Chris@76: { Chris@76: global $context, $txt, $scripturl, $boarddir; Chris@76: Chris@76: // This is just to keep the database password more secure. Chris@76: isAllowedTo('admin_forum'); Chris@76: Chris@76: // Load up all the tabs... Chris@76: $context[$context['admin_menu_name']]['tab_data'] = array( Chris@76: 'title' => $txt['admin_server_settings'], Chris@76: 'help' => 'serversettings', Chris@76: 'description' => $txt['admin_basic_settings'], Chris@76: ); Chris@76: Chris@76: checkSession('request'); Chris@76: Chris@76: // The settings are in here, I swear! Chris@76: loadLanguage('ManageSettings'); Chris@76: Chris@76: $context['page_title'] = $txt['admin_server_settings']; Chris@76: $context['sub_template'] = 'show_settings'; Chris@76: Chris@76: $subActions = array( Chris@76: 'general' => 'ModifyGeneralSettings', Chris@76: 'database' => 'ModifyDatabaseSettings', Chris@76: 'cookie' => 'ModifyCookieSettings', Chris@76: 'cache' => 'ModifyCacheSettings', Chris@76: 'loads' => 'ModifyLoadBalancingSettings', Chris@76: ); Chris@76: Chris@76: // By default we're editing the core settings Chris@76: $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'general'; Chris@76: $context['sub_action'] = $_REQUEST['sa']; Chris@76: Chris@76: // Warn the user if there's any relevant information regarding Settings.php. Chris@76: if ($_REQUEST['sa'] != 'cache') Chris@76: { Chris@76: // Warn the user if the backup of Settings.php failed. Chris@76: $settings_not_writable = !is_writable($boarddir . '/Settings.php'); Chris@76: $settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php'); Chris@76: Chris@76: if ($settings_not_writable) Chris@76: $context['settings_message'] = '
' . $txt['settings_not_writable'] . '

'; Chris@76: elseif ($settings_backup_fail) Chris@76: $context['settings_message'] = '
' . $txt['admin_backup_fail'] . '

'; Chris@76: Chris@76: $context['settings_not_writable'] = $settings_not_writable; Chris@76: } Chris@76: Chris@76: // Call the right function for this sub-action. Chris@76: $subActions[$_REQUEST['sa']](); Chris@76: } Chris@76: Chris@76: // General forum settings - forum name, maintenance mode, etc. Chris@76: function ModifyGeneralSettings($return_config = false) Chris@76: { Chris@76: global $scripturl, $context, $txt; Chris@76: Chris@76: /* If you're writing a mod, it's a bad idea to add things here.... Chris@76: For each option: Chris@76: variable name, description, type (constant), size/possible values, helptext. Chris@76: OR an empty string for a horizontal rule. Chris@76: OR a string for a titled section. */ Chris@76: $config_vars = array( Chris@76: array('mbname', $txt['admin_title'], 'file', 'text', 30), Chris@76: '', Chris@76: array('maintenance', $txt['admin_maintain'], 'file', 'check'), Chris@76: array('mtitle', $txt['maintenance_subject'], 'file', 'text', 36), Chris@76: array('mmessage', $txt['maintenance_message'], 'file', 'text', 36), Chris@76: '', Chris@76: array('webmaster_email', $txt['admin_webmaster_email'], 'file', 'text', 30), Chris@76: '', Chris@76: array('enableCompressedOutput', $txt['enableCompressedOutput'], 'db', 'check', null, 'enableCompressedOutput'), Chris@76: array('disableTemplateEval', $txt['disableTemplateEval'], 'db', 'check', null, 'disableTemplateEval'), Chris@76: array('disableHostnameLookup', $txt['disableHostnameLookup'], 'db', 'check', null, 'disableHostnameLookup'), Chris@76: ); Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: // Setup the template stuff. Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=general;save'; Chris@76: $context['settings_title'] = $txt['general_settings']; Chris@76: Chris@76: // Saving settings? Chris@76: if (isset($_REQUEST['save'])) Chris@76: { Chris@76: saveSettings($config_vars); Chris@76: redirectexit('action=admin;area=serversettings;sa=general;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: // Fill the config array. Chris@76: prepareServerSettingsContext($config_vars); Chris@76: } Chris@76: Chris@76: // Basic database and paths settings - database name, host, etc. Chris@76: function ModifyDatabaseSettings($return_config = false) Chris@76: { Chris@76: global $scripturl, $context, $settings, $txt, $boarddir; Chris@76: Chris@76: /* If you're writing a mod, it's a bad idea to add things here.... Chris@76: For each option: Chris@76: variable name, description, type (constant), size/possible values, helptext. Chris@76: OR an empty string for a horizontal rule. Chris@76: OR a string for a titled section. */ Chris@76: $config_vars = array( Chris@76: array('db_server', $txt['database_server'], 'file', 'text'), Chris@76: array('db_user', $txt['database_user'], 'file', 'text'), Chris@76: array('db_passwd', $txt['database_password'], 'file', 'password'), Chris@76: array('db_name', $txt['database_name'], 'file', 'text'), Chris@76: array('db_prefix', $txt['database_prefix'], 'file', 'text'), Chris@76: array('db_persist', $txt['db_persist'], 'file', 'check', null, 'db_persist'), Chris@76: array('db_error_send', $txt['db_error_send'], 'file', 'check'), Chris@76: array('ssi_db_user', $txt['ssi_db_user'], 'file', 'text', null, 'ssi_db_user'), Chris@76: array('ssi_db_passwd', $txt['ssi_db_passwd'], 'file', 'password'), Chris@76: '', Chris@76: array('autoFixDatabase', $txt['autoFixDatabase'], 'db', 'check', false, 'autoFixDatabase'), Chris@76: array('autoOptMaxOnline', $txt['autoOptMaxOnline'], 'db', 'int'), Chris@76: '', Chris@76: array('boardurl', $txt['admin_url'], 'file', 'text', 36), Chris@76: array('boarddir', $txt['boarddir'], 'file', 'text', 36), Chris@76: array('sourcedir', $txt['sourcesdir'], 'file', 'text', 36), Chris@76: array('cachedir', $txt['cachedir'], 'file', 'text', 36), Chris@76: ); Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: // Setup the template stuff. Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=database;save'; Chris@76: $context['settings_title'] = $txt['database_paths_settings']; Chris@76: $context['save_disabled'] = $context['settings_not_writable']; Chris@76: Chris@76: // Saving settings? Chris@76: if (isset($_REQUEST['save'])) Chris@76: { Chris@76: saveSettings($config_vars); Chris@76: redirectexit('action=admin;area=serversettings;sa=database;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: // Fill the config array. Chris@76: prepareServerSettingsContext($config_vars); Chris@76: } Chris@76: Chris@76: // This function basically edits anything which is configuration and stored in the database, except for caching. Chris@76: function ModifyCookieSettings($return_config = false) Chris@76: { Chris@76: global $context, $scripturl, $txt, $sourcedir, $modSettings, $cookiename, $user_settings; Chris@76: Chris@76: // Define the variables we want to edit. Chris@76: $config_vars = array( Chris@76: // Cookies... Chris@76: array('cookiename', $txt['cookie_name'], 'file', 'text', 20), Chris@76: array('cookieTime', $txt['cookieTime'], 'db', 'int'), Chris@76: array('localCookies', $txt['localCookies'], 'db', 'check', false, 'localCookies'), Chris@76: array('globalCookies', $txt['globalCookies'], 'db', 'check', false, 'globalCookies'), Chris@76: array('secureCookies', $txt['secureCookies'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == '1')), Chris@76: '', Chris@76: // Sessions Chris@76: array('databaseSession_enable', $txt['databaseSession_enable'], 'db', 'check', false, 'databaseSession_enable'), Chris@76: array('databaseSession_loose', $txt['databaseSession_loose'], 'db', 'check', false, 'databaseSession_loose'), Chris@76: array('databaseSession_lifetime', $txt['databaseSession_lifetime'], 'db', 'int', false, 'databaseSession_lifetime'), Chris@76: ); Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cookie;save'; Chris@76: $context['settings_title'] = $txt['cookies_sessions_settings']; Chris@76: Chris@76: // Saving settings? Chris@76: if (isset($_REQUEST['save'])) Chris@76: { Chris@76: saveSettings($config_vars); Chris@76: Chris@76: // If the cookie name was changed, reset the cookie. Chris@76: if ($cookiename != $_POST['cookiename']) Chris@76: { Chris@76: $original_session_id = $context['session_id']; Chris@76: include_once($sourcedir . '/Subs-Auth.php'); Chris@76: Chris@76: // Remove the old cookie. Chris@76: setLoginCookie(-3600, 0); Chris@76: Chris@76: // Set the new one. Chris@76: $cookiename = $_POST['cookiename']; Chris@76: setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], sha1($user_settings['passwd'] . $user_settings['password_salt'])); Chris@76: Chris@76: redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $original_session_id, $context['server']['needs_login_fix']); Chris@76: } Chris@76: Chris@76: redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: // Fill the config array. Chris@76: prepareServerSettingsContext($config_vars); Chris@76: } Chris@76: Chris@76: // Simply modifying cache functions Chris@76: function ModifyCacheSettings($return_config = false) Chris@76: { Chris@76: global $context, $scripturl, $txt, $helptxt, $modSettings; Chris@76: Chris@76: // Define the variables we want to edit. Chris@76: $config_vars = array( Chris@76: // Only a couple of settings, but they are important Chris@76: array('select', 'cache_enable', array($txt['cache_off'], $txt['cache_level1'], $txt['cache_level2'], $txt['cache_level3'])), Chris@76: array('text', 'cache_memcached'), Chris@76: ); Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: // Saving again? Chris@76: if (isset($_GET['save'])) Chris@76: { Chris@76: saveDBSettings($config_vars); Chris@76: Chris@76: // We have to manually force the clearing of the cache otherwise the changed settings might not get noticed. Chris@76: $modSettings['cache_enable'] = 1; Chris@76: cache_put_data('modSettings', null, 90); Chris@76: Chris@76: redirectexit('action=admin;area=serversettings;sa=cache;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cache;save'; Chris@76: $context['settings_title'] = $txt['caching_settings']; Chris@76: $context['settings_message'] = $txt['caching_information']; Chris@76: Chris@76: // Detect an optimizer? Chris@76: if (function_exists('eaccelerator_put')) Chris@76: $detected = 'eAccelerator'; Chris@76: elseif (function_exists('mmcache_put')) Chris@76: $detected = 'MMCache'; Chris@76: elseif (function_exists('apc_store')) Chris@76: $detected = 'APC'; Chris@76: elseif (function_exists('output_cache_put')) Chris@76: $detected = 'Zend'; Chris@76: elseif (function_exists('memcache_set')) Chris@76: $detected = 'Memcached'; Chris@76: elseif (function_exists('xcache_set')) Chris@76: $detected = 'XCache'; Chris@76: else Chris@76: $detected = 'no_caching'; Chris@76: Chris@76: $context['settings_message'] = sprintf($context['settings_message'], $txt['detected_' . $detected]); Chris@76: Chris@76: // Prepare the template. Chris@76: prepareDBSettingContext($config_vars); Chris@76: } Chris@76: Chris@76: function ModifyLoadBalancingSettings($return_config = false) Chris@76: { Chris@76: global $txt, $scripturl, $context, $settings, $modSettings; Chris@76: Chris@76: // Setup a warning message, but disabled by default. Chris@76: $disabled = true; Chris@76: $context['settings_message'] = $txt['loadavg_disabled_conf']; Chris@76: Chris@76: if (strpos(strtolower(PHP_OS), 'win') === 0) Chris@76: $context['settings_message'] = $txt['loadavg_disabled_windows']; Chris@76: else Chris@76: { Chris@76: $modSettings['load_average'] = @file_get_contents('/proc/loadavg'); Chris@76: if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) !== 0) Chris@76: $modSettings['load_average'] = (float) $matches[1]; Chris@76: elseif (($modSettings['load_average'] = @`uptime`) !== null && preg_match('~load averages?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) !== 0) Chris@76: $modSettings['load_average'] = (float) $matches[1]; Chris@76: else Chris@76: unset($modSettings['load_average']); Chris@76: Chris@76: if (!empty($modSettings['load_average'])) Chris@76: { Chris@76: $context['settings_message'] = sprintf($txt['loadavg_warning'], $modSettings['load_average']); Chris@76: $disabled = false; Chris@76: } Chris@76: } Chris@76: Chris@76: // Start with a simple checkbox. Chris@76: $config_vars = array( Chris@76: array('check', 'loadavg_enable'), Chris@76: ); Chris@76: Chris@76: // Set the default values for each option. Chris@76: $default_values = array( Chris@76: 'loadavg_auto_opt' => '1.0', Chris@76: 'loadavg_search' => '2.5', Chris@76: 'loadavg_allunread' => '2.0', Chris@76: 'loadavg_unreadreplies' => '3.5', Chris@76: 'loadavg_show_posts' => '2.0', Chris@76: 'loadavg_forum' => '40.0', Chris@76: ); Chris@76: Chris@76: // Loop through the settings. Chris@76: foreach ($default_values as $name => $value) Chris@76: { Chris@76: // Use the default value if the setting isn't set yet. Chris@76: $value = !isset($modSettings[$name]) ? $value : $modSettings[$name]; Chris@76: $config_vars[] = array('text', $name, 'value' => $value, 'disabled' => $disabled); Chris@76: } Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=loads;save'; Chris@76: $context['settings_title'] = $txt['load_balancing_settings']; Chris@76: Chris@76: // Saving? Chris@76: if (isset($_GET['save'])) Chris@76: { Chris@76: // Stupidity is not allowed. Chris@76: foreach ($_POST as $key => $value) Chris@76: { Chris@76: if (strpos($key, 'loadavg') === 0 || $key === 'loadavg_enable') Chris@76: continue; Chris@76: elseif ($key == 'loadavg_auto_opt' && $value <= 1) Chris@76: $_POST['loadavg_auto_opt'] = '1.0'; Chris@76: elseif ($key == 'loadavg_forum' && $value < 10) Chris@76: $_POST['loadavg_forum'] = '10.0'; Chris@76: elseif ($value < 2) Chris@76: $_POST[$key] = '2.0'; Chris@76: } Chris@76: Chris@76: saveDBSettings($config_vars); Chris@76: redirectexit('action=admin;area=serversettings;sa=loads;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: prepareDBSettingContext($config_vars); Chris@76: } Chris@76: Chris@76: // This is the main function for the language area. Chris@76: function ManageLanguages() Chris@76: { Chris@76: global $context, $txt, $scripturl, $modSettings; Chris@76: Chris@76: loadLanguage('ManageSettings'); Chris@76: Chris@76: $context['page_title'] = $txt['edit_languages']; Chris@76: $context['sub_template'] = 'show_settings'; Chris@76: Chris@76: $subActions = array( Chris@76: 'edit' => 'ModifyLanguages', Chris@76: 'add' => 'AddLanguage', Chris@76: 'settings' => 'ModifyLanguageSettings', Chris@76: 'downloadlang' => 'DownloadLanguage', Chris@76: 'editlang' => 'ModifyLanguage', Chris@76: ); Chris@76: Chris@76: // By default we're managing languages. Chris@76: $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'edit'; Chris@76: $context['sub_action'] = $_REQUEST['sa']; Chris@76: Chris@76: // Load up all the tabs... Chris@76: $context[$context['admin_menu_name']]['tab_data'] = array( Chris@76: 'title' => $txt['language_configuration'], Chris@76: 'description' => $txt['language_description'], Chris@76: ); Chris@76: Chris@76: // Call the right function for this sub-acton. Chris@76: $subActions[$_REQUEST['sa']](); Chris@76: } Chris@76: Chris@76: // Interface for adding a new language Chris@76: function AddLanguage() Chris@76: { Chris@76: global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl; Chris@76: Chris@76: // Are we searching for new languages courtesy of Simple Machines? Chris@76: if (!empty($_POST['smf_add_sub'])) Chris@76: { Chris@76: // Need fetch_web_data. Chris@76: require_once($sourcedir . '/Subs-Package.php'); Chris@76: Chris@76: $context['smf_search_term'] = htmlspecialchars(trim($_POST['smf_add'])); Chris@76: Chris@76: // We're going to use this URL. Chris@76: $url = 'http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))); Chris@76: Chris@76: // Load the class file and stick it into an array. Chris@76: loadClassFile('Class-Package.php'); Chris@76: $language_list = new xmlArray(fetch_web_data($url), true); Chris@76: Chris@76: // Check it exists. Chris@76: if (!$language_list->exists('languages')) Chris@76: $context['smf_error'] = 'no_response'; Chris@76: else Chris@76: { Chris@76: $language_list = $language_list->path('languages[0]'); Chris@76: $lang_files = $language_list->set('language'); Chris@76: $context['smf_languages'] = array(); Chris@76: foreach ($lang_files as $file) Chris@76: { Chris@76: // Were we searching? Chris@76: if (!empty($context['smf_search_term']) && strpos($file->fetch('name'), $smcFunc['strtolower']($context['smf_search_term'])) === false) Chris@76: continue; Chris@76: Chris@76: $context['smf_languages'][] = array( Chris@76: 'id' => $file->fetch('id'), Chris@76: 'name' => $smcFunc['ucwords']($file->fetch('name')), Chris@76: 'version' => $file->fetch('version'), Chris@76: 'utf8' => $file->fetch('utf8'), Chris@76: 'description' => $file->fetch('description'), Chris@76: 'link' => $scripturl . '?action=admin;area=languages;sa=downloadlang;did=' . $file->fetch('id') . ';' . $context['session_var'] . '=' . $context['session_id'], Chris@76: ); Chris@76: } Chris@76: if (empty($context['smf_languages'])) Chris@76: $context['smf_error'] = 'no_files'; Chris@76: } Chris@76: } Chris@76: Chris@76: $context['sub_template'] = 'add_language'; Chris@76: } Chris@76: Chris@76: // Download a language file from the Simple Machines website. Chris@76: function DownloadLanguage() Chris@76: { Chris@76: global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl, $modSettings; Chris@76: Chris@76: loadLanguage('ManageSettings'); Chris@76: require_once($sourcedir . '/Subs-Package.php'); Chris@76: Chris@76: // Clearly we need to know what to request. Chris@76: if (!isset($_GET['did'])) Chris@76: fatal_lang_error('no_access', false); Chris@76: Chris@76: // Some lovely context. Chris@76: $context['download_id'] = $_GET['did']; Chris@76: $context['sub_template'] = 'download_language'; Chris@76: $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'add'; Chris@76: Chris@76: // Can we actually do the installation - and do they want to? Chris@76: if (!empty($_POST['do_install']) && !empty($_POST['copy_file'])) Chris@76: { Chris@76: checkSession('get'); Chris@76: Chris@76: $chmod_files = array(); Chris@76: $install_files = array(); Chris@76: // Check writable status. Chris@76: foreach ($_POST['copy_file'] as $file) Chris@76: { Chris@76: // Check it's not very bad. Chris@76: if (strpos($file, '..') !== false || (substr($file, 0, 6) != 'Themes' && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file))) Chris@76: fatal_error($txt['languages_download_illegal_paths']); Chris@76: Chris@76: $chmod_files[] = $boarddir . '/' . $file; Chris@76: $install_files[] = $file; Chris@76: } Chris@76: Chris@76: // Call this in case we have work to do. Chris@76: $file_status = create_chmod_control($chmod_files); Chris@76: $files_left = $file_status['files']['notwritable']; Chris@76: Chris@76: // Something not writable? Chris@76: if (!empty($files_left)) Chris@76: $context['error_message'] = $txt['languages_download_not_chmod']; Chris@76: // Otherwise, go go go! Chris@76: elseif (!empty($install_files)) Chris@76: { Chris@76: $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), $boarddir, false, true, $install_files); Chris@76: // Make sure the files aren't stuck in the cache. Chris@76: package_flush_cache(); Chris@76: $context['install_complete'] = sprintf($txt['languages_download_complete_desc'], $scripturl . '?action=admin;area=languages'); Chris@76: Chris@76: return; Chris@76: } Chris@76: } Chris@76: Chris@76: // Open up the old china. Chris@76: if (!isset($archive_content)) Chris@76: $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), null); Chris@76: Chris@76: if (empty($archive_content)) Chris@76: fatal_error($txt['add_language_error_no_response']); Chris@76: Chris@76: // Now for each of the files, let's do some *stuff* Chris@76: $context['files'] = array( Chris@76: 'lang' => array(), Chris@76: 'other' => array(), Chris@76: ); Chris@76: $context['make_writable'] = array(); Chris@76: foreach ($archive_content as $file) Chris@76: { Chris@76: $dirname = dirname($file['filename']); Chris@76: $filename = basename($file['filename']); Chris@76: $extension = substr($filename, strrpos($filename, '.') + 1); Chris@76: Chris@76: // Don't do anything with files we don't understand. Chris@76: if (!in_array($extension, array('php', 'jpg', 'gif', 'jpeg', 'png', 'txt'))) Chris@76: continue; Chris@76: Chris@76: // Basic data. Chris@76: $context_data = array( Chris@76: 'name' => $filename, Chris@76: 'destination' => $boarddir . '/' . $file['filename'], Chris@76: 'generaldest' => $file['filename'], Chris@76: 'size' => $file['size'], Chris@76: // Does chmod status allow the copy? Chris@76: 'writable' => false, Chris@76: // Should we suggest they copy this file? Chris@76: 'default_copy' => true, Chris@76: // Does the file already exist, if so is it same or different? Chris@76: 'exists' => false, Chris@76: ); Chris@76: Chris@76: // Does the file exist, is it different and can we overwrite? Chris@76: if (file_exists($boarddir . '/' . $file['filename'])) Chris@76: { Chris@76: if (is_writable($boarddir . '/' . $file['filename'])) Chris@76: $context_data['writable'] = true; Chris@76: Chris@76: // Finally, do we actually think the content has changed? Chris@76: if ($file['size'] == filesize($boarddir . '/' . $file['filename']) && $file['md5'] == md5_file($boarddir . '/' . $file['filename'])) Chris@76: { Chris@76: $context_data['exists'] = 'same'; Chris@76: $context_data['default_copy'] = false; Chris@76: } Chris@76: // Attempt to discover newline character differences. Chris@76: elseif ($file['md5'] == md5(preg_replace("~[\r]?\n~", "\r\n", file_get_contents($boarddir . '/' . $file['filename'])))) Chris@76: { Chris@76: $context_data['exists'] = 'same'; Chris@76: $context_data['default_copy'] = false; Chris@76: } Chris@76: else Chris@76: $context_data['exists'] = 'different'; Chris@76: } Chris@76: // No overwrite? Chris@76: else Chris@76: { Chris@76: // Can we at least stick it in the directory... Chris@76: if (is_writable($boarddir . '/' . $dirname)) Chris@76: $context_data['writable'] = true; Chris@76: } Chris@76: Chris@76: // I love PHP files, that's why I'm a developer and not an artistic type spending my time drinking absinth and living a life of sin... Chris@76: if ($extension == 'php' && preg_match('~\w+\.\w+(?:-utf8)?\.php~', $filename)) Chris@76: { Chris@76: $context_data += array( Chris@76: 'version' => '??', Chris@76: 'cur_version' => false, Chris@76: 'version_compare' => 'newer', Chris@76: ); Chris@76: Chris@76: list ($name, $language) = explode('.', $filename); Chris@76: Chris@76: // Let's get the new version, I like versions, they tell me that I'm up to date. Chris@76: if (preg_match('~\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '~i', $file['preview'], $match) == 1) Chris@76: $context_data['version'] = $match[1]; Chris@76: Chris@76: // Now does the old file exist - if so what is it's version? Chris@76: if (file_exists($boarddir . '/' . $file['filename'])) Chris@76: { Chris@76: // OK - what is the current version? Chris@76: $fp = fopen($boarddir . '/' . $file['filename'], 'rb'); Chris@76: $header = fread($fp, 768); Chris@76: fclose($fp); Chris@76: Chris@76: // Find the version. Chris@76: if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1) Chris@76: { Chris@76: $context_data['cur_version'] = $match[1]; Chris@76: Chris@76: // How does this compare? Chris@76: if ($context_data['cur_version'] == $context_data['version']) Chris@76: $context_data['version_compare'] = 'same'; Chris@76: elseif ($context_data['cur_version'] > $context_data['version']) Chris@76: $context_data['version_compare'] = 'older'; Chris@76: Chris@76: // Don't recommend copying if the version is the same. Chris@76: if ($context_data['version_compare'] != 'newer') Chris@76: $context_data['default_copy'] = false; Chris@76: } Chris@76: } Chris@76: Chris@76: // Add the context data to the main set. Chris@76: $context['files']['lang'][] = $context_data; Chris@76: } Chris@76: else Chris@76: { Chris@76: // If we think it's a theme thing, work out what the theme is. Chris@76: if (substr($dirname, 0, 6) == 'Themes' && preg_match('~Themes[\\/]([^\\/]+)[\\/]~', $dirname, $match)) Chris@76: $theme_name = $match[1]; Chris@76: else Chris@76: $theme_name = 'misc'; Chris@76: Chris@76: // Assume it's an image, could be an acceptance note etc but rare. Chris@76: $context['files']['images'][$theme_name][] = $context_data; Chris@76: } Chris@76: Chris@76: // Collect together all non-writable areas. Chris@76: if (!$context_data['writable']) Chris@76: $context['make_writable'][] = $context_data['destination']; Chris@76: } Chris@76: Chris@76: // So, I'm a perfectionist - let's get the theme names. Chris@76: $theme_indexes = array(); Chris@76: foreach ($context['files']['images'] as $k => $dummy) Chris@76: $indexes[] = $k; Chris@76: Chris@76: $context['theme_names'] = array(); Chris@76: if (!empty($indexes)) Chris@76: { Chris@76: $value_data = array( Chris@76: 'query' => array(), Chris@76: 'params' => array(), Chris@76: ); Chris@76: Chris@76: foreach ($indexes as $k => $index) Chris@76: { Chris@76: $value_data['query'][] = 'value LIKE {string:value_' . $k . '}'; Chris@76: $value_data['params']['value_' . $k] = '%' . $index; Chris@76: } Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_theme, value Chris@76: FROM {db_prefix}themes Chris@76: WHERE id_member = {int:no_member} Chris@76: AND variable = {string:theme_dir} Chris@76: AND (' . implode(' OR ', $value_data['query']) . ')', Chris@76: array_merge($value_data['params'], array( Chris@76: 'no_member' => 0, Chris@76: 'theme_dir' => 'theme_dir', Chris@76: 'index_compare_explode' => 'value LIKE \'%' . implode('\' OR value LIKE \'%', $indexes) . '\'', Chris@76: )) Chris@76: ); Chris@76: $themes = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // Find the right one. Chris@76: foreach ($indexes as $index) Chris@76: if (strpos($row['value'], $index) !== false) Chris@76: $themes[$row['id_theme']] = $index; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (!empty($themes)) Chris@76: { Chris@76: // Now we have the id_theme we can get the pretty description. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_theme, value Chris@76: FROM {db_prefix}themes Chris@76: WHERE id_member = {int:no_member} Chris@76: AND variable = {string:name} Chris@76: AND id_theme IN ({array_int:theme_list})', Chris@76: array( Chris@76: 'theme_list' => array_keys($themes), Chris@76: 'no_member' => 0, Chris@76: 'name' => 'name', Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // Now we have it... Chris@76: $context['theme_names'][$themes[$row['id_theme']]] = $row['value']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: } Chris@76: } Chris@76: Chris@76: // Before we go to far can we make anything writable, eh, eh? Chris@76: if (!empty($context['make_writable'])) Chris@76: { Chris@76: // What is left to be made writable? Chris@76: $file_status = create_chmod_control($context['make_writable']); Chris@76: $context['still_not_writable'] = $file_status['files']['notwritable']; Chris@76: Chris@76: // Mark those which are now writable as such. Chris@76: foreach ($context['files'] as $type => $data) Chris@76: { Chris@76: if ($type == 'lang') Chris@76: { Chris@76: foreach ($data as $k => $file) Chris@76: if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable'])) Chris@76: $context['files'][$type][$k]['writable'] = true; Chris@76: } Chris@76: else Chris@76: { Chris@76: foreach ($data as $theme => $files) Chris@76: foreach ($files as $k => $file) Chris@76: if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable'])) Chris@76: $context['files'][$type][$theme][$k]['writable'] = true; Chris@76: } Chris@76: } Chris@76: Chris@76: // Are we going to need more language stuff? Chris@76: if (!empty($context['still_not_writable'])) Chris@76: loadLanguage('Packages'); Chris@76: } Chris@76: Chris@76: // This is the list for the main files. Chris@76: $listOptions = array( Chris@76: 'id' => 'lang_main_files_list', Chris@76: 'title' => $txt['languages_download_main_files'], Chris@76: 'get_items' => array( Chris@76: 'function' => create_function('', ' Chris@76: global $context; Chris@76: return $context[\'files\'][\'lang\']; Chris@76: '), Chris@76: ), Chris@76: 'columns' => array( Chris@76: 'name' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_download_filename'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: global $context, $txt; Chris@76: Chris@76: return \'\' . $rowData[\'name\'] . \'
\' . $txt[\'languages_download_dest\'] . \': \' . $rowData[\'destination\'] . \'\' . ($rowData[\'version_compare\'] == \'older\' ? \'
\' . $txt[\'languages_download_older\'] : \'\'); Chris@76: '), Chris@76: ), Chris@76: ), Chris@76: 'writable' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_download_writable'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: global $txt; Chris@76: Chris@76: return \'\' . ($rowData[\'writable\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'\'; Chris@76: '), Chris@76: 'style' => 'text-align: center', Chris@76: ), Chris@76: ), Chris@76: 'version' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_download_version'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: global $txt; Chris@76: Chris@76: return \'\' . $rowData[\'version\'] . \'\'; Chris@76: '), Chris@76: ), Chris@76: ), Chris@76: 'exists' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_download_exists'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: global $txt; Chris@76: Chris@76: return $rowData[\'exists\'] ? ($rowData[\'exists\'] == \'same\' ? $txt[\'languages_download_exists_same\'] : $txt[\'languages_download_exists_different\']) : $txt[\'no\']; Chris@76: '), Chris@76: ), Chris@76: ), Chris@76: 'copy' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_download_copy'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: return \'\'; Chris@76: '), Chris@76: 'style' => 'text-align: center; width: 4%;', Chris@76: ), Chris@76: ), Chris@76: ), Chris@76: ); Chris@76: Chris@76: // Kill the cache, as it is now invalid.. Chris@76: if (!empty($modSettings['cache_enable'])) Chris@76: { Chris@76: cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600); Chris@76: cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600); Chris@76: } Chris@76: Chris@76: require_once($sourcedir . '/Subs-List.php'); Chris@76: createList($listOptions); Chris@76: Chris@76: $context['default_list'] = 'lang_main_files_list'; Chris@76: } Chris@76: Chris@76: // This lists all the current languages and allows editing of them. Chris@76: function ModifyLanguages() Chris@76: { Chris@76: global $txt, $context, $scripturl; Chris@76: global $user_info, $smcFunc, $sourcedir, $language, $boarddir, $forum_version; Chris@76: Chris@76: // Setting a new default? Chris@76: if (!empty($_POST['set_default']) && !empty($_POST['def_language'])) Chris@76: { Chris@76: checkSession(); Chris@76: Chris@76: getLanguages(true, false); Chris@76: $lang_exists = false; Chris@76: foreach ($context['languages'] as $lang) Chris@76: { Chris@76: if ($_POST['def_language'] == $lang['filename']) Chris@76: { Chris@76: $lang_exists = true; Chris@76: break; Chris@76: } Chris@76: } Chris@76: Chris@76: if ($_POST['def_language'] != $language && $lang_exists) Chris@76: { Chris@76: require_once($sourcedir . '/Subs-Admin.php'); Chris@76: updateSettingsFile(array('language' => '\'' . $_POST['def_language'] . '\'')); Chris@76: $language = $_POST['def_language']; Chris@76: } Chris@76: } Chris@76: Chris@76: $listOptions = array( Chris@76: 'id' => 'language_list', Chris@76: 'items_per_page' => 20, Chris@76: 'base_href' => $scripturl . '?action=admin;area=languages', Chris@76: 'title' => $txt['edit_languages'], Chris@76: 'get_items' => array( Chris@76: 'function' => 'list_getLanguages', Chris@76: ), Chris@76: 'get_count' => array( Chris@76: 'function' => 'list_getNumLanguages', Chris@76: ), Chris@76: 'columns' => array( Chris@76: 'default' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_default'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: return \'\'; Chris@76: '), Chris@76: 'style' => 'text-align: center; width: 8%;', Chris@76: ), Chris@76: ), Chris@76: 'name' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_lang_name'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'function' => create_function('$rowData', ' Chris@76: global $scripturl, $context; Chris@76: Chris@76: return sprintf(\'%3$s\', $scripturl, $rowData[\'id\'], $rowData[\'name\']); Chris@76: '), Chris@76: ), Chris@76: ), Chris@76: 'character_set' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_character_set'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'db_htmlsafe' => 'char_set', Chris@76: ), Chris@76: ), Chris@76: 'count' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_users'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'db_htmlsafe' => 'count', Chris@76: 'style' => 'text-align: center', Chris@76: ), Chris@76: ), Chris@76: 'locale' => array( Chris@76: 'header' => array( Chris@76: 'value' => $txt['languages_locale'], Chris@76: ), Chris@76: 'data' => array( Chris@76: 'db_htmlsafe' => 'locale', Chris@76: ), Chris@76: ), Chris@76: ), Chris@76: 'form' => array( Chris@76: 'href' => $scripturl . '?action=admin;area=languages', Chris@76: ), Chris@76: 'additional_rows' => array( Chris@76: array( Chris@76: 'position' => 'below_table_data', Chris@76: 'value' => '', Chris@76: 'style' => 'text-align: right;', Chris@76: ), Chris@76: ), Chris@76: // For highlighting the default. Chris@76: 'javascript' => ' Chris@76: var prevClass = ""; Chris@76: var prevDiv = ""; Chris@76: function highlightSelected(box) Chris@76: { Chris@76: if (prevClass != "") Chris@76: prevDiv.className = prevClass; Chris@76: Chris@76: prevDiv = document.getElementById(box); Chris@76: prevClass = prevDiv.className; Chris@76: Chris@76: prevDiv.className = "highlight2"; Chris@76: } Chris@76: highlightSelected("list_language_list_' . ($language == '' ? 'english' : $language). '"); Chris@76: ', Chris@76: ); Chris@76: Chris@76: // Display a warning if we cannot edit the default setting. Chris@76: if (!is_writable($boarddir . '/Settings.php')) Chris@76: $listOptions['additional_rows'][] = array( Chris@76: 'position' => 'after_title', Chris@76: 'value' => $txt['language_settings_writable'], Chris@76: 'class' => 'smalltext alert', Chris@76: ); Chris@76: Chris@76: require_once($sourcedir . '/Subs-List.php'); Chris@76: createList($listOptions); Chris@76: Chris@76: $context['sub_template'] = 'show_list'; Chris@76: $context['default_list'] = 'language_list'; Chris@76: } Chris@76: Chris@76: // How many languages? Chris@76: function list_getNumLanguages() Chris@76: { Chris@76: global $settings; Chris@76: Chris@76: // Return how many we have. Chris@76: return count(getLanguages(true, false)); Chris@76: } Chris@76: Chris@76: // Fetch the actual language information. Chris@76: function list_getLanguages() Chris@76: { Chris@76: global $settings, $smcFunc, $language, $context, $txt; Chris@76: Chris@76: $languages = array(); Chris@76: // Keep our old entries. Chris@76: $old_txt = $txt; Chris@76: $backup_actual_theme_dir = $settings['actual_theme_dir']; Chris@76: $backup_base_theme_dir = !empty($settings['base_theme_dir']) ? $settings['base_theme_dir'] : ''; Chris@76: Chris@76: // Override these for now. Chris@76: $settings['actual_theme_dir'] = $settings['base_theme_dir'] = $settings['default_theme_dir']; Chris@76: getLanguages(true, false); Chris@76: Chris@76: // Put them back. Chris@76: $settings['actual_theme_dir'] = $backup_actual_theme_dir; Chris@76: if (!empty($backup_base_theme_dir)) Chris@76: $settings['base_theme_dir'] = $backup_base_theme_dir; Chris@76: else Chris@76: unset($settings['base_theme_dir']); Chris@76: Chris@76: // Get the language files and data... Chris@76: foreach ($context['languages'] as $lang) Chris@76: { Chris@76: // Load the file to get the character set. Chris@76: require($settings['default_theme_dir'] . '/languages/index.' . $lang['filename'] . '.php'); Chris@76: Chris@76: $languages[$lang['filename']] = array( Chris@76: 'id' => $lang['filename'], Chris@76: 'count' => 0, Chris@76: 'char_set' => $txt['lang_character_set'], Chris@76: 'default' => $language == $lang['filename'] || ($language == '' && $lang['filename'] == 'english'), Chris@76: 'locale' => $txt['lang_locale'], Chris@76: 'name' => $smcFunc['ucwords'](strtr($lang['filename'], array('_' => ' ', '-utf8' => ''))), Chris@76: ); Chris@76: } Chris@76: Chris@76: // Work out how many people are using each language. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT lngfile, COUNT(*) AS num_users Chris@76: FROM {db_prefix}members Chris@76: GROUP BY lngfile', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // Default? Chris@76: if (empty($row['lngfile']) || !isset($languages[$row['lngfile']])) Chris@76: $row['lngfile'] = $language; Chris@76: Chris@76: if (!isset($languages[$row['lngfile']]) && isset($languages['english'])) Chris@76: $languages['english']['count'] += $row['num_users']; Chris@76: elseif (isset($languages[$row['lngfile']])) Chris@76: $languages[$row['lngfile']]['count'] += $row['num_users']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Restore the current users language. Chris@76: $txt = $old_txt; Chris@76: Chris@76: // Return how many we have. Chris@76: return $languages; Chris@76: } Chris@76: Chris@76: // Edit language related settings. Chris@76: function ModifyLanguageSettings($return_config = false) Chris@76: { Chris@76: global $scripturl, $context, $txt, $boarddir, $settings, $smcFunc; Chris@76: Chris@76: // Warn the user if the backup of Settings.php failed. Chris@76: $settings_not_writable = !is_writable($boarddir . '/Settings.php'); Chris@76: $settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php'); Chris@76: Chris@76: /* If you're writing a mod, it's a bad idea to add things here.... Chris@76: For each option: Chris@76: variable name, description, type (constant), size/possible values, helptext. Chris@76: OR an empty string for a horizontal rule. Chris@76: OR a string for a titled section. */ Chris@76: $config_vars = array( Chris@76: 'language' => array('language', $txt['default_language'], 'file', 'select', array(), null, 'disabled' => $settings_not_writable), Chris@76: array('userLanguage', $txt['userLanguage'], 'db', 'check', null, 'userLanguage'), Chris@76: ); Chris@76: Chris@76: if ($return_config) Chris@76: return $config_vars; Chris@76: Chris@76: // Get our languages. No cache and use utf8. Chris@76: getLanguages(false, false); Chris@76: foreach ($context['languages'] as $lang) Chris@76: $config_vars['language'][4][$lang['filename']] = array($lang['filename'], strtr($lang['name'], array('-utf8' => ' (UTF-8)'))); Chris@76: Chris@76: // Saving settings? Chris@76: if (isset($_REQUEST['save'])) Chris@76: { Chris@76: checkSession(); Chris@76: saveSettings($config_vars); Chris@76: redirectexit('action=admin;area=languages;sa=settings'); Chris@76: } Chris@76: Chris@76: // Setup the template stuff. Chris@76: $context['post_url'] = $scripturl . '?action=admin;area=languages;sa=settings;save'; Chris@76: $context['settings_title'] = $txt['language_settings']; Chris@76: $context['save_disabled'] = $settings_not_writable; Chris@76: Chris@76: if ($settings_not_writable) Chris@76: $context['settings_message'] = '
' . $txt['settings_not_writable'] . '

'; Chris@76: elseif ($settings_backup_fail) Chris@76: $context['settings_message'] = '
' . $txt['admin_backup_fail'] . '

'; Chris@76: Chris@76: // Fill the config array. Chris@76: prepareServerSettingsContext($config_vars); Chris@76: } Chris@76: Chris@76: // Edit a particular set of language entries. Chris@76: function ModifyLanguage() Chris@76: { Chris@76: global $settings, $context, $smcFunc, $txt, $modSettings, $boarddir, $sourcedir, $language; Chris@76: Chris@76: loadLanguage('ManageSettings'); Chris@76: Chris@76: // Select the languages tab. Chris@76: $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'edit'; Chris@76: $context['page_title'] = $txt['edit_languages']; Chris@76: $context['sub_template'] = 'modify_language_entries'; Chris@76: Chris@76: $context['lang_id'] = $_GET['lid']; Chris@76: list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? array(1, '') : explode('+', $_REQUEST['tfid']); Chris@76: Chris@76: // Clean the ID - just in case. Chris@76: preg_match('~([A-Za-z0-9_-]+)~', $context['lang_id'], $matches); Chris@76: $context['lang_id'] = $matches[1]; Chris@76: Chris@76: // Get all the theme data. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_theme, variable, value Chris@76: FROM {db_prefix}themes Chris@76: WHERE id_theme != {int:default_theme} Chris@76: AND id_member = {int:no_member} Chris@76: AND variable IN ({string:name}, {string:theme_dir})', Chris@76: array( Chris@76: 'default_theme' => 1, Chris@76: 'no_member' => 0, Chris@76: 'name' => 'name', Chris@76: 'theme_dir' => 'theme_dir', Chris@76: ) Chris@76: ); Chris@76: $themes = array( Chris@76: 1 => array( Chris@76: 'name' => $txt['dvc_default'], Chris@76: 'theme_dir' => $settings['default_theme_dir'], Chris@76: ), Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $themes[$row['id_theme']][$row['variable']] = $row['value']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // This will be where we look Chris@76: $lang_dirs = array(); Chris@76: // Check we have themes with a path and a name - just in case - and add the path. Chris@76: foreach ($themes as $id => $data) Chris@76: { Chris@76: if (count($data) != 2) Chris@76: unset($themes[$id]); Chris@76: elseif (is_dir($data['theme_dir'] . '/languages')) Chris@76: $lang_dirs[$id] = $data['theme_dir'] . '/languages'; Chris@76: Chris@76: // How about image directories? Chris@76: if (is_dir($data['theme_dir'] . '/images/' . $context['lang_id'])) Chris@76: $images_dirs[$id] = $data['theme_dir'] . '/images/' . $context['lang_id']; Chris@76: } Chris@76: Chris@76: $current_file = $file_id ? $lang_dirs[$theme_id] . '/' . $file_id . '.' . $context['lang_id'] . '.php' : ''; Chris@76: Chris@76: // Now for every theme get all the files and stick them in context! Chris@76: $context['possible_files'] = array(); Chris@76: foreach ($lang_dirs as $theme => $theme_dir) Chris@76: { Chris@76: // Open it up. Chris@76: $dir = dir($theme_dir); Chris@76: while ($entry = $dir->read()) Chris@76: { Chris@76: // We're only after the files for this language. Chris@76: if (preg_match('~^([A-Za-z]+)\.' . $context['lang_id'] . '\.php$~', $entry, $matches) == 0) Chris@76: continue; Chris@76: Chris@76: //!!! Temp! Chris@76: if ($matches[1] == 'EmailTemplates') Chris@76: continue; Chris@76: Chris@76: if (!isset($context['possible_files'][$theme])) Chris@76: $context['possible_files'][$theme] = array( Chris@76: 'id' => $theme, Chris@76: 'name' => $themes[$theme]['name'], Chris@76: 'files' => array(), Chris@76: ); Chris@76: Chris@76: $context['possible_files'][$theme]['files'][] = array( Chris@76: 'id' => $matches[1], Chris@76: 'name' => isset($txt['lang_file_desc_' . $matches[1]]) ? $txt['lang_file_desc_' . $matches[1]] : $matches[1], Chris@76: 'selected' => $theme_id == $theme && $file_id == $matches[1], Chris@76: ); Chris@76: } Chris@76: $dir->close(); Chris@76: } Chris@76: Chris@76: // We no longer wish to speak this language. Chris@76: if (!empty($_POST['delete_main']) && $context['lang_id'] != 'english') Chris@76: { Chris@76: checkSession(); Chris@76: Chris@76: // !!! Todo: FTP Controls? Chris@76: require_once($sourcedir . '/Subs-Package.php'); Chris@76: Chris@76: // First, Make a backup? Chris@76: if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['lang_id'] . '$$$')) Chris@76: { Chris@76: $_SESSION['last_backup_for'] = $context['lang_id'] . '$$$'; Chris@76: package_create_backup('backup_lang_' . $context['lang_id']); Chris@76: } Chris@76: Chris@76: // Second, loop through the array to remove the files. Chris@76: foreach ($lang_dirs as $curPath) Chris@76: { Chris@76: foreach ($context['possible_files'][1]['files'] as $lang) Chris@76: if (file_exists($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php')) Chris@76: unlink($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php'); Chris@76: Chris@76: // Check for the email template. Chris@76: if (file_exists($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php')) Chris@76: unlink($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php'); Chris@76: } Chris@76: Chris@76: // Third, the agreement file. Chris@76: if (file_exists($boarddir . '/agreement.' . $context['lang_id'] . '.txt')) Chris@76: unlink($boarddir . '/agreement.' . $context['lang_id'] . '.txt'); Chris@76: Chris@76: // Fourth, a related images folder? Chris@76: foreach ($images_dirs as $curPath) Chris@76: if (is_dir($curPath)) Chris@76: deltree($curPath); Chris@76: Chris@76: // Members can no longer use this language. Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}members Chris@76: SET lngfile = {string:empty_string} Chris@76: WHERE lngfile = {string:current_language}', Chris@76: array( Chris@76: 'empty_string' => '', Chris@76: 'current_language' => $context['lang_id'], Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Fifth, update getLanguages() cache. Chris@76: if (!empty($modSettings['cache_enable'])) Chris@76: { Chris@76: cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600); Chris@76: cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600); Chris@76: } Chris@76: Chris@76: // Sixth, if we deleted the default language, set us back to english? Chris@76: if ($context['lang_id'] == $language) Chris@76: { Chris@76: require_once($sourcedir . '/Subs-Admin.php'); Chris@76: $language = 'english'; Chris@76: updateSettingsFile(array('language' => '\'' . $language . '\'')); Chris@76: } Chris@76: Chris@76: // Seventh, get out of here. Chris@76: redirectexit('action=admin;area=languages;sa=edit;' . $context['session_var'] . '=' . $context['session_id']); Chris@76: } Chris@76: Chris@76: // Saving primary settings? Chris@76: $madeSave = false; Chris@76: if (!empty($_POST['save_main']) && !$current_file) Chris@76: { Chris@76: checkSession(); Chris@76: Chris@76: // Read in the current file. Chris@76: $current_data = implode('', file($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php')); Chris@76: // These are the replacements. old => new Chris@76: $replace_array = array( Chris@76: '~\$txt\[\'lang_character_set\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_character_set\'] = \'' . addslashes($_POST['character_set']) . '\';', Chris@76: '~\$txt\[\'lang_locale\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_locale\'] = \'' . addslashes($_POST['locale']) . '\';', Chris@76: '~\$txt\[\'lang_dictionary\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_dictionary\'] = \'' . addslashes($_POST['dictionary']) . '\';', Chris@76: '~\$txt\[\'lang_spelling\'\]\s=\s(\'|")[^\r\n]+~' => '$txt[\'lang_spelling\'] = \'' . addslashes($_POST['spelling']) . '\';', Chris@76: '~\$txt\[\'lang_rtl\'\]\s=\s[A-Za-z0-9]+;~' => '$txt[\'lang_rtl\'] = ' . (!empty($_POST['rtl']) ? 'true' : 'false') . ';', Chris@76: ); Chris@76: $current_data = preg_replace(array_keys($replace_array), array_values($replace_array), $current_data); Chris@76: $fp = fopen($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php', 'w+'); Chris@76: fwrite($fp, $current_data); Chris@76: fclose($fp); Chris@76: Chris@76: $madeSave = true; Chris@76: } Chris@76: Chris@76: // Quickly load index language entries. Chris@76: $old_txt = $txt; Chris@76: require($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); Chris@76: $context['lang_file_not_writable_message'] = is_writable($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php') ? '' : sprintf($txt['lang_file_not_writable'], $settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); Chris@76: // Setup the primary settings context. Chris@76: $context['primary_settings'] = array( Chris@76: 'name' => $smcFunc['ucwords'](strtr($context['lang_id'], array('_' => ' ', '-utf8' => ''))), Chris@76: 'character_set' => $txt['lang_character_set'], Chris@76: 'locale' => $txt['lang_locale'], Chris@76: 'dictionary' => $txt['lang_dictionary'], Chris@76: 'spelling' => $txt['lang_spelling'], Chris@76: 'rtl' => $txt['lang_rtl'], Chris@76: ); Chris@76: Chris@76: // Restore normal service. Chris@76: $txt = $old_txt; Chris@76: Chris@76: // Are we saving? Chris@76: $save_strings = array(); Chris@76: if (isset($_POST['save_entries']) && !empty($_POST['entry'])) Chris@76: { Chris@76: checkSession(); Chris@76: Chris@76: // Clean each entry! Chris@76: foreach ($_POST['entry'] as $k => $v) Chris@76: { Chris@76: // Only try to save if it's changed! Chris@76: if ($_POST['entry'][$k] != $_POST['comp'][$k]) Chris@76: $save_strings[$k] = cleanLangString($v, false); Chris@76: } Chris@76: } Chris@76: Chris@76: // If we are editing a file work away at that. Chris@76: if ($current_file) Chris@76: { Chris@76: $context['entries_not_writable_message'] = is_writable($current_file) ? '' : sprintf($txt['lang_entries_not_writable'], $current_file); Chris@76: Chris@76: $entries = array(); Chris@76: // We can't just require it I'm afraid - otherwise we pass in all kinds of variables! Chris@76: $multiline_cache = ''; Chris@76: foreach (file($current_file) as $line) Chris@76: { Chris@76: // Got a new entry? Chris@76: if ($line[0] == '$' && !empty($multiline_cache)) Chris@76: { Chris@76: preg_match('~\$(helptxt|txt)\[\'(.+)\'\]\s=\s(.+);~', strtr($multiline_cache, array("\n" => '', "\t" => '')), $matches); Chris@76: if (!empty($matches[3])) Chris@76: { Chris@76: $entries[$matches[2]] = array( Chris@76: 'type' => $matches[1], Chris@76: 'full' => $matches[0], Chris@76: 'entry' => $matches[3], Chris@76: ); Chris@76: $multiline_cache = ''; Chris@76: } Chris@76: } Chris@76: $multiline_cache .= $line . "\n"; Chris@76: } Chris@76: // Last entry to add? Chris@76: if ($multiline_cache) Chris@76: { Chris@76: preg_match('~\$(helptxt|txt)\[\'(.+)\'\]\s=\s(.+);~', strtr($multiline_cache, array("\n" => '', "\t" => '')), $matches); Chris@76: if (!empty($matches[3])) Chris@76: $entries[$matches[2]] = array( Chris@76: 'type' => $matches[1], Chris@76: 'full' => $matches[0], Chris@76: 'entry' => $matches[3], Chris@76: ); Chris@76: } Chris@76: Chris@76: // These are the entries we can definitely save. Chris@76: $final_saves = array(); Chris@76: Chris@76: $context['file_entries'] = array(); Chris@76: foreach ($entries as $entryKey => $entryValue) Chris@76: { Chris@76: // Ignore some things we set separately. Chris@76: $ignore_files = array('lang_character_set', 'lang_locale', 'lang_dictionary', 'lang_spelling', 'lang_rtl'); Chris@76: if (in_array($entryKey, $ignore_files)) Chris@76: continue; Chris@76: Chris@76: // These are arrays that need breaking out. Chris@76: $arrays = array('days', 'days_short', 'months', 'months_titles', 'months_short'); Chris@76: if (in_array($entryKey, $arrays)) Chris@76: { Chris@76: // Get off the first bits. Chris@76: $entryValue['entry'] = substr($entryValue['entry'], strpos($entryValue['entry'], '(') + 1, strrpos($entryValue['entry'], ')') - strpos($entryValue['entry'], '(')); Chris@76: $entryValue['entry'] = explode(',', strtr($entryValue['entry'], array(' ' => ''))); Chris@76: Chris@76: // Now create an entry for each item. Chris@76: $cur_index = 0; Chris@76: $save_cache = array( Chris@76: 'enabled' => false, Chris@76: 'entries' => array(), Chris@76: ); Chris@76: foreach ($entryValue['entry'] as $id => $subValue) Chris@76: { Chris@76: // Is this a new index? Chris@76: if (preg_match('~^(\d+)~', $subValue, $matches)) Chris@76: { Chris@76: $cur_index = $matches[1]; Chris@76: $subValue = substr($subValue, strpos($subValue, '\'')); Chris@76: } Chris@76: Chris@76: // Clean up some bits. Chris@76: $subValue = strtr($subValue, array('"' => '', '\'' => '', ')' => '')); Chris@76: Chris@76: // Can we save? Chris@76: if (isset($save_strings[$entryKey . '-+- ' . $cur_index])) Chris@76: { Chris@76: $save_cache['entries'][$cur_index] = strtr($save_strings[$entryKey . '-+- ' . $cur_index], array('\'' => '')); Chris@76: $save_cache['enabled'] = true; Chris@76: } Chris@76: else Chris@76: $save_cache['entries'][$cur_index] = $subValue; Chris@76: Chris@76: $context['file_entries'][] = array( Chris@76: 'key' => $entryKey . '-+- ' . $cur_index, Chris@76: 'value' => $subValue, Chris@76: 'rows' => 1, Chris@76: ); Chris@76: $cur_index++; Chris@76: } Chris@76: Chris@76: // Do we need to save? Chris@76: if ($save_cache['enabled']) Chris@76: { Chris@76: // Format the string, checking the indexes first. Chris@76: $items = array(); Chris@76: $cur_index = 0; Chris@76: foreach ($save_cache['entries'] as $k2 => $v2) Chris@76: { Chris@76: // Manually show the custom index. Chris@76: if ($k2 != $cur_index) Chris@76: { Chris@76: $items[] = $k2 . ' => \'' . $v2 . '\''; Chris@76: $cur_index = $k2; Chris@76: } Chris@76: else Chris@76: $items[] = '\'' . $v2 . '\''; Chris@76: Chris@76: $cur_index++; Chris@76: } Chris@76: // Now create the string! Chris@76: $final_saves[$entryKey] = array( Chris@76: 'find' => $entryValue['full'], Chris@76: 'replace' => '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = array(' . implode(', ', $items) . ');', Chris@76: ); Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Saving? Chris@76: if (isset($save_strings[$entryKey]) && $save_strings[$entryKey] != $entryValue['entry']) Chris@76: { Chris@76: // !!! Fix this properly. Chris@76: if ($save_strings[$entryKey] == '') Chris@76: $save_strings[$entryKey] = '\'\''; Chris@76: Chris@76: // Set the new value. Chris@76: $entryValue['entry'] = $save_strings[$entryKey]; Chris@76: // And we know what to save now! Chris@76: $final_saves[$entryKey] = array( Chris@76: 'find' => $entryValue['full'], Chris@76: 'replace' => '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = ' . $save_strings[$entryKey] . ';', Chris@76: ); Chris@76: } Chris@76: Chris@76: $editing_string = cleanLangString($entryValue['entry'], true); Chris@76: $context['file_entries'][] = array( Chris@76: 'key' => $entryKey, Chris@76: 'value' => $editing_string, Chris@76: 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Any saves to make? Chris@76: if (!empty($final_saves)) Chris@76: { Chris@76: checkSession(); Chris@76: Chris@76: $file_contents = implode('', file($current_file)); Chris@76: foreach ($final_saves as $save) Chris@76: $file_contents = strtr($file_contents, array($save['find'] => $save['replace'])); Chris@76: Chris@76: // Save the actual changes. Chris@76: $fp = fopen($current_file, 'w+'); Chris@76: fwrite($fp, $file_contents); Chris@76: fclose($fp); Chris@76: Chris@76: $madeSave = true; Chris@76: } Chris@76: Chris@76: // Another restore. Chris@76: $txt = $old_txt; Chris@76: } Chris@76: Chris@76: // If we saved, redirect. Chris@76: if ($madeSave) Chris@76: redirectexit('action=admin;area=languages;sa=editlang;lid=' . $context['lang_id']); Chris@76: } Chris@76: Chris@76: // This function could be two functions - either way it cleans language entries to/from display. Chris@76: function cleanLangString($string, $to_display = true) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // If going to display we make sure it doesn't have any HTML in it - etc. Chris@76: $new_string = ''; Chris@76: if ($to_display) Chris@76: { Chris@76: // Are we in a string (0 = no, 1 = single quote, 2 = parsed) Chris@76: $in_string = 0; Chris@76: $is_escape = false; Chris@76: for ($i = 0; $i < strlen($string); $i++) Chris@76: { Chris@76: // Handle ecapes first. Chris@76: if ($string{$i} == '\\') Chris@76: { Chris@76: // Toggle the escape. Chris@76: $is_escape = !$is_escape; Chris@76: // If we're now escaped don't add this string. Chris@76: if ($is_escape) Chris@76: continue; Chris@76: } Chris@76: // Special case - parsed string with line break etc? Chris@76: elseif (($string{$i} == 'n' || $string{$i} == 't') && $in_string == 2 && $is_escape) Chris@76: { Chris@76: // Put the escape back... Chris@76: $new_string .= $string{$i} == 'n' ? "\n" : "\t"; Chris@76: $is_escape = false; Chris@76: continue; Chris@76: } Chris@76: // Have we got a single quote? Chris@76: elseif ($string{$i} == '\'') Chris@76: { Chris@76: // Already in a parsed string, or escaped in a linear string, means we print it - otherwise something special. Chris@76: if ($in_string != 2 && ($in_string != 1 || !$is_escape)) Chris@76: { Chris@76: // Is it the end of a single quote string? Chris@76: if ($in_string == 1) Chris@76: $in_string = 0; Chris@76: // Otherwise it's the start! Chris@76: else Chris@76: $in_string = 1; Chris@76: Chris@76: // Don't actually include this character! Chris@76: continue; Chris@76: } Chris@76: } Chris@76: // Otherwise a double quote? Chris@76: elseif ($string{$i} == '"') Chris@76: { Chris@76: // Already in a single quote string, or escaped in a parsed string, means we print it - otherwise something special. Chris@76: if ($in_string != 1 && ($in_string != 2 || !$is_escape)) Chris@76: { Chris@76: // Is it the end of a double quote string? Chris@76: if ($in_string == 2) Chris@76: $in_string = 0; Chris@76: // Otherwise it's the start! Chris@76: else Chris@76: $in_string = 2; Chris@76: Chris@76: // Don't actually include this character! Chris@76: continue; Chris@76: } Chris@76: } Chris@76: // A join/space outside of a string is simply removed. Chris@76: elseif ($in_string == 0 && (empty($string{$i}) || $string{$i} == '.')) Chris@76: continue; Chris@76: // Start of a variable? Chris@76: elseif ($in_string == 0 && $string{$i} == '$') Chris@76: { Chris@76: // Find the whole of it! Chris@76: preg_match('~([\$A-Za-z0-9\'\[\]_-]+)~', substr($string, $i), $matches); Chris@76: if (!empty($matches[1])) Chris@76: { Chris@76: // Come up with some pseudo thing to indicate this is a var. Chris@76: //!!! Do better than this, please! Chris@76: $new_string .= '{%' . $matches[1] . '%}'; Chris@76: Chris@76: // We're not going to reparse this. Chris@76: $i += strlen($matches[1]) - 1; Chris@76: } Chris@76: Chris@76: continue; Chris@76: } Chris@76: // Right, if we're outside of a string we have DANGER, DANGER! Chris@76: elseif ($in_string == 0) Chris@76: { Chris@76: continue; Chris@76: } Chris@76: Chris@76: // Actually add the character to the string! Chris@76: $new_string .= $string{$i}; Chris@76: // If anything was escaped it ain't any longer! Chris@76: $is_escape = false; Chris@76: } Chris@76: Chris@76: // Unhtml then rehtml the whole thing! Chris@76: $new_string = htmlspecialchars(un_htmlspecialchars($new_string)); Chris@76: } Chris@76: else Chris@76: { Chris@76: // Keep track of what we're doing... Chris@76: $in_string = 0; Chris@76: // This is for deciding whether to HTML a quote. Chris@76: $in_html = false; Chris@76: for ($i = 0; $i < strlen($string); $i++) Chris@76: { Chris@76: // Handle line breaks! Chris@76: if ($string{$i} == "\n" || $string{$i} == "\t") Chris@76: { Chris@76: // Are we in a string? Is it the right type? Chris@76: if ($in_string == 1) Chris@76: { Chris@76: // Change type! Chris@76: $new_string .= '\' . "\\' . ($string{$i} == "\n" ? 'n' : 't'); Chris@76: $in_string = 2; Chris@76: } Chris@76: elseif ($in_string == 2) Chris@76: $new_string .= '\\' . ($string{$i} == "\n" ? 'n' : 't'); Chris@76: // Otherwise start one off - joining if required. Chris@76: else Chris@76: $new_string .= ($new_string ? ' . ' : '') . '"\\' . ($string{$i} == "\n" ? 'n' : 't'); Chris@76: Chris@76: continue; Chris@76: } Chris@76: // We don't do parsed strings apart from for breaks. Chris@76: elseif ($in_string == 2) Chris@76: { Chris@76: $in_string = 0; Chris@76: $new_string .= '"'; Chris@76: } Chris@76: Chris@76: // Not in a string yet? Chris@76: if ($in_string != 1) Chris@76: { Chris@76: $in_string = 1; Chris@76: $new_string .= ($new_string ? ' . ' : '') . '\''; Chris@76: } Chris@76: Chris@76: // Is this a variable? Chris@76: if ($string{$i} == '{' && $string{$i + 1} == '%' && $string{$i + 2} == '$') Chris@76: { Chris@76: // Grab the variable. Chris@76: preg_match('~\{%([\$A-Za-z0-9\'\[\]_-]+)%\}~', substr($string, $i), $matches); Chris@76: if (!empty($matches[1])) Chris@76: { Chris@76: if ($in_string == 1) Chris@76: $new_string .= '\' . '; Chris@76: elseif ($new_string) Chris@76: $new_string .= ' . '; Chris@76: Chris@76: $new_string .= $matches[1]; Chris@76: $i += strlen($matches[1]) + 3; Chris@76: $in_string = 0; Chris@76: } Chris@76: Chris@76: continue; Chris@76: } Chris@76: // Is this a lt sign? Chris@76: elseif ($string{$i} == '<') Chris@76: { Chris@76: // Probably HTML? Chris@76: if ($string{$i + 1} != ' ') Chris@76: $in_html = true; Chris@76: // Assume we need an entity... Chris@76: else Chris@76: { Chris@76: $new_string .= '<'; Chris@76: continue; Chris@76: } Chris@76: } Chris@76: // What about gt? Chris@76: elseif ($string{$i} == '>') Chris@76: { Chris@76: // Will it be HTML? Chris@76: if ($in_html) Chris@76: $in_html = false; Chris@76: // Otherwise we need an entity... Chris@76: else Chris@76: { Chris@76: $new_string .= '>'; Chris@76: continue; Chris@76: } Chris@76: } Chris@76: // Is it a slash? If so escape it... Chris@76: if ($string{$i} == '\\') Chris@76: $new_string .= '\\'; Chris@76: // The infamous double quote? Chris@76: elseif ($string{$i} == '"') Chris@76: { Chris@76: // If we're in HTML we leave it as a quote - otherwise we entity it. Chris@76: if (!$in_html) Chris@76: { Chris@76: $new_string .= '"'; Chris@76: continue; Chris@76: } Chris@76: } Chris@76: // A single quote? Chris@76: elseif ($string{$i} == '\'') Chris@76: { Chris@76: // Must be in a string so escape it. Chris@76: $new_string .= '\\'; Chris@76: } Chris@76: Chris@76: // Finally add the character to the string! Chris@76: $new_string .= $string{$i}; Chris@76: } Chris@76: Chris@76: // If we ended as a string then close it off. Chris@76: if ($in_string == 1) Chris@76: $new_string .= '\''; Chris@76: elseif ($in_string == 2) Chris@76: $new_string .= '"'; Chris@76: } Chris@76: Chris@76: return $new_string; Chris@76: } Chris@76: Chris@76: // Helper function, it sets up the context for the manage server settings. Chris@76: function prepareServerSettingsContext(&$config_vars) Chris@76: { Chris@76: global $context, $modSettings; Chris@76: Chris@76: $context['config_vars'] = array(); Chris@76: foreach ($config_vars as $identifier => $config_var) Chris@76: { Chris@76: if (!is_array($config_var) || !isset($config_var[1])) Chris@76: $context['config_vars'][] = $config_var; Chris@76: else Chris@76: { Chris@76: $varname = $config_var[0]; Chris@76: global $$varname; Chris@76: Chris@76: $context['config_vars'][] = array( Chris@76: 'label' => $config_var[1], Chris@76: 'help' => isset($config_var[5]) ? $config_var[5] : '', Chris@76: 'type' => $config_var[3], Chris@76: 'size' => empty($config_var[4]) ? 0 : $config_var[4], Chris@76: 'data' => isset($config_var[4]) && is_array($config_var[4]) ? $config_var[4] : array(), Chris@76: 'name' => $config_var[0], Chris@76: 'value' => $config_var[2] == 'file' ? htmlspecialchars($$varname) : (isset($modSettings[$config_var[0]]) ? htmlspecialchars($modSettings[$config_var[0]]) : (in_array($config_var[3], array('int', 'float')) ? 0 : '')), Chris@76: 'disabled' => !empty($context['settings_not_writable']) || !empty($config_var['disabled']), Chris@76: 'invalid' => false, Chris@76: 'javascript' => '', Chris@76: 'preinput' => '', Chris@76: 'postinput' => '', Chris@76: ); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Helper function, it sets up the context for database settings. Chris@76: function prepareDBSettingContext(&$config_vars) Chris@76: { Chris@76: global $txt, $helptxt, $context, $modSettings, $sourcedir; Chris@76: Chris@76: loadLanguage('Help'); Chris@76: Chris@76: $context['config_vars'] = array(); Chris@76: $inlinePermissions = array(); Chris@76: $bbcChoice = array(); Chris@76: foreach ($config_vars as $config_var) Chris@76: { Chris@76: // HR? Chris@76: if (!is_array($config_var)) Chris@76: $context['config_vars'][] = $config_var; Chris@76: else Chris@76: { Chris@76: // If it has no name it doesn't have any purpose! Chris@76: if (empty($config_var[1])) Chris@76: continue; Chris@76: Chris@76: // Special case for inline permissions Chris@76: if ($config_var[0] == 'permissions' && allowedTo('manage_permissions')) Chris@76: $inlinePermissions[] = $config_var[1]; Chris@76: elseif ($config_var[0] == 'permissions') Chris@76: continue; Chris@76: Chris@76: // Are we showing the BBC selection box? Chris@76: if ($config_var[0] == 'bbc') Chris@76: $bbcChoice[] = $config_var[1]; Chris@76: Chris@76: $context['config_vars'][$config_var[1]] = array( Chris@76: 'label' => isset($config_var['text_label']) ? $config_var['text_label'] : (isset($txt[$config_var[1]]) ? $txt[$config_var[1]] : (isset($config_var[3]) && !is_array($config_var[3]) ? $config_var[3] : '')), Chris@76: 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '', Chris@76: 'type' => $config_var[0], Chris@76: 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0), Chris@76: 'data' => array(), Chris@76: 'name' => $config_var[1], Chris@76: 'value' => isset($modSettings[$config_var[1]]) ? ($config_var[0] == 'select' ? $modSettings[$config_var[1]] : htmlspecialchars($modSettings[$config_var[1]])) : (in_array($config_var[0], array('int', 'float')) ? 0 : ''), Chris@76: 'disabled' => false, Chris@76: 'invalid' => !empty($config_var['invalid']), Chris@76: 'javascript' => '', Chris@76: 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '', Chris@76: 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '', Chris@76: 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '', Chris@76: ); Chris@76: Chris@76: // If this is a select box handle any data. Chris@76: if (!empty($config_var[2]) && is_array($config_var[2])) Chris@76: { Chris@76: // If we allow multiple selections, we need to adjust a few things. Chris@76: if ($config_var[0] == 'select' && !empty($config_var['multiple'])) Chris@76: { Chris@76: $context['config_vars'][$config_var[1]]['name'] .= '[]'; Chris@76: $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']); Chris@76: } Chris@76: Chris@76: // If it's associative Chris@76: if (isset($config_var[2][0]) && is_array($config_var[2][0])) Chris@76: $context['config_vars'][$config_var[1]]['data'] = $config_var[2]; Chris@76: else Chris@76: { Chris@76: foreach ($config_var[2] as $key => $item) Chris@76: $context['config_vars'][$config_var[1]]['data'][] = array($key, $item); Chris@76: } Chris@76: } Chris@76: Chris@76: // Finally allow overrides - and some final cleanups. Chris@76: foreach ($config_var as $k => $v) Chris@76: { Chris@76: if (!is_numeric($k)) Chris@76: { Chris@76: if (substr($k, 0, 2) == 'on') Chris@76: $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"'; Chris@76: else Chris@76: $context['config_vars'][$config_var[1]][$k] = $v; Chris@76: } Chris@76: Chris@76: // See if there are any other labels that might fit? Chris@76: if (isset($txt['setting_' . $config_var[1]])) Chris@76: $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]]; Chris@76: elseif (isset($txt['groups_' . $config_var[1]])) Chris@76: $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]]; Chris@76: } Chris@76: Chris@76: // Set the subtext in case it's part of the label. Chris@76: // !!! Temporary. Preventing divs inside label tags. Chris@76: $divPos = strpos($context['config_vars'][$config_var[1]]['label'], ']*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos)); Chris@76: $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // If we have inline permissions we need to prep them. Chris@76: if (!empty($inlinePermissions) && allowedTo('manage_permissions')) Chris@76: { Chris@76: require_once($sourcedir . '/ManagePermissions.php'); Chris@76: init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array()); Chris@76: } Chris@76: Chris@76: // What about any BBC selection boxes? Chris@76: if (!empty($bbcChoice)) Chris@76: { Chris@76: // What are the options, eh? Chris@76: $temp = parse_bbc(false); Chris@76: $bbcTags = array(); Chris@76: foreach ($temp as $tag) Chris@76: $bbcTags[] = $tag['tag']; Chris@76: Chris@76: $bbcTags = array_unique($bbcTags); Chris@76: $totalTags = count($bbcTags); Chris@76: Chris@76: // The number of columns we want to show the BBC tags in. Chris@76: $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3; Chris@76: Chris@76: // Start working out the context stuff. Chris@76: $context['bbc_columns'] = array(); Chris@76: $tagsPerColumn = ceil($totalTags / $numColumns); Chris@76: Chris@76: $col = 0; $i = 0; Chris@76: foreach ($bbcTags as $tag) Chris@76: { Chris@76: if ($i % $tagsPerColumn == 0 && $i != 0) Chris@76: $col++; Chris@76: Chris@76: $context['bbc_columns'][$col][] = array( Chris@76: 'tag' => $tag, Chris@76: // !!! 'tag_' . ? Chris@76: 'show_help' => isset($helptxt[$tag]), Chris@76: ); Chris@76: Chris@76: $i++; Chris@76: } Chris@76: Chris@76: // Now put whatever BBC options we may have into context too! Chris@76: $context['bbc_sections'] = array(); Chris@76: foreach ($bbcChoice as $bbc) Chris@76: { Chris@76: $context['bbc_sections'][$bbc] = array( Chris@76: 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'], Chris@76: 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc], Chris@76: 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]), Chris@76: ); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Helper function. Saves settings by putting them in Settings.php or saving them in the settings table. Chris@76: function saveSettings(&$config_vars) Chris@76: { Chris@76: global $boarddir, $sc, $cookiename, $modSettings, $user_settings; Chris@76: global $sourcedir, $context, $cachedir; Chris@76: Chris@76: // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!) Chris@76: if (isset($_POST['cookiename'])) Chris@76: $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~' . ($context['utf8'] ? 'u' : ''), '', $_POST['cookiename']); Chris@76: Chris@76: // Fix the forum's URL if necessary. Chris@76: if (isset($_POST['boardurl'])) Chris@76: { Chris@76: if (substr($_POST['boardurl'], -10) == '/index.php') Chris@76: $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10); Chris@76: elseif (substr($_POST['boardurl'], -1) == '/') Chris@76: $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1); Chris@76: if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://') Chris@76: $_POST['boardurl'] = 'http://' . $_POST['boardurl']; Chris@76: } Chris@76: Chris@76: // Any passwords? Chris@76: $config_passwords = array( Chris@76: 'db_passwd', Chris@76: 'ssi_db_passwd', Chris@76: ); Chris@76: Chris@76: // All the strings to write. Chris@76: $config_strs = array( Chris@76: 'mtitle', 'mmessage', Chris@76: 'language', 'mbname', 'boardurl', Chris@76: 'cookiename', Chris@76: 'webmaster_email', Chris@76: 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user', Chris@76: 'boarddir', 'sourcedir', 'cachedir', Chris@76: ); Chris@76: // All the numeric variables. Chris@76: $config_ints = array( Chris@76: ); Chris@76: // All the checkboxes. Chris@76: $config_bools = array( Chris@76: 'db_persist', 'db_error_send', Chris@76: 'maintenance', Chris@76: ); Chris@76: Chris@76: // Now sort everything into a big array, and figure out arrays and etc. Chris@76: $new_settings = array(); Chris@76: foreach ($config_passwords as $config_var) Chris@76: { Chris@76: if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1]) Chris@76: $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\''; Chris@76: } Chris@76: foreach ($config_strs as $config_var) Chris@76: { Chris@76: if (isset($_POST[$config_var])) Chris@76: $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\''; Chris@76: } Chris@76: foreach ($config_ints as $config_var) Chris@76: { Chris@76: if (isset($_POST[$config_var])) Chris@76: $new_settings[$config_var] = (int) $_POST[$config_var]; Chris@76: } Chris@76: foreach ($config_bools as $key) Chris@76: { Chris@76: if (!empty($_POST[$key])) Chris@76: $new_settings[$key] = '1'; Chris@76: else Chris@76: $new_settings[$key] = '0'; Chris@76: } Chris@76: Chris@76: // Save the relevant settings in the Settings.php file. Chris@76: require_once($sourcedir . '/Subs-Admin.php'); Chris@76: updateSettingsFile($new_settings); Chris@76: Chris@76: // Now loopt through the remaining (database-based) settings. Chris@76: $new_settings = array(); Chris@76: foreach ($config_vars as $config_var) Chris@76: { Chris@76: // We just saved the file-based settings, so skip their definitions. Chris@76: if (!is_array($config_var) || $config_var[2] == 'file') Chris@76: continue; Chris@76: Chris@76: // Rewrite the definition a bit. Chris@76: $new_settings[] = array($config_var[3], $config_var[0]); Chris@76: } Chris@76: Chris@76: // Save the new database-based settings, if any. Chris@76: if (!empty($new_settings)) Chris@76: saveDBSettings($new_settings); Chris@76: } Chris@76: Chris@76: // Helper function for saving database settings. Chris@76: function saveDBSettings(&$config_vars) Chris@76: { Chris@76: global $sourcedir, $context; Chris@76: Chris@76: $inlinePermissions = array(); Chris@76: foreach ($config_vars as $var) Chris@76: { Chris@76: if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags'])))) Chris@76: continue; Chris@76: Chris@76: // Checkboxes! Chris@76: elseif ($var[0] == 'check') Chris@76: $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0'; Chris@76: // Select boxes! Chris@76: elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2]))) Chris@76: $setArray[$var[1]] = $_POST[$var[1]]; Chris@76: elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array()) Chris@76: { Chris@76: // For security purposes we validate this line by line. Chris@76: $options = array(); Chris@76: foreach ($_POST[$var[1]] as $invar) Chris@76: if (in_array($invar, array_keys($var[2]))) Chris@76: $options[] = $invar; Chris@76: Chris@76: $setArray[$var[1]] = serialize($options); Chris@76: } Chris@76: // Integers! Chris@76: elseif ($var[0] == 'int') Chris@76: $setArray[$var[1]] = (int) $_POST[$var[1]]; Chris@76: // Floating point! Chris@76: elseif ($var[0] == 'float') Chris@76: $setArray[$var[1]] = (float) $_POST[$var[1]]; Chris@76: // Text! Chris@76: elseif ($var[0] == 'text' || $var[0] == 'large_text') Chris@76: $setArray[$var[1]] = $_POST[$var[1]]; Chris@76: // Passwords! Chris@76: elseif ($var[0] == 'password') Chris@76: { Chris@76: if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1]) Chris@76: $setArray[$var[1]] = $_POST[$var[1]][0]; Chris@76: } Chris@76: // BBC. Chris@76: elseif ($var[0] == 'bbc') Chris@76: { Chris@76: Chris@76: $bbcTags = array(); Chris@76: foreach (parse_bbc(false) as $tag) Chris@76: $bbcTags[] = $tag['tag']; Chris@76: Chris@76: if (!isset($_POST[$var[1] . '_enabledTags'])) Chris@76: $_POST[$var[1] . '_enabledTags'] = array(); Chris@76: elseif (!is_array($_POST[$var[1] . '_enabledTags'])) Chris@76: $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']); Chris@76: Chris@76: $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags'])); Chris@76: } Chris@76: // Permissions? Chris@76: elseif ($var[0] == 'permissions') Chris@76: $inlinePermissions[] = $var[1]; Chris@76: } Chris@76: Chris@76: if (!empty($setArray)) Chris@76: updateSettings($setArray); Chris@76: Chris@76: // If we have inline permissions we need to save them. Chris@76: if (!empty($inlinePermissions) && allowedTo('manage_permissions')) Chris@76: { Chris@76: require_once($sourcedir . '/ManagePermissions.php'); Chris@76: save_inline_permissions($inlinePermissions); Chris@76: } Chris@76: } Chris@76: Chris@76: ?>