]>
Commit | Line | Data |
---|---|---|
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 | { | |
13 | public $user; | |
14 | public $store; | |
15 | public $tpl; | |
16 | public $messages; | |
17 | public $pagination; | |
18 | ||
19 | function __construct() | |
20 | { | |
21 | $this->initTpl(); | |
22 | if (!$this->checkBeforeInstall()) { | |
23 | exit; | |
24 | } | |
25 | $this->store = new Database(); | |
26 | $this->init(); | |
27 | $this->messages = new Messages(); | |
28 | ||
29 | # installation | |
30 | if(!$this->store->isInstalled()) | |
31 | { | |
32 | $this->install(); | |
33 | } | |
34 | } | |
35 | ||
36 | /** | |
37 | * all checks before installation. | |
38 | * @return boolean | |
39 | */ | |
40 | private function checkBeforeInstall() | |
41 | { | |
42 | $msg = ''; | |
43 | $allIsGood = TRUE; | |
44 | ||
45 | if (!is_writable(CACHE)) { | |
46 | Tools::logm('you don\'t have write access on cache directory'); | |
47 | die('You don\'t have write access on cache directory.'); | |
48 | } | |
49 | else if (file_exists('./install/update.php') && !DEBUG_POCHE) { | |
50 | $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>'; | |
51 | $allIsGood = FALSE; | |
52 | } | |
53 | else if (file_exists('./install') && !DEBUG_POCHE) { | |
54 | $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>'; | |
55 | $allIsGood = FALSE; | |
56 | } | |
57 | else if (STORAGE == 'sqlite' && !is_writable(STORAGE_SQLITE)) { | |
58 | Tools::logm('you don\'t have write access on sqlite file'); | |
59 | $msg = '<h1>error</h1><p>You don\'t have write access on sqlite file.</p>'; | |
60 | $allIsGood = FALSE; | |
61 | } | |
62 | ||
63 | if (!$allIsGood) { | |
64 | echo $this->tpl->render('error.twig', array( | |
65 | 'msg' => $msg | |
66 | )); | |
67 | } | |
68 | ||
69 | return $allIsGood; | |
70 | } | |
71 | ||
72 | private function initTpl() | |
73 | { | |
74 | # template engine | |
75 | $loader = new Twig_Loader_Filesystem(TPL); | |
76 | if (DEBUG_POCHE) { | |
77 | $twig_params = array(); | |
78 | } | |
79 | else { | |
80 | $twig_params = array('cache' => CACHE); | |
81 | } | |
82 | $this->tpl = new Twig_Environment($loader, $twig_params); | |
83 | $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); | |
84 | # filter to display domain name of an url | |
85 | $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); | |
86 | $this->tpl->addFilter($filter); | |
87 | ||
88 | # filter for reading time | |
89 | $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime'); | |
90 | $this->tpl->addFilter($filter); | |
91 | } | |
92 | ||
93 | private function init() | |
94 | { | |
95 | Tools::initPhp(); | |
96 | Session::init(); | |
97 | ||
98 | if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) { | |
99 | $this->user = $_SESSION['poche_user']; | |
100 | } | |
101 | else { | |
102 | # fake user, just for install & login screens | |
103 | $this->user = new User(); | |
104 | $this->user->setConfig($this->getDefaultConfig()); | |
105 | } | |
106 | ||
107 | # l10n | |
108 | $language = $this->user->getConfigValue('language'); | |
109 | putenv('LC_ALL=' . $language); | |
110 | setlocale(LC_ALL, $language); | |
111 | bindtextdomain($language, LOCALE); | |
112 | textdomain($language); | |
113 | ||
114 | # Pagination | |
115 | $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); | |
116 | } | |
117 | ||
118 | private function install() | |
119 | { | |
120 | Tools::logm('poche still not installed'); | |
121 | echo $this->tpl->render('install.twig', array( | |
122 | 'token' => Session::getToken() | |
123 | )); | |
124 | if (isset($_GET['install'])) { | |
125 | if (($_POST['password'] == $_POST['password_repeat']) | |
126 | && $_POST['password'] != "" && $_POST['login'] != "") { | |
127 | # let's rock, install poche baby ! | |
128 | if ($this->store->install($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) | |
129 | { | |
130 | Session::logout(); | |
131 | Tools::logm('poche is now installed'); | |
132 | Tools::redirect(); | |
133 | } | |
134 | } | |
135 | else { | |
136 | Tools::logm('error during installation'); | |
137 | Tools::redirect(); | |
138 | } | |
139 | } | |
140 | exit(); | |
141 | } | |
142 | ||
143 | public function getDefaultConfig() | |
144 | { | |
145 | return array( | |
146 | 'pager' => PAGINATION, | |
147 | 'language' => LANG, | |
148 | ); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Call action (mark as fav, archive, delete, etc.) | |
153 | */ | |
154 | public function action($action, Url $url, $id = 0, $import = FALSE) | |
155 | { | |
156 | switch ($action) | |
157 | { | |
158 | case 'add': | |
159 | $content = $url->extract(); | |
160 | ||
161 | if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) { | |
162 | Tools::logm('add link ' . $url->getUrl()); | |
163 | $sequence = ''; | |
164 | if (STORAGE == 'postgres') { | |
165 | $sequence = 'entries_id_seq'; | |
166 | } | |
167 | $last_id = $this->store->getLastId($sequence); | |
168 | if (DOWNLOAD_PICTURES) { | |
169 | $content = filtre_picture($parametres_url['body'], $url->getUrl(), $last_id); | |
170 | Tools::logm('updating content article'); | |
171 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
172 | } | |
173 | if (!$import) { | |
174 | $this->messages->add('s', _('the link has been added successfully')); | |
175 | } | |
176 | } | |
177 | else { | |
178 | if (!$import) { | |
179 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); | |
180 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
181 | } | |
182 | } | |
183 | ||
184 | if (!$import) { | |
185 | Tools::redirect(); | |
186 | } | |
187 | break; | |
188 | case 'delete': | |
189 | $msg = 'delete link #' . $id; | |
190 | if ($this->store->deleteById($id, $this->user->getId())) { | |
191 | if (DOWNLOAD_PICTURES) { | |
192 | remove_directory(ABS_PATH . $id); | |
193 | } | |
194 | $this->messages->add('s', _('the link has been deleted successfully')); | |
195 | } | |
196 | else { | |
197 | $this->messages->add('e', _('the link wasn\'t deleted')); | |
198 | $msg = 'error : can\'t delete link #' . $id; | |
199 | } | |
200 | Tools::logm($msg); | |
201 | Tools::redirect('?'); | |
202 | break; | |
203 | case 'toggle_fav' : | |
204 | $this->store->favoriteById($id, $this->user->getId()); | |
205 | Tools::logm('mark as favorite link #' . $id); | |
206 | if (!$import) { | |
207 | Tools::redirect(); | |
208 | } | |
209 | break; | |
210 | case 'toggle_archive' : | |
211 | $this->store->archiveById($id, $this->user->getId()); | |
212 | Tools::logm('archive link #' . $id); | |
213 | if (!$import) { | |
214 | Tools::redirect(); | |
215 | } | |
216 | break; | |
217 | default: | |
218 | break; | |
219 | } | |
220 | } | |
221 | ||
222 | function displayView($view, $id = 0) | |
223 | { | |
224 | $tpl_vars = array(); | |
225 | ||
226 | switch ($view) | |
227 | { | |
228 | case 'config': | |
229 | $dev = $this->getPocheVersion('dev'); | |
230 | $prod = $this->getPocheVersion('prod'); | |
231 | $compare_dev = version_compare(POCHE_VERSION, $dev); | |
232 | $compare_prod = version_compare(POCHE_VERSION, $prod); | |
233 | $tpl_vars = array( | |
234 | 'dev' => $dev, | |
235 | 'prod' => $prod, | |
236 | 'compare_dev' => $compare_dev, | |
237 | 'compare_prod' => $compare_prod, | |
238 | ); | |
239 | Tools::logm('config view'); | |
240 | break; | |
241 | case 'view': | |
242 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | |
243 | if ($entry != NULL) { | |
244 | Tools::logm('view link #' . $id); | |
245 | $content = $entry['content']; | |
246 | if (function_exists('tidy_parse_string')) { | |
247 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
248 | $tidy->cleanRepair(); | |
249 | $content = $tidy->value; | |
250 | } | |
251 | $tpl_vars = array( | |
252 | 'entry' => $entry, | |
253 | 'content' => $content, | |
254 | ); | |
255 | } | |
256 | else { | |
257 | Tools::logm('error in view call : entry is null'); | |
258 | } | |
259 | break; | |
260 | default: # home view | |
261 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); | |
262 | $this->pagination->set_total(count($entries)); | |
263 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
264 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); | |
265 | $tpl_vars = array( | |
266 | 'entries' => $datas, | |
267 | 'page_links' => $page_links, | |
268 | ); | |
269 | Tools::logm('display ' . $view . ' view'); | |
270 | break; | |
271 | } | |
272 | ||
273 | return $tpl_vars; | |
274 | } | |
275 | ||
276 | /** | |
277 | * update the password of the current user. | |
278 | * if MODE_DEMO is TRUE, the password can't be updated. | |
279 | * @todo add the return value | |
280 | * @todo set the new password in function header like this updatePassword($newPassword) | |
281 | * @return boolean | |
282 | */ | |
283 | public function updatePassword() | |
284 | { | |
285 | if (MODE_DEMO) { | |
286 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); | |
287 | Tools::logm('in demo mode, you can\'t do this'); | |
288 | Tools::redirect('?view=config'); | |
289 | } | |
290 | else { | |
291 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
292 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
293 | $this->messages->add('s', _('your password has been updated')); | |
294 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
295 | Session::logout(); | |
296 | Tools::logm('password updated'); | |
297 | Tools::redirect(); | |
298 | } | |
299 | else { | |
300 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); | |
301 | Tools::redirect('?view=config'); | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
307 | /** | |
308 | * checks if login & password are correct and save the user in session. | |
309 | * it redirects the user to the $referer link | |
310 | * @param string $referer the url to redirect after login | |
311 | * @todo add the return value | |
312 | * @return boolean | |
313 | */ | |
314 | public function login($referer) | |
315 | { | |
316 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | |
317 | $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])); | |
318 | if ($user != array()) { | |
319 | # Save login into Session | |
320 | Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user))); | |
321 | ||
322 | $this->messages->add('s', _('welcome to your poche')); | |
323 | if (!empty($_POST['longlastingsession'])) { | |
324 | $_SESSION['longlastingsession'] = 31536000; | |
325 | $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; | |
326 | session_set_cookie_params($_SESSION['longlastingsession']); | |
327 | } else { | |
328 | session_set_cookie_params(0); | |
329 | } | |
330 | session_regenerate_id(true); | |
331 | Tools::logm('login successful'); | |
332 | Tools::redirect($referer); | |
333 | } | |
334 | $this->messages->add('e', _('login failed: bad login or password')); | |
335 | Tools::logm('login failed'); | |
336 | Tools::redirect(); | |
337 | } else { | |
338 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
339 | Tools::logm('login failed'); | |
340 | Tools::redirect(); | |
341 | } | |
342 | } | |
343 | ||
344 | /** | |
345 | * log out the poche user. It cleans the session. | |
346 | * @todo add the return value | |
347 | * @return boolean | |
348 | */ | |
349 | public function logout() | |
350 | { | |
351 | $this->user = array(); | |
352 | Session::logout(); | |
353 | $this->messages->add('s', _('see you soon!')); | |
354 | Tools::logm('logout'); | |
355 | Tools::redirect(); | |
356 | } | |
357 | ||
358 | /** | |
359 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
360 | * @todo add the return value | |
361 | * @param string $targetFile the file used for importing | |
362 | * @return boolean | |
363 | */ | |
364 | private function importFromInstapaper($targetFile) | |
365 | { | |
366 | # TODO gestion des articles favs | |
367 | $html = new simple_html_dom(); | |
368 | $html->load_file($targetFile); | |
369 | Tools::logm('starting import from instapaper'); | |
370 | ||
371 | $read = 0; | |
372 | $errors = array(); | |
373 | foreach($html->find('ol') as $ul) | |
374 | { | |
375 | foreach($ul->find('li') as $li) | |
376 | { | |
377 | $a = $li->find('a'); | |
378 | $url = new Url(base64_encode($a[0]->href)); | |
379 | $this->action('add', $url, 0, TRUE); | |
380 | if ($read == '1') { | |
381 | $sequence = ''; | |
382 | if (STORAGE == 'postgres') { | |
383 | $sequence = 'entries_id_seq'; | |
384 | } | |
385 | $last_id = $this->store->getLastId($sequence); | |
386 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
387 | } | |
388 | } | |
389 | ||
390 | # the second <ol> is for read links | |
391 | $read = 1; | |
392 | } | |
393 | $this->messages->add('s', _('import from instapaper completed')); | |
394 | Tools::logm('import from instapaper completed'); | |
395 | Tools::redirect(); | |
396 | } | |
397 | ||
398 | /** | |
399 | * import from Pocket. poche needs a ./ril_export.html file | |
400 | * @todo add the return value | |
401 | * @param string $targetFile the file used for importing | |
402 | * @return boolean | |
403 | */ | |
404 | private function importFromPocket($targetFile) | |
405 | { | |
406 | # TODO gestion des articles favs | |
407 | $html = new simple_html_dom(); | |
408 | $html->load_file($targetFile); | |
409 | Tools::logm('starting import from pocket'); | |
410 | ||
411 | $read = 0; | |
412 | $errors = array(); | |
413 | foreach($html->find('ul') as $ul) | |
414 | { | |
415 | foreach($ul->find('li') as $li) | |
416 | { | |
417 | $a = $li->find('a'); | |
418 | $url = new Url(base64_encode($a[0]->href)); | |
419 | $this->action('add', $url, 0, TRUE); | |
420 | if ($read == '1') { | |
421 | $sequence = ''; | |
422 | if (STORAGE == 'postgres') { | |
423 | $sequence = 'entries_id_seq'; | |
424 | } | |
425 | $last_id = $this->store->getLastId($sequence); | |
426 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
427 | } | |
428 | } | |
429 | ||
430 | # the second <ul> is for read links | |
431 | $read = 1; | |
432 | } | |
433 | $this->messages->add('s', _('import from pocket completed')); | |
434 | Tools::logm('import from pocket completed'); | |
435 | Tools::redirect(); | |
436 | } | |
437 | ||
438 | /** | |
439 | * import from Readability. poche needs a ./readability file | |
440 | * @todo add the return value | |
441 | * @param string $targetFile the file used for importing | |
442 | * @return boolean | |
443 | */ | |
444 | private function importFromReadability($targetFile) | |
445 | { | |
446 | # TODO gestion des articles lus / favs | |
447 | $str_data = file_get_contents($targetFile); | |
448 | $data = json_decode($str_data,true); | |
449 | Tools::logm('starting import from Readability'); | |
450 | $count = 0; | |
451 | foreach ($data as $key => $value) { | |
452 | $url = NULL; | |
453 | $favorite = FALSE; | |
454 | $archive = FALSE; | |
455 | foreach ($value as $attr => $attr_value) { | |
456 | if ($attr == 'article__url') { | |
457 | $url = new Url(base64_encode($attr_value)); | |
458 | } | |
459 | $sequence = ''; | |
460 | if (STORAGE == 'postgres') { | |
461 | $sequence = 'entries_id_seq'; | |
462 | } | |
463 | if ($attr_value == 'true') { | |
464 | if ($attr == 'favorite') { | |
465 | $favorite = TRUE; | |
466 | } | |
467 | if ($attr == 'archive') { | |
468 | $archive = TRUE; | |
469 | } | |
470 | } | |
471 | } | |
472 | # we can add the url | |
473 | if (!is_null($url) && $url->isCorrect()) { | |
474 | $this->action('add', $url, 0, TRUE); | |
475 | $count++; | |
476 | if ($favorite) { | |
477 | $last_id = $this->store->getLastId($sequence); | |
478 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
479 | } | |
480 | if ($archive) { | |
481 | $last_id = $this->store->getLastId($sequence); | |
482 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
483 | } | |
484 | } | |
485 | } | |
486 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); | |
487 | Tools::logm('import from Readability completed'); | |
488 | Tools::redirect(); | |
489 | } | |
490 | ||
491 | /** | |
492 | * import datas into your poche | |
493 | * @param string $from name of the service to import : pocket, instapaper or readability | |
494 | * @todo add the return value | |
495 | * @return boolean | |
496 | */ | |
497 | public function import($from) | |
498 | { | |
499 | $providers = array( | |
500 | 'pocket' => 'importFromPocket', | |
501 | 'readability' => 'importFromReadability', | |
502 | 'instapaper' => 'importFromInstapaper' | |
503 | ); | |
504 | ||
505 | if (! isset($providers[$from])) { | |
506 | $this->messages->add('e', _('Unknown import provider.')); | |
507 | Tools::redirect(); | |
508 | } | |
509 | ||
510 | $targetDefinition = 'IMPORT_' . strtoupper($from) . '_FILE'; | |
511 | $targetFile = constant($targetDefinition); | |
512 | ||
513 | if (! defined($targetDefinition)) { | |
514 | $this->messages->add('e', _('Incomplete inc/poche/define.inc.php file, please define "' . $targetDefinition . '".')); | |
515 | Tools::redirect(); | |
516 | } | |
517 | ||
518 | if (! file_exists($targetFile)) { | |
519 | $this->messages->add('e', _('Could not find required "' . $targetFile . '" import file.')); | |
520 | Tools::redirect(); | |
521 | } | |
522 | ||
523 | $this->$providers[$from]($targetFile); | |
524 | } | |
525 | ||
526 | /** | |
527 | * export poche entries in json | |
528 | * @return json all poche entries | |
529 | */ | |
530 | public function export() | |
531 | { | |
532 | $entries = $this->store->retrieveAll($this->user->getId()); | |
533 | echo $this->tpl->render('export.twig', array( | |
534 | 'export' => Tools::renderJson($entries), | |
535 | )); | |
536 | Tools::logm('export view'); | |
537 | } | |
538 | ||
539 | /** | |
540 | * Checks online the latest version of poche and cache it | |
541 | * @param string $which 'prod' or 'dev' | |
542 | * @return string latest $which version | |
543 | */ | |
544 | private function getPocheVersion($which = 'prod') | |
545 | { | |
546 | $cache_file = CACHE . '/' . $which; | |
547 | ||
548 | # checks if the cached version file exists | |
549 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | |
550 | $version = file_get_contents($cache_file); | |
551 | } else { | |
552 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); | |
553 | file_put_contents($cache_file, $version, LOCK_EX); | |
554 | } | |
555 | return $version; | |
556 | } | |
557 | } |