From 3134f149d0f49d80d067bf06db5a5c885b4b5613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Tue, 4 Nov 2014 19:17:25 +0100 Subject: [PATCH 1/4] support postgresql --- odm/include/config.php | 2 + odm/include/db.php | 16 +++++--- odm/sql/odm_pg.sql | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 odm/sql/odm_pg.sql diff --git a/odm/include/config.php b/odm/include/config.php index 26cd09b..548bc10 100644 --- a/odm/include/config.php +++ b/odm/include/config.php @@ -7,6 +7,8 @@ $DB_USER = "root"; $DB_PASSWORD = ""; $DB_DATABASE = "odm"; + //$DB_DATABASE = "postgresql"; + $DB_DATABASE = "mysql"; /* * LDAP configuration diff --git a/odm/include/db.php b/odm/include/db.php index 222fa9d..032dbd6 100644 --- a/odm/include/db.php +++ b/odm/include/db.php @@ -3,8 +3,12 @@ $con = null; function dbconnect() { - global $DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE, $con; - $con = new PDO('mysql:dbname='.$DB_DATABASE.';host='.$DB_HOST.';charset=utf8', $DB_USER, $DB_PASSWORD, array(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>1024*1024*50)); + global $DB_ENGINE, $DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE, $con; + if ($DB_ENGINE == "postgresql") { + $con = new PDO('pgsql:dbname='.$DB_DATABASE.';host='.$DB_HOST, $DB_USER, $DB_PASSWORD); + } else { + $con = new PDO('mysql:dbname='.$DB_DATABASE.';host='.$DB_HOST.';charset=utf8', $DB_USER, $DB_PASSWORD, array(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>1024*1024*50)); + } $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } @@ -204,12 +208,12 @@ function generateRandomString($length = 16) { function checkDatabase() { global $con; // Database is missing the token field. Add it, and create a token for encryption - $sql = "show columns from users like 'token'"; + $sql = "select column_name from information_schema.columns where table_name='users' and column_name='token';"; $stmt = $con->prepare($sql); $stmt->execute(); $check_rows = $stmt->rowCount(); if ($check_rows == 0) { - $sql = "alter table users add token varchar(255) not null after hash;"; + $sql = "alter table users add token varchar(255) not null;"; $stmt = $con->prepare($sql); $stmt->execute(); $sql = "select * from users;"; @@ -226,7 +230,7 @@ function checkDatabase() { } } // Remove unrequired enckey from gcm_users - $sql = "show columns from gcm_users like 'enckey'"; + $sql = "select column_name from information_schema.columns where table_name='gcm_users' and column_name='enckey';"; $stmt = $con->prepare($sql); $stmt->execute(); $check_rows = $stmt->rowCount(); @@ -236,7 +240,7 @@ function checkDatabase() { $stmt->execute(); } // Expand the data field for larger submissions - $sql = "show columns from gcm_data like 'data'"; + $sql = "select data_type from information_schema.columns where table_name='gcm_data' and column_name='data';"; $stmt = $con->prepare($sql); $stmt->execute(); $check_rows = $stmt->rowCount(); diff --git a/odm/sql/odm_pg.sql b/odm/sql/odm_pg.sql new file mode 100644 index 0000000..14d19b3 --- /dev/null +++ b/odm/sql/odm_pg.sql @@ -0,0 +1,84 @@ +-- MySQL dump 10.13 Distrib 5.5.40, for debian-linux-gnu (x86_64) +-- +-- Host: localhost Database: odm +-- ------------------------------------------------------ +-- Server version 5.5.40-0+wheezy1 +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO,POSTGRESQL' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table "gcm_data" +-- + +DROP TABLE IF EXISTS "gcm_data"; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE "gcm_data" ( + "id" INTEGER NOT NULL, + "data" BYTEA NOT NULL +); +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table "gcm_messages" +-- + +DROP TABLE IF EXISTS "gcm_messages"; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE "gcm_messages" ( + "id" INTEGER NOT NULL, + "gcm_regid" text NOT NULL, + "message" text NOT NULL, + "data" INTEGER NOT NULL DEFAULT '0', + "created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY ("id") +); +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table "gcm_users" +-- + +DROP TABLE IF EXISTS "gcm_users"; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE "gcm_users" ( + "id" INTEGER NOT NULL, + "user_id" INTEGER DEFAULT NULL, + "gcm_regid" text, + "name" varchar(50) NOT NULL, + "created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY ("id") +); +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table "users" +-- + +DROP TABLE IF EXISTS "users"; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE "users" ( + "user_id" INTEGER NOT NULL, + "username" varchar(50) NOT NULL, + "hash" varchar(255) NOT NULL, + "token" varchar(255) NOT NULL, + "created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY ("user_id") +); +/*!40101 SET character_set_client = @saved_cs_client */; + +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2014-11-04 18:10:54 From 1e2edf90c37766e0f6356c1b5528a0fea37b1b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Wed, 5 Nov 2014 16:44:41 +0100 Subject: [PATCH 2/4] fix some issues with postgresql --- odm/include/db.php | 32 +++++++++++++++++++++----------- odm/sql/odm_pg.sql | 6 +++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/odm/include/db.php b/odm/include/db.php index 032dbd6..4950d98 100644 --- a/odm/include/db.php +++ b/odm/include/db.php @@ -46,17 +46,25 @@ function storeUser($name, $gcm_regid, $user_id) { // Insert message into database function storeMessage($message, $gcm_regid, $data) { - global $con; - $stmt = $con->prepare("INSERT INTO gcm_messages(message, gcm_regid, data, created_at) VALUES(?, ?, ?, NOW())"); - $stmt->execute(array($message, $gcm_regid, $data)); - $id = $con->lastInsertId(); + global $con, $DB_ENGINE; + if ($DB_ENGINE == "mysql") { + $stmt = $con->prepare("INSERT INTO gcm_messages(message, gcm_regid, data, created_at) VALUES(?, ?, ?, NOW())"); + $stmt->execute(array($message, $gcm_regid, $data)); + $id = $con->lastInsertId(); + } else { + $stmt = $con->prepare("INSERT INTO gcm_messages(message, gcm_regid, data, created_at) VALUES(?, ?, ?, NOW()) RETURNING id"); + $stmt->execute(array($message, $gcm_regid, $data)); + $id = $stmt->fetchAll(PDO::FETCH_ASSOC)[0]['id']; + } return $id; } function storeFile($id, $handle) { - global $con; - $stmt = $con->prepare("SET GLOBAL max_allowed_packet = 524288000"); // 500MB - $stmt->execute(); + global $con, $DB_ENGINE; + if ($DB_ENGINE == "mysql") { + $stmt = $con->prepare("SET GLOBAL max_allowed_packet = 524288000"); // 500MB + $stmt->execute(); + } $stmt = $con->prepare("INSERT INTO gcm_data(id, data) VALUES(?, ?)"); $stmt->bindParam(1, $id); $stmt->bindParam(2, $handle, PDO::PARAM_LOB); @@ -64,9 +72,11 @@ function storeFile($id, $handle) { } function storeData($id, $data) { - global $con; - $stmt = $con->prepare("SET GLOBAL max_allowed_packet = 524288000"); // 500MB - $stmt->execute(); + global $con, $DB_ENGINE; + if ($DB_ENGINE == "mysql") { + $stmt = $con->prepare("SET GLOBAL max_allowed_packet = 524288000"); // 500MB + $stmt->execute(); + } $stmt = $con->prepare("INSERT INTO gcm_data(id, data) VALUES(?, ?)"); $stmt->execute(array($id, $data)); } @@ -247,7 +257,7 @@ function checkDatabase() { if ($check_rows != 0) { $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $row) { - if ($row['Type'] == "blob") { + if ($row['data_type'] == "blob") { $sql = "alter table gcm_data modify column data longblob not null;"; $stmt = $con->prepare($sql); $stmt->execute(); diff --git a/odm/sql/odm_pg.sql b/odm/sql/odm_pg.sql index 14d19b3..4091b46 100644 --- a/odm/sql/odm_pg.sql +++ b/odm/sql/odm_pg.sql @@ -31,7 +31,7 @@ DROP TABLE IF EXISTS "gcm_messages"; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE "gcm_messages" ( - "id" INTEGER NOT NULL, + "id" SERIAL NOT NULL, "gcm_regid" text NOT NULL, "message" text NOT NULL, "data" INTEGER NOT NULL DEFAULT '0', @@ -48,7 +48,7 @@ DROP TABLE IF EXISTS "gcm_users"; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE "gcm_users" ( - "id" INTEGER NOT NULL, + "id" SERIAL NOT NULL, "user_id" INTEGER DEFAULT NULL, "gcm_regid" text, "name" varchar(50) NOT NULL, @@ -65,7 +65,7 @@ DROP TABLE IF EXISTS "users"; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE "users" ( - "user_id" INTEGER NOT NULL, + "user_id" SERIAL NOT NULL, "username" varchar(50) NOT NULL, "hash" varchar(255) NOT NULL, "token" varchar(255) NOT NULL, From 1bacd150d5a3cee2a0753e91334ecc6b88abeeec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Wed, 5 Nov 2014 19:16:10 +0100 Subject: [PATCH 3/4] fix getImg for postgres and fix added configuration option --- odm/include/config.php | 4 ++-- odm/include/db.php | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/odm/include/config.php b/odm/include/config.php index 548bc10..0340073 100644 --- a/odm/include/config.php +++ b/odm/include/config.php @@ -7,8 +7,8 @@ $DB_USER = "root"; $DB_PASSWORD = ""; $DB_DATABASE = "odm"; - //$DB_DATABASE = "postgresql"; - $DB_DATABASE = "mysql"; + //$DB_ENGINE = "postgresql"; + $DB_ENGINE = "mysql"; /* * LDAP configuration diff --git a/odm/include/db.php b/odm/include/db.php index 4950d98..7048bcd 100644 --- a/odm/include/db.php +++ b/odm/include/db.php @@ -102,14 +102,18 @@ function getFilename($user_id, $id) { } function getImg($user_id, $id) { - global $con; + global $con, $DB_ENGINE; $img = ""; //$stmt = $con->prepare("select d.data from gcm_data d, gcm_messages m, gcm_users u where d.id = m.id and m.gcm_regid = u.gcm_regid and u.user_id = ? and d.id = ?", array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $con->prepare("select d.data from gcm_data d, gcm_messages m, gcm_users u where d.id = m.id and m.gcm_regid = u.gcm_regid and u.user_id = ? and d.id = ?"); $stmt->execute(array($user_id, $id)); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $row) { - $img .= $row['data']; + if ($DB_ENGINE == "mysql") { + $img .= $row['data']; + } else { + $img .= stream_get_contents($row['data']); + } } return $img; } From bee04b3062567df235ee956739e07f7a138e338c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Wed, 5 Nov 2014 19:39:22 +0100 Subject: [PATCH 4/4] fix delete device on postgresql --- odm/include/db.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/odm/include/db.php b/odm/include/db.php index 7048bcd..87a4866 100644 --- a/odm/include/db.php +++ b/odm/include/db.php @@ -150,7 +150,7 @@ function updatePassword($username, $hash) { $stmt->execute(array($hash, $token, $username)); return $token; } - + function storeUsername($username, $hash) { global $con; $stmt = $con->prepare("select * from users where username = ?"); @@ -181,7 +181,7 @@ function validateRegId($user_id, $gcm_regid) { } function deleteDevice($id, $user_id) { - global $con; + global $con, $DB_ENGINE; $sql = "select gcm_regid from gcm_users where id = ? and user_id = ?"; $stmt = $con->prepare($sql); $stmt->execute(array($id, $user_id)); @@ -191,7 +191,11 @@ function deleteDevice($id, $user_id) { $gcm_regid = $row['gcm_regid']; } if ($gcm_regid != '') { - $sql = "delete d.* from gcm_data d where d.id in (select m.id from gcm_messages m where m.gcm_regid = ?)"; + if ($DB_ENGINE == "mysql") { + $sql = "delete d.* from gcm_data d where d.id in (select m.id from gcm_messages m where m.gcm_regid = ?)"; + } else { + $sql = "delete from gcm_data as d where d.id in (select id from gcm_messages as m where m.gcm_regid = ?)"; + } $stmt = $con->prepare($sql); $stmt->execute(array($gcm_regid)); $sql = "delete from gcm_messages where gcm_regid = ?";