X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=inc%2Fpoche%2FDatabase.class.php;h=84916b83efe3cebd2fc6d9f42d03ed327068b71e;hb=fd99a8c02d7c625771656a5c5081560027d8e6e9;hp=a226b31e999cfb5d10f3bf4cdca1aac6f5cb7294;hpb=bc1ee8524e0769ad37e3c4c02cfe96d2f60e52f6;p=github%2Fwallabag%2Fwallabag.git diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index a226b31e..84916b83 100644 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -9,25 +9,27 @@ */ class Database { - - #postgresql - public static $db_path = 'pgsql:host=localhost;dbname=poche'; - public static $user = 'postgres'; - public static $password = 'postgres'; - #sqlite - // public static $db_path = 'sqlite:./db/poche.sqlite'; - // public static $user = ''; - // public static $password = ''; - #mysql - // public static $db_path = 'mysql:host=localhost;dbname=poche'; - // public static $user = 'root'; - // public static $password = 'root'; - var $handle; - function __construct() { - $this->handle = new PDO(self::$db_path, self::$user, self::$password); + function __construct() + { + switch (STORAGE) { + case 'sqlite': + $db_path = 'sqlite:' . STORAGE_SQLITE; + $this->handle = new PDO($db_path); + break; + case 'mysql': + $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; + $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); + break; + case 'postgres': + $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; + $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); + break; + } + $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Tools::logm('storage type ' . STORAGE); } private function getHandle() { @@ -35,19 +37,34 @@ class Database { } public function isInstalled() { - $sql = "SELECT username FROM users WHERE id=?"; - $query = $this->executeQuery($sql, array('1')); - $hasAdmin = $query->fetchAll(); + $sql = "SELECT username FROM users"; + $query = $this->executeQuery($sql, array()); + $hasAdmin = count($query->fetchAll()); - if (count($hasAdmin) == 0) + if ($hasAdmin == 0) return FALSE; return TRUE; } public function install($login, $password) { - $sql = 'INSERT INTO users ( username, password ) VALUES (?, ?)'; - $params = array($login, $password); + $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)'; + $params = array($login, $password, $login, ' '); + $query = $this->executeQuery($sql, $params); + + $sequence = ''; + if (STORAGE == 'postgres') { + $sequence = 'users_id_seq'; + } + + $id_user = intval($this->getLastId($sequence)); + + $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; + $params = array($id_user, 'pager', '10'); + $query = $this->executeQuery($sql, $params); + + $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; + $params = array($id_user, 'language', 'en_EN.UTF8'); $query = $this->executeQuery($sql, $params); return TRUE; @@ -167,6 +184,13 @@ class Database { return $entries; } + public function updateContent($id, $content, $user_id) { + $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?'; + $params_action = array($content, $id, $user_id); + $query = $this->executeQuery($sql_action, $params_action); + return $query; + } + public function add($url, $title, $content, $user_id) { $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)'; $params_action = array($url, $title, $content, $user_id); @@ -193,7 +217,7 @@ class Database { $query = $this->executeQuery($sql_action, $params_action); } - public function getLastId() { - return $this->getHandle()->lastInsertId(); + public function getLastId($column = '') { + return $this->getHandle()->lastInsertId($column); } }