]> git.mxchange.org Git - friendica.git/blob - include/diaspora2.php
New Diaspora code
[friendica.git] / include / diaspora2.php
1 <?php
2 /**
3  * @file include/diaspora.php
4  * @brief The implementation of the diaspora protocol
5  */
6
7 require_once("include/diaspora.php");
8 require_once("include/Scrape.php");
9
10 function array_to_xml($array, &$xml) {
11
12         if (!is_object($xml)) {
13                 foreach($array as $key => $value) {
14                         $root = new SimpleXMLElement('<'.$key.'/>');
15                         array_to_xml($value, $root);
16
17                         $dom = dom_import_simplexml($root)->ownerDocument;
18                         $dom->formatOutput = true;
19                         return $dom->saveXML();
20                 }
21         }
22
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));
28         }
29 }
30
31 /**
32  * @brief This class contain functions to create and send DFRN XML files
33  *
34  */
35 class diaspora {
36
37         public static function dispatch_public($msg) {
38
39                 $enabled = intval(get_config("system", "diaspora_enabled"));
40                 if (!$enabled) {
41                         logger('diaspora is disabled');
42                         return false;
43                 }
44
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);
48
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),
54                         dbesc($msg["author"])
55                 );
56                 if(count($r)) {
57                         foreach($r as $rr) {
58                                 logger("delivering to: ".$rr["username"]);
59                                 self::dispatch($rr,$msg);
60                         }
61                 } else
62                         logger("No subscribers for ".$msg["author"]." ".print_r($msg, true));
63         }
64
65         public static function dispatch($importer, $msg) {
66
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;
70
71                 if (!diaspora::valid_posting($msg, $fields)) {
72                         logger("Invalid posting");
73                         return false;
74                 }
75
76                 $type = $fields->getName();
77
78                 switch ($type) {
79                         case "account_deletion":
80                                 return self::import_account_deletion($importer, $fields);
81
82                         case "comment":
83                                 return self::import_comment($importer, $sender, $fields);
84
85                         case "conversation":
86                                 return self::import_conversation($importer, $fields);
87
88                         case "like":
89                                 return self::import_like($importer, $sender, $fields);
90
91                         case "message":
92                                 return self::import_message($importer, $fields);
93
94                         case "participation":
95                                 return self::import_participation($importer, $fields);
96
97                         case "photo":
98                                 return self::import_photo($importer, $fields);
99
100                         case "poll_participation":
101                                 return self::import_poll_participation($importer, $fields);
102
103                         case "profile":
104                                 return self::import_profile($importer, $fields);
105
106                         case "request":
107                                 return self::import_request($importer, $fields);
108
109                         case "reshare":
110                                 return self::import_reshare($importer, $fields);
111
112                         case "retraction":
113                                 return self::import_retraction($importer, $fields);
114
115                         case "status_message":
116                                 return self::import_status_message($importer, $fields);
117
118                         default:
119                                 logger("Unknown message type ".$type);
120                                 return false;
121                 }
122
123                 return true;
124         }
125
126         /**
127          * @brief Checks if a posting is valid and fetches the data fields.
128          *
129          * This function does not only check the signature.
130          * It also does the conversion between the old and the new diaspora format.
131          *
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
134          *
135          * @return bool Is the posting valid?
136          */
137         private function valid_posting($msg, &$fields) {
138
139                 $data = parse_xml_string($msg->message, false);
140
141                 $first_child = $data->getName();
142
143                 if ($data->getName() == "XML") {
144                         $oldXML = true;
145                         foreach ($data->post->children() as $child)
146                                 $element = $child;
147                 } else {
148                         $oldXML = false;
149                         $element = $data;
150                 }
151
152                 $type = $element->getName();
153
154                 if (in_array($type, array("signed_retraction", "relayable_retraction")))
155                         $type = "retraction";
156
157                 $fields = new SimpleXMLElement("<".$type."/>");
158
159                 $signed_data = "";
160
161                 foreach ($element->children() AS $fieldname => $data) {
162
163                         if ($oldXML) {
164                                 // Translation for the old XML structure
165                                 if ($fieldname == "diaspora_handle")
166                                         $fieldname = "author";
167
168                                 if ($fieldname == "participant_handles")
169                                         $fieldname = "participants";
170
171                                 if (in_array($type, array("like", "participation"))) {
172                                         if ($fieldname == "target_type")
173                                                 $fieldname = "parent_type";
174                                 }
175
176                                 if ($fieldname == "sender_handle")
177                                         $fieldname = "author";
178
179                                 if ($fieldname == "recipient_handle")
180                                         $fieldname = "recipient";
181
182                                 if ($fieldname == "root_diaspora_id")
183                                         $fieldname = "root_author";
184
185                                 if ($type == "retraction") {
186                                         if ($fieldname == "post_guid")
187                                                 $fieldname = "target_guid";
188
189                                         if ($fieldname == "type")
190                                                 $fieldname = "target_type";
191                                 }
192                         }
193
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 != "") {
200                                         $signed_data .= ";";
201                                         $signed_data_parent .= ";";
202                                 }
203
204                                 $signed_data .= $data;
205                                 $fields->$fieldname = $data;
206                         }
207                 }
208
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.");
212                                 return false;
213                         }
214
215                 if (!in_array($type, array("comment", "conversation", "message", "like")))
216                         return true;
217
218                 if (!isset($author_signature))
219                         return false;
220
221                 if (isset($parent_author_signature)) {
222                         $key = self::get_key($msg->author);
223
224                         if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256"))
225                                 return false;
226                 }
227
228                 $key = self::get_key($fields->author);
229
230                 return rsa_verify($signed_data, $author_signature, $key, "sha256");
231         }
232
233         private function get_key($handle) {
234                 logger("Fetching diaspora key for: ".$handle);
235
236                 $r = self::get_person_by_handle($handle);
237                 if($r)
238                         return $r["pubkey"];
239
240                 return "";
241         }
242
243         private function get_person_by_handle($handle) {
244
245                 $r = q("SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
246                         dbesc(NETWORK_DIASPORA),
247                         dbesc($handle)
248                 );
249                 if (count($r)) {
250                         $person = $r[0];
251                         logger("In cache ".print_r($r,true), LOGGER_DEBUG);
252
253                         // update record occasionally so it doesn't get stale
254                         $d = strtotime($person["updated"]." +00:00");
255                         if ($d < strtotime("now - 14 days"))
256                                 $update = true;
257                 }
258
259                 if (!$person OR $update) {
260                         logger("create or refresh", LOGGER_DEBUG);
261                         $r = probe_url($handle, PROBE_DIASPORA);
262
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);
267                                 $person = $r;
268                         }
269                 }
270                 return $person;
271         }
272
273         private function add_fcontact($arr, $update = false) {
274                 /// @todo Remove this function from include/network.php
275
276                 if($update) {
277                         $r = q("UPDATE `fcontact` SET
278                                         `name` = '%s',
279                                         `photo` = '%s',
280                                         `request` = '%s',
281                                         `nick` = '%s',
282                                         `addr` = '%s',
283                                         `batch` = '%s',
284                                         `notify` = '%s',
285                                         `poll` = '%s',
286                                         `confirm` = '%s',
287                                         `alias` = '%s',
288                                         `pubkey` = '%s',
289                                         `updated` = '%s'
290                                 WHERE `url` = '%s' AND `network` = '%s'",
291                                         dbesc($arr["name"]),
292                                         dbesc($arr["photo"]),
293                                         dbesc($arr["request"]),
294                                         dbesc($arr["nick"]),
295                                         dbesc($arr["addr"]),
296                                         dbesc($arr["batch"]),
297                                         dbesc($arr["notify"]),
298                                         dbesc($arr["poll"]),
299                                         dbesc($arr["confirm"]),
300                                         dbesc($arr["alias"]),
301                                         dbesc($arr["pubkey"]),
302                                         dbesc(datetime_convert()),
303                                         dbesc($arr["url"]),
304                                         dbesc($arr["network"])
305                                 );
306                 } else {
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')",
310                                         dbesc($arr["url"]),
311                                         dbesc($arr["name"]),
312                                         dbesc($arr["photo"]),
313                                         dbesc($arr["request"]),
314                                         dbesc($arr["nick"]),
315                                         dbesc($arr["addr"]),
316                                         dbesc($arr["batch"]),
317                                         dbesc($arr["notify"]),
318                                         dbesc($arr["poll"]),
319                                         dbesc($arr["confirm"]),
320                                         dbesc($arr["network"]),
321                                         dbesc($arr["alias"]),
322                                         dbesc($arr["pubkey"]),
323                                         dbesc(datetime_convert())
324                                 );
325                 }
326
327                 return $r;
328         }
329
330         private function get_contact_by_handle($uid, $handle) {
331                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
332                         intval($uid),
333                         dbesc($handle)
334                 );
335
336                 if ($r AND count($r))
337                         return $r[0];
338
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",
342                         dbesc(NETWORK_DFRN),
343                         intval($uid),
344                         dbesc($nurl_sql)
345                 );
346                 if($r AND count($r))
347                         return $r[0];
348
349                 return false;
350         }
351
352 /*
353 function DiasporaFetchGuid($item) {
354         preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
355                 function ($match) use ($item){
356                         return(DiasporaFetchGuidSub($match, $item));
357                 },$item["body"]);
358 }
359
360 function DiasporaFetchGuidSub($match, $item) {
361         $a = get_app();
362
363         if (!diaspora_store_by_guid($match[1], $item["author-link"]))
364                 diaspora_store_by_guid($match[1], $item["owner-link"]);
365 }
366
367 function diaspora_store_by_guid($guid, $server, $uid = 0) {
368         require_once("include/Contact.php");
369
370         $serverparts = parse_url($server);
371         $server = $serverparts["scheme"]."://".$serverparts["host"];
372
373         logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
374
375         $item = diaspora_fetch_message($guid, $server);
376
377         if (!$item)
378                 return false;
379
380         logger("Successfully fetched item ".$guid." from ".$server, LOGGER_DEBUG);
381
382         $body = $item["body"];
383         $str_tags = $item["tag"];
384         $app = $item["app"];
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"];
391
392         $message_id = $author.':'.$guid;
393         $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
394                 intval($uid),
395                 dbesc($guid)
396         );
397         if(count($r))
398                 return $r[0]["id"];
399
400         $person = find_diaspora_person_by_handle($author);
401
402         $contact_id = get_contact($person['url'], $uid);
403
404         $contacts = q("SELECT * FROM `contact` WHERE `id` = %d", intval($contact_id));
405         $importers = q("SELECT * FROM `user` WHERE `uid` = %d", intval($uid));
406
407         if ($contacts AND $importers)
408                 if(!diaspora_post_allow($importers[0],$contacts[0], false)) {
409                         logger('Ignoring author '.$person['url'].' for uid '.$uid);
410                         return false;
411                 } else
412                         logger('Author '.$person['url'].' is allowed for uid '.$uid);
413
414         $datarray = array();
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;
437
438         if ($datarray['contact-id'] == 0)
439                 return false;
440
441         DiasporaFetchGuid($datarray);
442         $message_id = item_store($datarray);
443
444         /// @TODO
445         /// Looking if there is some subscribe mechanism in Diaspora to get all comments for this post
446
447         return $message_id;
448 }
449 */
450
451         private function import_account_deletion($importer, $data) {
452                 return true;
453         }
454
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));
460
461                 $contact = self::get_contact_by_handle($importer["uid"], $sender);
462                 if (!$contact) {
463                         logger("cannot find contact for sender: ".$sender);
464                         return false;
465                 }
466 /*
467         if(! diaspora_post_allow($importer,$contact, true)) {
468                 logger('diaspora_comment: Ignoring this author.');
469                 return 202;
470         }
471
472         $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
473                 intval($importer['uid']),
474                 dbesc($guid)
475         );
476         if(count($r)) {
477                 logger('diaspora_comment: our comment just got relayed back to us (or there was a guid collision) : ' . $guid);
478                 return;
479         }
480
481         $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
482                 intval($importer['uid']),
483                 dbesc($parent_guid)
484         );
485
486         if(!count($r)) {
487                 $result = diaspora_store_by_guid($parent_guid, $contact['url'], $importer['uid']);
488
489                 if (!$result) {
490                         $person = find_diaspora_person_by_handle($diaspora_handle);
491                         $result = diaspora_store_by_guid($parent_guid, $person['url'], $importer['uid']);
492                 }
493
494                 if ($result) {
495                         logger("Fetched missing item ".$parent_guid." - result: ".$result, LOGGER_DEBUG);
496
497                         $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
498                                 intval($importer['uid']),
499                                 dbesc($parent_guid)
500                         );
501                 }
502         }
503
504         if(! count($r)) {
505                 logger('diaspora_comment: parent item not found: parent: ' . $parent_guid . ' item: ' . $guid);
506                 return;
507         }
508         $parent_item = $r[0];
509
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)
514                 $person = $contact;
515         else {
516                 $person = find_diaspora_person_by_handle($diaspora_handle);
517
518                 if(! is_array($person)) {
519                         logger('diaspora_comment: unable to find author details');
520                         return;
521                 }
522         }
523
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']));
527         if ($r) {
528                 $cid = $r[0]['id'];
529                 $network = $r[0]['network'];
530         } else {
531                 $cid = $contact['id'];
532                 $network = NETWORK_DIASPORA;
533         }
534
535         $body = diaspora2bb($text);
536         $message_id = $diaspora_handle . ':' . $guid;
537
538         $datarray = array();
539
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'];
550
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'];
554
555         $datarray['owner-name'] = $parent_item['owner-name'];
556         $datarray['owner-link'] = $parent_item['owner-link'];
557         $datarray['owner-avatar'] = $parent_item['owner-avatar'];
558
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;
565
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';
569
570         DiasporaFetchGuid($datarray);
571         $message_id = item_store($datarray);
572
573         $datarray['id'] = $message_id;
574
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);
579
580                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
581                         intval($message_id),
582                         dbesc($signed_data),
583                         dbesc($author_signature_base64),
584                         dbesc($diaspora_handle)
585                 );
586
587                 // notify others
588                 proc_run('php','include/notifier.php','comment-import',$message_id);
589         }
590 */
591                 return true;
592         }
593
594         private function import_conversation($importer, $data) {
595                 return true;
596         }
597
598         private function import_like($importer, $sender, $data) {
599                 return true;
600         }
601
602         private function import_message($importer, $data) {
603                 return true;
604         }
605
606         private function import_participation($importer, $data) {
607                 return true;
608         }
609
610         private function import_photo($importer, $data) {
611                 return true;
612         }
613
614         private function import_poll_participation($importer, $data) {
615                 return true;
616         }
617
618         private function import_profile($importer, $data) {
619                 return true;
620         }
621
622         private function import_request($importer, $data) {
623                 return true;
624         }
625
626         private function import_reshare($importer, $data) {
627                 return true;
628         }
629
630         private function import_retraction($importer, $data) {
631                 return true;
632         }
633
634         private function import_status_message($importer, $data) {
635                 return true;
636         }
637 }
638 ?>