2015-11-13 23:11:55 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the post object class.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
2016-08-07 14:47:30 +00:00
|
|
|
use Sakura\BBCode\Parser as BBParser;
|
2016-03-20 16:37:59 +00:00
|
|
|
use Sakura\DB;
|
2016-12-04 20:00:58 +00:00
|
|
|
use Sakura\Exceptions\NetAddressTypeException;
|
2017-03-22 20:26:57 +00:00
|
|
|
use Sakura\Exceptions\NetInvalidAddressException;
|
2016-02-07 19:36:13 +00:00
|
|
|
use Sakura\Net;
|
2016-03-20 16:37:59 +00:00
|
|
|
use Sakura\User;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used to serve, create and update posts.
|
2015-11-13 23:11:55 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-11-13 23:11:55 +00:00
|
|
|
*/
|
|
|
|
class Post
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The ID of the post.
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-07-30 13:48:09 +00:00
|
|
|
* The id of the topic this post is a part of.
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-07-30 13:48:09 +00:00
|
|
|
public $topic = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the forum this post is a part of.
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $forum = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The User object of the poster.
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
public $poster = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The IP address from which this post was created.
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $ip = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp from when this post was created.
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $time = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The subject of this post.
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $subject = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The raw contents of this post.
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $text = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parsed contents of this post.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-10-07 16:06:07 +00:00
|
|
|
public $parsed = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of the last time this post was edited.
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $editTime = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The reason why this post was edited.
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $editReason = "";
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The User object of the user that last edited this post.
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
public $editUser = null;
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
/**
|
|
|
|
* Post deleted?
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $deleted = false;
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param int $postId
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function __construct(int $postId)
|
2015-11-13 23:11:55 +00:00
|
|
|
{
|
|
|
|
// Attempt to get the database row
|
2016-02-25 16:06:29 +00:00
|
|
|
$postRow = DB::table('posts')
|
|
|
|
->where('post_id', $postId)
|
2016-08-06 14:09:01 +00:00
|
|
|
->first();
|
2016-03-20 16:37:59 +00:00
|
|
|
|
2015-11-13 23:11:55 +00:00
|
|
|
// Assign data if a row was returned
|
|
|
|
if ($postRow) {
|
2016-07-31 01:32:37 +00:00
|
|
|
$this->id = intval($postRow->post_id);
|
|
|
|
$this->topic = intval($postRow->topic_id);
|
|
|
|
$this->forum = intval($postRow->forum_id);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->poster = User::construct($postRow->poster_id);
|
2016-07-31 01:32:37 +00:00
|
|
|
$this->time = intval($postRow->post_time);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->subject = $postRow->post_subject;
|
|
|
|
$this->text = $postRow->post_text;
|
2016-10-07 16:06:07 +00:00
|
|
|
$this->parsed = $postRow->post_text_parsed;
|
2016-07-31 01:32:37 +00:00
|
|
|
$this->editTime = intval($postRow->post_edit_time);
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->editReason = $postRow->post_edit_reason;
|
|
|
|
$this->editUser = User::construct($postRow->post_edit_user);
|
2016-09-13 22:05:03 +00:00
|
|
|
$this->deleted = boolval($postRow->post_deleted);
|
2016-04-25 02:01:14 +00:00
|
|
|
|
|
|
|
// Temporary backwards compatible IP storage system
|
|
|
|
try {
|
|
|
|
$this->ip = Net::ntop($postRow->poster_ip);
|
2016-12-04 20:00:58 +00:00
|
|
|
} catch (NetAddressTypeException $e) {
|
2016-04-25 02:01:14 +00:00
|
|
|
$this->ip = $postRow->poster_ip;
|
|
|
|
$this->update();
|
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2017-03-24 17:10:44 +00:00
|
|
|
$post_parsed = strlen($this->parsed) > 0;
|
|
|
|
$cache_enabled = config('performance.forum_cache');
|
|
|
|
|
|
|
|
if (!$cache_enabled || !$post_parsed) {
|
2016-10-07 19:21:05 +00:00
|
|
|
$this->parsed = BBParser::toHTML(htmlentities($this->text), $this->poster);
|
2017-03-24 17:10:44 +00:00
|
|
|
|
|
|
|
if ($cache_enabled) {
|
|
|
|
$this->update();
|
|
|
|
}
|
2016-10-07 16:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Creating a new post.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @param string $subject
|
|
|
|
* @param string $text
|
|
|
|
* @param User $poster
|
|
|
|
* @param int $topic
|
|
|
|
* @param int $forum
|
|
|
|
* @return Post
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public static function create(string $subject, string $text, User $poster, int $topic = 0, int $forum = 0): Post
|
2015-12-11 20:49:40 +00:00
|
|
|
{
|
2016-07-30 13:48:09 +00:00
|
|
|
// If no topic is specified create a new one
|
|
|
|
if ($topic) {
|
|
|
|
$topic = new Topic($topic);
|
2015-12-11 20:49:40 +00:00
|
|
|
} else {
|
2016-07-30 13:48:09 +00:00
|
|
|
$topic = Topic::create($forum, $subject);
|
2015-12-11 20:49:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:48:09 +00:00
|
|
|
// Stop if the topic ID is 0
|
|
|
|
if ($topic->id == 0) {
|
2015-12-11 20:49:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the post
|
2016-02-25 16:06:29 +00:00
|
|
|
$id = DB::table('posts')
|
|
|
|
->insertGetId([
|
2016-07-30 13:48:09 +00:00
|
|
|
'topic_id' => $topic->id,
|
|
|
|
'forum_id' => $topic->forum,
|
2016-02-25 16:06:29 +00:00
|
|
|
'poster_id' => $poster->id,
|
2016-04-25 02:01:14 +00:00
|
|
|
'poster_ip' => Net::pton(Net::ip()),
|
2016-02-25 16:06:29 +00:00
|
|
|
'post_time' => time(),
|
|
|
|
'post_subject' => $subject,
|
|
|
|
'post_text' => $text,
|
2016-10-13 18:45:44 +00:00
|
|
|
'post_text_parsed' => BBParser::toHTML(htmlentities($text), $poster),
|
2016-02-25 16:06:29 +00:00
|
|
|
]);
|
2015-12-11 20:49:40 +00:00
|
|
|
|
|
|
|
// Update the last post date
|
2016-07-30 13:48:09 +00:00
|
|
|
$topic->lastUpdate();
|
2015-12-11 20:49:40 +00:00
|
|
|
|
|
|
|
// Return the object
|
|
|
|
return new Post($id);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Commit the changes to the Database.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return Post
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2016-12-04 20:00:58 +00:00
|
|
|
public function update(): Post
|
2016-01-10 18:24:47 +00:00
|
|
|
{
|
2016-07-30 13:48:09 +00:00
|
|
|
// Create a topic object
|
|
|
|
$topic = new Topic($this->topic);
|
2017-03-22 20:25:36 +00:00
|
|
|
$ip = Net::pton("::1");
|
2016-01-09 21:57:54 +00:00
|
|
|
|
2017-03-22 19:56:43 +00:00
|
|
|
try {
|
|
|
|
$ip = Net::pton($this->ip);
|
|
|
|
} catch (NetAddressTypeException $e) {
|
2017-03-22 20:25:36 +00:00
|
|
|
} catch (NetInvalidAddressException $e) {
|
2017-03-22 19:56:43 +00:00
|
|
|
}
|
|
|
|
|
2016-01-09 21:57:54 +00:00
|
|
|
// Update the post
|
2016-02-25 16:06:29 +00:00
|
|
|
DB::table('posts')
|
|
|
|
->where('post_id', $this->id)
|
|
|
|
->update([
|
2016-07-30 13:48:09 +00:00
|
|
|
'topic_id' => $topic->id,
|
|
|
|
'forum_id' => $topic->forum,
|
2016-02-25 16:06:29 +00:00
|
|
|
'poster_id' => $this->poster->id,
|
2017-03-22 19:56:43 +00:00
|
|
|
'poster_ip' => $ip,
|
2016-02-25 16:06:29 +00:00
|
|
|
'post_time' => $this->time,
|
|
|
|
'post_subject' => $this->subject,
|
|
|
|
'post_text' => $this->text,
|
2016-10-07 19:21:05 +00:00
|
|
|
'post_text_parsed' => BBParser::toHTML(htmlentities($this->text), $this->poster),
|
2016-02-25 16:06:29 +00:00
|
|
|
'post_edit_time' => $this->editTime,
|
|
|
|
'post_edit_reason' => $this->editReason,
|
|
|
|
'post_edit_user' => $this->editUser->id,
|
|
|
|
]);
|
2016-01-09 21:57:54 +00:00
|
|
|
|
|
|
|
// Return a new post object
|
|
|
|
return new Post($this->id);
|
|
|
|
}
|
2016-03-20 16:37:59 +00:00
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
/**
|
|
|
|
* Undo deletion.
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function restore(): void
|
2016-09-13 22:05:03 +00:00
|
|
|
{
|
|
|
|
DB::table('posts')
|
|
|
|
->where('post_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'post_deleted' => 0,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
2016-12-04 16:33:52 +00:00
|
|
|
* delet this.
|
2016-08-05 02:35:37 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function delete(): void
|
2016-09-13 22:05:03 +00:00
|
|
|
{
|
|
|
|
DB::table('posts')
|
|
|
|
->where('post_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'post_deleted' => 1,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-04 16:33:52 +00:00
|
|
|
* DELET THIS.
|
2016-09-13 22:05:03 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function purge(): void
|
2016-03-20 16:37:59 +00:00
|
|
|
{
|
|
|
|
DB::table('posts')
|
|
|
|
->where('post_id', $this->id)
|
|
|
|
->delete();
|
|
|
|
}
|
2016-03-24 18:28:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a user has read this post before.
|
2016-12-04 16:33:52 +00:00
|
|
|
* @param int $user
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return bool
|
2016-03-24 18:28:32 +00:00
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function unread(int $user): bool
|
2016-03-24 18:28:32 +00:00
|
|
|
{
|
2016-12-04 16:33:52 +00:00
|
|
|
// Only check if user id is positive
|
|
|
|
if ($user >= 1) {
|
|
|
|
// Get track row from the database
|
|
|
|
$track = DB::table('topics_track')
|
|
|
|
->where('user_id', $user)
|
|
|
|
->where('topic_id', $this->topic)
|
|
|
|
->where('mark_time', '>', $this->time)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
return !$track;
|
2016-03-24 18:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Else just return false meaning everything is read
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|