]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Poche.class.php
typo
[github/wallabag/wallabag.git] / inc / poche / Poche.class.php
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 if (file_exists('./install') && !DEBUG_POCHE) {
22 Tools::logm('folder /install exists');
23 die('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.');
24 }
25
26 $this->store = new Database();
27 $this->init();
28 $this->messages = new Messages();
29
30 # installation
31 if(!$this->store->isInstalled())
32 {
33 $this->install();
34 }
35 }
36
37 private function init()
38 {
39 Tools::initPhp();
40 Session::init();
41
42 if (isset($_SESSION['poche_user']) && $_SESSION['poche_user'] != array()) {
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 }
50
51 # l10n
52 $language = $this->user->getConfigValue('language');
53 putenv('LC_ALL=' . $language);
54 setlocale(LC_ALL, $language);
55 bindtextdomain($language, LOCALE);
56 textdomain($language);
57
58 # template engine
59 $loader = new Twig_Loader_Filesystem(TPL);
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);
67 $this->tpl->addExtension(new Twig_Extensions_Extension_I18n());
68 # filter to display domain name of an url
69 $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
70 $this->tpl->addFilter($filter);
71
72 # filter for reading time
73 $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime');
74 $this->tpl->addFilter($filter);
75
76 # Pagination
77 $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p');
78 }
79
80 private function install()
81 {
82 Tools::logm('poche still not installed');
83 echo $this->tpl->render('install.twig', array(
84 'token' => Session::getToken()
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();
92 Tools::logm('poche is now installed');
93 Tools::redirect();
94 }
95 else {
96 Tools::logm('error during installation');
97 Tools::redirect();
98 }
99 }
100 exit();
101 }
102
103 public function getDefaultConfig()
104 {
105 return array(
106 'pager' => PAGINATION,
107 'language' => LANG,
108 );
109 }
110
111 /**
112 * Call action (mark as fav, archive, delete, etc.)
113 */
114 public function action($action, Url $url, $id = 0, $import = FALSE)
115 {
116 switch ($action)
117 {
118 case 'add':
119 if($parametres_url = $url->fetchContent()) {
120 if ($this->store->add($url->getUrl(), $parametres_url['title'], $parametres_url['content'], $this->user->getId())) {
121 Tools::logm('add link ' . $url->getUrl());
122 $sequence = '';
123 if (STORAGE == 'postgres') {
124 $sequence = 'entries_id_seq';
125 }
126 $last_id = $this->store->getLastId($sequence);
127 if (DOWNLOAD_PICTURES) {
128 $content = filtre_picture($parametres_url['content'], $url->getUrl(), $last_id);
129 }
130 if (!$import) {
131 $this->messages->add('s', _('the link has been added successfully'));
132 }
133 }
134 else {
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 }
139 }
140 }
141 else {
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();
149 }
150 break;
151 case 'delete':
152 $msg = 'delete link #' . $id;
153 if ($this->store->deleteById($id, $this->user->getId())) {
154 if (DOWNLOAD_PICTURES) {
155 remove_directory(ABS_PATH . $id);
156 }
157 $this->messages->add('s', _('the link has been deleted successfully'));
158 }
159 else {
160 $this->messages->add('e', _('the link wasn\'t deleted'));
161 $msg = 'error : can\'t delete link #' . $id;
162 }
163 Tools::logm($msg);
164 Tools::redirect('?');
165 break;
166 case 'toggle_fav' :
167 $this->store->favoriteById($id, $this->user->getId());
168 Tools::logm('mark as favorite link #' . $id);
169 if (!$import) {
170 Tools::redirect();
171 }
172 break;
173 case 'toggle_archive' :
174 $this->store->archiveById($id, $this->user->getId());
175 Tools::logm('archive link #' . $id);
176 if (!$import) {
177 Tools::redirect();
178 }
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 {
191 case 'config':
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 );
202 Tools::logm('config view');
203 break;
204 case 'view':
205 $entry = $this->store->retrieveOneById($id, $this->user->getId());
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
224 $entries = $this->store->getEntriesByView($view, $this->user->getId());
225 $this->pagination->set_total(count($entries));
226 $page_links = $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . '&');
227 $datas = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit());
228 $tpl_vars = array(
229 'entries' => $datas,
230 'page_links' => $page_links,
231 );
232 Tools::logm('display ' . $view . ' view');
233 break;
234 }
235
236 return $tpl_vars;
237 }
238
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 */
246 public function updatePassword()
247 {
248 if (MODE_DEMO) {
249 $this->messages->add('i', _('in demo mode, you can\'t update your password'));
250 Tools::logm('in demo mode, you can\'t do this');
251 Tools::redirect('?view=config');
252 }
253 else {
254 if (isset($_POST['password']) && isset($_POST['password_repeat'])) {
255 if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") {
256 $this->messages->add('s', _('your password has been updated'));
257 $this->store->updatePassword($this->user->getId(), Tools::encodeString($_POST['password'] . $this->user->getUsername()));
258 Session::logout();
259 Tools::logm('password updated');
260 Tools::redirect();
261 }
262 else {
263 $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields'));
264 Tools::redirect('?view=config');
265 }
266 }
267 }
268 }
269
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 */
277 public function login($referer)
278 {
279 if (!empty($_POST['login']) && !empty($_POST['password'])) {
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
285 $this->messages->add('s', _('welcome to your poche'));
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);
294 Tools::logm('login successful');
295 Tools::redirect($referer);
296 }
297 $this->messages->add('e', _('login failed: bad login or password'));
298 Tools::logm('login failed');
299 Tools::redirect();
300 } else {
301 $this->messages->add('e', _('login failed: you have to fill all fields'));
302 Tools::logm('login failed');
303 Tools::redirect();
304 }
305 }
306
307 /**
308 * log out the poche user. It cleans the session.
309 * @todo add the return value
310 * @return boolean
311 */
312 public function logout()
313 {
314 $this->user = array();
315 Session::logout();
316 $this->messages->add('s', _('see you soon!'));
317 Tools::logm('logout');
318 Tools::redirect();
319 }
320
321 /**
322 * import from Instapaper. poche needs a ./instapaper-export.html file
323 * @todo add the return value
324 * @return boolean
325 */
326 private function importFromInstapaper()
327 {
328 # TODO gestion des articles favs
329 $html = new simple_html_dom();
330 $html->load_file('./instapaper-export.html');
331 Tools::logm('starting import from instapaper');
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));
341 $this->action('add', $url, 0, TRUE);
342 if ($read == '1') {
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);
349 }
350 }
351
352 # the second <ol> is for read links
353 $read = 1;
354 }
355 $this->messages->add('s', _('import from instapaper completed'));
356 Tools::logm('import from instapaper completed');
357 Tools::redirect();
358 }
359
360 /**
361 * import from Pocket. poche needs a ./ril_export.html file
362 * @todo add the return value
363 * @return boolean
364 */
365 private function importFromPocket()
366 {
367 # TODO gestion des articles favs
368 $html = new simple_html_dom();
369 $html->load_file('./ril_export.html');
370 Tools::logm('starting import from pocket');
371
372 $read = 0;
373 $errors = array();
374 foreach($html->find('ul') as $ul)
375 {
376 foreach($ul->find('li') as $li)
377 {
378 $a = $li->find('a');
379 $url = new Url(base64_encode($a[0]->href));
380 $this->action('add', $url, 0, TRUE);
381 if ($read == '1') {
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);
388 }
389 }
390
391 # the second <ul> is for read links
392 $read = 1;
393 }
394 $this->messages->add('s', _('import from pocket completed'));
395 Tools::logm('import from pocket completed');
396 Tools::redirect();
397 }
398
399 /**
400 * import from Readability. poche needs a ./readability file
401 * @todo add the return value
402 * @return boolean
403 */
404 private function importFromReadability()
405 {
406 # TODO gestion des articles lus / favs
407 $str_data = file_get_contents("./readability");
408 $data = json_decode($str_data,true);
409 Tools::logm('starting import from Readability');
410
411 foreach ($data as $key => $value) {
412 $url = '';
413 foreach ($value as $attr => $attr_value) {
414 if ($attr == 'article__url') {
415 $url = new Url(base64_encode($attr_value));
416 }
417 $sequence = '';
418 if (STORAGE == 'postgres') {
419 $sequence = 'entries_id_seq';
420 }
421 // if ($attr_value == 'favorite' && $attr_value == 'true') {
422 // $last_id = $this->store->getLastId($sequence);
423 // $this->store->favoriteById($last_id);
424 // $this->action('toogle_fav', $url, $last_id, TRUE);
425 // }
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 }
430 }
431 if ($url->isCorrect())
432 $this->action('add', $url, 0, TRUE);
433 }
434 $this->messages->add('s', _('import from Readability completed'));
435 Tools::logm('import from Readability completed');
436 Tools::redirect();
437 }
438
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 */
445 public function import($from)
446 {
447 if ($from == 'pocket') {
448 return $this->importFromPocket();
449 }
450 else if ($from == 'readability') {
451 return $this->importFromReadability();
452 }
453 else if ($from == 'instapaper') {
454 return $this->importFromInstapaper();
455 }
456 }
457
458 /**
459 * export poche entries in json
460 * @return json all poche entries
461 */
462 public function export()
463 {
464 $entries = $this->store->retrieveAll($this->user->getId());
465 echo $this->tpl->render('export.twig', array(
466 'export' => Tools::renderJson($entries),
467 ));
468 Tools::logm('export view');
469 }
470
471 /**
472 * Checks online the latest version of poche and cache it
473 * @param string $which 'prod' or 'dev'
474 * @return string latest $which version
475 */
476 private function getPocheVersion($which = 'prod')
477 {
478 $cache_file = CACHE . '/' . $which;
479
480 # checks if the cached version file exists
481 if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) {
482 $version = file_get_contents($cache_file);
483 } else {
484 $version = file_get_contents('http://static.inthepoche.com/versions/' . $which);
485 file_put_contents($cache_file, $version, LOCK_EX);
486 }
487 return $version;
488 }
489 }