]>
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': | |
42c80841 NL |
335 | $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed'); |
336 | $content = json_decode($json, true); | |
337 | $title = $content['rss']['channel']['item']['title']; | |
338 | $body = $content['rss']['channel']['item']['description']; | |
ec397236 | 339 | |
42c80841 | 340 | if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { |
ec397236 NL |
341 | Tools::logm('add link ' . $url->getUrl()); |
342 | $sequence = ''; | |
343 | if (STORAGE == 'postgres') { | |
344 | $sequence = 'entries_id_seq'; | |
eb1af592 | 345 | } |
ec397236 NL |
346 | $last_id = $this->store->getLastId($sequence); |
347 | if (DOWNLOAD_PICTURES) { | |
42c80841 | 348 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
ec397236 NL |
349 | Tools::logm('updating content article'); |
350 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
351 | } | |
352 | if (!$import) { | |
353 | $this->messages->add('s', _('the link has been added successfully')); | |
eb1af592 NL |
354 | } |
355 | } | |
356 | else { | |
b916bcfc | 357 | if (!$import) { |
ec397236 NL |
358 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
359 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc NL |
360 | } |
361 | } | |
ec397236 | 362 | |
b916bcfc | 363 | if (!$import) { |
363bc4eb | 364 | if ($autoclose == TRUE) { |
365 | Tools::redirect('?view=home'); | |
366 | } else { | |
f616ab60 | 367 | Tools::redirect('?view=home&closewin=true'); |
363bc4eb | 368 | } |
eb1af592 NL |
369 | } |
370 | break; | |
371 | case 'delete': | |
bc1ee852 | 372 | $msg = 'delete link #' . $id; |
8d3275be | 373 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
374 | if (DOWNLOAD_PICTURES) { |
375 | remove_directory(ABS_PATH . $id); | |
376 | } | |
6a361945 | 377 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
378 | } |
379 | else { | |
6a361945 | 380 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 381 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 382 | } |
bc1ee852 | 383 | Tools::logm($msg); |
985ce3ec | 384 | Tools::redirect('?'); |
eb1af592 NL |
385 | break; |
386 | case 'toggle_fav' : | |
8d3275be | 387 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 388 | Tools::logm('mark as favorite link #' . $id); |
b916bcfc NL |
389 | if (!$import) { |
390 | Tools::redirect(); | |
391 | } | |
eb1af592 NL |
392 | break; |
393 | case 'toggle_archive' : | |
8d3275be | 394 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 395 | Tools::logm('archive link #' . $id); |
b916bcfc NL |
396 | if (!$import) { |
397 | Tools::redirect(); | |
398 | } | |
eb1af592 NL |
399 | break; |
400 | default: | |
401 | break; | |
402 | } | |
403 | } | |
404 | ||
405 | function displayView($view, $id = 0) | |
406 | { | |
407 | $tpl_vars = array(); | |
408 | ||
409 | switch ($view) | |
410 | { | |
eb1af592 | 411 | case 'config': |
32520785 NL |
412 | $dev = $this->getPocheVersion('dev'); |
413 | $prod = $this->getPocheVersion('prod'); | |
031df528 NL |
414 | $compare_dev = version_compare(POCHE, $dev); |
415 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 416 | $themes = $this->getInstalledThemes(); |
5011388f | 417 | $languages = $this->getInstalledLanguages(); |
72c20a52 | 418 | $token = $this->user->getConfigValue('token'); |
df6afaf0 | 419 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']))?true:false; |
32520785 | 420 | $tpl_vars = array( |
00dbaf90 | 421 | 'themes' => $themes, |
5011388f | 422 | 'languages' => $languages, |
32520785 NL |
423 | 'dev' => $dev, |
424 | 'prod' => $prod, | |
425 | 'compare_dev' => $compare_dev, | |
426 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
427 | 'token' => $token, |
428 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 429 | 'http_auth' => $http_auth, |
32520785 | 430 | ); |
eb1af592 NL |
431 | Tools::logm('config view'); |
432 | break; | |
433 | case 'view': | |
8d3275be | 434 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
435 | if ($entry != NULL) { |
436 | Tools::logm('view link #' . $id); | |
437 | $content = $entry['content']; | |
438 | if (function_exists('tidy_parse_string')) { | |
439 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
440 | $tidy->cleanRepair(); | |
441 | $content = $tidy->value; | |
3408ed48 | 442 | } |
a3223127 | 443 | |
3408ed48 NL |
444 | # flattr checking |
445 | $flattr = new FlattrItem(); | |
7b171c73 NL |
446 | $flattr->checkItem($entry['url'], $entry['id']); |
447 | ||
448 | # tags | |
449 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 450 | |
3408ed48 | 451 | $tpl_vars = array( |
7b171c73 NL |
452 | 'entry' => $entry, |
453 | 'content' => $content, | |
454 | 'flattr' => $flattr, | |
455 | 'tags' => $tags | |
3408ed48 | 456 | ); |
eb1af592 NL |
457 | } |
458 | else { | |
d8d1542e | 459 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
460 | } |
461 | break; | |
12d9cfbc | 462 | default: # home, favorites and archive views |
8d3275be | 463 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); |
eb1af592 | 464 | $tpl_vars = array( |
3eb04903 N |
465 | 'entries' => '', |
466 | 'page_links' => '', | |
7f9f5281 | 467 | 'nb_results' => '', |
eb1af592 | 468 | ); |
34d67c83 | 469 | |
3eb04903 N |
470 | if (count($entries) > 0) { |
471 | $this->pagination->set_total(count($entries)); | |
472 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
473 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); | |
474 | $tpl_vars['entries'] = $datas; | |
475 | $tpl_vars['page_links'] = $page_links; | |
7f9f5281 | 476 | $tpl_vars['nb_results'] = count($entries); |
3eb04903 | 477 | } |
6a361945 | 478 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
479 | break; |
480 | } | |
481 | ||
482 | return $tpl_vars; | |
483 | } | |
c765c367 | 484 | |
07ee09f4 NL |
485 | /** |
486 | * update the password of the current user. | |
487 | * if MODE_DEMO is TRUE, the password can't be updated. | |
488 | * @todo add the return value | |
489 | * @todo set the new password in function header like this updatePassword($newPassword) | |
490 | * @return boolean | |
491 | */ | |
c765c367 NL |
492 | public function updatePassword() |
493 | { | |
55821e04 | 494 | if (MODE_DEMO) { |
8d3275be | 495 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 496 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 497 | Tools::redirect('?view=config'); |
55821e04 NL |
498 | } |
499 | else { | |
500 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
501 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
502 | $this->messages->add('s', _('your password has been updated')); |
503 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 504 | Session::logout(); |
8d3275be | 505 | Tools::logm('password updated'); |
c765c367 NL |
506 | Tools::redirect(); |
507 | } | |
508 | else { | |
8d3275be | 509 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 510 | Tools::redirect('?view=config'); |
c765c367 NL |
511 | } |
512 | } | |
513 | } | |
514 | } | |
00dbaf90 NL |
515 | |
516 | public function updateTheme() | |
517 | { | |
518 | # no data | |
519 | if (empty($_POST['theme'])) { | |
520 | } | |
521 | ||
522 | # we are not going to change it to the current theme... | |
523 | if ($_POST['theme'] == $this->getTheme()) { | |
524 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
525 | Tools::redirect('?view=config'); | |
526 | } | |
527 | ||
528 | $themes = $this->getInstalledThemes(); | |
529 | $actualTheme = false; | |
530 | ||
531 | foreach ($themes as $theme) { | |
532 | if ($theme['name'] == $_POST['theme']) { | |
533 | $actualTheme = true; | |
534 | break; | |
535 | } | |
536 | } | |
537 | ||
538 | if (! $actualTheme) { | |
539 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
540 | Tools::redirect('?view=config'); | |
541 | } | |
542 | ||
543 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); | |
544 | $this->messages->add('s', _('you have changed your theme preferences')); | |
545 | ||
546 | $currentConfig = $_SESSION['poche_user']->config; | |
547 | $currentConfig['theme'] = $_POST['theme']; | |
548 | ||
549 | $_SESSION['poche_user']->setConfig($currentConfig); | |
550 | ||
551 | Tools::redirect('?view=config'); | |
552 | } | |
c765c367 | 553 | |
5011388f NL |
554 | public function updateLanguage() |
555 | { | |
556 | # no data | |
557 | if (empty($_POST['language'])) { | |
558 | } | |
559 | ||
560 | # we are not going to change it to the current language... | |
561 | if ($_POST['language'] == $this->getLanguage()) { | |
562 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
563 | Tools::redirect('?view=config'); | |
564 | } | |
565 | ||
566 | $languages = $this->getInstalledLanguages(); | |
567 | $actualLanguage = false; | |
568 | ||
569 | foreach ($languages as $language) { | |
570 | if ($language['name'] == $_POST['language']) { | |
571 | $actualLanguage = true; | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | if (! $actualLanguage) { | |
577 | $this->messages->add('e', _('that language does not seem to be installed')); | |
578 | Tools::redirect('?view=config'); | |
579 | } | |
580 | ||
581 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); | |
582 | $this->messages->add('s', _('you have changed your language preferences')); | |
583 | ||
584 | $currentConfig = $_SESSION['poche_user']->config; | |
585 | $currentConfig['language'] = $_POST['language']; | |
586 | ||
587 | $_SESSION['poche_user']->setConfig($currentConfig); | |
588 | ||
589 | Tools::redirect('?view=config'); | |
590 | } | |
591 | ||
df6afaf0 DS |
592 | /** |
593 | * get credentials from differents sources | |
594 | * it redirects the user to the $referer link | |
595 | * @return array | |
596 | */ | |
597 | private function credentials() { | |
598 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
599 | return array($_SERVER['PHP_AUTH_USER'],'php_auth'); | |
600 | } | |
601 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
602 | return array($_POST['login'],$_POST['password']); | |
603 | } | |
604 | return array(false,false); | |
605 | } | |
606 | ||
07ee09f4 NL |
607 | /** |
608 | * checks if login & password are correct and save the user in session. | |
609 | * it redirects the user to the $referer link | |
610 | * @param string $referer the url to redirect after login | |
611 | * @todo add the return value | |
612 | * @return boolean | |
613 | */ | |
c765c367 NL |
614 | public function login($referer) |
615 | { | |
df6afaf0 DS |
616 | list($login,$password)=$this->credentials(); |
617 | if($login === false || $password === false) { | |
618 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
619 | Tools::logm('login failed'); | |
620 | Tools::redirect(); | |
621 | } | |
622 | if (!empty($login) && !empty($password)) { | |
623 | $user = $this->store->login($login, Tools::encodeString($password . $login)); | |
7ce7ec4c NL |
624 | if ($user != array()) { |
625 | # Save login into Session | |
df6afaf0 | 626 | Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), array('poche_user' => new User($user))); |
8d3275be | 627 | $this->messages->add('s', _('welcome to your poche')); |
8d3275be | 628 | Tools::logm('login successful'); |
c765c367 NL |
629 | Tools::redirect($referer); |
630 | } | |
8d3275be | 631 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
632 | Tools::logm('login failed'); |
633 | Tools::redirect(); | |
c765c367 NL |
634 | } |
635 | } | |
636 | ||
07ee09f4 NL |
637 | /** |
638 | * log out the poche user. It cleans the session. | |
639 | * @todo add the return value | |
640 | * @return boolean | |
641 | */ | |
c765c367 NL |
642 | public function logout() |
643 | { | |
7ce7ec4c | 644 | $this->user = array(); |
c765c367 | 645 | Session::logout(); |
b916bcfc NL |
646 | $this->messages->add('s', _('see you soon!')); |
647 | Tools::logm('logout'); | |
c765c367 NL |
648 | Tools::redirect(); |
649 | } | |
650 | ||
07ee09f4 NL |
651 | /** |
652 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
653 | * @todo add the return value | |
66b6a3b5 | 654 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
655 | * @return boolean |
656 | */ | |
66b6a3b5 | 657 | private function importFromInstapaper($targetFile) |
c765c367 | 658 | { |
7f959169 | 659 | # TODO gestion des articles favs |
a62788c6 | 660 | $html = new simple_html_dom(); |
66b6a3b5 | 661 | $html->load_file($targetFile); |
b916bcfc | 662 | Tools::logm('starting import from instapaper'); |
a62788c6 NL |
663 | |
664 | $read = 0; | |
665 | $errors = array(); | |
666 | foreach($html->find('ol') as $ul) | |
667 | { | |
668 | foreach($ul->find('li') as $li) | |
669 | { | |
670 | $a = $li->find('a'); | |
671 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 672 | $this->action('add', $url, 0, TRUE); |
a62788c6 | 673 | if ($read == '1') { |
b916bcfc NL |
674 | $sequence = ''; |
675 | if (STORAGE == 'postgres') { | |
676 | $sequence = 'entries_id_seq'; | |
677 | } | |
678 | $last_id = $this->store->getLastId($sequence); | |
679 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
a62788c6 NL |
680 | } |
681 | } | |
7f959169 NL |
682 | |
683 | # the second <ol> is for read links | |
a62788c6 NL |
684 | $read = 1; |
685 | } | |
8d3275be | 686 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
687 | Tools::logm('import from instapaper completed'); |
688 | Tools::redirect(); | |
689 | } | |
c765c367 | 690 | |
07ee09f4 NL |
691 | /** |
692 | * import from Pocket. poche needs a ./ril_export.html file | |
693 | * @todo add the return value | |
66b6a3b5 | 694 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
695 | * @return boolean |
696 | */ | |
66b6a3b5 | 697 | private function importFromPocket($targetFile) |
63c35580 | 698 | { |
7f959169 | 699 | # TODO gestion des articles favs |
63c35580 | 700 | $html = new simple_html_dom(); |
66b6a3b5 | 701 | $html->load_file($targetFile); |
b916bcfc | 702 | Tools::logm('starting import from pocket'); |
63c35580 NL |
703 | |
704 | $read = 0; | |
705 | $errors = array(); | |
706 | foreach($html->find('ul') as $ul) | |
707 | { | |
708 | foreach($ul->find('li') as $li) | |
c765c367 | 709 | { |
63c35580 NL |
710 | $a = $li->find('a'); |
711 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 712 | $this->action('add', $url, 0, TRUE); |
63c35580 | 713 | if ($read == '1') { |
b916bcfc NL |
714 | $sequence = ''; |
715 | if (STORAGE == 'postgres') { | |
716 | $sequence = 'entries_id_seq'; | |
717 | } | |
718 | $last_id = $this->store->getLastId($sequence); | |
719 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
c765c367 | 720 | } |
c765c367 | 721 | } |
7f959169 NL |
722 | |
723 | # the second <ul> is for read links | |
63c35580 | 724 | $read = 1; |
c765c367 | 725 | } |
8d3275be | 726 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
727 | Tools::logm('import from pocket completed'); |
728 | Tools::redirect(); | |
729 | } | |
c765c367 | 730 | |
07ee09f4 NL |
731 | /** |
732 | * import from Readability. poche needs a ./readability file | |
733 | * @todo add the return value | |
66b6a3b5 | 734 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
735 | * @return boolean |
736 | */ | |
66b6a3b5 | 737 | private function importFromReadability($targetFile) |
63c35580 | 738 | { |
7f959169 | 739 | # TODO gestion des articles lus / favs |
66b6a3b5 | 740 | $str_data = file_get_contents($targetFile); |
63c35580 | 741 | $data = json_decode($str_data,true); |
b916bcfc | 742 | Tools::logm('starting import from Readability'); |
c0d321c1 | 743 | $count = 0; |
63c35580 | 744 | foreach ($data as $key => $value) { |
c0d321c1 NL |
745 | $url = NULL; |
746 | $favorite = FALSE; | |
747 | $archive = FALSE; | |
7f959169 NL |
748 | foreach ($value as $attr => $attr_value) { |
749 | if ($attr == 'article__url') { | |
750 | $url = new Url(base64_encode($attr_value)); | |
c765c367 | 751 | } |
b916bcfc NL |
752 | $sequence = ''; |
753 | if (STORAGE == 'postgres') { | |
754 | $sequence = 'entries_id_seq'; | |
755 | } | |
c0d321c1 NL |
756 | if ($attr_value == 'true') { |
757 | if ($attr == 'favorite') { | |
758 | $favorite = TRUE; | |
759 | } | |
760 | if ($attr == 'archive') { | |
761 | $archive = TRUE; | |
762 | } | |
763 | } | |
764 | } | |
765 | # we can add the url | |
766 | if (!is_null($url) && $url->isCorrect()) { | |
767 | $this->action('add', $url, 0, TRUE); | |
768 | $count++; | |
769 | if ($favorite) { | |
770 | $last_id = $this->store->getLastId($sequence); | |
771 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
772 | } | |
773 | if ($archive) { | |
b916bcfc NL |
774 | $last_id = $this->store->getLastId($sequence); |
775 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
776 | } | |
c765c367 | 777 | } |
c765c367 | 778 | } |
c0d321c1 | 779 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); |
63c35580 NL |
780 | Tools::logm('import from Readability completed'); |
781 | Tools::redirect(); | |
c765c367 NL |
782 | } |
783 | ||
07ee09f4 NL |
784 | /** |
785 | * import datas into your poche | |
786 | * @param string $from name of the service to import : pocket, instapaper or readability | |
787 | * @todo add the return value | |
788 | * @return boolean | |
789 | */ | |
63c35580 | 790 | public function import($from) |
c765c367 | 791 | { |
66b6a3b5 E |
792 | $providers = array( |
793 | 'pocket' => 'importFromPocket', | |
794 | 'readability' => 'importFromReadability', | |
795 | 'instapaper' => 'importFromInstapaper' | |
796 | ); | |
797 | ||
798 | if (! isset($providers[$from])) { | |
799 | $this->messages->add('e', _('Unknown import provider.')); | |
800 | Tools::redirect(); | |
63c35580 | 801 | } |
66b6a3b5 E |
802 | |
803 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
804 | $targetFile = constant($targetDefinition); | |
805 | ||
806 | if (! defined($targetDefinition)) { | |
807 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
808 | Tools::redirect(); | |
63c35580 | 809 | } |
66b6a3b5 E |
810 | |
811 | if (! file_exists($targetFile)) { | |
812 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
813 | Tools::redirect(); | |
63c35580 | 814 | } |
66b6a3b5 E |
815 | |
816 | $this->$providers[$from]($targetFile); | |
63c35580 | 817 | } |
c765c367 | 818 | |
07ee09f4 NL |
819 | /** |
820 | * export poche entries in json | |
821 | * @return json all poche entries | |
822 | */ | |
63c35580 NL |
823 | public function export() |
824 | { | |
8d3275be | 825 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
826 | echo $this->tpl->render('export.twig', array( |
827 | 'export' => Tools::renderJson($entries), | |
828 | )); | |
829 | Tools::logm('export view'); | |
c765c367 | 830 | } |
32520785 | 831 | |
07ee09f4 | 832 | /** |
a3436d4c | 833 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
834 | * @param string $which 'prod' or 'dev' |
835 | * @return string latest $which version | |
836 | */ | |
32520785 NL |
837 | private function getPocheVersion($which = 'prod') |
838 | { | |
839 | $cache_file = CACHE . '/' . $which; | |
a3436d4c NL |
840 | |
841 | # checks if the cached version file exists | |
32520785 NL |
842 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { |
843 | $version = file_get_contents($cache_file); | |
844 | } else { | |
bc1ee852 | 845 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); |
32520785 NL |
846 | file_put_contents($cache_file, $version, LOCK_EX); |
847 | } | |
848 | return $version; | |
849 | } | |
72c20a52 NL |
850 | |
851 | public function generateToken() | |
852 | { | |
853 | if (ini_get('open_basedir') === '') { | |
854 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
855 | } | |
856 | else { | |
857 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
858 | } | |
859 | ||
860 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | |
861 | $currentConfig = $_SESSION['poche_user']->config; | |
862 | $currentConfig['token'] = $token; | |
863 | $_SESSION['poche_user']->setConfig($currentConfig); | |
864 | } | |
865 | ||
866 | public function generateFeeds($token, $user_id, $type = 'home') | |
867 | { | |
59cc5852 | 868 | $allowed_types = array('home', 'fav', 'archive'); |
72c20a52 NL |
869 | $config = $this->store->getConfigUser($user_id); |
870 | ||
871 | if (!in_array($type, $allowed_types) || | |
872 | $token != $config['token']) { | |
873 | die(_('Uh, there is a problem while generating feeds.')); | |
874 | } | |
875 | // Check the token | |
876 | ||
9e7c840b | 877 | $feed = new FeedWriter(RSS2); |
72c20a52 NL |
878 | $feed->setTitle('poche - ' . $type . ' feed'); |
879 | $feed->setLink(Tools::getPocheUrl()); | |
9e7c840b | 880 | $feed->setChannelElement('updated', date(DATE_RSS , time())); |
72c20a52 NL |
881 | $feed->setChannelElement('author', 'poche'); |
882 | ||
883 | $entries = $this->store->getEntriesByView($type, $user_id); | |
884 | if (count($entries) > 0) { | |
885 | foreach ($entries as $entry) { | |
886 | $newItem = $feed->createNewItem(); | |
887 | $newItem->setTitle(htmlentities($entry['title'])); | |
888 | $newItem->setLink(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); | |
889 | $newItem->setDate(time()); | |
890 | $newItem->setDescription($entry['content']); | |
891 | $feed->addItem($newItem); | |
892 | } | |
893 | } | |
894 | ||
895 | $feed->genarateFeed(); | |
896 | exit; | |
897 | } | |
df6afaf0 | 898 | } |