aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Poche.class.php
diff options
context:
space:
mode:
authorMaryana Rozhankivska <mariroz@mr.lviv.ua>2014-04-02 20:55:19 +0300
committerMaryana Rozhankivska <mariroz@mr.lviv.ua>2014-04-02 20:55:19 +0300
commit182faf2696b38502d4784efc3f84f2f00290343f (patch)
tree619e725c1820773dcd8bb1fe9cbd07c0c325e865 /inc/poche/Poche.class.php
parent22db488d21bc23dc28fc2d60574803525fe53b53 (diff)
downloadwallabag-182faf2696b38502d4784efc3f84f2f00290343f.tar.gz
wallabag-182faf2696b38502d4784efc3f84f2f00290343f.tar.zst
wallabag-182faf2696b38502d4784efc3f84f2f00290343f.zip
import without cron
Diffstat (limited to 'inc/poche/Poche.class.php')
-rwxr-xr-xinc/poche/Poche.class.php292
1 files changed, 182 insertions, 110 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 7d9faed1..d1b0c945 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -18,7 +18,7 @@ class Poche
18 public $tpl; 18 public $tpl;
19 public $messages; 19 public $messages;
20 public $pagination; 20 public $pagination;
21 21
22 private $currentTheme = ''; 22 private $currentTheme = '';
23 private $currentLanguage = ''; 23 private $currentLanguage = '';
24 private $notInstalledMessage = array(); 24 private $notInstalledMessage = array();
@@ -42,11 +42,11 @@ class Poche
42 if ($this->configFileIsAvailable()) { 42 if ($this->configFileIsAvailable()) {
43 $this->init(); 43 $this->init();
44 } 44 }
45 45
46 if ($this->themeIsInstalled()) { 46 if ($this->themeIsInstalled()) {
47 $this->initTpl(); 47 $this->initTpl();
48 } 48 }
49 49
50 if ($this->systemIsInstalled()) { 50 if ($this->systemIsInstalled()) {
51 $this->store = new Database(); 51 $this->store = new Database();
52 $this->messages = new Messages(); 52 $this->messages = new Messages();
@@ -57,8 +57,8 @@ class Poche
57 $this->store->checkTags(); 57 $this->store->checkTags();
58 } 58 }
59 } 59 }
60 60
61 private function init() 61 private function init()
62 { 62 {
63 Tools::initPhp(); 63 Tools::initPhp();
64 64
@@ -74,28 +74,28 @@ class Poche
74 $language = $this->user->getConfigValue('language'); 74 $language = $this->user->getConfigValue('language');
75 putenv('LC_ALL=' . $language); 75 putenv('LC_ALL=' . $language);
76 setlocale(LC_ALL, $language); 76 setlocale(LC_ALL, $language);
77 bindtextdomain($language, LOCALE); 77 bindtextdomain($language, LOCALE);
78 textdomain($language); 78 textdomain($language);
79 79
80 # Pagination 80 # Pagination
81 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); 81 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
82 82
83 # Set up theme 83 # Set up theme
84 $themeDirectory = $this->user->getConfigValue('theme'); 84 $themeDirectory = $this->user->getConfigValue('theme');
85 85
86 if ($themeDirectory === false) { 86 if ($themeDirectory === false) {
87 $themeDirectory = DEFAULT_THEME; 87 $themeDirectory = DEFAULT_THEME;
88 } 88 }
89 89
90 $this->currentTheme = $themeDirectory; 90 $this->currentTheme = $themeDirectory;
91 91
92 # Set up language 92 # Set up language
93 $languageDirectory = $this->user->getConfigValue('language'); 93 $languageDirectory = $this->user->getConfigValue('language');
94 94
95 if ($languageDirectory === false) { 95 if ($languageDirectory === false) {
96 $languageDirectory = DEFAULT_THEME; 96 $languageDirectory = DEFAULT_THEME;
97 } 97 }
98 98
99 $this->currentLanguage = $languageDirectory; 99 $this->currentLanguage = $languageDirectory;
100 } 100 }
101 101
@@ -108,7 +108,7 @@ class Poche
108 108
109 return true; 109 return true;
110 } 110 }
111 111
112 public function themeIsInstalled() { 112 public function themeIsInstalled() {
113 $passTheme = TRUE; 113 $passTheme = TRUE;
114 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet 114 # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet
@@ -123,27 +123,27 @@ class Poche
123 self::$canRenderTemplates = false; 123 self::$canRenderTemplates = false;
124 124
125 $passTheme = FALSE; 125 $passTheme = FALSE;
126 } 126 }
127 127
128 # Check if the selected theme and its requirements are present 128 # Check if the selected theme and its requirements are present
129 $theme = $this->getTheme(); 129 $theme = $this->getTheme();
130 130
131 if ($theme != '' && ! is_dir(THEME . '/' . $theme)) { 131 if ($theme != '' && ! is_dir(THEME . '/' . $theme)) {
132 $this->notInstalledMessage[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $theme . ')'; 132 $this->notInstalledMessage[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $theme . ')';
133 133
134 self::$canRenderTemplates = false; 134 self::$canRenderTemplates = false;
135 135
136 $passTheme = FALSE; 136 $passTheme = FALSE;
137 } 137 }
138 138
139 $themeInfo = $this->getThemeInfo($theme); 139 $themeInfo = $this->getThemeInfo($theme);
140 if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { 140 if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) {
141 foreach ($themeInfo['requirements'] as $requiredTheme) { 141 foreach ($themeInfo['requirements'] as $requiredTheme) {
142 if (! is_dir(THEME . '/' . $requiredTheme)) { 142 if (! is_dir(THEME . '/' . $requiredTheme)) {
143 $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')'; 143 $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')';
144 144
145 self::$canRenderTemplates = false; 145 self::$canRenderTemplates = false;
146 146
147 $passTheme = FALSE; 147 $passTheme = FALSE;
148 } 148 }
149 } 149 }
@@ -153,21 +153,21 @@ class Poche
153 return FALSE; 153 return FALSE;
154 } 154 }
155 155
156 156
157 return true; 157 return true;
158 } 158 }
159 159
160 /** 160 /**
161 * all checks before installation. 161 * all checks before installation.
162 * @todo move HTML to template 162 * @todo move HTML to template
163 * @return boolean 163 * @return boolean
164 */ 164 */
165 public function systemIsInstalled() 165 public function systemIsInstalled()
166 { 166 {
167 $msg = TRUE; 167 $msg = TRUE;
168 168
169 $configSalt = defined('SALT') ? constant('SALT') : ''; 169 $configSalt = defined('SALT') ? constant('SALT') : '';
170 170
171 if (empty($configSalt)) { 171 if (empty($configSalt)) {
172 $this->notInstalledMessage[] = 'You have not yet filled in the SALT value in the config.inc.php file.'; 172 $this->notInstalledMessage[] = 'You have not yet filled in the SALT value in the config.inc.php file.';
173 $msg = FALSE; 173 $msg = FALSE;
@@ -193,7 +193,7 @@ class Poche
193 193
194 return true; 194 return true;
195 } 195 }
196 196
197 public function getNotInstalledMessage() { 197 public function getNotInstalledMessage() {
198 return $this->notInstalledMessage; 198 return $this->notInstalledMessage;
199 } 199 }
@@ -202,7 +202,7 @@ class Poche
202 { 202 {
203 $loaderChain = new Twig_Loader_Chain(); 203 $loaderChain = new Twig_Loader_Chain();
204 $theme = $this->getTheme(); 204 $theme = $this->getTheme();
205 205
206 # add the current theme as first to the loader chain so Twig will look there first for overridden template files 206 # add the current theme as first to the loader chain so Twig will look there first for overridden template files
207 try { 207 try {
208 $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme)); 208 $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme));
@@ -210,7 +210,7 @@ class Poche
210 # @todo isInstalled() should catch this, inject Twig later 210 # @todo isInstalled() should catch this, inject Twig later
211 die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme .' is missing)'); 211 die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme .' is missing)');
212 } 212 }
213 213
214 # add all required themes to the loader chain 214 # add all required themes to the loader chain
215 $themeInfo = $this->getThemeInfo($theme); 215 $themeInfo = $this->getThemeInfo($theme);
216 if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { 216 if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) {
@@ -223,16 +223,16 @@ class Poche
223 } 223 }
224 } 224 }
225 } 225 }
226 226
227 if (DEBUG_POCHE) { 227 if (DEBUG_POCHE) {
228 $twigParams = array(); 228 $twigParams = array();
229 } else { 229 } else {
230 $twigParams = array('cache' => CACHE); 230 $twigParams = array('cache' => CACHE);
231 } 231 }
232 232
233 $this->tpl = new Twig_Environment($loaderChain, $twigParams); 233 $this->tpl = new Twig_Environment($loaderChain, $twigParams);
234 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); 234 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
235 235
236 # filter to display domain name of an url 236 # filter to display domain name of an url
237 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); 237 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
238 $this->tpl->addFilter($filter); 238 $this->tpl->addFilter($filter);
@@ -251,7 +251,7 @@ class Poche
251 'poche_url' => Tools::getPocheUrl() 251 'poche_url' => Tools::getPocheUrl()
252 )); 252 ));
253 if (isset($_GET['install'])) { 253 if (isset($_GET['install'])) {
254 if (($_POST['password'] == $_POST['password_repeat']) 254 if (($_POST['password'] == $_POST['password_repeat'])
255 && $_POST['password'] != "" && $_POST['login'] != "") { 255 && $_POST['password'] != "" && $_POST['login'] != "") {
256 # let's rock, install poche baby ! 256 # let's rock, install poche baby !
257 if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) 257 if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])))
@@ -268,7 +268,7 @@ class Poche
268 } 268 }
269 exit(); 269 exit();
270 } 270 }
271 271
272 public function getTheme() { 272 public function getTheme() {
273 return $this->currentTheme; 273 return $this->currentTheme;
274 } 274 }
@@ -293,7 +293,7 @@ class Poche
293 if (is_file($themeIniFile) && is_readable($themeIniFile)) { 293 if (is_file($themeIniFile) && is_readable($themeIniFile)) {
294 $themeInfo = parse_ini_file($themeIniFile); 294 $themeInfo = parse_ini_file($themeIniFile);
295 } 295 }
296 296
297 if ($themeInfo === false) { 297 if ($themeInfo === false) {
298 $themeInfo = array(); 298 $themeInfo = array();
299 } 299 }
@@ -304,7 +304,7 @@ class Poche
304 304
305 return $themeInfo; 305 return $themeInfo;
306 } 306 }
307 307
308 public function getInstalledThemes() { 308 public function getInstalledThemes() {
309 $handle = opendir(THEME); 309 $handle = opendir(THEME);
310 $themes = array(); 310 $themes = array();
@@ -331,28 +331,28 @@ class Poche
331 public function getInstalledLanguages() { 331 public function getInstalledLanguages() {
332 $handle = opendir(LOCALE); 332 $handle = opendir(LOCALE);
333 $languages = array(); 333 $languages = array();
334 334
335 while (($language = readdir($handle)) !== false) { 335 while (($language = readdir($handle)) !== false) {
336 # Languages are stored in a directory, so all directory names are languages 336 # Languages are stored in a directory, so all directory names are languages
337 # @todo move language installation data to database 337 # @todo move language installation data to database
338 if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) { 338 if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) {
339 continue; 339 continue;
340 } 340 }
341 341
342 $current = false; 342 $current = false;
343 343
344 if ($language === $this->getLanguage()) { 344 if ($language === $this->getLanguage()) {
345 $current = true; 345 $current = true;
346 } 346 }
347 347
348 $languages[] = array('name' => (isset($this->language_names[$language]) ? $this->language_names[$language] : $language), 'value' => $language, 'current' => $current); 348 $languages[] = array('name' => (isset($this->language_names[$language]) ? $this->language_names[$language] : $language), 'value' => $language, 'current' => $current);
349 } 349 }
350 350
351 return $languages; 351 return $languages;
352 } 352 }
353 353
354 public function getDefaultConfig() 354 public function getDefaultConfig()
355 { 355 {
356 return array( 356 return array(
357 'pager' => PAGINATION, 357 'pager' => PAGINATION,
358 'language' => LANG, 358 'language' => LANG,
@@ -385,19 +385,15 @@ class Poche
385 $body = ''; 385 $body = '';
386 } 386 }
387 387
388 //search for possible duplicate if not in import mode 388 //search for possible duplicate
389 $duplicate = NULL; 389 $duplicate = NULL;
390 if (!$import) { 390 if (!$import) {
391 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); 391 $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
392 } 392 }
393 393
394 if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { 394 $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId());
395 if ( $last_id && !$import ) {
395 Tools::logm('add link ' . $url->getUrl()); 396 Tools::logm('add link ' . $url->getUrl());
396 $sequence = '';
397 if (STORAGE == 'postgres') {
398 $sequence = 'entries_id_seq';
399 }
400 $last_id = $this->store->getLastId($sequence);
401 if (DOWNLOAD_PICTURES) { 397 if (DOWNLOAD_PICTURES) {
402 $content = filtre_picture($body, $url->getUrl(), $last_id); 398 $content = filtre_picture($body, $url->getUrl(), $last_id);
403 Tools::logm('updating content article'); 399 Tools::logm('updating content article');
@@ -417,9 +413,7 @@ class Poche
417 } 413 }
418 } 414 }
419 415
420 if (!$import) { 416 $this->messages->add('s', _('the link has been added successfully'));
421 $this->messages->add('s', _('the link has been added successfully'));
422 }
423 } 417 }
424 else { 418 else {
425 if (!$import) { 419 if (!$import) {
@@ -603,7 +597,7 @@ class Poche
603 'tags' => $tags, 597 'tags' => $tags,
604 ); 598 );
605 break; 599 break;
606 600
607 case 'search': 601 case 'search':
608 if (isset($_GET['search'])){ 602 if (isset($_GET['search'])){
609 $search = $_GET['search']; 603 $search = $_GET['search'];
@@ -647,7 +641,7 @@ class Poche
647 'nb_results' => '', 641 'nb_results' => '',
648 'listmode' => (isset($_COOKIE['listmode']) ? true : false), 642 'listmode' => (isset($_COOKIE['listmode']) ? true : false),
649 ); 643 );
650 644
651 //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
652 if ($id) { 646 if ($id) {
653 $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId()); 647 $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId());
@@ -672,8 +666,8 @@ class Poche
672 } 666 }
673 667
674 /** 668 /**
675 * update the password of the current user. 669 * update the password of the current user.
676 * if MODE_DEMO is TRUE, the password can't be updated. 670 * if MODE_DEMO is TRUE, the password can't be updated.
677 * @todo add the return value 671 * @todo add the return value
678 * @todo set the new password in function header like this updatePassword($newPassword) 672 * @todo set the new password in function header like this updatePassword($newPassword)
679 * @return boolean 673 * @return boolean
@@ -701,44 +695,44 @@ class Poche
701 } 695 }
702 } 696 }
703 } 697 }
704 698
705 public function updateTheme() 699 public function updateTheme()
706 { 700 {
707 # no data 701 # no data
708 if (empty($_POST['theme'])) { 702 if (empty($_POST['theme'])) {
709 } 703 }
710 704
711 # we are not going to change it to the current theme... 705 # we are not going to change it to the current theme...
712 if ($_POST['theme'] == $this->getTheme()) { 706 if ($_POST['theme'] == $this->getTheme()) {
713 $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); 707 $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!'));
714 Tools::redirect('?view=config'); 708 Tools::redirect('?view=config');
715 } 709 }
716 710
717 $themes = $this->getInstalledThemes(); 711 $themes = $this->getInstalledThemes();
718 $actualTheme = false; 712 $actualTheme = false;
719 713
720 foreach (array_keys($themes) as $theme) { 714 foreach (array_keys($themes) as $theme) {
721 if ($theme == $_POST['theme']) { 715 if ($theme == $_POST['theme']) {
722 $actualTheme = true; 716 $actualTheme = true;
723 break; 717 break;
724 } 718 }
725 } 719 }
726 720
727 if (! $actualTheme) { 721 if (! $actualTheme) {
728 $this->messages->add('e', _('that theme does not seem to be installed')); 722 $this->messages->add('e', _('that theme does not seem to be installed'));
729 Tools::redirect('?view=config'); 723 Tools::redirect('?view=config');
730 } 724 }
731 725
732 $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); 726 $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']);
733 $this->messages->add('s', _('you have changed your theme preferences')); 727 $this->messages->add('s', _('you have changed your theme preferences'));
734 728
735 $currentConfig = $_SESSION['poche_user']->config; 729 $currentConfig = $_SESSION['poche_user']->config;
736 $currentConfig['theme'] = $_POST['theme']; 730 $currentConfig['theme'] = $_POST['theme'];
737 731
738 $_SESSION['poche_user']->setConfig($currentConfig); 732 $_SESSION['poche_user']->setConfig($currentConfig);
739 733
740 $this->emptyCache(); 734 $this->emptyCache();
741 735
742 Tools::redirect('?view=config'); 736 Tools::redirect('?view=config');
743 } 737 }
744 738
@@ -747,40 +741,40 @@ class Poche
747 # no data 741 # no data
748 if (empty($_POST['language'])) { 742 if (empty($_POST['language'])) {
749 } 743 }
750 744
751 # we are not going to change it to the current language... 745 # we are not going to change it to the current language...
752 if ($_POST['language'] == $this->getLanguage()) { 746 if ($_POST['language'] == $this->getLanguage()) {
753 $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); 747 $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!'));
754 Tools::redirect('?view=config'); 748 Tools::redirect('?view=config');
755 } 749 }
756 750
757 $languages = $this->getInstalledLanguages(); 751 $languages = $this->getInstalledLanguages();
758 $actualLanguage = false; 752 $actualLanguage = false;
759 753
760 foreach ($languages as $language) { 754 foreach ($languages as $language) {
761 if ($language['value'] == $_POST['language']) { 755 if ($language['value'] == $_POST['language']) {
762 $actualLanguage = true; 756 $actualLanguage = true;
763 break; 757 break;
764 } 758 }
765 } 759 }
766 760
767 if (! $actualLanguage) { 761 if (! $actualLanguage) {
768 $this->messages->add('e', _('that language does not seem to be installed')); 762 $this->messages->add('e', _('that language does not seem to be installed'));
769 Tools::redirect('?view=config'); 763 Tools::redirect('?view=config');
770 } 764 }
771 765
772 $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); 766 $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']);
773 $this->messages->add('s', _('you have changed your language preferences')); 767 $this->messages->add('s', _('you have changed your language preferences'));
774 768
775 $currentConfig = $_SESSION['poche_user']->config; 769 $currentConfig = $_SESSION['poche_user']->config;
776 $currentConfig['language'] = $_POST['language']; 770 $currentConfig['language'] = $_POST['language'];
777 771
778 $_SESSION['poche_user']->setConfig($currentConfig); 772 $_SESSION['poche_user']->setConfig($currentConfig);
779 773
780 $this->emptyCache(); 774 $this->emptyCache();
781 775
782 Tools::redirect('?view=config'); 776 Tools::redirect('?view=config');
783 } 777 }
784 /** 778 /**
785 * get credentials from differents sources 779 * get credentials from differents sources
786 * it redirects the user to the $referer link 780 * it redirects the user to the $referer link
@@ -835,7 +829,7 @@ class Poche
835 /** 829 /**
836 * log out the poche user. It cleans the session. 830 * log out the poche user. It cleans the session.
837 * @todo add the return value 831 * @todo add the return value
838 * @return boolean 832 * @return boolean
839 */ 833 */
840 public function logout() 834 public function logout()
841 { 835 {
@@ -891,7 +885,7 @@ class Poche
891 * import from Pocket. poche needs a ./ril_export.html file 885 * import from Pocket. poche needs a ./ril_export.html file
892 * @todo add the return value 886 * @todo add the return value
893 * @param string $targetFile the file used for importing 887 * @param string $targetFile the file used for importing
894 * @return boolean 888 * @return boolean
895 */ 889 */
896 private function importFromPocket($targetFile) 890 private function importFromPocket($targetFile)
897 { 891 {
@@ -922,7 +916,7 @@ class Poche
922 $this->action('add_tag',$url,$last_id,true,false,$tags); 916 $this->action('add_tag',$url,$last_id,true,false,$tags);
923 } 917 }
924 } 918 }
925 919
926 # the second <ul> is for read links 920 # the second <ul> is for read links
927 $read = 1; 921 $read = 1;
928 } 922 }
@@ -937,7 +931,7 @@ class Poche
937 * import from Readability. poche needs a ./readability file 931 * import from Readability. poche needs a ./readability file
938 * @todo add the return value 932 * @todo add the return value
939 * @param string $targetFile the file used for importing 933 * @param string $targetFile the file used for importing
940 * @return boolean 934 * @return boolean
941 */ 935 */
942 private function importFromReadability($targetFile) 936 private function importFromReadability($targetFile)
943 { 937 {
@@ -994,7 +988,7 @@ class Poche
994 /** 988 /**
995 * import from Poche exported file 989 * import from Poche exported file
996 * @param string $targetFile the file used for importing 990 * @param string $targetFile the file used for importing
997 * @return boolean 991 * @return boolean
998 */ 992 */
999 private function importFromPoche($targetFile) 993 private function importFromPoche($targetFile)
1000 { 994 {
@@ -1014,12 +1008,12 @@ class Poche
1014 $url = new Url(base64_encode($value['url'])); 1008 $url = new Url(base64_encode($value['url']));
1015 $favorite = ($value['is_fav'] == -1); 1009 $favorite = ($value['is_fav'] == -1);
1016 $archive = ($value['is_read'] == -1); 1010 $archive = ($value['is_read'] == -1);
1017 1011
1018 # we can add the url 1012 # we can add the url
1019 if (!is_null($url) && $url->isCorrect()) { 1013 if (!is_null($url) && $url->isCorrect()) {
1020 1014
1021 $this->action('add', $url, 0, TRUE); 1015 $this->action('add', $url, 0, TRUE);
1022 1016
1023 $count++; 1017 $count++;
1024 if ($favorite) { 1018 if ($favorite) {
1025 $last_id = $this->store->getLastId($sequence); 1019 $last_id = $this->store->getLastId($sequence);
@@ -1030,7 +1024,7 @@ class Poche
1030 $this->action('toggle_archive', $url, $last_id, TRUE); 1024 $this->action('toggle_archive', $url, $last_id, TRUE);
1031 } 1025 }
1032 } 1026 }
1033 1027
1034 } 1028 }
1035 1029
1036 unlink($targetFile); 1030 unlink($targetFile);
@@ -1041,37 +1035,115 @@ class Poche
1041 1035
1042 /** 1036 /**
1043 * import datas into your poche 1037 * import datas into your poche
1044 * @param string $from name of the service to import : pocket, instapaper or readability 1038 * @return boolean
1045 * @todo add the return value
1046 * @return boolean
1047 */ 1039 */
1048 public function import($from) 1040 public function import() {
1049 { 1041
1050 $providers = array( 1042 if ( isset($_FILES['file']) ) {
1051 'pocket' => 'importFromPocket', 1043 // assume, that file is in json format
1052 'readability' => 'importFromReadability', 1044 $str_data = file_get_contents($_FILES['file']['tmp_name']);
1053 'instapaper' => 'importFromInstapaper', 1045 $data = json_decode($str_data, true);
1054 'poche' => 'importFromPoche', 1046
1055 ); 1047 if ( $data === null ) {
1056 1048 //not json - assume html
1057 if (! isset($providers[$from])) { 1049 $html = new simple_html_dom();
1058 $this->messages->add('e', _('Unknown import provider.')); 1050 $html->load_file($_FILES['file']['tmp_name']);
1059 Tools::redirect(); 1051 $data = array();
1052 $read = 0;
1053 foreach (array('ol','ul') as $list) {
1054 foreach ($html->find($list) as $ul) {
1055 foreach ($ul->find('li') as $li) {
1056 $tmpEntry = array();
1057 $a = $li->find('a');
1058 $tmpEntry['url'] = $a[0]->href;
1059 $tmpEntry['tags'] = $a[0]->tags;
1060 $tmpEntry['is_read'] = $read;
1061 if ($tmpEntry['url']) {
1062 $data[] = $tmpEntry;
1063 }
1064 }
1065 # the second <ol/ul> is for read links
1066 $read = ((sizeof($data) && $read)?0:1);
1067 }
1068 }
1060 } 1069 }
1061 1070
1062 $targetFile = CACHE . '/' . constant(strtoupper($from) . '_FILE'); 1071 $i = 0; //counter for articles inserted
1063 1072 foreach ($data as $record) {
1064 if (! file_exists($targetFile)) { 1073 //echo '<pre>';
1065 $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); 1074 //var_dump($record);
1066 Tools::redirect(); 1075 // foreach ($record as $key=>$val) {
1076 // echo "\n=================\n$i: $key: $val\n";
1077 // }
1078 // exit;
1079
1080 $url = trim($record['url']);
1081 if ( $url ) {
1082 $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ').'</a> <a href="./?import">'._('click to finish import').'</a><a>');
1083 $body = (isset($record['content']) ? $record['content'] : '');
1084 $isRead = (isset($record['is_read']) ? intval($record['is_read']) : 0);
1085 $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : 0);
1086 //insert new record
1087 $id = $this->store->add($url, $title, $body, $this->user->getId(), $isFavorite, $isRead);
1088 if ( $id ) {
1089 //increment no of records inserted
1090 $i++;
1091 if ( isset($record['tags']) && trim($record['tags']) ) {
1092 //@TODO: set tags
1093
1094 }
1095 }
1096 }
1097 }
1098
1099 if ( $i > 0 ) {
1100 $this->messages->add('s', _('Articles inserted: ').$i._('. Please note, that some may be marked as "read".'));
1101 }
1102 }
1103 //file parsing finished here
1104
1105 //now download article contents if any
1106
1107 //check if we need to download any content
1108 $recordsDownloadRequired = $this->store->retrieveUnfetchedEntriesCount($this->user->getId());
1109 if ( $recordsDownloadRequired == 0 ) {
1110 //nothing to download
1111 $this->messages->add('s', _('Import finished.'));
1112 Tools::redirect();
1113 }
1114 else {
1115 //if just inserted - don't download anything, download will start in next reload
1116 if ( !isset($_FILES['file']) ) {
1117 //download next batch
1118 $items = $this->store->retrieveUnfetchedEntries($this->user->getId(), IMPORT_LIMIT);
1119
1120 $config = HTMLPurifier_Config::createDefault();
1121 $config->set('Cache.SerializerPath', CACHE);
1122 $purifier = new HTMLPurifier($config);
1123
1124 foreach ($items as $item) {
1125 $url = new Url(base64_encode($item['url']));
1126 $content = Tools::getPageContent($url);
1127
1128 $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'));
1129 $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined'));
1130
1131 //clean content to prevent xss attack
1132 $title = $purifier->purify($title);
1133 $body = $purifier->purify($body);
1134
1135 $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId());
1136 }
1137
1067 } 1138 }
1068 1139 }
1069 $this->$providers[$from]($targetFile); 1140
1141 return array('includeImport'=>true, 'import'=>array('recordsDownloadRequired'=>$recordsDownloadRequired, 'recordsUnderDownload'=> IMPORT_LIMIT, 'delay'=> IMPORT_DELAY * 1000) );
1070 } 1142 }
1071 1143
1072 public function uploadFile() { 1144 public function uploadFile() {
1073 if(isset($_FILES['file'])) 1145 if (isset($_FILES['file']))
1074 { 1146 {
1075 $dir = CACHE . '/'; 1147 $dir = CACHE . '/';
1076 $file = basename($_FILES['file']['name']); 1148 $file = basename($_FILES['file']['name']);
1077 if(move_uploaded_file($_FILES['file']['tmp_name'], $dir . $file)) { 1149 if(move_uploaded_file($_FILES['file']['tmp_name'], $dir . $file)) {
@@ -1081,7 +1153,7 @@ class Poche
1081 $this->messages->add('e', _('Error while importing file. Do you have access to upload it?')); 1153 $this->messages->add('e', _('Error while importing file. Do you have access to upload it?'));
1082 } 1154 }
1083 } 1155 }
1084 1156
1085 Tools::redirect('?view=config'); 1157 Tools::redirect('?view=config');
1086 } 1158 }
1087 1159
@@ -1093,7 +1165,7 @@ class Poche
1093 { 1165 {
1094 $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; 1166 $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json";
1095 header('Content-Disposition: attachment; filename='.$filename); 1167 header('Content-Disposition: attachment; filename='.$filename);
1096 1168
1097 $entries = $this->store->retrieveAll($this->user->getId()); 1169 $entries = $this->store->retrieveAll($this->user->getId());
1098 echo $this->tpl->render('export.twig', array( 1170 echo $this->tpl->render('export.twig', array(
1099 'export' => Tools::renderJson($entries), 1171 'export' => Tools::renderJson($entries),