3 * @file include/diaspora.php
4 * @brief The implementation of the diaspora protocol
7 require_once("include/diaspora.php");
8 require_once("include/Scrape.php");
10 function array_to_xml($array, &$xml) {
12 if (!is_object($xml)) {
13 foreach($array as $key => $value) {
14 $root = new SimpleXMLElement('<'.$key.'/>');
15 array_to_xml($value, $root);
17 $dom = dom_import_simplexml($root)->ownerDocument;
18 $dom->formatOutput = true;
19 return $dom->saveXML();
23 foreach($array as $key => $value) {
24 if (!is_array($value) AND !is_numeric($key))
25 $xml->addChild($key, $value);
26 elseif (is_array($value))
27 array_to_xml($value, $xml->addChild($key));
32 * @brief This class contain functions to create and send DFRN XML files
37 public static function dispatch_public($msg) {
39 $enabled = intval(get_config("system", "diaspora_enabled"));
41 logger('diaspora is disabled');
45 // Use a dummy importer to import the data for the public copy
46 $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
47 self::dispatch($importer,$msg);
49 // Now distribute it to the followers
50 $r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN
51 (SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s')
52 AND NOT `account_expired` AND NOT `account_removed`",
53 dbesc(NETWORK_DIASPORA),
58 logger("delivering to: ".$rr["username"]);
59 self::dispatch($rr,$msg);
62 logger("No subscribers for ".$msg["author"]." ".print_r($msg, true));
65 public static function dispatch($importer, $msg) {
67 // The sender is the handle of the contact that sent the message.
68 // This will often be different with relayed messages (for example "like" and "comment")
69 $sender = $msg->author;
71 if (!diaspora::valid_posting($msg, $fields)) {
72 logger("Invalid posting");
76 $type = $fields->getName();
79 case "account_deletion":
80 return self::import_account_deletion($importer, $fields);
83 return self::import_comment($importer, $sender, $fields);
86 return self::import_conversation($importer, $fields);
89 return self::import_like($importer, $sender, $fields);
92 return self::import_message($importer, $fields);
95 return self::import_participation($importer, $fields);
98 return self::import_photo($importer, $fields);
100 case "poll_participation":
101 return self::import_poll_participation($importer, $fields);
104 return self::import_profile($importer, $fields);
107 return self::import_request($importer, $fields);
110 return self::import_reshare($importer, $fields);
113 return self::import_retraction($importer, $fields);
115 case "status_message":
116 return self::import_status_message($importer, $fields);
119 logger("Unknown message type ".$type);
127 * @brief Checks if a posting is valid and fetches the data fields.
129 * This function does not only check the signature.
130 * It also does the conversion between the old and the new diaspora format.
132 * @param array $msg Array with the XML, the sender handle and the sender signature
133 * @param object $fields SimpleXML object that contains the posting
135 * @return bool Is the posting valid?
137 private function valid_posting($msg, &$fields) {
139 $data = parse_xml_string($msg->message, false);
141 $first_child = $data->getName();
143 if ($data->getName() == "XML") {
145 foreach ($data->post->children() as $child)
152 $type = $element->getName();
154 if (in_array($type, array("signed_retraction", "relayable_retraction")))
155 $type = "retraction";
157 $fields = new SimpleXMLElement("<".$type."/>");
161 foreach ($element->children() AS $fieldname => $data) {
164 // Translation for the old XML structure
165 if ($fieldname == "diaspora_handle")
166 $fieldname = "author";
168 if ($fieldname == "participant_handles")
169 $fieldname = "participants";
171 if (in_array($type, array("like", "participation"))) {
172 if ($fieldname == "target_type")
173 $fieldname = "parent_type";
176 if ($fieldname == "sender_handle")
177 $fieldname = "author";
179 if ($fieldname == "recipient_handle")
180 $fieldname = "recipient";
182 if ($fieldname == "root_diaspora_id")
183 $fieldname = "root_author";
185 if ($type == "retraction") {
186 if ($fieldname == "post_guid")
187 $fieldname = "target_guid";
189 if ($fieldname == "type")
190 $fieldname = "target_type";
194 if ($fieldname == "author_signature")
195 $author_signature = base64_decode($data);
196 elseif ($fieldname == "parent_author_signature")
197 $parent_author_signature = base64_decode($data);
198 elseif ($fieldname != "target_author_signature") {
199 if ($signed_data != "") {
201 $signed_data_parent .= ";";
204 $signed_data .= $data;
205 $fields->$fieldname = $data;
209 if (in_array($type, array("status_message", "reshare")))
210 if ($msg->author != $fields->author) {
211 logger("Message handle is not the same as envelope sender. Quitting this message.");
215 if (!in_array($type, array("comment", "conversation", "message", "like")))
218 if (!isset($author_signature))
221 if (isset($parent_author_signature)) {
222 $key = self::get_key($msg->author);
224 if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256"))
228 $key = self::get_key($fields->author);
230 return rsa_verify($signed_data, $author_signature, $key, "sha256");
233 private function get_key($handle) {
234 logger("Fetching diaspora key for: ".$handle);
236 $r = self::get_person_by_handle($handle);
243 private function get_person_by_handle($handle) {
245 $r = q("SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
246 dbesc(NETWORK_DIASPORA),
251 logger("In cache ".print_r($r,true), LOGGER_DEBUG);
253 // update record occasionally so it doesn't get stale
254 $d = strtotime($person["updated"]." +00:00");
255 if ($d < strtotime("now - 14 days"))
259 if (!$person OR $update) {
260 logger("create or refresh", LOGGER_DEBUG);
261 $r = probe_url($handle, PROBE_DIASPORA);
263 // Note that Friendica contacts will return a "Diaspora person"
264 // if Diaspora connectivity is enabled on their server
265 if (count($r) AND ($r["network"] === NETWORK_DIASPORA)) {
266 self::add_fcontact($r, $update);
273 private function add_fcontact($arr, $update = false) {
274 /// @todo Remove this function from include/network.php
277 $r = q("UPDATE `fcontact` SET
290 WHERE `url` = '%s' AND `network` = '%s'",
292 dbesc($arr["photo"]),
293 dbesc($arr["request"]),
296 dbesc($arr["batch"]),
297 dbesc($arr["notify"]),
299 dbesc($arr["confirm"]),
300 dbesc($arr["alias"]),
301 dbesc($arr["pubkey"]),
302 dbesc(datetime_convert()),
304 dbesc($arr["network"])
307 $r = q("INSERT INTO `fcontact` (`url`,`name`,`photo`,`request`,`nick`,`addr`,
308 `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated`)
309 VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
312 dbesc($arr["photo"]),
313 dbesc($arr["request"]),
316 dbesc($arr["batch"]),
317 dbesc($arr["notify"]),
319 dbesc($arr["confirm"]),
320 dbesc($arr["network"]),
321 dbesc($arr["alias"]),
322 dbesc($arr["pubkey"]),
323 dbesc(datetime_convert())
330 private function get_contact_by_handle($uid, $handle) {
331 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
336 if ($r AND count($r))
339 $handle_parts = explode("@", $handle);
340 $nurl_sql = '%%://' . $handle_parts[1] . '%%/profile/' . $handle_parts[0];
341 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1",
353 function DiasporaFetchGuid($item) {
354 preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
355 function ($match) use ($item){
356 return(DiasporaFetchGuidSub($match, $item));
360 function DiasporaFetchGuidSub($match, $item) {
363 if (!diaspora_store_by_guid($match[1], $item["author-link"]))
364 diaspora_store_by_guid($match[1], $item["owner-link"]);
367 function diaspora_store_by_guid($guid, $server, $uid = 0) {
368 require_once("include/Contact.php");
370 $serverparts = parse_url($server);
371 $server = $serverparts["scheme"]."://".$serverparts["host"];
373 logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
375 $item = diaspora_fetch_message($guid, $server);
380 logger("Successfully fetched item ".$guid." from ".$server, LOGGER_DEBUG);
382 $body = $item["body"];
383 $str_tags = $item["tag"];
385 $created = $item["created"];
386 $author = $item["author"];
387 $guid = $item["guid"];
388 $private = $item["private"];
389 $object = $item["object"];
390 $objecttype = $item["object-type"];
392 $message_id = $author.':'.$guid;
393 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
400 $person = find_diaspora_person_by_handle($author);
402 $contact_id = get_contact($person['url'], $uid);
404 $contacts = q("SELECT * FROM `contact` WHERE `id` = %d", intval($contact_id));
405 $importers = q("SELECT * FROM `user` WHERE `uid` = %d", intval($uid));
407 if ($contacts AND $importers)
408 if(!diaspora_post_allow($importers[0],$contacts[0], false)) {
409 logger('Ignoring author '.$person['url'].' for uid '.$uid);
412 logger('Author '.$person['url'].' is allowed for uid '.$uid);
415 $datarray['uid'] = $uid;
416 $datarray['contact-id'] = $contact_id;
417 $datarray['wall'] = 0;
418 $datarray['network'] = NETWORK_DIASPORA;
419 $datarray['guid'] = $guid;
420 $datarray['uri'] = $datarray['parent-uri'] = $message_id;
421 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created);
422 $datarray['private'] = $private;
423 $datarray['parent'] = 0;
424 $datarray['plink'] = diaspora_plink($author, $guid);
425 $datarray['author-name'] = $person['name'];
426 $datarray['author-link'] = $person['url'];
427 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
428 $datarray['owner-name'] = $datarray['author-name'];
429 $datarray['owner-link'] = $datarray['author-link'];
430 $datarray['owner-avatar'] = $datarray['author-avatar'];
431 $datarray['body'] = $body;
432 $datarray['tag'] = $str_tags;
433 $datarray['app'] = $app;
434 $datarray['visible'] = ((strlen($body)) ? 1 : 0);
435 $datarray['object'] = $object;
436 $datarray['object-type'] = $objecttype;
438 if ($datarray['contact-id'] == 0)
441 DiasporaFetchGuid($datarray);
442 $message_id = item_store($datarray);
445 /// Looking if there is some subscribe mechanism in Diaspora to get all comments for this post
451 private function import_account_deletion($importer, $data) {
455 private function import_comment($importer, $sender, $data) {
456 $guid = notags(unxmlify($data->guid));
457 $parent_guid = notags(unxmlify($data->parent_guid));
458 $text = unxmlify($data->text);
459 $author = notags(unxmlify($data->author));
461 $contact = self::get_contact_by_handle($importer["uid"], $sender);
463 logger("cannot find contact for sender: ".$sender);
467 if(! diaspora_post_allow($importer,$contact, true)) {
468 logger('diaspora_comment: Ignoring this author.');
472 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
473 intval($importer['uid']),
477 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
481 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
482 intval($importer['uid']),
487 $result = diaspora_store_by_guid($parent_guid, $contact['url'], $importer['uid']);
490 $person = find_diaspora_person_by_handle($diaspora_handle);
491 $result = diaspora_store_by_guid($parent_guid, $person['url'], $importer['uid']);
495 logger("Fetched missing item ".$parent_guid." - result: ".$result, LOGGER_DEBUG);
497 $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
498 intval($importer['uid']),
505 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
508 $parent_item = $r[0];
510 // Find the original comment author information.
511 // We need this to make sure we display the comment author
512 // information (name and avatar) correctly.
513 if(strcasecmp($diaspora_handle,$msg['author']) == 0)
516 $person = find_diaspora_person_by_handle($diaspora_handle);
518 if(! is_array($person)) {
519 logger('diaspora_comment: unable to find author details');
524 // Fetch the contact id - if we know this contact
525 $r = q("SELECT `id`, `network` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
526 dbesc(normalise_link($person['url'])), intval($importer['uid']));
529 $network = $r[0]['network'];
531 $cid = $contact['id'];
532 $network = NETWORK_DIASPORA;
535 $body = diaspora2bb($text);
536 $message_id = $diaspora_handle . ':' . $guid;
540 $datarray['uid'] = $importer['uid'];
541 $datarray['contact-id'] = $cid;
542 $datarray['type'] = 'remote-comment';
543 $datarray['wall'] = $parent_item['wall'];
544 $datarray['network'] = $network;
545 $datarray['verb'] = ACTIVITY_POST;
546 $datarray['gravity'] = GRAVITY_COMMENT;
547 $datarray['guid'] = $guid;
548 $datarray['uri'] = $message_id;
549 $datarray['parent-uri'] = $parent_item['uri'];
551 // No timestamps for comments? OK, we'll the use current time.
552 $datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert();
553 $datarray['private'] = $parent_item['private'];
555 $datarray['owner-name'] = $parent_item['owner-name'];
556 $datarray['owner-link'] = $parent_item['owner-link'];
557 $datarray['owner-avatar'] = $parent_item['owner-avatar'];
559 $datarray['author-name'] = $person['name'];
560 $datarray['author-link'] = $person['url'];
561 $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
562 $datarray['body'] = $body;
563 $datarray["object"] = json_encode($xml);
564 $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
566 // We can't be certain what the original app is if the message is relayed.
567 if(($parent_item['origin']) && (! $parent_author_signature))
568 $datarray['app'] = 'Diaspora';
570 DiasporaFetchGuid($datarray);
571 $message_id = item_store($datarray);
573 $datarray['id'] = $message_id;
575 // If we are the origin of the parent we store the original signature and notify our followers
576 if($parent_item['origin']) {
577 $author_signature_base64 = base64_encode($author_signature);
578 $author_signature_base64 = diaspora_repair_signature($author_signature_base64, $diaspora_handle);
580 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
583 dbesc($author_signature_base64),
584 dbesc($diaspora_handle)
588 proc_run('php','include/notifier.php','comment-import',$message_id);
594 private function import_conversation($importer, $data) {
598 private function import_like($importer, $sender, $data) {
602 private function import_message($importer, $data) {
606 private function import_participation($importer, $data) {
610 private function import_photo($importer, $data) {
614 private function import_poll_participation($importer, $data) {
618 private function import_profile($importer, $data) {
622 private function import_request($importer, $data) {
626 private function import_reshare($importer, $data) {
630 private function import_retraction($importer, $data) {
634 private function import_status_message($importer, $data) {