3 * @brief This class contain functions for probing URL
7 use \Friendica\Core\Config;
8 use \Friendica\Core\PConfig;
10 require_once("include/feed.php");
14 private function rearrange_data($data) {
15 $fields = array("name", "nick", "guid", "url", "addr", "batch",
16 "notify", "poll", "request", "confirm", "poco",
17 "photo", "priority", "network", "alias", "pubkey", "baseurl");
20 foreach ($fields AS $field)
21 if (isset($data[$field]))
22 $newdata[$field] = $data[$field];
24 $newdata[$field] = "";
26 // We don't use the "priority" field anymore and replace it with a dummy.
27 $newdata["priority"] = 0;
33 * @brief Probes for XRD data
36 * 'lrdd' => Link to LRDD endpoint
37 * 'lrdd-xml' => Link to LRDD endpoint in XML format
38 * 'lrdd-json' => Link to LRDD endpoint in JSON format
40 private function xrd($host) {
42 $ssl_url = "https://".$host."/.well-known/host-meta";
43 $url = "http://".$host."/.well-known/host-meta";
45 $xrd_timeout = Config::get('system','xrd_timeout', 20);
48 $xml = fetch_url($ssl_url, false, $redirects, $xrd_timeout, "application/xrd+xml");
49 $xrd = parse_xml_string($xml, false);
51 if (!is_object($xrd)) {
52 $xml = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
53 $xrd = parse_xml_string($xml, false);
58 $links = xml::element_to_array($xrd);
59 if (!isset($links["xrd"]["link"]))
63 foreach ($links["xrd"]["link"] AS $value => $link) {
64 if (isset($link["@attributes"]))
65 $attributes = $link["@attributes"];
66 elseif ($value == "@attributes")
71 if (($attributes["rel"] == "lrdd") AND
72 ($attributes["type"] == "application/xrd+xml"))
73 $xrd_data["lrdd-xml"] = $attributes["template"];
74 elseif (($attributes["rel"] == "lrdd") AND
75 ($attributes["type"] == "application/json"))
76 $xrd_data["lrdd-json"] = $attributes["template"];
77 elseif ($attributes["rel"] == "lrdd")
78 $xrd_data["lrdd"] = $attributes["template"];
83 public static function uri($uri) {
84 $data = self::detect($uri);
89 if (!isset($data["url"]))
92 if ($data["photo"] != "")
93 $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
95 $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
97 if (!isset($data["name"]))
98 $data["name"] = $data["url"];
100 if (!isset($data["nick"]))
101 $data["nick"] = strtolower($data["name"]);
103 if (!isset($data["network"]))
104 $data["network"] = NETWORK_PHANTOM;
106 $data = self::rearrange_data($data);
111 private function detect($uri) {
112 if (strstr($uri, '@')) {
113 // If the URI starts with "mailto:" then jum directly to the mail detection
114 if (strpos($url,'mailto:') !== false) {
115 $uri = str_replace('mailto:', '', $url);
116 return self::mail($uri);
119 // Remove "acct:" from the URI
120 $uri = str_replace('acct:', '', $uri);
122 $host = substr($uri,strpos($uri, '@') + 1);
123 $nick = substr($uri,0, strpos($uri, '@'));
125 $lrdd = self::xrd($host);
127 return self::mail($uri);
131 $parts = parse_url($uri);
132 if (!isset($parts["scheme"]) OR
133 !isset($parts["host"]) OR
134 !isset($parts["path"]))
138 $host = $parts["host"];
139 $lrdd = self::xrd($host);
141 $path_parts = explode("/", trim($parts["path"], "/"));
143 while (!$lrdd AND (sizeof($path_parts) > 1)) {
144 $host .= "/".array_shift($path_parts);
145 $lrdd = self::xrd($host);
148 return self::feed($uri);
150 $nick = array_pop($path_parts);
151 $addr = $nick."@".$host;
156 /// @todo Do we need the prefix "acct:" or "acct://"?
158 foreach ($lrdd AS $key => $link) {
162 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
165 $path = str_replace('{uri}', urlencode($addr), $link);
167 $webfinger = self::webfinger($path);
170 return self::feed($uri);
175 $result = self::dfrn($webfinger);
177 $result = self::diaspora($webfinger);
179 $result = self::ostatus($webfinger);
181 $result = self::pumpio($webfinger);
183 $result = self::feed($uri);
185 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
186 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
187 if (!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " ")))
188 $result["nick"] = $nick;
190 if (!isset($result["addr"]) OR ($result["addr"] == ""))
191 $result["addr"] = $addr;
194 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
195 $pos = strpos($result["url"], $host);
197 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
203 private function webfinger($url) {
205 $xrd_timeout = Config::get('system','xrd_timeout', 20);
208 $data = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
209 $xrd = parse_xml_string($data, false);
211 if (!is_object($xrd)) {
212 // If it is not XML, maybe it is JSON
213 $webfinger = json_decode($data, true);
215 if (!isset($webfinger["links"]))
221 $xrd_arr = xml::element_to_array($xrd);
222 if (!isset($xrd_arr["xrd"]["link"]))
225 $webfinger = array();
227 if (isset($xrd_arr["xrd"]["subject"]))
228 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
230 if (isset($xrd_arr["xrd"]["alias"]))
231 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
233 $webfinger["links"] = array();
235 foreach ($xrd_arr["xrd"]["link"] AS $value => $data) {
236 if (isset($data["@attributes"]))
237 $attributes = $data["@attributes"];
238 elseif ($value == "@attributes")
243 $webfinger["links"][] = $attributes;
248 private function dfrn($webfinger) {
252 foreach ($webfinger["links"] AS $link) {
253 if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != ""))
254 $data["network"] = NETWORK_DFRN;
255 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
256 $data["poll"] = $link["href"];
257 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
258 ($link["type"] == "text/html") AND ($link["href"] != ""))
259 $data["url"] = $link["href"];
260 elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
261 $hcard = $link["href"];
262 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
263 $data["poco"] = $link["href"];
264 elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != ""))
265 $data["photo"] = $link["href"];
267 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
268 $data["baseurl"] = trim($link["href"], '/');
269 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
270 $data["guid"] = $link["href"];
271 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
272 $data["pubkey"] = base64_decode($link["href"]);
274 if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
275 $data["pubkey"] = rsatopem($data["pubkey"]);
279 if (!isset($data["network"]) OR ($hcard == ""))
282 $data = self::poll_hcard($hcard, $data, true);
287 private function poll_hcard($hcard, $data, $dfrn = false) {
289 $doc = new DOMDocument();
290 if (!@$doc->loadHTMLFile($hcard))
293 $xpath = new DomXPath($doc);
295 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
296 if (!is_object($vcards))
299 if ($vcards->length == 0)
302 $vcard = $vcards->item(0);
304 // We have to discard the guid from the hcard in favour of the guid from lrdd
305 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
306 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
307 if (($search->length > 0) AND ($data["guid"] == ""))
308 $data["guid"] = $search->item(0)->nodeValue;
310 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
311 if ($search->length > 0)
312 $data["nick"] = $search->item(0)->nodeValue;
314 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
315 if ($search->length > 0)
316 $data["name"] = $search->item(0)->nodeValue;
318 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
319 if ($search->length > 0)
320 $data["searchable"] = $search->item(0)->nodeValue;
322 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
323 if ($search->length > 0) {
324 $data["pubkey"] = $search->item(0)->nodeValue;
325 if (strstr($data["pubkey"], 'RSA '))
326 $data["pubkey"] = rsatopem($data["pubkey"]);
329 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
330 if ($search->length > 0)
331 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
334 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
335 foreach ($photos AS $photo) {
337 foreach ($photo->attributes as $attribute)
338 $attr[$attribute->name] = trim($attribute->value);
340 if (isset($attr["src"]) AND isset($attr["width"]))
341 $avatar[$attr["width"]] = $attr["src"];
344 if (sizeof($avatar)) {
346 $data["photo"] = array_pop($avatar);
350 // Poll DFRN specific data
351 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
352 if ($search->length > 0) {
353 foreach ($search AS $link) {
354 //$data["request"] = $search->item(0)->nodeValue;
356 foreach ($link->attributes as $attribute)
357 $attr[$attribute->name] = trim($attribute->value);
359 $data[substr($attr["rel"], 5)] = $attr["href"];
363 // Older Friendica versions had used the "uid" field differently than newer versions
364 if ($data["nick"] == $data["guid"])
365 unset($data["guid"]);
372 private function diaspora($webfinger) {
376 foreach ($webfinger["links"] AS $link) {
377 if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
378 $hcard = $link["href"];
379 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
380 $data["baseurl"] = trim($link["href"], '/');
381 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
382 $data["guid"] = $link["href"];
383 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
384 ($link["type"] == "text/html") AND ($link["href"] != ""))
385 $data["url"] = $link["href"];
386 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
387 $data["poll"] = $link["href"];
388 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
389 $data["poco"] = $link["href"];
390 elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
391 $data["notify"] = $link["href"];
392 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
393 $data["pubkey"] = base64_decode($link["href"]);
395 if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
396 $data["pubkey"] = rsatopem($data["pubkey"]);
400 if (!isset($data["url"]) OR ($hcard == ""))
403 if (isset($webfinger["aliases"]))
404 foreach ($webfinger["aliases"] AS $alias)
405 if (normalise_link($alias) != normalise_link($data["url"]) AND !strstr($alias, "@"))
406 $data["alias"] = $alias;
408 // Fetch further information from the hcard
409 $data = self::poll_hcard($hcard, $data);
414 if (isset($data["url"]) AND isset($data["guid"]) AND isset($data["baseurl"]) AND
415 isset($data["pubkey"]) AND ($hcard != "")) {
416 $data["network"] = NETWORK_DIASPORA;
418 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
419 $data["notify"] = $data["baseurl"]."/receive/users/".$data["guid"];
420 $data["batch"] = $data["baseurl"]."/receive/public";
427 private function ostatus($webfinger) {
431 foreach ($webfinger["links"] AS $link) {
432 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
433 ($link["type"] == "text/html") AND ($link["href"] != ""))
434 $data["url"] = $link["href"];
435 elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
436 $data["notify"] = $link["href"];
437 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
438 $data["poll"] = $link["href"];
439 elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
440 $pubkey = $link["href"];
442 if (substr($pubkey, 0, 5) === 'data:') {
443 if (strstr($pubkey, ','))
444 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
446 $pubkey = substr($pubkey, 5);
448 $pubkey = fetch_url($pubkey);
450 $key = explode(".", $pubkey);
452 if (sizeof($key) >= 3) {
453 $m = base64url_decode($key[1]);
454 $e = base64url_decode($key[2]);
455 $data["pubkey"] = metopem($m,$e);
461 if (isset($data["notify"]) AND isset($data["pubkey"]) AND
462 isset($data["poll"]) AND isset($data["url"])) {
463 $data["network"] = NETWORK_OSTATUS;
467 // Fetch all additional data from the feed
468 $feed = fetch_url($data["poll"]);
469 $feed_data = feed_import($feed,$dummy1,$dummy2, $dummy3, true);
473 if ($feed_data["header"]["author-name"] != "")
474 $data["name"] = $feed_data["header"]["author-name"];
476 if ($feed_data["header"]["author-nick"] != "")
477 $data["nick"] = $feed_data["header"]["author-nick"];
479 if ($feed_data["header"]["author-avatar"] != "")
480 $data["photo"] = $feed_data["header"]["author-avatar"];
482 if ($feed_data["header"]["author-id"] != "")
483 $data["alias"] = $feed_data["header"]["author-id"];
485 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
486 // So we take the value that we just fetched, although the other one worked as well
487 if ($feed_data["header"]["author-link"] != "")
488 $data["url"] = $feed_data["header"]["author-link"];
490 /// @todo Fetch location and "about" from the feed as well
494 private function pumpio_profile_data($profile) {
496 $doc = new DOMDocument();
497 if (!@$doc->loadHTMLFile($profile))
500 $xpath = new DomXPath($doc);
504 // This is ugly - but pump.io doesn't seem to know a better way for it
505 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
506 $pos = strpos($data["name"], chr(10));
508 $data["name"] = trim(substr($data["name"], 0, $pos));
510 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
512 foreach ($avatar->attributes as $attribute)
513 if ($attribute->name == "src")
514 $data["photo"] = trim($attribute->value);
516 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
517 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
522 private function pumpio($webfinger) {
524 foreach ($webfinger["links"] AS $link) {
525 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
526 ($link["type"] == "text/html") AND ($link["href"] != ""))
527 $data["url"] = $link["href"];
528 elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != ""))
529 $data["activity-inbox"] = $link["href"];
530 elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != ""))
531 $data["activity-outbox"] = $link["href"];
532 elseif (($link["rel"] == "dialback") AND ($link["href"] != ""))
533 $data["dialback"] = $link["href"];
535 if (isset($data["activity-inbox"]) AND isset($data["activity-outbox"]) AND
536 isset($data["dialback"]) AND isset($data["url"])) {
538 // by now we use these fields only for the network type detection
539 // So we unset all data that isn't used at the moment
540 unset($data["activity-inbox"]);
541 unset($data["activity-outbox"]);
542 unset($data["dialback"]);
544 $data["network"] = NETWORK_PUMPIO;
548 $profile_data = self::pumpio_profile_data($data["url"]);
553 $data = array_merge($data, $profile_data);
558 private function feed($url) {
559 $feed = fetch_url($url);
560 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
565 if ($feed_data["header"]["author-name"] != "")
566 $data["name"] = $feed_data["header"]["author-name"];
568 if ($feed_data["header"]["author-nick"] != "")
569 $data["nick"] = $feed_data["header"]["author-nick"];
571 if ($feed_data["header"]["author-avatar"] != "")
572 $data["photo"] = $feed_data["header"]["author-avatar"];
574 if ($feed_data["header"]["author-id"] != "")
575 $data["alias"] = $feed_data["header"]["author-id"];
578 $data["poll"] = $url;
580 if ($feed_data["header"]["author-link"] != "")
581 $data["baseurl"] = $feed_data["header"]["author-link"];
583 $data["baseurl"] = $data["url"];
585 $data["network"] = NETWORK_FEED;
590 private function mail($uri) {
592 if (!validate_email($uri))
598 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
600 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
602 if(count($x) && count($r)) {
603 $mailbox = construct_mailbox_name($r[0]);
605 openssl_private_decrypt(hex2bin($r[0]['pass']), $password,$x[0]['prvkey']);
606 $mbox = email_connect($mailbox,$r[0]['user'], $password);
611 $msgs = email_poll($mbox, $uri);
612 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
619 $data["addr"] = $uri;
620 $data["network"] = NETWORK_MAIL;
621 $data["name"] = substr($uri, 0, strpos($uri,'@'));
622 $data["nick"] = $data["name"];
623 $data["photo"] = avatar_img($uri);
625 $phost = substr($uri, strpos($uri,'@') + 1);
626 $data["url"] = 'http://'.$phost."/".$data["nick"];
627 $data["notify"] = 'smtp '.random_string();
628 $data["poll"] = 'email '.random_string();
630 $x = email_msg_meta($mbox, $msgs[0]);
631 if(stristr($x[0]->from, $uri))
632 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
633 elseif(stristr($x[0]->to, $uri))
634 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
636 foreach($adr as $feadr) {
637 if((strcasecmp($feadr->mailbox, $data["name"]) == 0)
638 &&(strcasecmp($feadr->host, $phost) == 0)
639 && (strlen($feadr->personal))) {
641 $personal = imap_mime_header_decode($feadr->personal);
643 foreach($personal as $perspart)
644 if ($perspart->charset != "default")
645 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
647 $data["name"] .= $perspart->text;
649 $data["name"] = notags($data["name"]);