]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Poche.class.php
bug fix #227: Deleting element in archive redirect to home
[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{
00dbaf90
NL
13 public static $canRenderTemplates = true;
14 public static $configFileAvailable = true;
15
7ce7ec4c 16 public $user;
eb1af592
NL
17 public $store;
18 public $tpl;
55821e04 19 public $messages;
6a361945 20 public $pagination;
00dbaf90
NL
21
22 private $currentTheme = '';
23 private $notInstalledMessage = '';
24
25 # @todo make this dynamic (actually install themes and save them in the database including author information et cetera)
26 private $installedThemes = array(
27 'default' => array('requires' => array()),
28 'dark' => array('requires' => array('default')),
29 'dmagenta' => array('requires' => array('default')),
30 'solarized' => array('requires' => array('default')),
31 'solarized-dark' => array('requires' => array('default'))
32 );
eb1af592 33
00dbaf90 34 public function __construct()
eb1af592 35 {
00dbaf90
NL
36 if (! $this->configFileIsAvailable()) {
37 return;
38 }
39
40 $this->init();
41
42 if (! $this->themeIsInstalled()) {
43 return;
44 }
45
4a291288 46 $this->initTpl();
00dbaf90
NL
47
48 if (! $this->systemIsInstalled()) {
49 return;
6c158544 50 }
00dbaf90 51
bc1ee852 52 $this->store = new Database();
6a361945 53 $this->messages = new Messages();
eb1af592
NL
54
55 # installation
00dbaf90 56 if (! $this->store->isInstalled()) {
eb1af592
NL
57 $this->install();
58 }
eb1af592 59 }
00dbaf90
NL
60
61 private function init()
62 {
63 Tools::initPhp();
64 Session::$sessionName = 'poche';
65 Session::init();
eb1af592 66
00dbaf90
NL
67 if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
68 $this->user = $_SESSION['poche_user'];
69 } else {
70 # fake user, just for install & login screens
71 $this->user = new User();
72 $this->user->setConfig($this->getDefaultConfig());
73 }
74
75 # l10n
76 $language = $this->user->getConfigValue('language');
77 putenv('LC_ALL=' . $language);
78 setlocale(LC_ALL, $language);
79 bindtextdomain($language, LOCALE);
80 textdomain($language);
81
82 # Pagination
83 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
84
85 # Set up theme
86 $themeDirectory = $this->user->getConfigValue('theme');
87
88 if ($themeDirectory === false) {
89 $themeDirectory = DEFAULT_THEME;
90 }
91
92 $this->currentTheme = $themeDirectory;
93 }
94
95 public function configFileIsAvailable() {
96 if (! self::$configFileAvailable) {
97 $this->notInstalledMessage = 'You have to rename <strong>inc/poche/config.inc.php.new</strong> to <strong>inc/poche/config.inc.php</strong>.';
98
99 return false;
100 }
101
102 return true;
103 }
104
105 public function themeIsInstalled() {
106 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
107 if (! self::$canRenderTemplates) {
108 $this->notInstalledMessage = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://inthepoche.com/?pages/Documentation">the documentation.</a>';
109
110 return false;
111 }
7f17a38d
NL
112
113 if (! is_writable(CACHE)) {
114 $this->notInstalledMessage = '<h1>error</h1><p>You don\'t have write access on cache directory.</p>';
115
116 self::$canRenderTemplates = false;
117
118 return false;
119 }
00dbaf90
NL
120
121 # Check if the selected theme and its requirements are present
122 if (! is_dir(THEME . '/' . $this->getTheme())) {
123 $this->notInstalledMessage = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')';
124
125 self::$canRenderTemplates = false;
126
127 return false;
128 }
129
130 foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
131 if (! is_dir(THEME . '/' . $requiredTheme)) {
132 $this->notInstalledMessage = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')';
133
134 self::$canRenderTemplates = false;
135
136 return false;
137 }
138 }
139
140 return true;
141 }
142
4a291288
NL
143 /**
144 * all checks before installation.
00dbaf90 145 * @todo move HTML to template
4a291288
NL
146 * @return boolean
147 */
00dbaf90 148 public function systemIsInstalled()
eb1af592 149 {
4a291288 150 $msg = '';
00dbaf90
NL
151
152 $configSalt = defined('SALT') ? constant('SALT') : '';
153
154 if (empty($configSalt)) {
155 $msg = '<h1>error</h1><p>You have not yet filled in the SALT value in the config.inc.php file.</p>';
00dbaf90
NL
156 } else if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) {
157 Tools::logm('sqlite file doesn\'t exist');
7bda34c6 158 $msg = '<h1>error</h1><p>sqlite file doesn\'t exist, you can find it in install folder. Copy it in /db folder.</p>';
00dbaf90 159 } else if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) {
7f17a38d 160 $msg = '<h1>install folder</h1><p>you have to delete the /install folder before using poche.</p>';
00dbaf90 161 } else if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) {
bb5a7d9e 162 Tools::logm('you don\'t have write access on sqlite file');
6d7defc8 163 $msg = '<h1>error</h1><p>You don\'t have write access on sqlite file.</p>';
bb5a7d9e 164 }
00dbaf90
NL
165
166 if (! empty($msg)) {
167 $this->notInstalledMessage = $msg;
168
169 return false;
8d3275be 170 }
7ce7ec4c 171
00dbaf90
NL
172 return true;
173 }
174
175 public function getNotInstalledMessage() {
176 return $this->notInstalledMessage;
4a291288 177 }
eb1af592 178
4a291288
NL
179 private function initTpl()
180 {
00dbaf90
NL
181 $loaderChain = new Twig_Loader_Chain();
182
183 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
184 try {
185 $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme()));
186 } catch (Twig_Error_Loader $e) {
187 # @todo isInstalled() should catch this, inject Twig later
188 die('The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (' . THEME . '/' . $this->getTheme() .' is missing)');
189 }
190
191 # add all required themes to the loader chain
192 foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) {
193 try {
194 $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . DEFAULT_THEME));
195 } catch (Twig_Error_Loader $e) {
196 # @todo isInstalled() should catch this, inject Twig later
197 die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')');
198 }
199 }
200
bc1ee852
NL
201 if (DEBUG_POCHE) {
202 $twig_params = array();
00dbaf90 203 } else {
bc1ee852
NL
204 $twig_params = array('cache' => CACHE);
205 }
00dbaf90
NL
206
207 $this->tpl = new Twig_Environment($loaderChain, $twig_params);
eb1af592 208 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
00dbaf90 209
55821e04
NL
210 # filter to display domain name of an url
211 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
212 $this->tpl->addFilter($filter);
eb1af592 213
d9178758
NL
214 # filter for reading time
215 $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime');
216 $this->tpl->addFilter($filter);
00dbaf90
NL
217
218 # filter for simple filenames in config view
219 $filter = new Twig_SimpleFilter('getPrettyFilename', function($string) { return str_replace(ROOT, '', $string); });
220 $this->tpl->addFilter($filter);
eb1af592
NL
221 }
222
223 private function install()
224 {
225 Tools::logm('poche still not installed');
226 echo $this->tpl->render('install.twig', array(
00dbaf90
NL
227 'token' => Session::getToken(),
228 'theme' => $this->getTheme(),
229 'poche_url' => Tools::getPocheUrl()
eb1af592
NL
230 ));
231 if (isset($_GET['install'])) {
232 if (($_POST['password'] == $_POST['password_repeat'])
233 && $_POST['password'] != "" && $_POST['login'] != "") {
234 # let's rock, install poche baby !
bb5a7d9e
NL
235 if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])))
236 {
237 Session::logout();
238 Tools::logm('poche is now installed');
239 Tools::redirect();
240 }
6a361945
NL
241 }
242 else {
243 Tools::logm('error during installation');
eb1af592
NL
244 Tools::redirect();
245 }
246 }
247 exit();
248 }
00dbaf90
NL
249
250 public function getTheme() {
251 return $this->currentTheme;
252 }
253
254 public function getInstalledThemes() {
255 $handle = opendir(THEME);
256 $themes = array();
257
258 while (($theme = readdir($handle)) !== false) {
259 # Themes are stored in a directory, so all directory names are themes
260 # @todo move theme installation data to database
3cc22aab 261 if (! is_dir(THEME . '/' . $theme) || in_array($theme, array('..', '.', '.git'))) {
00dbaf90
NL
262 continue;
263 }
264
265 $current = false;
266
267 if ($theme === $this->getTheme()) {
268 $current = true;
269 }
270
271 $themes[] = array('name' => $theme, 'current' => $current);
272 }
273
274 return $themes;
275 }
eb1af592 276
8d3275be 277 public function getDefaultConfig()
00dbaf90 278 {
8d3275be
NL
279 return array(
280 'pager' => PAGINATION,
281 'language' => LANG,
00dbaf90
NL
282 'theme' => DEFAULT_THEME
283 );
8d3275be
NL
284 }
285
eb1af592
NL
286 /**
287 * Call action (mark as fav, archive, delete, etc.)
288 */
b916bcfc 289 public function action($action, Url $url, $id = 0, $import = FALSE)
eb1af592
NL
290 {
291 switch ($action)
292 {
293 case 'add':
ec397236
NL
294 $content = $url->extract();
295
296 if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) {
297 Tools::logm('add link ' . $url->getUrl());
298 $sequence = '';
299 if (STORAGE == 'postgres') {
300 $sequence = 'entries_id_seq';
eb1af592 301 }
ec397236
NL
302 $last_id = $this->store->getLastId($sequence);
303 if (DOWNLOAD_PICTURES) {
6fb46003 304 $content = filtre_picture($content['body'], $url->getUrl(), $last_id);
ec397236
NL
305 Tools::logm('updating content article');
306 $this->store->updateContent($last_id, $content, $this->user->getId());
307 }
308 if (!$import) {
309 $this->messages->add('s', _('the link has been added successfully'));
eb1af592
NL
310 }
311 }
312 else {
b916bcfc 313 if (!$import) {
ec397236
NL
314 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
315 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
b916bcfc
NL
316 }
317 }
ec397236 318
b916bcfc 319 if (!$import) {
ce4a1dcc 320 Tools::redirect('?view=home');
eb1af592
NL
321 }
322 break;
323 case 'delete':
bc1ee852 324 $msg = 'delete link #' . $id;
8d3275be 325 if ($this->store->deleteById($id, $this->user->getId())) {
eb1af592
NL
326 if (DOWNLOAD_PICTURES) {
327 remove_directory(ABS_PATH . $id);
328 }
6a361945 329 $this->messages->add('s', _('the link has been deleted successfully'));
eb1af592
NL
330 }
331 else {
6a361945 332 $this->messages->add('e', _('the link wasn\'t deleted'));
bc1ee852 333 $msg = 'error : can\'t delete link #' . $id;
eb1af592 334 }
bc1ee852 335 Tools::logm($msg);
6cd8af85 336 Tools::redirect();
eb1af592
NL
337 break;
338 case 'toggle_fav' :
8d3275be 339 $this->store->favoriteById($id, $this->user->getId());
eb1af592 340 Tools::logm('mark as favorite link #' . $id);
b916bcfc
NL
341 if (!$import) {
342 Tools::redirect();
343 }
eb1af592
NL
344 break;
345 case 'toggle_archive' :
8d3275be 346 $this->store->archiveById($id, $this->user->getId());
eb1af592 347 Tools::logm('archive link #' . $id);
b916bcfc
NL
348 if (!$import) {
349 Tools::redirect();
350 }
eb1af592
NL
351 break;
352 default:
353 break;
354 }
355 }
356
357 function displayView($view, $id = 0)
358 {
359 $tpl_vars = array();
360
361 switch ($view)
362 {
eb1af592 363 case 'config':
32520785
NL
364 $dev = $this->getPocheVersion('dev');
365 $prod = $this->getPocheVersion('prod');
366 $compare_dev = version_compare(POCHE_VERSION, $dev);
367 $compare_prod = version_compare(POCHE_VERSION, $prod);
00dbaf90 368 $themes = $this->getInstalledThemes();
32520785 369 $tpl_vars = array(
00dbaf90 370 'themes' => $themes,
32520785
NL
371 'dev' => $dev,
372 'prod' => $prod,
373 'compare_dev' => $compare_dev,
374 'compare_prod' => $compare_prod,
375 );
eb1af592
NL
376 Tools::logm('config view');
377 break;
378 case 'view':
8d3275be 379 $entry = $this->store->retrieveOneById($id, $this->user->getId());
eb1af592
NL
380 if ($entry != NULL) {
381 Tools::logm('view link #' . $id);
382 $content = $entry['content'];
383 if (function_exists('tidy_parse_string')) {
384 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
385 $tidy->cleanRepair();
386 $content = $tidy->value;
3408ed48 387 }
a3223127 388
3408ed48
NL
389 # flattr checking
390 $flattr = new FlattrItem();
391 $flattr->checkItem($entry['url']);
a3223127 392
3408ed48
NL
393 $tpl_vars = array(
394 'entry' => $entry,
395 'content' => $content,
396 'flattr' => $flattr
397 );
eb1af592
NL
398 }
399 else {
d8d1542e 400 Tools::logm('error in view call : entry is null');
eb1af592
NL
401 }
402 break;
12d9cfbc 403 default: # home, favorites and archive views
8d3275be 404 $entries = $this->store->getEntriesByView($view, $this->user->getId());
eb1af592 405 $tpl_vars = array(
3eb04903
N
406 'entries' => '',
407 'page_links' => '',
7f9f5281 408 'nb_results' => '',
eb1af592 409 );
34d67c83
NL
410
411 # want to display a page too far?
412 if ((count($entries) % PAGINATION) + 1 < $_GET['p']) {
413 Tools::redirect('');
414 }
415
3eb04903
N
416 if (count($entries) > 0) {
417 $this->pagination->set_total(count($entries));
418 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
419 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
420 $tpl_vars['entries'] = $datas;
421 $tpl_vars['page_links'] = $page_links;
7f9f5281 422 $tpl_vars['nb_results'] = count($entries);
3eb04903 423 }
6a361945 424 Tools::logm('display ' . $view . ' view');
eb1af592
NL
425 break;
426 }
427
428 return $tpl_vars;
429 }
c765c367 430
07ee09f4
NL
431 /**
432 * update the password of the current user.
433 * if MODE_DEMO is TRUE, the password can't be updated.
434 * @todo add the return value
435 * @todo set the new password in function header like this updatePassword($newPassword)
436 * @return boolean
437 */
c765c367
NL
438 public function updatePassword()
439 {
55821e04 440 if (MODE_DEMO) {
8d3275be 441 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
55821e04 442 Tools::logm('in demo mode, you can\'t do this');
6a361945 443 Tools::redirect('?view=config');
55821e04
NL
444 }
445 else {
446 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
447 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
8d3275be
NL
448 $this->messages->add('s', _('your password has been updated'));
449 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
c765c367 450 Session::logout();
8d3275be 451 Tools::logm('password updated');
c765c367
NL
452 Tools::redirect();
453 }
454 else {
8d3275be 455 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
6a361945 456 Tools::redirect('?view=config');
c765c367
NL
457 }
458 }
459 }
460 }
00dbaf90
NL
461
462 public function updateTheme()
463 {
464 # no data
465 if (empty($_POST['theme'])) {
466 }
467
468 # we are not going to change it to the current theme...
469 if ($_POST['theme'] == $this->getTheme()) {
470 $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!'));
471 Tools::redirect('?view=config');
472 }
473
474 $themes = $this->getInstalledThemes();
475 $actualTheme = false;
476
477 foreach ($themes as $theme) {
478 if ($theme['name'] == $_POST['theme']) {
479 $actualTheme = true;
480 break;
481 }
482 }
483
484 if (! $actualTheme) {
485 $this->messages->add('e', _('that theme does not seem to be installed'));
486 Tools::redirect('?view=config');
487 }
488
489 $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']);
490 $this->messages->add('s', _('you have changed your theme preferences'));
491
492 $currentConfig = $_SESSION['poche_user']->config;
493 $currentConfig['theme'] = $_POST['theme'];
494
495 $_SESSION['poche_user']->setConfig($currentConfig);
496
497 Tools::redirect('?view=config');
498 }
c765c367 499
07ee09f4
NL
500 /**
501 * checks if login & password are correct and save the user in session.
502 * it redirects the user to the $referer link
503 * @param string $referer the url to redirect after login
504 * @todo add the return value
505 * @return boolean
506 */
c765c367
NL
507 public function login($referer)
508 {
509 if (!empty($_POST['login']) && !empty($_POST['password'])) {
7ce7ec4c
NL
510 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
511 if ($user != array()) {
512 # Save login into Session
513 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
8d3275be 514 $this->messages->add('s', _('welcome to your poche'));
8d3275be 515 Tools::logm('login successful');
c765c367
NL
516 Tools::redirect($referer);
517 }
8d3275be 518 $this->messages->add('e', _('login failed: bad login or password'));
c765c367
NL
519 Tools::logm('login failed');
520 Tools::redirect();
521 } else {
8d3275be 522 $this->messages->add('e', _('login failed: you have to fill all fields'));
c765c367
NL
523 Tools::logm('login failed');
524 Tools::redirect();
525 }
526 }
527
07ee09f4
NL
528 /**
529 * log out the poche user. It cleans the session.
530 * @todo add the return value
531 * @return boolean
532 */
c765c367
NL
533 public function logout()
534 {
7ce7ec4c 535 $this->user = array();
c765c367 536 Session::logout();
b916bcfc
NL
537 $this->messages->add('s', _('see you soon!'));
538 Tools::logm('logout');
c765c367
NL
539 Tools::redirect();
540 }
541
07ee09f4
NL
542 /**
543 * import from Instapaper. poche needs a ./instapaper-export.html file
544 * @todo add the return value
66b6a3b5 545 * @param string $targetFile the file used for importing
07ee09f4
NL
546 * @return boolean
547 */
66b6a3b5 548 private function importFromInstapaper($targetFile)
c765c367 549 {
7f959169 550 # TODO gestion des articles favs
a62788c6 551 $html = new simple_html_dom();
66b6a3b5 552 $html->load_file($targetFile);
b916bcfc 553 Tools::logm('starting import from instapaper');
a62788c6
NL
554
555 $read = 0;
556 $errors = array();
557 foreach($html->find('ol') as $ul)
558 {
559 foreach($ul->find('li') as $li)
560 {
561 $a = $li->find('a');
562 $url = new Url(base64_encode($a[0]->href));
b916bcfc 563 $this->action('add', $url, 0, TRUE);
a62788c6 564 if ($read == '1') {
b916bcfc
NL
565 $sequence = '';
566 if (STORAGE == 'postgres') {
567 $sequence = 'entries_id_seq';
568 }
569 $last_id = $this->store->getLastId($sequence);
570 $this->action('toggle_archive', $url, $last_id, TRUE);
a62788c6
NL
571 }
572 }
7f959169
NL
573
574 # the second <ol> is for read links
a62788c6
NL
575 $read = 1;
576 }
8d3275be 577 $this->messages->add('s', _('import from instapaper completed'));
63c35580
NL
578 Tools::logm('import from instapaper completed');
579 Tools::redirect();
580 }
c765c367 581
07ee09f4
NL
582 /**
583 * import from Pocket. poche needs a ./ril_export.html file
584 * @todo add the return value
66b6a3b5 585 * @param string $targetFile the file used for importing
07ee09f4
NL
586 * @return boolean
587 */
66b6a3b5 588 private function importFromPocket($targetFile)
63c35580 589 {
7f959169 590 # TODO gestion des articles favs
63c35580 591 $html = new simple_html_dom();
66b6a3b5 592 $html->load_file($targetFile);
b916bcfc 593 Tools::logm('starting import from pocket');
63c35580
NL
594
595 $read = 0;
596 $errors = array();
597 foreach($html->find('ul') as $ul)
598 {
599 foreach($ul->find('li') as $li)
c765c367 600 {
63c35580
NL
601 $a = $li->find('a');
602 $url = new Url(base64_encode($a[0]->href));
b916bcfc 603 $this->action('add', $url, 0, TRUE);
63c35580 604 if ($read == '1') {
b916bcfc
NL
605 $sequence = '';
606 if (STORAGE == 'postgres') {
607 $sequence = 'entries_id_seq';
608 }
609 $last_id = $this->store->getLastId($sequence);
610 $this->action('toggle_archive', $url, $last_id, TRUE);
c765c367 611 }
c765c367 612 }
7f959169
NL
613
614 # the second <ul> is for read links
63c35580 615 $read = 1;
c765c367 616 }
8d3275be 617 $this->messages->add('s', _('import from pocket completed'));
63c35580
NL
618 Tools::logm('import from pocket completed');
619 Tools::redirect();
620 }
c765c367 621
07ee09f4
NL
622 /**
623 * import from Readability. poche needs a ./readability file
624 * @todo add the return value
66b6a3b5 625 * @param string $targetFile the file used for importing
07ee09f4
NL
626 * @return boolean
627 */
66b6a3b5 628 private function importFromReadability($targetFile)
63c35580 629 {
7f959169 630 # TODO gestion des articles lus / favs
66b6a3b5 631 $str_data = file_get_contents($targetFile);
63c35580 632 $data = json_decode($str_data,true);
b916bcfc 633 Tools::logm('starting import from Readability');
c0d321c1 634 $count = 0;
63c35580 635 foreach ($data as $key => $value) {
c0d321c1
NL
636 $url = NULL;
637 $favorite = FALSE;
638 $archive = FALSE;
7f959169
NL
639 foreach ($value as $attr => $attr_value) {
640 if ($attr == 'article__url') {
641 $url = new Url(base64_encode($attr_value));
c765c367 642 }
b916bcfc
NL
643 $sequence = '';
644 if (STORAGE == 'postgres') {
645 $sequence = 'entries_id_seq';
646 }
c0d321c1
NL
647 if ($attr_value == 'true') {
648 if ($attr == 'favorite') {
649 $favorite = TRUE;
650 }
651 if ($attr == 'archive') {
652 $archive = TRUE;
653 }
654 }
655 }
656 # we can add the url
657 if (!is_null($url) && $url->isCorrect()) {
658 $this->action('add', $url, 0, TRUE);
659 $count++;
660 if ($favorite) {
661 $last_id = $this->store->getLastId($sequence);
662 $this->action('toggle_fav', $url, $last_id, TRUE);
663 }
664 if ($archive) {
b916bcfc
NL
665 $last_id = $this->store->getLastId($sequence);
666 $this->action('toggle_archive', $url, $last_id, TRUE);
667 }
c765c367 668 }
c765c367 669 }
c0d321c1 670 $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.'));
63c35580
NL
671 Tools::logm('import from Readability completed');
672 Tools::redirect();
c765c367
NL
673 }
674
07ee09f4
NL
675 /**
676 * import datas into your poche
677 * @param string $from name of the service to import : pocket, instapaper or readability
678 * @todo add the return value
679 * @return boolean
680 */
63c35580 681 public function import($from)
c765c367 682 {
66b6a3b5
E
683 $providers = array(
684 'pocket' => 'importFromPocket',
685 'readability' => 'importFromReadability',
686 'instapaper' => 'importFromInstapaper'
687 );
688
689 if (! isset($providers[$from])) {
690 $this->messages->add('e', _('Unknown import provider.'));
691 Tools::redirect();
63c35580 692 }
66b6a3b5
E
693
694 $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE';
695 $targetFile = constant($targetDefinition);
696
697 if (! defined($targetDefinition)) {
698 $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".'));
699 Tools::redirect();
63c35580 700 }
66b6a3b5
E
701
702 if (! file_exists($targetFile)) {
703 $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.'));
704 Tools::redirect();
63c35580 705 }
66b6a3b5
E
706
707 $this->$providers[$from]($targetFile);
63c35580 708 }
c765c367 709
07ee09f4
NL
710 /**
711 * export poche entries in json
712 * @return json all poche entries
713 */
63c35580
NL
714 public function export()
715 {
8d3275be 716 $entries = $this->store->retrieveAll($this->user->getId());
63c35580
NL
717 echo $this->tpl->render('export.twig', array(
718 'export' => Tools::renderJson($entries),
719 ));
720 Tools::logm('export view');
c765c367 721 }
32520785 722
07ee09f4 723 /**
a3436d4c 724 * Checks online the latest version of poche and cache it
07ee09f4
NL
725 * @param string $which 'prod' or 'dev'
726 * @return string latest $which version
727 */
32520785
NL
728 private function getPocheVersion($which = 'prod')
729 {
730 $cache_file = CACHE . '/' . $which;
a3436d4c
NL
731
732 # checks if the cached version file exists
32520785
NL
733 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
734 $version = file_get_contents($cache_file);
735 } else {
bc1ee852 736 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
32520785
NL
737 file_put_contents($cache_file, $version, LOCK_EX);
738 }
739 return $version;
740 }
eb1af592 741}