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