]> git.mxchange.org Git - friendica.git/blob - include/diaspora.php
Merge pull request #2636 from rabuzarus/2506_frio_strings
[friendica.git] / include / diaspora.php
1 <?php
2 /**
3  * @file include/diaspora.php
4  * @brief The implementation of the diaspora protocol
5  */
6
7 require_once("include/items.php");
8 require_once("include/bb2diaspora.php");
9 require_once("include/Scrape.php");
10 require_once("include/Contact.php");
11 require_once("include/Photo.php");
12 require_once("include/socgraph.php");
13 require_once("include/group.php");
14 require_once("include/xml.php");
15 require_once("include/datetime.php");
16 require_once("include/queue_fn.php");
17
18 /**
19  * @brief This class contain functions to create and send Diaspora XML files
20  *
21  */
22 class diaspora {
23
24         /**
25          * @brief Return a list of relay servers
26          *
27          * This is an experimental Diaspora feature.
28          *
29          * @return array of relay servers
30          */
31         public static function relay_list() {
32
33                 $serverdata = get_config("system", "relay_server");
34                 if ($serverdata == "")
35                         return array();
36
37                 $relay = array();
38
39                 $servers = explode(",", $serverdata);
40
41                 foreach($servers AS $server) {
42                         $server = trim($server);
43                         $batch = $server."/receive/public";
44
45                         $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
46
47                         if (!$relais) {
48                                 $addr = "relay@".str_replace("http://", "", normalise_link($server));
49
50                                 $r = q("INSERT INTO `contact` (`uid`, `created`, `name`, `nick`, `addr`, `url`, `nurl`, `batch`, `network`, `rel`, `blocked`, `pending`, `writable`, `name-date`, `uri-date`, `avatar-date`)
51                                         VALUES (0, '%s', '%s', 'relay', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, '%s', '%s', '%s')",
52                                         datetime_convert(),
53                                         dbesc($addr),
54                                         dbesc($addr),
55                                         dbesc($server),
56                                         dbesc(normalise_link($server)),
57                                         dbesc($batch),
58                                         dbesc(NETWORK_DIASPORA),
59                                         intval(CONTACT_IS_FOLLOWER),
60                                         dbesc(datetime_convert()),
61                                         dbesc(datetime_convert()),
62                                         dbesc(datetime_convert())
63                                 );
64
65                                 $relais = q("SELECT `batch`, `id`, `name`,`network` FROM `contact` WHERE `uid` = 0 AND `batch` = '%s' LIMIT 1", dbesc($batch));
66                                 if ($relais)
67                                         $relay[] = $relais[0];
68                         } else
69                                 $relay[] = $relais[0];
70                 }
71
72                 return $relay;
73         }
74
75         /**
76          * @brief repairs a signature that was double encoded
77          *
78          * The function is unused at the moment. It was copied from the old implementation.
79          *
80          * @param string $signature The signature
81          * @param string $handle The handle of the signature owner
82          * @param integer $level This value is only set inside this function to avoid endless loops
83          *
84          * @return string the repaired signature
85          */
86         private function repair_signature($signature, $handle = "", $level = 1) {
87
88                 if ($signature == "")
89                         return ($signature);
90
91                 if (base64_encode(base64_decode(base64_decode($signature))) == base64_decode($signature)) {
92                         $signature = base64_decode($signature);
93                         logger("Repaired double encoded signature from Diaspora/Hubzilla handle ".$handle." - level ".$level, LOGGER_DEBUG);
94
95                         // Do a recursive call to be able to fix even multiple levels
96                         if ($level < 10)
97                                 $signature = self::repair_signature($signature, $handle, ++$level);
98                 }
99
100                 return($signature);
101         }
102
103         /**
104          * @brief: Decodes incoming Diaspora message
105          *
106          * @param array $importer Array of the importer user
107          * @param string $xml urldecoded Diaspora salmon
108          *
109          * @return array
110          * 'message' -> decoded Diaspora XML message
111          * 'author' -> author diaspora handle
112          * 'key' -> author public key (converted to pkcs#8)
113          */
114         public static function decode($importer, $xml) {
115
116                 $public = false;
117                 $basedom = parse_xml_string($xml);
118
119                 if (!is_object($basedom))
120                         return false;
121
122                 $children = $basedom->children('https://joindiaspora.com/protocol');
123
124                 if($children->header) {
125                         $public = true;
126                         $author_link = str_replace('acct:','',$children->header->author_id);
127                 } else {
128
129                         $encrypted_header = json_decode(base64_decode($children->encrypted_header));
130
131                         $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
132                         $ciphertext = base64_decode($encrypted_header->ciphertext);
133
134                         $outer_key_bundle = '';
135                         openssl_private_decrypt($encrypted_aes_key_bundle,$outer_key_bundle,$importer['prvkey']);
136
137                         $j_outer_key_bundle = json_decode($outer_key_bundle);
138
139                         $outer_iv = base64_decode($j_outer_key_bundle->iv);
140                         $outer_key = base64_decode($j_outer_key_bundle->key);
141
142                         $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $outer_key, $ciphertext, MCRYPT_MODE_CBC, $outer_iv);
143
144
145                         $decrypted = pkcs5_unpad($decrypted);
146
147                         logger('decrypted: '.$decrypted, LOGGER_DEBUG);
148                         $idom = parse_xml_string($decrypted,false);
149
150                         $inner_iv = base64_decode($idom->iv);
151                         $inner_aes_key = base64_decode($idom->aes_key);
152
153                         $author_link = str_replace('acct:','',$idom->author_id);
154                 }
155
156                 $dom = $basedom->children(NAMESPACE_SALMON_ME);
157
158                 // figure out where in the DOM tree our data is hiding
159
160                 if($dom->provenance->data)
161                         $base = $dom->provenance;
162                 elseif($dom->env->data)
163                         $base = $dom->env;
164                 elseif($dom->data)
165                         $base = $dom;
166
167                 if (!$base) {
168                         logger('unable to locate salmon data in xml');
169                         http_status_exit(400);
170                 }
171
172
173                 // Stash the signature away for now. We have to find their key or it won't be good for anything.
174                 $signature = base64url_decode($base->sig);
175
176                 // unpack the  data
177
178                 // strip whitespace so our data element will return to one big base64 blob
179                 $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
180
181
182                 // stash away some other stuff for later
183
184                 $type = $base->data[0]->attributes()->type[0];
185                 $keyhash = $base->sig[0]->attributes()->keyhash[0];
186                 $encoding = $base->encoding;
187                 $alg = $base->alg;
188
189
190                 $signed_data = $data.'.'.base64url_encode($type).'.'.base64url_encode($encoding).'.'.base64url_encode($alg);
191
192
193                 // decode the data
194                 $data = base64url_decode($data);
195
196
197                 if($public)
198                         $inner_decrypted = $data;
199                 else {
200
201                         // Decode the encrypted blob
202
203                         $inner_encrypted = base64_decode($data);
204                         $inner_decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $inner_encrypted, MCRYPT_MODE_CBC, $inner_iv);
205                         $inner_decrypted = pkcs5_unpad($inner_decrypted);
206                 }
207
208                 if (!$author_link) {
209                         logger('Could not retrieve author URI.');
210                         http_status_exit(400);
211                 }
212                 // Once we have the author URI, go to the web and try to find their public key
213                 // (first this will look it up locally if it is in the fcontact cache)
214                 // This will also convert diaspora public key from pkcs#1 to pkcs#8
215
216                 logger('Fetching key for '.$author_link);
217                 $key = self::key($author_link);
218
219                 if (!$key) {
220                         logger('Could not retrieve author key.');
221                         http_status_exit(400);
222                 }
223
224                 $verify = rsa_verify($signed_data,$signature,$key);
225
226                 if (!$verify) {
227                         logger('Message did not verify. Discarding.');
228                         http_status_exit(400);
229                 }
230
231                 logger('Message verified.');
232
233                 return array('message' => (string)$inner_decrypted,
234                                 'author' => unxmlify($author_link),
235                                 'key' => (string)$key);
236
237         }
238
239
240         /**
241          * @brief Dispatches public messages and find the fitting receivers
242          *
243          * @param array $msg The post that will be dispatched
244          *
245          * @return int The message id of the generated message, "true" or "false" if there was an error
246          */
247         public static function dispatch_public($msg) {
248
249                 $enabled = intval(get_config("system", "diaspora_enabled"));
250                 if (!$enabled) {
251                         logger("diaspora is disabled");
252                         return false;
253                 }
254
255                 // Use a dummy importer to import the data for the public copy
256                 $importer = array("uid" => 0, "page-flags" => PAGE_FREELOVE);
257                 $message_id = self::dispatch($importer,$msg);
258
259                 // Now distribute it to the followers
260                 $r = q("SELECT `user`.* FROM `user` WHERE `user`.`uid` IN
261                         (SELECT `contact`.`uid` FROM `contact` WHERE `contact`.`network` = '%s' AND `contact`.`addr` = '%s')
262                         AND NOT `account_expired` AND NOT `account_removed`",
263                         dbesc(NETWORK_DIASPORA),
264                         dbesc($msg["author"])
265                 );
266                 if($r) {
267                         foreach($r as $rr) {
268                                 logger("delivering to: ".$rr["username"]);
269                                 self::dispatch($rr,$msg);
270                         }
271                 } else
272                         logger("No subscribers for ".$msg["author"]." ".print_r($msg, true));
273
274                 return $message_id;
275         }
276
277         /**
278          * @brief Dispatches the different message types to the different functions
279          *
280          * @param array $importer Array of the importer user
281          * @param array $msg The post that will be dispatched
282          *
283          * @return int The message id of the generated message, "true" or "false" if there was an error
284          */
285         public static function dispatch($importer, $msg) {
286
287                 // The sender is the handle of the contact that sent the message.
288                 // This will often be different with relayed messages (for example "like" and "comment")
289                 $sender = $msg["author"];
290
291                 if (!diaspora::valid_posting($msg, $fields)) {
292                         logger("Invalid posting");
293                         return false;
294                 }
295
296                 $type = $fields->getName();
297
298                 logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG);
299
300                 switch ($type) {
301                         case "account_deletion":
302                                 return self::receive_account_deletion($importer, $fields);
303
304                         case "comment":
305                                 return self::receive_comment($importer, $sender, $fields, $msg["message"]);
306
307                         case "contact":
308                                 return self::receive_contact_request($importer, $fields);
309
310                         case "conversation":
311                                 return self::receive_conversation($importer, $msg, $fields);
312
313                         case "like":
314                                 return self::receive_like($importer, $sender, $fields);
315
316                         case "message":
317                                 return self::receive_message($importer, $fields);
318
319                         case "participation": // Not implemented
320                                 return self::receive_participation($importer, $fields);
321
322                         case "photo": // Not implemented
323                                 return self::receive_photo($importer, $fields);
324
325                         case "poll_participation": // Not implemented
326                                 return self::receive_poll_participation($importer, $fields);
327
328                         case "profile":
329                                 return self::receive_profile($importer, $fields);
330
331                         case "reshare":
332                                 return self::receive_reshare($importer, $fields, $msg["message"]);
333
334                         case "retraction":
335                                 return self::receive_retraction($importer, $sender, $fields);
336
337                         case "status_message":
338                                 return self::receive_status_message($importer, $fields, $msg["message"]);
339
340                         default:
341                                 logger("Unknown message type ".$type);
342                                 return false;
343                 }
344
345                 return true;
346         }
347
348         /**
349          * @brief Checks if a posting is valid and fetches the data fields.
350          *
351          * This function does not only check the signature.
352          * It also does the conversion between the old and the new diaspora format.
353          *
354          * @param array $msg Array with the XML, the sender handle and the sender signature
355          * @param object $fields SimpleXML object that contains the posting when it is valid
356          *
357          * @return bool Is the posting valid?
358          */
359         private function valid_posting($msg, &$fields) {
360
361                 $data = parse_xml_string($msg["message"], false);
362
363                 if (!is_object($data))
364                         return false;
365
366                 $first_child = $data->getName();
367
368                 // Is this the new or the old version?
369                 if ($data->getName() == "XML") {
370                         $oldXML = true;
371                         foreach ($data->post->children() as $child)
372                                 $element = $child;
373                 } else {
374                         $oldXML = false;
375                         $element = $data;
376                 }
377
378                 $type = $element->getName();
379                 $orig_type = $type;
380
381                 // All retractions are handled identically from now on.
382                 // In the new version there will only be "retraction".
383                 if (in_array($type, array("signed_retraction", "relayable_retraction")))
384                         $type = "retraction";
385
386                 if ($type == "request")
387                         $type = "contact";
388
389                 $fields = new SimpleXMLElement("<".$type."/>");
390
391                 $signed_data = "";
392
393                 foreach ($element->children() AS $fieldname => $entry) {
394                         if ($oldXML) {
395                                 // Translation for the old XML structure
396                                 if ($fieldname == "diaspora_handle")
397                                         $fieldname = "author";
398
399                                 if ($fieldname == "participant_handles")
400                                         $fieldname = "participants";
401
402                                 if (in_array($type, array("like", "participation"))) {
403                                         if ($fieldname == "target_type")
404                                                 $fieldname = "parent_type";
405                                 }
406
407                                 if ($fieldname == "sender_handle")
408                                         $fieldname = "author";
409
410                                 if ($fieldname == "recipient_handle")
411                                         $fieldname = "recipient";
412
413                                 if ($fieldname == "root_diaspora_id")
414                                         $fieldname = "root_author";
415
416                                 if ($type == "retraction") {
417                                         if ($fieldname == "post_guid")
418                                                 $fieldname = "target_guid";
419
420                                         if ($fieldname == "type")
421                                                 $fieldname = "target_type";
422                                 }
423                         }
424
425                         if ($fieldname == "author_signature")
426                                 $author_signature = base64_decode($entry);
427                         elseif ($fieldname == "parent_author_signature")
428                                 $parent_author_signature = base64_decode($entry);
429                         elseif ($fieldname != "target_author_signature") {
430                                 if ($signed_data != "") {
431                                         $signed_data .= ";";
432                                         $signed_data_parent .= ";";
433                                 }
434
435                                 $signed_data .= $entry;
436                         }
437                         if (!in_array($fieldname, array("parent_author_signature", "target_author_signature")) OR
438                                 ($orig_type == "relayable_retraction"))
439                                 xml::copy($entry, $fields, $fieldname);
440                 }
441
442                 // This is something that shouldn't happen at all.
443                 if (in_array($type, array("status_message", "reshare", "profile")))
444                         if ($msg["author"] != $fields->author) {
445                                 logger("Message handle is not the same as envelope sender. Quitting this message.");
446                                 return false;
447                         }
448
449                 // Only some message types have signatures. So we quit here for the other types.
450                 if (!in_array($type, array("comment", "message", "like")))
451                         return true;
452
453                 // No author_signature? This is a must, so we quit.
454                 if (!isset($author_signature))
455                         return false;
456
457                 if (isset($parent_author_signature)) {
458                         $key = self::key($msg["author"]);
459
460                         if (!rsa_verify($signed_data, $parent_author_signature, $key, "sha256"))
461                                 return false;
462                 }
463
464                 $key = self::key($fields->author);
465
466                 return rsa_verify($signed_data, $author_signature, $key, "sha256");
467         }
468
469         /**
470          * @brief Fetches the public key for a given handle
471          *
472          * @param string $handle The handle
473          *
474          * @return string The public key
475          */
476         private function key($handle) {
477                 $handle = strval($handle);
478
479                 logger("Fetching diaspora key for: ".$handle);
480
481                 $r = self::person_by_handle($handle);
482                 if($r)
483                         return $r["pubkey"];
484
485                 return "";
486         }
487
488         /**
489          * @brief Fetches data for a given handle
490          *
491          * @param string $handle The handle
492          *
493          * @return array the queried data
494          */
495         private function person_by_handle($handle) {
496
497                 $r = q("SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
498                         dbesc(NETWORK_DIASPORA),
499                         dbesc($handle)
500                 );
501                 if ($r) {
502                         $person = $r[0];
503                         logger("In cache ".print_r($r,true), LOGGER_DEBUG);
504
505                         // update record occasionally so it doesn't get stale
506                         $d = strtotime($person["updated"]." +00:00");
507                         if ($d < strtotime("now - 14 days"))
508                                 $update = true;
509                 }
510
511                 if (!$person OR $update) {
512                         logger("create or refresh", LOGGER_DEBUG);
513                         $r = probe_url($handle, PROBE_DIASPORA);
514
515                         // Note that Friendica contacts will return a "Diaspora person"
516                         // if Diaspora connectivity is enabled on their server
517                         if ($r AND ($r["network"] === NETWORK_DIASPORA)) {
518                                 self::add_fcontact($r, $update);
519                                 $person = $r;
520                         }
521                 }
522                 return $person;
523         }
524
525         /**
526          * @brief Updates the fcontact table
527          *
528          * @param array $arr The fcontact data
529          * @param bool $update Update or insert?
530          *
531          * @return string The id of the fcontact entry
532          */
533         private function add_fcontact($arr, $update = false) {
534
535                 if($update) {
536                         $r = q("UPDATE `fcontact` SET
537                                         `name` = '%s',
538                                         `photo` = '%s',
539                                         `request` = '%s',
540                                         `nick` = '%s',
541                                         `addr` = '%s',
542                                         `batch` = '%s',
543                                         `notify` = '%s',
544                                         `poll` = '%s',
545                                         `confirm` = '%s',
546                                         `alias` = '%s',
547                                         `pubkey` = '%s',
548                                         `updated` = '%s'
549                                 WHERE `url` = '%s' AND `network` = '%s'",
550                                         dbesc($arr["name"]),
551                                         dbesc($arr["photo"]),
552                                         dbesc($arr["request"]),
553                                         dbesc($arr["nick"]),
554                                         dbesc($arr["addr"]),
555                                         dbesc($arr["batch"]),
556                                         dbesc($arr["notify"]),
557                                         dbesc($arr["poll"]),
558                                         dbesc($arr["confirm"]),
559                                         dbesc($arr["alias"]),
560                                         dbesc($arr["pubkey"]),
561                                         dbesc(datetime_convert()),
562                                         dbesc($arr["url"]),
563                                         dbesc($arr["network"])
564                                 );
565                 } else {
566                         $r = q("INSERT INTO `fcontact` (`url`,`name`,`photo`,`request`,`nick`,`addr`,
567                                         `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated`)
568                                 VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
569                                         dbesc($arr["url"]),
570                                         dbesc($arr["name"]),
571                                         dbesc($arr["photo"]),
572                                         dbesc($arr["request"]),
573                                         dbesc($arr["nick"]),
574                                         dbesc($arr["addr"]),
575                                         dbesc($arr["batch"]),
576                                         dbesc($arr["notify"]),
577                                         dbesc($arr["poll"]),
578                                         dbesc($arr["confirm"]),
579                                         dbesc($arr["network"]),
580                                         dbesc($arr["alias"]),
581                                         dbesc($arr["pubkey"]),
582                                         dbesc(datetime_convert())
583                                 );
584                 }
585
586                 return $r;
587         }
588
589         /**
590          * @brief get a handle (user@domain.tld) from a given contact id or gcontact id
591          *
592          * @param int $contact_id The id in the contact table
593          * @param int $gcontact_id The id in the gcontact table
594          *
595          * @return string the handle
596          */
597         public static function handle_from_contact($contact_id, $gcontact_id = 0) {
598                 $handle = False;
599
600                 logger("contact id is ".$contact_id." - gcontact id is ".$gcontact_id, LOGGER_DEBUG);
601
602                 if ($gcontact_id != 0) {
603                         $r = q("SELECT `addr` FROM `gcontact` WHERE `id` = %d AND `addr` != ''",
604                                 intval($gcontact_id));
605                         if ($r)
606                                 return $r[0]["addr"];
607                 }
608
609                 $r = q("SELECT `network`, `addr`, `self`, `url`, `nick` FROM `contact` WHERE `id` = %d",
610                         intval($contact_id));
611                 if ($r) {
612                         $contact = $r[0];
613
614                         logger("contact 'self' = ".$contact['self']." 'url' = ".$contact['url'], LOGGER_DEBUG);
615
616                         if($contact['addr'] != "")
617                                 $handle = $contact['addr'];
618                         else {
619                                 $baseurl_start = strpos($contact['url'],'://') + 3;
620                                 $baseurl_length = strpos($contact['url'],'/profile') - $baseurl_start; // allows installations in a subdirectory--not sure how Diaspora will handle
621                                 $baseurl = substr($contact['url'], $baseurl_start, $baseurl_length);
622                                 $handle = $contact['nick'].'@'.$baseurl;
623                         }
624                 }
625
626                 return $handle;
627         }
628
629         /**
630          * @brief Get a contact id for a given handle
631          *
632          * @param int $uid The user id
633          * @param string $handle The handle in the format user@domain.tld
634          *
635          * @return The contact id
636          */
637         private function contact_by_handle($uid, $handle) {
638                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
639                         intval($uid),
640                         dbesc($handle)
641                 );
642
643                 if ($r)
644                         return $r[0];
645
646                 $handle_parts = explode("@", $handle);
647                 $nurl_sql = "%%://".$handle_parts[1]."%%/profile/".$handle_parts[0];
648                 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1",
649                         dbesc(NETWORK_DFRN),
650                         intval($uid),
651                         dbesc($nurl_sql)
652                 );
653                 if($r)
654                         return $r[0];
655
656                 return false;
657         }
658
659         /**
660          * @brief Check if posting is allowed for this contact
661          *
662          * @param array $importer Array of the importer user
663          * @param array $contact The contact that is checked
664          * @param bool $is_comment Is the check for a comment?
665          *
666          * @return bool is the contact allowed to post?
667          */
668         private function post_allow($importer, $contact, $is_comment = false) {
669
670                 // perhaps we were already sharing with this person. Now they're sharing with us.
671                 // That makes us friends.
672                 // Normally this should have handled by getting a request - but this could get lost
673                 if($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) {
674                         q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
675                                 intval(CONTACT_IS_FRIEND),
676                                 intval($contact["id"]),
677                                 intval($importer["uid"])
678                         );
679                         $contact["rel"] = CONTACT_IS_FRIEND;
680                         logger("defining user ".$contact["nick"]." as friend");
681                 }
682
683                 if(($contact["blocked"]) || ($contact["readonly"]) || ($contact["archive"]))
684                         return false;
685                 if($contact["rel"] == CONTACT_IS_SHARING || $contact["rel"] == CONTACT_IS_FRIEND)
686                         return true;
687                 if($contact["rel"] == CONTACT_IS_FOLLOWER)
688                         if(($importer["page-flags"] == PAGE_COMMUNITY) OR $is_comment)
689                                 return true;
690
691                 // Messages for the global users are always accepted
692                 if ($importer["uid"] == 0)
693                         return true;
694
695                 return false;
696         }
697
698         /**
699          * @brief Fetches the contact id for a handle and checks if posting is allowed
700          *
701          * @param array $importer Array of the importer user
702          * @param string $handle The checked handle in the format user@domain.tld
703          * @param bool $is_comment Is the check for a comment?
704          *
705          * @return array The contact data
706          */
707         private function allowed_contact_by_handle($importer, $handle, $is_comment = false) {
708                 $contact = self::contact_by_handle($importer["uid"], $handle);
709                 if (!$contact) {
710                         logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
711                         return false;
712                 }
713
714                 if (!self::post_allow($importer, $contact, $is_comment)) {
715                         logger("The handle: ".$handle." is not allowed to post to user ".$importer["uid"]);
716                         return false;
717                 }
718                 return $contact;
719         }
720
721         /**
722          * @brief Does the message already exists on the system?
723          *
724          * @param int $uid The user id
725          * @param string $guid The guid of the message
726          *
727          * @return int|bool message id if the message already was stored into the system - or false.
728          */
729         private function message_exists($uid, $guid) {
730                 $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
731                         intval($uid),
732                         dbesc($guid)
733                 );
734
735                 if($r) {
736                         logger("message ".$guid." already exists for user ".$uid);
737                         return $r[0]["id"];
738                 }
739
740                 return false;
741         }
742
743         /**
744          * @brief Checks for links to posts in a message
745          *
746          * @param array $item The item array
747          */
748         private function fetch_guid($item) {
749                 preg_replace_callback("&\[url=/posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
750                         function ($match) use ($item){
751                                 return(self::fetch_guid_sub($match, $item));
752                         },$item["body"]);
753         }
754
755         /**
756          * @brief sub function of "fetch_guid" which checks for links in messages
757          *
758          * @param array $match array containing a link that has to be checked for a message link
759          * @param array $item The item array
760          */
761         private function fetch_guid_sub($match, $item) {
762                 if (!self::store_by_guid($match[1], $item["author-link"]))
763                         self::store_by_guid($match[1], $item["owner-link"]);
764         }
765
766         /**
767          * @brief Fetches an item with a given guid from a given server
768          *
769          * @param string $guid the message guid
770          * @param string $server The server address
771          * @param int $uid The user id of the user
772          *
773          * @return int the message id of the stored message or false
774          */
775         private function store_by_guid($guid, $server, $uid = 0) {
776                 $serverparts = parse_url($server);
777                 $server = $serverparts["scheme"]."://".$serverparts["host"];
778
779                 logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
780
781                 $msg = self::message($guid, $server);
782
783                 if (!$msg)
784                         return false;
785
786                 logger("Successfully fetched item ".$guid." from ".$server, LOGGER_DEBUG);
787
788                 // Now call the dispatcher
789                 return self::dispatch_public($msg);
790         }
791
792         /**
793          * @brief Fetches a message from a server
794          *
795          * @param string $guid message guid
796          * @param string $server The url of the server
797          * @param int $level Endless loop prevention
798          *
799          * @return array
800          *      'message' => The message XML
801          *      'author' => The author handle
802          *      'key' => The public key of the author
803          */
804         private function message($guid, $server, $level = 0) {
805
806                 if ($level > 5)
807                         return false;
808
809                 // This will work for Diaspora and newer Friendica servers
810                 $source_url = $server."/p/".$guid.".xml";
811                 $x = fetch_url($source_url);
812                 if(!$x)
813                         return false;
814
815                 $source_xml = parse_xml_string($x, false);
816
817                 if (!is_object($source_xml))
818                         return false;
819
820                 if ($source_xml->post->reshare) {
821                         // Reshare of a reshare - old Diaspora version
822                         return self::message($source_xml->post->reshare->root_guid, $server, ++$level);
823                 } elseif ($source_xml->getName() == "reshare") {
824                         // Reshare of a reshare - new Diaspora version
825                         return self::message($source_xml->root_guid, $server, ++$level);
826                 }
827
828                 $author = "";
829
830                 // Fetch the author - for the old and the new Diaspora version
831                 if ($source_xml->post->status_message->diaspora_handle)
832                         $author = (string)$source_xml->post->status_message->diaspora_handle;
833                 elseif ($source_xml->author AND ($source_xml->getName() == "status_message"))
834                         $author = (string)$source_xml->author;
835
836                 // If this isn't a "status_message" then quit
837                 if (!$author)
838                         return false;
839
840                 $msg = array("message" => $x, "author" => $author);
841
842                 $msg["key"] = self::key($msg["author"]);
843
844                 return $msg;
845         }
846
847         /**
848          * @brief Fetches the item record of a given guid
849          *
850          * @param int $uid The user id
851          * @param string $guid message guid
852          * @param string $author The handle of the item
853          * @param array $contact The contact of the item owner
854          *
855          * @return array the item record
856          */
857         private function parent_item($uid, $guid, $author, $contact) {
858                 $r = q("SELECT `id`, `body`, `wall`, `uri`, `private`, `origin`,
859                                 `author-name`, `author-link`, `author-avatar`,
860                                 `owner-name`, `owner-link`, `owner-avatar`
861                         FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
862                         intval($uid), dbesc($guid));
863
864                 if(!$r) {
865                         $result = self::store_by_guid($guid, $contact["url"], $uid);
866
867                         if (!$result) {
868                                 $person = self::person_by_handle($author);
869                                 $result = self::store_by_guid($guid, $person["url"], $uid);
870                         }
871
872                         if ($result) {
873                                 logger("Fetched missing item ".$guid." - result: ".$result, LOGGER_DEBUG);
874
875                                 $r = q("SELECT `id`, `body`, `wall`, `uri`, `private`, `origin`,
876                                                 `author-name`, `author-link`, `author-avatar`,
877                                                 `owner-name`, `owner-link`, `owner-avatar`
878                                         FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
879                                         intval($uid), dbesc($guid));
880                         }
881                 }
882
883                 if (!$r) {
884                         logger("parent item not found: parent: ".$guid." - user: ".$uid);
885                         return false;
886                 } else {
887                         logger("parent item found: parent: ".$guid." - user: ".$uid);
888                         return $r[0];
889                 }
890         }
891
892         /**
893          * @brief returns contact details
894          *
895          * @param array $contact The default contact if the person isn't found
896          * @param array $person The record of the person
897          * @param int $uid The user id
898          *
899          * @return array
900          *      'cid' => contact id
901          *      'network' => network type
902          */
903         private function author_contact_by_url($contact, $person, $uid) {
904
905                 $r = q("SELECT `id`, `network` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
906                         dbesc(normalise_link($person["url"])), intval($uid));
907                 if ($r) {
908                         $cid = $r[0]["id"];
909                         $network = $r[0]["network"];
910                 } else {
911                         $cid = $contact["id"];
912                         $network = NETWORK_DIASPORA;
913                 }
914
915                 return (array("cid" => $cid, "network" => $network));
916         }
917
918         /**
919          * @brief Is the profile a hubzilla profile?
920          *
921          * @param string $url The profile link
922          *
923          * @return bool is it a hubzilla server?
924          */
925         public static function is_redmatrix($url) {
926                 return(strstr($url, "/channel/"));
927         }
928
929         /**
930          * @brief Generate a post link with a given handle and message guid
931          *
932          * @param string $addr The user handle
933          * @param string $guid message guid
934          *
935          * @return string the post link
936          */
937         private function plink($addr, $guid) {
938                 $r = q("SELECT `url`, `nick`, `network` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", dbesc($addr));
939
940                 // Fallback
941                 if (!$r)
942                         return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
943
944                 // Friendica contacts are often detected as Diaspora contacts in the "fcontact" table
945                 // So we try another way as well.
946                 $s = q("SELECT `network` FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($r[0]["url"])));
947                 if ($s)
948                         $r[0]["network"] = $s[0]["network"];
949
950                 if ($r[0]["network"] == NETWORK_DFRN)
951                         return(str_replace("/profile/".$r[0]["nick"]."/", "/display/".$guid, $r[0]["url"]."/"));
952
953                 if (self::is_redmatrix($r[0]["url"]))
954                         return $r[0]["url"]."/?f=&mid=".$guid;
955
956                 return "https://".substr($addr,strpos($addr,"@")+1)."/posts/".$guid;
957         }
958
959         /**
960          * @brief Processes an account deletion
961          *
962          * @param array $importer Array of the importer user
963          * @param object $data The message object
964          *
965          * @return bool Success
966          */
967         private function receive_account_deletion($importer, $data) {
968
969                 /// @todo Account deletion should remove the contact from the global contacts as well
970
971                 $author = notags(unxmlify($data->author));
972
973                 $contact = self::contact_by_handle($importer["uid"], $author);
974                 if (!$contact) {
975                         logger("cannot find contact for author: ".$author);
976                         return false;
977                 }
978
979                 // We now remove the contact
980                 contact_remove($contact["id"]);
981                 return true;
982         }
983
984         /**
985          * @brief Fetch the uri from our database if we already have this item (maybe from ourselves)
986          *
987          * @param string $author Author handle
988          * @param string $guid Message guid
989          *
990          * @return string The constructed uri or the one from our database
991          */
992         private function get_uri_from_guid($author, $guid) {
993
994                 $r = q("SELECT `uri` FROM `item` WHERE `guid` = '%s' LIMIT 1", dbesc($guid));
995                 if ($r)
996                         return $r[0]["uri"];
997                 else
998                         return $author.":".$guid;
999         }
1000
1001         /**
1002          * @brief Processes an incoming comment
1003          *
1004          * @param array $importer Array of the importer user
1005          * @param string $sender The sender of the message
1006          * @param object $data The message object
1007          * @param string $xml The original XML of the message
1008          *
1009          * @return int The message id of the generated comment or "false" if there was an error
1010          */
1011         private function receive_comment($importer, $sender, $data, $xml) {
1012                 $guid = notags(unxmlify($data->guid));
1013                 $parent_guid = notags(unxmlify($data->parent_guid));
1014                 $text = unxmlify($data->text);
1015                 $author = notags(unxmlify($data->author));
1016
1017                 $contact = self::allowed_contact_by_handle($importer, $sender, true);
1018                 if (!$contact)
1019                         return false;
1020
1021                 $message_id = self::message_exists($importer["uid"], $guid);
1022                 if ($message_id)
1023                         return $message_id;
1024
1025                 $parent_item = self::parent_item($importer["uid"], $parent_guid, $author, $contact);
1026                 if (!$parent_item)
1027                         return false;
1028
1029                 $person = self::person_by_handle($author);
1030                 if (!is_array($person)) {
1031                         logger("unable to find author details");
1032                         return false;
1033                 }
1034
1035                 // Fetch the contact id - if we know this contact
1036                 $author_contact = self::author_contact_by_url($contact, $person, $importer["uid"]);
1037
1038                 $datarray = array();
1039
1040                 $datarray["uid"] = $importer["uid"];
1041                 $datarray["contact-id"] = $author_contact["cid"];
1042                 $datarray["network"]  = $author_contact["network"];
1043
1044                 $datarray["author-name"] = $person["name"];
1045                 $datarray["author-link"] = $person["url"];
1046                 $datarray["author-avatar"] = ((x($person,"thumb")) ? $person["thumb"] : $person["photo"]);
1047
1048                 $datarray["owner-name"] = $contact["name"];
1049                 $datarray["owner-link"] = $contact["url"];
1050                 $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1051
1052                 $datarray["guid"] = $guid;
1053                 $datarray["uri"] = self::get_uri_from_guid($author, $guid);
1054
1055                 $datarray["type"] = "remote-comment";
1056                 $datarray["verb"] = ACTIVITY_POST;
1057                 $datarray["gravity"] = GRAVITY_COMMENT;
1058                 $datarray["parent-uri"] = $parent_item["uri"];
1059
1060                 $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
1061                 $datarray["object"] = $xml;
1062
1063                 $datarray["body"] = diaspora2bb($text);
1064
1065                 self::fetch_guid($datarray);
1066
1067                 $message_id = item_store($datarray);
1068
1069                 if ($message_id)
1070                         logger("Stored comment ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
1071
1072                 // If we are the origin of the parent we store the original data and notify our followers
1073                 if($message_id AND $parent_item["origin"]) {
1074
1075                         // Formerly we stored the signed text, the signature and the author in different fields.
1076                         // We now store the raw data so that we are more flexible.
1077                         q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
1078                                 intval($message_id),
1079                                 dbesc(json_encode($data))
1080                         );
1081
1082                         // notify others
1083                         proc_run("php", "include/notifier.php", "comment-import", $message_id);
1084                 }
1085
1086                 return $message_id;
1087         }
1088
1089         /**
1090          * @brief processes and stores private messages
1091          *
1092          * @param array $importer Array of the importer user
1093          * @param array $contact The contact of the message
1094          * @param object $data The message object
1095          * @param array $msg Array of the processed message, author handle and key
1096          * @param object $mesg The private message
1097          * @param array $conversation The conversation record to which this message belongs
1098          *
1099          * @return bool "true" if it was successful
1100          */
1101         private function receive_conversation_message($importer, $contact, $data, $msg, $mesg, $conversation) {
1102                 $guid = notags(unxmlify($data->guid));
1103                 $subject = notags(unxmlify($data->subject));
1104                 $author = notags(unxmlify($data->author));
1105
1106                 $reply = 0;
1107
1108                 $msg_guid = notags(unxmlify($mesg->guid));
1109                 $msg_parent_guid = notags(unxmlify($mesg->parent_guid));
1110                 $msg_parent_author_signature = notags(unxmlify($mesg->parent_author_signature));
1111                 $msg_author_signature = notags(unxmlify($mesg->author_signature));
1112                 $msg_text = unxmlify($mesg->text);
1113                 $msg_created_at = datetime_convert("UTC", "UTC", notags(unxmlify($mesg->created_at)));
1114
1115                 // "diaspora_handle" is the element name from the old version
1116                 // "author" is the element name from the new version
1117                 if ($mesg->author)
1118                         $msg_author = notags(unxmlify($mesg->author));
1119                 elseif ($mesg->diaspora_handle)
1120                         $msg_author = notags(unxmlify($mesg->diaspora_handle));
1121                 else
1122                         return false;
1123
1124                 $msg_conversation_guid = notags(unxmlify($mesg->conversation_guid));
1125
1126                 if($msg_conversation_guid != $guid) {
1127                         logger("message conversation guid does not belong to the current conversation.");
1128                         return false;
1129                 }
1130
1131                 $body = diaspora2bb($msg_text);
1132                 $message_uri = $msg_author.":".$msg_guid;
1133
1134                 $author_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
1135
1136                 $author_signature = base64_decode($msg_author_signature);
1137
1138                 if(strcasecmp($msg_author,$msg["author"]) == 0) {
1139                         $person = $contact;
1140                         $key = $msg["key"];
1141                 } else {
1142                         $person = self::person_by_handle($msg_author);
1143
1144                         if (is_array($person) && x($person, "pubkey"))
1145                                 $key = $person["pubkey"];
1146                         else {
1147                                 logger("unable to find author details");
1148                                         return false;
1149                         }
1150                 }
1151
1152                 if (!rsa_verify($author_signed_data, $author_signature, $key, "sha256")) {
1153                         logger("verification failed.");
1154                         return false;
1155                 }
1156
1157                 if($msg_parent_author_signature) {
1158                         $owner_signed_data = $msg_guid.";".$msg_parent_guid.";".$msg_text.";".unxmlify($mesg->created_at).";".$msg_author.";".$msg_conversation_guid;
1159
1160                         $parent_author_signature = base64_decode($msg_parent_author_signature);
1161
1162                         $key = $msg["key"];
1163
1164                         if (!rsa_verify($owner_signed_data, $parent_author_signature, $key, "sha256")) {
1165                                 logger("owner verification failed.");
1166                                 return false;
1167                         }
1168                 }
1169
1170                 $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' LIMIT 1",
1171                         dbesc($message_uri)
1172                 );
1173                 if($r) {
1174                         logger("duplicate message already delivered.", LOGGER_DEBUG);
1175                         return false;
1176                 }
1177
1178                 q("INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
1179                         VALUES (%d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1180                         intval($importer["uid"]),
1181                         dbesc($msg_guid),
1182                         intval($conversation["id"]),
1183                         dbesc($person["name"]),
1184                         dbesc($person["photo"]),
1185                         dbesc($person["url"]),
1186                         intval($contact["id"]),
1187                         dbesc($subject),
1188                         dbesc($body),
1189                         0,
1190                         0,
1191                         dbesc($message_uri),
1192                         dbesc($author.":".$guid),
1193                         dbesc($msg_created_at)
1194                 );
1195
1196                 q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d",
1197                         dbesc(datetime_convert()),
1198                         intval($conversation["id"])
1199                 );
1200
1201                 notification(array(
1202                         "type" => NOTIFY_MAIL,
1203                         "notify_flags" => $importer["notify-flags"],
1204                         "language" => $importer["language"],
1205                         "to_name" => $importer["username"],
1206                         "to_email" => $importer["email"],
1207                         "uid" =>$importer["uid"],
1208                         "item" => array("subject" => $subject, "body" => $body),
1209                         "source_name" => $person["name"],
1210                         "source_link" => $person["url"],
1211                         "source_photo" => $person["thumb"],
1212                         "verb" => ACTIVITY_POST,
1213                         "otype" => "mail"
1214                 ));
1215                 return true;
1216         }
1217
1218         /**
1219          * @brief Processes new private messages (answers to private messages are processed elsewhere)
1220          *
1221          * @param array $importer Array of the importer user
1222          * @param array $msg Array of the processed message, author handle and key
1223          * @param object $data The message object
1224          *
1225          * @return bool Success
1226          */
1227         private function receive_conversation($importer, $msg, $data) {
1228                 $guid = notags(unxmlify($data->guid));
1229                 $subject = notags(unxmlify($data->subject));
1230                 $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
1231                 $author = notags(unxmlify($data->author));
1232                 $participants = notags(unxmlify($data->participants));
1233
1234                 $messages = $data->message;
1235
1236                 if (!count($messages)) {
1237                         logger("empty conversation");
1238                         return false;
1239                 }
1240
1241                 $contact = self::allowed_contact_by_handle($importer, $msg["author"], true);
1242                 if (!$contact)
1243                         return false;
1244
1245                 $conversation = null;
1246
1247                 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1248                         intval($importer["uid"]),
1249                         dbesc($guid)
1250                 );
1251                 if($c)
1252                         $conversation = $c[0];
1253                 else {
1254                         $r = q("INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
1255                                 VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
1256                                 intval($importer["uid"]),
1257                                 dbesc($guid),
1258                                 dbesc($author),
1259                                 dbesc(datetime_convert("UTC", "UTC", $created_at)),
1260                                 dbesc(datetime_convert()),
1261                                 dbesc($subject),
1262                                 dbesc($participants)
1263                         );
1264                         if($r)
1265                                 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1266                                         intval($importer["uid"]),
1267                                         dbesc($guid)
1268                                 );
1269
1270                         if($c)
1271                                 $conversation = $c[0];
1272                 }
1273                 if (!$conversation) {
1274                         logger("unable to create conversation.");
1275                         return;
1276                 }
1277
1278                 foreach($messages as $mesg)
1279                         self::receive_conversation_message($importer, $contact, $data, $msg, $mesg, $conversation);
1280
1281                 return true;
1282         }
1283
1284         /**
1285          * @brief Creates the body for a "like" message
1286          *
1287          * @param array $contact The contact that send us the "like"
1288          * @param array $parent_item The item array of the parent item
1289          * @param string $guid message guid
1290          *
1291          * @return string the body
1292          */
1293         private function construct_like_body($contact, $parent_item, $guid) {
1294                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
1295
1296                 $ulink = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
1297                 $alink = "[url=".$parent_item["author-link"]."]".$parent_item["author-name"]."[/url]";
1298                 $plink = "[url=".App::get_baseurl()."/display/".urlencode($guid)."]".t("status")."[/url]";
1299
1300                 return sprintf($bodyverb, $ulink, $alink, $plink);
1301         }
1302
1303         /**
1304          * @brief Creates a XML object for a "like"
1305          *
1306          * @param array $importer Array of the importer user
1307          * @param array $parent_item The item array of the parent item
1308          *
1309          * @return string The XML
1310          */
1311         private function construct_like_object($importer, $parent_item) {
1312                 $objtype = ACTIVITY_OBJ_NOTE;
1313                 $link = '<link rel="alternate" type="text/html" href="'.App::get_baseurl()."/display/".$importer["nickname"]."/".$parent_item["id"].'" />';
1314                 $parent_body = $parent_item["body"];
1315
1316                 $xmldata = array("object" => array("type" => $objtype,
1317                                                 "local" => "1",
1318                                                 "id" => $parent_item["uri"],
1319                                                 "link" => $link,
1320                                                 "title" => "",
1321                                                 "content" => $parent_body));
1322
1323                 return xml::from_array($xmldata, $xml, true);
1324         }
1325
1326         /**
1327          * @brief Processes "like" messages
1328          *
1329          * @param array $importer Array of the importer user
1330          * @param string $sender The sender of the message
1331          * @param object $data The message object
1332          *
1333          * @return int The message id of the generated like or "false" if there was an error
1334          */
1335         private function receive_like($importer, $sender, $data) {
1336                 $positive = notags(unxmlify($data->positive));
1337                 $guid = notags(unxmlify($data->guid));
1338                 $parent_type = notags(unxmlify($data->parent_type));
1339                 $parent_guid = notags(unxmlify($data->parent_guid));
1340                 $author = notags(unxmlify($data->author));
1341
1342                 // likes on comments aren't supported by Diaspora - only on posts
1343                 // But maybe this will be supported in the future, so we will accept it.
1344                 if (!in_array($parent_type, array("Post", "Comment")))
1345                         return false;
1346
1347                 $contact = self::allowed_contact_by_handle($importer, $sender, true);
1348                 if (!$contact)
1349                         return false;
1350
1351                 $message_id = self::message_exists($importer["uid"], $guid);
1352                 if ($message_id)
1353                         return $message_id;
1354
1355                 $parent_item = self::parent_item($importer["uid"], $parent_guid, $author, $contact);
1356                 if (!$parent_item)
1357                         return false;
1358
1359                 $person = self::person_by_handle($author);
1360                 if (!is_array($person)) {
1361                         logger("unable to find author details");
1362                         return false;
1363                 }
1364
1365                 // Fetch the contact id - if we know this contact
1366                 $author_contact = self::author_contact_by_url($contact, $person, $importer["uid"]);
1367
1368                 // "positive" = "false" would be a Dislike - wich isn't currently supported by Diaspora
1369                 // We would accept this anyhow.
1370                 if ($positive == "true")
1371                         $verb = ACTIVITY_LIKE;
1372                 else
1373                         $verb = ACTIVITY_DISLIKE;
1374
1375                 $datarray = array();
1376
1377                 $datarray["uid"] = $importer["uid"];
1378                 $datarray["contact-id"] = $author_contact["cid"];
1379                 $datarray["network"]  = $author_contact["network"];
1380
1381                 $datarray["author-name"] = $person["name"];
1382                 $datarray["author-link"] = $person["url"];
1383                 $datarray["author-avatar"] = ((x($person,"thumb")) ? $person["thumb"] : $person["photo"]);
1384
1385                 $datarray["owner-name"] = $contact["name"];
1386                 $datarray["owner-link"] = $contact["url"];
1387                 $datarray["owner-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1388
1389                 $datarray["guid"] = $guid;
1390                 $datarray["uri"] = self::get_uri_from_guid($author, $guid);
1391
1392                 $datarray["type"] = "activity";
1393                 $datarray["verb"] = $verb;
1394                 $datarray["gravity"] = GRAVITY_LIKE;
1395                 $datarray["parent-uri"] = $parent_item["uri"];
1396
1397                 $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
1398                 $datarray["object"] = self::construct_like_object($importer, $parent_item);
1399
1400                 $datarray["body"] = self::construct_like_body($contact, $parent_item, $guid);
1401
1402                 $message_id = item_store($datarray);
1403
1404                 if ($message_id)
1405                         logger("Stored like ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
1406
1407                 // If we are the origin of the parent we store the original data and notify our followers
1408                 if($message_id AND $parent_item["origin"]) {
1409
1410                         // Formerly we stored the signed text, the signature and the author in different fields.
1411                         // We now store the raw data so that we are more flexible.
1412                         q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
1413                                 intval($message_id),
1414                                 dbesc(json_encode($data))
1415                         );
1416
1417                         // notify others
1418                         proc_run("php", "include/notifier.php", "comment-import", $message_id);
1419                 }
1420
1421                 return $message_id;
1422         }
1423
1424         /**
1425          * @brief Processes private messages
1426          *
1427          * @param array $importer Array of the importer user
1428          * @param object $data The message object
1429          *
1430          * @return bool Success?
1431          */
1432         private function receive_message($importer, $data) {
1433                 $guid = notags(unxmlify($data->guid));
1434                 $parent_guid = notags(unxmlify($data->parent_guid));
1435                 $text = unxmlify($data->text);
1436                 $created_at = datetime_convert("UTC", "UTC", notags(unxmlify($data->created_at)));
1437                 $author = notags(unxmlify($data->author));
1438                 $conversation_guid = notags(unxmlify($data->conversation_guid));
1439
1440                 $contact = self::allowed_contact_by_handle($importer, $author, true);
1441                 if (!$contact)
1442                         return false;
1443
1444                 $conversation = null;
1445
1446                 $c = q("SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
1447                         intval($importer["uid"]),
1448                         dbesc($conversation_guid)
1449                 );
1450                 if($c)
1451                         $conversation = $c[0];
1452                 else {
1453                         logger("conversation not available.");
1454                         return false;
1455                 }
1456
1457                 $reply = 0;
1458
1459                 $body = diaspora2bb($text);
1460                 $message_uri = $author.":".$guid;
1461
1462                 $person = self::person_by_handle($author);
1463                 if (!$person) {
1464                         logger("unable to find author details");
1465                         return false;
1466                 }
1467
1468                 $r = q("SELECT `id` FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1469                         dbesc($message_uri),
1470                         intval($importer["uid"])
1471                 );
1472                 if($r) {
1473                         logger("duplicate message already delivered.", LOGGER_DEBUG);
1474                         return false;
1475                 }
1476
1477                 q("INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
1478                                 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
1479                         intval($importer["uid"]),
1480                         dbesc($guid),
1481                         intval($conversation["id"]),
1482                         dbesc($person["name"]),
1483                         dbesc($person["photo"]),
1484                         dbesc($person["url"]),
1485                         intval($contact["id"]),
1486                         dbesc($conversation["subject"]),
1487                         dbesc($body),
1488                         0,
1489                         1,
1490                         dbesc($message_uri),
1491                         dbesc($author.":".$parent_guid),
1492                         dbesc($created_at)
1493                 );
1494
1495                 q("UPDATE `conv` SET `updated` = '%s' WHERE `id` = %d",
1496                         dbesc(datetime_convert()),
1497                         intval($conversation["id"])
1498                 );
1499
1500                 return true;
1501         }
1502
1503         /**
1504          * @brief Processes participations - unsupported by now
1505          *
1506          * @param array $importer Array of the importer user
1507          * @param object $data The message object
1508          *
1509          * @return bool always true
1510          */
1511         private function receive_participation($importer, $data) {
1512                 // I'm not sure if we can fully support this message type
1513                 return true;
1514         }
1515
1516         /**
1517          * @brief Processes photos - unneeded
1518          *
1519          * @param array $importer Array of the importer user
1520          * @param object $data The message object
1521          *
1522          * @return bool always true
1523          */
1524         private function receive_photo($importer, $data) {
1525                 // There doesn't seem to be a reason for this function, since the photo data is transmitted in the status message as well
1526                 return true;
1527         }
1528
1529         /**
1530          * @brief Processes poll participations - unssupported
1531          *
1532          * @param array $importer Array of the importer user
1533          * @param object $data The message object
1534          *
1535          * @return bool always true
1536          */
1537         private function receive_poll_participation($importer, $data) {
1538                 // We don't support polls by now
1539                 return true;
1540         }
1541
1542         /**
1543          * @brief Processes incoming profile updates
1544          *
1545          * @param array $importer Array of the importer user
1546          * @param object $data The message object
1547          *
1548          * @return bool Success
1549          */
1550         private function receive_profile($importer, $data) {
1551                 $author = notags(unxmlify($data->author));
1552
1553                 $contact = self::contact_by_handle($importer["uid"], $author);
1554                 if (!$contact)
1555                         return false;
1556
1557                 $name = unxmlify($data->first_name).((strlen($data->last_name)) ? " ".unxmlify($data->last_name) : "");
1558                 $image_url = unxmlify($data->image_url);
1559                 $birthday = unxmlify($data->birthday);
1560                 $location = diaspora2bb(unxmlify($data->location));
1561                 $about = diaspora2bb(unxmlify($data->bio));
1562                 $gender = unxmlify($data->gender);
1563                 $searchable = (unxmlify($data->searchable) == "true");
1564                 $nsfw = (unxmlify($data->nsfw) == "true");
1565                 $tags = unxmlify($data->tag_string);
1566
1567                 $tags = explode("#", $tags);
1568
1569                 $keywords = array();
1570                 foreach ($tags as $tag) {
1571                         $tag = trim(strtolower($tag));
1572                         if ($tag != "")
1573                                 $keywords[] = $tag;
1574                 }
1575
1576                 $keywords = implode(", ", $keywords);
1577
1578                 $handle_parts = explode("@", $author);
1579                 $nick = $handle_parts[0];
1580
1581                 if($name === "")
1582                         $name = $handle_parts[0];
1583
1584                 if( preg_match("|^https?://|", $image_url) === 0)
1585                         $image_url = "http://".$handle_parts[1].$image_url;
1586
1587                 update_contact_avatar($image_url, $importer["uid"], $contact["id"]);
1588
1589                 // Generic birthday. We don't know the timezone. The year is irrelevant.
1590
1591                 $birthday = str_replace("1000", "1901", $birthday);
1592
1593                 if ($birthday != "")
1594                         $birthday = datetime_convert("UTC", "UTC", $birthday, "Y-m-d");
1595
1596                 // this is to prevent multiple birthday notifications in a single year
1597                 // if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year
1598
1599                 if(substr($birthday,5) === substr($contact["bd"],5))
1600                         $birthday = $contact["bd"];
1601
1602                 $r = q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `name-date` = '%s', `bd` = '%s',
1603                                 `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d AND `uid` = %d",
1604                         dbesc($name),
1605                         dbesc($nick),
1606                         dbesc($author),
1607                         dbesc(datetime_convert()),
1608                         dbesc($birthday),
1609                         dbesc($location),
1610                         dbesc($about),
1611                         dbesc($keywords),
1612                         dbesc($gender),
1613                         intval($contact["id"]),
1614                         intval($importer["uid"])
1615                 );
1616
1617                 if ($searchable) {
1618                         poco_check($contact["url"], $name, NETWORK_DIASPORA, $image_url, $about, $location, $gender, $keywords, "",
1619                                 datetime_convert(), 2, $contact["id"], $importer["uid"]);
1620                 }
1621
1622                 $gcontact = array("url" => $contact["url"], "network" => NETWORK_DIASPORA, "generation" => 2,
1623                                         "photo" => $image_url, "name" => $name, "location" => $location,
1624                                         "about" => $about, "birthday" => $birthday, "gender" => $gender,
1625                                         "addr" => $author, "nick" => $nick, "keywords" => $keywords,
1626                                         "hide" => !$searchable, "nsfw" => $nsfw);
1627
1628                 update_gcontact($gcontact);
1629
1630                 logger("Profile of contact ".$contact["id"]." stored for user ".$importer["uid"], LOGGER_DEBUG);
1631
1632                 return true;
1633         }
1634
1635         /**
1636          * @brief Processes incoming friend requests
1637          *
1638          * @param array $importer Array of the importer user
1639          * @param array $contact The contact that send the request
1640          */
1641         private function receive_request_make_friend($importer, $contact) {
1642
1643                 $a = get_app();
1644
1645                 if($contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) {
1646                         q("UPDATE `contact` SET `rel` = %d, `writable` = 1 WHERE `id` = %d AND `uid` = %d",
1647                                 intval(CONTACT_IS_FRIEND),
1648                                 intval($contact["id"]),
1649                                 intval($importer["uid"])
1650                         );
1651                 }
1652                 // send notification
1653
1654                 $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
1655                         intval($importer["uid"])
1656                 );
1657
1658                 if($r && !$r[0]["hide-friends"] && !$contact["hidden"] && intval(get_pconfig($importer["uid"], "system", "post_newfriend"))) {
1659
1660                         $self = q("SELECT * FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1",
1661                                 intval($importer["uid"])
1662                         );
1663
1664                         // they are not CONTACT_IS_FOLLOWER anymore but that's what we have in the array
1665
1666                         if($self && $contact["rel"] == CONTACT_IS_FOLLOWER) {
1667
1668                                 $arr = array();
1669                                 $arr["uri"] = $arr["parent-uri"] = item_new_uri($a->get_hostname(), $importer["uid"]);
1670                                 $arr["uid"] = $importer["uid"];
1671                                 $arr["contact-id"] = $self[0]["id"];
1672                                 $arr["wall"] = 1;
1673                                 $arr["type"] = 'wall';
1674                                 $arr["gravity"] = 0;
1675                                 $arr["origin"] = 1;
1676                                 $arr["author-name"] = $arr["owner-name"] = $self[0]["name"];
1677                                 $arr["author-link"] = $arr["owner-link"] = $self[0]["url"];
1678                                 $arr["author-avatar"] = $arr["owner-avatar"] = $self[0]["thumb"];
1679                                 $arr["verb"] = ACTIVITY_FRIEND;
1680                                 $arr["object-type"] = ACTIVITY_OBJ_PERSON;
1681
1682                                 $A = "[url=".$self[0]["url"]."]".$self[0]["name"]."[/url]";
1683                                 $B = "[url=".$contact["url"]."]".$contact["name"]."[/url]";
1684                                 $BPhoto = "[url=".$contact["url"]."][img]".$contact["thumb"]."[/img][/url]";
1685                                 $arr["body"] = sprintf(t("%1$s is now friends with %2$s"), $A, $B)."\n\n\n".$Bphoto;
1686
1687                                 $arr["object"] = self::construct_new_friend_object($contact);
1688
1689                                 $arr["last-child"] = 1;
1690
1691                                 $arr["allow_cid"] = $user[0]["allow_cid"];
1692                                 $arr["allow_gid"] = $user[0]["allow_gid"];
1693                                 $arr["deny_cid"]  = $user[0]["deny_cid"];
1694                                 $arr["deny_gid"]  = $user[0]["deny_gid"];
1695
1696                                 $i = item_store($arr);
1697                                 if($i)
1698                                         proc_run("php", "include/notifier.php", "activity", $i);
1699                         }
1700                 }
1701         }
1702
1703         /**
1704          * @brief Creates a XML object for a "new friend" message
1705          *
1706          * @param array $contact Array of the contact
1707          *
1708          * @return string The XML
1709          */
1710         private function construct_new_friend_object($contact) {
1711                 $objtype = ACTIVITY_OBJ_PERSON;
1712                 $link = '<link rel="alternate" type="text/html" href="'.$contact["url"].'" />'."\n".
1713                         '<link rel="photo" type="image/jpeg" href="'.$contact["thumb"].'" />'."\n";
1714
1715                 $xmldata = array("object" => array("type" => $objtype,
1716                                                 "title" => $contact["name"],
1717                                                 "id" => $contact["url"]."/".$contact["name"],
1718                                                 "link" => $link));
1719
1720                 return xml::from_array($xmldata, $xml, true);
1721         }
1722
1723         /**
1724          * @brief Processes incoming sharing notification
1725          *
1726          * @param array $importer Array of the importer user
1727          * @param object $data The message object
1728          *
1729          * @return bool Success
1730          */
1731         private function receive_contact_request($importer, $data) {
1732                 $author = unxmlify($data->author);
1733                 $recipient = unxmlify($data->recipient);
1734
1735                 if (!$author || !$recipient)
1736                         return false;
1737
1738                 // the current protocol version doesn't know these fields
1739                 // That means that we will assume their existance
1740                 if (isset($data->following))
1741                         $following = (unxmlify($data->following) == "true");
1742                 else
1743                         $following = true;
1744
1745                 if (isset($data->sharing))
1746                         $sharing = (unxmlify($data->sharing) == "true");
1747                 else
1748                         $sharing = true;
1749
1750                 $contact = self::contact_by_handle($importer["uid"],$author);
1751
1752                 // perhaps we were already sharing with this person. Now they're sharing with us.
1753                 // That makes us friends.
1754                 if ($contact) {
1755                         if ($following AND $sharing) {
1756                                 self::receive_request_make_friend($importer, $contact);
1757                                 return true;
1758                         } else /// @todo Handle all possible variations of adding and retracting of permissions
1759                                 return false;
1760                 }
1761
1762                 if (!$following AND $sharing AND in_array($importer["page-flags"], array(PAGE_SOAPBOX, PAGE_NORMAL))) {
1763                         logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
1764                         return false;
1765                 } elseif (!$following AND !$sharing) {
1766                         logger("Author ".$author." doesn't want anything - and we don't know the author. Request is ignored.", LOGGER_DEBUG);
1767                         return false;
1768                 }
1769
1770                 $ret = self::person_by_handle($author);
1771
1772                 if (!$ret || ($ret["network"] != NETWORK_DIASPORA)) {
1773                         logger("Cannot resolve diaspora handle ".$author." for ".$recipient);
1774                         return false;
1775                 }
1776
1777                 $batch = (($ret["batch"]) ? $ret["batch"] : implode("/", array_slice(explode("/", $ret["url"]), 0, 3))."/receive/public");
1778
1779                 $r = q("INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
1780                         VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
1781                         intval($importer["uid"]),
1782                         dbesc($ret["network"]),
1783                         dbesc($ret["addr"]),
1784                         datetime_convert(),
1785                         dbesc($ret["url"]),
1786                         dbesc(normalise_link($ret["url"])),
1787                         dbesc($batch),
1788                         dbesc($ret["name"]),
1789                         dbesc($ret["nick"]),
1790                         dbesc($ret["photo"]),
1791                         dbesc($ret["pubkey"]),
1792                         dbesc($ret["notify"]),
1793                         dbesc($ret["poll"]),
1794                         1,
1795                         2
1796                 );
1797
1798                 // find the contact record we just created
1799
1800                 $contact_record = self::contact_by_handle($importer["uid"],$author);
1801
1802                 if (!$contact_record) {
1803                         logger("unable to locate newly created contact record.");
1804                         return;
1805                 }
1806
1807                 $def_gid = get_default_group($importer['uid'], $ret["network"]);
1808
1809                 if(intval($def_gid))
1810                         group_add_member($importer["uid"], "", $contact_record["id"], $def_gid);
1811
1812                 if($importer["page-flags"] == PAGE_NORMAL) {
1813
1814                         $hash = random_string().(string)time();   // Generate a confirm_key
1815
1816                         $ret = q("INSERT INTO `intro` (`uid`, `contact-id`, `blocked`, `knowyou`, `note`, `hash`, `datetime`)
1817                                 VALUES (%d, %d, %d, %d, '%s', '%s', '%s')",
1818                                 intval($importer["uid"]),
1819                                 intval($contact_record["id"]),
1820                                 0,
1821                                 0,
1822                                 dbesc(t("Sharing notification from Diaspora network")),
1823                                 dbesc($hash),
1824                                 dbesc(datetime_convert())
1825                         );
1826                 } else {
1827
1828                         // automatic friend approval
1829
1830                         update_contact_avatar($contact_record["photo"],$importer["uid"],$contact_record["id"]);
1831
1832                         // technically they are sharing with us (CONTACT_IS_SHARING),
1833                         // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX
1834                         // we are going to change the relationship and make them a follower.
1835
1836                         if (($importer["page-flags"] == PAGE_FREELOVE) AND $sharing AND $following)
1837                                 $new_relation = CONTACT_IS_FRIEND;
1838                         elseif (($importer["page-flags"] == PAGE_FREELOVE) AND $sharing)
1839                                 $new_relation = CONTACT_IS_SHARING;
1840                         else
1841                                 $new_relation = CONTACT_IS_FOLLOWER;
1842
1843                         $r = q("UPDATE `contact` SET `rel` = %d,
1844                                 `name-date` = '%s',
1845                                 `uri-date` = '%s',
1846                                 `blocked` = 0,
1847                                 `pending` = 0,
1848                                 `writable` = 1
1849                                 WHERE `id` = %d
1850                                 ",
1851                                 intval($new_relation),
1852                                 dbesc(datetime_convert()),
1853                                 dbesc(datetime_convert()),
1854                                 intval($contact_record["id"])
1855                         );
1856
1857                         $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
1858                         if($u)
1859                                 $ret = self::send_share($u[0], $contact_record);
1860                 }
1861
1862                 return true;
1863         }
1864
1865         /**
1866          * @brief Fetches a message with a given guid
1867          *
1868          * @param string $guid message guid
1869          * @param string $orig_author handle of the original post
1870          * @param string $author handle of the sharer
1871          *
1872          * @return array The fetched item
1873          */
1874         private function original_item($guid, $orig_author, $author) {
1875
1876                 // Do we already have this item?
1877                 $r = q("SELECT `body`, `tag`, `app`, `created`, `object-type`, `uri`, `guid`,
1878                                 `author-name`, `author-link`, `author-avatar`
1879                                 FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
1880                         dbesc($guid));
1881
1882                 if($r) {
1883                         logger("reshared message ".$guid." already exists on system.");
1884
1885                         // Maybe it is already a reshared item?
1886                         // Then refetch the content, if it is a reshare from a reshare.
1887                         // If it is a reshared post from another network then reformat to avoid display problems with two share elements
1888                         if (self::is_reshare($r[0]["body"], true))
1889                                 $r = array();
1890                         elseif (self::is_reshare($r[0]["body"], false)) {
1891                                 $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"]));
1892
1893                                 // Add OEmbed and other information to the body
1894                                 $r[0]["body"] = add_page_info_to_body($r[0]["body"], false, true);
1895
1896                                 return $r[0];
1897                         } else
1898                                 return $r[0];
1899                 }
1900
1901                 if (!$r) {
1902                         $server = "https://".substr($orig_author, strpos($orig_author, "@") + 1);
1903                         logger("1st try: reshared message ".$guid." will be fetched from original server: ".$server);
1904                         $item_id = self::store_by_guid($guid, $server);
1905
1906                         if (!$item_id) {
1907                                 $server = "http://".substr($orig_author, strpos($orig_author, "@") + 1);
1908                                 logger("2nd try: reshared message ".$guid." will be fetched from original server: ".$server);
1909                                 $item_id = self::store_by_guid($guid, $server);
1910                         }
1911
1912                         // Deactivated by now since there is a risk that someone could manipulate postings through this method
1913 /*                      if (!$item_id) {
1914                                 $server = "https://".substr($author, strpos($author, "@") + 1);
1915                                 logger("3rd try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
1916                                 $item_id = self::store_by_guid($guid, $server);
1917                         }
1918                         if (!$item_id) {
1919                                 $server = "http://".substr($author, strpos($author, "@") + 1);
1920                                 logger("4th try: reshared message ".$guid." will be fetched from sharer's server: ".$server);
1921                                 $item_id = self::store_by_guid($guid, $server);
1922                         }
1923 */
1924                         if ($item_id) {
1925                                 $r = q("SELECT `body`, `tag`, `app`, `created`, `object-type`, `uri`, `guid`,
1926                                                 `author-name`, `author-link`, `author-avatar`
1927                                         FROM `item` WHERE `id` = %d AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
1928                                         intval($item_id));
1929
1930                                 if ($r) {
1931                                         // If it is a reshared post from another network then reformat to avoid display problems with two share elements
1932                                         if (self::is_reshare($r[0]["body"], false))
1933                                                 $r[0]["body"] = diaspora2bb(bb2diaspora($r[0]["body"]));
1934
1935                                         return $r[0];
1936                                 }
1937
1938                         }
1939                 }
1940                 return false;
1941         }
1942
1943         /**
1944          * @brief Processes a reshare message
1945          *
1946          * @param array $importer Array of the importer user
1947          * @param object $data The message object
1948          * @param string $xml The original XML of the message
1949          *
1950          * @return int the message id
1951          */
1952         private function receive_reshare($importer, $data, $xml) {
1953                 $root_author = notags(unxmlify($data->root_author));
1954                 $root_guid = notags(unxmlify($data->root_guid));
1955                 $guid = notags(unxmlify($data->guid));
1956                 $author = notags(unxmlify($data->author));
1957                 $public = notags(unxmlify($data->public));
1958                 $created_at = notags(unxmlify($data->created_at));
1959
1960                 $contact = self::allowed_contact_by_handle($importer, $author, false);
1961                 if (!$contact)
1962                         return false;
1963
1964                 $message_id = self::message_exists($importer["uid"], $guid);
1965                 if ($message_id)
1966                         return $message_id;
1967
1968                 $original_item = self::original_item($root_guid, $root_author, $author);
1969                 if (!$original_item)
1970                         return false;
1971
1972                 $orig_url = App::get_baseurl()."/display/".$original_item["guid"];
1973
1974                 $datarray = array();
1975
1976                 $datarray["uid"] = $importer["uid"];
1977                 $datarray["contact-id"] = $contact["id"];
1978                 $datarray["network"]  = NETWORK_DIASPORA;
1979
1980                 $datarray["author-name"] = $contact["name"];
1981                 $datarray["author-link"] = $contact["url"];
1982                 $datarray["author-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
1983
1984                 $datarray["owner-name"] = $datarray["author-name"];
1985                 $datarray["owner-link"] = $datarray["author-link"];
1986                 $datarray["owner-avatar"] = $datarray["author-avatar"];
1987
1988                 $datarray["guid"] = $guid;
1989                 $datarray["uri"] = $datarray["parent-uri"] = self::get_uri_from_guid($author, $guid);
1990
1991                 $datarray["verb"] = ACTIVITY_POST;
1992                 $datarray["gravity"] = GRAVITY_PARENT;
1993
1994                 $datarray["object"] = $xml;
1995
1996                 $prefix = share_header($original_item["author-name"], $original_item["author-link"], $original_item["author-avatar"],
1997                                         $original_item["guid"], $original_item["created"], $orig_url);
1998                 $datarray["body"] = $prefix.$original_item["body"]."[/share]";
1999
2000                 $datarray["tag"] = $original_item["tag"];
2001                 $datarray["app"]  = $original_item["app"];
2002
2003                 $datarray["plink"] = self::plink($author, $guid);
2004                 $datarray["private"] = (($public == "false") ? 1 : 0);
2005                 $datarray["changed"] = $datarray["created"] = $datarray["edited"] = datetime_convert("UTC", "UTC", $created_at);
2006
2007                 $datarray["object-type"] = $original_item["object-type"];
2008
2009                 self::fetch_guid($datarray);
2010                 $message_id = item_store($datarray);
2011
2012                 if ($message_id)
2013                         logger("Stored reshare ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
2014
2015                 return $message_id;
2016         }
2017
2018         /**
2019          * @brief Processes retractions
2020          *
2021          * @param array $importer Array of the importer user
2022          * @param array $contact The contact of the item owner
2023          * @param object $data The message object
2024          *
2025          * @return bool success
2026          */
2027         private function item_retraction($importer, $contact, $data) {
2028                 $target_type = notags(unxmlify($data->target_type));
2029                 $target_guid = notags(unxmlify($data->target_guid));
2030                 $author = notags(unxmlify($data->author));
2031
2032                 $person = self::person_by_handle($author);
2033                 if (!is_array($person)) {
2034                         logger("unable to find author detail for ".$author);
2035                         return false;
2036                 }
2037
2038                 $r = q("SELECT `id`, `parent`, `parent-uri`, `author-link` FROM `item` WHERE `guid` = '%s' AND `uid` = %d AND NOT `file` LIKE '%%[%%' LIMIT 1",
2039                         dbesc($target_guid),
2040                         intval($importer["uid"])
2041                 );
2042                 if (!$r)
2043                         return false;
2044
2045                 // Only delete it if the author really fits
2046                 if (!link_compare($r[0]["author-link"], $person["url"])) {
2047                         logger("Item author ".$r[0]["author-link"]." doesn't fit to expected contact ".$person["url"], LOGGER_DEBUG);
2048                         return false;
2049                 }
2050
2051                 // Check if the sender is the thread owner
2052                 $p = q("SELECT `id`, `author-link`, `origin` FROM `item` WHERE `id` = %d",
2053                         intval($r[0]["parent"]));
2054
2055                 // Only delete it if the parent author really fits
2056                 if (!link_compare($p[0]["author-link"], $contact["url"]) AND !link_compare($r[0]["author-link"], $contact["url"])) {
2057                         logger("Thread author ".$p[0]["author-link"]." and item author ".$r[0]["author-link"]." don't fit to expected contact ".$contact["url"], LOGGER_DEBUG);
2058                         return false;
2059                 }
2060
2061                 // Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case
2062                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' , `title` = '' WHERE `id` = %d",
2063                         dbesc(datetime_convert()),
2064                         dbesc(datetime_convert()),
2065                         intval($r[0]["id"])
2066                 );
2067                 delete_thread($r[0]["id"], $r[0]["parent-uri"]);
2068
2069                 logger("Deleted target ".$target_guid." (".$r[0]["id"].") from user ".$importer["uid"]." parent: ".$p[0]["id"], LOGGER_DEBUG);
2070
2071                 // Now check if the retraction needs to be relayed by us
2072                 if($p[0]["origin"]) {
2073                         // notify others
2074                         proc_run("php", "include/notifier.php", "drop", $r[0]["id"]);
2075                 }
2076
2077                 return true;
2078         }
2079
2080         /**
2081          * @brief Receives retraction messages
2082          *
2083          * @param array $importer Array of the importer user
2084          * @param string $sender The sender of the message
2085          * @param object $data The message object
2086          *
2087          * @return bool Success
2088          */
2089         private function receive_retraction($importer, $sender, $data) {
2090                 $target_type = notags(unxmlify($data->target_type));
2091
2092                 $contact = self::contact_by_handle($importer["uid"], $sender);
2093                 if (!$contact) {
2094                         logger("cannot find contact for sender: ".$sender." and user ".$importer["uid"]);
2095                         return false;
2096                 }
2097
2098                 logger("Got retraction for ".$target_type.", sender ".$sender." and user ".$importer["uid"], LOGGER_DEBUG);
2099
2100                 switch ($target_type) {
2101                         case "Comment":
2102                         case "Like":
2103                         case "Post": // "Post" will be supported in a future version
2104                         case "Reshare":
2105                         case "StatusMessage":
2106                                 return self::item_retraction($importer, $contact, $data);;
2107
2108                         case "Contact":
2109                         case "Person":
2110                                 /// @todo What should we do with an "unshare"?
2111                                 // Removing the contact isn't correct since we still can read the public items
2112                                 contact_remove($contact["id"]);
2113                                 return true;
2114
2115                         default:
2116                                 logger("Unknown target type ".$target_type);
2117                                 return false;
2118                 }
2119                 return true;
2120         }
2121
2122         /**
2123          * @brief Receives status messages
2124          *
2125          * @param array $importer Array of the importer user
2126          * @param object $data The message object
2127          * @param string $xml The original XML of the message
2128          *
2129          * @return int The message id of the newly created item
2130          */
2131         private function receive_status_message($importer, $data, $xml) {
2132
2133                 $raw_message = unxmlify($data->raw_message);
2134                 $guid = notags(unxmlify($data->guid));
2135                 $author = notags(unxmlify($data->author));
2136                 $public = notags(unxmlify($data->public));
2137                 $created_at = notags(unxmlify($data->created_at));
2138                 $provider_display_name = notags(unxmlify($data->provider_display_name));
2139
2140                 /// @todo enable support for polls
2141                 //if ($data->poll) {
2142                 //      foreach ($data->poll AS $poll)
2143                 //              print_r($poll);
2144                 //      die("poll!\n");
2145                 //}
2146                 $contact = self::allowed_contact_by_handle($importer, $author, false);
2147                 if (!$contact)
2148                         return false;
2149
2150                 $message_id = self::message_exists($importer["uid"], $guid);
2151                 if ($message_id)
2152                         return $message_id;
2153
2154                 $address = array();
2155                 if ($data->location)
2156                         foreach ($data->location->children() AS $fieldname => $data)
2157                                 $address[$fieldname] = notags(unxmlify($data));
2158
2159                 $body = diaspora2bb($raw_message);
2160
2161                 $datarray = array();
2162
2163                 // Attach embedded pictures to the body
2164                 if ($data->photo) {
2165                         foreach ($data->photo AS $photo)
2166                                 $body = "[img]".unxmlify($photo->remote_photo_path).
2167                                         unxmlify($photo->remote_photo_name)."[/img]\n".$body;
2168
2169                         $datarray["object-type"] = ACTIVITY_OBJ_PHOTO;
2170                 } else {
2171                         $datarray["object-type"] = ACTIVITY_OBJ_NOTE;
2172
2173                         // Add OEmbed and other information to the body
2174                         if (!self::is_redmatrix($contact["url"]))
2175                                 $body = add_page_info_to_body($body, false, true);
2176                 }
2177
2178                 $datarray["uid"] = $importer["uid"];
2179                 $datarray["contact-id"] = $contact["id"];
2180                 $datarray["network"] = NETWORK_DIASPORA;
2181
2182                 $datarray["author-name"] = $contact["name"];
2183                 $datarray["author-link"] = $contact["url"];
2184                 $datarray["author-avatar"] = ((x($contact,"thumb")) ? $contact["thumb"] : $contact["photo"]);
2185
2186                 $datarray["owner-name"] = $datarray["author-name"];
2187                 $datarray["owner-link"] = $datarray["author-link"];
2188                 $datarray["owner-avatar"] = $datarray["author-avatar"];
2189
2190                 $datarray["guid"] = $guid;
2191                 $datarray["uri"] = $datarray["parent-uri"] = self::get_uri_from_guid($author, $guid);
2192
2193                 $datarray["verb"] = ACTIVITY_POST;
2194                 $datarray["gravity"] = GRAVITY_PARENT;
2195
2196                 $datarray["object"] = $xml;
2197
2198                 $datarray["body"] = $body;
2199
2200                 if ($provider_display_name != "")
2201                         $datarray["app"] = $provider_display_name;
2202
2203                 $datarray["plink"] = self::plink($author, $guid);
2204                 $datarray["private"] = (($public == "false") ? 1 : 0);
2205                 $datarray["changed"] = $datarray["created"] = $datarray["edited"] = datetime_convert("UTC", "UTC", $created_at);
2206
2207                 if (isset($address["address"]))
2208                         $datarray["location"] = $address["address"];
2209
2210                 if (isset($address["lat"]) AND isset($address["lng"]))
2211                         $datarray["coord"] = $address["lat"]." ".$address["lng"];
2212
2213                 self::fetch_guid($datarray);
2214                 $message_id = item_store($datarray);
2215
2216                 if ($message_id)
2217                         logger("Stored item ".$datarray["guid"]." with message id ".$message_id, LOGGER_DEBUG);
2218
2219                 return $message_id;
2220         }
2221
2222         /* ************************************************************************************** *
2223          * Here are all the functions that are needed to transmit data with the Diaspora protocol *
2224          * ************************************************************************************** */
2225
2226         /**
2227          * @brief returnes the handle of a contact
2228          *
2229          * @param array $me contact array
2230          *
2231          * @return string the handle in the format user@domain.tld
2232          */
2233         private function my_handle($contact) {
2234                 if ($contact["addr"] != "")
2235                         return $contact["addr"];
2236
2237                 // Normally we should have a filled "addr" field - but in the past this wasn't the case
2238                 // So - just in case - we build the the address here.
2239                 if ($contact["nickname"] != "")
2240                         $nick = $contact["nickname"];
2241                 else
2242                         $nick = $contact["nick"];
2243
2244                 return $nick."@".substr(App::get_baseurl(), strpos(App::get_baseurl(),"://") + 3);
2245         }
2246
2247         /**
2248          * @brief Creates the envelope for a public message
2249          *
2250          * @param string $msg The message that is to be transmitted
2251          * @param array $user The record of the sender
2252          * @param array $contact Target of the communication
2253          * @param string $prvkey The private key of the sender
2254          * @param string $pubkey The public key of the receiver
2255          *
2256          * @return string The envelope
2257          */
2258         private function build_public_message($msg, $user, $contact, $prvkey, $pubkey) {
2259
2260                 logger("Message: ".$msg, LOGGER_DATA);
2261
2262                 $handle = self::my_handle($user);
2263
2264                 $b64url_data = base64url_encode($msg);
2265
2266                 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
2267
2268                 $type = "application/xml";
2269                 $encoding = "base64url";
2270                 $alg = "RSA-SHA256";
2271
2272                 $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
2273
2274                 $signature = rsa_sign($signable_data,$prvkey);
2275                 $sig = base64url_encode($signature);
2276
2277                 $xmldata = array("diaspora" => array("header" => array("author_id" => $handle),
2278                                                 "me:env" => array("me:encoding" => "base64url",
2279                                                                 "me:alg" => "RSA-SHA256",
2280                                                                 "me:data" => $data,
2281                                                                 "@attributes" => array("type" => "application/xml"),
2282                                                                 "me:sig" => $sig)));
2283
2284                 $namespaces = array("" => "https://joindiaspora.com/protocol",
2285                                 "me" => "http://salmon-protocol.org/ns/magic-env");
2286
2287                 $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
2288
2289                 logger("magic_env: ".$magic_env, LOGGER_DATA);
2290                 return $magic_env;
2291         }
2292
2293         /**
2294          * @brief Creates the envelope for a private message
2295          *
2296          * @param string $msg The message that is to be transmitted
2297          * @param array $user The record of the sender
2298          * @param array $contact Target of the communication
2299          * @param string $prvkey The private key of the sender
2300          * @param string $pubkey The public key of the receiver
2301          *
2302          * @return string The envelope
2303          */
2304         private function build_private_message($msg, $user, $contact, $prvkey, $pubkey) {
2305
2306                 logger("Message: ".$msg, LOGGER_DATA);
2307
2308                 // without a public key nothing will work
2309
2310                 if (!$pubkey) {
2311                         logger("pubkey missing: contact id: ".$contact["id"]);
2312                         return false;
2313                 }
2314
2315                 $inner_aes_key = random_string(32);
2316                 $b_inner_aes_key = base64_encode($inner_aes_key);
2317                 $inner_iv = random_string(16);
2318                 $b_inner_iv = base64_encode($inner_iv);
2319
2320                 $outer_aes_key = random_string(32);
2321                 $b_outer_aes_key = base64_encode($outer_aes_key);
2322                 $outer_iv = random_string(16);
2323                 $b_outer_iv = base64_encode($outer_iv);
2324
2325                 $handle = self::my_handle($user);
2326
2327                 $padded_data = pkcs5_pad($msg,16);
2328                 $inner_encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $inner_aes_key, $padded_data, MCRYPT_MODE_CBC, $inner_iv);
2329
2330                 $b64_data = base64_encode($inner_encrypted);
2331
2332
2333                 $b64url_data = base64url_encode($b64_data);
2334                 $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
2335
2336                 $type = "application/xml";
2337                 $encoding = "base64url";
2338                 $alg = "RSA-SHA256";
2339
2340                 $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
2341
2342                 $signature = rsa_sign($signable_data,$prvkey);
2343                 $sig = base64url_encode($signature);
2344
2345                 $xmldata = array("decrypted_header" => array("iv" => $b_inner_iv,
2346                                                         "aes_key" => $b_inner_aes_key,
2347                                                         "author_id" => $handle));
2348
2349                 $decrypted_header = xml::from_array($xmldata, $xml, true);
2350                 $decrypted_header = pkcs5_pad($decrypted_header,16);
2351
2352                 $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $outer_aes_key, $decrypted_header, MCRYPT_MODE_CBC, $outer_iv);
2353
2354                 $outer_json = json_encode(array("iv" => $b_outer_iv, "key" => $b_outer_aes_key));
2355
2356                 $encrypted_outer_key_bundle = "";
2357                 openssl_public_encrypt($outer_json, $encrypted_outer_key_bundle, $pubkey);
2358
2359                 $b64_encrypted_outer_key_bundle = base64_encode($encrypted_outer_key_bundle);
2360
2361                 logger("outer_bundle: ".$b64_encrypted_outer_key_bundle." key: ".$pubkey, LOGGER_DATA);
2362
2363                 $encrypted_header_json_object = json_encode(array("aes_key" => base64_encode($encrypted_outer_key_bundle),
2364                                                                 "ciphertext" => base64_encode($ciphertext)));
2365                 $cipher_json = base64_encode($encrypted_header_json_object);
2366
2367                 $xmldata = array("diaspora" => array("encrypted_header" => $cipher_json,
2368                                                 "me:env" => array("me:encoding" => "base64url",
2369                                                                 "me:alg" => "RSA-SHA256",
2370                                                                 "me:data" => $data,
2371                                                                 "@attributes" => array("type" => "application/xml"),
2372                                                                 "me:sig" => $sig)));
2373
2374                 $namespaces = array("" => "https://joindiaspora.com/protocol",
2375                                 "me" => "http://salmon-protocol.org/ns/magic-env");
2376
2377                 $magic_env = xml::from_array($xmldata, $xml, false, $namespaces);
2378
2379                 logger("magic_env: ".$magic_env, LOGGER_DATA);
2380                 return $magic_env;
2381         }
2382
2383         /**
2384          * @brief Create the envelope for a message
2385          *
2386          * @param string $msg The message that is to be transmitted
2387          * @param array $user The record of the sender
2388          * @param array $contact Target of the communication
2389          * @param string $prvkey The private key of the sender
2390          * @param string $pubkey The public key of the receiver
2391          * @param bool $public Is the message public?
2392          *
2393          * @return string The message that will be transmitted to other servers
2394          */
2395         private function build_message($msg, $user, $contact, $prvkey, $pubkey, $public = false) {
2396
2397                 if ($public)
2398                         $magic_env =  self::build_public_message($msg,$user,$contact,$prvkey,$pubkey);
2399                 else
2400                         $magic_env =  self::build_private_message($msg,$user,$contact,$prvkey,$pubkey);
2401
2402                 // The data that will be transmitted is double encoded via "urlencode", strange ...
2403                 $slap = "xml=".urlencode(urlencode($magic_env));
2404                 return $slap;
2405         }
2406
2407         /**
2408          * @brief Creates a signature for a message
2409          *
2410          * @param array $owner the array of the owner of the message
2411          * @param array $message The message that is to be signed
2412          *
2413          * @return string The signature
2414          */
2415         private function signature($owner, $message) {
2416                 $sigmsg = $message;
2417                 unset($sigmsg["author_signature"]);
2418                 unset($sigmsg["parent_author_signature"]);
2419
2420                 $signed_text = implode(";", $sigmsg);
2421
2422                 return base64_encode(rsa_sign($signed_text, $owner["uprvkey"], "sha256"));
2423         }
2424
2425         /**
2426          * @brief Transmit a message to a target server
2427          *
2428          * @param array $owner the array of the item owner
2429          * @param array $contact Target of the communication
2430          * @param string $slap The message that is to be transmitted
2431          * @param bool $public_batch Is it a public post?
2432          * @param bool $queue_run Is the transmission called from the queue?
2433          * @param string $guid message guid
2434          *
2435          * @return int Result of the transmission
2436          */
2437         public static function transmit($owner, $contact, $slap, $public_batch, $queue_run=false, $guid = "") {
2438
2439                 $a = get_app();
2440
2441                 $enabled = intval(get_config("system", "diaspora_enabled"));
2442                 if(!$enabled)
2443                         return 200;
2444
2445                 $logid = random_string(4);
2446                 $dest_url = (($public_batch) ? $contact["batch"] : $contact["notify"]);
2447                 if (!$dest_url) {
2448                         logger("no url for contact: ".$contact["id"]." batch mode =".$public_batch);
2449                         return 0;
2450                 }
2451
2452                 logger("transmit: ".$logid."-".$guid." ".$dest_url);
2453
2454                 if (!$queue_run && was_recently_delayed($contact["id"])) {
2455                         $return_code = 0;
2456                 } else {
2457                         if (!intval(get_config("system", "diaspora_test"))) {
2458                                 post_url($dest_url."/", $slap);
2459                                 $return_code = $a->get_curl_code();
2460                         } else {
2461                                 logger("test_mode");
2462                                 return 200;
2463                         }
2464                 }
2465
2466                 logger("transmit: ".$logid."-".$guid." returns: ".$return_code);
2467
2468                 if(!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
2469                         logger("queue message");
2470
2471                         $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d AND `network` = '%s' AND `content` = '%s' AND `batch` = %d LIMIT 1",
2472                                 intval($contact["id"]),
2473                                 dbesc(NETWORK_DIASPORA),
2474                                 dbesc($slap),
2475                                 intval($public_batch)
2476                         );
2477                         if($r) {
2478                                 logger("add_to_queue ignored - identical item already in queue");
2479                         } else {
2480                                 // queue message for redelivery
2481                                 add_to_queue($contact["id"], NETWORK_DIASPORA, $slap, $public_batch);
2482                         }
2483                 }
2484
2485                 return(($return_code) ? $return_code : (-1));
2486         }
2487
2488
2489         /**
2490          * @brief Builds and transmit messages
2491          *
2492          * @param array $owner the array of the item owner
2493          * @param array $contact Target of the communication
2494          * @param string $type The message type
2495          * @param array $message The message data
2496          * @param bool $public_batch Is it a public post?
2497          * @param string $guid message guid
2498          * @param bool $spool Should the transmission be spooled or transmitted?
2499          *
2500          * @return int Result of the transmission
2501          */
2502         private function build_and_transmit($owner, $contact, $type, $message, $public_batch = false, $guid = "", $spool = false) {
2503
2504                 $data = array("XML" => array("post" => array($type => $message)));
2505
2506                 $msg = xml::from_array($data, $xml);
2507
2508                 logger('message: '.$msg, LOGGER_DATA);
2509                 logger('send guid '.$guid, LOGGER_DEBUG);
2510
2511                 $slap = self::build_message($msg, $owner, $contact, $owner['uprvkey'], $contact['pubkey'], $public_batch);
2512
2513                 if ($spool) {
2514                         add_to_queue($contact['id'], NETWORK_DIASPORA, $slap, $public_batch);
2515                         return true;
2516                 } else
2517                         $return_code = self::transmit($owner, $contact, $slap, $public_batch, false, $guid);
2518
2519                 logger("guid: ".$item["guid"]." result ".$return_code, LOGGER_DEBUG);
2520
2521                 return $return_code;
2522         }
2523
2524         /**
2525          * @brief Sends a "share" message
2526          *
2527          * @param array $owner the array of the item owner
2528          * @param array $contact Target of the communication
2529          *
2530          * @return int The result of the transmission
2531          */
2532         public static function send_share($owner,$contact) {
2533
2534                 $message = array("sender_handle" => self::my_handle($owner),
2535                                 "recipient_handle" => $contact["addr"]);
2536
2537                 return self::build_and_transmit($owner, $contact, "request", $message);
2538         }
2539
2540         /**
2541          * @brief sends an "unshare"
2542          *
2543          * @param array $owner the array of the item owner
2544          * @param array $contact Target of the communication
2545          *
2546          * @return int The result of the transmission
2547          */
2548         public static function send_unshare($owner,$contact) {
2549
2550                 $message = array("post_guid" => $owner["guid"],
2551                                 "diaspora_handle" => self::my_handle($owner),
2552                                 "type" => "Person");
2553
2554                 return self::build_and_transmit($owner, $contact, "retraction", $message);
2555         }
2556
2557         /**
2558          * @brief Checks a message body if it is a reshare
2559          *
2560          * @param string $body The message body that is to be check
2561          * @param bool $complete Should it be a complete check or a simple check?
2562          *
2563          * @return array|bool Reshare details or "false" if no reshare
2564          */
2565         public static function is_reshare($body, $complete = true) {
2566                 $body = trim($body);
2567
2568                 // Skip if it isn't a pure repeated messages
2569                 // Does it start with a share?
2570                 if ((strpos($body, "[share") > 0) AND $complete)
2571                         return(false);
2572
2573                 // Does it end with a share?
2574                 if (strlen($body) > (strrpos($body, "[/share]") + 8))
2575                         return(false);
2576
2577                 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
2578                 // Skip if there is no shared message in there
2579                 if ($body == $attributes)
2580                         return(false);
2581
2582                 // If we don't do the complete check we quit here
2583                 if (!$complete)
2584                         return true;
2585
2586                 $guid = "";
2587                 preg_match("/guid='(.*?)'/ism", $attributes, $matches);
2588                 if ($matches[1] != "")
2589                         $guid = $matches[1];
2590
2591                 preg_match('/guid="(.*?)"/ism', $attributes, $matches);
2592                 if ($matches[1] != "")
2593                         $guid = $matches[1];
2594
2595                 if ($guid != "") {
2596                         $r = q("SELECT `contact-id` FROM `item` WHERE `guid` = '%s' AND `network` IN ('%s', '%s') LIMIT 1",
2597                                 dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
2598                         if ($r) {
2599                                 $ret= array();
2600                                 $ret["root_handle"] = self::handle_from_contact($r[0]["contact-id"]);
2601                                 $ret["root_guid"] = $guid;
2602                                 return($ret);
2603                         }
2604                 }
2605
2606                 $profile = "";
2607                 preg_match("/profile='(.*?)'/ism", $attributes, $matches);
2608                 if ($matches[1] != "")
2609                         $profile = $matches[1];
2610
2611                 preg_match('/profile="(.*?)"/ism', $attributes, $matches);
2612                 if ($matches[1] != "")
2613                         $profile = $matches[1];
2614
2615                 $ret= array();
2616
2617                 $ret["root_handle"] = preg_replace("=https?://(.*)/u/(.*)=ism", "$2@$1", $profile);
2618                 if (($ret["root_handle"] == $profile) OR ($ret["root_handle"] == ""))
2619                         return(false);
2620
2621                 $link = "";
2622                 preg_match("/link='(.*?)'/ism", $attributes, $matches);
2623                 if ($matches[1] != "")
2624                         $link = $matches[1];
2625
2626                 preg_match('/link="(.*?)"/ism', $attributes, $matches);
2627                 if ($matches[1] != "")
2628                         $link = $matches[1];
2629
2630                 $ret["root_guid"] = preg_replace("=https?://(.*)/posts/(.*)=ism", "$2", $link);
2631                 if (($ret["root_guid"] == $link) OR (trim($ret["root_guid"]) == ""))
2632                         return(false);
2633
2634                 return($ret);
2635         }
2636
2637         /**
2638          * @brief Sends a post
2639          *
2640          * @param array $item The item that will be exported
2641          * @param array $owner the array of the item owner
2642          * @param array $contact Target of the communication
2643          * @param bool $public_batch Is it a public post?
2644          *
2645          * @return int The result of the transmission
2646          */
2647         public static function send_status($item, $owner, $contact, $public_batch = false) {
2648
2649                 $myaddr = self::my_handle($owner);
2650
2651                 $public = (($item["private"]) ? "false" : "true");
2652
2653                 $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d H:i:s \U\T\C');
2654
2655                 // Detect a share element and do a reshare
2656                 if (!$item['private'] AND ($ret = self::is_reshare($item["body"]))) {
2657                         $message = array("root_diaspora_id" => $ret["root_handle"],
2658                                         "root_guid" => $ret["root_guid"],
2659                                         "guid" => $item["guid"],
2660                                         "diaspora_handle" => $myaddr,
2661                                         "public" => $public,
2662                                         "created_at" => $created,
2663                                         "provider_display_name" => $item["app"]);
2664
2665                         $type = "reshare";
2666                 } else {
2667                         $title = $item["title"];
2668                         $body = $item["body"];
2669
2670                         // convert to markdown
2671                         $body = html_entity_decode(bb2diaspora($body));
2672
2673                         // Adding the title
2674                         if(strlen($title))
2675                                 $body = "## ".html_entity_decode($title)."\n\n".$body;
2676
2677                         if ($item["attach"]) {
2678                                 $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
2679                                 if(cnt) {
2680                                         $body .= "\n".t("Attachments:")."\n";
2681                                         foreach($matches as $mtch)
2682                                                 $body .= "[".$mtch[3]."](".$mtch[1].")\n";
2683                                 }
2684                         }
2685
2686                         $location = array();
2687
2688                         if ($item["location"] != "")
2689                                 $location["address"] = $item["location"];
2690
2691                         if ($item["coord"] != "") {
2692                                 $coord = explode(" ", $item["coord"]);
2693                                 $location["lat"] = $coord[0];
2694                                 $location["lng"] = $coord[1];
2695                         }
2696
2697                         $message = array("raw_message" => $body,
2698                                         "location" => $location,
2699                                         "guid" => $item["guid"],
2700                                         "diaspora_handle" => $myaddr,
2701                                         "public" => $public,
2702                                         "created_at" => $created,
2703                                         "provider_display_name" => $item["app"]);
2704
2705                         if (count($location) == 0)
2706                                 unset($message["location"]);
2707
2708                         $type = "status_message";
2709                 }
2710
2711                 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2712         }
2713
2714         /**
2715          * @brief Creates a "like" object
2716          *
2717          * @param array $item The item that will be exported
2718          * @param array $owner the array of the item owner
2719          *
2720          * @return array The data for a "like"
2721          */
2722         private function construct_like($item, $owner) {
2723
2724                 $p = q("SELECT `guid`, `uri`, `parent-uri` FROM `item` WHERE `uri` = '%s' LIMIT 1",
2725                         dbesc($item["thr-parent"]));
2726                 if(!$p)
2727                         return false;
2728
2729                 $parent = $p[0];
2730
2731                 $target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
2732                 $positive = "true";
2733
2734                 return(array("positive" => $positive,
2735                                 "guid" => $item["guid"],
2736                                 "target_type" => $target_type,
2737                                 "parent_guid" => $parent["guid"],
2738                                 "author_signature" => "",
2739                                 "diaspora_handle" => self::my_handle($owner)));
2740         }
2741
2742         /**
2743          * @brief Creates the object for a comment
2744          *
2745          * @param array $item The item that will be exported
2746          * @param array $owner the array of the item owner
2747          *
2748          * @return array The data for a comment
2749          */
2750         private function construct_comment($item, $owner) {
2751
2752                 $p = q("SELECT `guid` FROM `item` WHERE `parent` = %d AND `id` = %d LIMIT 1",
2753                         intval($item["parent"]),
2754                         intval($item["parent"])
2755                 );
2756
2757                 if (!$p)
2758                         return false;
2759
2760                 $parent = $p[0];
2761
2762                 $text = html_entity_decode(bb2diaspora($item["body"]));
2763
2764                 return(array("guid" => $item["guid"],
2765                                 "parent_guid" => $parent["guid"],
2766                                 "author_signature" => "",
2767                                 "text" => $text,
2768                                 "diaspora_handle" => self::my_handle($owner)));
2769         }
2770
2771         /**
2772          * @brief Send a like or a comment
2773          *
2774          * @param array $item The item that will be exported
2775          * @param array $owner the array of the item owner
2776          * @param array $contact Target of the communication
2777          * @param bool $public_batch Is it a public post?
2778          *
2779          * @return int The result of the transmission
2780          */
2781         public static function send_followup($item,$owner,$contact,$public_batch = false) {
2782
2783                 if($item['verb'] === ACTIVITY_LIKE) {
2784                         $message = self::construct_like($item, $owner);
2785                         $type = "like";
2786                 } else {
2787                         $message = self::construct_comment($item, $owner);
2788                         $type = "comment";
2789                 }
2790
2791                 if (!$message)
2792                         return false;
2793
2794                 $message["author_signature"] = self::signature($owner, $message);
2795
2796                 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2797         }
2798
2799         /**
2800          * @brief Creates a message from a signature record entry
2801          *
2802          * @param array $item The item that will be exported
2803          * @param array $signature The entry of the "sign" record
2804          *
2805          * @return string The message
2806          */
2807         private function message_from_signature($item, $signature) {
2808
2809                 // Split the signed text
2810                 $signed_parts = explode(";", $signature['signed_text']);
2811
2812                 if ($item["deleted"])
2813                         $message = array("parent_author_signature" => "",
2814                                         "target_guid" => $signed_parts[0],
2815                                         "target_type" => $signed_parts[1],
2816                                         "sender_handle" => $signature['signer'],
2817                                         "target_author_signature" => $signature['signature']);
2818                 elseif ($item['verb'] === ACTIVITY_LIKE)
2819                         $message = array("positive" => $signed_parts[0],
2820                                         "guid" => $signed_parts[1],
2821                                         "target_type" => $signed_parts[2],
2822                                         "parent_guid" => $signed_parts[3],
2823                                         "parent_author_signature" => "",
2824                                         "author_signature" => $signature['signature'],
2825                                         "diaspora_handle" => $signed_parts[4]);
2826                 else {
2827                         // Remove the comment guid
2828                         $guid = array_shift($signed_parts);
2829
2830                         // Remove the parent guid
2831                         $parent_guid = array_shift($signed_parts);
2832
2833                         // Remove the handle
2834                         $handle = array_pop($signed_parts);
2835
2836                         // Glue the parts together
2837                         $text = implode(";", $signed_parts);
2838
2839                         $message = array("guid" => $guid,
2840                                         "parent_guid" => $parent_guid,
2841                                         "parent_author_signature" => "",
2842                                         "author_signature" => $signature['signature'],
2843                                         "text" => implode(";", $signed_parts),
2844                                         "diaspora_handle" => $handle);
2845                 }
2846                 return $message;
2847         }
2848
2849         /**
2850          * @brief Relays messages (like, comment, retraction) to other servers if we are the thread owner
2851          *
2852          * @param array $item The item that will be exported
2853          * @param array $owner the array of the item owner
2854          * @param array $contact Target of the communication
2855          * @param bool $public_batch Is it a public post?
2856          *
2857          * @return int The result of the transmission
2858          */
2859         public static function send_relay($item, $owner, $contact, $public_batch = false) {
2860
2861                 if ($item["deleted"])
2862                         return self::send_retraction($item, $owner, $contact, $public_batch, true);
2863                 elseif ($item['verb'] === ACTIVITY_LIKE)
2864                         $type = "like";
2865                 else
2866                         $type = "comment";
2867
2868                 logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
2869
2870                 // fetch the original signature
2871
2872                 $r = q("SELECT `signed_text`, `signature`, `signer` FROM `sign` WHERE `iid` = %d LIMIT 1",
2873                         intval($item["id"]));
2874
2875                 if (!$r) {
2876                         logger("Couldn't fetch signatur for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG);
2877                         return false;
2878                 }
2879
2880                 $signature = $r[0];
2881
2882                 // Old way - is used by the internal Friendica functions
2883                 /// @todo Change all signatur storing functions to the new format
2884                 if ($signature['signed_text'] AND $signature['signature'] AND $signature['signer'])
2885                         $message = self::message_from_signature($item, $signature);
2886                 else {// New way
2887                         $msg = json_decode($signature['signed_text'], true);
2888
2889                         $message = array();
2890                         if (is_array($msg)) {
2891                                 foreach ($msg AS $field => $data) {
2892                                         if (!$item["deleted"]) {
2893                                                 if ($field == "author")
2894                                                         $field = "diaspora_handle";
2895                                                 if ($field == "parent_type")
2896                                                         $field = "target_type";
2897                                         }
2898
2899                                         $message[$field] = $data;
2900                                 }
2901                         } else
2902                                 logger("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$signature['signed_text'], LOGGER_DEBUG);
2903                 }
2904
2905                 $message["parent_author_signature"] = self::signature($owner, $message);
2906
2907                 logger("Relayed data ".print_r($message, true), LOGGER_DEBUG);
2908
2909                 return self::build_and_transmit($owner, $contact, $type, $message, $public_batch, $item["guid"]);
2910         }
2911
2912         /**
2913          * @brief Sends a retraction (deletion) of a message, like or comment
2914          *
2915          * @param array $item The item that will be exported
2916          * @param array $owner the array of the item owner
2917          * @param array $contact Target of the communication
2918          * @param bool $public_batch Is it a public post?
2919          * @param bool $relay Is the retraction transmitted from a relay?
2920          *
2921          * @return int The result of the transmission
2922          */
2923         public static function send_retraction($item, $owner, $contact, $public_batch = false, $relay = false) {
2924
2925                 $itemaddr = self::handle_from_contact($item["contact-id"], $item["gcontact-id"]);
2926
2927                 // Check whether the retraction is for a top-level post or whether it's a relayable
2928                 if ($item["uri"] !== $item["parent-uri"]) {
2929                         $msg_type = "relayable_retraction";
2930                         $target_type = (($item["verb"] === ACTIVITY_LIKE) ? "Like" : "Comment");
2931                 } else {
2932                         $msg_type = "signed_retraction";
2933                         $target_type = "StatusMessage";
2934                 }
2935
2936                 if ($relay AND ($item["uri"] !== $item["parent-uri"]))
2937                         $signature = "parent_author_signature";
2938                 else
2939                         $signature = "target_author_signature";
2940
2941                 $signed_text = $item["guid"].";".$target_type;
2942
2943                 $message = array("target_guid" => $item['guid'],
2944                                 "target_type" => $target_type,
2945                                 "sender_handle" => $itemaddr,
2946                                 $signature => base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256')));
2947
2948                 logger("Got message ".print_r($message, true), LOGGER_DEBUG);
2949
2950                 return self::build_and_transmit($owner, $contact, $msg_type, $message, $public_batch, $item["guid"]);
2951         }
2952
2953         /**
2954          * @brief Sends a mail
2955          *
2956          * @param array $item The item that will be exported
2957          * @param array $owner The owner
2958          * @param array $contact Target of the communication
2959          *
2960          * @return int The result of the transmission
2961          */
2962         public static function send_mail($item, $owner, $contact) {
2963
2964                 $myaddr = self::my_handle($owner);
2965
2966                 $r = q("SELECT * FROM `conv` WHERE `id` = %d AND `uid` = %d LIMIT 1",
2967                         intval($item["convid"]),
2968                         intval($item["uid"])
2969                 );
2970
2971                 if (!$r) {
2972                         logger("conversation not found.");
2973                         return;
2974                 }
2975                 $cnv = $r[0];
2976
2977                 $conv = array(
2978                         "guid" => $cnv["guid"],
2979                         "subject" => $cnv["subject"],
2980                         "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d H:i:s \U\T\C'),
2981                         "diaspora_handle" => $cnv["creator"],
2982                         "participant_handles" => $cnv["recips"]
2983                 );
2984
2985                 $body = bb2diaspora($item["body"]);
2986                 $created = datetime_convert("UTC", "UTC", $item["created"], 'Y-m-d H:i:s \U\T\C');
2987
2988                 $signed_text = $item["guid"].";".$cnv["guid"].";".$body.";".$created.";".$myaddr.";".$cnv['guid'];
2989                 $sig = base64_encode(rsa_sign($signed_text, $owner["uprvkey"], "sha256"));
2990
2991                 $msg = array(
2992                         "guid" => $item["guid"],
2993                         "parent_guid" => $cnv["guid"],
2994                         "parent_author_signature" => $sig,
2995                         "author_signature" => $sig,
2996                         "text" => $body,
2997                         "created_at" => $created,
2998                         "diaspora_handle" => $myaddr,
2999                         "conversation_guid" => $cnv["guid"]
3000                 );
3001
3002                 if ($item["reply"]) {
3003                         $message = $msg;
3004                         $type = "message";
3005                 } else {
3006                         $message = array("guid" => $cnv["guid"],
3007                                         "subject" => $cnv["subject"],
3008                                         "created_at" => datetime_convert("UTC", "UTC", $cnv['created'], 'Y-m-d H:i:s \U\T\C'),
3009                                         "message" => $msg,
3010                                         "diaspora_handle" => $cnv["creator"],
3011                                         "participant_handles" => $cnv["recips"]);
3012
3013                         $type = "conversation";
3014                 }
3015
3016                 return self::build_and_transmit($owner, $contact, $type, $message, false, $item["guid"]);
3017         }
3018
3019         /**
3020          * @brief Sends profile data
3021          *
3022          * @param int $uid The user id
3023          */
3024         public static function send_profile($uid) {
3025
3026                 if (!$uid)
3027                         return;
3028
3029                 $recips = q("SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
3030                         AND `uid` = %d AND `rel` != %d",
3031                         dbesc(NETWORK_DIASPORA),
3032                         intval($uid),
3033                         intval(CONTACT_IS_SHARING)
3034                 );
3035                 if (!$recips)
3036                         return;
3037
3038                 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.*, `user`.`prvkey` AS `uprvkey`, `contact`.`addr`
3039                         FROM `profile`
3040                         INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
3041                         INNER JOIN `contact` ON `profile`.`uid` = `contact`.`uid`
3042                         WHERE `user`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
3043                         intval($uid)
3044                 );
3045
3046                 if (!$r)
3047                         return;
3048
3049                 $profile = $r[0];
3050
3051                 $handle = $profile["addr"];
3052                 $first = ((strpos($profile['name'],' ')
3053                         ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']));
3054                 $last = (($first === $profile['name']) ? '' : trim(substr($profile['name'], strlen($first))));
3055                 $large = App::get_baseurl().'/photo/custom/300/'.$profile['uid'].'.jpg';
3056                 $medium = App::get_baseurl().'/photo/custom/100/'.$profile['uid'].'.jpg';
3057                 $small = App::get_baseurl().'/photo/custom/50/'  .$profile['uid'].'.jpg';
3058                 $searchable = (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false');
3059
3060                 if ($searchable === 'true') {
3061                         $dob = '1000-00-00';
3062
3063                         if (($profile['dob']) && ($profile['dob'] != '0000-00-00'))
3064                                 $dob = ((intval($profile['dob'])) ? intval($profile['dob']) : '1000') .'-'. datetime_convert('UTC','UTC',$profile['dob'],'m-d');
3065
3066                         $about = $profile['about'];
3067                         $about = strip_tags(bbcode($about));
3068
3069                         $location = formatted_location($profile);
3070                         $tags = '';
3071                         if ($profile['pub_keywords']) {
3072                                 $kw = str_replace(',',' ',$profile['pub_keywords']);
3073                                 $kw = str_replace('  ',' ',$kw);
3074                                 $arr = explode(' ',$profile['pub_keywords']);
3075                                 if (count($arr)) {
3076                                         for($x = 0; $x < 5; $x ++) {
3077                                                 if (trim($arr[$x]))
3078                                                         $tags .= '#'. trim($arr[$x]) .' ';
3079                                         }
3080                                 }
3081                         }
3082                         $tags = trim($tags);
3083                 }
3084
3085                 $message = array("diaspora_handle" => $handle,
3086                                 "first_name" => $first,
3087                                 "last_name" => $last,
3088                                 "image_url" => $large,
3089                                 "image_url_medium" => $medium,
3090                                 "image_url_small" => $small,
3091                                 "birthday" => $dob,
3092                                 "gender" => $profile['gender'],
3093                                 "bio" => $about,
3094                                 "location" => $location,
3095                                 "searchable" => $searchable,
3096                                 "tag_string" => $tags);
3097
3098                 foreach($recips as $recip)
3099                         self::build_and_transmit($profile, $recip, "profile", $message, false, "", true);
3100         }
3101
3102         /**
3103          * @brief Stores the signature for likes that are created on our system
3104          *
3105          * @param array $contact The contact array of the "like"
3106          * @param int $post_id The post id of the "like"
3107          *
3108          * @return bool Success
3109          */
3110         public static function store_like_signature($contact, $post_id) {
3111
3112                 // Is the contact the owner? Then fetch the private key
3113                 if (!$contact['self'] OR ($contact['uid'] == 0)) {
3114                         logger("No owner post, so not storing signature", LOGGER_DEBUG);
3115                         return false;
3116                 }
3117
3118                 $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
3119                 if(!$r)
3120                         return false;
3121
3122                 $contact["uprvkey"] = $r[0]['prvkey'];
3123
3124                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($post_id));
3125                 if (!$r)
3126                         return false;
3127
3128                 if (!in_array($r[0]["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE)))
3129                         return false;
3130
3131                 $message = self::construct_like($r[0], $contact);
3132                 $message["author_signature"] = self::signature($contact, $message);
3133
3134                 // In the future we will store the signature more flexible to support new fields.
3135                 // Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
3136                 // (We are transmitting this data here via DFRN)
3137
3138                 $signed_text = $message["positive"].";".$message["guid"].";".$message["target_type"].";".
3139                                 $message["parent_guid"].";".$message["diaspora_handle"];
3140
3141                 q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
3142                         intval($post_id),
3143                         dbesc($signed_text),
3144                         dbesc($message["author_signature"]),
3145                         dbesc($message["diaspora_handle"])
3146                 );
3147
3148                 // This here will replace the lines above, once Diaspora changed its protocol
3149                 //q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
3150                 //      intval($message_id),
3151                 //      dbesc(json_encode($message))
3152                 //);
3153
3154                 logger('Stored diaspora like signature');
3155                 return true;
3156         }
3157
3158         /**
3159          * @brief Stores the signature for comments that are created on our system
3160          *
3161          * @param array $item The item array of the comment
3162          * @param array $contact The contact array of the item owner
3163          * @param string $uprvkey The private key of the sender
3164          * @param int $message_id The message id of the comment
3165          *
3166          * @return bool Success
3167          */
3168         public static function store_comment_signature($item, $contact, $uprvkey, $message_id) {
3169
3170                 if ($uprvkey == "") {
3171                         logger('No private key, so not storing comment signature', LOGGER_DEBUG);
3172                         return false;
3173                 }
3174
3175                 $contact["uprvkey"] = $uprvkey;
3176
3177                 $message = self::construct_comment($item, $contact);
3178                 $message["author_signature"] = self::signature($contact, $message);
3179
3180                 // In the future we will store the signature more flexible to support new fields.
3181                 // Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
3182                 // (We are transmitting this data here via DFRN)
3183                 $signed_text = $message["guid"].";".$message["parent_guid"].";".
3184                                 $message["text"].";".$message["diaspora_handle"];
3185
3186                 q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
3187                         intval($message_id),
3188                         dbesc($signed_text),
3189                         dbesc($message["author_signature"]),
3190                         dbesc($message["diaspora_handle"])
3191                 );
3192
3193                 // This here will replace the lines above, once Diaspora changed its protocol
3194                 //q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
3195                 //      intval($message_id),
3196                 //      dbesc(json_encode($message))
3197                 //);
3198
3199                 logger('Stored diaspora comment signature');
3200                 return true;
3201         }
3202 }
3203 ?>