]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Merge pull request #2965 from tobiasd/20161122-cpActiveResponse
[friendica.git] / include / items.php
index 93df36fb39dcdd7c07b0943bdba09fd25b956950..e9354b62d3eda2906753cb6641259640294ba856 100644 (file)
@@ -1,5 +1,11 @@
 <?php
 
+/**
+ * @file include/items.php
+ */
+
+use \Friendica\ParseUrl;
+
 require_once('include/bbcode.php');
 require_once('include/oembed.php');
 require_once('include/salmon.php');
@@ -147,19 +153,23 @@ function add_page_info_data($data) {
        // It maybe is a rich content, but if it does have everything that a link has,
        // then treat it that way
        if (($data["type"] == "rich") AND is_string($data["title"]) AND
-               is_string($data["text"]) AND (sizeof($data["images"]) > 0))
+               is_string($data["text"]) AND (sizeof($data["images"]) > 0)) {
                $data["type"] = "link";
+       }
 
-       if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $url))
-               return("");
+       if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $data["url"])) {
+               return "";
+       }
 
-       if ($no_photos AND ($data["type"] == "photo"))
-               return("");
+       if ($no_photos AND ($data["type"] == "photo")) {
+               return "";
+       }
 
-       if (sizeof($data["images"]) > 0)
+       if (sizeof($data["images"]) > 0) {
                $preview = $data["images"][0];
-       else
+       } else {
                $preview = "";
+       }
 
        // Escape some bad characters
        $data["url"] = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($data["url"], ENT_QUOTES, 'UTF-8', false));
@@ -167,19 +177,33 @@ function add_page_info_data($data) {
 
        $text = "[attachment type='".$data["type"]."'";
 
-       if ($data["url"] != "")
+       if ($data["text"] == "") {
+               $data["text"] = $data["title"];
+       }
+
+       if ($data["text"] == "") {
+               $data["text"] = $data["url"];
+       }
+
+       if ($data["url"] != "") {
                $text .= " url='".$data["url"]."'";
-       if ($data["title"] != "")
+       }
+
+       if ($data["title"] != "") {
                $text .= " title='".$data["title"]."'";
+       }
+
        if (sizeof($data["images"]) > 0) {
                $preview = str_replace(array("[", "]"), array("&#91;", "&#93;"), 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"]))
+               if (($data["images"][0]["width"] >= 500) AND ($data["images"][0]["width"] >= $data["images"][0]["height"])) {
                        $text .= " image='".$preview."'";
-               else
+               } else {
                        $text .= " preview='".$preview."'";
+               }
        }
+
        $text .= "]".$data["text"]."[/attachment]";
 
        $hashtags = "";
@@ -198,9 +222,8 @@ function add_page_info_data($data) {
 }
 
 function query_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
-       require_once("mod/parse_url.php");
 
-       $data = parseurl_getsiteinfo_cached($url, true);
+       $data = ParseUrl::getSiteinfoCached($url, true);
 
        if ($photo != "")
                $data["images"][0]["src"] = $photo;
@@ -341,14 +364,20 @@ function item_add_language_opt(&$arr) {
  * @brief Creates an unique guid out of a given uri
  *
  * @param string $uri uri of an item entry
+ * @param string $host (Optional) hostname for the GUID prefix
  * @return string unique guid
  */
-function uri_to_guid($uri) {
+function uri_to_guid($uri, $host = "") {
 
        // Our regular guid routine is using this kind of prefix as well
        // We have to avoid that different routines could accidentally create the same value
        $parsed = parse_url($uri);
-       $guid_prefix = hash("crc32", $parsed["host"]);
+
+       if ($host == "") {
+               $host = $parsed["host"];
+       }
+
+       $guid_prefix = hash("crc32", $host);
 
        // Remove the scheme to make sure that "https" and "http" doesn't make a difference
        unset($parsed["scheme"]);
@@ -363,6 +392,8 @@ function uri_to_guid($uri) {
 
 function item_store($arr,$force_parent = false, $notify = false, $dontcache = false) {
 
+       $a = get_app();
+
        // If it is a posting where users should get notifications, then define it as wall posting
        if ($notify) {
                $arr['wall'] = 1;
@@ -370,6 +401,15 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                $arr['origin'] = 1;
                $arr['last-child'] = 1;
                $arr['network'] = NETWORK_DFRN;
+
+               // We have to avoid duplicates. So we create the GUID in form of a hash of the plink or uri.
+               // In difference to the call to "uri_to_guid" several lines below we add the hash of our own host.
+               // This is done because our host is the original creator of the post.
+               if (isset($arr['plink'])) {
+                       $arr['guid'] = uri_to_guid($arr['plink'], $a->get_hostname());
+               } elseif (isset($arr['uri'])) {
+                       $arr['guid'] = uri_to_guid($arr['uri'], $a->get_hostname());
+               }
        }
 
        // If a Diaspora signature structure was passed in, pull it out of the
@@ -456,7 +496,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
 
        $arr['wall']          = ((x($arr,'wall'))          ? intval($arr['wall'])                : 0);
        $arr['guid']          = ((x($arr,'guid'))          ? notags(trim($arr['guid']))          : get_guid(32, $guid_prefix));
-       $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : $arr['guid']);
+       $arr['uri']           = ((x($arr,'uri'))           ? notags(trim($arr['uri']))           : item_new_uri($a->get_hostname(), $uid, $arr['guid']));
        $arr['extid']         = ((x($arr,'extid'))         ? notags(trim($arr['extid']))         : '');
        $arr['author-name']   = ((x($arr,'author-name'))   ? trim($arr['author-name'])   : '');
        $arr['author-link']   = ((x($arr,'author-link'))   ? notags(trim($arr['author-link']))   : '');
@@ -475,7 +515,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
        $arr['last-child']    = ((x($arr,'last-child'))    ? intval($arr['last-child'])          : 0 );
        $arr['visible']       = ((x($arr,'visible') !== false) ? intval($arr['visible'])         : 1 );
        $arr['deleted']       = 0;
-       $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : '');
+       $arr['parent-uri']    = ((x($arr,'parent-uri'))    ? notags(trim($arr['parent-uri']))    : $arr['uri']);
        $arr['verb']          = ((x($arr,'verb'))          ? notags(trim($arr['verb']))          : '');
        $arr['object-type']   = ((x($arr,'object-type'))   ? notags(trim($arr['object-type']))   : '');
        $arr['object']        = ((x($arr,'object'))        ? trim($arr['object'])                : '');
@@ -1878,21 +1918,21 @@ function drop_item($id,$interactive = true) {
 
        $owner = $item['uid'];
 
-       $cid = 0;
+       $contact_id = 0;
 
        // check if logged in user is either the author or owner of this item
 
        if (is_array($_SESSION['remote'])) {
                foreach($_SESSION['remote'] as $visitor) {
                        if ($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
-                               $cid = $visitor['cid'];
+                               $contact_id = $visitor['cid'];
                                break;
                        }
                }
        }
 
 
-       if ((local_user() == $item['uid']) || ($cid) || (! $interactive)) {
+       if ((local_user() == $item['uid']) || ($contact_id) || (! $interactive)) {
 
                // Check if we should do HTML-based delete confirmation
                if ($_REQUEST['confirm']) {