From 2a41e2f8e4e26e32dc62163dc66e0adec7e77e17 Mon Sep 17 00:00:00 2001
From: Nina Lisitsinskaya <vvatashi4259@gmail.com>
Date: Fri, 16 Mar 2018 00:03:20 +0700
Subject: [PATCH] Add postgresql support

(cherry picked from commit b5ccfe321086eb5935180e5e93b08afcbf6a78eb)
---
 inc/database_pdo.php |  33 +++++++++---
 inc/functions.php    | 126 +++++++++++++++++++++++++++++--------------
 2 files changed, 111 insertions(+), 48 deletions(-)

diff --git a/inc/database_pdo.php b/inc/database_pdo.php
index feb059b..01fb634 100644
--- a/inc/database_pdo.php
+++ b/inc/database_pdo.php
@@ -13,9 +13,14 @@ if (TINYIB_DBDSN == '') { // Build a default (likely MySQL) DSN
 	$dsn = TINYIB_DBDSN;
 }
 
-$options = array(PDO::ATTR_PERSISTENT => true,
-	PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
-	PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
+if (TINYIB_DBDRIVER === 'pgsql') {
+	$options = array(PDO::ATTR_PERSISTENT => true,
+		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
+} else {
+	$options = array(PDO::ATTR_PERSISTENT => true,
+		PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+		PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
+}
 
 try {
 	$dbh = new PDO($dsn, TINYIB_DBUSERNAME, TINYIB_DBPASSWORD, $options);
@@ -24,14 +29,28 @@ try {
 }
 
 // Create the posts table if it does not exist
-$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBPOSTS));
-if ($dbh->query("SELECT FOUND_ROWS()")->fetchColumn() == 0) {
+if (TINYIB_DBDRIVER === 'pgsql') {
+	$query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename LIKE " . $dbh->quote(TINYIB_DBPOSTS);
+	$posts_exists = $dbh->query($query)->fetchColumn() != 0;
+} else {
+	$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBPOSTS));
+	$posts_exists = $dbh->query("SELECT FOUND_ROWS()")->fetchColumn() != 0;
+}
+
+if (!$posts_exists) {
 	$dbh->exec($posts_sql);
 }
 
 // Create the bans table if it does not exist
-$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBBANS));
-if ($dbh->query("SELECT FOUND_ROWS()")->fetchColumn() == 0) {
+if (TINYIB_DBDRIVER === 'pgsql') {
+	$query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename LIKE " . $dbh->quote(TINYIB_DBBANS);
+	$bans_exists = $dbh->query($query)->fetchColumn() != 0;
+} else {
+	$dbh->query("SHOW TABLES LIKE " . $dbh->quote(TINYIB_DBBANS));
+	$bans_exists = $dbh->query("SELECT FOUND_ROWS()")->fetchColumn() != 0;
+}
+
+if (!$bans_exists) {
 	$dbh->exec($bans_sql);
 }
 
diff --git a/inc/functions.php b/inc/functions.php
index 403037f..4d6d18f 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -3,47 +3,91 @@ if (!defined('TINYIB_BOARD')) {
 	die('');
 }
 
-$posts_sql = "CREATE TABLE `" . TINYIB_DBPOSTS . "` (
-	`id` mediumint(7) unsigned NOT NULL auto_increment,
-	`parent` mediumint(7) unsigned NOT NULL,
-	`timestamp` int(20) NOT NULL,
-	`bumped` int(20) NOT NULL,
-	`ip` varchar(39) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`name` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`tripcode` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`email` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`nameblock` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`subject` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`message` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`file` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`file_hex` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`file_original` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`file_size` int(20) unsigned NOT NULL default '0',
-	`file_size_formatted` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`image_width` smallint(5) unsigned NOT NULL default '0',
-	`image_height` smallint(5) unsigned NOT NULL default '0',
-	`thumb` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`thumb_width` smallint(5) unsigned NOT NULL default '0',
-	`thumb_height` smallint(5) unsigned NOT NULL default '0',
-	`stickied` tinyint(1) NOT NULL default '0',
-	`moderated` tinyint(1) NOT NULL default '1',
-	PRIMARY KEY	(`id`),
-	KEY `parent` (`parent`),
-	KEY `bumped` (`bumped`),
-	KEY `stickied` (`stickied`),
-	KEY `moderated` (`moderated`)
-)";
-
-$bans_sql = "CREATE TABLE `" . TINYIB_DBBANS . "` (
-	`id` mediumint(7) unsigned NOT NULL auto_increment,
-	`ip` varchar(39) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	`timestamp` int(20) NOT NULL,
-	`expire` int(20) NOT NULL,
-	`reason` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
-	PRIMARY KEY	(`id`),
-	KEY `ip` (`ip`)
-)";
+if (TINYIB_DBDRIVER === 'pgsql') {
+	$posts_sql = 'CREATE TABLE "' . TINYIB_DBPOSTS . '" (
+		"id" bigserial NOT NULL,
+		"parent" integer NOT NULL,
+		"timestamp" integer NOT NULL,
+		"bumped" integer NOT NULL,
+		"ip" varchar(39) NOT NULL,
+		"name" varchar(75) NOT NULL,
+		"tripcode" varchar(10) NOT NULL,
+		"email" varchar(75) NOT NULL,
+		"nameblock" varchar(255) NOT NULL,
+		"subject" varchar(75) NOT NULL,
+		"message" text NOT NULL,
+		"password" varchar(255) NOT NULL,
+		"file" text NOT NULL,
+		"file_hex" varchar(75) NOT NULL,
+		"file_original" varchar(255) NOT NULL,
+		"file_size" integer NOT NULL default \'0\',
+		"file_size_formatted" varchar(75) NOT NULL,
+		"image_width" smallint NOT NULL default \'0\',
+		"image_height" smallint NOT NULL default \'0\',
+		"thumb" varchar(255) NOT NULL,
+		"thumb_width" smallint NOT NULL default \'0\',
+		"thumb_height" smallint NOT NULL default \'0\',
+		"stickied" smallint NOT NULL default \'0\',
+		"moderated" smallint NOT NULL default \'1\',
+		PRIMARY KEY	("id")
+	);
+	CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("parent");
+	CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("bumped");
+	CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("stickied");
+	CREATE INDEX ON "' . TINYIB_DBPOSTS . '"("moderated");';
+	
+	$bans_sql = 'CREATE TABLE "' . TINYIB_DBBANS . '" (
+		"id" bigserial NOT NULL,
+		"ip" varchar(39) NOT NULL,
+		"timestamp" integer NOT NULL,
+		"expire" integer NOT NULL,
+		"reason" text NOT NULL,
+		PRIMARY KEY	("id")
+	);
+	CREATE INDEX ON "' . TINYIB_DBBANS . '"("ip");';
+} else {
+	$posts_sql = "CREATE TABLE `" . TINYIB_DBPOSTS . "` (
+		`id` mediumint(7) unsigned NOT NULL auto_increment,
+		`parent` mediumint(7) unsigned NOT NULL,
+		`timestamp` int(20) NOT NULL,
+		`bumped` int(20) NOT NULL,
+		`ip` varchar(39) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`name` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`tripcode` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`email` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`nameblock` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`subject` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`message` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`file` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`file_hex` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`file_original` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`file_size` int(20) unsigned NOT NULL default '0',
+		`file_size_formatted` varchar(75) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`image_width` smallint(5) unsigned NOT NULL default '0',
+		`image_height` smallint(5) unsigned NOT NULL default '0',
+		`thumb` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`thumb_width` smallint(5) unsigned NOT NULL default '0',
+		`thumb_height` smallint(5) unsigned NOT NULL default '0',
+		`stickied` tinyint(1) NOT NULL default '0',
+		`moderated` tinyint(1) NOT NULL default '1',
+		PRIMARY KEY	(`id`),
+		KEY `parent` (`parent`),
+		KEY `bumped` (`bumped`),
+		KEY `stickied` (`stickied`),
+		KEY `moderated` (`moderated`)
+	)";
+	
+	$bans_sql = "CREATE TABLE `" . TINYIB_DBBANS . "` (
+		`id` mediumint(7) unsigned NOT NULL auto_increment,
+		`ip` varchar(39) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		`timestamp` int(20) NOT NULL,
+		`expire` int(20) NOT NULL,
+		`reason` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+		PRIMARY KEY	(`id`),
+		KEY `ip` (`ip`)
+	)";
+}
 
 function cleanString($string) {
 	$search = array("<", ">");