]> git.mxchange.org Git - friendica.git/blob - src/Network/Probe.php
Merge pull request #8272 from MrPetovan/bug/8254-regex-url-img
[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", "gender", "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['gender'])) {
853                                 $data['gender'] = $profile['gender'];
854                         }
855                         if (!empty($profile['keywords'])) {
856                                 $keywords = implode(', ', $profile['keywords']);
857                                 if (!empty($keywords)) {
858                                         $data['keywords'] = $keywords;
859                                 }
860                         }
861
862                         $loc = [];
863                         if (!empty($profile['region'])) {
864                                 $loc['region'] = $profile['region'];
865                         }
866                         if (!empty($profile['country'])) {
867                                 $loc['country-name'] = $profile['country'];
868                         }
869                         $location = Profile::formatLocation($loc);
870                         if (!empty($location)) {
871                                 $data['location'] = $location;
872                         }
873                 }
874
875                 return $data;
876         }
877
878         /**
879          * Perform a webfinger request.
880          *
881          * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
882          *
883          * @param string $url  Address that should be probed
884          * @param string $type type
885          *
886          * @return array webfinger data
887          * @throws HTTPException\InternalServerErrorException
888          */
889         private static function webfinger($url, $type)
890         {
891                 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
892
893                 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
894                 if ($curlResult->isTimeout()) {
895                         self::$istimeout = true;
896                         return false;
897                 }
898                 $data = $curlResult->getBody();
899
900                 $webfinger = json_decode($data, true);
901                 if (is_array($webfinger)) {
902                         if (!isset($webfinger["links"])) {
903                                 Logger::log("No json webfinger links for ".$url, Logger::DEBUG);
904                                 return false;
905                         }
906                         return $webfinger;
907                 }
908
909                 // If it is not JSON, maybe it is XML
910                 $xrd = XML::parseString($data, false);
911                 if (!is_object($xrd)) {
912                         Logger::log("No webfinger data retrievable for ".$url, Logger::DEBUG);
913                         return false;
914                 }
915
916                 $xrd_arr = XML::elementToArray($xrd);
917                 if (!isset($xrd_arr["xrd"]["link"])) {
918                         Logger::log("No XML webfinger links for ".$url, Logger::DEBUG);
919                         return false;
920                 }
921
922                 $webfinger = [];
923
924                 if (!empty($xrd_arr["xrd"]["subject"])) {
925                         $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
926                 }
927
928                 if (!empty($xrd_arr["xrd"]["alias"])) {
929                         $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
930                 }
931
932                 $webfinger["links"] = [];
933
934                 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
935                         if (!empty($data["@attributes"])) {
936                                 $attributes = $data["@attributes"];
937                         } elseif ($value == "@attributes") {
938                                 $attributes = $data;
939                         } else {
940                                 continue;
941                         }
942
943                         $webfinger["links"][] = $attributes;
944                 }
945                 return $webfinger;
946         }
947
948         /**
949          * Poll the Friendica specific noscrape page.
950          *
951          * "noscrape" is a faster alternative to fetch the data from the hcard.
952          * This functionality was originally created for the directory.
953          *
954          * @param string $noscrape_url Link to the noscrape page
955          * @param array  $data         The already fetched data
956          *
957          * @return array noscrape data
958          * @throws HTTPException\InternalServerErrorException
959          */
960         private static function pollNoscrape($noscrape_url, $data)
961         {
962                 $curlResult = Network::curl($noscrape_url);
963                 if ($curlResult->isTimeout()) {
964                         self::$istimeout = true;
965                         return false;
966                 }
967                 $content = $curlResult->getBody();
968                 if (!$content) {
969                         Logger::log("Empty body for ".$noscrape_url, Logger::DEBUG);
970                         return false;
971                 }
972
973                 $json = json_decode($content, true);
974                 if (!is_array($json)) {
975                         Logger::log("No json data for ".$noscrape_url, Logger::DEBUG);
976                         return false;
977                 }
978
979                 if (!empty($json["fn"])) {
980                         $data["name"] = $json["fn"];
981                 }
982
983                 if (!empty($json["addr"])) {
984                         $data["addr"] = $json["addr"];
985                 }
986
987                 if (!empty($json["nick"])) {
988                         $data["nick"] = $json["nick"];
989                 }
990
991                 if (!empty($json["guid"])) {
992                         $data["guid"] = $json["guid"];
993                 }
994
995                 if (!empty($json["comm"])) {
996                         $data["community"] = $json["comm"];
997                 }
998
999                 if (!empty($json["tags"])) {
1000                         $keywords = implode(", ", $json["tags"]);
1001                         if ($keywords != "") {
1002                                 $data["keywords"] = $keywords;
1003                         }
1004                 }
1005
1006                 $location = Profile::formatLocation($json);
1007                 if ($location) {
1008                         $data["location"] = $location;
1009                 }
1010
1011                 if (!empty($json["about"])) {
1012                         $data["about"] = $json["about"];
1013                 }
1014
1015                 if (!empty($json["gender"])) {
1016                         $data["gender"] = $json["gender"];
1017                 }
1018
1019                 if (!empty($json["key"])) {
1020                         $data["pubkey"] = $json["key"];
1021                 }
1022
1023                 if (!empty($json["photo"])) {
1024                         $data["photo"] = $json["photo"];
1025                 }
1026
1027                 if (!empty($json["dfrn-request"])) {
1028                         $data["request"] = $json["dfrn-request"];
1029                 }
1030
1031                 if (!empty($json["dfrn-confirm"])) {
1032                         $data["confirm"] = $json["dfrn-confirm"];
1033                 }
1034
1035                 if (!empty($json["dfrn-notify"])) {
1036                         $data["notify"] = $json["dfrn-notify"];
1037                 }
1038
1039                 if (!empty($json["dfrn-poll"])) {
1040                         $data["poll"] = $json["dfrn-poll"];
1041                 }
1042
1043                 if (isset($json["hide"])) {
1044                         $data["hide"] = (bool)$json["hide"];
1045                 } else {
1046                         $data["hide"] = false;
1047                 }
1048
1049                 return $data;
1050         }
1051
1052         /**
1053          * Check for valid DFRN data
1054          *
1055          * @param array $data DFRN data
1056          *
1057          * @return int Number of errors
1058          */
1059         public static function validDfrn($data)
1060         {
1061                 $errors = 0;
1062                 if (!isset($data['key'])) {
1063                         $errors ++;
1064                 }
1065                 if (!isset($data['dfrn-request'])) {
1066                         $errors ++;
1067                 }
1068                 if (!isset($data['dfrn-confirm'])) {
1069                         $errors ++;
1070                 }
1071                 if (!isset($data['dfrn-notify'])) {
1072                         $errors ++;
1073                 }
1074                 if (!isset($data['dfrn-poll'])) {
1075                         $errors ++;
1076                 }
1077                 return $errors;
1078         }
1079
1080         /**
1081          * Fetch data from a DFRN profile page and via "noscrape"
1082          *
1083          * @param string $profile_link Link to the profile page
1084          *
1085          * @return array profile data
1086          * @throws HTTPException\InternalServerErrorException
1087          * @throws \ImagickException
1088          */
1089         public static function profile($profile_link)
1090         {
1091                 $data = [];
1092
1093                 Logger::log("Check profile ".$profile_link, Logger::DEBUG);
1094
1095                 // Fetch data via noscrape - this is faster
1096                 $noscrape_url = str_replace(["/hcard/", "/profile/"], "/noscrape/", $profile_link);
1097                 $data = self::pollNoscrape($noscrape_url, $data);
1098
1099                 if (!isset($data["notify"])
1100                         || !isset($data["confirm"])
1101                         || !isset($data["request"])
1102                         || !isset($data["poll"])
1103                         || !isset($data["name"])
1104                         || !isset($data["photo"])
1105                 ) {
1106                         $data = self::pollHcard($profile_link, $data, true);
1107                 }
1108
1109                 $prof_data = [];
1110
1111                 if (empty($data["addr"]) || empty($data["nick"])) {
1112                         $probe_data = self::uri($profile_link);
1113                         $data["addr"] = ($data["addr"] ?? '') ?: $probe_data["addr"];
1114                         $data["nick"] = ($data["nick"] ?? '') ?: $probe_data["nick"];
1115                 }
1116
1117                 $prof_data["addr"]         = $data["addr"];
1118                 $prof_data["nick"]         = $data["nick"];
1119                 $prof_data["dfrn-request"] = $data['request'] ?? null;
1120                 $prof_data["dfrn-confirm"] = $data['confirm'] ?? null;
1121                 $prof_data["dfrn-notify"]  = $data['notify']  ?? null;
1122                 $prof_data["dfrn-poll"]    = $data['poll']    ?? null;
1123                 $prof_data["photo"]        = $data['photo']   ?? null;
1124                 $prof_data["fn"]           = $data['name']    ?? null;
1125                 $prof_data["key"]          = $data['pubkey']  ?? null;
1126
1127                 Logger::log("Result for profile ".$profile_link.": ".print_r($prof_data, true), Logger::DEBUG);
1128
1129                 return $prof_data;
1130         }
1131
1132         /**
1133          * Check for DFRN contact
1134          *
1135          * @param array $webfinger Webfinger data
1136          *
1137          * @return array DFRN data
1138          * @throws HTTPException\InternalServerErrorException
1139          */
1140         private static function dfrn($webfinger)
1141         {
1142                 $hcard_url = "";
1143                 $data = [];
1144                 // The array is reversed to take into account the order of preference for same-rel links
1145                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1146                 foreach (array_reverse($webfinger["links"]) as $link) {
1147                         if (($link["rel"] == ActivityNamespace::DFRN) && !empty($link["href"])) {
1148                                 $data["network"] = Protocol::DFRN;
1149                         } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1150                                 $data["poll"] = $link["href"];
1151                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1152                                 $data["url"] = $link["href"];
1153                         } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1154                                 $hcard_url = $link["href"];
1155                         } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1156                                 $data["poco"] = $link["href"];
1157                         } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) {
1158                                 $data["photo"] = $link["href"];
1159                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1160                                 $data["baseurl"] = trim($link["href"], '/');
1161                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1162                                 $data["guid"] = $link["href"];
1163                         } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1164                                 $data["pubkey"] = base64_decode($link["href"]);
1165
1166                                 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1167                                 if (strstr($data["pubkey"], 'RSA ')) {
1168                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1169                                 }
1170                         }
1171                 }
1172
1173                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1174                         foreach ($webfinger["aliases"] as $alias) {
1175                                 if (empty($data["url"]) && !strstr($alias, "@")) {
1176                                         $data["url"] = $alias;
1177                                 } elseif (!strstr($alias, "@") && Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"])) {
1178                                         $data["alias"] = $alias;
1179                                 } elseif (substr($alias, 0, 5) == 'acct:') {
1180                                         $data["addr"] = substr($alias, 5);
1181                                 }
1182                         }
1183                 }
1184
1185                 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
1186                         $data["addr"] = substr($webfinger["subject"], 5);
1187                 }
1188
1189                 if (!isset($data["network"]) || ($hcard_url == "")) {
1190                         return false;
1191                 }
1192
1193                 // Fetch data via noscrape - this is faster
1194                 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
1195                 $data = self::pollNoscrape($noscrape_url, $data);
1196
1197                 if (isset($data["notify"])
1198                         && isset($data["confirm"])
1199                         && isset($data["request"])
1200                         && isset($data["poll"])
1201                         && isset($data["name"])
1202                         && isset($data["photo"])
1203                 ) {
1204                         return $data;
1205                 }
1206
1207                 $data = self::pollHcard($hcard_url, $data, true);
1208
1209                 return $data;
1210         }
1211
1212         /**
1213          * Poll the hcard page (Diaspora and Friendica specific)
1214          *
1215          * @param string  $hcard_url Link to the hcard page
1216          * @param array   $data      The already fetched data
1217          * @param boolean $dfrn      Poll DFRN specific data
1218          *
1219          * @return array hcard data
1220          * @throws HTTPException\InternalServerErrorException
1221          */
1222         private static function pollHcard($hcard_url, $data, $dfrn = false)
1223         {
1224                 $curlResult = Network::curl($hcard_url);
1225                 if ($curlResult->isTimeout()) {
1226                         self::$istimeout = true;
1227                         return false;
1228                 }
1229                 $content = $curlResult->getBody();
1230                 if (!$content) {
1231                         return false;
1232                 }
1233
1234                 $doc = new DOMDocument();
1235                 if (!@$doc->loadHTML($content)) {
1236                         return false;
1237                 }
1238
1239                 $xpath = new DomXPath($doc);
1240
1241                 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
1242                 if (!is_object($vcards)) {
1243                         return false;
1244                 }
1245
1246                 if (!isset($data["baseurl"])) {
1247                         $data["baseurl"] = "";
1248                 }
1249
1250                 if ($vcards->length > 0) {
1251                         $vcard = $vcards->item(0);
1252
1253                         // We have to discard the guid from the hcard in favour of the guid from lrdd
1254                         // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
1255                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
1256                         if (($search->length > 0) && empty($data["guid"])) {
1257                                 $data["guid"] = $search->item(0)->nodeValue;
1258                         }
1259
1260                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
1261                         if ($search->length > 0) {
1262                                 $data["nick"] = $search->item(0)->nodeValue;
1263                         }
1264
1265                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
1266                         if ($search->length > 0) {
1267                                 $data["name"] = $search->item(0)->nodeValue;
1268                         }
1269
1270                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
1271                         if ($search->length > 0) {
1272                                 $data["searchable"] = $search->item(0)->nodeValue;
1273                         }
1274
1275                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
1276                         if ($search->length > 0) {
1277                                 $data["pubkey"] = $search->item(0)->nodeValue;
1278                                 if (strstr($data["pubkey"], 'RSA ')) {
1279                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1280                                 }
1281                         }
1282
1283                         $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
1284                         if ($search->length > 0) {
1285                                 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
1286                         }
1287                 }
1288
1289                 $avatar = [];
1290                 if (!empty($vcard)) {
1291                         $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
1292                         foreach ($photos as $photo) {
1293                                 $attr = [];
1294                                 foreach ($photo->attributes as $attribute) {
1295                                         $attr[$attribute->name] = trim($attribute->value);
1296                                 }
1297
1298                                 if (isset($attr["src"]) && isset($attr["width"])) {
1299                                         $avatar[$attr["width"]] = $attr["src"];
1300                                 }
1301
1302                                 // We don't have a width. So we just take everything that we got.
1303                                 // This is a Hubzilla workaround which doesn't send a width.
1304                                 if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
1305                                         $avatar[] = $attr["src"];
1306                                 }
1307                         }
1308                 }
1309
1310                 if (sizeof($avatar)) {
1311                         ksort($avatar);
1312                         $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
1313                 }
1314
1315                 if ($dfrn) {
1316                         // Poll DFRN specific data
1317                         $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
1318                         if ($search->length > 0) {
1319                                 foreach ($search as $link) {
1320                                         //$data["request"] = $search->item(0)->nodeValue;
1321                                         $attr = [];
1322                                         foreach ($link->attributes as $attribute) {
1323                                                 $attr[$attribute->name] = trim($attribute->value);
1324                                         }
1325
1326                                         $data[substr($attr["rel"], 5)] = $attr["href"];
1327                                 }
1328                         }
1329
1330                         // Older Friendica versions had used the "uid" field differently than newer versions
1331                         if (!empty($data["nick"]) && !empty($data["guid"]) && ($data["nick"] == $data["guid"])) {
1332                                 unset($data["guid"]);
1333                         }
1334                 }
1335
1336
1337                 return $data;
1338         }
1339
1340         /**
1341          * Check for Diaspora contact
1342          *
1343          * @param array $webfinger Webfinger data
1344          *
1345          * @return array Diaspora data
1346          * @throws HTTPException\InternalServerErrorException
1347          */
1348         private static function diaspora($webfinger)
1349         {
1350                 $hcard_url = "";
1351                 $data = [];
1352
1353                 // The array is reversed to take into account the order of preference for same-rel links
1354                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1355                 foreach (array_reverse($webfinger["links"]) as $link) {
1356                         if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1357                                 $hcard_url = $link["href"];
1358                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1359                                 $data["baseurl"] = trim($link["href"], '/');
1360                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1361                                 $data["guid"] = $link["href"];
1362                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1363                                 $data["url"] = $link["href"];
1364                         } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1365                                 $data["poll"] = $link["href"];
1366                         } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1367                                 $data["poco"] = $link["href"];
1368                         } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1369                                 $data["notify"] = $link["href"];
1370                         } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1371                                 $data["pubkey"] = base64_decode($link["href"]);
1372
1373                                 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1374                                 if (strstr($data["pubkey"], 'RSA ')) {
1375                                         $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1376                                 }
1377                         }
1378                 }
1379
1380                 if (empty($data["url"]) || empty($hcard_url)) {
1381                         return false;
1382                 }
1383
1384                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1385                         foreach ($webfinger["aliases"] as $alias) {
1386                                 if (Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"]) && ! strstr($alias, "@")) {
1387                                         $data["alias"] = $alias;
1388                                 } elseif (substr($alias, 0, 5) == 'acct:') {
1389                                         $data["addr"] = substr($alias, 5);
1390                                 }
1391                         }
1392                 }
1393
1394                 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
1395                         $data["addr"] = substr($webfinger["subject"], 5);
1396                 }
1397
1398                 // Fetch further information from the hcard
1399                 $data = self::pollHcard($hcard_url, $data);
1400
1401                 if (!$data) {
1402                         return false;
1403                 }
1404
1405                 if (!empty($data["url"])
1406                         && !empty($data["guid"])
1407                         && !empty($data["baseurl"])
1408                         && !empty($data["pubkey"])
1409                         && !empty($hcard_url)
1410                 ) {
1411                         $data["network"] = Protocol::DIASPORA;
1412
1413                         // The Diaspora handle must always be lowercase
1414                         if (!empty($data["addr"])) {
1415                                 $data["addr"] = strtolower($data["addr"]);
1416                         }
1417
1418                         // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
1419                         $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
1420                         $data["batch"]  = $data["baseurl"] . "/receive/public";
1421                 } else {
1422                         return false;
1423                 }
1424
1425                 return $data;
1426         }
1427
1428         /**
1429          * Check for OStatus contact
1430          *
1431          * @param array $webfinger Webfinger data
1432          * @param bool  $short     Short detection mode
1433          *
1434          * @return array|bool OStatus data or "false" on error or "true" on short mode
1435          * @throws HTTPException\InternalServerErrorException
1436          */
1437         private static function ostatus($webfinger, $short = false)
1438         {
1439                 $data = [];
1440
1441                 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1442                         foreach ($webfinger["aliases"] as $alias) {
1443                                 if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) {
1444                                         $data["addr"] = str_replace('acct:', '', $alias);
1445                                 }
1446                         }
1447                 }
1448
1449                 if (!empty($webfinger["subject"]) && strstr($webfinger["subject"], "@")
1450                         && !strstr(Strings::normaliseLink($webfinger["subject"]), "http://")
1451                 ) {
1452                         $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1453                 }
1454
1455                 if (is_array($webfinger["links"])) {
1456                         // The array is reversed to take into account the order of preference for same-rel links
1457                         // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1458                         foreach (array_reverse($webfinger["links"]) as $link) {
1459                                 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1460                                         && (($link["type"] ?? "") == "text/html")
1461                                         && ($link["href"] != "")
1462                                 ) {
1463                                         $data["url"] = $link["href"];
1464                                 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1465                                         $data["notify"] = $link["href"];
1466                                 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1467                                         $data["poll"] = $link["href"];
1468                                 } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) {
1469                                         $pubkey = $link["href"];
1470
1471                                         if (substr($pubkey, 0, 5) === 'data:') {
1472                                                 if (strstr($pubkey, ',')) {
1473                                                         $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1474                                                 } else {
1475                                                         $pubkey = substr($pubkey, 5);
1476                                                 }
1477                                         } elseif (Strings::normaliseLink($pubkey) == 'http://') {
1478                                                 $curlResult = Network::curl($pubkey);
1479                                                 if ($curlResult->isTimeout()) {
1480                                                         self::$istimeout = true;
1481                                                         return false;
1482                                                 }
1483                                                 $pubkey = $curlResult->getBody();
1484                                         }
1485
1486                                         $key = explode(".", $pubkey);
1487
1488                                         if (sizeof($key) >= 3) {
1489                                                 $m = Strings::base64UrlDecode($key[1]);
1490                                                 $e = Strings::base64UrlDecode($key[2]);
1491                                                 $data["pubkey"] = Crypto::meToPem($m, $e);
1492                                         }
1493                                 }
1494                         }
1495                 }
1496
1497                 if (isset($data["notify"]) && isset($data["pubkey"])
1498                         && isset($data["poll"])
1499                         && isset($data["url"])
1500                 ) {
1501                         $data["network"] = Protocol::OSTATUS;
1502                 } else {
1503                         return false;
1504                 }
1505
1506                 if ($short) {
1507                         return true;
1508                 }
1509
1510                 // Fetch all additional data from the feed
1511                 $curlResult = Network::curl($data["poll"]);
1512                 if ($curlResult->isTimeout()) {
1513                         self::$istimeout = true;
1514                         return false;
1515                 }
1516                 $feed = $curlResult->getBody();
1517                 $feed_data = Feed::import($feed);
1518                 if (!$feed_data) {
1519                         return false;
1520                 }
1521
1522                 if (!empty($feed_data["header"]["author-name"])) {
1523                         $data["name"] = $feed_data["header"]["author-name"];
1524                 }
1525                 if (!empty($feed_data["header"]["author-nick"])) {
1526                         $data["nick"] = $feed_data["header"]["author-nick"];
1527                 }
1528                 if (!empty($feed_data["header"]["author-avatar"])) {
1529                         $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1530                 }
1531                 if (!empty($feed_data["header"]["author-id"])) {
1532                         $data["alias"] = $feed_data["header"]["author-id"];
1533                 }
1534                 if (!empty($feed_data["header"]["author-location"])) {
1535                         $data["location"] = $feed_data["header"]["author-location"];
1536                 }
1537                 if (!empty($feed_data["header"]["author-about"])) {
1538                         $data["about"] = $feed_data["header"]["author-about"];
1539                 }
1540                 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1541                 // So we take the value that we just fetched, although the other one worked as well
1542                 if (!empty($feed_data["header"]["author-link"])) {
1543                         $data["url"] = $feed_data["header"]["author-link"];
1544                 }
1545
1546                 if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
1547                         $data['url'] = $data["alias"];
1548                         $data["alias"] = '';
1549                 }
1550
1551                 /// @todo Fetch location and "about" from the feed as well
1552                 return $data;
1553         }
1554
1555         /**
1556          * Fetch data from a pump.io profile page
1557          *
1558          * @param string $profile_link Link to the profile page
1559          *
1560          * @return array profile data
1561          */
1562         private static function pumpioProfileData($profile_link)
1563         {
1564                 $curlResult = Network::curl($profile_link);
1565                 if (!$curlResult->isSuccess()) {
1566                         return false;
1567                 }
1568
1569                 $doc = new DOMDocument();
1570                 if (!@$doc->loadHTML($curlResult->getBody())) {
1571                         return false;
1572                 }
1573
1574                 $xpath = new DomXPath($doc);
1575
1576                 $data = [];
1577
1578                 $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue;
1579
1580                 if ($data["name"] == '') {
1581                         // This is ugly - but pump.io doesn't seem to know a better way for it
1582                         $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1583                         $pos = strpos($data["name"], chr(10));
1584                         if ($pos) {
1585                                 $data["name"] = trim(substr($data["name"], 0, $pos));
1586                         }
1587                 }
1588
1589                 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]");
1590
1591                 if ($data["location"] == '') {
1592                         $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]");
1593                 }
1594
1595                 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]");
1596
1597                 if ($data["about"] == '') {
1598                         $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]");
1599                 }
1600
1601                 $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0);
1602                 if (!$avatar) {
1603                         $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1604                 }
1605                 if ($avatar) {
1606                         foreach ($avatar->attributes as $attribute) {
1607                                 if ($attribute->name == "src") {
1608                                         $data["photo"] = trim($attribute->value);
1609                                 }
1610                         }
1611                 }
1612
1613                 return $data;
1614         }
1615
1616         /**
1617          * Check for pump.io contact
1618          *
1619          * @param array  $webfinger Webfinger data
1620          * @param string $addr
1621          * @return array pump.io data
1622          */
1623         private static function pumpio($webfinger, $addr)
1624         {
1625                 $data = [];
1626                 // The array is reversed to take into account the order of preference for same-rel links
1627                 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1628                 foreach (array_reverse($webfinger["links"]) as $link) {
1629                         if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1630                                 && (($link["type"] ?? "") == "text/html")
1631                                 && ($link["href"] != "")
1632                         ) {
1633                                 $data["url"] = $link["href"];
1634                         } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
1635                                 $data["notify"] = $link["href"];
1636                         } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
1637                                 $data["poll"] = $link["href"];
1638                         } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
1639                                 $data["dialback"] = $link["href"];
1640                         }
1641                 }
1642                 if (isset($data["poll"]) && isset($data["notify"])
1643                         && isset($data["dialback"])
1644                         && isset($data["url"])
1645                 ) {
1646                         // by now we use these fields only for the network type detection
1647                         // So we unset all data that isn't used at the moment
1648                         unset($data["dialback"]);
1649
1650                         $data["network"] = Protocol::PUMPIO;
1651                 } else {
1652                         return false;
1653                 }
1654
1655                 $profile_data = self::pumpioProfileData($data["url"]);
1656
1657                 if (!$profile_data) {
1658                         return false;
1659                 }
1660
1661                 $data = array_merge($data, $profile_data);
1662
1663                 if (($addr != '') && ($data['name'] != '')) {
1664                         $name = trim(str_replace($addr, '', $data['name']));
1665                         if ($name != '') {
1666                                 $data['name'] = $name;
1667                         }
1668                 }
1669
1670                 return $data;
1671         }
1672
1673         /**
1674          * Check for twitter contact
1675          *
1676          * @param string $uri
1677          *
1678          * @return array twitter data
1679          */
1680         private static function twitter($uri)
1681         {
1682                 if (preg_match('=(.*)@twitter.com=i', $uri, $matches)) {
1683                         $nick = $matches[1];
1684                 } elseif (preg_match('=https?://twitter.com/(.*)=i', $uri, $matches)) {
1685                         $nick = $matches[1];
1686                 } else {
1687                         return [];
1688                 }
1689
1690                 $data = [];
1691                 $data['url'] = 'https://twitter.com/' . $nick;
1692                 $data['addr'] = $nick . '@twitter.com';
1693                 $data['nick'] = $data['name'] = $nick;
1694                 $data['network'] = Protocol::TWITTER;
1695                 $data['baseurl'] = 'https://twitter.com';
1696
1697                 $curlResult = Network::curl($data['url'], false);
1698                 if (!$curlResult->isSuccess()) {
1699                         return [];
1700                 }
1701
1702                 $body = $curlResult->getBody();
1703                 $doc = new DOMDocument();
1704                 @$doc->loadHTML($body);
1705                 $xpath = new DOMXPath($doc);
1706
1707                 $list = $xpath->query('//img[@class]');
1708                 foreach ($list as $node) {
1709                         $img_attr = [];
1710                         if ($node->attributes->length) {
1711                                 foreach ($node->attributes as $attribute) {
1712                                         $img_attr[$attribute->name] = $attribute->value;
1713                                 }
1714                         }
1715
1716                         if (empty($img_attr['class'])) {
1717                                 continue;
1718                         }
1719
1720                         if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) {
1721                                 if (!empty($img_attr['src'])) {
1722                                         $data['photo'] = $img_attr['src'];
1723                                 }
1724                                 if (!empty($img_attr['alt'])) {
1725                                         $data['name'] = $img_attr['alt'];
1726                                 }
1727                         }
1728                 }
1729
1730                 return $data;
1731         }
1732
1733         /**
1734          * Check page for feed link
1735          *
1736          * @param string $url Page link
1737          *
1738          * @return string feed link
1739          */
1740         private static function getFeedLink($url)
1741         {
1742                 $curlResult = Network::curl($url);
1743                 if (!$curlResult->isSuccess()) {
1744                         return false;
1745                 }
1746
1747                 $doc = new DOMDocument();
1748                 if (!@$doc->loadHTML($curlResult->getBody())) {
1749                         return false;
1750                 }
1751
1752                 $xpath = new DomXPath($doc);
1753
1754                 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1755                 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1756                 if (!is_object($feeds)) {
1757                         return false;
1758                 }
1759
1760                 if ($feeds->length == 0) {
1761                         return false;
1762                 }
1763
1764                 $feed_url = "";
1765
1766                 foreach ($feeds as $feed) {
1767                         $attr = [];
1768                         foreach ($feed->attributes as $attribute) {
1769                                 $attr[$attribute->name] = trim($attribute->value);
1770                         }
1771
1772                         if (empty($feed_url) && !empty($attr['href'])) {
1773                                 $feed_url = $attr["href"];
1774                         }
1775                 }
1776
1777                 return $feed_url;
1778         }
1779
1780         /**
1781          * Check for feed contact
1782          *
1783          * @param string  $url   Profile link
1784          * @param boolean $probe Do a probe if the page contains a feed link
1785          *
1786          * @return array feed data
1787          * @throws HTTPException\InternalServerErrorException
1788          */
1789         private static function feed($url, $probe = true)
1790         {
1791                 $curlResult = Network::curl($url);
1792                 if ($curlResult->isTimeout()) {
1793                         self::$istimeout = true;
1794                         return false;
1795                 }
1796                 $feed = $curlResult->getBody();
1797                 $feed_data = Feed::import($feed);
1798
1799                 if (!$feed_data) {
1800                         if (!$probe) {
1801                                 return false;
1802                         }
1803
1804                         $feed_url = self::getFeedLink($url);
1805
1806                         if (!$feed_url) {
1807                                 return false;
1808                         }
1809
1810                         return self::feed($feed_url, false);
1811                 }
1812
1813                 if (!empty($feed_data["header"]["author-name"])) {
1814                         $data["name"] = $feed_data["header"]["author-name"];
1815                 }
1816
1817                 if (!empty($feed_data["header"]["author-nick"])) {
1818                         $data["nick"] = $feed_data["header"]["author-nick"];
1819                 }
1820
1821                 if (!empty($feed_data["header"]["author-avatar"])) {
1822                         $data["photo"] = $feed_data["header"]["author-avatar"];
1823                 }
1824
1825                 if (!empty($feed_data["header"]["author-id"])) {
1826                         $data["alias"] = $feed_data["header"]["author-id"];
1827                 }
1828
1829                 $data["url"] = $url;
1830                 $data["poll"] = $url;
1831
1832                 if (!empty($feed_data["header"]["author-link"])) {
1833                         $data["baseurl"] = $feed_data["header"]["author-link"];
1834                 } else {
1835                         $data["baseurl"] = $data["url"];
1836                 }
1837
1838                 $data["network"] = Protocol::FEED;
1839
1840                 return $data;
1841         }
1842
1843         /**
1844          * Check for mail contact
1845          *
1846          * @param string  $uri Profile link
1847          * @param integer $uid User ID
1848          *
1849          * @return array mail data
1850          * @throws \Exception
1851          */
1852         private static function mail($uri, $uid)
1853         {
1854                 if (!Network::isEmailDomainValid($uri)) {
1855                         return false;
1856                 }
1857
1858                 if ($uid == 0) {
1859                         return false;
1860                 }
1861
1862                 $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $uid]);
1863
1864                 $condition = ["`uid` = ? AND `server` != ''", $uid];
1865                 $fields = ['pass', 'user', 'server', 'port', 'ssltype', 'mailbox'];
1866                 $mailacct = DBA::selectFirst('mailacct', $fields, $condition);
1867
1868                 if (!DBA::isResult($user) || !DBA::isResult($mailacct)) {
1869                         return false;
1870                 }
1871
1872                 $mailbox = Email::constructMailboxName($mailacct);
1873                 $password = '';
1874                 openssl_private_decrypt(hex2bin($mailacct['pass']), $password, $user['prvkey']);
1875                 $mbox = Email::connect($mailbox, $mailacct['user'], $password);
1876                 if (!$mbox) {
1877                         return false;
1878                 }
1879
1880                 $msgs = Email::poll($mbox, $uri);
1881                 Logger::log('searching '.$uri.', '.count($msgs).' messages found.', Logger::DEBUG);
1882
1883                 if (!count($msgs)) {
1884                         return false;
1885                 }
1886
1887                 $phost = substr($uri, strpos($uri, '@') + 1);
1888
1889                 $data = [];
1890                 $data["addr"]    = $uri;
1891                 $data["network"] = Protocol::MAIL;
1892                 $data["name"]    = substr($uri, 0, strpos($uri, '@'));
1893                 $data["nick"]    = $data["name"];
1894                 $data["photo"]   = Network::lookupAvatarByEmail($uri);
1895                 $data["url"]     = 'mailto:'.$uri;
1896                 $data["notify"]  = 'smtp ' . Strings::getRandomHex();
1897                 $data["poll"]    = 'email ' . Strings::getRandomHex();
1898
1899                 $x = Email::messageMeta($mbox, $msgs[0]);
1900                 if (stristr($x[0]->from, $uri)) {
1901                         $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1902                 } elseif (stristr($x[0]->to, $uri)) {
1903                         $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1904                 }
1905                 if (isset($adr)) {
1906                         foreach ($adr as $feadr) {
1907                                 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1908                                         &&(strcasecmp($feadr->host, $phost) == 0)
1909                                         && (strlen($feadr->personal))
1910                                 ) {
1911                                         $personal = imap_mime_header_decode($feadr->personal);
1912                                         $data["name"] = "";
1913                                         foreach ($personal as $perspart) {
1914                                                 if ($perspart->charset != "default") {
1915                                                         $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1916                                                 } else {
1917                                                         $data["name"] .= $perspart->text;
1918                                                 }
1919                                         }
1920
1921                                         $data["name"] = Strings::escapeTags($data["name"]);
1922                                 }
1923                         }
1924                 }
1925                 if (!empty($mbox)) {
1926                         imap_close($mbox);
1927                 }
1928                 return $data;
1929         }
1930
1931         /**
1932          * Mix two paths together to possibly fix missing parts
1933          *
1934          * @param string $avatar Path to the avatar
1935          * @param string $base   Another path that is hopefully complete
1936          *
1937          * @return string fixed avatar path
1938          * @throws \Exception
1939          */
1940         public static function fixAvatar($avatar, $base)
1941         {
1942                 $base_parts = parse_url($base);
1943
1944                 // Remove all parts that could create a problem
1945                 unset($base_parts['path']);
1946                 unset($base_parts['query']);
1947                 unset($base_parts['fragment']);
1948
1949                 $avatar_parts = parse_url($avatar);
1950
1951                 // Now we mix them
1952                 $parts = array_merge($base_parts, $avatar_parts);
1953
1954                 // And put them together again
1955                 $scheme   = isset($parts['scheme'])   ? $parts['scheme'] . '://' : '';
1956                 $host     = isset($parts['host'])     ? $parts['host']           : '';
1957                 $port     = isset($parts['port'])     ? ':' . $parts['port']     : '';
1958                 $path     = isset($parts['path'])     ? $parts['path']           : '';
1959                 $query    = isset($parts['query'])    ? '?' . $parts['query']    : '';
1960                 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1961
1962                 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1963
1964                 Logger::log('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, Logger::DATA);
1965
1966                 return $fixed;
1967         }
1968 }