+ $suggest = $doc->createElement("dfrn:suggest");
+
+ xml::add_element($doc, $suggest, "dfrn:url", $item['url']);
+ xml::add_element($doc, $suggest, "dfrn:name", $item['name']);
+ xml::add_element($doc, $suggest, "dfrn:photo", $item['photo']);
+ xml::add_element($doc, $suggest, "dfrn:request", $item['request']);
+ xml::add_element($doc, $suggest, "dfrn:note", $item['note']);
+
+ $root->appendChild($suggest);
+
+ return(trim($doc->saveXML()));
+ }
+
+ /**
+ * @brief Create XML text for DFRN relocations
+ *
+ * @param array $owner Owner record
+ * @param int $uid User ID
+ *
+ * @return string DFRN relocations
+ */
+ public static function relocate($owner, $uid) {
+
+ /* get site pubkey. this could be a new installation with no site keys*/
+ $pubkey = get_config('system','site_pubkey');
+ if(! $pubkey) {
+ $res = new_keypair(1024);
+ set_config('system','site_prvkey', $res['prvkey']);
+ set_config('system','site_pubkey', $res['pubkey']);
+ }
+
+ $rp = q("SELECT `resource-id` , `scale`, type FROM `photo`
+ WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
+ $photos = array();
+ $ext = Photo::supportedTypes();
+
+ foreach($rp as $p)
+ $photos[$p['scale']] = app::get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
+
+ unset($rp, $ext);
+
+ $doc = new DOMDocument('1.0', 'utf-8');
+ $doc->formatOutput = true;
+
+ $root = self::add_header($doc, $owner, "dfrn:owner", "", false);
+
+ $relocate = $doc->createElement("dfrn:relocate");
+
+ xml::add_element($doc, $relocate, "dfrn:url", $owner['url']);
+ xml::add_element($doc, $relocate, "dfrn:name", $owner['name']);
+ xml::add_element($doc, $relocate, "dfrn:photo", $photos[4]);
+ xml::add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
+ xml::add_element($doc, $relocate, "dfrn:micro", $photos[6]);
+ xml::add_element($doc, $relocate, "dfrn:request", $owner['request']);
+ xml::add_element($doc, $relocate, "dfrn:confirm", $owner['confirm']);
+ xml::add_element($doc, $relocate, "dfrn:notify", $owner['notify']);
+ xml::add_element($doc, $relocate, "dfrn:poll", $owner['poll']);
+ xml::add_element($doc, $relocate, "dfrn:sitepubkey", get_config('system','site_pubkey'));
+
+ $root->appendChild($relocate);
+
+ return(trim($doc->saveXML()));
+ }
+
+ /**
+ * @brief Adds the header elements for the DFRN protocol
+ *
+ * @param object $doc XML document
+ * @param array $owner Owner record
+ * @param string $authorelement Element name for the author
+ * @param string $alternatelink link to profile or category
+ * @param bool $public Is it a header for public posts?
+ *
+ * @return object XML root object
+ */
+ private function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
+
+ if ($alternatelink == "")
+ $alternatelink = $owner['url'];
+
+ $root = $doc->createElementNS(NAMESPACE_ATOM1, 'feed');
+ $doc->appendChild($root);
+
+ $root->setAttribute("xmlns:thr", NAMESPACE_THREAD);
+ $root->setAttribute("xmlns:at", NAMESPACE_TOMB);
+ $root->setAttribute("xmlns:media", NAMESPACE_MEDIA);
+ $root->setAttribute("xmlns:dfrn", NAMESPACE_DFRN);
+ $root->setAttribute("xmlns:activity", NAMESPACE_ACTIVITY);
+ $root->setAttribute("xmlns:georss", NAMESPACE_GEORSS);
+ $root->setAttribute("xmlns:poco", NAMESPACE_POCO);
+ $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
+ $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
+
+ xml::add_element($doc, $root, "id", app::get_baseurl()."/profile/".$owner["nick"]);
+ xml::add_element($doc, $root, "title", $owner["name"]);
+
+ $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
+ xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
+
+ $attributes = array("rel" => "license", "href" => "http://creativecommons.org/licenses/by/3.0/");
+ xml::add_element($doc, $root, "link", "", $attributes);
+
+ $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $alternatelink);
+ xml::add_element($doc, $root, "link", "", $attributes);
+
+
+ if ($public) {
+ // DFRN itself doesn't uses this. But maybe someone else wants to subscribe to the public feed.
+ ostatus::hublinks($doc, $root);
+
+ $attributes = array("rel" => "salmon", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+ xml::add_element($doc, $root, "link", "", $attributes);
+
+ $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+ xml::add_element($doc, $root, "link", "", $attributes);
+
+ $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
+ xml::add_element($doc, $root, "link", "", $attributes);
+ }
+
+ if ($owner['page-flags'] == PAGE_COMMUNITY)
+ xml::add_element($doc, $root, "dfrn:community", 1);
+
+ /// @todo We need a way to transmit the different page flags like "PAGE_PRVGROUP"
+
+ xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
+
+ $author = self::add_author($doc, $owner, $authorelement, $public);
+ $root->appendChild($author);
+
+ return $root;
+ }
+
+ /**
+ * @brief Adds the author element in the header for the DFRN protocol
+ *
+ * @param object $doc XML document
+ * @param array $owner Owner record
+ * @param string $authorelement Element name for the author
+ *
+ * @return object XML author object
+ */
+ private function add_author($doc, $owner, $authorelement, $public) {
+
+ $author = $doc->createElement($authorelement);
+
+ $namdate = datetime_convert('UTC', 'UTC', $owner['name-date'].'+00:00' , ATOM_TIME);
+ $uridate = datetime_convert('UTC', 'UTC', $owner['uri-date'].'+00:00', ATOM_TIME);
+ $picdate = datetime_convert('UTC', 'UTC', $owner['avatar-date'].'+00:00', ATOM_TIME);
+
+ $attributes = array("dfrn:updated" => $namdate);
+ xml::add_element($doc, $author, "name", $owner["name"], $attributes);
+
+ $attributes = array("dfrn:updated" => $namdate);
+ xml::add_element($doc, $author, "uri", app::get_baseurl().'/profile/'.$owner["nickname"], $attributes);
+
+ $attributes = array("dfrn:updated" => $namdate);
+ xml::add_element($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
+
+ $attributes = array("rel" => "photo", "type" => "image/jpeg", "dfrn:updated" => $picdate,
+ "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
+ xml::add_element($doc, $author, "link", "", $attributes);
+
+ $attributes = array("rel" => "avatar", "type" => "image/jpeg", "dfrn:updated" => $picdate,
+ "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
+ xml::add_element($doc, $author, "link", "", $attributes);
+
+ $birthday = feed_birthday($owner['uid'], $owner['timezone']);
+
+ if ($birthday)
+ xml::add_element($doc, $author, "dfrn:birthday", $birthday);
+
+ // The following fields will only be generated if this isn't for a public feed
+ if ($public)
+ return $author;
+
+ // Only show contact details when we are allowed to
+ $r = q("SELECT `profile`.`about`, `profile`.`name`, `profile`.`homepage`, `user`.`nickname`, `user`.`timezone`,
+ `profile`.`locality`, `profile`.`region`, `profile`.`country-name`, `profile`.`pub_keywords`, `profile`.`dob`
+ FROM `profile`
+ INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
+ WHERE `profile`.`is-default` AND NOT `user`.`hidewall` AND `user`.`uid` = %d",
+ intval($owner['uid']));
+ if ($r) {
+ $profile = $r[0];
+ xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
+ xml::add_element($doc, $author, "poco:updated", $namdate);
+
+ if (trim($profile["dob"]) != "0000-00-00")
+ xml::add_element($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"])));
+
+ xml::add_element($doc, $author, "poco:note", $profile["about"]);
+ xml::add_element($doc, $author, "poco:preferredUsername", $profile["nickname"]);
+
+ $savetz = date_default_timezone_get();
+ date_default_timezone_set($profile["timezone"]);
+ xml::add_element($doc, $author, "poco:utcOffset", date("P"));
+ date_default_timezone_set($savetz);
+
+ if (trim($profile["homepage"]) != "") {
+ $urls = $doc->createElement("poco:urls");
+ xml::add_element($doc, $urls, "poco:type", "homepage");
+ xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
+ xml::add_element($doc, $urls, "poco:primary", "true");
+ $author->appendChild($urls);
+ }
+
+ if (trim($profile["pub_keywords"]) != "") {
+ $keywords = explode(",", $profile["pub_keywords"]);
+
+ foreach ($keywords AS $keyword)
+ xml::add_element($doc, $author, "poco:tags", trim($keyword));
+
+ }
+
+ /// @todo When we are having the XMPP address in the profile we should propagate it here
+ $xmpp = "";
+ if (trim($xmpp) != "") {
+ $ims = $doc->createElement("poco:ims");
+ xml::add_element($doc, $ims, "poco:type", "xmpp");
+ xml::add_element($doc, $ims, "poco:value", $xmpp);
+ xml::add_element($doc, $ims, "poco:primary", "true");
+ $author->appendChild($ims);
+ }
+
+ if (trim($profile["locality"].$profile["region"].$profile["country-name"]) != "") {
+ $element = $doc->createElement("poco:address");
+
+ xml::add_element($doc, $element, "poco:formatted", formatted_location($profile));
+
+ if (trim($profile["locality"]) != "")
+ xml::add_element($doc, $element, "poco:locality", $profile["locality"]);
+
+ if (trim($profile["region"]) != "")
+ xml::add_element($doc, $element, "poco:region", $profile["region"]);
+
+ if (trim($profile["country-name"]) != "")
+ xml::add_element($doc, $element, "poco:country", $profile["country-name"]);
+
+ $author->appendChild($element);
+ }
+ }
+
+ return $author;
+ }
+
+ /**
+ * @brief Adds the author elements in the "entry" elements of the DFRN protocol
+ *
+ * @param object $doc XML document
+ * @param string $element Element name for the author
+ * @param string $contact_url Link of the contact
+ * @param array $items Item elements
+ *
+ * @return object XML author object
+ */
+ private function add_entry_author($doc, $element, $contact_url, $item) {
+
+ $contact = get_contact_details_by_url($contact_url, $item["uid"]);
+
+ $author = $doc->createElement($element);
+ xml::add_element($doc, $author, "name", $contact["name"]);
+ xml::add_element($doc, $author, "uri", $contact["url"]);
+ xml::add_element($doc, $author, "dfrn:handle", $contact["addr"]);
+
+ /// @Todo
+ /// - Check real image type and image size
+ /// - Check which of these boths elements we should use
+ $attributes = array(
+ "rel" => "photo",
+ "type" => "image/jpeg",
+ "media:width" => 80,
+ "media:height" => 80,
+ "href" => $contact["photo"]);
+ xml::add_element($doc, $author, "link", "", $attributes);
+
+ $attributes = array(
+ "rel" => "avatar",
+ "type" => "image/jpeg",
+ "media:width" => 80,
+ "media:height" => 80,
+ "href" => $contact["photo"]);
+ xml::add_element($doc, $author, "link", "", $attributes);
+
+ return $author;
+ }
+
+ /**
+ * @brief Adds the activity elements
+ *
+ * @param object $doc XML document
+ * @param string $element Element name for the activity
+ * @param string $activity activity value
+ *
+ * @return object XML activity object
+ */
+ private function create_activity($doc, $element, $activity) {
+
+ if($activity) {
+ $entry = $doc->createElement($element);
+
+ $r = parse_xml_string($activity, false);
+ if(!$r)
+ return false;
+ if($r->type)
+ xml::add_element($doc, $entry, "activity:object-type", $r->type);
+ if($r->id)
+ xml::add_element($doc, $entry, "id", $r->id);
+ if($r->title)
+ xml::add_element($doc, $entry, "title", $r->title);
+ if($r->link) {
+ if(substr($r->link,0,1) == '<') {
+ if(strstr($r->link,'&') && (! strstr($r->link,'&')))
+ $r->link = str_replace('&','&', $r->link);
+
+ $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
+
+ // XML does need a single element as root element so we add a dummy element here
+ $data = parse_xml_string("<dummy>".$r->link."</dummy>", false);
+ if (is_object($data)) {
+ foreach ($data->link AS $link) {
+ $attributes = array();
+ foreach ($link->attributes() AS $parameter => $value)
+ $attributes[$parameter] = $value;
+ xml::add_element($doc, $entry, "link", "", $attributes);
+ }
+ }
+ } else {
+ $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $r->link);
+ xml::add_element($doc, $entry, "link", "", $attributes);
+ }
+ }
+ if($r->content)
+ xml::add_element($doc, $entry, "content", bbcode($r->content), array("type" => "html"));
+
+ return $entry;
+ }
+
+ return false;
+ }
+
+ /**
+ * @brief Adds the elements for attachments
+ *
+ * @param object $doc XML document
+ * @param object $root XML root
+ * @param array $item Item element
+ *
+ * @return object XML attachment object
+ */
+ private function get_attachment($doc, $root, $item) {
+ $arr = explode('[/attach],',$item['attach']);
+ if(count($arr)) {
+ foreach($arr as $r) {
+ $matches = false;
+ $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
+ if($cnt) {
+ $attributes = array("rel" => "enclosure",
+ "href" => $matches[1],
+ "type" => $matches[3]);
+
+ if(intval($matches[2]))
+ $attributes["length"] = intval($matches[2]);
+
+ if(trim($matches[4]) != "")
+ $attributes["title"] = trim($matches[4]);
+
+ xml::add_element($doc, $root, "link", "", $attributes);
+ }
+ }
+ }
+ }
+
+ /**
+ * @brief Adds the "entry" elements for the DFRN protocol
+ *
+ * @param object $doc XML document
+ * @param string $type "text" or "html"
+ * @param array $item Item element
+ * @param array $owner Owner record
+ * @param bool $comment Trigger the sending of the "comment" element
+ * @param int $cid Contact ID of the recipient
+ *
+ * @return object XML entry object
+ */
+ private function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
+
+ $mentioned = array();
+
+ if(!$item['parent'])
+ return;
+
+ if($item['deleted']) {
+ $attributes = array("ref" => $item['uri'], "when" => datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME));
+ return xml::create_element($doc, "at:deleted-entry", "", $attributes);
+ }
+
+ $entry = $doc->createElement("entry");
+
+ if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
+ $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
+ else
+ $body = $item['body'];
+
+ // Remove the abstract element. It is only locally important.
+ $body = remove_abstract($body);
+
+ if ($type == 'html') {
+ $htmlbody = $body;
+
+ if ($item['title'] != "")
+ $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
+
+ $htmlbody = bbcode($htmlbody, false, false, 7);
+ }
+
+ $author = self::add_entry_author($doc, "author", $item["author-link"], $item);
+ $entry->appendChild($author);
+
+ $dfrnowner = self::add_entry_author($doc, "dfrn:owner", $item["owner-link"], $item);
+ $entry->appendChild($dfrnowner);
+
+ if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
+ $parent = q("SELECT `guid` 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'],
+ "dfrn:diaspora_guid" => $parent[0]['guid']);
+ xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
+ }
+
+ xml::add_element($doc, $entry, "id", $item["uri"]);
+ xml::add_element($doc, $entry, "title", $item["title"]);
+
+ 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));
+
+ // "dfrn:env" is used to read the content
+ xml::add_element($doc, $entry, "dfrn:env", base64url_encode($body, true));
+
+ // The "content" field is not read by the receiver. We could remove it when the type is "text"
+ // We keep it at the moment, maybe there is some old version that doesn't read "dfrn:env"
+ xml::add_element($doc, $entry, "content", (($type == 'html') ? $htmlbody : $body), array("type" => $type));
+
+ // We save this value in "plink". Maybe we should read it from there as well?
+ xml::add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
+ "href" => app::get_baseurl()."/display/".$item["guid"]));
+
+ // "comment-allow" is some old fashioned stuff for old Friendica versions.
+ // It is included in the rewritten code for completeness
+ if ($comment)
+ xml::add_element($doc, $entry, "dfrn:comment-allow", intval($item['last-child']));
+
+ if($item['location'])
+ xml::add_element($doc, $entry, "dfrn:location", $item['location']);
+
+ if($item['coord'])
+ xml::add_element($doc, $entry, "georss:point", $item['coord']);
+
+ if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
+ xml::add_element($doc, $entry, "dfrn:private", (($item['private']) ? $item['private'] : 1));
+
+ if($item['extid'])
+ xml::add_element($doc, $entry, "dfrn:extid", $item['extid']);
+
+ if($item['bookmark'])
+ xml::add_element($doc, $entry, "dfrn:bookmark", "true");
+
+ if($item['app'])
+ xml::add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item['id'], "source" => $item['app']));
+
+ xml::add_element($doc, $entry, "dfrn:diaspora_guid", $item["guid"]);
+
+ // The signed text contains the content in Markdown, the sender handle and the signatur for the content
+ // It is needed for relayed comments to Diaspora.
+ if($item['signed_text']) {
+ $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
+ xml::add_element($doc, $entry, "dfrn:diaspora_signature", $sign);
+ }
+
+ xml::add_element($doc, $entry, "activity:verb", construct_verb($item));
+
+ if ($item['object-type'] != "")
+ xml::add_element($doc, $entry, "activity:object-type", $item['object-type']);
+ elseif ($item['id'] == $item['parent'])
+ xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+ else
+ xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_COMMENT);
+
+ $actobj = self::create_activity($doc, "activity:object", $item['object']);
+ if ($actobj)
+ $entry->appendChild($actobj);
+
+ $actarg = self::create_activity($doc, "activity:target", $item['target']);
+ if ($actarg)
+ $entry->appendChild($actarg);
+
+ $tags = item_getfeedtags($item);
+
+ if(count($tags)) {
+ foreach($tags as $t)
+ if (($type != 'html') OR ($t[0] != "@"))
+ xml::add_element($doc, $entry, "category", "", array("scheme" => "X-DFRN:".$t[0].":".$t[1], "term" => $t[2]));
+ }
+
+ if(count($tags))
+ foreach($tags as $t)
+ if ($t[0] == "@")
+ $mentioned[$t[1]] = $t[1];
+
+ 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));
+ }
+
+ self::get_attachment($doc, $entry, $item);
+
+ return $entry;
+ }
+
+ /**
+ * @brief Delivers the atom content to the contacts
+ *
+ * @param array $owner Owner record
+ * @param array $contactr Contact record of the receiver
+ * @param string $atom Content that will be transmitted
+ * @param bool $dissolve (to be documented)
+ *
+ * @return int Deliver status. -1 means an error.
+ */
+ public static function deliver($owner,$contact,$atom, $dissolve = false) {
+
+ $a = get_app();
+
+ $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
+
+ if($contact['duplex'] && $contact['dfrn-id'])
+ $idtosend = '0:' . $orig_id;
+ if($contact['duplex'] && $contact['issued-id'])
+ $idtosend = '1:' . $orig_id;
+
+
+ $rino = get_config('system','rino_encrypt');
+ $rino = intval($rino);
+ // use RINO1 if mcrypt isn't installed and RINO2 was selected
+ if ($rino==2 and !function_exists('mcrypt_create_iv')) $rino=1;
+
+ logger("Local rino version: ". $rino, LOGGER_DEBUG);
+
+ $ssl_val = intval(get_config('system','ssl_policy'));
+ $ssl_policy = '';
+
+ switch($ssl_val){
+ case SSL_POLICY_FULL:
+ $ssl_policy = 'full';