+ /**
+ * @brief Adds an entry element with reshared content
+ *
+ * @param object $doc XML document
+ * @param array $item Data of the item that is to be posted
+ * @param array $owner Contact data of the poster
+ * @param $repeated_guid
+ * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
+ *
+ * @return object Entry element
+ */
+ private function reshare_entry($doc, $item, $owner, $repeated_guid, $toplevel) {
+
+ if (($item["id"] != $item["parent"]) AND (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
+ logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
+ }
+
+ $title = self::entry_header($doc, $entry, $owner, $toplevel);
+
+ $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' AND NOT `private` AND `network` IN ('%s', '%s', '%s') LIMIT 1",
+ intval($owner["uid"]), dbesc($repeated_guid),
+ dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS));
+ if ($r)
+ $repeated_item = $r[0];
+ else
+ return false;
+
+ $contact = self::contact_entry($repeated_item['author-link'], $owner);
+
+ $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
+
+ $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
+
+ self::entry_content($doc, $entry, $item, $owner, $title, ACTIVITY_SHARE, false);
+
+ $as_object = $doc->createElement("activity:object");
+
+ xml::add_element($doc, $as_object, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA."activity");
+
+ self::entry_content($doc, $as_object, $repeated_item, $owner, "", "", false);
+
+ $author = self::add_author($doc, $contact);
+ $as_object->appendChild($author);
+
+ $as_object2 = $doc->createElement("activity:object");
+
+ xml::add_element($doc, $as_object2, "activity:object-type", self::construct_objecttype($repeated_item));
+
+ $title = sprintf("New comment by %s", $contact["nick"]);
+
+ self::entry_content($doc, $as_object2, $repeated_item, $owner, $title);
+
+ $as_object->appendChild($as_object2);
+
+ self::entry_footer($doc, $as_object, $item, $owner, false);
+
+ $source = self::source_entry($doc, $contact);
+
+ $as_object->appendChild($source);
+
+ $entry->appendChild($as_object);
+
+ self::entry_footer($doc, $entry, $item, $owner);
+
+ return $entry;
+ }
+
+ /**
+ * @brief Adds an entry element with a "like"
+ *
+ * @param object $doc XML document
+ * @param array $item Data of the item that is to be posted
+ * @param array $owner Contact data of the poster
+ * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
+ *
+ * @return object Entry element with "like"
+ */
+ private function like_entry($doc, $item, $owner, $toplevel) {
+
+ if (($item["id"] != $item["parent"]) AND (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
+ logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
+ }
+
+ $title = self::entry_header($doc, $entry, $owner, $toplevel);
+
+ $verb = NAMESPACE_ACTIVITY_SCHEMA."favorite";
+ self::entry_content($doc, $entry, $item, $owner, "Favorite", $verb, false);
+
+ $as_object = $doc->createElement("activity:object");
+
+ $parent = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
+ dbesc($item["thr-parent"]), intval($item["uid"]));
+ $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
+
+ xml::add_element($doc, $as_object, "activity:object-type", self::construct_objecttype($parent[0]));
+
+ self::entry_content($doc, $as_object, $parent[0], $owner, "New entry");
+
+ $entry->appendChild($as_object);
+
+ self::entry_footer($doc, $entry, $item, $owner);
+
+ return $entry;
+ }
+
+ /**
+ * @brief Adds a regular entry element
+ *
+ * @param object $doc XML document
+ * @param array $item Data of the item that is to be posted
+ * @param array $owner Contact data of the poster
+ * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
+ *
+ * @return object Entry element
+ */
+ private function note_entry($doc, $item, $owner, $toplevel) {
+
+ if (($item["id"] != $item["parent"]) AND (normalise_link($item["author-link"]) != normalise_link($owner["url"]))) {
+ logger("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", LOGGER_DEBUG);
+ }
+
+ $title = self::entry_header($doc, $entry, $owner, $toplevel);
+
+ xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+
+ self::entry_content($doc, $entry, $item, $owner, $title);
+
+ self::entry_footer($doc, $entry, $item, $owner);
+
+ return $entry;
+ }
+
+ /**
+ * @brief Adds a header element to the XML document
+ *
+ * @param object $doc XML document
+ * @param object $entry The entry element where the elements are added
+ * @param array $owner Contact data of the poster
+ * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
+ *
+ * @return string The title for the element
+ */
+ private function entry_header($doc, &$entry, $owner, $toplevel) {
+ /// @todo Check if this title stuff is really needed (I guess not)
+ if (!$toplevel) {
+ $entry = $doc->createElement("entry");
+ $title = sprintf("New note by %s", $owner["nick"]);
+ } else {
+ $entry = $doc->createElementNS(NAMESPACE_ATOM1, "entry");
+
+ $entry->setAttribute("xmlns:thr", NAMESPACE_THREAD);
+ $entry->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
+ $entry->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
+ $entry->setAttribute("xmlns:media", NAMESPACE_MEDIA);
+ $entry->setAttribute("xmlns:poco", NAMESPACE_POCO);
+ $entry->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
+ $entry->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
+
+ $author = self::add_author($doc, $owner);
+ $entry->appendChild($author);
+
+ $title = sprintf("New comment by %s", $owner["nick"]);
+ }
+ return $title;
+ }
+
+ /**
+ * @brief Adds elements to the XML document
+ *
+ * @param object $doc XML document
+ * @param object $entry Entry element where the content is added
+ * @param array $item Data of the item that is to be posted
+ * @param array $owner Contact data of the poster
+ * @param string $title Title for the post
+ * @param string $verb The activity verb
+ * @param bool $complete Add the "status_net" element?
+ */
+ private function entry_content($doc, $entry, $item, $owner, $title, $verb = "", $complete = true) {
+
+ if ($verb == "")
+ $verb = self::construct_verb($item);
+
+ xml::add_element($doc, $entry, "id", $item["uri"]);
+ xml::add_element($doc, $entry, "title", $title);
+
+ $body = self::format_picture_post($item['body']);
+
+ if ($item['title'] != "")
+ $body = "[b]".$item['title']."[/b]\n\n".$body;
+
+ $body = bbcode($body, false, false, 7);
+
+ xml::add_element($doc, $entry, "content", $body, array("type" => "html"));
+
+ xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
+ "href" => App::get_baseurl()."/display/".$item["guid"]));
+
+ if ($complete)
+ xml::add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
+
+ xml::add_element($doc, $entry, "activity:verb", $verb);
+
+ xml::add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
+ xml::add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
+ }
+
+ /**
+ * @brief Adds the elements at the foot of an entry to the XML document
+ *
+ * @param object $doc XML document
+ * @param object $entry The entry element where the elements are added
+ * @param array $item Data of the item that is to be posted
+ * @param array $owner Contact data of the poster
+ * @param $complete
+ */
+ private function entry_footer($doc, $entry, $item, $owner, $complete = true) {
+
+ $mentioned = array();
+
+ if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
+ $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
+ $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
+
+ $attributes = array(
+ "ref" => $parent_item,
+ "type" => "text/html",
+ "href" => App::get_baseurl()."/display/".$parent[0]["guid"]);
+ xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
+
+ $attributes = array(
+ "rel" => "related",
+ "href" => App::get_baseurl()."/display/".$parent[0]["guid"]);
+ xml::add_element($doc, $entry, "link", "", $attributes);
+
+ $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
+ $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
+
+ $thrparent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `uid` = %d AND `uri` = '%s'",
+ intval($owner["uid"]),
+ dbesc($parent_item));
+ if ($thrparent) {
+ $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"];
+ $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"];
+ }
+ }
+
+ xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation",
+ "href" => App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]));
+ xml::add_element($doc, $entry, "ostatus:conversation", App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]);
+
+ $tags = item_getfeedtags($item);
+
+ if(count($tags))
+ foreach($tags as $t)
+ if ($t[0] == "@")
+ $mentioned[$t[1]] = $t[1];
+
+ // Make sure that mentions are accepted (GNU Social has problems with mixing HTTP and HTTPS)
+ $newmentions = array();
+ foreach ($mentioned AS $mention) {
+ $newmentions[str_replace("http://", "https://", $mention)] = str_replace("http://", "https://", $mention);
+ $newmentions[str_replace("https://", "http://", $mention)] = str_replace("https://", "http://", $mention);
+ }
+ $mentioned = $newmentions;
+
+ foreach ($mentioned AS $mention) {
+ $r = q("SELECT `forum`, `prv` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s'",
+ intval($owner["uid"]),
+ dbesc(normalise_link($mention)));
+ if ($r[0]["forum"] OR $r[0]["prv"])
+ xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+ "ostatus:object-type" => ACTIVITY_OBJ_GROUP,
+ "href" => $mention));
+ else
+ xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+ "ostatus:object-type" => ACTIVITY_OBJ_PERSON,
+ "href" => $mention));
+ }
+
+ if (!$item["private"]) {
+ xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:attention",
+ "href" => "http://activityschema.org/collection/public"));
+ xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+ "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection",
+ "href" => "http://activityschema.org/collection/public"));
+ }
+
+ if(count($tags))
+ foreach($tags as $t)
+ if ($t[0] != "@")
+ xml::add_element($doc, $entry, "category", "", array("term" => $t[2]));
+
+ self::get_attachment($doc, $entry, $item);
+
+ if ($complete) {
+ $app = $item["app"];
+ if ($app == "")
+ $app = "web";
+
+ $attributes = array("local_id" => $item["id"], "source" => $app);
+
+ if (isset($parent["id"]))
+ $attributes["repeat_of"] = $parent["id"];
+
+ if ($item["coord"] != "")
+ xml::add_element($doc, $entry, "georss:point", $item["coord"]);
+
+ xml::add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
+ }
+ }
+
+ /**
+ * @brief Creates the XML feed for a given nickname
+ *
+ * @param app $a The application class
+ * @param string $owner_nick Nickname of the feed owner
+ * @param string $last_update Date of the last update
+ *
+ * @return string XML feed
+ */
+ public static function feed(&$a, $owner_nick, $last_update) {
+
+ $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
+ FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
+ WHERE `contact`.`self` AND `user`.`nickname` = '%s' LIMIT 1",
+ dbesc($owner_nick));
+ if (!$r)
+ return;
+
+ $owner = $r[0];
+
+ if(!strlen($last_update))
+ $last_update = 'now -30 days';
+
+ $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
+
+ $items = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id` FROM `item`
+ INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`
+ LEFT JOIN `item` AS `thritem` ON `thritem`.`uri`=`item`.`thr-parent` AND `thritem`.`uid`=`item`.`uid`
+ WHERE `item`.`uid` = %d AND `item`.`received` > '%s' AND NOT `item`.`private` AND NOT `item`.`deleted`
+ AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
+ AND ((`item`.`wall` AND (`item`.`parent` = `item`.`id`))
+ OR (`item`.`network` = '%s' AND ((`thread`.`network` IN ('%s', '%s')) OR (`thritem`.`network` IN ('%s', '%s')))) AND `thread`.`mention`)
+ AND ((`item`.`owner-link` IN ('%s', '%s') AND (`item`.`parent` = `item`.`id`))
+ OR (`item`.`author-link` IN ('%s', '%s')))
+ ORDER BY `item`.`received` DESC
+ LIMIT 0, 300",
+ intval($owner["uid"]), dbesc($check_date), dbesc(NETWORK_DFRN),
+ //dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+ //dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+ dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+ dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+ dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"])),
+ dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"]))
+ );
+
+ $doc = new DOMDocument('1.0', 'utf-8');
+ $doc->formatOutput = true;
+
+ $root = self::add_header($doc, $owner);
+
+ foreach ($items AS $item) {
+ $entry = self::entry($doc, $item, $owner);
+ $root->appendChild($entry);
+ }
+
+ return(trim($doc->saveXML()));
+ }