2015-11-13 23:11:55 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the post object class.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
2015-12-02 14:38:40 +00:00
|
|
|
use Sakura\BBcode;
|
2016-03-20 16:37:59 +00:00
|
|
|
use Sakura\DB;
|
2016-04-25 02:01:14 +00:00
|
|
|
use Sakura\Exception;
|
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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the thread this post is a part of.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $thread = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the forum this post is a part of.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
public $poster = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The IP address from which this post was created.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-15 14:29:26 +00:00
|
|
|
public $parsed = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of the last time this post was edited.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
public $editUser = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $postId ID of the post that should be constructed.
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public function __construct($postId)
|
|
|
|
{
|
|
|
|
// Attempt to get the database row
|
2016-02-25 16:06:29 +00:00
|
|
|
$postRow = DB::table('posts')
|
|
|
|
->where('post_id', $postId)
|
|
|
|
->get();
|
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-02-25 16:06:29 +00:00
|
|
|
$postRow = $postRow[0];
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->id = $postRow->post_id;
|
|
|
|
$this->thread = $postRow->topic_id;
|
|
|
|
$this->forum = $postRow->forum_id;
|
|
|
|
$this->poster = User::construct($postRow->poster_id);
|
|
|
|
$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;
|
|
|
|
$this->editUser = User::construct($postRow->post_edit_user);
|
2016-04-25 02:01:14 +00:00
|
|
|
|
|
|
|
// Temporary backwards compatible IP storage system
|
|
|
|
try {
|
|
|
|
$this->ip = Net::ntop($postRow->poster_ip);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->ip = $postRow->poster_ip;
|
|
|
|
$this->update();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Creating a new post.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $subject The subject of the thread.
|
|
|
|
* @param string $text The raw contents of the post.
|
|
|
|
* @param User $poster The User object of the poster.
|
|
|
|
* @param int $thread The ID of the thread this post is a reply to.
|
|
|
|
* @param mixed $forum The forum this post is a reply in.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-18 23:28:44 +00:00
|
|
|
* @return null|self Either null, indicating a failure, or the Post object.
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
2015-12-11 20:49:40 +00:00
|
|
|
public static function create($subject, $text, User $poster, $thread = 0, $forum = 0)
|
|
|
|
{
|
|
|
|
// 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
|
2016-02-25 16:06:29 +00:00
|
|
|
$id = DB::table('posts')
|
|
|
|
->insertGetId([
|
|
|
|
'topic_id' => $thread->id,
|
|
|
|
'forum_id' => $thread->forum,
|
|
|
|
'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,
|
|
|
|
]);
|
2015-12-11 20:49:40 +00:00
|
|
|
|
|
|
|
// Update the last post date
|
|
|
|
$thread->lastUpdate();
|
|
|
|
|
|
|
|
// Return the object
|
|
|
|
return new Post($id);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Commit the changes to the Database.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return null|Post Either null, indicating a failure, or the Post object.
|
|
|
|
*/
|
2016-01-10 18:24:47 +00:00
|
|
|
public function update()
|
|
|
|
{
|
2016-01-09 21:57:54 +00:00
|
|
|
// Create a thread object
|
|
|
|
$thread = new Thread($this->thread);
|
|
|
|
|
|
|
|
// Update the post
|
2016-02-25 16:06:29 +00:00
|
|
|
DB::table('posts')
|
|
|
|
->where('post_id', $this->id)
|
|
|
|
->update([
|
|
|
|
'topic_id' => $thread->id,
|
|
|
|
'forum_id' => $thread->forum,
|
|
|
|
'poster_id' => $this->poster->id,
|
2016-03-27 21:18:57 +00:00
|
|
|
'poster_ip' => Net::pton(Net::ip()),
|
2016-02-25 16:06:29 +00:00
|
|
|
'post_time' => $this->time,
|
|
|
|
'post_subject' => $this->subject,
|
|
|
|
'post_text' => $this->text,
|
|
|
|
'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
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param mixed $user The id of the user in question.
|
|
|
|
*
|
|
|
|
* @return bool A boolean indicating the read status.
|
|
|
|
*/
|
|
|
|
public function unread($user)
|
|
|
|
{
|
2016-04-25 02:01:14 +00:00
|
|
|
// Return false if the user id is less than 1
|
|
|
|
if ($user < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 18:28:32 +00:00
|
|
|
// Attempt to get track row from the database
|
|
|
|
$track = DB::table('topics_track')
|
|
|
|
->where('user_id', $user)
|
|
|
|
->where('topic_id', $this->thread)
|
|
|
|
->where('mark_time', '>', $this->time)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
// If nothing was returned it's obvious that the status is unread
|
|
|
|
if (!$track) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else just return false meaning everything is read
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|