]>
Commit | Line | Data |
---|---|---|
eb1af592 NL |
1 | <?php |
2 | /** | |
3 | * poche, a read it later open source system | |
4 | * | |
5 | * @category poche | |
6 | * @author Nicolas Lœuillet <support@inthepoche.com> | |
7 | * @copyright 2013 | |
8 | * @license http://www.wtfpl.net/ see COPYING file | |
9 | */ | |
10 | ||
11 | class Poche | |
12 | { | |
00dbaf90 NL |
13 | public static $canRenderTemplates = true; |
14 | public static $configFileAvailable = true; | |
15 | ||
7ce7ec4c | 16 | public $user; |
eb1af592 NL |
17 | public $store; |
18 | public $tpl; | |
55821e04 | 19 | public $messages; |
6a361945 | 20 | public $pagination; |
00dbaf90 NL |
21 | |
22 | private $currentTheme = ''; | |
5011388f | 23 | private $currentLanguage = ''; |
9d3b88b3 | 24 | private $notInstalledMessage = array(); |
00dbaf90 NL |
25 | |
26 | # @todo make this dynamic (actually install themes and save them in the database including author information et cetera) | |
27 | private $installedThemes = array( | |
28 | 'default' => array('requires' => array()), | |
29 | 'dark' => array('requires' => array('default')), | |
30 | 'dmagenta' => array('requires' => array('default')), | |
31 | 'solarized' => array('requires' => array('default')), | |
32 | 'solarized-dark' => array('requires' => array('default')) | |
33 | ); | |
eb1af592 | 34 | |
00dbaf90 | 35 | public function __construct() |
eb1af592 | 36 | { |
9d3b88b3 NL |
37 | if ($this->configFileIsAvailable()) { |
38 | $this->init(); | |
00dbaf90 NL |
39 | } |
40 | ||
9d3b88b3 NL |
41 | if ($this->themeIsInstalled()) { |
42 | $this->initTpl(); | |
00dbaf90 NL |
43 | } |
44 | ||
9d3b88b3 NL |
45 | if ($this->systemIsInstalled()) { |
46 | $this->store = new Database(); | |
47 | $this->messages = new Messages(); | |
48 | # installation | |
49 | if (! $this->store->isInstalled()) { | |
50 | $this->install(); | |
51 | } | |
eb1af592 | 52 | } |
eb1af592 | 53 | } |
00dbaf90 NL |
54 | |
55 | private function init() | |
56 | { | |
57 | Tools::initPhp(); | |
58 | Session::$sessionName = 'poche'; | |
59 | Session::init(); | |
eb1af592 | 60 | |
00dbaf90 NL |
61 | if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) { |
62 | $this->user = $_SESSION['poche_user']; | |
63 | } else { | |
64 | # fake user, just for install & login screens | |
65 | $this->user = new User(); | |
66 | $this->user->setConfig($this->getDefaultConfig()); | |
67 | } | |
68 | ||
69 | # l10n | |
70 | $language = $this->user->getConfigValue('language'); | |
71 | putenv('LC_ALL=' . $language); | |
72 | setlocale(LC_ALL, $language); | |
73 | bindtextdomain($language, LOCALE); | |
74 | textdomain($language); | |
75 | ||
76 | # Pagination | |
77 | $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); | |
78 | ||
79 | # Set up theme | |
80 | $themeDirectory = $this->user->getConfigValue('theme'); | |
81 | ||
82 | if ($themeDirectory === false) { | |
83 | $themeDirectory = DEFAULT_THEME; | |
84 | } | |
85 | ||
86 | $this->currentTheme = $themeDirectory; | |
5011388f NL |
87 | |
88 | # Set up language | |
89 | $languageDirectory = $this->user->getConfigValue('language'); | |
90 | ||
91 | if ($languageDirectory === false) { | |
92 | $languageDirectory = DEFAULT_THEME; | |
93 | } | |
94 | ||
95 | $this->currentLanguage = $languageDirectory; | |
00dbaf90 NL |
96 | } |
97 | ||
98 | public function configFileIsAvailable() { | |
99 | if (! self::$configFileAvailable) { | |
9d3b88b3 | 100 | $this->notInstalledMessage[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.'; |
00dbaf90 NL |
101 | |
102 | return false; | |
103 | } | |
104 | ||
105 | return true; | |
106 | } | |
107 | ||
108 | public function themeIsInstalled() { | |
9d3b88b3 | 109 | $passTheme = TRUE; |
00dbaf90 NL |
110 | # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet |
111 | if (! self::$canRenderTemplates) { | |
9d3b88b3 NL |
112 | $this->notInstalledMessage[] = 'Twig does not seem to be installed. Please initialize the Composer installation to automatically fetch dependencies. Have a look at <a href="http://doc.inthepoche.com/doku.php?id=users:begin:install">the documentation.</a>'; |
113 | $passTheme = FALSE; | |
00dbaf90 | 114 | } |
7f17a38d NL |
115 | |
116 | if (! is_writable(CACHE)) { | |
9d3b88b3 | 117 | $this->notInstalledMessage[] = 'You don\'t have write access on cache directory.'; |
7f17a38d NL |
118 | |
119 | self::$canRenderTemplates = false; | |
120 | ||
9d3b88b3 | 121 | $passTheme = FALSE; |
7f17a38d | 122 | } |
00dbaf90 NL |
123 | |
124 | # Check if the selected theme and its requirements are present | |
9d3b88b3 NL |
125 | if ($this->getTheme() != '' && ! is_dir(THEME . '/' . $this->getTheme())) { |
126 | $this->notInstalledMessage[] = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')'; | |
00dbaf90 NL |
127 | |
128 | self::$canRenderTemplates = false; | |
129 | ||
9d3b88b3 | 130 | $passTheme = FALSE; |
00dbaf90 NL |
131 | } |
132 | ||
133 | foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) { | |
134 | if (! is_dir(THEME . '/' . $requiredTheme)) { | |
9d3b88b3 | 135 | $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')'; |
00dbaf90 NL |
136 | |
137 | self::$canRenderTemplates = false; | |
138 | ||
9d3b88b3 | 139 | $passTheme = FALSE; |
00dbaf90 NL |
140 | } |
141 | } | |
9d3b88b3 NL |
142 | |
143 | if (!$passTheme) { | |
144 | return FALSE; | |
145 | } | |
146 | ||
00dbaf90 NL |
147 | |
148 | return true; | |
149 | } | |
150 | ||
4a291288 NL |
151 | /** |
152 | * all checks before installation. | |
00dbaf90 | 153 | * @todo move HTML to template |
4a291288 NL |
154 | * @return boolean |
155 | */ | |
00dbaf90 | 156 | public function systemIsInstalled() |
eb1af592 | 157 | { |
9d3b88b3 | 158 | $msg = TRUE; |
00dbaf90 NL |
159 | |
160 | $configSalt = defined('SALT') ? constant('SALT') : ''; | |
161 | ||
162 | if (empty($configSalt)) { | |
9d3b88b3 NL |
163 | $this->notInstalledMessage[] = 'You have not yet filled in the SALT value in the config.inc.php file.'; |
164 | $msg = FALSE; | |
165 | } | |
166 | if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) { | |
00dbaf90 | 167 | Tools::logm('sqlite file doesn\'t exist'); |
9d3b88b3 NL |
168 | $this->notInstalledMessage[] = 'sqlite file doesn\'t exist, you can find it in install folder. Copy it in /db folder.'; |
169 | $msg = FALSE; | |
170 | } | |
171 | if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) { | |
172 | $this->notInstalledMessage[] = 'you have to delete the /install folder before using poche.'; | |
173 | $msg = FALSE; | |
174 | } | |
175 | if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) { | |
bb5a7d9e | 176 | Tools::logm('you don\'t have write access on sqlite file'); |
9d3b88b3 NL |
177 | $this->notInstalledMessage[] = 'You don\'t have write access on sqlite file.'; |
178 | $msg = FALSE; | |
bb5a7d9e | 179 | } |
00dbaf90 | 180 | |
9d3b88b3 | 181 | if (! $msg) { |
00dbaf90 | 182 | return false; |
8d3275be | 183 | } |
7ce7ec4c | 184 | |
00dbaf90 NL |
185 | return true; |
186 | } | |
187 | ||
188 | public function getNotInstalledMessage() { | |
189 | return $this->notInstalledMessage; | |
4a291288 | 190 | } |
eb1af592 | 191 | |
4a291288 NL |
192 | private function initTpl() |
193 | { | |
00dbaf90 NL |
194 | $loaderChain = new Twig_Loader_Chain(); |
195 | ||
196 | # add the current theme as first to the loader chain so Twig will look there first for overridden template files | |
197 | try { | |
198 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme())); | |
199 | } catch (Twig_Error_Loader $e) { | |
200 | # @todo isInstalled() should catch this, inject Twig later | |
201 | die('The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (' . THEME . '/' . $this->getTheme() .' is missing)'); | |
202 | } | |
203 | ||
204 | # add all required themes to the loader chain | |
205 | foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) { | |
206 | try { | |
207 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . DEFAULT_THEME)); | |
208 | } catch (Twig_Error_Loader $e) { | |
209 | # @todo isInstalled() should catch this, inject Twig later | |
210 | die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')'); | |
211 | } | |
212 | } | |
213 | ||
bc1ee852 NL |
214 | if (DEBUG_POCHE) { |
215 | $twig_params = array(); | |
00dbaf90 | 216 | } else { |
bc1ee852 NL |
217 | $twig_params = array('cache' => CACHE); |
218 | } | |
00dbaf90 NL |
219 | |
220 | $this->tpl = new Twig_Environment($loaderChain, $twig_params); | |
eb1af592 | 221 | $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); |
00dbaf90 | 222 | |
55821e04 NL |
223 | # filter to display domain name of an url |
224 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | |
225 | $this->tpl->addFilter($filter); | |
eb1af592 | 226 | |
d9178758 NL |
227 | # filter for reading time |
228 | $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime'); | |
229 | $this->tpl->addFilter($filter); | |
00dbaf90 NL |
230 | |
231 | # filter for simple filenames in config view | |
232 | $filter = new Twig_SimpleFilter('getPrettyFilename', function($string) { return str_replace(ROOT, '', $string); }); | |
233 | $this->tpl->addFilter($filter); | |
eb1af592 NL |
234 | } |
235 | ||
236 | private function install() | |
237 | { | |
238 | Tools::logm('poche still not installed'); | |
239 | echo $this->tpl->render('install.twig', array( | |
00dbaf90 NL |
240 | 'token' => Session::getToken(), |
241 | 'theme' => $this->getTheme(), | |
242 | 'poche_url' => Tools::getPocheUrl() | |
eb1af592 NL |
243 | )); |
244 | if (isset($_GET['install'])) { | |
245 | if (($_POST['password'] == $_POST['password_repeat']) | |
246 | && $_POST['password'] != "" && $_POST['login'] != "") { | |
247 | # let's rock, install poche baby ! | |
bb5a7d9e NL |
248 | if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) |
249 | { | |
250 | Session::logout(); | |
251 | Tools::logm('poche is now installed'); | |
252 | Tools::redirect(); | |
253 | } | |
6a361945 NL |
254 | } |
255 | else { | |
256 | Tools::logm('error during installation'); | |
eb1af592 NL |
257 | Tools::redirect(); |
258 | } | |
259 | } | |
260 | exit(); | |
261 | } | |
00dbaf90 NL |
262 | |
263 | public function getTheme() { | |
264 | return $this->currentTheme; | |
265 | } | |
5011388f NL |
266 | |
267 | public function getLanguage() { | |
268 | return $this->currentLanguage; | |
269 | } | |
00dbaf90 NL |
270 | |
271 | public function getInstalledThemes() { | |
272 | $handle = opendir(THEME); | |
273 | $themes = array(); | |
274 | ||
275 | while (($theme = readdir($handle)) !== false) { | |
276 | # Themes are stored in a directory, so all directory names are themes | |
277 | # @todo move theme installation data to database | |
89812ec8 | 278 | if (! is_dir(THEME . '/' . $theme) || in_array($theme, array('..', '.'))) { |
00dbaf90 NL |
279 | continue; |
280 | } | |
281 | ||
282 | $current = false; | |
283 | ||
284 | if ($theme === $this->getTheme()) { | |
285 | $current = true; | |
286 | } | |
287 | ||
288 | $themes[] = array('name' => $theme, 'current' => $current); | |
289 | } | |
290 | ||
2287bf06 | 291 | sort($themes); |
00dbaf90 NL |
292 | return $themes; |
293 | } | |
eb1af592 | 294 | |
5011388f NL |
295 | public function getInstalledLanguages() { |
296 | $handle = opendir(LOCALE); | |
297 | $languages = array(); | |
298 | ||
299 | while (($language = readdir($handle)) !== false) { | |
300 | # Languages are stored in a directory, so all directory names are languages | |
301 | # @todo move language installation data to database | |
302 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.'))) { | |
303 | continue; | |
304 | } | |
305 | ||
306 | $current = false; | |
307 | ||
308 | if ($language === $this->getLanguage()) { | |
309 | $current = true; | |
310 | } | |
311 | ||
312 | $languages[] = array('name' => $language, 'current' => $current); | |
313 | } | |
314 | ||
315 | return $languages; | |
316 | } | |
317 | ||
8d3275be | 318 | public function getDefaultConfig() |
00dbaf90 | 319 | { |
8d3275be NL |
320 | return array( |
321 | 'pager' => PAGINATION, | |
322 | 'language' => LANG, | |
00dbaf90 NL |
323 | 'theme' => DEFAULT_THEME |
324 | ); | |
8d3275be NL |
325 | } |
326 | ||
eb1af592 NL |
327 | /** |
328 | * Call action (mark as fav, archive, delete, etc.) | |
329 | */ | |
363bc4eb | 330 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE) |
eb1af592 NL |
331 | { |
332 | switch ($action) | |
333 | { | |
334 | case 'add': | |
ec397236 NL |
335 | $content = $url->extract(); |
336 | ||
337 | if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) { | |
338 | Tools::logm('add link ' . $url->getUrl()); | |
339 | $sequence = ''; | |
340 | if (STORAGE == 'postgres') { | |
341 | $sequence = 'entries_id_seq'; | |
eb1af592 | 342 | } |
ec397236 NL |
343 | $last_id = $this->store->getLastId($sequence); |
344 | if (DOWNLOAD_PICTURES) { | |
6fb46003 | 345 | $content = filtre_picture($content['body'], $url->getUrl(), $last_id); |
ec397236 NL |
346 | Tools::logm('updating content article'); |
347 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
348 | } | |
349 | if (!$import) { | |
350 | $this->messages->add('s', _('the link has been added successfully')); | |
eb1af592 NL |
351 | } |
352 | } | |
353 | else { | |
b916bcfc | 354 | if (!$import) { |
ec397236 NL |
355 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
356 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc NL |
357 | } |
358 | } | |
ec397236 | 359 | |
b916bcfc | 360 | if (!$import) { |
363bc4eb | 361 | if ($autoclose == TRUE) { |
362 | Tools::redirect('?view=home'); | |
363 | } else { | |
f616ab60 | 364 | Tools::redirect('?view=home&closewin=true'); |
363bc4eb | 365 | } |
eb1af592 NL |
366 | } |
367 | break; | |
368 | case 'delete': | |
bc1ee852 | 369 | $msg = 'delete link #' . $id; |
8d3275be | 370 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
371 | if (DOWNLOAD_PICTURES) { |
372 | remove_directory(ABS_PATH . $id); | |
373 | } | |
6a361945 | 374 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
375 | } |
376 | else { | |
6a361945 | 377 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 378 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 379 | } |
bc1ee852 | 380 | Tools::logm($msg); |
985ce3ec | 381 | Tools::redirect('?'); |
eb1af592 NL |
382 | break; |
383 | case 'toggle_fav' : | |
8d3275be | 384 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 385 | Tools::logm('mark as favorite link #' . $id); |
b916bcfc NL |
386 | if (!$import) { |
387 | Tools::redirect(); | |
388 | } | |
eb1af592 NL |
389 | break; |
390 | case 'toggle_archive' : | |
8d3275be | 391 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 392 | Tools::logm('archive link #' . $id); |
b916bcfc NL |
393 | if (!$import) { |
394 | Tools::redirect(); | |
395 | } | |
eb1af592 NL |
396 | break; |
397 | default: | |
398 | break; | |
399 | } | |
400 | } | |
401 | ||
402 | function displayView($view, $id = 0) | |
403 | { | |
404 | $tpl_vars = array(); | |
405 | ||
406 | switch ($view) | |
407 | { | |
eb1af592 | 408 | case 'config': |
32520785 NL |
409 | $dev = $this->getPocheVersion('dev'); |
410 | $prod = $this->getPocheVersion('prod'); | |
031df528 NL |
411 | $compare_dev = version_compare(POCHE, $dev); |
412 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 413 | $themes = $this->getInstalledThemes(); |
5011388f | 414 | $languages = $this->getInstalledLanguages(); |
df6afaf0 | 415 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']))?true:false; |
32520785 | 416 | $tpl_vars = array( |
00dbaf90 | 417 | 'themes' => $themes, |
5011388f | 418 | 'languages' => $languages, |
32520785 NL |
419 | 'dev' => $dev, |
420 | 'prod' => $prod, | |
421 | 'compare_dev' => $compare_dev, | |
422 | 'compare_prod' => $compare_prod, | |
df6afaf0 | 423 | 'http_auth' => $http_auth, |
32520785 | 424 | ); |
eb1af592 NL |
425 | Tools::logm('config view'); |
426 | break; | |
427 | case 'view': | |
8d3275be | 428 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
429 | if ($entry != NULL) { |
430 | Tools::logm('view link #' . $id); | |
431 | $content = $entry['content']; | |
432 | if (function_exists('tidy_parse_string')) { | |
433 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
434 | $tidy->cleanRepair(); | |
435 | $content = $tidy->value; | |
3408ed48 | 436 | } |
a3223127 | 437 | |
3408ed48 NL |
438 | # flattr checking |
439 | $flattr = new FlattrItem(); | |
4e5b0411 | 440 | $flattr->checkItem($entry['url'],$entry['id']); |
a3223127 | 441 | |
3408ed48 NL |
442 | $tpl_vars = array( |
443 | 'entry' => $entry, | |
444 | 'content' => $content, | |
445 | 'flattr' => $flattr | |
446 | ); | |
eb1af592 NL |
447 | } |
448 | else { | |
d8d1542e | 449 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
450 | } |
451 | break; | |
12d9cfbc | 452 | default: # home, favorites and archive views |
8d3275be | 453 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); |
eb1af592 | 454 | $tpl_vars = array( |
3eb04903 N |
455 | 'entries' => '', |
456 | 'page_links' => '', | |
7f9f5281 | 457 | 'nb_results' => '', |
eb1af592 | 458 | ); |
34d67c83 | 459 | |
3eb04903 N |
460 | if (count($entries) > 0) { |
461 | $this->pagination->set_total(count($entries)); | |
462 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
463 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); | |
464 | $tpl_vars['entries'] = $datas; | |
465 | $tpl_vars['page_links'] = $page_links; | |
7f9f5281 | 466 | $tpl_vars['nb_results'] = count($entries); |
3eb04903 | 467 | } |
6a361945 | 468 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
469 | break; |
470 | } | |
471 | ||
472 | return $tpl_vars; | |
473 | } | |
c765c367 | 474 | |
07ee09f4 NL |
475 | /** |
476 | * update the password of the current user. | |
477 | * if MODE_DEMO is TRUE, the password can't be updated. | |
478 | * @todo add the return value | |
479 | * @todo set the new password in function header like this updatePassword($newPassword) | |
480 | * @return boolean | |
481 | */ | |
c765c367 NL |
482 | public function updatePassword() |
483 | { | |
55821e04 | 484 | if (MODE_DEMO) { |
8d3275be | 485 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 486 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 487 | Tools::redirect('?view=config'); |
55821e04 NL |
488 | } |
489 | else { | |
490 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
491 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
492 | $this->messages->add('s', _('your password has been updated')); |
493 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 494 | Session::logout(); |
8d3275be | 495 | Tools::logm('password updated'); |
c765c367 NL |
496 | Tools::redirect(); |
497 | } | |
498 | else { | |
8d3275be | 499 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 500 | Tools::redirect('?view=config'); |
c765c367 NL |
501 | } |
502 | } | |
503 | } | |
504 | } | |
00dbaf90 NL |
505 | |
506 | public function updateTheme() | |
507 | { | |
508 | # no data | |
509 | if (empty($_POST['theme'])) { | |
510 | } | |
511 | ||
512 | # we are not going to change it to the current theme... | |
513 | if ($_POST['theme'] == $this->getTheme()) { | |
514 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
515 | Tools::redirect('?view=config'); | |
516 | } | |
517 | ||
518 | $themes = $this->getInstalledThemes(); | |
519 | $actualTheme = false; | |
520 | ||
521 | foreach ($themes as $theme) { | |
522 | if ($theme['name'] == $_POST['theme']) { | |
523 | $actualTheme = true; | |
524 | break; | |
525 | } | |
526 | } | |
527 | ||
528 | if (! $actualTheme) { | |
529 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
530 | Tools::redirect('?view=config'); | |
531 | } | |
532 | ||
533 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); | |
534 | $this->messages->add('s', _('you have changed your theme preferences')); | |
535 | ||
536 | $currentConfig = $_SESSION['poche_user']->config; | |
537 | $currentConfig['theme'] = $_POST['theme']; | |
538 | ||
539 | $_SESSION['poche_user']->setConfig($currentConfig); | |
540 | ||
541 | Tools::redirect('?view=config'); | |
542 | } | |
c765c367 | 543 | |
5011388f NL |
544 | public function updateLanguage() |
545 | { | |
546 | # no data | |
547 | if (empty($_POST['language'])) { | |
548 | } | |
549 | ||
550 | # we are not going to change it to the current language... | |
551 | if ($_POST['language'] == $this->getLanguage()) { | |
552 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
553 | Tools::redirect('?view=config'); | |
554 | } | |
555 | ||
556 | $languages = $this->getInstalledLanguages(); | |
557 | $actualLanguage = false; | |
558 | ||
559 | foreach ($languages as $language) { | |
560 | if ($language['name'] == $_POST['language']) { | |
561 | $actualLanguage = true; | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
566 | if (! $actualLanguage) { | |
567 | $this->messages->add('e', _('that language does not seem to be installed')); | |
568 | Tools::redirect('?view=config'); | |
569 | } | |
570 | ||
571 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); | |
572 | $this->messages->add('s', _('you have changed your language preferences')); | |
573 | ||
574 | $currentConfig = $_SESSION['poche_user']->config; | |
575 | $currentConfig['language'] = $_POST['language']; | |
576 | ||
577 | $_SESSION['poche_user']->setConfig($currentConfig); | |
578 | ||
579 | Tools::redirect('?view=config'); | |
580 | } | |
581 | ||
df6afaf0 DS |
582 | /** |
583 | * get credentials from differents sources | |
584 | * it redirects the user to the $referer link | |
585 | * @return array | |
586 | */ | |
587 | private function credentials() { | |
588 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
589 | return array($_SERVER['PHP_AUTH_USER'],'php_auth'); | |
590 | } | |
591 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
592 | return array($_POST['login'],$_POST['password']); | |
593 | } | |
594 | return array(false,false); | |
595 | } | |
596 | ||
07ee09f4 NL |
597 | /** |
598 | * checks if login & password are correct and save the user in session. | |
599 | * it redirects the user to the $referer link | |
600 | * @param string $referer the url to redirect after login | |
601 | * @todo add the return value | |
602 | * @return boolean | |
603 | */ | |
c765c367 NL |
604 | public function login($referer) |
605 | { | |
df6afaf0 DS |
606 | list($login,$password)=$this->credentials(); |
607 | if($login === false || $password === false) { | |
608 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
609 | Tools::logm('login failed'); | |
610 | Tools::redirect(); | |
611 | } | |
612 | if (!empty($login) && !empty($password)) { | |
613 | $user = $this->store->login($login, Tools::encodeString($password . $login)); | |
7ce7ec4c NL |
614 | if ($user != array()) { |
615 | # Save login into Session | |
df6afaf0 | 616 | Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), array('poche_user' => new User($user))); |
8d3275be | 617 | $this->messages->add('s', _('welcome to your poche')); |
8d3275be | 618 | Tools::logm('login successful'); |
c765c367 NL |
619 | Tools::redirect($referer); |
620 | } | |
8d3275be | 621 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
622 | Tools::logm('login failed'); |
623 | Tools::redirect(); | |
c765c367 NL |
624 | } |
625 | } | |
626 | ||
07ee09f4 NL |
627 | /** |
628 | * log out the poche user. It cleans the session. | |
629 | * @todo add the return value | |
630 | * @return boolean | |
631 | */ | |
c765c367 NL |
632 | public function logout() |
633 | { | |
7ce7ec4c | 634 | $this->user = array(); |
c765c367 | 635 | Session::logout(); |
b916bcfc NL |
636 | $this->messages->add('s', _('see you soon!')); |
637 | Tools::logm('logout'); | |
c765c367 NL |
638 | Tools::redirect(); |
639 | } | |
640 | ||
07ee09f4 NL |
641 | /** |
642 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
643 | * @todo add the return value | |
66b6a3b5 | 644 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
645 | * @return boolean |
646 | */ | |
66b6a3b5 | 647 | private function importFromInstapaper($targetFile) |
c765c367 | 648 | { |
7f959169 | 649 | # TODO gestion des articles favs |
a62788c6 | 650 | $html = new simple_html_dom(); |
66b6a3b5 | 651 | $html->load_file($targetFile); |
b916bcfc | 652 | Tools::logm('starting import from instapaper'); |
a62788c6 NL |
653 | |
654 | $read = 0; | |
655 | $errors = array(); | |
656 | foreach($html->find('ol') as $ul) | |
657 | { | |
658 | foreach($ul->find('li') as $li) | |
659 | { | |
660 | $a = $li->find('a'); | |
661 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 662 | $this->action('add', $url, 0, TRUE); |
a62788c6 | 663 | if ($read == '1') { |
b916bcfc NL |
664 | $sequence = ''; |
665 | if (STORAGE == 'postgres') { | |
666 | $sequence = 'entries_id_seq'; | |
667 | } | |
668 | $last_id = $this->store->getLastId($sequence); | |
669 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
a62788c6 NL |
670 | } |
671 | } | |
7f959169 NL |
672 | |
673 | # the second <ol> is for read links | |
a62788c6 NL |
674 | $read = 1; |
675 | } | |
8d3275be | 676 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
677 | Tools::logm('import from instapaper completed'); |
678 | Tools::redirect(); | |
679 | } | |
c765c367 | 680 | |
07ee09f4 NL |
681 | /** |
682 | * import from Pocket. poche needs a ./ril_export.html file | |
683 | * @todo add the return value | |
66b6a3b5 | 684 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
685 | * @return boolean |
686 | */ | |
66b6a3b5 | 687 | private function importFromPocket($targetFile) |
63c35580 | 688 | { |
7f959169 | 689 | # TODO gestion des articles favs |
63c35580 | 690 | $html = new simple_html_dom(); |
66b6a3b5 | 691 | $html->load_file($targetFile); |
b916bcfc | 692 | Tools::logm('starting import from pocket'); |
63c35580 NL |
693 | |
694 | $read = 0; | |
695 | $errors = array(); | |
696 | foreach($html->find('ul') as $ul) | |
697 | { | |
698 | foreach($ul->find('li') as $li) | |
c765c367 | 699 | { |
63c35580 NL |
700 | $a = $li->find('a'); |
701 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 702 | $this->action('add', $url, 0, TRUE); |
63c35580 | 703 | if ($read == '1') { |
b916bcfc NL |
704 | $sequence = ''; |
705 | if (STORAGE == 'postgres') { | |
706 | $sequence = 'entries_id_seq'; | |
707 | } | |
708 | $last_id = $this->store->getLastId($sequence); | |
709 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
c765c367 | 710 | } |
c765c367 | 711 | } |
7f959169 NL |
712 | |
713 | # the second <ul> is for read links | |
63c35580 | 714 | $read = 1; |
c765c367 | 715 | } |
8d3275be | 716 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
717 | Tools::logm('import from pocket completed'); |
718 | Tools::redirect(); | |
719 | } | |
c765c367 | 720 | |
07ee09f4 NL |
721 | /** |
722 | * import from Readability. poche needs a ./readability file | |
723 | * @todo add the return value | |
66b6a3b5 | 724 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
725 | * @return boolean |
726 | */ | |
66b6a3b5 | 727 | private function importFromReadability($targetFile) |
63c35580 | 728 | { |
7f959169 | 729 | # TODO gestion des articles lus / favs |
66b6a3b5 | 730 | $str_data = file_get_contents($targetFile); |
63c35580 | 731 | $data = json_decode($str_data,true); |
b916bcfc | 732 | Tools::logm('starting import from Readability'); |
c0d321c1 | 733 | $count = 0; |
63c35580 | 734 | foreach ($data as $key => $value) { |
c0d321c1 NL |
735 | $url = NULL; |
736 | $favorite = FALSE; | |
737 | $archive = FALSE; | |
7f959169 NL |
738 | foreach ($value as $attr => $attr_value) { |
739 | if ($attr == 'article__url') { | |
740 | $url = new Url(base64_encode($attr_value)); | |
c765c367 | 741 | } |
b916bcfc NL |
742 | $sequence = ''; |
743 | if (STORAGE == 'postgres') { | |
744 | $sequence = 'entries_id_seq'; | |
745 | } | |
c0d321c1 NL |
746 | if ($attr_value == 'true') { |
747 | if ($attr == 'favorite') { | |
748 | $favorite = TRUE; | |
749 | } | |
750 | if ($attr == 'archive') { | |
751 | $archive = TRUE; | |
752 | } | |
753 | } | |
754 | } | |
755 | # we can add the url | |
756 | if (!is_null($url) && $url->isCorrect()) { | |
757 | $this->action('add', $url, 0, TRUE); | |
758 | $count++; | |
759 | if ($favorite) { | |
760 | $last_id = $this->store->getLastId($sequence); | |
761 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
762 | } | |
763 | if ($archive) { | |
b916bcfc NL |
764 | $last_id = $this->store->getLastId($sequence); |
765 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
766 | } | |
c765c367 | 767 | } |
c765c367 | 768 | } |
c0d321c1 | 769 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); |
63c35580 NL |
770 | Tools::logm('import from Readability completed'); |
771 | Tools::redirect(); | |
c765c367 NL |
772 | } |
773 | ||
07ee09f4 NL |
774 | /** |
775 | * import datas into your poche | |
776 | * @param string $from name of the service to import : pocket, instapaper or readability | |
777 | * @todo add the return value | |
778 | * @return boolean | |
779 | */ | |
63c35580 | 780 | public function import($from) |
c765c367 | 781 | { |
66b6a3b5 E |
782 | $providers = array( |
783 | 'pocket' => 'importFromPocket', | |
784 | 'readability' => 'importFromReadability', | |
785 | 'instapaper' => 'importFromInstapaper' | |
786 | ); | |
787 | ||
788 | if (! isset($providers[$from])) { | |
789 | $this->messages->add('e', _('Unknown import provider.')); | |
790 | Tools::redirect(); | |
63c35580 | 791 | } |
66b6a3b5 E |
792 | |
793 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
794 | $targetFile = constant($targetDefinition); | |
795 | ||
796 | if (! defined($targetDefinition)) { | |
797 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
798 | Tools::redirect(); | |
63c35580 | 799 | } |
66b6a3b5 E |
800 | |
801 | if (! file_exists($targetFile)) { | |
802 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
803 | Tools::redirect(); | |
63c35580 | 804 | } |
66b6a3b5 E |
805 | |
806 | $this->$providers[$from]($targetFile); | |
63c35580 | 807 | } |
c765c367 | 808 | |
07ee09f4 NL |
809 | /** |
810 | * export poche entries in json | |
811 | * @return json all poche entries | |
812 | */ | |
63c35580 NL |
813 | public function export() |
814 | { | |
8d3275be | 815 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
816 | echo $this->tpl->render('export.twig', array( |
817 | 'export' => Tools::renderJson($entries), | |
818 | )); | |
819 | Tools::logm('export view'); | |
c765c367 | 820 | } |
32520785 | 821 | |
07ee09f4 | 822 | /** |
a3436d4c | 823 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
824 | * @param string $which 'prod' or 'dev' |
825 | * @return string latest $which version | |
826 | */ | |
32520785 NL |
827 | private function getPocheVersion($which = 'prod') |
828 | { | |
829 | $cache_file = CACHE . '/' . $which; | |
a3436d4c NL |
830 | |
831 | # checks if the cached version file exists | |
32520785 NL |
832 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { |
833 | $version = file_get_contents($cache_file); | |
834 | } else { | |
bc1ee852 | 835 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); |
32520785 NL |
836 | file_put_contents($cache_file, $version, LOCK_EX); |
837 | } | |
838 | return $version; | |
839 | } | |
df6afaf0 | 840 | } |