]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/Wallabag/Wallabag.php
c172a95d1b4ea92d6ba566775e17acba6cb5a743
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
11 namespace Wallabag\Wallabag
;
44 public function __construct ()
49 private function init ()
53 $pocheUser = Session
:: getParam ( 'poche_user' );
55 if ( $pocheUser && $pocheUser != array ()) {
56 $this- > user
= $pocheUser ;
58 // fake user, just for install & login screens
59 $this- > user
= new User ();
60 $this- > user
-> setConfig ( $this- > getDefaultConfig ());
63 $this- > pagination
= new Paginator ( $this- > user
-> getConfigValue ( 'pager' ), 'p' );
64 $this- > language
= new Language ( $this );
65 $this- > tpl
= new Template ( $this );
66 $this- > store
= new Database ();
67 $this- > messages
= new Messages ();
68 $this- > routing
= new Routing ( $this );
73 $this- > routing
-> run ();
79 public function createNewUser ( $username , $password , $email = "" )
81 if (! empty ( $username ) && ! empty ( $password )){
82 $newUsername = filter_var ( $username , FILTER_SANITIZE_STRING
);
83 $email = filter_var ( $email , FILTER_SANITIZE_STRING
);
84 if (! $this- > store
-> userExists ( $newUsername )){
85 if ( $this- > store
-> install ( $newUsername , Tools
:: encodeString ( $password . $newUsername ), $email )) {
86 Tools
:: logm ( 'The new user ' . $newUsername . ' has been installed' );
87 $this- > messages
-> add ( 's' , sprintf ( _ ( 'The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>' ), $newUsername ));
91 Tools
:: logm ( 'error during adding new user' );
96 $this- > messages
-> add ( 'e' , sprintf ( _ ( 'Error : An user with the name %s already exists !' ), $newUsername ));
97 Tools
:: logm ( 'An user with the name ' . $newUsername . ' already exists !' );
104 * Delete an existing user
106 public function deleteUser ( $password )
108 if ( $this- > store
-> listUsers () > 1 ) {
109 if ( Tools
:: encodeString ( $password . $this- > user
-> getUsername ()) == $this- > store
-> getUserPassword ( $this- > user
-> getId ())) {
110 $username = $this- > user
-> getUsername ();
111 $this- > store
-> deleteUserConfig ( $this- > user
-> getId ());
112 Tools
:: logm ( 'The configuration for user ' . $username . ' has been deleted !' );
113 $this- > store
-> deleteTagsEntriesAndEntries ( $this- > user
-> getId ());
114 Tools
:: logm ( 'The entries for user ' . $username . ' has been deleted !' );
115 $this- > store
-> deleteUser ( $this- > user
-> getId ());
116 Tools
:: logm ( 'User ' . $username . ' has been completely deleted !' );
118 Tools
:: logm ( 'logout' );
120 $this- > messages
-> add ( 's' , sprintf ( _ ( 'User %s has been successfully deleted !' ), $username ));
123 Tools
:: logm ( 'Bad password !' );
124 $this- > messages
-> add ( 'e' , _ ( 'Error : The password is wrong !' ));
128 Tools
:: logm ( 'Only user !' );
129 $this- > messages
-> add ( 'e' , _ ( 'Error : You are the only user, you cannot delete your account !' ));
133 public function getDefaultConfig ()
136 'pager' => PAGINATION
,
138 'theme' => DEFAULT_THEME
143 * Call action (mark as fav, archive, delete, etc.)
145 public function action ( $action , Url
$url , $id = 0 , $import = FALSE , $autoclose = FALSE , $tags = null )
150 $content = Tools
:: getPageContent ( $url );
151 $title = ( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' );
152 $body = $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ];
154 // clean content from prevent xss attack
155 $purifier = $this- > _getPurifier ();
156 $title = $purifier- > purify ( $title );
157 $body = $purifier- > purify ( $body );
159 //search for possible duplicate
161 $duplicate = $this- > store
-> retrieveOneByURL ( $url- > getUrl (), $this- > user
-> getId ());
163 $last_id = $this- > store
-> add ( $url- > getUrl (), $title , $body , $this- > user
-> getId ());
165 Tools
:: logm ( 'add link ' . $url- > getUrl ());
166 if ( DOWNLOAD_PICTURES
) {
167 $content = Picture
:: filterPicture ( $body , $url- > getUrl (), $last_id );
168 Tools
:: logm ( 'updating content article' );
169 $this- > store
-> updateContent ( $last_id , $content , $this- > user
-> getId ());
172 if ( $duplicate != NULL ) {
173 // 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
174 Tools
:: logm ( 'link ' . $url- > getUrl () . ' is a duplicate' );
175 // 1) - preserve tags and favorite, then drop old entry
176 $this- > store
-> reassignTags ( $duplicate [ 'id' ], $last_id );
177 if ( $duplicate [ 'is_fav' ]) {
178 $this- > store
-> favoriteById ( $last_id , $this- > user
-> getId ());
180 if ( $this- > store
-> deleteById ( $duplicate [ 'id' ], $this- > user
-> getId ())) {
181 Tools
:: logm ( 'previous link ' . $url- > getUrl () . ' entry deleted' );
185 // if there are tags, add them to the new article
186 if ( isset ( $_GET [ 'tags' ])) {
187 $_POST [ 'value' ] = $_GET [ 'tags' ];
188 $_POST [ 'entry_id' ] = $last_id ;
189 $this- > action ( 'add_tag' , $url );
192 $this- > messages
-> add ( 's' , _ ( 'the link has been added successfully' ));
195 $this- > messages
-> add ( 'e' , _ ( 'error during insertion : the link wasn \' t added' ));
196 Tools
:: logm ( 'error during insertion : the link wasn \' t added ' . $url- > getUrl ());
199 if ( $autoclose == TRUE ) {
200 Tools
:: redirect ( '?view=home' );
202 Tools
:: redirect ( '?view=home&closewin=true' );
206 if ( isset ( $_GET [ 'search' ])) {
207 //when we want to apply a delete to a search
208 $tags = array ( $_GET [ 'search' ]);
209 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
210 $entry_ids = array ();
211 foreach ( $allentry_ids as $eachentry ) {
212 $entry_ids [] = $eachentry [ 0 ];
214 } else { // delete a single article
215 $entry_ids = array ( $id );
217 foreach ( $entry_ids as $id ) {
218 $msg = 'delete link #' . $id ;
219 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
220 if ( DOWNLOAD_PICTURES
) {
221 Picture
:: removeDirectory ( ABS_PATH
. $id );
223 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
226 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
227 $msg = 'error : can \' t delete link #' . $id ;
231 Tools
:: redirect ( '?' );
234 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
235 Tools
:: logm ( 'mark as favorite link #' . $id );
236 if ( Tools
:: isAjaxRequest () ) {
244 case 'toggle_archive' :
245 if ( isset ( $_GET [ 'tag_id' ])) {
246 //when we want to archive a whole tag
247 $tag_id = $_GET [ 'tag_id' ];
248 $allentry_ids = $this- > store
-> retrieveEntriesByTag ( $tag_id , $this- > user
-> getId ());
249 $entry_ids = array ();
250 foreach ( $allentry_ids as $eachentry ) {
251 $entry_ids [] = $eachentry [ 0 ];
253 } else { //archive a single article
254 $entry_ids = array ( $id );
256 foreach ( $entry_ids as $id ) {
257 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
258 Tools
:: logm ( 'archive link #' . $id );
260 if ( Tools
:: isAjaxRequest () ) {
269 $this- > store
-> archiveAll ( $this- > user
-> getId ());
270 Tools
:: logm ( 'archive all links' );
274 if ( isset ( $_GET [ 'search' ])) {
275 //when we want to apply a tag to a search
276 $tags = array ( $_GET [ 'search' ]);
277 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
278 $entry_ids = array ();
279 foreach ( $allentry_ids as $eachentry ) {
280 $entry_ids [] = $eachentry [ 0 ];
282 } else { //add a tag to a single article
283 $tags = explode ( ',' , $_POST [ 'value' ]);
284 $entry_ids = array ( $_POST [ 'entry_id' ]);
286 foreach ( $entry_ids as $entry_id ) {
287 $entry = $this- > store
-> retrieveOneById ( $entry_id , $this- > user
-> getId ());
289 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
290 Tools
:: logm ( 'error : article not found' );
293 //get all already set tags to preven duplicates
294 $already_set_tags = array ();
295 $entry_tags = $this- > store
-> retrieveTagsByEntry ( $entry_id );
296 foreach ( $entry_tags as $tag ) {
297 $already_set_tags [] = $tag [ 'value' ];
299 foreach ( $tags as $key => $tag_value ) {
300 $value = trim ( $tag_value );
301 if ( $value && ! in_array ( $value , $already_set_tags )) {
302 $tag = $this- > store
-> retrieveTagByValue ( $value );
305 $tag = $this- > store
-> createTag ( $value );
307 if ( STORAGE
== 'postgres' ) {
308 $sequence = 'tags_id_seq' ;
310 $tag_id = $this- > store
-> getLastId ( $sequence );
313 $tag_id = $tag [ 'id' ];
316 # we assign the tag to the article
317 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
321 $this- > messages
-> add ( 's' , _ ( 'The tag has been applied successfully' ));
322 Tools
:: logm ( 'The tag has been applied successfully' );
326 $tag_id = $_GET [ 'tag_id' ];
327 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
329 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
330 Tools
:: logm ( 'error : article not found' );
333 $this- > store
-> removeTagForEntry ( $id , $tag_id );
334 Tools
:: logm ( 'tag entry deleted' );
335 if ( $this- > store
-> cleanUnusedTag ( $tag_id )) {
336 Tools
:: logm ( 'tag deleted' );
338 $this- > messages
-> add ( 's' , _ ( 'The tag has been successfully deleted' ));
346 function displayView ( $view , $id = 0 )
355 $dev_infos = $this- > _getPocheVersion ( 'dev' );
356 $dev = trim ( $dev_infos [ 0 ]);
357 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
358 $prod_infos = $this- > _getPocheVersion ( 'prod' );
359 $prod = trim ( $prod_infos [ 0 ]);
360 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
361 $compare_dev = version_compare ( POCHE
, $dev );
362 $compare_prod = version_compare ( POCHE
, $prod );
363 $themes = $this- > tpl
-> getInstalledThemes ();
364 $languages = $this- > language
-> getInstalledLanguages ();
365 $token = $this- > user
-> getConfigValue ( 'token' );
366 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
367 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
370 'languages' => $languages ,
373 'check_time_dev' => $check_time_dev ,
374 'check_time_prod' => $check_time_prod ,
375 'compare_dev' => $compare_dev ,
376 'compare_prod' => $compare_prod ,
378 'user_id' => $this- > user
-> getId (),
379 'http_auth' => $http_auth ,
380 'only_user' => $only_user
382 Tools
:: logm ( 'config view' );
386 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
388 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
389 Tools
:: logm ( 'error : article not found' );
392 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
400 $token = $this- > user
-> getConfigValue ( 'token' );
401 //if term is set - search tags for this term
402 $term = Tools
:: checkVar ( 'term' );
403 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
404 if ( Tools
:: isAjaxRequest ()) {
406 foreach ( $tags as $tag ) {
407 $result [] = $tag [ 'value' ];
409 echo json_encode ( $result );
414 'user_id' => $this- > user
-> getId (),
419 if ( isset ( $_GET [ 'search' ])) {
420 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
421 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
422 $count = count ( $tpl_vars [ 'entries' ]);
423 $this- > pagination
-> set_total ( $count );
424 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
425 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
426 $tpl_vars [ 'page_links' ] = $page_links ;
427 $tpl_vars [ 'nb_results' ] = $count ;
428 $tpl_vars [ 'searchterm' ] = $search ;
432 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
433 if ( $entry != NULL ) {
434 Tools
:: logm ( 'view link #' . $id );
435 $content = $entry [ 'content' ];
436 if ( function_exists ( 'tidy_parse_string' )) {
437 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
438 $tidy- > cleanRepair ();
439 $content = $tidy- > value
;
445 $flattr = new FlattrItem ();
446 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
450 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
454 'content' => $content ,
460 Tools
:: logm ( 'error in view call : entry is null' );
463 default : # home, favorites, archive and tag views
468 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
471 //if id is given - we retrieve entries by tag: id is tag id
473 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
474 $tpl_vars [ 'id' ] = intval ( $id );
477 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
480 $this- > pagination
-> set_total ( $count );
481 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
482 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
483 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
484 $tpl_vars [ 'page_links' ] = $page_links ;
485 $tpl_vars [ 'nb_results' ] = $count ;
487 Tools
:: logm ( 'display ' . $view . ' view' );
495 * update the password of the current user.
496 * if MODE_DEMO is TRUE, the password can't be updated.
497 * @todo add the return value
498 * @todo set the new password in function header like this updatePassword($newPassword)
501 public function updatePassword ( $password , $confirmPassword )
504 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
505 Tools
:: logm ( 'in demo mode, you can \' t do this' );
506 Tools
:: redirect ( '?view=config' );
509 if ( isset ( $password ) && isset ( $confirmPassword )) {
510 if ( $password == $confirmPassword && ! empty ( $password )) {
511 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
512 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
514 Tools
:: logm ( 'password updated' );
518 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
519 Tools
:: redirect ( '?view=config' );
526 * Get credentials from differents sources
527 * It redirects the user to the $referer link
531 private function credentials ()
533 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
534 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
536 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
537 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
539 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
540 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
543 return array ( false , false , false );
547 * checks if login & password are correct and save the user in session.
548 * it redirects the user to the $referer link
549 * @param string $referer the url to redirect after login
550 * @todo add the return value
553 public function login ( $referer )
555 list ( $login , $password , $isauthenticated )= $this- > credentials ();
556 if ( $login === false || $password === false ) {
557 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
558 Tools
:: logm ( 'login failed' );
561 if (! empty ( $login ) && ! empty ( $password )) {
562 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
563 if ( $user != array ()) {
564 # Save login into Session
565 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
566 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
567 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
570 $language = $user [ 'config' ][ 'language' ];
571 @ putenv ( 'LC_ALL=' . $language );
572 setlocale ( LC_ALL
, $language );
573 bindtextdomain ( $language , LOCALE
);
574 textdomain ( $language );
576 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
577 Tools
:: logm ( 'login successful' );
578 Tools
:: redirect ( $referer );
580 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
581 // log login failure in web server log to allow fail2ban usage
582 error_log ( 'user ' . $login . ' authentication failure' );
583 Tools
:: logm ( 'login failed' );
589 * log out the poche user. It cleans the session.
590 * @todo add the return value
593 public function logout ()
595 $this- > user
= array ();
597 Tools
:: logm ( 'logout' );
602 * import datas into your wallabag
606 public function import () {
608 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
609 Tools
:: logm ( 'Import stated: parsing file' );
611 // assume, that file is in json format
612 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
613 $data = json_decode ( $str_data , true );
615 if ( $data === null ) {
616 //not json - assume html
617 $html = new simple_html_dom ();
618 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
621 foreach ( array ( 'ol' , 'ul' ) as $list ) {
622 foreach ( $html- > find ( $list ) as $ul ) {
623 foreach ( $ul- > find ( 'li' ) as $li ) {
626 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
627 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
628 $tmpEntry [ 'is_read' ] = $read ;
629 if ( $tmpEntry [ 'url' ]) {
633 # the second <ol/ul> is for read links
634 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
639 // for readability structure
641 foreach ( $data as $record ) {
642 if ( is_array ( $record )) {
644 foreach ( $record as $record2 ) {
645 if ( is_array ( $record2 )) {
652 $urlsInserted = array (); //urls of articles inserted
653 foreach ( $data as $record ) {
654 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
655 if ( $url and ! in_array ( $url , $urlsInserted )) {
656 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
657 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
658 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
659 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
663 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
665 $urlsInserted [] = $url ; //add
666 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
675 $i = sizeof ( $urlsInserted );
677 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
680 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
683 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
685 // file parsing finished here
686 // now download article contents if any
687 // check if we need to download any content
689 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
691 if ( $recordsDownloadRequired == 0 ) {
693 // nothing to download
695 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
696 Tools
:: logm ( 'Import finished completely' );
701 // if just inserted - don't download anything, download will start in next reload
703 if (! isset ( $_FILES [ 'file' ])) {
705 // download next batch
707 Tools
:: logm ( 'Fetching next batch of articles...' );
708 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
709 $purifier = $this- > _getPurifier ();
710 foreach ( $items as $item ) {
711 $url = new Url ( base64_encode ( $item [ 'url' ]));
712 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
713 $content = Tools
:: getPageContent ( $url );
714 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
715 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
717 // clean content to prevent xss attack
719 $title = $purifier- > purify ( $title );
720 $body = $purifier- > purify ( $body );
721 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
722 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
728 'includeImport' => true ,
730 'recordsDownloadRequired' => $recordsDownloadRequired ,
731 'recordsUnderDownload' => IMPORT_LIMIT
,
732 'delay' => IMPORT_DELAY
* 1000
738 * export poche entries in json
739 * @return json all poche entries
741 public function export ()
743 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
744 header ( 'Content-Disposition: attachment; filename=' . $filename );
746 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
747 echo $this- > tpl
-> render ( 'export.twig' , array (
748 'export' => Tools
:: renderJson ( $entries ),
750 Tools
:: logm ( 'export view' );
754 * Checks online the latest version of poche and cache it
755 * @param string $which 'prod' or 'dev'
756 * @return string latest $which version
758 private function _getPocheVersion ( $which = 'prod' ) {
759 $cache_file = CACHE
. '/' . $which ;
760 $check_time = time ();
762 # checks if the cached version file exists
763 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
764 $version = file_get_contents ( $cache_file );
765 $check_time = filemtime ( $cache_file );
767 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
768 file_put_contents ( $cache_file , $version , LOCK_EX
);
770 return array ( $version , $check_time );
774 * Update token for current user
776 public function updateToken ()
778 $token = Tools
:: generateToken ();
779 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
780 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
781 $currentConfig [ 'token' ] = $token ;
782 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
787 * Generate RSS feeds for current user
791 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
792 * @param string $type the type of feed to generate
793 * @param int $limit the maximum number of items (0 means all)
795 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' , $limit = 0 )
797 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
798 $config = $this- > store
-> getConfigUser ( $user_id );
800 if ( $config == null ) {
801 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
804 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
805 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
808 $feed = new FeedWriter ( RSS2
);
809 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
810 $feed- > setLink ( Tools
:: getPocheUrl ());
811 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
812 $feed- > setChannelElement ( 'generator' , 'wallabag' );
813 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
815 if ( $type == 'tag' ) {
816 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
819 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
822 // if $limit is set to zero, use all entries
824 $limit = count ( $entries );
826 if ( count ( $entries ) > 0 ) {
827 for ( $i = 0 ; $i < min ( count ( $entries ), $limit ); $i ++
) {
828 $entry = $entries [ $i ];
829 $newItem = $feed- > createNewItem ();
830 $newItem- > setTitle ( $entry [ 'title' ]);
831 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
832 $newItem- > setLink ( $entry [ 'url' ]);
833 $newItem- > setDate ( time ());
834 $newItem- > setDescription ( $entry [ 'content' ]);
835 $feed- > addItem ( $newItem );
839 $feed- > genarateFeed ();
846 * Returns new purifier object with actual config
848 private function _getPurifier ()
850 $config = HTMLPurifier_Config
:: createDefault ();
851 $config- > set ( 'Cache.SerializerPath' , CACHE
);
852 $config- > set ( 'HTML.SafeIframe' , true );
854 //allow YouTube, Vimeo and dailymotion videos
855 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
857 return new HTMLPurifier ( $config );