From 8ec7f1de56205f0801914f3255831556bb450939 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 Jul 2017 18:32:35 +0000 Subject: [PATCH 1/5] Issue 3571: Reworked "get_contact" function --- include/Contact.php | 134 +++++++++++++++++--------------------------- 1 file changed, 51 insertions(+), 83 deletions(-) diff --git a/include/Contact.php b/include/Contact.php index f99b0d5d0f..a1597cf304 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -517,7 +517,7 @@ 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; @@ -527,39 +527,28 @@ function get_contact($url, $uid = 0, $no_update = false) { } // 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)); - + $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 || $no_update) { + if (!$update_contact || $no_update) { return $contact_id; } } elseif ($uid != 0) { @@ -576,45 +565,28 @@ 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))); + $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), + array('nurl' => normalise_link($url)), array('limit' => 1)); if (!$gcontacts) { return 0; } - $data = $gcontacts[0]; + $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"], + '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"])), @@ -626,49 +598,45 @@ 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)) { + dba::update('contact', $gcontact, array('id' => $contact_id)); } - } - if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $url != "") { - q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 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', '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"] || - $data["alias"] != $contacts[0]["alias"] || - $data["name"] != $contacts[0]["name"] || - $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["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; } From 35b3723c4aa1a24e8bcdbc49bd96b47e6d0fe532 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 11 Jul 2017 19:14:49 +0000 Subject: [PATCH 2/5] database logging now works for database errors --- include/Contact.php | 15 +++++++++++---- include/dba.php | 10 ++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/Contact.php b/include/Contact.php index a1597cf304..fea4a2aaf1 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -577,16 +577,23 @@ function get_contact($url, $uid = 0, $no_update = false) { $url = $data["url"]; if (!$contact_id) { + if (!isset($data['priority'])) { + $data['priority'] = 0; + } + if (!isset($data['batch'])) { + $data['batch'] = ''; + } 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"], '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)); + '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"])), diff --git a/include/dba.php b/include/dba.php index e39e6f136e..49ec956453 100644 --- a/include/dba.php +++ b/include/dba.php @@ -647,8 +647,11 @@ class dba { $error = self::$dbo->error; $errorno = self::$dbo->errorno; + $sql = $args[0]; + array_shift($args); + logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". - $a->callstack(8))."\n".self::replace_parameters($sql, $args); + $a->callstack(8)."\n".self::replace_parameters($sql, $args)); self::$dbo->error = $error; self::$dbo->errorno = $errorno; @@ -711,8 +714,11 @@ class dba { $error = self::$dbo->error; $errorno = self::$dbo->errorno; + $sql = $args[0]; + array_shift($args); + logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". - $a->callstack(8))."\n".self::replace_parameters($sql, $args); + $a->callstack(8)."\n".self::replace_parameters($sql, $args)); self::$dbo->error = $error; self::$dbo->errorno = $errorno; From a47e1e13d72015d38dc6c45619aee8c0d13fd557 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Jul 2017 05:56:35 +0000 Subject: [PATCH 3/5] Process all data that we can fetch via probing --- include/Contact.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/include/Contact.php b/include/Contact.php index fea4a2aaf1..9307bfdded 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -567,26 +567,20 @@ function get_contact($url, $uid = 0, $no_update = false) { // Get data from the gcontact table $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'), array('nurl' => normalise_link($url)), array('limit' => 1)); - if (!$gcontacts) { + if (!dbm::is_result($gcontacts)) { return 0; } - $data = $gcontacts; + $data = array_merge($data, $gcontacts); } $url = $data["url"]; - if (!$contact_id) { - if (!isset($data['priority'])) { - $data['priority'] = 0; - } - if (!isset($data['batch'])) { - $data['batch'] = ''; - } 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"], @@ -608,6 +602,16 @@ function get_contact($url, $uid = 0, $no_update = false) { $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)); } From 69f20959ff330dd34ca2e01b3562b4f64c3d83dd Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Jul 2017 06:07:22 +0000 Subject: [PATCH 4/5] Update existing contact records with data from probing --- include/Contact.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/Contact.php b/include/Contact.php index 9307bfdded..feeb040acc 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -625,7 +625,7 @@ function get_contact($url, $uid = 0, $no_update = false) { update_contact_avatar($data["photo"], $uid, $contact_id); - $contact = dba::select('contact', array('addr', 'alias', 'name', 'nick', 'avatar-date'), + $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 @@ -638,6 +638,16 @@ function get_contact($url, $uid = 0, $no_update = false) { '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(); } From 568abd537a774290204fa45fcd02e53c53d66f5c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 12 Jul 2017 08:14:36 +0000 Subject: [PATCH 5/5] Fixed error reporting --- include/dba.php | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/include/dba.php b/include/dba.php index 49ec956453..08b6e0ad8a 100644 --- a/include/dba.php +++ b/include/dba.php @@ -638,20 +638,22 @@ class dba { } if (self::$dbo->errorno != 0) { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); - $called_from = array_shift($trace); + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); - // We are having an own error logging in the function "p" - if ($called_from['function'] != 'p') { + if (isset($trace[2])) { + $called_from = $trace[2]; + } else { + // We use just something that is defined to avoid warnings + $called_from = $trace[0]; + } + // We are having an own error logging in the function "e" + if ($called_from['function'] != 'e') { // We have to preserve the error code, somewhere in the logging it get lost $error = self::$dbo->error; $errorno = self::$dbo->errorno; - $sql = $args[0]; - array_shift($args); - logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". - $a->callstack(8)."\n".self::replace_parameters($sql, $args)); + $a->callstack(8)."\n".self::replace_parameters($sql, $params)); self::$dbo->error = $error; self::$dbo->errorno = $errorno; @@ -714,11 +716,17 @@ class dba { $error = self::$dbo->error; $errorno = self::$dbo->errorno; - $sql = $args[0]; array_shift($args); + // When the second function parameter is an array then use this as the parameter array + if ((count($args) > 0) && (is_array($args[0]))) { + $params = $args[0]; + } else { + $params = $args; + } + logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". - $a->callstack(8)."\n".self::replace_parameters($sql, $args)); + $a->callstack(8)."\n".self::replace_parameters($sql, $params)); self::$dbo->error = $error; self::$dbo->errorno = $errorno;