'ModifyLanguages',
'add' => 'AddLanguage',
'settings' => 'ModifyLanguageSettings',
'downloadlang' => 'DownloadLanguage',
'editlang' => 'ModifyLanguage',
);
call_integration_hook('integrate_manage_languages', array(&$config_vars));
// By default we're managing languages.
$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'edit';
$context['sub_action'] = $_REQUEST['sa'];
// Load up all the tabs...
$context[$context['admin_menu_name']]['tab_data'] = array(
'title' => $txt['language_configuration'],
'description' => $txt['language_description'],
);
// Call the right function for this sub-acton.
$subActions[$_REQUEST['sa']]();
}
/**
* Interface for adding a new language
*
* @uses ManageLanguages template, add_language sub-template.
*/
function AddLanguage()
{
global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl;
// Are we searching for new languages courtesy of Simple Machines?
if (!empty($_POST['smf_add_sub']))
{
// Need fetch_web_data.
require_once($sourcedir . '/Subs-Package.php');
$context['smf_search_term'] = htmlspecialchars(trim($_POST['smf_add']));
// We're going to use this URL.
$url = 'http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => '')));
// Load the class file and stick it into an array.
loadClassFile('Class-Package.php');
$language_list = new xmlArray(fetch_web_data($url), true);
// Check it exists.
if (!$language_list->exists('languages'))
$context['smf_error'] = 'no_response';
else
{
$language_list = $language_list->path('languages[0]');
$lang_files = $language_list->set('language');
$context['smf_languages'] = array();
foreach ($lang_files as $file)
{
// Were we searching?
if (!empty($context['smf_search_term']) && strpos($file->fetch('name'), $smcFunc['strtolower']($context['smf_search_term'])) === false)
continue;
$context['smf_languages'][] = array(
'id' => $file->fetch('id'),
'name' => $smcFunc['ucwords']($file->fetch('name')),
'version' => $file->fetch('version'),
'utf8' => $file->fetch('utf8'),
'description' => $file->fetch('description'),
'link' => $scripturl . '?action=admin;area=languages;sa=downloadlang;did=' . $file->fetch('id') . ';' . $context['session_var'] . '=' . $context['session_id'],
);
}
if (empty($context['smf_languages']))
$context['smf_error'] = 'no_files';
}
}
$context['sub_template'] = 'add_language';
}
/**
* Download a language file from the Simple Machines website.
* Requires a valid download ID ("did") in the URL.
* Also handles installing language files.
* Attempts to chmod things as needed.
* Uses a standard list to display information about all the files and where they'll be put.
*
* @uses ManageLanguages template, download_language sub-template.
* @uses Admin template, show_list sub-template.
*/
function DownloadLanguage()
{
global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl, $modSettings;
loadLanguage('ManageSettings');
require_once($sourcedir . '/Subs-Package.php');
// Clearly we need to know what to request.
if (!isset($_GET['did']))
fatal_lang_error('no_access', false);
// Some lovely context.
$context['download_id'] = $_GET['did'];
$context['sub_template'] = 'download_language';
$context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'add';
// Can we actually do the installation - and do they want to?
if (!empty($_POST['do_install']) && !empty($_POST['copy_file']))
{
checkSession('get');
validateToken('admin-dlang');
$chmod_files = array();
$install_files = array();
// Check writable status.
foreach ($_POST['copy_file'] as $file)
{
// Check it's not very bad.
if (strpos($file, '..') !== false || (substr($file, 0, 6) != 'Themes' && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file)))
fatal_error($txt['languages_download_illegal_paths']);
$chmod_files[] = $boarddir . '/' . $file;
$install_files[] = $file;
}
// Call this in case we have work to do.
$file_status = create_chmod_control($chmod_files);
$files_left = $file_status['files']['notwritable'];
// Something not writable?
if (!empty($files_left))
$context['error_message'] = $txt['languages_download_not_chmod'];
// Otherwise, go go go!
elseif (!empty($install_files))
{
$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);
// Make sure the files aren't stuck in the cache.
package_flush_cache();
$context['install_complete'] = sprintf($txt['languages_download_complete_desc'], $scripturl . '?action=admin;area=languages');
return;
}
}
// Open up the old china.
if (!isset($archive_content))
$archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), null);
if (empty($archive_content))
fatal_error($txt['add_language_error_no_response']);
// Now for each of the files, let's do some *stuff*
$context['files'] = array(
'lang' => array(),
'other' => array(),
);
$context['make_writable'] = array();
foreach ($archive_content as $file)
{
$dirname = dirname($file['filename']);
$filename = basename($file['filename']);
$extension = substr($filename, strrpos($filename, '.') + 1);
// Don't do anything with files we don't understand.
if (!in_array($extension, array('php', 'jpg', 'gif', 'jpeg', 'png', 'txt')))
continue;
// Basic data.
$context_data = array(
'name' => $filename,
'destination' => $boarddir . '/' . $file['filename'],
'generaldest' => $file['filename'],
'size' => $file['size'],
// Does chmod status allow the copy?
'writable' => false,
// Should we suggest they copy this file?
'default_copy' => true,
// Does the file already exist, if so is it same or different?
'exists' => false,
);
// Does the file exist, is it different and can we overwrite?
if (file_exists($boarddir . '/' . $file['filename']))
{
if (is_writable($boarddir . '/' . $file['filename']))
$context_data['writable'] = true;
// Finally, do we actually think the content has changed?
if ($file['size'] == filesize($boarddir . '/' . $file['filename']) && $file['md5'] == md5_file($boarddir . '/' . $file['filename']))
{
$context_data['exists'] = 'same';
$context_data['default_copy'] = false;
}
// Attempt to discover newline character differences.
elseif ($file['md5'] == md5(preg_replace("~[\r]?\n~", "\r\n", file_get_contents($boarddir . '/' . $file['filename']))))
{
$context_data['exists'] = 'same';
$context_data['default_copy'] = false;
}
else
$context_data['exists'] = 'different';
}
// No overwrite?
else
{
// Can we at least stick it in the directory...
if (is_writable($boarddir . '/' . $dirname))
$context_data['writable'] = true;
}
// 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...
if ($extension == 'php' && preg_match('~\w+\.\w+(?:-utf8)?\.php~', $filename))
{
$context_data += array(
'version' => '??',
'cur_version' => false,
'version_compare' => 'newer',
);
list ($name, $language) = explode('.', $filename);
// Let's get the new version, I like versions, they tell me that I'm up to date.
if (preg_match('~\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '~i', $file['preview'], $match) == 1)
$context_data['version'] = $match[1];
// Now does the old file exist - if so what is it's version?
if (file_exists($boarddir . '/' . $file['filename']))
{
// OK - what is the current version?
$fp = fopen($boarddir . '/' . $file['filename'], 'rb');
$header = fread($fp, 768);
fclose($fp);
// Find the version.
if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
{
$context_data['cur_version'] = $match[1];
// How does this compare?
if ($context_data['cur_version'] == $context_data['version'])
$context_data['version_compare'] = 'same';
elseif ($context_data['cur_version'] > $context_data['version'])
$context_data['version_compare'] = 'older';
// Don't recommend copying if the version is the same.
if ($context_data['version_compare'] != 'newer')
$context_data['default_copy'] = false;
}
}
// Add the context data to the main set.
$context['files']['lang'][] = $context_data;
}
else
{
// If we think it's a theme thing, work out what the theme is.
if (substr($dirname, 0, 6) == 'Themes' && preg_match('~Themes[\\/]([^\\/]+)[\\/]~', $dirname, $match))
$theme_name = $match[1];
else
$theme_name = 'misc';
// Assume it's an image, could be an acceptance note etc but rare.
$context['files']['images'][$theme_name][] = $context_data;
}
// Collect together all non-writable areas.
if (!$context_data['writable'])
$context['make_writable'][] = $context_data['destination'];
}
// So, I'm a perfectionist - let's get the theme names.
$theme_indexes = array();
foreach ($context['files']['images'] as $k => $dummy)
$indexes[] = $k;
$context['theme_names'] = array();
if (!empty($indexes))
{
$value_data = array(
'query' => array(),
'params' => array(),
);
foreach ($indexes as $k => $index)
{
$value_data['query'][] = 'value LIKE {string:value_' . $k . '}';
$value_data['params']['value_' . $k] = '%' . $index;
}
$request = $smcFunc['db_query']('', '
SELECT id_theme, value
FROM {db_prefix}themes
WHERE id_member = {int:no_member}
AND variable = {string:theme_dir}
AND (' . implode(' OR ', $value_data['query']) . ')',
array_merge($value_data['params'], array(
'no_member' => 0,
'theme_dir' => 'theme_dir',
'index_compare_explode' => 'value LIKE \'%' . implode('\' OR value LIKE \'%', $indexes) . '\'',
))
);
$themes = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Find the right one.
foreach ($indexes as $index)
if (strpos($row['value'], $index) !== false)
$themes[$row['id_theme']] = $index;
}
$smcFunc['db_free_result']($request);
if (!empty($themes))
{
// Now we have the id_theme we can get the pretty description.
$request = $smcFunc['db_query']('', '
SELECT id_theme, value
FROM {db_prefix}themes
WHERE id_member = {int:no_member}
AND variable = {string:name}
AND id_theme IN ({array_int:theme_list})',
array(
'theme_list' => array_keys($themes),
'no_member' => 0,
'name' => 'name',
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Now we have it...
$context['theme_names'][$themes[$row['id_theme']]] = $row['value'];
}
$smcFunc['db_free_result']($request);
}
}
// Before we go to far can we make anything writable, eh, eh?
if (!empty($context['make_writable']))
{
// What is left to be made writable?
$file_status = create_chmod_control($context['make_writable']);
$context['still_not_writable'] = $file_status['files']['notwritable'];
// Mark those which are now writable as such.
foreach ($context['files'] as $type => $data)
{
if ($type == 'lang')
{
foreach ($data as $k => $file)
if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable']))
$context['files'][$type][$k]['writable'] = true;
}
else
{
foreach ($data as $theme => $files)
foreach ($files as $k => $file)
if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable']))
$context['files'][$type][$theme][$k]['writable'] = true;
}
}
// Are we going to need more language stuff?
if (!empty($context['still_not_writable']))
loadLanguage('Packages');
}
// This is the list for the main files.
$listOptions = array(
'id' => 'lang_main_files_list',
'title' => $txt['languages_download_main_files'],
'get_items' => array(
'function' => create_function('', '
global $context;
return $context[\'files\'][\'lang\'];
'),
),
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['languages_download_filename'],
),
'data' => array(
'function' => create_function('$rowData', '
global $context, $txt;
return \'\' . $rowData[\'name\'] . \'
\' . $txt[\'languages_download_dest\'] . \': \' . $rowData[\'destination\'] . \'\' . ($rowData[\'version_compare\'] == \'older\' ? \'
\' . $txt[\'languages_download_older\'] : \'\');
'),
),
),
'writable' => array(
'header' => array(
'value' => $txt['languages_download_writable'],
),
'data' => array(
'function' => create_function('$rowData', '
global $txt;
return \'\' . ($rowData[\'writable\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'\';
'),
'style' => 'text-align: center',
),
),
'version' => array(
'header' => array(
'value' => $txt['languages_download_version'],
),
'data' => array(
'function' => create_function('$rowData', '
global $txt;
return \'\' . $rowData[\'version\'] . \'\';
'),
),
),
'exists' => array(
'header' => array(
'value' => $txt['languages_download_exists'],
),
'data' => array(
'function' => create_function('$rowData', '
global $txt;
return $rowData[\'exists\'] ? ($rowData[\'exists\'] == \'same\' ? $txt[\'languages_download_exists_same\'] : $txt[\'languages_download_exists_different\']) : $txt[\'no\'];
'),
),
),
'copy' => array(
'header' => array(
'value' => $txt['languages_download_copy'],
),
'data' => array(
'function' => create_function('$rowData', '
return \'\';
'),
'style' => 'text-align: center; width: 4%;',
),
),
),
);
// Kill the cache, as it is now invalid..
if (!empty($modSettings['cache_enable']))
{
cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
}
require_once($sourcedir . '/Subs-List.php');
createList($listOptions);
$context['default_list'] = 'lang_main_files_list';
createToken('admin-dlang');
}
/**
* This lists all the current languages and allows editing of them.
*/
function ModifyLanguages()
{
global $txt, $context, $scripturl;
global $user_info, $smcFunc, $sourcedir, $language, $boarddir, $forum_version;
// Setting a new default?
if (!empty($_POST['set_default']) && !empty($_POST['def_language']))
{
checkSession();
validateToken('admin-lang');
if ($_POST['def_language'] != $language)
{
require_once($sourcedir . '/Subs-Admin.php');
updateSettingsFile(array('language' => '\'' . $_POST['def_language'] . '\''));
$language = $_POST['def_language'];
}
}
// Create another one time token here.
createToken('admin-lang');
$listOptions = array(
'id' => 'language_list',
'items_per_page' => 20,
'base_href' => $scripturl . '?action=admin;area=languages',
'title' => $txt['edit_languages'],
'get_items' => array(
'function' => 'list_getLanguages',
),
'get_count' => array(
'function' => 'list_getNumLanguages',
),
'columns' => array(
'default' => array(
'header' => array(
'value' => $txt['languages_default'],
),
'data' => array(
'function' => create_function('$rowData', '
return \'\';
'),
'style' => 'text-align: center; width: 8%;',
),
),
'name' => array(
'header' => array(
'value' => $txt['languages_lang_name'],
),
'data' => array(
'function' => create_function('$rowData', '
global $scripturl, $context;
return sprintf(\'%3$s\', $scripturl, $rowData[\'id\'], $rowData[\'name\']);
'),
),
),
'character_set' => array(
'header' => array(
'value' => $txt['languages_character_set'],
),
'data' => array(
'db_htmlsafe' => 'char_set',
),
),
'count' => array(
'header' => array(
'value' => $txt['languages_users'],
),
'data' => array(
'db_htmlsafe' => 'count',
'style' => 'text-align: center',
),
),
'locale' => array(
'header' => array(
'value' => $txt['languages_locale'],
),
'data' => array(
'db_htmlsafe' => 'locale',
),
),
),
'form' => array(
'href' => $scripturl . '?action=admin;area=languages',
'token' => 'admin-lang',
),
'additional_rows' => array(
array(
'position' => 'below_table_data',
'value' => '',
'style' => 'text-align: right;',
),
),
// For highlighting the default.
'javascript' => '
var prevClass = "";
var prevDiv = "";
function highlightSelected(box)
{
if (prevClass != "")
prevDiv.className = prevClass;
prevDiv = document.getElementById(box);
prevClass = prevDiv.className;
prevDiv.className = "highlight2";
}
highlightSelected("list_language_list_' . ($language == '' ? 'english' : $language). '");
',
);
// Display a warning if we cannot edit the default setting.
if (!is_writable($boarddir . '/Settings.php'))
$listOptions['additional_rows'][] = array(
'position' => 'after_title',
'value' => $txt['language_settings_writable'],
'class' => 'smalltext alert',
);
require_once($sourcedir . '/Subs-List.php');
createList($listOptions);
$context['sub_template'] = 'show_list';
$context['default_list'] = 'language_list';
}
/**
* How many languages?
* Callback for the list in ManageLanguageSettings().
*/
function list_getNumLanguages()
{
global $settings;
// Return how many we have.
return count(getLanguages(true, false));
}
/**
* Fetch the actual language information.
* Callback for $listOptions['get_items']['function'] in ManageLanguageSettings.
* Determines which languages are available by looking for the "index.{language}.php" file.
* Also figures out how many users are using a particular language.
*/
function list_getLanguages()
{
global $settings, $smcFunc, $language, $context, $txt;
$languages = array();
// Keep our old entries.
$old_txt = $txt;
$backup_actual_theme_dir = $settings['actual_theme_dir'];
$backup_base_theme_dir = !empty($settings['base_theme_dir']) ? $settings['base_theme_dir'] : '';
// Override these for now.
$settings['actual_theme_dir'] = $settings['base_theme_dir'] = $settings['default_theme_dir'];
getLanguages(true, false);
// Put them back.
$settings['actual_theme_dir'] = $backup_actual_theme_dir;
if (!empty($backup_base_theme_dir))
$settings['base_theme_dir'] = $backup_base_theme_dir;
else
unset($settings['base_theme_dir']);
// Get the language files and data...
foreach ($context['languages'] as $lang)
{
// Load the file to get the character set.
require($settings['default_theme_dir'] . '/languages/index.' . $lang['filename'] . '.php');
$languages[$lang['filename']] = array(
'id' => $lang['filename'],
'count' => 0,
'char_set' => $txt['lang_character_set'],
'default' => $language == $lang['filename'] || ($language == '' && $lang['filename'] == 'english'),
'locale' => $txt['lang_locale'],
'name' => $smcFunc['ucwords'](strtr($lang['filename'], array('_' => ' ', '-utf8' => ''))),
);
}
// Work out how many people are using each language.
$request = $smcFunc['db_query']('', '
SELECT lngfile, COUNT(*) AS num_users
FROM {db_prefix}members
GROUP BY lngfile',
array(
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Default?
if (empty($row['lngfile']) || !isset($languages[$row['lngfile']]))
$row['lngfile'] = $language;
if (!isset($languages[$row['lngfile']]) && isset($languages['english']))
$languages['english']['count'] += $row['num_users'];
elseif (isset($languages[$row['lngfile']]))
$languages[$row['lngfile']]['count'] += $row['num_users'];
}
$smcFunc['db_free_result']($request);
// Restore the current users language.
$txt = $old_txt;
// Return how many we have.
return $languages;
}
/**
* Edit language related settings.
*
* @param bool $return_config = false
*/
function ModifyLanguageSettings($return_config = false)
{
global $scripturl, $context, $txt, $boarddir, $settings, $smcFunc, $sourcedir;
// We'll want to save them someday.
require_once $sourcedir . '/ManageServer.php';
// Warn the user if the backup of Settings.php failed.
$settings_not_writable = !is_writable($boarddir . '/Settings.php');
$settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php');
/* If you're writing a mod, it's a bad idea to add things here....
For each option:
variable name, description, type (constant), size/possible values, helptext.
OR an empty string for a horizontal rule.
OR a string for a titled section. */
$config_vars = array(
'language' => array('language', $txt['default_language'], 'file', 'select', array(), null, 'disabled' => $settings_not_writable),
array('userLanguage', $txt['userLanguage'], 'db', 'check', null, 'userLanguage'),
);
call_integration_hook('integrate_language_settings', array(&$config_vars));
if ($return_config)
return $config_vars;
// Get our languages. No cache and use utf8.
getLanguages(false, false);
foreach ($context['languages'] as $lang)
$config_vars['language'][4][$lang['filename']] = array($lang['filename'], strtr($lang['name'], array('-utf8' => ' (UTF-8)')));
// Saving settings?
if (isset($_REQUEST['save']))
{
checkSession();
call_integration_hook('integrate_save_language_settings');
saveSettings($config_vars);
redirectexit('action=admin;area=languages;sa=settings');
}
// Setup the template stuff.
$context['post_url'] = $scripturl . '?action=admin;area=languages;sa=settings;save';
$context['settings_title'] = $txt['language_settings'];
$context['save_disabled'] = $settings_not_writable;
if ($settings_not_writable)
$context['settings_message'] = '