- //is it a link or a full dfrn address?
- if((strpos($name,'@')) || (strpos($name,'http://'))) {
- $newname = $name;
-
- //get the profile links
- $links = @lrdd($name);
- if(count($links)) {
- //for all links, collect how is to inform and how's profile is to link
- foreach($links as $link) {
- if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
- $profile = $link['@attributes']['href'];
- if($link['@attributes']['rel'] === 'salmon') {
- if(strlen($inform))
- $inform .= ',';
- $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
- }
- }
- }
- } elseif (($network != NETWORK_OSTATUS) AND ($network != NETWORK_TWITTER) AND
- ($network != NETWORK_STATUSNET) AND ($network != NETWORK_APPNET)) {
- //if it is a name rather than an address
- $newname = $name;
- $alias = '';
- $tagcid = 0;
- //is it some generated name?
- if(strrpos($newname,'+')) {
- //get the id
- $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
- //remove the next word from tag's name
- if(strpos($name,' ')) {
- $name = substr($name,0,strpos($name,' '));
+
+ // Sometimes the tag detection doesn't seem to work right
+ // This is some workaround
+ $nameparts = explode(" ", $name);
+ $name = $nameparts[0];
+
+ // Try to detect the contact in various ways
+ if ((strpos($name,'@')) || (strpos($name,'http://'))) {
+ // Is it in format @user@domain.tld or @http://domain.tld/...?
+
+ // First check the contact table for the address
+ $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact` WHERE `addr` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($name),
+ intval($profile_uid)
+ );
+
+ // Then check in the contact table for the url
+ if (!$r)
+ $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `notify`, `network` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc(normalise_link($name)),
+ intval($profile_uid)
+ );
+
+ // Then check in the global contacts for the address
+ if (!$r)
+ $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `addr` = '%s' LIMIT 1", dbesc($name));
+
+ // Then check in the global contacts for the url
+ if (!$r)
+ $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", dbesc(normalise_link($name)));
+
+ // If the data isn't complete then refetch the data
+ if ($r AND ($r[0]["network"] == NETWORK_OSTATUS) AND (($r[0]["notify"] == "") OR ($r[0]["alias"] == "")))
+ $r = false;
+
+ if (!$r) {
+ $probed = probe_url($name);
+ if (isset($probed["url"])) {
+ update_gcontact($probed);
+ $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
+ dbesc(normalise_link($probed["url"])));