]> git.mxchange.org Git - friendica.git/blob - src/Network/Probe.php
No more gender
[friendica.git] / src / Network / Probe.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Network;
23
24 use DOMDocument;
25 use DomXPath;
26 use Friendica\Core\Cache\Duration;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Profile;
33 use Friendica\Protocol\ActivityNamespace;
34 use Friendica\Protocol\ActivityPub;
35 use Friendica\Protocol\Email;
36 use Friendica\Protocol\Feed;
37 use Friendica\Util\Crypto;
38 use Friendica\Util\Network;
39 use Friendica\Util\Strings;
40 use Friendica\Util\XML;
41
42 /**
43  * This class contain functions for probing URL
44  */
45 class Probe
46 {
47         private static $baseurl;
48         private static $istimeout;
49
50         /**
51          * Rearrange the array so that it always has the same order
52          *
53          * @param array $data Unordered data
54          *
55          * @return array Ordered data
56          */
57         private static function rearrangeData($data)
58         {
59                 $fields = ["name", "nick", "guid", "url", "addr", "alias", "photo", "account-type",
60                                 "community", "keywords", "location", "about", "hide",
61                                 "batch", "notify", "poll", "request", "confirm", "poco",
62                                 "following", "followers", "inbox", "outbox", "sharedinbox",
63                                 "priority", "network", "pubkey", "baseurl"];
64
65                 $newdata = [];
66                 foreach ($fields as $field) {
67                         if (isset($data[$field])) {
68                                 $newdata[$field] = $data[$field];
69                         } else {
70                                 $newdata[$field] = "";
71                         }
72                 }
73
74                 // We don't use the "priority" field anymore and replace it with a dummy.
75                 $newdata["priority"] = 0;
76
77                 return $newdata;
78         }
79
80         /**
81          * Check if the hostname belongs to the own server
82          *
83          * @param string $host The hostname that is to be checked
84          *
85          * @return bool Does the testes hostname belongs to the own server?
86          */
87         private static function ownHost($host)
88         {
89                 $own_host = DI::baseUrl()->getHostname();
90
91                 $parts = parse_url($host);
92
93                 if (!isset($parts['scheme'])) {
94                         $parts = parse_url('http://'.$host);
95                 }
96
97                 if (!isset($parts['host'])) {
98                         return false;
99                 }
100                 return $parts['host'] == $own_host;
101         }
102
103         /**
104          * Probes for webfinger path via "host-meta"
105          *
106          * We have to check if the servers in the future still will offer this.
107          * It seems as if it was dropped from the standard.
108          *
109          * @param string $host The host part of an url
110          *
111          * @return array with template and type of the webfinger template for JSON or XML
112          * @throws HTTPException\InternalServerErrorException
113          */
114         private static function hostMeta($host)
115         {
116                 // Reset the static variable
117                 self::$baseurl = '';
118
119                 $ssl_url = "https://".$host."/.well-known/host-meta";
120                 $url = "http://".$host."/.well-known/host-meta";
121
122                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
123
124                 Logger::log("Probing for ".$host, Logger::DEBUG);
125                 $xrd = null;
126
127                 $curlResult = Network::curl($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
128                 $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
129                 if ($curlResult->isSuccess()) {
130                         $xml = $curlResult->getBody();
131                         $xrd = XML::parseString($xml, false);
132                         $host_url = 'https://'.$host;
133                 } elseif ($curlResult->isTimeout()) {
134                         Logger::info('Probing timeout', ['url' => $ssl_url], Logger::DEBUG);
135                         self::$istimeout = true;
136                         return false;
137                 }
138
139                 if (!is_object($xrd)) {
140                         $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
141                         $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
142                         if ($curlResult->isTimeout()) {
143                                 Logger::info('Probing timeout', ['url' => $url], Logger::DEBUG);
144                                 self::$istimeout = true;
145                                 return false;
146                         } elseif ($connection_error && $ssl_connection_error) {
147                                 self::$istimeout = true;
148                                 return false;
149                         }
150
151                         $xml = $curlResult->getBody();
152                         $xrd = XML::parseString($xml, false);
153                         $host_url = 'http://'.$host;
154                 }
155                 if (!is_object($xrd)) {
156                         Logger::log("No xrd object found for ".$host, Logger::DEBUG);
157                         return [];
158                 }
159
160                 $links = XML::elementToArray($xrd);
161                 if (!isset($links["xrd"]["link"])) {
162                         Logger::log("No xrd data found for ".$host, Logger::DEBUG);
163                         return [];
164                 }
165
166                 $lrdd = ['application/jrd+json' => $host_url . '/.well-known/webfinger?resource={uri}'];
167
168                 foreach ($links["xrd"]["link"] as $value => $link) {
169                         if (!empty($link["@attributes"])) {
170                                 $attributes = $link["@attributes"];
171                         } elseif ($value == "@attributes") {
172                                 $attributes = $link;
173                         } else {
174                                 continue;
175                         }
176
177                         if (!empty($attributes["rel"]) && $attributes["rel"] == "lrdd" && !empty($attributes["template"])) {
178                                 $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
179
180                                 $lrdd[$type] = $attributes["template"];
181                         }
182                 }
183
184                 self::$baseurl = $host_url;
185
186                 Logger::log("Probing successful for ".$host, Logger::DEBUG);
187
188                 return $lrdd;
189         }
190
191         /**
192          * Perform Webfinger lookup and return DFRN data
193          *
194          * Given an email style address, perform webfinger lookup and
195          * return the resulting DFRN profile URL, or if no DFRN profile URL
196          * is located, returns an OStatus subscription template (prefixed
197          * with the string 'stat:' to identify it as on OStatus template).
198          * If this isn't an email style address just return $webbie.
199          * Return an empty string if email-style addresses but webfinger fails,
200          * or if the resultant personal XRD doesn't contain a supported
201          * subscription/friend-request attribute.
202          *
203          * amended 7/9/2011 to return an hcard which could save potentially loading
204          * a lengthy content page to scrape dfrn attributes
205          *
206          * @param string $webbie    Address that should be probed
207          * @param string $hcard_url Link to the hcard - is returned by reference
208          *
209          * @return string profile link
210          * @throws HTTPException\InternalServerErrorException
211          */
212         public static function webfingerDfrn($webbie, &$hcard_url)
213         {
214                 $profile_link = '';
215
216                 $links = self::lrdd($webbie);
217                 Logger::log('webfingerDfrn: '.$webbie.':'.print_r($links, true), Logger::DATA);
218                 if (!empty($links) && is_array($links)) {
219                         foreach ($links as $link) {
220                                 if ($link['@attributes']['rel'] === ActivityNamespace::DFRN) {
221                                         $profile_link = $link['@attributes']['href'];
222                                 }
223                                 if (($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) && ($profile_link == "")) {
224                                         $profile_link = 'stat:'.$link['@attributes']['template'];
225                                 }
226                                 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
227                                         $hcard_url = $link['@attributes']['href'];
228                                 }
229                         }
230                 }
231                 return $profile_link;
232         }
233
234         /**
235          * Get the link for the remote follow page for a given profile link
236          *
237          * @param sting $profile
238          * @return string Remote follow page link
239          */
240         public static function getRemoteFollowLink(string $profile)
241         {
242                 $follow_link = '';
243
244                 $links = self::lrdd($profile);
245
246                 if (!empty($links) && is_array($links)) {
247                         foreach ($links as $link) {
248                                 if ($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) {
249                                         $follow_link = $link['@attributes']['template'];
250                                 }
251                         }
252                 }
253                 return $follow_link;
254         }
255
256         /**
257          * Check an URI for LRDD data
258          *
259          * this is a replacement for the "lrdd" function.
260          * It isn't used in this class and has some redundancies in the code.
261          * When time comes we can check the existing calls for "lrdd" if we can rework them.
262          *
263          * @param string $uri Address that should be probed
264          *
265          * @return array uri data
266          * @throws HTTPException\InternalServerErrorException
267          */
268         public static function lrdd($uri)
269         {
270                 $lrdd = self::hostMeta($uri);
271                 $webfinger = null;
272
273                 if (is_bool($lrdd)) {
274                         return [];
275                 }
276
277                 if (!$lrdd) {
278                         $parts = @parse_url($uri);
279                         if (!$parts || empty($parts["host"]) || empty($parts["path"])) {
280                                 return [];
281                         }
282
283                         $host = $parts["host"];
284                         if (!empty($parts["port"])) {
285                                 $host .= ':'.$parts["port"];
286                         }
287
288                         $path_parts = explode("/", trim($parts["path"], "/"));
289
290                         $nick = array_pop($path_parts);
291
292                         do {
293                                 $lrdd = self::hostMeta($host);
294                                 $host .= "/".array_shift($path_parts);
295                         } while (!$lrdd && (sizeof($path_parts) > 0));
296                 }
297
298                 if (!$lrdd) {
299                         Logger::log("No lrdd data found for ".$uri, Logger::DEBUG);
300                         return [];
301                 }
302
303                 foreach ($lrdd as $type => $template) {
304                         if ($webfinger) {
305                                 continue;
306                         }
307
308                         $path = str_replace('{uri}', urlencode($uri), $template);
309                         $webfinger = self::webfinger($path, $type);
310
311                         if (!$webfinger && (strstr($uri, "@"))) {
312                                 $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
313                                 $webfinger = self::webfinger($path, $type);
314                         }
315
316                         // Special treatment for Mastodon
317                         // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
318                         // But the webfinger for this format fails.
319                         if (!$webfinger && !empty($nick)) {
320                                 // Mastodon uses a "@" as prefix for usernames in their url format
321                                 $nick = ltrim($nick, '@');
322
323                                 $addr = $nick."@".$host;
324
325                                 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
326                                 $webfinger = self::webfinger($path, $type);
327                         }
328                 }
329
330                 if (!is_array($webfinger["links"])) {
331                         Logger::log("No webfinger links found for ".$uri, Logger::DEBUG);
332                         return false;
333                 }
334
335                 $data = [];
336
337                 foreach ($webfinger["links"] as $link) {
338                         $data[] = ["@attributes" => $link];
339                 }
340
341                 if (is_array($webfinger["aliases"])) {
342                         foreach ($webfinger["aliases"] as $alias) {
343                                 $data[] = ["@attributes" =>
344                                                         ["rel" => "alias",
345                                                                 "href" => $alias]];
346                         }
347                 }
348
349                 return $data;
350         }
351
352         /**
353          * Fetch information (protocol endpoints and user information) about a given uri
354          *
355          * @param string  $uri     Address that should be probed
356          * @param string  $network Test for this specific network
357          * @param integer $uid     User ID for the probe (only used for mails)
358          * @param boolean $cache   Use cached values?
359          *
360          * @return array uri data
361          * @throws HTTPException\InternalServerErrorException
362          * @throws \ImagickException
363          */
364         public static function uri($uri, $network = '', $uid = -1, $cache = true)
365         {
366                 if ($cache) {
367                         $result = DI::cache()->get('Probe::uri:' . $network . ':' . $uri);
368                         if (!is_null($result)) {
369                                 return $result;
370                         }
371                 }
372
373                 if ($uid == -1) {
374                         $uid = local_user();
375                 }
376
377                 self::$istimeout = false;
378
379                 if ($network != Protocol::ACTIVITYPUB) {
380                         $data = self::detect($uri, $network, $uid);
381                 } else {
382                         $data = null;
383                 }
384
385                 // When the previous detection process had got a time out
386                 // we could falsely detect a Friendica profile as AP profile.
387                 if (!self::$istimeout) {
388                         $ap_profile = ActivityPub::probeProfile($uri);
389
390                         if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) {
391                                 $data = $ap_profile;
392                         } elseif (!empty($ap_profile)) {
393                                 $ap_profile['batch'] = '';
394                                 $data = array_merge($ap_profile, $data);
395                         }
396                 } else {
397                         Logger::notice('Time out detected. AP will not be probed.', ['uri' => $uri]);
398                 }
399
400                 if (!isset($data['url'])) {
401                         $data['url'] = $uri;
402                 }
403
404                 if (!empty($data['photo']) && !empty($data['baseurl'])) {
405                         $data['baseurl'] = Network::getUrlMatch(Strings::normaliseLink($data['baseurl']), Strings::normaliseLink($data['photo']));
406                 } elseif (empty($data['photo'])) {
407                         $data['photo'] = DI::baseUrl() . '/images/person-300.jpg';
408                 }
409
410                 if (empty($data['name'])) {
411                         if (!empty($data['nick'])) {
412                                 $data['name'] = $data['nick'];
413                         }
414
415                         if (empty($data['name'])) {
416                                 $data['name'] = $data['url'];
417                         }
418                 }
419
420                 if (empty($data['nick'])) {
421                         $data['nick'] = strtolower($data['name']);
422
423                         if (strpos($data['nick'], ' ')) {
424                                 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
425                         }
426                 }
427
428                 if (!empty(self::$baseurl)) {
429                         $data['baseurl'] = self::$baseurl;
430                 }
431
432                 if (empty($data['network'])) {
433                         $data['network'] = Protocol::PHANTOM;
434                 }
435
436                 // Ensure that local connections always are DFRN
437                 if (($network == '') && ($data['network'] != Protocol::PHANTOM) && (self::ownHost($data['baseurl'] ?? '') || self::ownHost($data['url']))) {
438                         $data['network'] = Protocol::DFRN;
439                 }
440
441                 if (!isset($data['hide']) && in_array($data['network'], Protocol::FEDERATED)) {
442                         $data['hide'] = self::getHideStatus($data['url']);
443                 }
444
445                 $data = self::rearrangeData($data);
446
447                 // Only store into the cache if the value seems to be valid
448                 if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
449                         DI::cache()->set('Probe::uri:' . $network . ':' . $uri, $data, Duration::DAY);
450                 }
451
452                 return $data;
453         }
454
455
456         /**
457          * Fetches the "hide" status from the profile
458          *
459          * @param string $url URL of the profile
460          *
461          * @return boolean "hide" status
462          */
463         private static function getHideStatus($url)
464         {
465                 $curlResult = Network::curl($url);
466                 if (!$curlResult->isSuccess()) {
467                         return false;
468                 }
469
470                 // If the file is too large then exit
471                 if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
472                         return false;
473                 }
474
475                 // If it isn't a HTML file then exit
476                 if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
477                         return false;
478                 }
479
480                 $body = $curlResult->getBody();
481
482                 $doc = new DOMDocument();
483                 @$doc->loadHTML($body);
484
485                 $xpath = new DOMXPath($doc);
486
487                 $list = $xpath->query('//meta[@name]');
488                 foreach ($list as $node) {
489                         $meta_tag = [];
490                         if ($node->attributes->length) {
491                                 foreach ($node->attributes as $attribute) {
492                                         $meta_tag[$attribute->name] = $attribute->value;
493                                 }
494                         }
495
496                         if (empty($meta_tag['content'])) {
497                                 continue;
498                         }
499
500                         $content = strtolower(trim($meta_tag['content']));
501
502                         switch (strtolower(trim($meta_tag['name']))) {
503                                 case 'dfrn-global-visibility':
504                                         if ($content == 'false') {
505                                                 return true;
506                                         }
507                                         break;
508                                 case 'robots':
509                                         if (strpos($content, 'noindex') !== false) {
510                                                 return true;
511                                         }
512                                         break;
513                         }
514                 }
515
516                 return false;
517         }
518
519         /**
520          * Checks if a profile url should be OStatus but only provides partial information
521          *
522          * @param array  $webfinger Webfinger data
523          * @param string $lrdd      Path template for webfinger request
524          * @param string $type      type
525          *
526          * @return array fixed webfinger data
527          * @throws HTTPException\InternalServerErrorException
528          */
529         private static function fixOStatus($webfinger, $lrdd, $type)
530         {
531                 if (empty($webfinger['links']) || empty($webfinger['subject'])) {
532                         return $webfinger;
533                 }
534
535                 $is_ostatus = false;
536                 $has_key = false;
537
538                 foreach ($webfinger['links'] as $link) {
539                         if ($link['rel'] == ActivityNamespace::OSTATUSSUB) {
540                                 $is_ostatus = true;
541                         }
542                         if ($link['rel'] == 'magic-public-key') {
543                                 $has_key = true;
544                         }
545                 }
546
547                 if (!$is_ostatus || $has_key) {
548                         return $webfinger;
549                 }
550
551                 $url = Network::switchScheme($webfinger['subject']);
552                 $path = str_replace('{uri}', urlencode($url), $lrdd);
553                 $webfinger2 = self::webfinger($path, $type);
554
555                 // Is the new webfinger detectable as OStatus?
556                 if (self::ostatus($webfinger2, true)) {
557                         $webfinger = $webfinger2;
558                 }
559
560                 return $webfinger;
561         }
562
563         /**
564          * Fetch information (protocol endpoints and user information) about a given uri
565          *
566          * This function is only called by the "uri" function that adds caching and rearranging of data.
567          *
568          * @param string  $uri     Address that should be probed
569          * @param string  $network Test for this specific network
570          * @param integer $uid     User ID for the probe (only used for mails)
571          *
572          * @return array uri data
573          * @throws HTTPException\InternalServerErrorException
574          */
575         private static function detect($uri, $network, $uid)
576         {
577                 $parts = parse_url($uri);
578
579                 if (!empty($parts["scheme"]) && !empty($parts["host"])) {
580                         $host = $parts["host"];
581                         if (!empty($parts["port"])) {
582                                 $host .= ':'.$parts["port"];
583                         }
584
585                         if ($host == 'twitter.com') {
586                                 return self::twitter($uri);
587                         }
588                         $lrdd = self::hostMeta($host);
589
590                         if (is_bool($lrdd)) {
591                                 return [];
592                         }
593
594                         $path_parts = explode("/", trim($parts['path'] ?? '', "/"));
595
596                         while (!$lrdd && (sizeof($path_parts) > 1)) {
597                                 $host .= "/".array_shift($path_parts);
598                                 $lrdd = self::hostMeta($host);
599                         }
600                         if (!$lrdd) {
601                                 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
602                                 return self::feed($uri);
603                         }
604                         $nick = array_pop($path_parts);
605
606                         // Mastodon uses a "@" as prefix for usernames in their url format
607                         $nick = ltrim($nick, '@');
608
609                         $addr = $nick."@".$host;
610                 } elseif (strstr($uri, '@')) {
611                         // If the URI starts with "mailto:" then jump directly to the mail detection
612                         if (strpos($uri, 'mailto:') !== false) {
613                                 $uri = str_replace('mailto:', '', $uri);
614                                 return self::mail($uri, $uid);
615                         }
616
617                         if ($network == Protocol::MAIL) {
618                                 return self::mail($uri, $uid);
619                         }
620                         // Remove "acct:" from the URI
621                         $uri = str_replace('acct:', '', $uri);
622
623                         $host = substr($uri, strpos($uri, '@') + 1);
624                         $nick = substr($uri, 0, strpos($uri, '@'));
625
626                         if (strpos($uri, '@twitter.com')) {
627                                 return self::twitter($uri);
628                         }
629                         $lrdd = self::hostMeta($host);
630
631                         if (is_bool($lrdd)) {
632                                 return [];
633                         }
634
635                         if (!$lrdd) {
636                                 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
637                                 return self::mail($uri, $uid);
638                         }
639                         $addr = $uri;
640                 } else {
641                         Logger::log("Uri ".$uri." was not detectable", Logger::DEBUG);
642                         return false;
643                 }
644
645                 $webfinger = false;
646
647                 /// @todo Do we need the prefix "acct:" or "acct://"?
648
649                 foreach ($lrdd as $type => $template) {
650                         if ($webfinger) {
651                                 continue;
652                         }
653
654                         // At first try it with the given uri
655                         $path = str_replace('{uri}', urlencode($uri), $template);
656                         $webfinger = self::webfinger($path, $type);
657
658                         // Fix possible problems with GNU Social probing to wrong scheme
659                         $webfinger = self::fixOStatus($webfinger, $template, $type);
660
661                         // We cannot be sure that the detected address was correct, so we don't use the values
662                         if ($webfinger && ($uri != $addr)) {
663                                 $nick = "";
664                                 $addr = "";
665                         }
666
667                         // Try webfinger with the address (user@domain.tld)
668                         if (!$webfinger) {
669                                 $path = str_replace('{uri}', urlencode($addr), $template);
670                                 $webfinger = self::webfinger($path, $type);
671                         }
672
673                         // Mastodon needs to have it with "acct:"
674                         if (!$webfinger) {
675                                 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
676                                 $webfinger = self::webfinger($path, $type);
677                         }
678                 }
679
680                 if (!$webfinger) {
681                         return self::feed($uri);
682                 }
683
684                 $result = false;
685
686                 Logger::log("Probing ".$uri, Logger::DEBUG);
687
688                 if (in_array($network, ["", Protocol::DFRN])) {
689                         $result = self::dfrn($webfinger);
690                 }
691                 if ((!$result && ($network == "")) || ($network == Protocol::DIASPORA)) {
692                         $result = self::diaspora($webfinger);
693                 }
694                 if ((!$result && ($network == "")) || ($network == Protocol::OSTATUS)) {
695                         $result = self::ostatus($webfinger);
696                 }
697                 if (in_array($network, ['', Protocol::ZOT])) {
698                         $result = self::zot($webfinger, $result);
699                 }
700                 if ((!$result && ($network == "")) || ($network == Protocol::PUMPIO)) {
701                         $result = self::pumpio($webfinger, $addr);
702                 }
703                 if ((!$result && ($network == "")) || ($network == Protocol::FEED)) {
704                         $result = self::feed($uri);
705                 } else {
706                         // We overwrite the detected nick with our try if the previois routines hadn't detected it.
707                         // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
708                         if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) {
709                                 $result["nick"] = $nick;
710                         }
711
712                         if (empty($result["addr"]) && ($addr != "")) {
713                                 $result["addr"] = $addr;
714                         }
715                 }
716
717                 if (empty($result["network"])) {
718                         $result["network"] = Protocol::PHANTOM;
719                 }
720
721                 if (empty($result["url"])) {
722                         $result["url"] = $uri;
723                 }
724
725                 Logger::log($uri." is ".$result["network"], Logger::DEBUG);
726
727                 if (empty($result["baseurl"]) && ($result["network"] != Protocol::PHANTOM)) {
728                         $pos = strpos($result["url"], $host);
729                         if ($pos) {
730                                 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
731                         }
732                 }
733                 return $result;
734         }
735
736         /**
737          * Check for Zot contact
738          *
739          * @param array $webfinger Webfinger data
740          * @param array $data      previously probed data
741          *
742          * @return array Zot data
743          * @throws HTTPException\InternalServerErrorException
744          */
745         private static function zot($webfinger, $data)
746         {
747                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
748                         foreach ($webfinger["aliases"] as $alias) {
749                                 if (substr($alias, 0, 5) == 'acct:') {
750                                         $data["addr"] = substr($alias, 5);
751                                 }
752                         }
753                 }
754
755                 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
756                         $data["addr"] = substr($webfinger["subject"], 5);
757                 }
758
759                 $zot_url = '';
760                 foreach ($webfinger['links'] as $link) {
761                         if (($link['rel'] == 'http://purl.org/zot/protocol') && !empty($link['href'])) {
762                                 $zot_url = $link['href'];
763                         }
764                 }
765
766                 if (empty($zot_url) && !empty($data['addr']) && !empty(self::$baseurl)) {
767                         $condition = ['nurl' => Strings::normaliseLink(self::$baseurl), 'platform' => ['hubzilla']];
768                         if (!DBA::exists('gserver', $condition)) {
769                                 return $data;
770                         }
771                         $zot_url = self::$baseurl . '/.well-known/zot-info?address=' . $data['addr'];
772                 }
773
774                 if (empty($zot_url)) {
775                         return $data;
776                 }
777
778                 $data = self::pollZot($zot_url, $data);
779
780                 if (!empty($data['url']) && !empty($webfinger['aliases']) && is_array($webfinger['aliases'])) {
781                         foreach ($webfinger['aliases'] as $alias) {
782                                 if (!strstr($alias, '@') && Strings::normaliseLink($alias) != Strings::normaliseLink($data['url'])) {
783                                         $data['alias'] = $alias;
784                                 }
785                         }
786                 }
787
788                 return $data;
789         }
790
791         public static function pollZot($url, $data)
792         {
793                 $curlResult = Network::curl($url);
794                 if ($curlResult->isTimeout()) {
795                         return $data;
796                 }
797                 $content = $curlResult->getBody();
798                 if (!$content) {
799                         return $data;
800                 }
801
802                 $json = json_decode($content, true);
803                 if (!is_array($json)) {
804                         return $data;
805                 }
806
807                 if (empty($data['network'])) {
808                         if (!empty($json['protocols']) && in_array('zot', $json['protocols'])) {
809                                 $data['network'] = Protocol::ZOT;
810                         } elseif (!isset($json['protocols'])) {
811                                 $data['network'] = Protocol::ZOT;
812                         }
813                 }
814
815                 if (!empty($json['guid']) && empty($data['guid'])) {
816                         $data['guid'] = $json['guid'];
817                 }
818                 if (!empty($json['key']) && empty($data['pubkey'])) {
819                         $data['pubkey'] = $json['key'];
820                 }
821                 if (!empty($json['name'])) {
822                         $data['name'] = $json['name'];
823                 }
824                 if (!empty($json['photo'])) {
825                         $data['photo'] = $json['photo'];
826                         if (!empty($json['photo_updated'])) {
827                                 $data['photo'] .= '?rev=' . urlencode($json['photo_updated']);
828                         }
829                 }
830                 if (!empty($json['address'])) {
831                         $data['addr'] = $json['address'];
832                 }
833                 if (!empty($json['url'])) {
834                         $data['url'] = $json['url'];
835                 }
836                 if (!empty($json['connections_url'])) {
837                         $data['poco'] = $json['connections_url'];
838                 }
839                 if (isset($json['searchable'])) {
840                         $data['hide'] = !$json['searchable'];
841                 }
842                 if (!empty($json['public_forum'])) {
843                         $data['community'] = $json['public_forum'];
844                         $data['account-type'] = Contact::PAGE_COMMUNITY;
845                 }
846
847                 if (!empty($json['profile'])) {
848                         $profile = $json['profile'];
849                         if (!empty($profile['description'])) {
850                                 $data['about'] = $profile['description'];
851                         }
852                         if (!empty($profile['keywords'])) {
853                                 $keywords = implode(', ', $profile['keywords']);
854                                 if (!empty($keywords)) {
855                                         $data['keywords'] = $keywords;
856                                 }
857                         }
858
859                         $loc = [];
860                         if (!empty($profile['region'])) {
861                                 $loc['region'] = $profile['region'];
862                         }
863                         if (!empty($profile['country'])) {
864                                 $loc['country-name'] = $profile['country'];
865                         }
866                         $location = Profile::formatLocation($loc);
867                         if (!empty($location)) {
868                                 $data['location'] = $location;
869                         }
870                 }
871
872                 return $data;
873         }
874
875         /**
876          * Perform a webfinger request.
877          *
878          * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
879          *
880          * @param string $url  Address that should be probed
881          * @param string $type type
882          *
883          * @return array webfinger data
884          * @throws HTTPException\InternalServerErrorException
885          */
886         private static function webfinger($url, $type)
887         {
888                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
889
890                 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
891                 if ($curlResult->isTimeout()) {
892                         self::$istimeout = true;
893                         return false;
894                 }
895                 $data = $curlResult->getBody();
896
897                 $webfinger = json_decode($data, true);
898                 if (is_array($webfinger)) {
899                         if (!isset($webfinger["links"])) {
900                                 Logger::log("No json webfinger links for ".$url, Logger::DEBUG);
901                                 return false;
902                         }
903                         return $webfinger;
904                 }
905
906                 // If it is not JSON, maybe it is XML
907                 $xrd = XML::parseString($data, false);
908                 if (!is_object($xrd)) {
909                         Logger::log("No webfinger data retrievable for ".$url, Logger::DEBUG);
910                         return false;
911                 }
912
913                 $xrd_arr = XML::elementToArray($xrd);
914                 if (!isset($xrd_arr["xrd"]["link"])) {
915                         Logger::log("No XML webfinger links for ".$url, Logger::DEBUG);
916                         return false;
917                 }
918
919                 $webfinger = [];
920
921                 if (!empty($xrd_arr["xrd"]["subject"])) {
922                         $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
923                 }
924
925                 if (!empty($xrd_arr["xrd"]["alias"])) {
926                         $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
927                 }
928
929                 $webfinger["links"] = [];
930
931                 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
932                         if (!empty($data["@attributes"])) {
933                                 $attributes = $data["@attributes"];
934                         } elseif ($value == "@attributes") {
935                                 $attributes = $data;
936                         } else {
937                                 continue;
938                         }
939
940                         $webfinger["links"][] = $attributes;
941                 }
942                 return $webfinger;
943         }
944
945         /**
946          * Poll the Friendica specific noscrape page.
947          *
948          * "noscrape" is a faster alternative to fetch the data from the hcard.
949          * This functionality was originally created for the directory.
950          *
951          * @param string $noscrape_url Link to the noscrape page
952          * @param array  $data         The already fetched data
953          *
954          * @return array noscrape data
955          * @throws HTTPException\InternalServerErrorException
956          */
957         private static function pollNoscrape($noscrape_url, $data)
958         {
959                 $curlResult = Network::curl($noscrape_url);
960                 if ($curlResult->isTimeout()) {
961                         self::$istimeout = true;
962                         return false;
963                 }
964                 $content = $curlResult->getBody();
965                 if (!$content) {
966                         Logger::log("Empty body for ".$noscrape_url, Logger::DEBUG);
967                         return false;
968                 }
969
970                 $json = json_decode($content, true);
971                 if (!is_array($json)) {
972                         Logger::log("No json data for ".$noscrape_url, Logger::DEBUG);
973                         return false;
974                 }
975
976                 if (!empty($json["fn"])) {
977                         $data["name"] = $json["fn"];
978                 }
979
980                 if (!empty($json["addr"])) {
981                         $data["addr"] = $json["addr"];
982                 }
983
984                 if (!empty($json["nick"])) {
985                         $data["nick"] = $json["nick"];
986                 }
987
988                 if (!empty($json["guid"])) {
989                         $data["guid"] = $json["guid"];
990                 }
991
992                 if (!empty($json["comm"])) {
993                         $data["community"] = $json["comm"];
994                 }
995
996                 if (!empty($json["tags"])) {
997                         $keywords = implode(", ", $json["tags"]);
998                         if ($keywords != "") {
999                                 $data["keywords"] = $keywords;
1000                         }
1001                 }
1002
1003                 $location = Profile::formatLocation($json);
1004                 if ($location) {
1005                         $data["location"] = $location;
1006                 }
1007
1008                 if (!empty($json["about"])) {
1009                         $data["about"] = $json["about"];
1010                 }
1011
1012                 if (!empty($json["key"])) {
1013                         $data["pubkey"] = $json["key"];
1014                 }
1015
1016                 if (!empty($json["photo"])) {
1017                         $data["photo"] = $json["photo"];
1018                 }
1019
1020                 if (!empty($json["dfrn-request"])) {
1021                         $data["request"] = $json["dfrn-request"];
1022                 }
1023
1024                 if (!empty($json["dfrn-confirm"])) {
1025                         $data["confirm"] = $json["dfrn-confirm"];
1026                 }
1027
1028                 if (!empty($json["dfrn-notify"])) {
1029                         $data["notify"] = $json["dfrn-notify"];
1030                 }
1031
1032                 if (!empty($json["dfrn-poll"])) {
1033                         $data["poll"] = $json["dfrn-poll"];
1034                 }
1035
1036                 if (isset($json["hide"])) {
1037                         $data["hide"] = (bool)$json["hide"];
1038                 } else {
1039                         $data["hide"] = false;
1040                 }
1041
1042                 return $data;
1043         }
1044
1045         /**
1046          * Check for valid DFRN data
1047          *
1048          * @param array $data DFRN data
1049          *
1050          * @return int Number of errors
1051          */
1052         public static function validDfrn($data)
1053         {
1054                 $errors = 0;
1055                 if (!isset($data['key'])) {
1056                         $errors ++;
1057                 }
1058                 if (!isset($data['dfrn-request'])) {
1059                         $errors ++;
1060                 }
1061                 if (!isset($data['dfrn-confirm'])) {
1062                         $errors ++;
1063                 }
1064                 if (!isset($data['dfrn-notify'])) {
1065                         $errors ++;
1066                 }
1067                 if (!isset($data['dfrn-poll'])) {
1068                         $errors ++;
1069                 }
1070                 return $errors;
1071         }
1072
1073         /**
1074          * Fetch data from a DFRN profile page and via "noscrape"
1075          *
1076          * @param string $profile_link Link to the profile page
1077          *
1078          * @return array profile data
1079          * @throws HTTPException\InternalServerErrorException
1080          * @throws \ImagickException
1081          */
1082         public static function profile($profile_link)
1083         {
1084                 $data = [];
1085
1086                 Logger::log("Check profile ".$profile_link, Logger::DEBUG);
1087
1088                 // Fetch data via noscrape - this is faster
1089                 $noscrape_url = str_replace(["/hcard/", "/profile/"], "/noscrape/", $profile_link);
1090                 $data = self::pollNoscrape($noscrape_url, $data);
1091
1092                 if (!isset($data["notify"])
1093                         || !isset($data["confirm"])
1094                         || !isset($data["request"])
1095                         || !isset($data["poll"])
1096                         || !isset($data["name"])
1097                         || !isset($data["photo"])
1098                 ) {
1099                         $data = self::pollHcard($profile_link, $data, true);
1100                 }
1101
1102                 $prof_data = [];
1103
1104                 if (empty($data["addr"]) || empty($data["nick"])) {
1105                         $probe_data = self::uri($profile_link);
1106                         $data["addr"] = ($data["addr"] ?? '') ?: $probe_data["addr"];
1107                         $data["nick"] = ($data["nick"] ?? '') ?: $probe_data["nick"];
1108                 }
1109
1110                 $prof_data["addr"]         = $data["addr"];
1111                 $prof_data["nick"]         = $data["nick"];
1112                 $prof_data["dfrn-request"] = $data['request'] ?? null;
1113                 $prof_data["dfrn-confirm"] = $data['confirm'] ?? null;
1114                 $prof_data["dfrn-notify"]  = $data['notify']  ?? null;
1115                 $prof_data["dfrn-poll"]    = $data['poll']    ?? null;
1116                 $prof_data["photo"]        = $data['photo']   ?? null;
1117                 $prof_data["fn"]           = $data['name']    ?? null;
1118                 $prof_data["key"]          = $data['pubkey']  ?? null;
1119
1120                 Logger::log("Result for profile ".$profile_link.": ".print_r($prof_data, true), Logger::DEBUG);
1121
1122                 return $prof_data;
1123         }
1124
1125         /**
1126          * Check for DFRN contact
1127          *
1128          * @param array $webfinger Webfinger data
1129          *
1130          * @return array DFRN data
1131          * @throws HTTPException\InternalServerErrorException
1132          */
1133         private static function dfrn($webfinger)
1134         {
1135                 $hcard_url = "";
1136                 $data = [];
1137                 // The array is reversed to take into account the order of preference for same-rel links
1138                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1139                 foreach (array_reverse($webfinger["links"]) as $link) {
1140                         if (($link["rel"] == ActivityNamespace::DFRN) && !empty($link["href"])) {
1141                                 $data["network"] = Protocol::DFRN;
1142                         } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1143                                 $data["poll"] = $link["href"];
1144                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1145                                 $data["url"] = $link["href"];
1146                         } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1147                                 $hcard_url = $link["href"];
1148                         } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1149                                 $data["poco"] = $link["href"];
1150                         } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) {
1151                                 $data["photo"] = $link["href"];
1152                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1153                                 $data["baseurl"] = trim($link["href"], '/');
1154                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1155                                 $data["guid"] = $link["href"];
1156                         } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1157                                 $data["pubkey"] = base64_decode($link["href"]);
1158
1159                                 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1160                                 if (strstr($data["pubkey"], 'RSA ')) {
1161                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1162                                 }
1163                         }
1164                 }
1165
1166                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1167                         foreach ($webfinger["aliases"] as $alias) {
1168                                 if (empty($data["url"]) && !strstr($alias, "@")) {
1169                                         $data["url"] = $alias;
1170                                 } elseif (!strstr($alias, "@") && Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"])) {
1171                                         $data["alias"] = $alias;
1172                                 } elseif (substr($alias, 0, 5) == 'acct:') {
1173                                         $data["addr"] = substr($alias, 5);
1174                                 }
1175                         }
1176                 }
1177
1178                 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
1179                         $data["addr"] = substr($webfinger["subject"], 5);
1180                 }
1181
1182                 if (!isset($data["network"]) || ($hcard_url == "")) {
1183                         return false;
1184                 }
1185
1186                 // Fetch data via noscrape - this is faster
1187                 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
1188                 $data = self::pollNoscrape($noscrape_url, $data);
1189
1190                 if (isset($data["notify"])
1191                         && isset($data["confirm"])
1192                         && isset($data["request"])
1193                         && isset($data["poll"])
1194                         && isset($data["name"])
1195                         && isset($data["photo"])
1196                 ) {
1197                         return $data;
1198                 }
1199
1200                 $data = self::pollHcard($hcard_url, $data, true);
1201
1202                 return $data;
1203         }
1204
1205         /**
1206          * Poll the hcard page (Diaspora and Friendica specific)
1207          *
1208          * @param string  $hcard_url Link to the hcard page
1209          * @param array   $data      The already fetched data
1210          * @param boolean $dfrn      Poll DFRN specific data
1211          *
1212          * @return array hcard data
1213          * @throws HTTPException\InternalServerErrorException
1214          */
1215         private static function pollHcard($hcard_url, $data, $dfrn = false)
1216         {
1217                 $curlResult = Network::curl($hcard_url);
1218                 if ($curlResult->isTimeout()) {
1219                         self::$istimeout = true;
1220                         return false;
1221                 }
1222                 $content = $curlResult->getBody();
1223                 if (!$content) {
1224                         return false;
1225                 }
1226
1227                 $doc = new DOMDocument();
1228                 if (!@$doc->loadHTML($content)) {
1229                         return false;
1230                 }
1231
1232                 $xpath = new DomXPath($doc);
1233
1234                 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
1235                 if (!is_object($vcards)) {
1236                         return false;
1237                 }
1238
1239                 if (!isset($data["baseurl"])) {
1240                         $data["baseurl"] = "";
1241                 }
1242
1243                 if ($vcards->length > 0) {
1244                         $vcard = $vcards->item(0);
1245
1246                         // We have to discard the guid from the hcard in favour of the guid from lrdd
1247                         // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
1248                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
1249                         if (($search->length > 0) && empty($data["guid"])) {
1250                                 $data["guid"] = $search->item(0)->nodeValue;
1251                         }
1252
1253                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
1254                         if ($search->length > 0) {
1255                                 $data["nick"] = $search->item(0)->nodeValue;
1256                         }
1257
1258                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
1259                         if ($search->length > 0) {
1260                                 $data["name"] = $search->item(0)->nodeValue;
1261                         }
1262
1263                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
1264                         if ($search->length > 0) {
1265                                 $data["searchable"] = $search->item(0)->nodeValue;
1266                         }
1267
1268                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
1269                         if ($search->length > 0) {
1270                                 $data["pubkey"] = $search->item(0)->nodeValue;
1271                                 if (strstr($data["pubkey"], 'RSA ')) {
1272                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1273                                 }
1274                         }
1275
1276                         $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
1277                         if ($search->length > 0) {
1278                                 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
1279                         }
1280                 }
1281
1282                 $avatar = [];
1283                 if (!empty($vcard)) {
1284                         $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
1285                         foreach ($photos as $photo) {
1286                                 $attr = [];
1287                                 foreach ($photo->attributes as $attribute) {
1288                                         $attr[$attribute->name] = trim($attribute->value);
1289                                 }
1290
1291                                 if (isset($attr["src"]) && isset($attr["width"])) {
1292                                         $avatar[$attr["width"]] = $attr["src"];
1293                                 }
1294
1295                                 // We don't have a width. So we just take everything that we got.
1296                                 // This is a Hubzilla workaround which doesn't send a width.
1297                                 if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
1298                                         $avatar[] = $attr["src"];
1299                                 }
1300                         }
1301                 }
1302
1303                 if (sizeof($avatar)) {
1304                         ksort($avatar);
1305                         $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
1306                 }
1307
1308                 if ($dfrn) {
1309                         // Poll DFRN specific data
1310                         $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
1311                         if ($search->length > 0) {
1312                                 foreach ($search as $link) {
1313                                         //$data["request"] = $search->item(0)->nodeValue;
1314                                         $attr = [];
1315                                         foreach ($link->attributes as $attribute) {
1316                                                 $attr[$attribute->name] = trim($attribute->value);
1317                                         }
1318
1319                                         $data[substr($attr["rel"], 5)] = $attr["href"];
1320                                 }
1321                         }
1322
1323                         // Older Friendica versions had used the "uid" field differently than newer versions
1324                         if (!empty($data["nick"]) && !empty($data["guid"]) && ($data["nick"] == $data["guid"])) {
1325                                 unset($data["guid"]);
1326                         }
1327                 }
1328
1329
1330                 return $data;
1331         }
1332
1333         /**
1334          * Check for Diaspora contact
1335          *
1336          * @param array $webfinger Webfinger data
1337          *
1338          * @return array Diaspora data
1339          * @throws HTTPException\InternalServerErrorException
1340          */
1341         private static function diaspora($webfinger)
1342         {
1343                 $hcard_url = "";
1344                 $data = [];
1345
1346                 // The array is reversed to take into account the order of preference for same-rel links
1347                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1348                 foreach (array_reverse($webfinger["links"]) as $link) {
1349                         if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1350                                 $hcard_url = $link["href"];
1351                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1352                                 $data["baseurl"] = trim($link["href"], '/');
1353                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1354                                 $data["guid"] = $link["href"];
1355                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1356                                 $data["url"] = $link["href"];
1357                         } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1358                                 $data["poll"] = $link["href"];
1359                         } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1360                                 $data["poco"] = $link["href"];
1361                         } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1362                                 $data["notify"] = $link["href"];
1363                         } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1364                                 $data["pubkey"] = base64_decode($link["href"]);
1365
1366                                 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1367                                 if (strstr($data["pubkey"], 'RSA ')) {
1368                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1369                                 }
1370                         }
1371                 }
1372
1373                 if (empty($data["url"]) || empty($hcard_url)) {
1374                         return false;
1375                 }
1376
1377                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1378                         foreach ($webfinger["aliases"] as $alias) {
1379                                 if (Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"]) && ! strstr($alias, "@")) {
1380                                         $data["alias"] = $alias;
1381                                 } elseif (substr($alias, 0, 5) == 'acct:') {
1382                                         $data["addr"] = substr($alias, 5);
1383                                 }
1384                         }
1385                 }
1386
1387                 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
1388                         $data["addr"] = substr($webfinger["subject"], 5);
1389                 }
1390
1391                 // Fetch further information from the hcard
1392                 $data = self::pollHcard($hcard_url, $data);
1393
1394                 if (!$data) {
1395                         return false;
1396                 }
1397
1398                 if (!empty($data["url"])
1399                         && !empty($data["guid"])
1400                         && !empty($data["baseurl"])
1401                         && !empty($data["pubkey"])
1402                         && !empty($hcard_url)
1403                 ) {
1404                         $data["network"] = Protocol::DIASPORA;
1405
1406                         // The Diaspora handle must always be lowercase
1407                         if (!empty($data["addr"])) {
1408                                 $data["addr"] = strtolower($data["addr"]);
1409                         }
1410
1411                         // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
1412                         $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
1413                         $data["batch"]  = $data["baseurl"] . "/receive/public";
1414                 } else {
1415                         return false;
1416                 }
1417
1418                 return $data;
1419         }
1420
1421         /**
1422          * Check for OStatus contact
1423          *
1424          * @param array $webfinger Webfinger data
1425          * @param bool  $short     Short detection mode
1426          *
1427          * @return array|bool OStatus data or "false" on error or "true" on short mode
1428          * @throws HTTPException\InternalServerErrorException
1429          */
1430         private static function ostatus($webfinger, $short = false)
1431         {
1432                 $data = [];
1433
1434                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1435                         foreach ($webfinger["aliases"] as $alias) {
1436                                 if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) {
1437                                         $data["addr"] = str_replace('acct:', '', $alias);
1438                                 }
1439                         }
1440                 }
1441
1442                 if (!empty($webfinger["subject"]) && strstr($webfinger["subject"], "@")
1443                         && !strstr(Strings::normaliseLink($webfinger["subject"]), "http://")
1444                 ) {
1445                         $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1446                 }
1447
1448                 if (is_array($webfinger["links"])) {
1449                         // The array is reversed to take into account the order of preference for same-rel links
1450                         // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1451                         foreach (array_reverse($webfinger["links"]) as $link) {
1452                                 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1453                                         && (($link["type"] ?? "") == "text/html")
1454                                         && ($link["href"] != "")
1455                                 ) {
1456                                         $data["url"] = $link["href"];
1457                                 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1458                                         $data["notify"] = $link["href"];
1459                                 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1460                                         $data["poll"] = $link["href"];
1461                                 } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) {
1462                                         $pubkey = $link["href"];
1463
1464                                         if (substr($pubkey, 0, 5) === 'data:') {
1465                                                 if (strstr($pubkey, ',')) {
1466                                                         $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1467                                                 } else {
1468                                                         $pubkey = substr($pubkey, 5);
1469                                                 }
1470                                         } elseif (Strings::normaliseLink($pubkey) == 'http://') {
1471                                                 $curlResult = Network::curl($pubkey);
1472                                                 if ($curlResult->isTimeout()) {
1473                                                         self::$istimeout = true;
1474                                                         return false;
1475                                                 }
1476                                                 $pubkey = $curlResult->getBody();
1477                                         }
1478
1479                                         $key = explode(".", $pubkey);
1480
1481                                         if (sizeof($key) >= 3) {
1482                                                 $m = Strings::base64UrlDecode($key[1]);
1483                                                 $e = Strings::base64UrlDecode($key[2]);
1484                                                 $data["pubkey"] = Crypto::meToPem($m, $e);
1485                                         }
1486                                 }
1487                         }
1488                 }
1489
1490                 if (isset($data["notify"]) && isset($data["pubkey"])
1491                         && isset($data["poll"])
1492                         && isset($data["url"])
1493                 ) {
1494                         $data["network"] = Protocol::OSTATUS;
1495                 } else {
1496                         return false;
1497                 }
1498
1499                 if ($short) {
1500                         return true;
1501                 }
1502
1503                 // Fetch all additional data from the feed
1504                 $curlResult = Network::curl($data["poll"]);
1505                 if ($curlResult->isTimeout()) {
1506                         self::$istimeout = true;
1507                         return false;
1508                 }
1509                 $feed = $curlResult->getBody();
1510                 $feed_data = Feed::import($feed);
1511                 if (!$feed_data) {
1512                         return false;
1513                 }
1514
1515                 if (!empty($feed_data["header"]["author-name"])) {
1516                         $data["name"] = $feed_data["header"]["author-name"];
1517                 }
1518                 if (!empty($feed_data["header"]["author-nick"])) {
1519                         $data["nick"] = $feed_data["header"]["author-nick"];
1520                 }
1521                 if (!empty($feed_data["header"]["author-avatar"])) {
1522                         $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1523                 }
1524                 if (!empty($feed_data["header"]["author-id"])) {
1525                         $data["alias"] = $feed_data["header"]["author-id"];
1526                 }
1527                 if (!empty($feed_data["header"]["author-location"])) {
1528                         $data["location"] = $feed_data["header"]["author-location"];
1529                 }
1530                 if (!empty($feed_data["header"]["author-about"])) {
1531                         $data["about"] = $feed_data["header"]["author-about"];
1532                 }
1533                 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1534                 // So we take the value that we just fetched, although the other one worked as well
1535                 if (!empty($feed_data["header"]["author-link"])) {
1536                         $data["url"] = $feed_data["header"]["author-link"];
1537                 }
1538
1539                 if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
1540                         $data['url'] = $data["alias"];
1541                         $data["alias"] = '';
1542                 }
1543
1544                 /// @todo Fetch location and "about" from the feed as well
1545                 return $data;
1546         }
1547
1548         /**
1549          * Fetch data from a pump.io profile page
1550          *
1551          * @param string $profile_link Link to the profile page
1552          *
1553          * @return array profile data
1554          */
1555         private static function pumpioProfileData($profile_link)
1556         {
1557                 $curlResult = Network::curl($profile_link);
1558                 if (!$curlResult->isSuccess()) {
1559                         return false;
1560                 }
1561
1562                 $doc = new DOMDocument();
1563                 if (!@$doc->loadHTML($curlResult->getBody())) {
1564                         return false;
1565                 }
1566
1567                 $xpath = new DomXPath($doc);
1568
1569                 $data = [];
1570
1571                 $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue;
1572
1573                 if ($data["name"] == '') {
1574                         // This is ugly - but pump.io doesn't seem to know a better way for it
1575                         $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1576                         $pos = strpos($data["name"], chr(10));
1577                         if ($pos) {
1578                                 $data["name"] = trim(substr($data["name"], 0, $pos));
1579                         }
1580                 }
1581
1582                 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]");
1583
1584                 if ($data["location"] == '') {
1585                         $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]");
1586                 }
1587
1588                 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]");
1589
1590                 if ($data["about"] == '') {
1591                         $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]");
1592                 }
1593
1594                 $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0);
1595                 if (!$avatar) {
1596                         $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1597                 }
1598                 if ($avatar) {
1599                         foreach ($avatar->attributes as $attribute) {
1600                                 if ($attribute->name == "src") {
1601                                         $data["photo"] = trim($attribute->value);
1602                                 }
1603                         }
1604                 }
1605
1606                 return $data;
1607         }
1608
1609         /**
1610          * Check for pump.io contact
1611          *
1612          * @param array  $webfinger Webfinger data
1613          * @param string $addr
1614          * @return array pump.io data
1615          */
1616         private static function pumpio($webfinger, $addr)
1617         {
1618                 $data = [];
1619                 // The array is reversed to take into account the order of preference for same-rel links
1620                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1621                 foreach (array_reverse($webfinger["links"]) as $link) {
1622                         if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1623                                 && (($link["type"] ?? "") == "text/html")
1624                                 && ($link["href"] != "")
1625                         ) {
1626                                 $data["url"] = $link["href"];
1627                         } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
1628                                 $data["notify"] = $link["href"];
1629                         } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
1630                                 $data["poll"] = $link["href"];
1631                         } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
1632                                 $data["dialback"] = $link["href"];
1633                         }
1634                 }
1635                 if (isset($data["poll"]) && isset($data["notify"])
1636                         && isset($data["dialback"])
1637                         && isset($data["url"])
1638                 ) {
1639                         // by now we use these fields only for the network type detection
1640                         // So we unset all data that isn't used at the moment
1641                         unset($data["dialback"]);
1642
1643                         $data["network"] = Protocol::PUMPIO;
1644                 } else {
1645                         return false;
1646                 }
1647
1648                 $profile_data = self::pumpioProfileData($data["url"]);
1649
1650                 if (!$profile_data) {
1651                         return false;
1652                 }
1653
1654                 $data = array_merge($data, $profile_data);
1655
1656                 if (($addr != '') && ($data['name'] != '')) {
1657                         $name = trim(str_replace($addr, '', $data['name']));
1658                         if ($name != '') {
1659                                 $data['name'] = $name;
1660                         }
1661                 }
1662
1663                 return $data;
1664         }
1665
1666         /**
1667          * Check for twitter contact
1668          *
1669          * @param string $uri
1670          *
1671          * @return array twitter data
1672          */
1673         private static function twitter($uri)
1674         {
1675                 if (preg_match('=(.*)@twitter.com=i', $uri, $matches)) {
1676                         $nick = $matches[1];
1677                 } elseif (preg_match('=https?://twitter.com/(.*)=i', $uri, $matches)) {
1678                         $nick = $matches[1];
1679                 } else {
1680                         return [];
1681                 }
1682
1683                 $data = [];
1684                 $data['url'] = 'https://twitter.com/' . $nick;
1685                 $data['addr'] = $nick . '@twitter.com';
1686                 $data['nick'] = $data['name'] = $nick;
1687                 $data['network'] = Protocol::TWITTER;
1688                 $data['baseurl'] = 'https://twitter.com';
1689
1690                 $curlResult = Network::curl($data['url'], false);
1691                 if (!$curlResult->isSuccess()) {
1692                         return [];
1693                 }
1694
1695                 $body = $curlResult->getBody();
1696                 $doc = new DOMDocument();
1697                 @$doc->loadHTML($body);
1698                 $xpath = new DOMXPath($doc);
1699
1700                 $list = $xpath->query('//img[@class]');
1701                 foreach ($list as $node) {
1702                         $img_attr = [];
1703                         if ($node->attributes->length) {
1704                                 foreach ($node->attributes as $attribute) {
1705                                         $img_attr[$attribute->name] = $attribute->value;
1706                                 }
1707                         }
1708
1709                         if (empty($img_attr['class'])) {
1710                                 continue;
1711                         }
1712
1713                         if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) {
1714                                 if (!empty($img_attr['src'])) {
1715                                         $data['photo'] = $img_attr['src'];
1716                                 }
1717                                 if (!empty($img_attr['alt'])) {
1718                                         $data['name'] = $img_attr['alt'];
1719                                 }
1720                         }
1721                 }
1722
1723                 return $data;
1724         }
1725
1726         /**
1727          * Check page for feed link
1728          *
1729          * @param string $url Page link
1730          *
1731          * @return string feed link
1732          */
1733         private static function getFeedLink($url)
1734         {
1735                 $curlResult = Network::curl($url);
1736                 if (!$curlResult->isSuccess()) {
1737                         return false;
1738                 }
1739
1740                 $doc = new DOMDocument();
1741                 if (!@$doc->loadHTML($curlResult->getBody())) {
1742                         return false;
1743                 }
1744
1745                 $xpath = new DomXPath($doc);
1746
1747                 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1748                 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1749                 if (!is_object($feeds)) {
1750                         return false;
1751                 }
1752
1753                 if ($feeds->length == 0) {
1754                         return false;
1755                 }
1756
1757                 $feed_url = "";
1758
1759                 foreach ($feeds as $feed) {
1760                         $attr = [];
1761                         foreach ($feed->attributes as $attribute) {
1762                                 $attr[$attribute->name] = trim($attribute->value);
1763                         }
1764
1765                         if (empty($feed_url) && !empty($attr['href'])) {
1766                                 $feed_url = $attr["href"];
1767                         }
1768                 }
1769
1770                 return $feed_url;
1771         }
1772
1773         /**
1774          * Check for feed contact
1775          *
1776          * @param string  $url   Profile link
1777          * @param boolean $probe Do a probe if the page contains a feed link
1778          *
1779          * @return array feed data
1780          * @throws HTTPException\InternalServerErrorException
1781          */
1782         private static function feed($url, $probe = true)
1783         {
1784                 $curlResult = Network::curl($url);
1785                 if ($curlResult->isTimeout()) {
1786                         self::$istimeout = true;
1787                         return false;
1788                 }
1789                 $feed = $curlResult->getBody();
1790                 $feed_data = Feed::import($feed);
1791
1792                 if (!$feed_data) {
1793                         if (!$probe) {
1794                                 return false;
1795                         }
1796
1797                         $feed_url = self::getFeedLink($url);
1798
1799                         if (!$feed_url) {
1800                                 return false;
1801                         }
1802
1803                         return self::feed($feed_url, false);
1804                 }
1805
1806                 if (!empty($feed_data["header"]["author-name"])) {
1807                         $data["name"] = $feed_data["header"]["author-name"];
1808                 }
1809
1810                 if (!empty($feed_data["header"]["author-nick"])) {
1811                         $data["nick"] = $feed_data["header"]["author-nick"];
1812                 }
1813
1814                 if (!empty($feed_data["header"]["author-avatar"])) {
1815                         $data["photo"] = $feed_data["header"]["author-avatar"];
1816                 }
1817
1818                 if (!empty($feed_data["header"]["author-id"])) {
1819                         $data["alias"] = $feed_data["header"]["author-id"];
1820                 }
1821
1822                 $data["url"] = $url;
1823                 $data["poll"] = $url;
1824
1825                 if (!empty($feed_data["header"]["author-link"])) {
1826                         $data["baseurl"] = $feed_data["header"]["author-link"];
1827                 } else {
1828                         $data["baseurl"] = $data["url"];
1829                 }
1830
1831                 $data["network"] = Protocol::FEED;
1832
1833                 return $data;
1834         }
1835
1836         /**
1837          * Check for mail contact
1838          *
1839          * @param string  $uri Profile link
1840          * @param integer $uid User ID
1841          *
1842          * @return array mail data
1843          * @throws \Exception
1844          */
1845         private static function mail($uri, $uid)
1846         {
1847                 if (!Network::isEmailDomainValid($uri)) {
1848                         return false;
1849                 }
1850
1851                 if ($uid == 0) {
1852                         return false;
1853                 }
1854
1855                 $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $uid]);
1856
1857                 $condition = ["`uid` = ? AND `server` != ''", $uid];
1858                 $fields = ['pass', 'user', 'server', 'port', 'ssltype', 'mailbox'];
1859                 $mailacct = DBA::selectFirst('mailacct', $fields, $condition);
1860
1861                 if (!DBA::isResult($user) || !DBA::isResult($mailacct)) {
1862                         return false;
1863                 }
1864
1865                 $mailbox = Email::constructMailboxName($mailacct);
1866                 $password = '';
1867                 openssl_private_decrypt(hex2bin($mailacct['pass']), $password, $user['prvkey']);
1868                 $mbox = Email::connect($mailbox, $mailacct['user'], $password);
1869                 if (!$mbox) {
1870                         return false;
1871                 }
1872
1873                 $msgs = Email::poll($mbox, $uri);
1874                 Logger::log('searching '.$uri.', '.count($msgs).' messages found.', Logger::DEBUG);
1875
1876                 if (!count($msgs)) {
1877                         return false;
1878                 }
1879
1880                 $phost = substr($uri, strpos($uri, '@') + 1);
1881
1882                 $data = [];
1883                 $data["addr"]    = $uri;
1884                 $data["network"] = Protocol::MAIL;
1885                 $data["name"]    = substr($uri, 0, strpos($uri, '@'));
1886                 $data["nick"]    = $data["name"];
1887                 $data["photo"]   = Network::lookupAvatarByEmail($uri);
1888                 $data["url"]     = 'mailto:'.$uri;
1889                 $data["notify"]  = 'smtp ' . Strings::getRandomHex();
1890                 $data["poll"]    = 'email ' . Strings::getRandomHex();
1891
1892                 $x = Email::messageMeta($mbox, $msgs[0]);
1893                 if (stristr($x[0]->from, $uri)) {
1894                         $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1895                 } elseif (stristr($x[0]->to, $uri)) {
1896                         $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1897                 }
1898                 if (isset($adr)) {
1899                         foreach ($adr as $feadr) {
1900                                 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1901                                         &&(strcasecmp($feadr->host, $phost) == 0)
1902                                         && (strlen($feadr->personal))
1903                                 ) {
1904                                         $personal = imap_mime_header_decode($feadr->personal);
1905                                         $data["name"] = "";
1906                                         foreach ($personal as $perspart) {
1907                                                 if ($perspart->charset != "default") {
1908                                                         $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1909                                                 } else {
1910                                                         $data["name"] .= $perspart->text;
1911                                                 }
1912                                         }
1913
1914                                         $data["name"] = Strings::escapeTags($data["name"]);
1915                                 }
1916                         }
1917                 }
1918                 if (!empty($mbox)) {
1919                         imap_close($mbox);
1920                 }
1921                 return $data;
1922         }
1923
1924         /**
1925          * Mix two paths together to possibly fix missing parts
1926          *
1927          * @param string $avatar Path to the avatar
1928          * @param string $base   Another path that is hopefully complete
1929          *
1930          * @return string fixed avatar path
1931          * @throws \Exception
1932          */
1933         public static function fixAvatar($avatar, $base)
1934         {
1935                 $base_parts = parse_url($base);
1936
1937                 // Remove all parts that could create a problem
1938                 unset($base_parts['path']);
1939                 unset($base_parts['query']);
1940                 unset($base_parts['fragment']);
1941
1942                 $avatar_parts = parse_url($avatar);
1943
1944                 // Now we mix them
1945                 $parts = array_merge($base_parts, $avatar_parts);
1946
1947                 // And put them together again
1948                 $scheme   = isset($parts['scheme'])   ? $parts['scheme'] . '://' : '';
1949                 $host     = isset($parts['host'])     ? $parts['host']           : '';
1950                 $port     = isset($parts['port'])     ? ':' . $parts['port']     : '';
1951                 $path     = isset($parts['path'])     ? $parts['path']           : '';
1952                 $query    = isset($parts['query'])    ? '?' . $parts['query']    : '';
1953                 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1954
1955                 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1956
1957                 Logger::log('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, Logger::DATA);
1958
1959                 return $fixed;
1960         }
1961 }