]>
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 ()) {
55 private function init ()
58 Session
:: $sessionName = 'poche' ;
61 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
62 $this- > user
= $_SESSION [ 'poche_user' ];
64 # fake user, just for install & login screens
65 $this- > user
= new User ();
66 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
70 $language = $this- > user
-> getConfigValue ( 'language' );
71 putenv ( 'LC_ALL=' . $language );
72 setlocale ( LC_ALL
, $language );
73 bindtextdomain ( $language , LOCALE
);
74 textdomain ( $language );
77 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
80 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
82 if ( $themeDirectory === false ) {
83 $themeDirectory = DEFAULT_THEME
;
86 $this- > currentTheme
= $themeDirectory ;
89 $languageDirectory = $this- > user
-> getConfigValue ( 'language' );
91 if ( $languageDirectory === false ) {
92 $languageDirectory = DEFAULT_THEME
;
95 $this- > currentLanguage
= $languageDirectory ;
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.' ;
108 public function themeIsInstalled () {
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>' ;
116 if (! is_writable ( CACHE
)) {
117 $this- > notInstalledMessage
[] = 'You don \' t have write access on cache directory.' ;
119 self
:: $canRenderTemplates = false ;
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 () . ')' ;
128 self
:: $canRenderTemplates = false ;
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 () . ')' ;
137 self
:: $canRenderTemplates = false ;
152 * all checks before installation.
153 * @todo move HTML to template
156 public function systemIsInstalled ()
160 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
162 if ( empty ( $configSalt )) {
163 $this- > notInstalledMessage
[] = 'You have not yet filled in the SALT value in the config.inc.php file.' ;
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.' ;
171 if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
172 $this- > notInstalledMessage
[] = 'you have to delete the /install folder before using poche.' ;
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.' ;
188 public function getNotInstalledMessage () {
189 return $this- > notInstalledMessage
;
192 private function initTpl ()
194 $loaderChain = new Twig_Loader_Chain ();
196 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
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)' );
204 # add all required themes to the loader chain
205 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
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 () . ')' );
215 $twig_params = array ();
217 $twig_params = array ( 'cache' => CACHE
);
220 $this- > tpl
= new Twig_Environment ( $loaderChain , $twig_params );
221 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
223 # filter to display domain name of an url
224 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
225 $this- > tpl
-> addFilter ( $filter );
227 # filter for reading time
228 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
229 $this- > tpl
-> addFilter ( $filter );
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 );
236 private function install ()
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 ()
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' ])))
251 Tools
:: logm ( 'poche is now installed' );
256 Tools
:: logm ( 'error during installation' );
263 public function getTheme () {
264 return $this- > currentTheme
;
267 public function getLanguage () {
268 return $this- > currentLanguage
;
271 public function getInstalledThemes () {
272 $handle = opendir ( THEME
);
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 ( '..' , '.' , '.git' ))) {
284 if ( $theme === $this- > getTheme ()) {
288 $themes [] = array ( 'name' => $theme , 'current' => $current );
294 public function getInstalledLanguages () {
295 $handle = opendir ( LOCALE
);
296 $languages = array ();
298 while (( $language = readdir ( $handle )) !== false ) {
299 # Languages are stored in a directory, so all directory names are languages
300 # @todo move language installation data to database
301 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
307 if ( $language === $this- > getLanguage ()) {
311 $languages [] = array ( 'name' => $language , 'current' => $current );
317 public function getDefaultConfig ()
320 'pager' => PAGINATION
,
322 'theme' => DEFAULT_THEME
327 * Call action (mark as fav, archive, delete, etc.)
329 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
334 $content = $url- > extract ();
336 if ( $this- > store
-> add ( $url- > getUrl (), $content [ 'title' ], $content [ 'body' ], $this- > user
-> getId ())) {
337 Tools
:: logm ( 'add link ' . $url- > getUrl ());
339 if ( STORAGE
== 'postgres' ) {
340 $sequence = 'entries_id_seq' ;
342 $last_id = $this- > store
-> getLastId ( $sequence );
343 if ( DOWNLOAD_PICTURES
) {
344 $content = filtre_picture ( $content [ 'body' ], $url- > getUrl (), $last_id );
345 Tools
:: logm ( 'updating content article' );
346 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
349 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
354 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
355 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
360 Tools
:: redirect ( '?view=home' );
364 $msg = 'delete link #' . $id ;
365 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
366 if ( DOWNLOAD_PICTURES
) {
367 remove_directory ( ABS_PATH
. $id );
369 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
372 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
373 $msg = 'error : can \' t delete link #' . $id ;
379 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
380 Tools
:: logm ( 'mark as favorite link #' . $id );
385 case 'toggle_archive' :
386 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
387 Tools
:: logm ( 'archive link #' . $id );
397 function displayView ( $view , $id = 0 )
404 $dev = $this- > getPocheVersion ( 'dev' );
405 $prod = $this- > getPocheVersion ( 'prod' );
406 $compare_dev = version_compare ( POCHE
, $dev );
407 $compare_prod = version_compare ( POCHE
, $prod );
408 $themes = $this- > getInstalledThemes ();
409 $languages = $this- > getInstalledLanguages ();
412 'languages' => $languages ,
415 'compare_dev' => $compare_dev ,
416 'compare_prod' => $compare_prod ,
418 Tools
:: logm ( 'config view' );
421 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
422 if ( $entry != NULL ) {
423 Tools
:: logm ( 'view link #' . $id );
424 $content = $entry [ 'content' ];
425 if ( function_exists ( 'tidy_parse_string' )) {
426 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
427 $tidy- > cleanRepair ();
428 $content = $tidy- > value
;
432 $flattr = new FlattrItem ();
433 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
437 'content' => $content ,
442 Tools
:: logm ( 'error in view call : entry is null' );
445 default : # home, favorites and archive views
446 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
453 if ( count ( $entries ) > 0 ) {
454 $this- > pagination
-> set_total ( count ( $entries ));
455 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
456 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
457 $tpl_vars [ 'entries' ] = $datas ;
458 $tpl_vars [ 'page_links' ] = $page_links ;
459 $tpl_vars [ 'nb_results' ] = count ( $entries );
461 Tools
:: logm ( 'display ' . $view . ' view' );
469 * update the password of the current user.
470 * if MODE_DEMO is TRUE, the password can't be updated.
471 * @todo add the return value
472 * @todo set the new password in function header like this updatePassword($newPassword)
475 public function updatePassword ()
478 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
479 Tools
:: logm ( 'in demo mode, you can \' t do this' );
480 Tools
:: redirect ( '?view=config' );
483 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
484 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
485 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
486 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
488 Tools
:: logm ( 'password updated' );
492 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
493 Tools
:: redirect ( '?view=config' );
499 public function updateTheme ()
502 if ( empty ( $_POST [ 'theme' ])) {
505 # we are not going to change it to the current theme...
506 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
507 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
508 Tools
:: redirect ( '?view=config' );
511 $themes = $this- > getInstalledThemes ();
512 $actualTheme = false ;
514 foreach ( $themes as $theme ) {
515 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
521 if (! $actualTheme ) {
522 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
523 Tools
:: redirect ( '?view=config' );
526 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
527 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
529 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
530 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
532 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
534 Tools
:: redirect ( '?view=config' );
537 public function updateLanguage ()
540 if ( empty ( $_POST [ 'language' ])) {
543 # we are not going to change it to the current language...
544 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
545 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
546 Tools
:: redirect ( '?view=config' );
549 $languages = $this- > getInstalledLanguages ();
550 $actualLanguage = false ;
552 foreach ( $languages as $language ) {
553 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
554 $actualLanguage = true ;
559 if (! $actualLanguage ) {
560 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
561 Tools
:: redirect ( '?view=config' );
564 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
565 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
567 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
568 $currentConfig [ 'language' ] = $_POST [ 'language' ];
570 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
572 Tools
:: redirect ( '?view=config' );
576 * checks if login & password are correct and save the user in session.
577 * it redirects the user to the $referer link
578 * @param string $referer the url to redirect after login
579 * @todo add the return value
582 public function login ( $referer )
584 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
585 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
586 if ( $user != array ()) {
587 # Save login into Session
588 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
589 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
590 Tools
:: logm ( 'login successful' );
591 Tools
:: redirect ( $referer );
593 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
594 Tools
:: logm ( 'login failed' );
597 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
598 Tools
:: logm ( 'login failed' );
604 * log out the poche user. It cleans the session.
605 * @todo add the return value
608 public function logout ()
610 $this- > user
= array ();
612 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
613 Tools
:: logm ( 'logout' );
618 * import from Instapaper. poche needs a ./instapaper-export.html file
619 * @todo add the return value
620 * @param string $targetFile the file used for importing
623 private function importFromInstapaper ( $targetFile )
625 # TODO gestion des articles favs
626 $html = new simple_html_dom ();
627 $html- > load_file ( $targetFile );
628 Tools
:: logm ( 'starting import from instapaper' );
632 foreach ( $html- > find ( 'ol' ) as $ul )
634 foreach ( $ul- > find ( 'li' ) as $li )
637 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
638 $this- > action ( 'add' , $url , 0 , TRUE );
641 if ( STORAGE
== 'postgres' ) {
642 $sequence = 'entries_id_seq' ;
644 $last_id = $this- > store
-> getLastId ( $sequence );
645 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
649 # the second <ol> is for read links
652 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
653 Tools
:: logm ( 'import from instapaper completed' );
658 * import from Pocket. poche needs a ./ril_export.html file
659 * @todo add the return value
660 * @param string $targetFile the file used for importing
663 private function importFromPocket ( $targetFile )
665 # TODO gestion des articles favs
666 $html = new simple_html_dom ();
667 $html- > load_file ( $targetFile );
668 Tools
:: logm ( 'starting import from pocket' );
672 foreach ( $html- > find ( 'ul' ) as $ul )
674 foreach ( $ul- > find ( 'li' ) as $li )
677 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
678 $this- > action ( 'add' , $url , 0 , TRUE );
681 if ( STORAGE
== 'postgres' ) {
682 $sequence = 'entries_id_seq' ;
684 $last_id = $this- > store
-> getLastId ( $sequence );
685 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
689 # the second <ul> is for read links
692 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
693 Tools
:: logm ( 'import from pocket completed' );
698 * import from Readability. poche needs a ./readability file
699 * @todo add the return value
700 * @param string $targetFile the file used for importing
703 private function importFromReadability ( $targetFile )
705 # TODO gestion des articles lus / favs
706 $str_data = file_get_contents ( $targetFile );
707 $data = json_decode ( $str_data , true );
708 Tools
:: logm ( 'starting import from Readability' );
710 foreach ( $data as $key => $value ) {
714 foreach ( $value as $attr => $attr_value ) {
715 if ( $attr == 'article__url' ) {
716 $url = new Url ( base64_encode ( $attr_value ));
719 if ( STORAGE
== 'postgres' ) {
720 $sequence = 'entries_id_seq' ;
722 if ( $attr_value == 'true' ) {
723 if ( $attr == 'favorite' ) {
726 if ( $attr == 'archive' ) {
732 if (! is_null ( $url ) && $url- > isCorrect ()) {
733 $this- > action ( 'add' , $url , 0 , TRUE );
736 $last_id = $this- > store
-> getLastId ( $sequence );
737 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
740 $last_id = $this- > store
-> getLastId ( $sequence );
741 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
745 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
746 Tools
:: logm ( 'import from Readability completed' );
751 * import datas into your poche
752 * @param string $from name of the service to import : pocket, instapaper or readability
753 * @todo add the return value
756 public function import ( $from )
759 'pocket' => 'importFromPocket' ,
760 'readability' => 'importFromReadability' ,
761 'instapaper' => 'importFromInstapaper'
764 if (! isset ( $providers [ $from ])) {
765 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
769 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
770 $targetFile = constant ( $targetDefinition );
772 if (! defined ( $targetDefinition )) {
773 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
777 if (! file_exists ( $targetFile )) {
778 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
782 $this- > $providers [ $from ]( $targetFile );
786 * export poche entries in json
787 * @return json all poche entries
789 public function export ()
791 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
792 echo $this- > tpl
-> render ( 'export.twig' , array (
793 'export' => Tools
:: renderJson ( $entries ),
795 Tools
:: logm ( 'export view' );
799 * Checks online the latest version of poche and cache it
800 * @param string $which 'prod' or 'dev'
801 * @return string latest $which version
803 private function getPocheVersion ( $which = 'prod' )
805 $cache_file = CACHE
. '/' . $which ;
807 # checks if the cached version file exists
808 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
809 $version = file_get_contents ( $cache_file );
811 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
812 file_put_contents ( $cache_file , $version , LOCK_EX
);