]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Merge pull request #2758 from annando/1609-sql-charset
[friendica.git] / include / items.php
index 4627b10ca29491955e1db5bef9c22c47901c728d..6d2c830f96ba8cf4a2c9ea63e59e1313371a7915 100644 (file)
@@ -18,6 +18,7 @@ require_once('include/Contact.php');
 require_once('mod/share.php');
 require_once('include/enotify.php');
 require_once('include/dfrn.php');
+require_once('include/group.php');
 
 require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
 
@@ -160,24 +161,31 @@ function add_page_info_data($data) {
        if ($no_photos AND ($data["type"] == "photo"))
                return("");
 
-       // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
-       if (strpos($data["url"], '[') OR strpos($data["url"], ']')) {
-               require_once("include/network.php");
-               $data["url"] = short_link($data["url"]);
-       }
-
-       if (($data["type"] != "photo") AND is_string($data["title"]))
-               $text .= "[bookmark=".$data["url"]."]".trim($data["title"])."[/bookmark]";
-
-       if (($data["type"] != "video") AND ($photo != ""))
-               $text .= '[img]'.$photo.'[/img]';
-       elseif (($data["type"] != "video") AND (sizeof($data["images"]) > 0)) {
-               $imagedata = $data["images"][0];
-               $text .= '[img]'.$imagedata["src"].'[/img]';
+       if (sizeof($data["images"]) > 0)
+               $preview = $data["images"][0];
+       else
+               $preview = "";
+
+       // Escape some bad characters
+       $data["url"] = str_replace(array("[", "]"), array("[", "]"), htmlentities($data["url"], ENT_QUOTES, 'UTF-8', false));
+       $data["title"] = str_replace(array("[", "]"), array("[", "]"), htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false));
+
+       $text = "[attachment type='".$data["type"]."'";
+
+       if ($data["url"] != "")
+               $text .= " url='".$data["url"]."'";
+       if ($data["title"] != "")
+               $text .= " title='".$data["title"]."'";
+       if (sizeof($data["images"]) > 0) {
+               $preview = str_replace(array("[", "]"), array("[", "]"), htmlentities($data["images"][0]["src"], ENT_QUOTES, 'UTF-8', false));
+               // if the preview picture is larger than 500 pixels then show it in a larger mode
+               // But only, if the picture isn't higher than large (To prevent huge posts)
+               if (($data["images"][0]["width"] >= 500) AND ($data["images"][0]["width"] >= $data["images"][0]["height"]))
+                       $text .= " image='".$preview."'";
+               else
+                       $text .= " preview='".$preview."'";
        }
-
-       if (($data["type"] != "photo") AND is_string($data["text"]))
-               $text .= "[quote]".$data["text"]."[/quote]";
+       $text .= "]".$data["text"]."[/attachment]";
 
        $hashtags = "";
        if (isset($data["keywords"]) AND count($data["keywords"])) {
@@ -191,7 +199,7 @@ function add_page_info_data($data) {
                }
        }
 
-       return("\n[class=type-".$data["type"]."]".$text."[/class]".$hashtags);
+       return "\n".$text.$hashtags;
 }
 
 function query_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
@@ -499,6 +507,13 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
        $arr['inform']        = ((x($arr,'inform'))        ? trim($arr['inform'])                : '');
        $arr['file']          = ((x($arr,'file'))          ? trim($arr['file'])                  : '');
 
+       // Items cannot be stored before they happen ...
+       if ($arr['created'] > datetime_convert())
+               $arr['created'] = datetime_convert();
+
+       // We haven't invented time travel by now.
+       if ($arr['edited'] > datetime_convert())
+               $arr['edited'] = datetime_convert();
 
        if (($arr['author-link'] == "") AND ($arr['owner-link'] == ""))
                logger("Both author-link and owner-link are empty. Called by: ".App::callstack(), LOGGER_DEBUG);
@@ -569,6 +584,12 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                                                                 "photo" => $arr['author-avatar'], "name" => $arr['author-name']));
        }
 
+       if ($arr["author-id"] == 0)
+               $arr["author-id"] = get_contact($arr["author-link"], 0);
+
+       if ($arr["owner-id"] == 0)
+               $arr["owner-id"] = get_contact($arr["owner-link"], 0);
+
        if ($arr['guid'] != "") {
                // Checking if there is already an item with the same guid
                logger('checking for an item for user '.$arr['uid'].' on network '.$arr['network'].' with the guid '.$arr['guid'], LOGGER_DEBUG);
@@ -896,7 +917,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
        check_item_notification($current_post, $uid);
 
        if ($notify)
-               proc_run('php', "include/notifier.php", $notify_type, $current_post);
+               proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $current_post);
 
        return $current_post;
 }
@@ -1135,7 +1156,7 @@ function tag_deliver($uid,$item_id) {
        );
        update_thread($item_id);
 
-       proc_run('php','include/notifier.php','tgroup',$item_id);
+       proc_run(PRIORITY_HIGH,'include/notifier.php', 'tgroup', $item_id);
 
 }
 
@@ -1260,17 +1281,17 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                logger("Consume DFRN messages", LOGGER_DEBUG);
 
                $r = q("SELECT  `contact`.*, `contact`.`uid` AS `importer_uid`,
-                                        `contact`.`pubkey` AS `cpubkey`,
-                                        `contact`.`prvkey` AS `cprvkey`,
-                                        `contact`.`thumb` AS `thumb`,
-                                        `contact`.`url` as `url`,
-                                        `contact`.`name` as `senderName`,
-                                        `user`.*
-                        FROM `contact`
-                        LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
-                        WHERE `contact`.`id` = %d AND `user`.`uid` = %d",
-                       dbesc($contact["id"]), dbesc($importer["uid"])
-               );
+                                       `contact`.`pubkey` AS `cpubkey`,
+                                       `contact`.`prvkey` AS `cprvkey`,
+                                       `contact`.`thumb` AS `thumb`,
+                                       `contact`.`url` as `url`,
+                                       `contact`.`name` as `senderName`,
+                                       `user`.*
+                       FROM `contact`
+                       LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
+                       WHERE `contact`.`id` = %d AND `user`.`uid` = %d",
+                       dbesc($contact["id"]), dbesc($importer["uid"])
+               );
                if ($r) {
                        logger("Now import the DFRN feed");
                        dfrn::import($xml,$r[0], true);
@@ -1378,21 +1399,13 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
                        dbesc(($sharing) ? NETWORK_ZOT : NETWORK_OSTATUS),
                        intval(($sharing) ? CONTACT_IS_SHARING : CONTACT_IS_FOLLOWER)
                );
-               $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
+               $r = q("SELECT `id`, `network` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
                                intval($importer['uid']),
                                dbesc($url)
                );
                if(count($r)) {
-                               $contact_record = $r[0];
-
-                               $photos = import_profile_photo($photo,$importer["uid"],$contact_record["id"]);
-
-                               q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `id` = %d",
-                                       dbesc($photos[0]),
-                                       dbesc($photos[1]),
-                                       dbesc($photos[2]),
-                                       intval($contact_record["id"])
-                               );
+                       $contact_record = $r[0];
+                       update_contact_avatar($photo, $importer["uid"], $contact_record["id"], true);
                }
 
 
@@ -1415,10 +1428,10 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) {
                                );
                        }
 
-                       if(intval($r[0]['def_gid'])) {
-                               require_once('include/group.php');
-                               group_add_member($r[0]['uid'],'',$contact_record['id'],$r[0]['def_gid']);
-                       }
+                       $def_gid = get_default_group($importer['uid'], $contact_record["network"]);
+
+                       if(intval($def_gid))
+                               group_add_member($importer['uid'],'',$contact_record['id'],$def_gid);
 
                        if(($r[0]['notify-flags'] & NOTIFY_INTRO) &&
                                in_array($r[0]['page-flags'], array(PAGE_NORMAL))) {
@@ -1648,7 +1661,6 @@ function compare_permissions($obj1,$obj2) {
 // returns an array of contact-ids that are allowed to see this object
 
 function enumerate_permissions($obj) {
-       require_once('include/group.php');
        $allow_people = expand_acl($obj['allow_cid']);
        $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
        $deny_people  = expand_acl($obj['deny_cid']);
@@ -1700,7 +1712,7 @@ function item_expire($uid, $days, $network = "", $force = false) {
        } else
                $range = "AND `created` < UTC_TIMESTAMP() - INTERVAL %d DAY ";
 
-       $r = q("SELECT * FROM `item`
+       $r = q("SELECT `file`, `resource-id`, `starred`, `type`, `id` FROM `item`
                WHERE `uid` = %d $range
                AND `id` = `parent`
                $sql_extra
@@ -1751,7 +1763,7 @@ function item_expire($uid, $days, $network = "", $force = false) {
                drop_item($item['id'],false);
        }
 
-       proc_run('php',"include/notifier.php","expire","$uid");
+       proc_run(PRIORITY_HIGH,"include/notifier.php", "expire", $uid);
 
 }
 
@@ -1773,7 +1785,7 @@ function drop_items($items) {
        // multiple threads may have been deleted, send an expire notification
 
        if($uid)
-               proc_run('php',"include/notifier.php","expire","$uid");
+               proc_run(PRIORITY_HIGH,"include/notifier.php", "expire", $uid);
 }
 
 
@@ -1986,7 +1998,7 @@ function drop_item($id,$interactive = true) {
 
                // send the notification upstream/downstream as the case may be
 
-               proc_run('php',"include/notifier.php","drop","$drop_id");
+               proc_run(PRIORITY_HIGH,"include/notifier.php", "drop", $drop_id);
 
                if(! $interactive)
                        return $owner;