]>
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' ));
340 /* 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 */
342 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId ());
343 $id = rand ( 1 , $count );
344 Tools
:: logm ( 'get a random article' );
345 Tools
:: redirect ( '?view=view&id=' . $id );
346 //$this->displayView('view', $id);
353 function displayView ( $view , $id = 0 )
362 $dev_infos = $this- > _getPocheVersion ( 'dev' );
363 $dev = trim ( $dev_infos [ 0 ]);
364 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
365 $prod_infos = $this- > _getPocheVersion ( 'prod' );
366 $prod = trim ( $prod_infos [ 0 ]);
367 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
368 $compare_dev = version_compare ( POCHE
, $dev );
369 $compare_prod = version_compare ( POCHE
, $prod );
370 $themes = $this- > tpl
-> getInstalledThemes ();
371 $languages = $this- > language
-> getInstalledLanguages ();
372 $token = $this- > user
-> getConfigValue ( 'token' );
373 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
374 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
377 'languages' => $languages ,
380 'check_time_dev' => $check_time_dev ,
381 'check_time_prod' => $check_time_prod ,
382 'compare_dev' => $compare_dev ,
383 'compare_prod' => $compare_prod ,
385 'user_id' => $this- > user
-> getId (),
386 'http_auth' => $http_auth ,
387 'only_user' => $only_user
389 Tools
:: logm ( 'config view' );
393 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
395 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
396 Tools
:: logm ( 'error : article not found' );
399 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
407 $token = $this- > user
-> getConfigValue ( 'token' );
408 //if term is set - search tags for this term
409 $term = Tools
:: checkVar ( 'term' );
410 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
411 if ( Tools
:: isAjaxRequest ()) {
413 foreach ( $tags as $tag ) {
414 $result [] = $tag [ 'value' ];
416 echo json_encode ( $result );
421 'user_id' => $this- > user
-> getId (),
426 if ( isset ( $_GET [ 'search' ])) {
427 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
428 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
429 $count = count ( $tpl_vars [ 'entries' ]);
430 $this- > pagination
-> set_total ( $count );
431 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
432 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
433 $tpl_vars [ 'page_links' ] = $page_links ;
434 $tpl_vars [ 'nb_results' ] = $count ;
435 $tpl_vars [ 'searchterm' ] = $search ;
439 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
440 if ( $entry != NULL ) {
441 Tools
:: logm ( 'view link #' . $id );
442 $content = $entry [ 'content' ];
443 if ( function_exists ( 'tidy_parse_string' )) {
444 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
445 $tidy- > cleanRepair ();
446 $content = $tidy- > value
;
452 $flattr = new FlattrItem ();
453 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
457 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
461 'content' => $content ,
467 Tools
:: logm ( 'error in view call : entry is null' );
470 default : # home, favorites, archive and tag views
475 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
478 //if id is given - we retrieve entries by tag: id is tag id
480 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
481 $tpl_vars [ 'id' ] = intval ( $id );
484 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
487 $this- > pagination
-> set_total ( $count );
488 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
489 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
490 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
491 $tpl_vars [ 'page_links' ] = $page_links ;
492 $tpl_vars [ 'nb_results' ] = $count ;
494 Tools
:: logm ( 'display ' . $view . ' view' );
502 * update the password of the current user.
503 * if MODE_DEMO is TRUE, the password can't be updated.
504 * @todo add the return value
505 * @todo set the new password in function header like this updatePassword($newPassword)
508 public function updatePassword ( $password , $confirmPassword )
511 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
512 Tools
:: logm ( 'in demo mode, you can \' t do this' );
513 Tools
:: redirect ( '?view=config' );
516 if ( isset ( $password ) && isset ( $confirmPassword )) {
517 if ( $password == $confirmPassword && ! empty ( $password )) {
518 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
519 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
521 Tools
:: logm ( 'password updated' );
525 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
526 Tools
:: redirect ( '?view=config' );
533 * Get credentials from differents sources
534 * It redirects the user to the $referer link
538 private function credentials ()
540 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
541 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
543 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
544 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
546 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
547 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
550 return array ( false , false , false );
554 * checks if login & password are correct and save the user in session.
555 * it redirects the user to the $referer link
556 * @param string $referer the url to redirect after login
557 * @todo add the return value
560 public function login ( $referer )
562 list ( $login , $password , $isauthenticated )= $this- > credentials ();
563 if ( $login === false || $password === false ) {
564 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
565 Tools
:: logm ( 'login failed' );
568 if (! empty ( $login ) && ! empty ( $password )) {
569 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
570 if ( $user != array ()) {
571 # Save login into Session
572 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
573 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
574 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
577 $language = $user [ 'config' ][ 'language' ];
578 @ putenv ( 'LC_ALL=' . $language );
579 setlocale ( LC_ALL
, $language );
580 bindtextdomain ( $language , LOCALE
);
581 textdomain ( $language );
583 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
584 Tools
:: logm ( 'login successful' );
585 Tools
:: redirect ( $referer );
587 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
588 // log login failure in web server log to allow fail2ban usage
589 error_log ( 'user ' . $login . ' authentication failure' );
590 Tools
:: logm ( 'login failed' );
596 * log out the poche user. It cleans the session.
597 * @todo add the return value
600 public function logout ()
602 $this- > user
= array ();
604 Tools
:: logm ( 'logout' );
609 * import datas into your wallabag
613 public function import () {
615 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
616 Tools
:: logm ( 'Import stated: parsing file' );
618 // assume, that file is in json format
619 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
620 $data = json_decode ( $str_data , true );
622 if ( $data === null ) {
623 //not json - assume html
624 $html = new simple_html_dom ();
625 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
628 foreach ( array ( 'ol' , 'ul' ) as $list ) {
629 foreach ( $html- > find ( $list ) as $ul ) {
630 foreach ( $ul- > find ( 'li' ) as $li ) {
633 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
634 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
635 $tmpEntry [ 'is_read' ] = $read ;
636 if ( $tmpEntry [ 'url' ]) {
640 # the second <ol/ul> is for read links
641 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
646 // for readability structure
648 foreach ( $data as $record ) {
649 if ( is_array ( $record )) {
651 foreach ( $record as $record2 ) {
652 if ( is_array ( $record2 )) {
659 $urlsInserted = array (); //urls of articles inserted
660 foreach ( $data as $record ) {
661 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
662 if ( $url and ! in_array ( $url , $urlsInserted )) {
663 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
664 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
665 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
666 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
670 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
672 $urlsInserted [] = $url ; //add
673 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
675 $tags = explode ( ',' , $record [ 'tags' ]);
676 foreach ( $tags as $tag ) {
678 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
680 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
682 $this- > store
-> createTag ( $tag );
683 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
684 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
693 $i = sizeof ( $urlsInserted );
695 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
698 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
701 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
703 // file parsing finished here
704 // now download article contents if any
705 // check if we need to download any content
707 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
709 if ( $recordsDownloadRequired == 0 ) {
711 // nothing to download
713 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
714 Tools
:: logm ( 'Import finished completely' );
719 // if just inserted - don't download anything, download will start in next reload
721 if (! isset ( $_FILES [ 'file' ])) {
723 // download next batch
725 Tools
:: logm ( 'Fetching next batch of articles...' );
726 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
727 $purifier = $this- > _getPurifier ();
728 foreach ( $items as $item ) {
729 $url = new Url ( base64_encode ( $item [ 'url' ]));
730 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
731 $content = Tools
:: getPageContent ( $url );
732 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
733 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
735 // clean content to prevent xss attack
737 $title = $purifier- > purify ( $title );
738 $body = $purifier- > purify ( $body );
739 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
740 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
746 'includeImport' => true ,
748 'recordsDownloadRequired' => $recordsDownloadRequired ,
749 'recordsUnderDownload' => IMPORT_LIMIT
,
750 'delay' => IMPORT_DELAY
* 1000
756 * export poche entries in json
757 * @return json all poche entries
759 public function export ()
761 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
762 header ( 'Content-Disposition: attachment; filename=' . $filename );
764 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
765 echo $this- > tpl
-> render ( 'export.twig' , array (
766 'export' => Tools
:: renderJson ( $entries ),
768 Tools
:: logm ( 'export view' );
772 * Checks online the latest version of poche and cache it
773 * @param string $which 'prod' or 'dev'
774 * @return string latest $which version
776 private function _getPocheVersion ( $which = 'prod' ) {
777 $cache_file = CACHE
. '/' . $which ;
778 $check_time = time ();
780 # checks if the cached version file exists
781 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
782 $version = file_get_contents ( $cache_file );
783 $check_time = filemtime ( $cache_file );
785 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
786 file_put_contents ( $cache_file , $version , LOCK_EX
);
788 return array ( $version , $check_time );
792 * Update token for current user
794 public function updateToken ()
796 $token = Tools
:: generateToken ();
797 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
798 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
799 $currentConfig [ 'token' ] = $token ;
800 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
805 * Generate RSS feeds for current user
809 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
810 * @param string $type the type of feed to generate
811 * @param int $limit the maximum number of items (0 means all)
813 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' , $limit = 0 )
815 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
816 $config = $this- > store
-> getConfigUser ( $user_id );
818 if ( $config == null ) {
819 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
822 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
823 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
826 $feed = new FeedWriter ( RSS2
);
827 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
828 $feed- > setLink ( Tools
:: getPocheUrl ());
829 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
830 $feed- > setChannelElement ( 'generator' , 'wallabag' );
831 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
833 if ( $type == 'tag' ) {
834 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
837 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
840 // if $limit is set to zero, use all entries
842 $limit = count ( $entries );
844 if ( count ( $entries ) > 0 ) {
845 for ( $i = 0 ; $i < min ( count ( $entries ), $limit ); $i ++
) {
846 $entry = $entries [ $i ];
847 $newItem = $feed- > createNewItem ();
848 $newItem- > setTitle ( $entry [ 'title' ]);
849 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
850 $newItem- > setLink ( $entry [ 'url' ]);
851 $newItem- > setDate ( time ());
852 $newItem- > setDescription ( $entry [ 'content' ]);
853 $feed- > addItem ( $newItem );
857 $feed- > genarateFeed ();
864 * Returns new purifier object with actual config
866 private function _getPurifier ()
868 $config = HTMLPurifier_Config
:: createDefault ();
869 $config- > set ( 'Cache.SerializerPath' , CACHE
);
870 $config- > set ( 'HTML.SafeIframe' , true );
872 //allow YouTube, Vimeo and dailymotion videos
873 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
875 return new HTMLPurifier ( $config );