This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Forum/Post.php

236 lines
6.2 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the post object class.
*
* @package Sakura
*/
namespace Sakura\Forum;
2016-01-17 01:58:31 +00:00
use Sakura\Utils;
use Sakura\Database;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
use Sakura\User;
use Sakura\BBcode;
use Sakura\Config;
use Sakura\Net;
/**
2016-02-02 21:04:15 +00:00
* Used to serve, create and update posts.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Post
{
2016-02-02 21:04:15 +00:00
/**
* The ID of the post.
*
* @var int
*/
public $id = 0;
2016-02-02 21:04:15 +00:00
/**
* The id of the thread this post is a part of.
*
* @var int
*/
public $thread = 0;
2016-02-02 21:04:15 +00:00
/**
* The id of the forum this post is a part of.
*
* @var int
*/
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
*/
public $ip = "";
2016-02-02 21:04:15 +00:00
/**
* The UNIX timestamp from when this post was created.
*
* @var int
*/
public $time = 0;
2016-02-02 21:04:15 +00:00
/**
* The subject of this post.
*
* @var string
*/
public $subject = "";
2016-02-02 21:04:15 +00:00
/**
* The raw contents of this post.
*
* @var string
*/
public $text = "";
2016-02-02 21:04:15 +00:00
/**
* The parsed contents of this post.
*
* @var string
*/
public $parsed = "";
2016-02-02 21:04:15 +00:00
/**
* The UNIX timestamp of the last time this post was edited.
*
* @var int
*/
public $editTime = 0;
2016-02-02 21:04:15 +00:00
/**
* The reason why this post was edited.
*
* @var string
*/
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;
/**
* Constructor.
*
* @param int $postId ID of the post that should be constructed.
*/
public function __construct($postId)
{
// Attempt to get the database row
2016-02-18 23:28:44 +00:00
$postRow = DB::prepare('SELECT * FROM `{prefix}posts` WHERE `post_id` = :id');
$postRow->execute([
'id' => $postId,
]);
$postRow = $postRow->fetch();
// Assign data if a row was returned
if ($postRow) {
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->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;
$this->editUser = User::construct($postRow->post_edit_user);
}
// Parse the markup
$this->parsed = BBcode::toHTML(htmlentities($this->text));
}
2016-02-02 21:04:15 +00:00
/**
* Creating a new post.
*
* @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-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
*/
public static function create($subject, $text, User $poster, $thread = 0, $forum = 0)
{
// 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;
}
// 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-18 23:28:44 +00:00
DB::prepare('INSERT INTO `{prefix}posts` (`topic_id`, `forum_id`, `poster_id`, `poster_ip`, `post_time`, `post_subject`, `post_text`) VALUES (:thread, :forum, :user, :ip, :time, :subject, :text)')
->execute([
'thread' => $thread->id,
'forum' => $thread->forum,
'user' => $poster->id,
'ip' => Net::IP(),
'time' => time(),
'subject' => $subject,
'text' => $text,
]);
// Get post id
2016-02-18 23:28:44 +00:00
$id = (int) DB::lastID();
// 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.
*
* @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
// 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
2016-02-18 23:28:44 +00:00
DB::prepare('UPDATE `{prefix}posts` SET `topic_id` = :thread, `forum_id` = :forum, `poster_id` = :user, `poster_ip` = :ip, `post_time` = :time, `post_subject` = :subject, `post_text` = :text, `post_edit_time` = :edit_time, `post_edit_reason` = :edit_reason, `post_edit_user` = :edit_user WHERE `post_id` = :post')
->execute([
'post' => $this->id,
'thread' => $thread->id,
'forum' => $thread->forum,
'user' => $this->poster->id,
'ip' => Net::pton(Net::IP()),
'time' => $this->time,
'subject' => $this->subject,
'text' => $this->text,
'edit_time' => $this->editTime,
'edit_reason' => $this->editReason,
'edit_user' => $this->editUser->id,
2016-01-09 21:57:54 +00:00
]);
// Return a new post object
return new Post($this->id);
}
}