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