3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Network;
26 use Friendica\Core\Cache\Duration;
27 use Friendica\Core\Logger;
28 use Friendica\Core\Protocol;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Profile;
33 use Friendica\Protocol\ActivityNamespace;
34 use Friendica\Protocol\ActivityPub;
35 use Friendica\Protocol\Email;
36 use Friendica\Protocol\Feed;
37 use Friendica\Util\Crypto;
38 use Friendica\Util\Network;
39 use Friendica\Util\Strings;
40 use Friendica\Util\XML;
43 * This class contain functions for probing URL
47 private static $baseurl;
48 private static $istimeout;
51 * Rearrange the array so that it always has the same order
53 * @param array $data Unordered data
55 * @return array Ordered data
57 private static function rearrangeData($data)
59 $fields = ["name", "nick", "guid", "url", "addr", "alias", "photo", "account-type",
60 "community", "keywords", "location", "about", "hide",
61 "batch", "notify", "poll", "request", "confirm", "poco",
62 "following", "followers", "inbox", "outbox", "sharedinbox",
63 "priority", "network", "pubkey", "baseurl"];
66 foreach ($fields as $field) {
67 if (isset($data[$field])) {
68 $newdata[$field] = $data[$field];
70 $newdata[$field] = "";
74 // We don't use the "priority" field anymore and replace it with a dummy.
75 $newdata["priority"] = 0;
81 * Check if the hostname belongs to the own server
83 * @param string $host The hostname that is to be checked
85 * @return bool Does the testes hostname belongs to the own server?
87 private static function ownHost($host)
89 $own_host = DI::baseUrl()->getHostname();
91 $parts = parse_url($host);
93 if (!isset($parts['scheme'])) {
94 $parts = parse_url('http://'.$host);
97 if (!isset($parts['host'])) {
100 return $parts['host'] == $own_host;
104 * Probes for webfinger path via "host-meta"
106 * We have to check if the servers in the future still will offer this.
107 * It seems as if it was dropped from the standard.
109 * @param string $host The host part of an url
111 * @return array with template and type of the webfinger template for JSON or XML
112 * @throws HTTPException\InternalServerErrorException
114 private static function hostMeta($host)
116 // Reset the static variable
119 $ssl_url = "https://".$host."/.well-known/host-meta";
120 $url = "http://".$host."/.well-known/host-meta";
122 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
124 Logger::log("Probing for ".$host, Logger::DEBUG);
127 $curlResult = Network::curl($ssl_url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
128 $ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
129 if ($curlResult->isSuccess()) {
130 $xml = $curlResult->getBody();
131 $xrd = XML::parseString($xml, false);
132 $host_url = 'https://'.$host;
133 } elseif ($curlResult->isTimeout()) {
134 Logger::info('Probing timeout', ['url' => $ssl_url], Logger::DEBUG);
135 self::$istimeout = true;
139 if (!is_object($xrd)) {
140 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
141 $connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
142 if ($curlResult->isTimeout()) {
143 Logger::info('Probing timeout', ['url' => $url], Logger::DEBUG);
144 self::$istimeout = true;
146 } elseif ($connection_error && $ssl_connection_error) {
147 self::$istimeout = true;
151 $xml = $curlResult->getBody();
152 $xrd = XML::parseString($xml, false);
153 $host_url = 'http://'.$host;
155 if (!is_object($xrd)) {
156 Logger::log("No xrd object found for ".$host, Logger::DEBUG);
160 $links = XML::elementToArray($xrd);
161 if (!isset($links["xrd"]["link"])) {
162 Logger::log("No xrd data found for ".$host, Logger::DEBUG);
166 $lrdd = ['application/jrd+json' => $host_url . '/.well-known/webfinger?resource={uri}'];
168 foreach ($links["xrd"]["link"] as $value => $link) {
169 if (!empty($link["@attributes"])) {
170 $attributes = $link["@attributes"];
171 } elseif ($value == "@attributes") {
177 if (!empty($attributes["rel"]) && $attributes["rel"] == "lrdd" && !empty($attributes["template"])) {
178 $type = (empty($attributes["type"]) ? '' : $attributes["type"]);
180 $lrdd[$type] = $attributes["template"];
184 self::$baseurl = $host_url;
186 Logger::log("Probing successful for ".$host, Logger::DEBUG);
192 * Perform Webfinger lookup and return DFRN data
194 * Given an email style address, perform webfinger lookup and
195 * return the resulting DFRN profile URL, or if no DFRN profile URL
196 * is located, returns an OStatus subscription template (prefixed
197 * with the string 'stat:' to identify it as on OStatus template).
198 * If this isn't an email style address just return $webbie.
199 * Return an empty string if email-style addresses but webfinger fails,
200 * or if the resultant personal XRD doesn't contain a supported
201 * subscription/friend-request attribute.
203 * amended 7/9/2011 to return an hcard which could save potentially loading
204 * a lengthy content page to scrape dfrn attributes
206 * @param string $webbie Address that should be probed
207 * @param string $hcard_url Link to the hcard - is returned by reference
209 * @return string profile link
210 * @throws HTTPException\InternalServerErrorException
212 public static function webfingerDfrn($webbie, &$hcard_url)
216 $links = self::lrdd($webbie);
217 Logger::log('webfingerDfrn: '.$webbie.':'.print_r($links, true), Logger::DATA);
218 if (!empty($links) && is_array($links)) {
219 foreach ($links as $link) {
220 if ($link['@attributes']['rel'] === ActivityNamespace::DFRN) {
221 $profile_link = $link['@attributes']['href'];
223 if (($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) && ($profile_link == "")) {
224 $profile_link = 'stat:'.$link['@attributes']['template'];
226 if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
227 $hcard_url = $link['@attributes']['href'];
231 return $profile_link;
235 * Get the link for the remote follow page for a given profile link
237 * @param sting $profile
238 * @return string Remote follow page link
240 public static function getRemoteFollowLink(string $profile)
244 $links = self::lrdd($profile);
246 if (!empty($links) && is_array($links)) {
247 foreach ($links as $link) {
248 if ($link['@attributes']['rel'] === ActivityNamespace::OSTATUSSUB) {
249 $follow_link = $link['@attributes']['template'];
257 * Check an URI for LRDD data
259 * this is a replacement for the "lrdd" function.
260 * It isn't used in this class and has some redundancies in the code.
261 * When time comes we can check the existing calls for "lrdd" if we can rework them.
263 * @param string $uri Address that should be probed
265 * @return array uri data
266 * @throws HTTPException\InternalServerErrorException
268 public static function lrdd($uri)
270 $lrdd = self::hostMeta($uri);
273 if (is_bool($lrdd)) {
278 $parts = @parse_url($uri);
279 if (!$parts || empty($parts["host"]) || empty($parts["path"])) {
283 $host = $parts["host"];
284 if (!empty($parts["port"])) {
285 $host .= ':'.$parts["port"];
288 $path_parts = explode("/", trim($parts["path"], "/"));
290 $nick = array_pop($path_parts);
293 $lrdd = self::hostMeta($host);
294 $host .= "/".array_shift($path_parts);
295 } while (!$lrdd && (sizeof($path_parts) > 0));
299 Logger::log("No lrdd data found for ".$uri, Logger::DEBUG);
303 foreach ($lrdd as $type => $template) {
308 $path = str_replace('{uri}', urlencode($uri), $template);
309 $webfinger = self::webfinger($path, $type);
311 if (!$webfinger && (strstr($uri, "@"))) {
312 $path = str_replace('{uri}', urlencode("acct:".$uri), $template);
313 $webfinger = self::webfinger($path, $type);
316 // Special treatment for Mastodon
317 // Problem is that Mastodon uses an URL format like http://domain.tld/@nick
318 // But the webfinger for this format fails.
319 if (!$webfinger && !empty($nick)) {
320 // Mastodon uses a "@" as prefix for usernames in their url format
321 $nick = ltrim($nick, '@');
323 $addr = $nick."@".$host;
325 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
326 $webfinger = self::webfinger($path, $type);
330 if (!is_array($webfinger["links"])) {
331 Logger::log("No webfinger links found for ".$uri, Logger::DEBUG);
337 foreach ($webfinger["links"] as $link) {
338 $data[] = ["@attributes" => $link];
341 if (is_array($webfinger["aliases"])) {
342 foreach ($webfinger["aliases"] as $alias) {
343 $data[] = ["@attributes" =>
353 * Fetch information (protocol endpoints and user information) about a given uri
355 * @param string $uri Address that should be probed
356 * @param string $network Test for this specific network
357 * @param integer $uid User ID for the probe (only used for mails)
358 * @param boolean $cache Use cached values?
360 * @return array uri data
361 * @throws HTTPException\InternalServerErrorException
362 * @throws \ImagickException
364 public static function uri($uri, $network = '', $uid = -1, $cache = true)
367 $result = DI::cache()->get('Probe::uri:' . $network . ':' . $uri);
368 if (!is_null($result)) {
377 self::$istimeout = false;
379 if ($network != Protocol::ACTIVITYPUB) {
380 $data = self::detect($uri, $network, $uid);
385 // When the previous detection process had got a time out
386 // we could falsely detect a Friendica profile as AP profile.
387 if (!self::$istimeout) {
388 $ap_profile = ActivityPub::probeProfile($uri);
390 if (empty($data) || (!empty($ap_profile) && empty($network) && (($data['network'] ?? '') != Protocol::DFRN))) {
392 } elseif (!empty($ap_profile)) {
393 $ap_profile['batch'] = '';
394 $data = array_merge($ap_profile, $data);
397 Logger::notice('Time out detected. AP will not be probed.', ['uri' => $uri]);
400 if (!isset($data['url'])) {
404 if (!empty($data['photo']) && !empty($data['baseurl'])) {
405 $data['baseurl'] = Network::getUrlMatch(Strings::normaliseLink($data['baseurl']), Strings::normaliseLink($data['photo']));
406 } elseif (empty($data['photo'])) {
407 $data['photo'] = DI::baseUrl() . '/images/person-300.jpg';
410 if (empty($data['name'])) {
411 if (!empty($data['nick'])) {
412 $data['name'] = $data['nick'];
415 if (empty($data['name'])) {
416 $data['name'] = $data['url'];
420 if (empty($data['nick'])) {
421 $data['nick'] = strtolower($data['name']);
423 if (strpos($data['nick'], ' ')) {
424 $data['nick'] = trim(substr($data['nick'], 0, strpos($data['nick'], ' ')));
428 if (!empty(self::$baseurl)) {
429 $data['baseurl'] = self::$baseurl;
432 if (empty($data['network'])) {
433 $data['network'] = Protocol::PHANTOM;
436 // Ensure that local connections always are DFRN
437 if (($network == '') && ($data['network'] != Protocol::PHANTOM) && (self::ownHost($data['baseurl'] ?? '') || self::ownHost($data['url']))) {
438 $data['network'] = Protocol::DFRN;
441 if (!isset($data['hide']) && in_array($data['network'], Protocol::FEDERATED)) {
442 $data['hide'] = self::getHideStatus($data['url']);
445 $data = self::rearrangeData($data);
447 // Only store into the cache if the value seems to be valid
448 if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
449 DI::cache()->set('Probe::uri:' . $network . ':' . $uri, $data, Duration::DAY);
457 * Fetches the "hide" status from the profile
459 * @param string $url URL of the profile
461 * @return boolean "hide" status
463 private static function getHideStatus($url)
465 $curlResult = Network::curl($url);
466 if (!$curlResult->isSuccess()) {
470 // If the file is too large then exit
471 if (($curlResult->getInfo()['download_content_length'] ?? 0) > 1000000) {
475 // If it isn't a HTML file then exit
476 if (($curlResult->getContentType() != '') && !strstr(strtolower($curlResult->getContentType()), 'html')) {
480 $body = $curlResult->getBody();
482 $doc = new DOMDocument();
483 @$doc->loadHTML($body);
485 $xpath = new DOMXPath($doc);
487 $list = $xpath->query('//meta[@name]');
488 foreach ($list as $node) {
490 if ($node->attributes->length) {
491 foreach ($node->attributes as $attribute) {
492 $meta_tag[$attribute->name] = $attribute->value;
496 if (empty($meta_tag['content'])) {
500 $content = strtolower(trim($meta_tag['content']));
502 switch (strtolower(trim($meta_tag['name']))) {
503 case 'dfrn-global-visibility':
504 if ($content == 'false') {
509 if (strpos($content, 'noindex') !== false) {
520 * Checks if a profile url should be OStatus but only provides partial information
522 * @param array $webfinger Webfinger data
523 * @param string $lrdd Path template for webfinger request
524 * @param string $type type
526 * @return array fixed webfinger data
527 * @throws HTTPException\InternalServerErrorException
529 private static function fixOStatus($webfinger, $lrdd, $type)
531 if (empty($webfinger['links']) || empty($webfinger['subject'])) {
538 foreach ($webfinger['links'] as $link) {
539 if ($link['rel'] == ActivityNamespace::OSTATUSSUB) {
542 if ($link['rel'] == 'magic-public-key') {
547 if (!$is_ostatus || $has_key) {
551 $url = Network::switchScheme($webfinger['subject']);
552 $path = str_replace('{uri}', urlencode($url), $lrdd);
553 $webfinger2 = self::webfinger($path, $type);
555 // Is the new webfinger detectable as OStatus?
556 if (self::ostatus($webfinger2, true)) {
557 $webfinger = $webfinger2;
564 * Fetch information (protocol endpoints and user information) about a given uri
566 * This function is only called by the "uri" function that adds caching and rearranging of data.
568 * @param string $uri Address that should be probed
569 * @param string $network Test for this specific network
570 * @param integer $uid User ID for the probe (only used for mails)
572 * @return array uri data
573 * @throws HTTPException\InternalServerErrorException
575 private static function detect($uri, $network, $uid)
577 $parts = parse_url($uri);
579 if (!empty($parts["scheme"]) && !empty($parts["host"])) {
580 $host = $parts["host"];
581 if (!empty($parts["port"])) {
582 $host .= ':'.$parts["port"];
585 if ($host == 'twitter.com') {
586 return self::twitter($uri);
588 $lrdd = self::hostMeta($host);
590 if (is_bool($lrdd)) {
594 $path_parts = explode("/", trim($parts['path'] ?? '', "/"));
596 while (!$lrdd && (sizeof($path_parts) > 1)) {
597 $host .= "/".array_shift($path_parts);
598 $lrdd = self::hostMeta($host);
601 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
602 return self::feed($uri);
604 $nick = array_pop($path_parts);
606 // Mastodon uses a "@" as prefix for usernames in their url format
607 $nick = ltrim($nick, '@');
609 $addr = $nick."@".$host;
610 } elseif (strstr($uri, '@')) {
611 // If the URI starts with "mailto:" then jump directly to the mail detection
612 if (strpos($uri, 'mailto:') !== false) {
613 $uri = str_replace('mailto:', '', $uri);
614 return self::mail($uri, $uid);
617 if ($network == Protocol::MAIL) {
618 return self::mail($uri, $uid);
620 // Remove "acct:" from the URI
621 $uri = str_replace('acct:', '', $uri);
623 $host = substr($uri, strpos($uri, '@') + 1);
624 $nick = substr($uri, 0, strpos($uri, '@'));
626 if (strpos($uri, '@twitter.com')) {
627 return self::twitter($uri);
629 $lrdd = self::hostMeta($host);
631 if (is_bool($lrdd)) {
636 Logger::log('No XRD data was found for '.$uri, Logger::DEBUG);
637 return self::mail($uri, $uid);
641 Logger::log("Uri ".$uri." was not detectable", Logger::DEBUG);
647 /// @todo Do we need the prefix "acct:" or "acct://"?
649 foreach ($lrdd as $type => $template) {
654 // At first try it with the given uri
655 $path = str_replace('{uri}', urlencode($uri), $template);
656 $webfinger = self::webfinger($path, $type);
658 // Fix possible problems with GNU Social probing to wrong scheme
659 $webfinger = self::fixOStatus($webfinger, $template, $type);
661 // We cannot be sure that the detected address was correct, so we don't use the values
662 if ($webfinger && ($uri != $addr)) {
667 // Try webfinger with the address (user@domain.tld)
669 $path = str_replace('{uri}', urlencode($addr), $template);
670 $webfinger = self::webfinger($path, $type);
673 // Mastodon needs to have it with "acct:"
675 $path = str_replace('{uri}', urlencode("acct:".$addr), $template);
676 $webfinger = self::webfinger($path, $type);
681 return self::feed($uri);
686 Logger::log("Probing ".$uri, Logger::DEBUG);
688 if (in_array($network, ["", Protocol::DFRN])) {
689 $result = self::dfrn($webfinger);
691 if ((!$result && ($network == "")) || ($network == Protocol::DIASPORA)) {
692 $result = self::diaspora($webfinger);
694 if ((!$result && ($network == "")) || ($network == Protocol::OSTATUS)) {
695 $result = self::ostatus($webfinger);
697 if (in_array($network, ['', Protocol::ZOT])) {
698 $result = self::zot($webfinger, $result);
700 if ((!$result && ($network == "")) || ($network == Protocol::PUMPIO)) {
701 $result = self::pumpio($webfinger, $addr);
703 if ((!$result && ($network == "")) || ($network == Protocol::FEED)) {
704 $result = self::feed($uri);
706 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
707 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
708 if ((empty($result["nick"]) || (strstr($result["nick"], " "))) && ($nick != "")) {
709 $result["nick"] = $nick;
712 if (empty($result["addr"]) && ($addr != "")) {
713 $result["addr"] = $addr;
717 if (empty($result["network"])) {
718 $result["network"] = Protocol::PHANTOM;
721 if (empty($result["url"])) {
722 $result["url"] = $uri;
725 Logger::log($uri." is ".$result["network"], Logger::DEBUG);
727 if (empty($result["baseurl"]) && ($result["network"] != Protocol::PHANTOM)) {
728 $pos = strpos($result["url"], $host);
730 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
737 * Check for Zot contact
739 * @param array $webfinger Webfinger data
740 * @param array $data previously probed data
742 * @return array Zot data
743 * @throws HTTPException\InternalServerErrorException
745 private static function zot($webfinger, $data)
747 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
748 foreach ($webfinger["aliases"] as $alias) {
749 if (substr($alias, 0, 5) == 'acct:') {
750 $data["addr"] = substr($alias, 5);
755 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
756 $data["addr"] = substr($webfinger["subject"], 5);
760 foreach ($webfinger['links'] as $link) {
761 if (($link['rel'] == 'http://purl.org/zot/protocol') && !empty($link['href'])) {
762 $zot_url = $link['href'];
766 if (empty($zot_url) && !empty($data['addr']) && !empty(self::$baseurl)) {
767 $condition = ['nurl' => Strings::normaliseLink(self::$baseurl), 'platform' => ['hubzilla']];
768 if (!DBA::exists('gserver', $condition)) {
771 $zot_url = self::$baseurl . '/.well-known/zot-info?address=' . $data['addr'];
774 if (empty($zot_url)) {
778 $data = self::pollZot($zot_url, $data);
780 if (!empty($data['url']) && !empty($webfinger['aliases']) && is_array($webfinger['aliases'])) {
781 foreach ($webfinger['aliases'] as $alias) {
782 if (!strstr($alias, '@') && Strings::normaliseLink($alias) != Strings::normaliseLink($data['url'])) {
783 $data['alias'] = $alias;
791 public static function pollZot($url, $data)
793 $curlResult = Network::curl($url);
794 if ($curlResult->isTimeout()) {
797 $content = $curlResult->getBody();
802 $json = json_decode($content, true);
803 if (!is_array($json)) {
807 if (empty($data['network'])) {
808 if (!empty($json['protocols']) && in_array('zot', $json['protocols'])) {
809 $data['network'] = Protocol::ZOT;
810 } elseif (!isset($json['protocols'])) {
811 $data['network'] = Protocol::ZOT;
815 if (!empty($json['guid']) && empty($data['guid'])) {
816 $data['guid'] = $json['guid'];
818 if (!empty($json['key']) && empty($data['pubkey'])) {
819 $data['pubkey'] = $json['key'];
821 if (!empty($json['name'])) {
822 $data['name'] = $json['name'];
824 if (!empty($json['photo'])) {
825 $data['photo'] = $json['photo'];
826 if (!empty($json['photo_updated'])) {
827 $data['photo'] .= '?rev=' . urlencode($json['photo_updated']);
830 if (!empty($json['address'])) {
831 $data['addr'] = $json['address'];
833 if (!empty($json['url'])) {
834 $data['url'] = $json['url'];
836 if (!empty($json['connections_url'])) {
837 $data['poco'] = $json['connections_url'];
839 if (isset($json['searchable'])) {
840 $data['hide'] = !$json['searchable'];
842 if (!empty($json['public_forum'])) {
843 $data['community'] = $json['public_forum'];
844 $data['account-type'] = Contact::PAGE_COMMUNITY;
847 if (!empty($json['profile'])) {
848 $profile = $json['profile'];
849 if (!empty($profile['description'])) {
850 $data['about'] = $profile['description'];
852 if (!empty($profile['keywords'])) {
853 $keywords = implode(', ', $profile['keywords']);
854 if (!empty($keywords)) {
855 $data['keywords'] = $keywords;
860 if (!empty($profile['region'])) {
861 $loc['region'] = $profile['region'];
863 if (!empty($profile['country'])) {
864 $loc['country-name'] = $profile['country'];
866 $location = Profile::formatLocation($loc);
867 if (!empty($location)) {
868 $data['location'] = $location;
876 * Perform a webfinger request.
878 * For details see RFC 7033: <https://tools.ietf.org/html/rfc7033>
880 * @param string $url Address that should be probed
881 * @param string $type type
883 * @return array webfinger data
884 * @throws HTTPException\InternalServerErrorException
886 private static function webfinger($url, $type)
888 $xrd_timeout = DI::config()->get('system', 'xrd_timeout', 20);
890 $curlResult = Network::curl($url, false, ['timeout' => $xrd_timeout, 'accept_content' => $type]);
891 if ($curlResult->isTimeout()) {
892 self::$istimeout = true;
895 $data = $curlResult->getBody();
897 $webfinger = json_decode($data, true);
898 if (is_array($webfinger)) {
899 if (!isset($webfinger["links"])) {
900 Logger::log("No json webfinger links for ".$url, Logger::DEBUG);
906 // If it is not JSON, maybe it is XML
907 $xrd = XML::parseString($data, false);
908 if (!is_object($xrd)) {
909 Logger::log("No webfinger data retrievable for ".$url, Logger::DEBUG);
913 $xrd_arr = XML::elementToArray($xrd);
914 if (!isset($xrd_arr["xrd"]["link"])) {
915 Logger::log("No XML webfinger links for ".$url, Logger::DEBUG);
921 if (!empty($xrd_arr["xrd"]["subject"])) {
922 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
925 if (!empty($xrd_arr["xrd"]["alias"])) {
926 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
929 $webfinger["links"] = [];
931 foreach ($xrd_arr["xrd"]["link"] as $value => $data) {
932 if (!empty($data["@attributes"])) {
933 $attributes = $data["@attributes"];
934 } elseif ($value == "@attributes") {
940 $webfinger["links"][] = $attributes;
946 * Poll the Friendica specific noscrape page.
948 * "noscrape" is a faster alternative to fetch the data from the hcard.
949 * This functionality was originally created for the directory.
951 * @param string $noscrape_url Link to the noscrape page
952 * @param array $data The already fetched data
954 * @return array noscrape data
955 * @throws HTTPException\InternalServerErrorException
957 private static function pollNoscrape($noscrape_url, $data)
959 $curlResult = Network::curl($noscrape_url);
960 if ($curlResult->isTimeout()) {
961 self::$istimeout = true;
964 $content = $curlResult->getBody();
966 Logger::log("Empty body for ".$noscrape_url, Logger::DEBUG);
970 $json = json_decode($content, true);
971 if (!is_array($json)) {
972 Logger::log("No json data for ".$noscrape_url, Logger::DEBUG);
976 if (!empty($json["fn"])) {
977 $data["name"] = $json["fn"];
980 if (!empty($json["addr"])) {
981 $data["addr"] = $json["addr"];
984 if (!empty($json["nick"])) {
985 $data["nick"] = $json["nick"];
988 if (!empty($json["guid"])) {
989 $data["guid"] = $json["guid"];
992 if (!empty($json["comm"])) {
993 $data["community"] = $json["comm"];
996 if (!empty($json["tags"])) {
997 $keywords = implode(", ", $json["tags"]);
998 if ($keywords != "") {
999 $data["keywords"] = $keywords;
1003 $location = Profile::formatLocation($json);
1005 $data["location"] = $location;
1008 if (!empty($json["about"])) {
1009 $data["about"] = $json["about"];
1012 if (!empty($json["key"])) {
1013 $data["pubkey"] = $json["key"];
1016 if (!empty($json["photo"])) {
1017 $data["photo"] = $json["photo"];
1020 if (!empty($json["dfrn-request"])) {
1021 $data["request"] = $json["dfrn-request"];
1024 if (!empty($json["dfrn-confirm"])) {
1025 $data["confirm"] = $json["dfrn-confirm"];
1028 if (!empty($json["dfrn-notify"])) {
1029 $data["notify"] = $json["dfrn-notify"];
1032 if (!empty($json["dfrn-poll"])) {
1033 $data["poll"] = $json["dfrn-poll"];
1036 if (isset($json["hide"])) {
1037 $data["hide"] = (bool)$json["hide"];
1039 $data["hide"] = false;
1046 * Check for valid DFRN data
1048 * @param array $data DFRN data
1050 * @return int Number of errors
1052 public static function validDfrn($data)
1055 if (!isset($data['key'])) {
1058 if (!isset($data['dfrn-request'])) {
1061 if (!isset($data['dfrn-confirm'])) {
1064 if (!isset($data['dfrn-notify'])) {
1067 if (!isset($data['dfrn-poll'])) {
1074 * Fetch data from a DFRN profile page and via "noscrape"
1076 * @param string $profile_link Link to the profile page
1078 * @return array profile data
1079 * @throws HTTPException\InternalServerErrorException
1080 * @throws \ImagickException
1082 public static function profile($profile_link)
1086 Logger::log("Check profile ".$profile_link, Logger::DEBUG);
1088 // Fetch data via noscrape - this is faster
1089 $noscrape_url = str_replace(["/hcard/", "/profile/"], "/noscrape/", $profile_link);
1090 $data = self::pollNoscrape($noscrape_url, $data);
1092 if (!isset($data["notify"])
1093 || !isset($data["confirm"])
1094 || !isset($data["request"])
1095 || !isset($data["poll"])
1096 || !isset($data["name"])
1097 || !isset($data["photo"])
1099 $data = self::pollHcard($profile_link, $data, true);
1104 if (empty($data["addr"]) || empty($data["nick"])) {
1105 $probe_data = self::uri($profile_link);
1106 $data["addr"] = ($data["addr"] ?? '') ?: $probe_data["addr"];
1107 $data["nick"] = ($data["nick"] ?? '') ?: $probe_data["nick"];
1110 $prof_data["addr"] = $data["addr"];
1111 $prof_data["nick"] = $data["nick"];
1112 $prof_data["dfrn-request"] = $data['request'] ?? null;
1113 $prof_data["dfrn-confirm"] = $data['confirm'] ?? null;
1114 $prof_data["dfrn-notify"] = $data['notify'] ?? null;
1115 $prof_data["dfrn-poll"] = $data['poll'] ?? null;
1116 $prof_data["photo"] = $data['photo'] ?? null;
1117 $prof_data["fn"] = $data['name'] ?? null;
1118 $prof_data["key"] = $data['pubkey'] ?? null;
1120 Logger::log("Result for profile ".$profile_link.": ".print_r($prof_data, true), Logger::DEBUG);
1126 * Check for DFRN contact
1128 * @param array $webfinger Webfinger data
1130 * @return array DFRN data
1131 * @throws HTTPException\InternalServerErrorException
1133 private static function dfrn($webfinger)
1137 // The array is reversed to take into account the order of preference for same-rel links
1138 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1139 foreach (array_reverse($webfinger["links"]) as $link) {
1140 if (($link["rel"] == ActivityNamespace::DFRN) && !empty($link["href"])) {
1141 $data["network"] = Protocol::DFRN;
1142 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1143 $data["poll"] = $link["href"];
1144 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1145 $data["url"] = $link["href"];
1146 } elseif (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1147 $hcard_url = $link["href"];
1148 } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1149 $data["poco"] = $link["href"];
1150 } elseif (($link["rel"] == "http://webfinger.net/rel/avatar") && !empty($link["href"])) {
1151 $data["photo"] = $link["href"];
1152 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1153 $data["baseurl"] = trim($link["href"], '/');
1154 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1155 $data["guid"] = $link["href"];
1156 } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1157 $data["pubkey"] = base64_decode($link["href"]);
1159 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1160 if (strstr($data["pubkey"], 'RSA ')) {
1161 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1166 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1167 foreach ($webfinger["aliases"] as $alias) {
1168 if (empty($data["url"]) && !strstr($alias, "@")) {
1169 $data["url"] = $alias;
1170 } elseif (!strstr($alias, "@") && Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"])) {
1171 $data["alias"] = $alias;
1172 } elseif (substr($alias, 0, 5) == 'acct:') {
1173 $data["addr"] = substr($alias, 5);
1178 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == "acct:")) {
1179 $data["addr"] = substr($webfinger["subject"], 5);
1182 if (!isset($data["network"]) || ($hcard_url == "")) {
1186 // Fetch data via noscrape - this is faster
1187 $noscrape_url = str_replace("/hcard/", "/noscrape/", $hcard_url);
1188 $data = self::pollNoscrape($noscrape_url, $data);
1190 if (isset($data["notify"])
1191 && isset($data["confirm"])
1192 && isset($data["request"])
1193 && isset($data["poll"])
1194 && isset($data["name"])
1195 && isset($data["photo"])
1200 $data = self::pollHcard($hcard_url, $data, true);
1206 * Poll the hcard page (Diaspora and Friendica specific)
1208 * @param string $hcard_url Link to the hcard page
1209 * @param array $data The already fetched data
1210 * @param boolean $dfrn Poll DFRN specific data
1212 * @return array hcard data
1213 * @throws HTTPException\InternalServerErrorException
1215 private static function pollHcard($hcard_url, $data, $dfrn = false)
1217 $curlResult = Network::curl($hcard_url);
1218 if ($curlResult->isTimeout()) {
1219 self::$istimeout = true;
1222 $content = $curlResult->getBody();
1227 $doc = new DOMDocument();
1228 if (!@$doc->loadHTML($content)) {
1232 $xpath = new DomXPath($doc);
1234 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
1235 if (!is_object($vcards)) {
1239 if (!isset($data["baseurl"])) {
1240 $data["baseurl"] = "";
1243 if ($vcards->length > 0) {
1244 $vcard = $vcards->item(0);
1246 // We have to discard the guid from the hcard in favour of the guid from lrdd
1247 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
1248 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
1249 if (($search->length > 0) && empty($data["guid"])) {
1250 $data["guid"] = $search->item(0)->nodeValue;
1253 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
1254 if ($search->length > 0) {
1255 $data["nick"] = $search->item(0)->nodeValue;
1258 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
1259 if ($search->length > 0) {
1260 $data["name"] = $search->item(0)->nodeValue;
1263 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
1264 if ($search->length > 0) {
1265 $data["searchable"] = $search->item(0)->nodeValue;
1268 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
1269 if ($search->length > 0) {
1270 $data["pubkey"] = $search->item(0)->nodeValue;
1271 if (strstr($data["pubkey"], 'RSA ')) {
1272 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1276 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
1277 if ($search->length > 0) {
1278 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
1283 if (!empty($vcard)) {
1284 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
1285 foreach ($photos as $photo) {
1287 foreach ($photo->attributes as $attribute) {
1288 $attr[$attribute->name] = trim($attribute->value);
1291 if (isset($attr["src"]) && isset($attr["width"])) {
1292 $avatar[$attr["width"]] = $attr["src"];
1295 // We don't have a width. So we just take everything that we got.
1296 // This is a Hubzilla workaround which doesn't send a width.
1297 if ((sizeof($avatar) == 0) && !empty($attr["src"])) {
1298 $avatar[] = $attr["src"];
1303 if (sizeof($avatar)) {
1305 $data["photo"] = self::fixAvatar(array_pop($avatar), $data["baseurl"]);
1309 // Poll DFRN specific data
1310 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
1311 if ($search->length > 0) {
1312 foreach ($search as $link) {
1313 //$data["request"] = $search->item(0)->nodeValue;
1315 foreach ($link->attributes as $attribute) {
1316 $attr[$attribute->name] = trim($attribute->value);
1319 $data[substr($attr["rel"], 5)] = $attr["href"];
1323 // Older Friendica versions had used the "uid" field differently than newer versions
1324 if (!empty($data["nick"]) && !empty($data["guid"]) && ($data["nick"] == $data["guid"])) {
1325 unset($data["guid"]);
1334 * Check for Diaspora contact
1336 * @param array $webfinger Webfinger data
1338 * @return array Diaspora data
1339 * @throws HTTPException\InternalServerErrorException
1341 private static function diaspora($webfinger)
1346 // The array is reversed to take into account the order of preference for same-rel links
1347 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1348 foreach (array_reverse($webfinger["links"]) as $link) {
1349 if (($link["rel"] == "http://microformats.org/profile/hcard") && !empty($link["href"])) {
1350 $hcard_url = $link["href"];
1351 } elseif (($link["rel"] == "http://joindiaspora.com/seed_location") && !empty($link["href"])) {
1352 $data["baseurl"] = trim($link["href"], '/');
1353 } elseif (($link["rel"] == "http://joindiaspora.com/guid") && !empty($link["href"])) {
1354 $data["guid"] = $link["href"];
1355 } elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") && (($link["type"] ?? "") == "text/html") && !empty($link["href"])) {
1356 $data["url"] = $link["href"];
1357 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1358 $data["poll"] = $link["href"];
1359 } elseif (($link["rel"] == ActivityNamespace::POCO) && !empty($link["href"])) {
1360 $data["poco"] = $link["href"];
1361 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1362 $data["notify"] = $link["href"];
1363 } elseif (($link["rel"] == "diaspora-public-key") && !empty($link["href"])) {
1364 $data["pubkey"] = base64_decode($link["href"]);
1366 //if (strstr($data["pubkey"], 'RSA ') || ($link["type"] == "RSA"))
1367 if (strstr($data["pubkey"], 'RSA ')) {
1368 $data["pubkey"] = Crypto::rsaToPem($data["pubkey"]);
1373 if (empty($data["url"]) || empty($hcard_url)) {
1377 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1378 foreach ($webfinger["aliases"] as $alias) {
1379 if (Strings::normaliseLink($alias) != Strings::normaliseLink($data["url"]) && ! strstr($alias, "@")) {
1380 $data["alias"] = $alias;
1381 } elseif (substr($alias, 0, 5) == 'acct:') {
1382 $data["addr"] = substr($alias, 5);
1387 if (!empty($webfinger["subject"]) && (substr($webfinger["subject"], 0, 5) == 'acct:')) {
1388 $data["addr"] = substr($webfinger["subject"], 5);
1391 // Fetch further information from the hcard
1392 $data = self::pollHcard($hcard_url, $data);
1398 if (!empty($data["url"])
1399 && !empty($data["guid"])
1400 && !empty($data["baseurl"])
1401 && !empty($data["pubkey"])
1402 && !empty($hcard_url)
1404 $data["network"] = Protocol::DIASPORA;
1406 // The Diaspora handle must always be lowercase
1407 if (!empty($data["addr"])) {
1408 $data["addr"] = strtolower($data["addr"]);
1411 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
1412 $data["notify"] = $data["baseurl"] . "/receive/users/" . $data["guid"];
1413 $data["batch"] = $data["baseurl"] . "/receive/public";
1422 * Check for OStatus contact
1424 * @param array $webfinger Webfinger data
1425 * @param bool $short Short detection mode
1427 * @return array|bool OStatus data or "false" on error or "true" on short mode
1428 * @throws HTTPException\InternalServerErrorException
1430 private static function ostatus($webfinger, $short = false)
1434 if (!empty($webfinger["aliases"]) && is_array($webfinger["aliases"])) {
1435 foreach ($webfinger["aliases"] as $alias) {
1436 if (strstr($alias, "@") && !strstr(Strings::normaliseLink($alias), "http://")) {
1437 $data["addr"] = str_replace('acct:', '', $alias);
1442 if (!empty($webfinger["subject"]) && strstr($webfinger["subject"], "@")
1443 && !strstr(Strings::normaliseLink($webfinger["subject"]), "http://")
1445 $data["addr"] = str_replace('acct:', '', $webfinger["subject"]);
1448 if (is_array($webfinger["links"])) {
1449 // The array is reversed to take into account the order of preference for same-rel links
1450 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1451 foreach (array_reverse($webfinger["links"]) as $link) {
1452 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1453 && (($link["type"] ?? "") == "text/html")
1454 && ($link["href"] != "")
1456 $data["url"] = $link["href"];
1457 } elseif (($link["rel"] == "salmon") && !empty($link["href"])) {
1458 $data["notify"] = $link["href"];
1459 } elseif (($link["rel"] == ActivityNamespace::FEED) && !empty($link["href"])) {
1460 $data["poll"] = $link["href"];
1461 } elseif (($link["rel"] == "magic-public-key") && !empty($link["href"])) {
1462 $pubkey = $link["href"];
1464 if (substr($pubkey, 0, 5) === 'data:') {
1465 if (strstr($pubkey, ',')) {
1466 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
1468 $pubkey = substr($pubkey, 5);
1470 } elseif (Strings::normaliseLink($pubkey) == 'http://') {
1471 $curlResult = Network::curl($pubkey);
1472 if ($curlResult->isTimeout()) {
1473 self::$istimeout = true;
1476 $pubkey = $curlResult->getBody();
1479 $key = explode(".", $pubkey);
1481 if (sizeof($key) >= 3) {
1482 $m = Strings::base64UrlDecode($key[1]);
1483 $e = Strings::base64UrlDecode($key[2]);
1484 $data["pubkey"] = Crypto::meToPem($m, $e);
1490 if (isset($data["notify"]) && isset($data["pubkey"])
1491 && isset($data["poll"])
1492 && isset($data["url"])
1494 $data["network"] = Protocol::OSTATUS;
1503 // Fetch all additional data from the feed
1504 $curlResult = Network::curl($data["poll"]);
1505 if ($curlResult->isTimeout()) {
1506 self::$istimeout = true;
1509 $feed = $curlResult->getBody();
1510 $feed_data = Feed::import($feed);
1515 if (!empty($feed_data["header"]["author-name"])) {
1516 $data["name"] = $feed_data["header"]["author-name"];
1518 if (!empty($feed_data["header"]["author-nick"])) {
1519 $data["nick"] = $feed_data["header"]["author-nick"];
1521 if (!empty($feed_data["header"]["author-avatar"])) {
1522 $data["photo"] = self::fixAvatar($feed_data["header"]["author-avatar"], $data["url"]);
1524 if (!empty($feed_data["header"]["author-id"])) {
1525 $data["alias"] = $feed_data["header"]["author-id"];
1527 if (!empty($feed_data["header"]["author-location"])) {
1528 $data["location"] = $feed_data["header"]["author-location"];
1530 if (!empty($feed_data["header"]["author-about"])) {
1531 $data["about"] = $feed_data["header"]["author-about"];
1533 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
1534 // So we take the value that we just fetched, although the other one worked as well
1535 if (!empty($feed_data["header"]["author-link"])) {
1536 $data["url"] = $feed_data["header"]["author-link"];
1539 if (($data['poll'] == $data['url']) && ($data["alias"] != '')) {
1540 $data['url'] = $data["alias"];
1541 $data["alias"] = '';
1544 /// @todo Fetch location and "about" from the feed as well
1549 * Fetch data from a pump.io profile page
1551 * @param string $profile_link Link to the profile page
1553 * @return array profile data
1555 private static function pumpioProfileData($profile_link)
1557 $curlResult = Network::curl($profile_link);
1558 if (!$curlResult->isSuccess()) {
1562 $doc = new DOMDocument();
1563 if (!@$doc->loadHTML($curlResult->getBody())) {
1567 $xpath = new DomXPath($doc);
1571 $data["name"] = $xpath->query("//span[contains(@class, 'p-name')]")->item(0)->nodeValue;
1573 if ($data["name"] == '') {
1574 // This is ugly - but pump.io doesn't seem to know a better way for it
1575 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
1576 $pos = strpos($data["name"], chr(10));
1578 $data["name"] = trim(substr($data["name"], 0, $pos));
1582 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-locality')]");
1584 if ($data["location"] == '') {
1585 $data["location"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'location')]");
1588 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'p-note')]");
1590 if ($data["about"] == '') {
1591 $data["about"] = XML::getFirstNodeValue($xpath, "//p[contains(@class, 'summary')]");
1594 $avatar = $xpath->query("//img[contains(@class, 'u-photo')]")->item(0);
1596 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
1599 foreach ($avatar->attributes as $attribute) {
1600 if ($attribute->name == "src") {
1601 $data["photo"] = trim($attribute->value);
1610 * Check for pump.io contact
1612 * @param array $webfinger Webfinger data
1613 * @param string $addr
1614 * @return array pump.io data
1616 private static function pumpio($webfinger, $addr)
1619 // The array is reversed to take into account the order of preference for same-rel links
1620 // See: https://tools.ietf.org/html/rfc7033#section-4.4.4
1621 foreach (array_reverse($webfinger["links"]) as $link) {
1622 if (($link["rel"] == "http://webfinger.net/rel/profile-page")
1623 && (($link["type"] ?? "") == "text/html")
1624 && ($link["href"] != "")
1626 $data["url"] = $link["href"];
1627 } elseif (($link["rel"] == "activity-inbox") && ($link["href"] != "")) {
1628 $data["notify"] = $link["href"];
1629 } elseif (($link["rel"] == "activity-outbox") && ($link["href"] != "")) {
1630 $data["poll"] = $link["href"];
1631 } elseif (($link["rel"] == "dialback") && ($link["href"] != "")) {
1632 $data["dialback"] = $link["href"];
1635 if (isset($data["poll"]) && isset($data["notify"])
1636 && isset($data["dialback"])
1637 && isset($data["url"])
1639 // by now we use these fields only for the network type detection
1640 // So we unset all data that isn't used at the moment
1641 unset($data["dialback"]);
1643 $data["network"] = Protocol::PUMPIO;
1648 $profile_data = self::pumpioProfileData($data["url"]);
1650 if (!$profile_data) {
1654 $data = array_merge($data, $profile_data);
1656 if (($addr != '') && ($data['name'] != '')) {
1657 $name = trim(str_replace($addr, '', $data['name']));
1659 $data['name'] = $name;
1667 * Check for twitter contact
1669 * @param string $uri
1671 * @return array twitter data
1673 private static function twitter($uri)
1675 if (preg_match('=(.*)@twitter.com=i', $uri, $matches)) {
1676 $nick = $matches[1];
1677 } elseif (preg_match('=https?://twitter.com/(.*)=i', $uri, $matches)) {
1678 $nick = $matches[1];
1684 $data['url'] = 'https://twitter.com/' . $nick;
1685 $data['addr'] = $nick . '@twitter.com';
1686 $data['nick'] = $data['name'] = $nick;
1687 $data['network'] = Protocol::TWITTER;
1688 $data['baseurl'] = 'https://twitter.com';
1690 $curlResult = Network::curl($data['url'], false);
1691 if (!$curlResult->isSuccess()) {
1695 $body = $curlResult->getBody();
1696 $doc = new DOMDocument();
1697 @$doc->loadHTML($body);
1698 $xpath = new DOMXPath($doc);
1700 $list = $xpath->query('//img[@class]');
1701 foreach ($list as $node) {
1703 if ($node->attributes->length) {
1704 foreach ($node->attributes as $attribute) {
1705 $img_attr[$attribute->name] = $attribute->value;
1709 if (empty($img_attr['class'])) {
1713 if (strpos($img_attr['class'], 'ProfileAvatar-image') !== false) {
1714 if (!empty($img_attr['src'])) {
1715 $data['photo'] = $img_attr['src'];
1717 if (!empty($img_attr['alt'])) {
1718 $data['name'] = $img_attr['alt'];
1727 * Check page for feed link
1729 * @param string $url Page link
1731 * @return string feed link
1733 private static function getFeedLink($url)
1735 $curlResult = Network::curl($url);
1736 if (!$curlResult->isSuccess()) {
1740 $doc = new DOMDocument();
1741 if (!@$doc->loadHTML($curlResult->getBody())) {
1745 $xpath = new DomXPath($doc);
1747 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
1748 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
1749 if (!is_object($feeds)) {
1753 if ($feeds->length == 0) {
1759 foreach ($feeds as $feed) {
1761 foreach ($feed->attributes as $attribute) {
1762 $attr[$attribute->name] = trim($attribute->value);
1765 if (empty($feed_url) && !empty($attr['href'])) {
1766 $feed_url = $attr["href"];
1774 * Check for feed contact
1776 * @param string $url Profile link
1777 * @param boolean $probe Do a probe if the page contains a feed link
1779 * @return array feed data
1780 * @throws HTTPException\InternalServerErrorException
1782 private static function feed($url, $probe = true)
1784 $curlResult = Network::curl($url);
1785 if ($curlResult->isTimeout()) {
1786 self::$istimeout = true;
1789 $feed = $curlResult->getBody();
1790 $feed_data = Feed::import($feed);
1797 $feed_url = self::getFeedLink($url);
1803 return self::feed($feed_url, false);
1806 if (!empty($feed_data["header"]["author-name"])) {
1807 $data["name"] = $feed_data["header"]["author-name"];
1810 if (!empty($feed_data["header"]["author-nick"])) {
1811 $data["nick"] = $feed_data["header"]["author-nick"];
1814 if (!empty($feed_data["header"]["author-avatar"])) {
1815 $data["photo"] = $feed_data["header"]["author-avatar"];
1818 if (!empty($feed_data["header"]["author-id"])) {
1819 $data["alias"] = $feed_data["header"]["author-id"];
1822 $data["url"] = $url;
1823 $data["poll"] = $url;
1825 if (!empty($feed_data["header"]["author-link"])) {
1826 $data["baseurl"] = $feed_data["header"]["author-link"];
1828 $data["baseurl"] = $data["url"];
1831 $data["network"] = Protocol::FEED;
1837 * Check for mail contact
1839 * @param string $uri Profile link
1840 * @param integer $uid User ID
1842 * @return array mail data
1843 * @throws \Exception
1845 private static function mail($uri, $uid)
1847 if (!Network::isEmailDomainValid($uri)) {
1855 $user = DBA::selectFirst('user', ['prvkey'], ['uid' => $uid]);
1857 $condition = ["`uid` = ? AND `server` != ''", $uid];
1858 $fields = ['pass', 'user', 'server', 'port', 'ssltype', 'mailbox'];
1859 $mailacct = DBA::selectFirst('mailacct', $fields, $condition);
1861 if (!DBA::isResult($user) || !DBA::isResult($mailacct)) {
1865 $mailbox = Email::constructMailboxName($mailacct);
1867 openssl_private_decrypt(hex2bin($mailacct['pass']), $password, $user['prvkey']);
1868 $mbox = Email::connect($mailbox, $mailacct['user'], $password);
1873 $msgs = Email::poll($mbox, $uri);
1874 Logger::log('searching '.$uri.', '.count($msgs).' messages found.', Logger::DEBUG);
1876 if (!count($msgs)) {
1880 $phost = substr($uri, strpos($uri, '@') + 1);
1883 $data["addr"] = $uri;
1884 $data["network"] = Protocol::MAIL;
1885 $data["name"] = substr($uri, 0, strpos($uri, '@'));
1886 $data["nick"] = $data["name"];
1887 $data["photo"] = Network::lookupAvatarByEmail($uri);
1888 $data["url"] = 'mailto:'.$uri;
1889 $data["notify"] = 'smtp ' . Strings::getRandomHex();
1890 $data["poll"] = 'email ' . Strings::getRandomHex();
1892 $x = Email::messageMeta($mbox, $msgs[0]);
1893 if (stristr($x[0]->from, $uri)) {
1894 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
1895 } elseif (stristr($x[0]->to, $uri)) {
1896 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
1899 foreach ($adr as $feadr) {
1900 if ((strcasecmp($feadr->mailbox, $data["name"]) == 0)
1901 &&(strcasecmp($feadr->host, $phost) == 0)
1902 && (strlen($feadr->personal))
1904 $personal = imap_mime_header_decode($feadr->personal);
1906 foreach ($personal as $perspart) {
1907 if ($perspart->charset != "default") {
1908 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
1910 $data["name"] .= $perspart->text;
1914 $data["name"] = Strings::escapeTags($data["name"]);
1918 if (!empty($mbox)) {
1925 * Mix two paths together to possibly fix missing parts
1927 * @param string $avatar Path to the avatar
1928 * @param string $base Another path that is hopefully complete
1930 * @return string fixed avatar path
1931 * @throws \Exception
1933 public static function fixAvatar($avatar, $base)
1935 $base_parts = parse_url($base);
1937 // Remove all parts that could create a problem
1938 unset($base_parts['path']);
1939 unset($base_parts['query']);
1940 unset($base_parts['fragment']);
1942 $avatar_parts = parse_url($avatar);
1945 $parts = array_merge($base_parts, $avatar_parts);
1947 // And put them together again
1948 $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
1949 $host = isset($parts['host']) ? $parts['host'] : '';
1950 $port = isset($parts['port']) ? ':' . $parts['port'] : '';
1951 $path = isset($parts['path']) ? $parts['path'] : '';
1952 $query = isset($parts['query']) ? '?' . $parts['query'] : '';
1953 $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
1955 $fixed = $scheme.$host.$port.$path.$query.$fragment;
1957 Logger::log('Base: '.$base.' - Avatar: '.$avatar.' - Fixed: '.$fixed, Logger::DATA);