From 96ea7772cb6053113fda94605e1cb0836dc750f0 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 4 Dec 2019 19:16:22 +0100 Subject: [PATCH] Ported Template class from Hanyuu project. --- misuzu.php | 19 ++++----- public/auth.php | 4 +- public/auth/login.php | 2 +- public/auth/logout.php | 2 +- public/auth/password.php | 2 +- public/auth/register.php | 2 +- public/auth/twofactor.php | 2 +- public/changelog.php | 8 ++-- public/forum/forum.php | 4 +- public/forum/index.php | 2 +- public/forum/leaderboard.php | 4 +- public/forum/post.php | 6 +-- public/forum/posting.php | 6 +-- public/forum/topic.php | 10 ++--- public/index.php | 6 +-- public/info.php | 4 +- public/manage/changelog/change.php | 2 +- public/manage/changelog/index.php | 2 +- public/manage/changelog/tag.php | 4 +- public/manage/changelog/tags.php | 2 +- public/manage/forum/category.php | 2 +- public/manage/forum/index.php | 4 +- public/manage/general/blacklist.php | 2 +- public/manage/general/emoticon.php | 2 +- public/manage/general/emoticons.php | 2 +- public/manage/general/index.php | 2 +- public/manage/general/logs.php | 2 +- public/manage/general/settings.php | 2 +- public/manage/news/categories.php | 2 +- public/manage/news/category.php | 2 +- public/manage/news/post.php | 2 +- public/manage/news/posts.php | 2 +- public/manage/users/index.php | 2 +- public/manage/users/role.php | 4 +- public/manage/users/roles.php | 2 +- public/manage/users/user.php | 2 +- public/manage/users/warnings.php | 2 +- public/members.php | 2 +- public/news/category.php | 2 +- public/news/index.php | 2 +- public/news/post.php | 2 +- public/profile.php | 26 ++++++------ public/search.php | 2 +- public/settings/account.php | 4 +- public/settings/data.php | 2 +- public/settings/logs.php | 2 +- public/settings/sessions.php | 2 +- src/Template.php | 58 ++++++++++++++++++++++++++ src/Twig.php | 23 ----------- src/tpl.php | 64 ----------------------------- utility.php | 10 ++--- 51 files changed, 148 insertions(+), 184 deletions(-) create mode 100644 src/Template.php delete mode 100644 src/Twig.php delete mode 100644 src/tpl.php diff --git a/misuzu.php b/misuzu.php index 658a2f70..fb86c021 100644 --- a/misuzu.php +++ b/misuzu.php @@ -50,7 +50,6 @@ require_once 'src/otp.php'; require_once 'src/pagination.php'; require_once 'src/perms.php'; require_once 'src/string.php'; -require_once 'src/tpl.php'; require_once 'src/twitter.php'; require_once 'src/url.php'; require_once 'src/zalgo.php'; @@ -402,24 +401,20 @@ MIG; mkdirs($twigCache, true); } - tpl_init([ - 'debug' => MSZ_DEBUG, - 'auto_reload' => MSZ_DEBUG, - 'cache' => $twigCache ?? false, - ]); + Template::init($twigCache ?? null, MSZ_DEBUG); - tpl_var('globals', [ + Template::set('globals', [ 'site_name' => Config::get('site.name', Config::TYPE_STR, 'Misuzu'), 'site_description' => Config::get('site.desc', Config::TYPE_STR), 'site_url' => Config::get('site.url', Config::TYPE_STR), 'site_twitter' => Config::get('social.twitter', Config::TYPE_STR), ]); - tpl_add_path(MSZ_ROOT . '/templates'); + Template::addPath(MSZ_ROOT . '/templates'); if(file_exists(MSZ_ROOT . '/.migrating')) { http_response_code(503); - echo tpl_render('home.migration'); + Template::render('home.migration'); exit; } @@ -499,14 +494,14 @@ MIG; } if(!empty($userDisplayInfo)) { - tpl_var('current_user', $userDisplayInfo); + Template::set('current_user', $userDisplayInfo); } $inManageMode = starts_with($_SERVER['REQUEST_URI'], '/manage'); $hasManageAccess = !empty($userDisplayInfo['user_id']) && !user_warning_check_restriction($userDisplayInfo['user_id']) && perms_check_user(MSZ_PERMS_GENERAL, $userDisplayInfo['user_id'], MSZ_PERM_GENERAL_CAN_MANAGE); - tpl_var('has_manage_access', $hasManageAccess); + Template::set('has_manage_access', $hasManageAccess); if($inManageMode) { if(!$hasManageAccess) { @@ -514,6 +509,6 @@ MIG; exit; } - tpl_var('manage_menu', manage_get_menu($userDisplayInfo['user_id'] ?? 0)); + Template::set('manage_menu', manage_get_menu($userDisplayInfo['user_id'] ?? 0)); } } diff --git a/public/auth.php b/public/auth.php index 44161445..c7614e7b 100644 --- a/public/auth.php +++ b/public/auth.php @@ -1,15 +1,13 @@ $notices, 'login_username' => $loginUsername, 'login_redirect' => $loginRedirect, diff --git a/public/auth/logout.php b/public/auth/logout.php index f2da2bf4..69649732 100644 --- a/public/auth/logout.php +++ b/public/auth/logout.php @@ -15,4 +15,4 @@ if(csrf_verify_request()) { return; } -echo tpl_render('auth.logout'); +Template::render('auth.logout'); diff --git a/public/auth/password.php b/public/auth/password.php index a1a92c61..2536c2bc 100644 --- a/public/auth/password.php +++ b/public/auth/password.php @@ -126,7 +126,7 @@ while($canResetPassword) { break; } -echo tpl_render($userId > 0 ? 'auth.password_reset' : 'auth.password_forgot', [ +Template::render($userId > 0 ? 'auth.password_reset' : 'auth.password_forgot', [ 'password_notices' => $notices, 'password_email' => !empty($forget['email']) && is_string($forget['email']) ? $forget['email'] : '', 'password_attempts_remaining' => $remainingAttempts, diff --git a/public/auth/register.php b/public/auth/register.php index 378d2245..589c8168 100644 --- a/public/auth/register.php +++ b/public/auth/register.php @@ -85,7 +85,7 @@ while(!$restricted && !empty($register)) { return; } -echo tpl_render('auth.register', [ +Template::render('auth.register', [ 'register_notices' => $notices, 'register_username' => !empty($register['username']) && is_string($register['username']) ? $register['username'] : '', 'register_email' => !empty($register['email']) && is_string($register['email']) ? $register['email'] : '', diff --git a/public/auth/twofactor.php b/public/auth/twofactor.php index 5ce88cd3..1ef906bf 100644 --- a/public/auth/twofactor.php +++ b/public/auth/twofactor.php @@ -80,7 +80,7 @@ while(!empty($twofactor)) { return; } -echo tpl_render('auth.twofactor', [ +Template::render('auth.twofactor', [ 'twofactor_notices' => $notices, 'twofactor_redirect' => !empty($_GET['redirect']) && is_string($_GET['redirect']) ? $_GET['redirect'] : url('index'), 'twofactor_attempts_remaining' => $remainingAttempts, diff --git a/public/changelog.php b/public/changelog.php index ffa95681..7b6ad320 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -8,7 +8,7 @@ $changelogDate = !empty($_GET['d']) && is_string($_GET['d']) ? (string)$_GET['d' $changelogUser = !empty($_GET['u']) && is_string($_GET['u']) ? (int)$_GET['u'] : 0; $changelogTags = !empty($_GET['t']) && is_string($_GET['t']) ? (string)$_GET['t'] : ''; -tpl_var('comments_perms', $commentPerms = comments_get_perms(user_session_current('user_id', 0))); +Template::set('comments_perms', $commentPerms = comments_get_perms(user_session_current('user_id', 0))); if($changelogChange > 0) { $change = changelog_change_get($changelogChange); @@ -18,7 +18,7 @@ if($changelogChange > 0) { return; } - echo tpl_render('changelog.change', [ + Template::render('changelog.change', [ 'change' => $change, 'tags' => changelog_change_tags_get($change['change_id']), 'comments_category' => $commentsCategory = comments_category_info( @@ -53,13 +53,13 @@ if(!$changes) { } if(!empty($changelogDate) && count($changes) > 0) { - tpl_vars([ + Template::set([ 'comments_category' => $commentsCategory = comments_category_info("changelog-date-{$changelogDate}", true), 'comments' => comments_category_get($commentsCategory['category_id'], user_session_current('user_id', 0)), ]); } -echo tpl_render('changelog.index', [ +Template::render('changelog.index', [ 'changes' => $changes, 'changelog_count' => $changesCount, 'changelog_date' => $changelogDate, diff --git a/public/forum/forum.php b/public/forum/forum.php index 1dfaece9..6da53ece 100644 --- a/public/forum/forum.php +++ b/public/forum/forum.php @@ -30,7 +30,7 @@ if(user_warning_check_restriction($forumUserId)) { $perms &= ~MSZ_FORUM_PERM_SET_WRITE; } -tpl_var('forum_perms', $perms); +Template::set('forum_perms', $perms); if($forum['forum_type'] == MSZ_FORUM_TYPE_LINK) { forum_increment_clicks($forum['forum_id']); @@ -69,7 +69,7 @@ if($forumMayHaveChildren) { } } -echo tpl_render('forum.forum', [ +Template::render('forum.forum', [ 'forum_breadcrumbs' => forum_get_breadcrumbs($forum['forum_id']), 'global_accent_colour' => forum_get_colour($forum['forum_id']), 'forum_may_have_topics' => $forumMayHaveTopics, diff --git a/public/forum/index.php b/public/forum/index.php index adecec56..1a48b6e2 100644 --- a/public/forum/index.php +++ b/public/forum/index.php @@ -40,7 +40,7 @@ switch($indexMode) { } } - echo tpl_render('forum.index', [ + Template::render('forum.index', [ 'forum_categories' => $categories, 'forum_empty' => $blankForum, ]); diff --git a/public/forum/leaderboard.php b/public/forum/leaderboard.php index 90208e87..90783cb5 100644 --- a/public/forum/leaderboard.php +++ b/public/forum/leaderboard.php @@ -46,10 +46,10 @@ MD; $markdown .= sprintf("| %s | [%s](%s%s) | %s |\r\n", $user['rank'], $user['username'], url_prefix(false), url('user-profile', ['user' => $user['user_id']]), $user['posts']); } - tpl_var('leaderboard_markdown', $markdown); + Template::set('leaderboard_markdown', $markdown); } -echo tpl_render('forum.leaderboard', [ +Template::render('forum.leaderboard', [ 'leaderboard_id' => $leaderboardId, 'leaderboard_name' => $leaderboardName, 'leaderboard_categories' => $leaderboards, diff --git a/public/forum/post.php b/public/forum/post.php index e8935c20..27df9a0e 100644 --- a/public/forum/post.php +++ b/public/forum/post.php @@ -121,7 +121,7 @@ switch($postMode) { ]); break; } elseif(!$postRequestVerified) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm post deletion', 'class' => 'far fa-trash-alt', 'message' => sprintf('You are about to delete post #%d. Are you sure about that?', $postInfo['post_id']), @@ -171,7 +171,7 @@ switch($postMode) { ]); break; } elseif(!$postRequestVerified) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm post nuke', 'class' => 'fas fa-radiation', 'message' => sprintf('You are about to PERMANENTLY DELETE post #%d. Are you sure about that?', $postInfo['post_id']), @@ -213,7 +213,7 @@ switch($postMode) { ]); break; } elseif(!$postRequestVerified) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm post restore', 'class' => 'fas fa-magic', 'message' => sprintf('You are about to restore post #%d. Are you sure about that?', $postInfo['post_id']), diff --git a/public/forum/posting.php b/public/forum/posting.php index aed08dcf..3b3f39df 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -238,16 +238,16 @@ if(!empty($_POST)) { } if(!empty($topic)) { - tpl_var('posting_topic', $topic); + Template::set('posting_topic', $topic); } if($mode === 'edit') { // $post is pretty much sure to be populated at this point - tpl_var('posting_post', $post); + Template::set('posting_post', $post); } $displayInfo = forum_posting_info(user_session_current('user_id')); -echo tpl_render('forum.posting', [ +Template::render('forum.posting', [ 'posting_breadcrumbs' => forum_get_breadcrumbs($forumId), 'global_accent_colour' => forum_get_colour($forumId), 'posting_forum' => $forum, diff --git a/public/forum/topic.php b/public/forum/topic.php index 07854c0c..f3c9af7f 100644 --- a/public/forum/topic.php +++ b/public/forum/topic.php @@ -163,7 +163,7 @@ if(in_array($moderationMode, $validModerationModes, true)) { if(!$isXHR) { if(!isset($_GET['confirm'])) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm topic deletion', 'class' => 'far fa-trash-alt', 'message' => sprintf('You are about to delete topic #%d. Are you sure about that?', $topic['topic_id']), @@ -215,7 +215,7 @@ if(in_array($moderationMode, $validModerationModes, true)) { if(!$isXHR) { if(!isset($_GET['confirm'])) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm topic restore', 'class' => 'fas fa-magic', 'message' => sprintf('You are about to restore topic #%d. Are you sure about that?', $topic['topic_id']), @@ -258,7 +258,7 @@ if(in_array($moderationMode, $validModerationModes, true)) { if(!$isXHR) { if(!isset($_GET['confirm'])) { - echo tpl_render('forum.confirm', [ + Template::render('forum.confirm', [ 'title' => 'Confirm topic nuke', 'class' => 'fas fa-radiation', 'message' => sprintf('You are about to PERMANENTLY DELETE topic #%d. Are you sure about that?', $topic['topic_id']), @@ -351,7 +351,7 @@ if(!pagination_is_valid_offset($postsOffset)) { return; } -tpl_var('topic_perms', $perms); +Template::set('topic_perms', $perms); $posts = forum_post_listing( $topic['topic_id'], @@ -369,7 +369,7 @@ $canReply = !$topicIsArchived && !$topicIsLocked && !$topicIsDeleted && perms_ch forum_topic_mark_read($topicUserId, $topic['topic_id'], $topic['forum_id']); -echo tpl_render('forum.topic', [ +Template::render('forum.topic', [ 'topic_breadcrumbs' => forum_get_breadcrumbs($topic['forum_id']), 'global_accent_colour' => forum_get_colour($topic['forum_id']), 'topic_info' => $topic, diff --git a/public/index.php b/public/index.php index e7713933..632cbcbc 100644 --- a/public/index.php +++ b/public/index.php @@ -11,7 +11,7 @@ if($requestPath !== '/' && $requestPath !== $_SERVER['PHP_SELF']) { } if(Config::get('social.embed_linked', Config::TYPE_BOOL)) { - tpl_var('linked_data', [ + Template::set('linked_data', [ 'name' => Config::get('site.name', Config::TYPE_STR, 'Misuzu'), 'url' => Config::get('site.url', Config::TYPE_STR), 'logo' => Config::get('site.ext_logo', Config::TYPE_STR), @@ -91,7 +91,7 @@ $onlineUsers = DB::query(' LIMIT 104 ')->fetchAll(); -tpl_vars([ +Template::set([ 'statistics' => $stats, 'latest_user' => $latestUser, 'online_users' => $onlineUsers, @@ -100,4 +100,4 @@ tpl_vars([ 'featured_news' => $news, ]); -echo tpl_render('home.landing'); +Template::render('home.landing'); diff --git a/public/info.php b/public/info.php index 9138d1ea..98df3363 100644 --- a/public/info.php +++ b/public/info.php @@ -6,7 +6,7 @@ require_once '../misuzu.php'; $pathInfo = $_SERVER['PATH_INFO'] ?? ''; if(empty($pathInfo) || $pathInfo === '/') { - echo tpl_render('info.index'); + Template::render('info.index'); return; } @@ -59,6 +59,6 @@ if(empty($document['title'])) { } } -echo tpl_render('info.view', [ +Template::render('info.view', [ 'document' => $document, ]); diff --git a/public/manage/changelog/change.php b/public/manage/changelog/change.php index 2be4d659..59ab689b 100644 --- a/public/manage/changelog/change.php +++ b/public/manage/changelog/change.php @@ -122,7 +122,7 @@ $getChangeTags = DB::prepare(' $getChangeTags->bind('change_id', $change['change_id'] ?? 0); $changeTags = $getChangeTags->fetchAll(); -echo tpl_render('manage.changelog.change', [ +Template::render('manage.changelog.change', [ 'change' => $change ?? null, 'change_tags' => $changeTags, 'change_actions' => $actions, diff --git a/public/manage/changelog/index.php b/public/manage/changelog/index.php index ec48e92d..0cb92b7b 100644 --- a/public/manage/changelog/index.php +++ b/public/manage/changelog/index.php @@ -55,7 +55,7 @@ for($i = 0; $i < count($changes); $i++) { $changes[$i]['tags'] = $getTags->fetchAll(); } -echo tpl_render('manage.changelog.changes', [ +Template::render('manage.changelog.changes', [ 'changelog_changes' => $changes, 'changelog_changes_count' => $changesCount, 'changelog_pagination' => $changelogPagination, diff --git a/public/manage/changelog/tag.php b/public/manage/changelog/tag.php index f9311ef8..9d53793c 100644 --- a/public/manage/changelog/tag.php +++ b/public/manage/changelog/tag.php @@ -54,11 +54,11 @@ if($tagId > 0) { $tag = $getTag->fetch(); if($tag) { - tpl_var('edit_tag', $tag); + Template::set('edit_tag', $tag); } else { url_redirect('manage-changelog-tags'); return; } } -echo tpl_render('manage.changelog.tag'); +Template::render('manage.changelog.tag'); diff --git a/public/manage/changelog/tags.php b/public/manage/changelog/tags.php index c2685137..e41980a3 100644 --- a/public/manage/changelog/tags.php +++ b/public/manage/changelog/tags.php @@ -20,6 +20,6 @@ $getTags = DB::prepare(' ORDER BY t.`tag_id` ASC '); -echo tpl_render('manage.changelog.tags', [ +Template::render('manage.changelog.tags', [ 'changelog_tags' => $getTags->fetchAll(), ]); diff --git a/public/manage/forum/category.php b/public/manage/forum/category.php index 2bded15a..897e9684 100644 --- a/public/manage/forum/category.php +++ b/public/manage/forum/category.php @@ -21,4 +21,4 @@ if(!$forum) { return; } -echo tpl_render('manage.forum.forum', compact('forum')); +Template::render('manage.forum.forum', compact('forum')); diff --git a/public/manage/forum/index.php b/public/manage/forum/index.php index 804987a3..346dcce8 100644 --- a/public/manage/forum/index.php +++ b/public/manage/forum/index.php @@ -15,7 +15,7 @@ $perms = manage_forum_perms_list($rawPerms); if(!empty($_POST['perms']) && is_array($_POST['perms'])) { $finalPerms = manage_perms_apply($perms, $_POST['perms'], $rawPerms); $perms = manage_forum_perms_list($finalPerms); - tpl_var('calculated_perms', $finalPerms); + Template::set('calculated_perms', $finalPerms); } -echo tpl_render('manage.forum.listing', compact('forums', 'perms')); +Template::render('manage.forum.listing', compact('forums', 'perms')); diff --git a/public/manage/general/blacklist.php b/public/manage/general/blacklist.php index 310fd274..89923bff 100644 --- a/public/manage/general/blacklist.php +++ b/public/manage/general/blacklist.php @@ -42,7 +42,7 @@ if(!empty($_POST)) { } } -echo tpl_render('manage.general.blacklist', [ +Template::render('manage.general.blacklist', [ 'notices' => $notices, 'blacklist' => ip_blacklist_list(), ]); diff --git a/public/manage/general/emoticon.php b/public/manage/general/emoticon.php index 00b33a53..ee763eae 100644 --- a/public/manage/general/emoticon.php +++ b/public/manage/general/emoticon.php @@ -34,6 +34,6 @@ if(csrf_verify_request() } } -echo tpl_render('manage.general.emoticon', [ +Template::render('manage.general.emoticon', [ 'emote_info' => $emoteInfo ?? null, ]); diff --git a/public/manage/general/emoticons.php b/public/manage/general/emoticons.php index 43ef998c..e48c2d27 100644 --- a/public/manage/general/emoticons.php +++ b/public/manage/general/emoticons.php @@ -24,6 +24,6 @@ if(csrf_verify_request() && !empty($_GET['emote']) && is_string($_GET['emote'])) return; } -echo tpl_render('manage.general.emoticons', [ +Template::render('manage.general.emoticons', [ 'emotes' => emotes_list(PHP_INT_MAX), ]); diff --git a/public/manage/general/index.php b/public/manage/general/index.php index 94e8b2ed..14e845b5 100644 --- a/public/manage/general/index.php +++ b/public/manage/general/index.php @@ -177,6 +177,6 @@ if(!empty($_GET['poll'])) { return; } -echo tpl_render('manage.general.overview', [ +Template::render('manage.general.overview', [ 'statistics' => $statistics, ]); diff --git a/public/manage/general/logs.php b/public/manage/general/logs.php index 764d9551..2fc77cfb 100644 --- a/public/manage/general/logs.php +++ b/public/manage/general/logs.php @@ -18,7 +18,7 @@ if(!pagination_is_valid_offset($logsOffset)) { $logs = audit_log_list($logsOffset, $logsPagination['range']); -echo tpl_render('manage.general.logs', [ +Template::render('manage.general.logs', [ 'global_logs' => $logs, 'global_logs_pagination' => $logsPagination, 'global_logs_strings' => MSZ_AUDIT_LOG_STRINGS, diff --git a/public/manage/general/settings.php b/public/manage/general/settings.php index 003f30a0..432156d4 100644 --- a/public/manage/general/settings.php +++ b/public/manage/general/settings.php @@ -8,4 +8,4 @@ if(!perms_check_user(MSZ_PERMS_GENERAL, user_session_current('user_id'), MSZ_PER return; } -echo tpl_render('manage.general.settings'); +Template::render('manage.general.settings'); diff --git a/public/manage/news/categories.php b/public/manage/news/categories.php index 750fc05b..a4012fd9 100644 --- a/public/manage/news/categories.php +++ b/public/manage/news/categories.php @@ -18,7 +18,7 @@ if(!pagination_is_valid_offset($categoriesOffset)) { $categories = news_categories_get($categoriesOffset, $categoriesPagination['range'], true, false, true); -echo tpl_render('manage.news.categories', [ +Template::render('manage.news.categories', [ 'news_categories' => $categories, 'categories_pagination' => $categoriesPagination, ]); diff --git a/public/manage/news/category.php b/public/manage/news/category.php index af33df36..20ef1622 100644 --- a/public/manage/news/category.php +++ b/public/manage/news/category.php @@ -33,4 +33,4 @@ if($categoryId > 0) { $category = news_category_get($categoryId); } -echo tpl_render('manage.news.category', compact('category')); +Template::render('manage.news.category', compact('category')); diff --git a/public/manage/news/post.php b/public/manage/news/post.php index ed22090f..a470810a 100644 --- a/public/manage/news/post.php +++ b/public/manage/news/post.php @@ -54,4 +54,4 @@ if($postId > 0) { $post = news_post_get($postId); } -echo tpl_render('manage.news.post', compact('post', 'categories')); +Template::render('manage.news.post', compact('post', 'categories')); diff --git a/public/manage/news/posts.php b/public/manage/news/posts.php index b1eca871..c71d34dc 100644 --- a/public/manage/news/posts.php +++ b/public/manage/news/posts.php @@ -18,7 +18,7 @@ if(!pagination_is_valid_offset($postsOffset)) { $posts = news_posts_get($postsOffset, $postsPagination['range'], null, false, true, false); -echo tpl_render('manage.news.posts', [ +Template::render('manage.news.posts', [ 'news_posts' => $posts, 'posts_pagination' => $postsPagination, ]); diff --git a/public/manage/users/index.php b/public/manage/users/index.php index 7f3b1175..17ade524 100644 --- a/public/manage/users/index.php +++ b/public/manage/users/index.php @@ -38,7 +38,7 @@ $getManageUsers->bind('offset', $usersOffset); $getManageUsers->bind('take', $usersPagination['range']); $manageUsers = $getManageUsers->fetchAll(); -echo tpl_render('manage.users.users', [ +Template::render('manage.users.users', [ 'manage_users' => $manageUsers, 'manage_users_pagination' => $usersPagination, ]); diff --git a/public/manage/users/role.php b/public/manage/users/role.php index a570ad73..cc8b825e 100644 --- a/public/manage/users/role.php +++ b/public/manage/users/role.php @@ -179,10 +179,10 @@ if($roleId !== null) { return; } - tpl_vars(['edit_role' => $editRole]); + Template::set(['edit_role' => $editRole]); } -echo tpl_render('manage.users.role', [ +Template::render('manage.users.role', [ 'can_manage_perms' => $canEditPerms, 'permissions' => $permissions ?? [], ]); diff --git a/public/manage/users/roles.php b/public/manage/users/roles.php index 2e2323b2..be6b9dd3 100644 --- a/public/manage/users/roles.php +++ b/public/manage/users/roles.php @@ -36,7 +36,7 @@ $getManageRoles->bind('offset', $rolesOffset); $getManageRoles->bind('take', $rolesPagination['range']); $manageRoles = $getManageRoles->fetchAll(); -echo tpl_render('manage.users.roles', [ +Template::render('manage.users.roles', [ 'manage_roles' => $manageRoles, 'manage_roles_pagination' => $rolesPagination, ]); diff --git a/public/manage/users/user.php b/public/manage/users/user.php index 8c5c4e13..0248ad75 100644 --- a/public/manage/users/user.php +++ b/public/manage/users/user.php @@ -239,7 +239,7 @@ $getRoles = DB::prepare(' $getRoles->bind('user_id', $manageUser['user_id']); $roles = $getRoles->fetchAll(); -echo tpl_render('manage.users.user', [ +Template::render('manage.users.user', [ 'manage_user' => $manageUser, 'manage_notices' => $notices, 'manage_roles' => $roles, diff --git a/public/manage/users/warnings.php b/public/manage/users/warnings.php index 93b167be..8cb92c25 100644 --- a/public/manage/users/warnings.php +++ b/public/manage/users/warnings.php @@ -147,7 +147,7 @@ $warningDurations = array_flip([ 'Until (strtotime) ->' => -3, ]); -echo tpl_render('manage.users.warnings', [ +Template::render('manage.users.warnings', [ 'warnings' => [ 'notices' => $notices, 'pagination' => $warningsPagination, diff --git a/public/members.php b/public/members.php index 0596ddd2..8b016f5d 100644 --- a/public/members.php +++ b/public/members.php @@ -166,7 +166,7 @@ if(empty($users)) { http_response_code(404); } -echo tpl_render('user.listing', [ +Template::render('user.listing', [ 'roles' => $roles, 'role' => $role, 'users' => $users, diff --git a/public/news/category.php b/public/news/category.php index c1f4e054..ffe31b7f 100644 --- a/public/news/category.php +++ b/public/news/category.php @@ -27,7 +27,7 @@ $posts = news_posts_get( $featured = news_posts_get(0, 10, $category['category_id'], true); -echo tpl_render('news.category', [ +Template::render('news.category', [ 'category' => $category, 'posts' => $posts, 'featured' => $featured, diff --git a/public/news/index.php b/public/news/index.php index 414ff5d7..f814ac5a 100644 --- a/public/news/index.php +++ b/public/news/index.php @@ -25,7 +25,7 @@ if(!$posts) { return; } -echo tpl_render('news.index', [ +Template::render('news.index', [ 'categories' => $categories, 'posts' => $posts, 'news_pagination' => $newsPagination, diff --git a/public/news/post.php b/public/news/post.php index 7379cf9f..83a3e64a 100644 --- a/public/news/post.php +++ b/public/news/post.php @@ -25,7 +25,7 @@ if($post['comment_section_id'] === null) { $commentsInfo = comments_category_info($post['comment_section_id']); } -echo tpl_render('news.post', [ +Template::render('news.post', [ 'post' => $post, 'comments_perms' => comments_get_perms(user_session_current('user_id', 0)), 'comments_category' => $commentsInfo, diff --git a/public/profile.php b/public/profile.php index 40eb765b..e5b1b8e5 100644 --- a/public/profile.php +++ b/public/profile.php @@ -13,7 +13,7 @@ $profileUser = User::findForProfile($userId); if(empty($profileUser)) { http_response_code(404); - echo tpl_render('profile.index'); + Template::render('profile.index'); return; } @@ -52,7 +52,7 @@ if($isEditing) { 'edit_signature' => MSZ_PERM_USER_EDIT_SIGNATURE, ]); - tpl_vars([ + Template::set([ 'perms' => $perms, 'guidelines' => [ 'avatar' => $avatarProps = user_avatar_default_options(), @@ -305,7 +305,7 @@ if(is_file($backgroundPath)) { $backgroundInfo = getimagesize($backgroundPath); if($backgroundInfo) { - tpl_var('site_background', [ + Template::set('site_background', [ 'url' => url('user-background', ['user' => $profileUser->user_id]), 'width' => $backgroundInfo[0], 'height' => $backgroundInfo[1], @@ -335,8 +335,8 @@ switch($profileMode) { $followingOffset, $currentUserId ); - tpl_vars([ - 'title' => $profile['username'] . ' / following', + Template::set([ + 'title' => $profileUser->username . ' / following', 'canonical_url' => url('user-profile-following', ['user' => $profileUser->user_id]), 'profile_users' => $following, 'profile_relation_pagination' => $followingPagination, @@ -356,8 +356,8 @@ switch($profileMode) { $followers = user_relation_users_to($profileUser->user_id, MSZ_USER_RELATION_FOLLOW, $followerPagination['range'], $followerOffset, $currentUserId); - tpl_vars([ - 'title' => $profile['username'] . ' / followers', + Template::set([ + 'title' => $profileUser->username . ' / followers', 'canonical_url' => url('user-profile-followers', ['user' => $profileUser->user_id]), 'profile_users' => $followers, 'profile_relation_pagination' => $followerPagination, @@ -377,8 +377,8 @@ switch($profileMode) { $topics = forum_topic_listing_user($profileUser->user_id, $currentUserId, $topicsOffset, $topicsPagination['range']); - tpl_vars([ - 'title' => $profile['username'] . ' / topics', + Template::set([ + 'title' => $profileUser->username . ' / topics', 'canonical_url' => url('user-profile-forum-topics', ['user' => $profileUser->user_id, 'page' => pagination_param()]), 'profile_topics' => $topics, 'profile_topics_pagination' => $topicsPagination, @@ -398,8 +398,8 @@ switch($profileMode) { $posts = forum_post_listing($profileUser->user_id, $postsOffset, $postsPagination['range'], false, true); - tpl_vars([ - 'title' => $profile['username'] . ' / posts', + Template::set([ + 'title' => $profileUser->username . ' / posts', 'canonical_url' => url('user-profile-forum-posts', ['user' => $profileUser->user_id, 'page' => pagination_param()]), 'profile_posts' => $posts, 'profile_posts_pagination' => $postsPagination, @@ -422,7 +422,7 @@ switch($profileMode) { ) ); - tpl_vars([ + Template::set([ 'profile_warnings' => $warnings, 'profile_warnings_view_private' => $viewingOwnProfile, 'profile_warnings_can_manage' => $canManageWarnings, @@ -431,7 +431,7 @@ switch($profileMode) { } if(!empty($template)) { - echo tpl_render($template, [ + Template::render($template, [ 'profile_user' => $profileUser, 'profile_stats' => $profileStats, 'profile_mode' => $profileMode, diff --git a/public/search.php b/public/search.php index 7f057b96..43d7e8ec 100644 --- a/public/search.php +++ b/public/search.php @@ -69,7 +69,7 @@ if(!empty($searchQuery)) { $users = $findUsers->fetchAll(); } -echo tpl_render('home.search', [ +Template::render('home.search', [ 'search_query' => $searchQuery, 'forum_topics' => $forumTopics ?? [], 'forum_posts' => $forumPosts ?? [], diff --git a/public/settings/account.php b/public/settings/account.php index c811d27d..5b480bbc 100644 --- a/public/settings/account.php +++ b/public/settings/account.php @@ -41,7 +41,7 @@ if($isVerifiedRequest && isset($_POST['tfa']['enable']) && (bool)$twoFactorInfo[ if((bool)$_POST['tfa']['enable']) { $tfaKey = totp_generate_key(); - tpl_vars([ + Template::set([ 'settings_2fa_code' => $tfaKey, 'settings_2fa_image' => totp_qrcode(totp_uri( sprintf( @@ -121,7 +121,7 @@ if($isVerifiedRequest && !empty($_POST['current_password'])) { $userRoles = user_role_all_user($currentUserId); -echo tpl_render('settings.account', [ +Template::render('settings.account', [ 'errors' => $errors, 'current_email' => $currentEmail, 'user_roles' => $userRoles, diff --git a/public/settings/data.php b/public/settings/data.php index 98f0ce16..5d053274 100644 --- a/public/settings/data.php +++ b/public/settings/data.php @@ -81,6 +81,6 @@ if(isset($_POST['action']) && is_string($_POST['action'])) { } } -echo tpl_render('settings.data', [ +Template::render('settings.data', [ 'errors' => $errors, ]); diff --git a/public/settings/logs.php b/public/settings/logs.php index 52d9f113..ad75bef9 100644 --- a/public/settings/logs.php +++ b/public/settings/logs.php @@ -34,7 +34,7 @@ $accountLogList = audit_log_list( $currentUserId ); -echo tpl_render('settings.logs', [ +Template::render('settings.logs', [ 'login_history_list' => $loginHistoryList, 'login_history_pagination' => $loginHistoryPagination, 'account_log_list' => $accountLogList, diff --git a/public/settings/sessions.php b/public/settings/sessions.php index f2a1a9ea..566038c7 100644 --- a/public/settings/sessions.php +++ b/public/settings/sessions.php @@ -57,7 +57,7 @@ $sessionList = user_session_list( $currentUserId ); -echo tpl_render('settings.sessions', [ +Template::render('settings.sessions', [ 'errors' => $errors, 'session_list' => $sessionList, 'session_active_id' => $sessionActive, diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 00000000..fd3b3944 --- /dev/null +++ b/src/Template.php @@ -0,0 +1,58 @@ + $cache ?? false, + 'strict_variables' => true, + 'auto_reload' => $debug, + 'debug' => $debug, + ]); + self::$env->addExtension(new Twig_Extensions_Extension_Date); + self::$env->addExtension(new TwigMisuzu); + } + + public static function addPath(string $path): void { + self::$loader->addPath($path); + } + + public static function renderRaw(string $file, array $vars = []): string { + if(!defined('MSZ_TPL_RENDER')) { + define('MSZ_TPL_RENDER', microtime(true)); + } + + if(!ends_with($file, self::FILE_EXT)) { + $file = str_replace('.', DIRECTORY_SEPARATOR, $file) . self::FILE_EXT; + } + + return self::$env->render($file, array_merge(self::$vars, $vars)); + } + + public static function render(string $file, array $vars = []): void { + echo self::renderRaw($file, $vars); + } + + /// DEPRECATED: Will be removed in the future, use the $vars param for render and renderRaw instead. + public static function set($arrayOrKey, $value = null): void { + if(is_string($arrayOrKey)) { + self::$vars[$arrayOrKey] = $value; + } elseif(is_array($arrayOrKey)) { + self::$vars = array_merge(self::$vars, $arrayOrKey); + } else { + throw new InvalidArgumentException('First parameter must be of type array or string.'); + } + } +} diff --git a/src/Twig.php b/src/Twig.php deleted file mode 100644 index 5a711584..00000000 --- a/src/Twig.php +++ /dev/null @@ -1,23 +0,0 @@ - false, - 'strict_variables' => true, - 'auto_reload' => false, - 'debug' => false, - ], $options); - - $loader = new Twig_Loader_Filesystem; - $twig = new Twig($loader, $options); - $twig->addExtension(new Twig_Extensions_Extension_Date); - $twig->addExtension(new TwigMisuzu); -} - -function tpl_var(string $key, $value): void { - tpl_vars([$key => $value]); -} - -function tpl_vars(array $append = []): array { - static $vars = []; - - if(!empty($append)) { - $vars = array_merge_recursive($vars, $append); - } - - return $vars; -} - -function tpl_add_path(string $path): void { - Twig::instance()->getLoader()->addPath($path); -} - -function tpl_sanitise_path(string $path): string { - // if the .twig extension if already present just assume that the path is already correct - if(ends_with($path, MSZ_TPL_FILE_EXT)) { - return $path; - } - - return str_replace('.', DIRECTORY_SEPARATOR, $path) . MSZ_TPL_FILE_EXT; -} - -function tpl_exists(string $path): bool { - return Twig::instance()->getLoader()->exists(tpl_sanitise_path($path)); -} - -function tpl_render(string $path, array $vars = []): string { - if(!defined('MSZ_TPL_RENDER')) { - define('MSZ_TPL_RENDER', microtime(true)); - } - - $path = tpl_sanitise_path($path); - - if(count($vars)) { - tpl_vars($vars); - } - - return Twig::instance()->render($path, tpl_vars()); -} diff --git a/utility.php b/utility.php index 749aad84..d8f07bd9 100644 --- a/utility.php +++ b/utility.php @@ -70,19 +70,19 @@ function render_info(?string $message, int $httpCode, string $template = 'errors http_response_code($httpCode); try { - tpl_var('http_code', $httpCode); + \Misuzu\Template::set('http_code', $httpCode); if(mb_strlen($message)) { - tpl_var('message', $message); + \Misuzu\Template::set('message', $message); } $template = sprintf($template, $httpCode); - if(!tpl_exists($template)) { + /*if(!tpl_exists($template)) { $template = 'errors.master'; - } + }*/ - return tpl_render(sprintf($template, $httpCode)); + return Template::renderRaw(sprintf($template, $httpCode)); } catch(Exception $ex) { echo $ex->getMessage(); return $message ?? '';