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