]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
245f6a138fc353049c52c2ccf3a2941432ff0938
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 $notInstalledMessage = array ();
25 # @todo make this dynamic (actually install themes and save them in the database including author information et cetera)
26 private $installedThemes = array (
27 'default' => array ( 'requires' => array ()),
28 'dark' => array ( 'requires' => array ( 'default' )),
29 'dmagenta' => array ( 'requires' => array ( 'default' )),
30 'solarized' => array ( 'requires' => array ( 'default' )),
31 'solarized-dark' => array ( 'requires' => array ( 'default' ))
34 public function __construct ()
36 if ( $this- > configFileIsAvailable ()) {
40 if ( $this- > themeIsInstalled ()) {
44 if ( $this- > systemIsInstalled ()) {
45 $this- > store
= new Database ();
46 $this- > messages
= new Messages ();
48 if (! $this- > store
-> isInstalled ()) {
54 private function init ()
57 Session
:: $sessionName = 'poche' ;
60 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
61 $this- > user
= $_SESSION [ 'poche_user' ];
63 # fake user, just for install & login screens
64 $this- > user
= new User ();
65 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
69 $language = $this- > user
-> getConfigValue ( 'language' );
70 putenv ( 'LC_ALL=' . $language );
71 setlocale ( LC_ALL
, $language );
72 bindtextdomain ( $language , LOCALE
);
73 textdomain ( $language );
76 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
79 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
81 if ( $themeDirectory === false ) {
82 $themeDirectory = DEFAULT_THEME
;
85 $this- > currentTheme
= $themeDirectory ;
88 public function configFileIsAvailable () {
89 if (! self
:: $configFileAvailable ) {
90 $this- > notInstalledMessage
[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.' ;
98 public function themeIsInstalled () {
100 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
101 if (! self
:: $canRenderTemplates ) {
102 $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>' ;
106 if (! is_writable ( CACHE
)) {
107 $this- > notInstalledMessage
[] = 'You don \' t have write access on cache directory.' ;
109 self
:: $canRenderTemplates = false ;
114 # Check if the selected theme and its requirements are present
115 if ( $this- > getTheme () != '' && ! is_dir ( THEME
. '/' . $this- > getTheme ())) {
116 $this- > notInstalledMessage
[] = 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $this- > getTheme () . ')' ;
118 self
:: $canRenderTemplates = false ;
123 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
124 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
125 $this- > notInstalledMessage
[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' ;
127 self
:: $canRenderTemplates = false ;
142 * all checks before installation.
143 * @todo move HTML to template
146 public function systemIsInstalled ()
150 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
152 if ( empty ( $configSalt )) {
153 $this- > notInstalledMessage
[] = 'You have not yet filled in the SALT value in the config.inc.php file.' ;
156 if ( STORAGE
== 'sqlite' && ! file_exists ( STORAGE_SQLITE
)) {
157 Tools
:: logm ( 'sqlite file doesn \' t exist' );
158 $this- > notInstalledMessage
[] = 'sqlite file doesn \' t exist, you can find it in install folder. Copy it in /db folder.' ;
161 if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
162 $this- > notInstalledMessage
[] = 'you have to delete the /install folder before using poche.' ;
165 if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
166 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
167 $this- > notInstalledMessage
[] = 'You don \' t have write access on sqlite file.' ;
178 public function getNotInstalledMessage () {
179 return $this- > notInstalledMessage
;
182 private function initTpl ()
184 $loaderChain = new Twig_Loader_Chain ();
186 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
188 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $this- > getTheme ()));
189 } catch ( Twig_Error_Loader
$e ) {
190 # @todo isInstalled() should catch this, inject Twig later
191 die ( 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (' . THEME
. '/' . $this- > getTheme () . ' is missing)' );
194 # add all required themes to the loader chain
195 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
197 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . DEFAULT_THEME
));
198 } catch ( Twig_Error_Loader
$e ) {
199 # @todo isInstalled() should catch this, inject Twig later
200 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' );
205 $twig_params = array ();
207 $twig_params = array ( 'cache' => CACHE
);
210 $this- > tpl
= new Twig_Environment ( $loaderChain , $twig_params );
211 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
213 # filter to display domain name of an url
214 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
215 $this- > tpl
-> addFilter ( $filter );
217 # filter for reading time
218 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
219 $this- > tpl
-> addFilter ( $filter );
221 # filter for simple filenames in config view
222 $filter = new Twig_SimpleFilter ( 'getPrettyFilename' , function ( $string ) { return str_replace ( ROOT
, '' , $
string ); });
223 $this- > tpl
-> addFilter ( $filter );
226 private function install ()
228 Tools
:: logm ( 'poche still not installed' );
229 echo $this- > tpl
-> render ( 'install.twig' , array (
230 'token' => Session
:: getToken (),
231 'theme' => $this- > getTheme (),
232 'poche_url' => Tools
:: getPocheUrl ()
234 if ( isset ( $_GET [ 'install' ])) {
235 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
236 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
237 # let's rock, install poche baby !
238 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
241 Tools
:: logm ( 'poche is now installed' );
246 Tools
:: logm ( 'error during installation' );
253 public function getTheme () {
254 return $this- > currentTheme
;
257 public function getInstalledThemes () {
258 $handle = opendir ( THEME
);
261 while (( $theme = readdir ( $handle )) !== false ) {
262 # Themes are stored in a directory, so all directory names are themes
263 # @todo move theme installation data to database
264 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '..' , '.' , '.git' ))) {
270 if ( $theme === $this- > getTheme ()) {
274 $themes [] = array ( 'name' => $theme , 'current' => $current );
280 public function getDefaultConfig ()
283 'pager' => PAGINATION
,
285 'theme' => DEFAULT_THEME
290 * Call action (mark as fav, archive, delete, etc.)
292 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
297 $content = $url- > extract ();
299 if ( $this- > store
-> add ( $url- > getUrl (), $content [ 'title' ], $content [ 'body' ], $this- > user
-> getId ())) {
300 Tools
:: logm ( 'add link ' . $url- > getUrl ());
302 if ( STORAGE
== 'postgres' ) {
303 $sequence = 'entries_id_seq' ;
305 $last_id = $this- > store
-> getLastId ( $sequence );
306 if ( DOWNLOAD_PICTURES
) {
307 $content = filtre_picture ( $content [ 'body' ], $url- > getUrl (), $last_id );
308 Tools
:: logm ( 'updating content article' );
309 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
312 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
317 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
318 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
323 Tools
:: redirect ( '?view=home' );
327 $msg = 'delete link #' . $id ;
328 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
329 if ( DOWNLOAD_PICTURES
) {
330 remove_directory ( ABS_PATH
. $id );
332 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
335 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
336 $msg = 'error : can \' t delete link #' . $id ;
342 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
343 Tools
:: logm ( 'mark as favorite link #' . $id );
348 case 'toggle_archive' :
349 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
350 Tools
:: logm ( 'archive link #' . $id );
360 function displayView ( $view , $id = 0 )
367 $dev = $this- > getPocheVersion ( 'dev' );
368 $prod = $this- > getPocheVersion ( 'prod' );
369 $compare_dev = version_compare ( POCHE
, $dev );
370 $compare_prod = version_compare ( POCHE
, $prod );
371 $themes = $this- > getInstalledThemes ();
376 'compare_dev' => $compare_dev ,
377 'compare_prod' => $compare_prod ,
379 Tools
:: logm ( 'config view' );
382 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
383 if ( $entry != NULL ) {
384 Tools
:: logm ( 'view link #' . $id );
385 $content = $entry [ 'content' ];
386 if ( function_exists ( 'tidy_parse_string' )) {
387 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
388 $tidy- > cleanRepair ();
389 $content = $tidy- > value
;
393 $flattr = new FlattrItem ();
394 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
398 'content' => $content ,
403 Tools
:: logm ( 'error in view call : entry is null' );
406 default : # home, favorites and archive views
407 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
414 if ( count ( $entries ) > 0 ) {
415 $this- > pagination
-> set_total ( count ( $entries ));
416 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
417 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
418 $tpl_vars [ 'entries' ] = $datas ;
419 $tpl_vars [ 'page_links' ] = $page_links ;
420 $tpl_vars [ 'nb_results' ] = count ( $entries );
422 Tools
:: logm ( 'display ' . $view . ' view' );
430 * update the password of the current user.
431 * if MODE_DEMO is TRUE, the password can't be updated.
432 * @todo add the return value
433 * @todo set the new password in function header like this updatePassword($newPassword)
436 public function updatePassword ()
439 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
440 Tools
:: logm ( 'in demo mode, you can \' t do this' );
441 Tools
:: redirect ( '?view=config' );
444 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
445 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
446 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
447 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
449 Tools
:: logm ( 'password updated' );
453 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
454 Tools
:: redirect ( '?view=config' );
460 public function updateTheme ()
463 if ( empty ( $_POST [ 'theme' ])) {
466 # we are not going to change it to the current theme...
467 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
468 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
469 Tools
:: redirect ( '?view=config' );
472 $themes = $this- > getInstalledThemes ();
473 $actualTheme = false ;
475 foreach ( $themes as $theme ) {
476 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
482 if (! $actualTheme ) {
483 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
484 Tools
:: redirect ( '?view=config' );
487 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
488 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
490 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
491 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
493 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
495 Tools
:: redirect ( '?view=config' );
499 * checks if login & password are correct and save the user in session.
500 * it redirects the user to the $referer link
501 * @param string $referer the url to redirect after login
502 * @todo add the return value
505 public function login ( $referer )
507 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
508 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
509 if ( $user != array ()) {
510 # Save login into Session
511 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
512 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
513 Tools
:: logm ( 'login successful' );
514 Tools
:: redirect ( $referer );
516 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
517 Tools
:: logm ( 'login failed' );
520 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
521 Tools
:: logm ( 'login failed' );
527 * log out the poche user. It cleans the session.
528 * @todo add the return value
531 public function logout ()
533 $this- > user
= array ();
535 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
536 Tools
:: logm ( 'logout' );
541 * import from Instapaper. poche needs a ./instapaper-export.html file
542 * @todo add the return value
543 * @param string $targetFile the file used for importing
546 private function importFromInstapaper ( $targetFile )
548 # TODO gestion des articles favs
549 $html = new simple_html_dom ();
550 $html- > load_file ( $targetFile );
551 Tools
:: logm ( 'starting import from instapaper' );
555 foreach ( $html- > find ( 'ol' ) as $ul )
557 foreach ( $ul- > find ( 'li' ) as $li )
560 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
561 $this- > action ( 'add' , $url , 0 , TRUE );
564 if ( STORAGE
== 'postgres' ) {
565 $sequence = 'entries_id_seq' ;
567 $last_id = $this- > store
-> getLastId ( $sequence );
568 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
572 # the second <ol> is for read links
575 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
576 Tools
:: logm ( 'import from instapaper completed' );
581 * import from Pocket. poche needs a ./ril_export.html file
582 * @todo add the return value
583 * @param string $targetFile the file used for importing
586 private function importFromPocket ( $targetFile )
588 # TODO gestion des articles favs
589 $html = new simple_html_dom ();
590 $html- > load_file ( $targetFile );
591 Tools
:: logm ( 'starting import from pocket' );
595 foreach ( $html- > find ( 'ul' ) as $ul )
597 foreach ( $ul- > find ( 'li' ) as $li )
600 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
601 $this- > action ( 'add' , $url , 0 , TRUE );
604 if ( STORAGE
== 'postgres' ) {
605 $sequence = 'entries_id_seq' ;
607 $last_id = $this- > store
-> getLastId ( $sequence );
608 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
612 # the second <ul> is for read links
615 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
616 Tools
:: logm ( 'import from pocket completed' );
621 * import from Readability. poche needs a ./readability file
622 * @todo add the return value
623 * @param string $targetFile the file used for importing
626 private function importFromReadability ( $targetFile )
628 # TODO gestion des articles lus / favs
629 $str_data = file_get_contents ( $targetFile );
630 $data = json_decode ( $str_data , true );
631 Tools
:: logm ( 'starting import from Readability' );
633 foreach ( $data as $key => $value ) {
637 foreach ( $value as $attr => $attr_value ) {
638 if ( $attr == 'article__url' ) {
639 $url = new Url ( base64_encode ( $attr_value ));
642 if ( STORAGE
== 'postgres' ) {
643 $sequence = 'entries_id_seq' ;
645 if ( $attr_value == 'true' ) {
646 if ( $attr == 'favorite' ) {
649 if ( $attr == 'archive' ) {
655 if (! is_null ( $url ) && $url- > isCorrect ()) {
656 $this- > action ( 'add' , $url , 0 , TRUE );
659 $last_id = $this- > store
-> getLastId ( $sequence );
660 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
663 $last_id = $this- > store
-> getLastId ( $sequence );
664 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
668 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
669 Tools
:: logm ( 'import from Readability completed' );
674 * import datas into your poche
675 * @param string $from name of the service to import : pocket, instapaper or readability
676 * @todo add the return value
679 public function import ( $from )
682 'pocket' => 'importFromPocket' ,
683 'readability' => 'importFromReadability' ,
684 'instapaper' => 'importFromInstapaper'
687 if (! isset ( $providers [ $from ])) {
688 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
692 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
693 $targetFile = constant ( $targetDefinition );
695 if (! defined ( $targetDefinition )) {
696 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
700 if (! file_exists ( $targetFile )) {
701 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
705 $this- > $providers [ $from ]( $targetFile );
709 * export poche entries in json
710 * @return json all poche entries
712 public function export ()
714 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
715 echo $this- > tpl
-> render ( 'export.twig' , array (
716 'export' => Tools
:: renderJson ( $entries ),
718 Tools
:: logm ( 'export view' );
722 * Checks online the latest version of poche and cache it
723 * @param string $which 'prod' or 'dev'
724 * @return string latest $which version
726 private function getPocheVersion ( $which = 'prod' )
728 $cache_file = CACHE
. '/' . $which ;
730 # checks if the cached version file exists
731 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
732 $version = file_get_contents ( $cache_file );
734 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
735 file_put_contents ( $cache_file , $version , LOCK_EX
);