3 * @file include/ostatus.php
7 use Friendica\Core\System;
8 use Friendica\Core\Config;
9 use Friendica\Network\Probe;
10 use Friendica\Util\Lock;
12 require_once 'include/Contact.php';
13 require_once 'include/threads.php';
14 require_once 'include/html2bbcode.php';
15 require_once 'include/bbcode.php';
16 require_once 'include/items.php';
17 require_once 'mod/share.php';
18 require_once 'include/enotify.php';
19 require_once 'include/socgraph.php';
20 require_once 'include/Photo.php';
21 require_once 'include/follow.php';
22 require_once 'include/api.php';
23 require_once 'mod/proxy.php';
24 require_once 'include/xml.php';
25 require_once 'include/cache.php';
28 * @brief This class contain functions for the OStatus protocol
33 private static $itemlist;
34 private static $conv_list = array();
37 * @brief Fetches author data
39 * @param object $xpath The xpath object
40 * @param object $context The xml context of the author details
41 * @param array $importer user record of the importing user
42 * @param array $contact Called by reference, will contain the fetched contact
43 * @param bool $onlyfetch Only fetch the header without updating the contact entries
45 * @return array Array of author related entries for the item
47 private static function fetchauthor($xpath, $context, $importer, &$contact, $onlyfetch) {
50 $author["author-link"] = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
51 $author["author-name"] = $xpath->evaluate('atom:author/atom:name/text()', $context)->item(0)->nodeValue;
52 $addr = $xpath->evaluate('atom:author/atom:email/text()', $context)->item(0)->nodeValue;
54 $aliaslink = $author["author-link"];
56 $alternate = $xpath->query("atom:author/atom:link[@rel='alternate']", $context)->item(0)->attributes;
57 if (is_object($alternate)) {
58 foreach ($alternate AS $attributes) {
59 if (($attributes->name == "href") && ($attributes->textContent != "")) {
60 $author["author-link"] = $attributes->textContent;
64 $author["contact-id"] = $contact["id"];
68 if ($aliaslink != '') {
69 $condition = array("`uid` = ? AND `alias` = ? AND `network` != ?",
70 $importer["uid"], $aliaslink, NETWORK_STATUSNET);
71 $r = dba::select('contact', array(), $condition, array('limit' => 1));
73 if (dbm::is_result($r)) {
79 $author["contact-id"] = $r["id"];
83 if (!$found && ($author["author-link"] != "")) {
84 if ($aliaslink == "") {
85 $aliaslink = $author["author-link"];
88 $condition = array("`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
89 normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET);
90 $r = dba::select('contact', array(), $condition, array('limit' => 1));
92 if (dbm::is_result($r)) {
98 $author["contact-id"] = $r["id"];
102 if (!$found && ($addr != "")) {
103 $condition = array("`uid` = ? AND `addr` = ? AND `network` != ?",
104 $importer["uid"], $addr, NETWORK_STATUSNET);
105 $r = dba::select('contact', array(), $condition, array('limit' => 1));
107 if (dbm::is_result($r)) {
113 $author["contact-id"] = $r["id"];
117 $avatarlist = array();
118 $avatars = $xpath->query("atom:author/atom:link[@rel='avatar']", $context);
119 foreach ($avatars AS $avatar) {
122 foreach ($avatar->attributes AS $attributes) {
123 if ($attributes->name == "href") {
124 $href = $attributes->textContent;
126 if ($attributes->name == "width") {
127 $width = $attributes->textContent;
131 $avatarlist[$width] = $href;
134 if (count($avatarlist) > 0) {
136 $author["author-avatar"] = Probe::fixAvatar(current($avatarlist), $author["author-link"]);
139 $displayname = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
140 if ($displayname != "") {
141 $author["author-name"] = $displayname;
144 $author["owner-name"] = $author["author-name"];
145 $author["owner-link"] = $author["author-link"];
146 $author["owner-avatar"] = $author["author-avatar"];
148 // Only update the contacts if it is an OStatus contact
149 if ($r && ($r['id'] > 0) && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
151 // This contact is vital, so we awake it from the dead
152 unmark_for_death($contact);
154 // Update contact data
157 unset($current['name-date']);
159 // This query doesn't seem to work
160 // $value = $xpath->query("atom:link[@rel='salmon']", $context)->item(0)->nodeValue;
162 // $contact["notify"] = $value;
164 // This query doesn't seem to work as well - I hate these queries
165 // $value = $xpath->query("atom:link[@rel='self' and @type='application/atom+xml']", $context)->item(0)->nodeValue;
167 // $contact["poll"] = $value;
169 $contact['url'] = $author["author-link"];
170 $contact['nurl'] = normalise_link($contact['url']);
172 $value = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
174 $contact["alias"] = $value;
176 $value = $xpath->evaluate('atom:author/poco:displayName/text()', $context)->item(0)->nodeValue;
178 $contact["name"] = $value;
180 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $context)->item(0)->nodeValue;
182 $contact["nick"] = $value;
184 $value = $xpath->evaluate('atom:author/poco:note/text()', $context)->item(0)->nodeValue;
186 $contact["about"] = html2bbcode($value);
188 $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
190 $contact["location"] = $value;
192 $contact['name-date'] = datetime_convert();
194 dba::update('contact', $contact, array('id' => $contact["id"]), $current);
196 if (!empty($author["author-avatar"]) && ($author["author-avatar"] != $current['avatar'])) {
197 logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
198 update_contact_avatar($author["author-avatar"], $importer["uid"], $contact["id"]);
201 // Ensure that we are having this contact (with uid=0)
202 $cid = get_contact($aliaslink, 0);
205 $fields = array('url', 'nurl', 'name', 'nick', 'alias', 'about', 'location');
206 $old_contact = dba::select('contact', $fields, array('id' => $cid), array('limit' => 1));
208 // Update it with the current values
209 $fields = array('url' => $author["author-link"], 'name' => $contact["name"],
210 'nurl' => normalise_link($author["author-link"]),
211 'nick' => $contact["nick"], 'alias' => $contact["alias"],
212 'about' => $contact["about"], 'location' => $contact["location"],
213 'success_update' => datetime_convert(), 'last-update' => datetime_convert());
215 dba::update('contact', $fields, array('id' => $cid), $old_contact);
218 update_contact_avatar($author["author-avatar"], 0, $cid);
221 $contact["generation"] = 2;
222 $contact["hide"] = false; // OStatus contacts are never hidden
223 $contact["photo"] = $author["author-avatar"];
224 $gcid = update_gcontact($contact);
226 link_gcontact($gcid, $contact["uid"], $contact["id"]);
233 * @brief Fetches author data from a given XML string
235 * @param string $xml The XML
236 * @param array $importer user record of the importing user
238 * @return array Array of author related entries for the item
240 public static function salmon_author($xml, $importer) {
245 $doc = new DOMDocument();
246 @$doc->loadXML($xml);
248 $xpath = new DomXPath($doc);
249 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
250 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
251 $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
252 $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
253 $xpath->registerNamespace('media', NAMESPACE_MEDIA);
254 $xpath->registerNamespace('poco', NAMESPACE_POCO);
255 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
256 $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
258 $entries = $xpath->query('/atom:entry');
260 foreach ($entries AS $entry) {
262 $author = self::fetchauthor($xpath, $entry, $importer, $contact, true);
268 * @brief Read attributes from element
270 * @param object $element Element object
272 * @return array attributes
274 private static function read_attributes($element) {
275 $attribute = array();
277 foreach ($element->attributes AS $attributes) {
278 $attribute[$attributes->name] = $attributes->textContent;
285 * @brief Imports an XML string containing OStatus elements
287 * @param string $xml The XML
288 * @param array $importer user record of the importing user
289 * @param array $contact
290 * @param string $hub Called by reference, returns the fetched hub data
292 public static function import($xml, $importer, &$contact, &$hub) {
293 self::process($xml, $importer, $contact, $hub);
297 * @brief Internal feed processing
299 * @param string $xml The XML
300 * @param array $importer user record of the importing user
301 * @param array $contact
302 * @param string $hub Called by reference, returns the fetched hub data
303 * @param boolean $stored Is the post fresh imported or from the database?
304 * @param boolean $initialize Is it the leading post so that data has to be initialized?
306 * @return boolean Could the XML be processed?
308 private static function process($xml, $importer, &$contact, &$hub, $stored = false, $initialize = true) {
310 self::$itemlist = array();
311 self::$conv_list = array();
314 logger("Import OStatus message", LOGGER_DEBUG);
319 $doc = new DOMDocument();
320 @$doc->loadXML($xml);
322 $xpath = new DomXPath($doc);
323 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
324 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
325 $xpath->registerNamespace('georss', NAMESPACE_GEORSS);
326 $xpath->registerNamespace('activity', NAMESPACE_ACTIVITY);
327 $xpath->registerNamespace('media', NAMESPACE_MEDIA);
328 $xpath->registerNamespace('poco', NAMESPACE_POCO);
329 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
330 $xpath->registerNamespace('statusnet', NAMESPACE_STATUSNET);
333 $hub_attributes = $xpath->query("/atom:feed/atom:link[@rel='hub']")->item(0)->attributes;
334 if (is_object($hub_attributes)) {
335 foreach ($hub_attributes AS $hub_attribute) {
336 if ($hub_attribute->name == "href") {
337 $hub = $hub_attribute->textContent;
338 logger("Found hub ".$hub, LOGGER_DEBUG);
344 $header["uid"] = $importer["uid"];
345 $header["network"] = NETWORK_OSTATUS;
346 $header["type"] = "remote";
348 $header["origin"] = 0;
349 $header["gravity"] = GRAVITY_PARENT;
351 $first_child = $doc->firstChild->tagName;
353 if ($first_child == "feed") {
354 $entries = $xpath->query('/atom:feed/atom:entry');
356 $entries = $xpath->query('/atom:entry');
359 if ($entries->length == 1) {
360 // We reformat the XML to make it better readable
361 $doc2 = new DOMDocument();
362 $doc2->loadXML($xml);
363 $doc2->preserveWhiteSpace = false;
364 $doc2->formatOutput = true;
365 $xml2 = $doc2->saveXML();
367 $header["protocol"] = PROTOCOL_OSTATUS_SALMON;
368 $header["source"] = $xml2;
369 } elseif (!$initialize) {
373 // Fetch the first author
374 $authordata = $xpath->query('//author')->item(0);
375 $author = self::fetchauthor($xpath, $authordata, $importer, $contact, $stored);
377 $entry = $xpath->query('/atom:entry');
379 // Reverse the order of the entries
380 $entrylist = array();
382 foreach ($entries AS $entry) {
383 $entrylist[] = $entry;
386 foreach (array_reverse($entrylist) AS $entry) {
388 $authorelement = $xpath->query('/atom:entry/atom:author', $entry);
390 if ($authorelement->length == 0) {
391 $authorelement = $xpath->query('atom:author', $entry);
394 if ($authorelement->length > 0) {
395 $author = self::fetchauthor($xpath, $entry, $importer, $contact, $stored);
398 $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()', $entry)->item(0)->nodeValue;
402 $nickname = $author["author-name"];
405 $item = array_merge($header, $author);
407 $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
409 $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
412 if (in_array($item["verb"], array('qvitter-delete-notice', ACTIVITY_DELETE, 'delete'))) {
413 self::deleteNotice($item);
417 if (in_array($item["verb"], array(NAMESPACE_OSTATUS."/unfavorite", ACTIVITY_UNFAVORITE))) {
418 // Ignore "Unfavorite" message
419 logger("Ignore unfavorite message ".print_r($item, true), LOGGER_DEBUG);
423 // Deletions come with the same uri, so we check for duplicates after processing deletions
424 if (dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]))) {
425 logger('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
428 logger('Processing post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
431 if ($item["verb"] == ACTIVITY_JOIN) {
432 // ignore "Join" messages
433 logger("Ignore join message ".print_r($item, true), LOGGER_DEBUG);
437 if ($item["verb"] == "http://mastodon.social/schema/1.0/block") {
438 // ignore mastodon "block" messages
439 logger("Ignore block message ".print_r($item, true), LOGGER_DEBUG);
443 if ($item["verb"] == ACTIVITY_FOLLOW) {
444 new_follower($importer, $contact, $item, $nickname);
448 if ($item["verb"] == NAMESPACE_OSTATUS."/unfollow") {
449 lose_follower($importer, $contact, $item, $dummy);
453 if ($item["verb"] == ACTIVITY_FAVORITE) {
454 $orig_uri = $xpath->query("activity:object/atom:id", $entry)->item(0)->nodeValue;
455 logger("Favorite ".$orig_uri." ".print_r($item, true));
457 $item["verb"] = ACTIVITY_LIKE;
458 $item["parent-uri"] = $orig_uri;
459 $item["gravity"] = GRAVITY_LIKE;
462 // http://activitystrea.ms/schema/1.0/rsvp-yes
463 if (!in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_LIKE, ACTIVITY_SHARE))) {
464 logger("Unhandled verb ".$item["verb"]." ".print_r($item, true), LOGGER_DEBUG);
467 self::processPost($xpath, $entry, $item, $importer);
469 if ($initialize && (count(self::$itemlist) > 0)) {
470 if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
471 // We will import it everytime, when it is started by our contacts
472 $valid = !empty(self::$itemlist[0]['contact-id']);
474 // If not, then it depends on this setting
475 $valid = !Config::get('system','ostatus_full_threads');
478 // Never post a thread when the only interaction by our contact was a like
480 $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
481 foreach (self::$itemlist AS $item) {
482 if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
488 // But we will only import complete threads
489 $valid = dba::exists('item', array('uid' => $importer["uid"], 'uri' => self::$itemlist[0]['parent-uri']));
493 $default_contact = 0;
494 $key = count(self::$itemlist);
495 for ($key = count(self::$itemlist) - 1; $key >= 0; $key--) {
496 if (empty(self::$itemlist[$key]['contact-id'])) {
497 self::$itemlist[$key]['contact-id'] = $default_contact;
499 $default_contact = $item['contact-id'];
502 foreach (self::$itemlist AS $item) {
503 $found = dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]));
505 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", LOGGER_DEBUG);
506 } elseif ($item['contact-id'] < 0) {
507 logger("Item with uri ".$item["uri"]." is from a blocked contact.", LOGGER_DEBUG);
509 // We are having duplicated entries. Hopefully this solves it.
510 if (Lock::set('ostatus_process_item_store')) {
511 $ret = item_store($item);
512 Lock::remove('ostatus_process_item_store');
513 logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
515 $ret = item_store($item);
516 logger("We couldn't lock - but tried to store the item anyway. Return value is ".$ret);
521 self::$itemlist = array();
523 logger('Processing done for post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
528 private static function deleteNotice($item) {
530 $condition = array('uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']);
531 $deleted = dba::select('item', array('id', 'parent-uri'), $condition, array('limit' => 1));
532 if (!dbm::is_result($deleted)) {
533 logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
537 // Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case
538 dba::update('item', array('deleted' => true, 'title' => '', 'body' => '',
539 'edited' => datetime_convert(), 'changed' => datetime_convert()),
540 array('id' => $deleted["id"]));
542 delete_thread($deleted["id"], $deleted["parent-uri"]);
544 logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
548 * @brief Processes the XML for a post
550 * @param object $xpath The xpath object
551 * @param object $entry The xml entry that is processed
552 * @param array $item The item array
553 * @param array $importer user record of the importing user
555 private static function processPost($xpath, $entry, &$item, $importer) {
556 $item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
557 $item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
558 if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
559 $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
560 $item["body"] = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
561 } elseif ($item["object-type"] == ACTIVITY_OBJ_QUESTION) {
562 $item["title"] = $xpath->query('atom:title/text()', $entry)->item(0)->nodeValue;
565 $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
566 $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
567 $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
568 $item['conversation-uri'] = $conversation;
570 $conv = $xpath->query('ostatus:conversation', $entry);
571 if (is_object($conv->item(0))) {
572 foreach ($conv->item(0)->attributes AS $attributes) {
573 if ($attributes->name == "ref") {
574 $item['conversation-uri'] = $attributes->textContent;
576 if ($attributes->name == "href") {
577 $item['conversation-href'] = $attributes->textContent;
584 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
585 if (is_object($inreplyto->item(0))) {
586 foreach ($inreplyto->item(0)->attributes AS $attributes) {
587 if ($attributes->name == "ref") {
588 $item["parent-uri"] = $attributes->textContent;
590 if ($attributes->name == "href") {
591 $related = $attributes->textContent;
596 $georsspoint = $xpath->query('georss:point', $entry);
597 if (!empty($georsspoint) && ($georsspoint->length > 0)) {
598 $item["coord"] = $georsspoint->item(0)->nodeValue;
601 $categories = $xpath->query('atom:category', $entry);
603 foreach ($categories AS $category) {
604 foreach ($category->attributes AS $attributes) {
605 if ($attributes->name == "term") {
606 $term = $attributes->textContent;
607 if (strlen($item["tag"])) {
610 $item["tag"] .= "#[url=".System::baseUrl()."/search?tag=".$term."]".$term."[/url]";
619 $links = $xpath->query('atom:link', $entry);
621 $link_data = self::processLinks($links, $item);
622 $self = $link_data['self'];
623 $add_body = $link_data['add_body'];
628 $notice_info = $xpath->query('statusnet:notice_info', $entry);
629 if ($notice_info && ($notice_info->length > 0)) {
630 foreach ($notice_info->item(0)->attributes AS $attributes) {
631 if ($attributes->name == "source") {
632 $item["app"] = strip_tags($attributes->textContent);
634 if ($attributes->name == "repeat_of") {
635 $repeat_of = $attributes->textContent;
639 // Is it a repeated post?
640 if (($repeat_of != "") || ($item["verb"] == ACTIVITY_SHARE)) {
641 $link_data = self::processRepeatedItem($xpath, $entry, $item, $importer);
642 if (!empty($link_data['add_body'])) {
643 $add_body .= $link_data['add_body'];
647 $item["body"] .= $add_body;
649 // Only add additional data when there is no picture in the post
650 if (!strstr($item["body"],'[/img]')) {
651 $item["body"] = add_page_info_to_body($item["body"]);
654 // Mastodon Content Warning
655 if (($item["verb"] == ACTIVITY_POST) && $xpath->evaluate('boolean(atom:summary)', $entry)) {
656 $clear_text = $xpath->query('atom:summary/text()', $entry)->item(0)->nodeValue;
658 $item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
661 if (($self != '') && empty($item['protocol'])) {
662 self::fetchSelf($self, $item);
665 if (!empty($item["conversation-href"])) {
666 self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
669 if (isset($item["parent-uri"]) && ($related != '')) {
670 if (!dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item['parent-uri']))) {
671 self::fetchRelated($related, $item["parent-uri"], $importer);
673 logger('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
676 $item["type"] = 'remote-comment';
677 $item["gravity"] = GRAVITY_COMMENT;
679 $item["parent-uri"] = $item["uri"];
682 if (($item['author-link'] != '') && !empty($item['protocol'])) {
683 $item = store_conversation($item);
686 self::$itemlist[] = $item;
690 * @brief Fetch the conversation for posts
692 * @param string $conversation The link to the conversation
693 * @param string $conversation_uri The conversation in "uri" format
695 private static function fetchConversation($conversation, $conversation_uri) {
697 // Ensure that we only store a conversation once in a process
698 if (isset(self::$conv_list[$conversation])) {
702 self::$conv_list[$conversation] = true;
704 $conversation_data = z_fetch_url($conversation, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
706 if (!$conversation_data['success']) {
712 if (stristr($conversation_data['header'], 'Content-Type: application/atom+xml')) {
713 $xml = $conversation_data['body'];
717 $doc = new DOMDocument();
718 if (!@$doc->loadHTML($conversation_data['body'])) {
721 $xpath = new DomXPath($doc);
723 $links = $xpath->query('//link');
725 foreach ($links AS $link) {
726 $attribute = ostatus::read_attributes($link);
727 if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
728 $file = $attribute['href'];
732 $conversation_atom = z_fetch_url($attribute['href']);
734 if ($conversation_atom['success']) {
735 $xml = $conversation_atom['body'];
745 self::storeConversation($xml, $conversation, $conversation_uri);
749 * @brief Store a feed in several conversation entries
751 * @param string $xml The feed
753 private static function storeConversation($xml, $conversation = '', $conversation_uri = '') {
754 $doc = new DOMDocument();
755 @$doc->loadXML($xml);
757 $xpath = new DomXPath($doc);
758 $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
759 $xpath->registerNamespace('thr', NAMESPACE_THREAD);
760 $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
762 $entries = $xpath->query('/atom:feed/atom:entry');
764 // Now store the entries
765 foreach ($entries AS $entry) {
766 $doc2 = new DOMDocument();
767 $doc2->preserveWhiteSpace = false;
768 $doc2->formatOutput = true;
770 $conv_data = array();
772 $conv_data['protocol'] = PROTOCOL_SPLITTED_CONV;
773 $conv_data['network'] = NETWORK_OSTATUS;
774 $conv_data['uri'] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
776 $inreplyto = $xpath->query('thr:in-reply-to', $entry);
777 if (is_object($inreplyto->item(0))) {
778 foreach ($inreplyto->item(0)->attributes AS $attributes) {
779 if ($attributes->name == "ref") {
780 $conv_data['reply-to-uri'] = $attributes->textContent;
785 $conv = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
786 $conv_data['conversation-uri'] = $conv;
788 $conv = $xpath->query('ostatus:conversation', $entry);
789 if (is_object($conv->item(0))) {
790 foreach ($conv->item(0)->attributes AS $attributes) {
791 if ($attributes->name == "ref") {
792 $conv_data['conversation-uri'] = $attributes->textContent;
794 if ($attributes->name == "href") {
795 $conv_data['conversation-href'] = $attributes->textContent;
800 if ($conversation != '') {
801 $conv_data['conversation-uri'] = $conversation;
804 if ($conversation_uri != '') {
805 $conv_data['conversation-uri'] = $conversation_uri;
808 $entry = $doc2->importNode($entry, true);
810 $doc2->appendChild($entry);
812 $conv_data['source'] = $doc2->saveXML();
814 $condition = array('item-uri' => $conv_data['uri'],'protocol' => PROTOCOL_OSTATUS_FEED);
815 if (dba::exists('conversation', $condition)) {
816 logger('Delete deprecated entry for URI '.$conv_data['uri'], LOGGER_DEBUG);
817 dba::delete('conversation', array('item-uri' => $conv_data['uri']));
820 logger('Store conversation data for uri '.$conv_data['uri'], LOGGER_DEBUG);
821 store_conversation($conv_data);
826 * @brief Fetch the own post so that it can be stored later
827 * @param array $item The item array
829 * We want to store the original data for later processing.
830 * This function is meant for cases where we process a feed with multiple entries.
831 * In that case we need to fetch the single posts here.
833 * @param string $self The link to the self item
835 private static function fetchSelf($self, &$item) {
836 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $self, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
837 if (dba::exists('conversation', $condition)) {
838 logger('Conversation '.$item['uri'].' is already stored.', LOGGER_DEBUG);
842 $self_data = z_fetch_url($self);
844 if (!$self_data['success']) {
848 // We reformat the XML to make it better readable
849 $doc = new DOMDocument();
850 $doc->loadXML($self_data['body']);
851 $doc->preserveWhiteSpace = false;
852 $doc->formatOutput = true;
853 $xml = $doc->saveXML();
855 $item["protocol"] = PROTOCOL_OSTATUS_SALMON;
856 $item["source"] = $xml;
858 logger('Conversation '.$item['uri'].' is now fetched.', LOGGER_DEBUG);
862 * @brief Fetch related posts and processes them
864 * @param string $related The link to the related item
865 * @param string $related_uri The related item in "uri" format
866 * @param array $importer user record of the importing user
868 private static function fetchRelated($related, $related_uri, $importer) {
869 $condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
870 $conversation = dba::select('conversation', array('source', 'protocol'), $condition, array('limit' => 1));
871 if (dbm::is_result($conversation)) {
873 $xml = $conversation['source'];
874 if (self::process($xml, $importer, $contact, $hub, $stored, false)) {
875 logger('Got valid cached XML for URI '.$related_uri, LOGGER_DEBUG);
878 if ($conversation['protocol'] == PROTOCOL_OSTATUS_SALMON) {
879 logger('Delete invalid cached XML for URI '.$related_uri, LOGGER_DEBUG);
880 dba::delete('conversation', array('item-uri' => $related_uri));
885 $related_data = z_fetch_url($related, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
887 if (!$related_data['success']) {
893 if (stristr($related_data['header'], 'Content-Type: application/atom+xml')) {
894 logger('Directly fetched XML for URI '.$related_uri, LOGGER_DEBUG);
895 $xml = $related_data['body'];
899 $doc = new DOMDocument();
900 if (!@$doc->loadHTML($related_data['body'])) {
903 $xpath = new DomXPath($doc);
907 $links = $xpath->query('//link');
909 foreach ($links AS $link) {
910 $attribute = self::read_attributes($link);
911 if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
912 $atom_file = $attribute['href'];
915 if ($atom_file != '') {
916 $related_atom = z_fetch_url($atom_file);
918 if ($related_atom['success']) {
919 logger('Fetched XML for URI '.$related_uri, LOGGER_DEBUG);
920 $xml = $related_atom['body'];
926 // Workaround for older GNU Social servers
927 if (($xml == '') && strstr($related, '/notice/')) {
928 $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related).'.atom');
930 if ($related_atom['success']) {
931 logger('GNU Social workaround to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
932 $xml = $related_atom['body'];
936 // Even more worse workaround for GNU Social ;-)
938 $related_guess = ostatus::convert_href($related_uri);
939 $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related_guess).'.atom');
941 if ($related_atom['success']) {
942 logger('GNU Social workaround 2 to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
943 $xml = $related_atom['body'];
947 // Finally we take the data that we fetched from "ostatus:conversation"
949 $condition = array('item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV);
950 $conversation = dba::select('conversation', array('source'), $condition, array('limit' => 1));
951 if (dbm::is_result($conversation)) {
953 logger('Got cached XML from conversation for URI '.$related_uri, LOGGER_DEBUG);
954 $xml = $conversation['source'];
959 self::process($xml, $importer, $contact, $hub, $stored, false);
961 logger("XML couldn't be fetched for URI: ".$related_uri." - href: ".$related, LOGGER_DEBUG);
967 * @brief Processes the XML for a repeated post
969 * @param object $xpath The xpath object
970 * @param object $entry The xml entry that is processed
971 * @param array $item The item array
972 * @param array $importer user record of the importing user
974 * @return array with data from links
976 private static function processRepeatedItem($xpath, $entry, &$item, $importer) {
977 $activityobjects = $xpath->query('activity:object', $entry)->item(0);
979 if (!is_object($activityobjects)) {
983 $link_data = array();
985 $orig_uri = $xpath->query('atom:id/text()', $activityobjects)->item(0)->nodeValue;
987 $links = $xpath->query("atom:link", $activityobjects);
989 $link_data = self::processLinks($links, $item);
992 $orig_body = $xpath->query('atom:content/text()', $activityobjects)->item(0)->nodeValue;
993 $orig_created = $xpath->query('atom:published/text()', $activityobjects)->item(0)->nodeValue;
994 $orig_edited = $xpath->query('atom:updated/text()', $activityobjects)->item(0)->nodeValue;
996 $orig_contact = $contact;
997 $orig_author = self::fetchauthor($xpath, $activityobjects, $importer, $orig_contact, false);
999 $item["author-name"] = $orig_author["author-name"];
1000 $item["author-link"] = $orig_author["author-link"];
1001 $item["author-avatar"] = $orig_author["author-avatar"];
1003 $item["body"] = html2bbcode($orig_body);
1004 $item["created"] = $orig_created;
1005 $item["edited"] = $orig_edited;
1007 $item["uri"] = $orig_uri;
1009 $item["verb"] = $xpath->query('activity:verb/text()', $activityobjects)->item(0)->nodeValue;
1011 $item["object-type"] = $xpath->query('activity:object-type/text()', $activityobjects)->item(0)->nodeValue;
1013 $inreplyto = $xpath->query('thr:in-reply-to', $activityobjects);
1014 if (is_object($inreplyto->item(0))) {
1015 foreach ($inreplyto->item(0)->attributes AS $attributes) {
1016 if ($attributes->name == "ref") {
1017 $item["parent-uri"] = $attributes->textContent;
1026 * @brief Processes links in the XML
1028 * @param object $links The xml data that contain links
1029 * @param array $item The item array
1031 * @return array with data from the links
1033 private static function processLinks($links, &$item) {
1034 $link_data = array('add_body' => '', 'self' => '');
1036 foreach ($links AS $link) {
1037 $attribute = self::read_attributes($link);
1039 if (($attribute['rel'] != "") && ($attribute['href'] != "")) {
1040 switch ($attribute['rel']) {
1042 $item["plink"] = $attribute['href'];
1043 if (($item["object-type"] == ACTIVITY_OBJ_QUESTION) ||
1044 ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
1045 $item["body"] .= add_page_info($attribute['href']);
1048 case "ostatus:conversation":
1049 $link_data['conversation'] = $attribute['href'];
1050 $item['conversation-href'] = $link_data['conversation'];
1051 if (!isset($item['conversation-uri'])) {
1052 $item['conversation-uri'] = $item['conversation-href'];
1056 $filetype = strtolower(substr($attribute['type'], 0, strpos($attribute['type'],'/')));
1057 if ($filetype == 'image') {
1058 $link_data['add_body'] .= "\n[img]".$attribute['href'].'[/img]';
1060 if (strlen($item["attach"])) {
1061 $item["attach"] .= ',';
1063 if (!isset($attribute['length'])) {
1064 $attribute['length'] = "0";
1066 $item["attach"] .= '[attach]href="'.$attribute['href'].'" length="'.$attribute['length'].'" type="'.$attribute['type'].'" title="'.$attribute['title'].'"[/attach]';
1070 if ($item["object-type"] != ACTIVITY_OBJ_BOOKMARK) {
1071 if (!isset($item["parent-uri"])) {
1072 $item["parent-uri"] = $attribute['href'];
1074 $link_data['related'] = $attribute['href'];
1076 $item["body"] .= add_page_info($attribute['href']);
1080 if ($item["plink"] == '') {
1081 $item["plink"] = $attribute['href'];
1083 $link_data['self'] = $attribute['href'];
1092 * @brief Create an url out of an uri
1094 * @param string $href URI in the format "parameter1:parameter1:..."
1096 * @return string URL in the format http(s)://....
1098 public static function convert_href($href) {
1099 $elements = explode(":",$href);
1101 if ((count($elements) <= 2) || ($elements[0] != "tag"))
1104 $server = explode(",", $elements[1]);
1105 $conversation = explode("=", $elements[2]);
1107 if ((count($elements) == 4) && ($elements[2] == "post"))
1108 return "http://".$server[0]."/notice/".$elements[3];
1110 if ((count($conversation) != 2) || ($conversation[1] =="")) {
1113 if ($elements[3] == "objectType=thread") {
1114 return "http://".$server[0]."/conversation/".$conversation[1];
1116 return "http://".$server[0]."/notice/".$conversation[1];
1122 * @brief Checks if the current post is a reshare
1124 * @param array $item The item array of thw post
1126 * @return string The guid if the post is a reshare
1128 private static function get_reshared_guid($item) {
1129 $body = trim($item["body"]);
1131 // Skip if it isn't a pure repeated messages
1132 // Does it start with a share?
1133 if (strpos($body, "[share") > 0)
1136 // Does it end with a share?
1137 if (strlen($body) > (strrpos($body, "[/share]") + 8))
1140 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
1141 // Skip if there is no shared message in there
1142 if ($body == $attributes)
1146 preg_match("/guid='(.*?)'/ism", $attributes, $matches);
1147 if ($matches[1] != "")
1148 $guid = $matches[1];
1150 preg_match('/guid="(.*?)"/ism', $attributes, $matches);
1151 if ($matches[1] != "")
1152 $guid = $matches[1];
1158 * @brief Cleans the body of a post if it contains picture links
1160 * @param string $body The body
1162 * @return string The cleaned body
1164 private static function format_picture_post($body) {
1165 $siteinfo = get_attached_data($body);
1167 if (($siteinfo["type"] == "photo")) {
1168 if (isset($siteinfo["preview"]))
1169 $preview = $siteinfo["preview"];
1171 $preview = $siteinfo["image"];
1173 // Is it a remote picture? Then make a smaller preview here
1174 $preview = proxy_url($preview, false, PROXY_SIZE_SMALL);
1176 // Is it a local picture? Then make it smaller here
1177 $preview = str_replace(array("-0.jpg", "-0.png"), array("-2.jpg", "-2.png"), $preview);
1178 $preview = str_replace(array("-1.jpg", "-1.png"), array("-2.jpg", "-2.png"), $preview);
1180 if (isset($siteinfo["url"]))
1181 $url = $siteinfo["url"];
1183 $url = $siteinfo["image"];
1185 $body = trim($siteinfo["text"])." [url]".$url."[/url]\n[img]".$preview."[/img]";
1192 * @brief Adds the header elements to the XML document
1194 * @param object $doc XML document
1195 * @param array $owner Contact data of the poster
1197 * @return object header root element
1199 private static function add_header($doc, $owner) {
1203 $root = $doc->createElementNS(NAMESPACE_ATOM1, 'feed');
1204 $doc->appendChild($root);
1206 $root->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1207 $root->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1208 $root->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1209 $root->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1210 $root->setAttribute("xmlns:poco", NAMESPACE_POCO);
1211 $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1212 $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1213 $root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1215 $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
1216 xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
1217 xml::add_element($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
1218 xml::add_element($doc, $root, "title", sprintf("%s timeline", $owner["name"]));
1219 xml::add_element($doc, $root, "subtitle", sprintf("Updates from %s on %s", $owner["name"], $a->config["sitename"]));
1220 xml::add_element($doc, $root, "logo", $owner["photo"]);
1221 xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
1223 $author = self::add_author($doc, $owner);
1224 $root->appendChild($author);
1226 $attributes = array("href" => $owner["url"], "rel" => "alternate", "type" => "text/html");
1227 xml::add_element($doc, $root, "link", "", $attributes);
1229 /// @TODO We have to find out what this is
1230 /// $attributes = array("href" => System::baseUrl()."/sup",
1231 /// "rel" => "http://api.friendfeed.com/2008/03#sup",
1232 /// "type" => "application/json");
1233 /// xml::add_element($doc, $root, "link", "", $attributes);
1235 self::hublinks($doc, $root, $owner["nick"]);
1237 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "salmon");
1238 xml::add_element($doc, $root, "link", "", $attributes);
1240 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-replies");
1241 xml::add_element($doc, $root, "link", "", $attributes);
1243 $attributes = array("href" => System::baseUrl()."/salmon/".$owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention");
1244 xml::add_element($doc, $root, "link", "", $attributes);
1246 $attributes = array("href" => System::baseUrl()."/api/statuses/user_timeline/".$owner["nick"].".atom",
1247 "rel" => "self", "type" => "application/atom+xml");
1248 xml::add_element($doc, $root, "link", "", $attributes);
1254 * @brief Add the link to the push hubs to the XML document
1256 * @param object $doc XML document
1257 * @param object $root XML root element where the hub links are added
1259 public static function hublinks($doc, $root, $nick) {
1260 $h = System::baseUrl() . '/pubsubhubbub/'.$nick;
1261 xml::add_element($doc, $root, "link", "", array("href" => $h, "rel" => "hub"));
1265 * @brief Adds attachement data to the XML document
1267 * @param object $doc XML document
1268 * @param object $root XML root element where the hub links are added
1269 * @param array $item Data of the item that is to be posted
1271 private static function get_attachment($doc, $root, $item) {
1273 $siteinfo = get_attached_data($item["body"]);
1275 switch ($siteinfo["type"]) {
1277 $imgdata = get_photo_info($siteinfo["image"]);
1278 $attributes = array("rel" => "enclosure",
1279 "href" => $siteinfo["image"],
1280 "type" => $imgdata["mime"],
1281 "length" => intval($imgdata["size"]));
1282 xml::add_element($doc, $root, "link", "", $attributes);
1285 $attributes = array("rel" => "enclosure",
1286 "href" => $siteinfo["url"],
1287 "type" => "text/html; charset=UTF-8",
1289 "title" => $siteinfo["title"]);
1290 xml::add_element($doc, $root, "link", "", $attributes);
1296 if (!Config::get('system', 'ostatus_not_attach_preview') && ($siteinfo["type"] != "photo") && isset($siteinfo["image"])) {
1297 $imgdata = get_photo_info($siteinfo["image"]);
1298 $attributes = array("rel" => "enclosure",
1299 "href" => $siteinfo["image"],
1300 "type" => $imgdata["mime"],
1301 "length" => intval($imgdata["size"]));
1303 xml::add_element($doc, $root, "link", "", $attributes);
1306 $arr = explode('[/attach],', $item['attach']);
1308 foreach ($arr as $r) {
1310 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|', $r, $matches);
1312 $attributes = array("rel" => "enclosure",
1313 "href" => $matches[1],
1314 "type" => $matches[3]);
1316 if (intval($matches[2])) {
1317 $attributes["length"] = intval($matches[2]);
1319 if (trim($matches[4]) != "") {
1320 $attributes["title"] = trim($matches[4]);
1322 xml::add_element($doc, $root, "link", "", $attributes);
1329 * @brief Adds the author element to the XML document
1331 * @param object $doc XML document
1332 * @param array $owner Contact data of the poster
1334 * @return object author element
1336 private static function add_author($doc, $owner) {
1338 $r = q("SELECT `homepage`, `publish` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"]));
1339 if (dbm::is_result($r)) {
1342 $author = $doc->createElement("author");
1343 xml::add_element($doc, $author, "id", $owner["url"]);
1344 xml::add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
1345 xml::add_element($doc, $author, "uri", $owner["url"]);
1346 xml::add_element($doc, $author, "name", $owner["nick"]);
1347 xml::add_element($doc, $author, "email", $owner["addr"]);
1348 xml::add_element($doc, $author, "summary", bbcode($owner["about"], false, false, 7));
1350 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]);
1351 xml::add_element($doc, $author, "link", "", $attributes);
1353 $attributes = array(
1355 "type" => "image/jpeg", // To-Do?
1356 "media:width" => 175,
1357 "media:height" => 175,
1358 "href" => $owner["photo"]);
1359 xml::add_element($doc, $author, "link", "", $attributes);
1361 if (isset($owner["thumb"])) {
1362 $attributes = array(
1364 "type" => "image/jpeg", // To-Do?
1365 "media:width" => 80,
1366 "media:height" => 80,
1367 "href" => $owner["thumb"]);
1368 xml::add_element($doc, $author, "link", "", $attributes);
1371 xml::add_element($doc, $author, "poco:preferredUsername", $owner["nick"]);
1372 xml::add_element($doc, $author, "poco:displayName", $owner["name"]);
1373 xml::add_element($doc, $author, "poco:note", bbcode($owner["about"], false, false, 7));
1375 if (trim($owner["location"]) != "") {
1376 $element = $doc->createElement("poco:address");
1377 xml::add_element($doc, $element, "poco:formatted", $owner["location"]);
1378 $author->appendChild($element);
1381 if (trim($profile["homepage"]) != "") {
1382 $urls = $doc->createElement("poco:urls");
1383 xml::add_element($doc, $urls, "poco:type", "homepage");
1384 xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
1385 xml::add_element($doc, $urls, "poco:primary", "true");
1386 $author->appendChild($urls);
1389 if (count($profile)) {
1390 xml::add_element($doc, $author, "followers", "", array("url" => System::baseUrl()."/viewcontacts/".$owner["nick"]));
1391 xml::add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"]));
1394 if ($profile["publish"]) {
1395 xml::add_element($doc, $author, "mastodon:scope", "public");
1401 * @TODO Picture attachments should look like this:
1402 * <a href="https://status.pirati.ca/attachment/572819" title="https://status.pirati.ca/file/heluecht-20151202T222602-rd3u49p.gif"
1403 * class="attachment thumbnail" id="attachment-572819" rel="nofollow external">https://status.pirati.ca/attachment/572819</a>
1408 * @brief Returns the given activity if present - otherwise returns the "post" activity
1410 * @param array $item Data of the item that is to be posted
1412 * @return string activity
1414 private static function construct_verb($item) {
1416 return $item['verb'];
1417 return ACTIVITY_POST;
1421 * @brief Returns the given object type if present - otherwise returns the "note" object type
1423 * @param array $item Data of the item that is to be posted
1425 * @return string Object type
1427 private static function construct_objecttype($item) {
1428 if (in_array($item['object-type'], array(ACTIVITY_OBJ_NOTE, ACTIVITY_OBJ_COMMENT)))
1429 return $item['object-type'];
1430 return ACTIVITY_OBJ_NOTE;
1434 * @brief Adds an entry element to the XML document
1436 * @param object $doc XML document
1437 * @param array $item Data of the item that is to be posted
1438 * @param array $owner Contact data of the poster
1439 * @param bool $toplevel
1441 * @return object Entry element
1443 private static function entry($doc, $item, $owner, $toplevel = false) {
1444 $repeated_guid = self::get_reshared_guid($item);
1445 if ($repeated_guid != "")
1446 $xml = self::reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel);
1451 if ($item["verb"] == ACTIVITY_LIKE) {
1452 return self::like_entry($doc, $item, $owner, $toplevel);
1453 } elseif (in_array($item["verb"], array(ACTIVITY_FOLLOW, NAMESPACE_OSTATUS."/unfollow"))) {
1454 return self::follow_entry($doc, $item, $owner, $toplevel);
1456 return self::note_entry($doc, $item, $owner, $toplevel);
1461 * @brief Adds a source entry to the XML document
1463 * @param object $doc XML document
1464 * @param array $contact Array of the contact that is added
1466 * @return object Source element
1468 private static function source_entry($doc, $contact) {
1469 $source = $doc->createElement("source");
1470 xml::add_element($doc, $source, "id", $contact["poll"]);
1471 xml::add_element($doc, $source, "title", $contact["name"]);
1472 xml::add_element($doc, $source, "link", "", array("rel" => "alternate",
1473 "type" => "text/html",
1474 "href" => $contact["alias"]));
1475 xml::add_element($doc, $source, "link", "", array("rel" => "self",
1476 "type" => "application/atom+xml",
1477 "href" => $contact["poll"]));
1478 xml::add_element($doc, $source, "icon", $contact["photo"]);
1479 xml::add_element($doc, $source, "updated", datetime_convert("UTC","UTC",$contact["success_update"]."+00:00",ATOM_TIME));
1485 * @brief Fetches contact data from the contact or the gcontact table
1487 * @param string $url URL of the contact
1488 * @param array $owner Contact data of the poster
1490 * @return array Contact array
1492 private static function contact_entry($url, $owner) {
1494 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` IN (0, %d) ORDER BY `uid` DESC LIMIT 1",
1495 dbesc(normalise_link($url)), intval($owner["uid"]));
1496 if (dbm::is_result($r)) {
1498 $contact["uid"] = -1;
1501 if (!dbm::is_result($r)) {
1502 $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
1503 dbesc(normalise_link($url)));
1504 if (dbm::is_result($r)) {
1506 $contact["uid"] = -1;
1507 $contact["success_update"] = $contact["updated"];
1511 if (!dbm::is_result($r))
1514 if (!isset($contact["poll"])) {
1515 $data = Probe::uri($url);
1516 $contact["poll"] = $data["poll"];
1518 if (!$contact["alias"])
1519 $contact["alias"] = $data["alias"];
1522 if (!isset($contact["alias"]))
1523 $contact["alias"] = $contact["url"];
1529 * @brief Adds an entry element with reshared content
1531 * @param object $doc XML document
1532 * @param array $item Data of the item that is to be posted
1533 * @param array $owner Contact data of the poster
1534 * @param $repeated_guid
1535 * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1537 * @return object Entry element
1539 private static function reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel) {
1541 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1542 logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1545 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1547 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1",
1548 intval($owner["uid"]), dbesc($repeated_guid),
1549 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
1550 if (dbm::is_result($r)) {
1551 $repeated_item = $r[0];
1555 $contact = self::contact_entry($repeated_item['author-link'], $owner);
1557 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1559 $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
1561 self::entry_content($doc, $entry, $item, $owner, $title, ACTIVITY_SHARE, false);
1563 $as_object = $doc->createElement("activity:object");
1565 xml::add_element($doc, $as_object, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA."activity");
1567 self::entry_content($doc, $as_object, $repeated_item, $owner, "", "", false);
1569 $author = self::add_author($doc, $contact);
1570 $as_object->appendChild($author);
1572 $as_object2 = $doc->createElement("activity:object");
1574 xml::add_element($doc, $as_object2, "activity:object-type", self::construct_objecttype($repeated_item));
1576 $title = sprintf("New comment by %s", $contact["nick"]);
1578 self::entry_content($doc, $as_object2, $repeated_item, $owner, $title);
1580 $as_object->appendChild($as_object2);
1582 self::entry_footer($doc, $as_object, $item, $owner, false);
1584 $source = self::source_entry($doc, $contact);
1586 $as_object->appendChild($source);
1588 $entry->appendChild($as_object);
1590 self::entry_footer($doc, $entry, $item, $owner);
1596 * @brief Adds an entry element with a "like"
1598 * @param object $doc XML document
1599 * @param array $item Data of the item that is to be posted
1600 * @param array $owner Contact data of the poster
1601 * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1603 * @return object Entry element with "like"
1605 private static function like_entry($doc, $item, $owner, $toplevel) {
1607 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1608 logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1611 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1613 $verb = NAMESPACE_ACTIVITY_SCHEMA."favorite";
1614 self::entry_content($doc, $entry, $item, $owner, "Favorite", $verb, false);
1616 $as_object = $doc->createElement("activity:object");
1618 $parent = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
1619 dbesc($item["thr-parent"]), intval($item["uid"]));
1620 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1622 xml::add_element($doc, $as_object, "activity:object-type", self::construct_objecttype($parent[0]));
1624 self::entry_content($doc, $as_object, $parent[0], $owner, "New entry");
1626 $entry->appendChild($as_object);
1628 self::entry_footer($doc, $entry, $item, $owner);
1634 * @brief Adds the person object element to the XML document
1636 * @param object $doc XML document
1637 * @param array $owner Contact data of the poster
1638 * @param array $contact Contact data of the target
1640 * @return object author element
1642 private static function add_person_object($doc, $owner, $contact) {
1644 $object = $doc->createElement("activity:object");
1645 xml::add_element($doc, $object, "activity:object-type", ACTIVITY_OBJ_PERSON);
1647 if ($contact['network'] == NETWORK_PHANTOM) {
1648 xml::add_element($doc, $object, "id", $contact['url']);
1652 xml::add_element($doc, $object, "id", $contact["alias"]);
1653 xml::add_element($doc, $object, "title", $contact["nick"]);
1655 $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $contact["url"]);
1656 xml::add_element($doc, $object, "link", "", $attributes);
1658 $attributes = array(
1660 "type" => "image/jpeg", // To-Do?
1661 "media:width" => 175,
1662 "media:height" => 175,
1663 "href" => $contact["photo"]);
1664 xml::add_element($doc, $object, "link", "", $attributes);
1666 xml::add_element($doc, $object, "poco:preferredUsername", $contact["nick"]);
1667 xml::add_element($doc, $object, "poco:displayName", $contact["name"]);
1669 if (trim($contact["location"]) != "") {
1670 $element = $doc->createElement("poco:address");
1671 xml::add_element($doc, $element, "poco:formatted", $contact["location"]);
1672 $object->appendChild($element);
1679 * @brief Adds a follow/unfollow entry element
1681 * @param object $doc XML document
1682 * @param array $item Data of the follow/unfollow message
1683 * @param array $owner Contact data of the poster
1684 * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1686 * @return object Entry element
1688 private static function follow_entry($doc, $item, $owner, $toplevel) {
1690 $item["id"] = $item["parent"] = 0;
1691 $item["created"] = $item["edited"] = date("c");
1692 $item["private"] = true;
1694 $contact = Probe::uri($item['follow']);
1696 if ($contact['alias'] == '') {
1697 $contact['alias'] = $contact["url"];
1699 $item['follow'] = $contact['alias'];
1702 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1703 intval($owner['uid']), dbesc(normalise_link($contact["url"])));
1705 if (dbm::is_result($r)) {
1706 $connect_id = $r[0]['id'];
1711 if ($item['verb'] == ACTIVITY_FOLLOW) {
1712 $message = t('%s is now following %s.');
1713 $title = t('following');
1714 $action = "subscription";
1716 $message = t('%s stopped following %s.');
1717 $title = t('stopped following');
1718 $action = "unfollow";
1721 $item["uri"] = $item['parent-uri'] = $item['thr-parent'] =
1722 'tag:'.get_app()->get_hostname().
1723 ','.date('Y-m-d').':'.$action.':'.$owner['uid'].
1724 ':person:'.$connect_id.':'.$item['created'];
1726 $item["body"] = sprintf($message, $owner["nick"], $contact["nick"]);
1728 self::entry_header($doc, $entry, $owner, $toplevel);
1730 self::entry_content($doc, $entry, $item, $owner, $title);
1732 $object = self::add_person_object($doc, $owner, $contact);
1733 $entry->appendChild($object);
1735 self::entry_footer($doc, $entry, $item, $owner);
1741 * @brief Adds a regular entry element
1743 * @param object $doc XML document
1744 * @param array $item Data of the item that is to be posted
1745 * @param array $owner Contact data of the poster
1746 * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1748 * @return object Entry element
1750 private static function note_entry($doc, $item, $owner, $toplevel) {
1752 if (($item["id"] != $item["parent"]) && (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
1753 logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
1756 $title = self::entry_header($doc, $entry, $owner, $toplevel);
1758 xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
1760 self::entry_content($doc, $entry, $item, $owner, $title);
1762 self::entry_footer($doc, $entry, $item, $owner);
1768 * @brief Adds a header element to the XML document
1770 * @param object $doc XML document
1771 * @param object $entry The entry element where the elements are added
1772 * @param array $owner Contact data of the poster
1773 * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
1775 * @return string The title for the element
1777 private static function entry_header($doc, &$entry, $owner, $toplevel) {
1778 /// @todo Check if this title stuff is really needed (I guess not)
1780 $entry = $doc->createElement("entry");
1781 $title = sprintf("New note by %s", $owner["nick"]);
1783 $entry = $doc->createElementNS(NAMESPACE_ATOM1, "entry");
1785 $entry->setAttribute("xmlns:thr", NAMESPACE_THREAD);
1786 $entry->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
1787 $entry->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
1788 $entry->setAttribute("xmlns:media", NAMESPACE_MEDIA);
1789 $entry->setAttribute("xmlns:poco", NAMESPACE_POCO);
1790 $entry->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
1791 $entry->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
1792 $entry->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
1794 $author = self::add_author($doc, $owner);
1795 $entry->appendChild($author);
1797 $title = sprintf("New comment by %s", $owner["nick"]);
1803 * @brief Adds elements to the XML document
1805 * @param object $doc XML document
1806 * @param object $entry Entry element where the content is added
1807 * @param array $item Data of the item that is to be posted
1808 * @param array $owner Contact data of the poster
1809 * @param string $title Title for the post
1810 * @param string $verb The activity verb
1811 * @param bool $complete Add the "status_net" element?
1813 private static function entry_content($doc, $entry, $item, $owner, $title, $verb = "", $complete = true) {
1816 $verb = self::construct_verb($item);
1818 xml::add_element($doc, $entry, "id", $item["uri"]);
1819 xml::add_element($doc, $entry, "title", $title);
1821 $body = self::format_picture_post($item['body']);
1823 if ($item['title'] != "")
1824 $body = "[b]".$item['title']."[/b]\n\n".$body;
1826 $body = bbcode($body, false, false, 7);
1828 xml::add_element($doc, $entry, "content", $body, array("type" => "html"));
1830 xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
1831 "href" => System::baseUrl()."/display/".$item["guid"]));
1833 if ($complete && ($item["id"] > 0))
1834 xml::add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
1836 xml::add_element($doc, $entry, "activity:verb", $verb);
1838 xml::add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
1839 xml::add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
1843 * @brief Adds the elements at the foot of an entry to the XML document
1845 * @param object $doc XML document
1846 * @param object $entry The entry element where the elements are added
1847 * @param array $item Data of the item that is to be posted
1848 * @param array $owner Contact data of the poster
1851 private static function entry_footer($doc, $entry, $item, $owner, $complete = true) {
1853 $mentioned = array();
1855 if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
1856 $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
1857 $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
1859 $thrparent = q("SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
1860 intval($owner["uid"]),
1861 dbesc($parent_item));
1863 $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
1864 $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
1865 $parent_plink = $thrparent[0]["plink"];
1867 $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
1868 $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
1869 $parent_plink = System::baseUrl()."/display/".$parent[0]["guid"];
1872 $attributes = array(
1873 "ref" => $parent_item,
1874 "href" => $parent_plink);
1875 xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
1877 $attributes = array(
1879 "href" => $parent_plink);
1880 xml::add_element($doc, $entry, "link", "", $attributes);
1883 if (intval($item["parent"]) > 0) {
1884 $conversation_href = System::baseUrl()."/display/".$owner["nick"]."/".$item["parent"];
1885 $conversation_uri = $conversation_href;
1887 if (isset($parent_item)) {
1888 $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $parent_item);
1889 if (dbm::is_result($r)) {
1890 if ($r['conversation-uri'] != '') {
1891 $conversation_uri = $r['conversation-uri'];
1893 if ($r['conversation-href'] != '') {
1894 $conversation_href = $r['conversation-href'];
1899 xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation_href));
1901 $attributes = array(
1902 "href" => $conversation_href,
1903 "local_id" => $item["parent"],
1904 "ref" => $conversation_uri);
1906 xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
1909 $tags = item_getfeedtags($item);
1912 foreach ($tags as $t)
1914 $mentioned[$t[1]] = $t[1];
1916 // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
1917 $newmentions = array();
1918 foreach ($mentioned AS $mention) {
1919 $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
1920 $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
1922 $mentioned = $newmentions;
1924 foreach ($mentioned AS $mention) {
1925 $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
1926 intval($owner["uid"]),
1927 dbesc(normalise_link($mention)));
1928 if ($r[0]["forum"] || $r[0]["prv"])
1929 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1930 "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
1931 "href" => $mention));
1933 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1934 "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
1935 "href" => $mention));
1938 if (!$item["private"]) {
1939 xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:attention",
1940 "href" => "http://activityschema.org/collection/public"));
1941 xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
1942 "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
1943 "href" => "http://activityschema.org/collection/public"));
1944 xml::add_element($doc, $entry, "mastodon:scope", "public");
1948 foreach ($tags as $t)
1950 xml::add_element($doc, $entry, "category", "", array("term" => $t[2]));
1952 self::get_attachment($doc, $entry, $item);
1954 if ($complete && ($item["id"] > 0)) {
1955 $app = $item["app"];
1959 $attributes = array("local_id" => $item["id"], "source" => $app);
1961 if (isset($parent["id"]))
1962 $attributes["repeat_of"] = $parent["id"];
1964 if ($item["coord"] != "")
1965 xml::add_element($doc, $entry, "georss:point", $item["coord"]);
1967 xml::add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
1972 * @brief Creates the XML feed for a given nickname
1974 * @param App $a The application class
1975 * @param string $owner_nick Nickname of the feed owner
1976 * @param string $last_update Date of the last update
1977 * @param integer $max_items Number of maximum items to fetch
1979 * @return string XML feed
1981 public static function feed(App $a, $owner_nick, &$last_update, $max_items = 300) {
1982 $stamp = microtime(true);
1984 $cachekey = "ostatus:feed:".$owner_nick.":".$last_update;
1986 $previous_created = $last_update;
1988 $result = Cache::get($cachekey);
1989 if (!is_null($result)) {
1990 logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created.' (cached)', LOGGER_DEBUG);
1991 $last_update = $result['last_update'];
1992 return $result['feed'];
1995 $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
1996 FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
1997 WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
1998 dbesc($owner_nick));
1999 if (!dbm::is_result($r)) {
2005 if (!strlen($last_update)) {
2006 $last_update = 'now -30 days';
2009 $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
2010 $authorid = get_contact($owner["url"], 0);
2012 $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
2013 STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
2014 WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
2015 `item`.`author-id` = %d AND `item`.`created` > '%s' AND
2016 NOT `item`.`deleted` AND NOT `item`.`private` AND
2017 `thread`.`network` IN ('%s', '%s')
2018 ORDER BY `item`.`created` DESC LIMIT %d",
2019 intval($owner["uid"]), intval($owner["id"]),
2020 intval($authorid), dbesc($check_date),
2021 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN), intval($max_items));
2023 $doc = new DOMDocument('1.0', 'utf-8');
2024 $doc->formatOutput = true;
2026 $root = self::add_header($doc, $owner);
2028 foreach ($items AS $item) {
2029 if (Config::get('system', 'ostatus_debug')) {
2030 $item['body'] .= '🍼';
2032 $entry = self::entry($doc, $item, $owner);
2033 $root->appendChild($entry);
2035 if ($last_update < $item['created']) {
2036 $last_update = $item['created'];
2040 $feeddata = trim($doc->saveXML());
2042 $msg = array('feed' => $feeddata, 'last_update' => $last_update);
2043 Cache::set($cachekey, $msg, CACHE_QUARTER_HOUR);
2045 logger('Feed duration: '.number_format(microtime(true) - $stamp, 3).' - '.$owner_nick.' - '.$previous_created, LOGGER_DEBUG);
2051 * @brief Creates the XML for a salmon message
2053 * @param array $item Data of the item that is to be posted
2054 * @param array $owner Contact data of the poster
2056 * @return string XML for the salmon
2058 public static function salmon($item,$owner) {
2060 $doc = new DOMDocument('1.0', 'utf-8');
2061 $doc->formatOutput = true;
2063 if (Config::get('system', 'ostatus_debug')) {
2064 $item['body'] .= '🐟';
2067 $entry = self::entry($doc, $item, $owner, true);
2069 $doc->appendChild($entry);
2071 return trim($doc->saveXML());