]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
postgres
[github/wallabag/wallabag.git] / inc / poche / Poche.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 Poche
12 {
13 public $user;
14 public $store;
15 public $tpl;
16 public $messages;
17 public $pagination;
18
19 function __construct()
20 {
21 $this->store = new Database();
22 $this->init();
23 $this->messages = new Messages();
24
25 # installation
26 if(!$this->store->isInstalled())
27 {
28 $this->install();
29 }
30 }
31
32 private function init()
33 {
34 Tools::initPhp();
35 Session::init();
36
37 if (isset($_SESSION['poche_user'])) {
38 $this->user = $_SESSION['poche_user'];
39 }
40 else {
41 # fake user, just for install & login screens
42 $this->user = new User();
43 $this->user->setConfig($this->getDefaultConfig());
44 }
45
46 # l10n
47 $language = $this->user->getConfigValue('language');
48 putenv('LC_ALL=' . $language);
49 setlocale(LC_ALL, $language);
50 bindtextdomain($language, LOCALE);
51 textdomain($language);
52
53 # template engine
54 $loader = new Twig_Loader_Filesystem(TPL);
55 if (DEBUG_POCHE) {
56 $twig_params = array();
57 }
58 else {
59 $twig_params = array('cache' => CACHE);
60 }
61 $this->tpl = new Twig_Environment($loader, $twig_params);
62 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
63 # filter to display domain name of an url
64 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
65 $this->tpl->addFilter($filter);
66
67 # Pagination
68 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
69 }
70
71 private function install()
72 {
73 Tools::logm('poche still not installed');
74 echo $this->tpl->render('install.twig', array(
75 'token' => Session::getToken()
76 ));
77 if (isset($_GET['install'])) {
78 if (($_POST['password'] == $_POST['password_repeat'])
79 && $_POST['password'] != "" && $_POST['login'] != "") {
80 # let's rock, install poche baby !
81 $this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
82 Session::logout();
83 Tools::logm('poche is now installed');
84 Tools::redirect();
85 }
86 else {
87 Tools::logm('error during installation');
88 Tools::redirect();
89 }
90 }
91 exit();
92 }
93
94 public function getDefaultConfig()
95 {
96 return array(
97 'pager' => PAGINATION,
98 'language' => LANG,
99 );
100 }
101
102 /**
103 * Call action (mark as fav, archive, delete, etc.)
104 */
105 public function action($action, Url $url, $id = 0)
106 {
107 switch ($action)
108 {
109 case 'add':
110 if($parametres_url = $url->fetchContent()) {
111 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
112 Tools::logm('add link ' . $url->getUrl());
113 $last_id = $this->store->getLastId();
114 if (DOWNLOAD_PICTURES) {
115 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
116 }
117 $this->messages->add('s', _('the link has been added successfully'));
118 }
119 else {
120 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
121 Tools::logm('error during insertion : the link wasn\'t added');
122 }
123 }
124 else {
125 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
126 Tools::logm('error during content fetch');
127 }
128 Tools::redirect();
129 break;
130 case 'delete':
131 $msg = 'delete link #' . $id;
132 if ($this->store->deleteById($id, $this->user->getId())) {
133 if (DOWNLOAD_PICTURES) {
134 remove_directory(ABS_PATH . $id);
135 }
136 $this->messages->add('s', _('the link has been deleted successfully'));
137 }
138 else {
139 $this->messages->add('e', _('the link wasn\'t deleted'));
140 $msg = 'error : can\'t delete link #' . $id;
141 }
142 Tools::logm($msg);
143 Tools::redirect('?');
144 break;
145 case 'toggle_fav' :
146 $this->store->favoriteById($id, $this->user->getId());
147 Tools::logm('mark as favorite link #' . $id);
148 Tools::redirect();
149 break;
150 case 'toggle_archive' :
151 $this->store->archiveById($id, $this->user->getId());
152 Tools::logm('archive link #' . $id);
153 Tools::redirect();
154 break;
155 default:
156 break;
157 }
158 }
159
160 function displayView($view, $id = 0)
161 {
162 $tpl_vars = array();
163
164 switch ($view)
165 {
166 case 'config':
167 $dev = $this->getPocheVersion('dev');
168 $prod = $this->getPocheVersion('prod');
169 $compare_dev = version_compare(POCHE_VERSION, $dev);
170 $compare_prod = version_compare(POCHE_VERSION, $prod);
171 $tpl_vars = array(
172 'dev' => $dev,
173 'prod' => $prod,
174 'compare_dev' => $compare_dev,
175 'compare_prod' => $compare_prod,
176 );
177 Tools::logm('config view');
178 break;
179 case 'view':
180 $entry = $this->store->retrieveOneById($id, $this->user->getId());
181 if ($entry != NULL) {
182 Tools::logm('view link #' . $id);
183 $content = $entry['content'];
184 if (function_exists('tidy_parse_string')) {
185 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
186 $tidy->cleanRepair();
187 $content = $tidy->value;
188 }
189 $tpl_vars = array(
190 'entry' => $entry,
191 'content' => $content,
192 );
193 }
194 else {
195 Tools::logm('error in view call : entry is NULL');
196 }
197 break;
198 default: # home view
199 $entries = $this->store->getEntriesByView($view, $this->user->getId());
200 $this->pagination->set_total(count($entries));
201 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
202 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
203 $tpl_vars = array(
204 'entries' => $datas,
205 'page_links' => $page_links,
206 );
207 Tools::logm('display ' . $view . ' view');
208 break;
209 }
210
211 return $tpl_vars;
212 }
213
214 public function updatePassword()
215 {
216 if (MODE_DEMO) {
217 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
218 Tools::logm('in demo mode, you can\'t do this');
219 Tools::redirect('?view=config');
220 }
221 else {
222 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
223 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
224 $this->messages->add('s', _('your password has been updated'));
225 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
226 Session::logout();
227 Tools::logm('password updated');
228 Tools::redirect();
229 }
230 else {
231 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
232 Tools::redirect('?view=config');
233 }
234 }
235 }
236 }
237
238 public function login($referer)
239 {
240 if (!empty($_POST['login']) && !empty($_POST['password'])) {
241 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
242 if ($user != array()) {
243 # Save login into Session
244 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
245
246 $this->messages->add('s', _('welcome to your poche'));
247 if (!empty($_POST['longlastingsession'])) {
248 $_SESSION['longlastingsession'] = 31536000;
249 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
250 session_set_cookie_params($_SESSION['longlastingsession']);
251 } else {
252 session_set_cookie_params(0);
253 }
254 session_regenerate_id(true);
255 Tools::logm('login successful');
256 Tools::redirect($referer);
257 }
258 $this->messages->add('e', _('login failed: bad login or password'));
259 Tools::logm('login failed');
260 Tools::redirect();
261 } else {
262 $this->messages->add('e', _('login failed: you have to fill all fields'));
263 Tools::logm('login failed');
264 Tools::redirect();
265 }
266 }
267
268 public function logout()
269 {
270 $this->messages->add('s', _('see you soon!'));
271 Tools::logm('logout');
272 $this->user = array();
273 Session::logout();
274 Tools::redirect();
275 }
276
277 private function importFromInstapaper()
278 {
279 # TODO gestion des articles favs
280 $html = new simple_html_dom();
281 $html->load_file('./instapaper-export.html');
282
283 $read = 0;
284 $errors = array();
285 foreach($html->find('ol') as $ul)
286 {
287 foreach($ul->find('li') as $li)
288 {
289 $a = $li->find('a');
290 $url = new Url(base64_encode($a[0]->href));
291 $this->action('add', $url);
292 if ($read == '1') {
293 $last_id = $this->store->getLastId();
294 $this->action('toggle_archive', $url, $last_id);
295 }
296 }
297
298 # the second <ol> is for read links
299 $read = 1;
300 }
301 $this->messages->add('s', _('import from instapaper completed'));
302 Tools::logm('import from instapaper completed');
303 Tools::redirect();
304 }
305
306 private function importFromPocket()
307 {
308 # TODO gestion des articles favs
309 $html = new simple_html_dom();
310 $html->load_file('./ril_export.html');
311
312 $read = 0;
313 $errors = array();
314 foreach($html->find('ul') as $ul)
315 {
316 foreach($ul->find('li') as $li)
317 {
318 $a = $li->find('a');
319 $url = new Url(base64_encode($a[0]->href));
320 $this->action('add', $url);
321 if ($read == '1') {
322 $last_id = $this->store->getLastId();
323 $this->action('toggle_archive', $url, $last_id);
324 }
325 }
326
327 # the second <ul> is for read links
328 $read = 1;
329 }
330 $this->messages->add('s', _('import from pocket completed'));
331 Tools::logm('import from pocket completed');
332 Tools::redirect();
333 }
334
335 private function importFromReadability()
336 {
337 # TODO gestion des articles lus / favs
338 $str_data = file_get_contents("./readability");
339 $data = json_decode($str_data,true);
340
341 foreach ($data as $key => $value) {
342 $url = '';
343 foreach ($value as $attr => $attr_value) {
344 if ($attr == 'article__url') {
345 $url = new Url(base64_encode($attr_value));
346 }
347 // if ($attr_value == 'favorite' && $attr_value == 'true') {
348 // $last_id = $this->store->getLastId();
349 // $this->store->favoriteById($last_id);
350 // $this->action('toogle_fav', $url, $last_id);
351 // }
352 // if ($attr_value == 'archive' && $attr_value == 'true') {
353 // $last_id = $this->store->getLastId();
354 // $this->action('toggle_archive', $url, $last_id);
355 // }
356 }
357 if ($url->isCorrect())
358 $this->action('add', $url);
359 }
360 $this->messages->add('s', _('import from Readability completed'));
361 Tools::logm('import from Readability completed');
362 Tools::redirect();
363 }
364
365 public function import($from)
366 {
367 if ($from == 'pocket') {
368 $this->importFromPocket();
369 }
370 else if ($from == 'readability') {
371 $this->importFromReadability();
372 }
373 else if ($from == 'instapaper') {
374 $this->importFromInstapaper();
375 }
376 }
377
378 public function export()
379 {
380 $entries = $this->store->retrieveAll($this->user->getId());
381 echo $this->tpl->render('export.twig', array(
382 'export' => Tools::renderJson($entries),
383 ));
384 Tools::logm('export view');
385 }
386
387 private function getPocheVersion($which = 'prod')
388 {
389 $cache_file = CACHE . '/' . $which;
390 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
391 $version = file_get_contents($cache_file);
392 } else {
393 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
394 file_put_contents($cache_file, $version, LOCK_EX);
395 }
396 return $version;
397 }
398 }