aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/store
diff options
context:
space:
mode:
Diffstat (limited to 'inc/store')
-rw-r--r--inc/store/file.class.php47
-rw-r--r--inc/store/mysql.class.php195
-rw-r--r--inc/store/sqlite.class.php197
-rw-r--r--inc/store/store.class.php55
4 files changed, 0 insertions, 494 deletions
diff --git a/inc/store/file.class.php b/inc/store/file.class.php
deleted file mode 100644
index c9d85dcc..00000000
--- a/inc/store/file.class.php
+++ /dev/null
@@ -1,47 +0,0 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class File extends Store {
12 function __construct() {
13
14 }
15
16 public function add() {
17
18 }
19
20 public function retrieveOneById($id) {
21
22 }
23
24 public function retrieveOneByURL($url) {
25
26 }
27
28 public function deleteById($id) {
29
30 }
31
32 public function favoriteById($id) {
33
34 }
35
36 public function archiveById($id) {
37
38 }
39
40 public function getEntriesByView($view) {
41
42 }
43
44 public function getLastId() {
45
46 }
47}
diff --git a/inc/store/mysql.class.php b/inc/store/mysql.class.php
deleted file mode 100644
index 8b7f83da..00000000
--- a/inc/store/mysql.class.php
+++ /dev/null
@@ -1,195 +0,0 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Mysql extends Store {
12
13 public static $db_path = 'mysql:host=localhost;dbname=poche';
14 public static $user = 'root';
15 public static $password = 'root';
16 var $handle;
17
18 function __construct() {
19 parent::__construct();
20
21 $this->handle = new PDO(self::$db_path, self::$user, self::$password);
22 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
23 }
24
25 private function getHandle() {
26 return $this->handle;
27 }
28
29 public function isInstalled() {
30 // $sql = "SELECT name FROM sqlite_sequence WHERE name=?";
31 // $query = $this->executeQuery($sql, array('config'));
32 // $hasConfig = $query->fetchAll();
33
34 // if (count($hasConfig) == 0)
35 // return FALSE;
36
37 // if (!$this->getLogin() || !$this->getPassword())
38 // return FALSE;
39
40 return TRUE;
41 }
42
43 public function install($login, $password) {
44 $this->getHandle()->exec('CREATE TABLE IF NOT EXISTS "config" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "name" VARCHAR UNIQUE, "value" BLOB)');
45
46 $this->handle->exec('CREATE TABLE IF NOT EXISTS "entries" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "title" VARCHAR, "url" VARCHAR UNIQUE , "is_read" INTEGER DEFAULT 0, "is_fav" INTEGER DEFAULT 0, "content" BLOB)');
47
48 if (!$this->getLogin()) {
49 $sql_login = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
50 $params_login = array('login', $login);
51 $query = $this->executeQuery($sql_login, $params_login);
52 }
53
54 if (!$this->getPassword()) {
55 $sql_pass = 'INSERT INTO config ( name, value ) VALUES (?, ?)';
56 $params_pass = array('password', $password);
57 $query = $this->executeQuery($sql_pass, $params_pass);
58 }
59
60 return TRUE;
61 }
62
63 public function getLogin() {
64 $sql = "SELECT value FROM config WHERE name=?";
65 $query = $this->executeQuery($sql, array('login'));
66 $login = $query->fetchAll();
67
68 return isset($login[0]['value']) ? $login[0]['value'] : FALSE;
69 }
70
71 public function getPassword() {
72 $sql = "SELECT value FROM config WHERE name=?";
73 $query = $this->executeQuery($sql, array('password'));
74 $pass = $query->fetchAll();
75
76 return isset($pass[0]['value']) ? $pass[0]['value'] : FALSE;
77 }
78
79 public function updatePassword($password)
80 {
81 $sql_update = "UPDATE config SET value=? WHERE name='password'";
82 $params_update = array($password);
83 $query = $this->executeQuery($sql_update, $params_update);
84 }
85
86 private function executeQuery($sql, $params) {
87 try
88 {
89 $query = $this->getHandle()->prepare($sql);
90 $query->execute($params);
91 return $query;
92 }
93 catch (Exception $e)
94 {
95 Tools::logm('execute query error : '.$e->getMessage());
96 }
97 }
98
99 public function retrieveAll() {
100 $sql = "SELECT * FROM entries ORDER BY id";
101 $query = $this->executeQuery($sql, array());
102 $entries = $query->fetchAll();
103
104 return $entries;
105 }
106
107 public function retrieveOneById($id) {
108 parent::__construct();
109
110 $entry = NULL;
111 $sql = "SELECT * FROM entries WHERE id=?";
112 $params = array(intval($id));
113 $query = $this->executeQuery($sql, $params);
114 $entry = $query->fetchAll();
115
116 return $entry[0];
117 }
118
119 public function getEntriesByView($view) {
120 parent::__construct();
121
122 switch ($_SESSION['sort'])
123 {
124 case 'ia':
125 $order = 'ORDER BY id';
126 break;
127 case 'id':
128 $order = 'ORDER BY id DESC';
129 break;
130 case 'ta':
131 $order = 'ORDER BY lower(title)';
132 break;
133 case 'td':
134 $order = 'ORDER BY lower(title) DESC';
135 break;
136 default:
137 $order = 'ORDER BY id';
138 break;
139 }
140
141 switch ($view)
142 {
143 case 'archive':
144 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
145 $params = array(1);
146 break;
147 case 'fav' :
148 $sql = "SELECT * FROM entries WHERE is_fav=? " . $order;
149 $params = array(1);
150 break;
151 default:
152 $sql = "SELECT * FROM entries WHERE is_read=? " . $order;
153 $params = array(0);
154 break;
155 }
156
157 $query = $this->executeQuery($sql, $params);
158 $entries = $query->fetchAll();
159
160 return $entries;
161 }
162
163 public function add($url, $title, $content) {
164 parent::__construct();
165 $sql_action = 'INSERT INTO entries ( url, title, content ) VALUES (?, ?, ?)';
166 $params_action = array($url, $title, $content);
167 $query = $this->executeQuery($sql_action, $params_action);
168 return $query;
169 }
170
171 public function deleteById($id) {
172 parent::__construct();
173 $sql_action = "DELETE FROM entries WHERE id=?";
174 $params_action = array($id);
175 $query = $this->executeQuery($sql_action, $params_action);
176 return $query;
177 }
178
179 public function favoriteById($id) {
180 parent::__construct();
181 $sql_action = "UPDATE entries SET is_fav = IF (is_fav, 0, 1)";
182 $query = $this->executeQuery($sql_action, array());
183 }
184
185 public function archiveById($id) {
186 parent::__construct();
187 $sql_action = "UPDATE entries SET is_read = IF (is_read, 0, 1)";
188 $query = $this->executeQuery($sql_action, array());
189 }
190
191 public function getLastId() {
192 parent::__construct();
193 return $this->getHandle()->lastInsertId();
194 }
195}
diff --git a/inc/store/sqlite.class.php b/inc/store/sqlite.class.php
deleted file mode 100644
index 4c628dc1..00000000
--- a/inc/store/sqlite.class.php
+++ /dev/null
@@ -1,197 +0,0 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Sqlite extends Store {
12
13 public static $db_path = 'sqlite:./db/poche.sqlite';
14 var $handle;
15
16 function __construct() {
17 parent::__construct();
18
19 $this->handle = new PDO(self::$db_path);
20 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
21 }
22
23 private function getHandle() {
24 return $this->handle;
25 }
26
27 public function isInstalled() {
28 $sql = "SELECT username FROM users WHERE id=?";
29 $query = $this->executeQuery($sql, array('1'));
30 $hasAdmin = $query->fetchAll();
31
32 if (count($hasAdmin) == 0)
33 return FALSE;
34
35 return TRUE;
36 }
37
38 public function install($login, $password) {
39 $sql = 'INSERT INTO users ( username, password ) VALUES (?, ?)';
40 $params = array($login, $password);
41 $query = $this->executeQuery($sql, $params);
42
43 return TRUE;
44 }
45
46 private function getConfigUser($id) {
47 $sql = "SELECT * FROM users_config WHERE user_id = ?";
48 $query = $this->executeQuery($sql, array($id));
49 $result = $query->fetchAll();
50 $user_config = array();
51
52 foreach ($result as $key => $value) {
53 $user_config[$value['name']] = $value['value'];
54 }
55
56 return $user_config;
57 }
58
59 public function login($username, $password) {
60 $sql = "SELECT * FROM users WHERE username=? AND password=?";
61 $query = $this->executeQuery($sql, array($username, $password));
62 $login = $query->fetchAll();
63
64 $user = array();
65 if (isset($login[0])) {
66 $user['id'] = $login[0]['id'];
67 $user['username'] = $login[0]['username'];
68 $user['password'] = $login[0]['password'];
69 $user['name'] = $login[0]['name'];
70 $user['email'] = $login[0]['email'];
71 $user['config'] = $this->getConfigUser($login[0]['id']);
72 }
73
74 return $user;
75 }
76
77 public function updatePassword($id, $password)
78 {
79 $sql_update = "UPDATE users SET password=? WHERE id=?";
80 $params_update = array($password, $id);
81 $query = $this->executeQuery($sql_update, $params_update);
82 }
83
84 private function executeQuery($sql, $params) {
85 try
86 {
87 $query = $this->getHandle()->prepare($sql);
88 $query->execute($params);
89 return $query;
90 }
91 catch (Exception $e)
92 {
93 Tools::logm('execute query error : '.$e->getMessage());
94 }
95 }
96
97 public function retrieveAll($user_id) {
98 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
99 $query = $this->executeQuery($sql, array($user_id));
100 $entries = $query->fetchAll();
101
102 return $entries;
103 }
104
105 public function retrieveOneById($id, $user_id) {
106 parent::__construct();
107
108 $entry = NULL;
109 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
110 $params = array(intval($id), $user_id);
111 $query = $this->executeQuery($sql, $params);
112 $entry = $query->fetchAll();
113
114 return $entry[0];
115 }
116
117 public function getEntriesByView($view, $user_id, $limit = '') {
118 parent::__construct();
119
120 switch ($_SESSION['sort'])
121 {
122 case 'ia':
123 $order = 'ORDER BY id';
124 break;
125 case 'id':
126 $order = 'ORDER BY id DESC';
127 break;
128 case 'ta':
129 $order = 'ORDER BY lower(title)';
130 break;
131 case 'td':
132 $order = 'ORDER BY lower(title) DESC';
133 break;
134 default:
135 $order = 'ORDER BY id';
136 break;
137 }
138
139 switch ($view)
140 {
141 case 'archive':
142 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
143 $params = array($user_id, -1);
144 break;
145 case 'fav' :
146 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
147 $params = array($user_id, -1);
148 break;
149 default:
150 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
151 $params = array($user_id, 0);
152 break;
153 }
154
155 $sql .= ' ' . $limit;
156
157 $query = $this->executeQuery($sql, $params);
158 $entries = $query->fetchAll();
159
160 return $entries;
161 }
162
163 public function add($url, $title, $content, $user_id) {
164 parent::__construct();
165 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
166 $params_action = array($url, $title, $content, $user_id);
167 $query = $this->executeQuery($sql_action, $params_action);
168 return $query;
169 }
170
171 public function deleteById($id, $user_id) {
172 parent::__construct();
173 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
174 $params_action = array($id, $user_id);
175 $query = $this->executeQuery($sql_action, $params_action);
176 return $query;
177 }
178
179 public function favoriteById($id, $user_id) {
180 parent::__construct();
181 $sql_action = "UPDATE entries SET is_fav=~is_fav WHERE id=? AND user_id=?";
182 $params_action = array($id, $user_id);
183 $query = $this->executeQuery($sql_action, $params_action);
184 }
185
186 public function archiveById($id, $user_id) {
187 parent::__construct();
188 $sql_action = "UPDATE entries SET is_read=~is_read WHERE id=? AND user_id=?";
189 $params_action = array($id, $user_id);
190 $query = $this->executeQuery($sql_action, $params_action);
191 }
192
193 public function getLastId() {
194 parent::__construct();
195 return $this->getHandle()->lastInsertId();
196 }
197}
diff --git a/inc/store/store.class.php b/inc/store/store.class.php
deleted file mode 100644
index d6e63014..00000000
--- a/inc/store/store.class.php
+++ /dev/null
@@ -1,55 +0,0 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class Store {
12 function __construct() {
13
14 }
15
16 public function login() {
17
18 }
19
20 public function add() {
21
22 }
23
24 public function retrieveAll() {
25
26 }
27
28 public function retrieveOneById($id) {
29
30 }
31
32 public function retrieveOneByURL($url) {
33
34 }
35
36 public function deleteById($id) {
37
38 }
39
40 public function favoriteById($id) {
41
42 }
43
44 public function archiveById($id) {
45
46 }
47
48 public function getEntriesByView($view) {
49
50 }
51
52 public function getLastId() {
53
54 }
55}