value).)
- tar and gzip the directory - and you're done!
- please include any special license in a license.txt file.
// !!! Thumbnail?
*/
// Subaction handler.
function ThemesMain()
{
global $txt, $context, $scripturl;
// Load the important language files...
loadLanguage('Themes');
loadLanguage('Settings');
// No funny business - guests only.
is_not_guest();
// Default the page title to Theme Administration by default.
$context['page_title'] = &$txt['themeadmin_title'];
// Theme administration, removal, choice, or installation...
$subActions = array(
'admin' => 'ThemeAdmin',
'list' => 'ThemeList',
'reset' => 'SetThemeOptions',
'settings' => 'SetThemeSettings',
'options' => 'SetThemeOptions',
'install' => 'ThemeInstall',
'remove' => 'RemoveTheme',
'pick' => 'PickTheme',
'edit' => 'EditTheme',
'copy' => 'CopyTemplate',
);
// !!! Layout Settings?
$context['admin_tabs'] = array(
'title' => &$txt['themeadmin_title'],
'help' => 'themes',
'description' => $txt['themeadmin_description'],
'tabs' => array(
'admin' => array(
'title' => $txt['themeadmin_admin_title'],
'description' => $txt['themeadmin_admin_desc'],
'href' => $scripturl . '?action=theme;sesc=' . $context['session_id'] . ';sa=admin',
),
'list' => array(
'title' => $txt['themeadmin_list_title'],
'description' => $txt['themeadmin_list_desc'],
'href' => $scripturl . '?action=theme;sesc=' . $context['session_id'] . ';sa=list',
),
'reset' => array(
'title' => $txt['themeadmin_reset_title'],
'description' => $txt['themeadmin_reset_desc'],
'href' => $scripturl . '?action=theme;sesc=' . $context['session_id'] . ';sa=reset',
),
'edit' => array(
'title' => $txt['themeadmin_edit_title'],
'description' => $txt['themeadmin_edit_desc'],
'href' => $scripturl . '?action=theme;sesc=' . $context['session_id'] . ';sa=edit',
'is_last' => true,
),
),
);
// Follow the sa or just go to administration.
if (!empty($subActions[$_GET['sa']]))
{
if (isset($context['admin_tabs']['tabs'][$_GET['sa']]))
$context['admin_tabs']['tabs'][$_GET['sa']]['is_selected'] = true;
$subActions[$_GET['sa']]();
}
else
{
$context['admin_tabs']['tabs']['admin']['is_selected'] = true;
$subActions['admin']();
}
}
function ThemeAdmin()
{
global $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.
$request = db_query("
SELECT ID_THEME, value AS name
FROM {$db_prefix}themes
WHERE variable = 'name'
AND ID_MEMBER = 0
ORDER BY ID_THEME", __FILE__, __LINE__);
$context['themes'] = array();
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'] = substr(realpath($boarddir . '/Themes/default'), 0, -7);
// 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;sesc=' . $sc . ';sa=admin');
}
}
function ThemeList()
{
global $context, $db_prefix, $boarddir, $boardurl;
loadLanguage('Admin');
isAllowedTo('admin_forum');
if (isset($_POST['submit']))
{
checkSession();
$request = db_query("
SELECT ID_THEME, variable, value
FROM {$db_prefix}themes
WHERE variable IN ('theme_dir', 'theme_url', 'images_url', 'base_theme_dir', 'base_theme_url', 'base_images_url')
AND ID_MEMBER = 0", __FILE__, __LINE__);
$themes = array();
while ($row = mysql_fetch_assoc($request))
$themes[$row['ID_THEME']][$row['variable']] = $row['value'];
mysql_free_result($request);
$_POST['reset_dir'] = stripslashes($_POST['reset_dir']);
$_POST['reset_url'] = stripslashes($_POST['reset_url']);
$setValues = array();
foreach ($themes as $id => $theme)
{
if (file_exists($_POST['reset_dir'] . '/' . basename($theme['theme_dir'])))
{
$setValues[] = "($id, 0, 'theme_dir', '" . addslashes(realpath($_POST['reset_dir'] . '/' . basename($theme['theme_dir']))) . "')";
$setValues[] = "($id, 0, 'theme_url', '" . addslashes($_POST['reset_url'] . '/' . basename($theme['theme_dir'])) . "')";
$setValues[] = "($id, 0, 'images_url', '" . addslashes($_POST['reset_url'] . '/' . basename($theme['theme_dir'])) . "/" . basename($theme['images_url']) . "')";
}
if (isset($theme['base_theme_dir']) && file_exists($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir'])))
{
$setValues[] = "($id, 0, 'base_theme_dir', '" . addslashes(realpath($_POST['reset_dir'] . '/' . basename($theme['base_theme_dir']))) . "')";
$setValues[] = "($id, 0, 'base_theme_url', '" . addslashes($_POST['reset_url'] . '/' . basename($theme['base_theme_dir'])) . "')";
$setValues[] = "($id, 0, 'base_images_url', '" . addslashes($_POST['reset_url'] . '/' . basename($theme['base_theme_dir'])) . "/" . basename($theme['base_images_url']) . "')";
}
cache_put_data('theme_settings-' . $id, null, 90);
}
if (!empty($setValues))
{
db_query("
REPLACE INTO {$db_prefix}themes
(ID_THEME, ID_MEMBER, variable, value)
VALUES " . implode(',
', $setValues), __FILE__, __LINE__);
}
redirectexit('action=theme;sa=list;sesc=' . $context['session_id']);
}
adminIndex('manage_themes');
checkSession('get');
loadTemplate('Themes');
$request = db_query("
SELECT ID_THEME, variable, value
FROM {$db_prefix}themes
WHERE variable IN ('name', 'theme_dir', 'theme_url', 'images_url')
AND ID_MEMBER = 0", __FILE__, __LINE__);
$context['themes'] = array();
while ($row = mysql_fetch_assoc($request))
{
if (!isset($context['themes'][$row['ID_THEME']]))
$context['themes'][$row['ID_THEME']] = array(
'id' => $row['ID_THEME'],
);
$context['themes'][$row['ID_THEME']][$row['variable']] = $row['value'];
}
mysql_free_result($request);
foreach ($context['themes'] as $i => $theme)
{
$context['themes'][$i]['theme_dir'] = realpath($context['themes'][$i]['theme_dir']);
if (file_exists($context['themes'][$i]['theme_dir'] . '/index.template.php'))
{
// Fetch the header... a good 256 bytes should be more than enough.
$fp = fopen($context['themes'][$i]['theme_dir'] . '/index.template.php', 'rb');
$header = fread($fp, 256);
fclose($fp);
// Can we find a version comment, at all?
if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*index(?:[\s]{2}|\*/)~i', $header, $match) == 1)
$context['themes'][$i]['version'] = $match[1];
}
$context['themes'][$i]['valid_path'] = file_exists($context['themes'][$i]['theme_dir']) && is_dir($context['themes'][$i]['theme_dir']);
}
$context['reset_dir'] = realpath($boarddir . '/Themes');
$context['reset_url'] = $boardurl . '/Themes';
$context['sub_template'] = 'list_themes';
}
// Administrative global settings.
function SetThemeOptions()
{
global $txt, $sc, $context, $settings, $db_prefix, $modSettings;
$_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (isset($_GET['id']) ? (int) $_GET['id'] : 0);
isAllowedTo('admin_forum');
adminIndex('manage_themes');
if (empty($_GET['th']) && empty($_GET['id']))
{
checkSession('get');
$request = db_query("
SELECT ID_THEME, variable, value
FROM {$db_prefix}themes
WHERE variable IN ('name', 'theme_dir')
AND ID_MEMBER = 0", __FILE__, __LINE__);
$context['themes'] = array();
while ($row = mysql_fetch_assoc($request))
{
if (!isset($context['themes'][$row['ID_THEME']]))
$context['themes'][$row['ID_THEME']] = array(
'id' => $row['ID_THEME'],
'num_default_options' => 0,
'num_members' => 0,
);
$context['themes'][$row['ID_THEME']][$row['variable']] = $row['value'];
}
mysql_free_result($request);
$request = db_query("
SELECT ID_THEME, COUNT(*) AS value
FROM {$db_prefix}themes
WHERE ID_MEMBER = -1
GROUP BY ID_THEME", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
$context['themes'][$row['ID_THEME']]['num_default_options'] = $row['value'];
mysql_free_result($request);
$request = db_query("
SELECT ID_THEME, COUNT(DISTINCT ID_MEMBER) AS value
FROM {$db_prefix}themes
WHERE ID_MEMBER > 0
GROUP BY ID_THEME", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
$context['themes'][$row['ID_THEME']]['num_members'] = $row['value'];
mysql_free_result($request);
foreach ($context['themes'] as $k => $v)
{
// There has to be a Settings template!
if (!file_exists($v['theme_dir'] . '/Settings.template.php') && empty($v['num_members']))
unset($context['themes'][$k]);
}
loadTemplate('Themes');
$context['sub_template'] = 'reset_list';
return;
}
// 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[th], SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)),";
$old_settings = array();
foreach ($_POST['default_options'] as $opt => $val)
{
$old_settings[] = $opt;
$setString .= "
(-1, 1, SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)),";
}
// If we're actually inserting something..
if ($setString != '')
{
// Get rid of the last comma.
$setString = substr($setString, 0, -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__);
}
cache_put_data('theme_settings-' . $_GET['th'], null, 90);
cache_put_data('theme_settings-1', null, 90);
redirectexit('action=theme;sesc=' . $sc . ';sa=reset');
}
elseif (isset($_POST['submit']) && $_POST['who'] == 1)
{
checkSession();
$_POST['options'] = empty($_POST['options']) ? array() : $_POST['options'];
$_POST['options_master'] = empty($_POST['options_master']) ? array() : $_POST['options_master'];
$_POST['default_options'] = empty($_POST['default_options']) ? array() : $_POST['default_options'];
$_POST['default_options_master'] = empty($_POST['default_options_master']) ? array() : $_POST['default_options_master'];
$old_settings = array();
foreach ($_POST['default_options'] as $opt => $val)
{
if ($_POST['default_options_master'][$opt] == 0)
continue;
elseif ($_POST['default_options_master'][$opt] == 1)
{
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
SELECT ID_MEMBER, 1, SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)
FROM {$db_prefix}members", __FILE__, __LINE__);
$old_settings[] = $opt;
}
elseif ($_POST['default_options_master'][$opt] == 2)
{
db_query("
DELETE FROM {$db_prefix}themes
WHERE variable = '$opt'
AND ID_MEMBER > 0", __FILE__, __LINE__);
}
}
// 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)
{
if ($_POST['options_master'][$opt] == 0)
continue;
elseif ($_POST['options_master'][$opt] == 1)
{
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
SELECT ID_MEMBER, $_GET[th], SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)
FROM {$db_prefix}members", __FILE__, __LINE__);
}
elseif ($_POST['options_master'][$opt] == 2)
{
db_query("
DELETE FROM {$db_prefix}themes
WHERE variable = '$opt'
AND ID_MEMBER > 0
AND ID_THEME = $_GET[th]", __FILE__, __LINE__);
}
}
redirectexit('action=theme;sesc=' . $sc . ';sa=reset');
}
elseif (!empty($_GET['who']) && $_GET['who'] == 2)
{
checkSession('get');
db_query("
DELETE FROM {$db_prefix}themes
WHERE ID_MEMBER > 0
AND ID_THEME = $_GET[th]", __FILE__, __LINE__);
redirectexit('action=theme;sesc=' . $sc . ';sa=reset');
}
checkSession('get');
$old_id = $settings['theme_id'];
$old_settings = $settings;
loadTheme($_GET['th'], false);
loadLanguage('Profile');
// 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['th'] . ")
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']];
}
// Restore the existing theme.
loadTheme($old_id, false);
$settings = $old_settings;
loadTemplate('Themes');
}
// Administrative global settings.
function SetThemeSettings()
{
global $txt, $sc, $context, $settings, $db_prefix, $modSettings;
if (empty($_GET['th']) && empty($_GET['id']))
return ThemeAdmin();
$_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
// Select the best fitting tab.
$context['admin_tabs']['tabs']['list']['is_selected'] = true;
loadLanguage('Admin');
isAllowedTo('admin_forum');
// Just for navigation, show some nice bar on the left.
adminIndex($settings['theme_id'] == $_GET['th'] ? 'edit_theme_settings' : 'manage_themes');
// Validate inputs/user.
if (empty($_GET['th']))
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[th], SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)),";
foreach ($_POST['default_options'] as $opt => $val)
$setString .= "
(0, 1, SUBSTRING('$opt', 1, 255), SUBSTRING('" . (is_array($val) ? implode(',', $val) : $val) . "', 1, 65534)),";
// If we're actually inserting something..
if ($setString != '')
{
// Get rid of the last comma.
$setString = substr($setString, 0, -1);
db_query("
REPLACE INTO {$db_prefix}themes
(ID_MEMBER, ID_THEME, variable, value)
VALUES $setString", __FILE__, __LINE__);
}
cache_put_data('theme_settings-' . $_GET['th'], null, 90);
cache_put_data('theme_settings-1', null, 90);
redirectexit('action=theme;sa=settings;th=' . $_GET['th'] . ';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'];
$old_settings = $settings;
loadTheme($_GET['th'], 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']];
}
// Restore the current theme.
loadTheme($old_id, false);
$settings = $old_settings;
loadTemplate('Themes');
}
// Remove a theme from the database.
function RemoveTheme()
{
global $db_prefix, $modSettings, $sc;
checkSession('get');
isAllowedTo('admin_forum');
// The theme's ID must be an integer.
$_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
// You can't delete the default theme!
if ($_GET['th'] == 1)
fatal_lang_error(1, false);
$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=list;sesc=' . $sc);
}
// Choose a theme from a list.
function PickTheme()
{
global $txt, $db_prefix, $sc, $context, $modSettings, $user_info, $ID_MEMBER, $language;
checkSession('get');
loadLanguage('Profile');
loadTemplate('Themes');
$_SESSION['ID_THEME'] = 0;
if (isset($_GET['id']))
$_GET['th'] = $_GET['id'];
// Have we made a desicion, or are we just browsing?
if (isset($_GET['th']))
{
// Save for this user.
if (!isset($_REQUEST['u']) || !allowedTo('admin_forum'))
{
updateMemberData($ID_MEMBER, array('ID_THEME' => (int) $_GET['th']));
redirectexit('action=profile;sa=theme');
}
// For everyone.
elseif ($_REQUEST['u'] == '0')
{
updateMemberData(null, array('ID_THEME' => (int) $_GET['th']));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
// Change the default/guest theme.
elseif ($_REQUEST['u'] == '-1')
{
updateSettings(array('theme_guests' => (int) $_GET['th']));
redirectexit('action=theme;sa=admin;sesc=' . $sc);
}
// Change a specific member's theme.
else
{
updateMemberData((int) $_REQUEST['u'], array('ID_THEME' => (int) $_GET['th']));
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'])) * 8, __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 ID_THEME, COUNT(*) AS theCount
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'];
}
mysql_free_result($request);
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');
require_once($sourcedir . '/Subs-Package.php');
loadTemplate('Themes');
if (isset($_GET['theme_id']))
{
adminIndex('manage_themes');
$result = db_query("
SELECT value
FROM {$db_prefix}themes
WHERE ID_THEME = " . (int) $_GET['theme_id'] . "
AND ID_MEMBER = 0
AND variable = 'name'
LIMIT 1", __FILE__, __LINE__);
list ($theme_name) = mysql_fetch_row($result);
mysql_free_result($result);
$context['sub_template'] = 'installed';
$context['page_title'] = $txt['theme_installed'];
$context['installed_theme'] = array(
'id' => (int) $_GET['theme_id'],
'name' => $theme_name,
);
return;
}
if ((!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4)) || !empty($_REQUEST['theme_gz']))
$method = 'upload';
elseif (isset($_REQUEST['theme_dir']) && realpath(stripslashes($_REQUEST['theme_dir'])) != realpath($boarddir . '/Themes') && file_exists(stripslashes($_REQUEST['theme_dir'])))
$method = 'path';
else
$method = 'copy';
if (!empty($_REQUEST['copy']) && $method == 'copy')
{
// 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);
}
// And now the entire images directory!
copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
package_flush_cache();
$theme_name = $_REQUEST['copy'];
$images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
$theme_dir = realpath($theme_dir);
}
elseif (isset($_REQUEST['theme_dir']) && $method == 'path')
{
if (!is_dir(stripslashes($_REQUEST['theme_dir'])) || !file_exists(stripslashes($_REQUEST['theme_dir']) . '/theme_info.xml'))
fatal_lang_error('theme_install_error', false);
$theme_name = basename($_REQUEST['theme_dir']);
$theme_dir = stripslashes($_REQUEST['theme_dir']);
}
elseif ($method = 'upload')
{
// 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']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name'])))
$extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
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, false, true);
}
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 = file_get_contents($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('~
$~i', $error) != 0)
$error_file = $theme_dir . '/tmp_' . session_id() . '.php';
else
unlink($theme_dir . '/tmp_' . session_id() . '.php');
}
if (!isset($error_file))
{
$fp = fopen($theme_dir . '/' . $_REQUEST['filename'], 'w');
fwrite($fp, $_POST['entire_file']);
fclose($fp);
redirectexit('action=theme;th=' . $_GET['th'] . ';sesc=' . $context['session_id'] . ';sa=edit;directory=' . dirname($_REQUEST['filename']));
}
}
// Session timed out.
else
{
loadLanguage('Errors');
$context['session_error'] = true;
$context['sub_template'] = 'edit_file';
// Recycle the submitted data.
$context['entire_file'] = htmlspecialchars(stripslashes($_POST['entire_file']));
// You were able to submit it, so it's reasonable to assume you are allowed to save.
$context['allow_save'] = true;
return;
}
}
else
checkSession('get');
$context['allow_save'] = is_writable($theme_dir . '/' . $_REQUEST['filename']);
$context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array($boarddir => '...'));
$context['edit_filename'] = htmlspecialchars($_REQUEST['filename']);
if (substr($_REQUEST['filename'], -4) == '.css')
{
$context['sub_template'] = 'edit_style';
$context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
}
elseif (substr($_REQUEST['filename'], -13) == '.template.php')
{
$context['sub_template'] = 'edit_template';
if (!isset($error_file))
$file_data = file($theme_dir . '/' . $_REQUEST['filename']);
else
{
if (preg_match('~(.+?:.+?).+?(.+?\d+)
$~i', $error, $match) != 0)
$context['parse_error'] = $match[1] . $_REQUEST['filename'] . $match[2];
$file_data = file($error_file);
unlink($error_file);
}
$j = 0;
$context['file_parts'] = array(array('lines' => 0, 'line' => 1, 'data' => ''));
for ($i = 0, $n = count($file_data); $i < $n; $i++)
{
if (isset($file_data[$i + 1]) && substr($file_data[$i + 1], 0, 9) == 'function ')
{
// Try to format the functions a little nicer...
$context['file_parts'][$j]['data'] = trim($context['file_parts'][$j]['data']) . "\n";
if (empty($context['file_parts'][$j]['lines']))
unset($context['file_parts'][$j]);
$context['file_parts'][++$j] = array('lines' => 0, 'line' => $i + 1, 'data' => '');
}
$context['file_parts'][$j]['lines']++;
$context['file_parts'][$j]['data'] .= htmlspecialchars(strtr($file_data[$i], array("\t" => ' ')));
}
$context['entire_file'] = htmlspecialchars(strtr(implode('', $file_data), array("\t" => ' ')));
}
else
{
$context['sub_template'] = 'edit_file';
$context['entire_file'] = htmlspecialchars(strtr(file_get_contents($theme_dir . '/' . $_REQUEST['filename']), array("\t" => ' ')));
}
}
function get_file_listing($path, $relative)
{
global $scripturl, $txt, $context;
$dir = dir($path);
$entries = array();
while ($entry = $dir->read())
$entries[] = $entry;
$dir->close();
natcasesort($entries);
$listing1 = array();
$listing2 = array();
foreach ($entries as $entry)
{
// Skip all dot files, including .htaccess.
if (substr($entry, 0, 1) == '.' || $entry == 'CVS')
continue;
if (is_dir($path . '/' . $entry))
$listing1[] = array(
'filename' => $entry,
'is_writable' => is_writable($path . '/' . $entry),
'is_directory' => true,
'is_template' => false,
'is_image' => false,
'is_editable' => false,
'href' => $scripturl . '?action=theme;th=' . $_GET['th'] . ';sesc=' . $context['session_id'] . ';sa=edit;directory=' . $relative . $entry,
'size' => '',
);
else
{
$size = filesize($path . '/' . $entry);
if ($size > 2048 || $size == 1024)
$size = comma_format($size / 1024) . ' ' . $txt['themeadmin_edit_kilobytes'];
else
$size = comma_format($size) . ' ' . $txt['themeadmin_edit_bytes'];
$listing2[] = array(
'filename' => $entry,
'is_writable' => is_writable($path . '/' . $entry),
'is_directory' => false,
'is_template' => preg_match('~\.template\.php$~', $entry) != 0,
'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0,
'is_editable' => is_writable($path . '/' . $entry) && preg_match('~\.(php|pl|css|js|vbs|xml|xslt|txt|xsl|html|htm|shtm|shtml|asp|aspx|cgi|py)$~', $entry) != 0,
'href' => $scripturl . '?action=theme;th=' . $_GET['th'] . ';sesc=' . $context['session_id'] . ';sa=edit;filename=' . $relative . $entry,
'size' => $size,
'last_modified' => timeformat(filemtime($path . '/' . $entry)),
);
}
}
return array_merge($listing1, $listing2);
}
function CopyTemplate()
{
global $context, $settings, $db_prefix;
isAllowedTo('admin_forum');
loadTemplate('Themes');
adminIndex('manage_themes');
$context['admin_tabs']['tabs']['edit']['is_selected'] = true;
$_GET['th'] = isset($_GET['th']) ? (int) $_GET['th'] : (int) $_GET['id'];
$request = db_query("
SELECT th1.value, th1.ID_THEME, th2.value
FROM {$db_prefix}themes AS th1
LEFT JOIN {$db_prefix}themes AS th2 ON (th2.variable = 'base_theme_dir' AND th2.ID_THEME = $_GET[th])
WHERE th1.variable = 'theme_dir'
AND th1.ID_THEME = $_GET[th]
LIMIT 1", __FILE__, __LINE__);
list ($theme_dir, $context['theme_id'], $base_theme_dir) = mysql_fetch_row($request);
mysql_free_result($request);
if (isset($_REQUEST['template']) && preg_match('~[\./\\\\:\0]~', $_REQUEST['template']) == 0)
{
if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/' . $_REQUEST['template'] . '.template.php'))
$filename = $base_theme_dir . '/' . $_REQUEST['template'] . '.template.php';
elseif (file_exists($settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php'))
$filename = $settings['default_theme_dir'] . '/' . $_REQUEST['template'] . '.template.php';
else
fatal_lang_error(1, false);
$fp = fopen($theme_dir . '/' . $_REQUEST['template'] . '.template.php', 'w');
fwrite($fp, file_get_contents($filename));
fclose($fp);
redirectexit('action=theme;th=' . $context['theme_id'] . ';sesc=' . $context['session_id'] . ';sa=copy');
}
elseif (isset($_REQUEST['lang_file']) && preg_match('~^[^\./\\\\:\0]\.[^\./\\\\:\0]$~', $_REQUEST['lang_file']) != 0)
{
if (!empty($base_theme_dir) && file_exists($base_theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php'))
$filename = $base_theme_dir . '/languages/' . $_REQUEST['template'] . '.php';
elseif (file_exists($settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php'))
$filename = $settings['default_theme_dir'] . '/languages/' . $_REQUEST['template'] . '.php';
else
fatal_lang_error(1, false);
$fp = fopen($theme_dir . '/languages/' . $_REQUEST['lang_file'] . '.php', 'w');
fwrite($fp, file_get_contents($filename));
fclose($fp);
redirectexit('action=theme;th=' . $context['theme_id'] . ';sesc=' . $context['session_id'] . ';sa=copy');
}
$templates = array();
$lang_files = array();
$dir = dir($settings['default_theme_dir']);
while ($entry = $dir->read())
{
if (substr($entry, -13) == '.template.php')
$templates[] = substr($entry, 0, -13);
}
$dir->close();
$dir = dir($settings['default_theme_dir'] . '/languages');
while ($entry = $dir->read())
{
if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches))
$lang_files[] = $matches[1];
}
$dir->close();
if (!empty($base_theme_dir))
{
$dir = dir($base_theme_dir);
while ($entry = $dir->read())
{
if (substr($entry, -13) == '.template.php' && !in_array(substr($entry, 0, -13), $templates))
$templates[] = substr($entry, 0, -13);
}
$dir->close();
if (file_exists($base_theme_dir . '/languages'))
{
$dir = dir($base_theme_dir . '/languages');
while ($entry = $dir->read())
{
if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && !in_array($matches[1], $lang_files))
$lang_files[] = $matches[1];
}
$dir->close();
}
}
natcasesort($templates);
natcasesort($lang_files);
$context['available_templates'] = array();
foreach ($templates as $template)
$context['available_templates'][$template] = array(
'filename' => $template . '.template.php',
'value' => $template,
'already_exists' => false,
'can_copy' => is_writable($theme_dir),
);
$context['available_language_files'] = array();
foreach ($lang_files as $file)
$context['available_language_files'][$file] = array(
'filename' => $file . '.php',
'value' => $file,
'already_exists' => false,
'can_copy' => file_exists($theme_dir . '/languages') ? is_writable($theme_dir . '/languages') : is_writable($theme_dir),
);
$dir = dir($theme_dir);
while ($entry = $dir->read())
{
if (substr($entry, -13) == '.template.php' && isset($context['available_templates'][substr($entry, 0, -13)]))
{
$context['available_templates'][substr($entry, 0, -13)]['already_exists'] = true;
$context['available_templates'][substr($entry, 0, -13)]['can_copy'] = is_writable($theme_dir . '/' . $entry);
}
}
$dir->close();
if (file_exists($theme_dir . '/languages'))
{
$dir = dir($theme_dir . '/languages');
while ($entry = $dir->read())
{
if (preg_match('~^([^\.]+\.[^\.]+)\.php$~', $entry, $matches) && isset($context['available_language_files'][$matches[1]]))
{
$context['available_language_files'][$matches[1]]['already_exists'] = true;
$context['available_language_files'][$matches[1]]['can_copy'] = is_writable($theme_dir . '/languages/' . $entry);
}
}
$dir->close();
}
$context['sub_template'] = 'copy_template';
}
function convert_template($output_dir, $old_template = '')
{
global $boarddir;
if ($old_template == '')
{
// Step 1: Get the template.php file.
if (file_exists($boarddir . '/template.php'))
$old_template = file_get_contents($boarddir . '/template.php');
elseif (file_exists($boarddir . '/template.html'))
$old_template = file_get_contents($boarddir . '/template.html');
else
fatal_lang_error('theme_convert_error');
}
// Step 2: Change any single quotes to \'.
$old_template = strtr($old_template, array('\'' => '\\\''));
// Step 3: Parse out any existing PHP code.
$old_template = preg_replace('~\<\?php(.*)\?\>~es', "phpcodefix('\$1')", $old_template);
// Step 4: Now we add the beginning and end...
$old_template = '\';
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context[\'linktree\'] as $k => $tree)
{
// Show the | | |-[] Folders.
if (!$settings[\'linktree_inline\'])
{
if ($k > 0)
echo str_repeat(\'\', $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('~\