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