3 * @file src/Network/Probe.php
5 namespace Friendica\Network;
8 * @file src/Network/Probe.php
9 * @brief Functions for probing URL
14 use Friendica\Core\Cache;
15 use Friendica\Core\Config;
16 use Friendica\Core\Logger;
17 use Friendica\Core\Protocol;
18 use Friendica\Core\System;
19 use Friendica\Database\DBA;
20 use Friendica\Model\Contact;
21 use Friendica\Model\Profile;
22 use Friendica\Protocol\ActivityNamespace;
23 use Friendica\Protocol\ActivityPub;
24 use Friendica\Protocol\Email;
25 use Friendica\Protocol\Feed;
26 use Friendica\Util\Crypto;
27 use Friendica\Util\Network;
28 use Friendica\Util\Strings;
29 use Friendica\Util\XML;
32 * @brief This class contain functions for probing URL
37 private static $baseurl;
38 private static $istimeout;
41 * @brief Rearrange the array so that it always has the same order
43 * @param array $data Unordered data
45 * @return array Ordered data
47 private static function rearrangeData($data)
49 $fields = ["name", "nick", "guid", "url", "addr", "alias", "photo", "account-type",
50 "community", "keywords", "location", "about", "gender", "hide",
51 "batch", "notify", "poll", "request", "confirm", "poco",
52 "following", "followers", "inbox", "outbox", "sharedinbox",
53 "priority", "network", "pubkey", "baseurl"];
56 foreach ($fields as $field) {
57 if (isset($data[$field])) {
58 $newdata[$field] = $data[$field];
60 $newdata[$field] = "";
64 // We don't use the "priority" field anymore and replace it with a dummy.
65 $newdata["priority"] = 0;
71 * @brief Check if the hostname belongs to the own server
73 * @param string $host The hostname that is to be checked
75 * @return bool Does the testes hostname belongs to the own server?
77 private static function ownHost($host)
79 $own_host = \get_app()->getHostName();
81 $parts = parse_url($host);
83 if (!isset($parts['scheme'])) {
84 $parts = parse_url('http://'.$host);
87 if (!isset($parts['host'])) {
90 return $parts['host'] == $own_host;
94 * @brief Probes for webfinger path via "host-meta"
96 * We have to check if the servers in the future still will offer this.
97 * It seems as if it was dropped from the standard.
99 * @param string $host The host part of an url
101 * @return array with template and type of the webfinger template for JSON or XML
102 * @throws HTTPException\InternalServerErrorException
104 private static function hostMeta($host)
106 // Reset the static variable
109 $ssl_url = "https://".$host."/.well-known/host-meta";
110 $url = "http://".$host."/.well-known/host-meta";
112 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
114 Logger::log("Probing for ".$host, Logger::DEBUG);
117 $curlResult = Network::curl($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
118 if ($curlResult->isSuccess()) {
119 $xml = $curlResult->getBody();
120 $xrd = XML::parseString($xml, false);
121 $host_url = 'https://'.$host;
124 if (!is_object($xrd)) {
125 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
126 if ($curlResult->isTimeout()) {
127 Logger::log("Probing timeout for " . $url, Logger::DEBUG);
128 self::$istimeout = true;
131 $xml = $curlResult->getBody();
132 $xrd = XML::parseString($xml, false);
133 $host_url = 'http://'.$host;
135 if (!is_object($xrd)) {
136 Logger::log("No xrd object found for ".$host, Logger::DEBUG);
140 $links = XML::elementToArray($xrd);
141 if (!isset($links["xrd"]["link"])) {
142 Logger::log("No xrd data found for ".$host, Logger::DEBUG);
146 $lrdd = ['application/jrd+json' => $host_url . '/.well-known/webfinger?resource={uri}'];
148 foreach ($links["xrd"]["link"] as $value => $link) {
149 if (!empty($link["@attributes"])) {
150 $attributes = $link["@attributes"];
151 } elseif ($value == "@attributes") {
157 if (!empty($attributes["rel"]) && $attributes["rel"] == "lrdd" && !empty($attributes["template"])) {
158 $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
160 $lrdd[$type] = $attributes["template"];
164 self::$baseurl = $host_url;
166 Logger::log("Probing successful for ".$host, Logger::DEBUG);
172 * @brief Perform Webfinger lookup and return DFRN data
174 * Given an email style address, perform webfinger lookup and
175 * return the resulting DFRN profile URL, or if no DFRN profile URL
176 * is located, returns an OStatus subscription template (prefixed
177 * with the string 'stat:' to identify it as on OStatus template).
178 * If this isn't an email style address just return $webbie.
179 * Return an empty string if email-style addresses but webfinger fails,
180 * or if the resultant personal XRD doesn't contain a supported
181 * subscription/friend-request attribute.
183 * amended 7/9/2011 to return an hcard which could save potentially loading
184 * a lengthy content page to scrape dfrn attributes
186 * @param string $webbie Address that should be probed
187 * @param string $hcard_url Link to the hcard - is returned by reference
189 * @return string profile link
190 * @throws HTTPException\InternalServerErrorException
192 public static function webfingerDfrn($webbie, &$hcard_url)
196 $links = self::lrdd($webbie);
197 Logger::log('webfingerDfrn: '.$webbie.':'.print_r($links, true), Logger::DATA);
198 if (!empty($links) && is_array($links)) {
199 foreach ($links as $link) {
200 if ($link['@attributes']['rel'] === ActivityNamespace::DFRN) {
201 $profile_link = $link['@attributes']['href'];
203 if (($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) && ($profile_link == "")) {
204 $profile_link = 'stat:'.$link['@attributes']['template'];
206 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
207 $hcard_url = $link['@attributes']['href'];
211 return $profile_link;
215 * @brief Check an URI for LRDD data
217 * this is a replacement for the "lrdd" function.
218 * It isn't used in this class and has some redundancies in the code.
219 * When time comes we can check the existing calls for "lrdd" if we can rework them.
221 * @param string $uri Address that should be probed
223 * @return array uri data
224 * @throws HTTPException\InternalServerErrorException
226 public static function lrdd($uri)
228 $lrdd = self::hostMeta($uri);
231 if (is_bool($lrdd)) {
236 $parts = @parse_url($uri);
237 if (!$parts || empty($parts["host"]) || empty($parts["path"])) {
241 $host = $parts["host"];
242 if (!empty($parts["port"])) {
243 $host .= ':'.$parts["port"];
246 $path_parts = explode("/", trim($parts["path"], "/"));
248 $nick = array_pop($path_parts);
251 $lrdd = self::hostMeta($host);
252 $host .= "/".array_shift($path_parts);
253 } while (!$lrdd && (sizeof($path_parts) > 0));
257 Logger::log("No lrdd data found for ".$uri, Logger::DEBUG);
261 foreach ($lrdd as $type => $template) {
266 $path = str_replace('{uri}', urlencode($uri), $template);
267 $webfinger = self::webfinger($path, $type);
269 if (!$webfinger && (strstr($uri, "@"))) {
270 $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
271 $webfinger = self::webfinger($path, $type);
274 // Special treatment for Mastodon
275 // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
276 // But the webfinger for this format fails.
277 if (!$webfinger && !empty($nick)) {
278 // Mastodon uses a "@" as prefix for usernames in their url format
279 $nick = ltrim($nick, '@');
281 $addr = $nick."@".$host;
283 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
284 $webfinger = self::webfinger($path, $type);
288 if (!is_array($webfinger["links"])) {
289 Logger::log("No webfinger links found for ".$uri, Logger::DEBUG);
295 foreach ($webfinger["links"] as $link) {
296 $data[] = ["@attributes" => $link];
299 if (is_array($webfinger["aliases"])) {
300 foreach ($webfinger["aliases"] as $alias) {
301 $data[] = ["@attributes" =>
311 * @brief Fetch information (protocol endpoints and user information) about a given uri
313 * @param string $uri Address that should be probed
314 * @param string $network Test for this specific network
315 * @param integer $uid User ID for the probe (only used for mails)
316 * @param boolean $cache Use cached values?
318 * @return array uri data
319 * @throws HTTPException\InternalServerErrorException
320 * @throws \ImagickException
322 public static function uri($uri, $network = '', $uid = -1, $cache = true)
325 $result = Cache::get('Probe::uri:' . $network . ':' . $uri);
326 if (!is_null($result)) {
335 self::$istimeout = false;
337 if ($network != Protocol::ACTIVITYPUB) {
338 $data = self::detect($uri, $network, $uid);
343 // When the previous detection process had got a time out
344 // we could falsely detect a Friendica profile as AP profile.
345 if (!self::$istimeout) {
346 $ap_profile = ActivityPub::probeProfile($uri);
348 if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) {
350 } elseif (!empty($ap_profile)) {
351 $ap_profile['batch'] = '';
352 $data = array_merge($ap_profile, $data);
355 Logger::notice('Time out detected. AP will not be probed.', ['uri' => $uri]);
358 if (!isset($data['url'])) {
362 if (!empty($data['photo'])) {
363 $data['baseurl'] = Network::getUrlMatch(Strings::normaliseLink($data['baseurl'] ?? ''), Strings::normaliseLink($data['photo']));
365 $data['photo'] = System::baseUrl() . '/images/person-300.jpg';
368 if (empty($data['name'])) {
369 if (!empty($data['nick'])) {
370 $data['name'] = $data['nick'];
373 if (empty($data['name'])) {
374 $data['name'] = $data['url'];
378 if (empty($data['nick'])) {
379 $data['nick'] = strtolower($data['name']);
381 if (strpos($data['nick'], ' ')) {
382 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
386 if (!empty(self::$baseurl)) {
387 $data['baseurl'] = self::$baseurl;
390 if (empty($data['network'])) {
391 $data['network'] = Protocol::PHANTOM;
394 if (!isset($data['hide']) && in_array($data['network'], Protocol::FEDERATED)) {
395 $data['hide'] = self::getHideStatus($data['url']);
398 $data = self::rearrangeData($data);
400 // Only store into the cache if the value seems to be valid
401 if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
402 Cache::set('Probe::uri:' . $network . ':' . $uri, $data, Cache::DAY);
410 * Fetches the "hide" status from the profile
412 * @param string $url URL of the profile
414 * @return boolean "hide" status
416 private static function getHideStatus($url)
418 $curlResult = Network::curl($url);
419 if (!$curlResult->isSuccess()) {
423 // If the file is too large then exit
424 if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
428 // If it isn't a HTML file then exit
429 if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
433 $body = $curlResult->getBody();
435 $doc = new DOMDocument();
436 @$doc->loadHTML($body);
438 $xpath = new DOMXPath($doc);
440 $list = $xpath->query('//meta[@name]');
441 foreach ($list as $node) {
443 if ($node->attributes->length) {
444 foreach ($node->attributes as $attribute) {
445 $meta_tag[$attribute->name] = $attribute->value;
449 if (empty($meta_tag['content'])) {
453 $content = strtolower(trim($meta_tag['content']));
455 switch (strtolower(trim($meta_tag['name']))) {
456 case 'dfrn-global-visibility':
457 if ($content == 'false') {
462 if (strpos($content, 'noindex') !== false) {
473 * @brief Checks if a profile url should be OStatus but only provides partial information
475 * @param array $webfinger Webfinger data
476 * @param string $lrdd Path template for webfinger request
477 * @param string $type type
479 * @return array fixed webfinger data
480 * @throws HTTPException\InternalServerErrorException
482 private static function fixOStatus($webfinger, $lrdd, $type)
484 if (empty($webfinger['links']) || empty($webfinger['subject'])) {
491 foreach ($webfinger['links'] as $link) {
492 if ($link['rel'] == ActivityNamespace::OSTATUSSUB) {
495 if ($link['rel'] == 'magic-public-key') {
500 if (!$is_ostatus || $has_key) {
504 $url = Network::switchScheme($webfinger['subject']);
505 $path = str_replace('{uri}', urlencode($url), $lrdd);
506 $webfinger2 = self::webfinger($path, $type);
508 // Is the new webfinger detectable as OStatus?
509 if (self::ostatus($webfinger2, true)) {
510 $webfinger = $webfinger2;
517 * @brief Fetch information (protocol endpoints and user information) about a given uri
519 * This function is only called by the "uri" function that adds caching and rearranging of data.
521 * @param string $uri Address that should be probed
522 * @param string $network Test for this specific network
523 * @param integer $uid User ID for the probe (only used for mails)
525 * @return array uri data
526 * @throws HTTPException\InternalServerErrorException
528 private static function detect($uri, $network, $uid)
530 $parts = parse_url($uri);
532 if (!empty($parts["scheme"]) && !empty($parts["host"])) {
533 $host = $parts["host"];
534 if (!empty($parts["port"])) {
535 $host .= ':'.$parts["port"];
538 if ($host == 'twitter.com') {
539 return self::twitter($uri);
541 $lrdd = self::hostMeta($host);
543 if (is_bool($lrdd)) {
547 $path_parts = explode("/", trim($parts['path'] ?? '', "/"));
549 while (!$lrdd && (sizeof($path_parts) > 1)) {
550 $host .= "/".array_shift($path_parts);
551 $lrdd = self::hostMeta($host);
554 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
555 return self::feed($uri);
557 $nick = array_pop($path_parts);
559 // Mastodon uses a "@" as prefix for usernames in their url format
560 $nick = ltrim($nick, '@');
562 $addr = $nick."@".$host;
563 } elseif (strstr($uri, '@')) {
564 // If the URI starts with "mailto:" then jump directly to the mail detection
565 if (strpos($uri, 'mailto:') !== false) {
566 $uri = str_replace('mailto:', '', $uri);
567 return self::mail($uri, $uid);
570 if ($network == Protocol::MAIL) {
571 return self::mail($uri, $uid);
573 // Remove "acct:" from the URI
574 $uri = str_replace('acct:', '', $uri);
576 $host = substr($uri, strpos($uri, '@') + 1);
577 $nick = substr($uri, 0, strpos($uri, '@'));
579 if (strpos($uri, '@twitter.com')) {
580 return self::twitter($uri);
582 $lrdd = self::hostMeta($host);
584 if (is_bool($lrdd)) {
589 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
590 return self::mail($uri, $uid);
594 Logger::log("Uri ".$uri." was not detectable", Logger::DEBUG);
600 /// @todo Do we need the prefix "acct:" or "acct://"?
602 foreach ($lrdd as $type => $template) {
607 // At first try it with the given uri
608 $path = str_replace('{uri}', urlencode($uri), $template);
609 $webfinger = self::webfinger($path, $type);
611 // Fix possible problems with GNU Social probing to wrong scheme
612 $webfinger = self::fixOStatus($webfinger, $template, $type);
614 // We cannot be sure that the detected address was correct, so we don't use the values
615 if ($webfinger && ($uri != $addr)) {
620 // Try webfinger with the address (user@domain.tld)
622 $path = str_replace('{uri}', urlencode($addr), $template);
623 $webfinger = self::webfinger($path, $type);
626 // Mastodon needs to have it with "acct:"
628 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
629 $webfinger = self::webfinger($path, $type);
634 return self::feed($uri);
639 Logger::log("Probing ".$uri, Logger::DEBUG);
641 if (in_array($network, ["", Protocol::DFRN])) {
642 $result = self::dfrn($webfinger);
644 if ((empty($result['network']) && ($network == "")) || ($network == Protocol::DIASPORA)) {
645 $result = self::diaspora($webfinger, $result);
647 if ((empty($result['network']) && ($network == "")) || ($network == Protocol::OSTATUS)) {
648 $result = self::ostatus($webfinger, false, $result);
650 if ((empty($result['network']) && ($network == "")) || ($network == Protocol::PUMPIO)) {
651 $result = self::pumpio($webfinger, $addr, $result);
653 if ((empty($result['network']) && ($network == "")) || ($network == Protocol::ZOT)) {
654 $result = self::hubzilla($webfinger, $result);
656 if ((empty($result['network']) && ($network == "")) || ($network == Protocol::FEED)) {
657 $result = self::feed($uri, true, $result);
659 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
660 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
661 if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) {
662 $result["nick"] = $nick;
665 if (empty($result["addr"]) && ($addr != "")) {
666 $result["addr"] = $addr;
670 if (empty($result["network"])) {
671 $result["network"] = Protocol::PHANTOM;
674 if (empty($result["url"])) {
675 $result["url"] = $uri;
678 Logger::log($uri." is ".$result["network"], Logger::DEBUG);
680 if (empty($result["baseurl"])) {
681 $pos = strpos($result["url"], $host);
683 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
689 private static function hubzilla($webfinger, $data)
691 if (strstr($webfinger['properties']['http://purl.org/zot/federation'] ?? '', 'zot')) {
692 $data['network'] = Protocol::ZOT;
694 if (!empty($webfinger['properties']['http://webfinger.net/ns/name'])) {
695 $data['name'] = $webfinger['properties']['http://webfinger.net/ns/name'];
697 if (!empty($webfinger['properties']['https://w3id.org/security/v1#publicKeyPem'])) {
698 $data['pubkey'] = $webfinger['properties']['https://w3id.org/security/v1#publicKeyPem'];
700 //print_r($webfinger);
703 foreach ($webfinger['links'] as $link) {
704 if (($link['rel'] == 'http://microformats.org/profile/hcard') && !empty($link['href'])) {
705 $hcard_url = $link['href'];
706 } elseif (($link['rel'] == 'http://purl.org/zot/protocol') && !empty($link['href'])) {
707 $zot_url = $link['href'];
708 } elseif (($link["rel"] == "http://purl.org/zot/protocol/6.0") && !empty($link["href"])) {
709 $data["url"] = $link["href"];
710 } elseif (($link["rel"] == "http://webfinger.net/rel/blog") && !empty($link["href"]) && empty($data["url"])) {
711 $data["url"] = $link["href"];
715 if (empty($zot_url) && !empty($data['addr']) && !empty(self::$baseurl)) {
716 $zot_url = self::$baseurl . '/.well-known/zot-info?address=' . $data['addr'];
719 if (!empty($hcard_url)) {
720 $data = self::pollHcard($hcard_url, $data, false);
723 if (!empty($zot_url)) {
724 $data = self::pollZot($zot_url, $data);
730 public static function pollZot($url, $data)
732 $curlResult = Network::curl($url);
733 if ($curlResult->isTimeout()) {
736 $content = $curlResult->getBody();
741 $json = json_decode($content, true);
742 if (!is_array($json)) {
746 if (!empty($json['protocols']) && in_array('zot', $json['protocols'])) {
747 $data['network'] = Protocol::ZOT;
750 if (!empty($json['guid'])) {
751 $data['guid'] = $json['guid'];
753 if (!empty($json['key'])) {
754 $data['pubkey'] = $json['key'];
756 if (!empty($json['name'])) {
757 $data['name'] = $json['name'];
759 if (!empty($json['address'])) {
760 $data['addr'] = $json['address'];
762 if (!empty($json['url'])) {
763 $data['url'] = $json['url'];
765 if (!empty($json['connections_url'])) {
766 $data['poco'] = $json['connections_url'];
768 if (isset($json['searchable'])) {
769 $data['hide'] = !$json['searchable'];
771 if (!empty($json['public_forum'])) {
772 $data['community'] = $json['public_forum'];
773 $data['account-type'] = Contact::PAGE_COMMUNITY;
776 if (!empty($json['profile'])) {
777 $profile = $json['profile'];
778 if (!empty($profile['description'])) {
779 $data['about'] = $profile['description'];
781 if (!empty($profile['gender'])) {
782 $data['gender'] = $profile['gender'];
784 if (!empty($profile['keywords'])) {
785 $keywords = implode(', ', $profile['keywords']);
786 if (!empty($keywords)) {
787 $data['keywords'] = $keywords;
792 if (!empty($profile['region'])) {
793 $loc['region'] = $profile['region'];
795 if (!empty($profile['country'])) {
796 $loc['country-name'] = $profile['country'];
798 if (!empty($profile['hometown'])) {
799 $loc['locality'] = $profile['hometown'];
801 $location = Profile::formatLocation($loc);
802 if (!empty($location)) {
803 $data['location'] = $location;
811 * @brief Perform a webfinger request.
813 * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
815 * @param string $url Address that should be probed
816 * @param string $type type
818 * @return array webfinger data
819 * @throws HTTPException\InternalServerErrorException
821 private static function webfinger($url, $type)
823 $xrd_timeout = Config::get('system', 'xrd_timeout', 20);
825 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
826 if ($curlResult->isTimeout()) {
827 self::$istimeout = true;
830 $data = $curlResult->getBody();
832 $webfinger = json_decode($data, true);
833 if (is_array($webfinger)) {
834 if (!isset($webfinger["links"])) {
835 Logger::log("No json webfinger links for ".$url, Logger::DEBUG);
841 // If it is not JSON, maybe it is XML
842 $xrd = XML::parseString($data, false);
843 if (!is_object($xrd)) {
844 Logger::log("No webfinger data retrievable for ".$url, Logger::DEBUG);
848 $xrd_arr = XML::elementToArray($xrd);
849 if (!isset($xrd_arr["xrd"]["link"])) {
850 Logger::log("No XML webfinger links for ".$url, Logger::DEBUG);
856 if (!empty($xrd_arr["xrd"]["subject"])) {
857 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
860 if (!empty($xrd_arr["xrd"]["alias"])) {
861 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
864 $webfinger["links"] = [];
866 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
867 if (!empty($data["@attributes"])) {
868 $attributes = $data["@attributes"];
869 } elseif ($value == "@attributes") {
875 $webfinger["links"][] = $attributes;
881 * @brief Poll the Friendica specific noscrape page.
883 * "noscrape" is a faster alternative to fetch the data from the hcard.
884 * This functionality was originally created for the directory.
886 * @param string $noscrape_url Link to the noscrape page
887 * @param array $data The already fetched data
889 * @return array noscrape data
890 * @throws HTTPException\InternalServerErrorException
892 private static function pollNoscrape($noscrape_url, $data)
894 $curlResult = Network::curl($noscrape_url);
895 if ($curlResult->isTimeout()) {
896 self::$istimeout = true;
899 $content = $curlResult->getBody();
901 Logger::log("Empty body for ".$noscrape_url, Logger::DEBUG);
905 $json = json_decode($content, true);
906 if (!is_array($json)) {
907 Logger::log("No json data for ".$noscrape_url, Logger::DEBUG);
911 if (!empty($json["fn"])) {
912 $data["name"] = $json["fn"];
915 if (!empty($json["addr"])) {
916 $data["addr"] = $json["addr"];
919 if (!empty($json["nick"])) {
920 $data["nick"] = $json["nick"];
923 if (!empty($json["guid"])) {
924 $data["guid"] = $json["guid"];
927 if (!empty($json["comm"])) {
928 $data["community"] = $json["comm"];
931 if (!empty($json["tags"])) {
932 $keywords = implode(", ", $json["tags"]);
933 if ($keywords != "") {
934 $data["keywords"] = $keywords;
938 $location = Profile::formatLocation($json);
940 $data["location"] = $location;
943 if (!empty($json["about"])) {
944 $data["about"] = $json["about"];
947 if (!empty($json["gender"])) {
948 $data["gender"] = $json["gender"];
951 if (!empty($json["key"])) {
952 $data["pubkey"] = $json["key"];
955 if (!empty($json["photo"])) {
956 $data["photo"] = $json["photo"];
959 if (!empty($json["dfrn-request"])) {
960 $data["request"] = $json["dfrn-request"];
963 if (!empty($json["dfrn-confirm"])) {
964 $data["confirm"] = $json["dfrn-confirm"];
967 if (!empty($json["dfrn-notify"])) {
968 $data["notify"] = $json["dfrn-notify"];
971 if (!empty($json["dfrn-poll"])) {
972 $data["poll"] = $json["dfrn-poll"];
975 if (isset($json["hide"])) {
976 $data["hide"] = (bool)$json["hide"];
978 $data["hide"] = false;
985 * @brief Check for valid DFRN data
987 * @param array $data DFRN data
989 * @return int Number of errors
991 public static function validDfrn($data)
994 if (!isset($data['key'])) {
997 if (!isset($data['dfrn-request'])) {
1000 if (!isset($data['dfrn-confirm'])) {
1003 if (!isset($data['dfrn-notify'])) {
1006 if (!isset($data['dfrn-poll'])) {
1013 * @brief Fetch data from a DFRN profile page and via "noscrape"
1015 * @param string $profile_link Link to the profile page
1017 * @return array profile data
1018 * @throws HTTPException\InternalServerErrorException
1019 * @throws \ImagickException
1021 public static function profile($profile_link)
1025 Logger::log("Check profile ".$profile_link, Logger::DEBUG);
1027 // Fetch data via noscrape - this is faster
1028 $noscrape_url = str_replace(["/hcard/", "/profile/"], "/noscrape/", $profile_link);
1029 $data = self::pollNoscrape($noscrape_url, $data);
1031 if (!isset($data["notify"])
1032 || !isset($data["confirm"])
1033 || !isset($data["request"])
1034 || !isset($data["poll"])
1035 || !isset($data["name"])
1036 || !isset($data["photo"])
1038 $data = self::pollHcard($profile_link, $data, true);
1043 if (empty($data["addr"]) || empty($data["nick"])) {
1044 $probe_data = self::uri($profile_link);
1045 $data["addr"] = ($data["addr"] ?? '') ?: $probe_data["addr"];
1046 $data["nick"] = ($data["nick"] ?? '') ?: $probe_data["nick"];
1049 $prof_data["addr"] = $data["addr"];
1050 $prof_data["nick"] = $data["nick"];
1051 $prof_data["dfrn-request"] = $data['request'] ?? null;
1052 $prof_data["dfrn-confirm"] = $data['confirm'] ?? null;
1053 $prof_data["dfrn-notify"] = $data['notify'] ?? null;
1054 $prof_data["dfrn-poll"] = $data['poll'] ?? null;
1055 $prof_data["photo"] = $data['photo'] ?? null;
1056 $prof_data["fn"] = $data['name'] ?? null;
1057 $prof_data["key"] = $data['pubkey'] ?? null;
1059 Logger::log("Result for profile ".$profile_link.": ".print_r($prof_data, true), Logger::DEBUG);
1065 * @brief Check for DFRN contact
1067 * @param array $webfinger Webfinger data
1069 * @return array DFRN data
1070 * @throws HTTPException\InternalServerErrorException
1072 private static function dfrn($webfinger)
1076 // The array is reversed to take into account the order of preference for same-rel links
1077 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1078 foreach (array_reverse($webfinger["links"]) as $link) {
1079 if (($link["rel"] == ActivityNamespace::DFRN) && !empty($link["href"])) {
1080 $data["network"] = Protocol::DFRN;
1081 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1082 $data["poll"] = $link["href"];
1083 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1084 $data["url"] = $link["href"];
1085 } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1086 $hcard_url = $link["href"];
1087 } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1088 $data["poco"] = $link["href"];
1089 } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) {
1090 $data["photo"] = $link["href"];
1091 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1092 $data["baseurl"] = trim($link["href"], '/');
1093 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1094 $data["guid"] = $link["href"];
1095 } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1096 $data["pubkey"] = base64_decode($link["href"]);
1098 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1099 if (strstr($data["pubkey"], 'RSA ')) {
1100 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1105 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1106 foreach ($webfinger["aliases"] as $alias) {
1107 if (empty($data["url"]) && !strstr($alias, "@")) {
1108 $data["url"] = $alias;
1109 } elseif (!strstr($alias, "@") && Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"])) {
1110 $data["alias"] = $alias;
1111 } elseif (substr($alias, 0, 5) == 'acct:') {
1112 $data["addr"] = substr($alias, 5);
1117 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
1118 $data["addr"] = substr($webfinger["subject"], 5);
1121 if (!isset($data["network"]) || ($hcard_url == "")) {
1125 // Fetch data via noscrape - this is faster
1126 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
1127 $data = self::pollNoscrape($noscrape_url, $data);
1129 if (isset($data["notify"])
1130 && isset($data["confirm"])
1131 && isset($data["request"])
1132 && isset($data["poll"])
1133 && isset($data["name"])
1134 && isset($data["photo"])
1139 $data = self::pollHcard($hcard_url, $data, true);
1145 * @brief Poll the hcard page (Diaspora and Friendica specific)
1147 * @param string $hcard_url Link to the hcard page
1148 * @param array $data The already fetched data
1149 * @param boolean $dfrn Poll DFRN specific data
1151 * @return array hcard data
1152 * @throws HTTPException\InternalServerErrorException
1154 private static function pollHcard($hcard_url, $data, $dfrn = false)
1156 $curlResult = Network::curl($hcard_url);
1157 if ($curlResult->isTimeout()) {
1158 self::$istimeout = true;
1161 $content = $curlResult->getBody();
1166 $doc = new DOMDocument();
1167 if (!@$doc->loadHTML($content)) {
1171 $xpath = new DomXPath($doc);
1173 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
1174 if (!is_object($vcards)) {
1178 if (!isset($data["baseurl"])) {
1179 $data["baseurl"] = "";
1182 if ($vcards->length > 0) {
1183 $vcard = $vcards->item(0);
1185 // We have to discard the guid from the hcard in favour of the guid from lrdd
1186 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
1187 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
1188 if (($search->length > 0) && empty($data["guid"])) {
1189 $data["guid"] = $search->item(0)->nodeValue;
1192 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
1193 if ($search->length > 0) {
1194 $data["nick"] = $search->item(0)->nodeValue;
1197 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
1198 if ($search->length > 0) {
1199 $data["name"] = $search->item(0)->nodeValue;
1202 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
1203 if ($search->length > 0) {
1204 $data["searchable"] = $search->item(0)->nodeValue;
1207 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
1208 if ($search->length > 0) {
1209 $data["pubkey"] = $search->item(0)->nodeValue;
1210 if (strstr($data["pubkey"], 'RSA ')) {
1211 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1215 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
1216 if ($search->length > 0) {
1217 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
1222 if (!empty($vcard)) {
1223 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
1224 foreach ($photos as $photo) {
1226 foreach ($photo->attributes as $attribute) {
1227 $attr[$attribute->name] = trim($attribute->value);
1230 if (isset($attr["src"]) && isset($attr["width"])) {
1231 $avatar[$attr["width"]] = $attr["src"];
1234 // We don't have a width. So we just take everything that we got.
1235 // This is a Hubzilla workaround which doesn't send a width.
1236 if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
1237 $avatar[] = $attr["src"];
1242 if (sizeof($avatar)) {
1244 $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
1248 // Poll DFRN specific data
1249 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
1250 if ($search->length > 0) {
1251 foreach ($search as $link) {
1252 //$data["request"] = $search->item(0)->nodeValue;
1254 foreach ($link->attributes as $attribute) {
1255 $attr[$attribute->name] = trim($attribute->value);
1258 $data[substr($attr["rel"], 5)] = $attr["href"];
1262 // Older Friendica versions had used the "uid" field differently than newer versions
1263 if (!empty($data["nick"]) && !empty($data["guid"]) && ($data["nick"] == $data["guid"])) {
1264 unset($data["guid"]);
1273 * @brief Check for Diaspora contact
1275 * @param array $webfinger Webfinger data
1277 * @return array Diaspora data
1278 * @throws HTTPException\InternalServerErrorException
1280 private static function diaspora($webfinger, $data)
1284 unset($data["guid"]);
1285 unset($data["pubkey"]);
1287 // The array is reversed to take into account the order of preference for same-rel links
1288 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1289 foreach (array_reverse($webfinger["links"]) as $link) {
1290 if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1291 $hcard_url = $link["href"];
1292 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1293 $data["baseurl"] = trim($link["href"], '/');
1294 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1295 $data["guid"] = $link["href"];
1296 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1297 $data["url"] = $link["href"];
1298 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1299 $data["poll"] = $link["href"];
1300 } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1301 $data["poco"] = $link["href"];
1302 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1303 $data["notify"] = $link["href"];
1304 } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1305 $data["pubkey"] = base64_decode($link["href"]);
1307 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1308 if (strstr($data["pubkey"], 'RSA ')) {
1309 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1314 if (empty($data["url"]) || empty($hcard_url)) {
1318 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1319 foreach ($webfinger["aliases"] as $alias) {
1320 if (Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"]) && ! strstr($alias, "@")) {
1321 $data["alias"] = $alias;
1322 } elseif (substr($alias, 0, 5) == 'acct:') {
1323 $data["addr"] = substr($alias, 5);
1328 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
1329 $data["addr"] = substr($webfinger["subject"], 5);
1332 // Fetch further information from the hcard
1333 $data = self::pollHcard($hcard_url, $data);
1335 if (!empty($data["url"])
1336 && !empty($data["guid"])
1337 && !empty($data["baseurl"])
1338 && !empty($data["pubkey"])
1339 && !empty($hcard_url)
1341 $data["network"] = Protocol::DIASPORA;
1343 // The Diaspora handle must always be lowercase
1344 if (!empty($data["addr"])) {
1345 $data["addr"] = strtolower($data["addr"]);
1348 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
1349 $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
1350 $data["batch"] = $data["baseurl"] . "/receive/public";
1357 * @brief Check for OStatus contact
1359 * @param array $webfinger Webfinger data
1360 * @param bool $short Short detection mode
1362 * @return array|bool OStatus data or "false" on error or "true" on short mode
1363 * @throws HTTPException\InternalServerErrorException
1365 private static function ostatus($webfinger, $short = false, $data = [])
1367 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1368 foreach ($webfinger["aliases"] as $alias) {
1369 if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) {
1370 $data["addr"] = str_replace('acct:', '', $alias);
1375 if (!empty($webfinger["subject"]) && strstr($webfinger["subject"], "@")
1376 && !strstr(Strings::normaliseLink($webfinger["subject"]), "http://")
1378 $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1381 if (is_array($webfinger["links"])) {
1382 // The array is reversed to take into account the order of preference for same-rel links
1383 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1384 foreach (array_reverse($webfinger["links"]) as $link) {
1385 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1386 && (($link["type"] ?? "") == "text/html")
1387 && ($link["href"] != "")
1389 $data["url"] = $link["href"];
1390 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1391 $data["notify"] = $link["href"];
1392 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1393 $data["poll"] = $link["href"];
1394 } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) {
1395 $pubkey = $link["href"];
1397 if (substr($pubkey, 0, 5) === 'data:') {
1398 if (strstr($pubkey, ',')) {
1399 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1401 $pubkey = substr($pubkey, 5);
1403 } elseif (Strings::normaliseLink($pubkey) == 'http://') {
1404 $curlResult = Network::curl($pubkey);
1405 if ($curlResult->isTimeout()) {
1406 self::$istimeout = true;
1413 $pubkey = $curlResult->getBody();
1416 $key = explode(".", $pubkey);
1418 if (sizeof($key) >= 3) {
1419 $m = Strings::base64UrlDecode($key[1]);
1420 $e = Strings::base64UrlDecode($key[2]);
1421 $data["pubkey"] = Crypto::meToPem($m, $e);
1427 if (isset($data["notify"]) && isset($data["pubkey"])
1428 && isset($data["poll"])
1429 && isset($data["url"])
1431 $data["network"] = Protocol::OSTATUS;
1442 // Fetch all additional data from the feed
1443 $curlResult = Network::curl($data["poll"]);
1444 if ($curlResult->isTimeout()) {
1445 self::$istimeout = true;
1448 $feed = $curlResult->getBody();
1452 $feed_data = Feed::import($feed, $dummy1, $dummy2, $dummy3, true);
1457 if (!empty($feed_data["header"]["author-name"])) {
1458 $data["name"] = $feed_data["header"]["author-name"];
1460 if (!empty($feed_data["header"]["author-nick"])) {
1461 $data["nick"] = $feed_data["header"]["author-nick"];
1463 if (!empty($feed_data["header"]["author-avatar"])) {
1464 $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1466 if (!empty($feed_data["header"]["author-id"])) {
1467 $data["alias"] = $feed_data["header"]["author-id"];
1469 if (!empty($feed_data["header"]["author-location"])) {
1470 $data["location"] = $feed_data["header"]["author-location"];
1472 if (!empty($feed_data["header"]["author-about"])) {
1473 $data["about"] = $feed_data["header"]["author-about"];
1475 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1476 // So we take the value that we just fetched, although the other one worked as well
1477 if (!empty($feed_data["header"]["author-link"])) {
1478 $data["url"] = $feed_data["header"]["author-link"];
1481 if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
1482 $data['url'] = $data["alias"];
1483 $data["alias"] = '';
1486 /// @todo Fetch location and "about" from the feed as well
1491 * @brief Fetch data from a pump.io profile page
1493 * @param string $profile_link Link to the profile page
1495 * @return array profile data
1497 private static function pumpioProfileData($profile_link)
1499 $doc = new DOMDocument();
1500 if (!@$doc->loadHTMLFile($profile_link)) {
1504 $xpath = new DomXPath($doc);
1508 $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue;
1510 if ($data["name"] == '') {
1511 // This is ugly - but pump.io doesn't seem to know a better way for it
1512 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1513 $pos = strpos($data["name"], chr(10));
1515 $data["name"] = trim(substr($data["name"], 0, $pos));
1519 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]");
1521 if ($data["location"] == '') {
1522 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]");
1525 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]");
1527 if ($data["about"] == '') {
1528 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]");
1531 $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0);
1533 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1536 foreach ($avatar->attributes as $attribute) {
1537 if ($attribute->name == "src") {
1538 $data["photo"] = trim($attribute->value);
1547 * @brief Check for pump.io contact
1549 * @param array $webfinger Webfinger data
1552 * @return array pump.io data
1554 private static function pumpio($webfinger, $addr, $data)
1556 // The array is reversed to take into account the order of preference for same-rel links
1557 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1558 foreach (array_reverse($webfinger["links"]) as $link) {
1559 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1560 && (($link["type"] ?? "") == "text/html")
1561 && ($link["href"] != "")
1563 $data["url"] = $link["href"];
1564 } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
1565 $data["notify"] = $link["href"];
1566 } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
1567 $data["poll"] = $link["href"];
1568 } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
1569 $data["dialback"] = $link["href"];
1572 if (isset($data["poll"]) && isset($data["notify"])
1573 && isset($data["dialback"])
1574 && isset($data["url"])
1576 // by now we use these fields only for the network type detection
1577 // So we unset all data that isn't used at the moment
1578 unset($data["dialback"]);
1580 $data["network"] = Protocol::PUMPIO;
1585 $profile_data = self::pumpioProfileData($data["url"]);
1587 if (!$profile_data) {
1591 $data = array_merge($data, $profile_data);
1593 if (($addr != '') && ($data['name'] != '')) {
1594 $name = trim(str_replace($addr, '', $data['name']));
1596 $data['name'] = $name;
1604 * @brief Check for twitter contact
1606 * @param string $uri
1608 * @return array twitter data
1610 private static function twitter($uri)
1612 if (preg_match('=(.*)@twitter.com=i', $uri, $matches)) {
1613 $nick = $matches[1];
1614 } elseif (preg_match('=https?://twitter.com/(.*)=i', $uri, $matches)) {
1615 $nick = $matches[1];
1621 $data['url'] = 'https://twitter.com/' . $nick;
1622 $data['addr'] = $nick . '@twitter.com';
1623 $data['nick'] = $data['name'] = $nick;
1624 $data['network'] = Protocol::TWITTER;
1625 $data['baseurl'] = 'https://twitter.com';
1627 $curlResult = Network::curl($data['url'], false);
1628 if (!$curlResult->isSuccess()) {
1632 $body = $curlResult->getBody();
1633 $doc = new DOMDocument();
1634 @$doc->loadHTML($body);
1635 $xpath = new DOMXPath($doc);
1637 $list = $xpath->query('//img[@class]');
1638 foreach ($list as $node) {
1640 if ($node->attributes->length) {
1641 foreach ($node->attributes as $attribute) {
1642 $img_attr[$attribute->name] = $attribute->value;
1646 if (empty($img_attr['class'])) {
1650 if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) {
1651 if (!empty($img_attr['src'])) {
1652 $data['photo'] = $img_attr['src'];
1654 if (!empty($img_attr['alt'])) {
1655 $data['name'] = $img_attr['alt'];
1664 * @brief Check page for feed link
1666 * @param string $url Page link
1668 * @return string feed link
1670 private static function getFeedLink($url)
1672 $doc = new DOMDocument();
1674 if (!@$doc->loadHTMLFile($url)) {
1678 $xpath = new DomXPath($doc);
1680 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1681 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1682 if (!is_object($feeds)) {
1686 if ($feeds->length == 0) {
1692 foreach ($feeds as $feed) {
1694 foreach ($feed->attributes as $attribute) {
1695 $attr[$attribute->name] = trim($attribute->value);
1698 if (empty($feed_url) && !empty($attr['href'])) {
1699 $feed_url = $attr["href"];
1707 * @brief Check for feed contact
1709 * @param string $url Profile link
1710 * @param boolean $probe Do a probe if the page contains a feed link
1712 * @return array feed data
1713 * @throws HTTPException\InternalServerErrorException
1715 private static function feed($url, $probe = true, $data = [])
1717 $curlResult = Network::curl($url);
1718 if ($curlResult->isTimeout()) {
1719 self::$istimeout = true;
1722 $feed = $curlResult->getBody();
1723 $dummy1 = $dummy2 = $dummy3 = null;
1724 $feed_data = Feed::import($feed, $dummy1, $dummy2, $dummy3, true);
1731 $feed_url = self::getFeedLink($url);
1737 return self::feed($feed_url, false);
1740 if (!empty($feed_data["header"]["author-name"])) {
1741 $data["name"] = $feed_data["header"]["author-name"];
1744 if (!empty($feed_data["header"]["author-nick"])) {
1745 $data["nick"] = $feed_data["header"]["author-nick"];
1748 if (!empty($feed_data["header"]["author-avatar"])) {
1749 $data["photo"] = $feed_data["header"]["author-avatar"];
1752 if (!empty($feed_data["header"]["author-id"])) {
1753 $data["alias"] = $feed_data["header"]["author-id"];
1756 $data["url"] = $url;
1757 $data["poll"] = $url;
1759 if (!empty($feed_data["header"]["author-link"])) {
1760 $data["baseurl"] = $feed_data["header"]["author-link"];
1762 $data["baseurl"] = $data["url"];
1765 $data["network"] = Protocol::FEED;
1771 * @brief Check for mail contact
1773 * @param string $uri Profile link
1774 * @param integer $uid User ID
1776 * @return array mail data
1777 * @throws \Exception
1779 private static function mail($uri, $uid)
1781 if (!Network::isEmailDomainValid($uri)) {
1789 $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $uid]);
1791 $condition = ["`uid` = ? AND `server` != ''", $uid];
1792 $fields = ['pass', 'user', 'server', 'port', 'ssltype', 'mailbox'];
1793 $mailacct = DBA::selectFirst('mailacct', $fields, $condition);
1795 if (!DBA::isResult($user) || !DBA::isResult($mailacct)) {
1799 $mailbox = Email::constructMailboxName($mailacct);
1801 openssl_private_decrypt(hex2bin($mailacct['pass']), $password, $user['prvkey']);
1802 $mbox = Email::connect($mailbox, $mailacct['user'], $password);
1807 $msgs = Email::poll($mbox, $uri);
1808 Logger::log('searching '.$uri.', '.count($msgs).' messages found.', Logger::DEBUG);
1810 if (!count($msgs)) {
1814 $phost = substr($uri, strpos($uri, '@') + 1);
1817 $data["addr"] = $uri;
1818 $data["network"] = Protocol::MAIL;
1819 $data["name"] = substr($uri, 0, strpos($uri, '@'));
1820 $data["nick"] = $data["name"];
1821 $data["photo"] = Network::lookupAvatarByEmail($uri);
1822 $data["url"] = 'mailto:'.$uri;
1823 $data["notify"] = 'smtp ' . Strings::getRandomHex();
1824 $data["poll"] = 'email ' . Strings::getRandomHex();
1826 $x = Email::messageMeta($mbox, $msgs[0]);
1827 if (stristr($x[0]->from, $uri)) {
1828 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1829 } elseif (stristr($x[0]->to, $uri)) {
1830 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1833 foreach ($adr as $feadr) {
1834 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1835 &&(strcasecmp($feadr->host, $phost) == 0)
1836 && (strlen($feadr->personal))
1838 $personal = imap_mime_header_decode($feadr->personal);
1840 foreach ($personal as $perspart) {
1841 if ($perspart->charset != "default") {
1842 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1844 $data["name"] .= $perspart->text;
1848 $data["name"] = Strings::escapeTags($data["name"]);
1852 if (!empty($mbox)) {
1859 * @brief Mix two paths together to possibly fix missing parts
1861 * @param string $avatar Path to the avatar
1862 * @param string $base Another path that is hopefully complete
1864 * @return string fixed avatar path
1865 * @throws \Exception
1867 public static function fixAvatar($avatar, $base)
1869 $base_parts = parse_url($base);
1871 // Remove all parts that could create a problem
1872 unset($base_parts['path']);
1873 unset($base_parts['query']);
1874 unset($base_parts['fragment']);
1876 $avatar_parts = parse_url($avatar);
1879 $parts = array_merge($base_parts, $avatar_parts);
1881 // And put them together again
1882 $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
1883 $host = isset($parts['host']) ? $parts['host'] : '';
1884 $port = isset($parts['port']) ? ':' . $parts['port'] : '';
1885 $path = isset($parts['path']) ? $parts['path'] : '';
1886 $query = isset($parts['query']) ? '?' . $parts['query'] : '';
1887 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1889 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1891 Logger::log('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, Logger::DATA);