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