]>
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 ( '..' , '.' ))) {
284 if ( $theme === $this- > getTheme ()) {
288 $themes [] = array ( 'name' => $theme , 'current' => $current );
295 public function getInstalledLanguages () {
296 $handle = opendir ( LOCALE
);
297 $languages = array ();
299 while (( $language = readdir ( $handle )) !== false ) {
300 # Languages are stored in a directory, so all directory names are languages
301 # @todo move language installation data to database
302 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
308 if ( $language === $this- > getLanguage ()) {
312 $languages [] = array ( 'name' => $language , 'current' => $current );
318 public function getDefaultConfig ()
321 'pager' => PAGINATION
,
323 'theme' => DEFAULT_THEME
328 * Call action (mark as fav, archive, delete, etc.)
330 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE )
335 $json = file_get_contents ( Tools
:: getPocheUrl () . '/inc/3rdparty/makefulltextfeed.php?url=' . urlencode ( $url- > getUrl ()). '&max=5&links=preserve&exc=&format=json&submit=Create+Feed' );
336 $content = json_decode ( $json , true );
337 $title = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ];
338 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
340 if ( $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ())) {
341 Tools
:: logm ( 'add link ' . $url- > getUrl ());
343 if ( STORAGE
== 'postgres' ) {
344 $sequence = 'entries_id_seq' ;
346 $last_id = $this- > store
-> getLastId ( $sequence );
347 if ( DOWNLOAD_PICTURES
) {
348 $content = filtre_picture ( $body , $url- > getUrl (), $last_id );
349 Tools
:: logm ( 'updating content article' );
350 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
353 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
358 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
359 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
364 if ( $autoclose == TRUE ) {
365 Tools
:: redirect ( '?view=home' );
367 Tools
:: redirect ( '?view=home&closewin=true' );
372 $msg = 'delete link #' . $id ;
373 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
374 if ( DOWNLOAD_PICTURES
) {
375 remove_directory ( ABS_PATH
. $id );
377 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
380 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
381 $msg = 'error : can \' t delete link #' . $id ;
384 Tools
:: redirect ( '?' );
387 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
388 Tools
:: logm ( 'mark as favorite link #' . $id );
393 case 'toggle_archive' :
394 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
395 Tools
:: logm ( 'archive link #' . $id );
405 function displayView ( $view , $id = 0 )
412 $dev = $this- > getPocheVersion ( 'dev' );
413 $prod = $this- > getPocheVersion ( 'prod' );
414 $compare_dev = version_compare ( POCHE
, $dev );
415 $compare_prod = version_compare ( POCHE
, $prod );
416 $themes = $this- > getInstalledThemes ();
417 $languages = $this- > getInstalledLanguages ();
418 $token = $this- > user
-> getConfigValue ( 'token' );
419 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]))? true : false ;
422 'languages' => $languages ,
425 'compare_dev' => $compare_dev ,
426 'compare_prod' => $compare_prod ,
428 'user_id' => $this- > user
-> getId (),
429 'http_auth' => $http_auth ,
431 Tools
:: logm ( 'config view' );
434 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
435 if ( $entry != NULL ) {
436 Tools
:: logm ( 'view link #' . $id );
437 $content = $entry [ 'content' ];
438 if ( function_exists ( 'tidy_parse_string' )) {
439 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
440 $tidy- > cleanRepair ();
441 $content = $tidy- > value
;
445 $flattr = new FlattrItem ();
446 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
450 'content' => $content ,
455 Tools
:: logm ( 'error in view call : entry is null' );
458 default : # home, favorites and archive views
459 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
466 if ( count ( $entries ) > 0 ) {
467 $this- > pagination
-> set_total ( count ( $entries ));
468 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
469 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
470 $tpl_vars [ 'entries' ] = $datas ;
471 $tpl_vars [ 'page_links' ] = $page_links ;
472 $tpl_vars [ 'nb_results' ] = count ( $entries );
474 Tools
:: logm ( 'display ' . $view . ' view' );
482 * update the password of the current user.
483 * if MODE_DEMO is TRUE, the password can't be updated.
484 * @todo add the return value
485 * @todo set the new password in function header like this updatePassword($newPassword)
488 public function updatePassword ()
491 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
492 Tools
:: logm ( 'in demo mode, you can \' t do this' );
493 Tools
:: redirect ( '?view=config' );
496 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
497 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
498 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
499 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
501 Tools
:: logm ( 'password updated' );
505 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
506 Tools
:: redirect ( '?view=config' );
512 public function updateTheme ()
515 if ( empty ( $_POST [ 'theme' ])) {
518 # we are not going to change it to the current theme...
519 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
520 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
521 Tools
:: redirect ( '?view=config' );
524 $themes = $this- > getInstalledThemes ();
525 $actualTheme = false ;
527 foreach ( $themes as $theme ) {
528 if ( $theme [ 'name' ] == $_POST [ 'theme' ]) {
534 if (! $actualTheme ) {
535 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
536 Tools
:: redirect ( '?view=config' );
539 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
540 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
542 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
543 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
545 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
547 Tools
:: redirect ( '?view=config' );
550 public function updateLanguage ()
553 if ( empty ( $_POST [ 'language' ])) {
556 # we are not going to change it to the current language...
557 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
558 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
559 Tools
:: redirect ( '?view=config' );
562 $languages = $this- > getInstalledLanguages ();
563 $actualLanguage = false ;
565 foreach ( $languages as $language ) {
566 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
567 $actualLanguage = true ;
572 if (! $actualLanguage ) {
573 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
574 Tools
:: redirect ( '?view=config' );
577 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
578 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
580 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
581 $currentConfig [ 'language' ] = $_POST [ 'language' ];
583 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
585 Tools
:: redirect ( '?view=config' );
589 * get credentials from differents sources
590 * it redirects the user to the $referer link
593 private function credentials () {
594 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
595 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' );
597 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
598 return array ( $_POST [ 'login' ], $_POST [ 'password' ]);
600 return array ( false , false );
604 * checks if login & password are correct and save the user in session.
605 * it redirects the user to the $referer link
606 * @param string $referer the url to redirect after login
607 * @todo add the return value
610 public function login ( $referer )
612 list ( $login , $password )= $this- > credentials ();
613 if ( $login === false || $password === false ) {
614 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
615 Tools
:: logm ( 'login failed' );
618 if (! empty ( $login ) && ! empty ( $password )) {
619 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ));
620 if ( $user != array ()) {
621 # Save login into Session
622 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , Tools
:: encodeString ( $password . $login ), array ( 'poche_user' => new User ( $user )));
623 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
624 Tools
:: logm ( 'login successful' );
625 Tools
:: redirect ( $referer );
627 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
628 Tools
:: logm ( 'login failed' );
634 * log out the poche user. It cleans the session.
635 * @todo add the return value
638 public function logout ()
640 $this- > user
= array ();
642 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
643 Tools
:: logm ( 'logout' );
648 * import from Instapaper. poche needs a ./instapaper-export.html file
649 * @todo add the return value
650 * @param string $targetFile the file used for importing
653 private function importFromInstapaper ( $targetFile )
655 # TODO gestion des articles favs
656 $html = new simple_html_dom ();
657 $html- > load_file ( $targetFile );
658 Tools
:: logm ( 'starting import from instapaper' );
662 foreach ( $html- > find ( 'ol' ) as $ul )
664 foreach ( $ul- > find ( 'li' ) as $li )
667 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
668 $this- > action ( 'add' , $url , 0 , TRUE );
671 if ( STORAGE
== 'postgres' ) {
672 $sequence = 'entries_id_seq' ;
674 $last_id = $this- > store
-> getLastId ( $sequence );
675 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
679 # the second <ol> is for read links
682 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
683 Tools
:: logm ( 'import from instapaper completed' );
688 * import from Pocket. poche needs a ./ril_export.html file
689 * @todo add the return value
690 * @param string $targetFile the file used for importing
693 private function importFromPocket ( $targetFile )
695 # TODO gestion des articles favs
696 $html = new simple_html_dom ();
697 $html- > load_file ( $targetFile );
698 Tools
:: logm ( 'starting import from pocket' );
702 foreach ( $html- > find ( 'ul' ) as $ul )
704 foreach ( $ul- > find ( 'li' ) as $li )
707 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
708 $this- > action ( 'add' , $url , 0 , TRUE );
711 if ( STORAGE
== 'postgres' ) {
712 $sequence = 'entries_id_seq' ;
714 $last_id = $this- > store
-> getLastId ( $sequence );
715 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
719 # the second <ul> is for read links
722 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
723 Tools
:: logm ( 'import from pocket completed' );
728 * import from Readability. poche needs a ./readability file
729 * @todo add the return value
730 * @param string $targetFile the file used for importing
733 private function importFromReadability ( $targetFile )
735 # TODO gestion des articles lus / favs
736 $str_data = file_get_contents ( $targetFile );
737 $data = json_decode ( $str_data , true );
738 Tools
:: logm ( 'starting import from Readability' );
740 foreach ( $data as $key => $value ) {
744 foreach ( $value as $attr => $attr_value ) {
745 if ( $attr == 'article__url' ) {
746 $url = new Url ( base64_encode ( $attr_value ));
749 if ( STORAGE
== 'postgres' ) {
750 $sequence = 'entries_id_seq' ;
752 if ( $attr_value == 'true' ) {
753 if ( $attr == 'favorite' ) {
756 if ( $attr == 'archive' ) {
762 if (! is_null ( $url ) && $url- > isCorrect ()) {
763 $this- > action ( 'add' , $url , 0 , TRUE );
766 $last_id = $this- > store
-> getLastId ( $sequence );
767 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
770 $last_id = $this- > store
-> getLastId ( $sequence );
771 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
775 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
776 Tools
:: logm ( 'import from Readability completed' );
781 * import datas into your poche
782 * @param string $from name of the service to import : pocket, instapaper or readability
783 * @todo add the return value
786 public function import ( $from )
789 'pocket' => 'importFromPocket' ,
790 'readability' => 'importFromReadability' ,
791 'instapaper' => 'importFromInstapaper'
794 if (! isset ( $providers [ $from ])) {
795 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
799 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
800 $targetFile = constant ( $targetDefinition );
802 if (! defined ( $targetDefinition )) {
803 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
807 if (! file_exists ( $targetFile )) {
808 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
812 $this- > $providers [ $from ]( $targetFile );
816 * export poche entries in json
817 * @return json all poche entries
819 public function export ()
821 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
822 echo $this- > tpl
-> render ( 'export.twig' , array (
823 'export' => Tools
:: renderJson ( $entries ),
825 Tools
:: logm ( 'export view' );
829 * Checks online the latest version of poche and cache it
830 * @param string $which 'prod' or 'dev'
831 * @return string latest $which version
833 private function getPocheVersion ( $which = 'prod' )
835 $cache_file = CACHE
. '/' . $which ;
837 # checks if the cached version file exists
838 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
839 $version = file_get_contents ( $cache_file );
841 $version = file_get_contents ( 'http://static.inthepoche.com/versions/' . $which );
842 file_put_contents ( $cache_file , $version , LOCK_EX
);
847 public function generateToken ()
849 if ( ini_get ( 'open_basedir' ) === '' ) {
850 $token = substr ( base64_encode ( file_get_contents ( '/dev/urandom' , false , null , 0 , 20 )), 0 , 15 );
853 $token = substr ( base64_encode ( uniqid ( mt_rand (), true )), 0 , 20 );
856 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
857 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
858 $currentConfig [ 'token' ] = $token ;
859 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
862 public function generateFeeds ( $token , $user_id , $type = 'home' )
864 $allowed_types = array ( 'home' , 'fav' , 'archive' );
865 $config = $this- > store
-> getConfigUser ( $user_id );
867 if (! in_array ( $type , $allowed_types ) ||
868 $token != $config [ 'token' ]) {
869 die ( _ ( 'Uh, there is a problem while generating feeds.' ));
873 $feed = new FeedWriter ( ATOM
);
874 $feed- > setTitle ( 'poche - ' . $type . ' feed' );
875 $feed- > setLink ( Tools
:: getPocheUrl ());
876 $feed- > setChannelElement ( 'updated' , date ( DATE_ATOM
, time ()));
877 $feed- > setChannelElement ( 'author' , 'poche' );
879 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
880 if ( count ( $entries ) > 0 ) {
881 foreach ( $entries as $entry ) {
882 $newItem = $feed- > createNewItem ();
883 $newItem- > setTitle ( htmlentities ( $entry [ 'title' ]));
884 $newItem- > setLink ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
885 $newItem- > setDate ( time ());
886 $newItem- > setDescription ( $entry [ 'content' ]);
887 $feed- > addItem ( $newItem );
891 $feed- > genarateFeed ();