]> git.mxchange.org Git - friendica.git/blob - src/Network/Probe.php
remove use of PConfig Class from Probe class because we don't use it here
[friendica.git] / src / Network / Probe.php
1 <?php
2
3 namespace Friendica\Network;
4
5 /**
6  * @file src/Network/Probe.php
7  * @brief Functions for probing URL
8  *
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Config;
13
14 require_once 'include/feed.php';
15 require_once 'include/email.php';
16 require_once 'include/network.php';
17
18 /**
19  * @brief This class contain functions for probing URL
20  *
21  */
22 class Probe {
23
24         private static $baseurl;
25
26         /**
27          * @brief Rearrange the array so that it always has the same order
28          *
29          * @param array $data Unordered data
30          *
31          * @return array Ordered data
32          */
33         private function rearrangeData($data) {
34                 $fields = array("name", "nick", "guid", "url", "addr", "alias",
35                                 "photo", "community", "keywords", "location", "about",
36                                 "batch", "notify", "poll", "request", "confirm", "poco",
37                                 "priority", "network", "pubkey", "baseurl");
38
39                 $newdata = array();
40                 foreach ($fields as $field) {
41                         if (isset($data[$field])) {
42                                 $newdata[$field] = $data[$field];
43                         } else {
44                                 $newdata[$field] = "";
45                         }
46                 }
47
48                 // We don't use the "priority" field anymore and replace it with a dummy.
49                 $newdata["priority"] = 0;
50
51                 return $newdata;
52         }
53
54         /**
55          * @brief Probes for XRD data
56          *
57          * @param string $host The host part of an url
58          *
59          * @return array
60          *      'lrdd' => Link to LRDD endpoint
61          *      'lrdd-xml' => Link to LRDD endpoint in XML format
62          *      'lrdd-json' => Link to LRDD endpoint in JSON format
63          */
64         private function xrd($host) {
65
66                 // Reset the static variable
67                 self::$baseurl = '';
68
69                 $ssl_url = "https://".$host."/.well-known/host-meta";
70                 $url = "http://".$host."/.well-known/host-meta";
71
72                 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
73                 $redirects = 0;
74
75                 $ret = z_fetch_url($ssl_url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
76                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
77                         return false;
78                 }
79                 $xml = $ret['body'];
80
81                 $xrd = parse_xml_string($xml, false);
82
83                 if (!is_object($xrd)) {
84                         $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
85                         if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
86                                 return false;
87                         }
88                         $xml = $ret['body'];
89                         $xrd = parse_xml_string($xml, false);
90                 }
91                 if (!is_object($xrd)) {
92                         return false;
93                 }
94
95                 $links = \xml::element_to_array($xrd);
96                 if (!isset($links["xrd"]["link"])) {
97                         return false;
98                 }
99
100                 $xrd_data = array();
101
102                 foreach ($links["xrd"]["link"] as $value => $link) {
103                         if (isset($link["@attributes"])) {
104                                 $attributes = $link["@attributes"];
105                         } elseif ($value == "@attributes") {
106                                 $attributes = $link;
107                         } else {
108                                 continue;
109                         }
110
111                         if (($attributes["rel"] == "lrdd")
112                                 AND ($attributes["type"] == "application/xrd+xml")
113                         ) {
114                                 $xrd_data["lrdd-xml"] = $attributes["template"];
115                         } elseif (($attributes["rel"] == "lrdd")
116                                 AND ($attributes["type"] == "application/json")
117                         ) {
118                                 $xrd_data["lrdd-json"] = $attributes["template"];
119                         } elseif ($attributes["rel"] == "lrdd") {
120                                 $xrd_data["lrdd"] = $attributes["template"];
121                         }
122                 }
123
124                 self::$baseurl = "http://".$host;
125
126                 return $xrd_data;
127         }
128
129         /**
130          * @brief Perform Webfinger lookup and return DFRN data
131          *
132          * Given an email style address, perform webfinger lookup and
133          * return the resulting DFRN profile URL, or if no DFRN profile URL
134          * is located, returns an OStatus subscription template (prefixed
135          * with the string 'stat:' to identify it as on OStatus template).
136          * If this isn't an email style address just return $webbie.
137          * Return an empty string if email-style addresses but webfinger fails,
138          * or if the resultant personal XRD doesn't contain a supported
139          * subscription/friend-request attribute.
140          *
141          * amended 7/9/2011 to return an hcard which could save potentially loading
142          * a lengthy content page to scrape dfrn attributes
143          *
144          * @param string $webbie Address that should be probed
145          * @param string $hcard_url Link to the hcard - is returned by reference
146          *
147          * @return string profile link
148          */
149
150         public static function webfingerDfrn($webbie, &$hcard_url) {
151
152                 $profile_link = '';
153
154                 $links = self::lrdd($webbie);
155                 logger('webfingerDfrn: '.$webbie.':'.print_r($links, true), LOGGER_DATA);
156                 if (count($links)) {
157                         foreach ($links as $link) {
158                                 if ($link['@attributes']['rel'] === NAMESPACE_DFRN) {
159                                         $profile_link = $link['@attributes']['href'];
160                                 }
161                                 if (($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) AND ($profile_link == "")) {
162                                         $profile_link = 'stat:'.$link['@attributes']['template'];
163                                 }
164                                 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
165                                         $hcard_url = $link['@attributes']['href'];
166                                 }
167                         }
168                 }
169                 return $profile_link;
170         }
171
172         /**
173          * @brief Check an URI for LRDD data
174          *
175          * this is a replacement for the "lrdd" function in include/network.php.
176          * It isn't used in this class and has some redundancies in the code.
177          * When time comes we can check the existing calls for "lrdd" if we can rework them.
178          *
179          * @param string $uri Address that should be probed
180          *
181          * @return array uri data
182          */
183         public static function lrdd($uri) {
184
185                 $lrdd = self::xrd($uri);
186                 $webfinger = null;
187
188                 if (!$lrdd) {
189                         $parts = @parse_url($uri);
190                         if (!$parts) {
191                                 return array();
192                         }
193
194                         $host = $parts["host"];
195                         if (isset($parts["port"])) {
196                                 $host .= ':'.$parts["port"];
197                         }
198
199                         $path_parts = explode("/", trim($parts["path"], "/"));
200
201                         $nick = array_pop($path_parts);
202
203                         do {
204                                 $lrdd = self::xrd($host);
205                                 $host .= "/".array_shift($path_parts);
206                         } while (!$lrdd AND (sizeof($path_parts) > 0));
207                 }
208
209                 if (!$lrdd) {
210                         return array();
211                 }
212
213                 foreach ($lrdd as $key => $link) {
214                         if ($webfinger) {
215                                 continue;
216                         }
217
218                         if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json"))) {
219                                 continue;
220                         }
221
222                         $path = str_replace('{uri}', urlencode($uri), $link);
223                         $webfinger = self::webfinger($path);
224
225                         if (!$webfinger AND (strstr($uri, "@"))) {
226                                 $path = str_replace('{uri}', urlencode("acct:".$uri), $link);
227                                 $webfinger = self::webfinger($path);
228                         }
229
230                         // Special treatment for Mastodon
231                         // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
232                         // But the webfinger for this format fails.
233                         if (!$webfinger AND isset($nick)) {
234                                 // Mastodon uses a "@" as prefix for usernames in their url format
235                                 $nick = ltrim($nick, '@');
236
237                                 $addr = $nick."@".$host;
238
239                                 $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
240                                 $webfinger = self::webfinger($path);
241                         }
242                 }
243
244                 if (!is_array($webfinger["links"])) {
245                         return false;
246                 }
247
248                 $data = array();
249
250                 foreach ($webfinger["links"] as $link) {
251                         $data[] = array("@attributes" => $link);
252                 }
253
254                 if (is_array($webfinger["aliases"])) {
255                         foreach ($webfinger["aliases"] as $alias) {
256                                 $data[] = array("@attributes" =>
257                                                         array("rel" => "alias",
258                                                                 "href" => $alias));
259                         }
260                 }
261
262                 return $data;
263         }
264
265         /**
266          * @brief Fetch information (protocol endpoints and user information) about a given uri
267          *
268          * @param string $uri Address that should be probed
269          * @param string $network Test for this specific network
270          * @param integer $uid User ID for the probe (only used for mails)
271          * @param boolean $cache Use cached values?
272          *
273          * @return array uri data
274          */
275         public static function uri($uri, $network = "", $uid = 0, $cache = true) {
276
277                 if ($cache) {
278                         $result = \Cache::get("probe_url:".$network.":".$uri);
279                         if (!is_null($result)) {
280                                 return $result;
281                         }
282                 }
283
284                 if ($uid == 0) {
285                         $uid = local_user();
286                 }
287
288                 $data = self::detect($uri, $network, $uid);
289
290                 if (!isset($data["url"])) {
291                         $data["url"] = $uri;
292                 }
293
294                 if ($data["photo"] != "") {
295                         $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
296                 } else {
297                         $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
298                 }
299
300                 if (!isset($data["name"]) OR ($data["name"] == "")) {
301                         if (isset($data["nick"])) {
302                                 $data["name"] = $data["nick"];
303                         }
304
305                         if ($data["name"] == "") {
306                                 $data["name"] = $data["url"];
307                         }
308                 }
309
310                 if (!isset($data["nick"]) OR ($data["nick"] == "")) {
311                         $data["nick"] = strtolower($data["name"]);
312
313                         if (strpos($data['nick'], ' ')) {
314                                 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
315                         }
316                 }
317
318                 if (self::$baseurl != "") {
319                         $data["baseurl"] = self::$baseurl;
320                 }
321
322                 if (!isset($data["network"])) {
323                         $data["network"] = NETWORK_PHANTOM;
324                 }
325
326                 $data = self::rearrangeData($data);
327
328                 // Only store into the cache if the value seems to be valid
329                 if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
330                         \Cache::set("probe_url:".$network.":".$uri, $data, CACHE_DAY);
331
332                         /// @todo temporary fix - we need a real contact update function that updates only changing fields
333                         /// The biggest problem is the avatar picture that could have a reduced image size.
334                         /// It should only be updated if the existing picture isn't existing anymore.
335                         if (($data['network'] != NETWORK_FEED)
336                                 AND ($data["network"] != NETWORK_DIASPORA)
337                                 AND $data["name"]
338                                 AND $data["nick"]
339                                 AND $data["url"]
340                                 AND $data["addr"]
341                                 AND $data["poll"]
342                         ) {
343                                 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
344                                                 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
345                                         WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
346                                         dbesc($data["name"]),
347                                         dbesc($data["nick"]),
348                                         dbesc($data["url"]),
349                                         dbesc($data["addr"]),
350                                         dbesc($data["notify"]),
351                                         dbesc($data["poll"]),
352                                         dbesc($data["alias"]),
353                                         dbesc(datetime_convert()),
354                                         dbesc(normalise_link($data['url']))
355                                 );
356                         }
357                 }
358
359                 return $data;
360         }
361
362         /**
363          * @brief Fetch information (protocol endpoints and user information) about a given uri
364          *
365          * This function is only called by the "uri" function that adds caching and rearranging of data.
366          *
367          * @param string $uri Address that should be probed
368          * @param string $network Test for this specific network
369          * @param integer $uid User ID for the probe (only used for mails)
370          *
371          * @return array uri data
372          */
373         private function detect($uri, $network, $uid) {
374                 $parts = parse_url($uri);
375
376                 if (isset($parts["scheme"]) AND isset($parts["host"]) AND isset($parts["path"])) {
377                         $host = $parts["host"];
378                         if (isset($parts["port"])) {
379                                 $host .= ':'.$parts["port"];
380                         }
381
382                         if ($host == 'twitter.com') {
383                                 return array("network" => NETWORK_TWITTER);
384                         }
385                         $lrdd = self::xrd($host);
386
387                         $path_parts = explode("/", trim($parts["path"], "/"));
388
389                         while (!$lrdd AND (sizeof($path_parts) > 1)) {
390                                 $host .= "/".array_shift($path_parts);
391                                 $lrdd = self::xrd($host);
392                         }
393                         if (!$lrdd) {
394                                 return self::feed($uri);
395                         }
396                         $nick = array_pop($path_parts);
397
398                         // Mastodon uses a "@" as prefix for usernames in their url format
399                         $nick = ltrim($nick, '@');
400
401                         $addr = $nick."@".$host;
402
403                 } elseif (strstr($uri, '@')) {
404                         // If the URI starts with "mailto:" then jump directly to the mail detection
405                         if (strpos($uri, 'mailto:') !== false) {
406                                 $uri = str_replace('mailto:', '', $uri);
407                                 return self::mail($uri, $uid);
408                         }
409
410                         if ($network == NETWORK_MAIL) {
411                                 return self::mail($uri, $uid);
412                         }
413                         // Remove "acct:" from the URI
414                         $uri = str_replace('acct:', '', $uri);
415
416                         $host = substr($uri, strpos($uri, '@') + 1);
417                         $nick = substr($uri, 0, strpos($uri, '@'));
418
419                         if (strpos($uri, '@twitter.com')) {
420                                 return array("network" => NETWORK_TWITTER);
421                         }
422                         $lrdd = self::xrd($host);
423
424                         if (!$lrdd) {
425                                 return self::mail($uri, $uid);
426                         }
427                         $addr = $uri;
428
429                 } else {
430                         return false;
431                 }
432
433                 $webfinger = false;
434
435                 /// @todo Do we need the prefix "acct:" or "acct://"?
436
437                 foreach ($lrdd as $key => $link) {
438                         if ($webfinger) {
439                                 continue;
440                         }
441                         if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json"))) {
442                                 continue;
443                         }
444                         // At first try it with the given uri
445                         $path = str_replace('{uri}', urlencode($uri), $link);
446                         $webfinger = self::webfinger($path);
447
448                         // We cannot be sure that the detected address was correct, so we don't use the values
449                         if ($webfinger AND ($uri != $addr)) {
450                                 $nick = "";
451                                 $addr = "";
452                         }
453
454                         // Try webfinger with the address (user@domain.tld)
455                         if (!$webfinger) {
456                                 $path = str_replace('{uri}', urlencode($addr), $link);
457                                 $webfinger = self::webfinger($path);
458                         }
459
460                         // Mastodon needs to have it with "acct:"
461                         if (!$webfinger) {
462                                 $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
463                                 $webfinger = self::webfinger($path);
464                         }
465                 }
466                 if (!$webfinger) {
467                         return self::feed($uri);
468                 }
469
470                 $result = false;
471
472                 logger("Probing ".$uri, LOGGER_DEBUG);
473
474                 if (in_array($network, array("", NETWORK_DFRN))) {
475                         $result = self::dfrn($webfinger);
476                 }
477                 if ((!$result AND ($network == "")) OR ($network == NETWORK_DIASPORA)) {
478                         $result = self::diaspora($webfinger);
479                 }
480                 if ((!$result AND ($network == "")) OR ($network == NETWORK_OSTATUS)) {
481                         $result = self::ostatus($webfinger);
482                 }
483                 if ((!$result AND ($network == "")) OR ($network == NETWORK_PUMPIO)) {
484                         $result = self::pumpio($webfinger);
485                 }
486                 if ((!$result AND ($network == "")) OR ($network == NETWORK_FEED)) {
487                         $result = self::feed($uri);
488                 } else {
489                         // We overwrite the detected nick with our try if the previois routines hadn't detected it.
490                         // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
491                         if ((!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " "))) AND ($nick != "")) {
492                                 $result["nick"] = $nick;
493                         }
494
495                         if ((!isset($result["addr"]) OR ($result["addr"] == "")) AND ($addr != "")) {
496                                 $result["addr"] = $addr;
497                         }
498                 }
499
500                 logger($uri." is ".$result["network"], LOGGER_DEBUG);
501
502                 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
503                         $pos = strpos($result["url"], $host);
504                         if ($pos) {
505                                 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
506                         }
507                 }
508
509                 return $result;
510         }
511
512         /**
513          * @brief Perform a webfinger request.
514          *
515          * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
516          *
517          * @param string $url Address that should be probed
518          *
519          * @return array webfinger data
520          */
521         private function webfinger($url) {
522
523                 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
524                 $redirects = 0;
525
526                 $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
527                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
528                         return false;
529                 }
530                 $data = $ret['body'];
531
532                 $xrd = parse_xml_string($data, false);
533
534                 if (!is_object($xrd)) {
535                         // If it is not XML, maybe it is JSON
536                         $webfinger = json_decode($data, true);
537
538                         if (!isset($webfinger["links"])) {
539                                 return false;
540                         }
541
542                         return $webfinger;
543                 }
544
545                 $xrd_arr = \xml::element_to_array($xrd);
546                 if (!isset($xrd_arr["xrd"]["link"])) {
547                         return false;
548                 }
549
550                 $webfinger = array();
551
552                 if (isset($xrd_arr["xrd"]["subject"])) {
553                         $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
554                 }
555
556                 if (isset($xrd_arr["xrd"]["alias"])) {
557                         $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
558                 }
559
560                 $webfinger["links"] = array();
561
562                 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
563                         if (isset($data["@attributes"])) {
564                                 $attributes = $data["@attributes"];
565                         } elseif ($value == "@attributes") {
566                                 $attributes = $data;
567                         } else {
568                                 continue;
569                         }
570
571                         $webfinger["links"][] = $attributes;
572                 }
573                 return $webfinger;
574         }
575
576         /**
577          * @brief Poll the Friendica specific noscrape page.
578          *
579          * "noscrape" is a faster alternative to fetch the data from the hcard.
580          * This functionality was originally created for the directory.
581          *
582          * @param string $noscrape_url Link to the noscrape page
583          * @param array $data The already fetched data
584          *
585          * @return array noscrape data
586          */
587         private function pollNoscrape($noscrape_url, $data) {
588                 $ret = z_fetch_url($noscrape_url);
589                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
590                         return false;
591                 }
592                 $content = $ret['body'];
593                 if (!$content) {
594                         return false;
595                 }
596
597                 $json = json_decode($content, true);
598                 if (!is_array($json)) {
599                         return false;
600                 }
601
602                 if (isset($json["fn"])) {
603                         $data["name"] = $json["fn"];
604                 }
605
606                 if (isset($json["addr"])) {
607                         $data["addr"] = $json["addr"];
608                 }
609
610                 if (isset($json["nick"])) {
611                         $data["nick"] = $json["nick"];
612                 }
613
614                 if (isset($json["comm"])) {
615                         $data["community"] = $json["comm"];
616                 }
617
618                 if (isset($json["tags"])) {
619                         $keywords = implode(" ", $json["tags"]);
620                         if ($keywords != "") {
621                                 $data["keywords"] = $keywords;
622                         }
623                 }
624
625                 $location = formatted_location($json);
626                 if ($location) {
627                         $data["location"] = $location;
628                 }
629
630                 if (isset($json["about"])) {
631                         $data["about"] = $json["about"];
632                 }
633
634                 if (isset($json["key"])) {
635                         $data["pubkey"] = $json["key"];
636                 }
637
638                 if (isset($json["photo"])) {
639                         $data["photo"] = $json["photo"];
640                 }
641
642                 if (isset($json["dfrn-request"])) {
643                         $data["request"] = $json["dfrn-request"];
644                 }
645
646                 if (isset($json["dfrn-confirm"])) {
647                         $data["confirm"] = $json["dfrn-confirm"];
648                 }
649
650                 if (isset($json["dfrn-notify"])) {
651                         $data["notify"] = $json["dfrn-notify"];
652                 }
653
654                 if (isset($json["dfrn-poll"])) {
655                         $data["poll"] = $json["dfrn-poll"];
656                 }
657
658                 return $data;
659         }
660
661         /**
662          * @brief Check for valid DFRN data
663          *
664          * @param array $data DFRN data
665          *
666          * @return int Number of errors
667          */
668         public static function validDfrn($data) {
669                 $errors = 0;
670                 if (!isset($data['key'])) {
671                         $errors ++;
672                 }
673                 if (!isset($data['dfrn-request'])) {
674                         $errors ++;
675                 }
676                 if (!isset($data['dfrn-confirm'])) {
677                         $errors ++;
678                 }
679                 if (!isset($data['dfrn-notify'])) {
680                         $errors ++;
681                 }
682                 if (!isset($data['dfrn-poll'])) {
683                         $errors ++;
684                 }
685                 return $errors;
686         }
687
688         /**
689          * @brief Fetch data from a DFRN profile page and via "noscrape"
690          *
691          * @param string $profile_link Link to the profile page
692          *
693          * @return array profile data
694          */
695         public static function profile($profile_link) {
696
697                 $data = array();
698
699                 logger("Check profile ".$profile_link, LOGGER_DEBUG);
700
701                 // Fetch data via noscrape - this is faster
702                 $noscrape_url = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile_link);
703                 $data = self::pollNoscrape($noscrape_url, $data);
704
705                 if (!isset($data["notify"])
706                         OR !isset($data["confirm"])
707                         OR !isset($data["request"])
708                         OR !isset($data["poll"])
709                         OR !isset($data["poco"])
710                         OR !isset($data["name"])
711                         OR !isset($data["photo"])
712                 ) {
713                         $data = self::pollHcard($profile_link, $data, true);
714                 }
715
716                 $prof_data = array();
717                 $prof_data["addr"]         = $data["addr"];
718                 $prof_data["nick"]         = $data["nick"];
719                 $prof_data["dfrn-request"] = $data["request"];
720                 $prof_data["dfrn-confirm"] = $data["confirm"];
721                 $prof_data["dfrn-notify"]  = $data["notify"];
722                 $prof_data["dfrn-poll"]    = $data["poll"];
723                 $prof_data["dfrn-poco"]    = $data["poco"];
724                 $prof_data["photo"]        = $data["photo"];
725                 $prof_data["fn"]           = $data["name"];
726                 $prof_data["key"]          = $data["pubkey"];
727
728                 logger("Result for profile ".$profile_link.": ".print_r($prof_data, true), LOGGER_DEBUG);
729
730                 return $prof_data;
731         }
732
733         /**
734          * @brief Check for DFRN contact
735          *
736          * @param array $webfinger Webfinger data
737          *
738          * @return array DFRN data
739          */
740         private function dfrn($webfinger) {
741
742                 $hcard_url = "";
743                 $data = array();
744                 foreach ($webfinger["links"] as $link) {
745                         if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != "")) {
746                                 $data["network"] = NETWORK_DFRN;
747                         } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
748                                 $data["poll"] = $link["href"];
749                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND ($link["type"] == "text/html") AND ($link["href"] != "")) {
750                                 $data["url"] = $link["href"];
751                         } elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != "")) {
752                                 $hcard_url = $link["href"];
753                         } elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != "")) {
754                                 $data["poco"] = $link["href"];
755                         } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != "")) {
756                                 $data["photo"] = $link["href"];
757                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != "")) {
758                                 $data["baseurl"] = trim($link["href"], '/');
759                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != "")) {
760                                 $data["guid"] = $link["href"];
761                         } elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
762                                 $data["pubkey"] = base64_decode($link["href"]);
763
764                                 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
765                                 if (strstr($data["pubkey"], 'RSA ')) {
766                                         $data["pubkey"] = rsatopem($data["pubkey"]);
767                                 }
768                         }
769                 }
770
771                 if (!isset($data["network"]) OR ($hcard_url == "")) {
772                         return false;
773                 }
774
775                 // Fetch data via noscrape - this is faster
776                 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
777                 $data = self::pollNoscrape($noscrape_url, $data);
778
779                 if (isset($data["notify"])
780                         AND isset($data["confirm"])
781                         AND isset($data["request"])
782                         AND isset($data["poll"])
783                         AND isset($data["name"])
784                         AND isset($data["photo"])
785                 ) {
786                         return $data;
787                 }
788
789                 $data = self::pollHcard($hcard_url, $data, true);
790
791                 return $data;
792         }
793
794         /**
795          * @brief Poll the hcard page (Diaspora and Friendica specific)
796          *
797          * @param string $hcard_url Link to the hcard page
798          * @param array $data The already fetched data
799          * @param boolean $dfrn Poll DFRN specific data
800          *
801          * @return array hcard data
802          */
803         private function pollHcard($hcard_url, $data, $dfrn = false) {
804                 $ret = z_fetch_url($hcard_url);
805                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
806                         return false;
807                 }
808                 $content = $ret['body'];
809                 if (!$content) {
810                         return false;
811                 }
812
813                 $doc = new \DOMDocument();
814                 if (!@$doc->loadHTML($content)) {
815                         return false;
816                 }
817
818                 $xpath = new \DomXPath($doc);
819
820                 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
821                 if (!is_object($vcards)) {
822                         return false;
823                 }
824
825                 if ($vcards->length > 0) {
826                         $vcard = $vcards->item(0);
827
828                         // We have to discard the guid from the hcard in favour of the guid from lrdd
829                         // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
830                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
831                         if (($search->length > 0) AND ($data["guid"] == "")) {
832                                 $data["guid"] = $search->item(0)->nodeValue;
833                         }
834
835                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
836                         if ($search->length > 0) {
837                                 $data["nick"] = $search->item(0)->nodeValue;
838                         }
839
840                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
841                         if ($search->length > 0) {
842                                 $data["name"] = $search->item(0)->nodeValue;
843                         }
844
845                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
846                         if ($search->length > 0) {
847                                 $data["searchable"] = $search->item(0)->nodeValue;
848                         }
849
850                         $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
851                         if ($search->length > 0) {
852                                 $data["pubkey"] = $search->item(0)->nodeValue;
853                                 if (strstr($data["pubkey"], 'RSA ')) {
854                                         $data["pubkey"] = rsatopem($data["pubkey"]);
855                                 }
856                         }
857
858                         $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
859                         if ($search->length > 0) {
860                                 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
861                         }
862                 }
863
864                 $avatar = array();
865                 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
866                 foreach ($photos as $photo) {
867                         $attr = array();
868                         foreach ($photo->attributes as $attribute) {
869                                 $attr[$attribute->name] = trim($attribute->value);
870                         }
871
872                         if (isset($attr["src"]) AND isset($attr["width"])) {
873                                 $avatar[$attr["width"]] = $attr["src"];
874                         }
875
876                         // We don't have a width. So we just take everything that we got.
877                         // This is a Hubzilla workaround which doesn't send a width.
878                         if ((sizeof($avatar) == 0) AND isset($attr["src"])) {
879                                 $avatar[] = $attr["src"];
880                         }
881                 }
882
883                 if (sizeof($avatar)) {
884                         ksort($avatar);
885                         $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
886                 }
887
888                 if ($dfrn) {
889                         // Poll DFRN specific data
890                         $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
891                         if ($search->length > 0) {
892                                 foreach ($search as $link) {
893                                         //$data["request"] = $search->item(0)->nodeValue;
894                                         $attr = array();
895                                         foreach ($link->attributes as $attribute) {
896                                                 $attr[$attribute->name] = trim($attribute->value);
897                                         }
898
899                                         $data[substr($attr["rel"], 5)] = $attr["href"];
900                                 }
901                         }
902
903                         // Older Friendica versions had used the "uid" field differently than newer versions
904                         if ($data["nick"] == $data["guid"]) {
905                                 unset($data["guid"]);
906                         }
907                 }
908
909
910                 return $data;
911         }
912
913         /**
914          * @brief Check for Diaspora contact
915          *
916          * @param array $webfinger Webfinger data
917          *
918          * @return array Diaspora data
919          */
920         private function diaspora($webfinger) {
921
922                 $hcard_url = "";
923                 $data = array();
924                 foreach ($webfinger["links"] as $link) {
925                         if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != "")) {
926                                 $hcard_url = $link["href"];
927                         } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != "")) {
928                                 $data["baseurl"] = trim($link["href"], '/');
929                         } elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != "")) {
930                                 $data["guid"] = $link["href"];
931                         } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND ($link["type"] == "text/html") AND ($link["href"] != "")) {
932                                 $data["url"] = $link["href"];
933                         } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
934                                 $data["poll"] = $link["href"];
935                         } elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != "")) {
936                                 $data["poco"] = $link["href"];
937                         } elseif (($link["rel"] == "salmon") AND ($link["href"] != "")) {
938                                 $data["notify"] = $link["href"];
939                         } elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
940                                 $data["pubkey"] = base64_decode($link["href"]);
941
942                                 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
943                                 if (strstr($data["pubkey"], 'RSA ')) {
944                                         $data["pubkey"] = rsatopem($data["pubkey"]);
945                                 }
946                         }
947                 }
948
949                 if (!isset($data["url"]) OR ($hcard_url == "")) {
950                         return false;
951                 }
952
953                 if (is_array($webfinger["aliases"])) {
954                         foreach ($webfinger["aliases"] as $alias) {
955                                 if (normalise_link($alias) != normalise_link($data["url"]) AND ! strstr($alias, "@")) {
956                                         $data["alias"] = $alias;
957                                 }
958                         }
959                 }
960
961                 // Fetch further information from the hcard
962                 $data = self::pollHcard($hcard_url, $data);
963
964                 if (!$data) {
965                         return false;
966                 }
967
968                 if (isset($data["url"])
969                         AND isset($data["guid"])
970                         AND isset($data["baseurl"])
971                         AND isset($data["pubkey"])
972                         AND ($hcard_url != "")
973                 ) {
974                         $data["network"] = NETWORK_DIASPORA;
975
976                         // The Diaspora handle must always be lowercase
977                         $data["addr"] = strtolower($data["addr"]);
978
979                         // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
980                         $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
981                         $data["batch"]  = $data["baseurl"] . "/receive/public";
982                 } else {
983                         return false;
984                 }
985
986                 return $data;
987         }
988
989         /**
990          * @brief Check for OStatus contact
991          *
992          * @param array $webfinger Webfinger data
993          *
994          * @return array OStatus data
995          */
996         private function ostatus($webfinger) {
997                 $data = array();
998                 if (is_array($webfinger["aliases"])) {
999                         foreach ($webfinger["aliases"] as $alias) {
1000                                 if (strstr($alias, "@")) {
1001                                         $data["addr"] = str_replace('acct:', '', $alias);
1002                                 }
1003                         }
1004                 }
1005
1006                 if (is_string($webfinger["subject"]) AND strstr($webfinger["subject"], "@")) {
1007                         $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1008                 }
1009                 $pubkey = "";
1010                 foreach ($webfinger["links"] as $link) {
1011                         if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1012                                 AND ($link["type"] == "text/html")
1013                                 AND ($link["href"] != "")
1014                         ) {
1015                                 $data["url"] = $link["href"];
1016                         } elseif (($link["rel"] == "salmon") AND ($link["href"] != "")) {
1017                                 $data["notify"] = $link["href"];
1018                         } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
1019                                 $data["poll"] = $link["href"];
1020                         } elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
1021                                 $pubkey = $link["href"];
1022
1023                                 if (substr($pubkey, 0, 5) === 'data:') {
1024                                         if (strstr($pubkey, ',')) {
1025                                                 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1026                                         } else {
1027                                                 $pubkey = substr($pubkey, 5);
1028                                         }
1029                                 } elseif (normalise_link($pubkey) == 'http://') {
1030                                         $ret = z_fetch_url($pubkey);
1031                                         if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1032                                                 return false;
1033                                         }
1034                                         $pubkey = $ret['body'];
1035                                 }
1036
1037                                 $key = explode(".", $pubkey);
1038
1039                                 if (sizeof($key) >= 3) {
1040                                         $m = base64url_decode($key[1]);
1041                                         $e = base64url_decode($key[2]);
1042                                         $data["pubkey"] = metopem($m, $e);
1043                                 }
1044                         }
1045                 }
1046
1047                 if (isset($data["notify"]) AND isset($data["pubkey"])
1048                         AND isset($data["poll"])
1049                         AND isset($data["url"])
1050                 ) {
1051                         $data["network"] = NETWORK_OSTATUS;
1052                 } else {
1053                         return false;
1054                 }
1055                 // Fetch all additional data from the feed
1056                 $ret = z_fetch_url($data["poll"]);
1057                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1058                         return false;
1059                 }
1060                 $feed = $ret['body'];
1061                 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1062                 if (!$feed_data) {
1063                         return false;
1064                 }
1065                 if ($feed_data["header"]["author-name"] != "") {
1066                         $data["name"] = $feed_data["header"]["author-name"];
1067                 }
1068                 if ($feed_data["header"]["author-nick"] != "") {
1069                         $data["nick"] = $feed_data["header"]["author-nick"];
1070                 }
1071                 if ($feed_data["header"]["author-avatar"] != "") {
1072                         $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1073                 }
1074                 if ($feed_data["header"]["author-id"] != "") {
1075                         $data["alias"] = $feed_data["header"]["author-id"];
1076                 }
1077                 if ($feed_data["header"]["author-location"] != "") {
1078                         $data["location"] = $feed_data["header"]["author-location"];
1079                 }
1080                 if ($feed_data["header"]["author-about"] != "") {
1081                         $data["about"] = $feed_data["header"]["author-about"];
1082                 }
1083                 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1084                 // So we take the value that we just fetched, although the other one worked as well
1085                 if ($feed_data["header"]["author-link"] != "") {
1086                         $data["url"] = $feed_data["header"]["author-link"];
1087                 }
1088                 /// @todo Fetch location and "about" from the feed as well
1089                 return $data;
1090         }
1091
1092         /**
1093          * @brief Fetch data from a pump.io profile page
1094          *
1095          * @param string $profile_link Link to the profile page
1096          *
1097          * @return array profile data
1098          */
1099         private function pumpioProfileData($profile_link) {
1100
1101                 $doc = new \DOMDocument();
1102                 if (!@$doc->loadHTMLFile($profile_link)) {
1103                         return false;
1104                 }
1105
1106                 $xpath = new \DomXPath($doc);
1107
1108                 $data = array();
1109
1110                 // This is ugly - but pump.io doesn't seem to know a better way for it
1111                 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1112                 $pos = strpos($data["name"], chr(10));
1113                 if ($pos) {
1114                         $data["name"] = trim(substr($data["name"], 0, $pos));
1115                 }
1116
1117                 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1118                 if ($avatar) {
1119                         foreach ($avatar->attributes as $attribute) {
1120                                 if ($attribute->name == "src") {
1121                                         $data["photo"] = trim($attribute->value);
1122                                 }
1123                         }
1124                 }
1125
1126                 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
1127                 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
1128
1129                 return $data;
1130         }
1131
1132         /**
1133          * @brief Check for pump.io contact
1134          *
1135          * @param array $webfinger Webfinger data
1136          *
1137          * @return array pump.io data
1138          */
1139         private function pumpio($webfinger) {
1140
1141                 $data = array();
1142                 foreach ($webfinger["links"] as $link) {
1143                         if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1144                                 AND ($link["type"] == "text/html")
1145                                 AND ($link["href"] != "")
1146                         ) {
1147                                 $data["url"] = $link["href"];
1148                         } elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != "")) {
1149                                 $data["notify"] = $link["href"];
1150                         } elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != "")) {
1151                                 $data["poll"] = $link["href"];
1152                         } elseif (($link["rel"] == "dialback") AND ($link["href"] != "")) {
1153                                 $data["dialback"] = $link["href"];
1154                         }
1155                 }
1156                 if (isset($data["poll"]) AND isset($data["notify"])
1157                         AND isset($data["dialback"])
1158                         AND isset($data["url"])
1159                 ) {
1160                         // by now we use these fields only for the network type detection
1161                         // So we unset all data that isn't used at the moment
1162                         unset($data["dialback"]);
1163
1164                         $data["network"] = NETWORK_PUMPIO;
1165                 } else {
1166                         return false;
1167                 }
1168
1169                 $profile_data = self::pumpioProfileData($data["url"]);
1170
1171                 if (!$profile_data) {
1172                         return false;
1173                 }
1174
1175                 $data = array_merge($data, $profile_data);
1176
1177                 return $data;
1178         }
1179
1180         /**
1181          * @brief Check page for feed link
1182          *
1183          * @param string $url Page link
1184          *
1185          * @return string feed link
1186          */
1187         private function getFeedLink($url) {
1188                 $doc = new \DOMDocument();
1189
1190                 if (!@$doc->loadHTMLFile($url)) {
1191                         return false;
1192                 }
1193
1194                 $xpath = new \DomXPath($doc);
1195
1196                 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1197                 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1198                 if (!is_object($feeds)) {
1199                         return false;
1200                 }
1201
1202                 if ($feeds->length == 0) {
1203                         return false;
1204                 }
1205
1206                 $feed_url = "";
1207
1208                 foreach ($feeds as $feed) {
1209                         $attr = array();
1210                         foreach ($feed->attributes as $attribute) {
1211                                 $attr[$attribute->name] = trim($attribute->value);
1212                         }
1213
1214                         if ($feed_url == "") {
1215                                 $feed_url = $attr["href"];
1216                         }
1217                 }
1218
1219                 return $feed_url;
1220         }
1221
1222         /**
1223          * @brief Check for feed contact
1224          *
1225          * @param string $url Profile link
1226          * @param boolean $probe Do a probe if the page contains a feed link
1227          *
1228          * @return array feed data
1229          */
1230         private function feed($url, $probe = true) {
1231                 $ret = z_fetch_url($url);
1232                 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1233                         return false;
1234                 }
1235                 $feed = $ret['body'];
1236                 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1237
1238                 if (!$feed_data) {
1239                         if (!$probe) {
1240                                 return false;
1241                         }
1242
1243                         $feed_url = self::getFeedLink($url);
1244
1245                         if (!$feed_url) {
1246                                 return false;
1247                         }
1248
1249                         return self::feed($feed_url, false);
1250                 }
1251
1252                 if ($feed_data["header"]["author-name"] != "") {
1253                         $data["name"] = $feed_data["header"]["author-name"];
1254                 }
1255
1256                 if ($feed_data["header"]["author-nick"] != "") {
1257                         $data["nick"] = $feed_data["header"]["author-nick"];
1258                 }
1259
1260                 if ($feed_data["header"]["author-avatar"] != "") {
1261                         $data["photo"] = $feed_data["header"]["author-avatar"];
1262                 }
1263
1264                 if ($feed_data["header"]["author-id"] != "") {
1265                         $data["alias"] = $feed_data["header"]["author-id"];
1266                 }
1267
1268                 $data["url"] = $url;
1269                 $data["poll"] = $url;
1270
1271                 if ($feed_data["header"]["author-link"] != "") {
1272                         $data["baseurl"] = $feed_data["header"]["author-link"];
1273                 } else {
1274                         $data["baseurl"] = $data["url"];
1275                 }
1276
1277                 $data["network"] = NETWORK_FEED;
1278
1279                 return $data;
1280         }
1281
1282         /**
1283          * @brief Check for mail contact
1284          *
1285          * @param string $uri Profile link
1286          * @param integer $uid User ID
1287          *
1288          * @return array mail data
1289          */
1290         private function mail($uri, $uid) {
1291
1292                 if (!validate_email($uri)) {
1293                         return false;
1294                 }
1295
1296                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
1297
1298                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
1299
1300                 if (\dbm::is_result($x) && \dbm::is_result($r)) {
1301                         $mailbox = construct_mailbox_name($r[0]);
1302                         $password = '';
1303                         openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']);
1304                         $mbox = email_connect($mailbox, $r[0]['user'], $password);
1305                         if (!mbox) {
1306                                 return false;
1307                         }
1308                 }
1309
1310                 $msgs = email_poll($mbox, $uri);
1311                 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
1312
1313                 if (!count($msgs)) {
1314                         return false;
1315                 }
1316
1317                 $phost = substr($uri, strpos($uri, '@') + 1);
1318
1319                 $data = array();
1320                 $data["addr"]    = $uri;
1321                 $data["network"] = NETWORK_MAIL;
1322                 $data["name"]    = substr($uri, 0, strpos($uri, '@'));
1323                 $data["nick"]    = $data["name"];
1324                 $data["photo"]   = avatar_img($uri);
1325                 $data["url"]     = 'http://'.$phost."/".$data["nick"];
1326                 $data["notify"]  = 'smtp '.random_string();
1327                 $data["poll"]    = 'email '.random_string();
1328
1329                 $x = email_msg_meta($mbox, $msgs[0]);
1330                 if (stristr($x[0]->from, $uri)) {
1331                         $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1332                 } elseif (stristr($x[0]->to, $uri)) {
1333                         $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1334                 }
1335                 if (isset($adr)) {
1336                         foreach ($adr as $feadr) {
1337                                 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1338                                         &&(strcasecmp($feadr->host, $phost) == 0)
1339                                         && (strlen($feadr->personal))
1340                                 ) {
1341                                         $personal = imap_mime_header_decode($feadr->personal);
1342                                         $data["name"] = "";
1343                                         foreach ($personal as $perspart) {
1344                                                 if ($perspart->charset != "default") {
1345                                                         $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1346                                                 } else {
1347                                                         $data["name"] .= $perspart->text;
1348                                                 }
1349                                         }
1350
1351                                         $data["name"] = notags($data["name"]);
1352                                 }
1353                         }
1354                 }
1355                 imap_close($mbox);
1356
1357                 return $data;
1358         }
1359
1360         /**
1361          * @brief Mix two paths together to possibly fix missing parts
1362          *
1363          * @param string $avatar Path to the avatar
1364          * @param string $base Another path that is hopefully complete
1365          *
1366          * @return string fixed avatar path
1367          */
1368         public static function fixAvatar($avatar, $base) {
1369                 $base_parts = parse_url($base);
1370
1371                 // Remove all parts that could create a problem
1372                 unset($base_parts['path']);
1373                 unset($base_parts['query']);
1374                 unset($base_parts['fragment']);
1375
1376                 $avatar_parts = parse_url($avatar);
1377
1378                 // Now we mix them
1379                 $parts = array_merge($base_parts, $avatar_parts);
1380
1381                 // And put them together again
1382                 $scheme   = isset($parts['scheme'])   ? $parts['scheme'] . '://' : '';
1383                 $host     = isset($parts['host'])     ? $parts['host']           : '';
1384                 $port     = isset($parts['port'])     ? ':' . $parts['port']     : '';
1385                 $path     = isset($parts['path'])     ? $parts['path']           : '';
1386                 $query    = isset($parts['query'])    ? '?' . $parts['query']    : '';
1387                 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1388
1389                 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1390
1391                 logger('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, LOGGER_DATA);
1392
1393                 return $fixed;
1394         }
1395 }