]>
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'); | |
b6413975 | 75 | @putenv('LC_ALL=' . $language); |
00dbaf90 | 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) { | |
43c7b978 | 104 | $this->notInstalledMessage[] = 'You have to copy (don\'t just rename!) inc/poche/config.inc.default.php 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 | 243 | } |
b6413975 | 244 | |
4d99bae8 | 245 | public function createNewUser() { |
246 | if (isset($_GET['newuser'])){ | |
247 | if ($_POST['newusername'] != "" && $_POST['password4newuser'] != ""){ | |
248 | $newusername = filter_var($_POST['newusername'], FILTER_SANITIZE_STRING); | |
b6413975 | 249 | if (!$this->store->userExists($newusername)){ |
4d99bae8 | 250 | if ($this->store->install($newusername, Tools::encodeString($_POST['password4newuser'] . $newusername))) { |
251 | Tools::logm('The new user '.$newusername.' has been installed'); | |
252 | $this->messages->add('s', sprintf(_('The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>'),$newusername)); | |
253 | Tools::redirect(); | |
254 | } | |
255 | else { | |
256 | Tools::logm('error during adding new user'); | |
257 | Tools::redirect(); | |
258 | } | |
259 | } | |
260 | else { | |
261 | $this->messages->add('e', sprintf(_('Error : An user with the name %s already exists !'),$newusername)); | |
262 | Tools::logm('An user with the name '.$newusername.' already exists !'); | |
263 | Tools::redirect(); | |
264 | } | |
265 | } | |
266 | } | |
267 | } | |
b6413975 | 268 | |
4d99bae8 | 269 | public function deleteUser(){ |
270 | if (isset($_GET['deluser'])){ | |
271 | if ($this->store->listUsers() > 1) { | |
272 | if (Tools::encodeString($_POST['password4deletinguser'].$this->user->getUsername()) == $this->store->getUserPassword($this->user->getId())) { | |
273 | $username = $this->user->getUsername(); | |
274 | $this->store->deleteUserConfig($this->user->getId()); | |
275 | Tools::logm('The configuration for user '. $username .' has been deleted !'); | |
276 | $this->store->deleteTagsEntriesAndEntries($this->user->getId()); | |
277 | Tools::logm('The entries for user '. $username .' has been deleted !'); | |
278 | $this->store->deleteUser($this->user->getId()); | |
279 | Tools::logm('User '. $username .' has been completely deleted !'); | |
280 | Session::logout(); | |
281 | Tools::logm('logout'); | |
282 | Tools::redirect(); | |
283 | $this->messages->add('s', sprintf(_('User %s has been successfully deleted !'),$newusername)); | |
284 | } | |
285 | else { | |
286 | Tools::logm('Bad password !'); | |
287 | $this->messages->add('e', _('Error : The password is wrong !')); | |
288 | } | |
289 | } | |
290 | else { | |
291 | Tools::logm('Only user !'); | |
292 | $this->messages->add('e', _('Error : You are the only user, you cannot delete your account !')); | |
293 | } | |
294 | } | |
295 | } | |
eb1af592 | 296 | |
f4fbfaa7 | 297 | private function install() |
eb1af592 NL |
298 | { |
299 | Tools::logm('poche still not installed'); | |
300 | echo $this->tpl->render('install.twig', array( | |
00dbaf90 NL |
301 | 'token' => Session::getToken(), |
302 | 'theme' => $this->getTheme(), | |
303 | 'poche_url' => Tools::getPocheUrl() | |
eb1af592 NL |
304 | )); |
305 | if (isset($_GET['install'])) { | |
182faf26 | 306 | if (($_POST['password'] == $_POST['password_repeat']) |
eb1af592 NL |
307 | && $_POST['password'] != "" && $_POST['login'] != "") { |
308 | # let's rock, install poche baby ! | |
bb5a7d9e NL |
309 | if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) |
310 | { | |
311 | Session::logout(); | |
312 | Tools::logm('poche is now installed'); | |
313 | Tools::redirect(); | |
314 | } | |
6a361945 NL |
315 | } |
316 | else { | |
317 | Tools::logm('error during installation'); | |
eb1af592 NL |
318 | Tools::redirect(); |
319 | } | |
320 | } | |
321 | exit(); | |
322 | } | |
182faf26 | 323 | |
00dbaf90 NL |
324 | public function getTheme() { |
325 | return $this->currentTheme; | |
326 | } | |
5011388f | 327 | |
f4fbfaa7 NL |
328 | /** |
329 | * Provides theme information by parsing theme.ini file if present in the theme's root directory. | |
330 | * In all cases, the following data will be returned: | |
331 | * - name: theme's name, or key if the theme is unnamed, | |
332 | * - current: boolean informing if the theme is the current user theme. | |
333 | * | |
334 | * @param string $theme Theme key (directory name) | |
335 | * @return array|boolean Theme information, or false if the theme doesn't exist. | |
336 | */ | |
337 | public function getThemeInfo($theme) { | |
338 | if (!is_dir(THEME . '/' . $theme)) { | |
339 | return false; | |
340 | } | |
341 | ||
342 | $themeIniFile = THEME . '/' . $theme . '/theme.ini'; | |
343 | $themeInfo = array(); | |
344 | ||
345 | if (is_file($themeIniFile) && is_readable($themeIniFile)) { | |
346 | $themeInfo = parse_ini_file($themeIniFile); | |
347 | } | |
182faf26 | 348 | |
f4fbfaa7 NL |
349 | if ($themeInfo === false) { |
350 | $themeInfo = array(); | |
351 | } | |
352 | if (!isset($themeInfo['name'])) { | |
353 | $themeInfo['name'] = $theme; | |
354 | } | |
355 | $themeInfo['current'] = ($theme === $this->getTheme()); | |
356 | ||
357 | return $themeInfo; | |
5011388f | 358 | } |
182faf26 | 359 | |
00dbaf90 NL |
360 | public function getInstalledThemes() { |
361 | $handle = opendir(THEME); | |
362 | $themes = array(); | |
f4fbfaa7 | 363 | |
00dbaf90 NL |
364 | while (($theme = readdir($handle)) !== false) { |
365 | # Themes are stored in a directory, so all directory names are themes | |
366 | # @todo move theme installation data to database | |
f4fbfaa7 | 367 | if (!is_dir(THEME . '/' . $theme) || in_array($theme, array('.', '..'))) { |
00dbaf90 NL |
368 | continue; |
369 | } | |
f4fbfaa7 NL |
370 | |
371 | $themes[$theme] = $this->getThemeInfo($theme); | |
00dbaf90 | 372 | } |
f4fbfaa7 | 373 | |
3ade95a3 NL |
374 | ksort($themes); |
375 | ||
00dbaf90 NL |
376 | return $themes; |
377 | } | |
eb1af592 | 378 | |
f4fbfaa7 NL |
379 | public function getLanguage() { |
380 | return $this->currentLanguage; | |
381 | } | |
382 | ||
5011388f NL |
383 | public function getInstalledLanguages() { |
384 | $handle = opendir(LOCALE); | |
385 | $languages = array(); | |
182faf26 | 386 | |
5011388f NL |
387 | while (($language = readdir($handle)) !== false) { |
388 | # Languages are stored in a directory, so all directory names are languages | |
389 | # @todo move language installation data to database | |
cbcae403 | 390 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.', 'tools'))) { |
5011388f NL |
391 | continue; |
392 | } | |
182faf26 | 393 | |
5011388f | 394 | $current = false; |
182faf26 | 395 | |
5011388f NL |
396 | if ($language === $this->getLanguage()) { |
397 | $current = true; | |
398 | } | |
182faf26 | 399 | |
cbcae403 | 400 | $languages[] = array('name' => (isset($this->language_names[$language]) ? $this->language_names[$language] : $language), 'value' => $language, 'current' => $current); |
5011388f | 401 | } |
182faf26 | 402 | |
5011388f NL |
403 | return $languages; |
404 | } | |
405 | ||
8d3275be | 406 | public function getDefaultConfig() |
182faf26 | 407 | { |
8d3275be NL |
408 | return array( |
409 | 'pager' => PAGINATION, | |
410 | 'language' => LANG, | |
00dbaf90 NL |
411 | 'theme' => DEFAULT_THEME |
412 | ); | |
8d3275be NL |
413 | } |
414 | ||
eb1af592 NL |
415 | /** |
416 | * Call action (mark as fav, archive, delete, etc.) | |
417 | */ | |
926acd7b | 418 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null) |
eb1af592 NL |
419 | { |
420 | switch ($action) | |
421 | { | |
422 | case 'add': | |
a297fb1e MR |
423 | $content = Tools::getPageContent($url); |
424 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); | |
425 | $body = $content['rss']['channel']['item']['description']; | |
426 | ||
427 | // clean content from prevent xss attack | |
0f859c6f | 428 | $purifier = $this->getPurifier(); |
a297fb1e MR |
429 | $title = $purifier->purify($title); |
430 | $body = $purifier->purify($body); | |
1570a653 | 431 | |
182faf26 | 432 | //search for possible duplicate |
8d7cd2cc | 433 | $duplicate = NULL; |
a297fb1e | 434 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); |
488fc63b | 435 | |
182faf26 | 436 | $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); |
a297fb1e | 437 | if ( $last_id ) { |
ec397236 | 438 | Tools::logm('add link ' . $url->getUrl()); |
ec397236 | 439 | if (DOWNLOAD_PICTURES) { |
42c80841 | 440 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
ec397236 NL |
441 | Tools::logm('updating content article'); |
442 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
443 | } | |
488fc63b MR |
444 | |
445 | if ($duplicate != NULL) { | |
446 | // 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 | |
447 | Tools::logm('link ' . $url->getUrl() . ' is a duplicate'); | |
448 | // 1) - preserve tags and favorite, then drop old entry | |
449 | $this->store->reassignTags($duplicate['id'], $last_id); | |
450 | if ($duplicate['is_fav']) { | |
451 | $this->store->favoriteById($last_id, $this->user->getId()); | |
452 | } | |
453 | if ($this->store->deleteById($duplicate['id'], $this->user->getId())) { | |
454 | Tools::logm('previous link ' . $url->getUrl() .' entry deleted'); | |
455 | } | |
456 | } | |
457 | ||
182faf26 | 458 | $this->messages->add('s', _('the link has been added successfully')); |
eb1af592 NL |
459 | } |
460 | else { | |
a297fb1e MR |
461 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
462 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc | 463 | } |
ec397236 | 464 | |
a297fb1e MR |
465 | if ($autoclose == TRUE) { |
466 | Tools::redirect('?view=home'); | |
467 | } else { | |
468 | Tools::redirect('?view=home&closewin=true'); | |
eb1af592 NL |
469 | } |
470 | break; | |
471 | case 'delete': | |
bc1ee852 | 472 | $msg = 'delete link #' . $id; |
8d3275be | 473 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
474 | if (DOWNLOAD_PICTURES) { |
475 | remove_directory(ABS_PATH . $id); | |
476 | } | |
6a361945 | 477 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
478 | } |
479 | else { | |
6a361945 | 480 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 481 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 482 | } |
bc1ee852 | 483 | Tools::logm($msg); |
985ce3ec | 484 | Tools::redirect('?'); |
eb1af592 NL |
485 | break; |
486 | case 'toggle_fav' : | |
8d3275be | 487 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 488 | Tools::logm('mark as favorite link #' . $id); |
c2cf7075 MR |
489 | if ( Tools::isAjaxRequest() ) { |
490 | echo 1; | |
491 | exit; | |
492 | } | |
493 | else { | |
494 | Tools::redirect(); | |
495 | } | |
eb1af592 NL |
496 | break; |
497 | case 'toggle_archive' : | |
8d3275be | 498 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 499 | Tools::logm('archive link #' . $id); |
c2cf7075 MR |
500 | if ( Tools::isAjaxRequest() ) { |
501 | echo 1; | |
502 | exit; | |
503 | } | |
504 | else { | |
505 | Tools::redirect(); | |
506 | } | |
eb1af592 | 507 | break; |
f14807de NL |
508 | case 'archive_all' : |
509 | $this->store->archiveAll($this->user->getId()); | |
510 | Tools::logm('archive all links'); | |
a297fb1e | 511 | Tools::redirect(); |
f14807de | 512 | break; |
c432fa16 | 513 | case 'add_tag' : |
decc23aa | 514 | if (isset($_GET['search'])) { |
515 | //when we want to apply a tag to a search | |
decc23aa | 516 | $tags = array($_GET['search']); |
517 | $allentry_ids = $this->store->search($tags[0], $this->user->getId()); | |
518 | $entry_ids = array(); | |
519 | foreach ($allentry_ids as $eachentry) { | |
520 | $entry_ids[] = $eachentry[0]; | |
521 | } | |
522 | } else { //add a tag to a single article | |
523 | $tags = explode(',', $_POST['value']); | |
524 | $entry_ids = array($_POST['entry_id']); | |
fb26cc93 | 525 | } |
decc23aa | 526 | foreach($entry_ids as $entry_id) { |
527 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); | |
528 | if (!$entry) { | |
529 | $this->messages->add('e', _('Article not found!')); | |
530 | Tools::logm('error : article not found'); | |
531 | Tools::redirect(); | |
532 | } | |
533 | //get all already set tags to preven duplicates | |
534 | $already_set_tags = array(); | |
535 | $entry_tags = $this->store->retrieveTagsByEntry($entry_id); | |
536 | foreach ($entry_tags as $tag) { | |
537 | $already_set_tags[] = $tag['value']; | |
538 | } | |
539 | foreach($tags as $key => $tag_value) { | |
540 | $value = trim($tag_value); | |
541 | if ($value && !in_array($value, $already_set_tags)) { | |
542 | $tag = $this->store->retrieveTagByValue($value); | |
543 | if (is_null($tag)) { | |
544 | # we create the tag | |
545 | $tag = $this->store->createTag($value); | |
546 | $sequence = ''; | |
547 | if (STORAGE == 'postgres') { | |
548 | $sequence = 'tags_id_seq'; | |
549 | } | |
550 | $tag_id = $this->store->getLastId($sequence); | |
fb26cc93 | 551 | } |
decc23aa | 552 | else { |
553 | $tag_id = $tag['id']; | |
554 | } | |
555 | ||
556 | # we assign the tag to the article | |
557 | $this->store->setTagToEntry($tag_id, $entry_id); | |
558 | } | |
c432fa16 | 559 | } |
c432fa16 | 560 | } |
9c743ab9 | 561 | $this->messages->add('s', _('The tag has been applied successfully')); |
a297fb1e | 562 | Tools::redirect(); |
c432fa16 NL |
563 | break; |
564 | case 'remove_tag' : | |
565 | $tag_id = $_GET['tag_id']; | |
b89d5a2b NL |
566 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
567 | if (!$entry) { | |
568 | $this->messages->add('e', _('Article not found!')); | |
569 | Tools::logm('error : article not found'); | |
570 | Tools::redirect(); | |
571 | } | |
c432fa16 | 572 | $this->store->removeTagForEntry($id, $tag_id); |
9c743ab9 | 573 | Tools::logm('tag entry deleted'); |
574 | $this->store->cleanUnusedTags(); | |
575 | Tools::logm('old tags cleaned'); | |
576 | $this->messages->add('s', _('The tag has been successfully deleted')); | |
c432fa16 NL |
577 | Tools::redirect(); |
578 | break; | |
eb1af592 NL |
579 | default: |
580 | break; | |
581 | } | |
582 | } | |
583 | ||
584 | function displayView($view, $id = 0) | |
585 | { | |
586 | $tpl_vars = array(); | |
587 | ||
588 | switch ($view) | |
589 | { | |
eb1af592 | 590 | case 'config': |
11c680f9 NL |
591 | $dev_infos = $this->getPocheVersion('dev'); |
592 | $dev = trim($dev_infos[0]); | |
593 | $check_time_dev = date('d-M-Y H:i', $dev_infos[1]); | |
594 | $prod_infos = $this->getPocheVersion('prod'); | |
595 | $prod = trim($prod_infos[0]); | |
596 | $check_time_prod = date('d-M-Y H:i', $prod_infos[1]); | |
031df528 NL |
597 | $compare_dev = version_compare(POCHE, $dev); |
598 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 599 | $themes = $this->getInstalledThemes(); |
5011388f | 600 | $languages = $this->getInstalledLanguages(); |
72c20a52 | 601 | $token = $this->user->getConfigValue('token'); |
1810c13b | 602 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; |
4d99bae8 | 603 | $only_user = ($this->store->listUsers() > 1) ? false : true; |
32520785 | 604 | $tpl_vars = array( |
00dbaf90 | 605 | 'themes' => $themes, |
5011388f | 606 | 'languages' => $languages, |
32520785 NL |
607 | 'dev' => $dev, |
608 | 'prod' => $prod, | |
11c680f9 NL |
609 | 'check_time_dev' => $check_time_dev, |
610 | 'check_time_prod' => $check_time_prod, | |
32520785 NL |
611 | 'compare_dev' => $compare_dev, |
612 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
613 | 'token' => $token, |
614 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 615 | 'http_auth' => $http_auth, |
4d99bae8 | 616 | 'only_user' => $only_user |
32520785 | 617 | ); |
eb1af592 NL |
618 | Tools::logm('config view'); |
619 | break; | |
6cab59c3 NL |
620 | case 'edit-tags': |
621 | # tags | |
b89d5a2b NL |
622 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
623 | if (!$entry) { | |
624 | $this->messages->add('e', _('Article not found!')); | |
625 | Tools::logm('error : article not found'); | |
626 | Tools::redirect(); | |
627 | } | |
6cab59c3 NL |
628 | $tags = $this->store->retrieveTagsByEntry($id); |
629 | $tpl_vars = array( | |
c432fa16 | 630 | 'entry_id' => $id, |
6cab59c3 | 631 | 'tags' => $tags, |
032e0ca1 | 632 | 'entry' => $entry, |
4886ed6d NL |
633 | ); |
634 | break; | |
2e2ebe5e | 635 | case 'tags': |
f778e472 | 636 | $token = $this->user->getConfigValue('token'); |
fb26cc93 MR |
637 | //if term is set - search tags for this term |
638 | $term = Tools::checkVar('term'); | |
639 | $tags = $this->store->retrieveAllTags($this->user->getId(), $term); | |
640 | if (Tools::isAjaxRequest()) { | |
641 | $result = array(); | |
642 | foreach ($tags as $tag) { | |
643 | $result[] = $tag['value']; | |
644 | } | |
645 | echo json_encode($result); | |
646 | exit; | |
647 | } | |
2e2ebe5e | 648 | $tpl_vars = array( |
f778e472 NL |
649 | 'token' => $token, |
650 | 'user_id' => $this->user->getId(), | |
2e2ebe5e NL |
651 | 'tags' => $tags, |
652 | ); | |
653 | break; | |
a4585f7e MR |
654 | case 'search': |
655 | if (isset($_GET['search'])) { | |
656 | $search = filter_var($_GET['search'], FILTER_SANITIZE_STRING); | |
657 | $tpl_vars['entries'] = $this->store->search($search, $this->user->getId()); | |
658 | $count = count($tpl_vars['entries']); | |
659 | $this->pagination->set_total($count); | |
660 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), | |
661 | $this->pagination->page_links('?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION['sort'] . '&' )); | |
662 | $tpl_vars['page_links'] = $page_links; | |
663 | $tpl_vars['nb_results'] = $count; | |
664 | $tpl_vars['search_term'] = $search; | |
665 | } | |
666 | break; | |
eb1af592 | 667 | case 'view': |
8d3275be | 668 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
669 | if ($entry != NULL) { |
670 | Tools::logm('view link #' . $id); | |
671 | $content = $entry['content']; | |
672 | if (function_exists('tidy_parse_string')) { | |
673 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
674 | $tidy->cleanRepair(); | |
675 | $content = $tidy->value; | |
3408ed48 | 676 | } |
a3223127 | 677 | |
3408ed48 NL |
678 | # flattr checking |
679 | $flattr = new FlattrItem(); | |
7b171c73 NL |
680 | $flattr->checkItem($entry['url'], $entry['id']); |
681 | ||
682 | # tags | |
683 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 684 | |
3408ed48 | 685 | $tpl_vars = array( |
7b171c73 NL |
686 | 'entry' => $entry, |
687 | 'content' => $content, | |
688 | 'flattr' => $flattr, | |
689 | 'tags' => $tags | |
3408ed48 | 690 | ); |
eb1af592 NL |
691 | } |
692 | else { | |
d8d1542e | 693 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
694 | } |
695 | break; | |
032e0ca1 | 696 | default: # home, favorites, archive and tag views |
eb1af592 | 697 | $tpl_vars = array( |
3eb04903 N |
698 | 'entries' => '', |
699 | 'page_links' => '', | |
7f9f5281 | 700 | 'nb_results' => '', |
6065553c | 701 | 'listmode' => (isset($_COOKIE['listmode']) ? true : false), |
eb1af592 | 702 | ); |
182faf26 | 703 | |
032e0ca1 MR |
704 | //if id is given - we retrive entries by tag: id is tag id |
705 | if ($id) { | |
706 | $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId()); | |
707 | $tpl_vars['id'] = intval($id); | |
708 | } | |
709 | ||
710 | $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id); | |
711 | ||
712 | if ($count > 0) { | |
713 | $this->pagination->set_total($count); | |
c515ffec | 714 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), |
032e0ca1 MR |
715 | $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' )); |
716 | $tpl_vars['entries'] = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit(), $id); | |
3eb04903 | 717 | $tpl_vars['page_links'] = $page_links; |
032e0ca1 | 718 | $tpl_vars['nb_results'] = $count; |
3eb04903 | 719 | } |
6a361945 | 720 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
721 | break; |
722 | } | |
723 | ||
724 | return $tpl_vars; | |
725 | } | |
c765c367 | 726 | |
07ee09f4 | 727 | /** |
182faf26 MR |
728 | * update the password of the current user. |
729 | * if MODE_DEMO is TRUE, the password can't be updated. | |
07ee09f4 NL |
730 | * @todo add the return value |
731 | * @todo set the new password in function header like this updatePassword($newPassword) | |
732 | * @return boolean | |
733 | */ | |
c765c367 NL |
734 | public function updatePassword() |
735 | { | |
55821e04 | 736 | if (MODE_DEMO) { |
8d3275be | 737 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 738 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 739 | Tools::redirect('?view=config'); |
55821e04 NL |
740 | } |
741 | else { | |
742 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
743 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
744 | $this->messages->add('s', _('your password has been updated')); |
745 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 746 | Session::logout(); |
8d3275be | 747 | Tools::logm('password updated'); |
c765c367 NL |
748 | Tools::redirect(); |
749 | } | |
750 | else { | |
8d3275be | 751 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 752 | Tools::redirect('?view=config'); |
c765c367 NL |
753 | } |
754 | } | |
755 | } | |
756 | } | |
182faf26 | 757 | |
00dbaf90 NL |
758 | public function updateTheme() |
759 | { | |
760 | # no data | |
761 | if (empty($_POST['theme'])) { | |
762 | } | |
182faf26 | 763 | |
00dbaf90 NL |
764 | # we are not going to change it to the current theme... |
765 | if ($_POST['theme'] == $this->getTheme()) { | |
766 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
767 | Tools::redirect('?view=config'); | |
768 | } | |
182faf26 | 769 | |
00dbaf90 NL |
770 | $themes = $this->getInstalledThemes(); |
771 | $actualTheme = false; | |
182faf26 | 772 | |
f4fbfaa7 NL |
773 | foreach (array_keys($themes) as $theme) { |
774 | if ($theme == $_POST['theme']) { | |
00dbaf90 NL |
775 | $actualTheme = true; |
776 | break; | |
777 | } | |
778 | } | |
182faf26 | 779 | |
00dbaf90 NL |
780 | if (! $actualTheme) { |
781 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
782 | Tools::redirect('?view=config'); | |
783 | } | |
182faf26 | 784 | |
00dbaf90 NL |
785 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); |
786 | $this->messages->add('s', _('you have changed your theme preferences')); | |
182faf26 | 787 | |
00dbaf90 NL |
788 | $currentConfig = $_SESSION['poche_user']->config; |
789 | $currentConfig['theme'] = $_POST['theme']; | |
182faf26 | 790 | |
00dbaf90 | 791 | $_SESSION['poche_user']->setConfig($currentConfig); |
56532c4e NL |
792 | |
793 | $this->emptyCache(); | |
182faf26 | 794 | |
00dbaf90 NL |
795 | Tools::redirect('?view=config'); |
796 | } | |
c765c367 | 797 | |
5011388f NL |
798 | public function updateLanguage() |
799 | { | |
800 | # no data | |
801 | if (empty($_POST['language'])) { | |
802 | } | |
182faf26 | 803 | |
5011388f NL |
804 | # we are not going to change it to the current language... |
805 | if ($_POST['language'] == $this->getLanguage()) { | |
806 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
807 | Tools::redirect('?view=config'); | |
808 | } | |
182faf26 | 809 | |
5011388f NL |
810 | $languages = $this->getInstalledLanguages(); |
811 | $actualLanguage = false; | |
182faf26 | 812 | |
5011388f | 813 | foreach ($languages as $language) { |
c9bd17a1 | 814 | if ($language['value'] == $_POST['language']) { |
5011388f NL |
815 | $actualLanguage = true; |
816 | break; | |
817 | } | |
818 | } | |
182faf26 | 819 | |
5011388f NL |
820 | if (! $actualLanguage) { |
821 | $this->messages->add('e', _('that language does not seem to be installed')); | |
822 | Tools::redirect('?view=config'); | |
823 | } | |
182faf26 | 824 | |
5011388f NL |
825 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); |
826 | $this->messages->add('s', _('you have changed your language preferences')); | |
182faf26 | 827 | |
5011388f NL |
828 | $currentConfig = $_SESSION['poche_user']->config; |
829 | $currentConfig['language'] = $_POST['language']; | |
182faf26 | 830 | |
5011388f | 831 | $_SESSION['poche_user']->setConfig($currentConfig); |
e145f767 NL |
832 | |
833 | $this->emptyCache(); | |
182faf26 | 834 | |
5011388f | 835 | Tools::redirect('?view=config'); |
182faf26 | 836 | } |
df6afaf0 DS |
837 | /** |
838 | * get credentials from differents sources | |
839 | * it redirects the user to the $referer link | |
840 | * @return array | |
841 | */ | |
1810c13b NL |
842 | private function credentials() { |
843 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
6af66b11 | 844 | return array($_SERVER['PHP_AUTH_USER'],'php_auth',true); |
1810c13b NL |
845 | } |
846 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
6af66b11 | 847 | return array($_POST['login'],$_POST['password'],false); |
1810c13b NL |
848 | } |
849 | if(isset($_SERVER['REMOTE_USER'])) { | |
6af66b11 | 850 | return array($_SERVER['REMOTE_USER'],'http_auth',true); |
1810c13b | 851 | } |
5cfafc61 | 852 | |
6af66b11 MR |
853 | return array(false,false,false); |
854 | } | |
df6afaf0 | 855 | |
07ee09f4 NL |
856 | /** |
857 | * checks if login & password are correct and save the user in session. | |
858 | * it redirects the user to the $referer link | |
859 | * @param string $referer the url to redirect after login | |
860 | * @todo add the return value | |
861 | * @return boolean | |
862 | */ | |
c765c367 NL |
863 | public function login($referer) |
864 | { | |
6af66b11 | 865 | list($login,$password,$isauthenticated)=$this->credentials(); |
df6afaf0 DS |
866 | if($login === false || $password === false) { |
867 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
868 | Tools::logm('login failed'); | |
869 | Tools::redirect(); | |
870 | } | |
871 | if (!empty($login) && !empty($password)) { | |
6af66b11 | 872 | $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated); |
7ce7ec4c NL |
873 | if ($user != array()) { |
874 | # Save login into Session | |
6af66b11 MR |
875 | $longlastingsession = isset($_POST['longlastingsession']); |
876 | $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login); | |
877 | Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user))); | |
26929c08 | 878 | $this->messages->add('s', _('welcome to your wallabag')); |
8d3275be | 879 | Tools::logm('login successful'); |
c765c367 NL |
880 | Tools::redirect($referer); |
881 | } | |
8d3275be | 882 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
883 | Tools::logm('login failed'); |
884 | Tools::redirect(); | |
c765c367 NL |
885 | } |
886 | } | |
887 | ||
07ee09f4 NL |
888 | /** |
889 | * log out the poche user. It cleans the session. | |
890 | * @todo add the return value | |
182faf26 | 891 | * @return boolean |
07ee09f4 | 892 | */ |
c765c367 NL |
893 | public function logout() |
894 | { | |
7ce7ec4c | 895 | $this->user = array(); |
c765c367 | 896 | Session::logout(); |
b916bcfc | 897 | Tools::logm('logout'); |
c765c367 NL |
898 | Tools::redirect(); |
899 | } | |
900 | ||
07ee09f4 NL |
901 | /** |
902 | * import datas into your poche | |
182faf26 | 903 | * @return boolean |
07ee09f4 | 904 | */ |
182faf26 MR |
905 | public function import() { |
906 | ||
907 | if ( isset($_FILES['file']) ) { | |
5ce39784 MR |
908 | Tools::logm('Import stated: parsing file'); |
909 | ||
182faf26 MR |
910 | // assume, that file is in json format |
911 | $str_data = file_get_contents($_FILES['file']['tmp_name']); | |
912 | $data = json_decode($str_data, true); | |
913 | ||
914 | if ( $data === null ) { | |
915 | //not json - assume html | |
916 | $html = new simple_html_dom(); | |
917 | $html->load_file($_FILES['file']['tmp_name']); | |
918 | $data = array(); | |
919 | $read = 0; | |
920 | foreach (array('ol','ul') as $list) { | |
921 | foreach ($html->find($list) as $ul) { | |
86da3988 MR |
922 | foreach ($ul->find('li') as $li) { |
923 | $tmpEntry = array(); | |
a8ef1f3f MR |
924 | $a = $li->find('a'); |
925 | $tmpEntry['url'] = $a[0]->href; | |
926 | $tmpEntry['tags'] = $a[0]->tags; | |
927 | $tmpEntry['is_read'] = $read; | |
928 | if ($tmpEntry['url']) { | |
929 | $data[] = $tmpEntry; | |
930 | } | |
86da3988 MR |
931 | } |
932 | # the second <ol/ul> is for read links | |
933 | $read = ((sizeof($data) && $read)?0:1); | |
182faf26 MR |
934 | } |
935 | } | |
63c35580 | 936 | } |
182faf26 | 937 | |
a297fb1e MR |
938 | //for readability structure |
939 | foreach ($data as $record) { | |
940 | if (is_array($record)) { | |
941 | $data[] = $record; | |
942 | foreach ($record as $record2) { | |
943 | if (is_array($record2)) { | |
86da3988 | 944 | $data[] = $record2; |
a297fb1e MR |
945 | } |
946 | } | |
947 | } | |
948 | } | |
949 | ||
86da3988 | 950 | $urlsInserted = array(); //urls of articles inserted |
182faf26 | 951 | foreach ($data as $record) { |
a297fb1e | 952 | $url = trim( isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : '') ); |
86da3988 | 953 | if ( $url and !in_array($url, $urlsInserted) ) { |
182faf26 MR |
954 | $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ').'</a> <a href="./?import">'._('click to finish import').'</a><a>'); |
955 | $body = (isset($record['content']) ? $record['content'] : ''); | |
a297fb1e MR |
956 | $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive'])?intval($record['archive']):0)); |
957 | $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite'])?intval($record['favorite']):0) ); | |
182faf26 MR |
958 | //insert new record |
959 | $id = $this->store->add($url, $title, $body, $this->user->getId(), $isFavorite, $isRead); | |
960 | if ( $id ) { | |
86da3988 MR |
961 | $urlsInserted[] = $url; //add |
962 | ||
182faf26 | 963 | if ( isset($record['tags']) && trim($record['tags']) ) { |
86da3988 | 964 | //@TODO: set tags |
182faf26 MR |
965 | |
966 | } | |
967 | } | |
968 | } | |
969 | } | |
970 | ||
86da3988 | 971 | $i = sizeof($urlsInserted); |
182faf26 MR |
972 | if ( $i > 0 ) { |
973 | $this->messages->add('s', _('Articles inserted: ').$i._('. Please note, that some may be marked as "read".')); | |
974 | } | |
5ce39784 | 975 | Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).'); |
182faf26 MR |
976 | } |
977 | //file parsing finished here | |
978 | ||
979 | //now download article contents if any | |
980 | ||
981 | //check if we need to download any content | |
982 | $recordsDownloadRequired = $this->store->retrieveUnfetchedEntriesCount($this->user->getId()); | |
983 | if ( $recordsDownloadRequired == 0 ) { | |
984 | //nothing to download | |
985 | $this->messages->add('s', _('Import finished.')); | |
5ce39784 | 986 | Tools::logm('Import finished completely'); |
182faf26 MR |
987 | Tools::redirect(); |
988 | } | |
989 | else { | |
990 | //if just inserted - don't download anything, download will start in next reload | |
991 | if ( !isset($_FILES['file']) ) { | |
992 | //download next batch | |
5ce39784 | 993 | Tools::logm('Fetching next batch of articles...'); |
182faf26 MR |
994 | $items = $this->store->retrieveUnfetchedEntries($this->user->getId(), IMPORT_LIMIT); |
995 | ||
0f859c6f | 996 | $purifier = $this->getPurifier(); |
182faf26 MR |
997 | |
998 | foreach ($items as $item) { | |
86da3988 | 999 | $url = new Url(base64_encode($item['url'])); |
5ce39784 | 1000 | Tools::logm('Fetching article '.$item['id']); |
86da3988 | 1001 | $content = Tools::getPageContent($url); |
182faf26 | 1002 | |
86da3988 MR |
1003 | $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled')); |
1004 | $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined')); | |
182faf26 | 1005 | |
86da3988 MR |
1006 | //clean content to prevent xss attack |
1007 | $title = $purifier->purify($title); | |
1008 | $body = $purifier->purify($body); | |
182faf26 | 1009 | |
86da3988 | 1010 | $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId()); |
5ce39784 | 1011 | Tools::logm('Article '.$item['id'].' updated.'); |
182faf26 MR |
1012 | } |
1013 | ||
63c35580 | 1014 | } |
182faf26 MR |
1015 | } |
1016 | ||
1017 | return array('includeImport'=>true, 'import'=>array('recordsDownloadRequired'=>$recordsDownloadRequired, 'recordsUnderDownload'=> IMPORT_LIMIT, 'delay'=> IMPORT_DELAY * 1000) ); | |
63c35580 | 1018 | } |
c765c367 | 1019 | |
07ee09f4 NL |
1020 | /** |
1021 | * export poche entries in json | |
1022 | * @return json all poche entries | |
1023 | */ | |
a8ef1f3f MR |
1024 | public function export() { |
1025 | $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; | |
1026 | header('Content-Disposition: attachment; filename='.$filename); | |
1027 | ||
1028 | $entries = $this->store->retrieveAll($this->user->getId()); | |
1029 | echo $this->tpl->render('export.twig', array( | |
1030 | 'export' => Tools::renderJson($entries), | |
1031 | )); | |
1032 | Tools::logm('export view'); | |
c765c367 | 1033 | } |
32520785 | 1034 | |
07ee09f4 | 1035 | /** |
a3436d4c | 1036 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
1037 | * @param string $which 'prod' or 'dev' |
1038 | * @return string latest $which version | |
1039 | */ | |
a8ef1f3f MR |
1040 | private function getPocheVersion($which = 'prod') { |
1041 | $cache_file = CACHE . '/' . $which; | |
1042 | $check_time = time(); | |
1043 | ||
1044 | # checks if the cached version file exists | |
1045 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | |
1046 | $version = file_get_contents($cache_file); | |
1047 | $check_time = filemtime($cache_file); | |
1048 | } else { | |
1049 | $version = file_get_contents('http://static.wallabag.org/versions/' . $which); | |
1050 | file_put_contents($cache_file, $version, LOCK_EX); | |
1051 | } | |
1052 | return array($version, $check_time); | |
32520785 | 1053 | } |
72c20a52 NL |
1054 | |
1055 | public function generateToken() | |
1056 | { | |
a8ef1f3f MR |
1057 | if (ini_get('open_basedir') === '') { |
1058 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
1059 | echo 'This is a server using Windows!'; | |
1060 | // alternative to /dev/urandom for Windows | |
1061 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
1062 | } else { | |
1063 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
72c20a52 | 1064 | } |
a8ef1f3f MR |
1065 | } |
1066 | else { | |
1067 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
1068 | } | |
72c20a52 | 1069 | |
a8ef1f3f MR |
1070 | $token = str_replace('+', '', $token); |
1071 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | |
1072 | $currentConfig = $_SESSION['poche_user']->config; | |
1073 | $currentConfig['token'] = $token; | |
1074 | $_SESSION['poche_user']->setConfig($currentConfig); | |
1075 | Tools::redirect(); | |
72c20a52 NL |
1076 | } |
1077 | ||
f778e472 | 1078 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') |
72c20a52 | 1079 | { |
f778e472 | 1080 | $allowed_types = array('home', 'fav', 'archive', 'tag'); |
72c20a52 NL |
1081 | $config = $this->store->getConfigUser($user_id); |
1082 | ||
17b2afef NL |
1083 | if ($config == null) { |
1084 | die(_('User with this id (' . $user_id . ') does not exist.')); | |
1085 | } | |
1086 | ||
72c20a52 NL |
1087 | if (!in_array($type, $allowed_types) || |
1088 | $token != $config['token']) { | |
1089 | die(_('Uh, there is a problem while generating feeds.')); | |
1090 | } | |
1091 | // Check the token | |
1092 | ||
9e7c840b | 1093 | $feed = new FeedWriter(RSS2); |
2e4440c3 | 1094 | $feed->setTitle('wallabag — ' . $type . ' feed'); |
72c20a52 | 1095 | $feed->setLink(Tools::getPocheUrl()); |
223268c2 NL |
1096 | $feed->setChannelElement('pubDate', date(DATE_RSS , time())); |
1097 | $feed->setChannelElement('generator', 'wallabag'); | |
1098 | $feed->setDescription('wallabag ' . $type . ' elements'); | |
72c20a52 | 1099 | |
f778e472 | 1100 | if ($type == 'tag') { |
b89d5a2b | 1101 | $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); |
f778e472 NL |
1102 | } |
1103 | else { | |
1104 | $entries = $this->store->getEntriesByView($type, $user_id); | |
1105 | } | |
1106 | ||
72c20a52 NL |
1107 | if (count($entries) > 0) { |
1108 | foreach ($entries as $entry) { | |
1109 | $newItem = $feed->createNewItem(); | |
0b57c682 | 1110 | $newItem->setTitle($entry['title']); |
f86784c2 | 1111 | $newItem->setSource(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); |
ed02e38e | 1112 | $newItem->setLink($entry['url']); |
72c20a52 NL |
1113 | $newItem->setDate(time()); |
1114 | $newItem->setDescription($entry['content']); | |
1115 | $feed->addItem($newItem); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | $feed->genarateFeed(); | |
1120 | exit; | |
1121 | } | |
6285e57c NL |
1122 | |
1123 | public function emptyCache() { | |
1124 | $files = new RecursiveIteratorIterator( | |
1125 | new RecursiveDirectoryIterator(CACHE, RecursiveDirectoryIterator::SKIP_DOTS), | |
1126 | RecursiveIteratorIterator::CHILD_FIRST | |
1127 | ); | |
1128 | ||
1129 | foreach ($files as $fileinfo) { | |
1130 | $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); | |
1131 | $todo($fileinfo->getRealPath()); | |
1132 | } | |
1133 | ||
1134 | Tools::logm('empty cache'); | |
1135 | $this->messages->add('s', _('Cache deleted.')); | |
1136 | Tools::redirect(); | |
1137 | } | |
0f859c6f MR |
1138 | |
1139 | /** | |
1140 | * return new purifier object with actual config | |
1141 | */ | |
1142 | protected function getPurifier() { | |
1143 | $config = HTMLPurifier_Config::createDefault();\r | |
1144 | $config->set('Cache.SerializerPath', CACHE);\r | |
1145 | $config->set('HTML.SafeIframe', true);\r | |
1146 | $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo$purifier = new HTMLPurifier($config); | |
1147 | \r | |
1148 | return new HTMLPurifier($config); | |
1149 | } | |
df6afaf0 | 1150 | } |