]>
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 = 'A poche update is needed. Please execute this update <a href="install/update.php">by clicking here</a>. If you have already do the update, please delete /install folder.'; | |
51 | $allIsGood = FALSE; | |
52 | } | |
53 | else if (file_exists('./install') && !DEBUG_POCHE) { | |
54 | $msg = 'If you want to update your poche, you just have to delete /install folder. <br />To install your poche with sqlite, copy /install/poche.sqlite in /db and delete the folder /install. you have to delete the /install folder before using poche.'; | |
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 = 'You don\'t have write access on sqlite file.'; | |
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 | if($parametres_url = $url->fetchContent()) { | |
160 | if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) { | |
161 | Tools::logm('add link ' . $url->getUrl()); | |
162 | $sequence = ''; | |
163 | if (STORAGE == 'postgres') { | |
164 | $sequence = 'entries_id_seq'; | |
165 | } | |
166 | $last_id = $this->store->getLastId($sequence); | |
167 | if (DOWNLOAD_PICTURES) { | |
168 | $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id); | |
169 | Tools::logm('updating content article'); | |
170 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
171 | } | |
172 | if (!$import) { | |
173 | $this->messages->add('s', _('the link has been added successfully')); | |
174 | } | |
175 | } | |
176 | else { | |
177 | if (!$import) { | |
178 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); | |
179 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | |
180 | } | |
181 | } | |
182 | } | |
183 | else { | |
184 | if (!$import) { | |
185 | $this->messages->add('e', _('error during fetching content : the link wasn\'t added')); | |
186 | Tools::logm('error during content fetch ' . $url->getUrl()); | |
187 | } | |
188 | } | |
189 | if (!$import) { | |
190 | Tools::redirect(); | |
191 | } | |
192 | break; | |
193 | case 'delete': | |
194 | $msg = 'delete link #' . $id; | |
195 | if ($this->store->deleteById($id, $this->user->getId())) { | |
196 | if (DOWNLOAD_PICTURES) { | |
197 | remove_directory(ABS_PATH . $id); | |
198 | } | |
199 | $this->messages->add('s', _('the link has been deleted successfully')); | |
200 | } | |
201 | else { | |
202 | $this->messages->add('e', _('the link wasn\'t deleted')); | |
203 | $msg = 'error : can\'t delete link #' . $id; | |
204 | } | |
205 | Tools::logm($msg); | |
206 | Tools::redirect('?'); | |
207 | break; | |
208 | case 'toggle_fav' : | |
209 | $this->store->favoriteById($id, $this->user->getId()); | |
210 | Tools::logm('mark as favorite link #' . $id); | |
211 | if (!$import) { | |
212 | Tools::redirect(); | |
213 | } | |
214 | break; | |
215 | case 'toggle_archive' : | |
216 | $this->store->archiveById($id, $this->user->getId()); | |
217 | Tools::logm('archive link #' . $id); | |
218 | if (!$import) { | |
219 | Tools::redirect(); | |
220 | } | |
221 | break; | |
222 | default: | |
223 | Tools::logm('action ' . $action . 'doesn\'t exist'); | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
228 | function displayView($view, $id = 0) | |
229 | { | |
230 | $tpl_vars = array(); | |
231 | ||
232 | switch ($view) | |
233 | { | |
234 | case 'config': | |
235 | $dev = $this->getPocheVersion('dev'); | |
236 | $prod = $this->getPocheVersion('prod'); | |
237 | $compare_dev = version_compare(POCHE_VERSION, $dev); | |
238 | $compare_prod = version_compare(POCHE_VERSION, $prod); | |
239 | $tpl_vars = array( | |
240 | 'dev' => $dev, | |
241 | 'prod' => $prod, | |
242 | 'compare_dev' => $compare_dev, | |
243 | 'compare_prod' => $compare_prod, | |
244 | ); | |
245 | Tools::logm('config view'); | |
246 | break; | |
247 | case 'view': | |
248 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | |
249 | if ($entry != NULL) { | |
250 | Tools::logm('view link #' . $id); | |
251 | $content = $entry['content']; | |
252 | if (function_exists('tidy_parse_string')) { | |
253 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | |
254 | $tidy->cleanRepair(); | |
255 | $content = $tidy->value; | |
256 | } | |
257 | $tpl_vars = array( | |
258 | 'entry' => $entry, | |
259 | 'content' => $content, | |
260 | ); | |
261 | } | |
262 | else { | |
263 | Tools::logm('error in view call : entry is null'); | |
264 | } | |
265 | break; | |
266 | default: # home view | |
267 | $entries = $this->store->getEntriesByView($view, $this->user->getId()); | |
268 | $this->pagination->set_total(count($entries)); | |
269 | $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&'); | |
270 | $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit()); | |
271 | $tpl_vars = array( | |
272 | 'entries' => $datas, | |
273 | 'page_links' => $page_links, | |
274 | ); | |
275 | Tools::logm('display ' . $view . ' view'); | |
276 | break; | |
277 | } | |
278 | ||
279 | return $tpl_vars; | |
280 | } | |
281 | ||
282 | /** | |
283 | * update the password of the current user. | |
284 | * if MODE_DEMO is TRUE, the password can't be updated. | |
285 | * @todo add the return value | |
286 | * @todo set the new password in function header like this updatePassword($newPassword) | |
287 | * @return boolean | |
288 | */ | |
289 | public function updatePassword() | |
290 | { | |
291 | if (MODE_DEMO) { | |
292 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); | |
293 | Tools::logm('in demo mode, you can\'t do this'); | |
294 | Tools::redirect('?view=config'); | |
295 | } | |
296 | else { | |
297 | if (isset($_POST['password']) && isset($_POST['password_repeat'])) { | |
298 | if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { | |
299 | $this->messages->add('s', _('your password has been updated')); | |
300 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername())); | |
301 | Session::logout(); | |
302 | Tools::logm('password updated'); | |
303 | Tools::redirect(); | |
304 | } | |
305 | else { | |
306 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); | |
307 | Tools::redirect('?view=config'); | |
308 | } | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | /** | |
314 | * checks if login & password are correct and save the user in session. | |
315 | * it redirects the user to the $referer link | |
316 | * @param string $referer the url to redirect after login | |
317 | * @todo add the return value | |
318 | * @return boolean | |
319 | */ | |
320 | public function login($referer) | |
321 | { | |
322 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | |
323 | $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login'])); | |
324 | if ($user != array()) { | |
325 | # Save login into Session | |
326 | Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user))); | |
327 | ||
328 | $this->messages->add('s', _('welcome to your poche')); | |
329 | if (!empty($_POST['longlastingsession'])) { | |
330 | $_SESSION['longlastingsession'] = 31536000; | |
331 | $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; | |
332 | session_set_cookie_params($_SESSION['longlastingsession']); | |
333 | } else { | |
334 | session_set_cookie_params(0); | |
335 | } | |
336 | session_regenerate_id(true); | |
337 | Tools::logm('login successful'); | |
338 | Tools::redirect($referer); | |
339 | } | |
340 | $this->messages->add('e', _('login failed: bad login or password')); | |
341 | Tools::logm('login failed'); | |
342 | Tools::redirect(); | |
343 | } else { | |
344 | $this->messages->add('e', _('login failed: you have to fill all fields')); | |
345 | Tools::logm('login failed'); | |
346 | Tools::redirect(); | |
347 | } | |
348 | } | |
349 | ||
350 | /** | |
351 | * log out the poche user. It cleans the session. | |
352 | * @todo add the return value | |
353 | * @return boolean | |
354 | */ | |
355 | public function logout() | |
356 | { | |
357 | $this->user = array(); | |
358 | Session::logout(); | |
359 | $this->messages->add('s', _('see you soon!')); | |
360 | Tools::logm('logout'); | |
361 | Tools::redirect(); | |
362 | } | |
363 | ||
364 | /** | |
365 | * import from Instapaper. poche needs a ./instapaper-export.html file | |
366 | * @todo add the return value | |
367 | * @return boolean | |
368 | */ | |
369 | private function importFromInstapaper() | |
370 | { | |
371 | # TODO gestion des articles favs | |
372 | $html = new simple_html_dom(); | |
373 | $html->load_file('./instapaper-export.html'); | |
374 | Tools::logm('starting import from instapaper'); | |
375 | ||
376 | $read = 0; | |
377 | $errors = array(); | |
378 | foreach($html->find('ol') as $ul) | |
379 | { | |
380 | foreach($ul->find('li') as $li) | |
381 | { | |
382 | $a = $li->find('a'); | |
383 | $url = new Url(base64_encode($a[0]->href)); | |
384 | $this->action('add', $url, 0, TRUE); | |
385 | if ($read == '1') { | |
386 | $sequence = ''; | |
387 | if (STORAGE == 'postgres') { | |
388 | $sequence = 'entries_id_seq'; | |
389 | } | |
390 | $last_id = $this->store->getLastId($sequence); | |
391 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
392 | } | |
393 | } | |
394 | ||
395 | # the second <ol> is for read links | |
396 | $read = 1; | |
397 | } | |
398 | $this->messages->add('s', _('import from instapaper completed')); | |
399 | Tools::logm('import from instapaper completed'); | |
400 | Tools::redirect(); | |
401 | } | |
402 | ||
403 | /** | |
404 | * import from Pocket. poche needs a ./ril_export.html file | |
405 | * @todo add the return value | |
406 | * @return boolean | |
407 | */ | |
408 | private function importFromPocket() | |
409 | { | |
410 | # TODO gestion des articles favs | |
411 | $html = new simple_html_dom(); | |
412 | $html->load_file('./ril_export.html'); | |
413 | Tools::logm('starting import from pocket'); | |
414 | ||
415 | $read = 0; | |
416 | $errors = array(); | |
417 | foreach($html->find('ul') as $ul) | |
418 | { | |
419 | foreach($ul->find('li') as $li) | |
420 | { | |
421 | $a = $li->find('a'); | |
422 | $url = new Url(base64_encode($a[0]->href)); | |
423 | $this->action('add', $url, 0, TRUE); | |
424 | if ($read == '1') { | |
425 | $sequence = ''; | |
426 | if (STORAGE == 'postgres') { | |
427 | $sequence = 'entries_id_seq'; | |
428 | } | |
429 | $last_id = $this->store->getLastId($sequence); | |
430 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
431 | } | |
432 | } | |
433 | ||
434 | # the second <ul> is for read links | |
435 | $read = 1; | |
436 | } | |
437 | $this->messages->add('s', _('import from pocket completed')); | |
438 | Tools::logm('import from pocket completed'); | |
439 | Tools::redirect(); | |
440 | } | |
441 | ||
442 | /** | |
443 | * import from Readability. poche needs a ./readability file | |
444 | * @todo add the return value | |
445 | * @return boolean | |
446 | */ | |
447 | private function importFromReadability() | |
448 | { | |
449 | # TODO gestion des articles lus / favs | |
450 | $str_data = file_get_contents("./readability"); | |
451 | $data = json_decode($str_data,true); | |
452 | Tools::logm('starting import from Readability'); | |
453 | $count = 0; | |
454 | foreach ($data as $key => $value) { | |
455 | $url = NULL; | |
456 | $favorite = FALSE; | |
457 | $archive = FALSE; | |
458 | foreach ($value as $attr => $attr_value) { | |
459 | if ($attr == 'article__url') { | |
460 | $url = new Url(base64_encode($attr_value)); | |
461 | } | |
462 | $sequence = ''; | |
463 | if (STORAGE == 'postgres') { | |
464 | $sequence = 'entries_id_seq'; | |
465 | } | |
466 | if ($attr_value == 'true') { | |
467 | if ($attr == 'favorite') { | |
468 | $favorite = TRUE; | |
469 | } | |
470 | if ($attr == 'archive') { | |
471 | $archive = TRUE; | |
472 | } | |
473 | } | |
474 | } | |
475 | # we can add the url | |
476 | if (!is_null($url) && $url->isCorrect()) { | |
477 | $this->action('add', $url, 0, TRUE); | |
478 | $count++; | |
479 | if ($favorite) { | |
480 | $last_id = $this->store->getLastId($sequence); | |
481 | $this->action('toggle_fav', $url, $last_id, TRUE); | |
482 | } | |
483 | if ($archive) { | |
484 | $last_id = $this->store->getLastId($sequence); | |
485 | $this->action('toggle_archive', $url, $last_id, TRUE); | |
486 | } | |
487 | } | |
488 | } | |
489 | $this->messages->add('s', _('import from Readability completed. ' . $count . ' new links.')); | |
490 | Tools::logm('import from Readability completed'); | |
491 | Tools::redirect(); | |
492 | } | |
493 | ||
494 | /** | |
495 | * import datas into your poche | |
496 | * @param string $from name of the service to import : pocket, instapaper or readability | |
497 | * @todo add the return value | |
498 | * @return boolean | |
499 | */ | |
500 | public function import($from) | |
501 | { | |
502 | if ($from == 'pocket') { | |
503 | return $this->importFromPocket(); | |
504 | } | |
505 | else if ($from == 'readability') { | |
506 | return $this->importFromReadability(); | |
507 | } | |
508 | else if ($from == 'instapaper') { | |
509 | return $this->importFromInstapaper(); | |
510 | } | |
511 | } | |
512 | ||
513 | /** | |
514 | * export poche entries in json | |
515 | * @return json all poche entries | |
516 | */ | |
517 | public function export() | |
518 | { | |
519 | $entries = $this->store->retrieveAll($this->user->getId()); | |
520 | echo $this->tpl->render('export.twig', array( | |
521 | 'export' => Tools::renderJson($entries), | |
522 | )); | |
523 | Tools::logm('export view'); | |
524 | } | |
525 | ||
526 | /** | |
527 | * Checks online the latest version of poche and cache it | |
528 | * @param string $which 'prod' or 'dev' | |
529 | * @return string latest $which version | |
530 | */ | |
531 | private function getPocheVersion($which = 'prod') | |
532 | { | |
533 | $cache_file = CACHE . '/' . $which; | |
534 | ||
535 | # checks if the cached version file exists | |
536 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | |
537 | $version = file_get_contents($cache_file); | |
538 | } else { | |
539 | $version = file_get_contents('http://static.inthepoche.com/versions/' . $which); | |
540 | file_put_contents($cache_file, $version, LOCK_EX); | |
541 | } | |
542 | return $version; | |
543 | } | |
544 | } |