]>
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 = "" , $internalRegistration = false )
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 if ( SEND_CONFIRMATION_EMAIL
&& function_exists ( 'mail' )) {
85 // if internal registration
86 $body_internal = "Hi, \r\n\r\n Someone just created an account for you on " . Tools
:: getPocheUrl () . ". \r\n Have fun with it !" ;
87 // if external (public) registration
88 $body = "Hi, \r\n\r\n You've just created an account on " . Tools
:: getPocheUrl () . . ". \r\n Have fun with it !" ;
89 $body = $internalRegistration ? $body_internal : $body ;
90 $body = wordwrap ( $body , 70 , " \r\n " ); // cut lines with more than 70 caracters (MIME standard)
91 if ( mail ( $email , sprintf ( _ ( 'Your new wallabag account on ' ), Tools
:: getPocheUrl ()), $body , 'From: {$email} ' )) {
92 Tools
:: logm ( 'The user ' . $newUsername . ' has been emailed' );
93 $this- > messages
-> add ( 'i' , sprintf ( _ ( 'The new user %1 $s has been sent an email at %2 $s .' ), $newUsername , $email ));
96 Tools
:: logm ( 'A problem has been encountered while sending an email' );
97 $this- > messages
-> add ( 'e' , _ ( 'A problem has been encountered while sending an email' );
100 Tools
:: logm ( 'The user has been created, but the server did not authorize sending emails' );
101 $this- > messages
-> add ( 'i' , _ ( 'The server did not authorize sending an email' );
103 Tools
:: logm ( 'The new user ' . $newUsername . ' has been installed' );
104 $this- > messages
-> add ( 's' , sprintf ( _ ( 'The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>' ), $newUsername ));
108 Tools
:: logm ( 'error during adding new user' );
113 $this- > messages
-> add ( 'e' , sprintf ( _ ( 'Error : An user with the name %s already exists !' ), $newUsername ));
114 Tools
:: logm ( 'An user with the name ' . $newUsername . ' already exists !' );
121 * Delete an existing user
123 public function deleteUser ( $password )
125 if ( $this- > store
-> listUsers () > 1 ) {
126 if ( Tools
:: encodeString ( $password . $this- > user
-> getUsername ()) == $this- > store
-> getUserPassword ( $this- > user
-> getId ())) {
127 $username = $this- > user
-> getUsername ();
128 $this- > store
-> deleteUserConfig ( $this- > user
-> getId ());
129 Tools
:: logm ( 'The configuration for user ' . $username . ' has been deleted !' );
130 $this- > store
-> deleteTagsEntriesAndEntries ( $this- > user
-> getId ());
131 Tools
:: logm ( 'The entries for user ' . $username . ' has been deleted !' );
132 $this- > store
-> deleteUser ( $this- > user
-> getId ());
133 Tools
:: logm ( 'User ' . $username . ' has been completely deleted !' );
135 Tools
:: logm ( 'logout' );
137 $this- > messages
-> add ( 's' , sprintf ( _ ( 'User %s has been successfully deleted !' ), $username ));
140 Tools
:: logm ( 'Bad password !' );
141 $this- > messages
-> add ( 'e' , _ ( 'Error : The password is wrong !' ));
145 Tools
:: logm ( 'Only user !' );
146 $this- > messages
-> add ( 'e' , _ ( 'Error : You are the only user, you cannot delete your account !' ));
150 public function getDefaultConfig ()
153 'pager' => PAGINATION
,
155 'theme' => DEFAULT_THEME
160 * Call action (mark as fav, archive, delete, etc.)
162 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE , $tags = null )
167 $content = Tools
:: getPageContent ( $url );
168 $title = ( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' );
169 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
171 // clean content from prevent xss attack
172 $purifier = $this- > _getPurifier ();
173 $title = $purifier- > purify ( $title );
174 $body = $purifier- > purify ( $body );
176 //search for possible duplicate
178 $duplicate = $this- > store
-> retrieveOneByURL ( $url- > getUrl (), $this- > user
-> getId ());
180 $last_id = $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ());
182 Tools
:: logm ( 'add link ' . $url- > getUrl ());
183 if ( DOWNLOAD_PICTURES
) {
184 $content = Picture
:: filterPicture ( $body , $url- > getUrl (), $last_id );
185 Tools
:: logm ( 'updating content article' );
186 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
189 if ( $duplicate != NULL ) {
190 // 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
191 Tools
:: logm ( 'link ' . $url- > getUrl () . ' is a duplicate' );
192 // 1) - preserve tags and favorite, then drop old entry
193 $this- > store
-> reassignTags ( $duplicate [ 'id' ], $last_id );
194 if ( $duplicate [ 'is_fav' ]) {
195 $this- > store
-> favoriteById ( $last_id , $this- > user
-> getId ());
197 if ( $this- > store
-> deleteById ( $duplicate [ 'id' ], $this- > user
-> getId ())) {
198 Tools
:: logm ( 'previous link ' . $url- > getUrl () . ' entry deleted' );
202 // if there are tags, add them to the new article
203 if ( isset ( $_GET [ 'tags' ])) {
204 $_POST [ 'value' ] = $_GET [ 'tags' ];
205 $_POST [ 'entry_id' ] = $last_id ;
206 $this- > action ( 'add_tag' , $url );
209 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
212 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
213 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
216 if ( $autoclose == TRUE ) {
217 Tools
:: redirect ( '?view=home' );
219 Tools
:: redirect ( '?view=home&closewin=true' );
224 if ( isset ( $_GET [ 'search' ])) {
225 //when we want to apply a delete to a search
226 $tags = array ( $_GET [ 'search' ]);
227 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
228 $entry_ids = array ();
229 foreach ( $allentry_ids as $eachentry ) {
230 $entry_ids [] = $eachentry [ 0 ];
232 } else { // delete a single article
233 $entry_ids = array ( $id );
235 foreach ( $entry_ids as $id ) {
236 $msg = 'delete link #' . $id ;
237 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
238 if ( DOWNLOAD_PICTURES
) {
239 Picture
:: removeDirectory ( ABS_PATH
. $id );
241 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
244 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
245 $msg = 'error : can \' t delete link #' . $id ;
249 Tools
:: redirect ( '?' );
252 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
253 Tools
:: logm ( 'mark as favorite link #' . $id );
254 if ( Tools
:: isAjaxRequest () ) {
262 case 'toggle_archive' :
263 if ( isset ( $_GET [ 'tag_id' ])) {
264 //when we want to archive a whole tag
265 $tag_id = $_GET [ 'tag_id' ];
266 $allentry_ids = $this- > store
-> retrieveEntriesByTag ( $tag_id , $this- > user
-> getId ());
267 $entry_ids = array ();
268 foreach ( $allentry_ids as $eachentry ) {
269 $entry_ids [] = $eachentry [ 0 ];
271 } else { //archive a single article
272 $entry_ids = array ( $id );
274 foreach ( $entry_ids as $id ) {
275 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
276 Tools
:: logm ( 'archive link #' . $id );
278 if ( Tools
:: isAjaxRequest () ) {
287 $this- > store
-> archiveAll ( $this- > user
-> getId ());
288 Tools
:: logm ( 'archive all links' );
292 if ( isset ( $_GET [ 'search' ])) {
293 //when we want to apply a tag to a search
294 $tags = array ( $_GET [ 'search' ]);
295 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
296 $entry_ids = array ();
297 foreach ( $allentry_ids as $eachentry ) {
298 $entry_ids [] = $eachentry [ 0 ];
300 } else { //add a tag to a single article
301 $tags = explode ( ',' , $_POST [ 'value' ]);
302 $entry_ids = array ( $_POST [ 'entry_id' ]);
304 foreach ( $entry_ids as $entry_id ) {
305 $entry = $this- > store
-> retrieveOneById ( $entry_id , $this- > user
-> getId ());
307 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
308 Tools
:: logm ( 'error : article not found' );
311 //get all already set tags to preven duplicates
312 $already_set_tags = array ();
313 $entry_tags = $this- > store
-> retrieveTagsByEntry ( $entry_id );
314 foreach ( $entry_tags as $tag ) {
315 $already_set_tags [] = $tag [ 'value' ];
317 foreach ( $tags as $key => $tag_value ) {
318 $value = trim ( $tag_value );
319 if ( $value && ! in_array ( $value , $already_set_tags )) {
320 $tag = $this- > store
-> retrieveTagByValue ( $value );
323 $tag = $this- > store
-> createTag ( $value );
325 if ( STORAGE
== 'postgres' ) {
326 $sequence = 'tags_id_seq' ;
328 $tag_id = $this- > store
-> getLastId ( $sequence );
331 $tag_id = $tag [ 'id' ];
334 # we assign the tag to the article
335 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
339 $this- > messages
-> add ( 's' , _ ( 'The tag has been applied successfully' ));
340 Tools
:: logm ( 'The tag has been applied successfully' );
344 $tag_id = $_GET [ 'tag_id' ];
345 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
347 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
348 Tools
:: logm ( 'error : article not found' );
351 $this- > store
-> removeTagForEntry ( $id , $tag_id );
352 Tools
:: logm ( 'tag entry deleted' );
353 if ( $this- > store
-> cleanUnusedTag ( $tag_id )) {
354 Tools
:: logm ( 'tag deleted' );
356 $this- > messages
-> add ( 's' , _ ( 'The tag has been successfully deleted' ));
364 function displayView ( $view , $id = 0 )
373 $dev_infos = $this- > _getPocheVersion ( 'dev' );
374 $dev = trim ( $dev_infos [ 0 ]);
375 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
376 $prod_infos = $this- > _getPocheVersion ( 'prod' );
377 $prod = trim ( $prod_infos [ 0 ]);
378 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
379 $compare_dev = version_compare ( POCHE
, $dev );
380 $compare_prod = version_compare ( POCHE
, $prod );
381 $themes = $this- > tpl
-> getInstalledThemes ();
382 $languages = $this- > language
-> getInstalledLanguages ();
383 $token = $this- > user
-> getConfigValue ( 'token' );
384 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
385 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
388 'languages' => $languages ,
391 'check_time_dev' => $check_time_dev ,
392 'check_time_prod' => $check_time_prod ,
393 'compare_dev' => $compare_dev ,
394 'compare_prod' => $compare_prod ,
396 'user_id' => $this- > user
-> getId (),
397 'http_auth' => $http_auth ,
398 'only_user' => $only_user
400 Tools
:: logm ( 'config view' );
404 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
406 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
407 Tools
:: logm ( 'error : article not found' );
410 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
418 $token = $this- > user
-> getConfigValue ( 'token' );
419 //if term is set - search tags for this term
420 $term = Tools
:: checkVar ( 'term' );
421 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
422 if ( Tools
:: isAjaxRequest ()) {
424 foreach ( $tags as $tag ) {
425 $result [] = $tag [ 'value' ];
427 echo json_encode ( $result );
432 'user_id' => $this- > user
-> getId (),
437 if ( isset ( $_GET [ 'search' ])) {
438 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
439 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
440 $count = count ( $tpl_vars [ 'entries' ]);
441 $this- > pagination
-> set_total ( $count );
442 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
443 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
444 $tpl_vars [ 'page_links' ] = $page_links ;
445 $tpl_vars [ 'nb_results' ] = $count ;
446 $tpl_vars [ 'searchterm' ] = $search ;
450 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
451 if ( $entry != NULL ) {
452 Tools
:: logm ( 'view link #' . $id );
453 $content = $entry [ 'content' ];
454 if ( function_exists ( 'tidy_parse_string' )) {
455 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
456 $tidy- > cleanRepair ();
457 $content = $tidy- > value
;
463 $flattr = new FlattrItem ();
464 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
468 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
472 'content' => $content ,
478 Tools
:: logm ( 'error in view call : entry is null' );
481 default : # home, favorites, archive and tag views
486 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
489 //if id is given - we retrieve entries by tag: id is tag id
491 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
492 $tpl_vars [ 'id' ] = intval ( $id );
495 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
498 $this- > pagination
-> set_total ( $count );
499 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
500 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
501 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
502 $tpl_vars [ 'page_links' ] = $page_links ;
503 $tpl_vars [ 'nb_results' ] = $count ;
505 Tools
:: logm ( 'display ' . $view . ' view' );
513 * update the password of the current user.
514 * if MODE_DEMO is TRUE, the password can't be updated.
515 * @todo add the return value
516 * @todo set the new password in function header like this updatePassword($newPassword)
519 public function updatePassword ( $password , $confirmPassword )
522 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
523 Tools
:: logm ( 'in demo mode, you can \' t do this' );
524 Tools
:: redirect ( '?view=config' );
527 if ( isset ( $password ) && isset ( $confirmPassword )) {
528 if ( $password == $confirmPassword && ! empty ( $password )) {
529 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
530 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
532 Tools
:: logm ( 'password updated' );
536 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
537 Tools
:: redirect ( '?view=config' );
544 * Get credentials from differents sources
545 * It redirects the user to the $referer link
549 private function credentials ()
551 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
552 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
554 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
555 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
557 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
558 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
561 return array ( false , false , false );
565 * checks if login & password are correct and save the user in session.
566 * it redirects the user to the $referer link
567 * @param string $referer the url to redirect after login
568 * @todo add the return value
571 public function login ( $referer )
573 list ( $login , $password , $isauthenticated )= $this- > credentials ();
574 if ( $login === false || $password === false ) {
575 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
576 Tools
:: logm ( 'login failed' );
579 if (! empty ( $login ) && ! empty ( $password )) {
580 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
581 if ( $user != array ()) {
582 # Save login into Session
583 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
584 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
585 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
588 $language = $user [ 'config' ][ 'language' ];
589 @ putenv ( 'LC_ALL=' . $language );
590 setlocale ( LC_ALL
, $language );
591 bindtextdomain ( $language , LOCALE
);
592 textdomain ( $language );
594 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
595 Tools
:: logm ( 'login successful' );
596 Tools
:: redirect ( $referer );
598 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
599 // log login failure in web server log to allow fail2ban usage
600 error_log ( 'user ' . $login . ' authentication failure' );
601 Tools
:: logm ( 'login failed' );
607 * log out the poche user. It cleans the session.
608 * @todo add the return value
611 public function logout ()
613 $this- > user
= array ();
615 Tools
:: logm ( 'logout' );
620 * import datas into your wallabag
624 public function import () {
626 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
627 Tools
:: logm ( 'Import stated: parsing file' );
629 // assume, that file is in json format
630 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
631 $data = json_decode ( $str_data , true );
633 if ( $data === null ) {
634 //not json - assume html
635 $html = new simple_html_dom ();
636 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
639 foreach ( array ( 'ol' , 'ul' ) as $list ) {
640 foreach ( $html- > find ( $list ) as $ul ) {
641 foreach ( $ul- > find ( 'li' ) as $li ) {
644 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
645 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
646 $tmpEntry [ 'is_read' ] = $read ;
647 if ( $tmpEntry [ 'url' ]) {
651 # the second <ol/ul> is for read links
652 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
657 // for readability structure
659 foreach ( $data as $record ) {
660 if ( is_array ( $record )) {
662 foreach ( $record as $record2 ) {
663 if ( is_array ( $record2 )) {
670 $urlsInserted = array (); //urls of articles inserted
671 foreach ( $data as $record ) {
672 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
673 if ( $url and ! in_array ( $url , $urlsInserted )) {
674 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
675 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
676 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
677 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
681 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
683 $urlsInserted [] = $url ; //add
684 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
686 $tags = explode ( ',' , $record [ 'tags' ]);
687 foreach ( $tags as $tag ) {
689 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
691 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
693 $this- > store
-> createTag ( $tag );
694 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
695 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
704 $i = sizeof ( $urlsInserted );
706 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
709 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
712 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
714 // file parsing finished here
715 // now download article contents if any
716 // check if we need to download any content
718 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
720 if ( $recordsDownloadRequired == 0 ) {
722 // nothing to download
724 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
725 Tools
:: logm ( 'Import finished completely' );
730 // if just inserted - don't download anything, download will start in next reload
732 if (! isset ( $_FILES [ 'file' ])) {
734 // download next batch
736 Tools
:: logm ( 'Fetching next batch of articles...' );
737 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
738 $purifier = $this- > _getPurifier ();
739 foreach ( $items as $item ) {
740 $url = new Url ( base64_encode ( $item [ 'url' ]));
741 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
742 $content = Tools
:: getPageContent ( $url );
743 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
744 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
746 // clean content to prevent xss attack
748 $title = $purifier- > purify ( $title );
749 $body = $purifier- > purify ( $body );
750 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
751 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
757 'includeImport' => true ,
759 'recordsDownloadRequired' => $recordsDownloadRequired ,
760 'recordsUnderDownload' => IMPORT_LIMIT
,
761 'delay' => IMPORT_DELAY
* 1000
767 * export poche entries in json
768 * @return json all poche entries
770 public function export ()
772 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
773 header ( 'Content-Disposition: attachment; filename=' . $filename );
775 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
776 echo $this- > tpl
-> render ( 'export.twig' , array (
777 'export' => Tools
:: renderJson ( $entries ),
779 Tools
:: logm ( 'export view' );
783 * Checks online the latest version of poche and cache it
784 * @param string $which 'prod' or 'dev'
785 * @return string latest $which version
787 private function _getPocheVersion ( $which = 'prod' ) {
788 $cache_file = CACHE
. '/' . $which ;
789 $check_time = time ();
791 # checks if the cached version file exists
792 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
793 $version = file_get_contents ( $cache_file );
794 $check_time = filemtime ( $cache_file );
796 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
797 file_put_contents ( $cache_file , $version , LOCK_EX
);
799 return array ( $version , $check_time );
803 * Update token for current user
805 public function updateToken ()
807 $token = Tools
:: generateToken ();
808 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
809 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
810 $currentConfig [ 'token' ] = $token ;
811 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
816 * Generate RSS feeds for current user
820 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
821 * @param string $type the type of feed to generate
822 * @param int $limit the maximum number of items (0 means all)
824 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' , $limit = 0 )
826 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
827 $config = $this- > store
-> getConfigUser ( $user_id );
829 if ( $config == null ) {
830 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
833 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
834 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
837 $feed = new FeedWriter ( RSS2
);
838 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
839 $feed- > setLink ( Tools
:: getPocheUrl ());
840 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
841 $feed- > setChannelElement ( 'generator' , 'wallabag' );
842 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
844 if ( $type == 'tag' ) {
845 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
848 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
851 // if $limit is set to zero, use all entries
853 $limit = count ( $entries );
855 if ( count ( $entries ) > 0 ) {
856 for ( $i = 0 ; $i < min ( count ( $entries ), $limit ); $i ++
) {
857 $entry = $entries [ $i ];
858 $newItem = $feed- > createNewItem ();
859 $newItem- > setTitle ( $entry [ 'title' ]);
860 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
861 $newItem- > setLink ( $entry [ 'url' ]);
862 $newItem- > setDate ( time ());
863 $newItem- > setDescription ( $entry [ 'content' ]);
864 $feed- > addItem ( $newItem );
868 $feed- > genarateFeed ();
875 * Returns new purifier object with actual config
877 private function _getPurifier ()
879 $config = HTMLPurifier_Config
:: createDefault ();
880 $config- > set ( 'Cache.SerializerPath' , CACHE
);
881 $config- > set ( 'HTML.SafeIframe' , true );
883 //allow YouTube, Vimeo and dailymotion videos
884 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
886 return new HTMLPurifier ( $config );