]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
8fe7a4f53869dd98fc96601586fdbbde74f4f7f1
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 = '' ;
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 ()) {
42 if (! $this- > themeIsInstalled ()) {
48 if (! $this- > systemIsInstalled ()) {
52 $this- > store
= new Database ();
53 $this- > messages
= new Messages ();
56 if (! $this- > store
-> isInstalled ()) {
61 private function init ()
64 Session
:: $sessionName = 'poche' ;
67 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
68 $this- > user
= $_SESSION [ 'poche_user' ];
70 # fake user, just for install & login screens
71 $this- > user
= new User ();
72 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
76 $language = $this- > user
-> getConfigValue ( 'language' );
77 putenv ( 'LC_ALL=' . $language );
78 setlocale ( LC_ALL
, $language );
79 bindtextdomain ( $language , LOCALE
);
80 textdomain ( $language );
83 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
86 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
88 if ( $themeDirectory === false ) {
89 $themeDirectory = DEFAULT_THEME
;
92 $this- > currentTheme
= $themeDirectory ;
95 public function configFileIsAvailable () {
96 if (! self
:: $configFileAvailable ) {
97 $this- > notInstalledMessage
= 'You have to rename <strong>inc/poche/config.inc.php.new</strong> to <strong>inc/poche/config.inc.php</strong>.' ;
105 public function themeIsInstalled () {
106 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
107 if (! self
:: $canRenderTemplates ) {
108 $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://inthepoche.com/?pages/Documentation">the documentation.</a>' ;
113 if (! is_writable ( CACHE
)) {
114 $this- > notInstalledMessage
= '<h1>error</h1><p>You don \' t have write access on cache directory.</p>' ;
116 self
:: $canRenderTemplates = false ;
121 # Check if the selected theme and its requirements are present
122 if (! is_dir ( THEME
. '/' . $this- > getTheme ())) {
123 $this- > notInstalledMessage
= 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $this- > getTheme () . ')' ;
125 self
:: $canRenderTemplates = false ;
130 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
131 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
132 $this- > notInstalledMessage
= 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' ;
134 self
:: $canRenderTemplates = false ;
144 * all checks before installation.
145 * @todo move HTML to template
148 public function systemIsInstalled ()
152 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
154 if ( empty ( $configSalt )) {
155 $msg = '<h1>error</h1><p>You have not yet filled in the SALT value in the config.inc.php file.</p>' ;
156 } else if ( STORAGE
== 'sqlite' && ! file_exists ( STORAGE_SQLITE
)) {
157 Tools
:: logm ( 'sqlite file doesn \' t exist' );
158 $msg = '<h1>error</h1><p>sqlite file doesn \' t exist, you can find it in install folder. Copy it in /db folder.</p>' ;
159 } else if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
160 $msg = '<h1>install folder</h1><p>you have to delete the /install folder before using poche.</p>' ;
161 } else if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
162 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
163 $msg = '<h1>error</h1><p>You don \' t have write access on sqlite file.</p>' ;
167 $this- > notInstalledMessage
= $msg ;
175 public function getNotInstalledMessage () {
176 return $this- > notInstalledMessage
;
179 private function initTpl ()
181 $loaderChain = new Twig_Loader_Chain ();
183 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
185 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $this- > getTheme ()));
186 } catch ( Twig_Error_Loader
$e ) {
187 # @todo isInstalled() should catch this, inject Twig later
188 die ( 'The currently selected theme (' . $this- > getTheme () . ') does not seem to be properly installed (' . THEME
. '/' . $this- > getTheme () . ' is missing)' );
191 # add all required themes to the loader chain
192 foreach ( $this- > installedThemes
[ $this- > getTheme ()][ 'requires' ] as $requiredTheme ) {
194 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . DEFAULT_THEME
));
195 } catch ( Twig_Error_Loader
$e ) {
196 # @todo isInstalled() should catch this, inject Twig later
197 die ( 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this- > getTheme () . ')' );
202 $twig_params = array ();
204 $twig_params = array ( 'cache' => CACHE
);
207 $this- > tpl
= new Twig_Environment ( $loaderChain , $twig_params );
208 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
210 # filter to display domain name of an url
211 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
212 $this- > tpl
-> addFilter ( $filter );
214 # filter for reading time
215 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
216 $this- > tpl
-> addFilter ( $filter );
218 # filter for simple filenames in config view
219 $filter = new Twig_SimpleFilter ( 'getPrettyFilename' , function ( $string ) { return str_replace ( ROOT
, '' , $
string ); });
220 $this- > tpl
-> addFilter ( $filter );
223 private function install ()
225 Tools
:: logm ( 'poche still not installed' );
226 echo $this- > tpl
-> render ( 'install.twig' , array (
227 'token' => Session
:: getToken (),
228 'theme' => $this- > getTheme (),
229 'poche_url' => Tools
:: getPocheUrl ()
231 if ( isset ( $_GET [ 'install' ])) {
232 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
233 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
234 # let's rock, install poche baby !
235 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
238 Tools
:: logm ( 'poche is now installed' );
243 Tools
:: logm ( 'error during installation' );
250 public function getTheme () {
251 return $this- > currentTheme
;
254 public function getInstalledThemes () {
255 $handle = opendir ( THEME
);
258 while (( $theme = readdir ( $handle )) !== false ) {
259 # Themes are stored in a directory, so all directory names are themes
260 # @todo move theme installation data to database
261 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '..' , '.' , '.git' ))) {
267 if ( $theme === $this- > getTheme ()) {
271 $themes [] = array ( 'name' => $theme , 'current' => $current );
277 public function getDefaultConfig ()
280 'pager' => PAGINATION
,
282 'theme' => DEFAULT_THEME
287 * Call action (mark as fav, archive, delete, etc.)
289 public function action ( $action , Url
$url , $id = 0 , $import = FALSE )
294 $content = $url- > extract ();
296 if ( $this- > store
-> add ( $url- > getUrl (), $content [ 'title' ], $content [ 'body' ], $this- > user
-> getId ())) {
297 Tools
:: logm ( 'add link ' . $url- > getUrl ());
299 if ( STORAGE
== 'postgres' ) {
300 $sequence = 'entries_id_seq' ;
302 $last_id = $this- > store
-> getLastId ( $sequence );
303 if ( DOWNLOAD_PICTURES
) {
304 $content = filtre_picture ( $content [ 'body' ], $url- > getUrl (), $last_id );
305 Tools
:: logm ( 'updating content article' );
306 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
309 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
314 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
315 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
320 Tools
:: redirect ( '?view=home' );
324 $msg = 'delete link #' . $id ;
325 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
326 if ( DOWNLOAD_PICTURES
) {
327 remove_directory ( ABS_PATH
. $id );
329 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
332 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
333 $msg = 'error : can \' t delete link #' . $id ;
339 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
340 Tools
:: logm ( 'mark as favorite link #' . $id );
345 case 'toggle_archive' :
346 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
347 Tools
:: logm ( 'archive link #' . $id );
357 function displayView ( $view , $id = 0 )
364 $dev = $this- > getPocheVersion ( 'dev' );
365 $prod = $this- > getPocheVersion ( 'prod' );
366 $compare_dev = version_compare ( POCHE_VERSION
, $dev );
367 $compare_prod = version_compare ( POCHE_VERSION
, $prod );
368 $themes = $this- > getInstalledThemes ();
373 'compare_dev' => $compare_dev ,
374 'compare_prod' => $compare_prod ,
376 Tools
:: logm ( 'config view' );
379 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
380 if ( $entry != NULL ) {
381 Tools
:: logm ( 'view link #' . $id );
382 $content = $entry [ 'content' ];
383 if ( function_exists ( 'tidy_parse_string' )) {
384 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
385 $tidy- > cleanRepair ();
386 $content = $tidy- > value
;
390 $flattr = new FlattrItem ();
391 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
395 'content' => $content ,
400 Tools
:: logm ( 'error in view call : entry is null' );
403 default : # home, favorites and archive views
404 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
411 if ( count ( $entries ) > 0 ) {
412 $this- > pagination
-> set_total ( count ( $entries ));
413 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
414 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
415 $tpl_vars [ 'entries' ] = $datas ;
416 $tpl_vars [ 'page_links' ] = $page_links ;
417 $tpl_vars [ 'nb_results' ] = count ( $entries );
419 Tools
:: logm ( 'display ' . $view . ' view' );
427 * update the password of the current user.
428 * if MODE_DEMO is TRUE, the password can't be updated.
429 * @todo add the return value
430 * @todo set the new password in function header like this updatePassword($newPassword)
433 public function updatePassword ()
436 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
437 Tools
:: logm ( 'in demo mode, you can \' t do this' );
438 Tools
:: redirect ( '?view=config' );
441 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
442 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
443 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
444 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
446 Tools
:: logm ( 'password updated' );
450 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
451 Tools
:: redirect ( '?view=config' );
457 public function updateTheme ()
460 if ( empty ( $_POST [ 'theme' ])) {
463 # we are not going to change it to the current theme...
464 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
465 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
466 Tools
:: redirect ( '?view=config' );
469 $themes = $this- > getInstalledThemes ();
470 $actualTheme = false ;
472 foreach ( $themes as $theme ) {
473 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
479 if (! $actualTheme ) {
480 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
481 Tools
:: redirect ( '?view=config' );
484 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
485 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
487 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
488 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
490 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
492 Tools
:: redirect ( '?view=config' );
496 * checks if login & password are correct and save the user in session.
497 * it redirects the user to the $referer link
498 * @param string $referer the url to redirect after login
499 * @todo add the return value
502 public function login ( $referer )
504 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
505 $user = $this- > store
-> login ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]));
506 if ( $user != array ()) {
507 # Save login into Session
508 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ]), array ( 'poche_user' => new User ( $user )));
509 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
510 Tools
:: logm ( 'login successful' );
511 Tools
:: redirect ( $referer );
513 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
514 Tools
:: logm ( 'login failed' );
517 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
518 Tools
:: logm ( 'login failed' );
524 * log out the poche user. It cleans the session.
525 * @todo add the return value
528 public function logout ()
530 $this- > user
= array ();
532 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
533 Tools
:: logm ( 'logout' );
538 * import from Instapaper. poche needs a ./instapaper-export.html file
539 * @todo add the return value
540 * @param string $targetFile the file used for importing
543 private function importFromInstapaper ( $targetFile )
545 # TODO gestion des articles favs
546 $html = new simple_html_dom ();
547 $html- > load_file ( $targetFile );
548 Tools
:: logm ( 'starting import from instapaper' );
552 foreach ( $html- > find ( 'ol' ) as $ul )
554 foreach ( $ul- > find ( 'li' ) as $li )
557 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
558 $this- > action ( 'add' , $url , 0 , TRUE );
561 if ( STORAGE
== 'postgres' ) {
562 $sequence = 'entries_id_seq' ;
564 $last_id = $this- > store
-> getLastId ( $sequence );
565 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
569 # the second <ol> is for read links
572 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
573 Tools
:: logm ( 'import from instapaper completed' );
578 * import from Pocket. poche needs a ./ril_export.html file
579 * @todo add the return value
580 * @param string $targetFile the file used for importing
583 private function importFromPocket ( $targetFile )
585 # TODO gestion des articles favs
586 $html = new simple_html_dom ();
587 $html- > load_file ( $targetFile );
588 Tools
:: logm ( 'starting import from pocket' );
592 foreach ( $html- > find ( 'ul' ) as $ul )
594 foreach ( $ul- > find ( 'li' ) as $li )
597 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
598 $this- > action ( 'add' , $url , 0 , TRUE );
601 if ( STORAGE
== 'postgres' ) {
602 $sequence = 'entries_id_seq' ;
604 $last_id = $this- > store
-> getLastId ( $sequence );
605 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
609 # the second <ul> is for read links
612 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
613 Tools
:: logm ( 'import from pocket completed' );
618 * import from Readability. poche needs a ./readability file
619 * @todo add the return value
620 * @param string $targetFile the file used for importing
623 private function importFromReadability ( $targetFile )
625 # TODO gestion des articles lus / favs
626 $str_data = file_get_contents ( $targetFile );
627 $data = json_decode ( $str_data , true );
628 Tools
:: logm ( 'starting import from Readability' );
630 foreach ( $data as $key => $value ) {
634 foreach ( $value as $attr => $attr_value ) {
635 if ( $attr == 'article__url' ) {
636 $url = new Url ( base64_encode ( $attr_value ));
639 if ( STORAGE
== 'postgres' ) {
640 $sequence = 'entries_id_seq' ;
642 if ( $attr_value == 'true' ) {
643 if ( $attr == 'favorite' ) {
646 if ( $attr == 'archive' ) {
652 if (! is_null ( $url ) && $url- > isCorrect ()) {
653 $this- > action ( 'add' , $url , 0 , TRUE );
656 $last_id = $this- > store
-> getLastId ( $sequence );
657 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
660 $last_id = $this- > store
-> getLastId ( $sequence );
661 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
665 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
666 Tools
:: logm ( 'import from Readability completed' );
671 * import datas into your poche
672 * @param string $from name of the service to import : pocket, instapaper or readability
673 * @todo add the return value
676 public function import ( $from )
679 'pocket' => 'importFromPocket' ,
680 'readability' => 'importFromReadability' ,
681 'instapaper' => 'importFromInstapaper'
684 if (! isset ( $providers [ $from ])) {
685 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
689 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
690 $targetFile = constant ( $targetDefinition );
692 if (! defined ( $targetDefinition )) {
693 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
697 if (! file_exists ( $targetFile )) {
698 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
702 $this- > $providers [ $from ]( $targetFile );
706 * export poche entries in json
707 * @return json all poche entries
709 public function export ()
711 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
712 echo $this- > tpl
-> render ( 'export.twig' , array (
713 'export' => Tools
:: renderJson ( $entries ),
715 Tools
:: logm ( 'export view' );
719 * Checks online the latest version of poche and cache it
720 * @param string $which 'prod' or 'dev'
721 * @return string latest $which version
723 private function getPocheVersion ( $which = 'prod' )
725 $cache_file = CACHE
. '/' . $which ;
727 # checks if the cached version file exists
728 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
729 $version = file_get_contents ( $cache_file );
731 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
732 file_put_contents ( $cache_file , $version , LOCK_EX
);