'ThemeAdmin',
'settings' => 'SetThemeSettings',
'options' => 'SetThemeOptions',
'remove' => 'RemoveTheme',
'pick' => 'PickTheme',
'install' => 'ThemeInstall',
'edit' => 'EditTheme'
);
// Follow the sa or just go to administration.
if (!empty($subActions[$_GET['sa']]))
$subActions[$_GET['sa']]();
else
$subActions['admin']();
}
// Administrative global settings.
function SetThemeSettings()
{
global $txt, $sc, $context, $settings, $db_prefix, $modSettings;
if (empty($_GET['id']))
return ThemeAdmin();
$_GET['id'] = (int) $_GET['id'];
loadLanguage('Admin');
isAllowedTo('admin_forum');
// Just for navigation, show some nice bar on the left.
adminIndex($settings['theme_id'] == $_GET['id'] ? 'edit_theme_settings' : 'manage_themes');
// Validate inputs/user.
if (empty($_GET['id']))
fatal_lang_error('theme3', false);
// Submitting!
if (isset($_POST['submit']))
{
checkSession();
if (empty($_POST['options']))
$_POST['options'] = array();
if (empty($_POST['default_options']))
$_POST['default_options'] = array();
// Set up the sql query.
$setString = '';
foreach ($_POST['options'] as $opt => $val)
$setString .= "
(0, $_GET[id], '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'),";
foreach ($_POST['default_options'] as $opt => $val)
$setString .= "
(0, 1, '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'),";
// If we're actually inserting something..
if ($setString != '')
{
// Get rid of the last comma.
$setString = substr($setString, 0, strlen($setString) - 1);
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
VALUES $setString", __FILE__, __LINE__);
}
redirectexit('action=theme;sa=settings;id=' . $_GET['id'] . ';sesc=' . $sc);
}
checkSession('get');
// Fetch the smiley sets...
$sets = explode(',', 'none,' . $modSettings['smiley_sets_known']);
$set_names = explode("\n", $txt['smileys_none'] . "\n" . $modSettings['smiley_sets_names']);
$context['smiley_sets'] = array(
'' => $txt['smileys_no_default']
);
foreach ($sets as $i => $set)
$context['smiley_sets'][$set] = $set_names[$i];
$old_id = $settings['theme_id'];
loadTheme($_GET['id'], false);
// Let the theme take care of the settings.
loadTemplate('Settings');
loadSubTemplate('settings');
$context['sub_template'] = 'set_settings';
$context['page_title'] = $txt['theme4'];
foreach ($settings as $setting => $dummy)
{
if (!in_array($setting, array('theme_url', 'theme_dir', 'images_url')))
$settings[$setting] = htmlspecialchars($settings[$setting]);
}
$context['settings'] = $context['theme_settings'];
$context['theme_settings'] = $settings;
foreach ($context['settings'] as $i => $setting)
{
if (!isset($setting['type']) || $setting['type'] == 'bool')
$context['settings'][$i]['type'] = 'checkbox';
elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
$context['settings'][$i]['type'] = 'number';
elseif ($setting['type'] == 'string')
$context['settings'][$i]['type'] = 'text';
if (isset($setting['options']))
$context['settings'][$i]['type'] = 'list';
$context['settings'][$i]['value'] = !isset($settings[$setting['id']]) ? '' : $settings[$setting['id']];
}
loadTheme($old_id, false);
loadTemplate('Themes');
}
// Administrative global settings.
function SetThemeOptions()
{
global $txt, $sc, $context, $settings, $db_prefix, $modSettings;
if (empty($_GET['id']))
return ThemeAdmin();
$_GET['id'] = (int) $_GET['id'];
if (empty($_GET['id']))
fatal_lang_error('theme3', false);
loadLanguage('Profile');
isAllowedTo('admin_forum');
adminIndex('manage_themes');
// Submit?
if (isset($_POST['submit']) && empty($_POST['who']))
{
checkSession();
if (empty($_POST['options']))
$_POST['options'] = array();
if (empty($_POST['default_options']))
$_POST['default_options'] = array();
// Set up the sql query.
$setString = '';
foreach ($_POST['options'] as $opt => $val)
$setString .= "
(-1, $_GET[id], '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'),";
$old_settings = array();
foreach ($_POST['default_options'] as $opt => $val)
{
$old_settings[] = $opt;
$setString .= "
(-1, 1, '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'),";
}
// If we're actually inserting something..
if ($setString != '')
{
// Get rid of the last comma.
$setString = substr($setString, 0, strlen($setString) - 1);
// Are there options in non-default themes set that should be cleared?
if (!empty($old_settings))
db_query("
DELETE FROM {$db_prefix}themes
WHERE ID_THEME != 1
AND ID_MEMBER = -1
AND variable IN ('" . implode("', '", $old_settings) . "')", __FILE__, __LINE__);
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
VALUES $setString", __FILE__, __LINE__);
}
redirectexit('action=theme;sa=options;id=' . $_GET['id'] . ';sesc=' . $sc);
}
elseif (isset($_POST['submit']) && !empty($_POST['who']))
{
checkSession();
if (empty($_POST['options']))
$_POST['options'] = array();
if (empty($_POST['default_options']))
$_POST['default_options'] = array();
$old_settings = array();
foreach ($_POST['default_options'] as $opt => $val)
{
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
SELECT ID_MEMBER, 1, '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'
FROM {$db_prefix}members", __FILE__, __LINE__);
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
VALUES (-1, 1, '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "')", __FILE__, __LINE__);
$old_settings[] = $opt;
}
// Delete options from other themes.
if (!empty($old_settings))
db_query("
DELETE FROM {$db_prefix}themes
WHERE ID_THEME != 1
AND ID_MEMBER != 0
AND variable IN ('" . implode("', '", $old_settings) . "')", __FILE__, __LINE__);
foreach ($_POST['options'] as $opt => $val)
{
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
SELECT ID_MEMBER, $_GET[id], '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "'
FROM {$db_prefix}members", __FILE__, __LINE__);
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
VALUES (-1, $_GET[id], '$opt', '" . (is_array($val) ? implode(',', $val) : $val) . "')", __FILE__, __LINE__);
}
redirectexit('action=theme;sa=options;id=' . $_GET['id'] . ';sesc=' . $sc . ';who=1');
}
checkSession('get');
$old_id = $settings['theme_id'];
loadTheme($_GET['id'], false);
// Let the theme take care of the settings.
loadTemplate('Settings');
loadSubTemplate('options');
$context['sub_template'] = 'set_options';
$context['page_title'] = $txt['theme4'];
$context['options'] = $context['theme_options'];
$context['theme_settings'] = $settings;
if (empty($_REQUEST['who']))
{
$request = db_query("
SELECT variable, value
FROM {$db_prefix}themes
WHERE ID_THEME IN (1, " . $_GET['id'] . ")
AND ID_MEMBER = -1", __FILE__, __LINE__);
$context['theme_options'] = array();
while ($row = mysql_fetch_assoc($request))
$context['theme_options'][$row['variable']] = $row['value'];
mysql_free_result($request);
$context['theme_options_reset'] = false;
}
else
{
$context['theme_options'] = array();
$context['theme_options_reset'] = true;
}
foreach ($context['options'] as $i => $setting)
{
if (!isset($setting['type']) || $setting['type'] == 'bool')
$context['options'][$i]['type'] = 'checkbox';
elseif ($setting['type'] == 'int' || $setting['type'] == 'integer')
$context['options'][$i]['type'] = 'number';
elseif ($setting['type'] == 'string')
$context['options'][$i]['type'] = 'text';
if (isset($setting['options']))
$context['options'][$i]['type'] = 'list';
$context['options'][$i]['value'] = !isset($context['theme_options'][$setting['id']]) ? '' : $context['theme_options'][$setting['id']];
}
loadTheme($old_id, false);
loadTemplate('Themes');
}
function ThemeAdmin()
{
global $txt, $context, $db_prefix, $sc, $boarddir;
loadLanguage('Admin');
isAllowedTo('admin_forum');
adminIndex('manage_themes');
// If we aren't submitting - that is, if we are about to...
if (!isset($_POST['submit']))
{
checkSession('get');
loadTemplate('Themes');
// Load up all the themes.
$context['themes'] = array();
$request = db_query("
SELECT value AS name, ID_THEME
FROM {$db_prefix}themes
WHERE variable = 'name'
AND ID_MEMBER = 0
ORDER BY ID_THEME", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
$context['themes'][] = array(
'id' => $row['ID_THEME'],
'name' => $row['name']
);
}
mysql_free_result($request);
// Can we create a new theme?
$context['can_create_new'] = is_writable($boarddir . '/Themes');
$context['new_theme_dir'] = $boarddir . '/Themes/';
// Look for a non existent theme directory. (ie theme87.)
$theme_dir = $boarddir . '/Themes/theme';
$i = 1;
while (file_exists($theme_dir . $i))
$i++;
$context['new_theme_name'] = 'theme' . $i;
}
else
{
checkSession();
// Commit the new settings.
updateSettings(array(
'theme_allow' => $_POST['options']['theme_allow'],
'theme_default' => $_POST['options']['theme_default'],
'theme_guests' => $_POST['options']['theme_guests']
));
if ((int) $_POST['theme_reset'] != -1)
updateMemberData(null, array('ID_THEME' => (int) $_POST['theme_reset']));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
}
// Remove a theme from the database.
function RemoveTheme()
{
global $db_prefix, $modSettings, $sc;
checkSession('get');
isAllowedTo('admin_forum');
// You can't delete the default theme!
if ($_GET['th'] == 1)
fatal_lang_error(1, false);
// The theme's ID must be an integer.
$_GET['th'] = (int) $_GET['th'];
$known = explode(',', $modSettings['knownThemes']);
for ($i = 0, $n = count($known); $i < $n; $i++)
{
if ($known[$i] == $_GET['th'])
unset($known[$i]);
}
db_query("
DELETE FROM {$db_prefix}themes
WHERE ID_THEME = $_GET[th]", __FILE__, __LINE__);
db_query("
UPDATE {$db_prefix}members
SET ID_THEME = 0
WHERE ID_THEME = $_GET[th]", __FILE__, __LINE__);
$known = strtr(implode(',', $known), array(',,' => ','));
// Fix it if the theme was the overall default theme.
if ($modSettings['theme_guests'] == $_GET['th'])
updateSettings(array('theme_guests' => '1', 'knownThemes' => $known));
else
updateSettings(array('knownThemes' => $known));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
// Choose a theme from a list.
function PickTheme()
{
global $txt, $db_prefix, $sc, $context, $modSettings, $user_info, $ID_MEMBER, $language;
checkSession('get');
loadTemplate('Themes');
loadLanguage('Profile');
$_SESSION['ID_THEME'] = 0;
// Have we made a desicion, or are we just browsing?
if (isset($_GET['id']))
{
// Save for this user.
if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
{
updateMemberData($ID_MEMBER, array('ID_THEME' => (int) $_GET['id']));
redirectexit('action=profile;sa=theme');
}
// For everyone.
elseif ($_REQUEST['u'] == '0')
{
updateMemberData(null, array('ID_THEME' => (int) $_GET['id']));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
// Change the default/guest theme.
elseif ($_REQUEST['u'] == '-1')
{
updateSettings(array('theme_guests' => (int) $_GET['id']));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
// Change a specific member's theme.
else
{
updateMemberData((int) $_REQUEST['u'], array('ID_THEME' => (int) $_GET['id']));
redirectexit('action=profile;u=' . (int) $_REQUEST['u'] . ';sa=theme');
}
}
// Figure out who the member of the minute is, and what theme they've chosen.
if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
{
$context['current_member'] = $ID_MEMBER;
$context['current_theme'] = $user_info['theme'];
}
// Everyone can't chose just one.
elseif ($_REQUEST['u'] == '0')
{
$context['current_member'] = 0;
$context['current_theme'] = 0;
}
// Guests and such...
elseif ($_REQUEST['u'] == '-1')
{
$context['current_member'] = -1;
$context['current_theme'] = $modSettings['theme_guests'];
}
// Someones else :P.
else
{
$context['current_member'] = (int) $_REQUEST['u'];
$request = db_query("
SELECT ID_THEME
FROM {$db_prefix}members
WHERE ID_MEMBER = $context[current_member]
LIMIT 1", __FILE__, __LINE__);
list ($context['current_theme']) = mysql_fetch_row($request);
mysql_free_result($request);
}
// Get the theme name and descriptions.
$context['available_themes'] = array();
if (!empty($modSettings['knownThemes']))
{
$knownThemes = implode("', '", explode(',', $modSettings['knownThemes']));
$request = db_query("
SELECT ID_THEME, variable, value
FROM {$db_prefix}themes
WHERE variable IN ('name', 'theme_url', 'theme_dir', 'images_url')" . (empty($modSettings['theme_default']) && !allowedTo('admin_forum') ? "
AND ID_THEME IN ('$knownThemes')
AND ID_THEME != 1" : '') . "
AND ID_THEME != 0
LIMIT " . count(explode(',', $modSettings['knownThemes'])) * 4, __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
if (!isset($context['available_themes'][$row['ID_THEME']]))
$context['available_themes'][$row['ID_THEME']] = array(
'id' => $row['ID_THEME'],
'selected' => $context['current_theme'] == $row['ID_THEME'],
'num_users' => 0
);
$context['available_themes'][$row['ID_THEME']][$row['variable']] = $row['value'];
}
mysql_free_result($request);
}
// Okay, this is a complicated problem: the default theme is 1, but they aren't allowed to access 1!
if (!isset($context['available_themes'][$modSettings['theme_guests']]))
{
$context['available_themes'][0] = array(
'num_users' => 0
);
$guest_theme = 0;
}
else
$guest_theme = $modSettings['theme_guests'];
$request = db_query("
SELECT COUNT(ID_MEMBER) AS theCount, ID_THEME
FROM {$db_prefix}members
GROUP BY ID_THEME DESC", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
// Figure out which theme it is they are REALLY using.
if ($row['ID_THEME'] == 1 && empty($modSettings['theme_default']))
$row['ID_THEME'] = $guest_theme;
elseif (empty($modSettings['theme_allow']))
$row['ID_THEME'] = $guest_theme;
if (isset($context['available_themes'][$row['ID_THEME']]))
$context['available_themes'][$row['ID_THEME']]['num_users'] += $row['theCount'];
else
$context['available_themes'][$guest_theme]['num_users'] += $row['theCount'];
}
foreach ($context['available_themes'] as $ID_THEME => $theme_data)
{
// Don't try to load the forum or board default theme's data... it doesn't have any!
if ($ID_THEME == 0)
continue;
$settings = $theme_data;
$settings['theme_id'] = $ID_THEME;
if (file_exists($settings['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php'))
include($settings['theme_dir'] . '/languages/Settings.' . $user_info['language'] . '.php');
elseif (file_exists($settings['theme_dir'] . '/languages/Settings.' . $language . '.php'))
include($settings['theme_dir'] . '/languages/Settings.' . $language . '.php');
else
{
$txt['theme_thumbnail_href'] = $settings['images_url'] . '/thumbnail.gif';
$txt['theme_description'] = '';
}
$context['available_themes'][$ID_THEME]['thumbnail_href'] = $txt['theme_thumbnail_href'];
$context['available_themes'][$ID_THEME]['description'] = $txt['theme_description'];
}
// As long as we're not doing the default theme...
if (!isset($_REQUEST['u']) || $_REQUEST['u'] >= 0)
{
if ($guest_theme != 0)
$context['available_themes'][0] = $context['available_themes'][$guest_theme];
$context['available_themes'][0]['id'] = 0;
$context['available_themes'][0]['name'] = $txt['theme_forum_default'];
$context['available_themes'][0]['selected'] = $context['current_theme'] == 0;
$context['available_themes'][0]['description'] = $txt['theme_global_description'];
}
ksort($context['available_themes']);
$context['page_title'] = &$txt['theme_pick'];
$context['sub_template'] = 'pick';
}
function ThemeInstall()
{
global $sourcedir, $boarddir, $boardurl, $db_prefix, $txt, $context, $settings, $modSettings;
checkSession('request');
isAllowedTo('admin_forum');
checkSession('request');
loadTemplate('Themes');
if (isset($_GET['theme_id']))
{
adminIndex('manage_themes');
$context['sub_template'] = 'installed';
$context['page_title'] = $txt['theme_installed'];
$context['installed_theme'] = array(
'id' => (int) $_GET['theme_id'],
'name' => $_GET['theme_name']
);
return;
}
if (!empty($_REQUEST['copy']) && (!isset($_REQUEST['theme_dir']) || stripslashes($_REQUEST['theme_dir']) == $boarddir . '/Themes/' || !file_exists($_REQUEST['theme_dir'])) && (empty($_FILES['theme_gz']) || $_FILES['theme_gz']['error'] == 4) && empty($_REQUEST['theme_gz']))
{
// Hopefully the themes directory is writable, or we might have a problem.
if (!is_writable($boarddir . '/Themes'))
fatal_lang_error('theme_install_write_error');
$theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\- ]~', '', $_REQUEST['copy']);
umask(0);
mkdir($theme_dir, 0777);
// Copy over the default non-theme files.
$to_copy = array('/style.css', '/index.php', '/index.template.php');
foreach ($to_copy as $file)
{
copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
@chmod($theme_dir . $file, 0777);
}
$theme_name = $_REQUEST['copy'];
$images_url = $settings['default_images_url'];
}
elseif (isset($_REQUEST['theme_dir']) && (empty($_FILES['theme_gz']) || $_FILES['theme_gz']['error'] == 4) && empty($_REQUEST['theme_gz']))
{
if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml'))
fatal_lang_error('theme_install_error', false);
$theme_name = basename($_REQUEST['theme_dir']);
$theme_dir = $_REQUEST['theme_dir'];
}
else
{
// Hopefully the themes directory is writable, or we might have a problem.
if (!is_writable($boarddir . '/Themes'))
fatal_lang_error('theme_install_write_error');
require_once($sourcedir . '/Subs-Package.php');
// Set the default settings...
$theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
$theme_name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $theme_name);
$theme_dir = $boarddir . '/Themes/' . $theme_name;
if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']))
$extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name);
elseif (isset($_REQUEST['theme_gz']))
{
// Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false)
fatal_lang_error('not_on_simplemachines');
$extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name);
}
else
redirectexit('action=theme;sa=admin;sesc=' . $context['session_id']);
}
// Something go wrong?
if ($theme_dir != '' && basename($theme_dir) != 'Themes')
{
// Defaults.
$install_info = array(
'theme_url' => $boardurl . '/Themes/' . basename($theme_dir),
'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images',
'theme_dir' => $theme_dir,
'name' => $theme_name
);
if (file_exists($theme_dir . '/theme_info.xml'))
{
$theme_info = implode('', file($theme_dir . '/theme_info.xml'));
$xml_elements = array(
'name' => 'name',
'theme_layers' => 'layers',
'theme_templates' => 'templates',
'based_on' => 'based-on',
);
foreach ($xml_elements as $var => $name)
{
if (preg_match('~<' . $name . '>(?:)?' . $name . '>~', $theme_info, $match) == 1)
$install_info[$var] = $match[1];
}
if (preg_match('~
\', $k - 1), \'
\';
echo \'
\';
}
if (isset($tree[\'extra_before\']))
echo $tree[\'extra_before\'];
echo \'\', $settings[\'linktree_link\'] && isset($tree[\'url\']) ? \'\' . $tree[\'name\'] . \'\' : $tree[\'name\'], \'\';
if (isset($tree[\'extra_after\']))
echo $tree[\'extra_after\'];
// Don\'t show a separator for the last one.
if ($k != count($context[\'linktree\']) - 1)
echo $settings[\'linktree_inline\'] ? \' | \' : \'
\';
}
echo \'\';
}
// Show the menu up top. Something like [home] [help] [profile] [logout]...
function template_menu()
{
global $context, $settings, $options, $scripturl, $txt;
// Show the [home] and [help] buttons.
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[103]), \'\', $context[\'menu_separator\'], \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[119]), \'\', $context[\'menu_separator\'];
// How about the [search] button?
if ($context[\'allow_search\'])
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[182]), \'\', $context[\'menu_separator\'];
// Is the user allowed to administrate at all? ([admin])
if ($context[\'allow_admin\'])
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[2]), \'\', $context[\'menu_separator\'];
// Edit Profile... [profile]
if ($context[\'allow_edit_profile\'])
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[467]), \'\', $context[\'menu_separator\'];
// The [calendar]!
if ($context[\'allow_calendar\'])
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[\'calendar24\']), \'\', $context[\'menu_separator\'];
// If the user is a guest, show [login] and [register] buttons.
if ($context[\'user\'][\'is_guest\'])
{
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[34]), \'\', $context[\'menu_separator\'], \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[97]), \'\';
}
// Otherwise, they might want to [logout]...
else
echo \'
\', ($settings[\'use_image_buttons\'] ? \'
\' : $txt[108]), \'\';
}
?>';
// Step 5: Do the html tag.
$old_template = preg_replace('~\~i', '', $old_template);
// Step 6: The javascript stuff.
$old_template = preg_replace('~\