diff --git a/composer.json b/composer.json index 80eaf918..92f9c76e 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "inc/functions/net.php", "inc/functions/num.php", "inc/functions/format.php", + "inc/functions/theme.php", "inc/driver/http-driver.php", "inc/driver/log-driver.php", "inc/service/captcha-queries.php" diff --git a/inc/bans.php b/inc/bans.php index a0ea5c76..58b054ec 100644 --- a/inc/bans.php +++ b/inc/bans.php @@ -15,7 +15,7 @@ class Bans { $query->bindValue(':id', $ban_ids[0], PDO::PARAM_INT); $query->execute() or error(db_error()); - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); } elseif ($len >= 1) { // Build the query. $query = 'DELETE FROM ``bans`` WHERE `id` IN ('; @@ -33,7 +33,7 @@ class Bans { $query->execute() or error(db_error()); - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); } } @@ -343,7 +343,7 @@ class Bans { static public function seen($ban_id) { $query = query("UPDATE ``bans`` SET `seen` = 1 WHERE `id` = " . (int)$ban_id) or error(db_error()); - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); } static public function purge($require_seen, $moratorium) { @@ -358,7 +358,7 @@ class Bans { $affected = $query->rowCount(); if ($affected > 0) { - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); } return $affected; } @@ -386,7 +386,7 @@ class Bans { query("DELETE FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error()); - if (!$dont_rebuild) rebuildThemes('bans'); + if (!$dont_rebuild) Vichan\Functions\Theme\rebuild_themes('bans'); return true; } @@ -465,7 +465,7 @@ class Bans { modLog("Created a new $ban_len ban on $ban_board for $ban_ip (# $ban_id ) with $ban_reason"); - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); return $pdo->lastInsertId(); } diff --git a/inc/controller.php b/inc/controller.php index 02e33443..0b4e2886 100644 --- a/inc/controller.php +++ b/inc/controller.php @@ -85,24 +85,24 @@ function sb_api($b) { global $config, $build_pages; } function sb_ukko() { - rebuildTheme("ukko", "post-thread"); + Vichan\Functions\Theme\rebuild_theme("ukko", "post-thread"); return true; } function sb_catalog($b) { if (!openBoard($b)) return false; - rebuildTheme("catalog", "post-thread", $b); + Vichan\Functions\Theme\rebuild_theme("catalog", "post-thread", $b); return true; } function sb_recent() { - rebuildTheme("recent", "post-thread"); + Vichan\Functions\Theme\rebuild_theme("recent", "post-thread"); return true; } function sb_sitemap() { - rebuildTheme("sitemap", "all"); + Vichan\Functions\Theme\rebuild_theme("sitemap", "all"); return true; } diff --git a/inc/functions.php b/inc/functions.php index 2cb97c50..e529a955 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -397,108 +397,6 @@ function create_antibot($board, $thread = null) { return _create_antibot($board, $thread); } -function rebuildThemes($action, $boardname = false) { - global $config, $board, $current_locale; - - // Save the global variables - $_config = $config; - $_board = $board; - - // List themes - if ($themes = Cache::get("themes")) { - // OK, we already have themes loaded - } - else { - $query = query("SELECT `theme` FROM ``theme_settings`` WHERE `name` IS NULL AND `value` IS NULL") or error(db_error()); - - $themes = array(); - - while ($theme = $query->fetch(PDO::FETCH_ASSOC)) { - $themes[] = $theme; - } - - Cache::set("themes", $themes); - } - - foreach ($themes as $theme) { - // Restore them - $config = $_config; - $board = $_board; - - // Reload the locale - if ($config['locale'] != $current_locale) { - $current_locale = $config['locale']; - init_locale($config['locale']); - } - - if (PHP_SAPI === 'cli') { - echo "Rebuilding theme ".$theme['theme']."... "; - } - - rebuildTheme($theme['theme'], $action, $boardname); - - if (PHP_SAPI === 'cli') { - echo "done\n"; - } - } - - // Restore them again - $config = $_config; - $board = $_board; - - // Reload the locale - if ($config['locale'] != $current_locale) { - $current_locale = $config['locale']; - init_locale($config['locale']); - } -} - - -function loadThemeConfig($_theme) { - global $config; - - if (!file_exists($config['dir']['themes'] . '/' . $_theme . '/info.php')) - return false; - - // Load theme information into $theme - include $config['dir']['themes'] . '/' . $_theme . '/info.php'; - - return $theme; -} - -function rebuildTheme($theme, $action, $board = false) { - global $config, $_theme; - $_theme = $theme; - - $theme = loadThemeConfig($_theme); - - if (file_exists($config['dir']['themes'] . '/' . $_theme . '/theme.php')) { - require_once $config['dir']['themes'] . '/' . $_theme . '/theme.php'; - - $theme['build_function']($action, themeSettings($_theme), $board); - } -} - - -function themeSettings($theme) { - if ($settings = Cache::get("theme_settings_".$theme)) { - return $settings; - } - - $query = prepare("SELECT `name`, `value` FROM ``theme_settings`` WHERE `theme` = :theme AND `name` IS NOT NULL"); - $query->bindValue(':theme', $theme); - $query->execute() or error(db_error($query)); - - $settings = array(); - while ($s = $query->fetch(PDO::FETCH_ASSOC)) { - $settings[$s['name']] = $s['value']; - } - - Cache::set("theme_settings_".$theme, $settings); - - return $settings; -} - function sprintf3($str, $vars, $delim = '%') { $replaces = array(); foreach ($vars as $k => $v) { diff --git a/inc/functions/theme.php b/inc/functions/theme.php new file mode 100644 index 00000000..6ac3a95b --- /dev/null +++ b/inc/functions/theme.php @@ -0,0 +1,98 @@ +fetchAll(\PDO::FETCH_NUM); + + \Cache::set("themes", $themes); + } + + foreach ($themes as $theme) { + // Restore them + $config = $_config; + $board = $_board; + + // Reload the locale + if ($config['locale'] != $current_locale) { + $current_locale = $config['locale']; + init_locale($config['locale']); + } + + if (PHP_SAPI === 'cli') { + echo "Rebuilding theme ".$theme['theme']."... "; + } + + rebuild_theme($theme['theme'], $action, $boardname); + + if (PHP_SAPI === 'cli') { + echo "done\n"; + } + } + + // Restore them again + $config = $_config; + $board = $_board; + + // Reload the locale + if ($config['locale'] != $current_locale) { + $current_locale = $config['locale']; + init_locale($config['locale']); + } +} + +function load_theme_config($_theme) { + global $config; + + if (!file_exists($config['dir']['themes'] . '/' . $_theme . '/info.php')) { + return false; + } + + // Load theme information into $theme + include $config['dir']['themes'] . '/' . $_theme . '/info.php'; + + return $theme; +} + +function rebuild_theme($theme, string $action, $board = false) { + global $config, $_theme; + $_theme = $theme; + + $theme = load_theme_config($_theme); + + if (file_exists($config['dir']['themes'] . '/' . $_theme . '/theme.php')) { + require_once $config['dir']['themes'] . '/' . $_theme . '/theme.php'; + + $theme['build_function']($action, theme_settings($_theme), $board); + } +} + +function theme_settings($theme): array { + if ($settings = \Cache::get("theme_settings_" . $theme)) { + return $settings; + } + + $query = prepare("SELECT `name`, `value` FROM ``theme_settings`` WHERE `theme` = :theme AND `name` IS NOT NULL"); + $query->bindValue(':theme', $theme); + $query->execute() or error(db_error($query)); + + $settings = []; + while ($s = $query->fetch(\PDO::FETCH_ASSOC)) { + $settings[$s['name']] = $s['value']; + } + + \Cache::set("theme_settings_".$theme, $settings); + + return $settings; +} diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 46e8fc7e..f2caff32 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -442,7 +442,7 @@ function mod_edit_board($boardName) { cache::delete('all_boards'); } - rebuildThemes('boards'); + Vichan\Functions\Theme\rebuild_themes('boards'); header('Location: ?/', true, $config['redirect_http']); } else { @@ -514,7 +514,7 @@ function mod_new_board() { // Build the board buildIndex(); - rebuildThemes('boards'); + Vichan\Functions\Theme\rebuild_themes('boards'); header('Location: ?/' . $board['uri'] . '/' . $config['file_index'], true, $config['redirect_http']); } @@ -617,7 +617,7 @@ function mod_news($page_no = 1) { modLog('Posted a news entry'); - rebuildThemes('news'); + Vichan\Functions\Theme\rebuild_themes('news'); header('Location: ?/edit_news#' . $pdo->lastInsertId(), true, $config['redirect_http']); } @@ -1013,7 +1013,7 @@ function mod_bans() { foreach ($unban as $id) { Bans::delete($id, true, $mod['boards'], true); } - rebuildThemes('bans'); + Vichan\Functions\Theme\rebuild_themes('bans'); header('Location: ?/bans', true, $config['redirect_http']); return; } @@ -1281,7 +1281,7 @@ function mod_move_reply($originBoard, $postID) { buildThread($newID); // trigger themes - rebuildThemes('post', $targetBoard); + Vichan\Functions\Theme\rebuild_themes('post', $targetBoard); // mod log modLog("Moved post #{$postID} to " . sprintf($config['board_abbreviation'], $targetBoard) . " (#{$newID})", $originBoard); @@ -1469,7 +1469,7 @@ function mod_move($originBoard, $postID) { buildIndex(); // trigger themes - rebuildThemes('post', $targetBoard); + Vichan\Functions\Theme\rebuild_themes('post', $targetBoard); $newboard = $board; @@ -1576,7 +1576,7 @@ function mod_ban_post($board, $delete, $post, $token = false) { // Rebuild board buildIndex(); // Rebuild themes - rebuildThemes('post-delete', $board); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board); } header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); @@ -1651,7 +1651,7 @@ function mod_edit_post($board, $edit_raw_html, $postID) { buildIndex(); - rebuildThemes('post', $board); + Vichan\Functions\Theme\rebuild_themes('post', $board); header('Location: ?/' . sprintf($config['board_path'], $board) . $config['dir']['res'] . link_for($post) . '#' . $postID, true, $config['redirect_http']); } else { @@ -1689,7 +1689,7 @@ function mod_delete($board, $post) { // Rebuild board buildIndex(); // Rebuild themes - rebuildThemes('post-delete', $board); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board); // Redirect header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); } @@ -1711,7 +1711,7 @@ function mod_deletefile($board, $post, $file) { // Rebuild board buildIndex(); // Rebuild themes - rebuildThemes('post-delete', $board); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board); // Redirect header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); @@ -1756,7 +1756,7 @@ function mod_spoiler_image($board, $post, $file) { buildIndex(); // Rebuild themes - rebuildThemes('post-delete', $board); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board); // Redirect header('Location: ?/' . sprintf($config['board_path'], $board) . $config['file_index'], true, $config['redirect_http']); @@ -1807,7 +1807,7 @@ function mod_deletebyip($boardName, $post, $global = false) { deletePost($post['id'], false, false); - rebuildThemes('post-delete', $board['uri']); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board['uri']); buildIndex(); @@ -2233,7 +2233,7 @@ function mod_rebuild() { if (isset($_POST['rebuild_themes'])) { $log[] = 'Regenerating theme files'; - rebuildThemes('all'); + Vichan\Functions\Theme\rebuild_themes('all'); } if (isset($_POST['rebuild_javascript'])) { @@ -2622,7 +2622,7 @@ function mod_themes_list() { $themes = array(); while ($file = readdir($dir)) { if ($file[0] != '.' && is_dir($config['dir']['themes'] . '/' . $file)) { - $themes[$file] = loadThemeConfig($file); + $themes[$file] = Vichan\Functions\Theme\load_theme_config($file); } } closedir($dir); @@ -2644,7 +2644,7 @@ function mod_theme_configure($theme_name) { if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); - if (!$theme = loadThemeConfig($theme_name)) { + if (!$theme = Vichan\Functions\Theme\load_theme_config($theme_name)) { error($config['error']['invalidtheme']); } @@ -2682,7 +2682,7 @@ function mod_theme_configure($theme_name) { $result = true; $message = false; if (isset($theme['install_callback'])) { - $ret = $theme['install_callback'](themeSettings($theme_name)); + $ret = $theme['install_callback'](Vichan\Functions\Theme\theme_settings($theme_name)); if ($ret && !empty($ret)) { if (is_array($ret) && count($ret) == 2) { $result = $ret[0]; @@ -2699,7 +2699,7 @@ function mod_theme_configure($theme_name) { } // Build themes - rebuildThemes('all'); + Vichan\Functions\Theme\rebuild_themes('all'); mod_page(sprintf(_($result ? 'Installed theme: %s' : 'Installation failed: %s'), $theme['name']), $config['file_mod_theme_installed'], array( 'theme_name' => $theme_name, @@ -2710,7 +2710,7 @@ function mod_theme_configure($theme_name) { return; } - $settings = themeSettings($theme_name); + $settings = Vichan\Functions\Theme\theme_settings($theme_name); mod_page(sprintf(_('Configuring theme: %s'), $theme['name']), $config['file_mod_theme_config'], array( 'theme_name' => $theme_name, @@ -2743,7 +2743,7 @@ function mod_theme_rebuild($theme_name) { if (!hasPermission($config['mod']['themes'])) error($config['error']['noaccess']); - rebuildTheme($theme_name, 'all'); + Vichan\Functions\Theme\rebuild_theme($theme_name, 'all'); mod_page(sprintf(_('Rebuilt theme: %s'), $theme_name), $config['file_mod_theme_rebuilt'], array( 'theme_name' => $theme_name, diff --git a/post.php b/post.php index 644ff111..18022047 100644 --- a/post.php +++ b/post.php @@ -477,7 +477,7 @@ if (isset($_POST['delete'])) { if (function_exists('fastcgi_finish_request')) @fastcgi_finish_request(); - rebuildThemes('post-delete', $board['uri']); + Vichan\Functions\Theme\rebuild_themes('post-delete', $board['uri']); } elseif (isset($_POST['report'])) { if (!isset($_POST['board'], $_POST['reason'])) @@ -1429,9 +1429,9 @@ if (isset($_POST['delete'])) { @fastcgi_finish_request(); if ($post['op']) - rebuildThemes('post-thread', $board['uri']); + Vichan\Functions\Theme\rebuild_themes('post-thread', $board['uri']); else - rebuildThemes('post', $board['uri']); + Vichan\Functions\Theme\rebuild_themes('post', $board['uri']); } elseif (isset($_POST['appeal'])) { if (!isset($_POST['ban_id']))