]>
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://opensource.org/licenses/MIT see COPYING file
42 public function __construct ()
47 private function init ()
51 $pocheUser = Session
:: getParam ( 'poche_user' );
53 if ( $pocheUser && $pocheUser != array ()) {
54 $this- > user
= $pocheUser ;
56 // fake user, just for install & login screens
57 $this- > user
= new User ();
58 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
61 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
62 $this- > language
= new Language ( $this );
63 $this- > tpl
= new Template ( $this );
64 $this- > store
= new Database ();
65 $this- > messages
= new Messages ();
66 $this- > routing
= new Routing ( $this );
71 $this- > routing
-> run ();
77 public function createNewUser ( $username , $password , $email = "" )
79 if (! empty ( $username ) && ! empty ( $password )){
80 $newUsername = filter_var ( $username , FILTER_SANITIZE_STRING
);
81 $email = filter_var ( $email , FILTER_SANITIZE_STRING
);
82 if (! $this- > store
-> userExists ( $newUsername )){
83 if ( $this- > store
-> install ( $newUsername , Tools
:: encodeString ( $password . $newUsername ), $email )) {
84 Tools
:: logm ( 'The new user ' . $newUsername . ' has been installed' );
85 $this- > messages
-> add ( 's' , sprintf ( _ ( 'The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>' ), $newUsername ));
89 Tools
:: logm ( 'error during adding new user' );
94 $this- > messages
-> add ( 'e' , sprintf ( _ ( 'Error : An user with the name %s already exists !' ), $newUsername ));
95 Tools
:: logm ( 'An user with the name ' . $newUsername . ' already exists !' );
102 * Delete an existing user
104 public function deleteUser ( $password )
106 if ( $this- > store
-> listUsers () > 1 ) {
107 if ( Tools
:: encodeString ( $password . $this- > user
-> getUsername ()) == $this- > store
-> getUserPassword ( $this- > user
-> getId ())) {
108 $username = $this- > user
-> getUsername ();
109 $this- > store
-> deleteUserConfig ( $this- > user
-> getId ());
110 Tools
:: logm ( 'The configuration for user ' . $username . ' has been deleted !' );
111 $this- > store
-> deleteTagsEntriesAndEntries ( $this- > user
-> getId ());
112 Tools
:: logm ( 'The entries for user ' . $username . ' has been deleted !' );
113 $this- > store
-> deleteUser ( $this- > user
-> getId ());
114 Tools
:: logm ( 'User ' . $username . ' has been completely deleted !' );
116 Tools
:: logm ( 'logout' );
118 $this- > messages
-> add ( 's' , sprintf ( _ ( 'User %s has been successfully deleted !' ), $username ));
121 Tools
:: logm ( 'Bad password !' );
122 $this- > messages
-> add ( 'e' , _ ( 'Error : The password is wrong !' ));
126 Tools
:: logm ( 'Only user !' );
127 $this- > messages
-> add ( 'e' , _ ( 'Error : You are the only user, you cannot delete your account !' ));
131 public function getDefaultConfig ()
134 'pager' => PAGINATION
,
136 'theme' => DEFAULT_THEME
141 * Call action (mark as fav, archive, delete, etc.)
143 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE , $tags = null )
148 $content = Tools
:: getPageContent ( $url );
149 $title = ( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' );
150 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
152 // clean content from prevent xss attack
153 $purifier = $this- > _getPurifier ();
154 $title = $purifier- > purify ( $title );
155 $body = $purifier- > purify ( $body );
157 //search for possible duplicate
159 $duplicate = $this- > store
-> retrieveOneByURL ( $url- > getUrl (), $this- > user
-> getId ());
161 $last_id = $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ());
163 Tools
:: logm ( 'add link ' . $url- > getUrl ());
164 if ( DOWNLOAD_PICTURES
) {
165 $content = Picture
:: filterPicture ( $body , $url- > getUrl (), $last_id );
166 Tools
:: logm ( 'updating content article' );
167 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
170 if ( $duplicate != NULL ) {
171 // duplicate exists, so, older entry needs to be deleted (as new entry should go to the top of list), BUT favorite mark and tags should be preserved
172 Tools
:: logm ( 'link ' . $url- > getUrl () . ' is a duplicate' );
173 // 1) - preserve tags and favorite, then drop old entry
174 $this- > store
-> reassignTags ( $duplicate [ 'id' ], $last_id );
175 if ( $duplicate [ 'is_fav' ]) {
176 $this- > store
-> favoriteById ( $last_id , $this- > user
-> getId ());
178 if ( $this- > store
-> deleteById ( $duplicate [ 'id' ], $this- > user
-> getId ())) {
179 Tools
:: logm ( 'previous link ' . $url- > getUrl () . ' entry deleted' );
183 // if there are tags, add them to the new article
184 if ( isset ( $_GET [ 'tags' ])) {
185 $_POST [ 'value' ] = $_GET [ 'tags' ];
186 $_POST [ 'entry_id' ] = $last_id ;
187 $this- > action ( 'add_tag' , $url );
190 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
193 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
194 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
197 if ( $autoclose == TRUE ) {
198 Tools
:: redirect ( '?view=home' );
200 Tools
:: redirect ( '?view=home&closewin=true' );
205 if ( isset ( $_GET [ 'search' ])) {
206 //when we want to apply a delete to a search
207 $tags = array ( $_GET [ 'search' ]);
208 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
209 $entry_ids = array ();
210 foreach ( $allentry_ids as $eachentry ) {
211 $entry_ids [] = $eachentry [ 0 ];
213 } else { // delete a single article
214 $entry_ids = array ( $id );
216 foreach ( $entry_ids as $id ) {
217 $msg = 'delete link #' . $id ;
218 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
219 if ( DOWNLOAD_PICTURES
) {
220 Picture
:: removeDirectory ( ABS_PATH
. $id );
222 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
225 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
226 $msg = 'error : can \' t delete link #' . $id ;
230 Tools
:: redirect ( '?' );
233 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
234 Tools
:: logm ( 'mark as favorite link #' . $id );
235 if ( Tools
:: isAjaxRequest () ) {
243 case 'toggle_archive' :
244 if ( isset ( $_GET [ 'tag_id' ])) {
245 //when we want to archive a whole tag
246 $tag_id = $_GET [ 'tag_id' ];
247 $allentry_ids = $this- > store
-> retrieveEntriesByTag ( $tag_id , $this- > user
-> getId ());
248 $entry_ids = array ();
249 foreach ( $allentry_ids as $eachentry ) {
250 $entry_ids [] = $eachentry [ 0 ];
252 } else { //archive a single article
253 $entry_ids = array ( $id );
255 foreach ( $entry_ids as $id ) {
256 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
257 Tools
:: logm ( 'archive link #' . $id );
259 if ( Tools
:: isAjaxRequest () ) {
268 $this- > store
-> archiveAll ( $this- > user
-> getId ());
269 Tools
:: logm ( 'archive all links' );
273 if ( isset ( $_GET [ 'search' ])) {
274 //when we want to apply a tag to a search
275 $tags = array ( $_GET [ 'search' ]);
276 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
277 $entry_ids = array ();
278 foreach ( $allentry_ids as $eachentry ) {
279 $entry_ids [] = $eachentry [ 0 ];
281 } else { //add a tag to a single article
282 $tags = explode ( ',' , $_POST [ 'value' ]);
283 $entry_ids = array ( $_POST [ 'entry_id' ]);
285 foreach ( $entry_ids as $entry_id ) {
286 $entry = $this- > store
-> retrieveOneById ( $entry_id , $this- > user
-> getId ());
288 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
289 Tools
:: logm ( 'error : article not found' );
292 //get all already set tags to preven duplicates
293 $already_set_tags = array ();
294 $entry_tags = $this- > store
-> retrieveTagsByEntry ( $entry_id );
295 foreach ( $entry_tags as $tag ) {
296 $already_set_tags [] = $tag [ 'value' ];
298 foreach ( $tags as $key => $tag_value ) {
299 $value = trim ( $tag_value );
300 if ( $value && ! in_array ( $value , $already_set_tags )) {
301 $tag = $this- > store
-> retrieveTagByValue ( $value );
304 $tag = $this- > store
-> createTag ( $value );
306 if ( STORAGE
== 'postgres' ) {
307 $sequence = 'tags_id_seq' ;
309 $tag_id = $this- > store
-> getLastId ( $sequence );
312 $tag_id = $tag [ 'id' ];
315 # we assign the tag to the article
316 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
320 $this- > messages
-> add ( 's' , _ ( 'The tag has been applied successfully' ));
321 Tools
:: logm ( 'The tag has been applied successfully' );
325 $tag_id = $_GET [ 'tag_id' ];
326 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
328 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
329 Tools
:: logm ( 'error : article not found' );
332 $this- > store
-> removeTagForEntry ( $id , $tag_id );
333 Tools
:: logm ( 'tag entry deleted' );
334 if ( $this- > store
-> cleanUnusedTag ( $tag_id )) {
335 Tools
:: logm ( 'tag deleted' );
337 $this- > messages
-> add ( 's' , _ ( 'The tag has been successfully deleted' ));
341 case 'reload_article' :
342 Tools
:: logm ( 'reload article' );
344 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
345 Tools
:: logm ( 'reload url ' . $entry [ 'url' ]);
346 $url = new Url ( base64_encode ( $entry [ 'url' ]));
347 $this- > action ( 'add' , $url );
350 /* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */
352 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId ());
353 $id = rand ( 1 , $count );
354 Tools
:: logm ( 'get a random article' );
355 Tools
:: redirect ( '?view=view&id=' . $id );
356 //$this->displayView('view', $id);
363 function displayView ( $view , $id = 0 )
372 $dev_infos = $this- > _getPocheVersion ( 'dev' );
373 $dev = trim ( $dev_infos [ 0 ]);
374 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
375 $prod_infos = $this- > _getPocheVersion ( 'prod' );
376 $prod = trim ( $prod_infos [ 0 ]);
377 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
378 $compare_dev = version_compare ( POCHE
, $dev );
379 $compare_prod = version_compare ( POCHE
, $prod );
380 $themes = $this- > tpl
-> getInstalledThemes ();
381 $languages = $this- > language
-> getInstalledLanguages ();
382 $token = $this- > user
-> getConfigValue ( 'token' );
383 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
384 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
387 'languages' => $languages ,
390 'check_time_dev' => $check_time_dev ,
391 'check_time_prod' => $check_time_prod ,
392 'compare_dev' => $compare_dev ,
393 'compare_prod' => $compare_prod ,
395 'user_id' => $this- > user
-> getId (),
396 'http_auth' => $http_auth ,
397 'only_user' => $only_user
399 Tools
:: logm ( 'config view' );
403 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
405 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
406 Tools
:: logm ( 'error : article not found' );
409 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
417 $token = $this- > user
-> getConfigValue ( 'token' );
418 //if term is set - search tags for this term
419 $term = Tools
:: checkVar ( 'term' );
420 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
421 if ( Tools
:: isAjaxRequest ()) {
423 foreach ( $tags as $tag ) {
424 $result [] = $tag [ 'value' ];
426 echo json_encode ( $result );
431 'user_id' => $this- > user
-> getId (),
436 if ( isset ( $_GET [ 'search' ])) {
437 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
438 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
439 $count = count ( $tpl_vars [ 'entries' ]);
440 $this- > pagination
-> set_total ( $count );
441 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
442 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
443 $tpl_vars [ 'page_links' ] = $page_links ;
444 $tpl_vars [ 'nb_results' ] = $count ;
445 $tpl_vars [ 'searchterm' ] = $search ;
449 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
450 if ( $entry != NULL ) {
451 Tools
:: logm ( 'view link #' . $id );
452 $content = $entry [ 'content' ];
453 if ( function_exists ( 'tidy_parse_string' )) {
454 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
455 $tidy- > cleanRepair ();
456 $content = $tidy- > value
;
462 $flattr = new FlattrItem ();
463 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
467 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
471 'content' => $content ,
477 Tools
:: logm ( 'error in view call : entry is null' );
480 default : # home, favorites, archive and tag views
485 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
488 //if id is given - we retrieve entries by tag: id is tag id
490 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
491 $tpl_vars [ 'id' ] = intval ( $id );
494 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
497 $this- > pagination
-> set_total ( $count );
498 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
499 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
500 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
501 $tpl_vars [ 'page_links' ] = $page_links ;
502 $tpl_vars [ 'nb_results' ] = $count ;
504 Tools
:: logm ( 'display ' . $view . ' view' );
512 * update the password of the current user.
513 * if MODE_DEMO is TRUE, the password can't be updated.
514 * @todo add the return value
515 * @todo set the new password in function header like this updatePassword($newPassword)
518 public function updatePassword ( $password , $confirmPassword )
521 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
522 Tools
:: logm ( 'in demo mode, you can \' t do this' );
523 Tools
:: redirect ( '?view=config' );
526 if ( isset ( $password ) && isset ( $confirmPassword )) {
527 if ( $password == $confirmPassword && ! empty ( $password )) {
528 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
529 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
531 Tools
:: logm ( 'password updated' );
535 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
536 Tools
:: redirect ( '?view=config' );
543 * Get credentials from differents sources
544 * It redirects the user to the $referer link
548 private function credentials ()
550 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
551 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
553 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
554 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
556 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
557 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
560 return array ( false , false , false );
564 * checks if login & password are correct and save the user in session.
565 * it redirects the user to the $referer link
566 * @param string $referer the url to redirect after login
567 * @todo add the return value
570 public function login ( $referer )
572 list ( $login , $password , $isauthenticated )= $this- > credentials ();
573 if ( $login === false || $password === false ) {
574 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
575 Tools
:: logm ( 'login failed' );
578 if (! empty ( $login ) && ! empty ( $password )) {
579 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
580 if ( $user != array ()) {
581 # Save login into Session
582 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
583 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
584 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
587 $language = $user [ 'config' ][ 'language' ];
588 @ putenv ( 'LC_ALL=' . $language );
589 setlocale ( LC_ALL
, $language );
590 bindtextdomain ( $language , LOCALE
);
591 textdomain ( $language );
593 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
594 Tools
:: logm ( 'login successful' );
595 Tools
:: redirect ( $referer );
597 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
598 // log login failure in web server log to allow fail2ban usage
599 error_log ( 'user ' . $login . ' authentication failure' );
600 Tools
:: logm ( 'login failed' );
606 * log out the poche user. It cleans the session.
607 * @todo add the return value
610 public function logout ()
612 $this- > user
= array ();
614 Tools
:: logm ( 'logout' );
619 * import datas into your wallabag
623 public function import () {
625 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
626 Tools
:: logm ( 'Import stated: parsing file' );
628 // assume, that file is in json format
629 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
630 $data = json_decode ( $str_data , true );
632 if ( $data === null ) {
633 //not json - assume html
634 $html = new simple_html_dom ();
635 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
638 foreach ( array ( 'ol' , 'ul' ) as $list ) {
639 foreach ( $html- > find ( $list ) as $ul ) {
640 foreach ( $ul- > find ( 'li' ) as $li ) {
643 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
644 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
645 $tmpEntry [ 'is_read' ] = $read ;
646 if ( $tmpEntry [ 'url' ]) {
650 # the second <ol/ul> is for read links
651 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
656 // for readability structure
658 foreach ( $data as $record ) {
659 if ( is_array ( $record )) {
661 foreach ( $record as $record2 ) {
662 if ( is_array ( $record2 )) {
669 $urlsInserted = array (); //urls of articles inserted
670 foreach ( $data as $record ) {
671 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
672 if ( $url and ! in_array ( $url , $urlsInserted )) {
673 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
674 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
675 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
676 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
680 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
682 $urlsInserted [] = $url ; //add
683 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
685 $tags = explode ( ',' , $record [ 'tags' ]);
686 foreach ( $tags as $tag ) {
688 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
690 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
692 $this- > store
-> createTag ( $tag );
693 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
694 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
703 $i = sizeof ( $urlsInserted );
705 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
708 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
711 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
713 // file parsing finished here
714 // now download article contents if any
715 // check if we need to download any content
717 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
719 if ( $recordsDownloadRequired == 0 ) {
721 // nothing to download
723 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
724 Tools
:: logm ( 'Import finished completely' );
729 // if just inserted - don't download anything, download will start in next reload
731 if (! isset ( $_FILES [ 'file' ])) {
733 // download next batch
735 Tools
:: logm ( 'Fetching next batch of articles...' );
736 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
737 $purifier = $this- > _getPurifier ();
738 foreach ( $items as $item ) {
739 $url = new Url ( base64_encode ( $item [ 'url' ]));
740 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
741 $content = Tools
:: getPageContent ( $url );
742 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
743 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
745 // clean content to prevent xss attack
747 $title = $purifier- > purify ( $title );
748 $body = $purifier- > purify ( $body );
749 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
750 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
756 'includeImport' => true ,
758 'recordsDownloadRequired' => $recordsDownloadRequired ,
759 'recordsUnderDownload' => IMPORT_LIMIT
,
760 'delay' => IMPORT_DELAY
* 1000
766 * export poche entries in json
767 * @return json all poche entries
769 public function export ()
771 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
772 header ( 'Content-Disposition: attachment; filename=' . $filename );
774 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
775 echo $this- > tpl
-> render ( 'export.twig' , array (
776 'export' => Tools
:: renderJson ( $entries ),
778 Tools
:: logm ( 'export view' );
782 * Checks online the latest version of poche and cache it
783 * @param string $which 'prod' or 'dev'
784 * @return string latest $which version
786 private function _getPocheVersion ( $which = 'prod' ) {
787 $cache_file = CACHE
. '/' . $which ;
788 $check_time = time ();
790 # checks if the cached version file exists
791 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
792 $version = file_get_contents ( $cache_file );
793 $check_time = filemtime ( $cache_file );
795 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
796 file_put_contents ( $cache_file , $version , LOCK_EX
);
798 return array ( $version , $check_time );
802 * Update token for current user
804 public function updateToken ()
806 $token = Tools
:: generateToken ();
807 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
808 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
809 $currentConfig [ 'token' ] = $token ;
810 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
815 * Generate RSS feeds for current user
819 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
820 * @param string $type the type of feed to generate
821 * @param int $limit the maximum number of items (0 means all)
823 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' , $limit = 0 )
825 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
826 $config = $this- > store
-> getConfigUser ( $user_id );
828 if ( $config == null ) {
829 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
832 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
833 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
836 $feed = new FeedWriter ( RSS2
);
837 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
838 $feed- > setLink ( Tools
:: getPocheUrl ());
839 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
840 $feed- > setChannelElement ( 'generator' , 'wallabag' );
841 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
843 if ( $type == 'tag' ) {
844 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
847 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
850 // if $limit is set to zero, use all entries
852 $limit = count ( $entries );
854 if ( count ( $entries ) > 0 ) {
855 for ( $i = 0 ; $i < min ( count ( $entries ), $limit ); $i ++
) {
856 $entry = $entries [ $i ];
857 $newItem = $feed- > createNewItem ();
858 $newItem- > setTitle ( $entry [ 'title' ]);
859 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
860 $newItem- > setLink ( $entry [ 'url' ]);
861 $newItem- > setDate ( time ());
862 $newItem- > setDescription ( $entry [ 'content' ]);
863 $feed- > addItem ( $newItem );
867 $feed- > genarateFeed ();
874 * Returns new purifier object with actual config
876 private function _getPurifier ()
878 $config = HTMLPurifier_Config
:: createDefault ();
879 $config- > set ( 'Cache.SerializerPath' , CACHE
);
880 $config- > set ( 'HTML.SafeIframe' , true );
882 //allow YouTube, Vimeo and dailymotion videos
883 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
885 return new HTMLPurifier ( $config );