2015-05-03 16:25:57 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Discussion Board
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
|
|
|
use Sakura\Main;
|
|
|
|
use Sakura\Database;
|
|
|
|
use Sakura\User;
|
2015-05-03 16:25:57 +00:00
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2015-11-06 22:30:37 +00:00
|
|
|
* Class Forums
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-11-06 12:05:35 +00:00
|
|
|
class Forums
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-06-27 19:29:37 +00:00
|
|
|
// Empty forum template
|
|
|
|
public static $emptyForum = [
|
2015-09-14 20:51:23 +00:00
|
|
|
'forum_id' => 0,
|
|
|
|
'forum_name' => 'Forum',
|
|
|
|
'forum_desc' => '',
|
|
|
|
'forum_link' => '',
|
|
|
|
'forum_category' => 0,
|
|
|
|
'forum_type' => 1,
|
|
|
|
'forum_posts' => 0,
|
|
|
|
'forum_topics' => 0,
|
2015-06-27 19:29:37 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Get a forum or category
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getForum($id)
|
|
|
|
{
|
2015-06-27 19:29:37 +00:00
|
|
|
|
|
|
|
// Get the forumlist from the database
|
|
|
|
$forums = Database::fetch('forums');
|
|
|
|
|
|
|
|
// Sneak the template in the array
|
|
|
|
$forums['fb'] = self::$emptyForum;
|
|
|
|
|
|
|
|
// Create an array to store the forum once we found it
|
|
|
|
$forum = [];
|
|
|
|
|
|
|
|
// Try to find the requested forum
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($forums as $list) {
|
2015-06-27 19:29:37 +00:00
|
|
|
// Once found set $forum to $list and break the loop
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($list['forum_id'] == $id) {
|
2015-06-27 19:29:37 +00:00
|
|
|
$forum['forum'] = $list;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If $forum is still empty after the foreach return false
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($forum)) {
|
2015-06-27 19:29:37 +00:00
|
|
|
return false;
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-06-27 19:29:37 +00:00
|
|
|
|
|
|
|
// Create conditions for fetching the forums
|
|
|
|
$conditions['forum_category'] = [$id, '='];
|
|
|
|
|
|
|
|
// If the current category is 0 (the built in fallback) prevent getting categories
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($id == 0) {
|
2015-06-27 19:29:37 +00:00
|
|
|
$conditions['forum_type'] = ['1', '!='];
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-06-27 19:29:37 +00:00
|
|
|
|
|
|
|
// Check if this forum/category has any subforums
|
|
|
|
$forum['forums'] = Database::fetch('forums', true, $conditions);
|
|
|
|
|
|
|
|
// Get the userdata related to last posts
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($forum['forums'] as $key => $sub) {
|
2015-07-03 17:33:02 +00:00
|
|
|
// Get last post in forum
|
|
|
|
$lastPost = Database::fetch('posts', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'forum_id' => [$sub['forum_id'], '='],
|
2015-07-03 17:33:02 +00:00
|
|
|
], ['post_id', true]);
|
|
|
|
|
2015-09-16 20:34:36 +00:00
|
|
|
$forum['forums'][$key]['last_poster'] = new User($lastPost['poster_id']);
|
|
|
|
$forum['forums'][$key]['last_post'] = array_merge(
|
|
|
|
empty($lastPost) ? [] : $lastPost,
|
|
|
|
['elapsed' => Main::timeElapsed($lastPost['post_time'])]
|
|
|
|
);
|
2015-06-27 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly grab the topics for this forum
|
2015-07-05 00:03:15 +00:00
|
|
|
$forum['topics'] = self::getTopics($forum['forum']['forum_id']);
|
2015-06-27 19:29:37 +00:00
|
|
|
|
|
|
|
// Return the forum/category
|
|
|
|
return $forum;
|
|
|
|
}
|
|
|
|
|
2015-11-15 21:43:39 +00:00
|
|
|
// Getting all topics from a forum
|
|
|
|
public static function getTopics($id)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Get the topics from the database
|
|
|
|
$topics = Database::fetch('topics', true, [
|
|
|
|
'forum_id' => [$id, '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Get the userdata related to last posts
|
|
|
|
foreach ($topics as $key => $topic) {
|
|
|
|
// Get the reply count
|
|
|
|
$topics[$key]['reply_count'] = Database::count('posts', [
|
|
|
|
'topic_id' => [$topic['topic_id'], '='],
|
|
|
|
])[0];
|
|
|
|
|
|
|
|
// Get first post in topics
|
|
|
|
$firstPost = Database::fetch('posts', false, [
|
|
|
|
'topic_id' => [$topic['topic_id'], '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$topics[$key]['first_poster'] = new User($firstPost['poster_id']);
|
|
|
|
|
|
|
|
$topics[$key]['first_post'] = array_merge(
|
|
|
|
empty($firstPost) ? [] : $firstPost,
|
|
|
|
['elapsed' => Main::timeElapsed($firstPost['post_time'])]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get last post in topics
|
|
|
|
$lastPost = Database::fetch('posts', false, [
|
|
|
|
'topic_id' => [$topic['topic_id'], '='],
|
|
|
|
], ['post_id', true]);
|
|
|
|
|
|
|
|
$topics[$key]['last_poster'] = new User($lastPost['poster_id']);
|
|
|
|
|
|
|
|
$topics[$key]['last_post'] = array_merge(
|
|
|
|
empty($lastPost) ? [] : $lastPost,
|
|
|
|
['elapsed' => Main::timeElapsed($lastPost['post_time'])]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $topics;
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:43:46 +00:00
|
|
|
// Get posts of a thread
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getTopic($id, $ignoreView = false)
|
|
|
|
{
|
2015-06-28 14:43:46 +00:00
|
|
|
|
|
|
|
// Get the topic data from the database
|
|
|
|
$topicInfo = Database::fetch('topics', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_id' => [$id, '='],
|
2015-06-28 14:43:46 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Check if there actually is anything
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($topicInfo)) {
|
2015-06-28 14:43:46 +00:00
|
|
|
return false;
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-06-28 14:43:46 +00:00
|
|
|
|
2015-07-05 15:03:58 +00:00
|
|
|
// Up the view count
|
2015-09-14 20:51:23 +00:00
|
|
|
if (!$ignoreView) {
|
2015-07-05 15:03:58 +00:00
|
|
|
// Get the new count
|
|
|
|
$topicInfo['topic_views'] = $topicInfo['topic_views'] + 1;
|
|
|
|
|
|
|
|
// Update the count
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_views' => $topicInfo['topic_views'],
|
2015-07-05 15:03:58 +00:00
|
|
|
],
|
|
|
|
[
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_id' => [$id, '='],
|
|
|
|
],
|
2015-07-05 15:03:58 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:43:46 +00:00
|
|
|
// Get the posts from the database
|
|
|
|
$rawPosts = Database::fetch('posts', true, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_id' => [$id, '='],
|
2015-06-28 14:43:46 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Create storage array
|
|
|
|
$topic = [];
|
|
|
|
|
|
|
|
// Add forum data
|
|
|
|
$topic['forum'] = self::getForum($topicInfo['forum_id']);
|
|
|
|
|
|
|
|
// Store the topic info
|
|
|
|
$topic['topic'] = $topicInfo;
|
|
|
|
|
2015-07-05 00:03:15 +00:00
|
|
|
// Get first post in topics
|
|
|
|
$firstPost = Database::fetch('posts', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_id' => [$topic['topic']['topic_id'], '='],
|
2015-07-05 00:03:15 +00:00
|
|
|
]);
|
|
|
|
|
2015-09-16 20:34:36 +00:00
|
|
|
$topic['topic']['first_poster'] = new User($firstPost['poster_id']);
|
|
|
|
|
|
|
|
$topic['topic']['first_post'] = array_merge(
|
|
|
|
empty($firstPost) ? [] : $firstPost,
|
|
|
|
['elapsed' => Main::timeElapsed($firstPost['post_time'])]
|
|
|
|
);
|
2015-06-28 14:43:46 +00:00
|
|
|
|
2015-07-05 00:03:15 +00:00
|
|
|
// Get last post in topics
|
|
|
|
$lastPost = Database::fetch('posts', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'topic_id' => [$topic['topic']['topic_id'], '='],
|
2015-07-05 00:03:15 +00:00
|
|
|
], ['post_id', true]);
|
|
|
|
|
2015-09-16 20:34:36 +00:00
|
|
|
$topic['topic']['last_poster'] = new User($lastPost['poster_id']);
|
|
|
|
|
|
|
|
$topic['topic']['last_post'] = array_merge(
|
|
|
|
empty($lastPost) ? [] : $lastPost,
|
|
|
|
['elapsed' => Main::timeElapsed($lastPost['post_time'])]
|
|
|
|
);
|
2015-06-28 14:43:46 +00:00
|
|
|
|
|
|
|
// Create space for posts
|
|
|
|
$topic['posts'] = [];
|
|
|
|
|
|
|
|
// Parse the data of every post
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($rawPosts as $post) {
|
2015-06-28 14:43:46 +00:00
|
|
|
// Add post and metadata to the global storage array
|
|
|
|
$topic['posts'][$post['post_id']] = array_merge($post, [
|
2015-09-16 20:34:36 +00:00
|
|
|
'user' => (new User($post['poster_id'])),
|
|
|
|
'elapsed' => Main::timeElapsed($post['post_time']),
|
2015-09-14 20:51:23 +00:00
|
|
|
'is_op' => ($post['poster_id'] == $firstPost['poster_id'] ? '1' : '0'),
|
2015-10-10 21:17:50 +00:00
|
|
|
'parsed_post' => self::parseMarkUp($post['post_text'], $post['post_parse'], $post['post_emotes']),
|
2015-06-28 14:43:46 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Just in case
|
|
|
|
unset($_POSTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the compiled topic data
|
|
|
|
return $topic;
|
|
|
|
}
|
|
|
|
|
2015-10-18 16:48:05 +00:00
|
|
|
// Get a forum ID from a topic ID
|
|
|
|
public static function getForumIdFromTopicId($id)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Get the topic
|
|
|
|
$topic = Database::fetch('topics', false, [
|
|
|
|
'topic_id' => [$id, '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return false if nothing was returned
|
|
|
|
if (empty($topic)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the forum id
|
|
|
|
return $topic['forum_id'];
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:43:46 +00:00
|
|
|
// Get a topic ID from a post ID
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getTopicIdFromPostId($id)
|
|
|
|
{
|
2015-06-28 14:43:46 +00:00
|
|
|
|
|
|
|
// Get the post
|
|
|
|
$post = Database::fetch('posts', false, [
|
2015-09-14 20:51:23 +00:00
|
|
|
'post_id' => [$id, '='],
|
2015-06-28 14:43:46 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Return false if nothing was returned
|
2015-09-14 20:51:23 +00:00
|
|
|
if (empty($post)) {
|
2015-06-28 14:43:46 +00:00
|
|
|
return false;
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-06-28 14:43:46 +00:00
|
|
|
|
|
|
|
// Return the topic id
|
|
|
|
return $post['topic_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse different markup flavours
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function parseMarkUp($text, $mode, $emotes = 1)
|
|
|
|
{
|
2015-06-28 14:43:46 +00:00
|
|
|
|
2015-06-29 12:40:00 +00:00
|
|
|
// Clean string
|
|
|
|
$text = Main::cleanString($text);
|
|
|
|
|
2015-07-05 15:03:58 +00:00
|
|
|
// Parse emotes
|
2015-09-14 20:51:23 +00:00
|
|
|
if ($emotes) {
|
2015-07-05 15:03:58 +00:00
|
|
|
$text = Main::parseEmotes($text);
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2015-07-05 15:03:58 +00:00
|
|
|
|
2015-06-28 14:43:46 +00:00
|
|
|
// Switch between modes
|
2015-09-14 20:51:23 +00:00
|
|
|
switch ($mode) {
|
2015-06-28 14:43:46 +00:00
|
|
|
case 1:
|
|
|
|
case 2:
|
2015-11-16 22:05:45 +00:00
|
|
|
return Main::bbParse($text);
|
2015-06-29 12:40:00 +00:00
|
|
|
|
2015-06-28 14:43:46 +00:00
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 13:09:57 +00:00
|
|
|
// Get forum statistics of a user
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function getUserStats($uid)
|
|
|
|
{
|
2015-07-08 13:09:57 +00:00
|
|
|
|
|
|
|
// Collect the stats
|
|
|
|
return [
|
2015-09-14 21:41:43 +00:00
|
|
|
'posts' => Database::count(
|
|
|
|
'posts',
|
|
|
|
['poster_id' => [$uid, '=']]
|
|
|
|
)[0],
|
2015-09-14 21:52:17 +00:00
|
|
|
'topics' => count(Database::fetch(
|
2015-09-14 21:41:43 +00:00
|
|
|
'posts',
|
|
|
|
true,
|
|
|
|
['poster_id' => [$uid, '=']],
|
|
|
|
['post_time'],
|
|
|
|
null,
|
|
|
|
['topic_id']
|
2015-09-14 21:52:17 +00:00
|
|
|
)),
|
2015-07-08 13:09:57 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:31:42 +00:00
|
|
|
// Creating a new post
|
2015-10-18 16:48:05 +00:00
|
|
|
public static function createPost($poster, $title, $text, $forum, $topic = 0, $parse = 0, $signature = 0, $emotes = 0, $type = 0, $status = 0)
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-05-23 03:31:42 +00:00
|
|
|
|
2015-10-18 16:48:05 +00:00
|
|
|
// Check if we're replying to a thread
|
|
|
|
$getThread = Database::fetch('topics', false, ['topic_id' => [$topic, '=']]);
|
|
|
|
|
|
|
|
// If nothing was returned create a new thread
|
|
|
|
if (!$getThread) {
|
|
|
|
// Insert the required data
|
|
|
|
Database::insert('topics', [
|
|
|
|
'forum_id' => $forum,
|
|
|
|
'topic_title' => $title,
|
|
|
|
'topic_time' => time(),
|
|
|
|
'topic_status' => $status,
|
|
|
|
'topic_type' => $type,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Fetch the last insert
|
2015-10-19 21:25:20 +00:00
|
|
|
$getThread = Database::fetch('topics', false, [
|
|
|
|
'topic_id' => [Database::lastInsertID(), '='],
|
|
|
|
]);
|
2015-10-18 16:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the post
|
|
|
|
Database::insert('posts', [
|
|
|
|
'topic_id' => $getThread['topic_id'],
|
|
|
|
'forum_id' => $getThread['forum_id'],
|
|
|
|
'poster_id' => $poster,
|
|
|
|
'post_time' => time(),
|
|
|
|
'post_parse' => $parse,
|
|
|
|
'post_signature' => $signature,
|
|
|
|
'post_emotes' => $emotes,
|
|
|
|
'post_subject' => $title,
|
|
|
|
'post_text' => $text,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Fetch the last insert
|
2015-10-19 21:25:20 +00:00
|
|
|
$getPost = Database::fetch('posts', false, [
|
|
|
|
'post_id' => [Database::lastInsertID(), '='],
|
|
|
|
]);
|
2015-10-18 16:48:05 +00:00
|
|
|
|
2015-11-16 22:05:45 +00:00
|
|
|
// Update the topic with the last details
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
|
|
|
'topic_last_reply' => time(),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$getPost['topic_id'], '='],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2015-10-18 16:48:05 +00:00
|
|
|
// Return success
|
|
|
|
return [1, 'SUCCESS', $getPost['forum_id'], $getPost['topic_id'], $getPost['post_id']];
|
2015-05-23 03:31:42 +00:00
|
|
|
}
|
2015-05-03 16:25:57 +00:00
|
|
|
}
|