3 namespace Friendica\Network;
6 * @file src/Network/Probe.php
7 * @brief Functions for probing URL
12 use Friendica\Core\System;
13 use Friendica\Core\Cache;
14 use Friendica\Core\Config;
15 use Friendica\Database\DBM;
23 require_once 'include/feed.php';
24 require_once 'include/email.php';
25 require_once 'include/network.php';
28 * @brief This class contain functions for probing URL
33 private static $baseurl;
36 * @brief Rearrange the array so that it always has the same order
38 * @param array $data Unordered data
40 * @return array Ordered data
42 private static function rearrangeData($data) {
43 $fields = array("name", "nick", "guid", "url", "addr", "alias",
44 "photo", "community", "keywords", "location", "about",
45 "batch", "notify", "poll", "request", "confirm", "poco",
46 "priority", "network", "pubkey", "baseurl");
49 foreach ($fields as $field) {
50 if (isset($data[$field])) {
51 $newdata[$field] = $data[$field];
53 $newdata[$field] = "";
57 // We don't use the "priority" field anymore and replace it with a dummy.
58 $newdata["priority"] = 0;
64 * @brief Check if the hostname belongs to the own server
66 * @param string $host The hostname that is to be checked
68 * @return bool Does the testes hostname belongs to the own server?
70 private static function ownHost($host) {
71 $own_host = get_app()->get_hostname();
73 $parts = parse_url($host);
75 if (!isset($parts['scheme'])) {
76 $parts = parse_url('http://'.$host);
79 if (!isset($parts['host'])) {
82 return $parts['host'] == $own_host;
86 * @brief Probes for webfinger path via "host-meta"
88 * @param string $host The host part of an url
90 * @return array with template and type of the webfinger template for JSON or XML
92 private static function hostMeta($host) {
94 // Reset the static variable
97 $ssl_url = "https://".$host."/.well-known/host-meta";
98 $url = "http://".$host."/.well-known/host-meta";
100 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
103 logger("Probing for ".$host, LOGGER_DEBUG);
105 $ret = z_fetch_url($ssl_url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
106 if ($ret['success']) {
108 $xrd = parse_xml_string($xml, false);
109 $host_url = 'https://'.$host;
112 if (!is_object($xrd)) {
113 $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml'));
114 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
115 logger("Probing timeout for ".$url, LOGGER_DEBUG);
119 $xrd = parse_xml_string($xml, false);
120 $host_url = 'http://'.$host;
122 if (!is_object($xrd)) {
123 logger("No xrd object found for ".$host, LOGGER_DEBUG);
127 $links = xml::element_to_array($xrd);
128 if (!isset($links["xrd"]["link"])) {
129 logger("No xrd data found for ".$host, LOGGER_DEBUG);
134 // The following webfinger path is defined in RFC 7033 https://tools.ietf.org/html/rfc7033
135 // Problem is that Hubzilla currently doesn't provide all data in the JSON webfinger
136 // compared to the XML webfinger. So this is commented out by now.
137 // $lrdd = array("application/jrd+json" => $host_url.'/.well-known/webfinger?resource={uri}');
139 foreach ($links["xrd"]["link"] as $value => $link) {
140 if (!empty($link["@attributes"])) {
141 $attributes = $link["@attributes"];
142 } elseif ($value == "@attributes") {
148 if (($attributes["rel"] == "lrdd") && !empty($attributes["template"])) {
149 $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
151 $lrdd[$type] = $attributes["template"];
155 self::$baseurl = "http://".$host;
157 logger("Probing successful for ".$host, LOGGER_DEBUG);
163 * @brief Perform Webfinger lookup and return DFRN data
165 * Given an email style address, perform webfinger lookup and
166 * return the resulting DFRN profile URL, or if no DFRN profile URL
167 * is located, returns an OStatus subscription template (prefixed
168 * with the string 'stat:' to identify it as on OStatus template).
169 * If this isn't an email style address just return $webbie.
170 * Return an empty string if email-style addresses but webfinger fails,
171 * or if the resultant personal XRD doesn't contain a supported
172 * subscription/friend-request attribute.
174 * amended 7/9/2011 to return an hcard which could save potentially loading
175 * a lengthy content page to scrape dfrn attributes
177 * @param string $webbie Address that should be probed
178 * @param string $hcard_url Link to the hcard - is returned by reference
180 * @return string profile link
182 public static function webfingerDfrn($webbie, &$hcard_url) {
186 $links = self::lrdd($webbie);
187 logger('webfingerDfrn: '.$webbie.':'.print_r($links, true), LOGGER_DATA);
189 foreach ($links as $link) {
190 if ($link['@attributes']['rel'] === NAMESPACE_DFRN) {
191 $profile_link = $link['@attributes']['href'];
193 if (($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) && ($profile_link == "")) {
194 $profile_link = 'stat:'.$link['@attributes']['template'];
196 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
197 $hcard_url = $link['@attributes']['href'];
201 return $profile_link;
205 * @brief Check an URI for LRDD data
207 * this is a replacement for the "lrdd" function in include/network.php.
208 * It isn't used in this class and has some redundancies in the code.
209 * When time comes we can check the existing calls for "lrdd" if we can rework them.
211 * @param string $uri Address that should be probed
213 * @return array uri data
215 public static function lrdd($uri) {
217 $lrdd = self::hostMeta($uri);
220 if (is_bool($lrdd)) {
225 $parts = @parse_url($uri);
230 $host = $parts["host"];
231 if (!empty($parts["port"])) {
232 $host .= ':'.$parts["port"];
235 $path_parts = explode("/", trim($parts["path"], "/"));
237 $nick = array_pop($path_parts);
240 $lrdd = self::hostMeta($host);
241 $host .= "/".array_shift($path_parts);
242 } while (!$lrdd && (sizeof($path_parts) > 0));
246 logger("No lrdd data found for ".$uri, LOGGER_DEBUG);
250 foreach ($lrdd AS $type => $template) {
255 $path = str_replace('{uri}', urlencode($uri), $template);
256 $webfinger = self::webfinger($path, $type);
258 if (!$webfinger && (strstr($uri, "@"))) {
259 $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
260 $webfinger = self::webfinger($path, $type);
263 // Special treatment for Mastodon
264 // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
265 // But the webfinger for this format fails.
266 if (!$webfinger && !empty($nick)) {
267 // Mastodon uses a "@" as prefix for usernames in their url format
268 $nick = ltrim($nick, '@');
270 $addr = $nick."@".$host;
272 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
273 $webfinger = self::webfinger($path, $type);
277 if (!is_array($webfinger["links"])) {
278 logger("No webfinger links found for ".$uri, LOGGER_DEBUG);
284 foreach ($webfinger["links"] as $link) {
285 $data[] = array("@attributes" => $link);
288 if (is_array($webfinger["aliases"])) {
289 foreach ($webfinger["aliases"] as $alias) {
290 $data[] = array("@attributes" =>
291 array("rel" => "alias",
300 * @brief Fetch information (protocol endpoints and user information) about a given uri
302 * @param string $uri Address that should be probed
303 * @param string $network Test for this specific network
304 * @param integer $uid User ID for the probe (only used for mails)
305 * @param boolean $cache Use cached values?
307 * @return array uri data
309 public static function uri($uri, $network = "", $uid = -1, $cache = true) {
312 $result = Cache::get("Probe::uri:".$network.":".$uri);
313 if (!is_null($result)) {
322 $data = self::detect($uri, $network, $uid);
324 if (!isset($data["url"])) {
328 if ($data["photo"] != "") {
329 $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
331 $data["photo"] = System::baseUrl().'/images/person-175.jpg';
334 if (empty($data["name"])) {
335 if (!empty($data["nick"])) {
336 $data["name"] = $data["nick"];
339 if ($data["name"] == "") {
340 $data["name"] = $data["url"];
344 if (empty($data["nick"])) {
345 $data["nick"] = strtolower($data["name"]);
347 if (strpos($data['nick'], ' ')) {
348 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
352 if (self::$baseurl != "") {
353 $data["baseurl"] = self::$baseurl;
356 if (!isset($data["network"])) {
357 $data["network"] = NETWORK_PHANTOM;
360 $data = self::rearrangeData($data);
362 // Only store into the cache if the value seems to be valid
363 if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
364 Cache::set("Probe::uri:".$network.":".$uri, $data, CACHE_DAY);
366 /// @todo temporary fix - we need a real contact update function that updates only changing fields
367 /// The biggest problem is the avatar picture that could have a reduced image size.
368 /// It should only be updated if the existing picture isn't existing anymore.
369 /// We only update the contact when it is no probing for a specific network.
370 if (($data['network'] != NETWORK_FEED)
378 $fields = array('name' => $data['name'],
379 'nick' => $data['nick'],
380 'url' => $data['url'],
381 'addr' => $data['addr'],
382 'photo' => $data['photo'],
383 'keywords' => $data['keywords'],
384 'location' => $data['location'],
385 'about' => $data['about'],
386 'notify' => $data['notify'],
387 'network' => $data['network'],
388 'server_url' => $data['baseurl']);
390 $fieldnames = array();
392 foreach ($fields AS $key => $val) {
394 unset($fields[$key]);
396 $fieldnames[] = $key;
400 $fields['updated'] = DBM::date();
402 $condition = array('nurl' => normalise_link($data["url"]));
404 $old_fields = dba::select('gcontact', $fieldnames, $condition, array('limit' => 1));
406 dba::update('gcontact', $fields, $condition, $old_fields);
408 $fields = array('name' => $data['name'],
409 'nick' => $data['nick'],
410 'url' => $data['url'],
411 'addr' => $data['addr'],
412 'alias' => $data['alias'],
413 'keywords' => $data['keywords'],
414 'location' => $data['location'],
415 'about' => $data['about'],
416 'batch' => $data['batch'],
417 'notify' => $data['notify'],
418 'poll' => $data['poll'],
419 'request' => $data['request'],
420 'confirm' => $data['confirm'],
421 'poco' => $data['poco'],
422 'network' => $data['network'],
423 'success_update' => DBM::date());
425 $fieldnames = array();
427 foreach ($fields AS $key => $val) {
429 unset($fields[$key]);
431 $fieldnames[] = $key;
435 $condition = array('nurl' => normalise_link($data["url"]), 'self' => false, 'uid' => 0);
437 $old_fields = dba::select('contact', $fieldnames, $condition, array('limit' => 1));
439 dba::update('contact', $fields, $condition, $old_fields);
447 * @brief Switch the scheme of an url between http and https
449 * @param string $url URL
451 * @return string switched URL
453 private static function switchScheme($url) {
454 $parts = parse_url($url);
456 if (!isset($parts['scheme'])) {
460 if ($parts['scheme'] == 'http') {
461 $url = str_replace('http://', 'https://', $url);
462 } elseif ($parts['scheme'] == 'https') {
463 $url = str_replace('https://', 'http://', $url);
470 * @brief Checks if a profile url should be OStatus but only provides partial information
472 * @param array $webfinger Webfinger data
473 * @param string $lrdd Path template for webfinger request
475 * @return array fixed webfinger data
477 private static function fixOstatus($webfinger, $lrdd, $type) {
478 if (empty($webfinger['links']) || empty($webfinger['subject'])) {
485 foreach ($webfinger['links'] as $link) {
486 if ($link['rel'] == NAMESPACE_OSTATUSSUB) {
489 if ($link['rel'] == 'magic-public-key') {
494 if (!$is_ostatus || $has_key) {
498 $url = self::switchScheme($webfinger['subject']);
499 $path = str_replace('{uri}', urlencode($url), $lrdd);
500 $webfinger2 = self::webfinger($path, $type);
502 // Is the new webfinger detectable as OStatus?
503 if (self::ostatus($webfinger2, true)) {
504 $webfinger = $webfinger2;
511 * @brief Fetch information (protocol endpoints and user information) about a given uri
513 * This function is only called by the "uri" function that adds caching and rearranging of data.
515 * @param string $uri Address that should be probed
516 * @param string $network Test for this specific network
517 * @param integer $uid User ID for the probe (only used for mails)
519 * @return array uri data
521 private static function detect($uri, $network, $uid) {
522 $parts = parse_url($uri);
524 if (!empty($parts["scheme"]) && !empty($parts["host"]) && !empty($parts["path"])) {
525 $host = $parts["host"];
526 if (!empty($parts["port"])) {
527 $host .= ':'.$parts["port"];
530 if ($host == 'twitter.com') {
531 return array("network" => NETWORK_TWITTER);
533 $lrdd = self::hostMeta($host);
535 if (is_bool($lrdd)) {
539 $path_parts = explode("/", trim($parts["path"], "/"));
541 while (!$lrdd && (sizeof($path_parts) > 1)) {
542 $host .= "/".array_shift($path_parts);
543 $lrdd = self::hostMeta($host);
546 logger('No XRD data was found for '.$uri, LOGGER_DEBUG);
547 return self::feed($uri);
549 $nick = array_pop($path_parts);
551 // Mastodon uses a "@" as prefix for usernames in their url format
552 $nick = ltrim($nick, '@');
554 $addr = $nick."@".$host;
556 } elseif (strstr($uri, '@')) {
557 // If the URI starts with "mailto:" then jump directly to the mail detection
558 if (strpos($uri, 'mailto:') !== false) {
559 $uri = str_replace('mailto:', '', $uri);
560 return self::mail($uri, $uid);
563 if ($network == NETWORK_MAIL) {
564 return self::mail($uri, $uid);
566 // Remove "acct:" from the URI
567 $uri = str_replace('acct:', '', $uri);
569 $host = substr($uri, strpos($uri, '@') + 1);
570 $nick = substr($uri, 0, strpos($uri, '@'));
572 if (strpos($uri, '@twitter.com')) {
573 return array("network" => NETWORK_TWITTER);
575 $lrdd = self::hostMeta($host);
577 if (is_bool($lrdd)) {
582 logger('No XRD data was found for '.$uri, LOGGER_DEBUG);
583 return self::mail($uri, $uid);
588 logger("Uri ".$uri." was not detectable", LOGGER_DEBUG);
594 /// @todo Do we need the prefix "acct:" or "acct://"?
596 foreach ($lrdd AS $type => $template) {
601 // At first try it with the given uri
602 $path = str_replace('{uri}', urlencode($uri), $template);
603 $webfinger = self::webfinger($path, $type);
605 // Fix possible problems with GNU Social probing to wrong scheme
606 $webfinger = self::fixOstatus($webfinger, $template, $type);
608 // We cannot be sure that the detected address was correct, so we don't use the values
609 if ($webfinger && ($uri != $addr)) {
614 // Try webfinger with the address (user@domain.tld)
616 $path = str_replace('{uri}', urlencode($addr), $template);
617 $webfinger = self::webfinger($path, $type);
620 // Mastodon needs to have it with "acct:"
622 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
623 $webfinger = self::webfinger($path, $type);
628 return self::feed($uri);
633 logger("Probing ".$uri, LOGGER_DEBUG);
635 if (in_array($network, array("", NETWORK_DFRN))) {
636 $result = self::dfrn($webfinger);
638 if ((!$result && ($network == "")) || ($network == NETWORK_DIASPORA)) {
639 $result = self::diaspora($webfinger);
641 if ((!$result && ($network == "")) || ($network == NETWORK_OSTATUS)) {
642 $result = self::ostatus($webfinger);
644 if ((!$result && ($network == "")) || ($network == NETWORK_PUMPIO)) {
645 $result = self::pumpio($webfinger);
647 if ((!$result && ($network == "")) || ($network == NETWORK_FEED)) {
648 $result = self::feed($uri);
650 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
651 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
652 if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) {
653 $result["nick"] = $nick;
656 if (empty($result["addr"]) && ($addr != "")) {
657 $result["addr"] = $addr;
661 logger($uri." is ".$result["network"], LOGGER_DEBUG);
663 if (empty($result["baseurl"])) {
664 $pos = strpos($result["url"], $host);
666 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
674 * @brief Perform a webfinger request.
676 * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
678 * @param string $url Address that should be probed
680 * @return array webfinger data
682 private static function webfinger($url, $type) {
684 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
687 $ret = z_fetch_url($url, false, $redirects, array('timeout' => $xrd_timeout, 'accept_content' => $type));
688 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
691 $data = $ret['body'];
693 $webfinger = json_decode($data, true);
694 if (is_array($webfinger)) {
695 if (!isset($webfinger["links"])) {
696 logger("No json webfinger links for ".$url, LOGGER_DEBUG);
702 // If it is not JSON, maybe it is XML
703 $xrd = parse_xml_string($data, false);
704 if (!is_object($xrd)) {
705 logger("No webfinger data retrievable for ".$url, LOGGER_DEBUG);
709 $xrd_arr = xml::element_to_array($xrd);
710 if (!isset($xrd_arr["xrd"]["link"])) {
711 logger("No XML webfinger links for ".$url, LOGGER_DEBUG);
715 $webfinger = array();
717 if (!empty($xrd_arr["xrd"]["subject"])) {
718 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
721 if (!empty($xrd_arr["xrd"]["alias"])) {
722 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
725 $webfinger["links"] = array();
727 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
728 if (!empty($data["@attributes"])) {
729 $attributes = $data["@attributes"];
730 } elseif ($value == "@attributes") {
736 $webfinger["links"][] = $attributes;
742 * @brief Poll the Friendica specific noscrape page.
744 * "noscrape" is a faster alternative to fetch the data from the hcard.
745 * This functionality was originally created for the directory.
747 * @param string $noscrape_url Link to the noscrape page
748 * @param array $data The already fetched data
750 * @return array noscrape data
752 private static function pollNoscrape($noscrape_url, $data) {
753 $ret = z_fetch_url($noscrape_url);
754 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
757 $content = $ret['body'];
759 logger("Empty body for ".$noscrape_url, LOGGER_DEBUG);
763 $json = json_decode($content, true);
764 if (!is_array($json)) {
765 logger("No json data for ".$noscrape_url, LOGGER_DEBUG);
769 if (!empty($json["fn"])) {
770 $data["name"] = $json["fn"];
773 if (!empty($json["addr"])) {
774 $data["addr"] = $json["addr"];
777 if (!empty($json["nick"])) {
778 $data["nick"] = $json["nick"];
781 if (!empty($json["guid"])) {
782 $data["guid"] = $json["guid"];
785 if (!empty($json["comm"])) {
786 $data["community"] = $json["comm"];
789 if (!empty($json["tags"])) {
790 $keywords = implode(" ", $json["tags"]);
791 if ($keywords != "") {
792 $data["keywords"] = $keywords;
796 $location = formatted_location($json);
798 $data["location"] = $location;
801 if (!empty($json["about"])) {
802 $data["about"] = $json["about"];
805 if (!empty($json["key"])) {
806 $data["pubkey"] = $json["key"];
809 if (!empty($json["photo"])) {
810 $data["photo"] = $json["photo"];
813 if (!empty($json["dfrn-request"])) {
814 $data["request"] = $json["dfrn-request"];
817 if (!empty($json["dfrn-confirm"])) {
818 $data["confirm"] = $json["dfrn-confirm"];
821 if (!empty($json["dfrn-notify"])) {
822 $data["notify"] = $json["dfrn-notify"];
825 if (!empty($json["dfrn-poll"])) {
826 $data["poll"] = $json["dfrn-poll"];
833 * @brief Check for valid DFRN data
835 * @param array $data DFRN data
837 * @return int Number of errors
839 public static function validDfrn($data) {
841 if (!isset($data['key'])) {
844 if (!isset($data['dfrn-request'])) {
847 if (!isset($data['dfrn-confirm'])) {
850 if (!isset($data['dfrn-notify'])) {
853 if (!isset($data['dfrn-poll'])) {
860 * @brief Fetch data from a DFRN profile page and via "noscrape"
862 * @param string $profile_link Link to the profile page
864 * @return array profile data
866 public static function profile($profile_link) {
870 logger("Check profile ".$profile_link, LOGGER_DEBUG);
872 // Fetch data via noscrape - this is faster
873 $noscrape_url = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile_link);
874 $data = self::pollNoscrape($noscrape_url, $data);
876 if (!isset($data["notify"])
877 || !isset($data["confirm"])
878 || !isset($data["request"])
879 || !isset($data["poll"])
880 || !isset($data["poco"])
881 || !isset($data["name"])
882 || !isset($data["photo"])
884 $data = self::pollHcard($profile_link, $data, true);
887 $prof_data = array();
888 $prof_data["addr"] = $data["addr"];
889 $prof_data["nick"] = $data["nick"];
890 $prof_data["dfrn-request"] = $data["request"];
891 $prof_data["dfrn-confirm"] = $data["confirm"];
892 $prof_data["dfrn-notify"] = $data["notify"];
893 $prof_data["dfrn-poll"] = $data["poll"];
894 $prof_data["dfrn-poco"] = $data["poco"];
895 $prof_data["photo"] = $data["photo"];
896 $prof_data["fn"] = $data["name"];
897 $prof_data["key"] = $data["pubkey"];
899 logger("Result for profile ".$profile_link.": ".print_r($prof_data, true), LOGGER_DEBUG);
905 * @brief Check for DFRN contact
907 * @param array $webfinger Webfinger data
909 * @return array DFRN data
911 private static function dfrn($webfinger) {
914 foreach ($webfinger["links"] as $link) {
915 if (($link["rel"] == NAMESPACE_DFRN) && ($link["href"] != "")) {
916 $data["network"] = NETWORK_DFRN;
917 } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
918 $data["poll"] = $link["href"];
919 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
920 $data["url"] = $link["href"];
921 } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
922 $hcard_url = $link["href"];
923 } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
924 $data["poco"] = $link["href"];
925 } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && ($link["href"] != "")) {
926 $data["photo"] = $link["href"];
927 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
928 $data["baseurl"] = trim($link["href"], '/');
929 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
930 $data["guid"] = $link["href"];
931 } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
932 $data["pubkey"] = base64_decode($link["href"]);
934 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
935 if (strstr($data["pubkey"], 'RSA ')) {
936 $data["pubkey"] = rsatopem($data["pubkey"]);
941 if (is_array($webfinger["aliases"])) {
942 foreach ($webfinger["aliases"] as $alias) {
943 if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
944 $data["alias"] = $alias;
945 } elseif (substr($alias, 0, 5) == 'acct:') {
946 $data["addr"] = substr($alias, 5);
951 if (!isset($data["network"]) || ($hcard_url == "")) {
955 // Fetch data via noscrape - this is faster
956 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
957 $data = self::pollNoscrape($noscrape_url, $data);
959 if (isset($data["notify"])
960 && isset($data["confirm"])
961 && isset($data["request"])
962 && isset($data["poll"])
963 && isset($data["name"])
964 && isset($data["photo"])
969 $data = self::pollHcard($hcard_url, $data, true);
975 * @brief Poll the hcard page (Diaspora and Friendica specific)
977 * @param string $hcard_url Link to the hcard page
978 * @param array $data The already fetched data
979 * @param boolean $dfrn Poll DFRN specific data
981 * @return array hcard data
983 private static function pollHcard($hcard_url, $data, $dfrn = false) {
984 $ret = z_fetch_url($hcard_url);
985 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
988 $content = $ret['body'];
993 $doc = new DOMDocument();
994 if (!@$doc->loadHTML($content)) {
998 $xpath = new DomXPath($doc);
1000 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
1001 if (!is_object($vcards)) {
1005 if ($vcards->length > 0) {
1006 $vcard = $vcards->item(0);
1008 // We have to discard the guid from the hcard in favour of the guid from lrdd
1009 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
1010 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
1011 if (($search->length > 0) && ($data["guid"] == "")) {
1012 $data["guid"] = $search->item(0)->nodeValue;
1015 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
1016 if ($search->length > 0) {
1017 $data["nick"] = $search->item(0)->nodeValue;
1020 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
1021 if ($search->length > 0) {
1022 $data["name"] = $search->item(0)->nodeValue;
1025 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
1026 if ($search->length > 0) {
1027 $data["searchable"] = $search->item(0)->nodeValue;
1030 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
1031 if ($search->length > 0) {
1032 $data["pubkey"] = $search->item(0)->nodeValue;
1033 if (strstr($data["pubkey"], 'RSA ')) {
1034 $data["pubkey"] = rsatopem($data["pubkey"]);
1038 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
1039 if ($search->length > 0) {
1040 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
1045 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
1046 foreach ($photos as $photo) {
1048 foreach ($photo->attributes as $attribute) {
1049 $attr[$attribute->name] = trim($attribute->value);
1052 if (isset($attr["src"]) && isset($attr["width"])) {
1053 $avatar[$attr["width"]] = $attr["src"];
1056 // We don't have a width. So we just take everything that we got.
1057 // This is a Hubzilla workaround which doesn't send a width.
1058 if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
1059 $avatar[] = $attr["src"];
1063 if (sizeof($avatar)) {
1065 $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
1069 // Poll DFRN specific data
1070 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
1071 if ($search->length > 0) {
1072 foreach ($search as $link) {
1073 //$data["request"] = $search->item(0)->nodeValue;
1075 foreach ($link->attributes as $attribute) {
1076 $attr[$attribute->name] = trim($attribute->value);
1079 $data[substr($attr["rel"], 5)] = $attr["href"];
1083 // Older Friendica versions had used the "uid" field differently than newer versions
1084 if ($data["nick"] == $data["guid"]) {
1085 unset($data["guid"]);
1094 * @brief Check for Diaspora contact
1096 * @param array $webfinger Webfinger data
1098 * @return array Diaspora data
1100 private static function diaspora($webfinger) {
1103 foreach ($webfinger["links"] as $link) {
1104 if (($link["rel"] == "http://microformats.org/profile/hcard") && ($link["href"] != "")) {
1105 $hcard_url = $link["href"];
1106 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && ($link["href"] != "")) {
1107 $data["baseurl"] = trim($link["href"], '/');
1108 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && ($link["href"] != "")) {
1109 $data["guid"] = $link["href"];
1110 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && ($link["type"] == "text/html") && ($link["href"] != "")) {
1111 $data["url"] = $link["href"];
1112 } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
1113 $data["poll"] = $link["href"];
1114 } elseif (($link["rel"] == NAMESPACE_POCO) && ($link["href"] != "")) {
1115 $data["poco"] = $link["href"];
1116 } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
1117 $data["notify"] = $link["href"];
1118 } elseif (($link["rel"] == "diaspora-public-key") && ($link["href"] != "")) {
1119 $data["pubkey"] = base64_decode($link["href"]);
1121 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1122 if (strstr($data["pubkey"], 'RSA ')) {
1123 $data["pubkey"] = rsatopem($data["pubkey"]);
1128 if (!isset($data["url"]) || ($hcard_url == "")) {
1132 if (is_array($webfinger["aliases"])) {
1133 foreach ($webfinger["aliases"] as $alias) {
1134 if (normalise_link($alias) != normalise_link($data["url"]) && ! strstr($alias, "@")) {
1135 $data["alias"] = $alias;
1136 } elseif (substr($alias, 0, 5) == 'acct:') {
1137 $data["addr"] = substr($alias, 5);
1142 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
1143 $data["addr"] = substr($webfinger["subject"], 5);
1146 // Fetch further information from the hcard
1147 $data = self::pollHcard($hcard_url, $data);
1153 if (isset($data["url"])
1154 && isset($data["guid"])
1155 && isset($data["baseurl"])
1156 && isset($data["pubkey"])
1157 && ($hcard_url != "")
1159 $data["network"] = NETWORK_DIASPORA;
1161 // The Diaspora handle must always be lowercase
1162 $data["addr"] = strtolower($data["addr"]);
1164 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
1165 $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
1166 $data["batch"] = $data["baseurl"] . "/receive/public";
1175 * @brief Check for OStatus contact
1177 * @param array $webfinger Webfinger data
1178 * @param bool $short Short detection mode
1180 * @return array|bool OStatus data or "false" on error or "true" on short mode
1182 private static function ostatus($webfinger, $short = false) {
1185 if (is_array($webfinger["aliases"])) {
1186 foreach ($webfinger["aliases"] as $alias) {
1187 if (strstr($alias, "@") && !strstr(normalise_link($alias), "http://")) {
1188 $data["addr"] = str_replace('acct:', '', $alias);
1193 if (is_string($webfinger["subject"]) && strstr($webfinger["subject"], "@") &&
1194 !strstr(normalise_link($webfinger["subject"]), "http://")) {
1195 $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1199 if (is_array($webfinger["links"])) {
1200 foreach ($webfinger["links"] as $link) {
1201 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1202 && ($link["type"] == "text/html")
1203 && ($link["href"] != "")
1205 $data["url"] = $link["href"];
1206 } elseif (($link["rel"] == "salmon") && ($link["href"] != "")) {
1207 $data["notify"] = $link["href"];
1208 } elseif (($link["rel"] == NAMESPACE_FEED) && ($link["href"] != "")) {
1209 $data["poll"] = $link["href"];
1210 } elseif (($link["rel"] == "magic-public-key") && ($link["href"] != "")) {
1211 $pubkey = $link["href"];
1213 if (substr($pubkey, 0, 5) === 'data:') {
1214 if (strstr($pubkey, ',')) {
1215 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1217 $pubkey = substr($pubkey, 5);
1219 } elseif (normalise_link($pubkey) == 'http://') {
1220 $ret = z_fetch_url($pubkey);
1221 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1224 $pubkey = $ret['body'];
1227 $key = explode(".", $pubkey);
1229 if (sizeof($key) >= 3) {
1230 $m = base64url_decode($key[1]);
1231 $e = base64url_decode($key[2]);
1232 $data["pubkey"] = metopem($m, $e);
1238 if (isset($data["notify"]) && isset($data["pubkey"])
1239 && isset($data["poll"])
1240 && isset($data["url"])
1242 $data["network"] = NETWORK_OSTATUS;
1251 // Fetch all additional data from the feed
1252 $ret = z_fetch_url($data["poll"]);
1253 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1256 $feed = $ret['body'];
1257 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1262 if ($feed_data["header"]["author-name"] != "") {
1263 $data["name"] = $feed_data["header"]["author-name"];
1265 if ($feed_data["header"]["author-nick"] != "") {
1266 $data["nick"] = $feed_data["header"]["author-nick"];
1268 if ($feed_data["header"]["author-avatar"] != "") {
1269 $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1271 if ($feed_data["header"]["author-id"] != "") {
1272 $data["alias"] = $feed_data["header"]["author-id"];
1274 if ($feed_data["header"]["author-location"] != "") {
1275 $data["location"] = $feed_data["header"]["author-location"];
1277 if ($feed_data["header"]["author-about"] != "") {
1278 $data["about"] = $feed_data["header"]["author-about"];
1280 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1281 // So we take the value that we just fetched, although the other one worked as well
1282 if ($feed_data["header"]["author-link"] != "") {
1283 $data["url"] = $feed_data["header"]["author-link"];
1286 if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
1287 $data['url'] = $data["alias"];
1288 $data["alias"] = '';
1291 /// @todo Fetch location and "about" from the feed as well
1296 * @brief Fetch data from a pump.io profile page
1298 * @param string $profile_link Link to the profile page
1300 * @return array profile data
1302 private static function pumpioProfileData($profile_link) {
1304 $doc = new DOMDocument();
1305 if (!@$doc->loadHTMLFile($profile_link)) {
1309 $xpath = new DomXPath($doc);
1313 // This is ugly - but pump.io doesn't seem to know a better way for it
1314 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1315 $pos = strpos($data["name"], chr(10));
1317 $data["name"] = trim(substr($data["name"], 0, $pos));
1320 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1322 foreach ($avatar->attributes as $attribute) {
1323 if ($attribute->name == "src") {
1324 $data["photo"] = trim($attribute->value);
1329 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
1330 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
1336 * @brief Check for pump.io contact
1338 * @param array $webfinger Webfinger data
1340 * @return array pump.io data
1342 private static function pumpio($webfinger) {
1345 foreach ($webfinger["links"] as $link) {
1346 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1347 && ($link["type"] == "text/html")
1348 && ($link["href"] != "")
1350 $data["url"] = $link["href"];
1351 } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
1352 $data["notify"] = $link["href"];
1353 } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
1354 $data["poll"] = $link["href"];
1355 } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
1356 $data["dialback"] = $link["href"];
1359 if (isset($data["poll"]) && isset($data["notify"])
1360 && isset($data["dialback"])
1361 && isset($data["url"])
1363 // by now we use these fields only for the network type detection
1364 // So we unset all data that isn't used at the moment
1365 unset($data["dialback"]);
1367 $data["network"] = NETWORK_PUMPIO;
1372 $profile_data = self::pumpioProfileData($data["url"]);
1374 if (!$profile_data) {
1378 $data = array_merge($data, $profile_data);
1384 * @brief Check page for feed link
1386 * @param string $url Page link
1388 * @return string feed link
1390 private static function getFeedLink($url) {
1391 $doc = new DOMDocument();
1393 if (!@$doc->loadHTMLFile($url)) {
1397 $xpath = new DomXPath($doc);
1399 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1400 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1401 if (!is_object($feeds)) {
1405 if ($feeds->length == 0) {
1411 foreach ($feeds as $feed) {
1413 foreach ($feed->attributes as $attribute) {
1414 $attr[$attribute->name] = trim($attribute->value);
1417 if ($feed_url == "") {
1418 $feed_url = $attr["href"];
1426 * @brief Check for feed contact
1428 * @param string $url Profile link
1429 * @param boolean $probe Do a probe if the page contains a feed link
1431 * @return array feed data
1433 private static function feed($url, $probe = true) {
1434 $ret = z_fetch_url($url);
1435 if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
1438 $feed = $ret['body'];
1439 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
1446 $feed_url = self::getFeedLink($url);
1452 return self::feed($feed_url, false);
1455 if ($feed_data["header"]["author-name"] != "") {
1456 $data["name"] = $feed_data["header"]["author-name"];
1459 if ($feed_data["header"]["author-nick"] != "") {
1460 $data["nick"] = $feed_data["header"]["author-nick"];
1463 if ($feed_data["header"]["author-avatar"] != "") {
1464 $data["photo"] = $feed_data["header"]["author-avatar"];
1467 if ($feed_data["header"]["author-id"] != "") {
1468 $data["alias"] = $feed_data["header"]["author-id"];
1471 $data["url"] = $url;
1472 $data["poll"] = $url;
1474 if ($feed_data["header"]["author-link"] != "") {
1475 $data["baseurl"] = $feed_data["header"]["author-link"];
1477 $data["baseurl"] = $data["url"];
1480 $data["network"] = NETWORK_FEED;
1486 * @brief Check for mail contact
1488 * @param string $uri Profile link
1489 * @param integer $uid User ID
1491 * @return array mail data
1493 private static function mail($uri, $uid) {
1495 if (!validate_email($uri)) {
1500 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
1502 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
1504 if (DBM::is_result($x) && DBM::is_result($r)) {
1505 $mailbox = construct_mailbox_name($r[0]);
1507 openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']);
1508 $mbox = email_connect($mailbox, $r[0]['user'], $password);
1514 $msgs = email_poll($mbox, $uri);
1515 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
1517 if (!count($msgs)) {
1522 $phost = substr($uri, strpos($uri, '@') + 1);
1525 $data["addr"] = $uri;
1526 $data["network"] = NETWORK_MAIL;
1527 $data["name"] = substr($uri, 0, strpos($uri, '@'));
1528 $data["nick"] = $data["name"];
1529 $data["photo"] = avatar_img($uri);
1530 $data["url"] = 'mailto:'.$uri;
1531 $data["notify"] = 'smtp '.random_string();
1532 $data["poll"] = 'email '.random_string();
1534 $x = email_msg_meta($mbox, $msgs[0]);
1535 if (stristr($x[0]->from, $uri)) {
1536 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1537 } elseif (stristr($x[0]->to, $uri)) {
1538 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1541 foreach ($adr as $feadr) {
1542 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1543 &&(strcasecmp($feadr->host, $phost) == 0)
1544 && (strlen($feadr->personal))
1546 $personal = imap_mime_header_decode($feadr->personal);
1548 foreach ($personal as $perspart) {
1549 if ($perspart->charset != "default") {
1550 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1552 $data["name"] .= $perspart->text;
1556 $data["name"] = notags($data["name"]);
1560 if (!empty($mbox)) {
1568 * @brief Mix two paths together to possibly fix missing parts
1570 * @param string $avatar Path to the avatar
1571 * @param string $base Another path that is hopefully complete
1573 * @return string fixed avatar path
1575 public static function fixAvatar($avatar, $base) {
1576 $base_parts = parse_url($base);
1578 // Remove all parts that could create a problem
1579 unset($base_parts['path']);
1580 unset($base_parts['query']);
1581 unset($base_parts['fragment']);
1583 $avatar_parts = parse_url($avatar);
1586 $parts = array_merge($base_parts, $avatar_parts);
1588 // And put them together again
1589 $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
1590 $host = isset($parts['host']) ? $parts['host'] : '';
1591 $port = isset($parts['port']) ? ':' . $parts['port'] : '';
1592 $path = isset($parts['path']) ? $parts['path'] : '';
1593 $query = isset($parts['query']) ? '?' . $parts['query'] : '';
1594 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1596 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1598 logger('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, LOGGER_DATA);