Restructure templating system.

This commit is contained in:
flash 2018-08-15 03:12:58 +02:00
parent d82277ebdd
commit f1979c8e4a
83 changed files with 290 additions and 463 deletions

View file

@ -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()));
}
}

View file

@ -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;
}

View file

@ -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,

View file

@ -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,

View file

@ -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,
]);

View file

@ -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,
]);

View file

@ -1,8 +1,6 @@
<?php
require_once __DIR__ . '/../../misuzu.php';
$templating = $app->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,

View file

@ -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,

View file

@ -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;
}

View file

@ -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':

View file

@ -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;
}

View file

@ -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,

View file

@ -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'));

View file

@ -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;
}

View file

@ -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}");

View file

@ -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

View file

@ -1,239 +0,0 @@
<?php
namespace Misuzu;
use Twig_Environment;
use Twig_Extensions_Extension_Date;
use Twig_Loader_Filesystem;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
* Wrapper for Twig.
* @package Misuzu
* @author flashwave <me@flash.moe>
*/
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));
}
}

26
src/Twig.php Normal file
View file

@ -0,0 +1,26 @@
<?php
namespace Misuzu;
use Twig_Environment;
use Twig_LoaderInterface;
use UnexpectedValueException;
final class Twig extends Twig_Environment
{
protected static $instance = null;
public static function instance(): Twig_Environment
{
return self::$instance;
}
public function __construct(Twig_LoaderInterface $loader, array $options = [])
{
if (self::$instance !== null) {
throw new UnexpectedValueException('Instance of Twig already present, use the static instance() function.');
}
parent::__construct($loader, $options);
self::$instance = $this;
}
}

80
src/tpl.php Normal file
View file

@ -0,0 +1,80 @@
<?php
use Misuzu\Database;
use Misuzu\Twig;
define('MSZ_TPL_FILE_EXT', '.twig');
define('MSZ_TPL_VARS_STORE', '_msz_tpl_vars');
function tpl_init(array $options = []): void
{
$options = array_merge([
'cache' => 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]);
}

View file

@ -1,4 +1,4 @@
{% extends '@auth/master.twig' %}
{% extends 'auth/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,4 +1,4 @@
{% extends '@auth/master.twig' %}
{% extends 'auth/master.twig' %}
{% block content %}
<div class="container logout">

View file

@ -1,4 +1,4 @@
{% extends '@auth/master.twig' %}
{% extends 'auth/master.twig' %}
{% block content %}
<div class="container logout">

View file

@ -1,4 +1,4 @@
{% extends '@auth/master.twig' %}
{% extends 'auth/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@mio/changelog/master.twig' %}
{% from '@mio/_layout/comments.twig' import comments_section %}
{% extends 'changelog/master.twig' %}
{% from '_layout/comments.twig' import comments_section %}
{% set is_valid = change|length > 0 %}
{% set title = 'Changelog » ' ~ (is_valid ? 'Change #' ~ change.change_id : 'Unknown Change') %}

View file

@ -1,7 +1,7 @@
{% extends '@mio/changelog/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% from '@mio/changelog/macros.twig' import changelog_listing %}
{% from '@mio/_layout/comments.twig' import comments_section %}
{% extends 'changelog/master.twig' %}
{% from 'macros.twig' import pagination %}
{% from 'changelog/macros.twig' import changelog_listing %}
{% from '_layout/comments.twig' import comments_section %}
{% set is_valid = changes|length > 0 %}
{% set is_date = changelog_date|length > 0 %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/master.twig' %}
{% extends 'master.twig' %}
{% if manage_link is not defined %}
{% set manage_link = '/manage/changelog.php' %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/errors/master.twig' %}
{% extends 'errors/master.twig' %}
{% set error_code = 400 %}
{% set error_text = 'Bad Request' %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/errors/master.twig' %}
{% extends 'errors/master.twig' %}
{% set error_code = 403 %}
{% set error_text = 'Access Denied!' %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/errors/master.twig' %}
{% extends 'errors/master.twig' %}
{% set error_code = 404 %}
{% set error_text = 'Not Found!' %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/master.twig' %}
{% extends 'master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,6 +1,6 @@
{% extends '@mio/forum/master.twig' %}
{% from '@mio/macros.twig' import navigation, pagination %}
{% from '@mio/forum/macros.twig' import forum_category_listing, forum_topic_listing, forum_category_buttons %}
{% extends 'forum/master.twig' %}
{% from 'macros.twig' import navigation, pagination %}
{% from 'forum/macros.twig' import forum_category_listing, forum_topic_listing, forum_category_buttons %}
{% set title = forum_info.forum_name %}
{% set canonical_url = '/forum/forum.php'|url_construct({

View file

@ -1,5 +1,5 @@
{% extends '@mio/forum/master.twig' %}
{% from '@mio/forum/macros.twig' import forum_category_listing %}
{% extends 'forum/master.twig' %}
{% from 'forum/macros.twig' import forum_category_listing %}
{% set title = 'Forum Listing' %}
{% set canonical_url = '/forum/' %}

View file

@ -0,0 +1 @@
{% extends 'master.twig' %}

View file

@ -1,6 +1,6 @@
{% extends '@mio/forum/master.twig' %}
{% from '@mio/macros.twig' import navigation %}
{% from '@mio/forum/macros.twig' import forum_posting_form %}
{% extends 'forum/master.twig' %}
{% from 'macros.twig' import navigation %}
{% from 'forum/macros.twig' import forum_posting_form %}
{% set title = 'Posting' %}

View file

@ -1,7 +1,7 @@
{% extends '@mio/forum/master.twig' %}
{% from '@mio/macros.twig' import navigation, pagination %}
{% extends 'forum/master.twig' %}
{% from 'macros.twig' import navigation, pagination %}
{%
from '@mio/forum/macros.twig'
from 'forum/macros.twig'
import
forum_post_listing,
forum_topic_buttons,

View file

@ -1,6 +1,6 @@
{% extends '@mio/home/master.twig' %}
{% from '@mio/news/macros.twig' import news_preview %}
{% from '@mio/changelog/macros.twig' import changelog_listing %}
{% extends 'home/master.twig' %}
{% from 'news/macros.twig' import news_preview %}
{% from 'changelog/macros.twig' import changelog_listing %}
{% set canonical_url = '/' %}

View file

@ -0,0 +1 @@
{% extends 'master.twig' %}

View file

@ -1,4 +1,4 @@
{% extends '@manage/changelog/master.twig' %}
{% extends 'manage/changelog/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@manage/changelog/master.twig' %}
{% from '@manage/macros.twig' import pagination %}
{% extends 'manage/changelog/master.twig' %}
{% from 'manage/macros.twig' import pagination %}
{% block content %}
<div class="container">

View file

@ -1,4 +1,4 @@
{% extends '@manage/changelog/master.twig' %}
{% extends 'manage/changelog/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@manage/changelog/master.twig' %}
{% from '@manage/macros.twig' import pagination %}
{% extends 'manage/changelog/master.twig' %}
{% from 'manage/macros.twig' import pagination %}
{% block content %}
<div class="container">

View file

@ -0,0 +1 @@
{% extends 'manage/master.twig' %}

View file

@ -1,4 +1,4 @@
{% extends '@manage/changelog/master.twig' %}
{% extends 'manage/changelog/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@manage/changelog/master.twig' %}
{% from '@manage/macros.twig' import pagination %}
{% extends 'manage/changelog/master.twig' %}
{% from 'manage/macros.twig' import pagination %}
{% block content %}
<div class="container">

View file

@ -0,0 +1 @@
{% extends 'manage/master.twig' %}

View file

@ -1,4 +1,4 @@
{% extends '@manage/general/master.twig' %}
{% extends 'manage/general/master.twig' %}
{% block content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@manage/users/master.twig' %}
{% from '@manage/macros.twig' import pagination %}
{% extends 'manage/users/master.twig' %}
{% from 'manage/macros.twig' import pagination %}
{% block content %}
<div class="container listing user-listing">

View file

@ -0,0 +1 @@
{% extends 'manage/master.twig' %}

View file

@ -1,5 +1,5 @@
{% extends '@manage/users/master.twig' %}
{% from '@manage/macros.twig' import pagination %}
{% extends 'manage/users/master.twig' %}
{% from 'manage/macros.twig' import pagination %}
{% block content %}
{% if can_manage_roles %}

View file

@ -1,5 +1,5 @@
{% extends '@manage/users/master.twig' %}
{% from '@manage/macros.twig' import permissions_table %}
{% extends 'manage/users/master.twig' %}
{% from 'manage/macros.twig' import permissions_table %}
{% block content %}
<form action="?v=role{{ edit_role is defined ? '&r=' ~ edit_role.role_id : '' }}" method="post">

View file

@ -1,5 +1,5 @@
{% extends '@manage/users/master.twig' %}
{% from '@manage/macros.twig' import permissions_table %}
{% extends 'manage/users/master.twig' %}
{% from 'manage/macros.twig' import permissions_table %}
{% block content %}
{% if can_manage_users %}

View file

@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% include '@mio/_layout/meta.twig' %}
{% include '_layout/meta.twig' %}
<link href="{{ '/css/mio.css'|asset_url }}" rel="stylesheet">
<link href="{{ '/css/libraries.css'|asset_url }}" rel="stylesheet">
</head>

View file

@ -1,6 +1,6 @@
{% extends '@mio/news/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% from '@mio/news/macros.twig' import news_preview %}
{% extends 'news/master.twig' %}
{% from 'macros.twig' import pagination %}
{% from 'news/macros.twig' import news_preview %}
{% set title = category.category_name ~ ' :: News' %}
{% set canonical_url = '/news.php'|url_construct({

View file

@ -1,6 +1,6 @@
{% extends '@mio/news/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% from '@mio/news/macros.twig' import news_preview %}
{% extends 'news/master.twig' %}
{% from 'macros.twig' import pagination %}
{% from 'news/macros.twig' import news_preview %}
{% set title = 'News' %}
{% set canonical_url = '/news.php'|url_construct({'o':posts_offset}) %}

View file

@ -0,0 +1 @@
{% extends 'master.twig' %}

View file

@ -1,5 +1,5 @@
{% extends '@mio/news/master.twig' %}
{% from '@mio/_layout/comments.twig' import comments_section %}
{% extends 'news/master.twig' %}
{% from '_layout/comments.twig' import comments_section %}
{% set title = post.post_title ~ ' :: News' %}
{% set canonical_url = '/news.php?p=' ~ post.post_id %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/settings/master.twig' %}
{% extends 'settings/master.twig' %}
{% block settings_content %}
<div class="container">

View file

@ -1,5 +1,5 @@
{% extends '@mio/settings/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% extends 'settings/master.twig' %}
{% from 'macros.twig' import pagination %}
{% set alpagination = pagination(
audit_log_count,

View file

@ -1,5 +1,5 @@
{% extends '@mio/master.twig' %}
{% from '@mio/macros.twig' import navigation %}
{% extends 'master.twig' %}
{% from 'macros.twig' import navigation %}
{% set title = 'Settings » ' ~ settings_title %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/settings/master.twig' %}
{% extends 'settings/master.twig' %}
{% block settings_content %}
<p>Could not find what you were looking for.</p>

View file

@ -1,5 +1,5 @@
{% extends '@mio/settings/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% extends 'settings/master.twig' %}
{% from 'macros.twig' import pagination %}
{% set spagination = pagination(sessions_count, sessions_take, sessions_offset, '?m=sessions', 'settings__') %}

View file

@ -1,5 +1,5 @@
{% extends '@mio/user/master.twig' %}
{% from '@mio/macros.twig' import pagination %}
{% extends 'user/master.twig' %}
{% from 'macros.twig' import pagination %}
{% set canonical_url = '/members.php'|url_construct({
'r': role.role_id != 1 ? role.role_id : 0,

View file

@ -0,0 +1 @@
{% extends 'master.twig' %}

View file

@ -1,4 +1,4 @@
{% extends '@mio/user/master.twig' %}
{% extends 'user/master.twig' %}
{% set title = 'User not found!' %}

View file

@ -1,5 +1,5 @@
{% extends '@mio/user/master.twig' %}
{% from '@mio/macros.twig' import navigation %}
{% extends 'user/master.twig' %}
{% from 'macros.twig' import navigation %}
{% set image = '/profile.php?u=' ~ profile.user_id ~ '&m=avatar' %}
{% set canonical_url = '/profile.php?u=' ~ profile.user_id %}

View file

@ -283,21 +283,19 @@ function render_info(?string $message, int $httpCode, string $template = 'errors
http_response_code($httpCode);
try {
$tpl = \Misuzu\Application::getInstance()->getTemplating();
$tpl->var('http_code', $httpCode);
tpl_var('http_code', $httpCode);
if (strlen($message)) {
$tpl->var('message', $message);
tpl_var('message', $message);
}
$template = sprintf($template, $httpCode);
if (!$tpl->exists($template, \Misuzu\TemplateEngine::TWIG_DEFAULT)) {
if (!tpl_exists($template)) {
$template = 'errors.master';
}
return $tpl->render(sprintf($template, $httpCode));
return tpl_render(sprintf($template, $httpCode));
} catch (Exception $ex) {
echo $ex->getMessage();
return $message ?? '';

View file

@ -1 +0,0 @@
{% extends '@manage/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@manage/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@manage/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@mio/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@mio/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@mio/master.twig' %}

View file

@ -1 +0,0 @@
{% extends '@mio/master.twig' %}