]>
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 | |
00dbaf90 NL |
310 | return $themes; |
311 | } | |
eb1af592 | 312 | |
f4fbfaa7 NL |
313 | public function getLanguage() { |
314 | return $this->currentLanguage; | |
315 | } | |
316 | ||
5011388f NL |
317 | public function getInstalledLanguages() { |
318 | $handle = opendir(LOCALE); | |
319 | $languages = array(); | |
320 | ||
321 | while (($language = readdir($handle)) !== false) { | |
322 | # Languages are stored in a directory, so all directory names are languages | |
323 | # @todo move language installation data to database | |
324 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.'))) { | |
325 | continue; | |
326 | } | |
327 | ||
328 | $current = false; | |
329 | ||
330 | if ($language === $this->getLanguage()) { | |
331 | $current = true; | |
332 | } | |
333 | ||
334 | $languages[] = array('name' => $language, 'current' => $current); | |
335 | } | |
336 | ||
337 | return $languages; | |
338 | } | |
339 | ||
8d3275be | 340 | public function getDefaultConfig() |
00dbaf90 | 341 | { |
8d3275be NL |
342 | return array( |
343 | 'pager' => PAGINATION, | |
344 | 'language' => LANG, | |
00dbaf90 NL |
345 | 'theme' => DEFAULT_THEME |
346 | ); | |
8d3275be NL |
347 | } |
348 | ||
f878daeb | 349 | protected function getPageContent(Url $url) |
350 | { | |
351 | $options = array('http' => array('user_agent' => 'poche')); | |
352 | if (isset($_SERVER['AUTH_TYPE']) && "basic" === strtolower($_SERVER['AUTH_TYPE'])) { | |
353 | $options['http']['header'] = sprintf( | |
354 | "Authorization: Basic %s", | |
355 | base64_encode( | |
356 | sprintf('%s:%s', $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) | |
357 | ) | |
358 | ); | |
359 | } | |
360 | $context = stream_context_create($options); | |
361 | $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed', false, $context); | |
362 | return json_decode($json, true); | |
363 | } | |
364 | ||
eb1af592 NL |
365 | /** |
366 | * Call action (mark as fav, archive, delete, etc.) | |
367 | */ | |
926acd7b | 368 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null) |
eb1af592 NL |
369 | { |
370 | switch ($action) | |
371 | { | |
372 | case 'add': | |
f878daeb | 373 | $content = $this->getPageContent($url); |
fa0bfb77 | 374 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); |
42c80841 | 375 | $body = $content['rss']['channel']['item']['description']; |
ec397236 | 376 | |
488fc63b MR |
377 | //search for possible duplicate if not in import mode |
378 | if (!$import) { | |
379 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); | |
380 | } | |
381 | ||
42c80841 | 382 | if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { |
ec397236 NL |
383 | Tools::logm('add link ' . $url->getUrl()); |
384 | $sequence = ''; | |
385 | if (STORAGE == 'postgres') { | |
386 | $sequence = 'entries_id_seq'; | |
eb1af592 | 387 | } |
ec397236 NL |
388 | $last_id = $this->store->getLastId($sequence); |
389 | if (DOWNLOAD_PICTURES) { | |
42c80841 | 390 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
ec397236 NL |
391 | Tools::logm('updating content article'); |
392 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
393 | } | |
488fc63b MR |
394 | |
395 | if ($duplicate != NULL) { | |
396 | // 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 | |
397 | Tools::logm('link ' . $url->getUrl() . ' is a duplicate'); | |
398 | // 1) - preserve tags and favorite, then drop old entry | |
399 | $this->store->reassignTags($duplicate['id'], $last_id); | |
400 | if ($duplicate['is_fav']) { | |
401 | $this->store->favoriteById($last_id, $this->user->getId()); | |
402 | } | |
403 | if ($this->store->deleteById($duplicate['id'], $this->user->getId())) { | |
404 | Tools::logm('previous link ' . $url->getUrl() .' entry deleted'); | |
405 | } | |
406 | } | |
407 | ||
ec397236 NL |
408 | if (!$import) { |
409 | $this->messages->add('s', _('the link has been added successfully')); | |
eb1af592 NL |
410 | } |
411 | } | |
412 | else { | |
b916bcfc | 413 | if (!$import) { |
ec397236 NL |
414 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
415 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc NL |
416 | } |
417 | } | |
ec397236 | 418 | |
b916bcfc | 419 | if (!$import) { |
363bc4eb | 420 | if ($autoclose == TRUE) { |
421 | Tools::redirect('?view=home'); | |
422 | } else { | |
f616ab60 | 423 | Tools::redirect('?view=home&closewin=true'); |
363bc4eb | 424 | } |
eb1af592 NL |
425 | } |
426 | break; | |
427 | case 'delete': | |
bc1ee852 | 428 | $msg = 'delete link #' . $id; |
8d3275be | 429 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
430 | if (DOWNLOAD_PICTURES) { |
431 | remove_directory(ABS_PATH . $id); | |
432 | } | |
6a361945 | 433 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
434 | } |
435 | else { | |
6a361945 | 436 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 437 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 438 | } |
bc1ee852 | 439 | Tools::logm($msg); |
985ce3ec | 440 | Tools::redirect('?'); |
eb1af592 NL |
441 | break; |
442 | case 'toggle_fav' : | |
8d3275be | 443 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 444 | Tools::logm('mark as favorite link #' . $id); |
b916bcfc NL |
445 | if (!$import) { |
446 | Tools::redirect(); | |
447 | } | |
eb1af592 NL |
448 | break; |
449 | case 'toggle_archive' : | |
8d3275be | 450 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 451 | Tools::logm('archive link #' . $id); |
b916bcfc NL |
452 | if (!$import) { |
453 | Tools::redirect(); | |
454 | } | |
eb1af592 | 455 | break; |
f14807de NL |
456 | case 'archive_all' : |
457 | $this->store->archiveAll($this->user->getId()); | |
458 | Tools::logm('archive all links'); | |
459 | if (!$import) { | |
460 | Tools::redirect(); | |
461 | } | |
462 | break; | |
c432fa16 | 463 | case 'add_tag' : |
926acd7b | 464 | if($import){ |
465 | $entry_id = $id; | |
466 | $tags = explode(',', $tags); | |
467 | } | |
468 | else{ | |
469 | $tags = explode(',', $_POST['value']); | |
470 | $entry_id = $_POST['entry_id']; | |
471 | } | |
b89d5a2b NL |
472 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); |
473 | if (!$entry) { | |
474 | $this->messages->add('e', _('Article not found!')); | |
475 | Tools::logm('error : article not found'); | |
476 | Tools::redirect(); | |
477 | } | |
c432fa16 NL |
478 | foreach($tags as $key => $tag_value) { |
479 | $value = trim($tag_value); | |
480 | $tag = $this->store->retrieveTagByValue($value); | |
481 | ||
482 | if (is_null($tag)) { | |
483 | # we create the tag | |
484 | $tag = $this->store->createTag($value); | |
485 | $sequence = ''; | |
486 | if (STORAGE == 'postgres') { | |
487 | $sequence = 'tags_id_seq'; | |
488 | } | |
489 | $tag_id = $this->store->getLastId($sequence); | |
490 | } | |
491 | else { | |
492 | $tag_id = $tag['id']; | |
493 | } | |
494 | ||
495 | # we assign the tag to the article | |
496 | $this->store->setTagToEntry($tag_id, $entry_id); | |
497 | } | |
926acd7b | 498 | if(!$import) { |
499 | Tools::redirect(); | |
500 | } | |
c432fa16 NL |
501 | break; |
502 | case 'remove_tag' : | |
503 | $tag_id = $_GET['tag_id']; | |
b89d5a2b NL |
504 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
505 | if (!$entry) { | |
506 | $this->messages->add('e', _('Article not found!')); | |
507 | Tools::logm('error : article not found'); | |
508 | Tools::redirect(); | |
509 | } | |
c432fa16 NL |
510 | $this->store->removeTagForEntry($id, $tag_id); |
511 | Tools::redirect(); | |
512 | break; | |
eb1af592 NL |
513 | default: |
514 | break; | |
515 | } | |
516 | } | |
517 | ||
518 | function displayView($view, $id = 0) | |
519 | { | |
520 | $tpl_vars = array(); | |
521 | ||
522 | switch ($view) | |
523 | { | |
eb1af592 | 524 | case 'config': |
044bf638 NL |
525 | $dev = trim($this->getPocheVersion('dev')); |
526 | $prod = trim($this->getPocheVersion('prod')); | |
031df528 NL |
527 | $compare_dev = version_compare(POCHE, $dev); |
528 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 529 | $themes = $this->getInstalledThemes(); |
5011388f | 530 | $languages = $this->getInstalledLanguages(); |
72c20a52 | 531 | $token = $this->user->getConfigValue('token'); |
1810c13b | 532 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; |
32520785 | 533 | $tpl_vars = array( |
00dbaf90 | 534 | 'themes' => $themes, |
5011388f | 535 | 'languages' => $languages, |
32520785 NL |
536 | 'dev' => $dev, |
537 | 'prod' => $prod, | |
538 | 'compare_dev' => $compare_dev, | |
539 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
540 | 'token' => $token, |
541 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 542 | 'http_auth' => $http_auth, |
32520785 | 543 | ); |
eb1af592 NL |
544 | Tools::logm('config view'); |
545 | break; | |
6cab59c3 NL |
546 | case 'edit-tags': |
547 | # tags | |
b89d5a2b NL |
548 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
549 | if (!$entry) { | |
550 | $this->messages->add('e', _('Article not found!')); | |
551 | Tools::logm('error : article not found'); | |
552 | Tools::redirect(); | |
553 | } | |
6cab59c3 NL |
554 | $tags = $this->store->retrieveTagsByEntry($id); |
555 | $tpl_vars = array( | |
c432fa16 | 556 | 'entry_id' => $id, |
6cab59c3 NL |
557 | 'tags' => $tags, |
558 | ); | |
559 | break; | |
4886ed6d | 560 | case 'tag': |
b89d5a2b NL |
561 | $entries = $this->store->retrieveEntriesByTag($id, $this->user->getId()); |
562 | $tag = $this->store->retrieveTag($id, $this->user->getId()); | |
4886ed6d NL |
563 | $tpl_vars = array( |
564 | 'tag' => $tag, | |
565 | 'entries' => $entries, | |
566 | ); | |
567 | break; | |
2e2ebe5e | 568 | case 'tags': |
f778e472 | 569 | $token = $this->user->getConfigValue('token'); |
b89d5a2b | 570 | $tags = $this->store->retrieveAllTags($this->user->getId()); |
2e2ebe5e | 571 | $tpl_vars = array( |
f778e472 NL |
572 | 'token' => $token, |
573 | 'user_id' => $this->user->getId(), | |
2e2ebe5e NL |
574 | 'tags' => $tags, |
575 | ); | |
576 | break; | |
eb1af592 | 577 | case 'view': |
8d3275be | 578 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
579 | if ($entry != NULL) { |
580 | Tools::logm('view link #' . $id); | |
581 | $content = $entry['content']; | |
582 | if (function_exists('tidy_parse_string')) { | |
583 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
584 | $tidy->cleanRepair(); | |
585 | $content = $tidy->value; | |
3408ed48 | 586 | } |
a3223127 | 587 | |
3408ed48 NL |
588 | # flattr checking |
589 | $flattr = new FlattrItem(); | |
7b171c73 NL |
590 | $flattr->checkItem($entry['url'], $entry['id']); |
591 | ||
592 | # tags | |
593 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 594 | |
3408ed48 | 595 | $tpl_vars = array( |
7b171c73 NL |
596 | 'entry' => $entry, |
597 | 'content' => $content, | |
598 | 'flattr' => $flattr, | |
599 | 'tags' => $tags | |
3408ed48 | 600 | ); |
eb1af592 NL |
601 | } |
602 | else { | |
d8d1542e | 603 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
604 | } |
605 | break; | |
12d9cfbc | 606 | default: # home, favorites and archive views |
8d3275be | 607 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); |
eb1af592 | 608 | $tpl_vars = array( |
3eb04903 N |
609 | 'entries' => '', |
610 | 'page_links' => '', | |
7f9f5281 | 611 | 'nb_results' => '', |
eb1af592 | 612 | ); |
34d67c83 | 613 | |
3eb04903 N |
614 | if (count($entries) > 0) { |
615 | $this->pagination->set_total(count($entries)); | |
c515ffec MR |
616 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), |
617 | $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&')); | |
3eb04903 N |
618 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); |
619 | $tpl_vars['entries'] = $datas; | |
620 | $tpl_vars['page_links'] = $page_links; | |
7f9f5281 | 621 | $tpl_vars['nb_results'] = count($entries); |
3eb04903 | 622 | } |
6a361945 | 623 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
624 | break; |
625 | } | |
626 | ||
627 | return $tpl_vars; | |
628 | } | |
c765c367 | 629 | |
07ee09f4 NL |
630 | /** |
631 | * update the password of the current user. | |
632 | * if MODE_DEMO is TRUE, the password can't be updated. | |
633 | * @todo add the return value | |
634 | * @todo set the new password in function header like this updatePassword($newPassword) | |
635 | * @return boolean | |
636 | */ | |
c765c367 NL |
637 | public function updatePassword() |
638 | { | |
55821e04 | 639 | if (MODE_DEMO) { |
8d3275be | 640 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 641 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 642 | Tools::redirect('?view=config'); |
55821e04 NL |
643 | } |
644 | else { | |
645 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
646 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
647 | $this->messages->add('s', _('your password has been updated')); |
648 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 649 | Session::logout(); |
8d3275be | 650 | Tools::logm('password updated'); |
c765c367 NL |
651 | Tools::redirect(); |
652 | } | |
653 | else { | |
8d3275be | 654 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 655 | Tools::redirect('?view=config'); |
c765c367 NL |
656 | } |
657 | } | |
658 | } | |
659 | } | |
00dbaf90 NL |
660 | |
661 | public function updateTheme() | |
662 | { | |
663 | # no data | |
664 | if (empty($_POST['theme'])) { | |
665 | } | |
666 | ||
667 | # we are not going to change it to the current theme... | |
668 | if ($_POST['theme'] == $this->getTheme()) { | |
669 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
670 | Tools::redirect('?view=config'); | |
671 | } | |
672 | ||
673 | $themes = $this->getInstalledThemes(); | |
674 | $actualTheme = false; | |
675 | ||
f4fbfaa7 NL |
676 | foreach (array_keys($themes) as $theme) { |
677 | if ($theme == $_POST['theme']) { | |
00dbaf90 NL |
678 | $actualTheme = true; |
679 | break; | |
680 | } | |
681 | } | |
682 | ||
683 | if (! $actualTheme) { | |
684 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
685 | Tools::redirect('?view=config'); | |
686 | } | |
687 | ||
688 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); | |
689 | $this->messages->add('s', _('you have changed your theme preferences')); | |
690 | ||
691 | $currentConfig = $_SESSION['poche_user']->config; | |
692 | $currentConfig['theme'] = $_POST['theme']; | |
693 | ||
694 | $_SESSION['poche_user']->setConfig($currentConfig); | |
695 | ||
696 | Tools::redirect('?view=config'); | |
697 | } | |
c765c367 | 698 | |
5011388f NL |
699 | public function updateLanguage() |
700 | { | |
701 | # no data | |
702 | if (empty($_POST['language'])) { | |
703 | } | |
704 | ||
705 | # we are not going to change it to the current language... | |
706 | if ($_POST['language'] == $this->getLanguage()) { | |
707 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
708 | Tools::redirect('?view=config'); | |
709 | } | |
710 | ||
711 | $languages = $this->getInstalledLanguages(); | |
712 | $actualLanguage = false; | |
713 | ||
714 | foreach ($languages as $language) { | |
715 | if ($language['name'] == $_POST['language']) { | |
716 | $actualLanguage = true; | |
717 | break; | |
718 | } | |
719 | } | |
720 | ||
721 | if (! $actualLanguage) { | |
722 | $this->messages->add('e', _('that language does not seem to be installed')); | |
723 | Tools::redirect('?view=config'); | |
724 | } | |
725 | ||
726 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); | |
727 | $this->messages->add('s', _('you have changed your language preferences')); | |
728 | ||
729 | $currentConfig = $_SESSION['poche_user']->config; | |
730 | $currentConfig['language'] = $_POST['language']; | |
731 | ||
732 | $_SESSION['poche_user']->setConfig($currentConfig); | |
733 | ||
734 | Tools::redirect('?view=config'); | |
735 | } | |
736 | ||
df6afaf0 DS |
737 | /** |
738 | * get credentials from differents sources | |
739 | * it redirects the user to the $referer link | |
740 | * @return array | |
741 | */ | |
1810c13b NL |
742 | private function credentials() { |
743 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
6af66b11 | 744 | return array($_SERVER['PHP_AUTH_USER'],'php_auth',true); |
1810c13b NL |
745 | } |
746 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
6af66b11 | 747 | return array($_POST['login'],$_POST['password'],false); |
1810c13b NL |
748 | } |
749 | if(isset($_SERVER['REMOTE_USER'])) { | |
6af66b11 | 750 | return array($_SERVER['REMOTE_USER'],'http_auth',true); |
1810c13b | 751 | } |
5cfafc61 | 752 | |
6af66b11 MR |
753 | return array(false,false,false); |
754 | } | |
df6afaf0 | 755 | |
07ee09f4 NL |
756 | /** |
757 | * checks if login & password are correct and save the user in session. | |
758 | * it redirects the user to the $referer link | |
759 | * @param string $referer the url to redirect after login | |
760 | * @todo add the return value | |
761 | * @return boolean | |
762 | */ | |
c765c367 NL |
763 | public function login($referer) |
764 | { | |
6af66b11 | 765 | list($login,$password,$isauthenticated)=$this->credentials(); |
df6afaf0 DS |
766 | if($login === false || $password === false) { |
767 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
768 | Tools::logm('login failed'); | |
769 | Tools::redirect(); | |
770 | } | |
771 | if (!empty($login) && !empty($password)) { | |
6af66b11 | 772 | $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated); |
7ce7ec4c NL |
773 | if ($user != array()) { |
774 | # Save login into Session | |
6af66b11 MR |
775 | $longlastingsession = isset($_POST['longlastingsession']); |
776 | $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login); | |
777 | Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user))); | |
26929c08 | 778 | $this->messages->add('s', _('welcome to your wallabag')); |
8d3275be | 779 | Tools::logm('login successful'); |
c765c367 NL |
780 | Tools::redirect($referer); |
781 | } | |
8d3275be | 782 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
783 | Tools::logm('login failed'); |
784 | Tools::redirect(); | |
c765c367 NL |
785 | } |
786 | } | |
787 | ||
07ee09f4 NL |
788 | /** |
789 | * log out the poche user. It cleans the session. | |
790 | * @todo add the return value | |
791 | * @return boolean | |
792 | */ | |
c765c367 NL |
793 | public function logout() |
794 | { | |
7ce7ec4c | 795 | $this->user = array(); |
c765c367 | 796 | Session::logout(); |
b916bcfc | 797 | Tools::logm('logout'); |
c765c367 NL |
798 | Tools::redirect(); |
799 | } | |
800 | ||
07ee09f4 NL |
801 | /** |
802 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
803 | * @todo add the return value | |
66b6a3b5 | 804 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
805 | * @return boolean |
806 | */ | |
66b6a3b5 | 807 | private function importFromInstapaper($targetFile) |
c765c367 | 808 | { |
7f959169 | 809 | # TODO gestion des articles favs |
a62788c6 | 810 | $html = new simple_html_dom(); |
66b6a3b5 | 811 | $html->load_file($targetFile); |
b916bcfc | 812 | Tools::logm('starting import from instapaper'); |
a62788c6 NL |
813 | |
814 | $read = 0; | |
815 | $errors = array(); | |
816 | foreach($html->find('ol') as $ul) | |
817 | { | |
818 | foreach($ul->find('li') as $li) | |
819 | { | |
820 | $a = $li->find('a'); | |
821 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 822 | $this->action('add', $url, 0, TRUE); |
a62788c6 | 823 | if ($read == '1') { |
b916bcfc NL |
824 | $sequence = ''; |
825 | if (STORAGE == 'postgres') { | |
826 | $sequence = 'entries_id_seq'; | |
827 | } | |
828 | $last_id = $this->store->getLastId($sequence); | |
829 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
a62788c6 NL |
830 | } |
831 | } | |
7f959169 NL |
832 | |
833 | # the second <ol> is for read links | |
a62788c6 NL |
834 | $read = 1; |
835 | } | |
8d3275be | 836 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
837 | Tools::logm('import from instapaper completed'); |
838 | Tools::redirect(); | |
839 | } | |
c765c367 | 840 | |
07ee09f4 NL |
841 | /** |
842 | * import from Pocket. poche needs a ./ril_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 importFromPocket($targetFile) |
63c35580 | 848 | { |
7f959169 | 849 | # TODO gestion des articles favs |
63c35580 | 850 | $html = new simple_html_dom(); |
66b6a3b5 | 851 | $html->load_file($targetFile); |
b916bcfc | 852 | Tools::logm('starting import from pocket'); |
63c35580 NL |
853 | |
854 | $read = 0; | |
855 | $errors = array(); | |
856 | foreach($html->find('ul') as $ul) | |
857 | { | |
858 | foreach($ul->find('li') as $li) | |
c765c367 | 859 | { |
63c35580 NL |
860 | $a = $li->find('a'); |
861 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 862 | $this->action('add', $url, 0, TRUE); |
926acd7b | 863 | $sequence = ''; |
864 | if (STORAGE == 'postgres') { | |
865 | $sequence = 'entries_id_seq'; | |
866 | } | |
867 | $last_id = $this->store->getLastId($sequence); | |
63c35580 | 868 | if ($read == '1') { |
b916bcfc | 869 | $this->action('toggle_archive', $url, $last_id, TRUE); |
c765c367 | 870 | } |
926acd7b | 871 | $tags = $a[0]->tags; |
872 | if(!empty($tags)) { | |
873 | $this->action('add_tag',$url,$last_id,true,false,$tags); | |
874 | } | |
c765c367 | 875 | } |
7f959169 NL |
876 | |
877 | # the second <ul> is for read links | |
63c35580 | 878 | $read = 1; |
c765c367 | 879 | } |
8d3275be | 880 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
881 | Tools::logm('import from pocket completed'); |
882 | Tools::redirect(); | |
883 | } | |
c765c367 | 884 | |
07ee09f4 NL |
885 | /** |
886 | * import from Readability. poche needs a ./readability file | |
887 | * @todo add the return value | |
66b6a3b5 | 888 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
889 | * @return boolean |
890 | */ | |
66b6a3b5 | 891 | private function importFromReadability($targetFile) |
63c35580 | 892 | { |
7f959169 | 893 | # TODO gestion des articles lus / favs |
66b6a3b5 | 894 | $str_data = file_get_contents($targetFile); |
63c35580 | 895 | $data = json_decode($str_data,true); |
b916bcfc | 896 | Tools::logm('starting import from Readability'); |
c0d321c1 | 897 | $count = 0; |
63c35580 | 898 | foreach ($data as $key => $value) { |
c0d321c1 NL |
899 | $url = NULL; |
900 | $favorite = FALSE; | |
901 | $archive = FALSE; | |
9bc32632 NL |
902 | foreach ($value as $item) { |
903 | foreach ($item as $attr => $value) { | |
904 | if ($attr == 'article__url') { | |
905 | $url = new Url(base64_encode($value)); | |
c0d321c1 | 906 | } |
9bc32632 NL |
907 | $sequence = ''; |
908 | if (STORAGE == 'postgres') { | |
909 | $sequence = 'entries_id_seq'; | |
910 | } | |
911 | if ($value == 'true') { | |
912 | if ($attr == 'favorite') { | |
913 | $favorite = TRUE; | |
914 | } | |
915 | if ($attr == 'archive') { | |
916 | $archive = TRUE; | |
917 | } | |
c0d321c1 NL |
918 | } |
919 | } | |
9bc32632 NL |
920 | |
921 | # we can add the url | |
922 | if (!is_null($url) && $url->isCorrect()) { | |
923 | $this->action('add', $url, 0, TRUE); | |
924 | $count++; | |
925 | if ($favorite) { | |
926 | $last_id = $this->store->getLastId($sequence); | |
927 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
928 | } | |
929 | if ($archive) { | |
930 | $last_id = $this->store->getLastId($sequence); | |
931 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
932 | } | |
b916bcfc | 933 | } |
c765c367 | 934 | } |
c765c367 | 935 | } |
c0d321c1 | 936 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); |
63c35580 NL |
937 | Tools::logm('import from Readability completed'); |
938 | Tools::redirect(); | |
c765c367 NL |
939 | } |
940 | ||
89051914 NL |
941 | /** |
942 | * import from Poche exported file | |
943 | * @param string $targetFile the file used for importing | |
944 | * @return boolean | |
945 | */ | |
946 | private function importFromPoche($targetFile) | |
947 | { | |
948 | $str_data = file_get_contents($targetFile); | |
949 | $data = json_decode($str_data,true); | |
950 | Tools::logm('starting import from Poche'); | |
951 | ||
952 | ||
953 | $sequence = ''; | |
954 | if (STORAGE == 'postgres') { | |
955 | $sequence = 'entries_id_seq'; | |
956 | } | |
957 | ||
958 | $count = 0; | |
959 | foreach ($data as $value) { | |
960 | ||
961 | $url = new Url(base64_encode($value['url'])); | |
962 | $favorite = ($value['is_fav'] == -1); | |
963 | $archive = ($value['is_read'] == -1); | |
964 | ||
965 | # we can add the url | |
966 | if (!is_null($url) && $url->isCorrect()) { | |
967 | ||
968 | $this->action('add', $url, 0, TRUE); | |
969 | ||
970 | $count++; | |
971 | if ($favorite) { | |
972 | $last_id = $this->store->getLastId($sequence); | |
973 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
974 | } | |
975 | if ($archive) { | |
976 | $last_id = $this->store->getLastId($sequence); | |
977 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
978 | } | |
979 | } | |
980 | ||
981 | } | |
982 | $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.')); | |
983 | Tools::logm('import from Poche completed'); | |
984 | Tools::redirect(); | |
985 | } | |
986 | ||
07ee09f4 NL |
987 | /** |
988 | * import datas into your poche | |
989 | * @param string $from name of the service to import : pocket, instapaper or readability | |
990 | * @todo add the return value | |
991 | * @return boolean | |
992 | */ | |
63c35580 | 993 | public function import($from) |
c765c367 | 994 | { |
66b6a3b5 E |
995 | $providers = array( |
996 | 'pocket' => 'importFromPocket', | |
997 | 'readability' => 'importFromReadability', | |
89051914 NL |
998 | 'instapaper' => 'importFromInstapaper', |
999 | 'poche' => 'importFromPoche', | |
66b6a3b5 E |
1000 | ); |
1001 | ||
1002 | if (! isset($providers[$from])) { | |
1003 | $this->messages->add('e', _('Unknown import provider.')); | |
1004 | Tools::redirect(); | |
63c35580 | 1005 | } |
66b6a3b5 E |
1006 | |
1007 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
1008 | $targetFile = constant($targetDefinition); | |
1009 | ||
1010 | if (! defined($targetDefinition)) { | |
1011 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
1012 | Tools::redirect(); | |
63c35580 | 1013 | } |
66b6a3b5 E |
1014 | |
1015 | if (! file_exists($targetFile)) { | |
1016 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
1017 | Tools::redirect(); | |
63c35580 | 1018 | } |
66b6a3b5 E |
1019 | |
1020 | $this->$providers[$from]($targetFile); | |
63c35580 | 1021 | } |
c765c367 | 1022 | |
07ee09f4 NL |
1023 | /** |
1024 | * export poche entries in json | |
1025 | * @return json all poche entries | |
1026 | */ | |
63c35580 NL |
1027 | public function export() |
1028 | { | |
8d3275be | 1029 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
1030 | echo $this->tpl->render('export.twig', array( |
1031 | 'export' => Tools::renderJson($entries), | |
1032 | )); | |
1033 | Tools::logm('export view'); | |
c765c367 | 1034 | } |
32520785 | 1035 | |
07ee09f4 | 1036 | /** |
a3436d4c | 1037 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
1038 | * @param string $which 'prod' or 'dev' |
1039 | * @return string latest $which version | |
1040 | */ | |
32520785 NL |
1041 | private function getPocheVersion($which = 'prod') |
1042 | { | |
1043 | $cache_file = CACHE . '/' . $which; | |
a3436d4c NL |
1044 | |
1045 | # checks if the cached version file exists | |
32520785 NL |
1046 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { |
1047 | $version = file_get_contents($cache_file); | |
1048 | } else { | |
3e0e7e12 | 1049 | $version = file_get_contents('http://static.wallabag.org/versions/' . $which); |
32520785 NL |
1050 | file_put_contents($cache_file, $version, LOCK_EX); |
1051 | } | |
1052 | return $version; | |
1053 | } | |
72c20a52 NL |
1054 | |
1055 | public function generateToken() | |
1056 | { | |
1057 | if (ini_get('open_basedir') === '') { | |
1058 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
1059 | } | |
1060 | else { | |
1061 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
1062 | } | |
1063 | ||
044bf638 | 1064 | $token = str_replace('+', '', $token); |
72c20a52 NL |
1065 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); |
1066 | $currentConfig = $_SESSION['poche_user']->config; | |
1067 | $currentConfig['token'] = $token; | |
1068 | $_SESSION['poche_user']->setConfig($currentConfig); | |
1069 | } | |
1070 | ||
f778e472 | 1071 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') |
72c20a52 | 1072 | { |
f778e472 | 1073 | $allowed_types = array('home', 'fav', 'archive', 'tag'); |
72c20a52 NL |
1074 | $config = $this->store->getConfigUser($user_id); |
1075 | ||
1076 | if (!in_array($type, $allowed_types) || | |
1077 | $token != $config['token']) { | |
1078 | die(_('Uh, there is a problem while generating feeds.')); | |
1079 | } | |
1080 | // Check the token | |
1081 | ||
9e7c840b | 1082 | $feed = new FeedWriter(RSS2); |
2e4440c3 | 1083 | $feed->setTitle('wallabag — ' . $type . ' feed'); |
72c20a52 | 1084 | $feed->setLink(Tools::getPocheUrl()); |
9e7c840b | 1085 | $feed->setChannelElement('updated', date(DATE_RSS , time())); |
2e4440c3 | 1086 | $feed->setChannelElement('author', 'wallabag'); |
72c20a52 | 1087 | |
f778e472 | 1088 | if ($type == 'tag') { |
b89d5a2b | 1089 | $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); |
f778e472 NL |
1090 | } |
1091 | else { | |
1092 | $entries = $this->store->getEntriesByView($type, $user_id); | |
1093 | } | |
1094 | ||
72c20a52 NL |
1095 | if (count($entries) > 0) { |
1096 | foreach ($entries as $entry) { | |
1097 | $newItem = $feed->createNewItem(); | |
0b57c682 | 1098 | $newItem->setTitle($entry['title']); |
72c20a52 NL |
1099 | $newItem->setLink(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); |
1100 | $newItem->setDate(time()); | |
1101 | $newItem->setDescription($entry['content']); | |
1102 | $feed->addItem($newItem); | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | $feed->genarateFeed(); | |
1107 | exit; | |
1108 | } | |
6285e57c NL |
1109 | |
1110 | public function emptyCache() { | |
1111 | $files = new RecursiveIteratorIterator( | |
1112 | new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS), | |
1113 | RecursiveIteratorIterator::CHILD_FIRST | |
1114 | ); | |
1115 | ||
1116 | foreach ($files as $fileinfo) { | |
1117 | $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); | |
1118 | $todo($fileinfo->getRealPath()); | |
1119 | } | |
1120 | ||
1121 | Tools::logm('empty cache'); | |
1122 | $this->messages->add('s', _('Cache deleted.')); | |
1123 | Tools::redirect(); | |
1124 | } | |
df6afaf0 | 1125 | } |