]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
13 public static $canRenderTemplates = true ;
14 public static $configFileAvailable = true ;
22 private $currentTheme = '' ;
23 private $currentLanguage = '' ;
24 private $notInstalledMessage = array ();
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' ))
35 public function __construct ()
37 if ( $this- > configFileIsAvailable ()) {
41 if ( $this- > themeIsInstalled ()) {
45 if ( $this- > systemIsInstalled ()) {
46 $this- > store
= new Database ();
47 $this- > messages
= new Messages ();
49 if (! $this- > store
-> isInstalled ()) {
52 $this- > store
-> checkTags ();
56 private function init ()
59 Session
:: $sessionName = 'poche' ;
62 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
63 $this- > user
= $_SESSION [ 'poche_user' ];
65 # fake user, just for install & login screens
66 $this- > user
= new User ();
67 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
71 $language = $this- > user
-> getConfigValue ( 'language' );
72 putenv ( 'LC_ALL=' . $language );
73 setlocale ( LC_ALL
, $language );
74 bindtextdomain ( $language , LOCALE
);
75 textdomain ( $language );
78 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
81 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
83 if ( $themeDirectory === false ) {
84 $themeDirectory = DEFAULT_THEME
;
87 $this- > currentTheme
= $themeDirectory ;
90 $languageDirectory = $this- > user
-> getConfigValue ( 'language' );
92 if ( $languageDirectory === false ) {
93 $languageDirectory = DEFAULT_THEME
;
96 $this- > currentLanguage
= $languageDirectory ;
99 public function configFileIsAvailable () {
100 if (! self
:: $configFileAvailable ) {
101 $this- > notInstalledMessage
[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.' ;
109 public function themeIsInstalled () {
111 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
112 if (! self
:: $canRenderTemplates ) {
113 $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>' ;
117 if (! is_writable ( CACHE
)) {
118 $this- > notInstalledMessage
[] = 'You don \' t have write access on cache directory.' ;
120 self
:: $canRenderTemplates = false ;
125 # Check if the selected theme and its requirements are present
126 if ( $this- > getTheme () != '' && ! is_dir ( THEME
. '/' . $this- > getTheme ())) {
127 $this- > notInstalledMessage
[] = 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $this- > getTheme () . ')' ;
129 self
:: $canRenderTemplates = false ;
134 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
135 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
136 $this- > notInstalledMessage
[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' ;
138 self
:: $canRenderTemplates = false ;
153 * all checks before installation.
154 * @todo move HTML to template
157 public function systemIsInstalled ()
161 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
163 if ( empty ( $configSalt )) {
164 $this- > notInstalledMessage
[] = 'You have not yet filled in the SALT value in the config.inc.php file.' ;
167 if ( STORAGE
== 'sqlite' && ! file_exists ( STORAGE_SQLITE
)) {
168 Tools
:: logm ( 'sqlite file doesn \' t exist' );
169 $this- > notInstalledMessage
[] = 'sqlite file doesn \' t exist, you can find it in install folder. Copy it in /db folder.' ;
172 if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
173 $this- > notInstalledMessage
[] = 'you have to delete the /install folder before using poche.' ;
176 if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
177 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
178 $this- > notInstalledMessage
[] = 'You don \' t have write access on sqlite file.' ;
189 public function getNotInstalledMessage () {
190 return $this- > notInstalledMessage
;
193 private function initTpl ()
195 $loaderChain = new Twig_Loader_Chain ();
197 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
199 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $this- > getTheme ()));
200 } catch ( Twig_Error_Loader
$e ) {
201 # @todo isInstalled() should catch this, inject Twig later
202 die ( 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (' . THEME
. '/' . $this- > getTheme () . ' is missing)' );
205 # add all required themes to the loader chain
206 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
208 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . DEFAULT_THEME
));
209 } catch ( Twig_Error_Loader
$e ) {
210 # @todo isInstalled() should catch this, inject Twig later
211 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' );
216 $twig_params = array ();
218 $twig_params = array ( 'cache' => CACHE
);
221 $this- > tpl
= new Twig_Environment ( $loaderChain , $twig_params );
222 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
224 # filter to display domain name of an url
225 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
226 $this- > tpl
-> addFilter ( $filter );
228 # filter for reading time
229 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
230 $this- > tpl
-> addFilter ( $filter );
232 # filter for simple filenames in config view
233 $filter = new Twig_SimpleFilter ( 'getPrettyFilename' , function ( $string ) { return str_replace ( ROOT
, '' , $
string ); });
234 $this- > tpl
-> addFilter ( $filter );
237 private function install ()
239 Tools
:: logm ( 'poche still not installed' );
240 echo $this- > tpl
-> render ( 'install.twig' , array (
241 'token' => Session
:: getToken (),
242 'theme' => $this- > getTheme (),
243 'poche_url' => Tools
:: getPocheUrl ()
245 if ( isset ( $_GET [ 'install' ])) {
246 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
247 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
248 # let's rock, install poche baby !
249 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
252 Tools
:: logm ( 'poche is now installed' );
257 Tools
:: logm ( 'error during installation' );
264 public function getTheme () {
265 return $this- > currentTheme
;
268 public function getLanguage () {
269 return $this- > currentLanguage
;
272 public function getInstalledThemes () {
273 $handle = opendir ( THEME
);
276 while (( $theme = readdir ( $handle )) !== false ) {
277 # Themes are stored in a directory, so all directory names are themes
278 # @todo move theme installation data to database
279 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '..' , '.' ))) {
285 if ( $theme === $this- > getTheme ()) {
289 $themes [] = array ( 'name' => $theme , 'current' => $current );
296 public function getInstalledLanguages () {
297 $handle = opendir ( LOCALE
);
298 $languages = array ();
300 while (( $language = readdir ( $handle )) !== false ) {
301 # Languages are stored in a directory, so all directory names are languages
302 # @todo move language installation data to database
303 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
309 if ( $language === $this- > getLanguage ()) {
313 $languages [] = array ( 'name' => $language , 'current' => $current );
319 public function getDefaultConfig ()
322 'pager' => PAGINATION
,
324 'theme' => DEFAULT_THEME
329 * Call action (mark as fav, archive, delete, etc.)
331 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE )
336 $json = file_get_contents ( Tools
:: getPocheUrl () . '/inc/3rdparty/makefulltextfeed.php?url=' . urlencode ( $url- > getUrl ()). '&max=5&links=preserve&exc=&format=json&submit=Create+Feed' );
337 $content = json_decode ( $json , true );
338 $title = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ];
339 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
341 if ( $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ())) {
342 Tools
:: logm ( 'add link ' . $url- > getUrl ());
344 if ( STORAGE
== 'postgres' ) {
345 $sequence = 'entries_id_seq' ;
347 $last_id = $this- > store
-> getLastId ( $sequence );
348 if ( DOWNLOAD_PICTURES
) {
349 $content = filtre_picture ( $body , $url- > getUrl (), $last_id );
350 Tools
:: logm ( 'updating content article' );
351 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
354 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
359 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
360 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
365 if ( $autoclose == TRUE ) {
366 Tools
:: redirect ( '?view=home' );
368 Tools
:: redirect ( '?view=home&closewin=true' );
373 $msg = 'delete link #' . $id ;
374 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
375 if ( DOWNLOAD_PICTURES
) {
376 remove_directory ( ABS_PATH
. $id );
378 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
381 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
382 $msg = 'error : can \' t delete link #' . $id ;
385 Tools
:: redirect ( '?' );
388 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
389 Tools
:: logm ( 'mark as favorite link #' . $id );
394 case 'toggle_archive' :
395 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
396 Tools
:: logm ( 'archive link #' . $id );
402 $tags = explode ( ',' , $_POST [ 'value' ]);
403 $entry_id = $_POST [ 'entry_id' ];
404 foreach ( $tags as $key => $tag_value ) {
405 $value = trim ( $tag_value );
406 $tag = $this- > store
-> retrieveTagByValue ( $value );
410 $tag = $this- > store
-> createTag ( $value );
412 if ( STORAGE
== 'postgres' ) {
413 $sequence = 'tags_id_seq' ;
415 $tag_id = $this- > store
-> getLastId ( $sequence );
418 $tag_id = $tag [ 'id' ];
421 # we assign the tag to the article
422 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
427 $tag_id = $_GET [ 'tag_id' ];
428 $this- > store
-> removeTagForEntry ( $id , $tag_id );
436 function displayView ( $view , $id = 0 )
443 $dev = $this- > getPocheVersion ( 'dev' );
444 $prod = $this- > getPocheVersion ( 'prod' );
445 $compare_dev = version_compare ( POCHE
, $dev );
446 $compare_prod = version_compare ( POCHE
, $prod );
447 $themes = $this- > getInstalledThemes ();
448 $languages = $this- > getInstalledLanguages ();
449 $token = $this- > user
-> getConfigValue ( 'token' );
450 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
453 'languages' => $languages ,
456 'compare_dev' => $compare_dev ,
457 'compare_prod' => $compare_prod ,
459 'user_id' => $this- > user
-> getId (),
460 'http_auth' => $http_auth ,
462 Tools
:: logm ( 'config view' );
466 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
473 $entries = $this- > store
-> retrieveEntriesByTag ( $id );
474 $tag = $this- > store
-> retrieveTag ( $id );
477 'entries' => $entries ,
481 $token = $this- > user
-> getConfigValue ( 'token' );
482 $tags = $this- > store
-> retrieveAllTags ();
485 'user_id' => $this- > user
-> getId (),
490 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
491 if ( $entry != NULL ) {
492 Tools
:: logm ( 'view link #' . $id );
493 $content = $entry [ 'content' ];
494 if ( function_exists ( 'tidy_parse_string' )) {
495 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
496 $tidy- > cleanRepair ();
497 $content = $tidy- > value
;
501 $flattr = new FlattrItem ();
502 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
505 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
509 'content' => $content ,
515 Tools
:: logm ( 'error in view call : entry is null' );
518 default : # home, favorites and archive views
519 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
526 if ( count ( $entries ) > 0 ) {
527 $this- > pagination
-> set_total ( count ( $entries ));
528 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
529 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
530 $tpl_vars [ 'entries' ] = $datas ;
531 $tpl_vars [ 'page_links' ] = $page_links ;
532 $tpl_vars [ 'nb_results' ] = count ( $entries );
534 Tools
:: logm ( 'display ' . $view . ' view' );
542 * update the password of the current user.
543 * if MODE_DEMO is TRUE, the password can't be updated.
544 * @todo add the return value
545 * @todo set the new password in function header like this updatePassword($newPassword)
548 public function updatePassword ()
551 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
552 Tools
:: logm ( 'in demo mode, you can \' t do this' );
553 Tools
:: redirect ( '?view=config' );
556 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
557 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
558 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
559 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
561 Tools
:: logm ( 'password updated' );
565 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
566 Tools
:: redirect ( '?view=config' );
572 public function updateTheme ()
575 if ( empty ( $_POST [ 'theme' ])) {
578 # we are not going to change it to the current theme...
579 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
580 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
581 Tools
:: redirect ( '?view=config' );
584 $themes = $this- > getInstalledThemes ();
585 $actualTheme = false ;
587 foreach ( $themes as $theme ) {
588 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
594 if (! $actualTheme ) {
595 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
596 Tools
:: redirect ( '?view=config' );
599 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
600 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
602 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
603 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
605 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
607 Tools
:: redirect ( '?view=config' );
610 public function updateLanguage ()
613 if ( empty ( $_POST [ 'language' ])) {
616 # we are not going to change it to the current language...
617 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
618 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
619 Tools
:: redirect ( '?view=config' );
622 $languages = $this- > getInstalledLanguages ();
623 $actualLanguage = false ;
625 foreach ( $languages as $language ) {
626 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
627 $actualLanguage = true ;
632 if (! $actualLanguage ) {
633 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
634 Tools
:: redirect ( '?view=config' );
637 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
638 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
640 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
641 $currentConfig [ 'language' ] = $_POST [ 'language' ];
643 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
645 Tools
:: redirect ( '?view=config' );
649 * get credentials from differents sources
650 * it redirects the user to the $referer link
653 private function credentials () {
654 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
655 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' );
657 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
658 return array ( $_POST [ 'login' ], $_POST [ 'password' ]);
660 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
661 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' );
664 return array ( false , false );
668 * checks if login & password are correct and save the user in session.
669 * it redirects the user to the $referer link
670 * @param string $referer the url to redirect after login
671 * @todo add the return value
674 public function login ( $referer )
676 list ( $login , $password )= $this- > credentials ();
677 if ( $login === false || $password === false ) {
678 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
679 Tools
:: logm ( 'login failed' );
682 if (! empty ( $login ) && ! empty ( $password )) {
683 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ));
684 if ( $user != array ()) {
685 # Save login into Session
686 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
687 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , Tools
:: encodeString ( $password . $login ), $longlastingsession , array ( 'poche_user' => new User ( $user )));
688 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
689 Tools
:: logm ( 'login successful' );
690 Tools
:: redirect ( $referer );
692 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
693 Tools
:: logm ( 'login failed' );
699 * log out the poche user. It cleans the session.
700 * @todo add the return value
703 public function logout ()
705 $this- > user
= array ();
707 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
708 Tools
:: logm ( 'logout' );
713 * import from Instapaper. poche needs a ./instapaper-export.html file
714 * @todo add the return value
715 * @param string $targetFile the file used for importing
718 private function importFromInstapaper ( $targetFile )
720 # TODO gestion des articles favs
721 $html = new simple_html_dom ();
722 $html- > load_file ( $targetFile );
723 Tools
:: logm ( 'starting import from instapaper' );
727 foreach ( $html- > find ( 'ol' ) as $ul )
729 foreach ( $ul- > find ( 'li' ) as $li )
732 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
733 $this- > action ( 'add' , $url , 0 , TRUE );
736 if ( STORAGE
== 'postgres' ) {
737 $sequence = 'entries_id_seq' ;
739 $last_id = $this- > store
-> getLastId ( $sequence );
740 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
744 # the second <ol> is for read links
747 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
748 Tools
:: logm ( 'import from instapaper completed' );
753 * import from Pocket. poche needs a ./ril_export.html file
754 * @todo add the return value
755 * @param string $targetFile the file used for importing
758 private function importFromPocket ( $targetFile )
760 # TODO gestion des articles favs
761 $html = new simple_html_dom ();
762 $html- > load_file ( $targetFile );
763 Tools
:: logm ( 'starting import from pocket' );
767 foreach ( $html- > find ( 'ul' ) as $ul )
769 foreach ( $ul- > find ( 'li' ) as $li )
772 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
773 $this- > action ( 'add' , $url , 0 , TRUE );
776 if ( STORAGE
== 'postgres' ) {
777 $sequence = 'entries_id_seq' ;
779 $last_id = $this- > store
-> getLastId ( $sequence );
780 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
784 # the second <ul> is for read links
787 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
788 Tools
:: logm ( 'import from pocket completed' );
793 * import from Readability. poche needs a ./readability file
794 * @todo add the return value
795 * @param string $targetFile the file used for importing
798 private function importFromReadability ( $targetFile )
800 # TODO gestion des articles lus / favs
801 $str_data = file_get_contents ( $targetFile );
802 $data = json_decode ( $str_data , true );
803 Tools
:: logm ( 'starting import from Readability' );
805 foreach ( $data as $key => $value ) {
809 foreach ( $value as $attr => $attr_value ) {
810 if ( $attr == 'article__url' ) {
811 $url = new Url ( base64_encode ( $attr_value ));
814 if ( STORAGE
== 'postgres' ) {
815 $sequence = 'entries_id_seq' ;
817 if ( $attr_value == 'true' ) {
818 if ( $attr == 'favorite' ) {
821 if ( $attr == 'archive' ) {
827 if (! is_null ( $url ) && $url- > isCorrect ()) {
828 $this- > action ( 'add' , $url , 0 , TRUE );
831 $last_id = $this- > store
-> getLastId ( $sequence );
832 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
835 $last_id = $this- > store
-> getLastId ( $sequence );
836 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
840 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
841 Tools
:: logm ( 'import from Readability completed' );
846 * import datas into your poche
847 * @param string $from name of the service to import : pocket, instapaper or readability
848 * @todo add the return value
851 public function import ( $from )
854 'pocket' => 'importFromPocket' ,
855 'readability' => 'importFromReadability' ,
856 'instapaper' => 'importFromInstapaper'
859 if (! isset ( $providers [ $from ])) {
860 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
864 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
865 $targetFile = constant ( $targetDefinition );
867 if (! defined ( $targetDefinition )) {
868 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
872 if (! file_exists ( $targetFile )) {
873 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
877 $this- > $providers [ $from ]( $targetFile );
881 * export poche entries in json
882 * @return json all poche entries
884 public function export ()
886 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
887 echo $this- > tpl
-> render ( 'export.twig' , array (
888 'export' => Tools
:: renderJson ( $entries ),
890 Tools
:: logm ( 'export view' );
894 * Checks online the latest version of poche and cache it
895 * @param string $which 'prod' or 'dev'
896 * @return string latest $which version
898 private function getPocheVersion ( $which = 'prod' )
900 $cache_file = CACHE
. '/' . $which ;
902 # checks if the cached version file exists
903 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
904 $version = file_get_contents ( $cache_file );
906 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
907 file_put_contents ( $cache_file , $version , LOCK_EX
);
912 public function generateToken ()
914 if ( ini_get ( 'open_basedir' ) === '' ) {
915 $token = substr ( base64_encode ( file_get_contents ( '/dev/urandom' , false , null , 0 , 20 )), 0 , 15 );
918 $token = substr ( base64_encode ( uniqid ( mt_rand (), true )), 0 , 20 );
921 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
922 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
923 $currentConfig [ 'token' ] = $token ;
924 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
927 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' )
929 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
930 $config = $this- > store
-> getConfigUser ( $user_id );
932 if (! in_array ( $type , $allowed_types ) ||
933 $token != $config [ 'token' ]) {
934 die ( _ ( 'Uh, there is a problem while generating feeds.' ));
938 $feed = new FeedWriter ( RSS2
);
939 $feed- > setTitle ( 'poche - ' . $type . ' feed' );
940 $feed- > setLink ( Tools
:: getPocheUrl ());
941 $feed- > setChannelElement ( 'updated' , date ( DATE_RSS
, time ()));
942 $feed- > setChannelElement ( 'author' , 'poche' );
944 if ( $type == 'tag' ) {
945 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id );
948 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
951 if ( count ( $entries ) > 0 ) {
952 foreach ( $entries as $entry ) {
953 $newItem = $feed- > createNewItem ();
954 $newItem- > setTitle ( $entry [ 'title' ]);
955 $newItem- > setLink ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
956 $newItem- > setDate ( time ());
957 $newItem- > setDescription ( $entry [ 'content' ]);
958 $feed- > addItem ( $newItem );
962 $feed- > genarateFeed ();