diff --git a/sources/Dispatcher.class.php b/sources/Dispatcher.class.php index 76a61a7e4d..6df5939806 100644 --- a/sources/Dispatcher.class.php +++ b/sources/Dispatcher.class.php @@ -271,7 +271,7 @@ public function __construct() // @todo remove this? if (!empty($settings['catch_action'])) { - $this->_file_name = ADMINDIR . '/Themes.php'; + $this->_file_name = SUBSDIR . '/Themes.subs.php'; $this->_function_name = 'WrapAction'; } else diff --git a/sources/admin/Themes.php b/sources/admin/Themes.php index 27adbd9ebe..75b16c2edc 100644 --- a/sources/admin/Themes.php +++ b/sources/admin/Themes.php @@ -361,17 +361,7 @@ public function action_options() elseif ($_POST['default_options_master'][$opt] == 1) { // Delete then insert for ease of database compatibility! - $db->query('substring', ' - DELETE FROM {db_prefix}themes - WHERE id_theme = {int:default_theme} - AND id_member != {int:no_member} - AND variable = SUBSTRING({string:option}, 1, 255)', - array( - 'default_theme' => 1, - 'no_member' => 0, - 'option' => $opt, - ) - ); + removeThemeOptions(true, false, $opt); addThemeOptions(1, $opt, $val); $old_settings[] = $opt; @@ -428,18 +418,8 @@ public function action_options() // Don't delete custom fields!! if ($_GET['th'] == 1) - { - $request = $db->query('', ' - SELECT col_name - FROM {db_prefix}custom_fields', - array( - ) - ); - $customFields = array(); - while ($row = $db->fetch_assoc($request)) - $customFields[] = $row['col_name']; - $db->free_result($request); - } + $customFields = loadCustomFields(); + $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})'); $db->query('', ' @@ -548,7 +528,7 @@ public function action_setthemesettings() { global $txt, $context, $settings, $modSettings; - $db = database(); + require_once(SUBSDIR . '/Themes.subs.php'); if (empty($_GET['th']) && empty($_GET['id'])) return $this->action_admin(); @@ -639,14 +619,8 @@ public function action_setthemesettings() $inserts[] = array(0, 1, $opt, is_array($val) ? implode(',', $val) : $val); // If we're actually inserting something.. if (!empty($inserts)) - { - $db->insert('replace', - '{db_prefix}themes', - array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), - $inserts, - array('id_member', 'id_theme', 'variable') - ); - } + updateThemeOptions($inserts); + cache_put_data('theme_settings-' . $_GET['th'], null, 90); cache_put_data('theme_settings-1', null, 90); @@ -727,7 +701,7 @@ public function action_remove() { global $modSettings, $context; - $db = database(); + require_once(SUBSDIR . '/Themes.subs.php'); checkSession('get'); @@ -748,33 +722,7 @@ public function action_remove() unset($known[$i]); } - $db->query('', ' - DELETE FROM {db_prefix}themes - WHERE id_theme = {int:current_theme}', - array( - 'current_theme' => $_GET['th'], - ) - ); - - $db->query('', ' - UPDATE {db_prefix}members - SET id_theme = {int:default_theme} - WHERE id_theme = {int:current_theme}', - array( - 'default_theme' => 0, - 'current_theme' => $_GET['th'], - ) - ); - - $db->query('', ' - UPDATE {db_prefix}boards - SET id_theme = {int:default_theme} - WHERE id_theme = {int:current_theme}', - array( - 'default_theme' => 0, - 'current_theme' => $_GET['th'], - ) - ); + deleteTheme($_GET['th']); $known = strtr(implode(',', $known), array(',,' => ',')); @@ -800,6 +748,7 @@ public function action_pick() { global $txt, $context, $modSettings, $user_info, $language, $settings, $scripturl; + require_once(SUBSDIR . '/Themes.subs.php'); $db = database(); loadLanguage('Profile'); @@ -842,12 +791,8 @@ public function action_pick() // A variants to save for the user? if (!empty($_GET['vrt'])) { - $db->insert('replace', - '{db_prefix}themes', - array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), - array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt']), - array('id_theme', 'id_member', 'variable') - ); + updateThemeOptions(array($_GET['th'], $user_info['id'], 'theme_variant', $_GET['vrt'])); + cache_put_data('theme_settings-' . $_GET['th'] . ':' . $user_info['id'], null, 90); $_SESSION['id_variant'] = 0; @@ -859,13 +804,7 @@ public function action_pick() // If changing members or guests - and there's a variant - assume changing default variant. if (!empty($_GET['vrt']) && ($_REQUEST['u'] == '0' || $_REQUEST['u'] == '-1')) { - $db->insert('replace', - '{db_prefix}themes', - array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), - array($_GET['th'], 0, 'default_variant', $_GET['vrt']), - array('id_theme', 'id_member', 'variable') - ); - + updateThemeOptions(array($_GET['th'], 0, 'default_variant', $_GET['vrt'])); // Make it obvious that it's changed cache_put_data('theme_settings-' . $_GET['th'], null, 90); } @@ -877,17 +816,7 @@ public function action_pick() // Remove any custom variants. if (!empty($_GET['vrt'])) - { - $db->query('', ' - DELETE FROM {db_prefix}themes - WHERE id_theme = {int:current_theme} - AND variable = {string:theme_variant}', - array( - 'current_theme' => (int) $_GET['th'], - 'theme_variant' => 'theme_variant', - ) - ); - } + deleteVariants((int) $_GET['th']); redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']); } @@ -909,12 +838,7 @@ public function action_pick() if (!empty($_GET['vrt'])) { - $db->insert('replace', - '{db_prefix}themes', - array('id_theme' => 'int', 'id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), - array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt']), - array('id_theme', 'id_member', 'variable') - ); + updateThemeOptions(array($_GET['th'], (int) $_REQUEST['u'], 'theme_variant', $_GET['vrt'])); cache_put_data('theme_settings-' . $_GET['th'] . ':' . (int) $_REQUEST['u'], null, 90); if ($user_info['id'] == $_REQUEST['u']) @@ -1142,33 +1066,20 @@ public function action_install() isAllowedTo('admin_forum'); checkSession('request'); + require_once(SUBSDIR . '/Themes.subs.php'); require_once(SUBSDIR . '/Package.subs.php'); loadTemplate('Themes'); if (isset($_GET['theme_id'])) { - $result = $db->query('', ' - SELECT value - FROM {db_prefix}themes - WHERE id_theme = {int:current_theme} - AND id_member = {int:no_member} - AND variable = {string:name} - LIMIT 1', - array( - 'current_theme' => (int) $_GET['theme_id'], - 'no_member' => 0, - 'name' => 'name', - ) - ); - list ($theme_name) = $db->fetch_row($result); - $db->free_result($result); + $_GET['theme_id'] = (int) $_GET['theme_id']; $context['sub_template'] = 'installed'; $context['page_title'] = $txt['theme_installed']; $context['installed_theme'] = array( - 'id' => (int) $_GET['theme_id'], - 'name' => $theme_name, + 'id' => $_GET['theme_id'], + 'name' => getThemeName($_GET['theme_id']), ); return; @@ -1404,29 +1315,14 @@ public function action_install() } // Find the newest id_theme. - $result = $db->query('', ' - SELECT MAX(id_theme) - FROM {db_prefix}themes', - array( - ) - ); - list ($id_theme) = $db->fetch_row($result); - $db->free_result($result); - - // This will be theme number... - $id_theme++; + $id_theme = nextTheme(); $inserts = array(); foreach ($install_info as $var => $val) $inserts[] = array($id_theme, $var, $val); if (!empty($inserts)) - $db->insert('insert', - '{db_prefix}themes', - array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), - $inserts, - array('id_theme', 'variable') - ); + addTheme($inserts); updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ',')))); } @@ -2126,46 +2022,4 @@ private function prepareThemeEditContext($theme_dir) $context['allow_save_filename'] = strtr($theme_dir . '/' . $_REQUEST['filename'], array(BOARDDIR => '...')); $context['edit_filename'] = htmlspecialchars($_REQUEST['filename']); } -} - -/** - * Possibly the simplest and best example of how to use the template system. - * - allows the theme to take care of actions. - * - happens if $settings['catch_action'] is set and action isn't found - * in the action array. - * - can use a template, layers, sub_template, filename, and/or function. - * @todo look at this - */ -function WrapAction() -{ - global $context, $settings; - - // Load any necessary template(s)? - if (isset($settings['catch_action']['template'])) - { - // Load both the template and language file. (but don't fret if the language file isn't there...) - loadTemplate($settings['catch_action']['template']); - loadLanguage($settings['catch_action']['template'], '', false); - } - - // Any special layers? - if (isset($settings['catch_action']['layers'])) - { - $template_layers = Template_Layers::getInstance(); - foreach ($settings['catch_action']['layers'] as $layer) - $template_layers->add($layer); - } - - // Just call a function? - if (isset($settings['catch_action']['function'])) - { - if (isset($settings['catch_action']['filename'])) - template_include(SOURCEDIR . '/' . $settings['catch_action']['filename'], true); - - $settings['catch_action']['function'](); - } - // And finally, the main sub template ;). - elseif (isset($settings['catch_action']['sub_template'])) - $context['sub_template'] = $settings['catch_action']['sub_template']; -} - +} \ No newline at end of file diff --git a/sources/subs/Themes.subs.php b/sources/subs/Themes.subs.php index 091a6aa0ce..2c9c02995d 100644 --- a/sources/subs/Themes.subs.php +++ b/sources/subs/Themes.subs.php @@ -408,16 +408,8 @@ function countConfiguredMemberOptions() $themes = array(); // Need to make sure we don't do custom fields. - $request = $db->query('', ' - SELECT col_name - FROM {db_prefix}custom_fields', - array( - ) - ); - $customFields = array(); - while ($row = $db->fetch_assoc($request)) - $customFields[] = $row['col_name']; - $db->free_result($request); + $customFields = loadCustomFields(); + $customFieldsQuery = empty($customFields) ? '' : ('AND variable NOT IN ({array_string:custom_fields})'); $request = $db->query('themes_count', ' @@ -441,8 +433,8 @@ function countConfiguredMemberOptions() /** * Deletes all outdated options from the themes table * - * @param bol $default_theme -> true is default, false for all custom themes - * @param bol $membergroups -> true is for members, false for guests + * @param bool $default_theme -> true is default, false for all custom themes + * @param bool $membergroups -> true is for members, false for guests * @param array $old_settings */ function removeThemeOptions($default_theme, $membergroups, $old_settings) @@ -457,12 +449,17 @@ function removeThemeOptions($default_theme, $membergroups, $old_settings) $mem_param = array('operator' => '=', 'id' => -1); else $mem_param = array('operator' => '>', 'id' => 0); + + if (is_array($old_settings)) + $var = 'variable IN ({array_string:old_settings})'; + else + $var = 'variable = {string:old_settings}'; $db->query('', ' DELETE FROM {db_prefix}themes WHERE id_theme '. $default . ' {int:default_theme} AND id_member ' . $mem_param['operator'] . ' {int:guest_member} - AND variable IN ({array_string:old_settings})', + AND ' . $var, array( 'default_theme' => 1, 'guest_member' => $mem_param['id'], @@ -532,4 +529,203 @@ function addThemeOptions($id_theme, $options, $value) 'value' => (is_array($value) ? implode(',', $value) : $value), ) ); +} + +/** + * Loads all the custom profile fields. + * + * @return array + */ +function loadCustomFields() +{ + $db = database(); + + $request = $db->query('', ' + SELECT col_name + FROM {db_prefix}custom_fields', + array( + ) + ); + $customFields = array(); + while ($row = $db->fetch_assoc($request)) + $customFields[] = $row['col_name']; + $db->free_result($request); + + return $customFields; +} + +/** + * Deletes a theme from the database. + * + * @param int $id + */ +function deleteTheme($id) +{ + $db = database(); + + // Make sure we never ever delete the default theme! + if ($id === 1) + fatal_lang_error('no_access', false); + + $db->query('', ' + DELETE FROM {db_prefix}themes + WHERE id_theme = {int:current_theme}', + array( + 'current_theme' => $id, + ) + ); + + // Update the members ... + $db->query('', ' + UPDATE {db_prefix}members + SET id_theme = {int:default_theme} + WHERE id_theme = {int:current_theme}', + array( + 'default_theme' => 0, + 'current_theme' => $id, + ) + ); + + // ... and the boards table. + $db->query('', ' + UPDATE {db_prefix}boards + SET id_theme = {int:default_theme} + WHERE id_theme = {int:current_theme}', + array( + 'default_theme' => 0, + 'current_theme' => $id, + ) + ); +} + +/** + * Get the next free id for the theme. + * + * @return int + */ +function nextTheme() +{ + $db = database(); + + // Find the newest id_theme. + $result = $db->query('', ' + SELECT MAX(id_theme) + FROM {db_prefix}themes', + array( + ) + ); + list ($id_theme) = $db->fetch_row($result); + $db->free_result($result); + + // This will be theme number... + $id_theme++; + + return $id_theme; +} + +/** + * Adds a new theme to the database. + * + * @param array $details + */ +function addTheme($details) +{ + $db = database(); + + $db->insert('insert', + '{db_prefix}themes', + array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), + $details, + array('id_theme', 'variable') + ); +} + +/** + * Get the name of a theme + * + * @param int $id + * @return string + */ +function getThemeName($id) +{ + $db = database(); + + $result = $db->query('', ' + SELECT value + FROM {db_prefix}themes + WHERE id_theme = {int:current_theme} + AND id_member = {int:no_member} + AND variable = {string:name} + LIMIT 1', + array( + 'current_theme' => $id, + 'no_member' => 0, + 'name' => 'name', + ) + ); + list ($theme_name) = $db->fetch_row($result); + $db->free_result($result); + + return $theme_name; +} + +/** + * Deletes all variants from a given theme id. + * + * @param int $id + */ +function deleteVariants($id) +{ + $db = database(); + + $db->query('', ' + DELETE FROM {db_prefix}themes + WHERE id_theme = {int:current_theme} + AND variable = {string:theme_variant}', + array( + 'current_theme' => $id, + 'theme_variant' => 'theme_variant', + ) + ); +} + +/** + * Possibly the simplest and best example of how to use the template system. + * - allows the theme to take care of actions. + * - happens if $settings['catch_action'] is set and action isn't found + * in the action array. + * - can use a template, layers, sub_template, filename, and/or function. + * @todo look at this + */ +function WrapAction() +{ + global $context, $settings; + + // Load any necessary template(s)? + if (isset($settings['catch_action']['template'])) + { + // Load both the template and language file. (but don't fret if the language file isn't there...) + loadTemplate($settings['catch_action']['template']); + loadLanguage($settings['catch_action']['template'], '', false); + } + + // Any special layers? + if (isset($settings['catch_action']['layers'])) + { + $template_layers = Template_Layers::getInstance(); + foreach ($settings['catch_action']['layers'] as $layer) + $template_layers->add($layer); + } + + // Just call a function? + if (isset($settings['catch_action']['function'])) + { + if (isset($settings['catch_action']['filename'])) + template_include(SOURCEDIR . '/' . $settings['catch_action']['filename'], true); + + $settings['catch_action']['function'](); + } + // And finally, the main sub template ;). + elseif (isset($settings['catch_action']['sub_template'])) + $context['sub_template'] = $settings['catch_action']['sub_template']; } \ No newline at end of file