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