diff --git a/misuzu.php b/misuzu.php index 9b9fa0db..dad3620b 100644 --- a/misuzu.php +++ b/misuzu.php @@ -13,6 +13,7 @@ require_once __DIR__ . '/src/git.php'; require_once __DIR__ . '/src/manage.php'; require_once __DIR__ . '/src/news.php'; require_once __DIR__ . '/src/perms.php'; +require_once __DIR__ . '/src/tpl.php'; require_once __DIR__ . '/src/zalgo.php'; require_once __DIR__ . '/src/Forum/forum.php'; require_once __DIR__ . '/src/Forum/post.php'; @@ -204,17 +205,15 @@ MIG; $app->startCache(); $app->startTemplating(); - $tpl = $app->getTemplating(); + + tpl_add_path(__DIR__ . '/templates'); if ($app->getConfig()->get('Auth', 'lockdown', 'bool', false)) { http_response_code(503); - $tpl->addPath('auth', __DIR__ . '/views/auth'); - echo $tpl->render('lockdown'); + echo tpl_render('auth/lockdown'); exit; } - $tpl->addPath('mio', __DIR__ . '/views/mio'); - if (isset($_COOKIE['msz_uid'], $_COOKIE['msz_sid'])) { $app->startSession((int)$_COOKIE['msz_uid'], $_COOKIE['msz_sid']); @@ -240,13 +239,13 @@ MIG; '); $getUserDisplayInfo->bindValue('user_id', $app->getUserId()); $userDisplayInfo = $getUserDisplayInfo->execute() ? $getUserDisplayInfo->fetch() : []; - $tpl->var('current_user', $userDisplayInfo); + tpl_var('current_user', $userDisplayInfo); } } $inManageMode = starts_with($_SERVER['REQUEST_URI'], '/manage'); $hasManageAccess = perms_check(perms_get_user(MSZ_PERMS_GENERAL, $app->getUserId()), MSZ_GENERAL_PERM_CAN_MANAGE); - $tpl->var('has_manage_access', $hasManageAccess); + tpl_var('has_manage_access', $hasManageAccess); if ($inManageMode) { if (!$hasManageAccess) { @@ -254,8 +253,6 @@ MIG; exit; } - $tpl = $app->getTemplating(); - $tpl->var('manage_menu', manage_get_menu($app->getUserId())); - $tpl->addPath('manage', __DIR__ . '/views/manage'); + tpl_var('manage_menu', manage_get_menu($app->getUserId())); } } diff --git a/public/auth.php b/public/auth.php index ef22511a..3dd675c0 100644 --- a/public/auth.php +++ b/public/auth.php @@ -8,7 +8,6 @@ use Misuzu\Users\Session; require_once __DIR__ . '/../misuzu.php'; $config = $app->getConfig(); -$tpl = $app->getTemplating(); $usernameValidationErrors = [ 'trim' => 'Your username may not start or end with spaces!', @@ -22,9 +21,8 @@ $usernameValidationErrors = [ $authMode = $_GET['m'] ?? 'login'; $preventRegistration = $config->get('Auth', 'prevent_registration', 'bool', false); -$tpl->addPath('auth', __DIR__ . '/../views/auth'); -$tpl->vars([ +tpl_vars([ 'prevent_registration' => $preventRegistration, 'auth_mode' => $authMode, 'auth_username' => $_REQUEST['username'] ?? '', @@ -46,7 +44,7 @@ switch ($authMode) { return; } - echo $tpl->render('@auth.logout'); + echo tpl_render('auth.logout'); break; case 'reset': @@ -69,7 +67,7 @@ switch ($authMode) { break; } - $tpl->var('auth_reset_message', 'A verification code should\'ve been sent to your e-mail address.'); + tpl_var('auth_reset_message', 'A verification code should\'ve been sent to your e-mail address.'); while ($_SERVER['REQUEST_METHOD'] === 'POST') { $validateRequest = Database::prepare(' @@ -87,21 +85,21 @@ switch ($authMode) { : false; if (!$validateRequest) { - $tpl->var('auth_reset_error', 'Invalid verification code!'); + tpl_var('auth_reset_error', 'Invalid verification code!'); break; } - $tpl->var('reset_verify', $_POST['verification']); + tpl_var('reset_verify', $_POST['verification']); if (empty($_POST['password']['new']) || empty($_POST['password']['confirm']) || $_POST['password']['new'] !== $_POST['password']['confirm']) { - $tpl->var('auth_reset_error', 'Your passwords didn\'t match!'); + tpl_var('auth_reset_error', 'Your passwords didn\'t match!'); break; } if (user_validate_password($_POST['password']['new']) !== '') { - $tpl->var('auth_reset_error', 'Your password is too weak!'); + tpl_var('auth_reset_error', 'Your password is too weak!'); break; } @@ -136,7 +134,7 @@ switch ($authMode) { break; } - echo $tpl->render('@auth.password', [ + echo tpl_render('auth.password', [ 'reset_user' => $resetUser, ]); break; @@ -149,7 +147,7 @@ switch ($authMode) { while ($_SERVER['REQUEST_METHOD'] === 'POST') { if (empty($_POST['email'])) { - $tpl->var('auth_forgot_error', 'Please enter an e-mail address.'); + tpl_var('auth_forgot_error', 'Please enter an e-mail address.'); break; } @@ -221,7 +219,7 @@ MSG; break; } - echo $tpl->render('@auth.auth'); + echo tpl_render('auth.auth'); break; case 'login': @@ -298,10 +296,10 @@ MSG; } if (!empty($authLoginError)) { - $tpl->var('auth_login_error', $authLoginError); + tpl_var('auth_login_error', $authLoginError); } - echo $tpl->render('@auth.auth'); + echo tpl_render('auth.auth'); break; case 'register': @@ -359,14 +357,14 @@ MSG; user_role_add($createUser, MSZ_ROLE_MAIN); - $tpl->var('auth_register_message', 'Welcome to Flashii! You may now log in.'); + tpl_var('auth_register_message', 'Welcome to Flashii! You may now log in.'); break; } if (!empty($authRegistrationError)) { - $tpl->var('auth_register_error', $authRegistrationError); + tpl_var('auth_register_error', $authRegistrationError); } - echo $tpl->render('@auth.auth'); + echo tpl_render('auth.auth'); break; } diff --git a/public/changelog.php b/public/changelog.php index d73c5549..2f75162c 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -3,8 +3,6 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; -$tpl = $app->getTemplating(); - $changelogOffset = max((int)($_GET['o'] ?? 0), 0); $changelogRange = 30; @@ -15,7 +13,7 @@ $changelogTags = $_GET['t'] ?? ''; $commentPerms = comments_get_perms($app->getUserId()); -$tpl->vars([ +tpl_vars([ 'changelog_offset' => $changelogOffset, 'changelog_take' => $changelogRange, 'comments_perms' => $commentPerms, @@ -55,10 +53,10 @@ if ($changelogChange > 0) { '); $getTags->bindValue('change_id', $change['change_id']); $tags = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []; - $tpl->var('tags', $tags); + tpl_var('tags', $tags); } - echo $tpl->render('changelog.change', [ + echo tpl_render('changelog.change', [ 'change' => $change, 'comments_category' => $commentsCategory = comments_category_info( "changelog-date-{$change['change_date']}", @@ -88,13 +86,13 @@ if (!$changes) { } if (!empty($changelogDate)) { - $tpl->vars([ + tpl_vars([ 'comments_category' => $commentsCategory = comments_category_info("changelog-date-{$changelogDate}", true), 'comments' => comments_category_get($commentsCategory['category_id'], $app->getUserId()), ]); } -echo $tpl->render('changelog.index', [ +echo tpl_render('changelog.index', [ 'changes' => $changes, 'changelog_count' => $changesCount, 'changelog_date' => $changelogDate, diff --git a/public/forum/forum.php b/public/forum/forum.php index c3b7abaf..df19c3dd 100644 --- a/public/forum/forum.php +++ b/public/forum/forum.php @@ -10,7 +10,6 @@ if ($forumId === 0) { exit; } -$templating = $app->getTemplating(); $forum = forum_fetch($forumId); if (empty($forum) || ($forum['forum_type'] == MSZ_FORUM_TYPE_LINK && empty($forum['forum_link']))) { @@ -35,7 +34,7 @@ foreach ($forum['forum_subforums'] as $skey => $subforum) { = forum_get_children($subforum['forum_id'], $app->getUserId(), true); } -echo $app->getTemplating()->render('forum.forum', [ +echo tpl_render('forum.forum', [ 'forum_breadcrumbs' => forum_get_breadcrumbs($forum['forum_id']), 'forum_info' => $forum, 'forum_topics' => $topics, diff --git a/public/forum/index.php b/public/forum/index.php index 0e2d6d98..299763b3 100644 --- a/public/forum/index.php +++ b/public/forum/index.php @@ -17,7 +17,7 @@ foreach ($categories as $key => $category) { } } -echo $app->getTemplating()->render('forum.index', [ +echo tpl_render('forum.index', [ 'forum_categories' => $categories, 'forum_empty' => $blankForum, ]); diff --git a/public/forum/posting.php b/public/forum/posting.php index 89906911..b6194163 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -4,8 +4,6 @@ use Misuzu\Net\IPAddress; require_once __DIR__ . '/../../misuzu.php'; -$templating = $app->getTemplating(); - if (!$app->hasActiveSession()) { echo render_error(403); return; @@ -132,10 +130,10 @@ if ($postRequest) { } if (!empty($topic)) { - $templating->var('posting_topic', $topic); + tpl_var('posting_topic', $topic); } -echo $templating->render('forum.posting', [ +echo tpl_render('forum.posting', [ 'posting_breadcrumbs' => forum_get_breadcrumbs($forumId), 'posting_forum' => $forum, ]); diff --git a/public/forum/topic.php b/public/forum/topic.php index 66c079f9..943f090a 100644 --- a/public/forum/topic.php +++ b/public/forum/topic.php @@ -1,8 +1,6 @@ getTemplating(); - $postId = (int)($_GET['p'] ?? 0); $topicId = (int)($_GET['t'] ?? 0); $postsOffset = max((int)($_GET['o'] ?? 0), 0); @@ -33,7 +31,7 @@ if (!$posts) { forum_topic_mark_read($app->getUserId(), $topic['topic_id'], $topic['forum_id']); -echo $templating->render('forum.topic', [ +echo tpl_render('forum.topic', [ 'topic_breadcrumbs' => forum_get_breadcrumbs($topic['forum_id']), 'topic_info' => $topic, 'topic_posts' => $posts, diff --git a/public/index.php b/public/index.php index 8812eb89..3b11a01d 100644 --- a/public/index.php +++ b/public/index.php @@ -6,10 +6,9 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; $config = $app->getConfig(); -$tpl = $app->getTemplating(); if ($config->get('Site', 'embed_linked_data', 'bool', false)) { - $tpl->vars([ + tpl_vars([ 'embed_linked_data' => true, 'embed_name' => $config->get('Site', 'name'), 'embed_url' => $config->get('Site', 'url'), @@ -72,7 +71,7 @@ $changelog = Cache::instance()->get('index:changelog:v1', function () { ')->fetchAll(PDO::FETCH_ASSOC); }, 1800); -echo $tpl->render('home.index', [ +echo tpl_render('home.index', [ 'users_count' => $statistics['users'], 'last_user' => $statistics['lastUser'], 'featured_changelog' => $changelog, diff --git a/public/manage/changelog.php b/public/manage/changelog.php index da589956..21f1caa4 100644 --- a/public/manage/changelog.php +++ b/public/manage/changelog.php @@ -3,10 +3,7 @@ use Misuzu\Database; require_once __DIR__ . '/../../misuzu.php'; -$tpl = $app->getTemplating(); - $changelogPerms = perms_get_user(MSZ_PERMS_CHANGELOG, $app->getUserId()); - $queryOffset = (int)($_GET['o'] ?? 0); switch ($_GET['v'] ?? null) { @@ -58,7 +55,7 @@ switch ($_GET['v'] ?? null) { $changes[$i]['tags'] = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []; } - echo $tpl->render('@manage.changelog.changes', [ + echo tpl_render('manage.changelog.changes', [ 'changelog_changes' => $changes, 'changelog_changes_count' => $changesCount, 'changelog_offset' => $queryOffset, @@ -157,7 +154,7 @@ switch ($_GET['v'] ?? null) { SELECT `action_id`, `action_name` FROM `msz_changelog_actions` ')->fetchAll(PDO::FETCH_ASSOC); - $tpl->var('changelog_actions', $actions); + tpl_var('changelog_actions', $actions); if ($changeId > 0) { $getChange = Database::prepare(' @@ -171,7 +168,7 @@ switch ($_GET['v'] ?? null) { $change = $getChange->execute() ? $getChange->fetch(PDO::FETCH_ASSOC) : []; if ($change) { - $tpl->var('edit_change', $change); + tpl_var('edit_change', $change); $assignedTags = Database::prepare(' SELECT `tag_id`, `tag_name` @@ -198,7 +195,7 @@ switch ($_GET['v'] ?? null) { $availableTags->bindValue('change_id', $change['change_id']); $availableTags = $availableTags->execute() ? $availableTags->fetchAll(PDO::FETCH_ASSOC) : []; - $tpl->vars([ + tpl_vars([ 'edit_change_assigned_tags' => $assignedTags, 'edit_change_available_tags' => $availableTags, ]); @@ -208,7 +205,7 @@ switch ($_GET['v'] ?? null) { } } - echo $tpl->render('@manage.changelog.change_edit'); + echo tpl_render('manage.changelog.change_edit'); break; case 'tags': @@ -240,7 +237,7 @@ switch ($_GET['v'] ?? null) { $getTags->bindValue('offset', $queryOffset); $tags = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []; - echo $tpl->render('@manage.changelog.tags', [ + echo tpl_render('manage.changelog.tags', [ 'changelog_tags' => $tags, 'changelog_tags_count' => $tagsCount, 'changelog_take' => $tagsTake, @@ -303,14 +300,14 @@ switch ($_GET['v'] ?? null) { $tag = $getTag->execute() ? $getTag->fetch(PDO::FETCH_ASSOC) : []; if ($tag) { - $tpl->var('edit_tag', $tag); + tpl_var('edit_tag', $tag); } else { header('Location: ?v=tags'); return; } } - echo $tpl->render('@manage.changelog.tag_edit'); + echo tpl_render('manage.changelog.tag_edit'); break; case 'actions': @@ -342,7 +339,7 @@ switch ($_GET['v'] ?? null) { $getActions->bindValue('offset', $queryOffset); $actions = $getActions->execute() ? $getActions->fetchAll(PDO::FETCH_ASSOC) : []; - echo $tpl->render('@manage.changelog.actions', [ + echo tpl_render('manage.changelog.actions', [ 'changelog_actions' => $actions, 'changelog_actions_count' => $actionTake, 'changelog_take' => $actionTake, @@ -414,13 +411,13 @@ switch ($_GET['v'] ?? null) { $action = $getAction->execute() ? $getAction->fetch(PDO::FETCH_ASSOC) : []; if ($action) { - $tpl->var('edit_action', $action); + tpl_var('edit_action', $action); } else { header('Location: ?v=actions'); return; } } - echo $tpl->render('@manage.changelog.action_edit'); + echo tpl_render('manage.changelog.action_edit'); break; } diff --git a/public/manage/index.php b/public/manage/index.php index 61d71f46..d7ba64b7 100644 --- a/public/manage/index.php +++ b/public/manage/index.php @@ -2,12 +2,11 @@ require_once __DIR__ . '/../../misuzu.php'; $generalPerms = perms_get_user(MSZ_PERMS_GENERAL, $app->getUserId()); -$tpl = $app->getTemplating(); switch ($_GET['v'] ?? null) { default: case 'overview': - echo $tpl->render('@manage.general.overview'); + echo tpl_render('manage.general.overview'); break; case 'logs': diff --git a/public/manage/users.php b/public/manage/users.php index 94664842..90408b00 100644 --- a/public/manage/users.php +++ b/public/manage/users.php @@ -3,14 +3,11 @@ use Misuzu\Database; require_once __DIR__ . '/../../misuzu.php'; -$tpl = $app->getTemplating(); - $userPerms = perms_get_user(MSZ_PERMS_USER, $app->getUserId()); - $isPostRequest = $_SERVER['REQUEST_METHOD'] === 'POST'; $queryQffset = (int)($_GET['o'] ?? 0); -$tpl->vars([ +tpl_vars([ 'can_manage_users' => $canManageUsers = perms_check($userPerms, MSZ_USER_PERM_MANAGE_USERS), 'can_manage_roles' => $canManageRoles = perms_check($userPerms, MSZ_USER_PERM_MANAGE_ROLES), 'can_manage_perms' => $canManagePerms = perms_check($userPerms, MSZ_USER_PERM_MANAGE_PERMS), @@ -44,13 +41,13 @@ switch ($_GET['v'] ?? null) { $getManageUsers->bindValue('take', $usersTake); $manageUsers = $getManageUsers->execute() ? $getManageUsers->fetchAll() : []; - $tpl->vars([ + tpl_vars([ 'manage_users' => $manageUsers, 'manage_users_count' => $manageUsersCount, 'manage_users_range' => $usersTake, 'manage_users_offset' => $queryQffset, ]); - echo $tpl->render('@manage.users.listing'); + echo tpl_render('manage.users.listing'); break; case 'view': @@ -111,7 +108,7 @@ switch ($_GET['v'] ?? null) { $availableRoles = $getAvailableRoles->execute() ? $getAvailableRoles->fetchAll() : []; if ($canManagePerms) { - $tpl->var('permissions', $permissions = manage_perms_list(perms_get_user_raw($userId))); + tpl_var('permissions', $permissions = manage_perms_list(perms_get_user_raw($userId))); } if ($isPostRequest) { @@ -251,13 +248,13 @@ switch ($_GET['v'] ?? null) { break; } - $tpl->vars([ + tpl_vars([ 'available_roles' => $availableRoles, 'has_roles' => $hasRoles, 'view_user' => $manageUser, 'profile_fields' => user_profile_fields_get(), ]); - echo $tpl->render('@manage.users.view'); + echo tpl_render('manage.users.view'); break; case 'roles': @@ -287,13 +284,13 @@ switch ($_GET['v'] ?? null) { $getManageRoles->bindValue('take', $rolesTake); $manageRoles = $getManageRoles->execute() ? $getManageRoles->fetchAll() : []; - $tpl->vars([ + tpl_vars([ 'manage_roles' => $manageRoles, 'manage_roles_count' => $manageRolesCount, 'manage_roles_range' => $rolesTake, 'manage_roles_offset' => $queryQffset, ]); - echo $tpl->render('@manage.users.roles'); + echo tpl_render('manage.users.roles'); break; case 'role': @@ -305,7 +302,7 @@ switch ($_GET['v'] ?? null) { $roleId = $_GET['r'] ?? null; if ($canManagePerms) { - $tpl->var('permissions', $permissions = manage_perms_list(perms_get_role_raw($roleId ?? 0))); + tpl_var('permissions', $permissions = manage_perms_list(perms_get_role_raw($roleId ?? 0))); } if ($isPostRequest) { @@ -470,9 +467,9 @@ switch ($_GET['v'] ?? null) { break; } - $tpl->vars(['edit_role' => $editRole]); + tpl_vars(['edit_role' => $editRole]); } - echo $tpl->render('@manage.users.roles_create'); + echo tpl_render('manage.users.roles_create'); break; } diff --git a/public/members.php b/public/members.php index 5a2496c7..02bb888e 100644 --- a/public/members.php +++ b/public/members.php @@ -58,8 +58,6 @@ if (empty($orderDir)) { return; } -$tpl = $app->getTemplating(); - $getRole = Database::prepare(' SELECT `role_id`, `role_name`, `role_colour`, `role_description`, `created_at`, @@ -118,7 +116,7 @@ $getUsers->bindValue('offset', $usersOffset); $getUsers->bindValue('take', $usersTake); $users = $getUsers->execute() ? $getUsers->fetchAll(PDO::FETCH_ASSOC) : []; -echo $tpl->render('user.listing', [ +echo tpl_render('user.listing', [ 'roles' => $roles, 'role' => $role, 'users' => $users, diff --git a/public/news.php b/public/news.php index ee0ccb62..01feb2b0 100644 --- a/public/news.php +++ b/public/news.php @@ -3,14 +3,12 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; -$templating = $app->getTemplating(); - $categoryId = isset($_GET['c']) ? (int)$_GET['c'] : null; $postId = isset($_GET['p']) ? (int)$_GET['p'] : (isset($_GET['n']) ? (int)$_GET['n'] : null); $postsOffset = (int)($_GET['o'] ?? 0); $postsTake = 5; -$templating->vars([ +tpl_vars([ 'posts_offset' => $postsOffset, 'posts_take' => $postsTake, ]); @@ -57,7 +55,7 @@ if ($postId !== null) { $commentsInfo = comments_category_info($post['comment_section_id']); } - echo $templating->render('news.post', [ + echo tpl_render('news.post', [ 'post' => $post, 'comments_perms' => comments_get_perms($app->getUserId()), 'comments_category' => $commentsInfo, @@ -123,7 +121,7 @@ if ($categoryId !== null) { $getFeatured->bindValue('category_id', $category['category_id'], PDO::PARAM_INT); $featured = $getFeatured->execute() ? $getFeatured->fetchAll() : []; - echo $templating->render('news.category', compact('category', 'posts', 'featured')); + echo tpl_render('news.category', compact('category', 'posts', 'featured')); return; } @@ -149,7 +147,7 @@ $postsCount = (int)Database::query(' AND c.`is_hidden` = false ')->fetchColumn(); -$templating->var('posts_count', $postsCount); +tpl_var('posts_count', $postsCount); if ($postsOffset < 0 || $postsOffset >= $postsCount) { echo render_error(404); @@ -188,4 +186,4 @@ if (!$posts) { return; } -echo $templating->render('news.index', compact('categories', 'posts')); +echo tpl_render('news.index', compact('categories', 'posts')); diff --git a/public/profile.php b/public/profile.php index 50dd5fda..950aeb1e 100644 --- a/public/profile.php +++ b/public/profile.php @@ -40,8 +40,6 @@ switch ($mode) { case 'view': default: - $templating = $app->getTemplating(); - $getProfile = Database::prepare(' SELECT u.*, @@ -77,11 +75,11 @@ switch ($mode) { if (!$profile) { http_response_code(404); - echo $templating->render('user.notfound'); + echo tpl_render('user.notfound'); break; } - $templating->vars(compact('profile')); - echo $templating->render('user.view'); + tpl_vars(compact('profile')); + echo tpl_render('user.view'); break; } diff --git a/public/settings.php b/public/settings.php index 7af984dd..f3bba381 100644 --- a/public/settings.php +++ b/public/settings.php @@ -4,8 +4,6 @@ use Misuzu\IO\File; require_once __DIR__ . '/../misuzu.php'; -$tpl = $app->getTemplating(); - $queryOffset = (int)($_GET['o'] ?? 0); $queryTake = 15; @@ -53,7 +51,7 @@ $avatarErrorStrings = [ ], ]; -$tpl->vars([ +tpl_vars([ 'settings_perms' => $perms, 'settings_mode' => $settingsMode, 'settings_modes' => $settingsModes, @@ -61,8 +59,8 @@ $tpl->vars([ if (!array_key_exists($settingsMode, $settingsModes)) { http_response_code(404); - $tpl->var('settings_title', 'Not Found'); - echo $tpl->render('settings.notfound'); + tpl_var('settings_title', 'Not Found'); + echo tpl_render('settings.notfound'); return; } @@ -269,8 +267,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } -$tpl->var('settings_title', $settingsModes[$settingsMode]); -$tpl->var('settings_errors', $settingsErrors); +tpl_vars([ + 'settings_title' => $settingsModes[$settingsMode], + 'settings_errors' => $settingsErrors, +]); switch ($settingsMode) { case 'account': @@ -292,7 +292,7 @@ switch ($settingsMode) { $currentEmail = $getMail->execute() ? $getMail->fetchColumn() : 'Failed to fetch e-mail address.'; $userHasAvatar = File::exists($app->getStore('avatars/original')->filename($avatarFileName)); - $tpl->vars([ + tpl_vars([ 'avatar_user_id' => $app->getUserId(), 'avatar_max_width' => $avatarWidthMax, 'avatar_max_height' => $avatarHeightMax, @@ -328,7 +328,7 @@ switch ($settingsMode) { $getSessions->bindValue('user_id', $app->getUserId()); $sessions = $getSessions->execute() ? $getSessions->fetchAll() : []; - $tpl->vars([ + tpl_vars([ 'active_session_id' => $app->getSessionId(), 'user_sessions' => $sessions, 'sessions_offset' => $queryOffset, @@ -370,7 +370,7 @@ switch ($settingsMode) { $app->getUserId() ); - $tpl->vars([ + tpl_vars([ 'audit_logs' => $auditLog, 'audit_log_count' => $auditLogCount, 'audit_log_take' => $queryTake, @@ -397,4 +397,4 @@ switch ($settingsMode) { break; } -echo $tpl->render("settings.{$settingsMode}"); +echo tpl_render("settings.{$settingsMode}"); diff --git a/src/Application.php b/src/Application.php index ea20ac45..55a5de45 100644 --- a/src/Application.php +++ b/src/Application.php @@ -270,54 +270,42 @@ class Application extends ApplicationBase throw new UnexpectedValueException('Templating module has already been started.'); } - $this->templatingInstance = new TemplateEngine; - $this->templatingInstance->debug($this->debugMode); + tpl_init([ + 'debug' => $this->debugMode, + ]); - $this->templatingInstance->var('globals', [ + tpl_var('globals', [ 'site_name' => $this->configInstance->get('Site', 'name', 'string', 'Flashii'), 'site_description' => $this->configInstance->get('Site', 'description'), 'site_twitter' => $this->configInstance->get('Site', 'twitter'), 'site_url' => $this->configInstance->get('Site', 'url'), ]); - $this->templatingInstance->addFilter('json_decode'); - $this->templatingInstance->addFilter('byte_symbol'); - $this->templatingInstance->addFilter('html_link'); - $this->templatingInstance->addFilter('html_colour'); - $this->templatingInstance->addFilter('url_construct'); - $this->templatingInstance->addFilter('country_name', 'get_country_name'); - $this->templatingInstance->addFilter('flip', 'array_flip'); - $this->templatingInstance->addFilter('first_paragraph'); - $this->templatingInstance->addFilter('colour_get_css'); - $this->templatingInstance->addFilter('colour_get_css_contrast'); - $this->templatingInstance->addFilter('colour_get_inherit'); - $this->templatingInstance->addFilter('colour_get_red'); - $this->templatingInstance->addFilter('colour_get_green'); - $this->templatingInstance->addFilter('colour_get_blue'); - $this->templatingInstance->addFilter('parse_line'); - $this->templatingInstance->addFilter('parse_text'); - $this->templatingInstance->addFilter('asset_url'); - $this->templatingInstance->addFilter('vsprintf'); + tpl_add_function('json_decode', true); + tpl_add_function('byte_symbol', true); + tpl_add_function('html_link', true); + tpl_add_function('html_colour', true); + tpl_add_function('url_construct', true); + tpl_add_function('country_name', true, 'get_country_name'); + tpl_add_function('flip', true, 'array_flip'); + tpl_add_function('first_paragraph', true); + tpl_add_function('colour_get_css', true); + tpl_add_function('colour_get_css_contrast', true); + tpl_add_function('colour_get_inherit', true); + tpl_add_function('colour_get_red', true); + tpl_add_function('colour_get_green', true); + tpl_add_function('colour_get_blue', true); + tpl_add_function('parse_line', true); + tpl_add_function('parse_text', true); + tpl_add_function('asset_url', true); + tpl_add_function('vsprintf', true); - $this->templatingInstance->addFunction('git_commit_hash'); - $this->templatingInstance->addFunction('git_branch'); - $this->templatingInstance->addFunction('csrf_token', 'tmp_csrf_token'); - $this->templatingInstance->addFunction('perms_check'); + tpl_add_function('git_commit_hash'); + tpl_add_function('git_branch'); + tpl_add_function('csrf_token', false, 'tmp_csrf_token'); + tpl_add_function('perms_check'); - $this->templatingInstance->var('app', $this); - } - - /** - * Gets an instance of the templating engine. - * @return TemplateEngine - */ - public function getTemplating(): TemplateEngine - { - if (is_null($this->templatingInstance)) { - throw new UnexpectedValueException('Internal templating engine instance is null, did you run startDatabase yet?'); - } - - return $this->templatingInstance; + tpl_var('app', $this); } public function startMailer(): void diff --git a/src/TemplateEngine.php b/src/TemplateEngine.php deleted file mode 100644 index eaeaa1de..00000000 --- a/src/TemplateEngine.php +++ /dev/null @@ -1,239 +0,0 @@ - - */ -class TemplateEngine -{ - /** - * Template file extension. - */ - private const FILE_EXTENSION = '.twig'; - - public const TWIG_DEFAULT = Twig_Loader_Filesystem::MAIN_NAMESPACE; - - /** - * Instance of the Twig Environment. - * @var Twig_Environment - */ - private $twig; - - /** - * Instance a Twig loader, probably only compatible with the Filesystem type. - * @var Twig_Loader_Filesystem - */ - private $loader; - - /** - * Render arguments. - * @var array - */ - private $vars = []; - - /** - * TemplateEngine constructor. - * @param null|string $cache - * @param bool $strict - * @param bool $autoReload - * @param bool $debug - */ - public function __construct( - ?string $cache = null, - bool $strict = true, - bool $autoReload = false, - bool $debug = false - ) { - $this->loader = new Twig_Loader_Filesystem; - $this->twig = new Twig_Environment($this->loader, [ - 'cache' => $cache ?? false, - 'strict_variables' => $strict, - 'auto_reload' => $autoReload, - 'debug' => $debug, - ]); - $this->twig->addExtension(new Twig_Extensions_Extension_Date); - } - - /** - * Toggles debug mode on or off. - * @param bool $mode - */ - public function debug(bool $mode): void - { - if ($this->twig->isDebug() === $mode) { - return; - } - - if ($mode) { - $this->twig->enableDebug(); - return; - } - - $this->twig->disableDebug(); - } - - /** - * Toggles cache auto reloading on or off. - * @param bool $mode - */ - public function autoReload(bool $mode): void - { - if ($this->twig->isAutoReload() === $mode) { - return; - } - - if ($mode) { - $this->twig->enableAutoReload(); - return; - } - - $this->twig->disableAutoReload(); - } - - /** - * Sets the cache path and alternatively turns it off. - * @param string $path - */ - public function cache(?string $path): void - { - $this->twig->setCache($path ?? false); - } - - /** - * Adds a template path, first one is regarded as the master. - * @param string $name - * @param string $path - */ - public function addPath(string $name, string $path): void - { - try { - if (count($this->loader->getPaths()) < 1) { - $this->loader->addPath($path); - } - - $this->loader->addPath($path, $name); - } catch (\Twig_Error_Loader $e) { - } - } - - /** - * Sets a render var. - * @param string $name - * @param mixed $value - */ - public function var(string $name, $value): void - { - $this->vars[$name] = $value; - } - - /** - * Sets render vars. - * @param array $vars - */ - public function vars(array $vars): void - { - $this->vars = array_merge($this->vars, $vars); - } - - /** - * Converts . to / and appends the file extension. - * @param string $path - * @return string - */ - private function fixPath(string $path): string - { - // if the .twig extension if already present just assume that the path is already correct - if (ends_with($path, self::FILE_EXTENSION)) { - return $path; - } - - return str_replace('.', '/', $path) . self::FILE_EXTENSION; - } - - /** - * Renders a template file. - * @param string $path - * @param array|null $vars - * @return string - * @throws \Twig_Error_Loader - * @throws \Twig_Error_Runtime - * @throws \Twig_Error_Syntax - */ - public function render(string $path, ?array $vars = null): string - { - if ($this->twig->isDebug()) { - $this->var('query_count', Database::queryCount()); - } - - $path = self::fixPath($path); - - if ($vars !== null) { - $this->vars($vars); - } - - if (!$this->exists($path, Twig_Loader_Filesystem::MAIN_NAMESPACE)) { - $namespace = $this->findNamespace($path); - - if ($namespace !== null) { - $path = "@{$this->findNamespace($path)}/{$path}"; - } - } - - return $this->twig->render($path, $this->vars); - } - - /** - * Adds a function. - * @param string $name - * @param Callable $callable - */ - public function addFunction(string $name, callable $callable = null): void - { - $this->twig->addFunction(new Twig_SimpleFunction($name, $callable === null ? $name : $callable)); - } - - /** - * Adds a filter. - * @param string $name - * @param Callable $callable - */ - public function addFilter(string $name, callable $callable = null): void - { - $this->twig->addFilter(new Twig_SimpleFilter($name, $callable === null ? $name : $callable)); - } - - /** - * Finds in which namespace a template exists. - * @param string $path - * @return string - */ - public function findNamespace(string $path): ?string - { - foreach ($this->loader->getNamespaces() as $namespace) { - if ($this->exists($path, $namespace)) { - return $namespace; - } - } - - return null; - } - - /** - * Checks if a template exists. - * @param string $path - * @param string $namespace - * @return bool - */ - public function exists(string $path, string $namespace): bool - { - return $this->loader->exists("@{$namespace}/" . self::fixPath($path)); - } -} diff --git a/src/Twig.php b/src/Twig.php new file mode 100644 index 00000000..6b15ffc6 --- /dev/null +++ b/src/Twig.php @@ -0,0 +1,26 @@ + false, + 'strict_variables' => true, + 'auto_reload' => false, + 'debug' => false, + ], $options); + + $GLOBALS[MSZ_TPL_VARS_STORE] = []; + + $loader = new Twig_Loader_Filesystem; + $twig = new Twig($loader, $options); + $twig->addExtension(new Twig_Extensions_Extension_Date); +} + +function tpl_var(string $key, $value): void +{ + $GLOBALS[MSZ_TPL_VARS_STORE][$key] = $value; +} + +function tpl_vars(array $vars): void +{ + $GLOBALS[MSZ_TPL_VARS_STORE] = array_merge($GLOBALS[MSZ_TPL_VARS_STORE], $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('.', '/', $path) . MSZ_TPL_FILE_EXT; +} + +function tpl_add_function(string $name, bool $isFilter = false, callable $callable = null): void +{ + $twig = Twig::instance(); + + if ($isFilter) { + $twig->addFilter(new Twig_SimpleFilter($name, $callable === null ? $name : $callable)); + } else { + $twig->addFunction(new Twig_SimpleFunction($name, $callable === null ? $name : $callable)); + } +} + +function tpl_exists(string $path): bool +{ + return Twig::instance()->getLoader()->exists(tpl_sanitise_path($path)); +} + +function tpl_render(string $path, array $vars = []): string +{ + $twig = Twig::instance(); + + if ($twig->isDebug()) { + tpl_var('query_count', Database::queryCount()); + } + + $path = tpl_sanitise_path($path); + + if (count($vars)) { + tpl_vars($vars); + } + + return $twig->render($path, $GLOBALS[MSZ_TPL_VARS_STORE]); +} diff --git a/views/mio/_layout/comments.twig b/templates/_layout/comments.twig similarity index 100% rename from views/mio/_layout/comments.twig rename to templates/_layout/comments.twig diff --git a/views/mio/_layout/header.twig b/templates/_layout/header.twig similarity index 100% rename from views/mio/_layout/header.twig rename to templates/_layout/header.twig diff --git a/views/mio/_layout/meta.twig b/templates/_layout/meta.twig similarity index 100% rename from views/mio/_layout/meta.twig rename to templates/_layout/meta.twig diff --git a/views/auth/auth.twig b/templates/auth/auth.twig similarity index 99% rename from views/auth/auth.twig rename to templates/auth/auth.twig index 69cafda5..876b049c 100644 --- a/views/auth/auth.twig +++ b/templates/auth/auth.twig @@ -1,4 +1,4 @@ -{% extends '@auth/master.twig' %} +{% extends 'auth/master.twig' %} {% block content %}