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