aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Poche.class.php
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-08-03 13:21:11 +0200
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2015-08-03 13:21:11 +0200
commitc766284d522206578e7ec154961e0fce456f70c8 (patch)
treea5246151bf8278a0e8053b9dd1ce9d770073c50b /inc/poche/Poche.class.php
parentc0cb52c07439c974075c302a190a034a8cce4f92 (diff)
parente968ec6c2244aee600358b3c87648b2409c97945 (diff)
downloadwallabag-c766284d522206578e7ec154961e0fce456f70c8.tar.gz
wallabag-c766284d522206578e7ec154961e0fce456f70c8.tar.zst
wallabag-c766284d522206578e7ec154961e0fce456f70c8.zip
Merge pull request #1267 from wallabag/dev1.9.1
1.9.1
Diffstat (limited to 'inc/poche/Poche.class.php')
-rwxr-xr-xinc/poche/Poche.class.php177
1 files changed, 133 insertions, 44 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index fd2600f3..9014f455 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -200,27 +200,34 @@ class Poche
200 200
201 //search for possible duplicate 201 //search for possible duplicate
202 $duplicate = NULL; 202 $duplicate = NULL;
203 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); 203 $clean_url = $url->getUrl();
204 204
205 $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); 205 // Clean URL to remove parameters from feedburner and all this stuff. Taken from Shaarli.
206 $i=strpos($clean_url,'&utm_source='); if ($i!==false) $clean_url=substr($clean_url,0,$i);
207 $i=strpos($clean_url,'?utm_source='); if ($i!==false) $clean_url=substr($clean_url,0,$i);
208 $i=strpos($clean_url,'#xtor=RSS-'); if ($i!==false) $clean_url=substr($clean_url,0,$i);
209
210 $duplicate = $this->store->retrieveOneByURL($clean_url, $this->user->getId());
211
212 $last_id = $this->store->add($clean_url, $title, $body, $this->user->getId());
206 if ( $last_id ) { 213 if ( $last_id ) {
207 Tools::logm('add link ' . $url->getUrl()); 214 Tools::logm('add link ' . $clean_url);
208 if (DOWNLOAD_PICTURES) { 215 if (DOWNLOAD_PICTURES) {
209 $content = Picture::filterPicture($body, $url->getUrl(), $last_id); 216 $content = Picture::filterPicture($body, $clean_url, $last_id);
210 Tools::logm('updating content article'); 217 Tools::logm('updating content article');
211 $this->store->updateContent($last_id, $content, $this->user->getId()); 218 $this->store->updateContent($last_id, $content, $this->user->getId());
212 } 219 }
213 220
214 if ($duplicate != NULL) { 221 if ($duplicate != NULL) {
215 // 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 222 // 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
216 Tools::logm('link ' . $url->getUrl() . ' is a duplicate'); 223 Tools::logm('link ' . $clean_url . ' is a duplicate');
217 // 1) - preserve tags and favorite, then drop old entry 224 // 1) - preserve tags and favorite, then drop old entry
218 $this->store->reassignTags($duplicate['id'], $last_id); 225 $this->store->reassignTags($duplicate['id'], $last_id);
219 if ($duplicate['is_fav']) { 226 if ($duplicate['is_fav']) {
220 $this->store->favoriteById($last_id, $this->user->getId()); 227 $this->store->favoriteById($last_id, $this->user->getId());
221 } 228 }
222 if ($this->store->deleteById($duplicate['id'], $this->user->getId())) { 229 if ($this->store->deleteById($duplicate['id'], $this->user->getId())) {
223 Tools::logm('previous link ' . $url->getUrl() .' entry deleted'); 230 Tools::logm('previous link ' . $clean_url .' entry deleted');
224 } 231 }
225 } 232 }
226 233
@@ -235,7 +242,7 @@ class Poche
235 } 242 }
236 else { 243 else {
237 $this->messages->add('e', _('error during insertion : the link wasn\'t added')); 244 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
238 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); 245 Tools::logm('error during insertion : the link wasn\'t added ' . $clean_url);
239 } 246 }
240 247
241 if ($autoclose == TRUE) { 248 if ($autoclose == TRUE) {
@@ -259,6 +266,15 @@ class Poche
259 } 266 }
260 foreach($entry_ids as $id) { 267 foreach($entry_ids as $id) {
261 $msg = 'delete link #' . $id; 268 $msg = 'delete link #' . $id;
269
270 // deleting tags and tags_entries
271 $tags = $this->store->retrieveTagsByEntry($id);
272 foreach ($tags as $tag) {
273 $this->store->removeTagForEntry($id, $tag['id']);
274 $this->store->cleanUnusedTag($tag['id']);
275 }
276
277 // deleting pictures
262 if ($this->store->deleteById($id, $this->user->getId())) { 278 if ($this->store->deleteById($id, $this->user->getId())) {
263 if (DOWNLOAD_PICTURES) { 279 if (DOWNLOAD_PICTURES) {
264 Picture::removeDirectory(ABS_PATH . $id); 280 Picture::removeDirectory(ABS_PATH . $id);
@@ -303,11 +319,16 @@ class Poche
303 if ( Tools::isAjaxRequest() ) { 319 if ( Tools::isAjaxRequest() ) {
304 echo 1; 320 echo 1;
305 exit; 321 exit;
306 } 322 } else {
307 else {
308 Tools::redirect(); 323 Tools::redirect();
309 } 324 }
310 break; 325 break;
326 case 'archive_and_next' :
327 $nextid = $this->store->getPreviousArticle($id, $this->user->getId());
328 $this->store->archiveById($id, $this->user->getId());
329 Tools::logm('archive link #' . $id);
330 Tools::redirect('?view=view&id=' . $nextid);
331 break;
311 case 'archive_all' : 332 case 'archive_all' :
312 $this->store->archiveAll($this->user->getId()); 333 $this->store->archiveAll($this->user->getId());
313 Tools::logm('archive all links'); 334 Tools::logm('archive all links');
@@ -394,8 +415,9 @@ class Poche
394 /* 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 */ 415 /* 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 */
395 case 'random': 416 case 'random':
396 Tools::logm('get a random article'); 417 Tools::logm('get a random article');
397 if ($this->store->getRandomId($this->user->getId())) { 418 $view = $_GET['view'];
398 $id_array = $this->store->getRandomId($this->user->getId()); 419 if ($this->store->getRandomId($this->user->getId(),$view)) {
420 $id_array = $this->store->getRandomId($this->user->getId(),$view);
399 $id = $id_array[0]; 421 $id = $id_array[0];
400 Tools::redirect('?view=view&id=' . $id[0]); 422 Tools::redirect('?view=view&id=' . $id[0]);
401 Tools::logm('got the article with id ' . $id[0]); 423 Tools::logm('got the article with id ' . $id[0]);
@@ -426,8 +448,9 @@ class Poche
426 $themes = $this->tpl->getInstalledThemes(); 448 $themes = $this->tpl->getInstalledThemes();
427 $languages = $this->language->getInstalledLanguages(); 449 $languages = $this->language->getInstalledLanguages();
428 $token = $this->user->getConfigValue('token'); 450 $token = $this->user->getConfigValue('token');
429 $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; 451 $http_auth = isset($_SERVER['REMOTE_USER']);
430 $only_user = ($this->store->listUsers() > 1) ? false : true; 452 $only_user = ($this->store->listUsers() > 1) ? false : true;
453 $https = substr(Tools::getPocheUrl(), 0, 5) == 'https';
431 $tpl_vars = array( 454 $tpl_vars = array(
432 'themes' => $themes, 455 'themes' => $themes,
433 'languages' => $languages, 456 'languages' => $languages,
@@ -440,7 +463,8 @@ class Poche
440 'token' => $token, 463 'token' => $token,
441 'user_id' => $this->user->getId(), 464 'user_id' => $this->user->getId(),
442 'http_auth' => $http_auth, 465 'http_auth' => $http_auth,
443 'only_user' => $only_user 466 'only_user' => $only_user,
467 'https' => $https
444 ); 468 );
445 Tools::logm('config view'); 469 Tools::logm('config view');
446 break; 470 break;
@@ -453,9 +477,31 @@ class Poche
453 Tools::redirect(); 477 Tools::redirect();
454 } 478 }
455 $tags = $this->store->retrieveTagsByEntry($id); 479 $tags = $this->store->retrieveTagsByEntry($id);
480 $all_tags = $this->store->retrieveAllTags($this->user->getId());
481 $maximus = 0;
482 foreach ($all_tags as $eachtag) { // search for the most times a tag is present
483 if ($eachtag["entriescount"] > $maximus) $maximus = $eachtag["entriescount"];
484 }
485 foreach ($all_tags as $key => $eachtag) { // get the percentage of presence of each tag
486 $percent = floor(($eachtag["entriescount"] / $maximus) * 100);
487
488 if ($percent < 20): // assign a css class, depending on the number of entries count
489 $cssclass = 'smallesttag';
490 elseif ($percent >= 20 and $percent < 40):
491 $cssclass = 'smalltag';
492 elseif ($percent >= 40 and $percent < 60):
493 $cssclass = 'mediumtag';
494 elseif ($percent >= 60 and $percent < 80):
495 $cssclass = 'largetag';
496 else:
497 $cssclass = 'largesttag';
498 endif;
499 $all_tags[$key]['cssclass'] = $cssclass;
500 }
456 $tpl_vars = array( 501 $tpl_vars = array(
457 'entry_id' => $id, 502 'entry_id' => $id,
458 'tags' => $tags, 503 'tags' => $tags,
504 'alltags' => $all_tags,
459 'entry' => $entry, 505 'entry' => $entry,
460 ); 506 );
461 break; 507 break;
@@ -509,6 +555,20 @@ class Poche
509 $flattr->checkItem($entry['url'], $entry['id']); 555 $flattr->checkItem($entry['url'], $entry['id']);
510 } 556 }
511 557
558 # previous and next
559 $previous = FALSE;
560 $previous_id = $this->store->getPreviousArticle($id, $this->user->getId());
561 $next = FALSE;
562 $next_id = $this->store->getNextArticle($id, $this->user->getId());
563
564 if ($this->store->retrieveOneById($previous_id, $this->user->getId())) {
565 $previous = TRUE;
566 }
567 if ($this->store->retrieveOneById($next_id, $this->user->getId())) {
568 $next = TRUE;
569 }
570 $navigate = array('previous' => $previous, 'previousid' => $previous_id, 'next' => $next, 'nextid' => $next_id);
571
512 # tags 572 # tags
513 $tags = $this->store->retrieveTagsByEntry($entry['id']); 573 $tags = $this->store->retrieveTagsByEntry($entry['id']);
514 574
@@ -516,7 +576,8 @@ class Poche
516 'entry' => $entry, 576 'entry' => $entry,
517 'content' => $content, 577 'content' => $content,
518 'flattr' => $flattr, 578 'flattr' => $flattr,
519 'tags' => $tags 579 'tags' => $tags,
580 'navigate' => $navigate
520 ); 581 );
521 } 582 }
522 else { 583 else {
@@ -529,6 +590,7 @@ class Poche
529 'page_links' => '', 590 'page_links' => '',
530 'nb_results' => '', 591 'nb_results' => '',
531 'listmode' => (isset($_COOKIE['listmode']) ? true : false), 592 'listmode' => (isset($_COOKIE['listmode']) ? true : false),
593 'view' => $view,
532 ); 594 );
533 595
534 //if id is given - we retrieve entries by tag: id is tag id 596 //if id is given - we retrieve entries by tag: id is tag id
@@ -539,7 +601,7 @@ class Poche
539 601
540 $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id); 602 $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id);
541 603
542 if ($count > 0) { 604 if ($count && $count > 0) {
543 $this->pagination->set_total($count); 605 $this->pagination->set_total($count);
544 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), 606 $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')),
545 $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' )); 607 $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' ));
@@ -593,9 +655,6 @@ class Poche
593 */ 655 */
594 private function credentials() 656 private function credentials()
595 { 657 {
596 if (isset($_SERVER['PHP_AUTH_USER'])) {
597 return array($_SERVER['PHP_AUTH_USER'], 'php_auth', true);
598 }
599 if (!empty($_POST['login']) && !empty($_POST['password'])) { 658 if (!empty($_POST['login']) && !empty($_POST['password'])) {
600 return array($_POST['login'], $_POST['password'], false); 659 return array($_POST['login'], $_POST['password'], false);
601 } 660 }
@@ -635,6 +694,7 @@ class Poche
635 setlocale(LC_ALL, $language); 694 setlocale(LC_ALL, $language);
636 bindtextdomain($language, LOCALE); 695 bindtextdomain($language, LOCALE);
637 textdomain($language); 696 textdomain($language);
697 bind_textdomain_codeset($language, 'UTF-8');
638 698
639 $this->messages->add('s', _('welcome to your wallabag')); 699 $this->messages->add('s', _('welcome to your wallabag'));
640 Tools::logm('login successful'); 700 Tools::logm('login successful');
@@ -681,23 +741,45 @@ class Poche
681 $html->load_file($_FILES['file']['tmp_name']); 741 $html->load_file($_FILES['file']['tmp_name']);
682 $data = array(); 742 $data = array();
683 $read = 0; 743 $read = 0;
684 foreach (array('ol','ul') as $list) { 744
685 foreach ($html->find($list) as $ul) { 745 if (Tools::get_doctype($html)->innertext == "<!DOCTYPE NETSCAPE-Bookmark-file-1>") {
686 foreach ($ul->find('li') as $li) { 746 // Firefox-bookmarks HTML
687 $tmpEntry = array(); 747 foreach (array('DL','ul') as $list) {
688 $a = $li->find('a'); 748 foreach ($html->find($list) as $ul) {
689 $tmpEntry['url'] = $a[0]->href; 749 foreach ($ul->find('DT') as $li) {
690 $tmpEntry['tags'] = $a[0]->tags; 750 $tmpEntry = array();
691 $tmpEntry['is_read'] = $read; 751 $a = $li->find('A');
692 if ($tmpEntry['url']) { 752 $tmpEntry['url'] = $a[0]->href;
693 $data[] = $tmpEntry; 753 $tmpEntry['tags'] = $a[0]->tags;
754 $tmpEntry['is_read'] = $read;
755 if ($tmpEntry['url']) {
756 $data[] = $tmpEntry;
757 }
694 } 758 }
695 } 759 # the second <ol/ul> is for read links
696 # the second <ol/ul> is for read links 760 $read = ((sizeof($data) && $read)?0:1);
697 $read = ((sizeof($data) && $read)?0:1); 761 }
698 } 762 }
699 } 763 } else {
700 } 764 // regular HTML
765 foreach (array('ol','ul') as $list) {
766 foreach ($html->find($list) as $ul) {
767 foreach ($ul->find('li') as $li) {
768 $tmpEntry = array();
769 $a = $li->find('a');
770 $tmpEntry['url'] = $a[0]->href;
771 $tmpEntry['tags'] = $a[0]->tags;
772 $tmpEntry['is_read'] = $read;
773 if ($tmpEntry['url']) {
774 $data[] = $tmpEntry;
775 }
776 }
777 # the second <ol/ul> is for read links
778 $read = ((sizeof($data) && $read)?0:1);
779 }
780 }
781 }
782 }
701 783
702 // for readability structure 784 // for readability structure
703 785
@@ -715,7 +797,7 @@ class Poche
715 $urlsInserted = array(); //urls of articles inserted 797 $urlsInserted = array(); //urls of articles inserted
716 foreach($data as $record) { 798 foreach($data as $record) {
717 $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : '')); 799 $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : ''));
718 if ($url and !in_array($url, $urlsInserted)) { 800 if (filter_var($url, FILTER_VALIDATE_URL) and !in_array($url, $urlsInserted)) {
719 $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>'); 801 $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>');
720 $body = (isset($record['content']) ? $record['content'] : ''); 802 $body = (isset($record['content']) ? $record['content'] : '');
721 $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0)); 803 $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0));
@@ -820,14 +902,18 @@ class Poche
820 */ 902 */
821 public function export() 903 public function export()
822 { 904 {
823 $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; 905 $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json";
824 header('Content-Disposition: attachment; filename='.$filename); 906 header('Content-Disposition: attachment; filename='.$filename);
825 907
826 $entries = $this->store->retrieveAll($this->user->getId()); 908 $entries = $this->store->retrieveAllWithTags($this->user->getId());
827 echo $this->tpl->render('export.twig', array( 909 if ($entries) {
828 'export' => Tools::renderJson($entries), 910 echo $this->tpl->render('export.twig', array(
829 )); 911 'export' => Tools::renderJson($entries),
830 Tools::logm('export view'); 912 ));
913 Tools::logm('export view');
914 } else {
915 Tools::logm('error accessing database while exporting');
916 }
831 } 917 }
832 918
833 /** 919 /**
@@ -903,7 +989,7 @@ class Poche
903 if (0 == $limit) { 989 if (0 == $limit) {
904 $limit = count($entries); 990 $limit = count($entries);
905 } 991 }
906 if (count($entries) > 0) { 992 if ($entries && count($entries) > 0) {
907 for ($i = 0; $i < min(count($entries), $limit); $i++) { 993 for ($i = 0; $i < min(count($entries), $limit); $i++) {
908 $entry = $entries[$i]; 994 $entry = $entries[$i];
909 $newItem = $feed->createNewItem(); 995 $newItem = $feed->createNewItem();
@@ -915,7 +1001,10 @@ class Poche
915 $feed->addItem($newItem); 1001 $feed->addItem($newItem);
916 } 1002 }
917 } 1003 }
918 1004 else
1005 {
1006 Tools::logm("database error while generating feeds");
1007 }
919 $feed->genarateFeed(); 1008 $feed->genarateFeed();
920 exit; 1009 exit;
921 } 1010 }