From ddc25583be7a97c3150d2cc59f35b7c3be857fcb Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 19 Dec 2017 06:47:39 +0000
Subject: [PATCH] Updated database calls

---
 src/Protocol/PortableContact.php | 138 ++++++++++++-------------------
 1 file changed, 55 insertions(+), 83 deletions(-)

diff --git a/src/Protocol/PortableContact.php b/src/Protocol/PortableContact.php
index 384256fe9..6a017120a 100644
--- a/src/Protocol/PortableContact.php
+++ b/src/Protocol/PortableContact.php
@@ -65,22 +65,19 @@ class PortableContact
 		$a = get_app();
 
 		if ($cid) {
-			if ((! $url) || (! $uid)) {
-				$r = q(
-					"select `poco`, `uid` from `contact` where `id` = %d limit 1",
-					intval($cid)
-				);
+			if (!$url || !$uid) {
+				$r = dba::select('contact', ['poco', 'uid'], ['id' => $cid], ['limit' => 1]);
 				if (DBM::is_result($r)) {
-					$url = $r[0]['poco'];
-					$uid = $r[0]['uid'];
+					$url = $r['poco'];
+					$uid = $r['uid'];
 				}
 			}
-			if (! $uid) {
+			if (!$uid) {
 				return;
 			}
 		}
 
-		if (! $url) {
+		if (!$url) {
 			return;
 		}
 
@@ -203,12 +200,8 @@ class PortableContact
 		}
 		logger("load: loaded $total entries", LOGGER_DEBUG);
 
-		q(
-			"DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
-			intval($cid),
-			intval($uid),
-			intval($zcid)
-		);
+		$condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
+		dba::delete('glink', $condition);
 	}
 
 	public static function reachable($profile, $server = "", $network = "", $force = false)
@@ -344,11 +337,8 @@ class PortableContact
 		if ($server_url != "") {
 			if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
 				if ($force) {
-					q(
-						"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-						dbesc(datetime_convert()),
-						dbesc(normalise_link($profile))
-					);
+					$fields = ['last_failure' => datetime_convert()];
+					dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 				}
 
 				logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
@@ -428,11 +418,8 @@ class PortableContact
 						GContact::update($contact);
 
 						if (trim($noscrape["updated"]) != "") {
-							q(
-								"UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
-								dbesc(datetime_convert()),
-								dbesc(normalise_link($profile))
-							);
+							$fields = ['last_contact' => datetime_convert()];
+							dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
 							logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
 
@@ -460,8 +447,7 @@ class PortableContact
 			&& (normalise_link($profile) != normalise_link($data["url"]))
 		) {
 			// Delete the old entry
-			q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
-			q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
+			dba::delete('gcontact', ['nurl' => normalise_link($profile)]);
 
 			$gcontact = array_merge($gcontacts[0], $data);
 
@@ -481,11 +467,8 @@ class PortableContact
 		}
 
 		if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
-			q(
-				"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-				dbesc(datetime_convert()),
-				dbesc(normalise_link($profile))
-			);
+			$fields = ['last_failure' => datetime_convert()];
+			dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
 			logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
 			return false;
@@ -500,11 +483,8 @@ class PortableContact
 		$feedret = z_fetch_url($data["poll"]);
 
 		if (!$feedret["success"]) {
-			q(
-				"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
-				dbesc(datetime_convert()),
-				dbesc(normalise_link($profile))
-			);
+			$fields = ['last_failure' => datetime_convert()];
+			dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
 			logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
 			return false;
@@ -537,18 +517,12 @@ class PortableContact
 				$last_updated = NULL_DATE;
 			}
 		}
-		q(
-			"UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
-			dbesc(DBM::date($last_updated)),
-			dbesc(DBM::date()),
-			dbesc(normalise_link($profile))
-		);
+		$fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
+		dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
 		if (($gcontacts[0]["generation"] == 0)) {
-			q(
-				"UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
-				dbesc(normalise_link($profile))
-			);
+			$fields = ['generation' => 9];
+			dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 		}
 
 		logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
@@ -834,32 +808,30 @@ class PortableContact
 			return false;
 		}
 
-		$servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
+		$servers = dba::select('gserver', [], ['nurl' => normalise_link($server_url)], ['limit' => 1]);
 		if (DBM::is_result($servers)) {
-			if ($servers[0]["created"] <= NULL_DATE) {
-				q(
-					"UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
-					dbesc(datetime_convert()),
-					dbesc(normalise_link($server_url))
-				);
+			if ($servers["created"] <= NULL_DATE) {
+				$fields = ['created' => datetime_convert()];
+				$condition = ['nurl' => normalise_link($server_url)];
+				dba::update('gserver', $fields, $condition);
 			}
-			$poco = $servers[0]["poco"];
-			$noscrape = $servers[0]["noscrape"];
+			$poco = $servers["poco"];
+			$noscrape = $servers["noscrape"];
 
 			if ($network == "") {
-				$network = $servers[0]["network"];
+				$network = $servers["network"];
 			}
 
-			$last_contact = $servers[0]["last_contact"];
-			$last_failure = $servers[0]["last_failure"];
-			$version = $servers[0]["version"];
-			$platform = $servers[0]["platform"];
-			$site_name = $servers[0]["site_name"];
-			$info = $servers[0]["info"];
-			$register_policy = $servers[0]["register_policy"];
-			$registered_users = $servers[0]["registered-users"];
+			$last_contact = $servers["last_contact"];
+			$last_failure = $servers["last_failure"];
+			$version = $servers["version"];
+			$platform = $servers["platform"];
+			$site_name = $servers["site_name"];
+			$info = $servers["info"];
+			$register_policy = $servers["register_policy"];
+			$registered_users = $servers["registered-users"];
 
-			if (!$force && !self::updateNeeded($servers[0]["created"], "", $last_failure, $last_contact)) {
+			if (!$force && !self::updateNeeded($servers["created"], "", $last_failure, $last_contact)) {
 				logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
 				return ($last_contact >= $last_failure);
 			}
@@ -876,7 +848,7 @@ class PortableContact
 			$last_contact = NULL_DATE;
 			$last_failure = NULL_DATE;
 		}
-		logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
+		logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
 
 		$failure = false;
 		$possible_failure = false;
@@ -1241,27 +1213,24 @@ class PortableContact
 		}
 
 		// Check again if the server exists
-		$servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
+		$found = dba::exists('gserver', array('nurl' => normalise_link($server_url)));
 
 		$version = strip_tags($version);
 		$site_name = strip_tags($site_name);
 		$info = strip_tags($info);
 		$platform = strip_tags($platform);
 
-		if ($servers) {
-			$fields = ['url' => $server_url, 'version' => $version,
-					'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
-					'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
-					'platform' => $platform, 'registered-users' => $registered_users,
-					'last_contact' => $last_contact, 'last_failure' => last_failure];
+		$fields = ['url' => $server_url, 'version' => $version,
+				'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
+				'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
+				'platform' => $platform, 'registered-users' => $registered_users,
+				'last_contact' => $last_contact, 'last_failure' => last_failure];
+
+		if ($found) {
 			dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
 		} elseif (!$failure) {
-			$fields = ['url' => $server_url, 'nurl' => normalise_link($server_url), 'version' => $version,
-					'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
-					'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
-					'platform' => $platform, 'registered-users' => $registered_users,
-					'created' => datetime_convert(),
-					'last_contact' => $last_contact, 'last_failure' => last_failure];
+			$fields['nurl'] = normalise_link($server_url);
+			$fields['created'] = datetime_convert();
 			dba::insert('gserver', $fields);
 		}
 		logger("End discovery for server " . $server_url, LOGGER_DEBUG);
@@ -1425,7 +1394,8 @@ class PortableContact
 				}
 			}
 
-			q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+			$fields = ['last_poco_query' => datetime_convert()];
+			dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
 			return true;
 		} else {
@@ -1433,7 +1403,8 @@ class PortableContact
 			self::checkServer($server["url"], $server["network"], true);
 
 			// If we couldn't reach the server, we will try it some time later
-			q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+			$fields = ['last_poco_query' => datetime_convert()];
+			dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
 			return false;
 		}
@@ -1458,7 +1429,8 @@ class PortableContact
 			foreach ($r as $server) {
 				if (!self::checkServer($server["url"], $server["network"])) {
 					// The server is not reachable? Okay, then we will try it later
-					q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
+					$fields = ['last_poco_query' => datetime_convert()];
+					dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 					continue;
 				}