]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #1659 from annando/1506-ostatus-enclosures
authorfabrixxm <fabrix.xm@gmail.com>
Wed, 10 Jun 2015 07:23:42 +0000 (09:23 +0200)
committerfabrixxm <fabrix.xm@gmail.com>
Wed, 10 Jun 2015 07:23:42 +0000 (09:23 +0200)
Many OStatus improvements

include/bbcode.php
include/items.php
include/notifier.php
include/ostatus.php [new file with mode: 0644]
include/ostatus_conversation.php
include/salmon.php
mod/item.php

index 403e46b6c76854e79a694b67490a90e7e7ab1d49..6c5a400ddc307a069ac67a56a57f09c0fc668017 100644 (file)
@@ -13,9 +13,9 @@ function bb_map_location($match) {
        return str_replace($match[0],'<div class="map"  >' . generate_named_map($match[1]) . '</div>', $match[0]);
 }
 
-function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
+function bb_attachment($Text, $simplehtml = false, $tryoembed = true) {
        $Text = preg_replace_callback("/(.*?)\[attachment(.*?)\](.*?)\[\/attachment\]/ism",
-               function ($match) use ($plaintext, $tryoembed){
+               function ($match) use ($simplehtml, $tryoembed){
 
                        $attributes = $match[2];
 
@@ -83,7 +83,10 @@ function bb_attachment($Text, $plaintext = false, $tryoembed = true) {
                                $image = "";
                        }
 
-                       if ($plaintext)
+                       if ($simplehtml == 7)
+                               $text = sprintf('<a href="%s" title="%s" class="attachment thumbnail" rel="nofollow external">%s</a>',
+                                               $url, $title, $title);
+                       elseif (($simplehtml != 4) AND ($simplehtml != 0))
                                $text = sprintf('<a href="%s" target="_blank">%s</a><br>', $url, $title);
                        else {
                                $text = sprintf('<span class="type-%s">', $type);
@@ -871,7 +874,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
        }
 
        // Handle attached links or videos
-       $Text = bb_attachment($Text, ($simplehtml != 4) AND ($simplehtml != 0), $tryoembed);
+       $Text = bb_attachment($Text, $simplehtml, $tryoembed);
 
        $Text = str_replace(array("\r","\n"), array('<br />','<br />'), $Text);
 
index 3fe624b00124d7c876aeab840b8b9ecdce5f0cbb..0a52d49d8da7642d820a135d5d6b2ed2d609118a 100644 (file)
@@ -9,9 +9,11 @@ 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');
+//require_once('include/ostatus_conversation.php');
 require_once('include/threads.php');
 require_once('include/socgraph.php');
+require_once('include/plaintext.php');
+require_once('include/ostatus.php');
 require_once('mod/share.php');
 
 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0, $forpubsub = false) {
@@ -118,7 +120,7 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0,
        // Include answers to status.net posts in pubsub feeds
        if($forpubsub) {
                $sql_post_table = "INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` ";
-               $visibility = sprintf("OR (`item`.`network` = '%s' AND `thread`.`network`='%s')",
+               $visibility = sprintf("AND (`item`.`parent` = `item`.`id`) OR (`item`.`network` = '%s' AND `thread`.`network`='%s')",
                                        dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS));
                $date_field = "`received`";
                $sql_order = "`item`.`received` DESC";
@@ -884,22 +886,22 @@ function get_atom_elements($feed, $item, $contact = array()) {
                }
        }
 
-       // Search for ostatus conversation url
-       $links = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
-
-       if (is_array($links)) {
-               foreach ($links as $link) {
-                       $conversation = array_shift($link["attribs"]);
-
-                       if ($conversation["rel"] == "ostatus:conversation") {
-                               $res["ostatus_conversation"] = ostatus_convert_href($conversation["href"]);
-                               logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
-                       //} elseif ($conversation["rel"] == "alternate") {
-                       //      $res["plink"] = $conversation["href"];
-                       //      logger('get_atom_elements: found plink '.$res["plink"]);
-                       }
-               };
-       }
+//     // Search for ostatus conversation url
+//     $links = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
+//
+//     if (is_array($links)) {
+//             foreach ($links as $link) {
+//                     $conversation = array_shift($link["attribs"]);
+//
+//                     if ($conversation["rel"] == "ostatus:conversation") {
+//                             $res["ostatus_conversation"] = ostatus_convert_href($conversation["href"]);
+//                             logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
+//                     //} elseif ($conversation["rel"] == "alternate") {
+//                     //      $res["plink"] = $conversation["href"];
+//                     //      logger('get_atom_elements: found plink '.$res["plink"]);
+//                     }
+//             };
+//     }
 
        if (isset($contact["network"]) AND ($contact["network"] == NETWORK_FEED) AND $contact['fetch_further_information']) {
                $preview = "";
@@ -1137,14 +1139,14 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                        $arr['plink'] = ostatus_convert_href($arr['uri']);
        }
 
-       // if an OStatus conversation url was passed in, it is stored and then
-       // removed from the array.
-       $ostatus_conversation = null;
+//     // if an OStatus conversation url was passed in, it is stored and then
+//     // removed from the array.
+//     $ostatus_conversation = null;
 
-       if (isset($arr["ostatus_conversation"])) {
-               $ostatus_conversation = $arr["ostatus_conversation"];
-               unset($arr["ostatus_conversation"]);
-       }
+//     if (isset($arr["ostatus_conversation"])) {
+//             $ostatus_conversation = $arr["ostatus_conversation"];
+//             unset($arr["ostatus_conversation"]);
+//     }
 
        if(x($arr, 'gravity'))
                $arr['gravity'] = intval($arr['gravity']);
@@ -1385,7 +1387,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                                $arr['gravity'] = 0;
                        }
                        else {
-                               logger('item_store: item parent was not found - ignoring item');
+                               logger('item_store: item parent '.$arr['parent-uri'].' for '.$arr['uid'].' was not found - ignoring item');
                                return 0;
                        }
 
@@ -1524,8 +1526,8 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
        );
 
        // Complete ostatus threads
-       if ($ostatus_conversation)
-               complete_conversation($current_post, $ostatus_conversation);
+       //if ($ostatus_conversation)
+       //      complete_conversation($current_post, $ostatus_conversation);
 
        $arr['id'] = $current_post;
        $arr['parent'] = $parent_id;
@@ -2223,6 +2225,13 @@ function edited_timestamp_is_newer($existing, $update) {
  */
 
 function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) {
+       if ($contact['network'] === NETWORK_OSTATUS) {
+               if ($pass < 2) {
+                       logger("Consume OStatus messages ", LOGGER_DEBUG);
+                       ostatus_import($xml,$importer,$contact, $hub);
+               }
+               return;
+       }
 
        require_once('library/simplepie/simplepie.inc');
        require_once('include/contact_selectors.php');
@@ -4344,7 +4353,8 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
        if ($item['title'] != "")
                $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
 
-       $htmlbody = bbcode(bb_remove_share_information($htmlbody), false, false, 7);
+       //$htmlbody = bbcode(bb_remove_share_information($htmlbody), false, false, 7);
+       $htmlbody = bbcode($htmlbody, false, false, 7);
 
        $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
        $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
@@ -4396,11 +4406,17 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
 
        $tags = item_getfeedtags($item);
        if(count($tags)) {
-               foreach($tags as $t) {
-                       $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
-               }
+               foreach($tags as $t)
+                       if (($type != 'html') OR ($t[0] != "@"))
+                               $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
        }
 
+       //$o .= '<link rel="ostatus:conversation" href="'.xmlify($a->get_baseurl().'/display/'.$owner['nickname'].'/'.$item['parent']).'"/>'."\r\n";
+       //$o .= '<link rel="self" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
+       //$o .= '<link rel="edit" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
+
+       $o .= item_get_attachment($item);
+
        $o .= item_getfeedattach($item);
 
        $mentioned = get_mentions($item);
@@ -4578,6 +4594,28 @@ function item_getfeedtags($item) {
        return $ret;
 }
 
+function item_get_attachment($item) {
+       $o = "";
+       $siteinfo = get_attached_data($item["body"]);
+
+       switch($siteinfo["type"]) {
+               case 'link':
+                       $o = '<link rel="enclosure" href="'.xmlify($siteinfo["url"]).'" type="text/html; charset=UTF-8" length="" title="'.xmlify($siteinfo["title"]).'"/>'."\r\n";
+                       break;
+               case 'photo':
+                       $imgdata = get_photo_info($siteinfo["image"]);
+                       $o = '<link rel="enclosure" href="'.xmlify($siteinfo["image"]).'" type="'.$imgdata["mime"].'" length="'.$imgdata["size"].'"/>'."\r\n";
+                       break;
+               case 'video':
+                       $o = '<link rel="enclosure" href="'.xmlify($siteinfo["url"]).'" type="text/html; charset=UTF-8" length="" title="'.xmlify($siteinfo["title"]).'"/>'."\r\n";
+                       break;
+               default:
+                       break;
+       }
+
+       return $o;
+}
+
 function item_getfeedattach($item) {
        $ret = '';
        $arr = explode('[/attach],',$item['attach']);
@@ -4951,7 +4989,7 @@ function list_post_dates($uid, $wall) {
                return array();
 
        // Set the start and end date to the beginning of the month
-        $dnow = substr($dnow,0,8).'01';
+       $dnow = substr($dnow,0,8).'01';
        $dthen = substr($dthen,0,8).'01';
 
        $ret = array();
@@ -4967,7 +5005,7 @@ function list_post_dates($uid, $wall) {
                $str = day_translate(datetime_convert('','',$dnow,'F'));
                if(! $ret[$dyear])
                        $ret[$dyear] = array();
-               $ret[$dyear][] = array($str,$end_month,$start_month);
+               $ret[$dyear][] = array($str,$end_month,$start_month);
                $dnow = datetime_convert('','',$dnow . ' -1 month', 'Y-m-d');
        }
        return $ret;
@@ -5015,12 +5053,12 @@ function posted_date_widget($url,$uid,$wall) {
        if(! $visible_years)
                $visible_years = 5;
 
-        $ret = list_post_dates($uid,$wall);
+       $ret = list_post_dates($uid,$wall);
 
        if(! count($ret))
                return $o;
 
-        $cutoff_year = intval(datetime_convert('',date_default_timezone_get(),'now','Y')) - $visible_years;
+       $cutoff_year = intval(datetime_convert('',date_default_timezone_get(),'now','Y')) - $visible_years;
        $cutoff = ((array_key_exists($cutoff_year,$ret))? true : false);
 
        $o = replace_macros(get_markup_template('posted_date_widget.tpl'),array(
@@ -5030,7 +5068,7 @@ function posted_date_widget($url,$uid,$wall) {
                '$cutoff' => $cutoff,
                '$url' => $url,
                '$dates' => $ret,
-                '$showmore' => t('show more')
+               '$showmore' => t('show more')
 
        ));
        return $o;
index dc4e1a2394e7aab5ee5f1e42a6b4865288735ee3..a0fd713c2053709d645f8315e5af17fddf1e3cf1 100644 (file)
@@ -2,6 +2,7 @@
 require_once("boot.php");
 require_once('include/queue_fn.php');
 require_once('include/html2plain.php');
+require_once("include/Scrape.php");
 
 /*
  * This file was at one time responsible for doing all deliveries, but this caused
@@ -295,9 +296,9 @@ function notifier_run(&$argv, &$argc){
                        $conversant_str = dbesc($parent['contact-id']);
                        $recipients = array($parent['contact-id']);
 
-                       if (!$item['private'] AND $item['wall'] AND
-                               (strlen($item['allow_cid'].$item['allow_gid'].
-                                       $item['deny_cid'].$item['deny_gid']) == 0))
+                       if (!$target_item['private'] AND $target_item['wall'] AND
+                               (strlen($target_item['allow_cid'].$target_item['allow_gid'].
+                                       $target_item['deny_cid'].$target_item['deny_gid']) == 0))
                                $push_notify = true;
 
                        if ($parent['network'] == NETWORK_OSTATUS) {
@@ -305,15 +306,29 @@ function notifier_run(&$argv, &$argc){
 
                                $push_notify = true;
 
+                               // Send a salmon notification to every person we mentioned in the post
+                               // To-Do: Send a Salmon to every Friendica user in that thread
+                               $arr = explode(',',$target_item['tag']);
+                               foreach($arr as $x) {
+                                       logger('Checking tag '.$x, LOGGER_DEBUG);
+                                       $matches = null;
+                                       if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
+                                               $probed_contact = probe_url($matches[1]);
+                                               if ($probed_contact["notify"] != "") {
+                                                       logger('scrape data for slapper: '.print_r($probed_contact, true));
+                                                       $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
+                                               }
+                                       }
+                               }
+
+/*
                                // Check if the recipient isn't in your contact list, try to slap it
                                // Not sure if it is working or not.
                                $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", $parent['contact-id']);
                                if (count($r)) {
-                                       $url_recipients = array();
 
                                        $thrparent = q("SELECT `author-link` FROM `item` WHERE `uri` = '%s'", dbesc($target_item["thr-parent"]));
                                        if (count($thrparent) AND (normalise_link($r[0]["url"]) != normalise_link($thrparent[0]["author-link"]))) {
-                                               require_once("include/Scrape.php");
                                                $probed_contact = probe_url($thrparent[0]["author-link"]);
                                                if ($probed_contact["notify"] != "") {
                                                        logger('scrape data for slapper: '.print_r($probed_contact, true));
@@ -321,7 +336,9 @@ function notifier_run(&$argv, &$argc){
                                                }
                                        }
                                }
-                               logger("url_recipients".print_r($url_recipients,true));
+*/
+                               if (count($url_recipients))
+                                       logger("url_recipients ".print_r($url_recipients,true));
                        }
                } else {
                        $followup = false;
@@ -372,7 +389,8 @@ function notifier_run(&$argv, &$argc){
                                }
                        }
 
-                       logger('notifier: url_recipients' . print_r($url_recipients,true));
+                       if (count($url_recipients))
+                               logger('notifier: url_recipients ' . print_r($url_recipients,true));
 
                        $conversants = array_unique($conversants);
 
@@ -911,7 +929,7 @@ function notifier_run(&$argv, &$argc){
 
        // send additional slaps to mentioned remote tags (@foo@example.com)
 
-       if($slap && count($url_recipients) && ($followup || $top_level) && $public_message && (! $expire)) {
+       if($slap && count($url_recipients) && ($followup || $top_level) && ($public_message || $push_notify) && (! $expire)) {
                if(! get_config('system','dfrn_only')) {
                        foreach($url_recipients as $url) {
                                if($url) {
diff --git a/include/ostatus.php b/include/ostatus.php
new file mode 100644 (file)
index 0000000..f57b148
--- /dev/null
@@ -0,0 +1,372 @@
+<?php
+require_once("mod/share.php");
+require_once('include/html2bbcode.php');
+require_once('include/enotify.php');
+require_once('include/items.php');
+require_once('include/ostatus_conversation.php');
+
+function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
+
+       $author = array();
+       $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
+       $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
+
+       // Preserve the value
+       $authorlink = $author["author-link"];
+
+       $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
+       if (is_object($alternate))
+               foreach($alternate AS $attributes)
+                       if ($attributes->name == "href")
+                               $author["author-link"] = $attributes->textContent;
+
+       $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
+               intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
+               dbesc(normalise_link($authorlink)), dbesc(NETWORK_STATUSNET));
+       if ($r) {
+               $contact = $r[0];
+               $author["contact-id"] = $r[0]["id"];
+       } else
+               $author["contact-id"] = $contact["id"];
+
+       $avatarlist = array();
+       $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
+       foreach($avatars AS $avatar) {
+               $href = "";
+               $width = 0;
+               foreach($avatar->attributes AS $attributes) {
+                       if ($attributes->name == "href")
+                               $href = $attributes->textContent;
+                       if ($attributes->name == "width")
+                               $width = $attributes->textContent;
+               }
+               if (($width > 0) AND ($href != ""))
+                       $avatarlist[$width] = $href;
+       }
+       if (count($avatarlist) > 0) {
+               krsort($avatarlist);
+               $author["author-avatar"] = current($avatarlist);
+       }
+
+       $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
+       if ($displayname != "")
+               $author["author-name"] = $displayname;
+
+       $author["owner-name"] = $author["author-name"];
+       $author["owner-link"] = $author["author-link"];
+       $author["owner-avatar"] = $author["author-avatar"];
+
+       return($author);
+}
+
+function ostatus_import($xml,$importer,&$contact, &$hub) {
+
+       // To-Do:
+       // Hub
+
+       $a = get_app();
+
+       logger("Import OStatus message", LOGGER_DEBUG);
+
+       if ($xml == "")
+               return;
+
+       $doc = new DOMDocument();
+       @$doc->loadXML($xml);
+
+       $xpath = new DomXPath($doc);
+       $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
+       $xpath->registerNamespace('thr', "http://purl.org/syndication/thread/1.0");
+       $xpath->registerNamespace('georss', "http://www.georss.org/georss");
+       $xpath->registerNamespace('activity', "http://activitystrea.ms/spec/1.0/");
+       $xpath->registerNamespace('media', "http://purl.org/syndication/atommedia");
+       $xpath->registerNamespace('poco', "http://portablecontacts.net/spec/1.0");
+       $xpath->registerNamespace('ostatus', "http://ostatus.org/schema/1.0");
+       $xpath->registerNamespace('statusnet', "http://status.net/schema/api/1/");
+
+       $gub = "";
+       $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
+       if (is_object($hub_attributes))
+               foreach($hub_attributes AS $hub_attribute)
+                       if ($hub_attribute->name == "href") {
+                               $hub = $hub_attribute->textContent;
+                               logger("Found hub ".$hub, LOGGER_DEBUG);
+                       }
+
+       $header = array();
+       $header["uid"] = $importer["uid"];
+       $header["network"] = NETWORK_OSTATUS;
+       $header["type"] = "remote";
+       $header["wall"] = 0;
+       $header["origin"] = 0;
+       $header["gravity"] = GRAVITY_PARENT;
+
+       // it could either be a received post or a post we fetched by ourselves
+       // depending on that, the first node is different
+       $first_child = $doc->firstChild->tagName;
+
+       if ($first_child == "feed")
+               $entries = $xpath->query('/atom:feed/atom:entry');
+       else
+               $entries = $xpath->query('/atom:entry');
+
+       $conversation = "";
+       $conversationlist = array();
+       $item_id = 0;
+
+       // Reverse the order of the entries
+       $entrylist = array();
+
+       foreach ($entries AS $entry)
+               $entrylist[] = $entry;
+
+       foreach (array_reverse($entrylist) AS $entry) {
+
+               $mention = false;
+
+               // fetch the author
+               if ($first_child == "feed")
+                       $author = ostatus_fetchauthor($xpath, $doc->firstChild, $importer, $contact);
+               else
+                       $author = ostatus_fetchauthor($xpath, $entry, $importer, $contact);
+
+               $item = array_merge($header, $author);
+
+               // Now get the item
+               $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
+
+               $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
+                       intval($importer["uid"]), dbesc($item["uri"]));
+               if ($r) {
+                       logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
+                       continue;
+               }
+
+               $item["body"] = add_page_info_to_body(html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue));
+               $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
+               $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
+
+               if ($item["verb"] == ACTIVITY_FOLLOW) {
+                       // ignore "Follow" messages
+                       continue;
+               }
+
+               if ($item["verb"] == ACTIVITY_FAVORITE) {
+                       // ignore "Favorite" messages
+                       continue;
+               }
+
+               $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
+               $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
+               $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
+
+               $related = "";
+
+               $inreplyto = $xpath->query('thr:in-reply-to', $entry);
+               if (is_object($inreplyto->item(0))) {
+                       foreach($inreplyto->item(0)->attributes AS $attributes) {
+                               if ($attributes->name == "ref")
+                                       $item["parent-uri"] = $attributes->textContent;
+                               if ($attributes->name == "href")
+                                       $related = $attributes->textContent;
+                       }
+               }
+
+               $georsspoint = $xpath->query('georss:point', $entry);
+               if ($georsspoint)
+                       $item["coord"] = $georsspoint->item(0)->nodeValue;
+
+               $categories = $xpath->query('atom:category', $entry);
+               if ($categories) {
+                       foreach ($categories AS $category) {
+                               foreach($category->attributes AS $attributes)
+                                       if ($attributes->name == "term") {
+                                               $term = $attributes->textContent;
+                                               if(strlen($item["tag"]))
+                                                       $item["tag"] .= ',';
+                                               $item["tag"] .= "#[url=".$a->get_baseurl()."/search?tag=".$term."]".$term."[/url]";
+                                       }
+                       }
+               }
+
+               $self = "";
+               $enclosure = "";
+
+               $links = $xpath->query('atom:link', $entry);
+               if ($links) {
+                       $rel = "";
+                       $href = "";
+                       $type = "";
+                       $length = "0";
+                       $title = "";
+                       foreach ($links AS $link) {
+                               foreach($link->attributes AS $attributes) {
+                                       if ($attributes->name == "href")
+                                               $href = $attributes->textContent;
+                                       if ($attributes->name == "rel")
+                                               $rel = $attributes->textContent;
+                                       if ($attributes->name == "type")
+                                               $type = $attributes->textContent;
+                                       if ($attributes->name == "length")
+                                               $length = $attributes->textContent;
+                                       if ($attributes->name == "title")
+                                               $title = $attributes->textContent;
+                               }
+                               if (($rel != "") AND ($href != ""))
+                                       switch($rel) {
+                                               case "alternate":
+                                                       $item["plink"] = $href;
+                                                       break;
+                                               case "ostatus:conversation":
+                                                       $conversation = $href;
+                                                       break;
+                                               case "enclosure":
+                                                       $enclosure = $href;
+                                                       if(strlen($item["attach"]))
+                                                               $item["attach"] .= ',';
+
+                                                       $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]';
+                                                       break;
+                                               case "related":
+                                                       if (!isset($item["parent-uri"]))
+                                                               $item["parent-uri"] = $href;
+
+                                                       if ($related == "")
+                                                               $related = $href;
+                                                       break;
+                                               case "self":
+                                                       $self = $href;
+                                                       break;
+                                               case "mentioned":
+                                                       // Notification check
+                                                       if ($importer["nurl"] == normalise_link($href))
+                                                               $mention = true;
+                                                       break;
+                                       }
+                       }
+               }
+
+               $local_id = "";
+               $repeat_of = "";
+
+               $notice_info = $xpath->query('statusnet:notice_info', $entry);
+               if ($notice_info)
+                       foreach($notice_info->item(0)->attributes AS $attributes) {
+                               if ($attributes->name == "source")
+                                       $item["app"] = strip_tags($attributes->textContent);
+                               if ($attributes->name == "local_id")
+                                       $local_id = $attributes->textContent;
+                               if ($attributes->name == "repeat_of")
+                                       $repeat_of = $attributes->textContent;
+                       }
+
+               // Is it a repeated post?
+               if ($repeat_of != "") {
+                       $activityobjects = $xpath->query('activity:object', $entry)->item(0);
+
+                       if (is_object($activityobjects)) {
+
+                               $orig_uris = $xpath->query("activity:object/atom:link[@rel='alternate']", $activityobjects);
+                               if ($orig_uris)
+                                       foreach($orig_uris->item(0)->attributes AS $attributes)
+                                               if ($attributes->name == "href")
+                                                       $orig_uri = $attributes->textContent;
+
+                               if (!isset($orig_uri))
+                                       $orig_uri = $xpath->query("atom:link[@rel='alternate']", $activityobjects)->item(0)->nodeValue;
+
+                               if (!isset($orig_uri))
+                                       $orig_uri = $xpath->query("activity:object/atom:id", $activityobjects)->item(0)->nodeValue;
+
+                               if (!isset($orig_uri))
+                                       $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
+
+                               $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
+                               $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
+
+                               $orig_contact = $contact;
+                               $orig_author = ostatus_fetchauthor($xpath, $activityobjects, $importer, $orig_contact);
+
+                               $prefix = share_header($orig_author['author-name'], $orig_author['author-link'], $orig_author['author-avatar'], "", $orig_created, $orig_uri);
+                               $item["body"] = $prefix.html2bbcode($orig_body)."[/share]";
+
+                               $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
+                               $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
+                       }
+               }
+
+               //if ($enclosure != "")
+               //      $item["body"] .= add_page_info($enclosure);
+
+               if (isset($item["parent-uri"])) {
+                       $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
+                               intval($importer["uid"]), dbesc($item["parent-uri"]));
+
+                       if (!$r AND ($related != "")) {
+                               $reply_path = str_replace("/notice/", "/api/statuses/show/", $related).".atom";
+
+                               if ($reply_path != $related) {
+                                       logger("Fetching related items for user ".$importer["uid"]." from ".$reply_path, LOGGER_DEBUG);
+                                       $reply_xml = fetch_url($reply_path);
+
+                                       $reply_contact = $contact;
+                                       ostatus_import($reply_xml,$importer,$reply_contact, $reply_hub);
+
+                                       // After the import try to fetch the parent item again
+                                       $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
+                                               intval($importer["uid"]), dbesc($item["parent-uri"]));
+                               }
+                       }
+                       if ($r) {
+                               $item["type"] = 'remote-comment';
+                               $item["gravity"] = GRAVITY_COMMENT;
+                       }
+               } else
+                       $item["parent-uri"] = $item["uri"];
+
+               $item_id = item_store($item);
+               //echo $xml;
+               //print_r($item);
+               //echo $item_id." ".$item["parent-uri"]."\n";
+
+               if (!$item_id) {
+                       logger("Error storing item ".print_r($item, true), LOGGER_DEBUG);
+                       continue;
+               }
+
+               logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
+               $item["id"] = $item_id;
+
+               if (!isset($item["parent"]) OR ($item["parent"] == 0))
+                       $item["parent"] = $item_id;
+
+               if ($mention) {
+                       $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
+
+                       notification(array(
+                               'type'         => NOTIFY_TAGSELF,
+                               'notify_flags' => $u[0]["notify-flags"],
+                               'language'     => $u[0]["language"],
+                               'to_name'      => $u[0]["username"],
+                               'to_email'     => $u[0]["email"],
+                               'uid'          => $item["uid"],
+                               'item'         => $item,
+                               'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
+                               'source_name'  => $item["author-name"],
+                               'source_link'  => $item["author-link"],
+                               'source_photo' => $item["author-avatar"],
+                               'verb'         => ACTIVITY_TAG,
+                               'otype'        => 'item',
+                               'parent'       => $item["parent"]
+                       ));
+               }
+
+               if ($conversation != "") {
+                       // Check for duplicates. We really don't need to check the same conversation twice.
+                       if (!in_array($conversation, $conversationlist)) {
+                               complete_conversation($item_id, $conversation);
+                               $conversationlist[] = $conversation;
+                       }
+               }
+       }
+}
index f40c4827b94759b8e416ce7181c9a6ce8a19b159..cbf0163b7cf5cbcd56f7dc4a6300139c6f0a8f18 100644 (file)
@@ -53,8 +53,9 @@ function check_conversations($override = false) {
         logger('cron_start');
 
         $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
-        $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
+        $conversations = q("SELECT `oid`, `url` FROM `term` WHERE `type` = 7 AND `term` > '%s' GROUP BY `url` ORDER BY `term` DESC",
                                 dbesc($start));
+
         foreach ($conversations AS $conversation) {
                 $id = $conversation['oid'];
                 $url = $conversation['url'];
@@ -137,13 +138,13 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                if (isset($single_conv->object->id))
                        $single_conv->id = $single_conv->object->id;
 
-               logger("Got id ".$single_conv->id, LOGGER_DEBUG);
+               //logger("Got id ".$single_conv->id, LOGGER_DEBUG);
 
                $plink = ostatus_convert_href($single_conv->id);
                if (isset($single_conv->object->url))
                        $plink = ostatus_convert_href($single_conv->object->url);
 
-               logger("Got url ".$plink, LOGGER_DEBUG);
+               //logger("Got url ".$plink, LOGGER_DEBUG);
 
                if (@!$single_conv->id)
                        continue;
@@ -151,12 +152,16 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                if ($first_id == "") {
                        $first_id = $single_conv->id;
 
+                       // To-Do:
+                       // Only fetch a new parent if the new one doesn't have parents
+                       // It can happen that OStatus servers have incomplete threads.
                        $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
                                intval($message["uid"]), dbesc($first_id),
                                dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
                        if ($new_parents) {
                                $parent = $new_parents[0];
-                               logger('adopting new parent '.$parent["id"].' for '.$itemid);
+                               if ($parent["id"] != $message["parent"])
+                                       logger('Fetch new parent id '.$parent["id"].' for '.$itemid.' Old parent: '.$message["parent"]);
                        } else {
                                $parent["id"] = 0;
                                $parent["uri"] = $first_id;
@@ -168,12 +173,12 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                else
                        $parent_uri = $parent["uri"];
 
-               $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+               $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
                                                        intval($message["uid"]), dbesc($plink),
                                                        dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
 
                if (!$message_exists)
-                       $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+                       $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
                                                        intval($message["uid"]), dbesc($single_conv->id),
                                                        dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
 
@@ -181,14 +186,17 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                        if ($parent["id"] != 0) {
                                $existing_message = $message_exists[0];
 
-                               logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
-
-                               // This is partly bad, since the entry in the thread table isn't updated
-                               $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
-                                       intval($parent["id"]),
-                                       dbesc($parent["uri"]),
-                                       dbesc($parent_uri),
-                                       intval($existing_message["id"]));
+                               // Normally this shouldn't happen anymore, since we improved the way we fetch OStatus messages
+                               if ($existing_message["parent"] != $parent["id"]) {
+                                       logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
+
+                                       // This is partly bad, since the entry in the thread table isn't updated
+                                       $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
+                                               intval($parent["id"]),
+                                               dbesc($parent["uri"]),
+                                               dbesc($parent_uri),
+                                               intval($existing_message["id"]));
+                               }
                        }
                        continue;
                }
@@ -241,7 +249,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                $arr["author-name"] = $arr["owner-name"];
                $arr["author-link"] = $actor;
                $arr["author-avatar"] = $single_conv->actor->image->url;
-               $arr["body"] = html2bbcode($single_conv->content);
+               $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->content));
 
                if (isset($single_conv->status_net->notice_info->source))
                        $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
@@ -273,6 +281,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                complete_conversation($newitem, $conversation_url, true);
 
                // If the newly created item is the top item then change the parent settings of the thread
+               // This shouldn't happen anymore. This could is supposed to be absolote.
                if ($newitem AND ($arr["uri"] == $first_id)) {
                        logger('setting new parent to id '.$newitem);
                        $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
index 3d525f51ad36d0a2d55349937c140e404f8b56ea..7574374907eabf3f4cca2789b5be712dfebadee5 100644 (file)
@@ -7,7 +7,7 @@ require_once('include/crypto.php');
 function get_salmon_key($uri,$keyhash) {
        $ret = array();
 
-       logger('Fetching salmon key');
+       logger('Fetching salmon key for '.$uri);
 
        $arr = lrdd($uri);
 
@@ -44,10 +44,10 @@ function get_salmon_key($uri,$keyhash) {
        if(count($ret) == 1) {
 
                // We only found one one key so we don't care if the hash matches.
-               // If it's the wrong key we'll find out soon enough because 
-               // message verification will fail. This also covers some older 
+               // If it's the wrong key we'll find out soon enough because
+               // message verification will fail. This also covers some older
                // software which don't supply a keyhash. As long as they only
-               // have one key we'll be right. 
+               // have one key we'll be right.
 
                return $ret[0];
        }
@@ -62,20 +62,20 @@ function get_salmon_key($uri,$keyhash) {
        return '';
 }
 
-       
-               
+
+
 function slapper($owner,$url,$slap) {
 
-       logger('slapper called. Data: ' . $slap);
+       logger('slapper called for '.$url.'. Data: ' . $slap);
 
-       // does contact have a salmon endpoint? 
+       // does contact have a salmon endpoint?
 
        if(! strlen($url))
                return;
 
 
        if(! $owner['sprvkey']) {
-               logger(sprintf("slapper: user '%s' (%d) does not have a salmon private key. Send failed.",
+               logger(sprintf("user '%s' (%d) does not have a salmon private key. Send failed.",
                $owner['username'],$owner['uid']));
                return;
        }
@@ -96,7 +96,7 @@ $namespaces = <<< EOT
 EOT;
 
        $slap = str_replace('<entry>',$namespaces,$slap);
-       
+
        // create a magic envelope
 
        $data      = base64url_encode($slap);
@@ -125,7 +125,7 @@ EOT;
                '$signature' => $signature
        ));
 
-       // slap them 
+       // slap them
        post_url($url,$salmon, array(
                'Content-type: application/magic-envelope+xml',
                'Content-length: ' . strlen($salmon)
@@ -138,7 +138,7 @@ EOT;
 
        if($return_code > 299) {
 
-               logger('slapper: compliant salmon failed. Falling back to status.net hack2');
+               logger('compliant salmon failed. Falling back to status.net hack2');
 
                // Entirely likely that their salmon implementation is
                // non-compliant. Let's try once more, this time only signing
@@ -152,7 +152,7 @@ EOT;
                        '$signature' => $signature2
                ));
 
-               // slap them 
+               // slap them
                post_url($url,$salmon, array(
                        'Content-type: application/magic-envelope+xml',
                        'Content-length: ' . strlen($salmon)
@@ -162,11 +162,11 @@ EOT;
 
                if($return_code > 299) {
 
-                       logger('slapper: compliant salmon failed. Falling back to status.net hack3');
+                       logger('compliant salmon failed. Falling back to status.net hack3');
 
                        // Entirely likely that their salmon implementation is
                        // non-compliant. Let's try once more, this time only signing
-                       // the data, without the precomputed blob 
+                       // the data, without the precomputed blob
 
                        $salmon = replace_macros($salmon_tpl,array(
                                '$data'      => $data,
@@ -176,7 +176,7 @@ EOT;
                                '$signature' => $signature3
                        ));
 
-                       // slap them 
+                       // slap them
                        post_url($url,$salmon, array(
                                'Content-type: application/magic-envelope+xml',
                                'Content-length: ' . strlen($salmon)
@@ -184,7 +184,7 @@ EOT;
                        $return_code = $a->get_curl_code();
                }
        }
-       logger('slapper returned ' . $return_code); 
+       logger('slapper for '.$url.' returned ' . $return_code);
        if(! $return_code)
                return(-1);
        if(($return_code == 503) && (stristr($a->get_curl_headers(),'retry-after')))
index 184920d4cef596c2dd4cbbd1693d07bad77204b4..a5f483b3524089f778a226b792fae04c845473de 100644 (file)
@@ -24,6 +24,7 @@ require_once('include/files.php');
 require_once('include/threads.php');
 require_once('include/text.php');
 require_once('include/items.php');
+require_once('include/Scrape.php');
 
 function item_post(&$a) {
 
@@ -128,28 +129,27 @@ function item_post(&$a) {
                                intval($parent_item['contact-id']),
                                intval($uid)
                        );
-                       if(count($r)) {
+                       if(count($r))
                                $parent_contact = $r[0];
 
-                               // If the contact id doesn't fit with the contact, then set the contact to null
-                               $thrparent = q("SELECT `author-link`, `network` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($thr_parent));
-                               if (count($thrparent) AND ($thrparent[0]["network"] === NETWORK_OSTATUS)
-                                       AND (normalise_link($parent_contact["url"]) != normalise_link($thrparent[0]["author-link"]))) {
-                                       $parent_contact = null;
-
-                                       require_once("include/Scrape.php");
-                                       $probed_contact = probe_url($thrparent[0]["author-link"]);
-                                       if ($probed_contact["network"] != NETWORK_FEED) {
-                                               $parent_contact = $probed_contact;
-                                               $parent_contact["nurl"] = normalise_link($probed_contact["url"]);
-                                               $parent_contact["thumb"] = $probed_contact["photo"];
-                                               $parent_contact["micro"] = $probed_contact["photo"];
-                                               $parent_contact["addr"] = $probed_contact["addr"];
-                                       }
-                                       logger('parent contact: '.print_r($parent_contact, true), LOGGER_DEBUG);
-                               } else
-                                       logger('no contact found: '.print_r($thrparent, true), LOGGER_DEBUG);
-                       }
+                       // If the contact id doesn't fit with the contact, then set the contact to null
+                       $thrparent = q("SELECT `author-link`, `network` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($thr_parent));
+                       if (count($thrparent) AND ($thrparent[0]["network"] === NETWORK_OSTATUS)
+                               AND (normalise_link($parent_contact["url"]) != normalise_link($thrparent[0]["author-link"]))) {
+                               $parent_contact = null;
+
+                               require_once("include/Scrape.php");
+                               $probed_contact = probe_url($thrparent[0]["author-link"]);
+                               if ($probed_contact["network"] != NETWORK_FEED) {
+                                       $parent_contact = $probed_contact;
+                                       $parent_contact["nurl"] = normalise_link($probed_contact["url"]);
+                                       $parent_contact["thumb"] = $probed_contact["photo"];
+                                       $parent_contact["micro"] = $probed_contact["photo"];
+                                       $parent_contact["addr"] = $probed_contact["addr"];
+                               }
+                               logger('no contact found: '.print_r($thrparent, true), LOGGER_DEBUG);
+                       } else
+                               logger('parent contact: '.print_r($parent_contact, true), LOGGER_DEBUG);
                }
        }
 
@@ -1097,8 +1097,30 @@ function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $netwo
        //is it a person tag?
        if(strpos($tag,'@') === 0) {
                //is it already replaced?
-               if(strpos($tag,'[url='))
+               if(strpos($tag,'[url=')) {
+                       //append tag to str_tags
+                       if(!stristr($str_tags,$tag)) {
+                               if(strlen($str_tags))
+                                       $str_tags .= ',';
+                               $str_tags .= $tag;
+                       }
+
+                       // Checking for the alias that is used for OStatus
+                       $pattern = "/@\[url\=(.*?)\](.*?)\[\/url\]/ism";
+                       if (preg_match($pattern, $tag, $matches)) {
+                               $data = probe_url($matches[1]);
+                               if ($data["alias"] != "") {
+                                       $newtag = '@[url='.$data["alias"].']'.$data["name"].'[/url]';
+                                       if(!stristr($str_tags,$newtag)) {
+                                               if(strlen($str_tags))
+                                                       $str_tags .= ',';
+                                               $str_tags .= $newtag;
+                                       }
+                               }
+                       }
+
                        return $replaced;
+               }
                $stat = false;
                //get the person's name
                $name = substr($tag,1);