3 * @file include/Probe.php
4 * @brief Functions for probing URL
9 use Friendica\Core\Config;
10 use Friendica\Core\PConfig;
12 require_once("include/feed.php");
13 require_once('include/email.php');
14 require_once('include/network.php');
17 * @brief This class contain functions for probing URL
22 private static $baseurl;
25 * @brief Rearrange the array so that it always has the same order
27 * @param array $data Unordered data
29 * @return array Ordered data
31 private function rearrange_data($data) {
32 $fields = array("name", "nick", "guid", "url", "addr", "alias",
33 "photo", "community", "keywords", "location", "about",
34 "batch", "notify", "poll", "request", "confirm", "poco",
35 "priority", "network", "pubkey", "baseurl");
38 foreach ($fields AS $field)
39 if (isset($data[$field]))
40 $newdata[$field] = $data[$field];
42 $newdata[$field] = "";
44 // We don't use the "priority" field anymore and replace it with a dummy.
45 $newdata["priority"] = 0;
51 * @brief Probes for XRD data
54 * 'lrdd' => Link to LRDD endpoint
55 * 'lrdd-xml' => Link to LRDD endpoint in XML format
56 * 'lrdd-json' => Link to LRDD endpoint in JSON format
58 private function xrd($host) {
60 // Reset the static variable
63 $ssl_url = "https://".$host."/.well-known/host-meta";
64 $url = "http://".$host."/.well-known/host-meta";
66 $xrd_timeout = Config::get('system','xrd_timeout', 20);
69 $ret = z_fetch_url($ssl_url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
70 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
75 $xrd = parse_xml_string($xml, false);
77 if (!is_object($xrd)) {
78 $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
79 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
83 $xrd = parse_xml_string($xml, false);
88 $links = xml::element_to_array($xrd);
89 if (!isset($links["xrd"]["link"]))
94 foreach ($links["xrd"]["link"] AS $value => $link) {
95 if (isset($link["@attributes"]))
96 $attributes = $link["@attributes"];
97 elseif ($value == "@attributes")
102 if (($attributes["rel"] == "lrdd") AND
103 ($attributes["type"] == "application/xrd+xml"))
104 $xrd_data["lrdd-xml"] = $attributes["template"];
105 elseif (($attributes["rel"] == "lrdd") AND
106 ($attributes["type"] == "application/json"))
107 $xrd_data["lrdd-json"] = $attributes["template"];
108 elseif ($attributes["rel"] == "lrdd")
109 $xrd_data["lrdd"] = $attributes["template"];
112 self::$baseurl = "http://".$host;
118 * @brief Perform Webfinger lookup and return DFRN data
120 * Given an email style address, perform webfinger lookup and
121 * return the resulting DFRN profile URL, or if no DFRN profile URL
122 * is located, returns an OStatus subscription template (prefixed
123 * with the string 'stat:' to identify it as on OStatus template).
124 * If this isn't an email style address just return $webbie.
125 * Return an empty string if email-style addresses but webfinger fails,
126 * or if the resultant personal XRD doesn't contain a supported
127 * subscription/friend-request attribute.
129 * amended 7/9/2011 to return an hcard which could save potentially loading
130 * a lengthy content page to scrape dfrn attributes
132 * @param string $webbie Address that should be probed
133 * @param string $hcard Link to the hcard - is returned by reference
135 * @return string profile link
138 public static function webfinger_dfrn($webbie, &$hcard) {
142 $links = self::lrdd($webbie);
143 logger('webfinger_dfrn: '.$webbie.':'.print_r($links,true), LOGGER_DATA);
145 foreach ($links as $link) {
146 if ($link['@attributes']['rel'] === NAMESPACE_DFRN)
147 $profile_link = $link['@attributes']['href'];
148 if (($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) AND ($profile_link == ""))
149 $profile_link = 'stat:'.$link['@attributes']['template'];
150 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
151 $hcard = $link['@attributes']['href'];
154 return $profile_link;
158 * @brief Check an URI for LRDD data
160 * this is a replacement for the "lrdd" function in include/network.php.
161 * It isn't used in this class and has some redundancies in the code.
162 * When time comes we can check the existing calls for "lrdd" if we can rework them.
164 * @param string $uri Address that should be probed
166 * @return array uri data
168 public static function lrdd($uri) {
170 $lrdd = self::xrd($uri);
173 $parts = @parse_url($uri);
177 $host = $parts["host"];
178 if (isset($parts["port"])) {
179 $host .= ':'.$parts["port"];
182 $path_parts = explode("/", trim($parts["path"], "/"));
184 $nick = array_pop($path_parts);
187 $lrdd = self::xrd($host);
188 $host .= "/".array_shift($path_parts);
189 } while (!$lrdd AND (sizeof($path_parts) > 0));
195 foreach ($lrdd AS $key => $link) {
199 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
202 $path = str_replace('{uri}', urlencode($uri), $link);
203 $webfinger = self::webfinger($path);
205 if (!$webfinger AND (strstr($uri, "@"))) {
206 $path = str_replace('{uri}', urlencode("acct:".$uri), $link);
207 $webfinger = self::webfinger($path);
210 // Special treatment for Mastodon
211 // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
212 // But the webfinger for this format fails.
213 if (!$webfinger AND isset($nick)) {
214 // Mastodon uses a "@" as prefix for usernames in their url format
215 $nick = ltrim($nick, '@');
217 $addr = $nick."@".$host;
219 $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
220 $webfinger = self::webfinger($path);
224 if (!is_array($webfinger["links"]))
229 foreach ($webfinger["links"] AS $link)
230 $data[] = array("@attributes" => $link);
232 if (is_array($webfinger["aliases"]))
233 foreach ($webfinger["aliases"] AS $alias)
234 $data[] = array("@attributes" =>
235 array("rel" => "alias",
242 * @brief Fetch information (protocol endpoints and user information) about a given uri
244 * @param string $uri Address that should be probed
245 * @param string $network Test for this specific network
246 * @param integer $uid User ID for the probe (only used for mails)
247 * @param boolean $cache Use cached values?
249 * @return array uri data
251 public static function uri($uri, $network = "", $uid = 0, $cache = true) {
254 $result = Cache::get("probe_url:".$network.":".$uri);
255 if (!is_null($result)) {
263 $data = self::detect($uri, $network, $uid);
265 if (!isset($data["url"]))
268 if ($data["photo"] != "")
269 $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
271 $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
273 if (!isset($data["name"]) OR ($data["name"] == "")) {
274 if (isset($data["nick"]))
275 $data["name"] = $data["nick"];
277 if ($data["name"] == "")
278 $data["name"] = $data["url"];
281 if (!isset($data["nick"]) OR ($data["nick"] == "")) {
282 $data["nick"] = strtolower($data["name"]);
284 if (strpos($data['nick'], ' '))
285 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
288 if (self::$baseurl != "") {
289 $data["baseurl"] = self::$baseurl;
292 if (!isset($data["network"])) {
293 $data["network"] = NETWORK_PHANTOM;
296 $data = self::rearrange_data($data);
298 // Only store into the cache if the value seems to be valid
299 if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
300 Cache::set("probe_url:".$network.":".$uri, $data, CACHE_DAY);
302 /// @todo temporary fix - we need a real contact update function that updates only changing fields
303 /// The biggest problem is the avatar picture that could have a reduced image size.
304 /// It should only be updated if the existing picture isn't existing anymore.
305 if (($data['network'] != NETWORK_FEED) AND ($mode == PROBE_NORMAL) AND
306 $data["name"] AND $data["nick"] AND $data["url"] AND $data["addr"] AND $data["poll"])
307 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
308 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
309 WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
310 dbesc($data["name"]),
311 dbesc($data["nick"]),
313 dbesc($data["addr"]),
314 dbesc($data["notify"]),
315 dbesc($data["poll"]),
316 dbesc($data["alias"]),
317 dbesc(datetime_convert()),
318 dbesc(normalise_link($data['url']))
326 * @brief Fetch information (protocol endpoints and user information) about a given uri
328 * This function is only called by the "uri" function that adds caching and rearranging of data.
330 * @param string $uri Address that should be probed
331 * @param string $network Test for this specific network
332 * @param integer $uid User ID for the probe (only used for mails)
334 * @return array uri data
336 private function detect($uri, $network, $uid) {
337 $parts = parse_url($uri);
339 if (isset($parts["scheme"]) AND isset($parts["host"]) AND isset($parts["path"])) {
341 $host = $parts["host"];
342 if (isset($parts["port"])) {
343 $host .= ':'.$parts["port"];
346 if ($host == 'twitter.com') {
347 return array("network" => NETWORK_TWITTER);
349 $lrdd = self::xrd($host);
351 $path_parts = explode("/", trim($parts["path"], "/"));
353 while (!$lrdd AND (sizeof($path_parts) > 1)) {
354 $host .= "/".array_shift($path_parts);
355 $lrdd = self::xrd($host);
358 return self::feed($uri);
360 $nick = array_pop($path_parts);
362 // Mastodon uses a "@" as prefix for usernames in their url format
363 $nick = ltrim($nick, '@');
365 $addr = $nick."@".$host;
366 } elseif (strstr($uri, '@')) {
367 // If the URI starts with "mailto:" then jump directly to the mail detection
368 if (strpos($url,'mailto:') !== false) {
369 $uri = str_replace('mailto:', '', $url);
370 return self::mail($uri, $uid);
373 if ($network == NETWORK_MAIL) {
374 return self::mail($uri, $uid);
376 // Remove "acct:" from the URI
377 $uri = str_replace('acct:', '', $uri);
379 $host = substr($uri,strpos($uri, '@') + 1);
380 $nick = substr($uri,0, strpos($uri, '@'));
382 if (strpos($uri, '@twitter.com')) {
383 return array("network" => NETWORK_TWITTER);
385 $lrdd = self::xrd($host);
388 return self::mail($uri, $uid);
397 /// @todo Do we need the prefix "acct:" or "acct://"?
399 foreach ($lrdd AS $key => $link) {
403 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json"))) {
406 // At first try it with the given uri
407 $path = str_replace('{uri}', urlencode($uri), $link);
408 $webfinger = self::webfinger($path);
410 // We cannot be sure that the detected address was correct, so we don't use the values
411 if ($webfinger AND ($uri != $addr)) {
416 // Try webfinger with the address (user@domain.tld)
418 $path = str_replace('{uri}', urlencode($addr), $link);
419 $webfinger = self::webfinger($path);
422 // Mastodon needs to have it with "acct:"
424 $path = str_replace('{uri}', urlencode("acct:".$addr), $link);
425 $webfinger = self::webfinger($path);
429 return self::feed($uri);
434 logger("Probing ".$uri, LOGGER_DEBUG);
436 if (in_array($network, array("", NETWORK_DFRN)))
437 $result = self::dfrn($webfinger);
438 if ((!$result AND ($network == "")) OR ($network == NETWORK_DIASPORA))
439 $result = self::diaspora($webfinger);
440 if ((!$result AND ($network == "")) OR ($network == NETWORK_OSTATUS))
441 $result = self::ostatus($webfinger);
442 if ((!$result AND ($network == "")) OR ($network == NETWORK_PUMPIO))
443 $result = self::pumpio($webfinger);
444 if ((!$result AND ($network == "")) OR ($network == NETWORK_FEED))
445 $result = self::feed($uri);
447 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
448 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
449 if ((!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " "))) AND ($nick != ""))
450 $result["nick"] = $nick;
452 if ((!isset($result["addr"]) OR ($result["addr"] == "")) AND ($addr != ""))
453 $result["addr"] = $addr;
456 logger($uri." is ".$result["network"], LOGGER_DEBUG);
458 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
459 $pos = strpos($result["url"], $host);
461 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
468 * @brief Perform a webfinger request.
470 * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
472 * @param string $url Address that should be probed
474 * @return array webfinger data
476 private function webfinger($url) {
478 $xrd_timeout = Config::get('system','xrd_timeout', 20);
481 $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
482 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
485 $data = $ret['body'];
487 $xrd = parse_xml_string($data, false);
489 if (!is_object($xrd)) {
490 // If it is not XML, maybe it is JSON
491 $webfinger = json_decode($data, true);
493 if (!isset($webfinger["links"]))
499 $xrd_arr = xml::element_to_array($xrd);
500 if (!isset($xrd_arr["xrd"]["link"]))
503 $webfinger = array();
505 if (isset($xrd_arr["xrd"]["subject"]))
506 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
508 if (isset($xrd_arr["xrd"]["alias"]))
509 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
511 $webfinger["links"] = array();
513 foreach ($xrd_arr["xrd"]["link"] AS $value => $data) {
514 if (isset($data["@attributes"]))
515 $attributes = $data["@attributes"];
516 elseif ($value == "@attributes")
521 $webfinger["links"][] = $attributes;
527 * @brief Poll the Friendica specific noscrape page.
529 * "noscrape" is a faster alternative to fetch the data from the hcard.
530 * This functionality was originally created for the directory.
532 * @param string $noscrape Link to the noscrape page
533 * @param array $data The already fetched data
535 * @return array noscrape data
537 private function poll_noscrape($noscrape, $data) {
538 $ret = z_fetch_url($noscrape);
539 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
542 $content = $ret['body'];
547 $json = json_decode($content, true);
548 if (!is_array($json))
551 if (isset($json["fn"]))
552 $data["name"] = $json["fn"];
554 if (isset($json["addr"]))
555 $data["addr"] = $json["addr"];
557 if (isset($json["nick"]))
558 $data["nick"] = $json["nick"];
560 if (isset($json["comm"]))
561 $data["community"] = $json["comm"];
563 if (isset($json["tags"])) {
564 $keywords = implode(" ", $json["tags"]);
566 $data["keywords"] = $keywords;
569 $location = formatted_location($json);
571 $data["location"] = $location;
573 if (isset($json["about"]))
574 $data["about"] = $json["about"];
576 if (isset($json["key"]))
577 $data["pubkey"] = $json["key"];
579 if (isset($json["photo"]))
580 $data["photo"] = $json["photo"];
582 if (isset($json["dfrn-request"]))
583 $data["request"] = $json["dfrn-request"];
585 if (isset($json["dfrn-confirm"]))
586 $data["confirm"] = $json["dfrn-confirm"];
588 if (isset($json["dfrn-notify"]))
589 $data["notify"] = $json["dfrn-notify"];
591 if (isset($json["dfrn-poll"]))
592 $data["poll"] = $json["dfrn-poll"];
598 * @brief Check for valid DFRN data
600 * @param array $data DFRN data
602 * @return int Number of errors
604 public static function valid_dfrn($data) {
606 if(!isset($data['key']))
608 if(!isset($data['dfrn-request']))
610 if(!isset($data['dfrn-confirm']))
612 if(!isset($data['dfrn-notify']))
614 if(!isset($data['dfrn-poll']))
620 * @brief Fetch data from a DFRN profile page and via "noscrape"
622 * @param string $profile Link to the profile page
624 * @return array profile data
626 public static function profile($profile) {
630 logger("Check profile ".$profile, LOGGER_DEBUG);
632 // Fetch data via noscrape - this is faster
633 $noscrape = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile);
634 $data = self::poll_noscrape($noscrape, $data);
636 if (!isset($data["notify"]) OR !isset($data["confirm"]) OR
637 !isset($data["request"]) OR !isset($data["poll"]) OR
638 !isset($data["poco"]) OR !isset($data["name"]) OR
639 !isset($data["photo"]))
640 $data = self::poll_hcard($profile, $data, true);
642 $prof_data = array();
643 $prof_data["addr"] = $data["addr"];
644 $prof_data["nick"] = $data["nick"];
645 $prof_data["dfrn-request"] = $data["request"];
646 $prof_data["dfrn-confirm"] = $data["confirm"];
647 $prof_data["dfrn-notify"] = $data["notify"];
648 $prof_data["dfrn-poll"] = $data["poll"];
649 $prof_data["dfrn-poco"] = $data["poco"];
650 $prof_data["photo"] = $data["photo"];
651 $prof_data["fn"] = $data["name"];
652 $prof_data["key"] = $data["pubkey"];
654 logger("Result for profile ".$profile.": ".print_r($prof_data, true), LOGGER_DEBUG);
660 * @brief Check for DFRN contact
662 * @param array $webfinger Webfinger data
664 * @return array DFRN data
666 private function dfrn($webfinger) {
670 foreach ($webfinger["links"] AS $link) {
671 if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != ""))
672 $data["network"] = NETWORK_DFRN;
673 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
674 $data["poll"] = $link["href"];
675 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
676 ($link["type"] == "text/html") AND ($link["href"] != ""))
677 $data["url"] = $link["href"];
678 elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
679 $hcard = $link["href"];
680 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
681 $data["poco"] = $link["href"];
682 elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != ""))
683 $data["photo"] = $link["href"];
685 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
686 $data["baseurl"] = trim($link["href"], '/');
687 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
688 $data["guid"] = $link["href"];
689 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
690 $data["pubkey"] = base64_decode($link["href"]);
692 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
693 if (strstr($data["pubkey"], 'RSA '))
694 $data["pubkey"] = rsatopem($data["pubkey"]);
698 if (!isset($data["network"]) OR ($hcard == ""))
701 // Fetch data via noscrape - this is faster
702 $noscrape = str_replace("/hcard/", "/noscrape/", $hcard);
703 $data = self::poll_noscrape($noscrape, $data);
705 if (isset($data["notify"]) AND isset($data["confirm"]) AND isset($data["request"]) AND
706 isset($data["poll"]) AND isset($data["name"]) AND isset($data["photo"]))
709 $data = self::poll_hcard($hcard, $data, true);
715 * @brief Poll the hcard page (Diaspora and Friendica specific)
717 * @param string $hcard Link to the hcard page
718 * @param array $data The already fetched data
719 * @param boolean $dfrn Poll DFRN specific data
721 * @return array hcard data
723 private function poll_hcard($hcard, $data, $dfrn = false) {
724 $ret = z_fetch_url($hcard);
725 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
728 $content = $ret['body'];
733 $doc = new DOMDocument();
734 if (!@$doc->loadHTML($content))
737 $xpath = new DomXPath($doc);
739 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
740 if (!is_object($vcards))
743 if ($vcards->length > 0) {
744 $vcard = $vcards->item(0);
746 // We have to discard the guid from the hcard in favour of the guid from lrdd
747 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
748 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
749 if (($search->length > 0) AND ($data["guid"] == ""))
750 $data["guid"] = $search->item(0)->nodeValue;
752 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
753 if ($search->length > 0)
754 $data["nick"] = $search->item(0)->nodeValue;
756 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
757 if ($search->length > 0)
758 $data["name"] = $search->item(0)->nodeValue;
760 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
761 if ($search->length > 0)
762 $data["searchable"] = $search->item(0)->nodeValue;
764 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
765 if ($search->length > 0) {
766 $data["pubkey"] = $search->item(0)->nodeValue;
767 if (strstr($data["pubkey"], 'RSA '))
768 $data["pubkey"] = rsatopem($data["pubkey"]);
771 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
772 if ($search->length > 0)
773 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
777 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
778 foreach ($photos AS $photo) {
780 foreach ($photo->attributes as $attribute) {
781 $attr[$attribute->name] = trim($attribute->value);
784 if (isset($attr["src"]) AND isset($attr["width"])) {
785 $avatar[$attr["width"]] = $attr["src"];
788 // We don't have a width. So we just take everything that we got.
789 // This is a Hubzilla workaround which doesn't send a width.
790 if ((sizeof($avatar) == 0) AND isset($attr["src"])) {
791 $avatar[] = $attr["src"];
795 if (sizeof($avatar)) {
797 $data["photo"] = self::fix_avatar(array_pop($avatar), $data["baseurl"]);
801 // Poll DFRN specific data
802 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
803 if ($search->length > 0) {
804 foreach ($search AS $link) {
805 //$data["request"] = $search->item(0)->nodeValue;
807 foreach ($link->attributes as $attribute)
808 $attr[$attribute->name] = trim($attribute->value);
810 $data[substr($attr["rel"], 5)] = $attr["href"];
814 // Older Friendica versions had used the "uid" field differently than newer versions
815 if ($data["nick"] == $data["guid"])
816 unset($data["guid"]);
824 * @brief Check for Diaspora contact
826 * @param array $webfinger Webfinger data
828 * @return array Diaspora data
830 private function diaspora($webfinger) {
834 foreach ($webfinger["links"] AS $link) {
835 if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
836 $hcard = $link["href"];
837 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
838 $data["baseurl"] = trim($link["href"], '/');
839 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
840 $data["guid"] = $link["href"];
841 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
842 ($link["type"] == "text/html") AND ($link["href"] != ""))
843 $data["url"] = $link["href"];
844 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
845 $data["poll"] = $link["href"];
846 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
847 $data["poco"] = $link["href"];
848 elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
849 $data["notify"] = $link["href"];
850 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
851 $data["pubkey"] = base64_decode($link["href"]);
853 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
854 if (strstr($data["pubkey"], 'RSA '))
855 $data["pubkey"] = rsatopem($data["pubkey"]);
859 if (!isset($data["url"]) OR ($hcard == ""))
862 if (is_array($webfinger["aliases"]))
863 foreach ($webfinger["aliases"] AS $alias)
864 if (normalise_link($alias) != normalise_link($data["url"]) AND !strstr($alias, "@"))
865 $data["alias"] = $alias;
867 // Fetch further information from the hcard
868 $data = self::poll_hcard($hcard, $data);
873 if (isset($data["url"]) AND isset($data["guid"]) AND isset($data["baseurl"]) AND
874 isset($data["pubkey"]) AND ($hcard != "")) {
875 $data["network"] = NETWORK_DIASPORA;
877 // The Diaspora handle must always be lowercase
878 $data["addr"] = strtolower($data["addr"]);
880 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
881 $data["notify"] = $data["baseurl"]."/receive/users/".$data["guid"];
882 $data["batch"] = $data["baseurl"]."/receive/public";
890 * @brief Check for OStatus contact
892 * @param array $webfinger Webfinger data
894 * @return array OStatus data
896 private function ostatus($webfinger) {
898 if (is_array($webfinger["aliases"])) {
899 foreach ($webfinger["aliases"] AS $alias) {
900 if (strstr($alias, "@")) {
901 $data["addr"] = str_replace('acct:', '', $alias);
906 if (is_string($webfinger["subject"]) AND strstr($webfinger["subject"], "@")) {
907 $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
910 foreach ($webfinger["links"] AS $link) {
911 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
912 ($link["type"] == "text/html") AND ($link["href"] != "")) {
913 $data["url"] = $link["href"];
914 } elseif (($link["rel"] == "salmon") AND ($link["href"] != "")) {
915 $data["notify"] = $link["href"];
916 } elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != "")) {
917 $data["poll"] = $link["href"];
918 } elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
919 $pubkey = $link["href"];
921 if (substr($pubkey, 0, 5) === 'data:') {
922 if (strstr($pubkey, ',')) {
923 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
925 $pubkey = substr($pubkey, 5);
927 } elseif (normalise_link($pubkey) == 'http://') {
928 $ret = z_fetch_url($pubkey);
929 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
932 $pubkey = $ret['body'];
935 $key = explode(".", $pubkey);
937 if (sizeof($key) >= 3) {
938 $m = base64url_decode($key[1]);
939 $e = base64url_decode($key[2]);
940 $data["pubkey"] = metopem($m,$e);
945 if (isset($data["notify"]) AND isset($data["pubkey"]) AND
946 isset($data["poll"]) AND isset($data["url"])) {
947 $data["network"] = NETWORK_OSTATUS;
951 // Fetch all additional data from the feed
952 $ret = z_fetch_url($data["poll"]);
953 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
956 $feed = $ret['body'];
957 $feed_data = feed_import($feed,$dummy1,$dummy2, $dummy3, true);
961 if ($feed_data["header"]["author-name"] != "") {
962 $data["name"] = $feed_data["header"]["author-name"];
964 if ($feed_data["header"]["author-nick"] != "") {
965 $data["nick"] = $feed_data["header"]["author-nick"];
967 if ($feed_data["header"]["author-avatar"] != "") {
968 $data["photo"] = self::fix_avatar($feed_data["header"]["author-avatar"], $data["url"]);
970 if ($feed_data["header"]["author-id"] != "") {
971 $data["alias"] = $feed_data["header"]["author-id"];
973 if ($feed_data["header"]["author-location"] != "") {
974 $data["location"] = $feed_data["header"]["author-location"];
976 if ($feed_data["header"]["author-about"] != "") {
977 $data["about"] = $feed_data["header"]["author-about"];
979 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
980 // So we take the value that we just fetched, although the other one worked as well
981 if ($feed_data["header"]["author-link"] != "") {
982 $data["url"] = $feed_data["header"]["author-link"];
984 /// @todo Fetch location and "about" from the feed as well
989 * @brief Fetch data from a pump.io profile page
991 * @param string $profile Link to the profile page
993 * @return array profile data
995 private function pumpio_profile_data($profile) {
997 $doc = new DOMDocument();
998 if (!@$doc->loadHTMLFile($profile))
1001 $xpath = new DomXPath($doc);
1005 // This is ugly - but pump.io doesn't seem to know a better way for it
1006 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1007 $pos = strpos($data["name"], chr(10));
1009 $data["name"] = trim(substr($data["name"], 0, $pos));
1011 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1013 foreach ($avatar->attributes as $attribute)
1014 if ($attribute->name == "src")
1015 $data["photo"] = trim($attribute->value);
1017 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
1018 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
1024 * @brief Check for pump.io contact
1026 * @param array $webfinger Webfinger data
1028 * @return array pump.io data
1030 private function pumpio($webfinger) {
1033 foreach ($webfinger["links"] AS $link) {
1034 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
1035 ($link["type"] == "text/html") AND ($link["href"] != ""))
1036 $data["url"] = $link["href"];
1037 elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != ""))
1038 $data["notify"] = $link["href"];
1039 elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != ""))
1040 $data["poll"] = $link["href"];
1041 elseif (($link["rel"] == "dialback") AND ($link["href"] != ""))
1042 $data["dialback"] = $link["href"];
1044 if (isset($data["poll"]) AND isset($data["notify"]) AND
1045 isset($data["dialback"]) AND isset($data["url"])) {
1047 // by now we use these fields only for the network type detection
1048 // So we unset all data that isn't used at the moment
1049 unset($data["dialback"]);
1051 $data["network"] = NETWORK_PUMPIO;
1055 $profile_data = self::pumpio_profile_data($data["url"]);
1060 $data = array_merge($data, $profile_data);
1066 * @brief Check page for feed link
1068 * @param string $url Page link
1070 * @return string feed link
1072 private function get_feed_link($url) {
1073 $doc = new DOMDocument();
1075 if (!@$doc->loadHTMLFile($url))
1078 $xpath = new DomXPath($doc);
1080 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1081 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1082 if (!is_object($feeds))
1085 if ($feeds->length == 0)
1090 foreach ($feeds AS $feed) {
1092 foreach ($feed->attributes as $attribute)
1093 $attr[$attribute->name] = trim($attribute->value);
1095 if ($feed_url == "")
1096 $feed_url = $attr["href"];
1103 * @brief Check for feed contact
1105 * @param string $url Profile link
1106 * @param boolean $probe Do a probe if the page contains a feed link
1108 * @return array feed data
1110 private function feed($url, $probe = true) {
1111 $ret = z_fetch_url($url);
1112 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1115 $feed = $ret['body'];
1116 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1122 $feed_url = self::get_feed_link($url);
1127 return self::feed($feed_url, false);
1130 if ($feed_data["header"]["author-name"] != "")
1131 $data["name"] = $feed_data["header"]["author-name"];
1133 if ($feed_data["header"]["author-nick"] != "")
1134 $data["nick"] = $feed_data["header"]["author-nick"];
1136 if ($feed_data["header"]["author-avatar"] != "")
1137 $data["photo"] = $feed_data["header"]["author-avatar"];
1139 if ($feed_data["header"]["author-id"] != "")
1140 $data["alias"] = $feed_data["header"]["author-id"];
1142 $data["url"] = $url;
1143 $data["poll"] = $url;
1145 if ($feed_data["header"]["author-link"] != "")
1146 $data["baseurl"] = $feed_data["header"]["author-link"];
1148 $data["baseurl"] = $data["url"];
1150 $data["network"] = NETWORK_FEED;
1156 * @brief Check for mail contact
1158 * @param string $uri Profile link
1159 * @param integer $uid User ID
1161 * @return array mail data
1163 private function mail($uri, $uid) {
1165 if (!validate_email($uri))
1168 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
1170 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
1172 if (dbm::is_result($x) && dbm::is_result($r)) {
1173 $mailbox = construct_mailbox_name($r[0]);
1175 openssl_private_decrypt(hex2bin($r[0]['pass']), $password,$x[0]['prvkey']);
1176 $mbox = email_connect($mailbox,$r[0]['user'], $password);
1181 $msgs = email_poll($mbox, $uri);
1182 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
1189 $data["addr"] = $uri;
1190 $data["network"] = NETWORK_MAIL;
1191 $data["name"] = substr($uri, 0, strpos($uri,'@'));
1192 $data["nick"] = $data["name"];
1193 $data["photo"] = avatar_img($uri);
1195 $phost = substr($uri, strpos($uri,'@') + 1);
1196 $data["url"] = 'http://'.$phost."/".$data["nick"];
1197 $data["notify"] = 'smtp '.random_string();
1198 $data["poll"] = 'email '.random_string();
1200 $x = email_msg_meta($mbox, $msgs[0]);
1201 if(stristr($x[0]->from, $uri))
1202 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1203 elseif(stristr($x[0]->to, $uri))
1204 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1206 foreach($adr as $feadr) {
1207 if((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1208 &&(strcasecmp($feadr->host, $phost) == 0)
1209 && (strlen($feadr->personal))) {
1211 $personal = imap_mime_header_decode($feadr->personal);
1213 foreach($personal as $perspart)
1214 if ($perspart->charset != "default")
1215 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1217 $data["name"] .= $perspart->text;
1219 $data["name"] = notags($data["name"]);
1229 * @brief Mix two paths together to possibly fix missing parts
1231 * @param string $avatar Path to the avatar
1232 * @param string $base Another path that is hopefully complete
1234 * @return string fixed avatar path
1236 public static function fix_avatar($avatar, $base) {
1237 $base_parts = parse_url($base);
1239 // Remove all parts that could create a problem
1240 unset($base_parts['path']);
1241 unset($base_parts['query']);
1242 unset($base_parts['fragment']);
1244 $avatar_parts = parse_url($avatar);
1247 $parts = array_merge($base_parts, $avatar_parts);
1249 // And put them together again
1250 $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
1251 $host = isset($parts['host']) ? $parts['host'] : '';
1252 $port = isset($parts['port']) ? ':' . $parts['port'] : '';
1253 $path = isset($parts['path']) ? $parts['path'] : '';
1254 $query = isset($parts['query']) ? '?' . $parts['query'] : '';
1255 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1257 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1259 logger('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, LOGGER_DATA);