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