Browse Source

initial PDO stuff

Andrew Dolgov 1 year ago
parent
commit
40a9cd7ec4
4 changed files with 51 additions and 30 deletions
  1. 8 4
      backend.php
  2. 26 0
      classes/db.php
  3. 16 26
      include/db-prefs.php
  4. 1 0
      include/functions.php

+ 8 - 4
backend.php

@@ -369,14 +369,18 @@
 		break;
 
 	case "init":
-		$result = db_query($link, "SELECT MAX(ttirc_messages.id) AS max_id
+
+		$dbh = Db::get();
+
+		$sth = $dbh->prepare("SELECT MAX(ttirc_messages.id) AS max_id
 			FROM ttirc_messages, ttirc_connections
-			WHERE connection_id = ttirc_connections.id AND owner_uid = " . $_SESSION["uid"]);
+			WHERE connection_id = ttirc_connections.id AND owner_uid = ?");
+		$sth->execute([$_SESSION["uid"]]);
 
 		$rv = array();
 
-		if (db_num_rows($result) != 0) {
-			$rv["max_id"] = db_fetch_result($result, 0, "max_id");
+		if ($row = $sth->fetch()) {
+			$rv["max_id"] = $row['max_id'];
 		} else {
 			$rv["max_id"] = 0;
 		}

+ 26 - 0
classes/db.php

@@ -0,0 +1,26 @@
+<?php
+require_once "config.php";
+
+class DB {
+	private static $instance;
+	private $dbh;
+
+	private function __construct() {
+
+		$this->dbh = new PDO('pgsql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT,
+			DB_USER,
+			DB_PASS);
+	}
+
+	private function __clone() {
+		//
+	}
+
+	public static function get() {
+		if (self::$instance == null)
+			self::$instance = new self();
+
+		return self::$instance->dbh;
+	}
+
+}

+ 16 - 26
include/db-prefs.php

@@ -9,13 +9,10 @@
 
 	function get_pref($link, $pref_name, $user_id = false, $die_on_error = false) {
 
-		$pref_name = db_escape_string($pref_name);
 		$prefs_cache = true;
-		$profile = false;
 
 		if (!$user_id) {
 			$user_id = $_SESSION["uid"];
-			@$profile = $_SESSION["profile"];
 		} else {
 			$user_id = sprintf("%d", $user_id);
 			//$prefs_cache = false;
@@ -28,26 +25,24 @@
 			}
 		}
 
-		if ($profile) {
-			$profile_qpart = "profile = '$profile' AND";
-		} else {
-			$profile_qpart = "profile IS NULL AND";
-		}
+		$dbh = DB::get();
 
-		$result = db_query($link, "SELECT
+		$sth = $dbh->prepare("SELECT
 			value,ttirc_prefs_types.type_name as type_name
 			FROM
 				ttirc_user_prefs,ttirc_prefs,ttirc_prefs_types
 			WHERE
-				$profile_qpart
-				ttirc_user_prefs.pref_name = '$pref_name' AND
+				ttirc_user_prefs.pref_name = ? AND
 				ttirc_prefs_types.id = type_id AND
-				owner_uid = '$user_id' AND
+				owner_uid = ? AND
 				ttirc_user_prefs.pref_name = ttirc_prefs.pref_name");
 
-		if (db_num_rows($result) > 0) {
-			$value = db_fetch_result($result, 0, "value");
-			$type_name = db_fetch_result($result, 0, "type_name");
+		$sth->execute([$pref_name, $user_id]);
+
+		if ($row = $sth->fetch()) {
+
+			$value = $row['value'];
+			$type_name = $row['type_name'];
 
 			if (!defined('DISABLE_SESSIONS')) {
 				if ($user_id == $_SESSION["uid"]) {
@@ -83,18 +78,11 @@
 
 		if (!$user_id) {
 			$user_id = $_SESSION["uid"];
-			@$profile = $_SESSION["profile"];
 		} else {
 			$user_id = sprintf("%d", $user_id);
 			$prefs_cache = false;
 		}
 
-		if ($profile) {
-			$profile_qpart = "AND profile = '$profile'";
-		} else {
-			$profile_qpart = "AND profile IS NULL";
-		}
-
 		$type_name = "";
 		$current_value = "";
 
@@ -131,10 +119,12 @@
 				$value = 'UTC';
 			}
 
-			db_query($link, "UPDATE ttirc_user_prefs SET
-				value = '$value' WHERE pref_name = '$pref_name'
-					$profile_qpart
-					AND owner_uid = " . $_SESSION["uid"]);
+			$dbh = DB::get();
+
+			$sth = $dbh->prepare("UPDATE ttirc_user_prefs SET
+				value = ? WHERE pref_name = ?
+					AND owner_uid = ?");
+			$sth->execute([$value, $pref_name, $user_id]);
 
 			if (!defined('DISABLE_SESSIONS')) {
 				if ($user_id == $_SESSION["uid"]) {

+ 1 - 0
include/functions.php

@@ -10,6 +10,7 @@
 	require_once "config.php";
 	require_once "version.php";
 	require_once "message_types.php";
+	require_once "classes/db.php";
 	require_once "db-prefs.php";
 
 	define('SINGLE_USER_MODE', false);