]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Database.class.php
48ec5abf88055fe447655670bd7820906925ca3f
[github/wallabag/wallabag.git] / inc / poche / Database.class.php
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
11 class Database {
12 var $handle;
13
14 function __construct()
15 {
16 switch (STORAGE) {
17 case 'sqlite':
18 $db_path = 'sqlite:' . STORAGE_SQLITE;
19 $this->handle = new PDO($db_path);
20 break;
21 case 'mysql':
22 $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
23 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD);
24 break;
25 case 'postgres':
26 $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB;
27 $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD);
28 break;
29 }
30
31 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 Tools::logm('storage type ' . STORAGE);
33 }
34
35 private function getHandle() {
36 return $this->handle;
37 }
38
39 public function isInstalled() {
40 $sql = "SELECT username FROM users";
41 $query = $this->executeQuery($sql, array());
42 if ($query == false) {
43 die(STORAGE . ' database looks empty. You have to create it (you can find database structure in install folder).');
44 }
45 $hasAdmin = count($query->fetchAll());
46
47 if ($hasAdmin == 0)
48 return false;
49
50 return true;
51 }
52
53 public function checkTags() {
54
55 if (STORAGE == 'sqlite') {
56 $sql = '
57 CREATE TABLE IF NOT EXISTS tags (
58 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
59 value TEXT
60 )';
61 }
62 elseif(STORAGE == 'mysql') {
63 $sql = '
64 CREATE TABLE IF NOT EXISTS `tags` (
65 `id` int(11) NOT NULL AUTO_INCREMENT,
66 `value` varchar(255) NOT NULL,
67 PRIMARY KEY (`id`)
68 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
69 ';
70 }
71 else {
72 $sql = '
73 CREATE TABLE tags (
74 id bigserial primary key,
75 value varchar(255) NOT NULL
76 );
77 ';
78 }
79
80 $query = $this->executeQuery($sql, array());
81
82 if (STORAGE == 'sqlite') {
83 $sql = '
84 CREATE TABLE IF NOT EXISTS tags_entries (
85 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
86 entry_id INTEGER,
87 tag_id INTEGER,
88 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
89 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE
90 )';
91 }
92 elseif(STORAGE == 'mysql') {
93 $sql = '
94 CREATE TABLE IF NOT EXISTS `tags_entries` (
95 `id` int(11) NOT NULL AUTO_INCREMENT,
96 `entry_id` int(11) NOT NULL,
97 `tag_id` int(11) NOT NULL,
98 FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE,
99 FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE,
100 PRIMARY KEY (`id`)
101 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
102 ';
103 }
104 else {
105 $sql = '
106 CREATE TABLE tags_entries (
107 id bigserial primary key,
108 entry_id integer NOT NULL,
109 tag_id integer NOT NULL
110 )
111 ';
112 }
113
114 $query = $this->executeQuery($sql, array());
115 }
116
117 public function install($login, $password) {
118 $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)';
119 $params = array($login, $password, $login, ' ');
120 $query = $this->executeQuery($sql, $params);
121
122 $sequence = '';
123 if (STORAGE == 'postgres') {
124 $sequence = 'users_id_seq';
125 }
126
127 $id_user = intval($this->getLastId($sequence));
128
129 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
130 $params = array($id_user, 'pager', PAGINATION);
131 $query = $this->executeQuery($sql, $params);
132
133 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
134 $params = array($id_user, 'language', LANG);
135 $query = $this->executeQuery($sql, $params);
136
137 $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
138 $params = array($id_user, 'theme', DEFAULT_THEME);
139 $query = $this->executeQuery($sql, $params);
140
141 return TRUE;
142 }
143
144 public function getConfigUser($id) {
145 $sql = "SELECT * FROM users_config WHERE user_id = ?";
146 $query = $this->executeQuery($sql, array($id));
147 $result = $query->fetchAll();
148 $user_config = array();
149
150 foreach ($result as $key => $value) {
151 $user_config[$value['name']] = $value['value'];
152 }
153
154 return $user_config;
155 }
156
157 public function userExists($username) {
158 $sql = "SELECT * FROM users WHERE username=?";
159 $query = $this->executeQuery($sql, array($username));
160 $login = $query->fetchAll();
161 if (isset($login[0])) {
162 return true;
163 } else {
164 return false;
165 }
166 }
167
168 public function login($username, $password) {
169 $sql = "SELECT * FROM users WHERE username=? AND password=?";
170 $query = $this->executeQuery($sql, array($username, $password));
171 $login = $query->fetchAll();
172
173 $user = array();
174 if (isset($login[0])) {
175 $user['id'] = $login[0]['id'];
176 $user['username'] = $login[0]['username'];
177 $user['password'] = $login[0]['password'];
178 $user['name'] = $login[0]['name'];
179 $user['email'] = $login[0]['email'];
180 $user['config'] = $this->getConfigUser($login[0]['id']);
181 }
182
183 return $user;
184 }
185
186 public function updatePassword($userId, $password)
187 {
188 $sql_update = "UPDATE users SET password=? WHERE id=?";
189 $params_update = array($password, $userId);
190 $query = $this->executeQuery($sql_update, $params_update);
191 }
192
193 public function updateUserConfig($userId, $key, $value) {
194 $config = $this->getConfigUser($userId);
195
196 if (! isset($config[$key])) {
197 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
198 }
199 else {
200 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
201 }
202
203 $params = array($value, $userId, $key);
204 $query = $this->executeQuery($sql, $params);
205 }
206
207 private function executeQuery($sql, $params) {
208 try
209 {
210 $query = $this->getHandle()->prepare($sql);
211 $query->execute($params);
212 return $query;
213 }
214 catch (Exception $e)
215 {
216 Tools::logm('execute query error : '.$e->getMessage());
217 return FALSE;
218 }
219 }
220
221 public function retrieveAll($user_id) {
222 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
223 $query = $this->executeQuery($sql, array($user_id));
224 $entries = $query->fetchAll();
225
226 return $entries;
227 }
228
229 public function retrieveOneById($id, $user_id) {
230 $entry = NULL;
231 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
232 $params = array(intval($id), $user_id);
233 $query = $this->executeQuery($sql, $params);
234 $entry = $query->fetchAll();
235
236 return isset($entry[0]) ? $entry[0] : null;
237 }
238
239 public function getEntriesByView($view, $user_id, $limit = '') {
240 switch ($_SESSION['sort'])
241 {
242 case 'ia':
243 $order = 'ORDER BY id';
244 break;
245 case 'id':
246 $order = 'ORDER BY id DESC';
247 break;
248 case 'ta':
249 $order = 'ORDER BY lower(title)';
250 break;
251 case 'td':
252 $order = 'ORDER BY lower(title) DESC';
253 break;
254 default:
255 $order = 'ORDER BY id';
256 break;
257 }
258
259 switch ($view)
260 {
261 case 'archive':
262 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
263 $params = array($user_id, 1);
264 break;
265 case 'fav' :
266 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
267 $params = array($user_id, 1);
268 break;
269 default:
270 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
271 $params = array($user_id, 0);
272 break;
273 }
274
275 $sql .= ' ' . $limit;
276
277 $query = $this->executeQuery($sql, $params);
278 $entries = $query->fetchAll();
279
280 return $entries;
281 }
282
283 public function updateContent($id, $content, $user_id) {
284 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
285 $params_action = array($content, $id, $user_id);
286 $query = $this->executeQuery($sql_action, $params_action);
287 return $query;
288 }
289
290 public function add($url, $title, $content, $user_id) {
291 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
292 $params_action = array($url, $title, $content, $user_id);
293 $query = $this->executeQuery($sql_action, $params_action);
294 return $query;
295 }
296
297 public function deleteById($id, $user_id) {
298 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
299 $params_action = array($id, $user_id);
300 $query = $this->executeQuery($sql_action, $params_action);
301 return $query;
302 }
303
304 public function favoriteById($id, $user_id) {
305 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
306 $params_action = array($id, $user_id);
307 $query = $this->executeQuery($sql_action, $params_action);
308 }
309
310 public function archiveById($id, $user_id) {
311 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
312 $params_action = array($id, $user_id);
313 $query = $this->executeQuery($sql_action, $params_action);
314 }
315
316 public function getLastId($column = '') {
317 return $this->getHandle()->lastInsertId($column);
318 }
319
320 public function retrieveAllTags() {
321 $sql = "SELECT * FROM tags";
322 $query = $this->executeQuery($sql, array());
323 $tags = $query->fetchAll();
324
325 return $tags;
326 }
327
328 public function retrieveTag($id) {
329 $tag = NULL;
330 $sql = "SELECT * FROM tags WHERE id=?";
331 $params = array(intval($id));
332 $query = $this->executeQuery($sql, $params);
333 $tag = $query->fetchAll();
334
335 return isset($tag[0]) ? $tag[0] : null;
336 }
337
338 public function retrieveEntriesByTag($tag_id) {
339 $sql =
340 "SELECT entries.* FROM entries
341 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
342 WHERE tags_entries.tag_id = ?";
343 $query = $this->executeQuery($sql, array($tag_id));
344 $entries = $query->fetchAll();
345
346 return $entries;
347 }
348
349 public function retrieveTagsByEntry($entry_id) {
350 $sql =
351 "SELECT tags.* FROM tags
352 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
353 WHERE tags_entries.entry_id = ?";
354 $query = $this->executeQuery($sql, array($entry_id));
355 $tags = $query->fetchAll();
356
357 return $tags;
358 }
359
360 public function removeTagForEntry($entry_id, $tag_id) {
361 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
362 $params_action = array($tag_id, $entry_id);
363 $query = $this->executeQuery($sql_action, $params_action);
364 return $query;
365 }
366
367 public function retrieveTagByValue($value) {
368 $tag = NULL;
369 $sql = "SELECT * FROM tags WHERE value=?";
370 $params = array($value);
371 $query = $this->executeQuery($sql, $params);
372 $tag = $query->fetchAll();
373
374 return isset($tag[0]) ? $tag[0] : null;
375 }
376
377 public function createTag($value) {
378 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
379 $params_action = array($value);
380 $query = $this->executeQuery($sql_action, $params_action);
381 return $query;
382 }
383
384 public function setTagToEntry($tag_id, $entry_id) {
385 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
386 $params_action = array($tag_id, $entry_id);
387 $query = $this->executeQuery($sql_action, $params_action);
388 return $query;
389 }
390 }