m1k1oblog/app/post.class.php

335 lines
8.9 KiB
PHP
Raw Normal View History

2016-12-27 20:25:32 +00:00
<?php
class Post
{
2016-12-28 13:19:08 +00:00
private static function login_protected(){
2016-12-28 14:46:07 +00:00
if(!User::is_logged_in()){
2017-06-19 17:51:59 +00:00
throw new Exception(__("You need to be logged in to perform this action."));
2016-12-28 13:19:08 +00:00
}
}
2017-09-24 18:01:23 +00:00
2016-12-27 20:25:32 +00:00
private static function parse_content($c){
2017-09-24 18:01:23 +00:00
require_once APP_PATH."jbbcode/Parser.php";
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
2016-12-27 20:25:32 +00:00
2017-09-23 10:19:00 +00:00
if(Config::get("highlight")){
2017-09-24 18:01:23 +00:00
$c = str_replace("\t", " ", $c);
$c = preg_replace("/\[(\/?)code(=(?:[^\[]+))?\]\s*?(?:\n|\r)?/i", '[$1code$2]', $c);
// Add code definiton
$parser->addCodeDefinition(new class extends \JBBCode\CodeDefinition {
public function __construct($useOption){
parent::__construct($useOption);
$this->setTagName("code");
$this->setParseContent(false);
$this->setUseOption(true);
}
public function asHtml(\JBBCode\ElementNode $el){
$content = $this->getContent($el);
return '<code class="'.$el->getAttribute().'">'.htmlentities($content).'</code>';
}
});
2017-09-23 10:19:00 +00:00
}
2017-09-24 18:01:23 +00:00
if(($tags = Config::get_safe("bbtags", [])) && !empty($tags)){
foreach($tags as $tag => $content){
$builder = new JBBCode\CodeDefinitionBuilder($tag, $content);
$parser->addCodeDefinition($builder->build());
}
}
$parser->parse($c);
// Visit every text node
$parser->accept(new class implements \JBBCode\NodeVisitor{
function visitDocumentElement(\JBBCode\DocumentElement $documentElement){
foreach($documentElement->getChildren() as $child) {
$child->accept($this);
}
}
2016-12-27 20:25:32 +00:00
2017-09-24 18:01:23 +00:00
function visitTextNode(\JBBCode\TextNode $textNode){
$c = $textNode->getValue();
$c = preg_replace('/\"([^\"]+)\"/i', "$1\"", $c);
$c = htmlentities($c);
$c = preg_replace('/\*([^\*]+)\*/i', "<strong>$1</strong>", $c);
$c = preg_replace('/(https?\:\/\/[^\" \n]+)/i', "<a href=\"\\0\" target=\"_blank\">\\0</a>", $c);
$c = preg_replace('/(\#[A-Za-z0-9-_]+)/i', "<span class=\"tag\">\\0</span>", $c);
$c = nl2br($c);
$textNode->setValue($c);
}
2016-12-27 20:25:32 +00:00
2017-09-24 18:01:23 +00:00
function visitElementNode(\JBBCode\ElementNode $elementNode){
/* We only want to visit text nodes within elements if the element's
* code definition allows for its content to be parsed.
*/
if ($elementNode->getCodeDefinition()->parseContent()) {
foreach ($elementNode->getChildren() as $child) {
$child->accept($this);
}
}
}
});
return $parser->getAsHtml();
2016-12-27 20:25:32 +00:00
}
2016-12-29 22:26:19 +00:00
private static function raw_data($raw_input){
$default_input = [
"text" => '',
2017-09-23 14:09:42 +00:00
"plain_text" => '',
2016-12-29 22:26:19 +00:00
"feeling" => '',
"persons" => '',
"location" => '',
"content_type" => '',
"content" => '',
2017-06-19 17:51:59 +00:00
"privacy" => ''
2016-12-29 22:26:19 +00:00
];
// Handle only allowed keys
$raw_output = array();
foreach($default_input as $key => $def){
// Key exists in input
if(array_key_exists($key, $raw_input)){
$raw_output[$key] = $raw_input[$key];
} else {
$raw_output[$key] = $default_input[$key];
}
}
2017-06-19 17:51:59 +00:00
if($raw_output['privacy'] != "public" && $raw_output['privacy'] != "friends"){
$raw_output['privacy'] = "private";
2016-12-29 22:26:19 +00:00
}
return $raw_output;
}
2016-12-27 20:25:32 +00:00
public static function insert($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2017-09-23 11:42:50 +00:00
2016-12-29 22:26:19 +00:00
$data = self::raw_data($r);
2017-09-23 11:42:50 +00:00
if(empty($data['text'])){
throw new Exception(__("No data."));
}
2016-12-29 22:26:19 +00:00
$data['plain_text'] = $data['text'];
$data['text'] = self::parse_content($data['text']);
$data['datetime'] = 'NOW()';
$data['status'] = '1';
$data['id'] = DB::get_instance()->insert('posts', $data)->last_id();
$data['datetime'] = date("d M Y H:i");
unset($data['plain_text']);
return $data;
2016-12-27 20:25:32 +00:00
}
public static function update($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-29 22:26:19 +00:00
$data = self::raw_data($r);
$data['plain_text'] = $data['text'];
$data['text'] = self::parse_content($data['text']);
DB::get_instance()->update('posts', $data, "WHERE `id` = ? AND `status` = 1", $r["id"]);
2016-12-27 20:25:32 +00:00
2016-12-29 22:26:19 +00:00
unset($data['plain_text']);
return $data;
2016-12-27 20:25:32 +00:00
}
public static function hide($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
DB::get_instance()->query("UPDATE `posts` SET `status` = 4 WHERE `id` = ?", $r["id"]);
2016-12-29 22:26:19 +00:00
return true;
2016-12-27 20:25:32 +00:00
}
public static function delete($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
DB::get_instance()->query("UPDATE `posts` SET `status` = 5 WHERE `id` = ?", $r["id"]);
2016-12-29 22:26:19 +00:00
return true;
2016-12-27 20:25:32 +00:00
}
public static function edit_data($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
2017-09-23 14:09:42 +00:00
return DB::get_instance()->query("SELECT `plain_text`, `feeling`, `persons`, `location`, `privacy`, `content_type`, `content` FROM `posts` WHERE `id` = ? AND `status` = 1", $r["id"])->first();
2016-12-27 20:25:32 +00:00
}
public static function get_date($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
$date = DB::get_instance()->query("SELECT DATE_FORMAT(`datetime`,'%Y %c %e %k %i') AS `date_format` FROM `posts` WHERE `id` = ? AND `status` = 1", $r["id"])->first("date_format");
$date = array_map("intval", explode(" ", $date));
$date[4] = floor($date[4]/10)*10;
return $date;
}
public static function set_date($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
$d = $r["date"];
$datetime = "{$d[0]}/{$d[1]}/{$d[2]} {$d[3]}:{$d[4]}";
DB::get_instance()->query("UPDATE `posts` SET `datetime` = ? WHERE `id` = ? AND `status` = 1", $datetime, $r["id"]);
2016-12-29 22:26:19 +00:00
return [ "datetime" => date("d M Y H:i", strtotime($datetime)) ];
2016-12-27 20:25:32 +00:00
}
public static function parse_link($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
$l = $r["link"];
preg_match('/^https?:\/\/([^:\/\s]+)([^\/\s]*\/)([^\.\s]+)\.(jpe?g|png|gif)((\?|\#)(.*))?$/i', $l, $img);
if($img){
return [
"valid" => true,
"content_type" => "img_link",
"content" => [
"src" => $l,
"host" => $img[1]
]
];
}
preg_match('/^https?:\/\/(www\.)?([^:\/\s]+)(.*)?$/i', $l, $url);
// Get content
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $l);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Proxycat/1.1)");
curl_setopt($ch, CURLOPT_REFERER, '');
$html = curl_exec($ch);
curl_close($ch);
// Parse
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html);
// Get title
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
// Content
$content = [
"link" => $l,
"title" => ($title ? $title : $url[2]),
"is_video" => false,
"host" => $url[2]
];
// Metas
$metas = $doc->getElementsByTagName('meta');
for($i = 0; $i < $metas->length; $i++){
$meta = $metas->item($i);
$n = $meta->getAttribute('name');
$p = $meta->getAttribute('property');
$c = $meta->getAttribute('content');
2016-12-28 13:30:21 +00:00
if($n == 'twitter:description' || $p == 'og:description' || $n == 'description'){
2016-12-27 20:25:32 +00:00
$content["desc"] = substr($c, 0, 180);
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
2016-12-28 13:30:21 +00:00
if($n == 'twitter:title' || $p == 'og:title' || $p == 'title'){
2016-12-27 20:25:32 +00:00
$content["title"] = $c;
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
2016-12-28 13:30:21 +00:00
if($p == 'og:url'){
2016-12-27 20:25:32 +00:00
$content["link"] = $c;
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
2016-12-28 13:30:21 +00:00
if($p == 'og:type'){
2016-12-27 20:25:32 +00:00
$content["is_video"] = ($c == "video");
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
2016-12-28 13:30:21 +00:00
if($n == 'twitter:image:src' || $p == 'og:image'){
2016-12-27 20:25:32 +00:00
$content["thumb"] = $c;
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
2016-12-28 13:30:21 +00:00
if($n == 'twitter:domain'){
2016-12-27 20:25:32 +00:00
$content["host"] = $c;
2016-12-28 13:30:21 +00:00
}
2016-12-27 20:25:32 +00:00
}
return [
"valid" => true,
"content_type" => "link",
"content" => $content
];
}
public static function upload_image($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2016-12-27 20:25:32 +00:00
2016-12-28 14:21:52 +00:00
return Image::upload($r["name"], $r["data"]);
2016-12-27 20:25:32 +00:00
}
public static function load($r){
$until = null;
2017-06-19 17:51:59 +00:00
if(preg_match("/^[0-9]{4}-[0-9]{2}$/", $r["filter"]["until"])){
2016-12-27 20:25:32 +00:00
$until = $r["filter"]["until"]."-01 00:00";
}
2017-06-19 17:51:59 +00:00
if(preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $r["filter"]["until"])){
$until = $r["filter"]["until"]." 23:59";
}
2016-12-27 20:25:32 +00:00
$id = null;
if($r["filter"]["id"]){
$id = intval($r["filter"]["id"]);
}
2017-06-19 17:51:59 +00:00
$tag = null;
if(preg_match("/^[A-Za-z0-9-_]+$/", $r["filter"]["tag"])){
$tag = '#'.$r["filter"]["tag"];
}
$loc = null;
if(preg_match("/^[^'\"]+$/", $r["filter"]["loc"])){
$loc = $r["filter"]["loc"];
}
$person = null;
if(preg_match("/^[^'\"]+$/", $r["filter"]["person"])){
$person = $r["filter"]["person"];
}
2016-12-27 20:25:32 +00:00
return DB::get_instance()->query(
2017-06-19 17:51:59 +00:00
"SELECT `id`, `text`, `feeling`, `persons`, `location`, `privacy`, `content_type`, `content`, DATE_FORMAT(`posts`.`datetime`,'%d %b %Y %H:%i') AS `datetime` ".
2016-12-27 20:25:32 +00:00
"FROM `posts` ".
"WHERE ".
2017-06-19 17:51:59 +00:00
(!User::is_logged_in() ? "`privacy` = 'public' AND " : "").
2016-12-27 20:25:32 +00:00
($until ? "`posts`.`datetime` < DATE_ADD('{$until}', INTERVAL +1 MONTH) AND " : "").
($id ? "`id` = {$id} AND " : "").
2017-06-19 17:51:59 +00:00
($tag ? "`plain_text` LIKE '%{$tag}%' AND " : "").
($loc ? "`location` LIKE '%{$loc}%' AND " : "").
($person ? "`persons` LIKE '%{$person}%' AND " : "").
2016-12-27 20:25:32 +00:00
"`status` = 1 ".
"ORDER BY `posts`.`datetime` DESC ".
"LIMIT ? OFFSET ?", $r["limit"], $r["offset"]
)->all();
}
public static function login($r){
2016-12-28 14:46:07 +00:00
return User::login($r["nick"], $r["pass"]);
2016-12-27 20:25:32 +00:00
}
2016-12-28 14:46:07 +00:00
public static function logout(){
return User::logout();
2016-12-27 20:25:32 +00:00
}
public static function handshake($r){
2016-12-28 14:46:07 +00:00
return ["logged_in" => User::is_logged_in()];
2016-12-27 20:25:32 +00:00
}
}