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