]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Poche.class.php
Add SHAARLI support to view template
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
CommitLineData
eb1af592
NL
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 Poche
12{
7ce7ec4c 13 public $user;
eb1af592
NL
14 public $store;
15 public $tpl;
55821e04 16 public $messages;
6a361945 17 public $pagination;
eb1af592 18
bc1ee852 19 function __construct()
eb1af592 20 {
4a291288
NL
21 $this->initTpl();
22 if (!$this->checkBeforeInstall()) {
23 exit;
6c158544 24 }
bc1ee852 25 $this->store = new Database();
eb1af592 26 $this->init();
6a361945 27 $this->messages = new Messages();
eb1af592
NL
28
29 # installation
30 if(!$this->store->isInstalled())
31 {
32 $this->install();
33 }
eb1af592
NL
34 }
35
4a291288
NL
36 /**
37 * all checks before installation.
f3e4f109 38 * @return boolean
4a291288
NL
39 */
40 private function checkBeforeInstall()
eb1af592 41 {
4a291288
NL
42 $msg = '';
43 $allIsGood = TRUE;
bb5a7d9e 44
abed0f21
NL
45 if (!is_writable(CACHE)) {
46 Tools::logm('you don\'t have write access on cache directory');
47 die('You don\'t have write access on cache directory.');
48 }
49 else if (file_exists('./install/update.php') && !DEBUG_POCHE) {
f3e4f109 50 $msg = 'A poche update is needed. Please execute this update <a href="install/update.php">by clicking here</a>. If you have already do the update, please delete /install folder.';
bb5a7d9e
NL
51 $allIsGood = FALSE;
52 }
53 else if (file_exists('./install') && !DEBUG_POCHE) {
4a291288
NL
54 $msg = 'If you want to update your poche, you just have to delete /install folder. <br />To install your poche with sqlite, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.';
55 $allIsGood = FALSE;
56 }
bb5a7d9e
NL
57 else if (STORAGE == 'sqlite' && !is_writable(STORAGE_SQLITE)) {
58 Tools::logm('you don\'t have write access on sqlite file');
59 $msg = 'You don\'t have write access on sqlite file.';
60 $allIsGood = FALSE;
61 }
f3e4f109 62
4a291288
NL
63 if (!$allIsGood) {
64 echo $this->tpl->render('error.twig', array(
65 'msg' => $msg
66 ));
8d3275be 67 }
7ce7ec4c 68
4a291288
NL
69 return $allIsGood;
70 }
eb1af592 71
4a291288
NL
72 private function initTpl()
73 {
eb1af592
NL
74 # template engine
75 $loader = new Twig_Loader_Filesystem(TPL);
bc1ee852
NL
76 if (DEBUG_POCHE) {
77 $twig_params = array();
78 }
79 else {
80 $twig_params = array('cache' => CACHE);
81 }
82 $this->tpl = new Twig_Environment($loader, $twig_params);
eb1af592 83 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
55821e04
NL
84 # filter to display domain name of an url
85 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
86 $this->tpl->addFilter($filter);
eb1af592 87
d9178758
NL
88 # filter for reading time
89 $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime');
90 $this->tpl->addFilter($filter);
4a291288
NL
91 }
92
f3e4f109 93 private function init()
4a291288
NL
94 {
95 Tools::initPhp();
96 Session::init();
97
98 if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
99 $this->user = $_SESSION['poche_user'];
100 }
101 else {
102 # fake user, just for install & login screens
103 $this->user = new User();
104 $this->user->setConfig($this->getDefaultConfig());
105 }
106
107 # l10n
108 $language = $this->user->getConfigValue('language');
109 putenv('LC_ALL=' . $language);
110 setlocale(LC_ALL, $language);
f3e4f109
NF
111 bindtextdomain($language, LOCALE);
112 textdomain($language);
d9178758 113
7ce7ec4c 114 # Pagination
8d3275be 115 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
eb1af592
NL
116 }
117
f3e4f109 118 private function install()
eb1af592
NL
119 {
120 Tools::logm('poche still not installed');
121 echo $this->tpl->render('install.twig', array(
6a361945 122 'token' => Session::getToken()
eb1af592
NL
123 ));
124 if (isset($_GET['install'])) {
f3e4f109 125 if (($_POST['password'] == $_POST['password_repeat'])
eb1af592
NL
126 && $_POST['password'] != "" && $_POST['login'] != "") {
127 # let's rock, install poche baby !
bb5a7d9e
NL
128 if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])))
129 {
130 Session::logout();
131 Tools::logm('poche is now installed');
132 Tools::redirect();
133 }
6a361945
NL
134 }
135 else {
136 Tools::logm('error during installation');
eb1af592
NL
137 Tools::redirect();
138 }
139 }
140 exit();
141 }
142
8d3275be
NL
143 public function getDefaultConfig()
144 {
145 return array(
146 'pager' => PAGINATION,
147 'language' => LANG,
148 );
149 }
150
eb1af592
NL
151 /**
152 * Call action (mark as fav, archive, delete, etc.)
153 */
b916bcfc 154 public function action($action, Url $url, $id = 0, $import = FALSE)
eb1af592
NL
155 {
156 switch ($action)
157 {
158 case 'add':
159 if($parametres_url = $url->fetchContent()) {
8d3275be 160 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
eb1af592 161 Tools::logm('add link ' . $url->getUrl());
b916bcfc
NL
162 $sequence = '';
163 if (STORAGE == 'postgres') {
164 $sequence = 'entries_id_seq';
165 }
166 $last_id = $this->store->getLastId($sequence);
eb1af592
NL
167 if (DOWNLOAD_PICTURES) {
168 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
b58e261d
NL
169 Tools::logm('updating content article');
170 $this->store->updateContent($last_id, $content, $this->user->getId());
eb1af592 171 }
b916bcfc
NL
172 if (!$import) {
173 $this->messages->add('s', _('the link has been added successfully'));
174 }
eb1af592
NL
175 }
176 else {
b916bcfc
NL
177 if (!$import) {
178 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
179 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
180 }
eb1af592
NL
181 }
182 }
183 else {
b916bcfc
NL
184 if (!$import) {
185 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
186 Tools::logm('error during content fetch ' . $url->getUrl());
187 }
188 }
189 if (!$import) {
190 Tools::redirect();
eb1af592
NL
191 }
192 break;
193 case 'delete':
bc1ee852 194 $msg = 'delete link #' . $id;
8d3275be 195 if ($this->store->deleteById($id, $this->user->getId())) {
eb1af592
NL
196 if (DOWNLOAD_PICTURES) {
197 remove_directory(ABS_PATH . $id);
198 }
6a361945 199 $this->messages->add('s', _('the link has been deleted successfully'));
eb1af592
NL
200 }
201 else {
6a361945 202 $this->messages->add('e', _('the link wasn\'t deleted'));
bc1ee852 203 $msg = 'error : can\'t delete link #' . $id;
eb1af592 204 }
bc1ee852
NL
205 Tools::logm($msg);
206 Tools::redirect('?');
eb1af592
NL
207 break;
208 case 'toggle_fav' :
8d3275be 209 $this->store->favoriteById($id, $this->user->getId());
eb1af592 210 Tools::logm('mark as favorite link #' . $id);
b916bcfc
NL
211 if (!$import) {
212 Tools::redirect();
213 }
eb1af592
NL
214 break;
215 case 'toggle_archive' :
8d3275be 216 $this->store->archiveById($id, $this->user->getId());
eb1af592 217 Tools::logm('archive link #' . $id);
b916bcfc
NL
218 if (!$import) {
219 Tools::redirect();
220 }
eb1af592
NL
221 break;
222 default:
c0d321c1 223 Tools::logm('action ' . $action . 'doesn\'t exist');
eb1af592
NL
224 break;
225 }
226 }
227
228 function displayView($view, $id = 0)
229 {
230 $tpl_vars = array();
231
232 switch ($view)
233 {
eb1af592 234 case 'config':
32520785
NL
235 $dev = $this->getPocheVersion('dev');
236 $prod = $this->getPocheVersion('prod');
237 $compare_dev = version_compare(POCHE_VERSION, $dev);
238 $compare_prod = version_compare(POCHE_VERSION, $prod);
239 $tpl_vars = array(
240 'dev' => $dev,
241 'prod' => $prod,
242 'compare_dev' => $compare_dev,
243 'compare_prod' => $compare_prod,
244 );
eb1af592
NL
245 Tools::logm('config view');
246 break;
247 case 'view':
8d3275be 248 $entry = $this->store->retrieveOneById($id, $this->user->getId());
eb1af592
NL
249 if ($entry != NULL) {
250 Tools::logm('view link #' . $id);
251 $content = $entry['content'];
252 if (function_exists('tidy_parse_string')) {
253 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
254 $tidy->cleanRepair();
255 $content = $tidy->value;
256 }
257 $tpl_vars = array(
258 'entry' => $entry,
259 'content' => $content,
260 );
261 }
262 else {
d8d1542e 263 Tools::logm('error in view call : entry is null');
eb1af592
NL
264 }
265 break;
266 default: # home view
8d3275be 267 $entries = $this->store->getEntriesByView($view, $this->user->getId());
6a361945
NL
268 $this->pagination->set_total(count($entries));
269 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
8d3275be 270 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
eb1af592 271 $tpl_vars = array(
6a361945
NL
272 'entries' => $datas,
273 'page_links' => $page_links,
eb1af592 274 );
6a361945 275 Tools::logm('display ' . $view . ' view');
eb1af592
NL
276 break;
277 }
278
279 return $tpl_vars;
280 }
c765c367 281
07ee09f4 282 /**
f3e4f109
NF
283 * update the password of the current user.
284 * if MODE_DEMO is TRUE, the password can't be updated.
07ee09f4
NL
285 * @todo add the return value
286 * @todo set the new password in function header like this updatePassword($newPassword)
287 * @return boolean
288 */
c765c367
NL
289 public function updatePassword()
290 {
55821e04 291 if (MODE_DEMO) {
8d3275be 292 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
55821e04 293 Tools::logm('in demo mode, you can\'t do this');
6a361945 294 Tools::redirect('?view=config');
55821e04
NL
295 }
296 else {
297 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
298 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
8d3275be
NL
299 $this->messages->add('s', _('your password has been updated'));
300 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
c765c367 301 Session::logout();
8d3275be 302 Tools::logm('password updated');
c765c367
NL
303 Tools::redirect();
304 }
305 else {
8d3275be 306 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
6a361945 307 Tools::redirect('?view=config');
c765c367
NL
308 }
309 }
310 }
311 }
312
07ee09f4
NL
313 /**
314 * checks if login & password are correct and save the user in session.
315 * it redirects the user to the $referer link
316 * @param string $referer the url to redirect after login
317 * @todo add the return value
318 * @return boolean
319 */
c765c367
NL
320 public function login($referer)
321 {
322 if (!empty($_POST['login']) && !empty($_POST['password'])) {
7ce7ec4c
NL
323 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
324 if ($user != array()) {
325 # Save login into Session
326 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
327
8d3275be 328 $this->messages->add('s', _('welcome to your poche'));
c765c367
NL
329 if (!empty($_POST['longlastingsession'])) {
330 $_SESSION['longlastingsession'] = 31536000;
331 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
332 session_set_cookie_params($_SESSION['longlastingsession']);
333 } else {
334 session_set_cookie_params(0);
335 }
336 session_regenerate_id(true);
8d3275be 337 Tools::logm('login successful');
c765c367
NL
338 Tools::redirect($referer);
339 }
8d3275be 340 $this->messages->add('e', _('login failed: bad login or password'));
c765c367
NL
341 Tools::logm('login failed');
342 Tools::redirect();
343 } else {
8d3275be 344 $this->messages->add('e', _('login failed: you have to fill all fields'));
c765c367
NL
345 Tools::logm('login failed');
346 Tools::redirect();
347 }
348 }
349
07ee09f4
NL
350 /**
351 * log out the poche user. It cleans the session.
352 * @todo add the return value
f3e4f109 353 * @return boolean
07ee09f4 354 */
c765c367
NL
355 public function logout()
356 {
7ce7ec4c 357 $this->user = array();
c765c367 358 Session::logout();
b916bcfc
NL
359 $this->messages->add('s', _('see you soon!'));
360 Tools::logm('logout');
c765c367
NL
361 Tools::redirect();
362 }
363
07ee09f4
NL
364 /**
365 * import from Instapaper. poche needs a ./instapaper-export.html file
366 * @todo add the return value
367 * @return boolean
368 */
63c35580 369 private function importFromInstapaper()
c765c367 370 {
7f959169 371 # TODO gestion des articles favs
a62788c6
NL
372 $html = new simple_html_dom();
373 $html->load_file('./instapaper-export.html');
b916bcfc 374 Tools::logm('starting import from instapaper');
a62788c6
NL
375
376 $read = 0;
377 $errors = array();
378 foreach($html->find('ol') as $ul)
379 {
380 foreach($ul->find('li') as $li)
381 {
382 $a = $li->find('a');
383 $url = new Url(base64_encode($a[0]->href));
b916bcfc 384 $this->action('add', $url, 0, TRUE);
a62788c6 385 if ($read == '1') {
b916bcfc
NL
386 $sequence = '';
387 if (STORAGE == 'postgres') {
388 $sequence = 'entries_id_seq';
389 }
390 $last_id = $this->store->getLastId($sequence);
391 $this->action('toggle_archive', $url, $last_id, TRUE);
a62788c6
NL
392 }
393 }
7f959169
NL
394
395 # the second <ol> is for read links
a62788c6
NL
396 $read = 1;
397 }
8d3275be 398 $this->messages->add('s', _('import from instapaper completed'));
63c35580
NL
399 Tools::logm('import from instapaper completed');
400 Tools::redirect();
401 }
c765c367 402
07ee09f4
NL
403 /**
404 * import from Pocket. poche needs a ./ril_export.html file
405 * @todo add the return value
f3e4f109 406 * @return boolean
07ee09f4 407 */
63c35580
NL
408 private function importFromPocket()
409 {
7f959169 410 # TODO gestion des articles favs
63c35580
NL
411 $html = new simple_html_dom();
412 $html->load_file('./ril_export.html');
b916bcfc 413 Tools::logm('starting import from pocket');
63c35580
NL
414
415 $read = 0;
416 $errors = array();
417 foreach($html->find('ul') as $ul)
418 {
419 foreach($ul->find('li') as $li)
c765c367 420 {
63c35580
NL
421 $a = $li->find('a');
422 $url = new Url(base64_encode($a[0]->href));
b916bcfc 423 $this->action('add', $url, 0, TRUE);
63c35580 424 if ($read == '1') {
b916bcfc
NL
425 $sequence = '';
426 if (STORAGE == 'postgres') {
427 $sequence = 'entries_id_seq';
428 }
429 $last_id = $this->store->getLastId($sequence);
430 $this->action('toggle_archive', $url, $last_id, TRUE);
c765c367 431 }
c765c367 432 }
f3e4f109 433
7f959169 434 # the second <ul> is for read links
63c35580 435 $read = 1;
c765c367 436 }
8d3275be 437 $this->messages->add('s', _('import from pocket completed'));
63c35580
NL
438 Tools::logm('import from pocket completed');
439 Tools::redirect();
440 }
c765c367 441
07ee09f4
NL
442 /**
443 * import from Readability. poche needs a ./readability file
444 * @todo add the return value
f3e4f109 445 * @return boolean
07ee09f4 446 */
63c35580
NL
447 private function importFromReadability()
448 {
7f959169 449 # TODO gestion des articles lus / favs
63c35580
NL
450 $str_data = file_get_contents("./readability");
451 $data = json_decode($str_data,true);
b916bcfc 452 Tools::logm('starting import from Readability');
c0d321c1 453 $count = 0;
63c35580 454 foreach ($data as $key => $value) {
c0d321c1
NL
455 $url = NULL;
456 $favorite = FALSE;
457 $archive = FALSE;
7f959169
NL
458 foreach ($value as $attr => $attr_value) {
459 if ($attr == 'article__url') {
460 $url = new Url(base64_encode($attr_value));
c765c367 461 }
b916bcfc
NL
462 $sequence = '';
463 if (STORAGE == 'postgres') {
464 $sequence = 'entries_id_seq';
465 }
c0d321c1
NL
466 if ($attr_value == 'true') {
467 if ($attr == 'favorite') {
468 $favorite = TRUE;
469 }
470 if ($attr == 'archive') {
471 $archive = TRUE;
472 }
473 }
474 }
475 # we can add the url
476 if (!is_null($url) && $url->isCorrect()) {
477 $this->action('add', $url, 0, TRUE);
478 $count++;
479 if ($favorite) {
480 $last_id = $this->store->getLastId($sequence);
481 $this->action('toggle_fav', $url, $last_id, TRUE);
482 }
483 if ($archive) {
b916bcfc
NL
484 $last_id = $this->store->getLastId($sequence);
485 $this->action('toggle_archive', $url, $last_id, TRUE);
486 }
c765c367 487 }
c765c367 488 }
c0d321c1 489 $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.'));
63c35580
NL
490 Tools::logm('import from Readability completed');
491 Tools::redirect();
c765c367
NL
492 }
493
07ee09f4
NL
494 /**
495 * import datas into your poche
496 * @param string $from name of the service to import : pocket, instapaper or readability
497 * @todo add the return value
f3e4f109 498 * @return boolean
07ee09f4 499 */
63c35580 500 public function import($from)
c765c367 501 {
63c35580 502 if ($from == 'pocket') {
07ee09f4 503 return $this->importFromPocket();
63c35580
NL
504 }
505 else if ($from == 'readability') {
07ee09f4 506 return $this->importFromReadability();
63c35580
NL
507 }
508 else if ($from == 'instapaper') {
07ee09f4 509 return $this->importFromInstapaper();
63c35580
NL
510 }
511 }
c765c367 512
07ee09f4
NL
513 /**
514 * export poche entries in json
515 * @return json all poche entries
516 */
63c35580
NL
517 public function export()
518 {
8d3275be 519 $entries = $this->store->retrieveAll($this->user->getId());
63c35580
NL
520 echo $this->tpl->render('export.twig', array(
521 'export' => Tools::renderJson($entries),
522 ));
523 Tools::logm('export view');
c765c367 524 }
32520785 525
07ee09f4 526 /**
a3436d4c 527 * Checks online the latest version of poche and cache it
07ee09f4
NL
528 * @param string $which 'prod' or 'dev'
529 * @return string latest $which version
530 */
32520785
NL
531 private function getPocheVersion($which = 'prod')
532 {
533 $cache_file = CACHE . '/' . $which;
a3436d4c
NL
534
535 # checks if the cached version file exists
32520785
NL
536 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
537 $version = file_get_contents($cache_file);
538 } else {
bc1ee852 539 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
32520785
NL
540 file_put_contents($cache_file, $version, LOCK_EX);
541 }
542 return $version;
543 }
eb1af592 544}