3 require_once('library/HTML5/Parser.php');
4 require_once('include/crypto.php');
5 require_once('include/feed.php');
7 if(! function_exists('scrape_dfrn')) {
8 function scrape_dfrn($url, $dont_probe = false) {
14 logger('scrape_dfrn: url=' . $url);
16 // Try to fetch the data from noscrape. This is faster than parsing the HTML
17 $noscrape = str_replace("/hcard/", "/noscrape/", $url);
18 $noscrapejson = fetch_url($noscrape);
19 $noscrapedata = array();
21 $noscrapedata = json_decode($noscrapejson, true);
23 if (is_array($noscrapedata)) {
24 if ($noscrapedata["nick"] != "")
25 return($noscrapedata);
27 unset($noscrapedata["nick"]);
29 $noscrapedata = array();
38 $probe = probe_url($url);
40 if (isset($probe["addr"]))
41 $ret["addr"] = $probe["addr"];
44 $headers = $a->get_curl_headers();
45 logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
48 $lines = explode("\n",$headers);
50 foreach($lines as $line) {
51 // don't try and run feeds through the html5 parser
52 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
58 $dom = HTML5_Parser::parse($s);
59 } catch (DOMException $e) {
60 logger('scrape_dfrn: parse error: ' . $e);
66 $items = $dom->getElementsByTagName('link');
68 // get DFRN link elements
70 foreach($items as $item) {
71 $x = $item->getAttribute('rel');
72 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
73 $ret['feed_atom'] = $item->getAttribute('href');
74 if(substr($x,0,5) == "dfrn-") {
75 $ret[$x] = $item->getAttribute('href');
78 $decoded = urldecode($item->getAttribute('href'));
79 if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
80 $ret['nick'] = $matches[1];
84 // Pull out hCard profile elements
88 $items = $dom->getElementsByTagName('*');
89 foreach($items as $item) {
90 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
91 $level2 = $item->getElementsByTagName('*');
92 foreach($level2 as $x) {
93 if(attribute_contains($x->getAttribute('class'),'uid'))
94 $ret['guid'] = $x->textContent;
95 if(attribute_contains($x->getAttribute('class'),'nickname'))
96 $ret['nickname'] = $x->textContent;
97 if(attribute_contains($x->getAttribute('class'),'fn'))
98 $ret['fn'] = $x->textContent;
99 if(attribute_contains($x->getAttribute('class'),'searchable'))
100 $ret['searchable'] = $x->textContent;
101 if(attribute_contains($x->getAttribute('class'),'key'))
102 $ret['key'] = $x->textContent;
103 if(attribute_contains($x->getAttribute('class'),'url'))
104 $ret['url'] = $x->textContent;
105 if((attribute_contains($x->getAttribute('class'),'photo'))
106 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
107 $size = intval($x->getAttribute('width'));
108 // dfrn prefers 175, so if we find this, we set largest_size so it can't be topped.
109 if(($size > $largest_photo) || ($size == 175) || (! $largest_photo)) {
110 $ret['photo'] = $x->getAttribute('src');
111 $largest_photo = (($size == 175) ? 9999 : $size);
117 return array_merge($ret, $noscrapedata);
125 if(! function_exists('validate_dfrn')) {
126 function validate_dfrn($a) {
130 if(! x($a,'dfrn-request'))
132 if(! x($a,'dfrn-confirm'))
134 if(! x($a,'dfrn-notify'))
136 if(! x($a,'dfrn-poll'))
141 if(! function_exists('scrape_meta')) {
142 function scrape_meta($url) {
148 logger('scrape_meta: url=' . $url);
150 $s = fetch_url($url);
155 $headers = $a->get_curl_headers();
156 logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
158 $lines = explode("\n",$headers);
160 foreach($lines as $line) {
161 // don't try and run feeds through the html5 parser
162 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
168 $dom = HTML5_Parser::parse($s);
169 } catch (DOMException $e) {
170 logger('scrape_meta: parse error: ' . $e);
176 $items = $dom->getElementsByTagName('meta');
178 // get DFRN link elements
180 foreach($items as $item) {
181 $x = $item->getAttribute('name');
182 if(substr($x,0,5) == "dfrn-")
183 $ret[$x] = $item->getAttribute('content');
190 if(! function_exists('scrape_vcard')) {
191 function scrape_vcard($url) {
197 logger('scrape_vcard: url=' . $url);
199 $s = fetch_url($url);
204 $headers = $a->get_curl_headers();
205 $lines = explode("\n",$headers);
207 foreach($lines as $line) {
208 // don't try and run feeds through the html5 parser
209 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
215 $dom = HTML5_Parser::parse($s);
216 } catch (DOMException $e) {
217 logger('scrape_vcard: parse error: ' . $e);
223 // Pull out hCard profile elements
227 $items = $dom->getElementsByTagName('*');
228 foreach($items as $item) {
229 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
230 $level2 = $item->getElementsByTagName('*');
231 foreach($level2 as $x) {
232 if(attribute_contains($x->getAttribute('class'),'fn'))
233 $ret['fn'] = $x->textContent;
234 if((attribute_contains($x->getAttribute('class'),'photo'))
235 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
236 $size = intval($x->getAttribute('width'));
237 if(($size > $largest_photo) || (! $largest_photo)) {
238 $ret['photo'] = $x->getAttribute('src');
239 $largest_photo = $size;
242 if((attribute_contains($x->getAttribute('class'),'nickname'))
243 || (attribute_contains($x->getAttribute('class'),'uid'))) {
244 $ret['nick'] = $x->textContent;
254 if(! function_exists('scrape_feed')) {
255 function scrape_feed($url) {
260 $cookiejar = tempnam(get_temppath(), 'cookiejar-scrape-feed-');
261 $s = fetch_url($url, false, $redirects, 0, Null, $cookiejar);
264 $headers = $a->get_curl_headers();
265 $code = $a->get_curl_code();
267 logger('scrape_feed: returns: ' . $code . ' headers=' . $headers, LOGGER_DEBUG);
270 logger('scrape_feed: no data returned for ' . $url);
275 $lines = explode("\n",$headers);
277 foreach($lines as $line) {
278 if(stristr($line,'content-type:')) {
279 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
280 $ret['feed_atom'] = $url;
283 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
284 $ret['feed_rss'] = $url;
289 // perhaps an RSS version 1 feed with a generic or incorrect content-type?
290 if(stristr($s,'</item>')) {
291 $ret['feed_rss'] = $url;
296 $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
298 $doc = new DOMDocument();
300 $xpath = new DomXPath($doc);
302 $base = $xpath->query("//base");
303 foreach ($base as $node) {
306 if ($node->attributes->length)
307 foreach ($node->attributes as $attribute)
308 $attr[$attribute->name] = $attribute->value;
310 if ($attr["href"] != "")
311 $basename = $attr["href"] ;
314 $list = $xpath->query("//link");
315 foreach ($list as $node) {
318 if ($node->attributes->length)
319 foreach ($node->attributes as $attribute)
320 $attr[$attribute->name] = $attribute->value;
322 if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/atom+xml"))
323 $ret["feed_atom"] = $attr["href"];
325 if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/rss+xml"))
326 $ret["feed_rss"] = $attr["href"];
329 // Drupal and perhaps others only provide relative URLs. Turn them into absolute.
331 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
332 $ret['feed_atom'] = $basename . $ret['feed_atom'];
333 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
334 $ret['feed_rss'] = $basename . $ret['feed_rss'];
342 * Probe a network address to discover what kind of protocols we need to communicate with it.
344 * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
351 * PROBE_DIASPORA has a bias towards returning Diaspora information
352 * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
353 * an address (such as a Friendica address) supports more than one type
359 define ( 'PROBE_NORMAL', 0);
360 define ( 'PROBE_DIASPORA', 1);
362 function probe_url($url, $mode = PROBE_NORMAL, $level = 1) {
363 require_once('include/email.php');
370 $result = Cache::get("probe_url:".$mode.":".$url);
371 if (!is_null($result)) {
372 $result = unserialize($result);
376 $original_url = $url;
383 $email_conversant = false;
384 $connectornetworks = false;
387 if (strpos($url,'twitter.com')) {
388 $connectornetworks = true;
389 $network = NETWORK_TWITTER;
392 $lastfm = ((strpos($url,'last.fm/user') !== false) ? true : false);
394 $at_addr = ((strpos($url,'@') !== false) ? true : false);
396 if((!$appnet) && (!$lastfm) && !$connectornetworks) {
398 if(strpos($url,'mailto:') !== false && $at_addr) {
399 $url = str_replace('mailto:','',$url);
405 if ((count($links) == 0) AND strstr($url, "/index.php")) {
406 $url = str_replace("/index.php", "", $url);
413 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
414 foreach($links as $link) {
415 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
416 $zot = unamp($link['@attributes']['href']);
417 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
418 $dfrn = unamp($link['@attributes']['href']);
419 if($link['@attributes']['rel'] === 'salmon')
420 $notify = unamp($link['@attributes']['href']);
421 if($link['@attributes']['rel'] === NAMESPACE_FEED)
422 $poll = unamp($link['@attributes']['href']);
423 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
424 $hcard = unamp($link['@attributes']['href']);
425 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
426 $profile = unamp($link['@attributes']['href']);
427 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
428 $poco = unamp($link['@attributes']['href']);
429 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
430 $diaspora_base = unamp($link['@attributes']['href']);
433 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
434 $diaspora_guid = unamp($link['@attributes']['href']);
437 if($link['@attributes']['rel'] === 'diaspora-public-key') {
438 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
439 if(strstr($diaspora_key,'RSA '))
440 $pubkey = rsatopem($diaspora_key);
442 $pubkey = $diaspora_key;
445 if(($link['@attributes']['rel'] === 'http://ostatus.org/schema/1.0/subscribe') AND ($mode == PROBE_NORMAL)) {
450 // Status.Net can have more than one profile URL. We need to match the profile URL
451 // to a contact on incoming messages to prevent spam, and we won't know which one
452 // to match. So in case of two, one of them is stored as an alias. Only store URL's
453 // and not webfinger user@host aliases. If they've got more than two non-email style
454 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
455 // otherwise we're screwed.
459 foreach($links as $link) {
460 if($link['@attributes']['rel'] === 'alias') {
461 if(strpos($link['@attributes']['href'],'@') === false) {
462 if(isset($profile)) {
463 $alias_url = $link['@attributes']['href'];
465 if(($alias_url !== $profile) AND ($backup_alias == "") AND
466 ($alias_url !== str_replace("/index.php", "", $profile)))
467 $backup_alias = $alias_url;
469 if(($alias_url !== $profile) AND !strstr($alias_url, "index.php") AND
470 ($alias_url !== str_replace("/index.php", "", $profile)))
474 $profile = unamp($link['@attributes']['href']);
480 $alias = $backup_alias;
482 // If the profile is different from the url then the url is abviously an alias
483 if (($alias == "") AND ($profile != "") AND !$at_addr AND (normalise_link($profile) != normalise_link($url)))
486 elseif($mode == PROBE_NORMAL) {
491 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
492 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
495 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
498 if(count($x) && count($r)) {
499 $mailbox = construct_mailbox_name($r[0]);
501 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
502 $mbox = email_connect($mailbox,$r[0]['user'],$password);
504 logger('probe_url: email_connect failed.');
508 $msgs = email_poll($mbox,$orig_url);
509 logger('probe_url: searching ' . $orig_url . ', ' . count($msgs) . ' messages found.', LOGGER_DEBUG);
512 $network = NETWORK_MAIL;
513 $name = substr($url,0,strpos($url,'@'));
514 $phost = substr($url,strpos($url,'@')+1);
515 $profile = 'http://' . $phost;
516 // fix nick character range
517 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => avatar_img($url));
518 $notify = 'smtp ' . random_string();
519 $poll = 'email ' . random_string();
521 $x = email_msg_meta($mbox,$msgs[0]);
522 if(stristr($x[0]->from,$orig_url))
523 $adr = imap_rfc822_parse_adrlist($x[0]->from,'');
524 elseif(stristr($x[0]->to,$orig_url))
525 $adr = imap_rfc822_parse_adrlist($x[0]->to,'');
527 foreach($adr as $feadr) {
528 if((strcasecmp($feadr->mailbox,$name) == 0)
529 &&(strcasecmp($feadr->host,$phost) == 0)
530 && (strlen($feadr->personal))) {
532 $personal = imap_mime_header_decode($feadr->personal);
534 foreach($personal as $perspart)
535 if ($perspart->charset != "default")
536 $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
538 $vcard['fn'] .= $perspart->text;
540 $vcard['fn'] = notags($vcard['fn']);
551 if($mode == PROBE_NORMAL) {
554 $s = fetch_url($zot);
556 $j = json_decode($s);
558 $network = NETWORK_ZOT;
560 'fn' => $j->fullname,
561 'nick' => $j->nickname,
566 $pubkey = $j->pubkey;
574 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn, true);
575 if(is_array($ret) && x($ret,'dfrn-request')) {
576 $network = NETWORK_DFRN;
577 $request = $ret['dfrn-request'];
578 $confirm = $ret['dfrn-confirm'];
579 $notify = $ret['dfrn-notify'];
580 $poll = $ret['dfrn-poll'];
583 $vcard['fn'] = $ret['fn'];
584 $vcard['nick'] = $ret['nick'];
585 $vcard['photo'] = $ret['photo'];
590 // Scrape the public key from the hcard.
591 // Diaspora will remove it from the webfinger somewhere in the future.
592 if (($hcard != "") AND ($pubkey == "")) {
593 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn, true);
594 if (isset($ret["guid"]))
595 $diaspora_guid = $ret["guid"];
596 if (isset($ret["url"]))
597 $diaspora_base = $ret["url"];
599 if (isset($ret["key"])) {
600 $hcard_key = $ret["key"];
601 if(strstr($hcard_key,'RSA '))
602 $pubkey = rsatopem($hcard_key);
604 $pubkey = $hcard_key;
607 if(($network != "") && $diaspora_base && $diaspora_guid) {
608 $diaspora_notify = $diaspora_base.'receive/users/'.$diaspora_guid;
611 if($mode == PROBE_DIASPORA || !$notify || ($notify == $diaspora_notify)) {
612 $notify = $diaspora_notify;
613 $batch = $diaspora_base . 'receive/public' ;
616 $addr = str_replace('acct:', '', $url);
619 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
621 $network = NETWORK_DIASPORA;
622 elseif($has_lrdd AND ($notify))
623 $network = NETWORK_OSTATUS;
626 $addr = str_replace('acct:', '', $url);
630 if($hcard && ! $vcard) {
631 $vcard = scrape_vcard($hcard);
633 // Google doesn't use absolute url in profile photos
635 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
636 $h = @parse_url($hcard);
638 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
641 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
644 if($diaspora && $addr) {
645 // Diaspora returns the name as the nick. As the nick will never be updated,
646 // let's use the Diaspora nickname (the first part of the handle) as the nick instead
647 $addr_parts = explode('@', $addr);
648 $vcard['nick'] = $addr_parts[0];
653 $poll = str_replace(array('www.','last.fm/'),array('','ws.audioscrobbler.com/1.0/'),$url) . '/recenttracks.rss';
654 $vcard['nick'] = basename($url);
655 $vcard['fn'] = $vcard['nick'] . t(' on Last.fm');
656 $network = NETWORK_FEED;
661 $vcard['fn'] = $vcard['nick'];
665 if(stristr($url,'tumblr.com') && (! stristr($url,'/rss'))) {
666 $poll = $url . '/rss';
668 // Will leave it to others to figure out how to grab the avatar, which is on the $url page in the open graph meta links
671 if($appnet || ! $poll)
673 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
675 if(($at_addr) && (! count($links)))
678 if ($connectornetworks)
683 $feedret = scrape_feed(($poll) ? $poll : $url);
685 logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
686 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
687 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
692 if(x($feedret,'photo') && (! x($vcard,'photo')))
693 $vcard['photo'] = $feedret['photo'];
695 $cookiejar = tempnam(get_temppath(), 'cookiejar-scrape-feed-');
696 $xml = fetch_url($poll, false, $redirects, 0, Null, $cookiejar);
699 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
702 logger("scrape_feed: XML is empty for feed ".$poll);
703 $network = NETWORK_PHANTOM;
705 $data = feed_import($xml,$dummy1,$dummy2, $dummy3, true);
707 if (!is_array($data)) {
708 logger("scrape_feed: This doesn't seem to be a feed: ".$poll);
709 $network = NETWORK_PHANTOM;
711 if (($vcard["photo"] == "") AND ($data["header"]["author-avatar"] != ""))
712 $vcard["photo"] = $data["header"]["author-avatar"];
714 if (($vcard["fn"] == "") AND ($data["header"]["author-name"] != ""))
715 $vcard["fn"] = $data["header"]["author-name"];
717 if (($vcard["nick"] == "") AND ($data["header"]["author-nick"] != ""))
718 $vcard["nick"] = $data["header"]["author-nick"];
720 if ($network == NETWORK_OSTATUS) {
721 if ($data["header"]["author-id"] != "")
722 $alias = $data["header"]["author-id"];
724 if ($data["header"]["author-link"] != "")
725 $profile = $data["header"]["author-link"];
727 } elseif(!$profile AND ($data["header"]["author-link"] != "") AND !in_array($network, array("", NETWORK_FEED)))
728 $profile = $data["header"]["author-link"];
732 // Workaround for misconfigured Friendica servers
733 if (($network == "") AND (strstr($url, "/profile/"))) {
734 $noscrape = str_replace("/profile/", "/noscrape/", $url);
735 $noscrapejson = fetch_url($noscrape);
738 $network = NETWORK_DFRN;
740 $poco = str_replace("/profile/", "/poco/", $url);
742 $noscrapedata = json_decode($noscrapejson, true);
744 if (isset($noscrapedata["addr"]))
745 $addr = $noscrapedata["addr"];
747 if (isset($noscrapedata["fn"]))
748 $vcard["fn"] = $noscrapedata["fn"];
750 if (isset($noscrapedata["key"]))
751 $pubkey = $noscrapedata["key"];
753 if (isset($noscrapedata["photo"]))
754 $vcard["photo"] = $noscrapedata["photo"];
756 if (isset($noscrapedata["dfrn-request"]))
757 $request = $noscrapedata["dfrn-request"];
759 if (isset($noscrapedata["dfrn-confirm"]))
760 $confirm = $noscrapedata["dfrn-confirm"];
762 if (isset($noscrapedata["dfrn-notify"]))
763 $notify = $noscrapedata["dfrn-notify"];
765 if (isset($noscrapedata["dfrn-poll"]))
766 $poll = $noscrapedata["dfrn-poll"];
772 $network = NETWORK_FEED;
774 if(! x($vcard,'nick')) {
775 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
776 if(strpos($vcard['nick'],' '))
777 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
784 if(! x($vcard,'photo')) {
786 $vcard['photo'] = App::get_baseurl() . '/images/person-175.jpg' ;
792 // No human could be associated with this link, use the URL as the contact name
794 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
797 if ($diaspora_base != "")
798 $baseurl = $diaspora_base;
800 if (($baseurl == "") AND ($notify != "") AND ($poll != "")) {
801 $baseurl = matching_url(normalise_link($notify), normalise_link($poll));
803 $baseurl2 = matching_url($baseurl, normalise_link($profile));
805 $baseurl = $baseurl2;
808 if (($baseurl == "") AND ($notify != ""))
809 $baseurl = matching_url(normalise_link($profile), normalise_link($notify));
811 if (($baseurl == "") AND ($poll != ""))
812 $baseurl = matching_url(normalise_link($profile), normalise_link($poll));
814 if (substr($baseurl, -10) == "/index.php")
815 $baseurl = str_replace("/index.php", "", $baseurl);
818 $network = NETWORK_PHANTOM;
820 $baseurl = rtrim($baseurl, "/");
822 if(strpos($url,'@') AND ($addr == "") AND ($network == NETWORK_DFRN))
823 $addr = str_replace('acct:', '', $url);
825 $vcard['fn'] = notags($vcard['fn']);
826 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
828 $result['name'] = $vcard['fn'];
829 $result['nick'] = $vcard['nick'];
830 $result['guid'] = $diaspora_guid;
831 $result['url'] = $profile;
832 $result['addr'] = $addr;
833 $result['batch'] = $batch;
834 $result['notify'] = $notify;
835 $result['poll'] = $poll;
836 $result['request'] = $request;
837 $result['confirm'] = $confirm;
838 $result['poco'] = $poco;
839 $result['photo'] = $vcard['photo'];
840 $result['priority'] = $priority;
841 $result['network'] = $network;
842 $result['alias'] = $alias;
843 $result['pubkey'] = $pubkey;
844 $result['baseurl'] = $baseurl;
846 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
849 // Trying if it maybe a diaspora account
850 if (($result['network'] == NETWORK_FEED) OR ($result['addr'] == "")) {
851 require_once('include/bbcode.php');
852 $address = GetProfileUsername($url, "", true);
853 $result2 = probe_url($address, $mode, ++$level);
854 if (!in_array($result2['network'], array("", NETWORK_PHANTOM, NETWORK_FEED)))
858 // Maybe it's some non standard GNU Social installation (Single user, subfolder or no uri rewrite)
859 if (($result['network'] == NETWORK_FEED) AND ($result['baseurl'] != "") AND ($result['nick'] != "")) {
860 $addr = $result['nick'].'@'.str_replace("http://", "", $result['baseurl']);
861 $result2 = probe_url($addr, $mode, ++$level);
862 if (!in_array($result2['network'], array("", NETWORK_PHANTOM, NETWORK_FEED)))
866 // Quickfix for Hubzilla systems with enabled OStatus plugin
867 if (($result['network'] == NETWORK_DIASPORA) AND ($result["batch"] == "")) {
868 $result2 = probe_url($url, PROBE_DIASPORA, ++$level);
869 if ($result2['network'] == NETWORK_DIASPORA) {
870 $addr = $result["addr"];
873 if (($result["addr"] == "") AND ($addr != ""))
874 $result["addr"] = $addr;
879 // Only store into the cache if the value seems to be valid
880 if ($result['network'] != NETWORK_PHANTOM) {
881 Cache::set("probe_url:".$mode.":".$original_url,serialize($result), CACHE_DAY);
883 /// @todo temporary fix - we need a real contact update function that updates only changing fields
884 /// The biggest problem is the avatar picture that could have a reduced image size.
885 /// It should only be updated if the existing picture isn't existing anymore.
886 if (($result['network'] != NETWORK_FEED) AND ($mode == PROBE_NORMAL) AND
887 $result["name"] AND $result["nick"] AND $result["url"] AND $result["addr"] AND $result["poll"])
888 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
889 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
890 WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
891 dbesc($result["name"]),
892 dbesc($result["nick"]),
893 dbesc($result["url"]),
894 dbesc($result["addr"]),
895 dbesc($result["notify"]),
896 dbesc($result["poll"]),
897 dbesc($result["alias"]),
898 dbesc(datetime_convert()),
899 dbesc(normalise_link($result['url']))
907 * @brief Find the matching part between two url
909 * @param string $url1
910 * @param string $url2
911 * @return string The matching part
913 function matching_url($url1, $url2) {
915 if (($url1 == "") OR ($url2 == ""))
918 $url1 = normalise_link($url1);
919 $url2 = normalise_link($url2);
921 $parts1 = parse_url($url1);
922 $parts2 = parse_url($url2);
924 if (!isset($parts1["host"]) OR !isset($parts2["host"]))
927 if ($parts1["scheme"] != $parts2["scheme"])
930 if ($parts1["host"] != $parts2["host"])
933 if ($parts1["port"] != $parts2["port"])
936 $match = $parts1["scheme"]."://".$parts1["host"];
939 $match .= ":".$parts1["port"];
941 $pathparts1 = explode("/", $parts1["path"]);
942 $pathparts2 = explode("/", $parts2["path"]);
947 $path1 = $pathparts1[$i];
948 $path2 = $pathparts2[$i];
950 if ($path1 == $path2)
953 } while (($path1 == $path2) AND ($i++ <= count($pathparts1)));
957 return normalise_link($match);