aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche
diff options
context:
space:
mode:
authorNicolas Lœuillet <nicolas.loeuillet@gmail.com>2013-08-07 14:24:07 +0200
committerNicolas Lœuillet <nicolas.loeuillet@gmail.com>2013-08-07 14:24:07 +0200
commitbc1ee8524e0769ad37e3c4c02cfe96d2f60e52f6 (patch)
tree5ea9d0f0560e84c07ab84c86b9e5fd4dd6ebb039 /inc/poche
parent8d3275bee488d058c6ff0efe6e81d20a584d3709 (diff)
downloadwallabag-bc1ee8524e0769ad37e3c4c02cfe96d2f60e52f6.tar.gz
wallabag-bc1ee8524e0769ad37e3c4c02cfe96d2f60e52f6.tar.zst
wallabag-bc1ee8524e0769ad37e3c4c02cfe96d2f60e52f6.zip
postgres
Diffstat (limited to 'inc/poche')
-rw-r--r--inc/poche/Database.class.php199
-rw-r--r--inc/poche/Poche.class.php23
-rw-r--r--inc/poche/Tools.class.php2
-rw-r--r--inc/poche/config.inc.php7
4 files changed, 218 insertions, 13 deletions
diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php
new file mode 100644
index 00000000..a226b31e
--- /dev/null
+++ b/inc/poche/Database.class.php
@@ -0,0 +1,199 @@
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 Database {
12
13 #postgresql
14 public static $db_path = 'pgsql:host=localhost;dbname=poche';
15 public static $user = 'postgres';
16 public static $password = 'postgres';
17 #sqlite
18 // public static $db_path = 'sqlite:./db/poche.sqlite';
19 // public static $user = '';
20 // public static $password = '';
21 #mysql
22 // public static $db_path = 'mysql:host=localhost;dbname=poche';
23 // public static $user = 'root';
24 // public static $password = 'root';
25
26 var $handle;
27
28 function __construct() {
29 $this->handle = new PDO(self::$db_path, self::$user, self::$password);
30 $this->handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
31 }
32
33 private function getHandle() {
34 return $this->handle;
35 }
36
37 public function isInstalled() {
38 $sql = "SELECT username FROM users WHERE id=?";
39 $query = $this->executeQuery($sql, array('1'));
40 $hasAdmin = $query->fetchAll();
41
42 if (count($hasAdmin) == 0)
43 return FALSE;
44
45 return TRUE;
46 }
47
48 public function install($login, $password) {
49 $sql = 'INSERT INTO users ( username, password ) VALUES (?, ?)';
50 $params = array($login, $password);
51 $query = $this->executeQuery($sql, $params);
52
53 return TRUE;
54 }
55
56 private function getConfigUser($id) {
57 $sql = "SELECT * FROM users_config WHERE user_id = ?";
58 $query = $this->executeQuery($sql, array($id));
59 $result = $query->fetchAll();
60 $user_config = array();
61
62 foreach ($result as $key => $value) {
63 $user_config[$value['name']] = $value['value'];
64 }
65
66 return $user_config;
67 }
68
69 public function login($username, $password) {
70 $sql = "SELECT * FROM users WHERE username=? AND password=?";
71 $query = $this->executeQuery($sql, array($username, $password));
72 $login = $query->fetchAll();
73
74 $user = array();
75 if (isset($login[0])) {
76 $user['id'] = $login[0]['id'];
77 $user['username'] = $login[0]['username'];
78 $user['password'] = $login[0]['password'];
79 $user['name'] = $login[0]['name'];
80 $user['email'] = $login[0]['email'];
81 $user['config'] = $this->getConfigUser($login[0]['id']);
82 }
83
84 return $user;
85 }
86
87 public function updatePassword($id, $password)
88 {
89 $sql_update = "UPDATE users SET password=? WHERE id=?";
90 $params_update = array($password, $id);
91 $query = $this->executeQuery($sql_update, $params_update);
92 }
93
94 private function executeQuery($sql, $params) {
95 try
96 {
97 $query = $this->getHandle()->prepare($sql);
98 $query->execute($params);
99 return $query;
100 }
101 catch (Exception $e)
102 {
103 Tools::logm('execute query error : '.$e->getMessage());
104 return FALSE;
105 }
106 }
107
108 public function retrieveAll($user_id) {
109 $sql = "SELECT * FROM entries WHERE user_id=? ORDER BY id";
110 $query = $this->executeQuery($sql, array($user_id));
111 $entries = $query->fetchAll();
112
113 return $entries;
114 }
115
116 public function retrieveOneById($id, $user_id) {
117 $entry = NULL;
118 $sql = "SELECT * FROM entries WHERE id=? AND user_id=?";
119 $params = array(intval($id), $user_id);
120 $query = $this->executeQuery($sql, $params);
121 $entry = $query->fetchAll();
122
123 return $entry[0];
124 }
125
126 public function getEntriesByView($view, $user_id, $limit = '') {
127 switch ($_SESSION['sort'])
128 {
129 case 'ia':
130 $order = 'ORDER BY id';
131 break;
132 case 'id':
133 $order = 'ORDER BY id DESC';
134 break;
135 case 'ta':
136 $order = 'ORDER BY lower(title)';
137 break;
138 case 'td':
139 $order = 'ORDER BY lower(title) DESC';
140 break;
141 default:
142 $order = 'ORDER BY id';
143 break;
144 }
145
146 switch ($view)
147 {
148 case 'archive':
149 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
150 $params = array($user_id, 1);
151 break;
152 case 'fav' :
153 $sql = "SELECT * FROM entries WHERE user_id=? AND is_fav=? " . $order;
154 $params = array($user_id, 1);
155 break;
156 default:
157 $sql = "SELECT * FROM entries WHERE user_id=? AND is_read=? " . $order;
158 $params = array($user_id, 0);
159 break;
160 }
161
162 $sql .= ' ' . $limit;
163
164 $query = $this->executeQuery($sql, $params);
165 $entries = $query->fetchAll();
166
167 return $entries;
168 }
169
170 public function add($url, $title, $content, $user_id) {
171 $sql_action = 'INSERT INTO entries ( url, title, content, user_id ) VALUES (?, ?, ?, ?)';
172 $params_action = array($url, $title, $content, $user_id);
173 $query = $this->executeQuery($sql_action, $params_action);
174 return $query;
175 }
176
177 public function deleteById($id, $user_id) {
178 $sql_action = "DELETE FROM entries WHERE id=? AND user_id=?";
179 $params_action = array($id, $user_id);
180 $query = $this->executeQuery($sql_action, $params_action);
181 return $query;
182 }
183
184 public function favoriteById($id, $user_id) {
185 $sql_action = "UPDATE entries SET is_fav=NOT is_fav WHERE id=? AND user_id=?";
186 $params_action = array($id, $user_id);
187 $query = $this->executeQuery($sql_action, $params_action);
188 }
189
190 public function archiveById($id, $user_id) {
191 $sql_action = "UPDATE entries SET is_read=NOT is_read WHERE id=? AND user_id=?";
192 $params_action = array($id, $user_id);
193 $query = $this->executeQuery($sql_action, $params_action);
194 }
195
196 public function getLastId() {
197 return $this->getHandle()->lastInsertId();
198 }
199}
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index ce5bb54a..0a43df71 100644
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -16,9 +16,9 @@ class Poche
16 public $messages; 16 public $messages;
17 public $pagination; 17 public $pagination;
18 18
19 function __construct($storage_type) 19 function __construct()
20 { 20 {
21 $this->store = new $storage_type(); 21 $this->store = new Database();
22 $this->init(); 22 $this->init();
23 $this->messages = new Messages(); 23 $this->messages = new Messages();
24 24
@@ -52,9 +52,13 @@ class Poche
52 52
53 # template engine 53 # template engine
54 $loader = new Twig_Loader_Filesystem(TPL); 54 $loader = new Twig_Loader_Filesystem(TPL);
55 $this->tpl = new Twig_Environment($loader, array( 55 if (DEBUG_POCHE) {
56 'cache' => CACHE, 56 $twig_params = array();
57 )); 57 }
58 else {
59 $twig_params = array('cache' => CACHE);
60 }
61 $this->tpl = new Twig_Environment($loader, $twig_params);
58 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); 62 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
59 # filter to display domain name of an url 63 # filter to display domain name of an url
60 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); 64 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
@@ -124,18 +128,19 @@ class Poche
124 Tools::redirect(); 128 Tools::redirect();
125 break; 129 break;
126 case 'delete': 130 case 'delete':
131 $msg = 'delete link #' . $id;
127 if ($this->store->deleteById($id, $this->user->getId())) { 132 if ($this->store->deleteById($id, $this->user->getId())) {
128 if (DOWNLOAD_PICTURES) { 133 if (DOWNLOAD_PICTURES) {
129 remove_directory(ABS_PATH . $id); 134 remove_directory(ABS_PATH . $id);
130 } 135 }
131 $this->messages->add('s', _('the link has been deleted successfully')); 136 $this->messages->add('s', _('the link has been deleted successfully'));
132 Tools::logm('delete link #' . $id);
133 } 137 }
134 else { 138 else {
135 $this->messages->add('e', _('the link wasn\'t deleted')); 139 $this->messages->add('e', _('the link wasn\'t deleted'));
136 Tools::logm('error : can\'t delete link #' . $id); 140 $msg = 'error : can\'t delete link #' . $id;
137 } 141 }
138 Tools::redirect(); 142 Tools::logm($msg);
143 Tools::redirect('?');
139 break; 144 break;
140 case 'toggle_fav' : 145 case 'toggle_fav' :
141 $this->store->favoriteById($id, $this->user->getId()); 146 $this->store->favoriteById($id, $this->user->getId());
@@ -385,7 +390,7 @@ class Poche
385 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { 390 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
386 $version = file_get_contents($cache_file); 391 $version = file_get_contents($cache_file);
387 } else { 392 } else {
388 $version = file_get_contents('http://www.inthepoche.com/' . $which); 393 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
389 file_put_contents($cache_file, $version, LOCK_EX); 394 file_put_contents($cache_file, $version, LOCK_EX);
390 } 395 }
391 return $version; 396 return $version;
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php
index 8b339ea5..d0e43166 100644
--- a/inc/poche/Tools.class.php
+++ b/inc/poche/Tools.class.php
@@ -77,6 +77,7 @@ class Tools
77 $url = $ref; 77 $url = $ref;
78 } 78 }
79 } 79 }
80 self::logm('redirect to ' . $url);
80 header('Location: '.$url); 81 header('Location: '.$url);
81 exit(); 82 exit();
82 } 83 }
@@ -198,6 +199,7 @@ class Tools
198 if (DEBUG_POCHE) { 199 if (DEBUG_POCHE) {
199 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; 200 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
200 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); 201 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
202 error_log('DEBUG POCHE : ' . $message);
201 } 203 }
202 } 204 }
203 205
diff --git a/inc/poche/config.inc.php b/inc/poche/config.inc.php
index a8a9c032..834b18ea 100644
--- a/inc/poche/config.inc.php
+++ b/inc/poche/config.inc.php
@@ -24,7 +24,7 @@ define ('CACHE', './cache');
24define ('LANG', 'en_EN.UTF8'); 24define ('LANG', 'en_EN.UTF8');
25define ('PAGINATION', '10'); 25define ('PAGINATION', '10');
26define ('THEME', 'light'); 26define ('THEME', 'light');
27$storage_type = 'sqlite'; # sqlite, mysql, (file, not yet) 27define ('STORAGE','postgres'); # postgres, mysql, sqlite
28 28
29# /!\ Be careful if you change the lines below /!\ 29# /!\ Be careful if you change the lines below /!\
30require_once './inc/poche/User.class.php'; 30require_once './inc/poche/User.class.php';
@@ -34,8 +34,7 @@ require_once './inc/3rdparty/class.messages.php';
34require_once './inc/poche/Poche.class.php'; 34require_once './inc/poche/Poche.class.php';
35require_once './inc/3rdparty/Readability.php'; 35require_once './inc/3rdparty/Readability.php';
36require_once './inc/3rdparty/Encoding.php'; 36require_once './inc/3rdparty/Encoding.php';
37require_once './inc/store/store.class.php'; 37require_once './inc/poche/Database.class.php';
38require_once './inc/store/' . $storage_type . '.class.php';
39require_once './vendor/autoload.php'; 38require_once './vendor/autoload.php';
40require_once './inc/3rdparty/simple_html_dom.php'; 39require_once './inc/3rdparty/simple_html_dom.php';
41require_once './inc/3rdparty/paginator.php'; 40require_once './inc/3rdparty/paginator.php';
@@ -45,7 +44,7 @@ if (DOWNLOAD_PICTURES) {
45 require_once './inc/poche/pochePictures.php'; 44 require_once './inc/poche/pochePictures.php';
46} 45}
47 46
48$poche = new Poche($storage_type); 47$poche = new Poche();
49 48
50#XSRF protection with token 49#XSRF protection with token
51// if (!empty($_POST)) { 50// if (!empty($_POST)) {