]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
allow to send confirmation emails when creating a new user
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
1 <?php
2 /**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
7 * @copyright 2013
8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */
10
11 class Poche
12 {
13 /**
14 * @var User
15 */
16 public $user;
17 /**
18 * @var Database
19 */
20 public $store;
21 /**
22 * @var Template
23 */
24 public $tpl;
25 /**
26 * @var Language
27 */
28 public $language;
29 /**
30 * @var Routing
31 */
32 public $routing;
33 /**
34 * @var Messages
35 */
36 public $messages;
37 /**
38 * @var Paginator
39 */
40 public $pagination;
41
42 public function __construct()
43 {
44 $this->init();
45 }
46
47 private function init()
48 {
49 Tools::initPhp();
50
51 $pocheUser = Session::getParam('poche_user');
52
53 if ($pocheUser && $pocheUser != array()) {
54 $this->user = $pocheUser;
55 } else {
56 // fake user, just for install & login screens
57 $this->user = new User();
58 $this->user->setConfig($this->getDefaultConfig());
59 }
60
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);
67 }
68
69 public function run()
70 {
71 $this->routing->run();
72 }
73
74 /**
75 * Creates a new user
76 */
77 public function createNewUser($username, $password, $email = "", $internalRegistration = false)
78 {
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 (SEND_CONFIRMATION_EMAIL && function_exists('mail')) {
85 // if internal registration
86 $body_internal = "Hi,\r\n\r\nSomeone just created an account for you on " . Tools::getPocheUrl() . ".\r\nHave fun with it !";
87 // if external (public) registration
88 $body = "Hi, \r\n\r\nYou've just created an account on " . Tools::getPocheUrl() . . ".\r\nHave fun with it !";
89 $body = $internalRegistration ? $body_internal : $body;
90 $body = wordwrap($body, 70, "\r\n"); // cut lines with more than 70 caracters (MIME standard)
91 if (mail($email, sprintf(_('Your new wallabag account on '), Tools::getPocheUrl()), $body, 'From: {$email}')) {
92 Tools::logm('The user ' . $newUsername . ' has been emailed');
93 $this->messages->add('i', sprintf(_('The new user %1$s has been sent an email at %2$s.'), $newUsername, $email));
94
95 } else {
96 Tools::logm('A problem has been encountered while sending an email');
97 $this->messages->add('e', _('A problem has been encountered while sending an email');
98 }
99 } else {
100 Tools::logm('The user has been created, but the server did not authorize sending emails');
101 $this->messages->add('i', _('The server did not authorize sending an email');
102 }
103 Tools::logm('The new user ' . $newUsername . ' has been installed');
104 $this->messages->add('s', sprintf(_('The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>'), $newUsername));
105 Tools::redirect();
106 }
107 else {
108 Tools::logm('error during adding new user');
109 Tools::redirect();
110 }
111 }
112 else {
113 $this->messages->add('e', sprintf(_('Error : An user with the name %s already exists !'), $newUsername));
114 Tools::logm('An user with the name ' . $newUsername . ' already exists !');
115 Tools::redirect();
116 }
117 }
118 }
119
120 /**
121 * Delete an existing user
122 */
123 public function deleteUser($password)
124 {
125 if ($this->store->listUsers() > 1) {
126 if (Tools::encodeString($password . $this->user->getUsername()) == $this->store->getUserPassword($this->user->getId())) {
127 $username = $this->user->getUsername();
128 $this->store->deleteUserConfig($this->user->getId());
129 Tools::logm('The configuration for user '. $username .' has been deleted !');
130 $this->store->deleteTagsEntriesAndEntries($this->user->getId());
131 Tools::logm('The entries for user '. $username .' has been deleted !');
132 $this->store->deleteUser($this->user->getId());
133 Tools::logm('User '. $username .' has been completely deleted !');
134 Session::logout();
135 Tools::logm('logout');
136 Tools::redirect();
137 $this->messages->add('s', sprintf(_('User %s has been successfully deleted !'), $username));
138 }
139 else {
140 Tools::logm('Bad password !');
141 $this->messages->add('e', _('Error : The password is wrong !'));
142 }
143 }
144 else {
145 Tools::logm('Only user !');
146 $this->messages->add('e', _('Error : You are the only user, you cannot delete your account !'));
147 }
148 }
149
150 public function getDefaultConfig()
151 {
152 return array(
153 'pager' => PAGINATION,
154 'language' => LANG,
155 'theme' => DEFAULT_THEME
156 );
157 }
158
159 /**
160 * Call action (mark as fav, archive, delete, etc.)
161 */
162 public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null)
163 {
164 switch ($action)
165 {
166 case 'add':
167 $content = Tools::getPageContent($url);
168 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
169 $body = $content['rss']['channel']['item']['description'];
170
171 // clean content from prevent xss attack
172 $purifier = $this->_getPurifier();
173 $title = $purifier->purify($title);
174 $body = $purifier->purify($body);
175
176 //search for possible duplicate
177 $duplicate = NULL;
178 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
179
180 $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId());
181 if ( $last_id ) {
182 Tools::logm('add link ' . $url->getUrl());
183 if (DOWNLOAD_PICTURES) {
184 $content = Picture::filterPicture($body, $url->getUrl(), $last_id);
185 Tools::logm('updating content article');
186 $this->store->updateContent($last_id, $content, $this->user->getId());
187 }
188
189 if ($duplicate != NULL) {
190 // 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
191 Tools::logm('link ' . $url->getUrl() . ' is a duplicate');
192 // 1) - preserve tags and favorite, then drop old entry
193 $this->store->reassignTags($duplicate['id'], $last_id);
194 if ($duplicate['is_fav']) {
195 $this->store->favoriteById($last_id, $this->user->getId());
196 }
197 if ($this->store->deleteById($duplicate['id'], $this->user->getId())) {
198 Tools::logm('previous link ' . $url->getUrl() .' entry deleted');
199 }
200 }
201
202 // if there are tags, add them to the new article
203 if (isset($_GET['tags'])) {
204 $_POST['value'] = $_GET['tags'];
205 $_POST['entry_id'] = $last_id;
206 $this->action('add_tag', $url);
207 }
208
209 $this->messages->add('s', _('the link has been added successfully'));
210 }
211 else {
212 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
213 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
214 }
215
216 if ($autoclose == TRUE) {
217 Tools::redirect('?view=home');
218 } else {
219 Tools::redirect('?view=home&closewin=true');
220 }
221 return $last_id;
222 break;
223 case 'delete':
224 if (isset($_GET['search'])) {
225 //when we want to apply a delete to a search
226 $tags = array($_GET['search']);
227 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
228 $entry_ids = array();
229 foreach ($allentry_ids as $eachentry) {
230 $entry_ids[] = $eachentry[0];
231 }
232 } else { // delete a single article
233 $entry_ids = array($id);
234 }
235 foreach($entry_ids as $id) {
236 $msg = 'delete link #' . $id;
237 if ($this->store->deleteById($id, $this->user->getId())) {
238 if (DOWNLOAD_PICTURES) {
239 Picture::removeDirectory(ABS_PATH . $id);
240 }
241 $this->messages->add('s', _('the link has been deleted successfully'));
242 }
243 else {
244 $this->messages->add('e', _('the link wasn\'t deleted'));
245 $msg = 'error : can\'t delete link #' . $id;
246 }
247 Tools::logm($msg);
248 }
249 Tools::redirect('?');
250 break;
251 case 'toggle_fav' :
252 $this->store->favoriteById($id, $this->user->getId());
253 Tools::logm('mark as favorite link #' . $id);
254 if ( Tools::isAjaxRequest() ) {
255 echo 1;
256 exit;
257 }
258 else {
259 Tools::redirect();
260 }
261 break;
262 case 'toggle_archive' :
263 if (isset($_GET['tag_id'])) {
264 //when we want to archive a whole tag
265 $tag_id = $_GET['tag_id'];
266 $allentry_ids = $this->store->retrieveEntriesByTag($tag_id, $this->user->getId());
267 $entry_ids = array();
268 foreach ($allentry_ids as $eachentry) {
269 $entry_ids[] = $eachentry[0];
270 }
271 } else { //archive a single article
272 $entry_ids = array($id);
273 }
274 foreach($entry_ids as $id) {
275 $this->store->archiveById($id, $this->user->getId());
276 Tools::logm('archive link #' . $id);
277 }
278 if ( Tools::isAjaxRequest() ) {
279 echo 1;
280 exit;
281 }
282 else {
283 Tools::redirect();
284 }
285 break;
286 case 'archive_all' :
287 $this->store->archiveAll($this->user->getId());
288 Tools::logm('archive all links');
289 Tools::redirect();
290 break;
291 case 'add_tag' :
292 if (isset($_GET['search'])) {
293 //when we want to apply a tag to a search
294 $tags = array($_GET['search']);
295 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
296 $entry_ids = array();
297 foreach ($allentry_ids as $eachentry) {
298 $entry_ids[] = $eachentry[0];
299 }
300 } else { //add a tag to a single article
301 $tags = explode(',', $_POST['value']);
302 $entry_ids = array($_POST['entry_id']);
303 }
304 foreach($entry_ids as $entry_id) {
305 $entry = $this->store->retrieveOneById($entry_id, $this->user->getId());
306 if (!$entry) {
307 $this->messages->add('e', _('Article not found!'));
308 Tools::logm('error : article not found');
309 Tools::redirect();
310 }
311 //get all already set tags to preven duplicates
312 $already_set_tags = array();
313 $entry_tags = $this->store->retrieveTagsByEntry($entry_id);
314 foreach ($entry_tags as $tag) {
315 $already_set_tags[] = $tag['value'];
316 }
317 foreach($tags as $key => $tag_value) {
318 $value = trim($tag_value);
319 if ($value && !in_array($value, $already_set_tags)) {
320 $tag = $this->store->retrieveTagByValue($value);
321 if (is_null($tag)) {
322 # we create the tag
323 $tag = $this->store->createTag($value);
324 $sequence = '';
325 if (STORAGE == 'postgres') {
326 $sequence = 'tags_id_seq';
327 }
328 $tag_id = $this->store->getLastId($sequence);
329 }
330 else {
331 $tag_id = $tag['id'];
332 }
333
334 # we assign the tag to the article
335 $this->store->setTagToEntry($tag_id, $entry_id);
336 }
337 }
338 }
339 $this->messages->add('s', _('The tag has been applied successfully'));
340 Tools::logm('The tag has been applied successfully');
341 Tools::redirect();
342 break;
343 case 'remove_tag' :
344 $tag_id = $_GET['tag_id'];
345 $entry = $this->store->retrieveOneById($id, $this->user->getId());
346 if (!$entry) {
347 $this->messages->add('e', _('Article not found!'));
348 Tools::logm('error : article not found');
349 Tools::redirect();
350 }
351 $this->store->removeTagForEntry($id, $tag_id);
352 Tools::logm('tag entry deleted');
353 if ($this->store->cleanUnusedTag($tag_id)) {
354 Tools::logm('tag deleted');
355 }
356 $this->messages->add('s', _('The tag has been successfully deleted'));
357 Tools::redirect();
358 break;
359 default:
360 break;
361 }
362 }
363
364 function displayView($view, $id = 0)
365 {
366 $tpl_vars = array();
367
368 switch ($view)
369 {
370 case 'about':
371 break;
372 case 'config':
373 $dev_infos = $this->_getPocheVersion('dev');
374 $dev = trim($dev_infos[0]);
375 $check_time_dev = date('d-M-Y H:i', $dev_infos[1]);
376 $prod_infos = $this->_getPocheVersion('prod');
377 $prod = trim($prod_infos[0]);
378 $check_time_prod = date('d-M-Y H:i', $prod_infos[1]);
379 $compare_dev = version_compare(POCHE, $dev);
380 $compare_prod = version_compare(POCHE, $prod);
381 $themes = $this->tpl->getInstalledThemes();
382 $languages = $this->language->getInstalledLanguages();
383 $token = $this->user->getConfigValue('token');
384 $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false;
385 $only_user = ($this->store->listUsers() > 1) ? false : true;
386 $tpl_vars = array(
387 'themes' => $themes,
388 'languages' => $languages,
389 'dev' => $dev,
390 'prod' => $prod,
391 'check_time_dev' => $check_time_dev,
392 'check_time_prod' => $check_time_prod,
393 'compare_dev' => $compare_dev,
394 'compare_prod' => $compare_prod,
395 'token' => $token,
396 'user_id' => $this->user->getId(),
397 'http_auth' => $http_auth,
398 'only_user' => $only_user
399 );
400 Tools::logm('config view');
401 break;
402 case 'edit-tags':
403 # tags
404 $entry = $this->store->retrieveOneById($id, $this->user->getId());
405 if (!$entry) {
406 $this->messages->add('e', _('Article not found!'));
407 Tools::logm('error : article not found');
408 Tools::redirect();
409 }
410 $tags = $this->store->retrieveTagsByEntry($id);
411 $tpl_vars = array(
412 'entry_id' => $id,
413 'tags' => $tags,
414 'entry' => $entry,
415 );
416 break;
417 case 'tags':
418 $token = $this->user->getConfigValue('token');
419 //if term is set - search tags for this term
420 $term = Tools::checkVar('term');
421 $tags = $this->store->retrieveAllTags($this->user->getId(), $term);
422 if (Tools::isAjaxRequest()) {
423 $result = array();
424 foreach ($tags as $tag) {
425 $result[] = $tag['value'];
426 }
427 echo json_encode($result);
428 exit;
429 }
430 $tpl_vars = array(
431 'token' => $token,
432 'user_id' => $this->user->getId(),
433 'tags' => $tags,
434 );
435 break;
436 case 'search':
437 if (isset($_GET['search'])) {
438 $search = filter_var($_GET['search'], FILTER_SANITIZE_STRING);
439 $tpl_vars['entries'] = $this->store->search($search, $this->user->getId());
440 $count = count($tpl_vars['entries']);
441 $this->pagination->set_total($count);
442 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
443 $this->pagination->page_links('?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION['sort'] . '&' ));
444 $tpl_vars['page_links'] = $page_links;
445 $tpl_vars['nb_results'] = $count;
446 $tpl_vars['searchterm'] = $search;
447 }
448 break;
449 case 'view':
450 $entry = $this->store->retrieveOneById($id, $this->user->getId());
451 if ($entry != NULL) {
452 Tools::logm('view link #' . $id);
453 $content = $entry['content'];
454 if (function_exists('tidy_parse_string')) {
455 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
456 $tidy->cleanRepair();
457 $content = $tidy->value;
458 }
459
460 # flattr checking
461 $flattr = NULL;
462 if (FLATTR) {
463 $flattr = new FlattrItem();
464 $flattr->checkItem($entry['url'], $entry['id']);
465 }
466
467 # tags
468 $tags = $this->store->retrieveTagsByEntry($entry['id']);
469
470 $tpl_vars = array(
471 'entry' => $entry,
472 'content' => $content,
473 'flattr' => $flattr,
474 'tags' => $tags
475 );
476 }
477 else {
478 Tools::logm('error in view call : entry is null');
479 }
480 break;
481 default: # home, favorites, archive and tag views
482 $tpl_vars = array(
483 'entries' => '',
484 'page_links' => '',
485 'nb_results' => '',
486 'listmode' => (isset($_COOKIE['listmode']) ? true : false),
487 );
488
489 //if id is given - we retrieve entries by tag: id is tag id
490 if ($id) {
491 $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId());
492 $tpl_vars['id'] = intval($id);
493 }
494
495 $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id);
496
497 if ($count > 0) {
498 $this->pagination->set_total($count);
499 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
500 $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' ));
501 $tpl_vars['entries'] = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit(), $id);
502 $tpl_vars['page_links'] = $page_links;
503 $tpl_vars['nb_results'] = $count;
504 }
505 Tools::logm('display ' . $view . ' view');
506 break;
507 }
508
509 return $tpl_vars;
510 }
511
512 /**
513 * update the password of the current user.
514 * if MODE_DEMO is TRUE, the password can't be updated.
515 * @todo add the return value
516 * @todo set the new password in function header like this updatePassword($newPassword)
517 * @return boolean
518 */
519 public function updatePassword($password, $confirmPassword)
520 {
521 if (MODE_DEMO) {
522 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
523 Tools::logm('in demo mode, you can\'t do this');
524 Tools::redirect('?view=config');
525 }
526 else {
527 if (isset($password) && isset($confirmPassword)) {
528 if ($password == $confirmPassword && !empty($password)) {
529 $this->messages->add('s', _('your password has been updated'));
530 $this->store->updatePassword($this->user->getId(), Tools::encodeString($password . $this->user->getUsername()));
531 Session::logout();
532 Tools::logm('password updated');
533 Tools::redirect();
534 }
535 else {
536 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
537 Tools::redirect('?view=config');
538 }
539 }
540 }
541 }
542
543 /**
544 * Get credentials from differents sources
545 * It redirects the user to the $referer link
546 *
547 * @return array
548 */
549 private function credentials()
550 {
551 if (isset($_SERVER['PHP_AUTH_USER'])) {
552 return array($_SERVER['PHP_AUTH_USER'], 'php_auth', true);
553 }
554 if (!empty($_POST['login']) && !empty($_POST['password'])) {
555 return array($_POST['login'], $_POST['password'], false);
556 }
557 if (isset($_SERVER['REMOTE_USER'])) {
558 return array($_SERVER['REMOTE_USER'], 'http_auth', true);
559 }
560
561 return array(false, false, false);
562 }
563
564 /**
565 * checks if login & password are correct and save the user in session.
566 * it redirects the user to the $referer link
567 * @param string $referer the url to redirect after login
568 * @todo add the return value
569 * @return boolean
570 */
571 public function login($referer)
572 {
573 list($login,$password,$isauthenticated)=$this->credentials();
574 if($login === false || $password === false) {
575 $this->messages->add('e', _('login failed: you have to fill all fields'));
576 Tools::logm('login failed');
577 Tools::redirect();
578 }
579 if (!empty($login) && !empty($password)) {
580 $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated);
581 if ($user != array()) {
582 # Save login into Session
583 $longlastingsession = isset($_POST['longlastingsession']);
584 $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login);
585 Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user)));
586
587 # reload l10n
588 $language = $user['config']['language'];
589 @putenv('LC_ALL=' . $language);
590 setlocale(LC_ALL, $language);
591 bindtextdomain($language, LOCALE);
592 textdomain($language);
593
594 $this->messages->add('s', _('welcome to your wallabag'));
595 Tools::logm('login successful');
596 Tools::redirect($referer);
597 }
598 $this->messages->add('e', _('login failed: bad login or password'));
599 // log login failure in web server log to allow fail2ban usage
600 error_log('user '.$login.' authentication failure');
601 Tools::logm('login failed');
602 Tools::redirect();
603 }
604 }
605
606 /**
607 * log out the poche user. It cleans the session.
608 * @todo add the return value
609 * @return boolean
610 */
611 public function logout()
612 {
613 $this->user = array();
614 Session::logout();
615 Tools::logm('logout');
616 Tools::redirect();
617 }
618
619 /**
620 * import datas into your wallabag
621 * @return boolean
622 */
623
624 public function import() {
625
626 if ( isset($_FILES['file']) && $_FILES['file']['tmp_name'] ) {
627 Tools::logm('Import stated: parsing file');
628
629 // assume, that file is in json format
630 $str_data = file_get_contents($_FILES['file']['tmp_name']);
631 $data = json_decode($str_data, true);
632
633 if ( $data === null ) {
634 //not json - assume html
635 $html = new simple_html_dom();
636 $html->load_file($_FILES['file']['tmp_name']);
637 $data = array();
638 $read = 0;
639 foreach (array('ol','ul') as $list) {
640 foreach ($html->find($list) as $ul) {
641 foreach ($ul->find('li') as $li) {
642 $tmpEntry = array();
643 $a = $li->find('a');
644 $tmpEntry['url'] = $a[0]->href;
645 $tmpEntry['tags'] = $a[0]->tags;
646 $tmpEntry['is_read'] = $read;
647 if ($tmpEntry['url']) {
648 $data[] = $tmpEntry;
649 }
650 }
651 # the second <ol/ul> is for read links
652 $read = ((sizeof($data) && $read)?0:1);
653 }
654 }
655 }
656
657 // for readability structure
658
659 foreach($data as $record) {
660 if (is_array($record)) {
661 $data[] = $record;
662 foreach($record as $record2) {
663 if (is_array($record2)) {
664 $data[] = $record2;
665 }
666 }
667 }
668 }
669
670 $urlsInserted = array(); //urls of articles inserted
671 foreach($data as $record) {
672 $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : ''));
673 if ($url and !in_array($url, $urlsInserted)) {
674 $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>');
675 $body = (isset($record['content']) ? $record['content'] : '');
676 $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0));
677 $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite']) ? intval($record['favorite']) : 0));
678
679 // insert new record
680
681 $id = $this->store->add($url, $title, $body, $this->user->getId() , $isFavorite, $isRead);
682 if ($id) {
683 $urlsInserted[] = $url; //add
684 if (isset($record['tags']) && trim($record['tags'])) {
685
686 $tags = explode(',', $record['tags']);
687 foreach($tags as $tag) {
688 $entry_id = $id;
689 $tag_id = $this->store->retrieveTagByValue($tag);
690 if ($tag_id) {
691 $this->store->setTagToEntry($tag_id['id'], $entry_id);
692 } else {
693 $this->store->createTag($tag);
694 $tag_id = $this->store->retrieveTagByValue($tag);
695 $this->store->setTagToEntry($tag_id['id'], $entry_id);
696 }
697 }
698
699 }
700 }
701 }
702 }
703
704 $i = sizeof($urlsInserted);
705 if ($i > 0) {
706 $this->messages->add('s', _('Articles inserted: ') . $i . _('. Please note, that some may be marked as "read".'));
707 }
708
709 Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).');
710 }
711 else {
712 $this->messages->add('s', _('Did you forget to select a file?'));
713 }
714 // file parsing finished here
715 // now download article contents if any
716 // check if we need to download any content
717
718 $recordsDownloadRequired = $this->store->retrieveUnfetchedEntriesCount($this->user->getId());
719
720 if ($recordsDownloadRequired == 0) {
721
722 // nothing to download
723
724 $this->messages->add('s', _('Import finished.'));
725 Tools::logm('Import finished completely');
726 Tools::redirect();
727 }
728 else {
729
730 // if just inserted - don't download anything, download will start in next reload
731
732 if (!isset($_FILES['file'])) {
733
734 // download next batch
735
736 Tools::logm('Fetching next batch of articles...');
737 $items = $this->store->retrieveUnfetchedEntries($this->user->getId() , IMPORT_LIMIT);
738 $purifier = $this->_getPurifier();
739 foreach($items as $item) {
740 $url = new Url(base64_encode($item['url']));
741 Tools::logm('Fetching article ' . $item['id']);
742 $content = Tools::getPageContent($url);
743 $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'));
744 $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined'));
745
746 // clean content to prevent xss attack
747
748 $title = $purifier->purify($title);
749 $body = $purifier->purify($body);
750 $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId());
751 Tools::logm('Article ' . $item['id'] . ' updated.');
752 }
753 }
754 }
755
756 return array(
757 'includeImport' => true,
758 'import' => array(
759 'recordsDownloadRequired' => $recordsDownloadRequired,
760 'recordsUnderDownload' => IMPORT_LIMIT,
761 'delay' => IMPORT_DELAY * 1000
762 )
763 );
764 }
765
766 /**
767 * export poche entries in json
768 * @return json all poche entries
769 */
770 public function export()
771 {
772 $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json";
773 header('Content-Disposition: attachment; filename='.$filename);
774
775 $entries = $this->store->retrieveAll($this->user->getId());
776 echo $this->tpl->render('export.twig', array(
777 'export' => Tools::renderJson($entries),
778 ));
779 Tools::logm('export view');
780 }
781
782 /**
783 * Checks online the latest version of poche and cache it
784 * @param string $which 'prod' or 'dev'
785 * @return string latest $which version
786 */
787 private function _getPocheVersion($which = 'prod') {
788 $cache_file = CACHE . '/' . $which;
789 $check_time = time();
790
791 # checks if the cached version file exists
792 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
793 $version = file_get_contents($cache_file);
794 $check_time = filemtime($cache_file);
795 } else {
796 $version = file_get_contents('http://static.wallabag.org/versions/' . $which);
797 file_put_contents($cache_file, $version, LOCK_EX);
798 }
799 return array($version, $check_time);
800 }
801
802 /**
803 * Update token for current user
804 */
805 public function updateToken()
806 {
807 $token = Tools::generateToken();
808 $this->store->updateUserConfig($this->user->getId(), 'token', $token);
809 $currentConfig = $_SESSION['poche_user']->config;
810 $currentConfig['token'] = $token;
811 $_SESSION['poche_user']->setConfig($currentConfig);
812 Tools::redirect();
813 }
814
815 /**
816 * Generate RSS feeds for current user
817 *
818 * @param $token
819 * @param $user_id
820 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
821 * @param string $type the type of feed to generate
822 * @param int $limit the maximum number of items (0 means all)
823 */
824 public function generateFeeds($token, $user_id, $tag_id, $type = 'home', $limit = 0)
825 {
826 $allowed_types = array('home', 'fav', 'archive', 'tag');
827 $config = $this->store->getConfigUser($user_id);
828
829 if ($config == null) {
830 die(sprintf(_('User with this id (%d) does not exist.'), $user_id));
831 }
832
833 if (!in_array($type, $allowed_types) || !isset($config['token']) || $token != $config['token']) {
834 die(_('Uh, there is a problem while generating feed. Wrong token used?'));
835 }
836
837 $feed = new FeedWriter(RSS2);
838 $feed->setTitle('wallabag — ' . $type . ' feed');
839 $feed->setLink(Tools::getPocheUrl());
840 $feed->setChannelElement('pubDate', date(DATE_RSS , time()));
841 $feed->setChannelElement('generator', 'wallabag');
842 $feed->setDescription('wallabag ' . $type . ' elements');
843
844 if ($type == 'tag') {
845 $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id);
846 }
847 else {
848 $entries = $this->store->getEntriesByView($type, $user_id);
849 }
850
851 // if $limit is set to zero, use all entries
852 if (0 == $limit) {
853 $limit = count($entries);
854 }
855 if (count($entries) > 0) {
856 for ($i = 0; $i < min(count($entries), $limit); $i++) {
857 $entry = $entries[$i];
858 $newItem = $feed->createNewItem();
859 $newItem->setTitle($entry['title']);
860 $newItem->setSource(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']);
861 $newItem->setLink($entry['url']);
862 $newItem->setDate(time());
863 $newItem->setDescription($entry['content']);
864 $feed->addItem($newItem);
865 }
866 }
867
868 $feed->genarateFeed();
869 exit;
870 }
871
872
873
874 /**
875 * Returns new purifier object with actual config
876 */
877 private function _getPurifier()
878 {
879 $config = HTMLPurifier_Config::createDefault();
880 $config->set('Cache.SerializerPath', CACHE);
881 $config->set('HTML.SafeIframe', true);
882
883 //allow YouTube, Vimeo and dailymotion videos
884 $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%');
885
886 return new HTMLPurifier($config);
887 }
888
889
890 }