diff --git a/database/2018_05_17_000055_forum_structure.php b/database/2018_05_17_000055_forum_structure.php index 4b90de14..17f996a7 100644 --- a/database/2018_05_17_000055_forum_structure.php +++ b/database/2018_05_17_000055_forum_structure.php @@ -59,6 +59,7 @@ function migrate_up(PDO $conn): void `user_id` INT(10) UNSIGNED NULL DEFAULT NULL, `post_ip` BLOB NOT NULL, `post_text` TEXT NOT NULL, + `post_parse` TINYINT(4) UNSIGNED NOT NULL DEFAULT '0', `post_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `post_edited` TIMESTAMP NULL DEFAULT NULL, `post_deleted` TIMESTAMP NULL DEFAULT NULL, diff --git a/public/forum/posting.php b/public/forum/posting.php index 6d4608eb..b899cdef 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -123,7 +123,8 @@ if ($postRequest) { $forum['forum_id'], $app->getUserId(), IPAddress::remote()->getString(), - $postText + $postText, + MSZ_FORUM_POST_PARSER_BBCODE ); forum_topic_mark_read($app->getUserId(), $topicId, $forum['forum_id']); diff --git a/public/index.php b/public/index.php index c1921365..407e8c9f 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,6 @@ templatingInstance->addFilter('colour_get_red'); $this->templatingInstance->addFilter('colour_get_green'); $this->templatingInstance->addFilter('colour_get_blue'); + $this->templatingInstance->addFilter('md', 'parse_markdown'); + $this->templatingInstance->addFilter('bbcode', 'parse_bbcode'); $this->templatingInstance->addFunction('git_hash', [Application::class, 'gitCommitHash']); $this->templatingInstance->addFunction('git_branch', [Application::class, 'gitBranch']); diff --git a/src/Forum/post.php b/src/Forum/post.php index 3ac50bc9..7ba3720e 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -1,26 +1,42 @@ prepare(' INSERT INTO `msz_forum_posts` - (`topic_id`, `forum_id`, `user_id`, `post_ip`, `post_text`) + (`topic_id`, `forum_id`, `user_id`, `post_ip`, `post_text`, `post_parse`) VALUES - (:topic_id, :forum_id, :user_id, INET6_ATON(:post_ip), :post_text) + (:topic_id, :forum_id, :user_id, INET6_ATON(:post_ip), :post_text, :post_parse) '); $createPost->bindValue('topic_id', $topicId); $createPost->bindValue('forum_id', $forumId); $createPost->bindValue('user_id', $userId); $createPost->bindValue('post_ip', $ipAddress); $createPost->bindValue('post_text', $text); + $createPost->bindValue('post_parse', $parser); return $createPost->execute() ? $dbc->lastInsertId() : 0; } @@ -50,7 +66,7 @@ function forum_post_find(int $postId): array define('MSZ_FORUM_POST_LISTING_QUERY_STANDARD', ' SELECT - p.`post_id`, p.`post_text`, p.`post_created`, + p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`, p.`topic_id`, u.`user_id` as `poster_id`, u.`username` as `poster_name`, diff --git a/src/Parsers/BBCode/BBCodeParser.php b/src/Parsers/BBCode/BBCodeParser.php new file mode 100644 index 00000000..bc242146 --- /dev/null +++ b/src/Parsers/BBCode/BBCodeParser.php @@ -0,0 +1,51 @@ +tags = $tags; + } + + public function parseText(string $text): string + { + foreach ($this->tags as $tag) { + $text = $tag->parseText($text); + } + + return $text; + } +} diff --git a/src/Parsers/BBCode/BBCodeSimpleTag.php b/src/Parsers/BBCode/BBCodeSimpleTag.php new file mode 100644 index 00000000..b8ad6ccb --- /dev/null +++ b/src/Parsers/BBCode/BBCodeSimpleTag.php @@ -0,0 +1,13 @@ +getPattern(), $this->getReplacement(), $text); + } +} diff --git a/src/Parsers/BBCode/BBCodeTag.php b/src/Parsers/BBCode/BBCodeTag.php new file mode 100644 index 00000000..5a4d14e5 --- /dev/null +++ b/src/Parsers/BBCode/BBCodeTag.php @@ -0,0 +1,7 @@ +"; + }, + $text + ); + } +} diff --git a/src/Parsers/BBCode/Tags/BoldTag.php b/src/Parsers/BBCode/Tags/BoldTag.php new file mode 100644 index 00000000..8ac8a299 --- /dev/null +++ b/src/Parsers/BBCode/Tags/BoldTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/BoxTag.php b/src/Parsers/BBCode/Tags/BoxTag.php new file mode 100644 index 00000000..ba54184e --- /dev/null +++ b/src/Parsers/BBCode/Tags/BoxTag.php @@ -0,0 +1,23 @@ +' + . "
{$title}
" + . "
{$matches[2]}
" + . ''; + }, + $text + ); + } +} diff --git a/src/Parsers/BBCode/Tags/CodeTag.php b/src/Parsers/BBCode/Tags/CodeTag.php new file mode 100644 index 00000000..0b58af54 --- /dev/null +++ b/src/Parsers/BBCode/Tags/CodeTag.php @@ -0,0 +1,20 @@ +'], ['[', ']', '<', '>'], $matches[2]); + return "
{$text}
"; + }, + $text + ); + } +} diff --git a/src/Parsers/BBCode/Tags/HeaderTag.php b/src/Parsers/BBCode/Tags/HeaderTag.php new file mode 100644 index 00000000..285f41fb --- /dev/null +++ b/src/Parsers/BBCode/Tags/HeaderTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/ImageTag.php b/src/Parsers/BBCode/Tags/ImageTag.php new file mode 100644 index 00000000..44c0c891 --- /dev/null +++ b/src/Parsers/BBCode/Tags/ImageTag.php @@ -0,0 +1,17 @@ +'; + } +} diff --git a/src/Parsers/BBCode/Tags/ItalicsTag.php b/src/Parsers/BBCode/Tags/ItalicsTag.php new file mode 100644 index 00000000..9a09b9ef --- /dev/null +++ b/src/Parsers/BBCode/Tags/ItalicsTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/MarkdownTag.php b/src/Parsers/BBCode/Tags/MarkdownTag.php new file mode 100644 index 00000000..035f20a3 --- /dev/null +++ b/src/Parsers/BBCode/Tags/MarkdownTag.php @@ -0,0 +1,19 @@ +parseText($matches[1]); + }, + $text + ); + } +} diff --git a/src/Parsers/BBCode/Tags/NewLineTag.php b/src/Parsers/BBCode/Tags/NewLineTag.php new file mode 100644 index 00000000..d83ccae1 --- /dev/null +++ b/src/Parsers/BBCode/Tags/NewLineTag.php @@ -0,0 +1,17 @@ +'; + } +} diff --git a/src/Parsers/BBCode/Tags/QuoteTag.php b/src/Parsers/BBCode/Tags/QuoteTag.php new file mode 100644 index 00000000..8d3903f5 --- /dev/null +++ b/src/Parsers/BBCode/Tags/QuoteTag.php @@ -0,0 +1,24 @@ +{$matches[1]}:"; + } + + return "
{$prefix}{$matches[2]}
"; + }, + $text + ); + } +} diff --git a/src/Parsers/BBCode/Tags/SpoilerTag.php b/src/Parsers/BBCode/Tags/SpoilerTag.php new file mode 100644 index 00000000..947d85d0 --- /dev/null +++ b/src/Parsers/BBCode/Tags/SpoilerTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/StrikeTag.php b/src/Parsers/BBCode/Tags/StrikeTag.php new file mode 100644 index 00000000..8d59caed --- /dev/null +++ b/src/Parsers/BBCode/Tags/StrikeTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/UnderlineTag.php b/src/Parsers/BBCode/Tags/UnderlineTag.php new file mode 100644 index 00000000..f56d255a --- /dev/null +++ b/src/Parsers/BBCode/Tags/UnderlineTag.php @@ -0,0 +1,17 @@ +$1'; + } +} diff --git a/src/Parsers/BBCode/Tags/VideoTag.php b/src/Parsers/BBCode/Tags/VideoTag.php new file mode 100644 index 00000000..6ebf4e1c --- /dev/null +++ b/src/Parsers/BBCode/Tags/VideoTag.php @@ -0,0 +1,26 @@ +'; + } + + return ""; + }, + $text + ); + } +} diff --git a/src/Parsers/MarkdownParser.php b/src/Parsers/MarkdownParser.php new file mode 100644 index 00000000..5ad92b6a --- /dev/null +++ b/src/Parsers/MarkdownParser.php @@ -0,0 +1,20 @@ +parsedown = new Parsedown; + } + + public function parseText(string $text): string + { + return $this->parsedown->text($text); + } +} diff --git a/src/Parsers/Parser.php b/src/Parsers/Parser.php new file mode 100644 index 00000000..e9c19750 --- /dev/null +++ b/src/Parsers/Parser.php @@ -0,0 +1,34 @@ +parseText($text); +} + +function parse_bbcode(string $text): string +{ + return \Misuzu\Parsers\BBCode\BBCodeParser::getOrCreateInstance()->parseText($text); +} diff --git a/views/mio/forum/macros.twig b/views/mio/forum/macros.twig index 0736a361..041efbb6 100644 --- a/views/mio/forum/macros.twig +++ b/views/mio/forum/macros.twig @@ -281,7 +281,13 @@
- {{ post.post_text }} + {% if post.post_parse == 2 %} + {{ post.post_text|escape|md|raw }} + {% elseif post.post_parse == 1 %} + {{ post.post_text|escape|bbcode|raw }} + {% else %} + {{ post.post_text|escape }} + {% endif %}
diff --git a/views/mio/master.twig b/views/mio/master.twig index 0096ec72..590b2db9 100644 --- a/views/mio/master.twig +++ b/views/mio/master.twig @@ -97,9 +97,9 @@ return; if (containerIsClosed(elem)) - openContainer(elem); + openContainer(id); else - closeContainer(elem); + closeContainer(id); } function openContainer(id) { diff --git a/views/mio/news/post.twig b/views/mio/news/post.twig index d7cf9ced..29e8ea6f 100644 --- a/views/mio/news/post.twig +++ b/views/mio/news/post.twig @@ -12,7 +12,7 @@
- {{ post.post_text|raw }} + {{ post.post_text|md|raw }}