]>
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 | } | |
5cfafc61 | 52 | $this->store->checkTags(); |
eb1af592 | 53 | } |
eb1af592 | 54 | } |
00dbaf90 NL |
55 | |
56 | private function init() | |
57 | { | |
58 | Tools::initPhp(); | |
59 | Session::$sessionName = 'poche'; | |
60 | Session::init(); | |
eb1af592 | 61 | |
00dbaf90 NL |
62 | if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) { |
63 | $this->user = $_SESSION['poche_user']; | |
64 | } else { | |
65 | # fake user, just for install & login screens | |
66 | $this->user = new User(); | |
67 | $this->user->setConfig($this->getDefaultConfig()); | |
68 | } | |
69 | ||
70 | # l10n | |
71 | $language = $this->user->getConfigValue('language'); | |
72 | putenv('LC_ALL=' . $language); | |
73 | setlocale(LC_ALL, $language); | |
74 | bindtextdomain($language, LOCALE); | |
75 | textdomain($language); | |
76 | ||
77 | # Pagination | |
78 | $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); | |
79 | ||
80 | # Set up theme | |
81 | $themeDirectory = $this->user->getConfigValue('theme'); | |
82 | ||
83 | if ($themeDirectory === false) { | |
84 | $themeDirectory = DEFAULT_THEME; | |
85 | } | |
86 | ||
87 | $this->currentTheme = $themeDirectory; | |
5011388f NL |
88 | |
89 | # Set up language | |
90 | $languageDirectory = $this->user->getConfigValue('language'); | |
91 | ||
92 | if ($languageDirectory === false) { | |
93 | $languageDirectory = DEFAULT_THEME; | |
94 | } | |
95 | ||
96 | $this->currentLanguage = $languageDirectory; | |
00dbaf90 NL |
97 | } |
98 | ||
99 | public function configFileIsAvailable() { | |
100 | if (! self::$configFileAvailable) { | |
9d3b88b3 | 101 | $this->notInstalledMessage[] = 'You have to rename inc/poche/config.inc.php.new to inc/poche/config.inc.php.'; |
00dbaf90 NL |
102 | |
103 | return false; | |
104 | } | |
105 | ||
106 | return true; | |
107 | } | |
108 | ||
109 | public function themeIsInstalled() { | |
9d3b88b3 | 110 | $passTheme = TRUE; |
00dbaf90 NL |
111 | # Twig is an absolute requirement for Poche to function. Abort immediately if the Composer installer hasn't been run yet |
112 | if (! self::$canRenderTemplates) { | |
9d3b88b3 NL |
113 | $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>'; |
114 | $passTheme = FALSE; | |
00dbaf90 | 115 | } |
7f17a38d NL |
116 | |
117 | if (! is_writable(CACHE)) { | |
9d3b88b3 | 118 | $this->notInstalledMessage[] = 'You don\'t have write access on cache directory.'; |
7f17a38d NL |
119 | |
120 | self::$canRenderTemplates = false; | |
121 | ||
9d3b88b3 | 122 | $passTheme = FALSE; |
7f17a38d | 123 | } |
00dbaf90 NL |
124 | |
125 | # Check if the selected theme and its requirements are present | |
9d3b88b3 NL |
126 | if ($this->getTheme() != '' && ! is_dir(THEME . '/' . $this->getTheme())) { |
127 | $this->notInstalledMessage[] = 'The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (Missing directory: ' . THEME . '/' . $this->getTheme() . ')'; | |
00dbaf90 NL |
128 | |
129 | self::$canRenderTemplates = false; | |
130 | ||
9d3b88b3 | 131 | $passTheme = FALSE; |
00dbaf90 NL |
132 | } |
133 | ||
134 | foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) { | |
135 | if (! is_dir(THEME . '/' . $requiredTheme)) { | |
9d3b88b3 | 136 | $this->notInstalledMessage[] = 'The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')'; |
00dbaf90 NL |
137 | |
138 | self::$canRenderTemplates = false; | |
139 | ||
9d3b88b3 | 140 | $passTheme = FALSE; |
00dbaf90 NL |
141 | } |
142 | } | |
9d3b88b3 NL |
143 | |
144 | if (!$passTheme) { | |
145 | return FALSE; | |
146 | } | |
147 | ||
00dbaf90 NL |
148 | |
149 | return true; | |
150 | } | |
151 | ||
4a291288 NL |
152 | /** |
153 | * all checks before installation. | |
00dbaf90 | 154 | * @todo move HTML to template |
4a291288 NL |
155 | * @return boolean |
156 | */ | |
00dbaf90 | 157 | public function systemIsInstalled() |
eb1af592 | 158 | { |
9d3b88b3 | 159 | $msg = TRUE; |
00dbaf90 NL |
160 | |
161 | $configSalt = defined('SALT') ? constant('SALT') : ''; | |
162 | ||
163 | if (empty($configSalt)) { | |
9d3b88b3 NL |
164 | $this->notInstalledMessage[] = 'You have not yet filled in the SALT value in the config.inc.php file.'; |
165 | $msg = FALSE; | |
166 | } | |
167 | if (STORAGE == 'sqlite' && ! file_exists(STORAGE_SQLITE)) { | |
00dbaf90 | 168 | Tools::logm('sqlite file doesn\'t exist'); |
9d3b88b3 NL |
169 | $this->notInstalledMessage[] = 'sqlite file doesn\'t exist, you can find it in install folder. Copy it in /db folder.'; |
170 | $msg = FALSE; | |
171 | } | |
172 | if (is_dir(ROOT . '/install') && ! DEBUG_POCHE) { | |
173 | $this->notInstalledMessage[] = 'you have to delete the /install folder before using poche.'; | |
174 | $msg = FALSE; | |
175 | } | |
176 | if (STORAGE == 'sqlite' && ! is_writable(STORAGE_SQLITE)) { | |
bb5a7d9e | 177 | Tools::logm('you don\'t have write access on sqlite file'); |
9d3b88b3 NL |
178 | $this->notInstalledMessage[] = 'You don\'t have write access on sqlite file.'; |
179 | $msg = FALSE; | |
bb5a7d9e | 180 | } |
00dbaf90 | 181 | |
9d3b88b3 | 182 | if (! $msg) { |
00dbaf90 | 183 | return false; |
8d3275be | 184 | } |
7ce7ec4c | 185 | |
00dbaf90 NL |
186 | return true; |
187 | } | |
188 | ||
189 | public function getNotInstalledMessage() { | |
190 | return $this->notInstalledMessage; | |
4a291288 | 191 | } |
eb1af592 | 192 | |
4a291288 NL |
193 | private function initTpl() |
194 | { | |
00dbaf90 NL |
195 | $loaderChain = new Twig_Loader_Chain(); |
196 | ||
197 | # add the current theme as first to the loader chain so Twig will look there first for overridden template files | |
198 | try { | |
199 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $this->getTheme())); | |
200 | } catch (Twig_Error_Loader $e) { | |
201 | # @todo isInstalled() should catch this, inject Twig later | |
202 | die('The currently selected theme (' . $this->getTheme() . ') does not seem to be properly installed (' . THEME . '/' . $this->getTheme() .' is missing)'); | |
203 | } | |
204 | ||
205 | # add all required themes to the loader chain | |
206 | foreach ($this->installedThemes[$this->getTheme()]['requires'] as $requiredTheme) { | |
207 | try { | |
208 | $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . DEFAULT_THEME)); | |
209 | } catch (Twig_Error_Loader $e) { | |
210 | # @todo isInstalled() should catch this, inject Twig later | |
211 | die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $this->getTheme() . ')'); | |
212 | } | |
213 | } | |
214 | ||
bc1ee852 NL |
215 | if (DEBUG_POCHE) { |
216 | $twig_params = array(); | |
00dbaf90 | 217 | } else { |
bc1ee852 NL |
218 | $twig_params = array('cache' => CACHE); |
219 | } | |
00dbaf90 NL |
220 | |
221 | $this->tpl = new Twig_Environment($loaderChain, $twig_params); | |
eb1af592 | 222 | $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); |
00dbaf90 | 223 | |
55821e04 NL |
224 | # filter to display domain name of an url |
225 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | |
226 | $this->tpl->addFilter($filter); | |
eb1af592 | 227 | |
d9178758 NL |
228 | # filter for reading time |
229 | $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime'); | |
230 | $this->tpl->addFilter($filter); | |
00dbaf90 NL |
231 | |
232 | # filter for simple filenames in config view | |
233 | $filter = new Twig_SimpleFilter('getPrettyFilename', function($string) { return str_replace(ROOT, '', $string); }); | |
234 | $this->tpl->addFilter($filter); | |
eb1af592 NL |
235 | } |
236 | ||
237 | private function install() | |
238 | { | |
239 | Tools::logm('poche still not installed'); | |
240 | echo $this->tpl->render('install.twig', array( | |
00dbaf90 NL |
241 | 'token' => Session::getToken(), |
242 | 'theme' => $this->getTheme(), | |
243 | 'poche_url' => Tools::getPocheUrl() | |
eb1af592 NL |
244 | )); |
245 | if (isset($_GET['install'])) { | |
246 | if (($_POST['password'] == $_POST['password_repeat']) | |
247 | && $_POST['password'] != "" && $_POST['login'] != "") { | |
248 | # let's rock, install poche baby ! | |
bb5a7d9e NL |
249 | if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) |
250 | { | |
251 | Session::logout(); | |
252 | Tools::logm('poche is now installed'); | |
253 | Tools::redirect(); | |
254 | } | |
6a361945 NL |
255 | } |
256 | else { | |
257 | Tools::logm('error during installation'); | |
eb1af592 NL |
258 | Tools::redirect(); |
259 | } | |
260 | } | |
261 | exit(); | |
262 | } | |
00dbaf90 NL |
263 | |
264 | public function getTheme() { | |
265 | return $this->currentTheme; | |
266 | } | |
5011388f NL |
267 | |
268 | public function getLanguage() { | |
269 | return $this->currentLanguage; | |
270 | } | |
00dbaf90 NL |
271 | |
272 | public function getInstalledThemes() { | |
273 | $handle = opendir(THEME); | |
274 | $themes = array(); | |
275 | ||
276 | while (($theme = readdir($handle)) !== false) { | |
277 | # Themes are stored in a directory, so all directory names are themes | |
278 | # @todo move theme installation data to database | |
89812ec8 | 279 | if (! is_dir(THEME . '/' . $theme) || in_array($theme, array('..', '.'))) { |
00dbaf90 NL |
280 | continue; |
281 | } | |
282 | ||
283 | $current = false; | |
284 | ||
285 | if ($theme === $this->getTheme()) { | |
286 | $current = true; | |
287 | } | |
288 | ||
289 | $themes[] = array('name' => $theme, 'current' => $current); | |
290 | } | |
291 | ||
2287bf06 | 292 | sort($themes); |
00dbaf90 NL |
293 | return $themes; |
294 | } | |
eb1af592 | 295 | |
5011388f NL |
296 | public function getInstalledLanguages() { |
297 | $handle = opendir(LOCALE); | |
298 | $languages = array(); | |
299 | ||
300 | while (($language = readdir($handle)) !== false) { | |
301 | # Languages are stored in a directory, so all directory names are languages | |
302 | # @todo move language installation data to database | |
303 | if (! is_dir(LOCALE . '/' . $language) || in_array($language, array('..', '.'))) { | |
304 | continue; | |
305 | } | |
306 | ||
307 | $current = false; | |
308 | ||
309 | if ($language === $this->getLanguage()) { | |
310 | $current = true; | |
311 | } | |
312 | ||
313 | $languages[] = array('name' => $language, 'current' => $current); | |
314 | } | |
315 | ||
316 | return $languages; | |
317 | } | |
318 | ||
8d3275be | 319 | public function getDefaultConfig() |
00dbaf90 | 320 | { |
8d3275be NL |
321 | return array( |
322 | 'pager' => PAGINATION, | |
323 | 'language' => LANG, | |
00dbaf90 NL |
324 | 'theme' => DEFAULT_THEME |
325 | ); | |
8d3275be NL |
326 | } |
327 | ||
eb1af592 NL |
328 | /** |
329 | * Call action (mark as fav, archive, delete, etc.) | |
330 | */ | |
363bc4eb | 331 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE) |
eb1af592 NL |
332 | { |
333 | switch ($action) | |
334 | { | |
335 | case 'add': | |
e1cf0fda NL |
336 | $options = array('http' => array('user_agent' => 'poche')); |
337 | $context = stream_context_create($options); | |
338 | $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed', false, $context); | |
42c80841 NL |
339 | $content = json_decode($json, true); |
340 | $title = $content['rss']['channel']['item']['title']; | |
341 | $body = $content['rss']['channel']['item']['description']; | |
ec397236 | 342 | |
42c80841 | 343 | if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { |
ec397236 NL |
344 | Tools::logm('add link ' . $url->getUrl()); |
345 | $sequence = ''; | |
346 | if (STORAGE == 'postgres') { | |
347 | $sequence = 'entries_id_seq'; | |
eb1af592 | 348 | } |
ec397236 NL |
349 | $last_id = $this->store->getLastId($sequence); |
350 | if (DOWNLOAD_PICTURES) { | |
42c80841 | 351 | $content = filtre_picture($body, $url->getUrl(), $last_id); |
ec397236 NL |
352 | Tools::logm('updating content article'); |
353 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
354 | } | |
355 | if (!$import) { | |
356 | $this->messages->add('s', _('the link has been added successfully')); | |
eb1af592 NL |
357 | } |
358 | } | |
359 | else { | |
b916bcfc | 360 | if (!$import) { |
ec397236 NL |
361 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); |
362 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
b916bcfc NL |
363 | } |
364 | } | |
ec397236 | 365 | |
b916bcfc | 366 | if (!$import) { |
363bc4eb | 367 | if ($autoclose == TRUE) { |
368 | Tools::redirect('?view=home'); | |
369 | } else { | |
f616ab60 | 370 | Tools::redirect('?view=home&closewin=true'); |
363bc4eb | 371 | } |
eb1af592 NL |
372 | } |
373 | break; | |
374 | case 'delete': | |
bc1ee852 | 375 | $msg = 'delete link #' . $id; |
8d3275be | 376 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 NL |
377 | if (DOWNLOAD_PICTURES) { |
378 | remove_directory(ABS_PATH . $id); | |
379 | } | |
6a361945 | 380 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
381 | } |
382 | else { | |
6a361945 | 383 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 384 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 385 | } |
bc1ee852 | 386 | Tools::logm($msg); |
985ce3ec | 387 | Tools::redirect('?'); |
eb1af592 NL |
388 | break; |
389 | case 'toggle_fav' : | |
8d3275be | 390 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 391 | Tools::logm('mark as favorite link #' . $id); |
b916bcfc NL |
392 | if (!$import) { |
393 | Tools::redirect(); | |
394 | } | |
eb1af592 NL |
395 | break; |
396 | case 'toggle_archive' : | |
8d3275be | 397 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 398 | Tools::logm('archive link #' . $id); |
b916bcfc NL |
399 | if (!$import) { |
400 | Tools::redirect(); | |
401 | } | |
eb1af592 | 402 | break; |
c432fa16 NL |
403 | case 'add_tag' : |
404 | $tags = explode(',', $_POST['value']); | |
405 | $entry_id = $_POST['entry_id']; | |
406 | foreach($tags as $key => $tag_value) { | |
407 | $value = trim($tag_value); | |
408 | $tag = $this->store->retrieveTagByValue($value); | |
409 | ||
410 | if (is_null($tag)) { | |
411 | # we create the tag | |
412 | $tag = $this->store->createTag($value); | |
413 | $sequence = ''; | |
414 | if (STORAGE == 'postgres') { | |
415 | $sequence = 'tags_id_seq'; | |
416 | } | |
417 | $tag_id = $this->store->getLastId($sequence); | |
418 | } | |
419 | else { | |
420 | $tag_id = $tag['id']; | |
421 | } | |
422 | ||
423 | # we assign the tag to the article | |
424 | $this->store->setTagToEntry($tag_id, $entry_id); | |
425 | } | |
426 | Tools::redirect(); | |
427 | break; | |
428 | case 'remove_tag' : | |
429 | $tag_id = $_GET['tag_id']; | |
430 | $this->store->removeTagForEntry($id, $tag_id); | |
431 | Tools::redirect(); | |
432 | break; | |
eb1af592 NL |
433 | default: |
434 | break; | |
435 | } | |
436 | } | |
437 | ||
438 | function displayView($view, $id = 0) | |
439 | { | |
440 | $tpl_vars = array(); | |
441 | ||
442 | switch ($view) | |
443 | { | |
eb1af592 | 444 | case 'config': |
32520785 NL |
445 | $dev = $this->getPocheVersion('dev'); |
446 | $prod = $this->getPocheVersion('prod'); | |
031df528 NL |
447 | $compare_dev = version_compare(POCHE, $dev); |
448 | $compare_prod = version_compare(POCHE, $prod); | |
00dbaf90 | 449 | $themes = $this->getInstalledThemes(); |
5011388f | 450 | $languages = $this->getInstalledLanguages(); |
72c20a52 | 451 | $token = $this->user->getConfigValue('token'); |
1810c13b | 452 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; |
32520785 | 453 | $tpl_vars = array( |
00dbaf90 | 454 | 'themes' => $themes, |
5011388f | 455 | 'languages' => $languages, |
32520785 NL |
456 | 'dev' => $dev, |
457 | 'prod' => $prod, | |
458 | 'compare_dev' => $compare_dev, | |
459 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
460 | 'token' => $token, |
461 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 462 | 'http_auth' => $http_auth, |
32520785 | 463 | ); |
eb1af592 NL |
464 | Tools::logm('config view'); |
465 | break; | |
6cab59c3 NL |
466 | case 'edit-tags': |
467 | # tags | |
468 | $tags = $this->store->retrieveTagsByEntry($id); | |
469 | $tpl_vars = array( | |
c432fa16 | 470 | 'entry_id' => $id, |
6cab59c3 NL |
471 | 'tags' => $tags, |
472 | ); | |
473 | break; | |
4886ed6d NL |
474 | case 'tag': |
475 | $entries = $this->store->retrieveEntriesByTag($id); | |
476 | $tag = $this->store->retrieveTag($id); | |
477 | $tpl_vars = array( | |
478 | 'tag' => $tag, | |
479 | 'entries' => $entries, | |
480 | ); | |
481 | break; | |
2e2ebe5e | 482 | case 'tags': |
f778e472 | 483 | $token = $this->user->getConfigValue('token'); |
2e2ebe5e NL |
484 | $tags = $this->store->retrieveAllTags(); |
485 | $tpl_vars = array( | |
f778e472 NL |
486 | 'token' => $token, |
487 | 'user_id' => $this->user->getId(), | |
2e2ebe5e NL |
488 | 'tags' => $tags, |
489 | ); | |
490 | break; | |
eb1af592 | 491 | case 'view': |
8d3275be | 492 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
493 | if ($entry != NULL) { |
494 | Tools::logm('view link #' . $id); | |
495 | $content = $entry['content']; | |
496 | if (function_exists('tidy_parse_string')) { | |
497 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
498 | $tidy->cleanRepair(); | |
499 | $content = $tidy->value; | |
3408ed48 | 500 | } |
a3223127 | 501 | |
3408ed48 NL |
502 | # flattr checking |
503 | $flattr = new FlattrItem(); | |
7b171c73 NL |
504 | $flattr->checkItem($entry['url'], $entry['id']); |
505 | ||
506 | # tags | |
507 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 508 | |
3408ed48 | 509 | $tpl_vars = array( |
7b171c73 NL |
510 | 'entry' => $entry, |
511 | 'content' => $content, | |
512 | 'flattr' => $flattr, | |
513 | 'tags' => $tags | |
3408ed48 | 514 | ); |
eb1af592 NL |
515 | } |
516 | else { | |
d8d1542e | 517 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
518 | } |
519 | break; | |
12d9cfbc | 520 | default: # home, favorites and archive views |
8d3275be | 521 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); |
eb1af592 | 522 | $tpl_vars = array( |
3eb04903 N |
523 | 'entries' => '', |
524 | 'page_links' => '', | |
7f9f5281 | 525 | 'nb_results' => '', |
eb1af592 | 526 | ); |
34d67c83 | 527 | |
3eb04903 N |
528 | if (count($entries) > 0) { |
529 | $this->pagination->set_total(count($entries)); | |
530 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
531 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); | |
532 | $tpl_vars['entries'] = $datas; | |
533 | $tpl_vars['page_links'] = $page_links; | |
7f9f5281 | 534 | $tpl_vars['nb_results'] = count($entries); |
3eb04903 | 535 | } |
6a361945 | 536 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
537 | break; |
538 | } | |
539 | ||
540 | return $tpl_vars; | |
541 | } | |
c765c367 | 542 | |
07ee09f4 NL |
543 | /** |
544 | * update the password of the current user. | |
545 | * if MODE_DEMO is TRUE, the password can't be updated. | |
546 | * @todo add the return value | |
547 | * @todo set the new password in function header like this updatePassword($newPassword) | |
548 | * @return boolean | |
549 | */ | |
c765c367 NL |
550 | public function updatePassword() |
551 | { | |
55821e04 | 552 | if (MODE_DEMO) { |
8d3275be | 553 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 554 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 555 | Tools::redirect('?view=config'); |
55821e04 NL |
556 | } |
557 | else { | |
558 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
559 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
8d3275be NL |
560 | $this->messages->add('s', _('your password has been updated')); |
561 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
c765c367 | 562 | Session::logout(); |
8d3275be | 563 | Tools::logm('password updated'); |
c765c367 NL |
564 | Tools::redirect(); |
565 | } | |
566 | else { | |
8d3275be | 567 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 568 | Tools::redirect('?view=config'); |
c765c367 NL |
569 | } |
570 | } | |
571 | } | |
572 | } | |
00dbaf90 NL |
573 | |
574 | public function updateTheme() | |
575 | { | |
576 | # no data | |
577 | if (empty($_POST['theme'])) { | |
578 | } | |
579 | ||
580 | # we are not going to change it to the current theme... | |
581 | if ($_POST['theme'] == $this->getTheme()) { | |
582 | $this->messages->add('w', _('still using the "' . $this->getTheme() . '" theme!')); | |
583 | Tools::redirect('?view=config'); | |
584 | } | |
585 | ||
586 | $themes = $this->getInstalledThemes(); | |
587 | $actualTheme = false; | |
588 | ||
589 | foreach ($themes as $theme) { | |
590 | if ($theme['name'] == $_POST['theme']) { | |
591 | $actualTheme = true; | |
592 | break; | |
593 | } | |
594 | } | |
595 | ||
596 | if (! $actualTheme) { | |
597 | $this->messages->add('e', _('that theme does not seem to be installed')); | |
598 | Tools::redirect('?view=config'); | |
599 | } | |
600 | ||
601 | $this->store->updateUserConfig($this->user->getId(), 'theme', $_POST['theme']); | |
602 | $this->messages->add('s', _('you have changed your theme preferences')); | |
603 | ||
604 | $currentConfig = $_SESSION['poche_user']->config; | |
605 | $currentConfig['theme'] = $_POST['theme']; | |
606 | ||
607 | $_SESSION['poche_user']->setConfig($currentConfig); | |
608 | ||
609 | Tools::redirect('?view=config'); | |
610 | } | |
c765c367 | 611 | |
5011388f NL |
612 | public function updateLanguage() |
613 | { | |
614 | # no data | |
615 | if (empty($_POST['language'])) { | |
616 | } | |
617 | ||
618 | # we are not going to change it to the current language... | |
619 | if ($_POST['language'] == $this->getLanguage()) { | |
620 | $this->messages->add('w', _('still using the "' . $this->getLanguage() . '" language!')); | |
621 | Tools::redirect('?view=config'); | |
622 | } | |
623 | ||
624 | $languages = $this->getInstalledLanguages(); | |
625 | $actualLanguage = false; | |
626 | ||
627 | foreach ($languages as $language) { | |
628 | if ($language['name'] == $_POST['language']) { | |
629 | $actualLanguage = true; | |
630 | break; | |
631 | } | |
632 | } | |
633 | ||
634 | if (! $actualLanguage) { | |
635 | $this->messages->add('e', _('that language does not seem to be installed')); | |
636 | Tools::redirect('?view=config'); | |
637 | } | |
638 | ||
639 | $this->store->updateUserConfig($this->user->getId(), 'language', $_POST['language']); | |
640 | $this->messages->add('s', _('you have changed your language preferences')); | |
641 | ||
642 | $currentConfig = $_SESSION['poche_user']->config; | |
643 | $currentConfig['language'] = $_POST['language']; | |
644 | ||
645 | $_SESSION['poche_user']->setConfig($currentConfig); | |
646 | ||
647 | Tools::redirect('?view=config'); | |
648 | } | |
649 | ||
df6afaf0 DS |
650 | /** |
651 | * get credentials from differents sources | |
652 | * it redirects the user to the $referer link | |
653 | * @return array | |
654 | */ | |
1810c13b NL |
655 | private function credentials() { |
656 | if(isset($_SERVER['PHP_AUTH_USER'])) { | |
657 | return array($_SERVER['PHP_AUTH_USER'],'php_auth'); | |
658 | } | |
659 | if(!empty($_POST['login']) && !empty($_POST['password'])) { | |
660 | return array($_POST['login'],$_POST['password']); | |
661 | } | |
662 | if(isset($_SERVER['REMOTE_USER'])) { | |
663 | return array($_SERVER['REMOTE_USER'],'http_auth'); | |
664 | } | |
5cfafc61 | 665 | |
1810c13b | 666 | return array(false,false); |
df6afaf0 DS |
667 | } |
668 | ||
07ee09f4 NL |
669 | /** |
670 | * checks if login & password are correct and save the user in session. | |
671 | * it redirects the user to the $referer link | |
672 | * @param string $referer the url to redirect after login | |
673 | * @todo add the return value | |
674 | * @return boolean | |
675 | */ | |
c765c367 NL |
676 | public function login($referer) |
677 | { | |
df6afaf0 DS |
678 | list($login,$password)=$this->credentials(); |
679 | if($login === false || $password === false) { | |
680 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
681 | Tools::logm('login failed'); | |
682 | Tools::redirect(); | |
683 | } | |
684 | if (!empty($login) && !empty($password)) { | |
685 | $user = $this->store->login($login, Tools::encodeString($password . $login)); | |
7ce7ec4c NL |
686 | if ($user != array()) { |
687 | # Save login into Session | |
a0aa1504 DS |
688 | $longlastingsession = isset($_POST['longlastingsession']); |
689 | Session::login($user['username'], $user['password'], $login, Tools::encodeString($password . $login), $longlastingsession, array('poche_user' => new User($user))); | |
8d3275be | 690 | $this->messages->add('s', _('welcome to your poche')); |
8d3275be | 691 | Tools::logm('login successful'); |
c765c367 NL |
692 | Tools::redirect($referer); |
693 | } | |
8d3275be | 694 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
695 | Tools::logm('login failed'); |
696 | Tools::redirect(); | |
c765c367 NL |
697 | } |
698 | } | |
699 | ||
07ee09f4 NL |
700 | /** |
701 | * log out the poche user. It cleans the session. | |
702 | * @todo add the return value | |
703 | * @return boolean | |
704 | */ | |
c765c367 NL |
705 | public function logout() |
706 | { | |
7ce7ec4c | 707 | $this->user = array(); |
c765c367 | 708 | Session::logout(); |
b916bcfc NL |
709 | $this->messages->add('s', _('see you soon!')); |
710 | Tools::logm('logout'); | |
c765c367 NL |
711 | Tools::redirect(); |
712 | } | |
713 | ||
07ee09f4 NL |
714 | /** |
715 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
716 | * @todo add the return value | |
66b6a3b5 | 717 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
718 | * @return boolean |
719 | */ | |
66b6a3b5 | 720 | private function importFromInstapaper($targetFile) |
c765c367 | 721 | { |
7f959169 | 722 | # TODO gestion des articles favs |
a62788c6 | 723 | $html = new simple_html_dom(); |
66b6a3b5 | 724 | $html->load_file($targetFile); |
b916bcfc | 725 | Tools::logm('starting import from instapaper'); |
a62788c6 NL |
726 | |
727 | $read = 0; | |
728 | $errors = array(); | |
729 | foreach($html->find('ol') as $ul) | |
730 | { | |
731 | foreach($ul->find('li') as $li) | |
732 | { | |
733 | $a = $li->find('a'); | |
734 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 735 | $this->action('add', $url, 0, TRUE); |
a62788c6 | 736 | if ($read == '1') { |
b916bcfc NL |
737 | $sequence = ''; |
738 | if (STORAGE == 'postgres') { | |
739 | $sequence = 'entries_id_seq'; | |
740 | } | |
741 | $last_id = $this->store->getLastId($sequence); | |
742 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
a62788c6 NL |
743 | } |
744 | } | |
7f959169 NL |
745 | |
746 | # the second <ol> is for read links | |
a62788c6 NL |
747 | $read = 1; |
748 | } | |
8d3275be | 749 | $this->messages->add('s', _('import from instapaper completed')); |
63c35580 NL |
750 | Tools::logm('import from instapaper completed'); |
751 | Tools::redirect(); | |
752 | } | |
c765c367 | 753 | |
07ee09f4 NL |
754 | /** |
755 | * import from Pocket. poche needs a ./ril_export.html file | |
756 | * @todo add the return value | |
66b6a3b5 | 757 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
758 | * @return boolean |
759 | */ | |
66b6a3b5 | 760 | private function importFromPocket($targetFile) |
63c35580 | 761 | { |
7f959169 | 762 | # TODO gestion des articles favs |
63c35580 | 763 | $html = new simple_html_dom(); |
66b6a3b5 | 764 | $html->load_file($targetFile); |
b916bcfc | 765 | Tools::logm('starting import from pocket'); |
63c35580 NL |
766 | |
767 | $read = 0; | |
768 | $errors = array(); | |
769 | foreach($html->find('ul') as $ul) | |
770 | { | |
771 | foreach($ul->find('li') as $li) | |
c765c367 | 772 | { |
63c35580 NL |
773 | $a = $li->find('a'); |
774 | $url = new Url(base64_encode($a[0]->href)); | |
b916bcfc | 775 | $this->action('add', $url, 0, TRUE); |
63c35580 | 776 | if ($read == '1') { |
b916bcfc NL |
777 | $sequence = ''; |
778 | if (STORAGE == 'postgres') { | |
779 | $sequence = 'entries_id_seq'; | |
780 | } | |
781 | $last_id = $this->store->getLastId($sequence); | |
782 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
c765c367 | 783 | } |
c765c367 | 784 | } |
7f959169 NL |
785 | |
786 | # the second <ul> is for read links | |
63c35580 | 787 | $read = 1; |
c765c367 | 788 | } |
8d3275be | 789 | $this->messages->add('s', _('import from pocket completed')); |
63c35580 NL |
790 | Tools::logm('import from pocket completed'); |
791 | Tools::redirect(); | |
792 | } | |
c765c367 | 793 | |
07ee09f4 NL |
794 | /** |
795 | * import from Readability. poche needs a ./readability file | |
796 | * @todo add the return value | |
66b6a3b5 | 797 | * @param string $targetFile the file used for importing |
07ee09f4 NL |
798 | * @return boolean |
799 | */ | |
66b6a3b5 | 800 | private function importFromReadability($targetFile) |
63c35580 | 801 | { |
7f959169 | 802 | # TODO gestion des articles lus / favs |
66b6a3b5 | 803 | $str_data = file_get_contents($targetFile); |
63c35580 | 804 | $data = json_decode($str_data,true); |
b916bcfc | 805 | Tools::logm('starting import from Readability'); |
c0d321c1 | 806 | $count = 0; |
63c35580 | 807 | foreach ($data as $key => $value) { |
c0d321c1 NL |
808 | $url = NULL; |
809 | $favorite = FALSE; | |
810 | $archive = FALSE; | |
9bc32632 NL |
811 | foreach ($value as $item) { |
812 | foreach ($item as $attr => $value) { | |
813 | if ($attr == 'article__url') { | |
814 | $url = new Url(base64_encode($value)); | |
c0d321c1 | 815 | } |
9bc32632 NL |
816 | $sequence = ''; |
817 | if (STORAGE == 'postgres') { | |
818 | $sequence = 'entries_id_seq'; | |
819 | } | |
820 | if ($value == 'true') { | |
821 | if ($attr == 'favorite') { | |
822 | $favorite = TRUE; | |
823 | } | |
824 | if ($attr == 'archive') { | |
825 | $archive = TRUE; | |
826 | } | |
c0d321c1 NL |
827 | } |
828 | } | |
9bc32632 NL |
829 | |
830 | # we can add the url | |
831 | if (!is_null($url) && $url->isCorrect()) { | |
832 | $this->action('add', $url, 0, TRUE); | |
833 | $count++; | |
834 | if ($favorite) { | |
835 | $last_id = $this->store->getLastId($sequence); | |
836 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
837 | } | |
838 | if ($archive) { | |
839 | $last_id = $this->store->getLastId($sequence); | |
840 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
841 | } | |
b916bcfc | 842 | } |
c765c367 | 843 | } |
c765c367 | 844 | } |
c0d321c1 | 845 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); |
63c35580 NL |
846 | Tools::logm('import from Readability completed'); |
847 | Tools::redirect(); | |
c765c367 NL |
848 | } |
849 | ||
89051914 NL |
850 | /** |
851 | * import from Poche exported file | |
852 | * @param string $targetFile the file used for importing | |
853 | * @return boolean | |
854 | */ | |
855 | private function importFromPoche($targetFile) | |
856 | { | |
857 | $str_data = file_get_contents($targetFile); | |
858 | $data = json_decode($str_data,true); | |
859 | Tools::logm('starting import from Poche'); | |
860 | ||
861 | ||
862 | $sequence = ''; | |
863 | if (STORAGE == 'postgres') { | |
864 | $sequence = 'entries_id_seq'; | |
865 | } | |
866 | ||
867 | $count = 0; | |
868 | foreach ($data as $value) { | |
869 | ||
870 | $url = new Url(base64_encode($value['url'])); | |
871 | $favorite = ($value['is_fav'] == -1); | |
872 | $archive = ($value['is_read'] == -1); | |
873 | ||
874 | # we can add the url | |
875 | if (!is_null($url) && $url->isCorrect()) { | |
876 | ||
877 | $this->action('add', $url, 0, TRUE); | |
878 | ||
879 | $count++; | |
880 | if ($favorite) { | |
881 | $last_id = $this->store->getLastId($sequence); | |
882 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
883 | } | |
884 | if ($archive) { | |
885 | $last_id = $this->store->getLastId($sequence); | |
886 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
887 | } | |
888 | } | |
889 | ||
890 | } | |
891 | $this->messages->add('s', _('import from Poche completed. ' . $count . ' new links.')); | |
892 | Tools::logm('import from Poche completed'); | |
893 | Tools::redirect(); | |
894 | } | |
895 | ||
07ee09f4 NL |
896 | /** |
897 | * import datas into your poche | |
898 | * @param string $from name of the service to import : pocket, instapaper or readability | |
899 | * @todo add the return value | |
900 | * @return boolean | |
901 | */ | |
63c35580 | 902 | public function import($from) |
c765c367 | 903 | { |
66b6a3b5 E |
904 | $providers = array( |
905 | 'pocket' => 'importFromPocket', | |
906 | 'readability' => 'importFromReadability', | |
89051914 NL |
907 | 'instapaper' => 'importFromInstapaper', |
908 | 'poche' => 'importFromPoche', | |
66b6a3b5 E |
909 | ); |
910 | ||
911 | if (! isset($providers[$from])) { | |
912 | $this->messages->add('e', _('Unknown import provider.')); | |
913 | Tools::redirect(); | |
63c35580 | 914 | } |
66b6a3b5 E |
915 | |
916 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
917 | $targetFile = constant($targetDefinition); | |
918 | ||
919 | if (! defined($targetDefinition)) { | |
920 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
921 | Tools::redirect(); | |
63c35580 | 922 | } |
66b6a3b5 E |
923 | |
924 | if (! file_exists($targetFile)) { | |
925 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
926 | Tools::redirect(); | |
63c35580 | 927 | } |
66b6a3b5 E |
928 | |
929 | $this->$providers[$from]($targetFile); | |
63c35580 | 930 | } |
c765c367 | 931 | |
07ee09f4 NL |
932 | /** |
933 | * export poche entries in json | |
934 | * @return json all poche entries | |
935 | */ | |
63c35580 NL |
936 | public function export() |
937 | { | |
8d3275be | 938 | $entries = $this->store->retrieveAll($this->user->getId()); |
63c35580 NL |
939 | echo $this->tpl->render('export.twig', array( |
940 | 'export' => Tools::renderJson($entries), | |
941 | )); | |
942 | Tools::logm('export view'); | |
c765c367 | 943 | } |
32520785 | 944 | |
07ee09f4 | 945 | /** |
a3436d4c | 946 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
947 | * @param string $which 'prod' or 'dev' |
948 | * @return string latest $which version | |
949 | */ | |
32520785 NL |
950 | private function getPocheVersion($which = 'prod') |
951 | { | |
952 | $cache_file = CACHE . '/' . $which; | |
a3436d4c NL |
953 | |
954 | # checks if the cached version file exists | |
32520785 NL |
955 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { |
956 | $version = file_get_contents($cache_file); | |
957 | } else { | |
bc1ee852 | 958 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); |
32520785 NL |
959 | file_put_contents($cache_file, $version, LOCK_EX); |
960 | } | |
961 | return $version; | |
962 | } | |
72c20a52 NL |
963 | |
964 | public function generateToken() | |
965 | { | |
966 | if (ini_get('open_basedir') === '') { | |
967 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
968 | } | |
969 | else { | |
970 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
971 | } | |
972 | ||
973 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | |
974 | $currentConfig = $_SESSION['poche_user']->config; | |
975 | $currentConfig['token'] = $token; | |
976 | $_SESSION['poche_user']->setConfig($currentConfig); | |
977 | } | |
978 | ||
f778e472 | 979 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') |
72c20a52 | 980 | { |
f778e472 | 981 | $allowed_types = array('home', 'fav', 'archive', 'tag'); |
72c20a52 NL |
982 | $config = $this->store->getConfigUser($user_id); |
983 | ||
984 | if (!in_array($type, $allowed_types) || | |
985 | $token != $config['token']) { | |
986 | die(_('Uh, there is a problem while generating feeds.')); | |
987 | } | |
988 | // Check the token | |
989 | ||
9e7c840b | 990 | $feed = new FeedWriter(RSS2); |
72c20a52 NL |
991 | $feed->setTitle('poche - ' . $type . ' feed'); |
992 | $feed->setLink(Tools::getPocheUrl()); | |
9e7c840b | 993 | $feed->setChannelElement('updated', date(DATE_RSS , time())); |
72c20a52 NL |
994 | $feed->setChannelElement('author', 'poche'); |
995 | ||
f778e472 NL |
996 | if ($type == 'tag') { |
997 | $entries = $this->store->retrieveEntriesByTag($tag_id); | |
998 | } | |
999 | else { | |
1000 | $entries = $this->store->getEntriesByView($type, $user_id); | |
1001 | } | |
1002 | ||
72c20a52 NL |
1003 | if (count($entries) > 0) { |
1004 | foreach ($entries as $entry) { | |
1005 | $newItem = $feed->createNewItem(); | |
0b57c682 | 1006 | $newItem->setTitle($entry['title']); |
72c20a52 NL |
1007 | $newItem->setLink(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); |
1008 | $newItem->setDate(time()); | |
1009 | $newItem->setDescription($entry['content']); | |
1010 | $feed->addItem($newItem); | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | $feed->genarateFeed(); | |
1015 | exit; | |
1016 | } | |
df6afaf0 | 1017 | } |