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");
11 require_once('include/email.php');
12 require_once('include/network.php');
17 * @brief Rearrange the array so that it always has the same order
19 * @param array $data Unordered data
21 * @return array Ordered data
23 private function rearrange_data($data) {
24 $fields = array("name", "nick", "guid", "url", "addr", "alias",
25 "photo", "community", "keywords", "location", "about",
26 "batch", "notify", "poll", "request", "confirm", "poco",
27 "priority", "network", "pubkey", "baseurl");
30 foreach ($fields AS $field)
31 if (isset($data[$field]))
32 $newdata[$field] = $data[$field];
34 $newdata[$field] = "";
36 // We don't use the "priority" field anymore and replace it with a dummy.
37 $newdata["priority"] = 0;
43 * @brief Probes for XRD data
46 * 'lrdd' => Link to LRDD endpoint
47 * 'lrdd-xml' => Link to LRDD endpoint in XML format
48 * 'lrdd-json' => Link to LRDD endpoint in JSON format
50 private function xrd($host) {
52 $ssl_url = "https://".$host."/.well-known/host-meta";
53 $url = "http://".$host."/.well-known/host-meta";
55 $xrd_timeout = Config::get('system','xrd_timeout', 20);
58 $xml = fetch_url($ssl_url, false, $redirects, $xrd_timeout, "application/xrd+xml");
59 $xrd = parse_xml_string($xml, false);
61 if (!is_object($xrd)) {
62 $xml = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
63 $xrd = parse_xml_string($xml, false);
68 $links = xml::element_to_array($xrd);
69 if (!isset($links["xrd"]["link"]))
73 foreach ($links["xrd"]["link"] AS $value => $link) {
74 if (isset($link["@attributes"]))
75 $attributes = $link["@attributes"];
76 elseif ($value == "@attributes")
81 if (($attributes["rel"] == "lrdd") AND
82 ($attributes["type"] == "application/xrd+xml"))
83 $xrd_data["lrdd-xml"] = $attributes["template"];
84 elseif (($attributes["rel"] == "lrdd") AND
85 ($attributes["type"] == "application/json"))
86 $xrd_data["lrdd-json"] = $attributes["template"];
87 elseif ($attributes["rel"] == "lrdd")
88 $xrd_data["lrdd"] = $attributes["template"];
94 * @brief Fetch information about a given uri
96 * @param string $uri Address that should be probed
97 * @param string $network Test for this specific network
98 * @param integer $uid User ID for the probe (only used for mails)
99 * @param boolean $cache Use cached values?
101 * @return array uri data
103 public static function uri($uri, $network = "", $uid = 0, $cache = true) {
106 $result = Cache::get("probe_url:".$network.":".$uri);
107 if (!is_null($result)) {
108 $result = unserialize($result);
116 $data = self::detect($uri, $network, $uid);
118 if (!isset($data["url"]))
121 if ($data["photo"] != "")
122 $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
124 $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
126 if (!isset($data["name"]))
127 $data["name"] = $data["url"];
129 if (!isset($data["nick"]))
130 $data["nick"] = strtolower($data["name"]);
132 if (!isset($data["network"]))
133 $data["network"] = NETWORK_PHANTOM;
135 $data = self::rearrange_data($data);
137 // Only store into the cache if the value seems to be valid
138 if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
139 Cache::set("probe_url:".$network.":".$uri,serialize($data), CACHE_DAY);
141 /// @todo temporary fix - we need a real contact update function that updates only changing fields
142 /// The biggest problem is the avatar picture that could have a reduced image size.
143 /// It should only be updated if the existing picture isn't existing anymore.
144 if (($data['network'] != NETWORK_FEED) AND ($mode == PROBE_NORMAL) AND
145 $data["name"] AND $data["nick"] AND $data["url"] AND $data["addr"] AND $data["poll"])
146 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
147 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
148 WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
149 dbesc($data["name"]),
150 dbesc($data["nick"]),
152 dbesc($data["addr"]),
153 dbesc($data["notify"]),
154 dbesc($data["poll"]),
155 dbesc($data["alias"]),
156 dbesc(datetime_convert()),
157 dbesc(normalise_link($data['url']))
164 * @brief Detect information about a given uri
166 * @param string $uri Address that should be probed
167 * @param string $network Test for this specific network
168 * @param integer $uid User ID for the probe (only used for mails)
170 * @return array uri data
172 private function detect($uri, $network, $uid) {
173 if (strstr($uri, '@')) {
174 // If the URI starts with "mailto:" then jum directly to the mail detection
175 if (strpos($url,'mailto:') !== false) {
176 $uri = str_replace('mailto:', '', $url);
177 return self::mail($uri, $uid);
180 if ($network == NETWORK_MAIL)
181 return self::mail($uri, $uid);
183 // Remove "acct:" from the URI
184 $uri = str_replace('acct:', '', $uri);
186 $host = substr($uri,strpos($uri, '@') + 1);
187 $nick = substr($uri,0, strpos($uri, '@'));
189 $lrdd = self::xrd($host);
191 return self::mail($uri, $uid);
195 $parts = parse_url($uri);
196 if (!isset($parts["scheme"]) OR
197 !isset($parts["host"]) OR
198 !isset($parts["path"]))
202 $host = $parts["host"];
203 $lrdd = self::xrd($host);
205 $path_parts = explode("/", trim($parts["path"], "/"));
207 while (!$lrdd AND (sizeof($path_parts) > 1)) {
208 $host .= "/".array_shift($path_parts);
209 $lrdd = self::xrd($host);
212 return self::feed($uri);
214 $nick = array_pop($path_parts);
215 $addr = $nick."@".$host;
220 /// @todo Do we need the prefix "acct:" or "acct://"?
222 foreach ($lrdd AS $key => $link) {
226 if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
229 $path = str_replace('{uri}', urlencode($addr), $link);
231 $webfinger = self::webfinger($path);
234 return self::feed($uri);
238 logger("Probing ".$uri, LOGGER_DEBUG);
240 if (in_array($network, array("", NETWORK_DFRN)))
241 $result = self::dfrn($webfinger);
242 if ((!$result AND ($network == "")) OR ($network == NETWORK_DIASPORA))
243 $result = self::diaspora($webfinger);
244 if ((!$result AND ($network == "")) OR ($network == NETWORK_OSTATUS))
245 $result = self::ostatus($webfinger);
246 if ((!$result AND ($network == "")) OR ($network == NETWORK_PUMPIO))
247 $result = self::pumpio($webfinger);
248 if ((!$result AND ($network == "")) OR ($network == NETWORK_FEED))
249 $result = self::feed($uri);
251 // We overwrite the detected nick with our try if the previois routines hadn't detected it.
252 // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
253 if (!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " ")))
254 $result["nick"] = $nick;
256 if (!isset($result["addr"]) OR ($result["addr"] == ""))
257 $result["addr"] = $addr;
260 logger($uri." is ".$result["network"], LOGGER_DEBUG);
262 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
263 $pos = strpos($result["url"], $host);
265 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
272 * @brief Do a webfinger request
274 * @param string $url Address that should be probed
276 * @return array webfinger data
278 private function webfinger($url) {
280 $xrd_timeout = Config::get('system','xrd_timeout', 20);
283 $data = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
284 $xrd = parse_xml_string($data, false);
286 if (!is_object($xrd)) {
287 // If it is not XML, maybe it is JSON
288 $webfinger = json_decode($data, true);
290 if (!isset($webfinger["links"]))
296 $xrd_arr = xml::element_to_array($xrd);
297 if (!isset($xrd_arr["xrd"]["link"]))
300 $webfinger = array();
302 if (isset($xrd_arr["xrd"]["subject"]))
303 $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
305 if (isset($xrd_arr["xrd"]["alias"]))
306 $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
308 $webfinger["links"] = array();
310 foreach ($xrd_arr["xrd"]["link"] AS $value => $data) {
311 if (isset($data["@attributes"]))
312 $attributes = $data["@attributes"];
313 elseif ($value == "@attributes")
318 $webfinger["links"][] = $attributes;
324 * @brief Poll the noscrape page (Friendica specific)
326 * @param string $noscrape Link to the noscrape page
327 * @param array $data The already fetched data
329 * @return array noscrape data
331 private function poll_noscrape($noscrape, $data) {
332 $content = fetch_url($noscrape);
336 $json = json_decode($content, true);
337 if (!is_array($json))
340 if (isset($json["fn"]))
341 $data["name"] = $json["fn"];
343 if (isset($json["addr"]))
344 $data["addr"] = $json["addr"];
346 if (isset($json["nick"]))
347 $data["nick"] = $json["nick"];
349 if (isset($json["comm"]))
350 $data["community"] = $json["comm"];
352 if (isset($json["tags"])) {
353 $keywords = implode(" ", $json["tags"]);
355 $data["keywords"] = $keywords;
358 $location = formatted_location($json);
360 $data["location"] = $location;
362 if (isset($json["about"]))
363 $data["about"] = $json["about"];
365 if (isset($json["key"]))
366 $data["pubkey"] = $json["key"];
368 if (isset($json["photo"]))
369 $data["photo"] = $json["photo"];
371 if (isset($json["dfrn-request"]))
372 $data["request"] = $json["dfrn-request"];
374 if (isset($json["dfrn-confirm"]))
375 $data["confirm"] = $json["dfrn-confirm"];
377 if (isset($json["dfrn-notify"]))
378 $data["notify"] = $json["dfrn-notify"];
380 if (isset($json["dfrn-poll"]))
381 $data["poll"] = $json["dfrn-poll"];
387 * @brief Check for valid DFRN data
389 * @param array $data DFRN data
391 * @return int Number of errors
393 public static function valid_dfrn($data) {
395 if(!isset($data['key']))
397 if(!isset($data['dfrn-request']))
399 if(!isset($data['dfrn-confirm']))
401 if(!isset($data['dfrn-notify']))
403 if(!isset($data['dfrn-poll']))
409 * @brief Fetch data from a DFRN profile page
411 * @param string $profile Link to the profile page
413 * @return array profile data
415 public static function profile($profile) {
419 // Fetch data via noscrape - this is faster
420 $noscrape = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile);
421 $data = self::poll_noscrape($noscrape, $data);
423 if (!isset($data["notify"]) OR !isset($data["confirm"]) OR
424 !isset($data["request"]) OR !isset($data["poll"]) OR
425 !isset($data["poco"]) OR !isset($data["name"]) OR
426 !isset($data["photo"]))
427 $data = self::poll_hcard($profile, $data, true);
429 $prof_data = array();
430 $prof_data["addr"] = $data["addr"];
431 $prof_data["nick"] = $data["nick"];
432 $prof_data["dfrn-request"] = $data["request"];
433 $prof_data["dfrn-confirm"] = $data["confirm"];
434 $prof_data["dfrn-notify"] = $data["notify"];
435 $prof_data["dfrn-poll"] = $data["poll"];
436 $prof_data["dfrn-poco"] = $data["poco"];
437 $prof_data["photo"] = $data["photo"];
438 $prof_data["fn"] = $data["name"];
439 $prof_data["key"] = $data["pubkey"];
445 * @brief Check for DFRN contact
447 * @param array $webfinger Webfinger data
449 * @return array DFRN data
451 private function dfrn($webfinger) {
455 foreach ($webfinger["links"] AS $link) {
456 if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != ""))
457 $data["network"] = NETWORK_DFRN;
458 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
459 $data["poll"] = $link["href"];
460 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
461 ($link["type"] == "text/html") AND ($link["href"] != ""))
462 $data["url"] = $link["href"];
463 elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
464 $hcard = $link["href"];
465 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
466 $data["poco"] = $link["href"];
467 elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != ""))
468 $data["photo"] = $link["href"];
470 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
471 $data["baseurl"] = trim($link["href"], '/');
472 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
473 $data["guid"] = $link["href"];
474 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
475 $data["pubkey"] = base64_decode($link["href"]);
477 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
478 if (strstr($data["pubkey"], 'RSA '))
479 $data["pubkey"] = rsatopem($data["pubkey"]);
483 if (!isset($data["network"]) OR ($hcard == ""))
486 // Fetch data via noscrape - this is faster
487 $noscrape = str_replace("/hcard/", "/noscrape/", $hcard);
488 $data = self::poll_noscrape($noscrape, $data);
490 if (isset($data["notify"]) AND isset($data["confirm"]) AND isset($data["request"]) AND
491 isset($data["poll"]) AND isset($data["name"]) AND isset($data["photo"]))
494 $data = self::poll_hcard($hcard, $data, true);
500 * @brief Poll the hcard page (Diaspora and Friendica specific)
502 * @param string $hcard Link to the hcard page
503 * @param array $data The already fetched data
504 * @param boolean $dfrn Poll DFRN specific data
506 * @return array hcard data
508 private function poll_hcard($hcard, $data, $dfrn = false) {
510 $doc = new DOMDocument();
511 if (!@$doc->loadHTMLFile($hcard))
514 $xpath = new DomXPath($doc);
516 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
517 if (!is_object($vcards))
520 if ($vcards->length == 0)
523 $vcard = $vcards->item(0);
525 // We have to discard the guid from the hcard in favour of the guid from lrdd
526 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
527 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
528 if (($search->length > 0) AND ($data["guid"] == ""))
529 $data["guid"] = $search->item(0)->nodeValue;
531 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
532 if ($search->length > 0)
533 $data["nick"] = $search->item(0)->nodeValue;
535 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
536 if ($search->length > 0)
537 $data["name"] = $search->item(0)->nodeValue;
539 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
540 if ($search->length > 0)
541 $data["searchable"] = $search->item(0)->nodeValue;
543 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
544 if ($search->length > 0) {
545 $data["pubkey"] = $search->item(0)->nodeValue;
546 if (strstr($data["pubkey"], 'RSA '))
547 $data["pubkey"] = rsatopem($data["pubkey"]);
550 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
551 if ($search->length > 0)
552 $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
555 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
556 foreach ($photos AS $photo) {
558 foreach ($photo->attributes as $attribute)
559 $attr[$attribute->name] = trim($attribute->value);
561 if (isset($attr["src"]) AND isset($attr["width"]))
562 $avatar[$attr["width"]] = $attr["src"];
565 if (sizeof($avatar)) {
567 $data["photo"] = array_pop($avatar);
571 // Poll DFRN specific data
572 $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
573 if ($search->length > 0) {
574 foreach ($search AS $link) {
575 //$data["request"] = $search->item(0)->nodeValue;
577 foreach ($link->attributes as $attribute)
578 $attr[$attribute->name] = trim($attribute->value);
580 $data[substr($attr["rel"], 5)] = $attr["href"];
584 // Older Friendica versions had used the "uid" field differently than newer versions
585 if ($data["nick"] == $data["guid"])
586 unset($data["guid"]);
594 * @brief Check for Diaspora contact
596 * @param array $webfinger Webfinger data
598 * @return array Diaspora data
600 private function diaspora($webfinger) {
604 foreach ($webfinger["links"] AS $link) {
605 if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
606 $hcard = $link["href"];
607 elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
608 $data["baseurl"] = trim($link["href"], '/');
609 elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
610 $data["guid"] = $link["href"];
611 elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
612 ($link["type"] == "text/html") AND ($link["href"] != ""))
613 $data["url"] = $link["href"];
614 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
615 $data["poll"] = $link["href"];
616 elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
617 $data["poco"] = $link["href"];
618 elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
619 $data["notify"] = $link["href"];
620 elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
621 $data["pubkey"] = base64_decode($link["href"]);
623 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
624 if (strstr($data["pubkey"], 'RSA '))
625 $data["pubkey"] = rsatopem($data["pubkey"]);
629 if (!isset($data["url"]) OR ($hcard == ""))
632 if (is_array($webfinger["aliases"]))
633 foreach ($webfinger["aliases"] AS $alias)
634 if (normalise_link($alias) != normalise_link($data["url"]) AND !strstr($alias, "@"))
635 $data["alias"] = $alias;
637 // Fetch further information from the hcard
638 $data = self::poll_hcard($hcard, $data);
643 if (isset($data["url"]) AND isset($data["guid"]) AND isset($data["baseurl"]) AND
644 isset($data["pubkey"]) AND ($hcard != "")) {
645 $data["network"] = NETWORK_DIASPORA;
647 // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
648 $data["notify"] = $data["baseurl"]."/receive/users/".$data["guid"];
649 $data["batch"] = $data["baseurl"]."/receive/public";
657 * @brief Check for OStatus contact
659 * @param array $webfinger Webfinger data
661 * @return array OStatus data
663 private function ostatus($webfinger) {
667 foreach ($webfinger["links"] AS $link) {
668 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
669 ($link["type"] == "text/html") AND ($link["href"] != ""))
670 $data["url"] = $link["href"];
671 elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
672 $data["notify"] = $link["href"];
673 elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
674 $data["poll"] = $link["href"];
675 elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
676 $pubkey = $link["href"];
678 if (substr($pubkey, 0, 5) === 'data:') {
679 if (strstr($pubkey, ','))
680 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
682 $pubkey = substr($pubkey, 5);
684 $pubkey = fetch_url($pubkey);
686 $key = explode(".", $pubkey);
688 if (sizeof($key) >= 3) {
689 $m = base64url_decode($key[1]);
690 $e = base64url_decode($key[2]);
691 $data["pubkey"] = metopem($m,$e);
697 if (isset($data["notify"]) AND isset($data["pubkey"]) AND
698 isset($data["poll"]) AND isset($data["url"])) {
699 $data["network"] = NETWORK_OSTATUS;
703 // Fetch all additional data from the feed
704 $feed = fetch_url($data["poll"]);
705 $feed_data = feed_import($feed,$dummy1,$dummy2, $dummy3, true);
709 if ($feed_data["header"]["author-name"] != "")
710 $data["name"] = $feed_data["header"]["author-name"];
712 if ($feed_data["header"]["author-nick"] != "")
713 $data["nick"] = $feed_data["header"]["author-nick"];
715 if ($feed_data["header"]["author-avatar"] != "")
716 $data["photo"] = $feed_data["header"]["author-avatar"];
718 if ($feed_data["header"]["author-id"] != "")
719 $data["alias"] = $feed_data["header"]["author-id"];
721 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
722 // So we take the value that we just fetched, although the other one worked as well
723 if ($feed_data["header"]["author-link"] != "")
724 $data["url"] = $feed_data["header"]["author-link"];
726 /// @todo Fetch location and "about" from the feed as well
731 * @brief Fetch data from a pump.io profile page
733 * @param string $profile Link to the profile page
735 * @return array profile data
737 private function pumpio_profile_data($profile) {
739 $doc = new DOMDocument();
740 if (!@$doc->loadHTMLFile($profile))
743 $xpath = new DomXPath($doc);
747 // This is ugly - but pump.io doesn't seem to know a better way for it
748 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
749 $pos = strpos($data["name"], chr(10));
751 $data["name"] = trim(substr($data["name"], 0, $pos));
753 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
755 foreach ($avatar->attributes as $attribute)
756 if ($attribute->name == "src")
757 $data["photo"] = trim($attribute->value);
759 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
760 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
766 * @brief Check for pump.io contact
768 * @param array $webfinger Webfinger data
770 * @return array pump.io data
772 private function pumpio($webfinger) {
774 foreach ($webfinger["links"] AS $link) {
775 if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
776 ($link["type"] == "text/html") AND ($link["href"] != ""))
777 $data["url"] = $link["href"];
778 elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != ""))
779 $data["activity-inbox"] = $link["href"];
780 elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != ""))
781 $data["activity-outbox"] = $link["href"];
782 elseif (($link["rel"] == "dialback") AND ($link["href"] != ""))
783 $data["dialback"] = $link["href"];
785 if (isset($data["activity-inbox"]) AND isset($data["activity-outbox"]) AND
786 isset($data["dialback"]) AND isset($data["url"])) {
788 // by now we use these fields only for the network type detection
789 // So we unset all data that isn't used at the moment
790 unset($data["activity-inbox"]);
791 unset($data["activity-outbox"]);
792 unset($data["dialback"]);
794 $data["network"] = NETWORK_PUMPIO;
798 $profile_data = self::pumpio_profile_data($data["url"]);
803 $data = array_merge($data, $profile_data);
809 * @brief Check page for feed link
811 * @param string $url Page link
813 * @return string feed link
815 private function get_feed_link($url) {
816 $doc = new DOMDocument();
818 if (!@$doc->loadHTMLFile($url))
821 $xpath = new DomXPath($doc);
823 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
824 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
825 if (!is_object($feeds))
828 if ($feeds->length == 0)
833 foreach ($feeds AS $feed) {
835 foreach ($feed->attributes as $attribute)
836 $attr[$attribute->name] = trim($attribute->value);
839 $feed_url = $attr["href"];
846 * @brief Check for feed contact
848 * @param string $url Profile link
849 * @param boolean $probe Do a probe if the page contains a feed link
851 * @return array feed data
853 private function feed($url, $probe = true) {
854 $feed = fetch_url($url);
855 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
861 $feed_url = self::get_feed_link($url);
866 return self::feed($feed_url, false);
869 if ($feed_data["header"]["author-name"] != "")
870 $data["name"] = $feed_data["header"]["author-name"];
872 if ($feed_data["header"]["author-nick"] != "")
873 $data["nick"] = $feed_data["header"]["author-nick"];
875 if ($feed_data["header"]["author-avatar"] != "")
876 $data["photo"] = $feed_data["header"]["author-avatar"];
878 if ($feed_data["header"]["author-id"] != "")
879 $data["alias"] = $feed_data["header"]["author-id"];
882 $data["poll"] = $url;
884 if ($feed_data["header"]["author-link"] != "")
885 $data["baseurl"] = $feed_data["header"]["author-link"];
887 $data["baseurl"] = $data["url"];
889 $data["network"] = NETWORK_FEED;
895 * @brief Check for mail contact
897 * @param string $uri Profile link
898 * @param integer $uid User ID
900 * @return array mail data
902 private function mail($uri, $uid) {
904 if (!validate_email($uri))
907 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
909 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
911 if(count($x) && count($r)) {
912 $mailbox = construct_mailbox_name($r[0]);
914 openssl_private_decrypt(hex2bin($r[0]['pass']), $password,$x[0]['prvkey']);
915 $mbox = email_connect($mailbox,$r[0]['user'], $password);
920 $msgs = email_poll($mbox, $uri);
921 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
928 $data["addr"] = $uri;
929 $data["network"] = NETWORK_MAIL;
930 $data["name"] = substr($uri, 0, strpos($uri,'@'));
931 $data["nick"] = $data["name"];
932 $data["photo"] = avatar_img($uri);
934 $phost = substr($uri, strpos($uri,'@') + 1);
935 $data["url"] = 'http://'.$phost."/".$data["nick"];
936 $data["notify"] = 'smtp '.random_string();
937 $data["poll"] = 'email '.random_string();
939 $x = email_msg_meta($mbox, $msgs[0]);
940 if(stristr($x[0]->from, $uri))
941 $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
942 elseif(stristr($x[0]->to, $uri))
943 $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
945 foreach($adr as $feadr) {
946 if((strcasecmp($feadr->mailbox, $data["name"]) == 0)
947 &&(strcasecmp($feadr->host, $phost) == 0)
948 && (strlen($feadr->personal))) {
950 $personal = imap_mime_header_decode($feadr->personal);
952 foreach($personal as $perspart)
953 if ($perspart->charset != "default")
954 $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
956 $data["name"] .= $perspart->text;
958 $data["name"] = notags($data["name"]);