diff options
author | Maryana Rozhankivska <mariroz@mr.lviv.ua> | 2014-04-03 12:18:49 +0300 |
---|---|---|
committer | Maryana Rozhankivska <mariroz@mr.lviv.ua> | 2014-04-03 12:18:49 +0300 |
commit | a297fb1e38ce70a2c25f5dd9096fcfbf988ddf05 (patch) | |
tree | 91bcece7c7d959c2350335f8cc96e8e73b84868a /inc/poche/Poche.class.php | |
parent | a4585f7eaa74d5f705c1622ac2419ea1198a7aba (diff) | |
download | wallabag-a297fb1e38ce70a2c25f5dd9096fcfbf988ddf05.tar.gz wallabag-a297fb1e38ce70a2c25f5dd9096fcfbf988ddf05.tar.zst wallabag-a297fb1e38ce70a2c25f5dd9096fcfbf988ddf05.zip |
import w/o cron
Diffstat (limited to 'inc/poche/Poche.class.php')
-rwxr-xr-x | inc/poche/Poche.class.php | 316 |
1 files changed, 46 insertions, 270 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index ee3b2ac2..a662f695 100755 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php | |||
@@ -368,31 +368,23 @@ class Poche | |||
368 | switch ($action) | 368 | switch ($action) |
369 | { | 369 | { |
370 | case 'add': | 370 | case 'add': |
371 | if (!$import) { | 371 | $content = Tools::getPageContent($url); |
372 | $content = Tools::getPageContent($url); | 372 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); |
373 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); | 373 | $body = $content['rss']['channel']['item']['description']; |
374 | $body = $content['rss']['channel']['item']['description']; | 374 | |
375 | 375 | // clean content from prevent xss attack | |
376 | // clean content from prevent xss attack | 376 | $config = HTMLPurifier_Config::createDefault(); |
377 | $config = HTMLPurifier_Config::createDefault(); | 377 | $config->set('Cache.SerializerPath', CACHE); |
378 | $config->set('Cache.SerializerPath', CACHE); | 378 | $purifier = new HTMLPurifier($config); |
379 | $purifier = new HTMLPurifier($config); | 379 | $title = $purifier->purify($title); |
380 | $title = $purifier->purify($title); | 380 | $body = $purifier->purify($body); |
381 | $body = $purifier->purify($body); | ||
382 | } | ||
383 | else { | ||
384 | $title = ''; | ||
385 | $body = ''; | ||
386 | } | ||
387 | 381 | ||
388 | //search for possible duplicate | 382 | //search for possible duplicate |
389 | $duplicate = NULL; | 383 | $duplicate = NULL; |
390 | if (!$import) { | 384 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); |
391 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); | ||
392 | } | ||
393 | 385 | ||
394 | $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); | 386 | $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); |
395 | if ( $last_id && !$import ) { | 387 | if ( $last_id ) { |
396 | Tools::logm('add link ' . $url->getUrl()); | 388 | Tools::logm('add link ' . $url->getUrl()); |
397 | if (DOWNLOAD_PICTURES) { | 389 | if (DOWNLOAD_PICTURES) { |
398 | $content = filtre_picture($body, $url->getUrl(), $last_id); | 390 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
@@ -416,18 +408,14 @@ class Poche | |||
416 | $this->messages->add('s', _('the link has been added successfully')); | 408 | $this->messages->add('s', _('the link has been added successfully')); |
417 | } | 409 | } |
418 | else { | 410 | else { |
419 | if (!$import) { | 411 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
420 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); | 412 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); |
421 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | ||
422 | } | ||
423 | } | 413 | } |
424 | 414 | ||
425 | if (!$import) { | 415 | if ($autoclose == TRUE) { |
426 | if ($autoclose == TRUE) { | 416 | Tools::redirect('?view=home'); |
427 | Tools::redirect('?view=home'); | 417 | } else { |
428 | } else { | 418 | Tools::redirect('?view=home&closewin=true'); |
429 | Tools::redirect('?view=home&closewin=true'); | ||
430 | } | ||
431 | } | 419 | } |
432 | break; | 420 | break; |
433 | case 'delete': | 421 | case 'delete': |
@@ -448,33 +436,21 @@ class Poche | |||
448 | case 'toggle_fav' : | 436 | case 'toggle_fav' : |
449 | $this->store->favoriteById($id, $this->user->getId()); | 437 | $this->store->favoriteById($id, $this->user->getId()); |
450 | Tools::logm('mark as favorite link #' . $id); | 438 | Tools::logm('mark as favorite link #' . $id); |
451 | if (!$import) { | 439 | Tools::redirect(); |
452 | Tools::redirect(); | ||
453 | } | ||
454 | break; | 440 | break; |
455 | case 'toggle_archive' : | 441 | case 'toggle_archive' : |
456 | $this->store->archiveById($id, $this->user->getId()); | 442 | $this->store->archiveById($id, $this->user->getId()); |
457 | Tools::logm('archive link #' . $id); | 443 | Tools::logm('archive link #' . $id); |
458 | if (!$import) { | 444 | Tools::redirect(); |
459 | Tools::redirect(); | ||
460 | } | ||
461 | break; | 445 | break; |
462 | case 'archive_all' : | 446 | case 'archive_all' : |
463 | $this->store->archiveAll($this->user->getId()); | 447 | $this->store->archiveAll($this->user->getId()); |
464 | Tools::logm('archive all links'); | 448 | Tools::logm('archive all links'); |
465 | if (!$import) { | 449 | Tools::redirect(); |
466 | Tools::redirect(); | ||
467 | } | ||
468 | break; | 450 | break; |
469 | case 'add_tag' : | 451 | case 'add_tag' : |
470 | if($import){ | 452 | $tags = explode(',', $_POST['value']); |
471 | $entry_id = $id; | 453 | $entry_id = $_POST['entry_id']; |
472 | $tags = explode(',', $tags); | ||
473 | } | ||
474 | else{ | ||
475 | $tags = explode(',', $_POST['value']); | ||
476 | $entry_id = $_POST['entry_id']; | ||
477 | } | ||
478 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); | 454 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); |
479 | if (!$entry) { | 455 | if (!$entry) { |
480 | $this->messages->add('e', _('Article not found!')); | 456 | $this->messages->add('e', _('Article not found!')); |
@@ -509,9 +485,7 @@ class Poche | |||
509 | $this->store->setTagToEntry($tag_id, $entry_id); | 485 | $this->store->setTagToEntry($tag_id, $entry_id); |
510 | } | 486 | } |
511 | } | 487 | } |
512 | if(!$import) { | 488 | Tools::redirect(); |
513 | Tools::redirect(); | ||
514 | } | ||
515 | break; | 489 | break; |
516 | case 'remove_tag' : | 490 | case 'remove_tag' : |
517 | $tag_id = $_GET['tag_id']; | 491 | $tag_id = $_GET['tag_id']; |
@@ -845,205 +819,18 @@ class Poche | |||
845 | } | 819 | } |
846 | 820 | ||
847 | /** | 821 | /** |
848 | * import from Instapaper. poche needs a ./instapaper-export.html file | ||
849 | * @todo add the return value | ||
850 | * @param string $targetFile the file used for importing | ||
851 | * @return boolean | ||
852 | */ | ||
853 | private function importFromInstapaper($targetFile) | ||
854 | { | ||
855 | # TODO gestion des articles favs | ||
856 | $html = new simple_html_dom(); | ||
857 | $html->load_file($targetFile); | ||
858 | Tools::logm('starting import from instapaper'); | ||
859 | |||
860 | $read = 0; | ||
861 | $errors = array(); | ||
862 | foreach($html->find('ol') as $ul) | ||
863 | { | ||
864 | foreach($ul->find('li') as $li) | ||
865 | { | ||
866 | $a = $li->find('a'); | ||
867 | $url = new Url(base64_encode($a[0]->href)); | ||
868 | $this->action('add', $url, 0, TRUE); | ||
869 | if ($read == '1') { | ||
870 | $sequence = ''; | ||
871 | if (STORAGE == 'postgres') { | ||
872 | $sequence = 'entries_id_seq'; | ||
873 | } | ||
874 | $last_id = $this->store->getLastId($sequence); | ||
875 | $this->action('toggle_archive', $url, $last_id, TRUE); | ||
876 | } | ||
877 | } | ||
878 | |||
879 | # the second <ol> is for read links | ||
880 | $read = 1; | ||
881 | } | ||
882 | |||
883 | $unlink = unlink($targetFile); | ||
884 | $this->messages->add('s', _('import from instapaper completed. You have to execute the cron to fetch content.')); | ||
885 | Tools::logm('import from instapaper completed'); | ||
886 | Tools::redirect(); | ||
887 | } | ||
888 | |||
889 | /** | ||
890 | * import from Pocket. poche needs a ./ril_export.html file | ||
891 | * @todo add the return value | ||
892 | * @param string $targetFile the file used for importing | ||
893 | * @return boolean | ||
894 | */ | ||
895 | private function importFromPocket($targetFile) | ||
896 | { | ||
897 | # TODO gestion des articles favs | ||
898 | $html = new simple_html_dom(); | ||
899 | $html->load_file($targetFile); | ||
900 | Tools::logm('starting import from pocket'); | ||
901 | |||
902 | $read = 0; | ||
903 | $errors = array(); | ||
904 | foreach($html->find('ul') as $ul) | ||
905 | { | ||
906 | foreach($ul->find('li') as $li) | ||
907 | { | ||
908 | $a = $li->find('a'); | ||
909 | $url = new Url(base64_encode($a[0]->href)); | ||
910 | $this->action('add', $url, 0, TRUE); | ||
911 | $sequence = ''; | ||
912 | if (STORAGE == 'postgres') { | ||
913 | $sequence = 'entries_id_seq'; | ||
914 | } | ||
915 | $last_id = $this->store->getLastId($sequence); | ||
916 | if ($read == '1') { | ||
917 | $this->action('toggle_archive', $url, $last_id, TRUE); | ||
918 | } | ||
919 | $tags = $a[0]->tags; | ||
920 | if(!empty($tags)) { | ||
921 | $this->action('add_tag',$url,$last_id,true,false,$tags); | ||
922 | } | ||
923 | } | ||
924 | |||
925 | # the second <ul> is for read links | ||
926 | $read = 1; | ||
927 | } | ||
928 | |||
929 | $unlink = unlink($targetFile); | ||
930 | $this->messages->add('s', _('import from pocket completed. You have to execute the cron to fetch content.')); | ||
931 | Tools::logm('import from pocket completed'); | ||
932 | Tools::redirect(); | ||
933 | } | ||
934 | |||
935 | /** | ||
936 | * import from Readability. poche needs a ./readability file | ||
937 | * @todo add the return value | ||
938 | * @param string $targetFile the file used for importing | ||
939 | * @return boolean | ||
940 | */ | ||
941 | private function importFromReadability($targetFile) | ||
942 | { | ||
943 | # TODO gestion des articles lus / favs | ||
944 | $str_data = file_get_contents($targetFile); | ||
945 | $data = json_decode($str_data,true); | ||
946 | Tools::logm('starting import from Readability'); | ||
947 | $count = 0; | ||
948 | foreach ($data as $key => $value) { | ||
949 | $url = NULL; | ||
950 | $favorite = FALSE; | ||
951 | $archive = FALSE; | ||
952 | foreach ($value as $item) { | ||
953 | foreach ($item as $attr => $value) { | ||
954 | if ($attr == 'article__url') { | ||
955 | $url = new Url(base64_encode($value)); | ||
956 | } | ||
957 | $sequence = ''; | ||
958 | if (STORAGE == 'postgres') { | ||
959 | $sequence = 'entries_id_seq'; | ||
960 | } | ||
961 | if ($value == 'true') { | ||
962 | if ($attr == 'favorite') { | ||
963 | $favorite = TRUE; | ||
964 | } | ||
965 | if ($attr == 'archive') { | ||
966 | $archive = TRUE; | ||
967 | } | ||
968 | } | ||
969 | } | ||
970 | |||
971 | # we can add the url | ||
972 | if (!is_null($url) && $url->isCorrect()) { | ||
973 | $this->action('add', $url, 0, TRUE); | ||
974 | $count++; | ||
975 | if ($favorite) { | ||
976 | $last_id = $this->store->getLastId($sequence); | ||
977 | $this->action('toggle_fav', $url, $last_id, TRUE); | ||
978 | } | ||
979 | if ($archive) { | ||
980 | $last_id = $this->store->getLastId($sequence); | ||
981 | $this->action('toggle_archive', $url, $last_id, TRUE); | ||
982 | } | ||
983 | } | ||
984 | } | ||
985 | } | ||
986 | |||
987 | unlink($targetFile); | ||
988 | $this->messages->add('s', _('import from Readability completed. You have to execute the cron to fetch content.')); | ||
989 | Tools::logm('import from Readability completed'); | ||
990 | Tools::redirect(); | ||
991 | } | ||
992 | |||
993 | /** | ||
994 | * import from Poche exported file | ||
995 | * @param string $targetFile the file used for importing | ||
996 | * @return boolean | ||
997 | */ | ||
998 | private function importFromPoche($targetFile) | ||
999 | { | ||
1000 | $str_data = file_get_contents($targetFile); | ||
1001 | $data = json_decode($str_data,true); | ||
1002 | Tools::logm('starting import from Poche'); | ||
1003 | |||
1004 | |||
1005 | $sequence = ''; | ||
1006 | if (STORAGE == 'postgres') { | ||
1007 | $sequence = 'entries_id_seq'; | ||
1008 | } | ||
1009 | |||
1010 | $count = 0; | ||
1011 | foreach ($data as $value) { | ||
1012 | |||
1013 | $url = new Url(base64_encode($value['url'])); | ||
1014 | $favorite = ($value['is_fav'] == -1); | ||
1015 | $archive = ($value['is_read'] == -1); | ||
1016 | |||
1017 | # we can add the url | ||
1018 | if (!is_null($url) && $url->isCorrect()) { | ||
1019 | |||
1020 | $this->action('add', $url, 0, TRUE); | ||
1021 | |||
1022 | $count++; | ||
1023 | if ($favorite) { | ||
1024 | $last_id = $this->store->getLastId($sequence); | ||
1025 | $this->action('toggle_fav', $url, $last_id, TRUE); | ||
1026 | } | ||
1027 | if ($archive) { | ||
1028 | $last_id = $this->store->getLastId($sequence); | ||
1029 | $this->action('toggle_archive', $url, $last_id, TRUE); | ||
1030 | } | ||
1031 | } | ||
1032 | |||
1033 | } | ||
1034 | |||
1035 | unlink($targetFile); | ||
1036 | $this->messages->add('s', _('import from Poche completed. You have to execute the cron to fetch content.')); | ||
1037 | Tools::logm('import from Poche completed'); | ||
1038 | Tools::redirect(); | ||
1039 | } | ||
1040 | |||
1041 | /** | ||
1042 | * import datas into your poche | 822 | * import datas into your poche |
1043 | * @return boolean | 823 | * @return boolean |
1044 | */ | 824 | */ |
1045 | public function import() { | 825 | public function import() { |
1046 | 826 | ||
827 | if (!defined('IMPORT_LIMIT')) { | ||
828 | define('IMPORT_LIMIT', 5); | ||
829 | } | ||
830 | if (!defined('IMPORT_DELAY')) { | ||
831 | define('IMPORT_DELAY', 5); | ||
832 | } | ||
833 | |||
1047 | if ( isset($_FILES['file']) ) { | 834 | if ( isset($_FILES['file']) ) { |
1048 | // assume, that file is in json format | 835 | // assume, that file is in json format |
1049 | $str_data = file_get_contents($_FILES['file']['tmp_name']); | 836 | $str_data = file_get_contents($_FILES['file']['tmp_name']); |
@@ -1073,21 +860,26 @@ class Poche | |||
1073 | } | 860 | } |
1074 | } | 861 | } |
1075 | 862 | ||
863 | //for readability structure | ||
864 | foreach ($data as $record) { | ||
865 | if (is_array($record)) { | ||
866 | $data[] = $record; | ||
867 | foreach ($record as $record2) { | ||
868 | if (is_array($record2)) { | ||
869 | $data[] = $record2; | ||
870 | } | ||
871 | } | ||
872 | } | ||
873 | } | ||
874 | |||
1076 | $i = 0; //counter for articles inserted | 875 | $i = 0; //counter for articles inserted |
1077 | foreach ($data as $record) { | 876 | foreach ($data as $record) { |
1078 | //echo '<pre>'; | 877 | $url = trim( isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : '') ); |
1079 | //var_dump($record); | ||
1080 | // foreach ($record as $key=>$val) { | ||
1081 | // echo "\n=================\n$i: $key: $val\n"; | ||
1082 | // } | ||
1083 | // exit; | ||
1084 | |||
1085 | $url = trim($record['url']); | ||
1086 | if ( $url ) { | 878 | if ( $url ) { |
1087 | $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ').'</a> <a href="./?import">'._('click to finish import').'</a><a>'); | 879 | $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ').'</a> <a href="./?import">'._('click to finish import').'</a><a>'); |
1088 | $body = (isset($record['content']) ? $record['content'] : ''); | 880 | $body = (isset($record['content']) ? $record['content'] : ''); |
1089 | $isRead = (isset($record['is_read']) ? intval($record['is_read']) : 0); | 881 | $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive'])?intval($record['archive']):0)); |
1090 | $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : 0); | 882 | $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite'])?intval($record['favorite']):0) ); |
1091 | //insert new record | 883 | //insert new record |
1092 | $id = $this->store->add($url, $title, $body, $this->user->getId(), $isFavorite, $isRead); | 884 | $id = $this->store->add($url, $title, $body, $this->user->getId(), $isFavorite, $isRead); |
1093 | if ( $id ) { | 885 | if ( $id ) { |
@@ -1146,22 +938,6 @@ class Poche | |||
1146 | return array('includeImport'=>true, 'import'=>array('recordsDownloadRequired'=>$recordsDownloadRequired, 'recordsUnderDownload'=> IMPORT_LIMIT, 'delay'=> IMPORT_DELAY * 1000) ); | 938 | return array('includeImport'=>true, 'import'=>array('recordsDownloadRequired'=>$recordsDownloadRequired, 'recordsUnderDownload'=> IMPORT_LIMIT, 'delay'=> IMPORT_DELAY * 1000) ); |
1147 | } | 939 | } |
1148 | 940 | ||
1149 | public function uploadFile() { | ||
1150 | if (isset($_FILES['file'])) | ||
1151 | { | ||
1152 | $dir = CACHE . '/'; | ||
1153 | $file = basename($_FILES['file']['name']); | ||
1154 | if(move_uploaded_file($_FILES['file']['tmp_name'], $dir . $file)) { | ||
1155 | $this->messages->add('s', _('File uploaded. You can now execute import.')); | ||
1156 | } | ||
1157 | else { | ||
1158 | $this->messages->add('e', _('Error while importing file. Do you have access to upload it?')); | ||
1159 | } | ||
1160 | } | ||
1161 | |||
1162 | Tools::redirect('?view=config'); | ||
1163 | } | ||
1164 | |||
1165 | /** | 941 | /** |
1166 | * export poche entries in json | 942 | * export poche entries in json |
1167 | * @return json all poche entries | 943 | * @return json all poche entries |