]>
Commit | Line | Data |
---|---|---|
eb1af592 NL |
1 | <?php |
2 | /** | |
c95b78a8 | 3 | * wallabag, self hostable application allowing you to not miss any content anymore |
eb1af592 | 4 | * |
c95b78a8 NL |
5 | * @category wallabag |
6 | * @author Nicolas Lœuillet <nicolas@loeuillet.org> | |
eb1af592 | 7 | * @copyright 2013 |
3602405e | 8 | * @license http://opensource.org/licenses/MIT see COPYING file |
eb1af592 NL |
9 | */ |
10 | ||
11 | class Poche | |
12 | { | |
3602405e NL |
13 | /** |
14 | * @var User | |
15 | */ | |
7ce7ec4c | 16 | public $user; |
3602405e NL |
17 | /** |
18 | * @var Database | |
19 | */ | |
eb1af592 | 20 | public $store; |
3602405e NL |
21 | /** |
22 | * @var Template | |
23 | */ | |
eb1af592 | 24 | public $tpl; |
3602405e NL |
25 | /** |
26 | * @var Language | |
27 | */ | |
28 | public $language; | |
29 | /** | |
30 | * @var Routing | |
31 | */ | |
32 | public $routing; | |
33 | /** | |
34 | * @var Messages | |
35 | */ | |
55821e04 | 36 | public $messages; |
3602405e NL |
37 | /** |
38 | * @var Paginator | |
39 | */ | |
6a361945 | 40 | public $pagination; |
182faf26 | 41 | |
00dbaf90 | 42 | public function __construct() |
eb1af592 | 43 | { |
3602405e | 44 | $this->init(); |
eb1af592 | 45 | } |
182faf26 MR |
46 | |
47 | private function init() | |
00dbaf90 NL |
48 | { |
49 | Tools::initPhp(); | |
eb1af592 | 50 | |
3602405e NL |
51 | $pocheUser = Session::getParam('poche_user'); |
52 | ||
53 | if ($pocheUser && $pocheUser != array()) { | |
54 | $this->user = $pocheUser; | |
00dbaf90 | 55 | } else { |
3602405e | 56 | // fake user, just for install & login screens |
00dbaf90 NL |
57 | $this->user = new User(); |
58 | $this->user->setConfig($this->getDefaultConfig()); | |
59 | } | |
60 | ||
3602405e NL |
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); | |
00dbaf90 | 67 | } |
182faf26 | 68 | |
3602405e NL |
69 | public function run() |
70 | { | |
71 | $this->routing->run(); | |
00dbaf90 | 72 | } |
182faf26 | 73 | |
4a291288 | 74 | /** |
3602405e | 75 | * Creates a new user |
4a291288 | 76 | */ |
2f26729c | 77 | public function createNewUser($username, $password) |
eb1af592 | 78 | { |
2f26729c NL |
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(); | |
4d99bae8 | 86 | } |
87 | else { | |
2f26729c | 88 | Tools::logm('error during adding new user'); |
4d99bae8 | 89 | Tools::redirect(); |
90 | } | |
91 | } | |
2f26729c NL |
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 | } | |
4d99bae8 | 97 | } |
98 | } | |
b6413975 | 99 | |
3602405e NL |
100 | /** |
101 | * Delete an existing user | |
102 | */ | |
2f26729c | 103 | public function deleteUser($password) |
3602405e | 104 | { |
2f26729c NL |
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)); | |
4d99bae8 | 118 | } |
119 | else { | |
2f26729c NL |
120 | Tools::logm('Bad password !'); |
121 | $this->messages->add('e', _('Error : The password is wrong !')); | |
4d99bae8 | 122 | } |
123 | } | |
2f26729c NL |
124 | else { |
125 | Tools::logm('Only user !'); | |
126 | $this->messages->add('e', _('Error : You are the only user, you cannot delete your account !')); | |
127 | } | |
4d99bae8 | 128 | } |
eb1af592 | 129 | |
8d3275be | 130 | public function getDefaultConfig() |
182faf26 | 131 | { |
8d3275be NL |
132 | return array( |
133 | 'pager' => PAGINATION, | |
134 | 'language' => LANG, | |
00dbaf90 NL |
135 | 'theme' => DEFAULT_THEME |
136 | ); | |
8d3275be NL |
137 | } |
138 | ||
eb1af592 NL |
139 | /** |
140 | * Call action (mark as fav, archive, delete, etc.) | |
141 | */ | |
926acd7b | 142 | public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null) |
eb1af592 NL |
143 | { |
144 | switch ($action) | |
145 | { | |
146 | case 'add': | |
a297fb1e MR |
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 | |
2f26729c | 152 | $purifier = $this->_getPurifier(); |
a297fb1e MR |
153 | $title = $purifier->purify($title); |
154 | $body = $purifier->purify($body); | |
1570a653 | 155 | |
182faf26 | 156 | //search for possible duplicate |
8d7cd2cc | 157 | $duplicate = NULL; |
a297fb1e | 158 | $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId()); |
488fc63b | 159 | |
182faf26 | 160 | $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId()); |
a297fb1e | 161 | if ( $last_id ) { |
ec397236 | 162 | Tools::logm('add link ' . $url->getUrl()); |
ec397236 | 163 | if (DOWNLOAD_PICTURES) { |
3602405e | 164 | $content = Picture::filterPicture($body, $url->getUrl(), $last_id); |
ec397236 NL |
165 | Tools::logm('updating content article'); |
166 | $this->store->updateContent($last_id, $content, $this->user->getId()); | |
167 | } | |
488fc63b MR |
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 | ||
182faf26 | 182 | $this->messages->add('s', _('the link has been added successfully')); |
eb1af592 NL |
183 | } |
184 | else { | |
a297fb1e MR |
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()); | |
b916bcfc | 187 | } |
ec397236 | 188 | |
a297fb1e MR |
189 | if ($autoclose == TRUE) { |
190 | Tools::redirect('?view=home'); | |
191 | } else { | |
192 | Tools::redirect('?view=home&closewin=true'); | |
eb1af592 NL |
193 | } |
194 | break; | |
195 | case 'delete': | |
bc1ee852 | 196 | $msg = 'delete link #' . $id; |
8d3275be | 197 | if ($this->store->deleteById($id, $this->user->getId())) { |
eb1af592 | 198 | if (DOWNLOAD_PICTURES) { |
3602405e | 199 | Picture::removeDirectory(ABS_PATH . $id); |
eb1af592 | 200 | } |
6a361945 | 201 | $this->messages->add('s', _('the link has been deleted successfully')); |
eb1af592 NL |
202 | } |
203 | else { | |
6a361945 | 204 | $this->messages->add('e', _('the link wasn\'t deleted')); |
bc1ee852 | 205 | $msg = 'error : can\'t delete link #' . $id; |
eb1af592 | 206 | } |
bc1ee852 | 207 | Tools::logm($msg); |
985ce3ec | 208 | Tools::redirect('?'); |
eb1af592 NL |
209 | break; |
210 | case 'toggle_fav' : | |
8d3275be | 211 | $this->store->favoriteById($id, $this->user->getId()); |
eb1af592 | 212 | Tools::logm('mark as favorite link #' . $id); |
c2cf7075 MR |
213 | if ( Tools::isAjaxRequest() ) { |
214 | echo 1; | |
215 | exit; | |
216 | } | |
217 | else { | |
218 | Tools::redirect(); | |
219 | } | |
eb1af592 NL |
220 | break; |
221 | case 'toggle_archive' : | |
8d3275be | 222 | $this->store->archiveById($id, $this->user->getId()); |
eb1af592 | 223 | Tools::logm('archive link #' . $id); |
c2cf7075 MR |
224 | if ( Tools::isAjaxRequest() ) { |
225 | echo 1; | |
226 | exit; | |
227 | } | |
228 | else { | |
229 | Tools::redirect(); | |
230 | } | |
eb1af592 | 231 | break; |
f14807de NL |
232 | case 'archive_all' : |
233 | $this->store->archiveAll($this->user->getId()); | |
234 | Tools::logm('archive all links'); | |
a297fb1e | 235 | Tools::redirect(); |
f14807de | 236 | break; |
c432fa16 | 237 | case 'add_tag' : |
decc23aa | 238 | if (isset($_GET['search'])) { |
239 | //when we want to apply a tag to a search | |
decc23aa | 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']); | |
fb26cc93 | 249 | } |
decc23aa | 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); | |
fb26cc93 | 275 | } |
decc23aa | 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 | } | |
c432fa16 | 283 | } |
c432fa16 | 284 | } |
9c743ab9 | 285 | $this->messages->add('s', _('The tag has been applied successfully')); |
24696800 | 286 | Tools::logm('The tag has been applied successfully'); |
a297fb1e | 287 | Tools::redirect(); |
c432fa16 NL |
288 | break; |
289 | case 'remove_tag' : | |
290 | $tag_id = $_GET['tag_id']; | |
b89d5a2b NL |
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 | } | |
c432fa16 | 297 | $this->store->removeTagForEntry($id, $tag_id); |
9c743ab9 | 298 | Tools::logm('tag entry deleted'); |
24696800 | 299 | if ($this->store->cleanUnusedTag($tag_id)) { |
300 | Tools::logm('tag deleted'); | |
301 | } | |
9c743ab9 | 302 | $this->messages->add('s', _('The tag has been successfully deleted')); |
c432fa16 NL |
303 | Tools::redirect(); |
304 | break; | |
eb1af592 NL |
305 | default: |
306 | break; | |
307 | } | |
308 | } | |
309 | ||
310 | function displayView($view, $id = 0) | |
311 | { | |
312 | $tpl_vars = array(); | |
313 | ||
314 | switch ($view) | |
315 | { | |
eb1af592 | 316 | case 'config': |
2f26729c | 317 | $dev_infos = $this->_getPocheVersion('dev'); |
11c680f9 NL |
318 | $dev = trim($dev_infos[0]); |
319 | $check_time_dev = date('d-M-Y H:i', $dev_infos[1]); | |
2f26729c | 320 | $prod_infos = $this->_getPocheVersion('prod'); |
11c680f9 NL |
321 | $prod = trim($prod_infos[0]); |
322 | $check_time_prod = date('d-M-Y H:i', $prod_infos[1]); | |
031df528 NL |
323 | $compare_dev = version_compare(POCHE, $dev); |
324 | $compare_prod = version_compare(POCHE, $prod); | |
3602405e NL |
325 | $themes = $this->tpl->getInstalledThemes(); |
326 | $languages = $this->language->getInstalledLanguages(); | |
72c20a52 | 327 | $token = $this->user->getConfigValue('token'); |
1810c13b | 328 | $http_auth = (isset($_SERVER['PHP_AUTH_USER']) || isset($_SERVER['REMOTE_USER'])) ? true : false; |
4d99bae8 | 329 | $only_user = ($this->store->listUsers() > 1) ? false : true; |
32520785 | 330 | $tpl_vars = array( |
00dbaf90 | 331 | 'themes' => $themes, |
5011388f | 332 | 'languages' => $languages, |
32520785 NL |
333 | 'dev' => $dev, |
334 | 'prod' => $prod, | |
11c680f9 NL |
335 | 'check_time_dev' => $check_time_dev, |
336 | 'check_time_prod' => $check_time_prod, | |
32520785 NL |
337 | 'compare_dev' => $compare_dev, |
338 | 'compare_prod' => $compare_prod, | |
72c20a52 NL |
339 | 'token' => $token, |
340 | 'user_id' => $this->user->getId(), | |
df6afaf0 | 341 | 'http_auth' => $http_auth, |
4d99bae8 | 342 | 'only_user' => $only_user |
32520785 | 343 | ); |
eb1af592 NL |
344 | Tools::logm('config view'); |
345 | break; | |
6cab59c3 NL |
346 | case 'edit-tags': |
347 | # tags | |
b89d5a2b NL |
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 | } | |
6cab59c3 NL |
354 | $tags = $this->store->retrieveTagsByEntry($id); |
355 | $tpl_vars = array( | |
c432fa16 | 356 | 'entry_id' => $id, |
6cab59c3 | 357 | 'tags' => $tags, |
032e0ca1 | 358 | 'entry' => $entry, |
4886ed6d NL |
359 | ); |
360 | break; | |
2e2ebe5e | 361 | case 'tags': |
f778e472 | 362 | $token = $this->user->getConfigValue('token'); |
fb26cc93 MR |
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 | } | |
2e2ebe5e | 374 | $tpl_vars = array( |
f778e472 NL |
375 | 'token' => $token, |
376 | 'user_id' => $this->user->getId(), | |
2e2ebe5e NL |
377 | 'tags' => $tags, |
378 | ); | |
379 | break; | |
a4585f7e MR |
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; | |
eb1af592 | 393 | case 'view': |
8d3275be | 394 | $entry = $this->store->retrieveOneById($id, $this->user->getId()); |
eb1af592 NL |
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; | |
3408ed48 | 402 | } |
a3223127 | 403 | |
3408ed48 NL |
404 | # flattr checking |
405 | $flattr = new FlattrItem(); | |
7b171c73 NL |
406 | $flattr->checkItem($entry['url'], $entry['id']); |
407 | ||
408 | # tags | |
409 | $tags = $this->store->retrieveTagsByEntry($entry['id']); | |
a3223127 | 410 | |
3408ed48 | 411 | $tpl_vars = array( |
7b171c73 NL |
412 | 'entry' => $entry, |
413 | 'content' => $content, | |
414 | 'flattr' => $flattr, | |
415 | 'tags' => $tags | |
3408ed48 | 416 | ); |
eb1af592 NL |
417 | } |
418 | else { | |
d8d1542e | 419 | Tools::logm('error in view call : entry is null'); |
eb1af592 NL |
420 | } |
421 | break; | |
032e0ca1 | 422 | default: # home, favorites, archive and tag views |
eb1af592 | 423 | $tpl_vars = array( |
3eb04903 N |
424 | 'entries' => '', |
425 | 'page_links' => '', | |
7f9f5281 | 426 | 'nb_results' => '', |
6065553c | 427 | 'listmode' => (isset($_COOKIE['listmode']) ? true : false), |
eb1af592 | 428 | ); |
182faf26 | 429 | |
3602405e | 430 | //if id is given - we retrieve entries by tag: id is tag id |
032e0ca1 MR |
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); | |
c515ffec | 440 | $page_links = str_replace(array('previous', 'next'), array(_('previous'), _('next')), |
032e0ca1 MR |
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); | |
3eb04903 | 443 | $tpl_vars['page_links'] = $page_links; |
032e0ca1 | 444 | $tpl_vars['nb_results'] = $count; |
3eb04903 | 445 | } |
6a361945 | 446 | Tools::logm('display ' . $view . ' view'); |
eb1af592 NL |
447 | break; |
448 | } | |
449 | ||
450 | return $tpl_vars; | |
451 | } | |
c765c367 | 452 | |
07ee09f4 | 453 | /** |
182faf26 MR |
454 | * update the password of the current user. |
455 | * if MODE_DEMO is TRUE, the password can't be updated. | |
07ee09f4 NL |
456 | * @todo add the return value |
457 | * @todo set the new password in function header like this updatePassword($newPassword) | |
458 | * @return boolean | |
459 | */ | |
2f26729c | 460 | public function updatePassword($password, $confirmPassword) |
c765c367 | 461 | { |
55821e04 | 462 | if (MODE_DEMO) { |
8d3275be | 463 | $this->messages->add('i', _('in demo mode, you can\'t update your password')); |
55821e04 | 464 | Tools::logm('in demo mode, you can\'t do this'); |
6a361945 | 465 | Tools::redirect('?view=config'); |
55821e04 NL |
466 | } |
467 | else { | |
2f26729c NL |
468 | if (isset($password) && isset($confirmPassword)) { |
469 | if ($password == $confirmPassword && !empty($password)) { | |
8d3275be | 470 | $this->messages->add('s', _('your password has been updated')); |
2f26729c | 471 | $this->store->updatePassword($this->user->getId(), Tools::encodeString($password . $this->user->getUsername())); |
c765c367 | 472 | Session::logout(); |
8d3275be | 473 | Tools::logm('password updated'); |
c765c367 NL |
474 | Tools::redirect(); |
475 | } | |
476 | else { | |
8d3275be | 477 | $this->messages->add('e', _('the two fields have to be filled & the password must be the same in the two fields')); |
6a361945 | 478 | Tools::redirect('?view=config'); |
c765c367 NL |
479 | } |
480 | } | |
481 | } | |
482 | } | |
182faf26 | 483 | |
df6afaf0 | 484 | /** |
2f26729c NL |
485 | * Get credentials from differents sources |
486 | * It redirects the user to the $referer link | |
487 | * | |
df6afaf0 DS |
488 | * @return array |
489 | */ | |
2f26729c NL |
490 | private function credentials() |
491 | { | |
492 | if (isset($_SERVER['PHP_AUTH_USER'])) { | |
493 | return array($_SERVER['PHP_AUTH_USER'], 'php_auth', true); | |
1810c13b | 494 | } |
2f26729c NL |
495 | if (!empty($_POST['login']) && !empty($_POST['password'])) { |
496 | return array($_POST['login'], $_POST['password'], false); | |
1810c13b | 497 | } |
2f26729c NL |
498 | if (isset($_SERVER['REMOTE_USER'])) { |
499 | return array($_SERVER['REMOTE_USER'], 'http_auth', true); | |
1810c13b | 500 | } |
5cfafc61 | 501 | |
2f26729c | 502 | return array(false, false, false); |
6af66b11 | 503 | } |
df6afaf0 | 504 | |
07ee09f4 NL |
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 | */ | |
c765c367 NL |
512 | public function login($referer) |
513 | { | |
6af66b11 | 514 | list($login,$password,$isauthenticated)=$this->credentials(); |
df6afaf0 DS |
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)) { | |
6af66b11 | 521 | $user = $this->store->login($login, Tools::encodeString($password . $login), $isauthenticated); |
7ce7ec4c NL |
522 | if ($user != array()) { |
523 | # Save login into Session | |
6af66b11 MR |
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))); | |
9cf6bac1 NL |
527 | |
528 | # reload l10n | |
529 | $language = $user['config']['language']; | |
530 | @putenv('LC_ALL=' . $language); | |
531 | setlocale(LC_ALL, $language); | |
532 | bindtextdomain($language, LOCALE); | |
533 | textdomain($language); | |
534 | ||
26929c08 | 535 | $this->messages->add('s', _('welcome to your wallabag')); |
8d3275be | 536 | Tools::logm('login successful'); |
c765c367 NL |
537 | Tools::redirect($referer); |
538 | } | |
8d3275be | 539 | $this->messages->add('e', _('login failed: bad login or password')); |
c765c367 NL |
540 | Tools::logm('login failed'); |
541 | Tools::redirect(); | |
c765c367 NL |
542 | } |
543 | } | |
544 | ||
07ee09f4 NL |
545 | /** |
546 | * log out the poche user. It cleans the session. | |
547 | * @todo add the return value | |
182faf26 | 548 | * @return boolean |
07ee09f4 | 549 | */ |
c765c367 NL |
550 | public function logout() |
551 | { | |
7ce7ec4c | 552 | $this->user = array(); |
c765c367 | 553 | Session::logout(); |
b916bcfc | 554 | Tools::logm('logout'); |
c765c367 NL |
555 | Tools::redirect(); |
556 | } | |
557 | ||
07ee09f4 | 558 | /** |
2f26729c | 559 | * import datas into your wallabag |
182faf26 | 560 | * @return boolean |
07ee09f4 | 561 | */ |
2f26729c | 562 | |
182faf26 MR |
563 | public function import() { |
564 | ||
dc764892 | 565 | if ( isset($_FILES['file']) && $_FILES['file']['tmp_name'] ) { |
5ce39784 MR |
566 | Tools::logm('Import stated: parsing file'); |
567 | ||
182faf26 MR |
568 | // assume, that file is in json format |
569 | $str_data = file_get_contents($_FILES['file']['tmp_name']); | |
570 | $data = json_decode($str_data, true); | |
571 | ||
572 | if ( $data === null ) { | |
573 | //not json - assume html | |
574 | $html = new simple_html_dom(); | |
575 | $html->load_file($_FILES['file']['tmp_name']); | |
576 | $data = array(); | |
577 | $read = 0; | |
578 | foreach (array('ol','ul') as $list) { | |
579 | foreach ($html->find($list) as $ul) { | |
86da3988 MR |
580 | foreach ($ul->find('li') as $li) { |
581 | $tmpEntry = array(); | |
a8ef1f3f MR |
582 | $a = $li->find('a'); |
583 | $tmpEntry['url'] = $a[0]->href; | |
584 | $tmpEntry['tags'] = $a[0]->tags; | |
585 | $tmpEntry['is_read'] = $read; | |
586 | if ($tmpEntry['url']) { | |
587 | $data[] = $tmpEntry; | |
588 | } | |
86da3988 MR |
589 | } |
590 | # the second <ol/ul> is for read links | |
591 | $read = ((sizeof($data) && $read)?0:1); | |
182faf26 MR |
592 | } |
593 | } | |
1daa8e4a | 594 | } |
182faf26 | 595 | |
2f26729c NL |
596 | // for readability structure |
597 | ||
598 | foreach($data as $record) { | |
599 | if (is_array($record)) { | |
600 | $data[] = $record; | |
601 | foreach($record as $record2) { | |
602 | if (is_array($record2)) { | |
603 | $data[] = $record2; | |
604 | } | |
605 | } | |
606 | } | |
a297fb1e | 607 | } |
a297fb1e | 608 | |
2f26729c NL |
609 | $urlsInserted = array(); //urls of articles inserted |
610 | foreach($data as $record) { | |
611 | $url = trim(isset($record['article__url']) ? $record['article__url'] : (isset($record['url']) ? $record['url'] : '')); | |
612 | if ($url and !in_array($url, $urlsInserted)) { | |
613 | $title = (isset($record['title']) ? $record['title'] : _('Untitled - Import - ') . '</a> <a href="./?import">' . _('click to finish import') . '</a><a>'); | |
614 | $body = (isset($record['content']) ? $record['content'] : ''); | |
615 | $isRead = (isset($record['is_read']) ? intval($record['is_read']) : (isset($record['archive']) ? intval($record['archive']) : 0)); | |
616 | $isFavorite = (isset($record['is_fav']) ? intval($record['is_fav']) : (isset($record['favorite']) ? intval($record['favorite']) : 0)); | |
617 | ||
618 | // insert new record | |
619 | ||
620 | $id = $this->store->add($url, $title, $body, $this->user->getId() , $isFavorite, $isRead); | |
621 | if ($id) { | |
622 | $urlsInserted[] = $url; //add | |
623 | if (isset($record['tags']) && trim($record['tags'])) { | |
624 | ||
625 | // @TODO: set tags | |
626 | ||
627 | } | |
628 | } | |
629 | } | |
182faf26 | 630 | } |
182faf26 | 631 | |
2f26729c NL |
632 | $i = sizeof($urlsInserted); |
633 | if ($i > 0) { | |
634 | $this->messages->add('s', _('Articles inserted: ') . $i . _('. Please note, that some may be marked as "read".')); | |
635 | } | |
636 | ||
5ce39784 | 637 | Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).'); |
182faf26 | 638 | } |
dc764892 MR |
639 | else { |
640 | $this->messages->add('s', _('Did you forget to select a file?')); | |
641 | } | |
2f26729c NL |
642 | // file parsing finished here |
643 | // now download article contents if any | |
644 | // check if we need to download any content | |
182faf26 | 645 | |
2f26729c NL |
646 | $recordsDownloadRequired = $this->store->retrieveUnfetchedEntriesCount($this->user->getId()); |
647 | ||
648 | if ($recordsDownloadRequired == 0) { | |
182faf26 | 649 | |
2f26729c | 650 | // nothing to download |
182faf26 | 651 | |
2f26729c NL |
652 | $this->messages->add('s', _('Import finished.')); |
653 | Tools::logm('Import finished completely'); | |
654 | Tools::redirect(); | |
655 | } | |
656 | else { | |
182faf26 | 657 | |
2f26729c | 658 | // if just inserted - don't download anything, download will start in next reload |
182faf26 | 659 | |
2f26729c | 660 | if (!isset($_FILES['file'])) { |
182faf26 | 661 | |
2f26729c | 662 | // download next batch |
182faf26 | 663 | |
2f26729c NL |
664 | Tools::logm('Fetching next batch of articles...'); |
665 | $items = $this->store->retrieveUnfetchedEntries($this->user->getId() , IMPORT_LIMIT); | |
666 | $purifier = $this->_getPurifier(); | |
667 | foreach($items as $item) { | |
668 | $url = new Url(base64_encode($item['url'])); | |
669 | Tools::logm('Fetching article ' . $item['id']); | |
670 | $content = Tools::getPageContent($url); | |
671 | $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled')); | |
672 | $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined')); | |
673 | ||
674 | // clean content to prevent xss attack | |
675 | ||
676 | $title = $purifier->purify($title); | |
677 | $body = $purifier->purify($body); | |
678 | $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId()); | |
679 | Tools::logm('Article ' . $item['id'] . ' updated.'); | |
680 | } | |
681 | } | |
63c35580 | 682 | } |
182faf26 | 683 | |
2f26729c NL |
684 | return array( |
685 | 'includeImport' => true, | |
686 | 'import' => array( | |
687 | 'recordsDownloadRequired' => $recordsDownloadRequired, | |
688 | 'recordsUnderDownload' => IMPORT_LIMIT, | |
689 | 'delay' => IMPORT_DELAY * 1000 | |
690 | ) | |
691 | ); | |
63c35580 | 692 | } |
c765c367 | 693 | |
07ee09f4 NL |
694 | /** |
695 | * export poche entries in json | |
696 | * @return json all poche entries | |
697 | */ | |
2f26729c NL |
698 | public function export() |
699 | { | |
a8ef1f3f MR |
700 | $filename = "wallabag-export-".$this->user->getId()."-".date("Y-m-d").".json"; |
701 | header('Content-Disposition: attachment; filename='.$filename); | |
702 | ||
703 | $entries = $this->store->retrieveAll($this->user->getId()); | |
704 | echo $this->tpl->render('export.twig', array( | |
705 | 'export' => Tools::renderJson($entries), | |
706 | )); | |
707 | Tools::logm('export view'); | |
c765c367 | 708 | } |
32520785 | 709 | |
07ee09f4 | 710 | /** |
a3436d4c | 711 | * Checks online the latest version of poche and cache it |
07ee09f4 NL |
712 | * @param string $which 'prod' or 'dev' |
713 | * @return string latest $which version | |
714 | */ | |
2f26729c | 715 | private function _getPocheVersion($which = 'prod') { |
a8ef1f3f MR |
716 | $cache_file = CACHE . '/' . $which; |
717 | $check_time = time(); | |
718 | ||
719 | # checks if the cached version file exists | |
720 | if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 86400 ))) { | |
721 | $version = file_get_contents($cache_file); | |
722 | $check_time = filemtime($cache_file); | |
723 | } else { | |
724 | $version = file_get_contents('http://static.wallabag.org/versions/' . $which); | |
725 | file_put_contents($cache_file, $version, LOCK_EX); | |
726 | } | |
727 | return array($version, $check_time); | |
32520785 | 728 | } |
72c20a52 | 729 | |
2f26729c NL |
730 | /** |
731 | * Update token for current user | |
732 | */ | |
733 | public function updateToken() | |
72c20a52 | 734 | { |
2f26729c NL |
735 | $token = Tools::generateToken(); |
736 | $this->store->updateUserConfig($this->user->getId(), 'token', $token); | |
737 | $currentConfig = $_SESSION['poche_user']->config; | |
738 | $currentConfig['token'] = $token; | |
739 | $_SESSION['poche_user']->setConfig($currentConfig); | |
740 | Tools::redirect(); | |
72c20a52 NL |
741 | } |
742 | ||
2f26729c NL |
743 | /** |
744 | * Generate RSS feeds for current user | |
745 | * | |
746 | * @param $token | |
747 | * @param $user_id | |
748 | * @param $tag_id | |
749 | * @param string $type | |
750 | */ | |
f778e472 | 751 | public function generateFeeds($token, $user_id, $tag_id, $type = 'home') |
72c20a52 | 752 | { |
f778e472 | 753 | $allowed_types = array('home', 'fav', 'archive', 'tag'); |
72c20a52 NL |
754 | $config = $this->store->getConfigUser($user_id); |
755 | ||
17b2afef | 756 | if ($config == null) { |
30bd2735 | 757 | die(sprintf(_('User with this id (%d) does not exist.'), $user_id)); |
17b2afef NL |
758 | } |
759 | ||
7dd8b502 MR |
760 | if (!in_array($type, $allowed_types) || !isset($config['token']) || $token != $config['token']) { |
761 | die(_('Uh, there is a problem while generating feed. Wrong token used?')); | |
72c20a52 | 762 | } |
72c20a52 | 763 | |
9e7c840b | 764 | $feed = new FeedWriter(RSS2); |
2e4440c3 | 765 | $feed->setTitle('wallabag — ' . $type . ' feed'); |
72c20a52 | 766 | $feed->setLink(Tools::getPocheUrl()); |
223268c2 NL |
767 | $feed->setChannelElement('pubDate', date(DATE_RSS , time())); |
768 | $feed->setChannelElement('generator', 'wallabag'); | |
769 | $feed->setDescription('wallabag ' . $type . ' elements'); | |
72c20a52 | 770 | |
f778e472 | 771 | if ($type == 'tag') { |
b89d5a2b | 772 | $entries = $this->store->retrieveEntriesByTag($tag_id, $user_id); |
f778e472 NL |
773 | } |
774 | else { | |
775 | $entries = $this->store->getEntriesByView($type, $user_id); | |
776 | } | |
777 | ||
72c20a52 NL |
778 | if (count($entries) > 0) { |
779 | foreach ($entries as $entry) { | |
780 | $newItem = $feed->createNewItem(); | |
0b57c682 | 781 | $newItem->setTitle($entry['title']); |
f86784c2 | 782 | $newItem->setSource(Tools::getPocheUrl() . '?view=view&id=' . $entry['id']); |
ed02e38e | 783 | $newItem->setLink($entry['url']); |
72c20a52 NL |
784 | $newItem->setDate(time()); |
785 | $newItem->setDescription($entry['content']); | |
786 | $feed->addItem($newItem); | |
787 | } | |
788 | } | |
789 | ||
790 | $feed->genarateFeed(); | |
791 | exit; | |
792 | } | |
6285e57c | 793 | |
6285e57c | 794 | |
0f859c6f MR |
795 | |
796 | /** | |
2f26729c | 797 | * Returns new purifier object with actual config |
0f859c6f | 798 | */ |
2f26729c NL |
799 | private function _getPurifier() |
800 | { | |
801 | $config = HTMLPurifier_Config::createDefault(); | |
802 | $config->set('Cache.SerializerPath', CACHE); | |
803 | $config->set('HTML.SafeIframe', true); | |
35d4e275 | 804 | |
2f26729c NL |
805 | //allow YouTube, Vimeo and dailymotion videos |
806 | $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%'); | |
ec15d0a7 | 807 | |
2f26729c | 808 | return new HTMLPurifier($config); |
0f859c6f | 809 | } |
cbc75bef | 810 | |
cbc75bef | 811 | |
df6afaf0 | 812 | } |