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