]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
The CSS code for shared messages is moved from "vier" to the global.css, so that...
[friendica.git] / include / items.php
index 6c438f463962bff56161192b0b6b5f2d4da972d2..27be613d70996e082360efee9b57ecc53eb8ead2 100755 (executable)
@@ -6,6 +6,7 @@ require_once('include/salmon.php');
 require_once('include/crypto.php');
 require_once('include/Photo.php');
 require_once('include/tags.php');
+require_once('include/files.php');
 require_once('include/text.php');
 require_once('include/email.php');
 require_once('include/ostatus_conversation.php');
@@ -51,6 +52,8 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $birthday = feed_birthday($owner_id,$owner['timezone']);
 
+       $sql_post_table = "";
+
        if(! $public_feed) {
 
                $sql_extra = '';
@@ -84,18 +87,18 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                $groups = init_groups_visitor($contact['id']);
 
                if(count($groups)) {
-                       for($x = 0; $x < count($groups); $x ++) 
+                       for($x = 0; $x < count($groups); $x ++)
                                $groups[$x] = '<' . intval($groups[$x]) . '>' ;
                        $gs = implode('|', $groups);
                }
                else
-                       $gs = '<<>>' ; // Impossible to match 
+                       $gs = '<<>>' ; // Impossible to match
 
-               $sql_extra = sprintf(" 
-                       AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
-                       AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
+               $sql_extra = sprintf("
+                       AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' )
+                       AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' )
                        AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
-                       AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
+                       AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s')
                ",
                        intval($contact['id']),
                        intval($contact['id']),
@@ -113,7 +116,9 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
                $last_update = 'now -30 days';
 
        if(isset($category)) {
-               $sql_extra .= file_tag_file_query('item',$category,'category');
+               $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
+                               dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($owner_id));
+               //$sql_extra .= file_tag_file_query('item',$category,'category');
        }
 
        if($public_feed) {
@@ -123,22 +128,25 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0)
 
        $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
 
+       //      AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
+       //      dbesc($check_date),
+
        $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
                `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
                `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
                `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
                `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
                `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-               FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid`
+               FROM `item` $sql_post_table
+               INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+               AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
                WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
-               AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-               AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
+               AND `item`.`wall` = 1 AND `item`.`changed` > '%s'
                $sql_extra
                ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
                intval($owner_id),
                dbesc($check_date),
-               dbesc($check_date),
                dbesc($sort)
        );
 
@@ -619,7 +627,7 @@ function get_atom_elements($feed, $item, $contact = array()) {
                $res['edited'] = unxmlify($rawedited[0]['data']);
 
        if((x($res,'edited')) && (! (x($res,'created'))))
-               $res['created'] = $res['edited']; 
+               $res['created'] = $res['edited'];
 
        if(! $res['created'])
                $res['created'] = $item->get_date('c');
@@ -848,9 +856,10 @@ function get_atom_elements($feed, $item, $contact = array()) {
        }
 
        if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND $contact['fetch_further_information']) {
-               $res["body"] = $res["title"]."\n\n[class=type-link]".fetch_siteinfo($res['plink'])."[/class]";
+               $res["body"] = $res["title"].add_page_info($res['plink']);
                $res["title"] = "";
-       }
+       } elseif (isset($contact["network"]) AND ($contact["network"] == NETWORK_OSTATUS))
+               $res["body"] = add_page_info_to_body($res["body"]);
 
        $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
 
@@ -866,29 +875,63 @@ function get_atom_elements($feed, $item, $contact = array()) {
        return $res;
 }
 
-function fetch_siteinfo($url) {
-       require_once("mod/parse_url.php");
+function add_page_info($url, $no_photos = false) {
+        require_once("mod/parse_url.php");
+        $data = parseurl_getsiteinfo($url, true);
+
+        logger('add_page_info: fetch page info for '.$url.' '.print_r($data, true), LOGGER_DEBUG);
 
-       // Fetch site infos - but only from the meta data
-       $data = parseurl_getsiteinfo($url, true);
+       // 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))
+               $data["type"] = "link";
 
-       $text = "";
+        if ((($data["type"] != "link") AND ($data["type"] != "video") AND ($data["type"] != "photo")) OR ($data["title"] == $url))
+                return("");
 
-       if (!is_string($data["text"]) AND (sizeof($data["images"]) == 0) AND ($data["title"] == $url))
+       if ($no_photos AND ($data["type"] == "photo"))
                return("");
 
-       if (is_string($data["title"]))
-               $text .= "[bookmark=".$url."]".trim($data["title"])."[/bookmark]\n";
+        if (($data["type"] != "photo") AND is_string($data["title"]))
+                $text .= "[bookmark=".$url."]".trim($data["title"])."[/bookmark]";
+
+        if (($data["type"] != "video") AND (sizeof($data["images"]) > 0)) {
+                $imagedata = $data["images"][0];
+                $text .= '[img]'.$imagedata["src"].'[/img]';
+        }
+
+        if (($data["type"] != "photo") AND is_string($data["text"]))
+                $text .= "[quote]".$data["text"]."[/quote]";
+
+        return("\n[class=type-".$data["type"]."]".$text."[/class]");
+}
+
+function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
+
+        logger('add_page_info_to_body: fetch page info for body '.$body, LOGGER_DEBUG);
+
+        $URLSearchString = "^\[\]";
+
+        // Adding these spaces is a quick hack due to my problems with regular expressions :)
+        preg_match("/[^!#@]\[url\]([$URLSearchString]*)\[\/url\]/ism", " ".$body, $matches);
 
-       if (sizeof($data["images"]) > 0) {
-               $imagedata = $data["images"][0];
-               $text .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
+        if (!$matches)
+                preg_match("/[^!#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", " ".$body, $matches);
+
+       // Convert urls without bbcode elements
+       if (!$matches AND $texturl) {
+               preg_match("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", " ".$body, $matches);
+
+               // Yeah, a hack. I really hate regular expressions :)
+               if ($matches)
+                       $matches[1] = $matches[2];
        }
 
-       if (is_string($data["text"]))
-               $text .= "[quote]".$data["text"]."[/quote]";
+        if ($matches)
+                $body .= add_page_info($matches[1], $no_photos);
 
-       return($text);
+        return $body;
 }
 
 function encode_rel_links($links) {
@@ -1161,7 +1204,6 @@ function item_store($arr,$force_parent = false) {
        if(count($r)) {
                $current_post = $r[0]['id'];
                logger('item_store: created item ' . $current_post);
-               create_tags_from_item($r[0]['id']);
 
                // Only check for notifications on start posts
                if ($arr['parent-uri'] === $arr['uri']) {
@@ -1240,7 +1282,6 @@ function item_store($arr,$force_parent = false) {
                intval($parent_deleted),
                intval($current_post)
        );
-       create_tags_from_item($current_post);
 
        // Complete ostatus threads
        if ($ostatus_conversation)
@@ -1304,17 +1345,20 @@ function item_store($arr,$force_parent = false) {
                        logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
                }
 
-        $r = q('SELECT * FROM `item` WHERE id = %d', intval($current_post));
-        if (count($r) == 1) {
-            call_hooks('post_remote_end', $r[0]);
-        }
-        else {
-            logger('item_store: new item not found in DB, id ' . $current_post);
-        }
+               $r = q('SELECT * FROM `item` WHERE id = %d', intval($current_post));
+               if (count($r) == 1) {
+                       call_hooks('post_remote_end', $r[0]);
+               } else {
+                       logger('item_store: new item not found in DB, id ' . $current_post);
+               }
        }
+
+       create_tags_from_item($current_post);
+       create_files_from_item($current_post);
+
        return $current_post;
 }
-
+// return - test
 function get_item_contact($item,$contacts) {
        if(! count($contacts) || (! is_array($item)))
                return false;
@@ -1762,7 +1806,7 @@ function edited_timestamp_is_newer($existing, $update) {
  * thing regardless of feed ordering. This won't be adequate in a fully-threaded
  * model where comments can have sub-threads. That would require some massive sorting
  * to get all the feed items into a mostly linear ordering, and might still require
- * recursion.  
+ * recursion.
  */
 
 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
@@ -2041,6 +2085,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($item['uri'], $importer['uid']);
+                                               create_files_from_itemuri($item['uri'], $importer['uid']);
                                                update_thread_uri($item['uri'], $importer['uid']);
                                        }
                                        else {
@@ -2053,6 +2098,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                        intval($importer['uid'])
                                                );
                                                create_tags_from_itemuri($uri, $importer['uid']);
+                                               create_files_from_itemuri($uri, $importer['uid']);
                                                if($item['last-child']) {
                                                        // ensure that last-child is set in case the comment that had it just got wiped.
                                                        q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
@@ -2159,11 +2205,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
@@ -2319,11 +2366,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['uid'])
                                                );
@@ -2948,6 +2996,7 @@ function local_delivery($importer,$data) {
                                                        intval($importer['importer_uid'])
                                                );
                                                create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
+                                               create_files_from_itemuri($item['uri'], $importer['importer_uid']);
                                                update_thread_uri($item['uri'], $importer['importer_uid']);
                                        }
                                        else {
@@ -2960,6 +3009,7 @@ function local_delivery($importer,$data) {
                                                        intval($importer['importer_uid'])
                                                );
                                                create_tags_from_itemuri($uri, $importer['importer_uid']);
+                                               create_files_from_itemuri($uri, $importer['importer_uid']);
                                                update_thread_uri($uri, $importer['importer_uid']);
                                                if($item['last-child']) {
                                                        // ensure that last-child is set in case the comment that had it just got wiped.
@@ -3078,11 +3128,12 @@ function local_delivery($importer,$data) {
                                                        continue;
 
                                                logger('received updated comment' , LOGGER_DEBUG);
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['importer_uid'])
                                                );
@@ -3164,9 +3215,10 @@ function local_delivery($importer,$data) {
                                                                        intval($importer['importer_uid'])
                                                                );
                                                                if(count($i) && ! intval($i[0]['blocktags'])) {
-                                                                       q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d",
+                                                                       q("UPDATE item SET tag = '%s', `edited` = '%s', `changed` = '%s' WHERE id = %d",
                                                                                dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag),
                                                                                intval($tagp[0]['id']),
+                                                                               dbesc(datetime_convert()),
                                                                                dbesc(datetime_convert())
                                                                        );
                                                                        create_tags_from_item($tagp[0]['id']);
@@ -3262,11 +3314,12 @@ function local_delivery($importer,$data) {
                                                if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                        continue;
 
-                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
+                                               $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                        dbesc($datarray['title']),
                                                        dbesc($datarray['body']),
                                                        dbesc($datarray['tag']),
                                                        dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                                       dbesc(datetime_convert()),
                                                        dbesc($item_id),
                                                        intval($importer['importer_uid'])
                                                );
@@ -3447,11 +3500,12 @@ function local_delivery($importer,$data) {
                                        if(datetime_convert('UTC','UTC',$datarray['edited']) < $r[0]['edited'])
                                                continue;
 
-                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
+                                       $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
                                                dbesc($datarray['title']),
                                                dbesc($datarray['body']),
                                                dbesc($datarray['tag']),
                                                dbesc(datetime_convert('UTC','UTC',$datarray['edited'])),
+                                               dbesc(datetime_convert()),
                                                dbesc($item_id),
                                                intval($importer['importer_uid'])
                                        );
@@ -3828,11 +3882,11 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
        $mentioned = get_mentions($item);
        if($mentioned)
                $o .= $mentioned;
-       
+
        call_hooks('atom_entry', $o);
 
        $o .= '</entry>' . "\r\n";
-       
+
        return $o;
 }
 
@@ -4021,7 +4075,7 @@ function item_getfeedattach($item) {
 }
 
 
-       
+
 function item_expire($uid,$days) {
 
        if((! $uid) || ($days < 1))
@@ -4181,6 +4235,7 @@ function drop_item($id,$interactive = true) {
                        intval($item['id'])
                );
                create_tags_from_item($item['id']);
+               create_files_from_item($item['id']);
                delete_thread($item['id']);
 
                // clean up categories and tags so they don't end up as orphans
@@ -4276,6 +4331,7 @@ function drop_item($id,$interactive = true) {
                                intval($item['uid'])
                        );
                        create_tags_from_item($item['parent-uri'], $item['uid']);
+                       create_files_from_item($item['parent-uri'], $item['uid']);
                        delete_thread_uri($item['parent-uri'], $item['uid']);
                        // ignore the result
                }