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