m1k1oblog/app/post.class.php

469 lines
12 KiB
PHP
Raw Permalink Normal View History

2016-12-27 20:25:32 +00:00
<?php
defined('PROJECT_PATH') OR exit('No direct script access allowed');
2016-12-27 20:25:32 +00:00
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
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
2019-12-20 17:38:48 +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);
2017-09-24 18:07:22 +00:00
$c = preg_replace("/\[code(?:=([^\[]+))?\]\s*?(?:\n|\r)?/i", '[code=$1]', $c);
$c = preg_replace("/\[\/code\]\s*?(?:\n|\r)?/i", '[/code]', $c);
2017-09-24 18:01:23 +00:00
// Add code definiton
$parser->addCodeDefinition(new class extends \JBBCode\CodeDefinition {
2017-09-24 18:43:58 +00:00
public function __construct(){
parent::__construct();
2017-09-24 18:01:23 +00:00
$this->setTagName("code");
$this->setParseContent(false);
$this->setUseOption(true);
}
2019-12-20 17:38:48 +00:00
2017-09-24 18:01:23 +00:00
public function asHtml(\JBBCode\ElementNode $el){
$content = $this->getContent($el);
2020-08-22 08:57:38 +00:00
$class = $el->getAttribute()['code'];
return '<code class="'.$class.'">'.htmlentities($content).'</code>';
2017-09-24 18:01:23 +00:00
}
});
2017-09-23 10:19:00 +00:00
}
2017-09-24 18:01:23 +00:00
2019-09-04 20:54:17 +00:00
// Custom tags
$builder = new JBBCode\CodeDefinitionBuilder("goal", "<div class=\"b_goal star\">{param}</div>");
$parser->addCodeDefinition($builder->build());
$builder = new JBBCode\CodeDefinitionBuilder("goal", "<div class=\"b_goal {option}\">{param}</div>");
$builder->setUseOption(true);
$parser->addCodeDefinition($builder->build());
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);
}
}
2019-12-20 17:38:48 +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);
2020-01-08 00:15:56 +00:00
$c = preg_replace('/(\#[A-Za-z0-9-_]+)(\s|$)/i', "<span class=\"tag\">\\1</span>\\2", $c);
2017-09-24 18:01:23 +00:00
$c = nl2br($c);
$textNode->setValue($c);
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +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
];
2019-12-20 17:38:48 +00:00
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];
}
}
2019-12-20 17:38:48 +00:00
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
}
2019-12-20 17:38:48 +00:00
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);
2019-12-20 17:38:48 +00:00
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';
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
$data['id'] = DB::get_instance()->insert('posts', $data)->last_id();
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
$data['datetime'] = date("d M Y H:i");
unset($data['plain_text']);
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
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);
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
$data['plain_text'] = $data['text'];
$data['text'] = self::parse_content($data['text']);
2019-12-20 17:38:48 +00:00
2020-02-07 23:19:24 +00:00
DB::get_instance()->update('posts', $data, "WHERE `id` = ? AND `status` <> 5", $r["id"]);
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
unset($data['plain_text']);
2019-12-20 17:38:48 +00:00
2016-12-29 22:26:19 +00:00
return $data;
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
public static function hide($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2019-12-20 17:38:48 +00:00
2020-02-07 23:19:24 +00:00
DB::get_instance()->query("
UPDATE `posts`
SET `status` = 4
WHERE `id` = ?
AND `status` <> 5
", $r["id"]);
return true;
}
public static function show($r){
self::login_protected();
DB::get_instance()->query("
UPDATE `posts`
SET `status` = 1
WHERE `id` = ?
AND `status` <> 5
", $r["id"]);
2016-12-29 22:26:19 +00:00
return true;
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
public static function delete($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2019-12-20 17:38:48 +00:00
2020-02-07 23:19:24 +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
}
2019-12-20 17:38:48 +00:00
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();
2019-12-20 17:38:48 +00:00
2020-02-07 23:19:24 +00:00
return DB::get_instance()->query("
SELECT `plain_text`, `feeling`, `persons`, `location`, `privacy`, `content_type`, `content`
FROM `posts`
WHERE `id` = ?
AND `status` <> 5
", $r["id"])->first();
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
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();
2019-12-20 17:38:48 +00:00
2020-06-10 23:47:02 +00:00
if (DB::connection() === 'sqlite') {
$datetime = "strftime('%Y %m %d %H %M', `posts`.`datetime`)";
2021-10-29 20:58:54 +00:00
} else if (DB::connection() === 'postgres') {
$datetime = "to_char(datetime,'YYYY MM DD HH24 MI')";
2020-06-10 23:47:02 +00:00
} else {
$datetime = "DATE_FORMAT(`datetime`,'%Y %c %e %k %i')";
}
2020-02-07 23:19:24 +00:00
$date = DB::get_instance()->query("
2020-06-10 23:47:02 +00:00
SELECT $datetime AS `date_format`
2020-02-07 23:19:24 +00:00
FROM `posts`
WHERE `id` = ?
AND `status` <> 5
", $r["id"])->first("date_format");
2016-12-27 20:25:32 +00:00
$date = array_map("intval", explode(" ", $date));
$date[4] = floor($date[4]/10)*10;
return $date;
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
public static function set_date($r){
2016-12-28 13:19:08 +00:00
self::login_protected();
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
$d = $r["date"];
2020-06-10 23:47:02 +00:00
if (DB::connection() === 'sqlite') {
$datetime = vsprintf("%04d-%02d-%02d %02d:%02d", $d);
} else {
$datetime = vsprintf("%04d/%02d/%02d %02d:%02d", $d);
}
2020-02-07 23:19:24 +00:00
DB::get_instance()->query("
UPDATE `posts`
SET `datetime` = ?
WHERE `id` = ?
AND `status` <> 5
", $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
}
2019-12-20 17:38:48 +00:00
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();
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
$l = $r["link"];
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
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]
]
];
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
preg_match('/^https?:\/\/(www\.)?([^:\/\s]+)(.*)?$/i', $l, $url);
2020-02-21 16:57:26 +00:00
$curl_request_url = $l;
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
// Get content
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
2020-02-28 22:38:54 +00:00
curl_setopt($ch, CURLOPT_ENCODING , "");
2016-12-27 20:25:32 +00:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Proxycat/1.1)");
curl_setopt($ch, CURLOPT_REFERER, '');
2020-03-27 00:36:07 +00:00
curl_setopt($ch, CURLOPT_TIMEOUT, 7); // 7sec
2020-02-21 12:54:56 +00:00
// Proxy settings
if($proxy = Config::get_safe("proxy", false)){
2020-02-21 16:57:26 +00:00
$proxytype = Config::get_safe("proxytype", false);
$proxyauth = Config::get_safe("proxyauth", false);
if($proxytype === 'URL_PREFIX'){
$curl_request_url = $proxy.$curl_request_url;
2020-02-21 12:54:56 +00:00
2020-02-21 16:57:26 +00:00
if($proxyauth){
curl_setopt($ch, CURLOPT_USERPWD, $proxyauth);
}
} else {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if($proxyauth){
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
}
2020-02-21 12:54:56 +00:00
switch ($proxytype) {
case 'CURLPROXY_SOCKS4':
$proxytype = CURLPROXY_SOCKS4;
break;
case 'CURLPROXY_SOCKS5':
$proxytype = CURLPROXY_SOCKS5;
break;
case 'CURLPROXY_HTTP':
default:
$proxytype = CURLPROXY_HTTP;
break;
}
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxytype);
}
}
2020-02-21 16:57:26 +00:00
curl_setopt($ch, CURLOPT_URL, $curl_request_url);
2016-12-27 20:25:32 +00:00
$html = curl_exec($ch);
curl_close($ch);
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
// Parse
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html);
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
// Get title
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
// Content
$content = [
"link" => $l,
"title" => ($title ? $title : $url[2]),
"is_video" => false,
"host" => $url[2]
];
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
// Metas
$metas = $doc->getElementsByTagName('meta');
for($i = 0; $i < $metas->length; $i++){
$meta = $metas->item($i);
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
$n = $meta->getAttribute('name');
$p = $meta->getAttribute('property');
$c = $meta->getAttribute('content');
2019-12-20 17:38:48 +00:00
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
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
if($p == 'og:type'){
2020-01-24 11:18:10 +00:00
$content["is_video"] = (preg_match("/video/", $c));
2016-12-28 13:30:21 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-28 13:30:21 +00:00
if($n == 'twitter:image:src' || $p == 'og:image'){
2020-02-28 23:00:48 +00:00
// Absolute url
if(preg_match("/^(https?:)?\/\//", $c)) {
$content["thumb"] = $c;
}
// Relative url from root
elseif(preg_match("/^\//", $c)) {
preg_match("/^((?:https?:)?\/\/([^\/]+))(\/|$)/", $l, $m);
$content["thumb"] = $m[1].'/'.$c;
}
// Relative url from current directory
else {
preg_match("/^((?:https?:)?\/\/[^\/]+.*?)(\/[^\/]*)?$/", $l, $m);
$content["thumb"] = $m[1].'/'.$c;
}
2016-12-28 13:30:21 +00:00
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
return [
"valid" => true,
"content_type" => "link",
"content" => $content
];
}
2019-12-20 17:38:48 +00:00
2019-12-18 23:18:50 +00:00
public static function upload_image(){
2016-12-28 13:19:08 +00:00
self::login_protected();
2019-12-20 17:38:48 +00:00
2019-12-18 23:18:50 +00:00
return Image::upload();
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
public static function load($r){
2020-01-23 21:33:07 +00:00
$from = [];
if(preg_match("/^[0-9]{4}-[0-9]{2}$/", @$r["filter"]["from"])){
$from = $r["filter"]["from"]."-01 00:00";
2016-12-27 20:25:32 +00:00
}
2019-12-20 17:33:54 +00:00
2020-01-23 21:33:07 +00:00
if(preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", @$r["filter"]["from"])){
$from = $r["filter"]["from"]." 00:00";
}
$to = [];
if(preg_match("/^[0-9]{4}-[0-9]{2}$/", @$r["filter"]["to"])){
$to = $r["filter"]["to"]."-01 00:00";
}
if(preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", @$r["filter"]["to"])){
$to = $r["filter"]["to"]." 00:00";
2017-06-19 17:51:59 +00:00
}
2019-12-20 17:33:54 +00:00
$id = [];
2019-12-23 19:33:00 +00:00
if(@$r["filter"]["id"]){
2016-12-27 20:25:32 +00:00
$id = intval($r["filter"]["id"]);
}
2019-12-20 17:38:48 +00:00
2019-12-20 17:33:54 +00:00
$tag = [];
2019-12-23 19:33:00 +00:00
if(preg_match("/^[A-Za-z0-9-_]+$/", @$r["filter"]["tag"])){
2017-06-19 17:51:59 +00:00
$tag = '#'.$r["filter"]["tag"];
}
2019-12-20 17:33:54 +00:00
$loc = [];
2019-12-23 19:33:00 +00:00
if(@$r["filter"]["loc"]){
2017-06-19 17:51:59 +00:00
$loc = $r["filter"]["loc"];
}
2019-12-20 17:33:54 +00:00
$person = [];
2019-12-23 19:33:00 +00:00
if(@$r["filter"]["person"]){
2017-06-19 17:51:59 +00:00
$person = $r["filter"]["person"];
}
2020-06-10 23:47:02 +00:00
if (DB::connection() === 'sqlite') {
$datetime = "strftime('%d %m %Y %H:%M', `posts`.`datetime`)";
2021-10-29 20:58:54 +00:00
} else if (DB::connection() === 'postgres') {
$datetime = "to_char(posts.datetime,'DD Mon YYYY HH24:MI')";
2020-06-10 23:47:02 +00:00
} else {
$datetime = "DATE_FORMAT(`posts`.`datetime`,'%d %b %Y %H:%i')";
}
2021-06-13 10:45:52 +00:00
$like_match = "LIKE ".DB::concat("'%'", "?", "'%'");
2020-02-07 23:19:24 +00:00
return DB::get_instance()->query("
SELECT
`id`, `text`, `feeling`, `persons`, `location`, `privacy`, `content_type`, `content`,
2020-06-10 23:47:02 +00:00
$datetime AS `datetime`, (`status` <> 1) AS `is_hidden`
2020-02-07 23:19:24 +00:00
FROM `posts`
WHERE ".
2017-09-25 18:43:27 +00:00
(!User::is_logged_in() ? (User::is_visitor() ? "`privacy` IN ('public', 'friends') AND " : "`privacy` = 'public' AND ") : "").
2020-01-23 21:33:07 +00:00
($from ? "`posts`.`datetime` > ? AND " : "").
($to ? "`posts`.`datetime` < ? AND " : "").
2019-12-20 17:33:54 +00:00
($id ? "`id` = ? AND " : "").
2021-06-13 10:45:52 +00:00
($tag ? "`plain_text` $like_match AND " : "").
($loc ? "`location` $like_match AND " : "").
($person ? "`persons` $like_match AND " : "").
2020-02-07 23:19:24 +00:00
"`status` <> 5
2023-01-24 17:02:48 +00:00
ORDER BY `posts`.`datetime` ".(@$r["sort"] == 'reverse' ? "ASC" : "DESC")."
2020-02-07 23:19:24 +00:00
LIMIT ? OFFSET ?
", $from, $to, $id, $tag, $loc, $person, $r["limit"], $r["offset"]
2016-12-27 20:25:32 +00:00
)->all();
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
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
}
2019-12-20 17:38:48 +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
}
2019-12-20 17:38:48 +00:00
2016-12-27 20:25:32 +00:00
public static function handshake($r){
2017-09-25 18:43:27 +00:00
return ["logged_in" => User::is_logged_in(), "is_visitor" => User::is_visitor()];
2016-12-27 20:25:32 +00:00
}
}