Addition of files

This commit is contained in:
Edi
2022-03-15 16:34:48 +01:00
parent 9688e6c8ca
commit a48d911d1c
52 changed files with 10720 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/abstract.comments-theme.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
abstract class CommentsTheme{
/*
| REQUIRED :: FORM
| @note This method renders the comment form used on the frontend.
|
| @param multi The previously passed username (on errors only)
| An `array(username, hash, nickname)` array if the user is logged in.
| @param string The previously passed email address (on errors only)!
| @param string The previously passed comment title (on errors only)!
| @param string The previously passed comment message (on errors only)!
*/
abstract public function form($username = "", $email = "", $title = "", $message = "");
/*
| REQUIRED :: COMMENT
| @note This method renders the single shown comments on the frontend.
|
| @param object The comment instance.
| @param string The unique comment UID.
*/
abstract public function comment($comment, $uid, $depth);
/*
| REQUIRED :: PAGINATION
| @note This method renders the pagination for the comment section.
|
| @param string The called location: "top" or "bottom".
| @param int The current comment page.<, startin with 1.
| @param int The number of comments to be shown per page.
| @param int The total number of comments for the content page.
*/
abstract public function pagination($loction, $cpage, $limit, $count);
}

494
system/class.comment.php Normal file
View File

@@ -0,0 +1,494 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/class.comment.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
class Comment{
/*
| CONSTRUCTOR
| @since 0.1.0
|
| @param multi The unique comment id or FALSE.
| @param multi The unique page comment ID.
*/
public function __construct($uid, $uuid){
$this->vars["uid"] = $uid;
if($uid === false){
$row = array(
"type" => "comment",
"depth" => 1,
"title" => "",
"comment" => "",
"rating" => [0, 0],
"page_uuid" => "",
"parent_uid" => "",
"author" => "",
"subscribe" => false,
"date" => "",
"dateModified" => "",
"dateAudit" => "",
"custom" => array()
);
} else {
$comments = new Comments($uuid);
if(Text::isEmpty($uid) || !$comments->exists($uid)){
// @todo Throw Error
}
$row = $comments->getCommentDB($uid);
}
// Set Class
foreach($row AS $field => $value){
if(strpos($field, "date") === 0){
$this->vars["{$field}Raw"] = $value;
} else {
$this->vars[$field] = $value;
}
}
}
/*
| PUBLIC :: GET VALUE
| @since 0.1.0
|
| @param string The unique field key.
| @param multi The default value, which should return if the field key doesnt exist.
|
| @multi multi The respective field value on success, $default otherwise.
*/
public function getValue($field, $default = false){
if(isset($this->vars[$field])){
return $this->vars[$field];
}
return $default;
}
/*
| PUBLIC :: SET FIELD
| @since 0.1.0
|
| @param string The unique field key.
| @param multi The respective field value, which you want to set.
|
| @return bool TRUE
*/
public function setField($field, $value = NULL){
if(is_array($field)){
foreach($field AS $k => $v){
$this->setField($k, $v);
}
return true;
}
$this->vars[$field] = $value;
return true;
}
/*
| FIELD :: COMMENT RAW
| @since 0.1.0
|
| @return string The (sanitized) raw content on success, FALSE on failure.
*/
public function commentRaw(){
return $this->getValue("comment");
}
/*
| FIELD :: COMMENT
| @since 0.1.0
|
| @return string The (sanitized) content on success, FALSE on failure.
*/
public function comment(){
$content = $this->getValue("comment");
if(sn_config("comment_markup_html")){
$content = Sanitize::htmlDecode($content);
}
if(sn_config("comment_markup_markdown")){
$parsedown = new Parsedown();
$content = $parsedown->text($content);
}
return $content;
}
/*
| FIELD :: GET UID
| @since 0.1.0
*/
public function uid(){
return $this->getValue("uid");
}
public function key(){
return $this->getValue("uid");
}
/*
| FIELD :: GET (COMMENT FILE) PATH
| @since 0.1.0
*/
public function path(){
return PATH_PAGES . $this->getValue("page_key") . DS . "comments";
}
/*
| FIELD :: GET (COMMENT FILE) PATH / FILE
| @since 0.1.0
*/
public function file(){
return $this->path() . DS . "c_" . $this->getValue("uid") . ".php";
}
/*
| FIELD :: GET TYPE
| @since 0.1.0
*/
public function type(){
return $this->getValue("type");
}
public function isComment(){
return $this->getValue("type") === "comment";
}
public function isReply(){
return $this->getValue("type") === "reply";
}
public function isPingback(){
return $this->getValue("type") === "pingback";
}
/*
| FIELD :: GET DEPTH
| @since 0.1.0
*/
public function depth(){
return (int) $this->getValue("depth");
}
/*
| FIELD :: TITLE
| @since 0.1.0
|
| @param bool TRUE to sanitize the content, FALSE to return it plain.
|
| @return string The respective comment title as STRING.
*/
public function title($sanitize = true){
if($sanitize){
return Sanitize::html($this->getValue("title"));
}
return $this->getValue("title");
}
/*
| FIELD :: GET STATUS
| @since 0.1.0
*/
public function status(){
return $this->getValue("status");
}
public function isPending(){
return $this->getValue("status") === "pending";
}
public function isPublic(){
return $this->getValue("status") === "approved";
}
public function isApproved(){
return $this->getValue("status") === "approved";
}
public function isRejected(){
return $this->getValue("status") === "rejected";
}
public function isSpam(){
return $this->getValue("status") === "spam";
}
/*
| FIELD :: GET RATING
| @since 0.1.0
*/
public function rating(){
return $this->getValue("rating");
}
/*
| FIELD :: GET LIKE
| @since 0.1.0
*/
public function like(){
$rating = $this->getValue("rating");
if(is_array($rating) && count($rating) >= 1){
return $rating[0];
}
return 0;
}
/*
| FIELD :: GET DISLIKE
| @since 0.1.0
*/
public function dislike(){
$rating = $this->getValue("rating");
if(is_array($rating) && count($rating) >= 2){
return $rating[1];
}
return 0;
}
/*
| FIELD :: GET PAGE KEY
| @since 0.1.0
*/
public function page_key(){
return $this->getValue("page_key");
}
/*
| FIELD :: GET PAGE UUID
| @since 0.1.0
*/
public function page_uuid(){
return $this->getValue("page_uuid");
}
/*
| FIELD :: GET PARENT UID
| @since 0.1.0
*/
public function parent_uid(){
return $this->getValue("parent_uid");
}
/*
| FIELD :: GET PARENT
| @since 0.1.0
*/
public function parent(){
global $comments;
if($comments->exists($this->getValue("parent_uid"))){
return new Comment($this->getValue("parent_uid"));
}
return false;
}
/*
| FIELD :: GET CHILDREN
| @since 0.1.0
|
| @param multi The single comment status which should return, multiple as ARRAY.
| Use `null` to return each children comment.
| @param string The return type, which allows the following strings:
| "uids" Return just the respective UID / keys
| "keys" Return just the respective UID / keys
| "objects" Return single Comment instances
| "arrays" Return the unformatted DB arrays
|
| @return multi FALSE on error, the respective array on succes.
*/
public function children($status = "approved", $return = "objects"){
global $comments;
// Check Parameter
if(is_string($status)){
$status = array($status);
}
if(!is_array($status) && $status !== null){
return false;
}
// Get Children
$return = array();
foreach($this->getDB(false) AS $uid => $value){
if($value["parent"] !== $this->getValue("uid")){
continue;
}
if(is_array($status) && !in_array($value["status"], $status)){
continue;
}
if($return === "uids" || $return == "keys"){
$return[] = $uid;
} else if($return === "objects"){
$return[$uid] = new Comment($uid);
} else {
$return[$uid] = $value;
}
}
return $return;
}
/*
| FIELD :: GET UUID
| @since 0.1.0
*/
public function uuid(){
return $this->getValue("uuid");
}
/*
| FIELD :: GET USERNAME
| @since 0.1.0
*/
public function username(){
global $L, $users, $SnickerUsers;
list($type, $id) = array_pad(explode("::", $this->getValue("author"), 2), 2, null);
switch($type){
case "bludit":
if(!$users->exists($id)){
break;
}
$user = new User($id);
return $user->nickname();
case "guest":
if(!$SnickerUsers->exists($id)){
break;
}
return $SnickerUsers->db[$id]["username"];
case "unknown":
return $L->g("Unknown User");
}
return false;
}
/*
| FIELD :: GET EMAIL
| @since 0.1.0
*/
public function email(){
global $L, $users, $SnickerUsers;
list($type, $id) = array_pad(explode("::", $this->getValue("author"), 2), 2, null);
switch($type){
case "bludit":
if(!$users->exists($id)){
break;
}
$user = new User($id);
return $user->email();
case "guest":
if(!$SnickerUsers->exists($id)){
break;
}
return $SnickerUsers->db[$id]["email"];
case "unknown":
return "unknown@" . $_SERVER["SERVER_NAME"];
}
return false;
}
/*
| FIELD :: SUBSCRIBE
| @since 0.1.0
*/
public function subscribe(){
return $this->getValue("subscribe");
}
public function hasSubscribed(){
return $this->getValue("subscribe") === true;
}
/*
| FIELD :: GET AVATAR
| @since 0.1.0
*/
public function avatar($size = "64"){
$user = $this->username();
$email = md5(strtolower(trim($this->email())));
$avatar = $this->avatar_url($size);
// Force Profile Picture
$force = false;
if(sn_config("frontend_avatar_users")){
$force = (strpos($avatar, DOMAIN_UPLOADS_PROFILES) !== false);
}
// Return IMG Tag
if(sn_config("frontend_avatar") === "identicon" && !$force){
return '<img src="'.$avatar.'" width="'.$size.'px" height="'.$size.'px" data-identicon="'.$email.'" alt="'.$user.'" />';
}
return '<img src="'.$avatar.'" width="'.$size.'px" height="'.$size.'px" alt="'.$user.'" />';
}
/*
| FIELD :: GET AVATAR URL
| @since 0.1.0
*/
public function avatar_url($size = "64"){
global $users;
// Return Profile Picture
if(sn_config("frontend_avatar_users") && strpos($this->getValue("author"), "bludit") === 0){
$username = substr($this->getValue("author"), strlen("bludit::"));
if($users->exists($username)){
$user = new User($username);
if(($avatar = $user->profilePicture()) !== false){
return $avatar;
}
}
}
// Return Gravatar
if(sn_config("frontend_avatar") === "gravatar"){
$hash = md5(strtolower(trim($this->email())));
return "https://www.gravatar.com/avatar/{$hash}?s={$size}&d=" . sn_config("frontend_gravatar");
}
// Return Identicon
if(sn_config("frontend_avatar") === "identicon"){
$hash = md5(strtolower(trim($this->email())));
$ident = new Identicon\Identicon();
return $ident->getImageDataUri($hash, $size);
}
// Return Mystery Man
return SNICKER_DOMAIN . "includes/img/default-avatar.jpg";
}
/*
| FIELD :: GET / FORMAT DATE
| @since 0.1.0
|
| @param string The respective format, which should be used for the output.
|
| @return string The formatted Date Output.
*/
public function date($format = false, $type = "date"){
global $site;
$date = $this->getValue("{$type}Raw");
return Date::format($date, DB_DATE_FORMAT, ($format? $format: $site->dateFormat()));
}
public function dateModified($format = false){
return $this->date($format, "dateModified");
}
public function dateAudit($format = false){
return $this->date($format, "dateAudit");
}
/*
| FIELD :: GET CUSTOM
| @since 0.1.0
|
| @param string The respective custom key, to get the value.
| Use `null` to get all custom values.
|
| @return multi The custom value, all customs as ARRAY or FALSE on failure.
*/
public function custom($key = NULL){
$custom = $this->getValue("custom");
if($key !== null){
if(array_key_exists($key, $custom)){
return $custom[$key];
}
return false;
}
return $custom;
}
}

View File

@@ -0,0 +1,476 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/class.comments-index.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
class CommentsIndex extends dbJSON{
/*
| DATABASE FIELDS
*/
protected $dbFields = array(
"title" => "", // Comment Title
"excerpt" => "", // Comment Excerpt (142)
"status" => "", // Comment Status
"page_uuid" => "", // Comment Page UUID
"parent_uid" => "", // Comment Parent UID
"author" => "", // Comment Author (bludt::username or guest::uuid)
"date" => "" // Comment Date
);
/*
| CONSTRUCTOR
| @since 0.1.0
*/
public function __construct(){
parent::__construct(DB_SNICKER_INDEX);
if(!file_exists(DB_SNICKER_INDEX)){
$this->db = array();
$this->save();
}
}
/*
| OVERWRITE :: EXISTS
| @since 0.1.0
*/
public function exists($uid){
return array_key_exists($uid, $this->db);
}
/*
| HELPER :: LIMIT LIST
| @since 0.1.0
*/
private function limitList($list, $page, $limit){
if($limit == -1){
return $list;
}
$offset = $limit * (max($page, 1) - 1);
$count = min(($offset + $limit - 1), count($list));
if($offset < 0 || $offset > $count){
return false;
}
return array_slice($list, $offset, $limit, true);
}
/*
| DATA :: GET PENDING INDEX
| @since 0.1.0
|
| @param bool TRUE to just return the keys, FALSE to return the complete array.
|
| @return array All pending comments with basic comment data as ARRAY.
*/
public function getPending($keys = false){
$db = array();
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if($value["status"] === "pending"){
$db[$key] = $value;
}
}
if($keys){
return array_keys($db);
}
return $db;
}
/*
| DATA :: GET APPROVED INDEX
| @since 0.1.0
|
| @param bool TRUE to just return the keys, FALSE to return the complete array.
|
| @return array All approved comments with basic comment data as ARRAY.
*/
public function getApproved($keys = false){
$db = array();
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if($value["status"] === "approved"){
$db[$key] = $value;
}
}
if($keys){
return array_keys($db);
}
return $db;
}
/*
| DATA :: GET REJECTED INDEX
| @since 0.1.0
|
| @param bool TRUE to just return the keys, FALSE to return the complete array.
|
| @return array All rejected comments with basic comment data as ARRAY.
*/
public function getRejected($keys = false){
$db = array();
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if($value["status"] === "rejected"){
$db[$key] = $value;
}
}
if($keys){
return array_keys($db);
}
return $db;
}
/*
| DATA :: GET SPAM INDEX
| @since 0.1.0
|
| @param bool TRUE to just return the keys, FALSE to return the complete array.
|
| @return array All spam comments with basic comment data as ARRAY.
*/
public function getSpam($keys = false){
$db = array();
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if($value["status"] === "spam"){
$db[$key] = $value;
}
}
if($keys){
return array_keys($db);
}
return $db;
}
/*
| DATA :: COUNT COMMENTS
| @since 0.1.0
|
| @param multi A single comment status as STRING, multiple as ARRAY.
| Use `null` to count all comments.
|
| @return int The number of comments of the respective index.
*/
public function count($status = array("approved")){
if($status === null){
return count($this->db);
}
if(!is_array($status)){
$status = array($status);
}
$count = 0;
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if(in_array($value["status"], $status)){
$count++;
}
}
return $count;
}
/*
| DATA :: GET COMMENT
| @since 0.1.0
|
| @param string The desired comment UID.
|
| @return multi The comment index array on success, FALSE on failure
*/
public function getComment($uid){
return array_key_exists($uid, $this->db)? $this->db[$uid]: false;
}
/*
| DATA :: LIST COMMENTS
| @since 0.1.0
|
| @param multi A single comment status as STRING, multiple as ARRAY.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective unique comment IDs as ARRAY, FALSE on failure.
*/
public function getList($status = array("approved"), $page = 1, $limit = -1){
if($status === null){
return count($this->db);
}
if(!is_array($status)){
$status = array($status);
}
// Get List
$list = array();
foreach($this->db AS $key => $value){
if(!isset($value["status"]) || empty($value["status"])){
continue;
}
if(in_array($value["status"], $status)){
$list[] = $key;
}
}
return $this->limitList($list, $page, $limit);
}
/*
| DATA :: LIST COMMENTS BY UUID
| @since 0.1.0
|
| @param multi A single page UUID as STRING, multiple as ARRAY.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective unique comment IDs as ARRAY, FALSE on failure.
*/
public function getListByUUID($uuid, $page = 1, $limit = -1){
if(!is_array($uuid)){
$uuid = array($uuid);
}
// Get List
$list = array();
foreach($this->db AS $key => $value){
if(!isset($value["page_uuid"]) || empty($value["page_uuid"])){
continue;
}
if(in_array($value["page_uuid"], $uuid)){
$list[] = $key;
}
}
return $this->limitList($list, $page, $limit);
}
/*
| DATA :: LIST COMMENTS BY PARENT
| @since 0.1.0
|
| @param multi A single comment UID as STRING.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective unique comment IDs as ARRAY, FALSE on failure.
*/
public function getListByParent($uid, $page = 1, $limit = -1){
if(!is_string($uid) || !$this->exists($uid)){
return array();
}
// Get List
$list = array($uid);
foreach($this->db AS $key => $value){
if(!isset($value["parent_uid"]) || empty($value["parent_uid"])){
continue;
}
if($value["parent_uid"] === $uid){
$list[] = $key;
}
}
return $this->limitList($list, $page, $limit);
}
/*
| DATA :: LIST COMMENTS BY USER
| @since 0.1.0
|
| @param string A single username, unique user id or eMail address.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective unique comment IDs as ARRAY, FALSE on failure.
*/
public function getListByUser($string, $page = 1, $limit = -1){
global $users, $SnickerUsers;
// Get Member / Guest
$guest = false;
$member = false;
if(Valid::email($string)){
if(($user = $users->getByEmail($string)) !== false){
$member = "bludit::{$user}";
}
} else {
if($users->exists($string)){
$member = "bludit::{$string}";
}
}
if(($user = $SnickerUsers->get($string)) !== false){
$guest = "guest::{$user["uuid"]}";
}
if(!$member && !$guest){
return array();
}
// Get List
$list = array();
foreach($this->db AS $key => $value){
if(!isset($value["author"]) || empty($value["author"])){
continue;
}
if($value["author"] == $member || $value["author"] == $guest){
$list[] = $key;
}
}
return $this->limitList($list, $page, $limit);
}
/*
| DATA :: SEARCH COMMENTS BY TITLE & EXCERPT
| @since 0.1.0
|
| @param string The string to be searched.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective unique comment IDs as ARRAY, FALSE on failure.
*/
public function searchComments($search, $page = 1, $limit = -1){
$list = array();
foreach($this->db AS $key => $value){
if(isset($value["title"]) && stripos($value["title"], $search) !== false){
$list[] = $key;
} else if(isset($value["excerpt"]) && stripos($value["excerpt"], $search) !== false){
$list[] = $key;
}
}
return $this->limitList($list, $page, $limit);
}
/*
| HANDLE :: ADD COMMENT
| @since 0.1.0
|
| @param string The unique comment ID.
| @param array The comment array.
|
| @return bool TRUE if everything is fluffy, FALSE if not.
*/
public function add($uid, $comment){
$row = array();
foreach($this->dbFields AS $field => $value){
if(isset($comment[$field])){
$final = is_string($comment[$field])? Sanitize::html($comment[$field]): $comment[$field];
} else {
$final = $value;
}
settype($final, gettype($value));
$row[$field] = $final;
}
// Format Excerpt
$row["excerpt"] = strip_tags($comment["comment"]);
if(strlen($row["excerpt"]) > 142){
$row["excerpt"] = substr($row["excerpt"], 0, 139) . "...";
}
// Insert and Return
$this->db[$uid] = $row;
$this->sortBy();
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return true;
}
/*
| HANDLE :: UPDATE COMMENT
| @since 0.1.0
|
| @param string The unique comment ID.
| @param array The comment array.
|
| @return bool TRUE if everything is fluffy, FALSE if not.
*/
public function edit($uid, $comment){
if(!$this->exists($uid)){
$this->log(__METHOD__, "error-comment-uid", array($uid));
return false;
}
$data = $this->db[$uid];
// Loop Fields
$row = array();
foreach($this->dbFields AS $field => $value){
if(isset($comment[$field])){
$final = is_string($comment[$field])? Sanitize::html($comment[$field]): $comments[$field];
} else {
$final = $data[$field];
}
settype($final, gettype($value));
$row[$field] = $final;
}
// Format Excerpt
$row["excerpt"] = strip_tags($comment["comment"]);
if(strlen($row["excerpt"]) > 142){
$row["excerpt"] = substr($row["excerpt"], 0, 139) . "...";
}
// Update and Return
$this->db[$uid] = $row;
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return true;
}
/*
| HANDLE :: DELETE COMMENT
| @since 0.1.0
|
| @param string The unique comment ID.
|
| @return bool TRUE if everything is fluffy, FALSE if not.
*/
public function delete($uid){
if(!$this->exists($uid)){
return false;
}
unset($this->db[$uid]);
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return true;
}
/*
| INTERNAL :: SORT COMMENTS
| @since 0.1.0
|
| @return bool TRUE
*/
public function sortBy(){
global $SnickerPlugin;
if($SnickerPlugin->getValue("frontend_order") === "date_asc"){
uasort($this->db, function($a, $b){
return $a["date"] > $b["date"];
});
} else if($SnickerPlugin->getValue("frontend_order") === "date_desc"){
uasort($this->db, function($a, $b){
return $a["date"] < $b["date"];
});
}
return true;
}
}

View File

@@ -0,0 +1,392 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/class.comments-users.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
class CommentsUsers extends dbJSON{
/*
| DATABASE FIELDS
*/
protected $dbFields = array(
"username" => "", // Username
"email" => "", // User eMail Address
"hash" => "", // Hashed IP + User Agent
"blocked" => false, // Blocked?
"comments" => array() // Page UIDs => array(CommentUIDs)
);
/*
| CONSTRUCTOR
| @since 0.1.0
*/
public function __construct(){
parent::__construct(DB_SNICKER_USERS);
if(!file_exists(DB_SNICKER_USERS)){
$this->db = array();
$this->save();
}
}
/*
| GET COMMENTS BY UNIQUE USER ID
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
| @param bool TRUE to just return the keys, FALSE to return it as Comment objects.
|
| @return multi The comment keys / objects as ARRAY, FALSE on failure.
*/
public function getComments($uuid, $keys = true){
global $Snicker;
// Validate Data
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
// Return Keys
$data = $this->db[$uuid]["comments"];
if($keys === true){
return $data;
}
// Return Objects
foreach($data AS &$key){
$key = $Snicker->getComment($key);
}
return $key;
}
/*
| EXISTS
| @since 0.1.0
*/
public function exists($uid){
return isset($this->db[$uid]);
}
/*
| GET USER BY UUID
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
|
| @return multi The user database array on success, FALSE on failure.
*/
public function get($uuid){
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
$data = $this->db[$uuid];
$data["uuid"] = $uuid;
return $data;
}
/*
| GET CURRENT USER ID
| @since 0.1.0
|
| @return multi The user UUID on success, FALSE on failure.
*/
public function getCurrent(){
global $security;
$hash = md5($security->getUserIp() . $_SERVER["HTTP_USER_AGENT"]);
foreach($this->db AS $uuid => $fields){
if($fields["hash"] === $hash){
return $uuid;
}
}
return false;
}
/*
| GET USER
| @since 0.1.0
|
| @param string Get the user by Comment Author STRING.
|
| @return multi The user data array on success, FALSE on failure.
*/
public function getByString($string){
global $users;
// Check User Instance
if(strpos($string, "bludit::") === 0){
$username = substr($string, strlen("bludit::"));
if($users->exists($username)){
$user = $users->getUserDB($username);
$user["username"] = $user["nickname"];
return $user;
}
return false;
}
// Check Guest Instance
if(strpos($string, "guest::") === 0){
$uuid = substr($string, strlen("guest::"));
if($this->exists($uuid)){
return $this->db[$uuid];
}
return false;
}
// Return as Anonymous
return array(
"username" => "Anonymous",
"email" => "anonymous@" . $_SERVER["SERVER_NAME"]
);
}
/*
| GET LIST
| @since 0.1.0
|
| @param string The string to be searched or NULL.
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
|
| @return array The respective user keys with an ARRAY or FALSE on failure.
*/
public function getList($search = null, $page = 1, $limit = -1){
if($search !== null){
$list = array();
foreach($this->db AS $uuid => $fields){
if(stripos($fields["username"], $search) === false){
continue;
}
if(stripos($fields["email"], $search) === false){
continue;
}
$list[$uuid] = $fields;
}
} else {
$list = $this->db;
}
// Limit
if($limit == -1){
return $list;
}
// Offset
$offset = $limit * ($page - 1);
$count = min(($offset + $limit - 1), count($list));
if($offset < 0 || $offset > $count){
return false;
}
return array_slice($list, $offset, $limit, true);
}
/*
| MAIN USER HANDLER
| @since 0.1.0
|
| @param string The username as STRING.
| @param string The email address as STRING.
|
| @return multi The (new) UUID on success, FALSE on failure.
*/
public function user($username, $email){
global $security;
// Validate Username
$username = Sanitize::html(strip_tags(trim($username)));
if(empty($username) || strlen($username) > 42){
return false;
}
// Validate eMail Address
$email = strtolower(Sanitize::email($email));
if(empty($email) || Valid::email($email) === false){
return false;
}
// Check User
$uuid = md5($email);
if(array_key_exists($uuid, $this->db)){
return $uuid;
}
// Add User
$this->db[$uuid] = array(
"username" => $username,
"email" => $email,
"hash" => md5($security->getUserIp() . $_SERVER["HTTP_USER_AGENT"]),
"blocked" => false,
"comments" => array()
);
if(!$this->save()){
return false;
}
return $uuid;
}
public function add($username, $email, $meta = array()){
return $this->user($username, $email, $meta);
}
/*
| EDIT USER DATA
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
| @param multi The new username (or NULL to keep the existing one).
| @param multi The new eMail address (or NULL to keep the existing one).
| ATTENTION: The new eMail address CANNOT be used already!
| ATTENTION: The new eMail address CHANGES the unique user id (UUID)!
| @param multi TRUE to block the user, FALSE to unblock, null to keep the current.
|
| @return multi The (new) UUID on success, FALSE on failure.
*/
public function edit($uuid, $username = null, $email = null, $blocked = null){
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
$data = $this->db[$uuid];
// Change Username
if($username !== null){
$username = Sanitize::html(strip_tags(trim($username)));
if(empty($username) || strlen($username) > 42){
return false;
}
$data["username"] = $username;
}
// Change eMail
if($email !== null){
$email = strtolower(Sanitize::email($uuid));
if(Valid::email($email) === false){
return false;
}
$data["email"] = $email;
$newuuid = md5($email);
}
// Change Blocked
if(is_bool($blocked)){
$data["blocked"] = $blocked;
}
// Update UUID
if(isset($newuuid) && $uuid !== $newuuid){
unset($this->db[$uuid]);
$uuid = $newuuid;
}
// Store new Data
$this->db[$uuid] = $data;
if(!$this->save()){
return false;
}
return $uuid;
}
/*
| ADD COMMENT ID TO USER
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
| @param string The unique comment ID as STRING.
|
| @return bool TRUE on success, FALSE on failure.
*/
public function addComment($uuid, $uid){
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
// Add Comment UID
$user = $this->db[$uuid];
if(!isset($user["comments"]) || !is_array($user["comments"])){
$user["comments"] = array();
}
if(!in_array($uid, $user["comments"])){
$user["comments"][] = $uid;
}
// Save & Return
$this->db[$uuid] = $user;
if(!$this->save()){
return false;
}
return true;
}
/*
| DELETE COMMENT ID TO USER
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
| @param string The unique comment ID as STRING.
|
| @return bool TRUE on success, FALSE on failure.
*/
public function deleteComment($uuid, $uid){
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
// Delete Comment UID
$user = $this->db[$uuid];
if(!isset($user["comments"])){
$user["comments"] = array();
}
if(in_array($uid, $user["comments"])){
unset($user["comments"][array_search($uid, $user["comments"])]);
}
// Save & Return
$this->db[$uuid] = $user;
if(!$this->save()){
return false;
}
return true;
}
/*
| DELETE USER
| @since 0.1.0
|
| @param string The unique user ID as string (or the user eMail address).
|
| @return bool TRUE on success, FALSE on failure.
*/
public function delete($uuid){
if(Valid::email($uuid) !== false){
$uuid = md5(strtolower(Sanitize::email($uuid)));
}
if(!array_key_exists($uuid, $this->db)){
return false;
}
// Delete & Return
unset($this->db[$uuid]);
if(!$this->save()){
return false;
}
return true;
}
}

View File

@@ -0,0 +1,226 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/class.comments-votes.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
class CommentsVotes extends dbJSON{
const KEY = "snicker-ratings";
/*
| DATABASE FIELDS
*/
protected $dbFields = array( );
/*
| CONSTRUCTOR
| @since 0.1.0
*/
public function __construct(){
parent::__construct(DB_SNICKER_VOTES);
if(!file_exists(DB_SNICKER_VOTES)){
$this->db = array();
$this->save();
}
}
/*
| HANDLE :: CURRENT USER
| @since 0.1.0
*/
public function currentUser(){
global $login, $security;
if(!is_a($login, "Login")){
$login = new Login();
}
// Get Current User
if($login->isLogged()){
return "bludit::" . $login->username();
}
return "guest::" . md5($security->getUserIp() . $_SERVER["HTTP_USER_AGENT"]);
}
/*
| HANDLE :: HAS VOTED
| @since 0.1.0
*/
public function hasVoted($uid, $vote = null){
$user = $this->currentUser();
$config = sn_config("comment_vote_storage");
// Database Storage
$db = strpos($user, "bludit::") === 0 || $config === "database";
if($db){
if(!array_key_exists($user, $this->db)){
return false;
}
$data = $this->db[$user];
} else {
$store = ($config == "cookie")? "Cookie": "Session";
$data = $store::get(self::KEY);
$data = !empty($data)? @unserialize($data): false;
if(!is_array($data)){
return false;
}
}
// Check Data
if(!array_key_exists($uid, $data)){
return false;
}
return ($vote === null || $data[$uid] === $vote);
}
public function hasLiked($uid){
return $this->hasVoted($uid, "like");
}
public function hasDisliked($uid){
return $this->hasVoted($uid, "dislike");
}
/*
| HANDLE :: ADD NEW COMMENT VOTING
| @since 0.1.0
*/
public function add($uid, $vote = "like"){
$user = $this->currentUser();
$config = sn_config("comment_vote_storage");
// Database Storage
$db = strpos($user, "bludit::") === 0 || $config === "database";
if($db){
if(!array_key_exists($user, $this->db)){
$this->db[$user] = array();
}
if(array_key_exists($uid, $this->db[$user])){
return false;
}
$this->db[$user][$uid] = $vote;
return $this->save() !== false;
}
// Cookie | Session Storage
$store = ($config == "cookie")? "Cookie": "Session";
$data = $store::get(self::KEY);
$data = !empty($data)? @unserialize($data): false;
if(is_array($data)){
if(array_key_exists($uid, $data)){
return false;
}
} else {
$data = array();
}
$data[$uid] = $vote;
$store::set(self::KEY, serialize($data));
return true;
}
/*
| HANDLE :: EDIT COMMENT VOTING
| @since 0.1.0
*/
public function edit($uid, $vote = "like"){
$user = $this->currentUser();
$config = sn_config("comment_vote_storage");
// Database Storage
$db = strpos($user, "bludit::") === 0 || $config === "database";
if($db){
if(!array_key_exists($user, $this->db)){
$this->db[$user] = array();
}
if(array_key_exists($uid, $this->db[$user]) && $this->db[$user][$uid] === $vote){
return false;
}
$this->db[$user][$uid] = $vote;
return $this->save() !== false;
}
// Cookie | Session Storage
$store = ($config == "cookie")? "Cookie": "Session";
$data = $store::get(self::KEY);
$data = !empty($data)? @unserialize($data): false;
if(is_array($data)){
if(array_key_exists($uid, $data) && $data[$uid] === $vote){
return false;
}
} else {
$data = array();
}
$data[$uid] = $vote;
$store::set(self::KEY, serialize($data));
return true;
}
/*
| HANDLE :: DELETE COMMENT VOTING
| @since 0.1.0
*/
public function delete($uid){
$user = $this->currentUser();
$config = sn_config("comment_vote_storage");
// Database Storage
$db = strpos($user, "bludit::") === 0 || $config === "database";
if($db){
if(!array_key_exists($user, $this->db)){
return true;
}
if(!array_key_exists($uid, $this->db[$user])){
return true;
}
unset($this->db[$user][$uid]);
return $this->save() !== false;
}
// Cookie | Session Storage
$store = ($config == "cookie")? "Cookie": "Session";
$data = $store::get(self::KEY);
$data = !empty($data)? @unserialize($data): false;
if(!is_array($data)){
return true;
}
if(!array_key_exists($uid, $data)){
return true;
}
unset($data[$uid]);
$store::set(self::KEY, serialize($data));
return true;
}
/*
| HANDLE :: DELETE BY USER
| @since 0.1.0
*/
public function deleteByUser($user){
$config = sn_config("comment_vote_storage");
// Database Storage
$db = strpos($user, "bludit::") === 0 || $config === "database";
if($db){
if(!array_key_exists($user, $this->db)){
return true;
}
unset($this->db[$user]);
return $this->save() !== false;
}
// Cookie | Session Storage
$store = ($config == "cookie")? "Cookie": "Session";
$data = $store::get(self::KEY);
$data = !empty($data)? @unserialize($data): false;
if(!is_array($data)){
return true;
}
$store::set(self::KEY, serialize(array()));
return true;
}
}

564
system/class.comments.php Normal file
View File

@@ -0,0 +1,564 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/class.comments.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
class Comments extends dbJSON{
/*
| PAGE UUID
*/
protected $uuid;
/*
| DATABASE FIELDS
*/
protected $dbFields = array(
"type" => "comment", // Comment Type ("comment", "reply", "pingback")
"depth" => 1, // Comment Depth (starting with 1)
"title" => "", // Comment Title
"status" => "", // Comment Status ("pending", "approved", "rejected", "spam")
"comment" => "", // Comment Content
"rating" => [0, 0], // Comment Rating
"page_uuid" => "", // Comment Page UUID
"parent_uid" => "", // Comment Parent UID
"author" => "", // Comment Author (bludt::username or guest::uuid)
"subscribe" => false, // eMail Subscription
"date" => "", // Date Comment Written
"dateModified" => "", // Date Comment Modified
"dateAudit" => "", // Date Comment Audit
"custom" => array(), // Custom Data
);
/*
| CONSTRUCTOR
| @since 0.1.0
|
| @param string The UUID of the respective page.
*/
public function __construct($uuid){
global $pages;
// Get Page
if($pages->getByUUID($uuid) === false){
$error = "The Page UUID couldn't be found in the database [{$uuid}]";
Log::set(__METHOD__ . LOG_SEP . $error);
throw new Exception($error);
}
$this->uuid = $uuid;
parent::__construct(DB_SNICKER_COMMENTS . "comments-{$uuid}.php");
}
/*
| HELPER :: FILL LOG FILE
| @since 0.1.0
|
| @param string The respective method for the log (Use __METHOD__)
| @param string The respective error message to be logged.
| @param array Additional values AS array for the `vsprintf` function.
*/
private function log($method, $string, $args){
$strings = array(
"error-comment-uid" => "The comment UID is invalid or does not exist [%s]",
"error-page-uuid" => "The page uuid is invalid or does not exist [%s]",
"error-create-dir" => "The comment directory could not be created [%s]",
"error-create-file" => "The comment file could not be created [%s]",
"error-comment-file" => "The comment file does not exist [%s]",
"error-comment-update" => "The comment file could not be updated [%s]",
"error-comment-remove" => "The comment file could not be deleted [%s]",
"error-update-db" => "The comment database could not be updated"
);
if(array_key_exists($string, $strings)){
$string = $strings[$string];
}
Log::set($method . LOG_SEP . vsprintf("Error occured: {$string}", $args), LOG_TYPE_ERROR);
}
/*
| HELPER :: GENERATE UNIQUE COMMENT ID
| @since 0.1.0
*/
private function generateUID(){
if(function_exists("random_bytes")){
return md5(bin2hex(random_bytes(16)) . time());
} else if(function_exists("openssl_random_pseudo_bytes")){
return md5(bin2hex(openssl_random_pseudo_bytes(16)) . time());
}
return md5(uniqid() . time());
}
/*
| PUBLIC :: GET DEFAULT FIELDS
| @since 0.1.0
|
| @return array An array with all default fields and values per entry.
*/
public function getDefaultFields(){
return $this->dbFields;
}
/*
| DATA :: GET DATABASE
| @since 0.1.0
|
| @param bool TRUE to just return the keys, FALSE to return the complete DB.
|
| @return array The complete database entries (or keys) within an ARRAY.
*/
public function getDB($keys = true){
return ($keys)? array_keys($this->db): $this->db;
}
/*
| DATA :: CHECK IF COMMENT ITEM EXISTS
| @since 0.1.0
|
| @param string The unique comment ID.
|
| @return bool TRUE if the comment ID exists, FALSE if not.
*/
public function exists($uid){
return isset($this->db[$uid]);
}
/*
| DATA :: GET COMMENT ITEM
| @since 0.1.0
|
| @param string The unique comment ID.
|
| @return array The comment data array on success, FALSE on failure.
*/
public function getCommentDB($uid){
return ($this->exists($uid))? $this->db[$uid]: false;
}
/*
| DATA :: LIST COMMENTS
| @since 0.1.0
|
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
| @param multi The desired comment type as STRING, multiple as ARRAY.
| Pass `null` to get each comment type.
| @param multi The desired comment status as STRING, multiple as ARRAY.
| Pass `null` to get each comment status.
|
| @return array The respective database keys with an ARRAY or FALSE on failure.
*/
public function getList($page, $limit, $type = array("comment", "reply"), $status = array("approved")){
$type = is_string($type)? array($type): $type;
if(!is_array($type)){
$type = null;
}
$status = is_string($status)? array($status): $status;
if(!is_array($status)){
$type = null;
}
// Format List
$list = array();
foreach($this->db AS $key => $fields){
if($type !== null && !in_array($fields["type"], $type)){
continue;
}
if($status !== null && !in_array($fields["status"], $status)){
continue;
}
array_push($list, $key);
}
// Limit
if($limit == -1){
return $list;
}
// Offset
$offset = $limit * ($page - 1);
$count = min(($offset + $limit - 1), count($list));
if($offset < 0 || $offset > $count){
return false;
}
return array_slice($list, $offset, $limit, true);
}
/*
| DATA :: GENERATE A DEPTH LIST
| @since 0.1.0
|
| @param int The current comment page number, starting with 1.
| @param int The number of comments to be shown per page.
| @param multi The desired comment type as STRING, multiple as ARRAY.
| Pass `null` to get each comment type.
| @param multi The desired comment status as STRING, multiple as ARRAY.
| Pass `null` to get each comment status.
|
| @return array The respective database keys with an ARRAY or FALSE on failure.
*/
public function getDepthList($page, $limit, $type = array("comment", "reply"), $status = array("approved")){
global $login, $SnickerUsers;
$this->sortBy();
// Validate Parameters
$type = is_string($type)? array($type): $type;
if(!is_array($type)){
$type = null;
}
$status = is_string($status)? array($status): $status;
if(!is_array($status)){
$type = null;
}
// Get User Pending
if(in_array("pending", $status)){
if(!is_a($login, "Login")){
$login = new Login();
}
if($login->isLogged()){
$user = "bludit::" . $login->username();
} else {
if(($user = $SnickerUsers->getCurrent()) !== false){
$user = "guest::" . $user;
}
}
}
// Format List
$list = array();
$children = array();
foreach($this->db AS $key => $fields){
if($type !== null && !in_array($fields["type"], $type)){
continue;
}
if($status !== null && !in_array($fields["status"], $status)){
continue;
}
if($fields["status"] === "pending" && $fields["author"] !== $user){
continue;
}
if(!empty($fields["parent_uid"])){
if(!array_key_exists($fields["parent_uid"], $children)){
$children[$fields["parent_uid"]] = array();
}
array_push($children[$fields["parent_uid"]], $key);
} else {
array_push($list, $key);
}
}
// Offset
$count = 0;
$offset = $limit * ($page - 1);
for(; $count < $offset ;){
$key = array_shift($list);
$count++;
if(array_key_exists($key, $children)){
$count += count($children[$key]);
unset($children[$key]);
}
}
// Generator
$count = 0;
foreach($list AS $key){
if($count >= $limit){
break;
}
$count++;
yield $key;
if(!array_key_exists($key, $children)){
continue;
}
$loop = $key;
$depth = array();
while(true){
if(empty($depth) && empty($children[$key])){
break;
}
if(array_key_exists($loop, $children) && count($children[$loop]) > 0){
array_push($depth, $loop);
$loop = array_shift($children[$loop]);
$count++;
yield $loop;
} else {
$loop = array_pop($depth);
continue;
}
}
}
}
/*
| DATA :: COUNT COMMENTS
| @since 0.1.0
|
| @param multi The desired comment type as STRING, multiple as ARRAY.
| Pass `null` to get each comment type.
|
| @return int The total number of comments.
*/
public function count($type = array("comment", "reply")){
$type = is_string($type)? array($type): $type;
if(!is_array($type)){
$type = null;
}
// Count All
if($type === null){
return count($this->db);
}
// Count
$count = 0;
foreach($this->db AS $key => $fields){
if(!in_array($fields["type"], $type)){
continue;
}
$count++;
}
return $count;
}
/*
| HANDLE :: ADD A NEW COMMENT
| @since 0.1.0
|
| @param array The respective comment array.
|
| @return multi The comment UID on success, FALSE on failure.
*/
public function add($args){
global $SnickerIndex, $SnickerUsers;
// Loop Default Fields
$row = array();
foreach($this->dbFields AS $field => $value){
if(isset($args[$field])){
$final = $args[$field];
} else {
$final = $value;
}
settype($final, gettype($value));
$row[$field] = $final;
}
// Create (U)UIDs
$uid = $this->generateUID();
$row["page_uuid"] = $this->uuid;
// Validate Parent UID
if(!empty($row["parent_uid"]) && !$this->exists($row["parent_uid"])){
$row["parent_uid"] = null;
}
// Validate Type and Depth
if(!empty($row["parent_uid"])){
$row["type"] = "reply";
$row["depth"] = $this->db[$row["parent_uid"]]["depth"] + 1;
} else {
$row["type"] = "comment";
$row["depth"] = 1;
}
// Validata Status
if(!in_array($row["status"], array("pending", "approved", "rejected", "spam"))){
$row["status"] = "pending";
}
// Sanitize Strings
$row["title"] = Sanitize::html(strip_tags($row["title"]));
$row["author"] = Sanitize::html($row["author"]);
// Sanitize Comment
$allowed = "<a><b><strong><i><em><u><del><ins><s><strike><p><br><br/><br />";
$allowed .= "<mark><abbr><acronym><dfn><ul><ol><li><dl><dt><dd><hr><hr/><hr />";
if(sn_config("comment_markup_html")){
$row["comment"] = strip_tags($row["comment"], $allowed);
} else {
$row["comment"] = strip_tags($row["comment"]);
}
$row["comment"] = Sanitize::html($row["comment"]);
// Validate Comment
$limit = sn_config("comment_limit");
if($limit > 0 && strlen($row["comment"]) > $limit){
$row["comment"] = substr($row["comment"], 0, $limit);
}
// Set Static Data
$row["rating"] = array(0, 0);
$row["subscribe"] = $row["subscribe"] === true;
$row["date"] = Date::current(DB_DATE_FORMAT);
$row["dateModified"] = "";
if($row["status"] !== "pending"){
$row["dateAudit"] = Date::current(DB_DATE_FORMAT);
}
// Add Index
if(!is_a($SnickerIndex, "CommentsIndex")){
$SnickerIndex = new CommentsIndex();
}
if(!$SnickerIndex->add($uid, $row)){
return false;
}
if(strpos($row["author"], "guest::") === 0){
$SnickerUsers->addComment(substr($row["author"], strlen("guest::")), $uid);
}
// Insert Comment
$this->db[$uid] = $row;
$this->sortBy();
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return $uid;
}
/*
| HANDLE :: EDIT AN EXISTING COMMENT
| @since 0.1.0
|
| @param string The unique comment ID as STRING.
| @param array The respective comment data, whcih you want to update.
|
| @return multi The comment UID on success, FALSE on failure.
*/
public function edit($uid, $args){
global $SnickerIndex;
// Loop Default Fields
$row = array();
foreach($this->dbFields AS $field => $value){
if(isset($args[$field])){
$final = is_string($args[$field])? Sanitize::html($args[$field]): $args[$field];
} else {
$final = $this->db[$uid][$field];
}
settype($final, gettype($value));
$row[$field] = $final;
}
// Create / Check (U)UIDs
if(!$this->exists($uid)){
$this->log(__METHOD__, "error-comment-uid", array($uid));
return false;
}
$row["page_uuid"] = $this->uuid;
// Validate Parent UID
if(!empty($row["parent_uid"]) && !$this->exists($row["parent_uid"])){
$row["parent_uid"] = $data["parent_uid"];
}
// Validate Type and Depth
if(!empty($row["parent_uid"])){
$row["type"] = "reply";
$row["depth"] = $this->db[$row["parent_uid"]]["depth"] + 1;
} else {
$row["type"] = "comment";
$row["depth"] = 1;
}
// Validata Status
if(!in_array($row["status"], array("pending", "approved", "rejected", "spam"))){
$row["status"] = $this->db[$uid]["status"];
}
// Sanitize Strings
$row["title"] = Sanitize::html($row["title"]);
$row["comment"] = Sanitize::html($row["comment"]);
$row["author"] = Sanitize::html($row["author"]);
// Set Static Data
$row["subscribe"] = $row["subscribe"] === true;
$row["dateModified"] = Date::current(DB_DATE_FORMAT);
if($row["status"] !== $this->db[$uid]["status"]){
$row["dateAudit"] = Date::current(DB_DATE_FORMAT);
}
// Update Index
if(!$SnickerIndex->edit($uid, $row)){
return false;
}
// Update and Return
$this->db[$uid] = $row;
$this->sortBy();
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return $uid;
}
/*
| HANDLE :: DELETE AN EXISTING COMMENT
| @since 0.1.0
|
| @param array The respective comment UID to delete.
|
| @return bool TRUE on success, FALSE on failure.
*/
public function delete($uid){
global $SnickerIndex, $SnickerUsers;
if(!isset($this->db[$uid])){
return false;
}
$row = $this->db[$uid];
// Remove Index
if(!$SnickerIndex->delete($uid)){
return false;
}
if(strpos($row["author"], "guest::") === 0){
$SnickerUsers->deleteComment(substr($row["author"], strlen("guest::")), $uid);
}
// Remove Database Item
unset($this->db[$uid]);
if($this->save() !== true){
Log::set(__METHOD__, "error-update-db");
return false;
}
return true;
}
/*
| INTERNAL :: SORT COMMENTS
| @since 0.1.0
|
| @return bool TRUE
*/
public function sortBy(){
global $SnickerPlugin;
if($SnickerPlugin->getValue("frontend_order") === "date_asc"){
uasort($this->db, function($a, $b){
return $a["date"] > $b["date"];
});
} else if($SnickerPlugin->getValue("frontend_order") === "date_desc"){
uasort($this->db, function($a, $b){
return $a["date"] < $b["date"];
});
}
return true;
}
}

1045
system/class.snicker.php Normal file

File diff suppressed because it is too large Load Diff

90
system/functions.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
/*
| Snicker The first native FlatFile Comment Plugin 4 Bludit
| @file ./system/functions.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.2 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/snicker
| @license X11 / MIT License
| @copyright Copyright © 2019 SamBrishes, pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")){ die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
/*
| S18N :: FORMAT AND GET STRING
| @since 0.1.0
|
| @param string The respective string to translate.
| @param array Some additional array for `printf()`.
|
| @return string The translated and formated string.
*/
function sn__($string, $args = array()){
global $L;
$hash = "s18n-" . md5(strtolower($string));
$value = $L->g($hash);
if($hash === $value){
$value = $string;
}
return (count($args) > 0)? vsprintf($value, $args): $value;
}
/*
| S18N :: FORMAT AND PRINT STRING
| @since 0.1.0
|
| @param string The respective string to translate.
| @param array Some additional array for `printf()`.
|
| @return <print>
*/
function sn_e($string, $args = array()){
print(sn__($string, $args));
}
/*
| SHORTFUNC :: GET VALUE
| @since 0.1.0
|
| @param string The respective Snicker configuration key.
|
| @return multi The respective value or FALSE if the option doens't exist.
*/
function sn_config($key){
global $SnickerPlugin;
return $SnickerPlugin->getValue($key);
}
/*
| SHORTFUNC :: RESPONSE
| @since 0.1.0
|
| @return die();
*/
function sn_response($data, $key = null){
global $SnickerPlugin;
return $SnickerPlugin->response($data, $key);
}
/*
| SHORTFUNC :: SELECTED
| @since 0.1.0
|
| @return die();
*/
function sn_selected($field, $value = true, $print = true){
global $SnickerPlugin;
return $SnickerPlugin->selected($field, $value, $print);
}
/*
| SHORTFUNC :: CHECKED
| @since 0.1.0
|
| @return die();
*/
function sn_checked($field, $value = true, $print = true){
global $SnickerPlugin;
return $SnickerPlugin->checked($field, $value, $print);
}