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