login checks and some minor javascriptery

This commit is contained in:
flash 2018-05-22 04:09:53 +02:00
parent 9d3074dfd0
commit 4c366ce454
9 changed files with 156 additions and 54 deletions

View file

@ -8,6 +8,12 @@
background-color: #23172a;
}
&--hidden {
.container__content {
display: none;
}
}
&__title {
display: block;
text-decoration: none;

View file

@ -18,7 +18,7 @@ $templating = $app->getTemplating();
if ($forumId > 0) {
$getForum = $db->prepare('
SELECT
`forum_id`, `forum_name`, `forum_type`, `forum_link`, `forum_parent`,
`forum_id`, `forum_name`, `forum_type`, `forum_link`, `forum_link_clicks`, `forum_parent`,
(
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics`
@ -38,6 +38,16 @@ if (empty($forum) || ($forum['forum_type'] == 2 && empty($forum['forum_link'])))
}
if ($forum['forum_type'] == 2) {
if ($forum['forum_link_clicks'] !== null) {
$incrementLinkClicks = $db->prepare('
UPDATE `msz_forum_categories`
SET `forum_link_clicks` = `forum_link_clicks` + 1
WHERE `forum_id` = :forum_id
');
$incrementLinkClicks->bindValue('forum_id', $forum['forum_id']);
$incrementLinkClicks->execute();
}
header('Location: ' . $forum['forum_link']);
return;
}

View file

@ -31,7 +31,7 @@ $categories = array_merge([
$getSubCategories = $db->prepare('
SELECT
f.`forum_id`, f.`forum_name`, f.`forum_description`, f.`forum_type`, f.`forum_link`,
f.`forum_id`, f.`forum_name`, f.`forum_description`, f.`forum_type`, f.`forum_link`, f.`forum_link_clicks`,
t.`topic_id` as `recent_topic_id`, p.`post_id` as `recent_post_id`,
t.`topic_title` as `recent_topic_title`,
p.`post_created` as `recent_post_created`,

View file

@ -4,21 +4,17 @@ use Misuzu\Net\IPAddress;
require_once __DIR__ . '/../../misuzu.php';
$db = Database::connection();
$templating = $app->getTemplating();
if (!$app->hasActiveSession()) {
header('Location: /');
http_response_code(403);
echo $templating->render('errors.403');
return;
}
$postRequest = $_SERVER['REQUEST_METHOD'] === 'POST';
$db = Database::connection();
$templating = $app->getTemplating();
// ORDER OF CHECKING
// - $postId non-zero: enter quote mode
// - $topicId non-zero: enter reply mode
// - $forumId non-zero: enter create mode
// - all zero: enter explode mode
if ($postRequest) {
$topicId = max(0, (int)($_POST['post']['topic'] ?? 0));
$forumId = max(0, (int)($_POST['post']['forum'] ?? 0));
@ -28,6 +24,12 @@ if ($postRequest) {
$forumId = max(0, (int)($_GET['f'] ?? 0));
}
if (empty($postId) && empty($topicId) && empty($forumId)) {
http_response_code(404);
echo $templating->render('errors.404');
return;
}
if (!empty($postId)) {
$getPost = $db->prepare('
SELECT `post_id`, `topic_id`
@ -58,7 +60,7 @@ if (!empty($topicId)) {
if (!empty($forumId)) {
$getForum = $db->prepare('
SELECT `forum_id`, `forum_name`
SELECT `forum_id`, `forum_name`, `forum_type`
FROM `msz_forum_categories`
WHERE `forum_id` = :forum_id
');
@ -66,6 +68,18 @@ if (!empty($forumId)) {
$forum = $getForum->execute() ? $getForum->fetch() : false;
}
if (empty($forum)) {
http_response_code(404);
echo $templating->render('errors.404');
return;
}
if ($forum['forum_type'] != 0) {
http_response_code(400);
echo $templating->render('errors.400');
return;
}
if ($postRequest) {
$createPost = $db->prepare('
INSERT INTO `msz_forum_posts`

View file

@ -0,0 +1,8 @@
{% extends '@mio/errors/master.twig' %}
{% set error_code = 400 %}
{% set error_text = 'Bad Request' %}
{% block error_message %}
<p>Whatever you tried to do, you probably shouldn't.</p>
{% endblock %}

View file

@ -13,7 +13,7 @@
{% endif %}
{% if forum_info.forum_type == 0 %}
{% set fcbuttons = forum_category_buttons(forum_info) %}
{% set fcbuttons = app.hasActiveSession ? forum_category_buttons(forum_info) : '' %}
{% set fcpagination = pagination(forum_info.forum_topic_count, forum_range, forum_offset, canonical_url) %}
{{ fcbuttons }}

View file

@ -63,50 +63,62 @@
{% endif %}
</div>
<div class="forum__listing__entry__stats">
<div class="forum__listing__entry__topics" title="Topics">{{ forum.forum_topic_count|number_format }}</div>
<div class="forum__listing__entry__posts" title="Posts">{{ forum.forum_post_count|number_format }}</div>
</div>
<div class="forum__listing__entry__activity">
{% if forum.recent_topic_id is null %}
<div class="forum__listing__entry__activity__none">
There are no posts in this forum yet.
{% if forum.forum_type == 2 %}
{% if forum.forum_link_clicks is not null %}
<div class="forum__listing__entry__stats">
<div class="forum__listing__entry__topics" title="Clicks">{{ forum.forum_link_clicks|number_format }}</div>
</div>
{% else %}
<div class="forum__listing__entry__activity__details">
<div class="forum__listing__entry__activity__title">
<a class="forum__listing__entry__activity__title__link"
href="/forum/topic.php?p={{ forum.recent_post_id }}#p{{ forum.recent_post_id }}">
{{ forum.recent_topic_title|slice(0, 30) ~ (forum.recent_topic_title|length > 30 ? '...' : '') }}
</a>
</div>
<div class="forum__listing__entry__activity__info">
{% if forum.recent_post_user_id is not null %}
by <a
href="/profile.php?u={{ forum.recent_post_user_id }}"
style="color:{{ forum.recent_post_user_colour|colour_get_css }}"
class="forum__listing__entry__activity__user">{{ forum.recent_post_username }}</a>,
{% endif %}
{{ forum.recent_post_created }}
</div>
</div>
{% if forum.recent_post_user_id is not null %}
<a
href="/profile.php?u={{ forum.recent_post_user_id }}"
class="avatar forum__listing__entry__activity__avatar"
style="background-image:url('/profile.php?u={{ forum.recent_post_user_id }}&amp;m=avatar')">
</a>
{% endif %}
{% endif %}
</div>
{% else %}
<div class="forum__listing__entry__stats">
<div class="forum__listing__entry__topics" title="Topics">{{ forum.forum_topic_count|number_format }}</div>
<div class="forum__listing__entry__posts" title="Posts">{{ forum.forum_post_count|number_format }}</div>
</div>
{% endif %}
{% if forum.forum_type != 2 or forum.forum_link_clicks is not null %}
<div class="forum__listing__entry__activity">
{% if forum.forum_type != 2 %}
{% if forum.recent_topic_id is null %}
<div class="forum__listing__entry__activity__none">
There are no posts in this forum yet.
</div>
{% else %}
<div class="forum__listing__entry__activity__details">
<div class="forum__listing__entry__activity__title">
<a class="forum__listing__entry__activity__title__link"
href="/forum/topic.php?p={{ forum.recent_post_id }}#p{{ forum.recent_post_id }}">
{{ forum.recent_topic_title|slice(0, 30) ~ (forum.recent_topic_title|length > 30 ? '...' : '') }}
</a>
</div>
<div class="forum__listing__entry__activity__info">
{% if forum.recent_post_user_id is not null %}
by <a
href="/profile.php?u={{ forum.recent_post_user_id }}"
style="color:{{ forum.recent_post_user_colour|colour_get_css }}"
class="forum__listing__entry__activity__user">{{ forum.recent_post_username }}</a>,
{% endif %}
{{ forum.recent_post_created }}
</div>
</div>
{% if forum.recent_post_user_id is not null %}
<a
href="/profile.php?u={{ forum.recent_post_user_id }}"
class="avatar forum__listing__entry__activity__avatar"
style="background-image:url('/profile.php?u={{ forum.recent_post_user_id }}&amp;m=avatar')">
</a>
{% endif %}
{% endif %}
{% endif %}
</div>
{% endif %}
</div>
{% endmacro %}
{% macro forum_topic_buttons(topic) %}
<div class="forum__actions forum__actions__content">
<a href="#reply" class="input__button forum__actions__button">Reply</a>
<a href="#reply" class="input__button forum__actions__button" onclick="openContainer('reply')">Reply</a>
</div>
{% endmacro %}
@ -258,7 +270,7 @@
{% macro forum_posting_form(title, target_id, is_reply, element_id) %}
{% set is_reply = is_reply ? true : false %}
<form
<form{% if element_id is defined %} id="{{ element_id }}"{% endif %}
class="container forum__posting"
method="post"
action="/forum/posting.php">
@ -266,7 +278,7 @@
{{ title }}
</div>
<div class="container__content forum__posting__content"{% if element_id is defined %} id="{{ element_id }}"{% endif %}>
<div class="container__content forum__posting__content">
<input type="hidden" name="post[{{ is_reply ? 'topic' : 'forum' }}]" value="{{ target_id }}">
{% if not is_reply %}

View file

@ -6,7 +6,7 @@
{% set base_url = '/forum/topic.php?t=' ~ topic_info.topic_id %}
{% set canonical_url = base_url %}
{% set ftbuttons = forum_topic_buttons(topic_info) %}
{% set ftbuttons = app.hasActiveSession ? forum_topic_buttons(topic_info) : '' %}
{% set ftpagination = pagination(topic_info.topic_post_count, topic_range, topic_offset, base_url) %}
{% block content %}
@ -23,7 +23,16 @@
{{ forum_post_listing(topic_posts, topic_info.topic_first_post_id) }}
{{ ftpagination }}
{{ forum_posting_form('Reply', topic_info.topic_id, true, 'reply') }}
{% if app.hasActiveSession %}
{{ forum_posting_form('Reply', topic_info.topic_id, true, 'reply', true) }}
<script>
window.addEventListener('load', () => {
closeContainer('reply');
document.getElementById('reply').children[0].addEventListener('mouseover', () => openContainer('reply'));
});
</script>
{% endif %}
{{ navigation(mio_navigation, '/forum/') }}
{% endblock %}

View file

@ -77,5 +77,48 @@
</div>
</footer>
</div>
<script>
// move this to an external JS/TS file eventually.
const containerClass = 'container',
containerHiddenClass = 'container--hidden';
function validateContainer(elem) {
return elem.classList.contains(containerClass);
}
function containerIsClosed(elem) {
return elem.classList.contains(containerHiddenClass);
}
function toggleContainer(id) {
const elem = document.getElementById(id);
if (!validateContainer(elem))
return;
if (containerIsClosed(elem))
openContainer(elem);
else
closeContainer(elem);
}
function openContainer(id) {
const elem = document.getElementById(id);
if (!validateContainer(elem) || !containerIsClosed(elem))
return;
elem.classList.remove(containerHiddenClass);
}
function closeContainer(id) {
const elem = document.getElementById(id);
if (!validateContainer(elem) || containerIsClosed(elem))
return;
elem.classList.add(containerHiddenClass);
}
</script>
</body>
</html>