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