]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/poche/Poche.class.php
install
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
CommitLineData
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
11class 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 {
6c158544
NL
21 if (file_exists('./install') && !DEBUG_POCHE) {
22 Tools::logm('folder /install exists');
cd03b575 23 die('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.');
6c158544
NL
24 }
25
bc1ee852 26 $this->store = new Database();
eb1af592 27 $this->init();
6a361945 28 $this->messages = new Messages();
eb1af592
NL
29
30 # installation
31 if(!$this->store->isInstalled())
32 {
33 $this->install();
34 }
eb1af592
NL
35 }
36
37 private function init()
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);
b58e261d
NL
129 Tools::logm('updating content article');
130 $this->store->updateContent($last_id, $content, $this->user->getId());
eb1af592 131 }
b916bcfc
NL
132 if (!$import) {
133 $this->messages->add('s', _('the link has been added successfully'));
134 }
eb1af592
NL
135 }
136 else {
b916bcfc
NL
137 if (!$import) {
138 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
139 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
140 }
eb1af592
NL
141 }
142 }
143 else {
b916bcfc
NL
144 if (!$import) {
145 $this->messages->add('e', _('error during fetching content : the link wasn\'t added'));
146 Tools::logm('error during content fetch ' . $url->getUrl());
147 }
148 }
149 if (!$import) {
150 Tools::redirect();
eb1af592
NL
151 }
152 break;
153 case 'delete':
bc1ee852 154 $msg = 'delete link #' . $id;
8d3275be 155 if ($this->store->deleteById($id, $this->user->getId())) {
eb1af592
NL
156 if (DOWNLOAD_PICTURES) {
157 remove_directory(ABS_PATH . $id);
158 }
6a361945 159 $this->messages->add('s', _('the link has been deleted successfully'));
eb1af592
NL
160 }
161 else {
6a361945 162 $this->messages->add('e', _('the link wasn\'t deleted'));
bc1ee852 163 $msg = 'error : can\'t delete link #' . $id;
eb1af592 164 }
bc1ee852
NL
165 Tools::logm($msg);
166 Tools::redirect('?');
eb1af592
NL
167 break;
168 case 'toggle_fav' :
8d3275be 169 $this->store->favoriteById($id, $this->user->getId());
eb1af592 170 Tools::logm('mark as favorite link #' . $id);
b916bcfc
NL
171 if (!$import) {
172 Tools::redirect();
173 }
eb1af592
NL
174 break;
175 case 'toggle_archive' :
8d3275be 176 $this->store->archiveById($id, $this->user->getId());
eb1af592 177 Tools::logm('archive link #' . $id);
b916bcfc
NL
178 if (!$import) {
179 Tools::redirect();
180 }
eb1af592
NL
181 break;
182 default:
183 break;
184 }
185 }
186
187 function displayView($view, $id = 0)
188 {
189 $tpl_vars = array();
190
191 switch ($view)
192 {
eb1af592 193 case 'config':
32520785
NL
194 $dev = $this->getPocheVersion('dev');
195 $prod = $this->getPocheVersion('prod');
196 $compare_dev = version_compare(POCHE_VERSION, $dev);
197 $compare_prod = version_compare(POCHE_VERSION, $prod);
198 $tpl_vars = array(
199 'dev' => $dev,
200 'prod' => $prod,
201 'compare_dev' => $compare_dev,
202 'compare_prod' => $compare_prod,
203 );
eb1af592
NL
204 Tools::logm('config view');
205 break;
206 case 'view':
8d3275be 207 $entry = $this->store->retrieveOneById($id, $this->user->getId());
eb1af592
NL
208 if ($entry != NULL) {
209 Tools::logm('view link #' . $id);
210 $content = $entry['content'];
211 if (function_exists('tidy_parse_string')) {
212 $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8');
213 $tidy->cleanRepair();
214 $content = $tidy->value;
215 }
216 $tpl_vars = array(
217 'entry' => $entry,
218 'content' => $content,
219 );
220 }
221 else {
d8d1542e 222 Tools::logm('error in view call : entry is null');
eb1af592
NL
223 }
224 break;
225 default: # home view
8d3275be 226 $entries = $this->store->getEntriesByView($view, $this->user->getId());
6a361945
NL
227 $this->pagination->set_total(count($entries));
228 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
8d3275be 229 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
eb1af592 230 $tpl_vars = array(
6a361945
NL
231 'entries' => $datas,
232 'page_links' => $page_links,
eb1af592 233 );
6a361945 234 Tools::logm('display ' . $view . ' view');
eb1af592
NL
235 break;
236 }
237
238 return $tpl_vars;
239 }
c765c367 240
07ee09f4
NL
241 /**
242 * update the password of the current user.
243 * if MODE_DEMO is TRUE, the password can't be updated.
244 * @todo add the return value
245 * @todo set the new password in function header like this updatePassword($newPassword)
246 * @return boolean
247 */
c765c367
NL
248 public function updatePassword()
249 {
55821e04 250 if (MODE_DEMO) {
8d3275be 251 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
55821e04 252 Tools::logm('in demo mode, you can\'t do this');
6a361945 253 Tools::redirect('?view=config');
55821e04
NL
254 }
255 else {
256 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
257 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
8d3275be
NL
258 $this->messages->add('s', _('your password has been updated'));
259 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
c765c367 260 Session::logout();
8d3275be 261 Tools::logm('password updated');
c765c367
NL
262 Tools::redirect();
263 }
264 else {
8d3275be 265 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
6a361945 266 Tools::redirect('?view=config');
c765c367
NL
267 }
268 }
269 }
270 }
271
07ee09f4
NL
272 /**
273 * checks if login & password are correct and save the user in session.
274 * it redirects the user to the $referer link
275 * @param string $referer the url to redirect after login
276 * @todo add the return value
277 * @return boolean
278 */
c765c367
NL
279 public function login($referer)
280 {
281 if (!empty($_POST['login']) && !empty($_POST['password'])) {
7ce7ec4c
NL
282 $user = $this->store->login($_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']));
283 if ($user != array()) {
284 # Save login into Session
285 Session::login($user['username'], $user['password'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']), array('poche_user' => new User($user)));
286
8d3275be 287 $this->messages->add('s', _('welcome to your poche'));
c765c367
NL
288 if (!empty($_POST['longlastingsession'])) {
289 $_SESSION['longlastingsession'] = 31536000;
290 $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession'];
291 session_set_cookie_params($_SESSION['longlastingsession']);
292 } else {
293 session_set_cookie_params(0);
294 }
295 session_regenerate_id(true);
8d3275be 296 Tools::logm('login successful');
c765c367
NL
297 Tools::redirect($referer);
298 }
8d3275be 299 $this->messages->add('e', _('login failed: bad login or password'));
c765c367
NL
300 Tools::logm('login failed');
301 Tools::redirect();
302 } else {
8d3275be 303 $this->messages->add('e', _('login failed: you have to fill all fields'));
c765c367
NL
304 Tools::logm('login failed');
305 Tools::redirect();
306 }
307 }
308
07ee09f4
NL
309 /**
310 * log out the poche user. It cleans the session.
311 * @todo add the return value
312 * @return boolean
313 */
c765c367
NL
314 public function logout()
315 {
7ce7ec4c 316 $this->user = array();
c765c367 317 Session::logout();
b916bcfc
NL
318 $this->messages->add('s', _('see you soon!'));
319 Tools::logm('logout');
c765c367
NL
320 Tools::redirect();
321 }
322
07ee09f4
NL
323 /**
324 * import from Instapaper. poche needs a ./instapaper-export.html file
325 * @todo add the return value
326 * @return boolean
327 */
63c35580 328 private function importFromInstapaper()
c765c367 329 {
7f959169 330 # TODO gestion des articles favs
a62788c6
NL
331 $html = new simple_html_dom();
332 $html->load_file('./instapaper-export.html');
b916bcfc 333 Tools::logm('starting import from instapaper');
a62788c6
NL
334
335 $read = 0;
336 $errors = array();
337 foreach($html->find('ol') as $ul)
338 {
339 foreach($ul->find('li') as $li)
340 {
341 $a = $li->find('a');
342 $url = new Url(base64_encode($a[0]->href));
b916bcfc 343 $this->action('add', $url, 0, TRUE);
a62788c6 344 if ($read == '1') {
b916bcfc
NL
345 $sequence = '';
346 if (STORAGE == 'postgres') {
347 $sequence = 'entries_id_seq';
348 }
349 $last_id = $this->store->getLastId($sequence);
350 $this->action('toggle_archive', $url, $last_id, TRUE);
a62788c6
NL
351 }
352 }
7f959169
NL
353
354 # the second <ol> is for read links
a62788c6
NL
355 $read = 1;
356 }
8d3275be 357 $this->messages->add('s', _('import from instapaper completed'));
63c35580
NL
358 Tools::logm('import from instapaper completed');
359 Tools::redirect();
360 }
c765c367 361
07ee09f4
NL
362 /**
363 * import from Pocket. poche needs a ./ril_export.html file
364 * @todo add the return value
365 * @return boolean
366 */
63c35580
NL
367 private function importFromPocket()
368 {
7f959169 369 # TODO gestion des articles favs
63c35580
NL
370 $html = new simple_html_dom();
371 $html->load_file('./ril_export.html');
b916bcfc 372 Tools::logm('starting import from pocket');
63c35580
NL
373
374 $read = 0;
375 $errors = array();
376 foreach($html->find('ul') as $ul)
377 {
378 foreach($ul->find('li') as $li)
c765c367 379 {
63c35580
NL
380 $a = $li->find('a');
381 $url = new Url(base64_encode($a[0]->href));
b916bcfc 382 $this->action('add', $url, 0, TRUE);
63c35580 383 if ($read == '1') {
b916bcfc
NL
384 $sequence = '';
385 if (STORAGE == 'postgres') {
386 $sequence = 'entries_id_seq';
387 }
388 $last_id = $this->store->getLastId($sequence);
389 $this->action('toggle_archive', $url, $last_id, TRUE);
c765c367 390 }
c765c367 391 }
7f959169
NL
392
393 # the second <ul> is for read links
63c35580 394 $read = 1;
c765c367 395 }
8d3275be 396 $this->messages->add('s', _('import from pocket completed'));
63c35580
NL
397 Tools::logm('import from pocket completed');
398 Tools::redirect();
399 }
c765c367 400
07ee09f4
NL
401 /**
402 * import from Readability. poche needs a ./readability file
403 * @todo add the return value
404 * @return boolean
405 */
63c35580
NL
406 private function importFromReadability()
407 {
7f959169 408 # TODO gestion des articles lus / favs
63c35580
NL
409 $str_data = file_get_contents("./readability");
410 $data = json_decode($str_data,true);
b916bcfc 411 Tools::logm('starting import from Readability');
63c35580
NL
412
413 foreach ($data as $key => $value) {
414 $url = '';
7f959169
NL
415 foreach ($value as $attr => $attr_value) {
416 if ($attr == 'article__url') {
417 $url = new Url(base64_encode($attr_value));
c765c367 418 }
b916bcfc
NL
419 $sequence = '';
420 if (STORAGE == 'postgres') {
421 $sequence = 'entries_id_seq';
422 }
7f959169 423 // if ($attr_value == 'favorite' && $attr_value == 'true') {
b916bcfc 424 // $last_id = $this->store->getLastId($sequence);
7f959169 425 // $this->store->favoriteById($last_id);
b916bcfc 426 // $this->action('toogle_fav', $url, $last_id, TRUE);
7f959169 427 // }
b916bcfc
NL
428 if ($attr_value == 'archive' && $attr_value == 'true') {
429 $last_id = $this->store->getLastId($sequence);
430 $this->action('toggle_archive', $url, $last_id, TRUE);
431 }
c765c367 432 }
63c35580 433 if ($url->isCorrect())
b916bcfc 434 $this->action('add', $url, 0, TRUE);
c765c367 435 }
8d3275be 436 $this->messages->add('s', _('import from Readability completed'));
63c35580
NL
437 Tools::logm('import from Readability completed');
438 Tools::redirect();
c765c367
NL
439 }
440
07ee09f4
NL
441 /**
442 * import datas into your poche
443 * @param string $from name of the service to import : pocket, instapaper or readability
444 * @todo add the return value
445 * @return boolean
446 */
63c35580 447 public function import($from)
c765c367 448 {
63c35580 449 if ($from == 'pocket') {
07ee09f4 450 return $this->importFromPocket();
63c35580
NL
451 }
452 else if ($from == 'readability') {
07ee09f4 453 return $this->importFromReadability();
63c35580
NL
454 }
455 else if ($from == 'instapaper') {
07ee09f4 456 return $this->importFromInstapaper();
63c35580
NL
457 }
458 }
c765c367 459
07ee09f4
NL
460 /**
461 * export poche entries in json
462 * @return json all poche entries
463 */
63c35580
NL
464 public function export()
465 {
8d3275be 466 $entries = $this->store->retrieveAll($this->user->getId());
63c35580
NL
467 echo $this->tpl->render('export.twig', array(
468 'export' => Tools::renderJson($entries),
469 ));
470 Tools::logm('export view');
c765c367 471 }
32520785 472
07ee09f4 473 /**
a3436d4c 474 * Checks online the latest version of poche and cache it
07ee09f4
NL
475 * @param string $which 'prod' or 'dev'
476 * @return string latest $which version
477 */
32520785
NL
478 private function getPocheVersion($which = 'prod')
479 {
480 $cache_file = CACHE . '/' . $which;
a3436d4c
NL
481
482 # checks if the cached version file exists
32520785
NL
483 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
484 $version = file_get_contents($cache_file);
485 } else {
bc1ee852 486 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
32520785
NL
487 file_put_contents($cache_file, $version, LOCK_EX);
488 }
489 return $version;
490 }
eb1af592 491}