diff options
Diffstat (limited to 'inc/poche/Poche.class.php')
-rw-r--r-- | inc/poche/Poche.class.php | 237 |
1 files changed, 194 insertions, 43 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 806da54b..e9ff7b46 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -49,6 +49,7 @@ class Poche | |||
49 | if (! $this->store->isInstalled()) { | 49 | if (! $this->store->isInstalled()) { |
50 | $this->install(); | 50 | $this->install(); |
51 | } | 51 | } |
52 | $this->store->checkTags(); | ||
52 | } | 53 | } |
53 | } | 54 | } |
54 | 55 | ||
@@ -327,14 +328,17 @@ class Poche | |||
327 | /** | 328 | /** |
328 | * Call action (mark as fav, archive, delete, etc.) | 329 | * Call action (mark as fav, archive, delete, etc.) |
329 | */ | 330 | */ |
330 | public function action($action, Url $url, $id = 0, $import = FALSE) | 331 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE) |
331 | { | 332 | { |
332 | switch ($action) | 333 | switch ($action) |
333 | { | 334 | { |
334 | case 'add': | 335 | case 'add': |
335 | $content = $url->extract(); | 336 | $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed'); |
337 | $content = json_decode($json, true); | ||
338 | $title = $content['rss']['channel']['item']['title']; | ||
339 | $body = $content['rss']['channel']['item']['description']; | ||
336 | 340 | ||
337 | if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) { | 341 | if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { |
338 | Tools::logm('add link ' . $url->getUrl()); | 342 | Tools::logm('add link ' . $url->getUrl()); |
339 | $sequence = ''; | 343 | $sequence = ''; |
340 | if (STORAGE == 'postgres') { | 344 | if (STORAGE == 'postgres') { |
@@ -342,7 +346,7 @@ class Poche | |||
342 | } | 346 | } |
343 | $last_id = $this->store->getLastId($sequence); | 347 | $last_id = $this->store->getLastId($sequence); |
344 | if (DOWNLOAD_PICTURES) { | 348 | if (DOWNLOAD_PICTURES) { |
345 | $content = filtre_picture($content['body'], $url->getUrl(), $last_id); | 349 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
346 | Tools::logm('updating content article'); | 350 | Tools::logm('updating content article'); |
347 | $this->store->updateContent($last_id, $content, $this->user->getId()); | 351 | $this->store->updateContent($last_id, $content, $this->user->getId()); |
348 | } | 352 | } |
@@ -358,7 +362,11 @@ class Poche | |||
358 | } | 362 | } |
359 | 363 | ||
360 | if (!$import) { | 364 | if (!$import) { |
361 | Tools::redirect('?view=home'); | 365 | if ($autoclose == TRUE) { |
366 | Tools::redirect('?view=home'); | ||
367 | } else { | ||
368 | Tools::redirect('?view=home&closewin=true'); | ||
369 | } | ||
362 | } | 370 | } |
363 | break; | 371 | break; |
364 | case 'delete': | 372 | case 'delete': |
@@ -374,7 +382,7 @@ class Poche | |||
374 | $msg = 'error : can\'t delete link #' . $id; | 382 | $msg = 'error : can\'t delete link #' . $id; |
375 | } | 383 | } |
376 | Tools::logm($msg); | 384 | Tools::logm($msg); |
377 | Tools::redirect(); | 385 | Tools::redirect('?'); |
378 | break; | 386 | break; |
379 | case 'toggle_fav' : | 387 | case 'toggle_fav' : |
380 | $this->store->favoriteById($id, $this->user->getId()); | 388 | $this->store->favoriteById($id, $this->user->getId()); |
@@ -390,6 +398,36 @@ class Poche | |||
390 | Tools::redirect(); | 398 | Tools::redirect(); |
391 | } | 399 | } |
392 | break; | 400 | break; |
401 | case 'add_tag' : | ||
402 | $tags = explode(',', $_POST['value']); | ||
403 | $entry_id = $_POST['entry_id']; | ||
404 | foreach($tags as $key => $tag_value) { | ||
405 | $value = trim($tag_value); | ||
406 | $tag = $this->store->retrieveTagByValue($value); | ||
407 | |||
408 | if (is_null($tag)) { | ||
409 | # we create the tag | ||
410 | $tag = $this->store->createTag($value); | ||
411 | $sequence = ''; | ||
412 | if (STORAGE == 'postgres') { | ||
413 | $sequence = 'tags_id_seq'; | ||
414 | } | ||
415 | $tag_id = $this->store->getLastId($sequence); | ||
416 | } | ||
417 | else { | ||
418 | $tag_id = $tag['id']; | ||
419 | } | ||
420 | |||
421 | # we assign the tag to the article | ||
422 | $this->store->setTagToEntry($tag_id, $entry_id); | ||
423 | } | ||
424 | Tools::redirect(); | ||
425 | break; | ||
426 | case 'remove_tag' : | ||
427 | $tag_id = $_GET['tag_id']; | ||
428 | $this->store->removeTagForEntry($id, $tag_id); | ||
429 | Tools::redirect(); | ||
430 | break; | ||
393 | default: | 431 | default: |
394 | break; | 432 | break; |
395 | } | 433 | } |
@@ -408,6 +446,8 @@ class Poche | |||
408 | $compare_prod = version_compare(POCHE, $prod); | 446 | $compare_prod = version_compare(POCHE, $prod); |
409 | $themes = $this->getInstalledThemes(); | 447 | $themes = $this->getInstalledThemes(); |
410 | $languages = $this->getInstalledLanguages(); | 448 | $languages = $this->getInstalledLanguages(); |
449 | $token = $this->user->getConfigValue('token'); | ||
450 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; | ||
411 | $tpl_vars = array( | 451 | $tpl_vars = array( |
412 | 'themes' => $themes, | 452 | 'themes' => $themes, |
413 | 'languages' => $languages, | 453 | 'languages' => $languages, |
@@ -415,9 +455,37 @@ class Poche | |||
415 | 'prod' => $prod, | 455 | 'prod' => $prod, |
416 | 'compare_dev' => $compare_dev, | 456 | 'compare_dev' => $compare_dev, |
417 | 'compare_prod' => $compare_prod, | 457 | 'compare_prod' => $compare_prod, |
458 | 'token' => $token, | ||
459 | 'user_id' => $this->user->getId(), | ||
460 | 'http_auth' => $http_auth, | ||
418 | ); | 461 | ); |
419 | Tools::logm('config view'); | 462 | Tools::logm('config view'); |
420 | break; | 463 | break; |
464 | case 'edit-tags': | ||
465 | # tags | ||
466 | $tags = $this->store->retrieveTagsByEntry($id); | ||
467 | $tpl_vars = array( | ||
468 | 'entry_id' => $id, | ||
469 | 'tags' => $tags, | ||
470 | ); | ||
471 | break; | ||
472 | case 'tag': | ||
473 | $entries = $this->store->retrieveEntriesByTag($id); | ||
474 | $tag = $this->store->retrieveTag($id); | ||
475 | $tpl_vars = array( | ||
476 | 'tag' => $tag, | ||
477 | 'entries' => $entries, | ||
478 | ); | ||
479 | break; | ||
480 | case 'tags': | ||
481 | $token = $this->user->getConfigValue('token'); | ||
482 | $tags = $this->store->retrieveAllTags(); | ||
483 | $tpl_vars = array( | ||
484 | 'token' => $token, | ||
485 | 'user_id' => $this->user->getId(), | ||
486 | 'tags' => $tags, | ||
487 | ); | ||
488 | break; | ||
421 | case 'view': | 489 | case 'view': |
422 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | 490 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
423 | if ($entry != NULL) { | 491 | if ($entry != NULL) { |
@@ -431,12 +499,16 @@ class Poche | |||
431 | 499 | ||
432 | # flattr checking | 500 | # flattr checking |
433 | $flattr = new FlattrItem(); | 501 | $flattr = new FlattrItem(); |
434 | $flattr->checkItem($entry['url'],$entry['id']); | 502 | $flattr->checkItem($entry['url'], $entry['id']); |
503 | |||
504 | # tags | ||
505 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | ||
435 | 506 | ||
436 | $tpl_vars = array( | 507 | $tpl_vars = array( |
437 | 'entry' => $entry, | 508 | 'entry' => $entry, |
438 | 'content' => $content, | 509 | 'content' => $content, |
439 | 'flattr' => $flattr | 510 | 'flattr' => $flattr, |
511 | 'tags' => $tags | ||
440 | ); | 512 | ); |
441 | } | 513 | } |
442 | else { | 514 | else { |
@@ -574,6 +646,25 @@ class Poche | |||
574 | } | 646 | } |
575 | 647 | ||
576 | /** | 648 | /** |
649 | * get credentials from differents sources | ||
650 | * it redirects the user to the $referer link | ||
651 | * @return array | ||
652 | */ | ||
653 | private function credentials() { | ||
654 | if(isset($_SERVER['PHP_AUTH_USER'])) { | ||
655 | return array($_SERVER['PHP_AUTH_USER'],'php_auth'); | ||
656 | } | ||
657 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | ||
658 | return array($_POST['login'],$_POST['password']); | ||
659 | } | ||
660 | if(isset($_SERVER['REMOTE_USER'])) { | ||
661 | return array($_SERVER['REMOTE_USER'],'http_auth'); | ||
662 | } | ||
663 | |||
664 | return array(false,false); | ||
665 | } | ||
666 | |||
667 | /** | ||
577 | * checks if login & password are correct and save the user in session. | 668 | * checks if login & password are correct and save the user in session. |
578 | * it redirects the user to the $referer link | 669 | * it redirects the user to the $referer link |
579 | * @param string $referer the url to redirect after login | 670 | * @param string $referer the url to redirect after login |
@@ -582,11 +673,18 @@ class Poche | |||
582 | */ | 673 | */ |
583 | public function login($referer) | 674 | public function login($referer) |
584 | { | 675 | { |
585 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | 676 | list($login,$password)=$this->credentials(); |
586 | $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])); | 677 | if($login === false || $password === false) { |
678 | $this->messages->add('e', _('login failed: you have to fill all fields')); | ||
679 | Tools::logm('login failed'); | ||
680 | Tools::redirect(); | ||
681 | } | ||
682 | if (!empty($login) && !empty($password)) { | ||
683 | $user = $this->store->login($login, Tools::encodeString($password . $login)); | ||
587 | if ($user != array()) { | 684 | if ($user != array()) { |
588 | # Save login into Session | 685 | # Save login into Session |
589 | Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user))); | 686 | $longlastingsession = isset($_POST['longlastingsession']); |
687 | Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), $longlastingsession, array('poche_user' => new User($user))); | ||
590 | $this->messages->add('s', _('welcome to your poche')); | 688 | $this->messages->add('s', _('welcome to your poche')); |
591 | Tools::logm('login successful'); | 689 | Tools::logm('login successful'); |
592 | Tools::redirect($referer); | 690 | Tools::redirect($referer); |
@@ -594,10 +692,6 @@ class Poche | |||
594 | $this->messages->add('e', _('login failed: bad login or password')); | 692 | $this->messages->add('e', _('login failed: bad login or password')); |
595 | Tools::logm('login failed'); | 693 | Tools::logm('login failed'); |
596 | Tools::redirect(); | 694 | Tools::redirect(); |
597 | } else { | ||
598 | $this->messages->add('e', _('login failed: you have to fill all fields')); | ||
599 | Tools::logm('login failed'); | ||
600 | Tools::redirect(); | ||
601 | } | 695 | } |
602 | } | 696 | } |
603 | 697 | ||
@@ -712,34 +806,37 @@ class Poche | |||
712 | $url = NULL; | 806 | $url = NULL; |
713 | $favorite = FALSE; | 807 | $favorite = FALSE; |
714 | $archive = FALSE; | 808 | $archive = FALSE; |
715 | foreach ($value as $attr => $attr_value) { | 809 | foreach ($value as $item) { |
716 | if ($attr == 'article__url') { | 810 | foreach ($item as $attr => $value) { |
717 | $url = new Url(base64_encode($attr_value)); | 811 | if ($attr == 'article__url') { |
718 | } | 812 | $url = new Url(base64_encode($value)); |
719 | $sequence = ''; | ||
720 | if (STORAGE == 'postgres') { | ||
721 | $sequence = 'entries_id_seq'; | ||
722 | } | ||
723 | if ($attr_value == 'true') { | ||
724 | if ($attr == 'favorite') { | ||
725 | $favorite = TRUE; | ||
726 | } | 813 | } |
727 | if ($attr == 'archive') { | 814 | $sequence = ''; |
728 | $archive = TRUE; | 815 | if (STORAGE == 'postgres') { |
816 | $sequence = 'entries_id_seq'; | ||
817 | } | ||
818 | if ($value == 'true') { | ||
819 | if ($attr == 'favorite') { | ||
820 | $favorite = TRUE; | ||
821 | } | ||
822 | if ($attr == 'archive') { | ||
823 | $archive = TRUE; | ||
824 | } | ||
729 | } | 825 | } |
730 | } | 826 | } |
731 | } | 827 | |
732 | # we can add the url | 828 | # we can add the url |
733 | if (!is_null($url) && $url->isCorrect()) { | 829 | if (!is_null($url) && $url->isCorrect()) { |
734 | $this->action('add', $url, 0, TRUE); | 830 | $this->action('add', $url, 0, TRUE); |
735 | $count++; | 831 | $count++; |
736 | if ($favorite) { | 832 | if ($favorite) { |
737 | $last_id = $this->store->getLastId($sequence); | 833 | $last_id = $this->store->getLastId($sequence); |
738 | $this->action('toggle_fav', $url, $last_id, TRUE); | 834 | $this->action('toggle_fav', $url, $last_id, TRUE); |
739 | } | 835 | } |
740 | if ($archive) { | 836 | if ($archive) { |
741 | $last_id = $this->store->getLastId($sequence); | 837 | $last_id = $this->store->getLastId($sequence); |
742 | $this->action('toggle_archive', $url, $last_id, TRUE); | 838 | $this->action('toggle_archive', $url, $last_id, TRUE); |
839 | } | ||
743 | } | 840 | } |
744 | } | 841 | } |
745 | } | 842 | } |
@@ -814,4 +911,58 @@ class Poche | |||
814 | } | 911 | } |
815 | return $version; | 912 | return $version; |
816 | } | 913 | } |
817 | } \ No newline at end of file | 914 | |
915 | public function generateToken() | ||
916 | { | ||
917 | if (ini_get('open_basedir') === '') { | ||
918 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | ||
919 | } | ||
920 | else { | ||
921 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | ||
922 | } | ||
923 | |||
924 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | ||
925 | $currentConfig = $_SESSION['poche_user']->config; | ||
926 | $currentConfig['token'] = $token; | ||
927 | $_SESSION['poche_user']->setConfig($currentConfig); | ||
928 | } | ||
929 | |||
930 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') | ||
931 | { | ||
932 | $allowed_types = array('home', 'fav', 'archive', 'tag'); | ||
933 | $config = $this->store->getConfigUser($user_id); | ||
934 | |||
935 | if (!in_array($type, $allowed_types) || | ||
936 | $token != $config['token']) { | ||
937 | die(_('Uh, there is a problem while generating feeds.')); | ||
938 | } | ||
939 | // Check the token | ||
940 | |||
941 | $feed = new FeedWriter(RSS2); | ||
942 | $feed->setTitle('poche - ' . $type . ' feed'); | ||
943 | $feed->setLink(Tools::getPocheUrl()); | ||
944 | $feed->setChannelElement('updated', date(DATE_RSS , time())); | ||
945 | $feed->setChannelElement('author', 'poche'); | ||
946 | |||
947 | if ($type == 'tag') { | ||
948 | $entries = $this->store->retrieveEntriesByTag($tag_id); | ||
949 | } | ||
950 | else { | ||
951 | $entries = $this->store->getEntriesByView($type, $user_id); | ||
952 | } | ||
953 | |||
954 | if (count($entries) > 0) { | ||
955 | foreach ($entries as $entry) { | ||
956 | $newItem = $feed->createNewItem(); | ||
957 | $newItem->setTitle($entry['title']); | ||
958 | $newItem->setLink(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); | ||
959 | $newItem->setDate(time()); | ||
960 | $newItem->setDescription($entry['content']); | ||
961 | $feed->addItem($newItem); | ||
962 | } | ||
963 | } | ||
964 | |||
965 | $feed->genarateFeed(); | ||
966 | exit; | ||
967 | } | ||
968 | } | ||