2015-11-13 23:11:55 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Post class
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
use Sakura\Utils;
|
2015-11-17 19:30:34 +00:00
|
|
|
use Sakura\Database;
|
|
|
|
use Sakura\User;
|
2015-12-02 14:38:40 +00:00
|
|
|
use Sakura\BBcode;
|
2015-12-13 23:29:53 +00:00
|
|
|
use Sakura\Config;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Post
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
class Post
|
|
|
|
{
|
|
|
|
// Variables
|
|
|
|
public $id = 0;
|
|
|
|
public $thread = 0;
|
|
|
|
public $forum = 0;
|
|
|
|
public $poster = [];
|
|
|
|
public $ip = "";
|
|
|
|
public $time = 0;
|
|
|
|
public $parse = 0;
|
|
|
|
public $signature = 0;
|
|
|
|
public $emotes = 0;
|
|
|
|
public $subject = "";
|
|
|
|
public $text = "";
|
2015-11-15 14:29:26 +00:00
|
|
|
public $parsed = "";
|
2015-11-13 23:11:55 +00:00
|
|
|
public $editTime = 0;
|
|
|
|
public $editReason = "";
|
|
|
|
public $editUser = [];
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public function __construct($postId)
|
|
|
|
{
|
|
|
|
// Attempt to get the database row
|
|
|
|
$postRow = Database::fetch('posts', false, ['post_id' => [$postId, '=']]);
|
|
|
|
|
|
|
|
// Assign data if a row was returned
|
|
|
|
if ($postRow) {
|
|
|
|
$this->id = $postRow['post_id'];
|
|
|
|
$this->thread = $postRow['topic_id'];
|
|
|
|
$this->forum = $postRow['forum_id'];
|
2015-12-29 01:27:49 +00:00
|
|
|
$this->poster = User::construct($postRow['poster_id']);
|
2015-11-13 23:11:55 +00:00
|
|
|
$this->ip = $postRow['poster_ip'];
|
|
|
|
$this->time = $postRow['post_time'];
|
|
|
|
$this->subject = $postRow['post_subject'];
|
|
|
|
$this->text = $postRow['post_text'];
|
|
|
|
$this->editTime = $postRow['post_edit_time'];
|
|
|
|
$this->editReason = $postRow['post_edit_reason'];
|
2015-12-29 01:27:49 +00:00
|
|
|
$this->editUser = User::construct($postRow['post_edit_user']);
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
|
|
|
|
// Parse the markup
|
2015-12-03 18:41:14 +00:00
|
|
|
$this->parsed = BBcode::toHTML(htmlentities($this->text));
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 20:49:40 +00:00
|
|
|
// Create a new post
|
|
|
|
public static function create($subject, $text, User $poster, $thread = 0, $forum = 0)
|
|
|
|
{
|
2015-12-13 23:29:53 +00:00
|
|
|
// Check if the data meets the requirements
|
|
|
|
if (strlen($subject) < Config::get('forum_title_min')
|
|
|
|
|| strlen($subject) > Config::get('forum_title_max')
|
|
|
|
|| strlen($text) < Config::get('forum_text_min')
|
|
|
|
|| strlen($text) > Config::get('forum_text_max')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-12-11 20:49:40 +00:00
|
|
|
// If no thread is specified create a new one
|
|
|
|
if ($thread) {
|
|
|
|
$thread = new Thread($thread);
|
|
|
|
} else {
|
|
|
|
$thread = Thread::create($forum, $subject);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop if the thread ID is 0
|
|
|
|
if ($thread->id == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the post
|
|
|
|
Database::insert('posts', [
|
|
|
|
'topic_id' => $thread->id,
|
|
|
|
'forum_id' => $thread->forum,
|
2016-01-17 01:58:31 +00:00
|
|
|
'poster_id' => $poster->id,
|
|
|
|
'poster_ip' => Utils::getRemoteIP(),
|
2015-12-11 20:49:40 +00:00
|
|
|
'post_time' => time(),
|
|
|
|
'post_subject' => $subject,
|
|
|
|
'post_text' => $text,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Get post id
|
|
|
|
$id = Database::lastInsertID();
|
|
|
|
|
|
|
|
// Update the last post date
|
|
|
|
$thread->lastUpdate();
|
|
|
|
|
|
|
|
// Return the object
|
|
|
|
return new Post($id);
|
|
|
|
}
|
|
|
|
|
2016-01-09 21:57:54 +00:00
|
|
|
// Update a post
|
2016-01-10 18:24:47 +00:00
|
|
|
public function update()
|
|
|
|
{
|
2016-01-09 21:57:54 +00:00
|
|
|
// Check if the data meets the requirements
|
|
|
|
if (strlen($this->subject) < Config::get('forum_title_min')
|
|
|
|
|| strlen($this->subject) > Config::get('forum_title_max')
|
|
|
|
|| strlen($this->text) < Config::get('forum_text_min')
|
|
|
|
|| strlen($this->text) > Config::get('forum_text_max')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a thread object
|
|
|
|
$thread = new Thread($this->thread);
|
|
|
|
|
|
|
|
// Update the post
|
|
|
|
Database::update('posts', [
|
|
|
|
[
|
|
|
|
'topic_id' => $thread->id,
|
|
|
|
'forum_id' => $thread->forum,
|
2016-01-17 01:58:31 +00:00
|
|
|
'poster_id' => $this->poster->id,
|
|
|
|
'poster_ip' => Utils::getRemoteIP(),
|
2016-01-09 21:57:54 +00:00
|
|
|
'post_time' => $this->time,
|
|
|
|
'post_subject' => $this->subject,
|
|
|
|
'post_text' => $this->text,
|
|
|
|
'post_edit_time' => $this->editTime,
|
|
|
|
'post_edit_reason' => $this->editReason,
|
2016-01-17 01:58:31 +00:00
|
|
|
'post_edit_user' => $this->editUser->id,
|
2016-01-09 21:57:54 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'post_id' => [$this->id, '='],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return a new post object
|
|
|
|
return new Post($this->id);
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:29:26 +00:00
|
|
|
// Time elapsed since creation
|
|
|
|
public function timeElapsed()
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return Utils::timeElapsed($this->time);
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Time elapsed since last edit
|
|
|
|
public function editTimeElapsed()
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return Utils::timeElapsed($this->editTime);
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
}
|