3 * wallabag, self hostable application allowing you to not miss any content anymore
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
8 * @license http://opensource.org/licenses/MIT see COPYING file
42 public function __construct()
47 private function init()
51 $pocheUser = Session
::getParam('poche_user');
53 if ($pocheUser && $pocheUser != array()) {
54 $this->user
= $pocheUser;
56 // fake user, just for install & login screens
57 $this->user
= new User();
58 $this->user
->setConfig($this->getDefaultConfig());
61 $this->pagination
= new Paginator($this->user
->getConfigValue('pager'), 'p');
62 $this->language
= new Language($this);
63 $this->tpl
= new Template($this);
64 $this->store
= new Database();
65 $this->messages
= new Messages();
66 $this->routing
= new Routing($this);
71 $this->routing
->run();
77 public function createNewUser($username, $password, $email = "", $internalRegistration = false)
79 if (!empty($username) && !empty($password)){
80 $newUsername = filter_var($username, FILTER_SANITIZE_STRING
);
81 $email = filter_var($email, FILTER_SANITIZE_STRING
);
82 if (!$this->store
->userExists($newUsername)){
83 if ($this->store
->install($newUsername, Tools
::encodeString($password . $newUsername), $email)) {
84 if ($email != "") { // if email is filled
85 if (SEND_CONFIRMATION_EMAIL
&& function_exists('mail')) {
87 // if internal registration
88 $body_internal = _('Hi,') . "\r\n\r\n" . sprintf(_('Someone just created a wallabag account for you on %1$s.'), Tools
::getPocheUrl()) .
89 "\r\n\r\n" . sprintf(_('Your login is %1$s.'), $newUsername) ."\r\n\r\n" .
90 _('Note : The password has been chosen by the person who created your account. Get in touch with that person to know your password and change it as soon as possible') . "\r\n\r\n" .
91 _('Have fun with it !') . "\r\n\r\n" .
92 _('This is an automatically generated message, no one will answer if you respond to it.');
94 // if external (public) registration
95 $body = "Hi, " . $newUsername . "\r\n\r\nYou've just created a wallabag account on " . Tools
::getPocheUrl() . ".\r\nHave fun with it !";
96 $body = $internalRegistration ? $body_internal : $body;
98 $body = wordwrap($body, 70, "\r\n"); // cut lines with more than 70 caracters (MIME standard)
99 if (mail($email, sprintf(_('Your new wallabag account on %1$s'), Tools::getPocheUrl()), $body,
100 'X
-Mailer
: PHP
/' . phpversion() . "\r\n" .
101 'Content
-type
: text
/plain
; charset
=UTF
-8' . "\r\n" .
102 "From: " . $newUsername . "@" . gethostname() . "\r\n")) {
103 Tools::logm('The user
' . $newUsername . ' has been emailed
');
104 $this->messages->add('i
', sprintf(_('The
new user %
1$s has been sent an email at %
2$s. You may have to check spam folder
.'), $newUsername, $email));
107 Tools::logm('A problem has been encountered
while sending an email
');
108 $this->messages->add('e
', _('A problem has been encountered
while sending an email
'));
111 Tools::logm('The user has been created
, but the server did not authorize sending emails
');
112 $this->messages->add('i
', _('The server did not authorize sending a confirmation email
'));
115 Tools::logm('The user has been created
, but no email was saved
, so no confimation email was sent
');
116 $this->messages->add('i
', _('The user was created
, but no email was sent because email was not filled in
'));
118 Tools::logm('The
new user
' . $newUsername . ' has been installed
');
119 $this->messages->add('s
', sprintf(_('The
new user %s has been installed
. Do you want to
<a href
="?logout">logout
?</a
>'), $newUsername));
123 Tools::logm('error during adding
new user
');
128 $this->messages->add('e
', sprintf(_('Error
: An user with the name %s already exists
!'), $newUsername));
129 Tools::logm('An user with the name
' . $newUsername . ' already exists
!');
136 * Delete an existing user
138 public function deleteUser($password)
140 if ($this->store->listUsers() > 1) {
141 if (Tools::encodeString($password . $this->user->getUsername()) == $this->store->getUserPassword($this->user->getId())) {
142 $username = $this->user->getUsername();
143 $this->store->deleteUserConfig($this->user->getId());
144 Tools::logm('The configuration
for user
'. $username .' has been deleted
!');
145 $this->store->deleteTagsEntriesAndEntries($this->user->getId());
146 Tools::logm('The entries
for user
'. $username .' has been deleted
!');
147 $this->store->deleteUser($this->user->getId());
148 Tools::logm('User
'. $username .' has been completely deleted
!');
150 Tools::logm('logout
');
152 $this->messages->add('s
', sprintf(_('User %s has been successfully deleted
!'), $username));
155 Tools::logm('Bad password
!');
156 $this->messages->add('e
', _('Error
: The password is wrong
!'));
160 Tools::logm('Only user
!');
161 $this->messages->add('e
', _('Error
: You are the only user
, you cannot delete your account
!'));
165 public function getDefaultConfig()
168 'pager
' => PAGINATION,
170 'theme
' => DEFAULT_THEME
175 * Call action (mark as fav, archive, delete, etc.)
177 public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null)
182 $content = Tools::getPageContent($url);
183 $title = ($content['rss
']['channel
']['item
']['title
'] != '') ? $content['rss
']['channel
']['item
']['title
'] : _('Untitled
');
184 $body = $content['rss
']['channel
']['item
']['description
'];
186 // clean content from prevent xss attack
187 $purifier = $this->_getPurifier();
188 $title = $purifier->purify($title);
189 $body = $purifier->purify($body);
191 //search for possible duplicate
193 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
195 $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId());
197 Tools::logm('add link
' . $url->getUrl());
198 if (DOWNLOAD_PICTURES) {
199 $content = Picture::filterPicture($body, $url->getUrl(), $last_id);
200 Tools::logm('updating content article
');
201 $this->store->updateContent($last_id, $content, $this->user->getId());
204 if ($duplicate != NULL) {
205 // 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
206 Tools::logm('link
' . $url->getUrl() . ' is a duplicate
');
207 // 1) - preserve tags and favorite, then drop old entry
208 $this->store->reassignTags($duplicate['id
'], $last_id);
209 if ($duplicate['is_fav
']) {
210 $this->store->favoriteById($last_id, $this->user->getId());
212 if ($this->store->deleteById($duplicate['id
'], $this->user->getId())) {
213 Tools::logm('previous link
' . $url->getUrl() .' entry deleted
');
217 // if there are tags, add them to the new article
218 if (isset($_GET['tags
'])) {
219 $_POST['value
'] = $_GET['tags
'];
220 $_POST['entry_id
'] = $last_id;
221 $this->action('add_tag
', $url);
224 $this->messages->add('s
', _('the link has been added successfully
'));
227 $this->messages->add('e
', _('error during insertion
: the link wasn\'t added
'));
228 Tools::logm('error during insertion
: the link wasn\'t added
' . $url->getUrl());
231 if ($autoclose == TRUE) {
232 Tools::redirect('?view
=home
&closewin
=true');
234 Tools::redirect('?view
=home
');
239 if (isset($_GET['search
'])) {
240 //when we want to apply a delete to a search
241 $tags = array($_GET['search
']);
242 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
243 $entry_ids = array();
244 foreach ($allentry_ids as $eachentry) {
245 $entry_ids[] = $eachentry[0];
247 } else { // delete a single article
248 $entry_ids = array($id);
250 foreach($entry_ids as $id) {
251 $msg = 'delete link
#' . $id;
252 if ($this->store
->deleteById($id, $this->user
->getId())) {
253 if (DOWNLOAD_PICTURES
) {
254 Picture
::removeDirectory(ABS_PATH
. $id);
256 $this->messages
->add('s', _('the link has been deleted successfully'));
259 $this->messages
->add('e', _('the link wasn\'t deleted'));
260 $msg = 'error : can\'t delete link #' . $id;
264 Tools
::redirect('?');
267 $this->store
->favoriteById($id, $this->user
->getId());
268 Tools
::logm('mark as favorite link #' . $id);
269 if ( Tools
::isAjaxRequest() ) {
277 case 'toggle_archive' :
278 if (isset($_GET['tag_id'])) {
279 //when we want to archive a whole tag
280 $tag_id = $_GET['tag_id'];
281 $allentry_ids = $this->store
->retrieveEntriesByTag($tag_id, $this->user
->getId());
282 $entry_ids = array();
283 foreach ($allentry_ids as $eachentry) {
284 $entry_ids[] = $eachentry[0];
286 } else { //archive a single article
287 $entry_ids = array($id);
289 foreach($entry_ids as $id) {
290 $this->store
->archiveById($id, $this->user
->getId());
291 Tools
::logm('archive link #' . $id);
293 if ( Tools
::isAjaxRequest() ) {
302 $this->store
->archiveAll($this->user
->getId());
303 Tools
::logm('archive all links');
307 if (isset($_GET['search'])) {
308 //when we want to apply a tag to a search
309 $tags = array($_GET['search']);
310 $allentry_ids = $this->store
->search($tags[0], $this->user
->getId());
311 $entry_ids = array();
312 foreach ($allentry_ids as $eachentry) {
313 $entry_ids[] = $eachentry[0];
315 } else { //add a tag to a single article
316 $tags = explode(',', $_POST['value']);
317 $entry_ids = array($_POST['entry_id']);
319 foreach($entry_ids as $entry_id) {
320 $entry = $this->store
->retrieveOneById($entry_id, $this->user
->getId());
322 $this->messages
->add('e', _('Article not found!'));
323 Tools
::logm('error : article not found');
326 //get all already set tags to preven duplicates
327 $already_set_tags = array();
328 $entry_tags = $this->store
->retrieveTagsByEntry($entry_id);
329 foreach ($entry_tags as $tag) {
330 $already_set_tags[] = $tag['value'];
332 foreach($tags as $key => $tag_value) {
333 $value = trim($tag_value);
334 if ($value && !in_array($value, $already_set_tags)) {
335 $tag = $this->store
->retrieveTagByValue($value);
338 $tag = $this->store
->createTag($value);
340 if (STORAGE
== 'postgres') {
341 $sequence = 'tags_id_seq';
343 $tag_id = $this->store
->getLastId($sequence);
346 $tag_id = $tag['id'];
349 # we assign the tag to the article
350 $this->store
->setTagToEntry($tag_id, $entry_id);
354 $this->messages
->add('s', _('The tag has been applied successfully'));
355 Tools
::logm('The tag has been applied successfully');
359 $tag_id = $_GET['tag_id'];
360 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
362 $this->messages
->add('e', _('Article not found!'));
363 Tools
::logm('error : article not found');
366 $this->store
->removeTagForEntry($id, $tag_id);
367 Tools
::logm('tag entry deleted');
368 if ($this->store
->cleanUnusedTag($tag_id)) {
369 Tools
::logm('tag deleted');
371 $this->messages
->add('s', _('The tag has been successfully deleted'));
375 case 'reload_article' :
376 Tools
::logm('reload article');
378 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
379 Tools
::logm('reload url ' . $entry['url']);
380 $url = new Url(base64_encode($entry['url']));
381 $this->action('add', $url);
384 /* 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 */
387 while ($this->store
->retrieveOneById($id,$this->user
->getId()) == null) {
388 $count = $this->store
->getEntriesByViewCount($view, $this->user
->getId());
389 $id = rand(1,$count);
391 Tools
::logm('get a random article');
392 Tools
::redirect('?view=view&id=' . $id);
393 //$this->displayView('view', $id);
400 function displayView($view, $id = 0)
409 $dev_infos = $this->_getPocheVersion('dev');
410 $dev = trim($dev_infos[0]);
411 $check_time_dev = date('d-M-Y H:i', $dev_infos[1]);
412 $prod_infos = $this->_getPocheVersion('prod');
413 $prod = trim($prod_infos[0]);
414 $check_time_prod = date('d-M-Y H:i', $prod_infos[1]);
415 $compare_dev = version_compare(POCHE
, $dev);
416 $compare_prod = version_compare(POCHE
, $prod);
417 $themes = $this->tpl
->getInstalledThemes();
418 $languages = $this->language
->getInstalledLanguages();
419 $token = $this->user
->getConfigValue('token');
420 $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false;
421 $only_user = ($this->store
->listUsers() > 1) ? false : true;
424 'languages' => $languages,
427 'check_time_dev' => $check_time_dev,
428 'check_time_prod' => $check_time_prod,
429 'compare_dev' => $compare_dev,
430 'compare_prod' => $compare_prod,
432 'user_id' => $this->user
->getId(),
433 'http_auth' => $http_auth,
434 'only_user' => $only_user
436 Tools
::logm('config view');
440 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
442 $this->messages
->add('e', _('Article not found!'));
443 Tools
::logm('error : article not found');
446 $tags = $this->store
->retrieveTagsByEntry($id);
454 $token = $this->user
->getConfigValue('token');
455 //if term is set - search tags for this term
456 $term = Tools
::checkVar('term');
457 $tags = $this->store
->retrieveAllTags($this->user
->getId(), $term);
458 if (Tools
::isAjaxRequest()) {
460 foreach ($tags as $tag) {
461 $result[] = $tag['value'];
463 echo json_encode($result);
468 'user_id' => $this->user
->getId(),
473 if (isset($_GET['search'])) {
474 $search = filter_var($_GET['search'], FILTER_SANITIZE_STRING
);
475 $tpl_vars['entries'] = $this->store
->search($search, $this->user
->getId());
476 $count = count($tpl_vars['entries']);
477 $this->pagination
->set_total($count);
478 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
479 $this->pagination
->page_links('?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION['sort'] . '&' ));
480 $tpl_vars['page_links'] = $page_links;
481 $tpl_vars['nb_results'] = $count;
482 $tpl_vars['searchterm'] = $search;
486 $entry = $this->store
->retrieveOneById($id, $this->user
->getId());
487 if ($entry != NULL) {
488 Tools
::logm('view link #' . $id);
489 $content = $entry['content'];
490 if (function_exists('tidy_parse_string')) {
491 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
492 $tidy->cleanRepair();
493 $content = $tidy->value
;
499 $flattr = new FlattrItem();
500 $flattr->checkItem($entry['url'], $entry['id']);
504 $tags = $this->store
->retrieveTagsByEntry($entry['id']);
508 'content' => $content,
514 Tools
::logm('error in view call : entry is null');
517 default: # home, favorites, archive and tag views
522 'listmode' => (isset($_COOKIE['listmode']) ? true : false),
525 //if id is given - we retrieve entries by tag: id is tag id
527 $tpl_vars['tag'] = $this->store
->retrieveTag($id, $this->user
->getId());
528 $tpl_vars['id'] = intval($id);
531 $count = $this->store
->getEntriesByViewCount($view, $this->user
->getId(), $id);
534 $this->pagination
->set_total($count);
535 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
536 $this->pagination
->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' ));
537 $tpl_vars['entries'] = $this->store
->getEntriesByView($view, $this->user
->getId(), $this->pagination
->get_limit(), $id);
538 $tpl_vars['page_links'] = $page_links;
539 $tpl_vars['nb_results'] = $count;
541 Tools
::logm('display ' . $view . ' view');
549 * update the password of the current user.
550 * if MODE_DEMO is TRUE, the password can't be updated.
551 * @todo add the return value
552 * @todo set the new password in function header like this updatePassword($newPassword)
555 public function updatePassword($password, $confirmPassword)
558 $this->messages
->add('i', _('in demo mode, you can\'t update your password'));
559 Tools
::logm('in demo mode, you can\'t do this');
560 Tools
::redirect('?view=config');
563 if (isset($password) && isset($confirmPassword)) {
564 if ($password == $confirmPassword && !empty($password)) {
565 $this->messages
->add('s', _('your password has been updated'));
566 $this->store
->updatePassword($this->user
->getId(), Tools
::encodeString($password . $this->user
->getUsername()));
568 Tools
::logm('password updated');
572 $this->messages
->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
573 Tools
::redirect('?view=config');
580 * Get credentials from differents sources
581 * It redirects the user to the $referer link
585 private function credentials()
587 if (isset($_SERVER['PHP_AUTH_USER'])) {
588 return array($_SERVER['PHP_AUTH_USER'], 'php_auth', true);
590 if (!empty($_POST['login']) && !empty($_POST['password'])) {
591 return array($_POST['login'], $_POST['password'], false);
593 if (isset($_SERVER['REMOTE_USER'])) {
594 return array($_SERVER['REMOTE_USER'], 'http_auth', true);
597 return array(false, false, false);
601 * checks if login & password are correct and save the user in session.
602 * it redirects the user to the $referer link
603 * @param string $referer the url to redirect after login
604 * @todo add the return value
607 public function login($referer)
609 list($login,$password,$isauthenticated)=$this->credentials();
610 if($login === false || $password === false) {
611 $this->messages
->add('e', _('login failed: you have to fill all fields'));
612 Tools
::logm('login failed');
615 if (!empty($login) && !empty($password)) {
616 $user = $this->store
->login($login, Tools
::encodeString($password . $login), $isauthenticated);
617 if ($user != array()) {
618 # Save login into Session
619 $longlastingsession = isset($_POST['longlastingsession']);
620 $passwordTest = ($isauthenticated) ? $user['password'] : Tools
::encodeString($password . $login);
621 Session
::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user)));
624 $language = $user['config']['language'];
625 @putenv('LC_ALL=' . $language);
626 setlocale(LC_ALL
, $language);
627 bindtextdomain($language, LOCALE
);
628 textdomain($language);
630 $this->messages
->add('s', _('welcome to your wallabag'));
631 Tools
::logm('login successful');
632 Tools
::redirect($referer);
634 $this->messages
->add('e', _('login failed: bad login or password'));
635 // log login failure in web server log to allow fail2ban usage
636 error_log('user '.$login.' authentication failure');
637 Tools
::logm('login failed');
643 * log out the poche user. It cleans the session.
644 * @todo add the return value
647 public function logout()
649 $this->user
= array();
651 Tools
::logm('logout');
656 * import datas into your wallabag
660 public function import() {
662 if ( isset($_FILES['file']) && $_FILES['file']['tmp_name'] ) {
663 Tools
::logm('Import stated: parsing file');
665 // assume, that file is in json format
666 $str_data = file_get_contents($_FILES['file']['tmp_name']);
667 $data = json_decode($str_data, true);
669 if ( $data === null ) {
670 //not json - assume html
671 $html = new simple_html_dom();
672 $html->load_file($_FILES['file']['tmp_name']);
675 foreach (array('ol','ul') as $list) {
676 foreach ($html->find($list) as $ul) {
677 foreach ($ul->find('li') as $li) {
680 $tmpEntry['url'] = $a[0]->href
;
681 $tmpEntry['tags'] = $a[0]->tags
;
682 $tmpEntry['is_read'] = $read;
683 if ($tmpEntry['url']) {
687 # the second <ol/ul> is for read links
688 $read = ((sizeof($data) && $read)?0:1);
693 // for readability structure
695 foreach($data as $record) {
696 if (is_array($record)) {
698 foreach($record as $record2) {
699 if (is_array($record2)) {
706 $urlsInserted = array(); //urls of articles inserted
707 foreach($data as $record) {
708 $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : ''));
709 if ($url and !in_array($url, $urlsInserted)) {
710 $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>');
711 $body = (isset($record['content']) ? $record['content'] : '');
712 $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0));
713 $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite']) ? intval($record['favorite']) : 0));
717 $id = $this->store
->add($url, $title, $body, $this->user
->getId() , $isFavorite, $isRead);
719 $urlsInserted[] = $url; //add
720 if (isset($record['tags']) && trim($record['tags'])) {
722 $tags = explode(',', $record['tags']);
723 foreach($tags as $tag) {
725 $tag_id = $this->store
->retrieveTagByValue($tag);
727 $this->store
->setTagToEntry($tag_id['id'], $entry_id);
729 $this->store
->createTag($tag);
730 $tag_id = $this->store
->retrieveTagByValue($tag);
731 $this->store
->setTagToEntry($tag_id['id'], $entry_id);
740 $i = sizeof($urlsInserted);
742 $this->messages
->add('s', _('Articles inserted: ') . $i . _('. Please note, that some may be marked as "read".'));
745 Tools
::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).');
748 $this->messages
->add('s', _('Did you forget to select a file?'));
750 // file parsing finished here
751 // now download article contents if any
752 // check if we need to download any content
754 $recordsDownloadRequired = $this->store
->retrieveUnfetchedEntriesCount($this->user
->getId());
756 if ($recordsDownloadRequired == 0) {
758 // nothing to download
760 $this->messages
->add('s', _('Import finished.'));
761 Tools
::logm('Import finished completely');
766 // if just inserted - don't download anything, download will start in next reload
768 if (!isset($_FILES['file'])) {
770 // download next batch
772 Tools
::logm('Fetching next batch of articles...');
773 $items = $this->store
->retrieveUnfetchedEntries($this->user
->getId() , IMPORT_LIMIT
);
774 $purifier = $this->_getPurifier();
775 foreach($items as $item) {
776 $url = new Url(base64_encode($item['url']));
777 if( $url->isCorrect() )
779 Tools
::logm('Fetching article ' . $item['id']);
780 $content = Tools
::getPageContent($url);
781 $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'));
782 $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined'));
784 // clean content to prevent xss attack
786 $title = $purifier->purify($title);
787 $body = $purifier->purify($body);
788 $this->store
->updateContentAndTitle($item['id'], $title, $body, $this->user
->getId());
789 Tools
::logm('Article ' . $item['id'] . ' updated.');
792 Tools
::logm('Unvalid URL (' . $item['url'] .') to fetch for article ' . $item['id']);
799 'includeImport' => true,
801 'recordsDownloadRequired' => $recordsDownloadRequired,
802 'recordsUnderDownload' => IMPORT_LIMIT
,
803 'delay' => IMPORT_DELAY
* 1000
809 * export poche entries in json
810 * @return json all poche entries
812 public function export()
814 $filename = "wallabag-export-".$this->user
->getId()."-".date("Y-m-d").".json";
815 header('Content-Disposition: attachment; filename='.$filename);
817 $entries = $this->store
->retrieveAll($this->user
->getId());
818 echo $this->tpl
->render('export.twig', array(
819 'export' => Tools
::renderJson($entries),
821 Tools
::logm('export view');
825 * Checks online the latest version of poche and cache it
826 * @param string $which 'prod' or 'dev'
827 * @return string latest $which version
829 private function _getPocheVersion($which = 'prod') {
830 $cache_file = CACHE
. '/' . $which;
831 $check_time = time();
833 # checks if the cached version file exists
834 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
835 $version = file_get_contents($cache_file);
836 $check_time = filemtime($cache_file);
838 $version = file_get_contents('http://static.wallabag.org/versions/' . $which);
839 file_put_contents($cache_file, $version, LOCK_EX
);
841 return array($version, $check_time);
845 * Update token for current user
847 public function updateToken()
849 $token = Tools
::generateToken();
850 $this->store
->updateUserConfig($this->user
->getId(), 'token', $token);
851 $currentConfig = $_SESSION['poche_user']->config
;
852 $currentConfig['token'] = $token;
853 $_SESSION['poche_user']->setConfig($currentConfig);
858 * Generate RSS feeds for current user
862 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
863 * @param string $type the type of feed to generate
864 * @param int $limit the maximum number of items (0 means all)
866 public function generateFeeds($token, $user_id, $tag_id, $type = 'home', $limit = 0)
868 $allowed_types = array('home', 'fav', 'archive', 'tag');
869 $config = $this->store
->getConfigUser($user_id);
871 if ($config == null) {
872 die(sprintf(_('User with this id (%d) does not exist.'), $user_id));
875 if (!in_array($type, $allowed_types) || !isset($config['token']) || $token != $config['token']) {
876 die(_('Uh, there is a problem while generating feed. Wrong token used?'));
879 $feed = new FeedWriter(RSS2
);
880 $feed->setTitle('wallabag — ' . $type . ' feed');
881 $feed->setLink(Tools
::getPocheUrl());
882 $feed->setChannelElement('pubDate', date(DATE_RSS
, time()));
883 $feed->setChannelElement('generator', 'wallabag');
884 $feed->setDescription('wallabag ' . $type . ' elements');
886 if ($type == 'tag') {
887 $entries = $this->store
->retrieveEntriesByTag($tag_id, $user_id);
890 $entries = $this->store
->getEntriesByView($type, $user_id);
893 // if $limit is set to zero, use all entries
895 $limit = count($entries);
897 if (count($entries) > 0) {
898 for ($i = 0; $i < min(count($entries), $limit); $i++
) {
899 $entry = $entries[$i];
900 $newItem = $feed->createNewItem();
901 $newItem->setTitle($entry['title']);
902 $newItem->setSource(Tools
::getPocheUrl() . '?view=view&id=' . $entry['id']);
903 $newItem->setLink($entry['url']);
904 $newItem->setDate(time());
905 $newItem->setDescription($entry['content']);
906 $feed->addItem($newItem);
910 $feed->genarateFeed();
917 * Returns new purifier object with actual config
919 private function _getPurifier()
921 $config = HTMLPurifier_Config
::createDefault();
922 $config->set('Cache.SerializerPath', CACHE
);
923 $config->set('HTML.SafeIframe', true);
925 //allow YouTube, Vimeo and dailymotion videos
926 $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%');
928 return new HTMLPurifier($config);