diff --git a/odm/include/config.php b/odm/include/config.php index 26cd09b..0340073 100644 --- a/odm/include/config.php +++ b/odm/include/config.php @@ -7,6 +7,8 @@ $DB_USER = "root"; $DB_PASSWORD = ""; $DB_DATABASE = "odm"; + //$DB_ENGINE = "postgresql"; + $DB_ENGINE = "mysql"; /* * LDAP configuration diff --git a/odm/include/db.php b/odm/include/db.php index 222fa9d..87a4866 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); } @@ -42,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); @@ -60,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)); } @@ -88,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; } @@ -132,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 = ?"); @@ -163,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)); @@ -173,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 = ?"; @@ -204,12 +226,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 +248,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,14 +258,14 @@ 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(); 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 new file mode 100644 index 0000000..4091b46 --- /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" SERIAL 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" SERIAL 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" SERIAL 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