]>
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 Tools
:: logm ( 'Trying to create a new user...' );
80 if (! empty ( $username ) && ! empty ( $password )){
81 $newUsername = filter_var ( $username , FILTER_SANITIZE_STRING
);
82 $email = filter_var ( $email , FILTER_SANITIZE_STRING
);
83 if (! $this- > store
-> userExists ( $newUsername )){
84 if ( $this- > store
-> install ( $newUsername , Tools
:: encodeString ( $password . $newUsername ), $email )) {
85 Tools
:: logm ( 'The new user ' . $newUsername . ' has been installed' );
86 $this- > messages
-> add ( 's' , sprintf ( _ ( 'The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>' ), $newUsername ));
90 Tools
:: logm ( 'error during adding new user' );
95 $this- > messages
-> add ( 'e' , sprintf ( _ ( 'Error : An user with the name %s already exists !' ), $newUsername ));
96 Tools
:: logm ( 'An user with the name ' . $newUsername . ' already exists !' );
101 Tools
:: logm ( 'Password or username were empty' );
106 * Delete an existing user
108 public function deleteUser ( $password )
110 if ( $this- > store
-> listUsers () > 1 ) {
111 if ( Tools
:: encodeString ( $password . $this- > user
-> getUsername ()) == $this- > store
-> getUserPassword ( $this- > user
-> getId ())) {
112 $username = $this- > user
-> getUsername ();
113 $this- > store
-> deleteUserConfig ( $this- > user
-> getId ());
114 Tools
:: logm ( 'The configuration for user ' . $username . ' has been deleted !' );
115 $this- > store
-> deleteTagsEntriesAndEntries ( $this- > user
-> getId ());
116 Tools
:: logm ( 'The entries for user ' . $username . ' has been deleted !' );
117 $this- > store
-> deleteUser ( $this- > user
-> getId ());
118 Tools
:: logm ( 'User ' . $username . ' has been completely deleted !' );
120 Tools
:: logm ( 'logout' );
122 $this- > messages
-> add ( 's' , sprintf ( _ ( 'User %s has been successfully deleted !' ), $username ));
125 Tools
:: logm ( 'Bad password !' );
126 $this- > messages
-> add ( 'e' , _ ( 'Error : The password is wrong !' ));
130 Tools
:: logm ( 'Only user !' );
131 $this- > messages
-> add ( 'e' , _ ( 'Error : You are the only user, you cannot delete your account !' ));
135 public function getDefaultConfig ()
138 'pager' => PAGINATION
,
140 'theme' => DEFAULT_THEME
145 * Call action (mark as fav, archive, delete, etc.)
147 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE , $tags = null )
152 $content = Tools
:: getPageContent ( $url );
153 $title = ( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' );
154 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
156 // clean content from prevent xss attack
157 $purifier = $this- > _getPurifier ();
158 $title = $purifier- > purify ( $title );
159 $body = $purifier- > purify ( $body );
161 //search for possible duplicate
163 $duplicate = $this- > store
-> retrieveOneByURL ( $url- > getUrl (), $this- > user
-> getId ());
165 $last_id = $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ());
167 Tools
:: logm ( 'add link ' . $url- > getUrl ());
168 if ( DOWNLOAD_PICTURES
) {
169 $content = Picture
:: filterPicture ( $body , $url- > getUrl (), $last_id );
170 Tools
:: logm ( 'updating content article' );
171 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
174 if ( $duplicate != NULL ) {
175 // 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
176 Tools
:: logm ( 'link ' . $url- > getUrl () . ' is a duplicate' );
177 // 1) - preserve tags and favorite, then drop old entry
178 $this- > store
-> reassignTags ( $duplicate [ 'id' ], $last_id );
179 if ( $duplicate [ 'is_fav' ]) {
180 $this- > store
-> favoriteById ( $last_id , $this- > user
-> getId ());
182 if ( $this- > store
-> deleteById ( $duplicate [ 'id' ], $this- > user
-> getId ())) {
183 Tools
:: logm ( 'previous link ' . $url- > getUrl () . ' entry deleted' );
187 // if there are tags, add them to the new article
188 if ( isset ( $_GET [ 'tags' ])) {
189 $_POST [ 'value' ] = $_GET [ 'tags' ];
190 $_POST [ 'entry_id' ] = $last_id ;
191 $this- > action ( 'add_tag' , $url );
194 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
197 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
198 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
201 if ( $autoclose == TRUE ) {
202 Tools
:: redirect ( '?view=home' );
204 Tools
:: redirect ( '?view=home&closewin=true' );
209 if ( isset ( $_GET [ 'search' ])) {
210 //when we want to apply a delete to a search
211 $tags = array ( $_GET [ 'search' ]);
212 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
213 $entry_ids = array ();
214 foreach ( $allentry_ids as $eachentry ) {
215 $entry_ids [] = $eachentry [ 0 ];
217 } else { // delete a single article
218 $entry_ids = array ( $id );
220 foreach ( $entry_ids as $id ) {
221 $msg = 'delete link #' . $id ;
222 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
223 if ( DOWNLOAD_PICTURES
) {
224 Picture
:: removeDirectory ( ABS_PATH
. $id );
226 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
229 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
230 $msg = 'error : can \' t delete link #' . $id ;
234 Tools
:: redirect ( '?' );
237 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
238 Tools
:: logm ( 'mark as favorite link #' . $id );
239 if ( Tools
:: isAjaxRequest () ) {
247 case 'toggle_archive' :
248 if ( isset ( $_GET [ 'tag_id' ])) {
249 //when we want to archive a whole tag
250 $tag_id = $_GET [ 'tag_id' ];
251 $allentry_ids = $this- > store
-> retrieveEntriesByTag ( $tag_id , $this- > user
-> getId ());
252 $entry_ids = array ();
253 foreach ( $allentry_ids as $eachentry ) {
254 $entry_ids [] = $eachentry [ 0 ];
256 } else { //archive a single article
257 $entry_ids = array ( $id );
259 foreach ( $entry_ids as $id ) {
260 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
261 Tools
:: logm ( 'archive link #' . $id );
263 if ( Tools
:: isAjaxRequest () ) {
272 $this- > store
-> archiveAll ( $this- > user
-> getId ());
273 Tools
:: logm ( 'archive all links' );
277 if ( isset ( $_GET [ 'search' ])) {
278 //when we want to apply a tag to a search
279 $tags = array ( $_GET [ 'search' ]);
280 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
281 $entry_ids = array ();
282 foreach ( $allentry_ids as $eachentry ) {
283 $entry_ids [] = $eachentry [ 0 ];
285 } else { //add a tag to a single article
286 $tags = explode ( ',' , $_POST [ 'value' ]);
287 $entry_ids = array ( $_POST [ 'entry_id' ]);
289 foreach ( $entry_ids as $entry_id ) {
290 $entry = $this- > store
-> retrieveOneById ( $entry_id , $this- > user
-> getId ());
292 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
293 Tools
:: logm ( 'error : article not found' );
296 //get all already set tags to preven duplicates
297 $already_set_tags = array ();
298 $entry_tags = $this- > store
-> retrieveTagsByEntry ( $entry_id );
299 foreach ( $entry_tags as $tag ) {
300 $already_set_tags [] = $tag [ 'value' ];
302 foreach ( $tags as $key => $tag_value ) {
303 $value = trim ( $tag_value );
304 if ( $value && ! in_array ( $value , $already_set_tags )) {
305 $tag = $this- > store
-> retrieveTagByValue ( $value );
308 $tag = $this- > store
-> createTag ( $value );
310 if ( STORAGE
== 'postgres' ) {
311 $sequence = 'tags_id_seq' ;
313 $tag_id = $this- > store
-> getLastId ( $sequence );
316 $tag_id = $tag [ 'id' ];
319 # we assign the tag to the article
320 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
324 $this- > messages
-> add ( 's' , _ ( 'The tag has been applied successfully' ));
325 Tools
:: logm ( 'The tag has been applied successfully' );
329 $tag_id = $_GET [ 'tag_id' ];
330 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
332 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
333 Tools
:: logm ( 'error : article not found' );
336 $this- > store
-> removeTagForEntry ( $id , $tag_id );
337 Tools
:: logm ( 'tag entry deleted' );
338 if ( $this- > store
-> cleanUnusedTag ( $tag_id )) {
339 Tools
:: logm ( 'tag deleted' );
341 $this- > messages
-> add ( 's' , _ ( 'The tag has been successfully deleted' ));
349 function displayView ( $view , $id = 0 )
358 $dev_infos = $this- > _getPocheVersion ( 'dev' );
359 $dev = trim ( $dev_infos [ 0 ]);
360 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
361 $prod_infos = $this- > _getPocheVersion ( 'prod' );
362 $prod = trim ( $prod_infos [ 0 ]);
363 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
364 $compare_dev = version_compare ( POCHE
, $dev );
365 $compare_prod = version_compare ( POCHE
, $prod );
366 $themes = $this- > tpl
-> getInstalledThemes ();
367 $languages = $this- > language
-> getInstalledLanguages ();
368 $token = $this- > user
-> getConfigValue ( 'token' );
369 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
370 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
373 'languages' => $languages ,
376 'check_time_dev' => $check_time_dev ,
377 'check_time_prod' => $check_time_prod ,
378 'compare_dev' => $compare_dev ,
379 'compare_prod' => $compare_prod ,
381 'user_id' => $this- > user
-> getId (),
382 'http_auth' => $http_auth ,
383 'only_user' => $only_user
385 Tools
:: logm ( 'config view' );
389 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
391 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
392 Tools
:: logm ( 'error : article not found' );
395 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
403 $token = $this- > user
-> getConfigValue ( 'token' );
404 //if term is set - search tags for this term
405 $term = Tools
:: checkVar ( 'term' );
406 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
407 if ( Tools
:: isAjaxRequest ()) {
409 foreach ( $tags as $tag ) {
410 $result [] = $tag [ 'value' ];
412 echo json_encode ( $result );
417 'user_id' => $this- > user
-> getId (),
422 if ( isset ( $_GET [ 'search' ])) {
423 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
424 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
425 $count = count ( $tpl_vars [ 'entries' ]);
426 $this- > pagination
-> set_total ( $count );
427 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
428 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
429 $tpl_vars [ 'page_links' ] = $page_links ;
430 $tpl_vars [ 'nb_results' ] = $count ;
431 $tpl_vars [ 'searchterm' ] = $search ;
435 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
436 if ( $entry != NULL ) {
437 Tools
:: logm ( 'view link #' . $id );
438 $content = $entry [ 'content' ];
439 if ( function_exists ( 'tidy_parse_string' )) {
440 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
441 $tidy- > cleanRepair ();
442 $content = $tidy- > value
;
448 $flattr = new FlattrItem ();
449 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
453 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
457 'content' => $content ,
463 Tools
:: logm ( 'error in view call : entry is null' );
466 default : # home, favorites, archive and tag views
471 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
474 //if id is given - we retrieve entries by tag: id is tag id
476 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
477 $tpl_vars [ 'id' ] = intval ( $id );
480 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
483 $this- > pagination
-> set_total ( $count );
484 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
485 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
486 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
487 $tpl_vars [ 'page_links' ] = $page_links ;
488 $tpl_vars [ 'nb_results' ] = $count ;
490 Tools
:: logm ( 'display ' . $view . ' view' );
498 * update the password of the current user.
499 * if MODE_DEMO is TRUE, the password can't be updated.
500 * @todo add the return value
501 * @todo set the new password in function header like this updatePassword($newPassword)
504 public function updatePassword ( $password , $confirmPassword )
507 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
508 Tools
:: logm ( 'in demo mode, you can \' t do this' );
509 Tools
:: redirect ( '?view=config' );
512 if ( isset ( $password ) && isset ( $confirmPassword )) {
513 if ( $password == $confirmPassword && ! empty ( $password )) {
514 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
515 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
517 Tools
:: logm ( 'password updated' );
521 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
522 Tools
:: redirect ( '?view=config' );
529 * Get credentials from differents sources
530 * It redirects the user to the $referer link
534 private function credentials ()
536 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
537 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
539 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
540 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
542 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
543 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
546 return array ( false , false , false );
550 * checks if login & password are correct and save the user in session.
551 * it redirects the user to the $referer link
552 * @param string $referer the url to redirect after login
553 * @todo add the return value
556 public function login ( $referer )
558 list ( $login , $password , $isauthenticated )= $this- > credentials ();
559 if ( $login === false || $password === false ) {
560 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
561 Tools
:: logm ( 'login failed' );
564 if (! empty ( $login ) && ! empty ( $password )) {
565 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
566 if ( $user != array ()) {
567 # Save login into Session
568 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
569 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
570 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
573 $language = $user [ 'config' ][ 'language' ];
574 @ putenv ( 'LC_ALL=' . $language );
575 setlocale ( LC_ALL
, $language );
576 bindtextdomain ( $language , LOCALE
);
577 textdomain ( $language );
579 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
580 Tools
:: logm ( 'login successful' );
581 Tools
:: redirect ( $referer );
583 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
584 // log login failure in web server log to allow fail2ban usage
585 error_log ( 'user ' . $login . ' authentication failure' );
586 Tools
:: logm ( 'login failed' );
592 * log out the poche user. It cleans the session.
593 * @todo add the return value
596 public function logout ()
598 $this- > user
= array ();
600 Tools
:: logm ( 'logout' );
605 * import datas into your wallabag
609 public function import () {
611 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
612 Tools
:: logm ( 'Import stated: parsing file' );
614 // assume, that file is in json format
615 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
616 $data = json_decode ( $str_data , true );
618 if ( $data === null ) {
619 //not json - assume html
620 $html = new simple_html_dom ();
621 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
624 foreach ( array ( 'ol' , 'ul' ) as $list ) {
625 foreach ( $html- > find ( $list ) as $ul ) {
626 foreach ( $ul- > find ( 'li' ) as $li ) {
629 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
630 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
631 $tmpEntry [ 'is_read' ] = $read ;
632 if ( $tmpEntry [ 'url' ]) {
636 # the second <ol/ul> is for read links
637 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
642 // for readability structure
644 foreach ( $data as $record ) {
645 if ( is_array ( $record )) {
647 foreach ( $record as $record2 ) {
648 if ( is_array ( $record2 )) {
655 $urlsInserted = array (); //urls of articles inserted
656 foreach ( $data as $record ) {
657 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
658 if ( $url and ! in_array ( $url , $urlsInserted )) {
659 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
660 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
661 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
662 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
666 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
668 $urlsInserted [] = $url ; //add
669 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
671 $tags = explode ( ',' , $record [ 'tags' ]);
672 foreach ( $tags as $tag ) {
674 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
676 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
678 $this- > store
-> createTag ( $tag );
679 $tag_id = $this- > store
-> retrieveTagByValue ( $tag );
680 $this- > store
-> setTagToEntry ( $tag_id [ 'id' ], $entry_id );
689 $i = sizeof ( $urlsInserted );
691 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
694 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
697 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
699 // file parsing finished here
700 // now download article contents if any
701 // check if we need to download any content
703 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
705 if ( $recordsDownloadRequired == 0 ) {
707 // nothing to download
709 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
710 Tools
:: logm ( 'Import finished completely' );
715 // if just inserted - don't download anything, download will start in next reload
717 if (! isset ( $_FILES [ 'file' ])) {
719 // download next batch
721 Tools
:: logm ( 'Fetching next batch of articles...' );
722 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
723 $purifier = $this- > _getPurifier ();
724 foreach ( $items as $item ) {
725 $url = new Url ( base64_encode ( $item [ 'url' ]));
726 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
727 $content = Tools
:: getPageContent ( $url );
728 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
729 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
731 // clean content to prevent xss attack
733 $title = $purifier- > purify ( $title );
734 $body = $purifier- > purify ( $body );
735 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
736 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
742 'includeImport' => true ,
744 'recordsDownloadRequired' => $recordsDownloadRequired ,
745 'recordsUnderDownload' => IMPORT_LIMIT
,
746 'delay' => IMPORT_DELAY
* 1000
752 * export poche entries in json
753 * @return json all poche entries
755 public function export ()
757 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
758 header ( 'Content-Disposition: attachment; filename=' . $filename );
760 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
761 echo $this- > tpl
-> render ( 'export.twig' , array (
762 'export' => Tools
:: renderJson ( $entries ),
764 Tools
:: logm ( 'export view' );
768 * Checks online the latest version of poche and cache it
769 * @param string $which 'prod' or 'dev'
770 * @return string latest $which version
772 private function _getPocheVersion ( $which = 'prod' ) {
773 $cache_file = CACHE
. '/' . $which ;
774 $check_time = time ();
776 # checks if the cached version file exists
777 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
778 $version = file_get_contents ( $cache_file );
779 $check_time = filemtime ( $cache_file );
781 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
782 file_put_contents ( $cache_file , $version , LOCK_EX
);
784 return array ( $version , $check_time );
788 * Update token for current user
790 public function updateToken ()
792 $token = Tools
:: generateToken ();
793 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
794 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
795 $currentConfig [ 'token' ] = $token ;
796 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
801 * Generate RSS feeds for current user
805 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
806 * @param string $type the type of feed to generate
807 * @param int $limit the maximum number of items (0 means all)
809 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' , $limit = 0 )
811 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
812 $config = $this- > store
-> getConfigUser ( $user_id );
814 if ( $config == null ) {
815 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
818 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
819 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
822 $feed = new FeedWriter ( RSS2
);
823 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
824 $feed- > setLink ( Tools
:: getPocheUrl ());
825 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
826 $feed- > setChannelElement ( 'generator' , 'wallabag' );
827 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
829 if ( $type == 'tag' ) {
830 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
833 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
836 // if $limit is set to zero, use all entries
838 $limit = count ( $entries );
840 if ( count ( $entries ) > 0 ) {
841 for ( $i = 0 ; $i < min ( count ( $entries ), $limit ); $i ++
) {
842 $entry = $entries [ $i ];
843 $newItem = $feed- > createNewItem ();
844 $newItem- > setTitle ( $entry [ 'title' ]);
845 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
846 $newItem- > setLink ( $entry [ 'url' ]);
847 $newItem- > setDate ( time ());
848 $newItem- > setDescription ( $entry [ 'content' ]);
849 $feed- > addItem ( $newItem );
853 $feed- > genarateFeed ();
860 * Returns new purifier object with actual config
862 private function _getPurifier ()
864 $config = HTMLPurifier_Config
:: createDefault ();
865 $config- > set ( 'Cache.SerializerPath' , CACHE
);
866 $config- > set ( 'HTML.SafeIframe' , true );
868 //allow YouTube, Vimeo and dailymotion videos
869 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
871 return new HTMLPurifier ( $config );