]>
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' );
204 if ( isset ( $_GET [ 'search' ])) {
205 //when we want to apply a delete to a search
206 $tags = array ( $_GET [ 'search' ]);
207 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
208 $entry_ids = array ();
209 foreach ( $allentry_ids as $eachentry ) {
210 $entry_ids [] = $eachentry [ 0 ];
212 } else { // delete a single article
213 $entry_ids = array ( $id );
215 foreach ( $entry_ids as $id ) {
216 $msg = 'delete link #' . $id ;
217 if ( $this- > store
-> deleteById ( $id , $this- > user
-> getId ())) {
218 if ( DOWNLOAD_PICTURES
) {
219 Picture
:: removeDirectory ( ABS_PATH
. $id );
221 $this- > messages
-> add ( 's' , _ ( 'the link has been deleted successfully' ));
224 $this- > messages
-> add ( 'e' , _ ( 'the link wasn \' t deleted' ));
225 $msg = 'error : can \' t delete link #' . $id ;
229 Tools
:: redirect ( '?' );
232 $this- > store
-> favoriteById ( $id , $this- > user
-> getId ());
233 Tools
:: logm ( 'mark as favorite link #' . $id );
234 if ( Tools
:: isAjaxRequest () ) {
242 case 'toggle_archive' :
243 if ( isset ( $_GET [ 'tag_id' ])) {
244 //when we want to archive a whole tag
245 $tag_id = $_GET [ 'tag_id' ];
246 $allentry_ids = $this- > store
-> retrieveEntriesByTag ( $tag_id , $this- > user
-> getId ());
247 $entry_ids = array ();
248 foreach ( $allentry_ids as $eachentry ) {
249 $entry_ids [] = $eachentry [ 0 ];
251 } else { //archive a single article
252 $entry_ids = array ( $id );
254 foreach ( $entry_ids as $id ) {
255 $this- > store
-> archiveById ( $id , $this- > user
-> getId ());
256 Tools
:: logm ( 'archive link #' . $id );
258 if ( Tools
:: isAjaxRequest () ) {
267 $this- > store
-> archiveAll ( $this- > user
-> getId ());
268 Tools
:: logm ( 'archive all links' );
272 if ( isset ( $_GET [ 'search' ])) {
273 //when we want to apply a tag to a search
274 $tags = array ( $_GET [ 'search' ]);
275 $allentry_ids = $this- > store
-> search ( $tags [ 0 ], $this- > user
-> getId ());
276 $entry_ids = array ();
277 foreach ( $allentry_ids as $eachentry ) {
278 $entry_ids [] = $eachentry [ 0 ];
280 } else { //add a tag to a single article
281 $tags = explode ( ',' , $_POST [ 'value' ]);
282 $entry_ids = array ( $_POST [ 'entry_id' ]);
284 foreach ( $entry_ids as $entry_id ) {
285 $entry = $this- > store
-> retrieveOneById ( $entry_id , $this- > user
-> getId ());
287 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
288 Tools
:: logm ( 'error : article not found' );
291 //get all already set tags to preven duplicates
292 $already_set_tags = array ();
293 $entry_tags = $this- > store
-> retrieveTagsByEntry ( $entry_id );
294 foreach ( $entry_tags as $tag ) {
295 $already_set_tags [] = $tag [ 'value' ];
297 foreach ( $tags as $key => $tag_value ) {
298 $value = trim ( $tag_value );
299 if ( $value && ! in_array ( $value , $already_set_tags )) {
300 $tag = $this- > store
-> retrieveTagByValue ( $value );
303 $tag = $this- > store
-> createTag ( $value );
305 if ( STORAGE
== 'postgres' ) {
306 $sequence = 'tags_id_seq' ;
308 $tag_id = $this- > store
-> getLastId ( $sequence );
311 $tag_id = $tag [ 'id' ];
314 # we assign the tag to the article
315 $this- > store
-> setTagToEntry ( $tag_id , $entry_id );
319 $this- > messages
-> add ( 's' , _ ( 'The tag has been applied successfully' ));
320 Tools
:: logm ( 'The tag has been applied successfully' );
324 $tag_id = $_GET [ 'tag_id' ];
325 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
327 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
328 Tools
:: logm ( 'error : article not found' );
331 $this- > store
-> removeTagForEntry ( $id , $tag_id );
332 Tools
:: logm ( 'tag entry deleted' );
333 if ( $this- > store
-> cleanUnusedTag ( $tag_id )) {
334 Tools
:: logm ( 'tag deleted' );
336 $this- > messages
-> add ( 's' , _ ( 'The tag has been successfully deleted' ));
344 function displayView ( $view , $id = 0 )
353 $dev_infos = $this- > _getPocheVersion ( 'dev' );
354 $dev = trim ( $dev_infos [ 0 ]);
355 $check_time_dev = date ( 'd-M-Y H:i' , $dev_infos [ 1 ]);
356 $prod_infos = $this- > _getPocheVersion ( 'prod' );
357 $prod = trim ( $prod_infos [ 0 ]);
358 $check_time_prod = date ( 'd-M-Y H:i' , $prod_infos [ 1 ]);
359 $compare_dev = version_compare ( POCHE
, $dev );
360 $compare_prod = version_compare ( POCHE
, $prod );
361 $themes = $this- > tpl
-> getInstalledThemes ();
362 $languages = $this- > language
-> getInstalledLanguages ();
363 $token = $this- > user
-> getConfigValue ( 'token' );
364 $http_auth = ( isset ( $_SERVER [ 'PHP_AUTH_USER' ]) || isset ( $_SERVER [ 'REMOTE_USER' ])) ? true : false ;
365 $only_user = ( $this- > store
-> listUsers () > 1 ) ? false : true ;
368 'languages' => $languages ,
371 'check_time_dev' => $check_time_dev ,
372 'check_time_prod' => $check_time_prod ,
373 'compare_dev' => $compare_dev ,
374 'compare_prod' => $compare_prod ,
376 'user_id' => $this- > user
-> getId (),
377 'http_auth' => $http_auth ,
378 'only_user' => $only_user
380 Tools
:: logm ( 'config view' );
384 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
386 $this- > messages
-> add ( 'e' , _ ( 'Article not found!' ));
387 Tools
:: logm ( 'error : article not found' );
390 $tags = $this- > store
-> retrieveTagsByEntry ( $id );
398 $token = $this- > user
-> getConfigValue ( 'token' );
399 //if term is set - search tags for this term
400 $term = Tools
:: checkVar ( 'term' );
401 $tags = $this- > store
-> retrieveAllTags ( $this- > user
-> getId (), $term );
402 if ( Tools
:: isAjaxRequest ()) {
404 foreach ( $tags as $tag ) {
405 $result [] = $tag [ 'value' ];
407 echo json_encode ( $result );
412 'user_id' => $this- > user
-> getId (),
417 if ( isset ( $_GET [ 'search' ])) {
418 $search = filter_var ( $_GET [ 'search' ], FILTER_SANITIZE_STRING
);
419 $tpl_vars [ 'entries' ] = $this- > store
-> search ( $search , $this- > user
-> getId ());
420 $count = count ( $tpl_vars [ 'entries' ]);
421 $this- > pagination
-> set_total ( $count );
422 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
423 $this- > pagination
-> page_links ( '?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION [ 'sort' ] . '&' ));
424 $tpl_vars [ 'page_links' ] = $page_links ;
425 $tpl_vars [ 'nb_results' ] = $count ;
426 $tpl_vars [ 'searchterm' ] = $search ;
430 $entry = $this- > store
-> retrieveOneById ( $id , $this- > user
-> getId ());
431 if ( $entry != NULL ) {
432 Tools
:: logm ( 'view link #' . $id );
433 $content = $entry [ 'content' ];
434 if ( function_exists ( 'tidy_parse_string' )) {
435 $tidy = tidy_parse_string ( $content , array ( 'indent' => true , 'show-body-only' => true ), 'UTF8' );
436 $tidy- > cleanRepair ();
437 $content = $tidy- > value
;
443 $flattr = new FlattrItem ();
444 $flattr- > checkItem ( $entry [ 'url' ], $entry [ 'id' ]);
448 $tags = $this- > store
-> retrieveTagsByEntry ( $entry [ 'id' ]);
452 'content' => $content ,
458 Tools
:: logm ( 'error in view call : entry is null' );
461 default : # home, favorites, archive and tag views
466 'listmode' => ( isset ( $_COOKIE [ 'listmode' ]) ? true : false ),
469 //if id is given - we retrieve entries by tag: id is tag id
471 $tpl_vars [ 'tag' ] = $this- > store
-> retrieveTag ( $id , $this- > user
-> getId ());
472 $tpl_vars [ 'id' ] = intval ( $id );
475 $count = $this- > store
-> getEntriesByViewCount ( $view , $this- > user
-> getId (), $id );
478 $this- > pagination
-> set_total ( $count );
479 $page_links = str_replace ( array ( 'previous' , 'next' ), array ( _ ( 'previous' ), _ ( 'next' )),
480 $this- > pagination
-> page_links ( '?view=' . $view . '&sort=' . $_SESSION [ 'sort' ] . (( $id )? '&id=' . $id : '' ) . '&' ));
481 $tpl_vars [ 'entries' ] = $this- > store
-> getEntriesByView ( $view , $this- > user
-> getId (), $this- > pagination
-> get_limit (), $id );
482 $tpl_vars [ 'page_links' ] = $page_links ;
483 $tpl_vars [ 'nb_results' ] = $count ;
485 Tools
:: logm ( 'display ' . $view . ' view' );
493 * update the password of the current user.
494 * if MODE_DEMO is TRUE, the password can't be updated.
495 * @todo add the return value
496 * @todo set the new password in function header like this updatePassword($newPassword)
499 public function updatePassword ( $password , $confirmPassword )
502 $this- > messages
-> add ( 'i' , _ ( 'in demo mode, you can \' t update your password' ));
503 Tools
:: logm ( 'in demo mode, you can \' t do this' );
504 Tools
:: redirect ( '?view=config' );
507 if ( isset ( $password ) && isset ( $confirmPassword )) {
508 if ( $password == $confirmPassword && ! empty ( $password )) {
509 $this- > messages
-> add ( 's' , _ ( 'your password has been updated' ));
510 $this- > store
-> updatePassword ( $this- > user
-> getId (), Tools
:: encodeString ( $password . $this- > user
-> getUsername ()));
512 Tools
:: logm ( 'password updated' );
516 $this- > messages
-> add ( 'e' , _ ( 'the two fields have to be filled & the password must be the same in the two fields' ));
517 Tools
:: redirect ( '?view=config' );
524 * Get credentials from differents sources
525 * It redirects the user to the $referer link
529 private function credentials ()
531 if ( isset ( $_SERVER [ 'PHP_AUTH_USER' ])) {
532 return array ( $_SERVER [ 'PHP_AUTH_USER' ], 'php_auth' , true );
534 if (! empty ( $_POST [ 'login' ]) && ! empty ( $_POST [ 'password' ])) {
535 return array ( $_POST [ 'login' ], $_POST [ 'password' ], false );
537 if ( isset ( $_SERVER [ 'REMOTE_USER' ])) {
538 return array ( $_SERVER [ 'REMOTE_USER' ], 'http_auth' , true );
541 return array ( false , false , false );
545 * checks if login & password are correct and save the user in session.
546 * it redirects the user to the $referer link
547 * @param string $referer the url to redirect after login
548 * @todo add the return value
551 public function login ( $referer )
553 list ( $login , $password , $isauthenticated )= $this- > credentials ();
554 if ( $login === false || $password === false ) {
555 $this- > messages
-> add ( 'e' , _ ( 'login failed: you have to fill all fields' ));
556 Tools
:: logm ( 'login failed' );
559 if (! empty ( $login ) && ! empty ( $password )) {
560 $user = $this- > store
-> login ( $login , Tools
:: encodeString ( $password . $login ), $isauthenticated );
561 if ( $user != array ()) {
562 # Save login into Session
563 $longlastingsession = isset ( $_POST [ 'longlastingsession' ]);
564 $passwordTest = ( $isauthenticated ) ? $user [ 'password' ] : Tools
:: encodeString ( $password . $login );
565 Session
:: login ( $user [ 'username' ], $user [ 'password' ], $login , $passwordTest , $longlastingsession , array ( 'poche_user' => new User ( $user )));
568 $language = $user [ 'config' ][ 'language' ];
569 @ putenv ( 'LC_ALL=' . $language );
570 setlocale ( LC_ALL
, $language );
571 bindtextdomain ( $language , LOCALE
);
572 textdomain ( $language );
574 $this- > messages
-> add ( 's' , _ ( 'welcome to your wallabag' ));
575 Tools
:: logm ( 'login successful' );
576 Tools
:: redirect ( $referer );
578 $this- > messages
-> add ( 'e' , _ ( 'login failed: bad login or password' ));
579 Tools
:: logm ( 'login failed' );
585 * log out the poche user. It cleans the session.
586 * @todo add the return value
589 public function logout ()
591 $this- > user
= array ();
593 Tools
:: logm ( 'logout' );
598 * import datas into your wallabag
602 public function import () {
604 if ( isset ( $_FILES [ 'file' ]) && $_FILES [ 'file' ][ 'tmp_name' ] ) {
605 Tools
:: logm ( 'Import stated: parsing file' );
607 // assume, that file is in json format
608 $str_data = file_get_contents ( $_FILES [ 'file' ][ 'tmp_name' ]);
609 $data = json_decode ( $str_data , true );
611 if ( $data === null ) {
612 //not json - assume html
613 $html = new simple_html_dom ();
614 $html- > load_file ( $_FILES [ 'file' ][ 'tmp_name' ]);
617 foreach ( array ( 'ol' , 'ul' ) as $list ) {
618 foreach ( $html- > find ( $list ) as $ul ) {
619 foreach ( $ul- > find ( 'li' ) as $li ) {
622 $tmpEntry [ 'url' ] = $a [ 0 ]-> href
;
623 $tmpEntry [ 'tags' ] = $a [ 0 ]-> tags
;
624 $tmpEntry [ 'is_read' ] = $read ;
625 if ( $tmpEntry [ 'url' ]) {
629 # the second <ol/ul> is for read links
630 $read = (( sizeof ( $data ) && $read )? 0 : 1 );
635 // for readability structure
637 foreach ( $data as $record ) {
638 if ( is_array ( $record )) {
640 foreach ( $record as $record2 ) {
641 if ( is_array ( $record2 )) {
648 $urlsInserted = array (); //urls of articles inserted
649 foreach ( $data as $record ) {
650 $url = trim ( isset ( $record [ 'article__url' ]) ? $record [ 'article__url' ] : ( isset ( $record [ 'url' ]) ? $record [ 'url' ] : '' ));
651 if ( $url and ! in_array ( $url , $urlsInserted )) {
652 $title = ( isset ( $record [ 'title' ]) ? $record [ 'title' ] : _ ( 'Untitled - Import - ' ) . '</a> <a href="./?import">' . _ ( 'click to finish import' ) . '</a><a>' );
653 $body = ( isset ( $record [ 'content' ]) ? $record [ 'content' ] : '' );
654 $isRead = ( isset ( $record [ 'is_read' ]) ? intval ( $record [ 'is_read' ]) : ( isset ( $record [ 'archive' ]) ? intval ( $record [ 'archive' ]) : 0 ));
655 $isFavorite = ( isset ( $record [ 'is_fav' ]) ? intval ( $record [ 'is_fav' ]) : ( isset ( $record [ 'favorite' ]) ? intval ( $record [ 'favorite' ]) : 0 ));
659 $id = $this- > store
-> add ( $url , $title , $body , $this- > user
-> getId () , $isFavorite , $isRead );
661 $urlsInserted [] = $url ; //add
662 if ( isset ( $record [ 'tags' ]) && trim ( $record [ 'tags' ])) {
671 $i = sizeof ( $urlsInserted );
673 $this- > messages
-> add ( 's' , _ ( 'Articles inserted: ' ) . $i . _ ( '. Please note, that some may be marked as "read".' ));
676 Tools
:: logm ( 'Import of articles finished: ' . $i . ' articles added (w/o content if not provided).' );
679 $this- > messages
-> add ( 's' , _ ( 'Did you forget to select a file?' ));
681 // file parsing finished here
682 // now download article contents if any
683 // check if we need to download any content
685 $recordsDownloadRequired = $this- > store
-> retrieveUnfetchedEntriesCount ( $this- > user
-> getId ());
687 if ( $recordsDownloadRequired == 0 ) {
689 // nothing to download
691 $this- > messages
-> add ( 's' , _ ( 'Import finished.' ));
692 Tools
:: logm ( 'Import finished completely' );
697 // if just inserted - don't download anything, download will start in next reload
699 if (! isset ( $_FILES [ 'file' ])) {
701 // download next batch
703 Tools
:: logm ( 'Fetching next batch of articles...' );
704 $items = $this- > store
-> retrieveUnfetchedEntries ( $this- > user
-> getId () , IMPORT_LIMIT
);
705 $purifier = $this- > _getPurifier ();
706 foreach ( $items as $item ) {
707 $url = new Url ( base64_encode ( $item [ 'url' ]));
708 Tools
:: logm ( 'Fetching article ' . $item [ 'id' ]);
709 $content = Tools
:: getPageContent ( $url );
710 $title = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'title' ] : _ ( 'Untitled' ));
711 $body = (( $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] != '' ) ? $content [ 'rss' ][ 'channel' ][ 'item' ][ 'description' ] : _ ( 'Undefined' ));
713 // clean content to prevent xss attack
715 $title = $purifier- > purify ( $title );
716 $body = $purifier- > purify ( $body );
717 $this- > store
-> updateContentAndTitle ( $item [ 'id' ], $title , $body , $this- > user
-> getId ());
718 Tools
:: logm ( 'Article ' . $item [ 'id' ] . ' updated.' );
724 'includeImport' => true ,
726 'recordsDownloadRequired' => $recordsDownloadRequired ,
727 'recordsUnderDownload' => IMPORT_LIMIT
,
728 'delay' => IMPORT_DELAY
* 1000
734 * export poche entries in json
735 * @return json all poche entries
737 public function export ()
739 $filename = "wallabag-export-" . $this- > user
-> getId (). "-" . date ( "Y-m-d" ). ".json" ;
740 header ( 'Content-Disposition: attachment; filename=' . $filename );
742 $entries = $this- > store
-> retrieveAll ( $this- > user
-> getId ());
743 echo $this- > tpl
-> render ( 'export.twig' , array (
744 'export' => Tools
:: renderJson ( $entries ),
746 Tools
:: logm ( 'export view' );
750 * Checks online the latest version of poche and cache it
751 * @param string $which 'prod' or 'dev'
752 * @return string latest $which version
754 private function _getPocheVersion ( $which = 'prod' ) {
755 $cache_file = CACHE
. '/' . $which ;
756 $check_time = time ();
758 # checks if the cached version file exists
759 if ( file_exists ( $cache_file ) && ( filemtime ( $cache_file ) > ( time () - 86400 ))) {
760 $version = file_get_contents ( $cache_file );
761 $check_time = filemtime ( $cache_file );
763 $version = file_get_contents ( 'http://static.wallabag.org/versions/' . $which );
764 file_put_contents ( $cache_file , $version , LOCK_EX
);
766 return array ( $version , $check_time );
770 * Update token for current user
772 public function updateToken ()
774 $token = Tools
:: generateToken ();
775 $this- > store
-> updateUserConfig ( $this- > user
-> getId (), 'token' , $token );
776 $currentConfig = $_SESSION [ 'poche_user' ]-> config
;
777 $currentConfig [ 'token' ] = $token ;
778 $_SESSION [ 'poche_user' ]-> setConfig ( $currentConfig );
783 * Generate RSS feeds for current user
788 * @param string $type
790 public function generateFeeds ( $token , $user_id , $tag_id , $type = 'home' )
792 $allowed_types = array ( 'home' , 'fav' , 'archive' , 'tag' );
793 $config = $this- > store
-> getConfigUser ( $user_id );
795 if ( $config == null ) {
796 die ( sprintf ( _ ( 'User with this id (%d) does not exist.' ), $user_id ));
799 if (! in_array ( $type , $allowed_types ) || ! isset ( $config [ 'token' ]) || $token != $config [ 'token' ]) {
800 die ( _ ( 'Uh, there is a problem while generating feed. Wrong token used?' ));
803 $feed = new FeedWriter ( RSS2
);
804 $feed- > setTitle ( 'wallabag — ' . $type . ' feed' );
805 $feed- > setLink ( Tools
:: getPocheUrl ());
806 $feed- > setChannelElement ( 'pubDate' , date ( DATE_RSS
, time ()));
807 $feed- > setChannelElement ( 'generator' , 'wallabag' );
808 $feed- > setDescription ( 'wallabag ' . $type . ' elements' );
810 if ( $type == 'tag' ) {
811 $entries = $this- > store
-> retrieveEntriesByTag ( $tag_id , $user_id );
814 $entries = $this- > store
-> getEntriesByView ( $type , $user_id );
817 if ( count ( $entries ) > 0 ) {
818 foreach ( $entries as $entry ) {
819 $newItem = $feed- > createNewItem ();
820 $newItem- > setTitle ( $entry [ 'title' ]);
821 $newItem- > setSource ( Tools
:: getPocheUrl () . '?view=view&id=' . $entry [ 'id' ]);
822 $newItem- > setLink ( $entry [ 'url' ]);
823 $newItem- > setDate ( time ());
824 $newItem- > setDescription ( $entry [ 'content' ]);
825 $feed- > addItem ( $newItem );
829 $feed- > genarateFeed ();
836 * Returns new purifier object with actual config
838 private function _getPurifier ()
840 $config = HTMLPurifier_Config
:: createDefault ();
841 $config- > set ( 'Cache.SerializerPath' , CACHE
);
842 $config- > set ( 'HTML.SafeIframe' , true );
844 //allow YouTube, Vimeo and dailymotion videos
845 $config- > set ( 'URI.SafeIframeRegexp' , '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%' );
847 return new HTMLPurifier ( $config );