]>
Commit | Line | Data |
---|---|---|
eb1af592 NL |
1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
eb1af592 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
eb1af592 NL |
7 | * @copyright 2013 |
8 | * @license http://www.wtfpl.net/ see COPYING file | |
9 | */ | |
10 | ||
11 | class Poche | |
12 | { | |
00dbaf90 NL |
13 | public static $canRenderTemplates = true; |
14 | public static $configFileAvailable = true; | |
15 | ||
7ce7ec4c | 16 | public $user; |
eb1af592 NL |
17 | public $store; |
18 | public $tpl; | |
55821e04 | 19 | public $messages; |
6a361945 | 20 | public $pagination; |
00dbaf90 NL |
21 | |
22 | private $currentTheme = ''; | |
5011388f | 23 | private $currentLanguage = ''; |
9d3b88b3 | 24 | private $notInstalledMessage = array(); |
eb1af592 | 25 | |
c9bd17a1 NL |
26 | private $language_names = array( |
27 | 'cs_CZ.utf8' => 'čeština', | |
28 | 'de_DE.utf8' => 'German', | |
29 | 'en_EN.utf8' => 'English', | |
30 | 'es_ES.utf8' => 'Español', | |
31 | 'fa_IR.utf8' => 'فارسی', | |
32 | 'fr_FR.utf8' => 'Français', | |
33 | 'it_IT.utf8' => 'Italiano', | |
34 | 'pl_PL.utf8' => 'Polski', | |
35 | 'ru_RU.utf8' => 'Pусский', | |
36 | 'sl_SI.utf8' => 'Slovenščina', | |
cbcae403 | 37 | 'uk_UA.utf8' => 'Українська', |
c9bd17a1 | 38 | ); |
00dbaf90 | 39 | public function __construct() |
eb1af592 | 40 | { |
9d3b88b3 NL |
41 | if ($this->configFileIsAvailable()) { |
42 | $this->init(); | |
00dbaf90 NL |
43 | } |
44 | ||
9d3b88b3 NL |
45 | if ($this->themeIsInstalled()) { |
46 | $this->initTpl(); | |
00dbaf90 NL |
47 | } |
48 | ||
9d3b88b3 NL |
49 | if ($this->systemIsInstalled()) { |
50 | $this->store = new Database(); | |
51 | $this->messages = new Messages(); | |
52 | # installation | |
53 | if (! $this->store->isInstalled()) { | |
54 | $this->install(); | |
55 | } | |
5cfafc61 | 56 | $this->store->checkTags(); |
eb1af592 | 57 | } |
eb1af592 | 58 | } |
00dbaf90 NL |
59 | |
60 | private function init() | |
61 | { | |
62 | Tools::initPhp(); | |
63 | Session::$sessionName = 'poche'; | |
64 | Session::init(); | |
eb1af592 | 65 | |
00dbaf90 NL |
66 | if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) { |
67 | $this->user = $_SESSION['poche_user']; | |
68 | } else { | |
69 | # fake user, just for install & login screens | |
70 | $this->user = new User(); | |
71 | $this->user->setConfig($this->getDefaultConfig()); | |
72 | } | |
73 | ||
74 | # l10n | |
75 | $language = $this->user->getConfigValue('language'); | |
76 | putenv('LC_ALL=' . $language); | |
77 | setlocale(LC_ALL, $language); | |
78 | bindtextdomain($language, LOCALE); | |
79 | textdomain($language); | |
80 | ||
81 | # Pagination | |
82 | $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); | |
83 | ||
84 | # Set up theme | |
85 | $themeDirectory = $this->user->getConfigValue('theme'); | |
86 | ||
87 | if ($themeDirectory === false) { | |
88 | $themeDirectory = DEFAULT_THEME; | |
89 | } | |
90 | ||
91 | $this->currentTheme = $themeDirectory; | |
5011388f NL |
92 | |
93 | # Set up language | |
94 | $languageDirectory = $this->user->getConfigValue('language'); | |
95 | ||
96 | if ($languageDirectory === false) { | |
97 | $languageDirectory = DEFAULT_THEME; | |
98 | } | |
99 | ||
100 | $this->currentLanguage = $languageDirectory; | |
00dbaf90 NL |
101 | } |
102 | ||
103 | public function configFileIsAvailable() { | |
104 | if (! self::$configFileAvailable) { | |
9d3b88b3 | 105 | $this->notInstalledMessage[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.'; |
00dbaf90 NL |
106 | |
107 | return false; | |
108 | } | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
113 | public function themeIsInstalled() { | |
9d3b88b3 | 114 | $passTheme = TRUE; |
00dbaf90 NL |
115 | # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet |
116 | if (! self::$canRenderTemplates) { | |
41265e07 | 117 | $this->notInstalledMessage[] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. You can also download <a href="http://wllbg.org/vendor">vendor.zip</a> and extract it in your wallabag folder.'; |
9d3b88b3 | 118 | $passTheme = FALSE; |
00dbaf90 | 119 | } |
7f17a38d NL |
120 | |
121 | if (! is_writable(CACHE)) { | |
9d3b88b3 | 122 | $this->notInstalledMessage[] = 'You don\'t have write access on cache directory.'; |
7f17a38d NL |
123 | |
124 | self::$canRenderTemplates = false; | |
125 | ||
9d3b88b3 | 126 | $passTheme = FALSE; |
7f17a38d | 127 | } |
00dbaf90 NL |
128 | |
129 | # Check if the selected theme and its requirements are present | |
f4fbfaa7 NL |
130 | $theme = $this->getTheme(); |
131 | ||
132 | if ($theme != '' && ! is_dir(THEME . '/' . $theme)) { | |
133 | $this->notInstalledMessage[] = 'The currently selected theme (' . $theme . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $theme . ')'; | |
00dbaf90 NL |
134 | |
135 | self::$canRenderTemplates = false; | |
136 | ||
9d3b88b3 | 137 | $passTheme = FALSE; |
00dbaf90 NL |
138 | } |
139 | ||
f4fbfaa7 NL |
140 | $themeInfo = $this->getThemeInfo($theme); |
141 | if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { | |
142 | foreach ($themeInfo['requirements'] as $requiredTheme) { | |
143 | if (! is_dir(THEME . '/' . $requiredTheme)) { | |
144 | $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')'; | |
00dbaf90 | 145 | |
f4fbfaa7 | 146 | self::$canRenderTemplates = false; |
00dbaf90 | 147 | |
f4fbfaa7 NL |
148 | $passTheme = FALSE; |
149 | } | |
00dbaf90 NL |
150 | } |
151 | } | |
9d3b88b3 NL |
152 | |
153 | if (!$passTheme) { | |
154 | return FALSE; | |
155 | } | |
156 | ||
00dbaf90 NL |
157 | |
158 | return true; | |
159 | } | |
160 | ||
4a291288 NL |
161 | /** |
162 | * all checks before installation. | |
00dbaf90 | 163 | * @todo move HTML to template |
4a291288 NL |
164 | * @return boolean |
165 | */ | |
00dbaf90 | 166 | public function systemIsInstalled() |
eb1af592 | 167 | { |
9d3b88b3 | 168 | $msg = TRUE; |
00dbaf90 NL |
169 | |
170 | $configSalt = defined('SALT') ? constant('SALT') : ''; | |
171 | ||
172 | if (empty($configSalt)) { | |
9d3b88b3 NL |
173 | $this->notInstalledMessage[] = 'You have not yet filled in the SALT value in the config.inc.php file.'; |
174 | $msg = FALSE; | |
175 | } | |
176 | if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) { | |
00dbaf90 | 177 | Tools::logm('sqlite file doesn\'t exist'); |
9d3b88b3 NL |
178 | $this->notInstalledMessage[] = 'sqlite file doesn\'t exist, you can find it in install folder. Copy it in /db folder.'; |
179 | $msg = FALSE; | |
180 | } | |
181 | if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) { | |
182 | $this->notInstalledMessage[] = 'you have to delete the /install folder before using poche.'; | |
183 | $msg = FALSE; | |
184 | } | |
185 | if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) { | |
bb5a7d9e | 186 | Tools::logm('you don\'t have write access on sqlite file'); |
9d3b88b3 NL |
187 | $this->notInstalledMessage[] = 'You don\'t have write access on sqlite file.'; |
188 | $msg = FALSE; | |
bb5a7d9e | 189 | } |
00dbaf90 | 190 | |
9d3b88b3 | 191 | if (! $msg) { |
00dbaf90 | 192 | return false; |
8d3275be | 193 | } |
7ce7ec4c | 194 | |
00dbaf90 NL |
195 | return true; |
196 | } | |
197 | ||
198 | public function getNotInstalledMessage() { | |
199 | return $this->notInstalledMessage; | |
4a291288 | 200 | } |
eb1af592 | 201 | |
4a291288 NL |
202 | private function initTpl() |
203 | { | |
00dbaf90 | 204 | $loaderChain = new Twig_Loader_Chain(); |
f4fbfaa7 | 205 | $theme = $this->getTheme(); |
00dbaf90 NL |
206 | |
207 | # add the current theme as first to the loader chain so Twig will look there first for overridden template files | |
208 | try { | |
f4fbfaa7 | 209 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme)); |
00dbaf90 NL |
210 | } catch (Twig_Error_Loader $e) { |
211 | # @todo isInstalled() should catch this, inject Twig later | |
f4fbfaa7 | 212 | die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme .' is missing)'); |
00dbaf90 NL |
213 | } |
214 | ||
215 | # add all required themes to the loader chain | |
f4fbfaa7 NL |
216 | $themeInfo = $this->getThemeInfo($theme); |
217 | if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) { | |
218 | foreach ($themeInfo['requirements'] as $requiredTheme) { | |
219 | try { | |
220 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $requiredTheme)); | |
221 | } catch (Twig_Error_Loader $e) { | |
222 | # @todo isInstalled() should catch this, inject Twig later | |
223 | die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')'); | |
224 | } | |
00dbaf90 NL |
225 | } |
226 | } | |
227 | ||
bc1ee852 | 228 | if (DEBUG_POCHE) { |
f4fbfaa7 | 229 | $twigParams = array(); |
00dbaf90 | 230 | } else { |
f4fbfaa7 | 231 | $twigParams = array('cache' => CACHE); |
bc1ee852 | 232 | } |
00dbaf90 | 233 | |
f4fbfaa7 | 234 | $this->tpl = new Twig_Environment($loaderChain, $twigParams); |
eb1af592 | 235 | $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); |
00dbaf90 | 236 | |
55821e04 NL |
237 | # filter to display domain name of an url |
238 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | |
239 | $this->tpl->addFilter($filter); | |
eb1af592 | 240 | |
d9178758 NL |
241 | # filter for reading time |
242 | $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime'); | |
243 | $this->tpl->addFilter($filter); | |
eb1af592 NL |
244 | } |
245 | ||
f4fbfaa7 | 246 | private function install() |
eb1af592 NL |
247 | { |
248 | Tools::logm('poche still not installed'); | |
249 | echo $this->tpl->render('install.twig', array( | |
00dbaf90 NL |
250 | 'token' => Session::getToken(), |
251 | 'theme' => $this->getTheme(), | |
252 | 'poche_url' => Tools::getPocheUrl() | |
eb1af592 NL |
253 | )); |
254 | if (isset($_GET['install'])) { | |
255 | if (($_POST['password'] == $_POST['password_repeat']) | |
256 | && $_POST['password'] != "" && $_POST['login'] != "") { | |
257 | # let's rock, install poche baby ! | |
bb5a7d9e NL |
258 | if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) |
259 | { | |
260 | Session::logout(); | |
261 | Tools::logm('poche is now installed'); | |
262 | Tools::redirect(); | |
263 | } | |
6a361945 NL |
264 | } |
265 | else { | |
266 | Tools::logm('error during installation'); | |
eb1af592 NL |
267 | Tools::redirect(); |
268 | } | |
269 | } | |
270 | exit(); | |
271 | } | |
00dbaf90 NL |
272 | |
273 | public function getTheme() { | |
274 | return $this->currentTheme; | |
275 | } | |
5011388f | 276 | |
f4fbfaa7 NL |
277 | /** |
278 | * Provides theme information by parsing theme.ini file if present in the theme's root directory. | |
279 | * In all cases, the following data will be returned: | |
280 | * - name: theme's name, or key if the theme is unnamed, | |
281 | * - current: boolean informing if the theme is the current user theme. | |
282 | * | |
283 | * @param string $theme Theme key (directory name) | |
284 | * @return array|boolean Theme information, or false if the theme doesn't exist. | |
285 | */ | |
286 | public function getThemeInfo($theme) { | |
287 | if (!is_dir(THEME . '/' . $theme)) { | |
288 | return false; | |
289 | } | |
290 | ||
291 | $themeIniFile = THEME . '/' . $theme . '/theme.ini'; | |
292 | $themeInfo = array(); | |
293 | ||
294 | if (is_file($themeIniFile) && is_readable($themeIniFile)) { | |
295 | $themeInfo = parse_ini_file($themeIniFile); | |
296 | } | |
297 | ||
298 | if ($themeInfo === false) { | |
299 | $themeInfo = array(); | |
300 | } | |
301 | if (!isset($themeInfo['name'])) { | |
302 | $themeInfo['name'] = $theme; | |
303 | } | |
304 | $themeInfo['current'] = ($theme === $this->getTheme()); | |
305 | ||
306 | return $themeInfo; | |
5011388f | 307 | } |
00dbaf90 NL |
308 | |
309 | public function getInstalledThemes() { | |
310 | $handle = opendir(THEME); | |
311 | $themes = array(); | |
f4fbfaa7 | 312 | |
00dbaf90 NL |
313 | while (($theme = readdir($handle)) !== false) { |
314 | # Themes are stored in a directory, so all directory names are themes | |
315 | # @todo move theme installation data to database | |
f4fbfaa7 | 316 | if (!is_dir(THEME . '/' . $theme) || in_array($theme, array('.', '..'))) { |
00dbaf90 NL |
317 | continue; |
318 | } | |
f4fbfaa7 NL |
319 | |
320 | $themes[$theme] = $this->getThemeInfo($theme); | |
00dbaf90 | 321 | } |
f4fbfaa7 | 322 | |
3ade95a3 NL |
323 | ksort($themes); |
324 | ||
00dbaf90 NL |
325 | return $themes; |
326 | } | |
eb1af592 | 327 | |
f4fbfaa7 NL |
328 | public function getLanguage() { |
329 | return $this->currentLanguage; | |
330 | } | |
331 | ||
5011388f NL |
332 | public function getInstalledLanguages() { |
333 | $handle = opendir(LOCALE); | |
334 | $languages = array(); | |
335 | ||
336 | while (($language = readdir($handle)) !== false) { | |
337 | # Languages are stored in a directory, so all directory names are languages | |
338 | # @todo move language installation data to database | |
cbcae403 | 339 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) { |
5011388f NL |
340 | continue; |
341 | } | |
342 | ||
343 | $current = false; | |
344 | ||
345 | if ($language === $this->getLanguage()) { | |
346 | $current = true; | |
347 | } | |
348 | ||
cbcae403 | 349 | $languages[] = array('name' => (isset($this->language_names[$language]) ? $this->language_names[$language] : $language), 'value' => $language, 'current' => $current); |
5011388f NL |
350 | } |
351 | ||
352 | return $languages; | |
353 | } | |
354 | ||
8d3275be | 355 | public function getDefaultConfig() |
00dbaf90 | 356 | { |
8d3275be NL |
357 | return array( |
358 | 'pager' => PAGINATION, | |
359 | 'language' => LANG, | |
00dbaf90 NL |
360 | 'theme' => DEFAULT_THEME |
361 | ); | |
8d3275be NL |
362 | } |
363 | ||
f878daeb | 364 | protected function getPageContent(Url $url) |
365 | { | |
b4fd2154 MR |
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" | |
f878daeb | 391 | ); |
b4fd2154 MR |
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; | |
f878daeb | 414 | } |
f878daeb | 415 | return json_decode($json, true); |
416 | } | |
417 | ||
eb1af592 NL |
418 | /** |
419 | * Call action (mark as fav, archive, delete, etc.) | |
420 | */ | |
926acd7b | 421 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null) |
eb1af592 NL |
422 | { |
423 | switch ($action) | |
424 | { | |
425 | case 'add': | |
f878daeb | 426 | $content = $this->getPageContent($url); |
fa0bfb77 | 427 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); |
42c80841 | 428 | $body = $content['rss']['channel']['item']['description']; |
ec397236 | 429 | |
1570a653 NL |
430 | // clean content from prevent xss attack |
431 | $config = HTMLPurifier_Config::createDefault(); | |
432 | $purifier = new HTMLPurifier($config); | |
433 | $title = $purifier->purify($title); | |
434 | $body = $purifier->purify($body); | |
435 | ||
488fc63b MR |
436 | //search for possible duplicate if not in import mode |
437 | if (!$import) { | |
438 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); | |
439 | } | |
440 | ||
42c80841 | 441 | if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { |
ec397236 NL |
442 | Tools::logm('add link ' . $url->getUrl()); |
443 | $sequence = ''; | |
444 | if (STORAGE == 'postgres') { | |
445 | $sequence = 'entries_id_seq'; | |
eb1af592 | 446 | } |
ec397236 NL |
447 | $last_id = $this->store->getLastId($sequence); |
448 | if (DOWNLOAD_PICTURES) { | |
42c80841 | 449 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
ec397236 NL |
450 | Tools::logm('updating content article'); |
451 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
452 | } | |
488fc63b MR |
453 | |
454 | if ($duplicate != NULL) { | |
455 | // 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 | |
456 | Tools::logm('link ' . $url->getUrl() . ' is a duplicate'); | |
457 | // 1) - preserve tags and favorite, then drop old entry | |
458 | $this->store->reassignTags($duplicate['id'], $last_id); | |
459 | if ($duplicate['is_fav']) { | |
460 | $this->store->favoriteById($last_id, $this->user->getId()); | |
461 | } | |
462 | if ($this->store->deleteById($duplicate['id'], $this->user->getId())) { | |
463 | Tools::logm('previous link ' . $url->getUrl() .' entry deleted'); | |
464 | } | |
465 | } | |
466 | ||
ec397236 NL |
467 | if (!$import) { |
468 | $this->messages->add('s', _('the link has been added successfully')); | |
eb1af592 NL |
469 | } |
470 | } | |
471 | else { | |
b916bcfc | 472 | if (!$import) { |
ec397236 NL |
473 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
474 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc NL |
475 | } |
476 | } | |
ec397236 | 477 | |
b916bcfc | 478 | if (!$import) { |
363bc4eb | 479 | if ($autoclose == TRUE) { |
480 | Tools::redirect('?view=home'); | |
481 | } else { | |
f616ab60 | 482 | Tools::redirect('?view=home&closewin=true'); |
363bc4eb | 483 | } |
eb1af592 NL |
484 | } |
485 | break; | |
486 | case 'delete': | |
bc1ee852 | 487 | $msg = 'delete link #' . $id; |
8d3275be | 488 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
489 | if (DOWNLOAD_PICTURES) { |
490 | remove_directory(ABS_PATH . $id); | |
491 | } | |
6a361945 | 492 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
493 | } |
494 | else { | |
6a361945 | 495 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 496 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 497 | } |
bc1ee852 | 498 | Tools::logm($msg); |
985ce3ec | 499 | Tools::redirect('?'); |
eb1af592 NL |
500 | break; |
501 | case 'toggle_fav' : | |
8d3275be | 502 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 503 | Tools::logm('mark as favorite link #' . $id); |
b916bcfc NL |
504 | if (!$import) { |
505 | Tools::redirect(); | |
506 | } | |
eb1af592 NL |
507 | break; |
508 | case 'toggle_archive' : | |
8d3275be | 509 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 510 | Tools::logm('archive link #' . $id); |
b916bcfc NL |
511 | if (!$import) { |
512 | Tools::redirect(); | |
513 | } | |
eb1af592 | 514 | break; |
f14807de NL |
515 | case 'archive_all' : |
516 | $this->store->archiveAll($this->user->getId()); | |
517 | Tools::logm('archive all links'); | |
518 | if (!$import) { | |
519 | Tools::redirect(); | |
520 | } | |
521 | break; | |
c432fa16 | 522 | case 'add_tag' : |
926acd7b | 523 | if($import){ |
524 | $entry_id = $id; | |
525 | $tags = explode(',', $tags); | |
526 | } | |
527 | else{ | |
528 | $tags = explode(',', $_POST['value']); | |
529 | $entry_id = $_POST['entry_id']; | |
530 | } | |
b89d5a2b NL |
531 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); |
532 | if (!$entry) { | |
533 | $this->messages->add('e', _('Article not found!')); | |
534 | Tools::logm('error : article not found'); | |
535 | Tools::redirect(); | |
536 | } | |
c432fa16 NL |
537 | foreach($tags as $key => $tag_value) { |
538 | $value = trim($tag_value); | |
539 | $tag = $this->store->retrieveTagByValue($value); | |
540 | ||
541 | if (is_null($tag)) { | |
542 | # we create the tag | |
543 | $tag = $this->store->createTag($value); | |
544 | $sequence = ''; | |
545 | if (STORAGE == 'postgres') { | |
546 | $sequence = 'tags_id_seq'; | |
547 | } | |
548 | $tag_id = $this->store->getLastId($sequence); | |
549 | } | |
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 | } | |
926acd7b | 557 | if(!$import) { |
558 | Tools::redirect(); | |
559 | } | |
c432fa16 NL |
560 | break; |
561 | case 'remove_tag' : | |
562 | $tag_id = $_GET['tag_id']; | |
b89d5a2b NL |
563 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
564 | if (!$entry) { | |
565 | $this->messages->add('e', _('Article not found!')); | |
566 | Tools::logm('error : article not found'); | |
567 | Tools::redirect(); | |
568 | } | |
c432fa16 NL |
569 | $this->store->removeTagForEntry($id, $tag_id); |
570 | Tools::redirect(); | |
571 | break; | |
eb1af592 NL |
572 | default: |
573 | break; | |
574 | } | |
575 | } | |
576 | ||
577 | function displayView($view, $id = 0) | |
578 | { | |
579 | $tpl_vars = array(); | |
580 | ||
581 | switch ($view) | |
582 | { | |
eb1af592 | 583 | case 'config': |
044bf638 NL |
584 | $dev = trim($this->getPocheVersion('dev')); |
585 | $prod = trim($this->getPocheVersion('prod')); | |
031df528 NL |
586 | $compare_dev = version_compare(POCHE, $dev); |
587 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 588 | $themes = $this->getInstalledThemes(); |
5011388f | 589 | $languages = $this->getInstalledLanguages(); |
72c20a52 | 590 | $token = $this->user->getConfigValue('token'); |
1810c13b | 591 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; |
32520785 | 592 | $tpl_vars = array( |
00dbaf90 | 593 | 'themes' => $themes, |
5011388f | 594 | 'languages' => $languages, |
32520785 NL |
595 | 'dev' => $dev, |
596 | 'prod' => $prod, | |
597 | 'compare_dev' => $compare_dev, | |
598 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
599 | 'token' => $token, |
600 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 601 | 'http_auth' => $http_auth, |
32520785 | 602 | ); |
eb1af592 NL |
603 | Tools::logm('config view'); |
604 | break; | |
6cab59c3 NL |
605 | case 'edit-tags': |
606 | # tags | |
b89d5a2b NL |
607 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
608 | if (!$entry) { | |
609 | $this->messages->add('e', _('Article not found!')); | |
610 | Tools::logm('error : article not found'); | |
611 | Tools::redirect(); | |
612 | } | |
6cab59c3 NL |
613 | $tags = $this->store->retrieveTagsByEntry($id); |
614 | $tpl_vars = array( | |
c432fa16 | 615 | 'entry_id' => $id, |
6cab59c3 | 616 | 'tags' => $tags, |
032e0ca1 | 617 | 'entry' => $entry, |
4886ed6d NL |
618 | ); |
619 | break; | |
2e2ebe5e | 620 | case 'tags': |
f778e472 | 621 | $token = $this->user->getConfigValue('token'); |
b89d5a2b | 622 | $tags = $this->store->retrieveAllTags($this->user->getId()); |
2e2ebe5e | 623 | $tpl_vars = array( |
f778e472 NL |
624 | 'token' => $token, |
625 | 'user_id' => $this->user->getId(), | |
2e2ebe5e NL |
626 | 'tags' => $tags, |
627 | ); | |
628 | break; | |
eb1af592 | 629 | case 'view': |
8d3275be | 630 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
631 | if ($entry != NULL) { |
632 | Tools::logm('view link #' . $id); | |
633 | $content = $entry['content']; | |
634 | if (function_exists('tidy_parse_string')) { | |
635 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
636 | $tidy->cleanRepair(); | |
637 | $content = $tidy->value; | |
3408ed48 | 638 | } |
a3223127 | 639 | |
3408ed48 NL |
640 | # flattr checking |
641 | $flattr = new FlattrItem(); | |
7b171c73 NL |
642 | $flattr->checkItem($entry['url'], $entry['id']); |
643 | ||
644 | # tags | |
645 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 646 | |
3408ed48 | 647 | $tpl_vars = array( |
7b171c73 NL |
648 | 'entry' => $entry, |
649 | 'content' => $content, | |
650 | 'flattr' => $flattr, | |
651 | 'tags' => $tags | |
3408ed48 | 652 | ); |
eb1af592 NL |
653 | } |
654 | else { | |
d8d1542e | 655 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
656 | } |
657 | break; | |
032e0ca1 | 658 | default: # home, favorites, archive and tag views |
eb1af592 | 659 | $tpl_vars = array( |
3eb04903 N |
660 | 'entries' => '', |
661 | 'page_links' => '', | |
7f9f5281 | 662 | 'nb_results' => '', |
eb1af592 | 663 | ); |
34d67c83 | 664 | |
032e0ca1 MR |
665 | //if id is given - we retrive entries by tag: id is tag id |
666 | if ($id) { | |
667 | $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId()); | |
668 | $tpl_vars['id'] = intval($id); | |
669 | } | |
670 | ||
671 | $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id); | |
672 | ||
673 | if ($count > 0) { | |
674 | $this->pagination->set_total($count); | |
c515ffec | 675 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), |
032e0ca1 MR |
676 | $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' )); |
677 | $tpl_vars['entries'] = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit(), $id); | |
3eb04903 | 678 | $tpl_vars['page_links'] = $page_links; |
032e0ca1 | 679 | $tpl_vars['nb_results'] = $count; |
3eb04903 | 680 | } |
6a361945 | 681 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
682 | break; |
683 | } | |
684 | ||
685 | return $tpl_vars; | |
686 | } | |
c765c367 | 687 | |
07ee09f4 NL |
688 | /** |
689 | * update the password of the current user. | |
690 | * if MODE_DEMO is TRUE, the password can't be updated. | |
691 | * @todo add the return value | |
692 | * @todo set the new password in function header like this updatePassword($newPassword) | |
693 | * @return boolean | |
694 | */ | |
c765c367 NL |
695 | public function updatePassword() |
696 | { | |
55821e04 | 697 | if (MODE_DEMO) { |
8d3275be | 698 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 699 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 700 | Tools::redirect('?view=config'); |
55821e04 NL |
701 | } |
702 | else { | |
703 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
704 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
705 | $this->messages->add('s', _('your password has been updated')); |
706 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 707 | Session::logout(); |
8d3275be | 708 | Tools::logm('password updated'); |
c765c367 NL |
709 | Tools::redirect(); |
710 | } | |
711 | else { | |
8d3275be | 712 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 713 | Tools::redirect('?view=config'); |
c765c367 NL |
714 | } |
715 | } | |
716 | } | |
717 | } | |
00dbaf90 NL |
718 | |
719 | public function updateTheme() | |
720 | { | |
721 | # no data | |
722 | if (empty($_POST['theme'])) { | |
723 | } | |
724 | ||
725 | # we are not going to change it to the current theme... | |
726 | if ($_POST['theme'] == $this->getTheme()) { | |
727 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
728 | Tools::redirect('?view=config'); | |
729 | } | |
730 | ||
731 | $themes = $this->getInstalledThemes(); | |
732 | $actualTheme = false; | |
733 | ||
f4fbfaa7 NL |
734 | foreach (array_keys($themes) as $theme) { |
735 | if ($theme == $_POST['theme']) { | |
00dbaf90 NL |
736 | $actualTheme = true; |
737 | break; | |
738 | } | |
739 | } | |
740 | ||
741 | if (! $actualTheme) { | |
742 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
743 | Tools::redirect('?view=config'); | |
744 | } | |
745 | ||
746 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); | |
747 | $this->messages->add('s', _('you have changed your theme preferences')); | |
748 | ||
749 | $currentConfig = $_SESSION['poche_user']->config; | |
750 | $currentConfig['theme'] = $_POST['theme']; | |
751 | ||
752 | $_SESSION['poche_user']->setConfig($currentConfig); | |
56532c4e NL |
753 | |
754 | $this->emptyCache(); | |
00dbaf90 NL |
755 | |
756 | Tools::redirect('?view=config'); | |
757 | } | |
c765c367 | 758 | |
5011388f NL |
759 | public function updateLanguage() |
760 | { | |
761 | # no data | |
762 | if (empty($_POST['language'])) { | |
763 | } | |
764 | ||
765 | # we are not going to change it to the current language... | |
766 | if ($_POST['language'] == $this->getLanguage()) { | |
767 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
768 | Tools::redirect('?view=config'); | |
769 | } | |
770 | ||
771 | $languages = $this->getInstalledLanguages(); | |
772 | $actualLanguage = false; | |
773 | ||
774 | foreach ($languages as $language) { | |
c9bd17a1 | 775 | if ($language['value'] == $_POST['language']) { |
5011388f NL |
776 | $actualLanguage = true; |
777 | break; | |
778 | } | |
779 | } | |
780 | ||
781 | if (! $actualLanguage) { | |
782 | $this->messages->add('e', _('that language does not seem to be installed')); | |
783 | Tools::redirect('?view=config'); | |
784 | } | |
785 | ||
786 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); | |
787 | $this->messages->add('s', _('you have changed your language preferences')); | |
788 | ||
789 | $currentConfig = $_SESSION['poche_user']->config; | |
790 | $currentConfig['language'] = $_POST['language']; | |
791 | ||
792 | $_SESSION['poche_user']->setConfig($currentConfig); | |
e145f767 NL |
793 | |
794 | $this->emptyCache(); | |
5011388f NL |
795 | |
796 | Tools::redirect('?view=config'); | |
797 | } | |
798 | ||
df6afaf0 DS |
799 | /** |
800 | * get credentials from differents sources | |
801 | * it redirects the user to the $referer link | |
802 | * @return array | |
803 | */ | |
1810c13b NL |
804 | private function credentials() { |
805 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
6af66b11 | 806 | return array($_SERVER['PHP_AUTH_USER'],'php_auth',true); |
1810c13b NL |
807 | } |
808 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
6af66b11 | 809 | return array($_POST['login'],$_POST['password'],false); |
1810c13b NL |
810 | } |
811 | if(isset($_SERVER['REMOTE_USER'])) { | |
6af66b11 | 812 | return array($_SERVER['REMOTE_USER'],'http_auth',true); |
1810c13b | 813 | } |
5cfafc61 | 814 | |
6af66b11 MR |
815 | return array(false,false,false); |
816 | } | |
df6afaf0 | 817 | |
07ee09f4 NL |
818 | /** |
819 | * checks if login & password are correct and save the user in session. | |
820 | * it redirects the user to the $referer link | |
821 | * @param string $referer the url to redirect after login | |
822 | * @todo add the return value | |
823 | * @return boolean | |
824 | */ | |
c765c367 NL |
825 | public function login($referer) |
826 | { | |
6af66b11 | 827 | list($login,$password,$isauthenticated)=$this->credentials(); |
df6afaf0 DS |
828 | if($login === false || $password === false) { |
829 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
830 | Tools::logm('login failed'); | |
831 | Tools::redirect(); | |
832 | } | |
833 | if (!empty($login) && !empty($password)) { | |
6af66b11 | 834 | $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated); |
7ce7ec4c NL |
835 | if ($user != array()) { |
836 | # Save login into Session | |
6af66b11 MR |
837 | $longlastingsession = isset($_POST['longlastingsession']); |
838 | $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login); | |
839 | Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user))); | |
26929c08 | 840 | $this->messages->add('s', _('welcome to your wallabag')); |
8d3275be | 841 | Tools::logm('login successful'); |
c765c367 NL |
842 | Tools::redirect($referer); |
843 | } | |
8d3275be | 844 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
845 | Tools::logm('login failed'); |
846 | Tools::redirect(); | |
c765c367 NL |
847 | } |
848 | } | |
849 | ||
07ee09f4 NL |
850 | /** |
851 | * log out the poche user. It cleans the session. | |
852 | * @todo add the return value | |
853 | * @return boolean | |
854 | */ | |
c765c367 NL |
855 | public function logout() |
856 | { | |
7ce7ec4c | 857 | $this->user = array(); |
c765c367 | 858 | Session::logout(); |
b916bcfc | 859 | Tools::logm('logout'); |
c765c367 NL |
860 | Tools::redirect(); |
861 | } | |
862 | ||
07ee09f4 NL |
863 | /** |
864 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
865 | * @todo add the return value | |
66b6a3b5 | 866 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
867 | * @return boolean |
868 | */ | |
66b6a3b5 | 869 | private function importFromInstapaper($targetFile) |
c765c367 | 870 | { |
7f959169 | 871 | # TODO gestion des articles favs |
a62788c6 | 872 | $html = new simple_html_dom(); |
66b6a3b5 | 873 | $html->load_file($targetFile); |
b916bcfc | 874 | Tools::logm('starting import from instapaper'); |
a62788c6 NL |
875 | |
876 | $read = 0; | |
877 | $errors = array(); | |
878 | foreach($html->find('ol') as $ul) | |
879 | { | |
880 | foreach($ul->find('li') as $li) | |
881 | { | |
882 | $a = $li->find('a'); | |
883 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 884 | $this->action('add', $url, 0, TRUE); |
a62788c6 | 885 | if ($read == '1') { |
b916bcfc NL |
886 | $sequence = ''; |
887 | if (STORAGE == 'postgres') { | |
888 | $sequence = 'entries_id_seq'; | |
889 | } | |
890 | $last_id = $this->store->getLastId($sequence); | |
891 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
a62788c6 NL |
892 | } |
893 | } | |
7f959169 NL |
894 | |
895 | # the second <ol> is for read links | |
a62788c6 NL |
896 | $read = 1; |
897 | } | |
8d3275be | 898 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
899 | Tools::logm('import from instapaper completed'); |
900 | Tools::redirect(); | |
901 | } | |
c765c367 | 902 | |
07ee09f4 NL |
903 | /** |
904 | * import from Pocket. poche needs a ./ril_export.html file | |
905 | * @todo add the return value | |
66b6a3b5 | 906 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
907 | * @return boolean |
908 | */ | |
66b6a3b5 | 909 | private function importFromPocket($targetFile) |
63c35580 | 910 | { |
7f959169 | 911 | # TODO gestion des articles favs |
63c35580 | 912 | $html = new simple_html_dom(); |
66b6a3b5 | 913 | $html->load_file($targetFile); |
b916bcfc | 914 | Tools::logm('starting import from pocket'); |
63c35580 NL |
915 | |
916 | $read = 0; | |
917 | $errors = array(); | |
918 | foreach($html->find('ul') as $ul) | |
919 | { | |
920 | foreach($ul->find('li') as $li) | |
c765c367 | 921 | { |
63c35580 NL |
922 | $a = $li->find('a'); |
923 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 924 | $this->action('add', $url, 0, TRUE); |
926acd7b | 925 | $sequence = ''; |
926 | if (STORAGE == 'postgres') { | |
927 | $sequence = 'entries_id_seq'; | |
928 | } | |
929 | $last_id = $this->store->getLastId($sequence); | |
63c35580 | 930 | if ($read == '1') { |
b916bcfc | 931 | $this->action('toggle_archive', $url, $last_id, TRUE); |
c765c367 | 932 | } |
926acd7b | 933 | $tags = $a[0]->tags; |
934 | if(!empty($tags)) { | |
935 | $this->action('add_tag',$url,$last_id,true,false,$tags); | |
936 | } | |
c765c367 | 937 | } |
7f959169 NL |
938 | |
939 | # the second <ul> is for read links | |
63c35580 | 940 | $read = 1; |
c765c367 | 941 | } |
8d3275be | 942 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
943 | Tools::logm('import from pocket completed'); |
944 | Tools::redirect(); | |
945 | } | |
c765c367 | 946 | |
07ee09f4 NL |
947 | /** |
948 | * import from Readability. poche needs a ./readability file | |
949 | * @todo add the return value | |
66b6a3b5 | 950 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
951 | * @return boolean |
952 | */ | |
66b6a3b5 | 953 | private function importFromReadability($targetFile) |
63c35580 | 954 | { |
7f959169 | 955 | # TODO gestion des articles lus / favs |
66b6a3b5 | 956 | $str_data = file_get_contents($targetFile); |
63c35580 | 957 | $data = json_decode($str_data,true); |
b916bcfc | 958 | Tools::logm('starting import from Readability'); |
c0d321c1 | 959 | $count = 0; |
63c35580 | 960 | foreach ($data as $key => $value) { |
c0d321c1 NL |
961 | $url = NULL; |
962 | $favorite = FALSE; | |
963 | $archive = FALSE; | |
9bc32632 NL |
964 | foreach ($value as $item) { |
965 | foreach ($item as $attr => $value) { | |
966 | if ($attr == 'article__url') { | |
967 | $url = new Url(base64_encode($value)); | |
c0d321c1 | 968 | } |
9bc32632 NL |
969 | $sequence = ''; |
970 | if (STORAGE == 'postgres') { | |
971 | $sequence = 'entries_id_seq'; | |
972 | } | |
973 | if ($value == 'true') { | |
974 | if ($attr == 'favorite') { | |
975 | $favorite = TRUE; | |
976 | } | |
977 | if ($attr == 'archive') { | |
978 | $archive = TRUE; | |
979 | } | |
c0d321c1 NL |
980 | } |
981 | } | |
9bc32632 NL |
982 | |
983 | # we can add the url | |
984 | if (!is_null($url) && $url->isCorrect()) { | |
985 | $this->action('add', $url, 0, TRUE); | |
986 | $count++; | |
987 | if ($favorite) { | |
988 | $last_id = $this->store->getLastId($sequence); | |
989 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
990 | } | |
991 | if ($archive) { | |
992 | $last_id = $this->store->getLastId($sequence); | |
993 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
994 | } | |
b916bcfc | 995 | } |
c765c367 | 996 | } |
c765c367 | 997 | } |
c0d321c1 | 998 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); |
63c35580 NL |
999 | Tools::logm('import from Readability completed'); |
1000 | Tools::redirect(); | |
c765c367 NL |
1001 | } |
1002 | ||
89051914 NL |
1003 | /** |
1004 | * import from Poche exported file | |
1005 | * @param string $targetFile the file used for importing | |
1006 | * @return boolean | |
1007 | */ | |
1008 | private function importFromPoche($targetFile) | |
1009 | { | |
1010 | $str_data = file_get_contents($targetFile); | |
1011 | $data = json_decode($str_data,true); | |
1012 | Tools::logm('starting import from Poche'); | |
1013 | ||
1014 | ||
1015 | $sequence = ''; | |
1016 | if (STORAGE == 'postgres') { | |
1017 | $sequence = 'entries_id_seq'; | |
1018 | } | |
1019 | ||
1020 | $count = 0; | |
1021 | foreach ($data as $value) { | |
1022 | ||
1023 | $url = new Url(base64_encode($value['url'])); | |
1024 | $favorite = ($value['is_fav'] == -1); | |
1025 | $archive = ($value['is_read'] == -1); | |
1026 | ||
1027 | # we can add the url | |
1028 | if (!is_null($url) && $url->isCorrect()) { | |
1029 | ||
1030 | $this->action('add', $url, 0, TRUE); | |
1031 | ||
1032 | $count++; | |
1033 | if ($favorite) { | |
1034 | $last_id = $this->store->getLastId($sequence); | |
1035 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
1036 | } | |
1037 | if ($archive) { | |
1038 | $last_id = $this->store->getLastId($sequence); | |
1039 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | } | |
1044 | $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.')); | |
1045 | Tools::logm('import from Poche completed'); | |
1046 | Tools::redirect(); | |
1047 | } | |
1048 | ||
07ee09f4 NL |
1049 | /** |
1050 | * import datas into your poche | |
1051 | * @param string $from name of the service to import : pocket, instapaper or readability | |
1052 | * @todo add the return value | |
1053 | * @return boolean | |
1054 | */ | |
63c35580 | 1055 | public function import($from) |
c765c367 | 1056 | { |
66b6a3b5 E |
1057 | $providers = array( |
1058 | 'pocket' => 'importFromPocket', | |
1059 | 'readability' => 'importFromReadability', | |
89051914 NL |
1060 | 'instapaper' => 'importFromInstapaper', |
1061 | 'poche' => 'importFromPoche', | |
66b6a3b5 E |
1062 | ); |
1063 | ||
1064 | if (! isset($providers[$from])) { | |
1065 | $this->messages->add('e', _('Unknown import provider.')); | |
1066 | Tools::redirect(); | |
63c35580 | 1067 | } |
66b6a3b5 E |
1068 | |
1069 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
1070 | $targetFile = constant($targetDefinition); | |
1071 | ||
1072 | if (! defined($targetDefinition)) { | |
1073 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
1074 | Tools::redirect(); | |
63c35580 | 1075 | } |
66b6a3b5 E |
1076 | |
1077 | if (! file_exists($targetFile)) { | |
1078 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
1079 | Tools::redirect(); | |
63c35580 | 1080 | } |
66b6a3b5 E |
1081 | |
1082 | $this->$providers[$from]($targetFile); | |
63c35580 | 1083 | } |
c765c367 | 1084 | |
07ee09f4 NL |
1085 | /** |
1086 | * export poche entries in json | |
1087 | * @return json all poche entries | |
1088 | */ | |
63c35580 NL |
1089 | public function export() |
1090 | { | |
8d3275be | 1091 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
1092 | echo $this->tpl->render('export.twig', array( |
1093 | 'export' => Tools::renderJson($entries), | |
1094 | )); | |
1095 | Tools::logm('export view'); | |
c765c367 | 1096 | } |
32520785 | 1097 | |
07ee09f4 | 1098 | /** |
a3436d4c | 1099 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
1100 | * @param string $which 'prod' or 'dev' |
1101 | * @return string latest $which version | |
1102 | */ | |
32520785 NL |
1103 | private function getPocheVersion($which = 'prod') |
1104 | { | |
1105 | $cache_file = CACHE . '/' . $which; | |
a3436d4c NL |
1106 | |
1107 | # checks if the cached version file exists | |
32520785 NL |
1108 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { |
1109 | $version = file_get_contents($cache_file); | |
1110 | } else { | |
3e0e7e12 | 1111 | $version = file_get_contents('http://static.wallabag.org/versions/' . $which); |
32520785 NL |
1112 | file_put_contents($cache_file, $version, LOCK_EX); |
1113 | } | |
1114 | return $version; | |
1115 | } | |
72c20a52 NL |
1116 | |
1117 | public function generateToken() | |
1118 | { | |
1119 | if (ini_get('open_basedir') === '') { | |
1120 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
1121 | } | |
1122 | else { | |
1123 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
1124 | } | |
1125 | ||
044bf638 | 1126 | $token = str_replace('+', '', $token); |
72c20a52 NL |
1127 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); |
1128 | $currentConfig = $_SESSION['poche_user']->config; | |
1129 | $currentConfig['token'] = $token; | |
1130 | $_SESSION['poche_user']->setConfig($currentConfig); | |
92fc97ee | 1131 | Tools::redirect(); |
72c20a52 NL |
1132 | } |
1133 | ||
f778e472 | 1134 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') |
72c20a52 | 1135 | { |
f778e472 | 1136 | $allowed_types = array('home', 'fav', 'archive', 'tag'); |
72c20a52 NL |
1137 | $config = $this->store->getConfigUser($user_id); |
1138 | ||
1139 | if (!in_array($type, $allowed_types) || | |
1140 | $token != $config['token']) { | |
1141 | die(_('Uh, there is a problem while generating feeds.')); | |
1142 | } | |
1143 | // Check the token | |
1144 | ||
9e7c840b | 1145 | $feed = new FeedWriter(RSS2); |
2e4440c3 | 1146 | $feed->setTitle('wallabag — ' . $type . ' feed'); |
72c20a52 | 1147 | $feed->setLink(Tools::getPocheUrl()); |
9e7c840b | 1148 | $feed->setChannelElement('updated', date(DATE_RSS , time())); |
2e4440c3 | 1149 | $feed->setChannelElement('author', 'wallabag'); |
72c20a52 | 1150 | |
f778e472 | 1151 | if ($type == 'tag') { |
b89d5a2b | 1152 | $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); |
f778e472 NL |
1153 | } |
1154 | else { | |
1155 | $entries = $this->store->getEntriesByView($type, $user_id); | |
1156 | } | |
1157 | ||
72c20a52 NL |
1158 | if (count($entries) > 0) { |
1159 | foreach ($entries as $entry) { | |
1160 | $newItem = $feed->createNewItem(); | |
0b57c682 | 1161 | $newItem->setTitle($entry['title']); |
72c20a52 NL |
1162 | $newItem->setLink(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); |
1163 | $newItem->setDate(time()); | |
1164 | $newItem->setDescription($entry['content']); | |
1165 | $feed->addItem($newItem); | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | $feed->genarateFeed(); | |
1170 | exit; | |
1171 | } | |
6285e57c NL |
1172 | |
1173 | public function emptyCache() { | |
1174 | $files = new RecursiveIteratorIterator( | |
1175 | new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS), | |
1176 | RecursiveIteratorIterator::CHILD_FIRST | |
1177 | ); | |
1178 | ||
1179 | foreach ($files as $fileinfo) { | |
1180 | $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); | |
1181 | $todo($fileinfo->getRealPath()); | |
1182 | } | |
1183 | ||
1184 | Tools::logm('empty cache'); | |
1185 | $this->messages->add('s', _('Cache deleted.')); | |
1186 | Tools::redirect(); | |
1187 | } | |
df6afaf0 | 1188 | } |