X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2FContact.php;h=feeb040acc909246094dca2793a6a4f0e5608403;hb=28eb5d57a72aae22428061b26b90195f7feacadf;hp=dc29d38257a38a6cca4f0ae60c7f0df152215661;hpb=09851331a9dc8601919cd0c9200686b92843d235;p=friendica.git diff --git a/include/Contact.php b/include/Contact.php index dc29d38257..feeb040acc 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -1,11 +1,14 @@ datetime_convert('UTC','UTC',$expiry)) { + if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) { // relationship is really truly dead. // archive them rather than delete @@ -214,6 +193,10 @@ function unmark_for_death($contact) { function get_contact_details_by_url($url, $uid = -1, $default = array()) { static $cache = array(); + if ($url == '') { + return $default; + } + if ($uid == -1) { $uid = local_user(); } @@ -256,7 +239,7 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { // "bd" always contains the upcoming birthday of a contact. // "birthday" might contain the birthday including the year of birth. - if ($profile["birthday"] != "0000-00-00") { + if ($profile["birthday"] > '0001-01-01') { $bd_timestamp = strtotime($profile["birthday"]); $month = date("m", $bd_timestamp); $day = date("d", $bd_timestamp); @@ -273,43 +256,43 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { $profile["bd"] = (++$current_year)."-".$month."-".$day; } } else { - $profile["bd"] = "0000-00-00"; + $profile["bd"] = '0001-01-01'; } } else { $profile = $default; } - if (($profile["photo"] == "") AND isset($default["photo"])) { + if (($profile["photo"] == "") && isset($default["photo"])) { $profile["photo"] = $default["photo"]; } - if (($profile["name"] == "") AND isset($default["name"])) { + if (($profile["name"] == "") && isset($default["name"])) { $profile["name"] = $default["name"]; } - if (($profile["network"] == "") AND isset($default["network"])) { + if (($profile["network"] == "") && isset($default["network"])) { $profile["network"] = $default["network"]; } - if (($profile["thumb"] == "") AND isset($profile["photo"])) { + if (($profile["thumb"] == "") && isset($profile["photo"])) { $profile["thumb"] = $profile["photo"]; } - if (($profile["micro"] == "") AND isset($profile["thumb"])) { + if (($profile["micro"] == "") && isset($profile["thumb"])) { $profile["micro"] = $profile["thumb"]; } - if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND + if ((($profile["addr"] == "") || ($profile["name"] == "")) && ($profile["gid"] != 0) && in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]); } // Show contact details of Diaspora contacts only if connected - if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) { + if (($profile["cid"] == 0) && ($profile["network"] == NETWORK_DIASPORA)) { $profile["location"] = ""; $profile["about"] = ""; $profile["gender"] = ""; - $profile["birthday"] = "0000-00-00"; + $profile["birthday"] = '0001-01-01'; } $cache[$url][$uid] = $profile; @@ -330,6 +313,10 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { function get_contact_details_by_addr($addr, $uid = -1) { static $cache = array(); + if ($addr == '') { + return array(); + } + if ($uid == -1) { $uid = local_user(); } @@ -355,7 +342,6 @@ function get_contact_details_by_addr($addr, $uid = -1) { dbesc($addr)); if (!dbm::is_result($r)) { - require_once('include/Probe.php'); $data = Probe::uri($addr); $profile = get_contact_details_by_url($data['url'], $uid); @@ -485,7 +471,7 @@ function random_profile() { function contacts_not_grouped($uid,$start = 0,$count = 0) { - if (! $count) { + if(! $count) { $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ", intval($uid), intval($uid) @@ -531,45 +517,38 @@ function contacts_not_grouped($uid,$start = 0,$count = 0) { * @return integer Contact ID */ function get_contact($url, $uid = 0, $no_update = false) { - logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);; + logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG); $data = array(); $contact_id = 0; - // We first try the nurl (http://server.tld/nick), most common case - $contacts = q("SELECT `id`, `avatar-date` FROM `contact` - WHERE `nurl` = '%s' - AND `uid` = %d", - dbesc(normalise_link($url)), - intval($uid)); + if ($url == '') { + return 0; + } + // We first try the nurl (http://server.tld/nick), most common case + $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1)); // Then the addr (nick@server.tld) - if (! dbm::is_result($contacts)) { - $contacts = q("SELECT `id`, `avatar-date` FROM `contact` - WHERE `addr` = '%s' - AND `uid` = %d", - dbesc($url), - intval($uid)); + if (!dbm::is_result($contact)) { + $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1)); } // Then the alias (which could be anything) - if (! dbm::is_result($contacts)) { - $contacts = q("SELECT `id`, `avatar-date` FROM `contact` - WHERE `alias` IN ('%s', '%s') - AND `uid` = %d", - dbesc($url), - dbesc(normalise_link($url)), - intval($uid)); + if (!dbm::is_result($contact)) { + $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?) AND `uid` = ? LIMIT 1", + $url, normalise_link($url), $uid); + $contact = dba::fetch($r); + dba::close($r); } - if (dbm::is_result($contacts)) { - $contact_id = $contacts[0]["id"]; + if (dbm::is_result($contact)) { + $contact_id = $contact["id"]; // Update the contact every 7 days - $update_photo = ($contacts[0]['avatar-date'] < datetime_convert('','','now -7 days')); + $update_contact = ($contact['avatar-date'] < datetime_convert('','','now -7 days')); - if (!$update_photo OR $no_update) { + if (!$update_contact || $no_update) { return $contact_id; } } elseif ($uid != 0) { @@ -577,7 +556,6 @@ function get_contact($url, $uid = 0, $no_update = false) { return 0; } - require_once('include/Probe.php'); $data = Probe::uri($url); // Last try in gcontact for unsupported networks @@ -587,45 +565,29 @@ function get_contact($url, $uid = 0, $no_update = false) { } // Get data from the gcontact table - $gcontacts = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'", - dbesc(normalise_link($url))); - if (!$gcontacts) { + $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), + array('nurl' => normalise_link($url)), array('limit' => 1)); + if (!dbm::is_result($gcontacts)) { return 0; } - $data = $gcontacts[0]; + $data = array_merge($data, $gcontacts); } $url = $data["url"]; - if (!$contact_id) { - q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, - `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`, - `batch`, `request`, `confirm`, `poco`, `name-date`, `uri-date`, - `writable`, `blocked`, `readonly`, `pending`) - VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', 1, 0, 0, 0)", - intval($uid), - dbesc(datetime_convert()), - dbesc($data["url"]), - dbesc(normalise_link($data["url"])), - dbesc($data["addr"]), - dbesc($data["alias"]), - dbesc($data["notify"]), - dbesc($data["poll"]), - dbesc($data["name"]), - dbesc($data["nick"]), - dbesc($data["photo"]), - dbesc($data["network"]), - dbesc($data["pubkey"]), - intval(CONTACT_IS_SHARING), - intval($data["priority"]), - dbesc($data["batch"]), - dbesc($data["request"]), - dbesc($data["confirm"]), - dbesc($data["poco"]), - dbesc(datetime_convert()), - dbesc(datetime_convert()) - ); + dba::insert('contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"], + 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"], + 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"], + 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"], + 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"], + 'network' => $data["network"], 'pubkey' => $data["pubkey"], + 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"], + 'batch' => $data["batch"], 'request' => $data["request"], + 'confirm' => $data["confirm"], 'poco' => $data["poco"], + 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(), + 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0, + 'readonly' => 0, 'pending' => 0)); $contacts = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2", dbesc(normalise_link($data["url"])), @@ -637,49 +599,65 @@ function get_contact($url, $uid = 0, $no_update = false) { $contact_id = $contacts[0]["id"]; // Update the newly created contact from data in the gcontact table - $gcontacts = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'", - dbesc(normalise_link($data["url"]))); - if (dbm::is_result($gcontacts)) { - logger("Update contact " . $data["url"] . ' from gcontact'); - q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d", - dbesc($gcontacts[0]["location"]), dbesc($gcontacts[0]["about"]), dbesc($gcontacts[0]["keywords"]), - dbesc($gcontacts[0]["gender"]), intval($contact_id)); + $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'), + array('nurl' => normalise_link($data["url"])), array('limit' => 1)); + if (dbm::is_result($gcontact)) { + // Only use the information when the probing hadn't fetched these values + if ($data['keywords'] != '') { + unset($gcontact['keywords']); + } + if ($data['location'] != '') { + unset($gcontact['location']); + } + if ($data['about'] != '') { + unset($gcontact['about']); + } + dba::update('contact', $gcontact, array('id' => $contact_id)); } - } - if (count($contacts) > 1 AND $uid == 0 AND $contact_id != 0 AND $url != "") { - q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d AND NOT `self`", - dbesc(normalise_link($url)), - intval($contact_id)); + if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") { + dba::e("DELETE FROM `contact` WHERE `nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`", + normalise_link($data["url"]), $contact_id); + } } require_once "Photo.php"; update_contact_avatar($data["photo"], $uid, $contact_id); - $contacts = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact` WHERE `id` = %d", intval($contact_id)); + $contact = dba::select('contact', array('addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'), + array('id' => $contact_id), array('limit' => 1)); // This condition should always be true - if (!dbm::is_result($contacts)) { + if (!dbm::is_result($contact)) { return $contact_id; } - // Only update if there had something been changed - if ($data["addr"] != $contacts[0]["addr"] OR - $data["alias"] != $contacts[0]["alias"] OR - $data["name"] != $contacts[0]["name"] OR - $data["nick"] != $contacts[0]["nick"]) { - q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s', - `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d", - dbesc($data["addr"]), - dbesc($data["alias"]), - dbesc($data["name"]), - dbesc($data["nick"]), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($contact_id) - ); + $updated = array('addr' => $data['addr'], + 'alias' => $data['alias'], + 'name' => $data['name'], + 'nick' => $data['nick']); + + if ($data['keywords'] != '') { + $updated['keywords'] = $data['keywords']; } + if ($data['location'] != '') { + $updated['location'] = $data['location']; + } + if ($data['about'] != '') { + $updated['about'] = $data['about']; + } + + if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) { + $updated['uri-date'] = datetime_convert(); + } + if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) { + $updated['name-date'] = datetime_convert(); + } + + $updated['avatar-date'] = datetime_convert(); + + dba::update('contact', $updated, array('id' => $contact_id), $contact); return $contact_id; } @@ -694,7 +672,7 @@ function get_contact($url, $uid = 0, $no_update = false) { */ function posts_from_gcontact(App $a, $gcontact_id) { - require_once('include/conversation.php'); + require_once 'include/conversation.php'; // There are no posts with "uid = 0" with connector networks // This speeds up the query a lot @@ -733,7 +711,7 @@ function posts_from_gcontact(App $a, $gcontact_id) { */ function posts_from_contact_url(App $a, $contact_url) { - require_once('include/conversation.php'); + require_once 'include/conversation.php'; // There are no posts with "uid = 0" with connector networks // This speeds up the query a lot @@ -777,18 +755,18 @@ function posts_from_contact_url(App $a, $contact_url) { function formatted_location($profile) { $location = ''; - if ($profile['locality']) + if($profile['locality']) $location .= $profile['locality']; - if ($profile['region'] AND ($profile['locality'] != $profile['region'])) { - if ($location) + if($profile['region'] && ($profile['locality'] != $profile['region'])) { + if($location) $location .= ', '; $location .= $profile['region']; } - if ($profile['country-name']) { - if ($location) + if($profile['country-name']) { + if($location) $location .= ', '; $location .= $profile['country-name']; @@ -810,7 +788,7 @@ function account_type($contact) { // "page-flags" is a field in the user table, // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP. // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP. - if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY)) + if((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY)) || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP)) || (isset($contact['forum']) && intval($contact['forum'])) || (isset($contact['prv']) && intval($contact['prv'])) @@ -842,4 +820,3 @@ function account_type($contact) { return $account_type; } -?>