]>
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 );
233 private function install ()
235 Tools
:: logm ( 'poche still not installed' );
236 echo $this- > tpl
-> render ( 'install.twig' , array (
237 'token' => Session
:: getToken (),
238 'theme' => $this- > getTheme (),
239 'poche_url' => Tools
:: getPocheUrl ()
241 if ( isset ( $_GET [ 'install' ])) {
242 if (( $_POST [ 'password' ] == $_POST [ 'password_repeat' ])
243 && $_POST [ 'password' ] != "" && $_POST [ 'login' ] != "" ) {
244 # let's rock, install poche baby !
245 if ( $this- > store
-> install ( $_POST [ 'login' ], Tools
:: encodeString ( $_POST [ 'password' ] . $_POST [ 'login' ])))
248 Tools
:: logm ( 'poche is now installed' );
253 Tools
:: logm ( 'error during installation' );
260 public function getTheme () {
261 return $this- > currentTheme
;
265 * Provides theme information by parsing theme.ini file if present in the theme's root directory.
266 * In all cases, the following data will be returned:
267 * - name: theme's name, or key if the theme is unnamed,
268 * - current: boolean informing if the theme is the current user theme.
270 * @param string $theme Theme key (directory name)
271 * @return array|boolean Theme information, or false if the theme doesn't exist.
273 public function getThemeInfo ( $theme ) {
274 if (! is_dir ( THEME
. '/' . $theme )) {
278 $themeIniFile = THEME
. '/' . $theme . '/theme.ini' ;
279 $themeInfo = array ();
281 if ( is_file ( $themeIniFile ) && is_readable ( $themeIniFile )) {
282 $themeInfo = parse_ini_file ( $themeIniFile );
285 if ( $themeInfo === false ) {
286 $themeInfo = array ();
288 if (! isset ( $themeInfo [ 'name' ])) {
289 $themeInfo [ 'name' ] = $theme ;
291 $themeInfo [ 'current' ] = ( $theme === $this- > getTheme ());
296 public function getInstalledThemes () {
297 $handle = opendir ( THEME
);
300 while (( $theme = readdir ( $handle )) !== false ) {
301 # Themes are stored in a directory, so all directory names are themes
302 # @todo move theme installation data to database
303 if (! is_dir ( THEME
. '/' . $theme ) || in_array ( $theme , array ( '.' , '..' ))) {
307 $themes [ $theme ] = $this- > getThemeInfo ( $theme );
313 public function getLanguage () {
314 return $this- > currentLanguage
;
317 public function getInstalledLanguages () {
318 $handle = opendir ( LOCALE
);
319 $languages = array ();
321 while (( $language = readdir ( $handle )) !== false ) {
322 # Languages are stored in a directory, so all directory names are languages
323 # @todo move language installation data to database
324 if (! is_dir ( LOCALE
. '/' . $language ) || in_array ( $language , array ( '..' , '.' ))) {
330 if ( $language === $this- > getLanguage ()) {
334 $languages [] = array ( 'name' => $language , 'current' => $current );
340 public function getDefaultConfig ()
343 'pager' => PAGINATION
,
345 'theme' => DEFAULT_THEME
349 protected function getPageContent ( Url
$url )
351 $options = array ( 'http' => array ( 'user_agent' => 'poche' ));
352 if ( isset ( $_SERVER [ 'AUTH_TYPE' ]) && "basic" === strtolower ( $_SERVER [ 'AUTH_TYPE' ])) {
353 $options [ 'http' ][ 'header' ] = sprintf (
354 "Authorization: Basic %s" ,
356 sprintf ( '%s:%s' , $_SERVER [ 'PHP_AUTH_USER' ], $_SERVER [ 'PHP_AUTH_PW' ])
360 $context = stream_context_create ( $options );
361 $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 );
362 return json_decode ( $json , true );
366 * Call action (mark as fav, archive, delete, etc.)
368 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE )
373 $content = $this- > getPageContent ( $url );
374 $title = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ];
375 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
377 if ( $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ())) {
378 Tools
:: logm ( 'add link ' . $url- > getUrl ());
380 if ( STORAGE
== 'postgres' ) {
381 $sequence = 'entries_id_seq' ;
383 $last_id = $this- > store
-> getLastId ( $sequence );
384 if ( DOWNLOAD_PICTURES
) {
385 $content = filtre_picture ( $body , $url- > getUrl (), $last_id );
386 Tools
:: logm ( 'updating content article' );
387 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
390 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
395 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
396 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
401 if ( $autoclose == TRUE ) {
402 Tools
:: redirect ( '?view=home' );
404 Tools
:: redirect ( '?view=home&closewin=true' );
409 $msg = 'delete link #' . $id ;
410 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
411 if ( DOWNLOAD_PICTURES
) {
412 remove_directory ( ABS_PATH
. $id );
414 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
417 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
418 $msg = 'error : can \' t delete link #' . $id ;
421 Tools
:: redirect ( '?' );
424 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
425 Tools
:: logm ( 'mark as favorite link #' . $id );
430 case 'toggle_archive' :
431 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
432 Tools
:: logm ( 'archive link #' . $id );
438 $tags = explode ( ',' , $_POST [ 'value' ]);
439 $entry_id = $_POST [ 'entry_id' ];
440 foreach ( $tags as $key => $tag_value ) {
441 $value = trim ( $tag_value );
442 $tag = $this- > store
-> retrieveTagByValue ( $value );
446 $tag = $this- > store
-> createTag ( $value );
448 if ( STORAGE
== 'postgres' ) {
449 $sequence = 'tags_id_seq' ;
451 $tag_id = $this- > store
-> getLastId ( $sequence );
454 $tag_id = $tag [ 'id' ];
457 # we assign the tag to the article
458 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
463 $tag_id = $_GET [ 'tag_id' ];
464 $this- > store
-> removeTagForEntry ( $id , $tag_id );
472 function displayView ( $view , $id = 0 )
479 $dev = trim ( $this- > getPocheVersion ( 'dev' ));
480 $prod = trim ( $this- > getPocheVersion ( 'prod' ));
481 $compare_dev = version_compare ( POCHE
, $dev );
482 $compare_prod = version_compare ( POCHE
, $prod );
483 $themes = $this- > getInstalledThemes ();
484 $languages = $this- > getInstalledLanguages ();
485 $token = $this- > user
-> getConfigValue ( 'token' );
486 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
489 'languages' => $languages ,
492 'compare_dev' => $compare_dev ,
493 'compare_prod' => $compare_prod ,
495 'user_id' => $this- > user
-> getId (),
496 'http_auth' => $http_auth ,
498 Tools
:: logm ( 'config view' );
502 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
509 $entries = $this- > store
-> retrieveEntriesByTag ( $id );
510 $tag = $this- > store
-> retrieveTag ( $id );
513 'entries' => $entries ,
517 $token = $this- > user
-> getConfigValue ( 'token' );
518 $tags = $this- > store
-> retrieveAllTags ();
521 'user_id' => $this- > user
-> getId (),
526 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
527 if ( $entry != NULL ) {
528 Tools
:: logm ( 'view link #' . $id );
529 $content = $entry [ 'content' ];
530 if ( function_exists ( 'tidy_parse_string' )) {
531 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
532 $tidy- > cleanRepair ();
533 $content = $tidy- > value
;
537 $flattr = new FlattrItem ();
538 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
541 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
545 'content' => $content ,
551 Tools
:: logm ( 'error in view call : entry is null' );
554 default : # home, favorites and archive views
555 $entries = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId ());
562 if ( count ( $entries ) > 0 ) {
563 $this- > pagination
-> set_total ( count ( $entries ));
564 $page_links = $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . '&' );
565 $datas = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit ());
566 $tpl_vars [ 'entries' ] = $datas ;
567 $tpl_vars [ 'page_links' ] = $page_links ;
568 $tpl_vars [ 'nb_results' ] = count ( $entries );
570 Tools
:: logm ( 'display ' . $view . ' view' );
578 * update the password of the current user.
579 * if MODE_DEMO is TRUE, the password can't be updated.
580 * @todo add the return value
581 * @todo set the new password in function header like this updatePassword($newPassword)
584 public function updatePassword ()
587 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
588 Tools
:: logm ( 'in demo mode, you can \' t do this' );
589 Tools
:: redirect ( '?view=config' );
592 if ( isset ( $_POST [ 'password' ]) && isset ( $_POST [ 'password_repeat' ])) {
593 if ( $_POST [ 'password' ] == $_POST [ 'password_repeat' ] && $_POST [ 'password' ] != "" ) {
594 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
595 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $_POST [ 'password' ] . $this- > user
-> getUsername ()));
597 Tools
:: logm ( 'password updated' );
601 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
602 Tools
:: redirect ( '?view=config' );
608 public function updateTheme ()
611 if ( empty ( $_POST [ 'theme' ])) {
614 # we are not going to change it to the current theme...
615 if ( $_POST [ 'theme' ] == $this- > getTheme ()) {
616 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getTheme () . '" theme!' ));
617 Tools
:: redirect ( '?view=config' );
620 $themes = $this- > getInstalledThemes ();
621 $actualTheme = false ;
623 foreach ( array_keys ( $themes ) as $theme ) {
624 if ( $theme == $_POST [ 'theme' ]) {
630 if (! $actualTheme ) {
631 $this- > messages
-> add ( 'e' , _ ( 'that theme does not seem to be installed' ));
632 Tools
:: redirect ( '?view=config' );
635 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'theme' , $_POST [ 'theme' ]);
636 $this- > messages
-> add ( 's' , _ ( 'you have changed your theme preferences' ));
638 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
639 $currentConfig [ 'theme' ] = $_POST [ 'theme' ];
641 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
643 Tools
:: redirect ( '?view=config' );
646 public function updateLanguage ()
649 if ( empty ( $_POST [ 'language' ])) {
652 # we are not going to change it to the current language...
653 if ( $_POST [ 'language' ] == $this- > getLanguage ()) {
654 $this- > messages
-> add ( 'w' , _ ( 'still using the "' . $this- > getLanguage () . '" language!' ));
655 Tools
:: redirect ( '?view=config' );
658 $languages = $this- > getInstalledLanguages ();
659 $actualLanguage = false ;
661 foreach ( $languages as $language ) {
662 if ( $language [ 'name' ] == $_POST [ 'language' ]) {
663 $actualLanguage = true ;
668 if (! $actualLanguage ) {
669 $this- > messages
-> add ( 'e' , _ ( 'that language does not seem to be installed' ));
670 Tools
:: redirect ( '?view=config' );
673 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'language' , $_POST [ 'language' ]);
674 $this- > messages
-> add ( 's' , _ ( 'you have changed your language preferences' ));
676 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
677 $currentConfig [ 'language' ] = $_POST [ 'language' ];
679 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
681 Tools
:: redirect ( '?view=config' );
685 * get credentials from differents sources
686 * it redirects the user to the $referer link
689 private function credentials () {
690 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
691 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
693 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
694 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
696 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
697 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
700 return array ( false , false , false );
704 * checks if login & password are correct and save the user in session.
705 * it redirects the user to the $referer link
706 * @param string $referer the url to redirect after login
707 * @todo add the return value
710 public function login ( $referer )
712 list ( $login , $password , $isauthenticated )= $this- > credentials ();
713 if ( $login === false || $password === false ) {
714 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
715 Tools
:: logm ( 'login failed' );
718 if (! empty ( $login ) && ! empty ( $password )) {
719 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
720 if ( $user != array ()) {
721 # Save login into Session
722 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
723 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
724 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
725 $this- > messages
-> add ( 's' , _ ( 'welcome to your poche' ));
726 Tools
:: logm ( 'login successful' );
727 Tools
:: redirect ( $referer );
729 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
730 Tools
:: logm ( 'login failed' );
736 * log out the poche user. It cleans the session.
737 * @todo add the return value
740 public function logout ()
742 $this- > user
= array ();
744 Tools
:: logm ( 'logout' );
749 * import from Instapaper. poche needs a ./instapaper-export.html file
750 * @todo add the return value
751 * @param string $targetFile the file used for importing
754 private function importFromInstapaper ( $targetFile )
756 # TODO gestion des articles favs
757 $html = new simple_html_dom ();
758 $html- > load_file ( $targetFile );
759 Tools
:: logm ( 'starting import from instapaper' );
763 foreach ( $html- > find ( 'ol' ) as $ul )
765 foreach ( $ul- > find ( 'li' ) as $li )
768 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
769 $this- > action ( 'add' , $url , 0 , TRUE );
772 if ( STORAGE
== 'postgres' ) {
773 $sequence = 'entries_id_seq' ;
775 $last_id = $this- > store
-> getLastId ( $sequence );
776 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
780 # the second <ol> is for read links
783 $this- > messages
-> add ( 's' , _ ( 'import from instapaper completed' ));
784 Tools
:: logm ( 'import from instapaper completed' );
789 * import from Pocket. poche needs a ./ril_export.html file
790 * @todo add the return value
791 * @param string $targetFile the file used for importing
794 private function importFromPocket ( $targetFile )
796 # TODO gestion des articles favs
797 $html = new simple_html_dom ();
798 $html- > load_file ( $targetFile );
799 Tools
:: logm ( 'starting import from pocket' );
803 foreach ( $html- > find ( 'ul' ) as $ul )
805 foreach ( $ul- > find ( 'li' ) as $li )
808 $url = new Url ( base64_encode ( $a [ 0 ]-> href
));
809 $this- > action ( 'add' , $url , 0 , TRUE );
812 if ( STORAGE
== 'postgres' ) {
813 $sequence = 'entries_id_seq' ;
815 $last_id = $this- > store
-> getLastId ( $sequence );
816 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
820 # the second <ul> is for read links
823 $this- > messages
-> add ( 's' , _ ( 'import from pocket completed' ));
824 Tools
:: logm ( 'import from pocket completed' );
829 * import from Readability. poche needs a ./readability file
830 * @todo add the return value
831 * @param string $targetFile the file used for importing
834 private function importFromReadability ( $targetFile )
836 # TODO gestion des articles lus / favs
837 $str_data = file_get_contents ( $targetFile );
838 $data = json_decode ( $str_data , true );
839 Tools
:: logm ( 'starting import from Readability' );
841 foreach ( $data as $key => $value ) {
845 foreach ( $value as $item ) {
846 foreach ( $item as $attr => $value ) {
847 if ( $attr == 'article__url' ) {
848 $url = new Url ( base64_encode ( $value ));
851 if ( STORAGE
== 'postgres' ) {
852 $sequence = 'entries_id_seq' ;
854 if ( $value == 'true' ) {
855 if ( $attr == 'favorite' ) {
858 if ( $attr == 'archive' ) {
865 if (! is_null ( $url ) && $url- > isCorrect ()) {
866 $this- > action ( 'add' , $url , 0 , TRUE );
869 $last_id = $this- > store
-> getLastId ( $sequence );
870 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
873 $last_id = $this- > store
-> getLastId ( $sequence );
874 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
879 $this- > messages
-> add ( 's' , _ ( 'import from Readability completed. ' . $count . ' new links.' ));
880 Tools
:: logm ( 'import from Readability completed' );
885 * import from Poche exported file
886 * @param string $targetFile the file used for importing
889 private function importFromPoche ( $targetFile )
891 $str_data = file_get_contents ( $targetFile );
892 $data = json_decode ( $str_data , true );
893 Tools
:: logm ( 'starting import from Poche' );
897 if ( STORAGE
== 'postgres' ) {
898 $sequence = 'entries_id_seq' ;
902 foreach ( $data as $value ) {
904 $url = new Url ( base64_encode ( $value [ 'url' ]));
905 $favorite = ( $value [ 'is_fav' ] == - 1 );
906 $archive = ( $value [ 'is_read' ] == - 1 );
909 if (! is_null ( $url ) && $url- > isCorrect ()) {
911 $this- > action ( 'add' , $url , 0 , TRUE );
915 $last_id = $this- > store
-> getLastId ( $sequence );
916 $this- > action ( 'toggle_fav' , $url , $last_id , TRUE );
919 $last_id = $this- > store
-> getLastId ( $sequence );
920 $this- > action ( 'toggle_archive' , $url , $last_id , TRUE );
925 $this- > messages
-> add ( 's' , _ ( 'import from Poche completed. ' . $count . ' new links.' ));
926 Tools
:: logm ( 'import from Poche completed' );
931 * import datas into your poche
932 * @param string $from name of the service to import : pocket, instapaper or readability
933 * @todo add the return value
936 public function import ( $from )
939 'pocket' => 'importFromPocket' ,
940 'readability' => 'importFromReadability' ,
941 'instapaper' => 'importFromInstapaper' ,
942 'poche' => 'importFromPoche' ,
945 if (! isset ( $providers [ $from ])) {
946 $this- > messages
-> add ( 'e' , _ ( 'Unknown import provider.' ));
950 $targetDefinition = 'IMPORT_' . strtoupper ( $from ) . '_FILE' ;
951 $targetFile = constant ( $targetDefinition );
953 if (! defined ( $targetDefinition )) {
954 $this- > messages
-> add ( 'e' , _ ( 'Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".' ));
958 if (! file_exists ( $targetFile )) {
959 $this- > messages
-> add ( 'e' , _ ( 'Could not find required "' . $targetFile . '" import file.' ));
963 $this- > $providers [ $from ]( $targetFile );
967 * export poche entries in json
968 * @return json all poche entries
970 public function export ()
972 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
973 echo $this- > tpl
-> render ( 'export.twig' , array (
974 'export' => Tools
:: renderJson ( $entries ),
976 Tools
:: logm ( 'export view' );
980 * Checks online the latest version of poche and cache it
981 * @param string $which 'prod' or 'dev'
982 * @return string latest $which version
984 private function getPocheVersion ( $which = 'prod' )
986 $cache_file = CACHE
. '/' . $which ;
988 # checks if the cached version file exists
989 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
990 $version = file_get_contents ( $cache_file );
992 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
993 file_put_contents ( $cache_file , $version , LOCK_EX
);
998 public function generateToken ()
1000 if ( ini_get ( 'open_basedir' ) === '' ) {
1001 $token = substr ( base64_encode ( file_get_contents ( '/dev/urandom' , false , null , 0 , 20 )), 0 , 15 );
1004 $token = substr ( base64_encode ( uniqid ( mt_rand (), true )), 0 , 20 );
1007 $token = str_replace ( '+' , '' , $token );
1008 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
1009 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
1010 $currentConfig [ 'token' ] = $token ;
1011 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
1014 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' )
1016 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
1017 $config = $this- > store
-> getConfigUser ( $user_id );
1019 if (! in_array ( $type , $allowed_types ) ||
1020 $token != $config [ 'token' ]) {
1021 die ( _ ( 'Uh, there is a problem while generating feeds.' ));
1025 $feed = new FeedWriter ( RSS2
);
1026 $feed- > setTitle ( 'poche - ' . $type . ' feed' );
1027 $feed- > setLink ( Tools
:: getPocheUrl ());
1028 $feed- > setChannelElement ( 'updated' , date ( DATE_RSS
, time ()));
1029 $feed- > setChannelElement ( 'author' , 'poche' );
1031 if ( $type == 'tag' ) {
1032 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id );
1035 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
1038 if ( count ( $entries ) > 0 ) {
1039 foreach ( $entries as $entry ) {
1040 $newItem = $feed- > createNewItem ();
1041 $newItem- > setTitle ( $entry [ 'title' ]);
1042 $newItem- > setLink ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
1043 $newItem- > setDate ( time ());
1044 $newItem- > setDescription ( $entry [ 'content' ]);
1045 $feed- > addItem ( $newItem );
1049 $feed- > genarateFeed ();