3 * @file include/Probe.php
4 * @brief Functions for probing URL
8 use \Friendica\Core\Config;
9 use \Friendica\Core\PConfig;
11 require_once("include/feed.php");
12 require_once('include/email.php');
13 require_once('include/network.php');
16 * @brief This class contain functions for probing URL
22 * @brief Rearrange the array so that it always has the same order
24 * @param array $data Unordered data
26 * @return array Ordered data
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");
35 foreach ($fields AS $field)
36 if (isset($data[$field]))
37 $newdata[$field] = $data[$field];
39 $newdata[$field] = "";
41 // We don't use the "priority" field anymore and replace it with a dummy.
42 $newdata["priority"] = 0;
48 * @brief Probes for XRD data
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
55 private function xrd($host) {
57 $ssl_url = "https://".$host."/.well-known/host-meta";
58 $url = "http://".$host."/.well-known/host-meta";
60 $xrd_timeout = Config::get('system','xrd_timeout', 20);
63 $xml = fetch_url($ssl_url, false, $redirects, $xrd_timeout, "application/xrd+xml");
64 $xrd = parse_xml_string($xml, false);
66 if (!is_object($xrd)) {
67 $xml = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
68 $xrd = parse_xml_string($xml, false);
73 $links = xml::element_to_array($xrd);
74 if (!isset($links["xrd"]["link"]))
79 foreach ($links["xrd"]["link"] AS $value => $link) {
80 if (isset($link["@attributes"]))
81 $attributes = $link["@attributes"];
82 elseif ($value == "@attributes")
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"];
100 * @brief Perform Webfinger lookup and return DFRN data
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.
111 * amended 7/9/2011 to return an hcard which could save potentially loading
112 * a lengthy content page to scrape dfrn attributes
114 * @param string $webbie Address that should be probed
115 * @param string $hcard Link to the hcard - is returned by reference
117 * @return string profile link
120 public static function webfinger_dfrn($webbie, &$hcard) {
121 if (!strstr($webbie, '@'))
126 $links = self::webfinger($webbie);
127 logger('webfinger_dfrn: '.$webbie.':'.print_r($links,true), LOGGER_DATA);
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'];
138 return $profile_link;
142 * @brief Check an URI for LRDD data
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.
148 * @param string $uri Address that should be probed
150 * @return array uri data
152 public static function lrdd($uri) {
154 $lrdd = self::xrd($uri);
157 $parts = @parse_url($uri);
161 $host = $parts["host"];
163 $path_parts = explode("/", trim($parts["path"], "/"));
166 $lrdd = self::xrd($host);
167 $host .= "/".array_shift($path_parts);
168 } while (!$lrdd AND (sizeof($path_parts) > 0));
174 foreach ($lrdd AS $key => $link) {
178 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
181 $path = str_replace('{uri}', urlencode($uri), $link);
182 $webfinger = self::webfinger($path);
185 if (!is_array($webfinger["links"]))
190 foreach ($webfinger["links"] AS $link)
191 $data[] = array("@attributes" => $link);
193 if (is_array($webfinger["aliases"]))
194 foreach ($webfinger["aliases"] AS $alias)
195 $data[] = array("@attributes" =>
196 array("rel" => "alias",
203 * @brief Fetch information (protocol endpoints and user information) about a given uri
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?
210 * @return array uri data
212 public static function uri($uri, $network = "", $uid = 0, $cache = true) {
215 $result = Cache::get("probe_url:".$network.":".$uri);
216 if (!is_null($result)) {
217 $result = unserialize($result);
225 $data = self::detect($uri, $network, $uid);
227 if (!isset($data["url"]))
230 if ($data["photo"] != "")
231 $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
233 $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
235 if (!isset($data["name"]) OR ($data["name"] == "")) {
236 if (isset($data["nick"]))
237 $data["name"] = $data["nick"];
239 if ($data["name"] == "")
240 $data["name"] = $data["url"];
243 if (!isset($data["nick"]) OR ($data["nick"] == "")) {
244 $data["nick"] = strtolower($data["name"]);
246 if (strpos($data['nick'], ' '))
247 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
250 if (!isset($data["network"]))
251 $data["network"] = NETWORK_PHANTOM;
253 $data = self::rearrange_data($data);
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);
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"]),
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']))
282 * @brief Fetch information (protocol endpoints and user information) about a given uri
284 * This function is only called by the "uri" function that adds caching and rearranging of data.
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)
290 * @return array uri data
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);
300 if ($network == NETWORK_MAIL)
301 return self::mail($uri, $uid);
303 // Remove "acct:" from the URI
304 $uri = str_replace('acct:', '', $uri);
306 $host = substr($uri,strpos($uri, '@') + 1);
307 $nick = substr($uri,0, strpos($uri, '@'));
309 if (strpos($uri, '@twitter.com'))
310 return array("network" => NETWORK_TWITTER);
312 $lrdd = self::xrd($host);
314 return self::mail($uri, $uid);
318 $parts = parse_url($uri);
319 if (!isset($parts["scheme"]) OR
320 !isset($parts["host"]) OR
321 !isset($parts["path"]))
325 $host = $parts["host"];
327 if ($host == 'twitter.com')
328 return array("network" => NETWORK_TWITTER);
330 $lrdd = self::xrd($host);
332 $path_parts = explode("/", trim($parts["path"], "/"));
334 while (!$lrdd AND (sizeof($path_parts) > 1)) {
335 $host .= "/".array_shift($path_parts);
336 $lrdd = self::xrd($host);
339 return self::feed($uri);
341 $nick = array_pop($path_parts);
342 $addr = $nick."@".$host;
346 /// @todo Do we need the prefix "acct:" or "acct://"?
348 foreach ($lrdd AS $key => $link) {
352 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
355 // Try webfinger with the address (user@domain.tld)
356 $path = str_replace('{uri}', urlencode($addr), $link);
357 $webfinger = self::webfinger($path);
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);
364 // Since the detection with the address wasn't successful, we delete it.
373 return self::feed($uri);
377 logger("Probing ".$uri, LOGGER_DEBUG);
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);
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;
395 if ((!isset($result["addr"]) OR ($result["addr"] == "")) AND ($addr != ""))
396 $result["addr"] = $addr;
399 logger($uri." is ".$result["network"], LOGGER_DEBUG);
401 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
402 $pos = strpos($result["url"], $host);
404 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
411 * @brief Perform a webfinger request.
413 * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
415 * @param string $url Address that should be probed
417 * @return array webfinger data
419 private function webfinger($url) {
421 $xrd_timeout = Config::get('system','xrd_timeout', 20);
424 $data = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
425 $xrd = parse_xml_string($data, false);
427 if (!is_object($xrd)) {
428 // If it is not XML, maybe it is JSON
429 $webfinger = json_decode($data, true);
431 if (!isset($webfinger["links"]))
437 $xrd_arr = xml::element_to_array($xrd);
438 if (!isset($xrd_arr["xrd"]["link"]))
441 $webfinger = array();
443 if (isset($xrd_arr["xrd"]["subject"]))
444 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
446 if (isset($xrd_arr["xrd"]["alias"]))
447 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
449 $webfinger["links"] = array();
451 foreach ($xrd_arr["xrd"]["link"] AS $value => $data) {
452 if (isset($data["@attributes"]))
453 $attributes = $data["@attributes"];
454 elseif ($value == "@attributes")
459 $webfinger["links"][] = $attributes;
465 * @brief Poll the Friendica specific noscrape page.
467 * "noscrape" is a faster alternative to fetch the data from the hcard.
468 * This functionality was originally created for the directory.
470 * @param string $noscrape Link to the noscrape page
471 * @param array $data The already fetched data
473 * @return array noscrape data
475 private function poll_noscrape($noscrape, $data) {
476 $content = fetch_url($noscrape);
480 $json = json_decode($content, true);
481 if (!is_array($json))
484 if (isset($json["fn"]))
485 $data["name"] = $json["fn"];
487 if (isset($json["addr"]))
488 $data["addr"] = $json["addr"];
490 if (isset($json["nick"]))
491 $data["nick"] = $json["nick"];
493 if (isset($json["comm"]))
494 $data["community"] = $json["comm"];
496 if (isset($json["tags"])) {
497 $keywords = implode(" ", $json["tags"]);
499 $data["keywords"] = $keywords;
502 $location = formatted_location($json);
504 $data["location"] = $location;
506 if (isset($json["about"]))
507 $data["about"] = $json["about"];
509 if (isset($json["key"]))
510 $data["pubkey"] = $json["key"];
512 if (isset($json["photo"]))
513 $data["photo"] = $json["photo"];
515 if (isset($json["dfrn-request"]))
516 $data["request"] = $json["dfrn-request"];
518 if (isset($json["dfrn-confirm"]))
519 $data["confirm"] = $json["dfrn-confirm"];
521 if (isset($json["dfrn-notify"]))
522 $data["notify"] = $json["dfrn-notify"];
524 if (isset($json["dfrn-poll"]))
525 $data["poll"] = $json["dfrn-poll"];
531 * @brief Check for valid DFRN data
533 * @param array $data DFRN data
535 * @return int Number of errors
537 public static function valid_dfrn($data) {
539 if(!isset($data['key']))
541 if(!isset($data['dfrn-request']))
543 if(!isset($data['dfrn-confirm']))
545 if(!isset($data['dfrn-notify']))
547 if(!isset($data['dfrn-poll']))
553 * @brief Fetch data from a DFRN profile page and via "noscrape"
555 * @param string $profile Link to the profile page
557 * @return array profile data
559 public static function profile($profile) {
563 // Fetch data via noscrape - this is faster
564 $noscrape = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile);
565 $data = self::poll_noscrape($noscrape, $data);
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);
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"];
589 * @brief Check for DFRN contact
591 * @param array $webfinger Webfinger data
593 * @return array DFRN data
595 private function dfrn($webfinger) {
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"];
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"]);
621 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
622 if (strstr($data["pubkey"], 'RSA '))
623 $data["pubkey"] = rsatopem($data["pubkey"]);
627 if (!isset($data["network"]) OR ($hcard == ""))
630 // Fetch data via noscrape - this is faster
631 $noscrape = str_replace("/hcard/", "/noscrape/", $hcard);
632 $data = self::poll_noscrape($noscrape, $data);
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"]))
638 $data = self::poll_hcard($hcard, $data, true);
644 * @brief Poll the hcard page (Diaspora and Friendica specific)
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
650 * @return array hcard data
652 private function poll_hcard($hcard, $data, $dfrn = false) {
654 $doc = new DOMDocument();
655 if (!@$doc->loadHTMLFile($hcard))
658 $xpath = new DomXPath($doc);
660 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
661 if (!is_object($vcards))
664 if ($vcards->length == 0)
667 $vcard = $vcards->item(0);
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;
675 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
676 if ($search->length > 0)
677 $data["nick"] = $search->item(0)->nodeValue;
679 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
680 if ($search->length > 0)
681 $data["name"] = $search->item(0)->nodeValue;
683 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
684 if ($search->length > 0)
685 $data["searchable"] = $search->item(0)->nodeValue;
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"]);
694 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
695 if ($search->length > 0)
696 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
699 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
700 foreach ($photos AS $photo) {
702 foreach ($photo->attributes as $attribute)
703 $attr[$attribute->name] = trim($attribute->value);
705 if (isset($attr["src"]) AND isset($attr["width"]))
706 $avatar[$attr["width"]] = $attr["src"];
709 if (sizeof($avatar)) {
711 $data["photo"] = array_pop($avatar);
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;
721 foreach ($link->attributes as $attribute)
722 $attr[$attribute->name] = trim($attribute->value);
724 $data[substr($attr["rel"], 5)] = $attr["href"];
728 // Older Friendica versions had used the "uid" field differently than newer versions
729 if ($data["nick"] == $data["guid"])
730 unset($data["guid"]);
738 * @brief Check for Diaspora contact
740 * @param array $webfinger Webfinger data
742 * @return array Diaspora data
744 private function diaspora($webfinger) {
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"]);
767 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
768 if (strstr($data["pubkey"], 'RSA '))
769 $data["pubkey"] = rsatopem($data["pubkey"]);
773 if (!isset($data["url"]) OR ($hcard == ""))
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;
781 // Fetch further information from the hcard
782 $data = self::poll_hcard($hcard, $data);
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;
791 // The Diaspora handle must always be lowercase
792 $data["addr"] = strtolower($data["addr"]);
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";
804 * @brief Check for OStatus contact
806 * @param array $webfinger Webfinger data
808 * @return array OStatus data
810 private function ostatus($webfinger) {
813 if (is_array($webfinger["aliases"]))
814 foreach($webfinger["aliases"] AS $alias)
815 if (strstr($alias, "@"))
816 $data["addr"] = str_replace('acct:', '', $alias);
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"];
830 if (substr($pubkey, 0, 5) === 'data:') {
831 if (strstr($pubkey, ','))
832 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
834 $pubkey = substr($pubkey, 5);
836 $pubkey = fetch_url($pubkey);
838 $key = explode(".", $pubkey);
840 if (sizeof($key) >= 3) {
841 $m = base64url_decode($key[1]);
842 $e = base64url_decode($key[2]);
843 $data["pubkey"] = metopem($m,$e);
849 if (isset($data["notify"]) AND isset($data["pubkey"]) AND
850 isset($data["poll"]) AND isset($data["url"])) {
851 $data["network"] = NETWORK_OSTATUS;
855 // Fetch all additional data from the feed
856 $feed = fetch_url($data["poll"]);
857 $feed_data = feed_import($feed,$dummy1,$dummy2, $dummy3, true);
861 if ($feed_data["header"]["author-name"] != "")
862 $data["name"] = $feed_data["header"]["author-name"];
864 if ($feed_data["header"]["author-nick"] != "")
865 $data["nick"] = $feed_data["header"]["author-nick"];
867 if ($feed_data["header"]["author-avatar"] != "")
868 $data["photo"] = $feed_data["header"]["author-avatar"];
870 if ($feed_data["header"]["author-id"] != "")
871 $data["alias"] = $feed_data["header"]["author-id"];
873 if ($feed_data["header"]["author-location"] != "")
874 $data["location"] = $feed_data["header"]["author-location"];
876 if ($feed_data["header"]["author-about"] != "")
877 $data["about"] = $feed_data["header"]["author-about"];
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"];
884 /// @todo Fetch location and "about" from the feed as well
889 * @brief Fetch data from a pump.io profile page
891 * @param string $profile Link to the profile page
893 * @return array profile data
895 private function pumpio_profile_data($profile) {
897 $doc = new DOMDocument();
898 if (!@$doc->loadHTMLFile($profile))
901 $xpath = new DomXPath($doc);
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));
909 $data["name"] = trim(substr($data["name"], 0, $pos));
911 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
913 foreach ($avatar->attributes as $attribute)
914 if ($attribute->name == "src")
915 $data["photo"] = trim($attribute->value);
917 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
918 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
924 * @brief Check for pump.io contact
926 * @param array $webfinger Webfinger data
928 * @return array pump.io data
930 private function pumpio($webfinger) {
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"];
944 if (isset($data["poll"]) AND isset($data["notify"]) AND
945 isset($data["dialback"]) AND isset($data["url"])) {
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"]);
951 $data["network"] = NETWORK_PUMPIO;
955 $profile_data = self::pumpio_profile_data($data["url"]);
960 $data = array_merge($data, $profile_data);
966 * @brief Check page for feed link
968 * @param string $url Page link
970 * @return string feed link
972 private function get_feed_link($url) {
973 $doc = new DOMDocument();
975 if (!@$doc->loadHTMLFile($url))
978 $xpath = new DomXPath($doc);
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))
985 if ($feeds->length == 0)
990 foreach ($feeds AS $feed) {
992 foreach ($feed->attributes as $attribute)
993 $attr[$attribute->name] = trim($attribute->value);
996 $feed_url = $attr["href"];
1003 * @brief Check for feed contact
1005 * @param string $url Profile link
1006 * @param boolean $probe Do a probe if the page contains a feed link
1008 * @return array feed data
1010 private function feed($url, $probe = true) {
1011 $feed = fetch_url($url);
1012 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1018 $feed_url = self::get_feed_link($url);
1023 return self::feed($feed_url, false);
1026 if ($feed_data["header"]["author-name"] != "")
1027 $data["name"] = $feed_data["header"]["author-name"];
1029 if ($feed_data["header"]["author-nick"] != "")
1030 $data["nick"] = $feed_data["header"]["author-nick"];
1032 if ($feed_data["header"]["author-avatar"] != "")
1033 $data["photo"] = $feed_data["header"]["author-avatar"];
1035 if ($feed_data["header"]["author-id"] != "")
1036 $data["alias"] = $feed_data["header"]["author-id"];
1038 $data["url"] = $url;
1039 $data["poll"] = $url;
1041 if ($feed_data["header"]["author-link"] != "")
1042 $data["baseurl"] = $feed_data["header"]["author-link"];
1044 $data["baseurl"] = $data["url"];
1046 $data["network"] = NETWORK_FEED;
1052 * @brief Check for mail contact
1054 * @param string $uri Profile link
1055 * @param integer $uid User ID
1057 * @return array mail data
1059 private function mail($uri, $uid) {
1061 if (!validate_email($uri))
1064 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
1066 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
1068 if(count($x) && count($r)) {
1069 $mailbox = construct_mailbox_name($r[0]);
1071 openssl_private_decrypt(hex2bin($r[0]['pass']), $password,$x[0]['prvkey']);
1072 $mbox = email_connect($mailbox,$r[0]['user'], $password);
1077 $msgs = email_poll($mbox, $uri);
1078 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
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);
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();
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, '');
1102 foreach($adr as $feadr) {
1103 if((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1104 &&(strcasecmp($feadr->host, $phost) == 0)
1105 && (strlen($feadr->personal))) {
1107 $personal = imap_mime_header_decode($feadr->personal);
1109 foreach($personal as $perspart)
1110 if ($perspart->charset != "default")
1111 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1113 $data["name"] .= $perspart->text;
1115 $data["name"] = notags($data["name"]);