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