]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Database.class.php
Merge branch 'dev' of git://github.com/mariroz/wallabag into mariroz-dev
[github/wallabag/wallabag.git] / inc / poche / Database.class.php
1 <?php
2 /**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
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, $isauthenticated=false) {
169 if ($isauthenticated) {
170 $sql = "SELECT * FROM users WHERE username=?";
171 $query = $this->executeQuery($sql, array($username));
172 } else {
173 $sql = "SELECT * FROM users WHERE username=? AND password=?";
174 $query = $this->executeQuery($sql, array($username, $password));
175 }
176 $login = $query->fetchAll();
177
178 $user = array();
179 if (isset($login[0])) {
180 $user['id'] = $login[0]['id'];
181 $user['username'] = $login[0]['username'];
182 $user['password'] = $login[0]['password'];
183 $user['name'] = $login[0]['name'];
184 $user['email'] = $login[0]['email'];
185 $user['config'] = $this->getConfigUser($login[0]['id']);
186 }
187
188 return $user;
189 }
190
191 public function updatePassword($userId, $password)
192 {
193 $sql_update = "UPDATE users SET password=? WHERE id=?";
194 $params_update = array($password, $userId);
195 $query = $this->executeQuery($sql_update, $params_update);
196 }
197
198 public function updateUserConfig($userId, $key, $value) {
199 $config = $this->getConfigUser($userId);
200
201 if (! isset($config[$key])) {
202 $sql = "INSERT INTO users_config (value, user_id, name) VALUES (?, ?, ?)";
203 }
204 else {
205 $sql = "UPDATE users_config SET value=? WHERE user_id=? AND name=?";
206 }
207
208 $params = array($value, $userId, $key);
209 $query = $this->executeQuery($sql, $params);
210 }
211
212 private function executeQuery($sql, $params) {
213 try
214 {
215 $query = $this->getHandle()->prepare($sql);
216 $query->execute($params);
217 return $query;
218 }
219 catch (Exception $e)
220 {
221 Tools::logm('execute query error : '.$e->getMessage());
222 return FALSE;
223 }
224 }
225
226 public function retrieveAll($user_id) {
227 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
228 $query = $this->executeQuery($sql, array($user_id));
229 $entries = $query->fetchAll();
230
231 return $entries;
232 }
233
234 public function retrieveOneById($id, $user_id) {
235 $entry = NULL;
236 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
237 $params = array(intval($id), $user_id);
238 $query = $this->executeQuery($sql, $params);
239 $entry = $query->fetchAll();
240
241 return isset($entry[0]) ? $entry[0] : null;
242 }
243
244 public function getEntriesByView($view, $user_id, $limit = '') {
245 switch ($_SESSION['sort'])
246 {
247 case 'ia':
248 $order = 'ORDER BY id';
249 break;
250 case 'id':
251 $order = 'ORDER BY id DESC';
252 break;
253 case 'ta':
254 $order = 'ORDER BY lower(title)';
255 break;
256 case 'td':
257 $order = 'ORDER BY lower(title) DESC';
258 break;
259 default:
260 $order = 'ORDER BY id';
261 break;
262 }
263
264 switch ($view)
265 {
266 case 'archive':
267 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
268 $params = array($user_id, 1);
269 break;
270 case 'fav' :
271 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
272 $params = array($user_id, 1);
273 break;
274 default:
275 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
276 $params = array($user_id, 0);
277 break;
278 }
279
280 $sql .= ' ' . $limit;
281
282 $query = $this->executeQuery($sql, $params);
283 $entries = $query->fetchAll();
284
285 return $entries;
286 }
287
288 public function updateContent($id, $content, $user_id) {
289 $sql_action = 'UPDATE entries SET content = ? WHERE id=? AND user_id=?';
290 $params_action = array($content, $id, $user_id);
291 $query = $this->executeQuery($sql_action, $params_action);
292 return $query;
293 }
294
295 public function add($url, $title, $content, $user_id) {
296 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
297 $params_action = array($url, $title, $content, $user_id);
298 $query = $this->executeQuery($sql_action, $params_action);
299 return $query;
300 }
301
302 public function deleteById($id, $user_id) {
303 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
304 $params_action = array($id, $user_id);
305 $query = $this->executeQuery($sql_action, $params_action);
306 return $query;
307 }
308
309 public function favoriteById($id, $user_id) {
310 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
311 $params_action = array($id, $user_id);
312 $query = $this->executeQuery($sql_action, $params_action);
313 }
314
315 public function archiveById($id, $user_id) {
316 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
317 $params_action = array($id, $user_id);
318 $query = $this->executeQuery($sql_action, $params_action);
319 }
320
321 public function getLastId($column = '') {
322 return $this->getHandle()->lastInsertId($column);
323 }
324
325 public function retrieveAllTags() {
326 $sql = "SELECT * FROM tags";
327 $query = $this->executeQuery($sql, array());
328 $tags = $query->fetchAll();
329
330 return $tags;
331 }
332
333 public function retrieveTag($id) {
334 $tag = NULL;
335 $sql = "SELECT * FROM tags WHERE id=?";
336 $params = array(intval($id));
337 $query = $this->executeQuery($sql, $params);
338 $tag = $query->fetchAll();
339
340 return isset($tag[0]) ? $tag[0] : null;
341 }
342
343 public function retrieveEntriesByTag($tag_id) {
344 $sql =
345 "SELECT entries.* FROM entries
346 LEFT JOIN tags_entries ON tags_entries.entry_id=entries.id
347 WHERE tags_entries.tag_id = ?";
348 $query = $this->executeQuery($sql, array($tag_id));
349 $entries = $query->fetchAll();
350
351 return $entries;
352 }
353
354 public function retrieveTagsByEntry($entry_id) {
355 $sql =
356 "SELECT tags.* FROM tags
357 LEFT JOIN tags_entries ON tags_entries.tag_id=tags.id
358 WHERE tags_entries.entry_id = ?";
359 $query = $this->executeQuery($sql, array($entry_id));
360 $tags = $query->fetchAll();
361
362 return $tags;
363 }
364
365 public function removeTagForEntry($entry_id, $tag_id) {
366 $sql_action = "DELETE FROM tags_entries WHERE tag_id=? AND entry_id=?";
367 $params_action = array($tag_id, $entry_id);
368 $query = $this->executeQuery($sql_action, $params_action);
369 return $query;
370 }
371
372 public function retrieveTagByValue($value) {
373 $tag = NULL;
374 $sql = "SELECT * FROM tags WHERE value=?";
375 $params = array($value);
376 $query = $this->executeQuery($sql, $params);
377 $tag = $query->fetchAll();
378
379 return isset($tag[0]) ? $tag[0] : null;
380 }
381
382 public function createTag($value) {
383 $sql_action = 'INSERT INTO tags ( value ) VALUES (?)';
384 $params_action = array($value);
385 $query = $this->executeQuery($sql_action, $params_action);
386 return $query;
387 }
388
389 public function setTagToEntry($tag_id, $entry_id) {
390 $sql_action = 'INSERT INTO tags_entries ( tag_id, entry_id ) VALUES (?, ?)';
391 $params_action = array($tag_id, $entry_id);
392 $query = $this->executeQuery($sql_action, $params_action);
393 return $query;
394 }
395 }