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