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