+ /**
+ * @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 AND ($item["id"] > 0))
+ 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"];
+ }
+ }
+
+ if (intval($item["parent"]) > 0) {
+ $conversation = App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"];
+ xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation));
+ xml::add_element($doc, $entry, "ostatus:conversation", $conversation);
+ }
+
+ $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 AND ($item["id"] > 0)) {
+ $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(App $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');
+ $authorid = get_contact($owner["url"], 0);
+
+ $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item` USE INDEX (`uid_contactid_created`)
+ STRAIGHT_JOIN `thread` ON `thread`.`iid` = `item`.`parent`
+ WHERE `item`.`uid` = %d AND `item`.`contact-id` = %d AND
+ `item`.`author-id` = %d AND `item`.`created` > '%s' AND
+ NOT `item`.`deleted` AND NOT `item`.`private` AND
+ `thread`.`network` IN ('%s', '%s')
+ ORDER BY `item`.`created` DESC LIMIT 300",
+ intval($owner["uid"]), intval($owner["id"]),
+ intval($authorid), dbesc($check_date),
+ dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+
+/* 2016-10-23: The old query will be kept until we are sure that the query above is a good and fast replacement
+
+ $items = q("SELECT `item`.*, `item`.`id` AS `item_id` FROM `item`
+ STRAIGHT_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`.`id` 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()));
+ }