]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas LÅ“uillet <nicolas@loeuillet.org>
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' )),
33 'courgette' => array ( 'requires' => array ())
36 public function __construct ()
38 if ( $this- > configFileIsAvailable ()) {
42 if ( $this- > themeIsInstalled ()) {
46 if ( $this- > systemIsInstalled ()) {
47 $this- > store
= new Database ();
48 $this- > messages
= new Messages ();
50 if (! $this- > store
-> isInstalled ()) {
53 $this- > store
-> checkTags ();
57 private function init ()
60 Session
:: $sessionName = 'poche' ;
63 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
64 $this- > user
= $_SESSION [ 'poche_user' ];
66 # fake user, just for install & login screens
67 $this- > user
= new User ();
68 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
72 $language = $this- > user
-> getConfigValue ( 'language' );
73 putenv ( 'LC_ALL=' . $language );
74 setlocale ( LC_ALL
, $language );
75 bindtextdomain ( $language , LOCALE
);
76 textdomain ( $language );
79 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
82 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
84 if ( $themeDirectory === false ) {
85 $themeDirectory = DEFAULT_THEME
;
88 $this- > currentTheme
= $themeDirectory ;
91 $languageDirectory = $this- > user
-> getConfigValue ( 'language' );
93 if ( $languageDirectory === false ) {
94 $languageDirectory = DEFAULT_THEME
;
97 $this- > currentLanguage
= $languageDirectory ;
100 public function configFileIsAvailable () {
101 if (! self
:: $configFileAvailable ) {
102 $this- > notInstalledMessage
[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.' ;
110 public function themeIsInstalled () {
112 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
113 if (! self
:: $canRenderTemplates ) {
114 $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.wallabag.org/doku.php?id=users:begin:install">the documentation.</a>' ;
118 if (! is_writable ( CACHE
)) {
119 $this- > notInstalledMessage
[] = 'You don \' t have write access on cache directory.' ;
121 self
:: $canRenderTemplates = false ;
126 # Check if the selected theme and its requirements are present
127 if ( $this- > getTheme () != '' && ! is_dir ( THEME
. '/' . $this- > getTheme ())) {
128 $this- > notInstalledMessage
[] = 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $this- > getTheme () . ')' ;
130 self
:: $canRenderTemplates = false ;
135 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
136 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
137 $this- > notInstalledMessage
[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' ;
139 self
:: $canRenderTemplates = false ;
154 * all checks before installation.
155 * @todo move HTML to template
158 public function systemIsInstalled ()
162 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
164 if ( empty ( $configSalt )) {
165 $this- > notInstalledMessage
[] = 'You have not yet filled in the SALT value in the config.inc.php file.' ;
168 if ( STORAGE
== 'sqlite' && ! file_exists ( STORAGE_SQLITE
)) {
169 Tools
:: logm ( 'sqlite file doesn \' t exist' );
170 $this- > notInstalledMessage
[] = 'sqlite file doesn \' t exist, you can find it in install folder. Copy it in /db folder.' ;
173 if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
174 $this- > notInstalledMessage
[] = 'you have to delete the /install folder before using poche.' ;
177 if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
178 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
179 $this- > notInstalledMessage
[] = 'You don \' t have write access on sqlite file.' ;
190 public function getNotInstalledMessage () {
191 return $this- > notInstalledMessage
;
194 private function initTpl ()
196 $loaderChain = new Twig_Loader_Chain ();
198 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
200 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $this- > getTheme ()));
201 } catch ( Twig_Error_Loader
$e ) {
202 # @todo isInstalled() should catch this, inject Twig later
203 die ( 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (' . THEME
. '/' . $this- > getTheme () . ' is missing)' );
206 # add all required themes to the loader chain
207 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
209 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . DEFAULT_THEME
));
210 } catch ( Twig_Error_Loader
$e ) {
211 # @todo isInstalled() should catch this, inject Twig later
212 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' );
217 $twig_params = array ();
219 $twig_params = array ( 'cache' => CACHE
);
222 $this- > tpl
= new Twig_Environment ( $loaderChain , $twig_params );
223 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
225 # filter to display domain name of an url
226 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
227 $this- > tpl
-> addFilter ( $filter );
229 # filter for reading time
230 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
231 $this- > tpl
-> addFilter ( $filter );
233 # filter for simple filenames in config view
234 $filter = new Twig_SimpleFilter ( 'getPrettyFilename' , function ( $string ) { return str_replace ( ROOT
, '' , $
string ); });
235 $this- > tpl
-> addFilter ( $filter );
238 private function install ()
240 Tools
:: logm ( 'poche still not installed' );
241 echo $this- > tpl
-> render ( 'install.twig' , array (
242 'token' => Session
:: getToken (),
243 'theme' => $this- > getTheme (),
244 'poche_url' => Tools
:: getPocheUrl ()
246 if ( isset ( $_GET [ 'install' ])) {
247 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
248 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
249 # let's rock, install poche baby !
250 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
253 Tools
:: logm ( 'poche is now installed' );
258 Tools
:: logm ( 'error during installation' );
265 public function getTheme () {
266 return $this- > currentTheme
;
269 public function getLanguage () {
270 return $this- > currentLanguage
;
273 public function getInstalledThemes () {
274 $handle = opendir ( THEME
);
277 while (( $theme = readdir ( $handle )) !== false ) {
278 # Themes are stored in a directory, so all directory names are themes
279 # @todo move theme installation data to database
280 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '..' , '.' ))) {
286 if ( $theme === $this- > getTheme ()) {
290 $themes [] = array ( 'name' => $theme , 'current' => $current );
297 public function getInstalledLanguages () {
298 $handle = opendir ( LOCALE
);
299 $languages = array ();
301 while (( $language = readdir ( $handle )) !== false ) {
302 # Languages are stored in a directory, so all directory names are languages
303 # @todo move language installation data to database
304 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
310 if ( $language === $this- > getLanguage ()) {
314 $languages [] = array ( 'name' => $language , 'current' => $current );
320 public function getDefaultConfig ()
323 'pager' => PAGINATION
,
325 'theme' => DEFAULT_THEME
330 * Call action (mark as fav, archive, delete, etc.)
332 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE )
337 $options = array ( 'http' => array ( 'user_agent' => 'poche' ));
338 $context = stream_context_create ( $options );
339 $json = file_get_contents ( Tools
:: getPocheUrl () . '/inc/3rdparty/makefulltextfeed.php?url=' . urlencode ( $url- > getUrl ()). '&max=5&links=preserve&exc=&format=json&submit=Create+Feed' , false , $context );
340 $content = json_decode ( $json , true );
341 $title = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ];
342 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
344 if ( $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ())) {
345 Tools
:: logm ( 'add link ' . $url- > getUrl ());
347 if ( STORAGE
== 'postgres' ) {
348 $sequence = 'entries_id_seq' ;
350 $last_id = $this- > store
-> getLastId ( $sequence );
351 if ( DOWNLOAD_PICTURES
) {
352 $content = filtre_picture ( $body , $url- > getUrl (), $last_id );
353 Tools
:: logm ( 'updating content article' );
354 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
357 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
362 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
363 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
368 if ( $autoclose == TRUE ) {
369 Tools
:: redirect ( '?view=home' );
371 Tools
:: redirect ( '?view=home&closewin=true' );
376 $msg = 'delete link #' . $id ;
377 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
378 if ( DOWNLOAD_PICTURES
) {
379 remove_directory ( ABS_PATH
. $id );
381 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
384 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
385 $msg = 'error : can \' t delete link #' . $id ;
388 Tools
:: redirect ( '?' );
391 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
392 Tools
:: logm ( 'mark as favorite link #' . $id );
397 case 'toggle_archive' :
398 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
399 Tools
:: logm ( 'archive link #' . $id );
405 $tags = explode ( ',' , $_POST [ 'value' ]);
406 $entry_id = $_POST [ 'entry_id' ];
407 foreach ( $tags as $key => $tag_value ) {
408 $value = trim ( $tag_value );
409 $tag = $this- > store
-> retrieveTagByValue ( $value );
413 $tag = $this- > store
-> createTag ( $value );
415 if ( STORAGE
== 'postgres' ) {
416 $sequence = 'tags_id_seq' ;
418 $tag_id = $this- > store
-> getLastId ( $sequence );
421 $tag_id = $tag [ 'id' ];
424 # we assign the tag to the article
425 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
430 $tag_id = $_GET [ 'tag_id' ];
431 $this- > store
-> removeTagForEntry ( $id , $tag_id );
439 function displayView ( $view , $id = 0 )
446 $dev = $this- > getPocheVersion ( 'dev' );
447 $prod = $this- > getPocheVersion ( 'prod' );
448 $compare_dev = version_compare ( POCHE
, $dev );
449 $compare_prod = version_compare ( POCHE
, $prod );
450 $themes = $this- > getInstalledThemes ();
451 $languages = $this- > getInstalledLanguages ();
452 $token = $this- > user
-> getConfigValue ( 'token' );
453 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
456 'languages' => $languages ,
459 'compare_dev' => $compare_dev ,
460 'compare_prod' => $compare_prod ,
462 'user_id' => $this- > user
-> getId (),
463 'http_auth' => $http_auth ,
465 Tools
:: logm ( 'config view' );
469 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
476 $entries = $this- > store
-> retrieveEntriesByTag ( $id );
477 $tag = $this- > store
-> retrieveTag ( $id );
480 'entries' => $entries ,
484 $token = $this- > user
-> getConfigValue ( 'token' );
485 $tags = $this- > store
-> retrieveAllTags ();
488 'user_id' => $this- > user
-> getId (),
493 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
494 if ( $entry != NULL ) {
495 Tools
:: logm ( 'view link #' . $id );
496 $content = $entry [ 'content' ];
497 if ( function_exists ( 'tidy_parse_string' )) {
498 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
499 $tidy- > cleanRepair ();
500 $content = $tidy- > value
;
504 $flattr = new FlattrItem ();
505 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
508 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
512 'content' => $content ,
518 Tools
:: logm ( 'error in view call : entry is null' );
521 default : # home, favorites and archive views
522 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
529 if ( count ( $entries ) > 0 ) {
530 $this- > pagination
-> set_total ( count ( $entries ));
531 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
532 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
533 $tpl_vars [ 'entries' ] = $datas ;
534 $tpl_vars [ 'page_links' ] = $page_links ;
535 $tpl_vars [ 'nb_results' ] = count ( $entries );
537 Tools
:: logm ( 'display ' . $view . ' view' );
545 * update the password of the current user.
546 * if MODE_DEMO is TRUE, the password can't be updated.
547 * @todo add the return value
548 * @todo set the new password in function header like this updatePassword($newPassword)
551 public function updatePassword ()
554 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
555 Tools
:: logm ( 'in demo mode, you can \' t do this' );
556 Tools
:: redirect ( '?view=config' );
559 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
560 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
561 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
562 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
564 Tools
:: logm ( 'password updated' );
568 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
569 Tools
:: redirect ( '?view=config' );
575 public function updateTheme ()
578 if ( empty ( $_POST [ 'theme' ])) {
581 # we are not going to change it to the current theme...
582 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
583 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
584 Tools
:: redirect ( '?view=config' );
587 $themes = $this- > getInstalledThemes ();
588 $actualTheme = false ;
590 foreach ( $themes as $theme ) {
591 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
597 if (! $actualTheme ) {
598 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
599 Tools
:: redirect ( '?view=config' );
602 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
603 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
605 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
606 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
608 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
610 Tools
:: redirect ( '?view=config' );
613 public function updateLanguage ()
616 if ( empty ( $_POST [ 'language' ])) {
619 # we are not going to change it to the current language...
620 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
621 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
622 Tools
:: redirect ( '?view=config' );
625 $languages = $this- > getInstalledLanguages ();
626 $actualLanguage = false ;
628 foreach ( $languages as $language ) {
629 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
630 $actualLanguage = true ;
635 if (! $actualLanguage ) {
636 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
637 Tools
:: redirect ( '?view=config' );
640 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
641 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
643 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
644 $currentConfig [ 'language' ] = $_POST [ 'language' ];
646 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
648 Tools
:: redirect ( '?view=config' );
652 * get credentials from differents sources
653 * it redirects the user to the $referer link
656 private function credentials () {
657 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
658 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' );
660 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
661 return array ( $_POST [ 'login' ], $_POST [ 'password' ]);
663 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
664 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' );
667 return array ( false , false );
671 * checks if login & password are correct and save the user in session.
672 * it redirects the user to the $referer link
673 * @param string $referer the url to redirect after login
674 * @todo add the return value
677 public function login ( $referer )
679 list ( $login , $password )= $this- > credentials ();
680 if ( $login === false || $password === false ) {
681 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
682 Tools
:: logm ( 'login failed' );
685 if (! empty ( $login ) && ! empty ( $password )) {
686 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ));
687 if ( $user != array ()) {
688 # Save login into Session
689 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
690 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , Tools
:: encodeString ( $password . $login ), $longlastingsession , array ( 'poche_user' => new User ( $user )));
691 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
692 Tools
:: logm ( 'login successful' );
693 Tools
:: redirect ( $referer );
695 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
696 Tools
:: logm ( 'login failed' );
702 * log out the poche user. It cleans the session.
703 * @todo add the return value
706 public function logout ()
708 $this- > user
= array ();
710 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
711 Tools
:: logm ( 'logout' );
716 * import from Instapaper. poche needs a ./instapaper-export.html file
717 * @todo add the return value
718 * @param string $targetFile the file used for importing
721 private function importFromInstapaper ( $targetFile )
723 # TODO gestion des articles favs
724 $html = new simple_html_dom ();
725 $html- > load_file ( $targetFile );
726 Tools
:: logm ( 'starting import from instapaper' );
730 foreach ( $html- > find ( 'ol' ) as $ul )
732 foreach ( $ul- > find ( 'li' ) as $li )
735 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
736 $this- > action ( 'add' , $url , 0 , TRUE );
739 if ( STORAGE
== 'postgres' ) {
740 $sequence = 'entries_id_seq' ;
742 $last_id = $this- > store
-> getLastId ( $sequence );
743 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
747 # the second <ol> is for read links
750 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
751 Tools
:: logm ( 'import from instapaper completed' );
756 * import from Pocket. poche needs a ./ril_export.html file
757 * @todo add the return value
758 * @param string $targetFile the file used for importing
761 private function importFromPocket ( $targetFile )
763 # TODO gestion des articles favs
764 $html = new simple_html_dom ();
765 $html- > load_file ( $targetFile );
766 Tools
:: logm ( 'starting import from pocket' );
770 foreach ( $html- > find ( 'ul' ) as $ul )
772 foreach ( $ul- > find ( 'li' ) as $li )
775 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
776 $this- > action ( 'add' , $url , 0 , TRUE );
779 if ( STORAGE
== 'postgres' ) {
780 $sequence = 'entries_id_seq' ;
782 $last_id = $this- > store
-> getLastId ( $sequence );
783 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
787 # the second <ul> is for read links
790 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
791 Tools
:: logm ( 'import from pocket completed' );
796 * import from Readability. poche needs a ./readability file
797 * @todo add the return value
798 * @param string $targetFile the file used for importing
801 private function importFromReadability ( $targetFile )
803 # TODO gestion des articles lus / favs
804 $str_data = file_get_contents ( $targetFile );
805 $data = json_decode ( $str_data , true );
806 Tools
:: logm ( 'starting import from Readability' );
808 foreach ( $data as $key => $value ) {
812 foreach ( $value as $item ) {
813 foreach ( $item as $attr => $value ) {
814 if ( $attr == 'article__url' ) {
815 $url = new Url ( base64_encode ( $value ));
818 if ( STORAGE
== 'postgres' ) {
819 $sequence = 'entries_id_seq' ;
821 if ( $value == 'true' ) {
822 if ( $attr == 'favorite' ) {
825 if ( $attr == 'archive' ) {
832 if (! is_null ( $url ) && $url- > isCorrect ()) {
833 $this- > action ( 'add' , $url , 0 , TRUE );
836 $last_id = $this- > store
-> getLastId ( $sequence );
837 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
840 $last_id = $this- > store
-> getLastId ( $sequence );
841 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
846 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
847 Tools
:: logm ( 'import from Readability completed' );
852 * import from Poche exported file
853 * @param string $targetFile the file used for importing
856 private function importFromPoche ( $targetFile )
858 $str_data = file_get_contents ( $targetFile );
859 $data = json_decode ( $str_data , true );
860 Tools
:: logm ( 'starting import from Poche' );
864 if ( STORAGE
== 'postgres' ) {
865 $sequence = 'entries_id_seq' ;
869 foreach ( $data as $value ) {
871 $url = new Url ( base64_encode ( $value [ 'url' ]));
872 $favorite = ( $value [ 'is_fav' ] == - 1 );
873 $archive = ( $value [ 'is_read' ] == - 1 );
876 if (! is_null ( $url ) && $url- > isCorrect ()) {
878 $this- > action ( 'add' , $url , 0 , TRUE );
882 $last_id = $this- > store
-> getLastId ( $sequence );
883 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
886 $last_id = $this- > store
-> getLastId ( $sequence );
887 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
892 $this- > messages
-> add ( 's' , _ ( 'import from Poche completed. ' . $count . ' new links.' ));
893 Tools
:: logm ( 'import from Poche completed' );
898 * import datas into your poche
899 * @param string $from name of the service to import : pocket, instapaper or readability
900 * @todo add the return value
903 public function import ( $from )
906 'pocket' => 'importFromPocket' ,
907 'readability' => 'importFromReadability' ,
908 'instapaper' => 'importFromInstapaper' ,
909 'poche' => 'importFromPoche' ,
912 if (! isset ( $providers [ $from ])) {
913 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
917 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
918 $targetFile = constant ( $targetDefinition );
920 if (! defined ( $targetDefinition )) {
921 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
925 if (! file_exists ( $targetFile )) {
926 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
930 $this- > $providers [ $from ]( $targetFile );
934 * export poche entries in json
935 * @return json all poche entries
937 public function export ()
939 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
940 echo $this- > tpl
-> render ( 'export.twig' , array (
941 'export' => Tools
:: renderJson ( $entries ),
943 Tools
:: logm ( 'export view' );
947 * Checks online the latest version of poche and cache it
948 * @param string $which 'prod' or 'dev'
949 * @return string latest $which version
951 private function getPocheVersion ( $which = 'prod' )
953 $cache_file = CACHE
. '/' . $which ;
955 # checks if the cached version file exists
956 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
957 $version = file_get_contents ( $cache_file );
959 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
960 file_put_contents ( $cache_file , $version , LOCK_EX
);
965 public function generateToken ()
967 if ( ini_get ( 'open_basedir' ) === '' ) {
968 $token = substr ( base64_encode ( file_get_contents ( '/dev/urandom' , false , null , 0 , 20 )), 0 , 15 );
971 $token = substr ( base64_encode ( uniqid ( mt_rand (), true )), 0 , 20 );
974 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
975 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
976 $currentConfig [ 'token' ] = $token ;
977 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
980 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' )
982 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
983 $config = $this- > store
-> getConfigUser ( $user_id );
985 if (! in_array ( $type , $allowed_types ) ||
986 $token != $config [ 'token' ]) {
987 die ( _ ( 'Uh, there is a problem while generating feeds.' ));
991 $feed = new FeedWriter ( RSS2
);
992 $feed- > setTitle ( 'poche - ' . $type . ' feed' );
993 $feed- > setLink ( Tools
:: getPocheUrl ());
994 $feed- > setChannelElement ( 'updated' , date ( DATE_RSS
, time ()));
995 $feed- > setChannelElement ( 'author' , 'poche' );
997 if ( $type == 'tag' ) {
998 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id );
1001 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
1004 if ( count ( $entries ) > 0 ) {
1005 foreach ( $entries as $entry ) {
1006 $newItem = $feed- > createNewItem ();
1007 $newItem- > setTitle ( $entry [ 'title' ]);
1008 $newItem- > setLink ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
1009 $newItem- > setDate ( time ());
1010 $newItem- > setDescription ( $entry [ 'content' ]);
1011 $feed- > addItem ( $newItem );
1015 $feed- > genarateFeed ();