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