diff options
Diffstat (limited to 'src/Wallabag/Wallabag/Wallabag.php')
-rwxr-xr-x | src/Wallabag/Wallabag/Wallabag.php | 861 |
1 files changed, 861 insertions, 0 deletions
diff --git a/src/Wallabag/Wallabag/Wallabag.php b/src/Wallabag/Wallabag/Wallabag.php new file mode 100755 index 00000000..c172a95d --- /dev/null +++ b/src/Wallabag/Wallabag/Wallabag.php | |||
@@ -0,0 +1,861 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * wallabag, self hostable application allowing you to not miss any content anymore | ||
4 | * | ||
5 | * @category wallabag | ||
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | ||
7 | * @copyright 2013 | ||
8 | * @license http://opensource.org/licenses/MIT see COPYING file | ||
9 | */ | ||
10 | |||
11 | namespace Wallabag\Wallabag; | ||
12 | |||
13 | class Wallabag | ||
14 | { | ||
15 | /** | ||
16 | * @var User | ||
17 | */ | ||
18 | public $user; | ||
19 | /** | ||
20 | * @var Database | ||
21 | */ | ||
22 | public $store; | ||
23 | /** | ||
24 | * @var Template | ||
25 | */ | ||
26 | public $tpl; | ||
27 | /** | ||
28 | * @var Language | ||
29 | */ | ||
30 | public $language; | ||
31 | /** | ||
32 | * @var Routing | ||
33 | */ | ||
34 | public $routing; | ||
35 | /** | ||
36 | * @var Messages | ||
37 | */ | ||
38 | public $messages; | ||
39 | /** | ||
40 | * @var Paginator | ||
41 | */ | ||
42 | public $pagination; | ||
43 | |||
44 | public function __construct() | ||
45 | { | ||
46 | $this->init(); | ||
47 | } | ||
48 | |||
49 | private function init() | ||
50 | { | ||
51 | Tools::initPhp(); | ||
52 | |||
53 | $pocheUser = Session::getParam('poche_user'); | ||
54 | |||
55 | if ($pocheUser && $pocheUser != array()) { | ||
56 | $this->user = $pocheUser; | ||
57 | } else { | ||
58 | // fake user, just for install & login screens | ||
59 | $this->user = new User(); | ||
60 | $this->user->setConfig($this->getDefaultConfig()); | ||
61 | } | ||
62 | |||
63 | $this->pagination = new Paginator($this->user->getConfigValue('pager'), 'p'); | ||
64 | $this->language = new Language($this); | ||
65 | $this->tpl = new Template($this); | ||
66 | $this->store = new Database(); | ||
67 | $this->messages = new Messages(); | ||
68 | $this->routing = new Routing($this); | ||
69 | } | ||
70 | |||
71 | public function run() | ||
72 | { | ||
73 | $this->routing->run(); | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Creates a new user | ||
78 | */ | ||
79 | public function createNewUser($username, $password, $email = "") | ||
80 | { | ||
81 | if (!empty($username) && !empty($password)){ | ||
82 | $newUsername = filter_var($username, FILTER_SANITIZE_STRING); | ||
83 | $email = filter_var($email, FILTER_SANITIZE_STRING); | ||
84 | if (!$this->store->userExists($newUsername)){ | ||
85 | if ($this->store->install($newUsername, Tools::encodeString($password . $newUsername), $email)) { | ||
86 | Tools::logm('The new user ' . $newUsername . ' has been installed'); | ||
87 | $this->messages->add('s', sprintf(_('The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>'), $newUsername)); | ||
88 | Tools::redirect(); | ||
89 | } | ||
90 | else { | ||
91 | Tools::logm('error during adding new user'); | ||
92 | Tools::redirect(); | ||
93 | } | ||
94 | } | ||
95 | else { | ||
96 | $this->messages->add('e', sprintf(_('Error : An user with the name %s already exists !'), $newUsername)); | ||
97 | Tools::logm('An user with the name ' . $newUsername . ' already exists !'); | ||
98 | Tools::redirect(); | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Delete an existing user | ||
105 | */ | ||
106 | public function deleteUser($password) | ||
107 | { | ||
108 | if ($this->store->listUsers() > 1) { | ||
109 | if (Tools::encodeString($password . $this->user->getUsername()) == $this->store->getUserPassword($this->user->getId())) { | ||
110 | $username = $this->user->getUsername(); | ||
111 | $this->store->deleteUserConfig($this->user->getId()); | ||
112 | Tools::logm('The configuration for user '. $username .' has been deleted !'); | ||
113 | $this->store->deleteTagsEntriesAndEntries($this->user->getId()); | ||
114 | Tools::logm('The entries for user '. $username .' has been deleted !'); | ||
115 | $this->store->deleteUser($this->user->getId()); | ||
116 | Tools::logm('User '. $username .' has been completely deleted !'); | ||
117 | Session::logout(); | ||
118 | Tools::logm('logout'); | ||
119 | Tools::redirect(); | ||
120 | $this->messages->add('s', sprintf(_('User %s has been successfully deleted !'), $username)); | ||
121 | } | ||
122 | else { | ||
123 | Tools::logm('Bad password !'); | ||
124 | $this->messages->add('e', _('Error : The password is wrong !')); | ||
125 | } | ||
126 | } | ||
127 | else { | ||
128 | Tools::logm('Only user !'); | ||
129 | $this->messages->add('e', _('Error : You are the only user, you cannot delete your account !')); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | public function getDefaultConfig() | ||
134 | { | ||
135 | return array( | ||
136 | 'pager' => PAGINATION, | ||
137 | 'language' => LANG, | ||
138 | 'theme' => DEFAULT_THEME | ||
139 | ); | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * Call action (mark as fav, archive, delete, etc.) | ||
144 | */ | ||
145 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null) | ||
146 | { | ||
147 | switch ($action) | ||
148 | { | ||
149 | case 'add': | ||
150 | $content = Tools::getPageContent($url); | ||
151 | $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'); | ||
152 | $body = $content['rss']['channel']['item']['description']; | ||
153 | |||
154 | // clean content from prevent xss attack | ||
155 | $purifier = $this->_getPurifier(); | ||
156 | $title = $purifier->purify($title); | ||
157 | $body = $purifier->purify($body); | ||
158 | |||
159 | //search for possible duplicate | ||
160 | $duplicate = NULL; | ||
161 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); | ||
162 | |||
163 | $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); | ||
164 | if ( $last_id ) { | ||
165 | Tools::logm('add link ' . $url->getUrl()); | ||
166 | if (DOWNLOAD_PICTURES) { | ||
167 | $content = Picture::filterPicture($body, $url->getUrl(), $last_id); | ||
168 | Tools::logm('updating content article'); | ||
169 | $this->store->updateContent($last_id, $content, $this->user->getId()); | ||
170 | } | ||
171 | |||
172 | if ($duplicate != NULL) { | ||
173 | // duplicate exists, so, older entry needs to be deleted (as new entry should go to the top of list), BUT favorite mark and tags should be preserved | ||
174 | Tools::logm('link ' . $url->getUrl() . ' is a duplicate'); | ||
175 | // 1) - preserve tags and favorite, then drop old entry | ||
176 | $this->store->reassignTags($duplicate['id'], $last_id); | ||
177 | if ($duplicate['is_fav']) { | ||
178 | $this->store->favoriteById($last_id, $this->user->getId()); | ||
179 | } | ||
180 | if ($this->store->deleteById($duplicate['id'], $this->user->getId())) { | ||
181 | Tools::logm('previous link ' . $url->getUrl() .' entry deleted'); | ||
182 | } | ||
183 | } | ||
184 | |||
185 | // if there are tags, add them to the new article | ||
186 | if (isset($_GET['tags'])) { | ||
187 | $_POST['value'] = $_GET['tags']; | ||
188 | $_POST['entry_id'] = $last_id; | ||
189 | $this->action('add_tag', $url); | ||
190 | } | ||
191 | |||
192 | $this->messages->add('s', _('the link has been added successfully')); | ||
193 | } | ||
194 | else { | ||
195 | $this->messages->add('e', _('error during insertion : the link wasn\'t added')); | ||
196 | Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl()); | ||
197 | } | ||
198 | |||
199 | if ($autoclose == TRUE) { | ||
200 | Tools::redirect('?view=home'); | ||
201 | } else { | ||
202 | Tools::redirect('?view=home&closewin=true'); | ||
203 | } | ||
204 | break; | ||
205 | case 'delete': | ||
206 | if (isset($_GET['search'])) { | ||
207 | //when we want to apply a delete to a search | ||
208 | $tags = array($_GET['search']); | ||
209 | $allentry_ids = $this->store->search($tags[0], $this->user->getId()); | ||
210 | $entry_ids = array(); | ||
211 | foreach ($allentry_ids as $eachentry) { | ||
212 | $entry_ids[] = $eachentry[0]; | ||
213 | } | ||
214 | } else { // delete a single article | ||
215 | $entry_ids = array($id); | ||
216 | } | ||
217 | foreach($entry_ids as $id) { | ||
218 | $msg = 'delete link #' . $id; | ||
219 | if ($this->store->deleteById($id, $this->user->getId())) { | ||
220 | if (DOWNLOAD_PICTURES) { | ||
221 | Picture::removeDirectory(ABS_PATH . $id); | ||
222 | } | ||
223 | $this->messages->add('s', _('the link has been deleted successfully')); | ||
224 | } | ||
225 | else { | ||
226 | $this->messages->add('e', _('the link wasn\'t deleted')); | ||
227 | $msg = 'error : can\'t delete link #' . $id; | ||
228 | } | ||
229 | Tools::logm($msg); | ||
230 | } | ||
231 | Tools::redirect('?'); | ||
232 | break; | ||
233 | case 'toggle_fav' : | ||
234 | $this->store->favoriteById($id, $this->user->getId()); | ||
235 | Tools::logm('mark as favorite link #' . $id); | ||
236 | if ( Tools::isAjaxRequest() ) { | ||
237 | echo 1; | ||
238 | exit; | ||
239 | } | ||
240 | else { | ||
241 | Tools::redirect(); | ||
242 | } | ||
243 | break; | ||
244 | case 'toggle_archive' : | ||
245 | if (isset($_GET['tag_id'])) { | ||
246 | //when we want to archive a whole tag | ||
247 | $tag_id = $_GET['tag_id']; | ||
248 | $allentry_ids = $this->store->retrieveEntriesByTag($tag_id, $this->user->getId()); | ||
249 | $entry_ids = array(); | ||
250 | foreach ($allentry_ids as $eachentry) { | ||
251 | $entry_ids[] = $eachentry[0]; | ||
252 | } | ||
253 | } else { //archive a single article | ||
254 | $entry_ids = array($id); | ||
255 | } | ||
256 | foreach($entry_ids as $id) { | ||
257 | $this->store->archiveById($id, $this->user->getId()); | ||
258 | Tools::logm('archive link #' . $id); | ||
259 | } | ||
260 | if ( Tools::isAjaxRequest() ) { | ||
261 | echo 1; | ||
262 | exit; | ||
263 | } | ||
264 | else { | ||
265 | Tools::redirect(); | ||
266 | } | ||
267 | break; | ||
268 | case 'archive_all' : | ||
269 | $this->store->archiveAll($this->user->getId()); | ||
270 | Tools::logm('archive all links'); | ||
271 | Tools::redirect(); | ||
272 | break; | ||
273 | case 'add_tag' : | ||
274 | if (isset($_GET['search'])) { | ||
275 | //when we want to apply a tag to a search | ||
276 | $tags = array($_GET['search']); | ||
277 | $allentry_ids = $this->store->search($tags[0], $this->user->getId()); | ||
278 | $entry_ids = array(); | ||
279 | foreach ($allentry_ids as $eachentry) { | ||
280 | $entry_ids[] = $eachentry[0]; | ||
281 | } | ||
282 | } else { //add a tag to a single article | ||
283 | $tags = explode(',', $_POST['value']); | ||
284 | $entry_ids = array($_POST['entry_id']); | ||
285 | } | ||
286 | foreach($entry_ids as $entry_id) { | ||
287 | $entry = $this->store->retrieveOneById($entry_id, $this->user->getId()); | ||
288 | if (!$entry) { | ||
289 | $this->messages->add('e', _('Article not found!')); | ||
290 | Tools::logm('error : article not found'); | ||
291 | Tools::redirect(); | ||
292 | } | ||
293 | //get all already set tags to preven duplicates | ||
294 | $already_set_tags = array(); | ||
295 | $entry_tags = $this->store->retrieveTagsByEntry($entry_id); | ||
296 | foreach ($entry_tags as $tag) { | ||
297 | $already_set_tags[] = $tag['value']; | ||
298 | } | ||
299 | foreach($tags as $key => $tag_value) { | ||
300 | $value = trim($tag_value); | ||
301 | if ($value && !in_array($value, $already_set_tags)) { | ||
302 | $tag = $this->store->retrieveTagByValue($value); | ||
303 | if (is_null($tag)) { | ||
304 | # we create the tag | ||
305 | $tag = $this->store->createTag($value); | ||
306 | $sequence = ''; | ||
307 | if (STORAGE == 'postgres') { | ||
308 | $sequence = 'tags_id_seq'; | ||
309 | } | ||
310 | $tag_id = $this->store->getLastId($sequence); | ||
311 | } | ||
312 | else { | ||
313 | $tag_id = $tag['id']; | ||
314 | } | ||
315 | |||
316 | # we assign the tag to the article | ||
317 | $this->store->setTagToEntry($tag_id, $entry_id); | ||
318 | } | ||
319 | } | ||
320 | } | ||
321 | $this->messages->add('s', _('The tag has been applied successfully')); | ||
322 | Tools::logm('The tag has been applied successfully'); | ||
323 | Tools::redirect(); | ||
324 | break; | ||
325 | case 'remove_tag' : | ||
326 | $tag_id = $_GET['tag_id']; | ||
327 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | ||
328 | if (!$entry) { | ||
329 | $this->messages->add('e', _('Article not found!')); | ||
330 | Tools::logm('error : article not found'); | ||
331 | Tools::redirect(); | ||
332 | } | ||
333 | $this->store->removeTagForEntry($id, $tag_id); | ||
334 | Tools::logm('tag entry deleted'); | ||
335 | if ($this->store->cleanUnusedTag($tag_id)) { | ||
336 | Tools::logm('tag deleted'); | ||
337 | } | ||
338 | $this->messages->add('s', _('The tag has been successfully deleted')); | ||
339 | Tools::redirect(); | ||
340 | break; | ||
341 | default: | ||
342 | break; | ||
343 | } | ||
344 | } | ||
345 | |||
346 | function displayView($view, $id = 0) | ||
347 | { | ||
348 | $tpl_vars = array(); | ||
349 | |||
350 | switch ($view) | ||
351 | { | ||
352 | case 'about': | ||
353 | break; | ||
354 | case 'config': | ||
355 | $dev_infos = $this->_getPocheVersion('dev'); | ||
356 | $dev = trim($dev_infos[0]); | ||
357 | $check_time_dev = date('d-M-Y H:i', $dev_infos[1]); | ||
358 | $prod_infos = $this->_getPocheVersion('prod'); | ||
359 | $prod = trim($prod_infos[0]); | ||
360 | $check_time_prod = date('d-M-Y H:i', $prod_infos[1]); | ||
361 | $compare_dev = version_compare(POCHE, $dev); | ||
362 | $compare_prod = version_compare(POCHE, $prod); | ||
363 | $themes = $this->tpl->getInstalledThemes(); | ||
364 | $languages = $this->language->getInstalledLanguages(); | ||
365 | $token = $this->user->getConfigValue('token'); | ||
366 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; | ||
367 | $only_user = ($this->store->listUsers() > 1) ? false : true; | ||
368 | $tpl_vars = array( | ||
369 | 'themes' => $themes, | ||
370 | 'languages' => $languages, | ||
371 | 'dev' => $dev, | ||
372 | 'prod' => $prod, | ||
373 | 'check_time_dev' => $check_time_dev, | ||
374 | 'check_time_prod' => $check_time_prod, | ||
375 | 'compare_dev' => $compare_dev, | ||
376 | 'compare_prod' => $compare_prod, | ||
377 | 'token' => $token, | ||
378 | 'user_id' => $this->user->getId(), | ||
379 | 'http_auth' => $http_auth, | ||
380 | 'only_user' => $only_user | ||
381 | ); | ||
382 | Tools::logm('config view'); | ||
383 | break; | ||
384 | case 'edit-tags': | ||
385 | # tags | ||
386 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | ||
387 | if (!$entry) { | ||
388 | $this->messages->add('e', _('Article not found!')); | ||
389 | Tools::logm('error : article not found'); | ||
390 | Tools::redirect(); | ||
391 | } | ||
392 | $tags = $this->store->retrieveTagsByEntry($id); | ||
393 | $tpl_vars = array( | ||
394 | 'entry_id' => $id, | ||
395 | 'tags' => $tags, | ||
396 | 'entry' => $entry, | ||
397 | ); | ||
398 | break; | ||
399 | case 'tags': | ||
400 | $token = $this->user->getConfigValue('token'); | ||
401 | //if term is set - search tags for this term | ||
402 | $term = Tools::checkVar('term'); | ||
403 | $tags = $this->store->retrieveAllTags($this->user->getId(), $term); | ||
404 | if (Tools::isAjaxRequest()) { | ||
405 | $result = array(); | ||
406 | foreach ($tags as $tag) { | ||
407 | $result[] = $tag['value']; | ||
408 | } | ||
409 | echo json_encode($result); | ||
410 | exit; | ||
411 | } | ||
412 | $tpl_vars = array( | ||
413 | 'token' => $token, | ||
414 | 'user_id' => $this->user->getId(), | ||
415 | 'tags' => $tags, | ||
416 | ); | ||
417 | break; | ||
418 | case 'search': | ||
419 | if (isset($_GET['search'])) { | ||
420 | $search = filter_var($_GET['search'], FILTER_SANITIZE_STRING); | ||
421 | $tpl_vars['entries'] = $this->store->search($search, $this->user->getId()); | ||
422 | $count = count($tpl_vars['entries']); | ||
423 | $this->pagination->set_total($count); | ||
424 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), | ||
425 | $this->pagination->page_links('?view=' . $view . '?search=' . $search . '&sort=' . $_SESSION['sort'] . '&' )); | ||
426 | $tpl_vars['page_links'] = $page_links; | ||
427 | $tpl_vars['nb_results'] = $count; | ||
428 | $tpl_vars['searchterm'] = $search; | ||
429 | } | ||
430 | break; | ||
431 | case 'view': | ||
432 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); | ||
433 | if ($entry != NULL) { | ||
434 | Tools::logm('view link #' . $id); | ||
435 | $content = $entry['content']; | ||
436 | if (function_exists('tidy_parse_string')) { | ||
437 | $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); | ||
438 | $tidy->cleanRepair(); | ||
439 | $content = $tidy->value; | ||
440 | } | ||
441 | |||
442 | # flattr checking | ||
443 | $flattr = NULL; | ||
444 | if (FLATTR) { | ||
445 | $flattr = new FlattrItem(); | ||
446 | $flattr->checkItem($entry['url'], $entry['id']); | ||
447 | } | ||
448 | |||
449 | # tags | ||
450 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | ||
451 | |||
452 | $tpl_vars = array( | ||
453 | 'entry' => $entry, | ||
454 | 'content' => $content, | ||
455 | 'flattr' => $flattr, | ||
456 | 'tags' => $tags | ||
457 | ); | ||
458 | } | ||
459 | else { | ||
460 | Tools::logm('error in view call : entry is null'); | ||
461 | } | ||
462 | break; | ||
463 | default: # home, favorites, archive and tag views | ||
464 | $tpl_vars = array( | ||
465 | 'entries' => '', | ||
466 | 'page_links' => '', | ||
467 | 'nb_results' => '', | ||
468 | 'listmode' => (isset($_COOKIE['listmode']) ? true : false), | ||
469 | ); | ||
470 | |||
471 | //if id is given - we retrieve entries by tag: id is tag id | ||
472 | if ($id) { | ||
473 | $tpl_vars['tag'] = $this->store->retrieveTag($id, $this->user->getId()); | ||
474 | $tpl_vars['id'] = intval($id); | ||
475 | } | ||
476 | |||
477 | $count = $this->store->getEntriesByViewCount($view, $this->user->getId(), $id); | ||
478 | |||
479 | if ($count > 0) { | ||
480 | $this->pagination->set_total($count); | ||
481 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), | ||
482 | $this->pagination->page_links('?view=' . $view . '&sort=' . $_SESSION['sort'] . (($id)?'&id='.$id:'') . '&' )); | ||
483 | $tpl_vars['entries'] = $this->store->getEntriesByView($view, $this->user->getId(), $this->pagination->get_limit(), $id); | ||
484 | $tpl_vars['page_links'] = $page_links; | ||
485 | $tpl_vars['nb_results'] = $count; | ||
486 | } | ||
487 | Tools::logm('display ' . $view . ' view'); | ||
488 | break; | ||
489 | } | ||
490 | |||
491 | return $tpl_vars; | ||
492 | } | ||
493 | |||
494 | /** | ||
495 | * update the password of the current user. | ||
496 | * if MODE_DEMO is TRUE, the password can't be updated. | ||
497 | * @todo add the return value | ||
498 | * @todo set the new password in function header like this updatePassword($newPassword) | ||
499 | * @return boolean | ||
500 | */ | ||
501 | public function updatePassword($password, $confirmPassword) | ||
502 | { | ||
503 | if (MODE_DEMO) { | ||
504 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); | ||
505 | Tools::logm('in demo mode, you can\'t do this'); | ||
506 | Tools::redirect('?view=config'); | ||
507 | } | ||
508 | else { | ||
509 | if (isset($password) && isset($confirmPassword)) { | ||
510 | if ($password == $confirmPassword && !empty($password)) { | ||
511 | $this->messages->add('s', _('your password has been updated')); | ||
512 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($password . $this->user->getUsername())); | ||
513 | Session::logout(); | ||
514 | Tools::logm('password updated'); | ||
515 | Tools::redirect(); | ||
516 | } | ||
517 | else { | ||
518 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); | ||
519 | Tools::redirect('?view=config'); | ||
520 | } | ||
521 | } | ||
522 | } | ||
523 | } | ||
524 | |||
525 | /** | ||
526 | * Get credentials from differents sources | ||
527 | * It redirects the user to the $referer link | ||
528 | * | ||
529 | * @return array | ||
530 | */ | ||
531 | private function credentials() | ||
532 | { | ||
533 | if (isset($_SERVER['PHP_AUTH_USER'])) { | ||
534 | return array($_SERVER['PHP_AUTH_USER'], 'php_auth', true); | ||
535 | } | ||
536 | if (!empty($_POST['login']) && !empty($_POST['password'])) { | ||
537 | return array($_POST['login'], $_POST['password'], false); | ||
538 | } | ||
539 | if (isset($_SERVER['REMOTE_USER'])) { | ||
540 | return array($_SERVER['REMOTE_USER'], 'http_auth', true); | ||
541 | } | ||
542 | |||
543 | return array(false, false, false); | ||
544 | } | ||
545 | |||
546 | /** | ||
547 | * checks if login & password are correct and save the user in session. | ||
548 | * it redirects the user to the $referer link | ||
549 | * @param string $referer the url to redirect after login | ||
550 | * @todo add the return value | ||
551 | * @return boolean | ||
552 | */ | ||
553 | public function login($referer) | ||
554 | { | ||
555 | list($login,$password,$isauthenticated)=$this->credentials(); | ||
556 | if($login === false || $password === false) { | ||
557 | $this->messages->add('e', _('login failed: you have to fill all fields')); | ||
558 | Tools::logm('login failed'); | ||
559 | Tools::redirect(); | ||
560 | } | ||
561 | if (!empty($login) && !empty($password)) { | ||
562 | $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated); | ||
563 | if ($user != array()) { | ||
564 | # Save login into Session | ||
565 | $longlastingsession = isset($_POST['longlastingsession']); | ||
566 | $passwordTest = ($isauthenticated) ? $user['password'] : Tools::encodeString($password . $login); | ||
567 | Session::login($user['username'], $user['password'], $login, $passwordTest, $longlastingsession, array('poche_user' => new User($user))); | ||
568 | |||
569 | # reload l10n | ||
570 | $language = $user['config']['language']; | ||
571 | @putenv('LC_ALL=' . $language); | ||
572 | setlocale(LC_ALL, $language); | ||
573 | bindtextdomain($language, LOCALE); | ||
574 | textdomain($language); | ||
575 | |||
576 | $this->messages->add('s', _('welcome to your wallabag')); | ||
577 | Tools::logm('login successful'); | ||
578 | Tools::redirect($referer); | ||
579 | } | ||
580 | $this->messages->add('e', _('login failed: bad login or password')); | ||
581 | // log login failure in web server log to allow fail2ban usage | ||
582 | error_log('user '.$login.' authentication failure'); | ||
583 | Tools::logm('login failed'); | ||
584 | Tools::redirect(); | ||
585 | } | ||
586 | } | ||
587 | |||
588 | /** | ||
589 | * log out the poche user. It cleans the session. | ||
590 | * @todo add the return value | ||
591 | * @return boolean | ||
592 | */ | ||
593 | public function logout() | ||
594 | { | ||
595 | $this->user = array(); | ||
596 | Session::logout(); | ||
597 | Tools::logm('logout'); | ||
598 | Tools::redirect(); | ||
599 | } | ||
600 | |||
601 | /** | ||
602 | * import datas into your wallabag | ||
603 | * @return boolean | ||
604 | */ | ||
605 | |||
606 | public function import() { | ||
607 | |||
608 | if ( isset($_FILES['file']) && $_FILES['file']['tmp_name'] ) { | ||
609 | Tools::logm('Import stated: parsing file'); | ||
610 | |||
611 | // assume, that file is in json format | ||
612 | $str_data = file_get_contents($_FILES['file']['tmp_name']); | ||
613 | $data = json_decode($str_data, true); | ||
614 | |||
615 | if ( $data === null ) { | ||
616 | //not json - assume html | ||
617 | $html = new simple_html_dom(); | ||
618 | $html->load_file($_FILES['file']['tmp_name']); | ||
619 | $data = array(); | ||
620 | $read = 0; | ||
621 | foreach (array('ol','ul') as $list) { | ||
622 | foreach ($html->find($list) as $ul) { | ||
623 | foreach ($ul->find('li') as $li) { | ||
624 | $tmpEntry = array(); | ||
625 | $a = $li->find('a'); | ||
626 | $tmpEntry['url'] = $a[0]->href; | ||
627 | $tmpEntry['tags'] = $a[0]->tags; | ||
628 | $tmpEntry['is_read'] = $read; | ||
629 | if ($tmpEntry['url']) { | ||
630 | $data[] = $tmpEntry; | ||
631 | } | ||
632 | } | ||
633 | # the second <ol/ul> is for read links | ||
634 | $read = ((sizeof($data) && $read)?0:1); | ||
635 | } | ||
636 | } | ||
637 | } | ||
638 | |||
639 | // for readability structure | ||
640 | |||
641 | foreach($data as $record) { | ||
642 | if (is_array($record)) { | ||
643 | $data[] = $record; | ||
644 | foreach($record as $record2) { | ||
645 | if (is_array($record2)) { | ||
646 | $data[] = $record2; | ||
647 | } | ||
648 | } | ||
649 | } | ||
650 | } | ||
651 | |||
652 | $urlsInserted = array(); //urls of articles inserted | ||
653 | foreach($data as $record) { | ||
654 | $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : '')); | ||
655 | if ($url and !in_array($url, $urlsInserted)) { | ||
656 | $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>'); | ||
657 | $body = (isset($record['content']) ? $record['content'] : ''); | ||
658 | $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0)); | ||
659 | $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite']) ? intval($record['favorite']) : 0)); | ||
660 | |||
661 | // insert new record | ||
662 | |||
663 | $id = $this->store->add($url, $title, $body, $this->user->getId() , $isFavorite, $isRead); | ||
664 | if ($id) { | ||
665 | $urlsInserted[] = $url; //add | ||
666 | if (isset($record['tags']) && trim($record['tags'])) { | ||
667 | |||
668 | // @TODO: set tags | ||
669 | |||
670 | } | ||
671 | } | ||
672 | } | ||
673 | } | ||
674 | |||
675 | $i = sizeof($urlsInserted); | ||
676 | if ($i > 0) { | ||
677 | $this->messages->add('s', _('Articles inserted: ') . $i . _('. Please note, that some may be marked as "read".')); | ||
678 | } | ||
679 | |||
680 | Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).'); | ||
681 | } | ||
682 | else { | ||
683 | $this->messages->add('s', _('Did you forget to select a file?')); | ||
684 | } | ||
685 | // file parsing finished here | ||
686 | // now download article contents if any | ||
687 | // check if we need to download any content | ||
688 | |||
689 | $recordsDownloadRequired = $this->store->retrieveUnfetchedEntriesCount($this->user->getId()); | ||
690 | |||
691 | if ($recordsDownloadRequired == 0) { | ||
692 | |||
693 | // nothing to download | ||
694 | |||
695 | $this->messages->add('s', _('Import finished.')); | ||
696 | Tools::logm('Import finished completely'); | ||
697 | Tools::redirect(); | ||
698 | } | ||
699 | else { | ||
700 | |||
701 | // if just inserted - don't download anything, download will start in next reload | ||
702 | |||
703 | if (!isset($_FILES['file'])) { | ||
704 | |||
705 | // download next batch | ||
706 | |||
707 | Tools::logm('Fetching next batch of articles...'); | ||
708 | $items = $this->store->retrieveUnfetchedEntries($this->user->getId() , IMPORT_LIMIT); | ||
709 | $purifier = $this->_getPurifier(); | ||
710 | foreach($items as $item) { | ||
711 | $url = new Url(base64_encode($item['url'])); | ||
712 | Tools::logm('Fetching article ' . $item['id']); | ||
713 | $content = Tools::getPageContent($url); | ||
714 | $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled')); | ||
715 | $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined')); | ||
716 | |||
717 | // clean content to prevent xss attack | ||
718 | |||
719 | $title = $purifier->purify($title); | ||
720 | $body = $purifier->purify($body); | ||
721 | $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId()); | ||
722 | Tools::logm('Article ' . $item['id'] . ' updated.'); | ||
723 | } | ||
724 | } | ||
725 | } | ||
726 | |||
727 | return array( | ||
728 | 'includeImport' => true, | ||
729 | 'import' => array( | ||
730 | 'recordsDownloadRequired' => $recordsDownloadRequired, | ||
731 | 'recordsUnderDownload' => IMPORT_LIMIT, | ||
732 | 'delay' => IMPORT_DELAY * 1000 | ||
733 | ) | ||
734 | ); | ||
735 | } | ||
736 | |||
737 | /** | ||
738 | * export poche entries in json | ||
739 | * @return json all poche entries | ||
740 | */ | ||
741 | public function export() | ||
742 | { | ||
743 | $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; | ||
744 | header('Content-Disposition: attachment; filename='.$filename); | ||
745 | |||
746 | $entries = $this->store->retrieveAll($this->user->getId()); | ||
747 | echo $this->tpl->render('export.twig', array( | ||
748 | 'export' => Tools::renderJson($entries), | ||
749 | )); | ||
750 | Tools::logm('export view'); | ||
751 | } | ||
752 | |||
753 | /** | ||
754 | * Checks online the latest version of poche and cache it | ||
755 | * @param string $which 'prod' or 'dev' | ||
756 | * @return string latest $which version | ||
757 | */ | ||
758 | private function _getPocheVersion($which = 'prod') { | ||
759 | $cache_file = CACHE . '/' . $which; | ||
760 | $check_time = time(); | ||
761 | |||
762 | # checks if the cached version file exists | ||
763 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | ||
764 | $version = file_get_contents($cache_file); | ||
765 | $check_time = filemtime($cache_file); | ||
766 | } else { | ||
767 | $version = file_get_contents('http://static.wallabag.org/versions/' . $which); | ||
768 | file_put_contents($cache_file, $version, LOCK_EX); | ||
769 | } | ||
770 | return array($version, $check_time); | ||
771 | } | ||
772 | |||
773 | /** | ||
774 | * Update token for current user | ||
775 | */ | ||
776 | public function updateToken() | ||
777 | { | ||
778 | $token = Tools::generateToken(); | ||
779 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | ||
780 | $currentConfig = $_SESSION['poche_user']->config; | ||
781 | $currentConfig['token'] = $token; | ||
782 | $_SESSION['poche_user']->setConfig($currentConfig); | ||
783 | Tools::redirect(); | ||
784 | } | ||
785 | |||
786 | /** | ||
787 | * Generate RSS feeds for current user | ||
788 | * | ||
789 | * @param $token | ||
790 | * @param $user_id | ||
791 | * @param $tag_id if $type is 'tag', the id of the tag to generate feed for | ||
792 | * @param string $type the type of feed to generate | ||
793 | * @param int $limit the maximum number of items (0 means all) | ||
794 | */ | ||
795 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home', $limit = 0) | ||
796 | { | ||
797 | $allowed_types = array('home', 'fav', 'archive', 'tag'); | ||
798 | $config = $this->store->getConfigUser($user_id); | ||
799 | |||
800 | if ($config == null) { | ||
801 | die(sprintf(_('User with this id (%d) does not exist.'), $user_id)); | ||
802 | } | ||
803 | |||
804 | if (!in_array($type, $allowed_types) || !isset($config['token']) || $token != $config['token']) { | ||
805 | die(_('Uh, there is a problem while generating feed. Wrong token used?')); | ||
806 | } | ||
807 | |||
808 | $feed = new FeedWriter(RSS2); | ||
809 | $feed->setTitle('wallabag — ' . $type . ' feed'); | ||
810 | $feed->setLink(Tools::getPocheUrl()); | ||
811 | $feed->setChannelElement('pubDate', date(DATE_RSS , time())); | ||
812 | $feed->setChannelElement('generator', 'wallabag'); | ||
813 | $feed->setDescription('wallabag ' . $type . ' elements'); | ||
814 | |||
815 | if ($type == 'tag') { | ||
816 | $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); | ||
817 | } | ||
818 | else { | ||
819 | $entries = $this->store->getEntriesByView($type, $user_id); | ||
820 | } | ||
821 | |||
822 | // if $limit is set to zero, use all entries | ||
823 | if (0 == $limit) { | ||
824 | $limit = count($entries); | ||
825 | } | ||
826 | if (count($entries) > 0) { | ||
827 | for ($i = 0; $i < min(count($entries), $limit); $i++) { | ||
828 | $entry = $entries[$i]; | ||
829 | $newItem = $feed->createNewItem(); | ||
830 | $newItem->setTitle($entry['title']); | ||
831 | $newItem->setSource(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); | ||
832 | $newItem->setLink($entry['url']); | ||
833 | $newItem->setDate(time()); | ||
834 | $newItem->setDescription($entry['content']); | ||
835 | $feed->addItem($newItem); | ||
836 | } | ||
837 | } | ||
838 | |||
839 | $feed->genarateFeed(); | ||
840 | exit; | ||
841 | } | ||
842 | |||
843 | |||
844 | |||
845 | /** | ||
846 | * Returns new purifier object with actual config | ||
847 | */ | ||
848 | private function _getPurifier() | ||
849 | { | ||
850 | $config = HTMLPurifier_Config::createDefault(); | ||
851 | $config->set('Cache.SerializerPath', CACHE); | ||
852 | $config->set('HTML.SafeIframe', true); | ||
853 | |||
854 | //allow YouTube, Vimeo and dailymotion videos | ||
855 | $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%'); | ||
856 | |||
857 | return new HTMLPurifier($config); | ||
858 | } | ||
859 | |||
860 | |||
861 | } | ||