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