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;
608 if(($network == "") && $diaspora_base && $diaspora_guid) {
609 $diaspora_notify = $diaspora_base.'receive/users/'.$diaspora_guid;
612 if($mode == PROBE_DIASPORA || !$notify || ($notify == $diaspora_notify)) {
613 $notify = $diaspora_notify;
614 $batch = $diaspora_base . 'receive/public' ;
617 $addr = str_replace('acct:', '', $url);
620 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
622 $network = NETWORK_DIASPORA;
623 elseif($has_lrdd AND ($notify))
624 $network = NETWORK_OSTATUS;
627 $addr = str_replace('acct:', '', $url);
631 if($hcard && ! $vcard) {
632 $vcard = scrape_vcard($hcard);
634 // Google doesn't use absolute url in profile photos
636 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
637 $h = @parse_url($hcard);
639 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
642 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
645 if($diaspora && $addr) {
646 // Diaspora returns the name as the nick. As the nick will never be updated,
647 // let's use the Diaspora nickname (the first part of the handle) as the nick instead
648 $addr_parts = explode('@', $addr);
649 $vcard['nick'] = $addr_parts[0];
654 $poll = str_replace(array('www.','last.fm/'),array('','ws.audioscrobbler.com/1.0/'),$url) . '/recenttracks.rss';
655 $vcard['nick'] = basename($url);
656 $vcard['fn'] = $vcard['nick'] . t(' on Last.fm');
657 $network = NETWORK_FEED;
662 $vcard['fn'] = $vcard['nick'];
666 if(stristr($url,'tumblr.com') && (! stristr($url,'/rss'))) {
667 $poll = $url . '/rss';
669 // 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
672 if($appnet || ! $poll)
674 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
676 if(($at_addr) && (! count($links)))
679 if ($connectornetworks)
684 $feedret = scrape_feed(($poll) ? $poll : $url);
686 logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
687 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
688 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
693 if(x($feedret,'photo') && (! x($vcard,'photo')))
694 $vcard['photo'] = $feedret['photo'];
696 $cookiejar = tempnam(get_temppath(), 'cookiejar-scrape-feed-');
697 $xml = fetch_url($poll, false, $redirects, 0, Null, $cookiejar);
700 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
703 logger("scrape_feed: XML is empty for feed ".$poll);
704 $network = NETWORK_PHANTOM;
706 $data = feed_import($xml,$dummy1,$dummy2, $dummy3, true);
708 if (!is_array($data)) {
709 logger("scrape_feed: This doesn't seem to be a feed: ".$poll);
710 $network = NETWORK_PHANTOM;
712 if (($vcard["photo"] == "") AND ($data["header"]["author-avatar"] != ""))
713 $vcard["photo"] = $data["header"]["author-avatar"];
715 if (($vcard["fn"] == "") AND ($data["header"]["author-name"] != ""))
716 $vcard["fn"] = $data["header"]["author-name"];
718 if (($vcard["nick"] == "") AND ($data["header"]["author-nick"] != ""))
719 $vcard["nick"] = $data["header"]["author-nick"];
721 if ($network == NETWORK_OSTATUS) {
722 if ($data["header"]["author-id"] != "")
723 $alias = $data["header"]["author-id"];
725 if ($data["header"]["author-link"] != "")
726 $profile = $data["header"]["author-link"];
728 } elseif(!$profile AND ($data["header"]["author-link"] != "") AND !in_array($network, array("", NETWORK_FEED)))
729 $profile = $data["header"]["author-link"];
733 // Workaround for misconfigured Friendica servers
734 if (($network == "") AND (strstr($url, "/profile/"))) {
735 $noscrape = str_replace("/profile/", "/noscrape/", $url);
736 $noscrapejson = fetch_url($noscrape);
739 $network = NETWORK_DFRN;
741 $poco = str_replace("/profile/", "/poco/", $url);
743 $noscrapedata = json_decode($noscrapejson, true);
745 if (isset($noscrapedata["addr"]))
746 $addr = $noscrapedata["addr"];
748 if (isset($noscrapedata["fn"]))
749 $vcard["fn"] = $noscrapedata["fn"];
751 if (isset($noscrapedata["key"]))
752 $pubkey = $noscrapedata["key"];
754 if (isset($noscrapedata["photo"]))
755 $vcard["photo"] = $noscrapedata["photo"];
757 if (isset($noscrapedata["dfrn-request"]))
758 $request = $noscrapedata["dfrn-request"];
760 if (isset($noscrapedata["dfrn-confirm"]))
761 $confirm = $noscrapedata["dfrn-confirm"];
763 if (isset($noscrapedata["dfrn-notify"]))
764 $notify = $noscrapedata["dfrn-notify"];
766 if (isset($noscrapedata["dfrn-poll"]))
767 $poll = $noscrapedata["dfrn-poll"];
773 $network = NETWORK_FEED;
775 if(! x($vcard,'nick')) {
776 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
777 if(strpos($vcard['nick'],' '))
778 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
785 if(! x($vcard,'photo')) {
787 $vcard['photo'] = App::get_baseurl() . '/images/person-175.jpg' ;
793 // No human could be associated with this link, use the URL as the contact name
795 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
798 if ($diaspora_base != "")
799 $baseurl = $diaspora_base;
801 if (($baseurl == "") AND ($notify != "") AND ($poll != "")) {
802 $baseurl = matching_url(normalise_link($notify), normalise_link($poll));
804 $baseurl2 = matching_url($baseurl, normalise_link($profile));
806 $baseurl = $baseurl2;
809 if (($baseurl == "") AND ($notify != ""))
810 $baseurl = matching_url(normalise_link($profile), normalise_link($notify));
812 if (($baseurl == "") AND ($poll != ""))
813 $baseurl = matching_url(normalise_link($profile), normalise_link($poll));
815 if (substr($baseurl, -10) == "/index.php")
816 $baseurl = str_replace("/index.php", "", $baseurl);
819 $network = NETWORK_PHANTOM;
821 $baseurl = rtrim($baseurl, "/");
823 if(strpos($url,'@') AND ($addr == "") AND ($network == NETWORK_DFRN))
824 $addr = str_replace('acct:', '', $url);
826 $vcard['fn'] = notags($vcard['fn']);
827 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
829 $result['name'] = $vcard['fn'];
830 $result['nick'] = $vcard['nick'];
831 $result['guid'] = $diaspora_guid;
832 $result['url'] = $profile;
833 $result['addr'] = $addr;
834 $result['batch'] = $batch;
835 $result['notify'] = $notify;
836 $result['poll'] = $poll;
837 $result['request'] = $request;
838 $result['confirm'] = $confirm;
839 $result['poco'] = $poco;
840 $result['photo'] = $vcard['photo'];
841 $result['priority'] = $priority;
842 $result['network'] = $network;
843 $result['alias'] = $alias;
844 $result['pubkey'] = $pubkey;
845 $result['baseurl'] = $baseurl;
847 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
850 // Trying if it maybe a diaspora account
851 if (($result['network'] == NETWORK_FEED) OR ($result['addr'] == "")) {
852 require_once('include/bbcode.php');
853 $address = GetProfileUsername($url, "", true);
854 $result2 = probe_url($address, $mode, ++$level);
855 if (!in_array($result2['network'], array("", NETWORK_PHANTOM, NETWORK_FEED)))
859 // Maybe it's some non standard GNU Social installation (Single user, subfolder or no uri rewrite)
860 if (($result['network'] == NETWORK_FEED) AND ($result['baseurl'] != "") AND ($result['nick'] != "")) {
861 $addr = $result['nick'].'@'.str_replace("http://", "", $result['baseurl']);
862 $result2 = probe_url($addr, $mode, ++$level);
863 if (!in_array($result2['network'], array("", NETWORK_PHANTOM, NETWORK_FEED)))
867 // Quickfix for Hubzilla systems with enabled OStatus plugin
868 if (($result['network'] == NETWORK_DIASPORA) AND ($result["batch"] == "")) {
869 $result2 = probe_url($url, PROBE_DIASPORA, ++$level);
870 if ($result2['network'] == NETWORK_DIASPORA) {
871 $addr = $result["addr"];
874 if (($result["addr"] == "") AND ($addr != ""))
875 $result["addr"] = $addr;
880 // Only store into the cache if the value seems to be valid
881 if ($result['network'] != NETWORK_PHANTOM) {
882 Cache::set("probe_url:".$mode.":".$original_url,serialize($result), CACHE_DAY);
884 /// @todo temporary fix - we need a real contact update function that updates only changing fields
885 /// The biggest problem is the avatar picture that could have a reduced image size.
886 /// It should only be updated if the existing picture isn't existing anymore.
887 if (($result['network'] != NETWORK_FEED) AND ($mode == PROBE_NORMAL) AND
888 $result["name"] AND $result["nick"] AND $result["url"] AND $result["addr"] AND $result["poll"])
889 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
890 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
891 WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
892 dbesc($result["name"]),
893 dbesc($result["nick"]),
894 dbesc($result["url"]),
895 dbesc($result["addr"]),
896 dbesc($result["notify"]),
897 dbesc($result["poll"]),
898 dbesc($result["alias"]),
899 dbesc(datetime_convert()),
900 dbesc(normalise_link($result['url']))
908 * @brief Find the matching part between two url
910 * @param string $url1
911 * @param string $url2
912 * @return string The matching part
914 function matching_url($url1, $url2) {
916 if (($url1 == "") OR ($url2 == ""))
919 $url1 = normalise_link($url1);
920 $url2 = normalise_link($url2);
922 $parts1 = parse_url($url1);
923 $parts2 = parse_url($url2);
925 if (!isset($parts1["host"]) OR !isset($parts2["host"]))
928 if ($parts1["scheme"] != $parts2["scheme"])
931 if ($parts1["host"] != $parts2["host"])
934 if ($parts1["port"] != $parts2["port"])
937 $match = $parts1["scheme"]."://".$parts1["host"];
940 $match .= ":".$parts1["port"];
942 $pathparts1 = explode("/", $parts1["path"]);
943 $pathparts2 = explode("/", $parts2["path"]);
948 $path1 = $pathparts1[$i];
949 $path2 = $pathparts2[$i];
951 if ($path1 == $path2)
954 } while (($path1 == $path2) AND ($i++ <= count($pathparts1)));
958 return normalise_link($match);