]>
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 public function __construct ()
28 if ( $this- > configFileIsAvailable ()) {
32 if ( $this- > themeIsInstalled ()) {
36 if ( $this- > systemIsInstalled ()) {
37 $this- > store
= new Database ();
38 $this- > messages
= new Messages ();
40 if (! $this- > store
-> isInstalled ()) {
43 $this- > store
-> checkTags ();
47 private function init ()
50 Session
:: $sessionName = 'poche' ;
53 if ( isset ( $_SESSION [ 'poche_user' ]) && $_SESSION [ 'poche_user' ] != array ()) {
54 $this- > user
= $_SESSION [ 'poche_user' ];
56 # fake user, just for install & login screens
57 $this- > user
= new User ();
58 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
62 $language = $this- > user
-> getConfigValue ( 'language' );
63 putenv ( 'LC_ALL=' . $language );
64 setlocale ( LC_ALL
, $language );
65 bindtextdomain ( $language , LOCALE
);
66 textdomain ( $language );
69 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
72 $themeDirectory = $this- > user
-> getConfigValue ( 'theme' );
74 if ( $themeDirectory === false ) {
75 $themeDirectory = DEFAULT_THEME
;
78 $this- > currentTheme
= $themeDirectory ;
81 $languageDirectory = $this- > user
-> getConfigValue ( 'language' );
83 if ( $languageDirectory === false ) {
84 $languageDirectory = DEFAULT_THEME
;
87 $this- > currentLanguage
= $languageDirectory ;
90 public function configFileIsAvailable () {
91 if (! self
:: $configFileAvailable ) {
92 $this- > notInstalledMessage
[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.' ;
100 public function themeIsInstalled () {
102 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
103 if (! self
:: $canRenderTemplates ) {
104 $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>' ;
108 if (! is_writable ( CACHE
)) {
109 $this- > notInstalledMessage
[] = 'You don \' t have write access on cache directory.' ;
111 self
:: $canRenderTemplates = false ;
116 # Check if the selected theme and its requirements are present
117 $theme = $this- > getTheme ();
119 if ( $theme != '' && ! is_dir ( THEME
. '/' . $theme )) {
120 $this- > notInstalledMessage
[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME
. '/' . $theme . ')' ;
122 self
:: $canRenderTemplates = false ;
127 $themeInfo = $this- > getThemeInfo ( $theme );
128 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
129 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
130 if (! is_dir ( THEME
. '/' . $requiredTheme )) {
131 $this- > notInstalledMessage
[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')' ;
133 self
:: $canRenderTemplates = false ;
149 * all checks before installation.
150 * @todo move HTML to template
153 public function systemIsInstalled ()
157 $configSalt = defined ( 'SALT' ) ? constant ( 'SALT' ) : '' ;
159 if ( empty ( $configSalt )) {
160 $this- > notInstalledMessage
[] = 'You have not yet filled in the SALT value in the config.inc.php file.' ;
163 if ( STORAGE
== 'sqlite' && ! file_exists ( STORAGE_SQLITE
)) {
164 Tools
:: logm ( 'sqlite file doesn \' t exist' );
165 $this- > notInstalledMessage
[] = 'sqlite file doesn \' t exist, you can find it in install folder. Copy it in /db folder.' ;
168 if ( is_dir ( ROOT
. '/install' ) && ! DEBUG_POCHE
) {
169 $this- > notInstalledMessage
[] = 'you have to delete the /install folder before using poche.' ;
172 if ( STORAGE
== 'sqlite' && ! is_writable ( STORAGE_SQLITE
)) {
173 Tools
:: logm ( 'you don \' t have write access on sqlite file' );
174 $this- > notInstalledMessage
[] = 'You don \' t have write access on sqlite file.' ;
185 public function getNotInstalledMessage () {
186 return $this- > notInstalledMessage
;
189 private function initTpl ()
191 $loaderChain = new Twig_Loader_Chain ();
192 $theme = $this- > getTheme ();
194 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
196 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $theme ));
197 } catch ( Twig_Error_Loader
$e ) {
198 # @todo isInstalled() should catch this, inject Twig later
199 die ( 'The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME
. '/' . $theme . ' is missing)' );
202 # add all required themes to the loader chain
203 $themeInfo = $this- > getThemeInfo ( $theme );
204 if ( isset ( $themeInfo [ 'requirements' ]) && is_array ( $themeInfo [ 'requirements' ])) {
205 foreach ( $themeInfo [ 'requirements' ] as $requiredTheme ) {
207 $loaderChain- > addLoader ( new Twig_Loader_Filesystem ( THEME
. '/' . $requiredTheme ));
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 (' . $theme . ')' );
216 $twigParams = array ();
218 $twigParams = array ( 'cache' => CACHE
);
221 $this- > tpl
= new Twig_Environment ( $loaderChain , $twigParams );
222 $this- > tpl
-> addExtension ( new Twig_Extensions_Extension_I18n ());
224 # filter to display domain name of an url
225 $filter = new Twig_SimpleFilter ( 'getDomain' , 'Tools::getDomain' );
226 $this- > tpl
-> addFilter ( $filter );
228 # filter for reading time
229 $filter = new Twig_SimpleFilter ( 'getReadingTime' , 'Tools::getReadingTime' );
230 $this- > tpl
-> addFilter ( $filter );
232 # filter for simple filenames in config view
233 $filter = new Twig_SimpleFilter ( 'getPrettyFilename' , function ( $string ) { return str_replace ( ROOT
, '' , $
string ); });
234 $this- > tpl
-> addFilter ( $filter );
237 private function install ()
239 Tools
:: logm ( 'poche still not installed' );
240 echo $this- > tpl
-> render ( 'install.twig' , array (
241 'token' => Session
:: getToken (),
242 'theme' => $this- > getTheme (),
243 'poche_url' => Tools
:: getPocheUrl ()
245 if ( isset ( $_GET [ 'install' ])) {
246 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
247 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
248 # let's rock, install poche baby !
249 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
252 Tools
:: logm ( 'poche is now installed' );
257 Tools
:: logm ( 'error during installation' );
264 public function getTheme () {
265 return $this- > currentTheme
;
269 * Provides theme information by parsing theme.ini file if present in the theme's root directory.
270 * In all cases, the following data will be returned:
271 * - name: theme's name, or key if the theme is unnamed,
272 * - current: boolean informing if the theme is the current user theme.
274 * @param string $theme Theme key (directory name)
275 * @return array|boolean Theme information, or false if the theme doesn't exist.
277 public function getThemeInfo ( $theme ) {
278 if (! is_dir ( THEME
. '/' . $theme )) {
282 $themeIniFile = THEME
. '/' . $theme . '/theme.ini' ;
283 $themeInfo = array ();
285 if ( is_file ( $themeIniFile ) && is_readable ( $themeIniFile )) {
286 $themeInfo = parse_ini_file ( $themeIniFile );
289 if ( $themeInfo === false ) {
290 $themeInfo = array ();
292 if (! isset ( $themeInfo [ 'name' ])) {
293 $themeInfo [ 'name' ] = $theme ;
295 $themeInfo [ 'current' ] = ( $theme === $this- > getTheme ());
300 public function getInstalledThemes () {
301 $handle = opendir ( THEME
);
304 while (( $theme = readdir ( $handle )) !== false ) {
305 # Themes are stored in a directory, so all directory names are themes
306 # @todo move theme installation data to database
307 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '.' , '..' ))) {
311 $themes [ $theme ] = $this- > getThemeInfo ( $theme );
317 public function getLanguage () {
318 return $this- > currentLanguage
;
321 public function getInstalledLanguages () {
322 $handle = opendir ( LOCALE
);
323 $languages = array ();
325 while (( $language = readdir ( $handle )) !== false ) {
326 # Languages are stored in a directory, so all directory names are languages
327 # @todo move language installation data to database
328 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
334 if ( $language === $this- > getLanguage ()) {
338 $languages [] = array ( 'name' => $language , 'current' => $current );
344 public function getDefaultConfig ()
347 'pager' => PAGINATION
,
349 'theme' => DEFAULT_THEME
353 protected function getPageContent ( Url
$url )
355 $options = array ( 'http' => array ( 'user_agent' => 'poche' ));
356 if ( isset ( $_SERVER [ 'AUTH_TYPE' ]) && "basic" === strtolower ( $_SERVER [ 'AUTH_TYPE' ])) {
357 $options [ 'http' ][ 'header' ] = sprintf (
358 "Authorization: Basic %s" ,
360 sprintf ( '%s:%s' , $_SERVER [ 'PHP_AUTH_USER' ], $_SERVER [ 'PHP_AUTH_PW' ])
364 $context = stream_context_create ( $options );
365 $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 );
366 return json_decode ( $json , true );
370 * Call action (mark as fav, archive, delete, etc.)
372 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE )
377 $content = $this- > getPageContent ( $url );
378 $title = ( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' );
379 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
381 if ( $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ())) {
382 Tools
:: logm ( 'add link ' . $url- > getUrl ());
384 if ( STORAGE
== 'postgres' ) {
385 $sequence = 'entries_id_seq' ;
387 $last_id = $this- > store
-> getLastId ( $sequence );
388 if ( DOWNLOAD_PICTURES
) {
389 $content = filtre_picture ( $body , $url- > getUrl (), $last_id );
390 Tools
:: logm ( 'updating content article' );
391 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
394 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
399 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
400 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
405 if ( $autoclose == TRUE ) {
406 Tools
:: redirect ( '?view=home' );
408 Tools
:: redirect ( '?view=home&closewin=true' );
413 $msg = 'delete link #' . $id ;
414 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
415 if ( DOWNLOAD_PICTURES
) {
416 remove_directory ( ABS_PATH
. $id );
418 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
421 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
422 $msg = 'error : can \' t delete link #' . $id ;
425 Tools
:: redirect ( '?' );
428 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
429 Tools
:: logm ( 'mark as favorite link #' . $id );
434 case 'toggle_archive' :
435 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
436 Tools
:: logm ( 'archive link #' . $id );
442 $tags = explode ( ',' , $_POST [ 'value' ]);
443 $entry_id = $_POST [ 'entry_id' ];
444 foreach ( $tags as $key => $tag_value ) {
445 $value = trim ( $tag_value );
446 $tag = $this- > store
-> retrieveTagByValue ( $value );
450 $tag = $this- > store
-> createTag ( $value );
452 if ( STORAGE
== 'postgres' ) {
453 $sequence = 'tags_id_seq' ;
455 $tag_id = $this- > store
-> getLastId ( $sequence );
458 $tag_id = $tag [ 'id' ];
461 # we assign the tag to the article
462 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
467 $tag_id = $_GET [ 'tag_id' ];
468 $this- > store
-> removeTagForEntry ( $id , $tag_id );
476 function displayView ( $view , $id = 0 )
483 $dev = $this- > getPocheVersion ( 'dev' );
484 $prod = $this- > getPocheVersion ( 'prod' );
485 $compare_dev = version_compare ( POCHE
, $dev );
486 $compare_prod = version_compare ( POCHE
, $prod );
487 $themes = $this- > getInstalledThemes ();
488 $languages = $this- > getInstalledLanguages ();
489 $token = $this- > user
-> getConfigValue ( 'token' );
490 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
493 'languages' => $languages ,
496 'compare_dev' => $compare_dev ,
497 'compare_prod' => $compare_prod ,
499 'user_id' => $this- > user
-> getId (),
500 'http_auth' => $http_auth ,
502 Tools
:: logm ( 'config view' );
506 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
513 $entries = $this- > store
-> retrieveEntriesByTag ( $id );
514 $tag = $this- > store
-> retrieveTag ( $id );
517 'entries' => $entries ,
521 $token = $this- > user
-> getConfigValue ( 'token' );
522 $tags = $this- > store
-> retrieveAllTags ();
525 'user_id' => $this- > user
-> getId (),
530 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
531 if ( $entry != NULL ) {
532 Tools
:: logm ( 'view link #' . $id );
533 $content = $entry [ 'content' ];
534 if ( function_exists ( 'tidy_parse_string' )) {
535 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
536 $tidy- > cleanRepair ();
537 $content = $tidy- > value
;
541 $flattr = new FlattrItem ();
542 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
545 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
549 'content' => $content ,
555 Tools
:: logm ( 'error in view call : entry is null' );
558 default : # home, favorites and archive views
559 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
566 if ( count ( $entries ) > 0 ) {
567 $this- > pagination
-> set_total ( count ( $entries ));
568 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
569 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
570 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
571 $tpl_vars [ 'entries' ] = $datas ;
572 $tpl_vars [ 'page_links' ] = $page_links ;
573 $tpl_vars [ 'nb_results' ] = count ( $entries );
575 Tools
:: logm ( 'display ' . $view . ' view' );
583 * update the password of the current user.
584 * if MODE_DEMO is TRUE, the password can't be updated.
585 * @todo add the return value
586 * @todo set the new password in function header like this updatePassword($newPassword)
589 public function updatePassword ()
592 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
593 Tools
:: logm ( 'in demo mode, you can \' t do this' );
594 Tools
:: redirect ( '?view=config' );
597 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
598 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
599 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
600 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
602 Tools
:: logm ( 'password updated' );
606 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
607 Tools
:: redirect ( '?view=config' );
613 public function updateTheme ()
616 if ( empty ( $_POST [ 'theme' ])) {
619 # we are not going to change it to the current theme...
620 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
621 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
622 Tools
:: redirect ( '?view=config' );
625 $themes = $this- > getInstalledThemes ();
626 $actualTheme = false ;
628 foreach ( array_keys ( $themes ) as $theme ) {
629 if ( $theme == $_POST [ 'theme' ]) {
635 if (! $actualTheme ) {
636 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
637 Tools
:: redirect ( '?view=config' );
640 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
641 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
643 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
644 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
646 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
648 Tools
:: redirect ( '?view=config' );
651 public function updateLanguage ()
654 if ( empty ( $_POST [ 'language' ])) {
657 # we are not going to change it to the current language...
658 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
659 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
660 Tools
:: redirect ( '?view=config' );
663 $languages = $this- > getInstalledLanguages ();
664 $actualLanguage = false ;
666 foreach ( $languages as $language ) {
667 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
668 $actualLanguage = true ;
673 if (! $actualLanguage ) {
674 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
675 Tools
:: redirect ( '?view=config' );
678 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
679 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
681 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
682 $currentConfig [ 'language' ] = $_POST [ 'language' ];
684 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
686 Tools
:: redirect ( '?view=config' );
690 * get credentials from differents sources
691 * it redirects the user to the $referer link
694 private function credentials () {
695 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
696 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
698 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
699 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
701 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
702 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
705 return array ( false , false , false );
709 * checks if login & password are correct and save the user in session.
710 * it redirects the user to the $referer link
711 * @param string $referer the url to redirect after login
712 * @todo add the return value
715 public function login ( $referer )
717 list ( $login , $password , $isauthenticated )= $this- > credentials ();
718 if ( $login === false || $password === false ) {
719 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
720 Tools
:: logm ( 'login failed' );
723 if (! empty ( $login ) && ! empty ( $password )) {
724 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
725 if ( $user != array ()) {
726 # Save login into Session
727 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
728 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
729 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
730 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
731 Tools
:: logm ( 'login successful' );
732 Tools
:: redirect ( $referer );
734 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
735 Tools
:: logm ( 'login failed' );
741 * log out the poche user. It cleans the session.
742 * @todo add the return value
745 public function logout ()
747 $this- > user
= array ();
749 $this- > messages
-> add ( 's' , _ ( 'see you soon!' ));
750 Tools
:: logm ( 'logout' );
755 * import from Instapaper. poche needs a ./instapaper-export.html file
756 * @todo add the return value
757 * @param string $targetFile the file used for importing
760 private function importFromInstapaper ( $targetFile )
762 # TODO gestion des articles favs
763 $html = new simple_html_dom ();
764 $html- > load_file ( $targetFile );
765 Tools
:: logm ( 'starting import from instapaper' );
769 foreach ( $html- > find ( 'ol' ) as $ul )
771 foreach ( $ul- > find ( 'li' ) as $li )
774 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
775 $this- > action ( 'add' , $url , 0 , TRUE );
778 if ( STORAGE
== 'postgres' ) {
779 $sequence = 'entries_id_seq' ;
781 $last_id = $this- > store
-> getLastId ( $sequence );
782 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
786 # the second <ol> is for read links
789 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
790 Tools
:: logm ( 'import from instapaper completed' );
795 * import from Pocket. poche needs a ./ril_export.html file
796 * @todo add the return value
797 * @param string $targetFile the file used for importing
800 private function importFromPocket ( $targetFile )
802 # TODO gestion des articles favs
803 $html = new simple_html_dom ();
804 $html- > load_file ( $targetFile );
805 Tools
:: logm ( 'starting import from pocket' );
809 foreach ( $html- > find ( 'ul' ) as $ul )
811 foreach ( $ul- > find ( 'li' ) as $li )
814 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
815 $this- > action ( 'add' , $url , 0 , TRUE );
818 if ( STORAGE
== 'postgres' ) {
819 $sequence = 'entries_id_seq' ;
821 $last_id = $this- > store
-> getLastId ( $sequence );
822 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
826 # the second <ul> is for read links
829 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
830 Tools
:: logm ( 'import from pocket completed' );
835 * import from Readability. poche needs a ./readability file
836 * @todo add the return value
837 * @param string $targetFile the file used for importing
840 private function importFromReadability ( $targetFile )
842 # TODO gestion des articles lus / favs
843 $str_data = file_get_contents ( $targetFile );
844 $data = json_decode ( $str_data , true );
845 Tools
:: logm ( 'starting import from Readability' );
847 foreach ( $data as $key => $value ) {
851 foreach ( $value as $item ) {
852 foreach ( $item as $attr => $value ) {
853 if ( $attr == 'article__url' ) {
854 $url = new Url ( base64_encode ( $value ));
857 if ( STORAGE
== 'postgres' ) {
858 $sequence = 'entries_id_seq' ;
860 if ( $value == 'true' ) {
861 if ( $attr == 'favorite' ) {
864 if ( $attr == 'archive' ) {
871 if (! is_null ( $url ) && $url- > isCorrect ()) {
872 $this- > action ( 'add' , $url , 0 , TRUE );
875 $last_id = $this- > store
-> getLastId ( $sequence );
876 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
879 $last_id = $this- > store
-> getLastId ( $sequence );
880 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
885 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
886 Tools
:: logm ( 'import from Readability completed' );
891 * import from Poche exported file
892 * @param string $targetFile the file used for importing
895 private function importFromPoche ( $targetFile )
897 $str_data = file_get_contents ( $targetFile );
898 $data = json_decode ( $str_data , true );
899 Tools
:: logm ( 'starting import from Poche' );
903 if ( STORAGE
== 'postgres' ) {
904 $sequence = 'entries_id_seq' ;
908 foreach ( $data as $value ) {
910 $url = new Url ( base64_encode ( $value [ 'url' ]));
911 $favorite = ( $value [ 'is_fav' ] == - 1 );
912 $archive = ( $value [ 'is_read' ] == - 1 );
915 if (! is_null ( $url ) && $url- > isCorrect ()) {
917 $this- > action ( 'add' , $url , 0 , TRUE );
921 $last_id = $this- > store
-> getLastId ( $sequence );
922 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
925 $last_id = $this- > store
-> getLastId ( $sequence );
926 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
931 $this- > messages
-> add ( 's' , _ ( 'import from Poche completed. ' . $count . ' new links.' ));
932 Tools
:: logm ( 'import from Poche completed' );
937 * import datas into your poche
938 * @param string $from name of the service to import : pocket, instapaper or readability
939 * @todo add the return value
942 public function import ( $from )
945 'pocket' => 'importFromPocket' ,
946 'readability' => 'importFromReadability' ,
947 'instapaper' => 'importFromInstapaper' ,
948 'poche' => 'importFromPoche' ,
951 if (! isset ( $providers [ $from ])) {
952 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
956 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
957 $targetFile = constant ( $targetDefinition );
959 if (! defined ( $targetDefinition )) {
960 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
964 if (! file_exists ( $targetFile )) {
965 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
969 $this- > $providers [ $from ]( $targetFile );
973 * export poche entries in json
974 * @return json all poche entries
976 public function export ()
978 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
979 echo $this- > tpl
-> render ( 'export.twig' , array (
980 'export' => Tools
:: renderJson ( $entries ),
982 Tools
:: logm ( 'export view' );
986 * Checks online the latest version of poche and cache it
987 * @param string $which 'prod' or 'dev'
988 * @return string latest $which version
990 private function getPocheVersion ( $which = 'prod' )
992 $cache_file = CACHE
. '/' . $which ;
994 # checks if the cached version file exists
995 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
996 $version = file_get_contents ( $cache_file );
998 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
999 file_put_contents ( $cache_file , $version , LOCK_EX
);
1004 public function generateToken ()
1006 if ( ini_get ( 'open_basedir' ) === '' ) {
1007 $token = substr ( base64_encode ( file_get_contents ( '/dev/urandom' , false , null , 0 , 20 )), 0 , 15 );
1010 $token = substr ( base64_encode ( uniqid ( mt_rand (), true )), 0 , 20 );
1013 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
1014 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
1015 $currentConfig [ 'token' ] = $token ;
1016 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
1019 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' )
1021 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
1022 $config = $this- > store
-> getConfigUser ( $user_id );
1024 if (! in_array ( $type , $allowed_types ) ||
1025 $token != $config [ 'token' ]) {
1026 die ( _ ( 'Uh, there is a problem while generating feeds.' ));
1030 $feed = new FeedWriter ( RSS2
);
1031 $feed- > setTitle ( 'poche - ' . $type . ' feed' );
1032 $feed- > setLink ( Tools
:: getPocheUrl ());
1033 $feed- > setChannelElement ( 'updated' , date ( DATE_RSS
, time ()));
1034 $feed- > setChannelElement ( 'author' , 'poche' );
1036 if ( $type == 'tag' ) {
1037 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id );
1040 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
1043 if ( count ( $entries ) > 0 ) {
1044 foreach ( $entries as $entry ) {
1045 $newItem = $feed- > createNewItem ();
1046 $newItem- > setTitle ( $entry [ 'title' ]);
1047 $newItem- > setLink ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
1048 $newItem- > setDate ( time ());
1049 $newItem- > setDescription ( $entry [ 'content' ]);
1050 $feed- > addItem ( $newItem );
1054 $feed- > genarateFeed ();