aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Poche.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/poche/Poche.class.php')
-rwxr-xr-xinc/poche/Poche.class.php210
1 files changed, 110 insertions, 100 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 34f2ff5a..7bf33308 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -34,7 +34,8 @@ class Poche
34 'pl_PL.utf8' => 'Polski', 34 'pl_PL.utf8' => 'Polski',
35 'ru_RU.utf8' => 'Pусский', 35 'ru_RU.utf8' => 'Pусский',
36 'sl_SI.utf8' => 'Slovenščina', 36 'sl_SI.utf8' => 'Slovenščina',
37 'uk_UA.utf8' => 'Український', 37 'uk_UA.utf8' => 'Українська',
38 'pt_BR.utf8' => 'Brasileiro',
38 ); 39 );
39 public function __construct() 40 public function __construct()
40 { 41 {
@@ -336,7 +337,7 @@ class Poche
336 while (($language = readdir($handle)) !== false) { 337 while (($language = readdir($handle)) !== false) {
337 # Languages are stored in a directory, so all directory names are languages 338 # Languages are stored in a directory, so all directory names are languages
338 # @todo move language installation data to database 339 # @todo move language installation data to database
339 if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.'))) { 340 if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) {
340 continue; 341 continue;
341 } 342 }
342 343
@@ -346,7 +347,7 @@ class Poche
346 $current = true; 347 $current = true;
347 } 348 }
348 349
349 $languages[] = array('name' => $this->language_names[$language], 'value' => $language, 'current' => $current); 350 $languages[] = array('name' => (isset($this->language_names[$language]) ? $this->language_names[$language] : $language), 'value' => $language, 'current' => $current);
350 } 351 }
351 352
352 return $languages; 353 return $languages;
@@ -361,60 +362,6 @@ class Poche
361 ); 362 );
362 } 363 }
363 364
364 protected function getPageContent(Url $url)
365 {
366 // Saving and clearing context
367 $REAL = array();
368 foreach( $GLOBALS as $key => $value ) {
369 if( $key != "GLOBALS" && $key != "_SESSION" ) {
370 $GLOBALS[$key] = array();
371 $REAL[$key] = $value;
372 }
373 }
374 // Saving and clearing session
375 $REAL_SESSION = array();
376 foreach( $_SESSION as $key => $value ) {
377 $REAL_SESSION[$key] = $value;
378 unset($_SESSION[$key]);
379 }
380
381 // Running code in different context
382 $scope = function() {
383 extract( func_get_arg(1) );
384 $_GET = $_REQUEST = array(
385 "url" => $url->getUrl(),
386 "max" => 5,
387 "links" => "preserve",
388 "exc" => "",
389 "format" => "json",
390 "submit" => "Create Feed"
391 );
392 ob_start();
393 require func_get_arg(0);
394 $json = ob_get_flush();
395 return $json;
396 };
397 $json = $scope( "inc/3rdparty/makefulltextfeed.php", array("url" => $url) );
398
399 // Clearing and restoring context
400 foreach( $GLOBALS as $key => $value ) {
401 if( $key != "GLOBALS" && $key != "_SESSION" ) {
402 unset($GLOBALS[$key]);
403 }
404 }
405 foreach( $REAL as $key => $value ) {
406 $GLOBALS[$key] = $value;
407 }
408 // Clearing and restoring session
409 foreach( $_SESSION as $key => $value ) {
410 unset($_SESSION[$key]);
411 }
412 foreach( $REAL_SESSION as $key => $value ) {
413 $_SESSION[$key] = $value;
414 }
415 return json_decode($json, true);
416 }
417
418 /** 365 /**
419 * Call action (mark as fav, archive, delete, etc.) 366 * Call action (mark as fav, archive, delete, etc.)
420 */ 367 */
@@ -423,17 +370,25 @@ class Poche
423 switch ($action) 370 switch ($action)
424 { 371 {
425 case 'add': 372 case 'add':
426 $content = $this->getPageContent($url); 373 if (!$import) {
427 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); 374 $content = Tools::getPageContent($url);
428 $body = $content['rss']['channel']['item']['description']; 375 $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
429 376 $body = $content['rss']['channel']['item']['description'];
430 // clean content from prevent xss attack 377
431 $config = HTMLPurifier_Config::createDefault(); 378 // clean content from prevent xss attack
432 $purifier = new HTMLPurifier($config); 379 $config = HTMLPurifier_Config::createDefault();
433 $title = $purifier->purify($title); 380 $config->set('Cache.SerializerPath', CACHE);
434 $body = $purifier->purify($body); 381 $purifier = new HTMLPurifier($config);
382 $title = $purifier->purify($title);
383 $body = $purifier->purify($body);
384 }
385 else {
386 $title = '';
387 $body = '';
388 }
435 389
436 //search for possible duplicate if not in import mode 390 //search for possible duplicate if not in import mode
391 $duplicate = NULL;
437 if (!$import) { 392 if (!$import) {
438 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); 393 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
439 } 394 }
@@ -534,25 +489,33 @@ class Poche
534 Tools::logm('error : article not found'); 489 Tools::logm('error : article not found');
535 Tools::redirect(); 490 Tools::redirect();
536 } 491 }
492 //get all already set tags to preven duplicates
493 $already_set_tags = array();
494 $entry_tags = $this->store->retrieveTagsByEntry($entry_id);
495 foreach ($entry_tags as $tag) {
496 $already_set_tags[] = $tag['value'];
497 }
537 foreach($tags as $key => $tag_value) { 498 foreach($tags as $key => $tag_value) {
538 $value = trim($tag_value); 499 $value = trim($tag_value);
539 $tag = $this->store->retrieveTagByValue($value); 500 if ($value && !in_array($value, $already_set_tags)) {
540 501 $tag = $this->store->retrieveTagByValue($value);
541 if (is_null($tag)) { 502
542 # we create the tag 503 if (is_null($tag)) {
543 $tag = $this->store->createTag($value); 504 # we create the tag
544 $sequence = ''; 505 $tag = $this->store->createTag($value);
545 if (STORAGE == 'postgres') { 506 $sequence = '';
546 $sequence = 'tags_id_seq'; 507 if (STORAGE == 'postgres') {
547 } 508 $sequence = 'tags_id_seq';
548 $tag_id = $this->store->getLastId($sequence); 509 }
510 $tag_id = $this->store->getLastId($sequence);
511 }
512 else {
513 $tag_id = $tag['id'];
514 }
515
516 # we assign the tag to the article
517 $this->store->setTagToEntry($tag_id, $entry_id);
549 } 518 }
550 else {
551 $tag_id = $tag['id'];
552 }
553
554 # we assign the tag to the article
555 $this->store->setTagToEntry($tag_id, $entry_id);
556 } 519 }
557 if(!$import) { 520 if(!$import) {
558 Tools::redirect(); 521 Tools::redirect();
@@ -581,8 +544,12 @@ class Poche
581 switch ($view) 544 switch ($view)
582 { 545 {
583 case 'config': 546 case 'config':
584 $dev = trim($this->getPocheVersion('dev')); 547 $dev_infos = $this->getPocheVersion('dev');
585 $prod = trim($this->getPocheVersion('prod')); 548 $dev = trim($dev_infos[0]);
549 $check_time_dev = date('d-M-Y H:i', $dev_infos[1]);
550 $prod_infos = $this->getPocheVersion('prod');
551 $prod = trim($prod_infos[0]);
552 $check_time_prod = date('d-M-Y H:i', $prod_infos[1]);
586 $compare_dev = version_compare(POCHE, $dev); 553 $compare_dev = version_compare(POCHE, $dev);
587 $compare_prod = version_compare(POCHE, $prod); 554 $compare_prod = version_compare(POCHE, $prod);
588 $themes = $this->getInstalledThemes(); 555 $themes = $this->getInstalledThemes();
@@ -594,6 +561,8 @@ class Poche
594 'languages' => $languages, 561 'languages' => $languages,
595 'dev' => $dev, 562 'dev' => $dev,
596 'prod' => $prod, 563 'prod' => $prod,
564 'check_time_dev' => $check_time_dev,
565 'check_time_prod' => $check_time_prod,
597 'compare_dev' => $compare_dev, 566 'compare_dev' => $compare_dev,
598 'compare_prod' => $compare_prod, 567 'compare_prod' => $compare_prod,
599 'token' => $token, 568 'token' => $token,
@@ -619,7 +588,17 @@ class Poche
619 break; 588 break;
620 case 'tags': 589 case 'tags':
621 $token = $this->user->getConfigValue('token'); 590 $token = $this->user->getConfigValue('token');
622 $tags = $this->store->retrieveAllTags($this->user->getId()); 591 //if term is set - search tags for this term
592 $term = Tools::checkVar('term');
593 $tags = $this->store->retrieveAllTags($this->user->getId(), $term);
594 if (Tools::isAjaxRequest()) {
595 $result = array();
596 foreach ($tags as $tag) {
597 $result[] = $tag['value'];
598 }
599 echo json_encode($result);
600 exit;
601 }
623 $tpl_vars = array( 602 $tpl_vars = array(
624 'token' => $token, 603 'token' => $token,
625 'user_id' => $this->user->getId(), 604 'user_id' => $this->user->getId(),
@@ -660,6 +639,7 @@ class Poche
660 'entries' => '', 639 'entries' => '',
661 'page_links' => '', 640 'page_links' => '',
662 'nb_results' => '', 641 'nb_results' => '',
642 'listmode' => (isset($_COOKIE['listmode']) ? true : false),
663 ); 643 );
664 644
665 //if id is given - we retrive entries by tag: id is tag id 645 //if id is given - we retrive entries by tag: id is tag id
@@ -750,6 +730,8 @@ class Poche
750 $currentConfig['theme'] = $_POST['theme']; 730 $currentConfig['theme'] = $_POST['theme'];
751 731
752 $_SESSION['poche_user']->setConfig($currentConfig); 732 $_SESSION['poche_user']->setConfig($currentConfig);
733
734 $this->emptyCache();
753 735
754 Tools::redirect('?view=config'); 736 Tools::redirect('?view=config');
755 } 737 }
@@ -788,6 +770,8 @@ class Poche
788 $currentConfig['language'] = $_POST['language']; 770 $currentConfig['language'] = $_POST['language'];
789 771
790 $_SESSION['poche_user']->setConfig($currentConfig); 772 $_SESSION['poche_user']->setConfig($currentConfig);
773
774 $this->emptyCache();
791 775
792 Tools::redirect('?view=config'); 776 Tools::redirect('?view=config');
793 } 777 }
@@ -891,7 +875,9 @@ class Poche
891 # the second <ol> is for read links 875 # the second <ol> is for read links
892 $read = 1; 876 $read = 1;
893 } 877 }
894 $this->messages->add('s', _('import from instapaper completed')); 878
879 $unlink = unlink($targetFile);
880 $this->messages->add('s', _('import from instapaper completed. You have to execute the cron to fetch content.'));
895 Tools::logm('import from instapaper completed'); 881 Tools::logm('import from instapaper completed');
896 Tools::redirect(); 882 Tools::redirect();
897 } 883 }
@@ -935,7 +921,9 @@ class Poche
935 # the second <ul> is for read links 921 # the second <ul> is for read links
936 $read = 1; 922 $read = 1;
937 } 923 }
938 $this->messages->add('s', _('import from pocket completed')); 924
925 $unlink = unlink($targetFile);
926 $this->messages->add('s', _('import from pocket completed. You have to execute the cron to fetch content.'));
939 Tools::logm('import from pocket completed'); 927 Tools::logm('import from pocket completed');
940 Tools::redirect(); 928 Tools::redirect();
941 } 929 }
@@ -991,7 +979,9 @@ class Poche
991 } 979 }
992 } 980 }
993 } 981 }
994 $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); 982
983 unlink($targetFile);
984 $this->messages->add('s', _('import from Readability completed. You have to execute the cron to fetch content.'));
995 Tools::logm('import from Readability completed'); 985 Tools::logm('import from Readability completed');
996 Tools::redirect(); 986 Tools::redirect();
997 } 987 }
@@ -1037,7 +1027,9 @@ class Poche
1037 } 1027 }
1038 1028
1039 } 1029 }
1040 $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.')); 1030
1031 unlink($targetFile);
1032 $this->messages->add('s', _('import from Poche completed. You have to execute the cron to fetch content.'));
1041 Tools::logm('import from Poche completed'); 1033 Tools::logm('import from Poche completed');
1042 Tools::redirect(); 1034 Tools::redirect();
1043 } 1035 }
@@ -1062,13 +1054,7 @@ class Poche
1062 Tools::redirect(); 1054 Tools::redirect();
1063 } 1055 }
1064 1056
1065 $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; 1057 $targetFile = CACHE . '/' . constant(strtoupper($from) . '_FILE');
1066 $targetFile = constant($targetDefinition);
1067
1068 if (! defined($targetDefinition)) {
1069 $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".'));
1070 Tools::redirect();
1071 }
1072 1058
1073 if (! file_exists($targetFile)) { 1059 if (! file_exists($targetFile)) {
1074 $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); 1060 $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.'));
@@ -1078,6 +1064,22 @@ class Poche
1078 $this->$providers[$from]($targetFile); 1064 $this->$providers[$from]($targetFile);
1079 } 1065 }
1080 1066
1067 public function uploadFile() {
1068 if(isset($_FILES['file']))
1069 {
1070 $dir = CACHE . '/';
1071 $file = basename($_FILES['file']['name']);
1072 if(move_uploaded_file($_FILES['file']['tmp_name'], $dir . $file)) {
1073 $this->messages->add('s', _('File uploaded. You can now execute import.'));
1074 }
1075 else {
1076 $this->messages->add('e', _('Error while importing file. Do you have access to upload it?'));
1077 }
1078 }
1079
1080 Tools::redirect('?view=config');
1081 }
1082
1081 /** 1083 /**
1082 * export poche entries in json 1084 * export poche entries in json
1083 * @return json all poche entries 1085 * @return json all poche entries
@@ -1099,15 +1101,17 @@ class Poche
1099 private function getPocheVersion($which = 'prod') 1101 private function getPocheVersion($which = 'prod')
1100 { 1102 {
1101 $cache_file = CACHE . '/' . $which; 1103 $cache_file = CACHE . '/' . $which;
1104 $check_time = time();
1102 1105
1103 # checks if the cached version file exists 1106 # checks if the cached version file exists
1104 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { 1107 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
1105 $version = file_get_contents($cache_file); 1108 $version = file_get_contents($cache_file);
1109 $check_time = filemtime($cache_file);
1106 } else { 1110 } else {
1107 $version = file_get_contents('http://static.wallabag.org/versions/' . $which); 1111 $version = file_get_contents('http://static.wallabag.org/versions/' . $which);
1108 file_put_contents($cache_file, $version, LOCK_EX); 1112 file_put_contents($cache_file, $version, LOCK_EX);
1109 } 1113 }
1110 return $version; 1114 return array($version, $check_time);
1111 } 1115 }
1112 1116
1113 public function generateToken() 1117 public function generateToken()
@@ -1124,6 +1128,7 @@ class Poche
1124 $currentConfig = $_SESSION['poche_user']->config; 1128 $currentConfig = $_SESSION['poche_user']->config;
1125 $currentConfig['token'] = $token; 1129 $currentConfig['token'] = $token;
1126 $_SESSION['poche_user']->setConfig($currentConfig); 1130 $_SESSION['poche_user']->setConfig($currentConfig);
1131 Tools::redirect();
1127 } 1132 }
1128 1133
1129 public function generateFeeds($token, $user_id, $tag_id, $type = 'home') 1134 public function generateFeeds($token, $user_id, $tag_id, $type = 'home')
@@ -1131,6 +1136,10 @@ class Poche
1131 $allowed_types = array('home', 'fav', 'archive', 'tag'); 1136 $allowed_types = array('home', 'fav', 'archive', 'tag');
1132 $config = $this->store->getConfigUser($user_id); 1137 $config = $this->store->getConfigUser($user_id);
1133 1138
1139 if ($config == null) {
1140 die(_('User with this id (' . $user_id . ') does not exist.'));
1141 }
1142
1134 if (!in_array($type, $allowed_types) || 1143 if (!in_array($type, $allowed_types) ||
1135 $token != $config['token']) { 1144 $token != $config['token']) {
1136 die(_('Uh, there is a problem while generating feeds.')); 1145 die(_('Uh, there is a problem while generating feeds.'));
@@ -1140,8 +1149,9 @@ class Poche
1140 $feed = new FeedWriter(RSS2); 1149 $feed = new FeedWriter(RSS2);
1141 $feed->setTitle('wallabag — ' . $type . ' feed'); 1150 $feed->setTitle('wallabag — ' . $type . ' feed');
1142 $feed->setLink(Tools::getPocheUrl()); 1151 $feed->setLink(Tools::getPocheUrl());
1143 $feed->setChannelElement('updated', date(DATE_RSS , time())); 1152 $feed->setChannelElement('pubDate', date(DATE_RSS , time()));
1144 $feed->setChannelElement('author', 'wallabag'); 1153 $feed->setChannelElement('generator', 'wallabag');
1154 $feed->setDescription('wallabag ' . $type . ' elements');
1145 1155
1146 if ($type == 'tag') { 1156 if ($type == 'tag') {
1147 $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); 1157 $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id);
@@ -1154,7 +1164,7 @@ class Poche
1154 foreach ($entries as $entry) { 1164 foreach ($entries as $entry) {
1155 $newItem = $feed->createNewItem(); 1165 $newItem = $feed->createNewItem();
1156 $newItem->setTitle($entry['title']); 1166 $newItem->setTitle($entry['title']);
1157 $newItem->setLink(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']); 1167 $newItem->setLink($entry['url']);
1158 $newItem->setDate(time()); 1168 $newItem->setDate(time());
1159 $newItem->setDescription($entry['content']); 1169 $newItem->setDescription($entry['content']);
1160 $feed->addItem($newItem); 1170 $feed->addItem($newItem);