X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=inc%2Fpoche%2FDatabase.class.php;h=5c40b026a18a949aeacd5ef2077c9cf9fb9f262f;hb=df6afaf0909506a334ef2b8c6f69770cd9890e0d;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..5c40b026 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,38 @@ 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', PAGINATION); + $query = $this->executeQuery($sql, $params); + + $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; + $params = array($id_user, 'language', LANG); + $query = $this->executeQuery($sql, $params); + + $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; + $params = array($id_user, 'theme', DEFAULT_THEME); $query = $this->executeQuery($sql, $params); return TRUE; @@ -84,12 +105,26 @@ class Database { return $user; } - public function updatePassword($id, $password) + public function updatePassword($userId, $password) { $sql_update = "UPDATE users SET password=? WHERE id=?"; - $params_update = array($password, $id); + $params_update = array($password, $userId); $query = $this->executeQuery($sql_update, $params_update); } + + public function updateUserConfig($userId, $key, $value) { + $config = $this->getConfigUser($userId); + + if (!isset ($user_config[$key])) { + $sql = "INSERT INTO users_config (`value`, `user_id`, `name`) VALUES (?, ?, ?)"; + } + else { + $sql = "UPDATE users_config SET `value`=? WHERE `user_id`=? AND `name`=?"; + } + + $params = array($value, $userId, $key); + $query = $this->executeQuery($sql, $params); + } private function executeQuery($sql, $params) { try @@ -167,6 +202,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 +235,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); } }