]>
Commit | Line | Data |
---|---|---|
14890de3 | 1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
14890de3 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
14890de3 | 7 | * @copyright 2013 |
8 | * @license http://www.wtfpl.net/ see COPYING file | |
9 | */ | |
10 | ||
bc1ee852 | 11 | class Database { |
14890de3 | 12 | var $handle; |
032e0ca1 MR |
13 | private $order = array( |
14 | 'ia' => 'ORDER BY entries.id', | |
15 | 'id' => 'ORDER BY entries.id DESC', | |
16 | 'ta' => 'ORDER BY lower(entries.title)', | |
17 | 'td' => 'ORDER BY lower(entries.title) DESC', | |
18 | 'default' => 'ORDER BY entries.id' | |
19 | ); | |
20 | ||
21 | function __construct() | |
68857cea NL |
22 | { |
23 | switch (STORAGE) { | |
24 | case 'sqlite': | |
25 | $db_path = 'sqlite:' . STORAGE_SQLITE; | |
26 | $this->handle = new PDO($db_path); | |
27 | break; | |
28 | case 'mysql': | |
29 | $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | |
30 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); | |
31 | break; | |
32 | case 'postgres': | |
33 | $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; | |
34 | $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); | |
35 | break; | |
36 | } | |
37 | ||
14890de3 | 38 | $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
580d60b9 | 39 | Tools::logm('storage type ' . STORAGE); |
14890de3 | 40 | } |
41 | ||
42 | private function getHandle() { | |
43 | return $this->handle; | |
44 | } | |
45 | ||
aa8c9f2a | 46 | public function isInstalled() { |
b916bcfc NL |
47 | $sql = "SELECT username FROM users"; |
48 | $query = $this->executeQuery($sql, array()); | |
5cfafc61 NL |
49 | if ($query == false) { |
50 | die(STORAGE . ' database looks empty. You have to create it (you can find database structure in install folder).'); | |
51 | } | |
b916bcfc | 52 | $hasAdmin = count($query->fetchAll()); |
aa8c9f2a | 53 | |
b916bcfc | 54 | if ($hasAdmin == 0) |
5cfafc61 NL |
55 | return false; |
56 | ||
57 | return true; | |
58 | } | |
59 | ||
60 | public function checkTags() { | |
61 | ||
62 | if (STORAGE == 'sqlite') { | |
63 | $sql = ' | |
64 | CREATE TABLE IF NOT EXISTS tags ( | |
65 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, | |
66 | value TEXT | |
67 | )'; | |
68 | } | |
69 | elseif(STORAGE == 'mysql') { | |
70 | $sql = ' | |
71 | CREATE TABLE IF NOT EXISTS `tags` ( | |
72 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
73 | `value` varchar(255) NOT NULL, | |
74 | PRIMARY KEY (`id`) | |
75 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
76 | '; | |
77 | } | |
78 | else { | |
79 | $sql = ' | |
80 | CREATE TABLE tags ( | |
81 | id bigserial primary key, | |
82 | value varchar(255) NOT NULL | |
83 | ); | |
84 | '; | |
85 | } | |
aa8c9f2a | 86 | |
5cfafc61 NL |
87 | $query = $this->executeQuery($sql, array()); |
88 | ||
89 | if (STORAGE == 'sqlite') { | |
90 | $sql = ' | |
a562e390 | 91 | CREATE TABLE IF NOT EXISTS tags_entries ( |
5cfafc61 NL |
92 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, |
93 | entry_id INTEGER, | |
94 | tag_id INTEGER, | |
95 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
96 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE | |
97 | )'; | |
98 | } | |
99 | elseif(STORAGE == 'mysql') { | |
100 | $sql = ' | |
101 | CREATE TABLE IF NOT EXISTS `tags_entries` ( | |
102 | `id` int(11) NOT NULL AUTO_INCREMENT, | |
103 | `entry_id` int(11) NOT NULL, | |
104 | `tag_id` int(11) NOT NULL, | |
105 | FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, | |
106 | FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE, | |
107 | PRIMARY KEY (`id`) | |
108 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
109 | '; | |
110 | } | |
111 | else { | |
112 | $sql = ' | |
113 | CREATE TABLE tags_entries ( | |
114 | id bigserial primary key, | |
115 | entry_id integer NOT NULL, | |
116 | tag_id integer NOT NULL | |
117 | ) | |
118 | '; | |
119 | } | |
120 | ||
121 | $query = $this->executeQuery($sql, array()); | |
aa8c9f2a NL |
122 | } |
123 | ||
124 | public function install($login, $password) { | |
b916bcfc NL |
125 | $sql = 'INSERT INTO users ( username, password, name, email) VALUES (?, ?, ?, ?)'; |
126 | $params = array($login, $password, $login, ' '); | |
127 | $query = $this->executeQuery($sql, $params); | |
128 | ||
129 | $sequence = ''; | |
130 | if (STORAGE == 'postgres') { | |
131 | $sequence = 'users_id_seq'; | |
132 | } | |
133 | ||
134 | $id_user = intval($this->getLastId($sequence)); | |
135 | ||
136 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 | 137 | $params = array($id_user, 'pager', PAGINATION); |
b916bcfc NL |
138 | $query = $this->executeQuery($sql, $params); |
139 | ||
140 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
00dbaf90 NL |
141 | $params = array($id_user, 'language', LANG); |
142 | $query = $this->executeQuery($sql, $params); | |
143 | ||
144 | $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)'; | |
145 | $params = array($id_user, 'theme', DEFAULT_THEME); | |
7ce7ec4c | 146 | $query = $this->executeQuery($sql, $params); |
aa8c9f2a | 147 | |
7ce7ec4c NL |
148 | return TRUE; |
149 | } | |
aa8c9f2a | 150 | |
5846b0f1 | 151 | public function getConfigUser($id) { |
7ce7ec4c NL |
152 | $sql = "SELECT * FROM users_config WHERE user_id = ?"; |
153 | $query = $this->executeQuery($sql, array($id)); | |
154 | $result = $query->fetchAll(); | |
155 | $user_config = array(); | |
156 | ||
157 | foreach ($result as $key => $value) { | |
158 | $user_config[$value['name']] = $value['value']; | |
aa8c9f2a NL |
159 | } |
160 | ||
7ce7ec4c | 161 | return $user_config; |
aa8c9f2a NL |
162 | } |
163 | ||
027b4e15 DS |
164 | public function userExists($username) { |
165 | $sql = "SELECT * FROM users WHERE username=?"; | |
166 | $query = $this->executeQuery($sql, array($username)); | |
167 | $login = $query->fetchAll(); | |
168 | if (isset($login[0])) { | |
169 | return true; | |
170 | } else { | |
171 | return false; | |
172 | } | |
173 | } | |
174 | ||
6af66b11 MR |
175 | public function login($username, $password, $isauthenticated=false) { |
176 | if ($isauthenticated) { | |
177 | $sql = "SELECT * FROM users WHERE username=?"; | |
178 | $query = $this->executeQuery($sql, array($username)); | |
179 | } else { | |
180 | $sql = "SELECT * FROM users WHERE username=? AND password=?"; | |
181 | $query = $this->executeQuery($sql, array($username, $password)); | |
182 | } | |
8d3275be | 183 | $login = $query->fetchAll(); |
aa8c9f2a | 184 | |
7ce7ec4c NL |
185 | $user = array(); |
186 | if (isset($login[0])) { | |
187 | $user['id'] = $login[0]['id']; | |
188 | $user['username'] = $login[0]['username']; | |
189 | $user['password'] = $login[0]['password']; | |
190 | $user['name'] = $login[0]['name']; | |
191 | $user['email'] = $login[0]['email']; | |
192 | $user['config'] = $this->getConfigUser($login[0]['id']); | |
193 | } | |
aa8c9f2a | 194 | |
7ce7ec4c | 195 | return $user; |
aa8c9f2a NL |
196 | } |
197 | ||
00dbaf90 | 198 | public function updatePassword($userId, $password) |
6499b26a | 199 | { |
8d3275be | 200 | $sql_update = "UPDATE users SET password=? WHERE id=?"; |
1e98ee1d NL |
201 | $params_update = array($password, $userId); |
202 | $query = $this->executeQuery($sql_update, $params_update); | |
00dbaf90 NL |
203 | } |
204 | ||
205 | public function updateUserConfig($userId, $key, $value) { | |
58ace494 NL |
206 | $config = $this->getConfigUser($userId); |
207 | ||
d1d3498b | 208 | if (! isset($config[$key])) { |
da5fc42f | 209 | $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)"; |
58ace494 NL |
210 | } |
211 | else { | |
da5fc42f | 212 | $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?"; |
58ace494 NL |
213 | } |
214 | ||
215 | $params = array($value, $userId, $key); | |
216 | $query = $this->executeQuery($sql, $params); | |
6499b26a NL |
217 | } |
218 | ||
14890de3 | 219 | private function executeQuery($sql, $params) { |
220 | try | |
221 | { | |
222 | $query = $this->getHandle()->prepare($sql); | |
223 | $query->execute($params); | |
224 | return $query; | |
225 | } | |
226 | catch (Exception $e) | |
227 | { | |
eb1af592 | 228 | Tools::logm('execute query error : '.$e->getMessage()); |
bc1ee852 | 229 | return FALSE; |
14890de3 | 230 | } |
231 | } | |
232 | ||
53e3158d NL |
233 | public function updateContentAndTitle($id, $title, $body, $user_id) { |
234 | $sql_action = 'UPDATE entries SET content = ?, title = ? WHERE id=? AND user_id=?'; | |
235 | $params_action = array($body, $title, $id, $user_id); | |
236 | $query = $this->executeQuery($sql_action, $params_action); | |
237 | ||
238 | return $query; | |
239 | } | |
240 | ||
241 | public function retrieveUnfetchedEntries($user_id, $limit) { | |
242 | ||
243 | $sql_limit = "LIMIT 0,".$limit; | |
244 | if (STORAGE == 'postgres') { | |
245 | $sql_limit = "LIMIT ".$limit." OFFSET 0"; | |
246 | } | |
247 | ||
248 | $sql = "SELECT * FROM entries WHERE (content = '' OR content IS NULL) AND user_id=? ORDER BY id " . $sql_limit; | |
249 | $query = $this->executeQuery($sql, array($user_id)); | |
250 | $entries = $query->fetchAll(); | |
251 | ||
252 | return $entries; | |
253 | } | |
254 | ||
8d3275be | 255 | public function retrieveAll($user_id) { |
53e3158d | 256 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? ORDER BY id"; |
8d3275be | 257 | $query = $this->executeQuery($sql, array($user_id)); |
44e77bfa | 258 | $entries = $query->fetchAll(); |
259 | ||
260 | return $entries; | |
261 | } | |
262 | ||
8d3275be | 263 | public function retrieveOneById($id, $user_id) { |
14890de3 | 264 | $entry = NULL; |
8d3275be NL |
265 | $sql = "SELECT * FROM entries WHERE id=? AND user_id=?"; |
266 | $params = array(intval($id), $user_id); | |
14890de3 | 267 | $query = $this->executeQuery($sql, $params); |
268 | $entry = $query->fetchAll(); | |
269 | ||
0c2f4537 | 270 | return isset($entry[0]) ? $entry[0] : null; |
14890de3 | 271 | } |
272 | ||
488fc63b MR |
273 | public function retrieveOneByURL($url, $user_id) { |
274 | $entry = NULL; | |
53e3158d | 275 | $sql = "SELECT * FROM entries WHERE content <> '' AND url=? AND user_id=?"; |
488fc63b MR |
276 | $params = array($url, $user_id); |
277 | $query = $this->executeQuery($sql, $params); | |
278 | $entry = $query->fetchAll(); | |
279 | ||
280 | return isset($entry[0]) ? $entry[0] : null; | |
281 | } | |
282 | ||
283 | public function reassignTags($old_entry_id, $new_entry_id) { | |
284 | $sql = "UPDATE tags_entries SET entry_id=? WHERE entry_id=?"; | |
285 | $params = array($new_entry_id, $old_entry_id); | |
286 | $query = $this->executeQuery($sql, $params); | |
287 | } | |
288 | ||
032e0ca1 MR |
289 | public function getEntriesByView($view, $user_id, $limit = '', $tag_id = 0) { |
290 | switch ($view) { | |
291 | case 'archive': | |
53e3158d | 292 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; |
032e0ca1 | 293 | $params = array($user_id, 1); |
14890de3 | 294 | break; |
032e0ca1 | 295 | case 'fav' : |
53e3158d | 296 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_fav=? "; |
032e0ca1 | 297 | $params = array($user_id, 1); |
14890de3 | 298 | break; |
032e0ca1 MR |
299 | case 'tag' : |
300 | $sql = "SELECT entries.* FROM entries | |
301 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
53e3158d NL |
302 | WHERE entries.content <> '' AND |
303 | entries.user_id=? AND tags_entries.tag_id = ? "; | |
032e0ca1 | 304 | $params = array($user_id, $tag_id); |
14890de3 | 305 | break; |
306 | default: | |
53e3158d | 307 | $sql = "SELECT * FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; |
032e0ca1 | 308 | $params = array($user_id, 0); |
14890de3 | 309 | break; |
310 | } | |
311 | ||
032e0ca1 MR |
312 | $sql .= $this->getEntriesOrder().' ' . $limit; |
313 | ||
314 | $query = $this->executeQuery($sql, $params); | |
315 | $entries = $query->fetchAll(); | |
316 | ||
317 | return $entries; | |
318 | } | |
319 | ||
53e3158d NL |
320 | public function getEntriesByViewCount($view, $user_id, $tag_id = 0) { |
321 | switch ($view) { | |
14890de3 | 322 | case 'archive': |
53e3158d | 323 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; |
bc1ee852 | 324 | $params = array($user_id, 1); |
14890de3 | 325 | break; |
326 | case 'fav' : | |
53e3158d | 327 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_fav=? "; |
bc1ee852 | 328 | $params = array($user_id, 1); |
14890de3 | 329 | break; |
53e3158d NL |
330 | case 'tag' : |
331 | $sql = "SELECT count(*) FROM entries | |
332 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id | |
333 | WHERE entries.content <> '' AND | |
334 | entries.user_id=? AND tags_entries.tag_id = ? "; | |
335 | $params = array($user_id, $tag_id); | |
336 | break; | |
14890de3 | 337 | default: |
53e3158d | 338 | $sql = "SELECT count(*) FROM entries WHERE content <> '' AND user_id=? AND is_read=? "; |
8d3275be | 339 | $params = array($user_id, 0); |
14890de3 | 340 | break; |
341 | } | |
342 | ||
8d3275be | 343 | $query = $this->executeQuery($sql, $params); |
032e0ca1 | 344 | list($count) = $query->fetch(); |
14890de3 | 345 | |
53e3158d | 346 | return $count; |
14890de3 | 347 | } |
348 | ||
b58e261d NL |
349 | public function updateContent($id, $content, $user_id) { |
350 | $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?'; | |
351 | $params_action = array($content, $id, $user_id); | |
352 | $query = $this->executeQuery($sql_action, $params_action); | |
353 | return $query; | |
354 | } | |
355 | ||
8d3275be | 356 | public function add($url, $title, $content, $user_id) { |
8d3275be NL |
357 | $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)'; |
358 | $params_action = array($url, $title, $content, $user_id); | |
359 | $query = $this->executeQuery($sql_action, $params_action); | |
29c6fd46 | 360 | return $query; |
14890de3 | 361 | } |
362 | ||
8d3275be | 363 | public function deleteById($id, $user_id) { |
8d3275be NL |
364 | $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?"; |
365 | $params_action = array($id, $user_id); | |
14890de3 | 366 | $query = $this->executeQuery($sql_action, $params_action); |
29c6fd46 | 367 | return $query; |
14890de3 | 368 | } |
369 | ||
8d3275be | 370 | public function favoriteById($id, $user_id) { |
bc1ee852 | 371 | $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?"; |
8d3275be | 372 | $params_action = array($id, $user_id); |
14890de3 | 373 | $query = $this->executeQuery($sql_action, $params_action); |
374 | } | |
375 | ||
8d3275be | 376 | public function archiveById($id, $user_id) { |
bc1ee852 | 377 | $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?"; |
8d3275be | 378 | $params_action = array($id, $user_id); |
14890de3 | 379 | $query = $this->executeQuery($sql_action, $params_action); |
380 | } | |
381 | ||
f14807de NL |
382 | public function archiveAll($user_id) { |
383 | $sql_action = "UPDATE entries SET is_read=? WHERE user_id=? AND is_read=?"; | |
384 | $params_action = array($user_id, 1, 0); | |
385 | $query = $this->executeQuery($sql_action, $params_action); | |
386 | } | |
387 | ||
b916bcfc NL |
388 | public function getLastId($column = '') { |
389 | return $this->getHandle()->lastInsertId($column); | |
14890de3 | 390 | } |
5bea1a4d | 391 | |
fb26cc93 MR |
392 | public function retrieveAllTags($user_id, $term = null) { |
393 | $sql = "SELECT DISTINCT tags.*, count(entries.id) AS entriescount FROM tags | |
b89d5a2b NL |
394 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
395 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
fb26cc93 MR |
396 | WHERE entries.content <> '' AND entries.user_id=? |
397 | ". (($term) ? "AND lower(tags.value) LIKE ?" : '') ." | |
398 | GROUP BY tags.id, tags.value | |
399 | ORDER BY tags.value"; | |
400 | $query = $this->executeQuery($sql, (($term)? array($user_id, strtolower('%'.$term.'%')) : array($user_id) )); | |
2e2ebe5e NL |
401 | $tags = $query->fetchAll(); |
402 | ||
403 | return $tags; | |
404 | } | |
405 | ||
b89d5a2b | 406 | public function retrieveTag($id, $user_id) { |
4886ed6d | 407 | $tag = NULL; |
e83cf5a7 | 408 | $sql = "SELECT DISTINCT tags.* FROM tags |
b89d5a2b NL |
409 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
410 | LEFT JOIN entries ON tags_entries.entry_id=entries.id | |
53e3158d | 411 | WHERE entries.content <> '' AND tags.id=? AND entries.user_id=?"; |
b89d5a2b | 412 | $params = array(intval($id), $user_id); |
4886ed6d NL |
413 | $query = $this->executeQuery($sql, $params); |
414 | $tag = $query->fetchAll(); | |
415 | ||
416 | return isset($tag[0]) ? $tag[0] : null; | |
417 | } | |
418 | ||
b89d5a2b | 419 | public function retrieveEntriesByTag($tag_id, $user_id) { |
4886ed6d | 420 | $sql = |
9de34d4e | 421 | "SELECT entries.* FROM entries |
4886ed6d | 422 | LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id |
53e3158d NL |
423 | WHERE entries.content <> '' AND |
424 | tags_entries.tag_id = ? AND entries.user_id=?"; | |
b89d5a2b | 425 | $query = $this->executeQuery($sql, array($tag_id, $user_id)); |
4886ed6d NL |
426 | $entries = $query->fetchAll(); |
427 | ||
428 | return $entries; | |
429 | } | |
430 | ||
5bea1a4d | 431 | public function retrieveTagsByEntry($entry_id) { |
2e2ebe5e | 432 | $sql = |
9de34d4e | 433 | "SELECT tags.* FROM tags |
5bea1a4d NL |
434 | LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id |
435 | WHERE tags_entries.entry_id = ?"; | |
2e2ebe5e NL |
436 | $query = $this->executeQuery($sql, array($entry_id)); |
437 | $tags = $query->fetchAll(); | |
5bea1a4d NL |
438 | |
439 | return $tags; | |
440 | } | |
c432fa16 NL |
441 | |
442 | public function removeTagForEntry($entry_id, $tag_id) { | |
443 | $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?"; | |
444 | $params_action = array($tag_id, $entry_id); | |
445 | $query = $this->executeQuery($sql_action, $params_action); | |
446 | return $query; | |
447 | } | |
448 | ||
449 | public function retrieveTagByValue($value) { | |
450 | $tag = NULL; | |
451 | $sql = "SELECT * FROM tags WHERE value=?"; | |
452 | $params = array($value); | |
453 | $query = $this->executeQuery($sql, $params); | |
454 | $tag = $query->fetchAll(); | |
455 | ||
456 | return isset($tag[0]) ? $tag[0] : null; | |
457 | } | |
458 | ||
459 | public function createTag($value) { | |
460 | $sql_action = 'INSERT INTO tags ( value ) VALUES (?)'; | |
461 | $params_action = array($value); | |
462 | $query = $this->executeQuery($sql_action, $params_action); | |
463 | return $query; | |
464 | } | |
465 | ||
466 | public function setTagToEntry($tag_id, $entry_id) { | |
467 | $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)'; | |
468 | $params_action = array($tag_id, $entry_id); | |
469 | $query = $this->executeQuery($sql_action, $params_action); | |
470 | return $query; | |
471 | } | |
032e0ca1 MR |
472 | |
473 | private function getEntriesOrder() { | |
474 | if (isset($_SESSION['sort']) and array_key_exists($_SESSION['sort'], $this->order)) { | |
475 | return $this->order[$_SESSION['sort']]; | |
476 | } | |
477 | else { | |
478 | return $this->order['default']; | |
479 | } | |
480 | } | |
481 | ||
2cd263e0 | 482 | } |