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