aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/Poche.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/poche/Poche.class.php')
-rwxr-xr-xinc/poche/Poche.class.php198
1 files changed, 162 insertions, 36 deletions
diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php
index 16235474..fd2600f3 100755
--- a/inc/poche/Poche.class.php
+++ b/inc/poche/Poche.class.php
@@ -74,16 +74,57 @@ class Poche
74 /** 74 /**
75 * Creates a new user 75 * Creates a new user
76 */ 76 */
77 public function createNewUser($username, $password, $email = "") 77 public function createNewUser($username, $password, $email = "", $internalRegistration = false)
78 { 78 {
79 Tools::logm('Trying to create a new user...');
79 if (!empty($username) && !empty($password)){ 80 if (!empty($username) && !empty($password)){
80 $newUsername = filter_var($username, FILTER_SANITIZE_STRING); 81 $newUsername = filter_var($username, FILTER_SANITIZE_STRING);
81 $email = filter_var($email, FILTER_SANITIZE_STRING); 82 $email = filter_var($email, FILTER_SANITIZE_STRING);
82 if (!$this->store->userExists($newUsername)){ 83 if (!$this->store->userExists($newUsername)){
83 if ($this->store->install($newUsername, Tools::encodeString($password . $newUsername), $email)) { 84 if ($this->store->install($newUsername, Tools::encodeString($password . $newUsername), $email)) {
84 Tools::logm('The new user ' . $newUsername . ' has been installed'); 85 if ($email != "") { // if email is filled
86 if (SEND_CONFIRMATION_EMAIL && function_exists('mail')) {
87
88 // if internal registration from config screen
89 $body_internal = _('Hi,') . "\r\n\r\n" . sprintf(_('Someone just created a wallabag account for you on %1$s.'), Tools::getPocheUrl()) .
90 "\r\n\r\n" . sprintf(_('Your login is %1$s.'), $newUsername) ."\r\n\r\n" .
91 _('Note : The password has been chosen by the person who created your account. Get in touch with that person to know your password and change it as soon as possible') . "\r\n\r\n" .
92 _('Have fun with it !') . "\r\n\r\n" .
93 _('This is an automatically generated message, no one will answer if you respond to it.');
94
95 // if external (public) registration
96 $body = sprintf(_('Hi, %1$s'), $newUsername) . "\r\n\r\n" .
97 sprintf(_('You\'ve just created a wallabag account on %1$s.'), Tools::getPocheUrl()) .
98 "\r\n\r\n" . _("Have fun with it !");
99
100 $body = $internalRegistration ? $body_internal : $body;
101
102 $body = wordwrap($body, 70, "\r\n"); // cut lines with more than 70 caracters (MIME standard)
103 if (mail($email, sprintf(_('Your new wallabag account on %1$s'), Tools::getPocheUrl()), $body,
104 'X-Mailer: PHP/' . phpversion() . "\r\n" .
105 'Content-type: text/plain; charset=UTF-8' . "\r\n" .
106 "From: " . $newUsername . "@" . gethostname() . "\r\n")) {
107 Tools::logm('The user ' . $newUsername . ' has been emailed');
108 $this->messages->add('i', sprintf(_('The new user %1$s has been sent an email at %2$s. You may have to check spam folder.'), $newUsername, $email));
109 Tools::redirect('?');
110
111 } else {
112 Tools::logm('A problem has been encountered while sending an email');
113 $this->messages->add('e', _('A problem has been encountered while sending an email'));
114 }
115 } else {
116 Tools::logm('The user has been created, but the server did not authorize sending emails');
117 $this->messages->add('i', _('The server did not authorize sending a confirmation email, but the user was created.'));
118 }
119 } else {
120 Tools::logm('The user has been created, but no email was saved, so no confimation email was sent');
121 $this->messages->add('i', _('The user was created, but no email was sent because email was not filled in'));
122 }
123 Tools::logm('The new user ' . $newUsername . ' has been installed');
124 if (\Session::isLogged()) {
85 $this->messages->add('s', sprintf(_('The new user %s has been installed. Do you want to <a href="?logout">logout ?</a>'), $newUsername)); 125 $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(); 126 }
127 Tools::redirect();
87 } 128 }
88 else { 129 else {
89 Tools::logm('error during adding new user'); 130 Tools::logm('error during adding new user');
@@ -96,6 +137,9 @@ class Poche
96 Tools::redirect(); 137 Tools::redirect();
97 } 138 }
98 } 139 }
140 else {
141 Tools::logm('Password or username were empty');
142 }
99 } 143 }
100 144
101 /** 145 /**
@@ -180,6 +224,13 @@ class Poche
180 } 224 }
181 } 225 }
182 226
227 // if there are tags, add them to the new article
228 if (isset($_GET['tags'])) {
229 $_POST['value'] = $_GET['tags'];
230 $_POST['entry_id'] = $last_id;
231 $this->action('add_tag', $url);
232 }
233
183 $this->messages->add('s', _('the link has been added successfully')); 234 $this->messages->add('s', _('the link has been added successfully'));
184 } 235 }
185 else { 236 else {
@@ -188,24 +239,38 @@ class Poche
188 } 239 }
189 240
190 if ($autoclose == TRUE) { 241 if ($autoclose == TRUE) {
191 Tools::redirect('?view=home'); 242 Tools::redirect('?view=home&closewin=true');
192 } else { 243 } else {
193 Tools::redirect('?view=home&closewin=true'); 244 Tools::redirect('?view=home');
194 } 245 }
246 return $last_id;
195 break; 247 break;
196 case 'delete': 248 case 'delete':
197 $msg = 'delete link #' . $id; 249 if (isset($_GET['search'])) {
198 if ($this->store->deleteById($id, $this->user->getId())) { 250 //when we want to apply a delete to a search
199 if (DOWNLOAD_PICTURES) { 251 $tags = array($_GET['search']);
200 Picture::removeDirectory(ABS_PATH . $id); 252 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
253 $entry_ids = array();
254 foreach ($allentry_ids as $eachentry) {
255 $entry_ids[] = $eachentry[0];
201 } 256 }
202 $this->messages->add('s', _('the link has been deleted successfully')); 257 } else { // delete a single article
258 $entry_ids = array($id);
203 } 259 }
204 else { 260 foreach($entry_ids as $id) {
205 $this->messages->add('e', _('the link wasn\'t deleted')); 261 $msg = 'delete link #' . $id;
206 $msg = 'error : can\'t delete link #' . $id; 262 if ($this->store->deleteById($id, $this->user->getId())) {
263 if (DOWNLOAD_PICTURES) {
264 Picture::removeDirectory(ABS_PATH . $id);
265 }
266 $this->messages->add('s', _('the link has been deleted successfully'));
267 }
268 else {
269 $this->messages->add('e', _('the link wasn\'t deleted'));
270 $msg = 'error : can\'t delete link #' . $id;
271 }
272 Tools::logm($msg);
207 } 273 }
208 Tools::logm($msg);
209 Tools::redirect('?'); 274 Tools::redirect('?');
210 break; 275 break;
211 case 'toggle_fav' : 276 case 'toggle_fav' :
@@ -220,8 +285,21 @@ class Poche
220 } 285 }
221 break; 286 break;
222 case 'toggle_archive' : 287 case 'toggle_archive' :
223 $this->store->archiveById($id, $this->user->getId()); 288 if (isset($_GET['tag_id'])) {
224 Tools::logm('archive link #' . $id); 289 //when we want to archive a whole tag
290 $tag_id = $_GET['tag_id'];
291 $allentry_ids = $this->store->retrieveEntriesByTag($tag_id, $this->user->getId());
292 $entry_ids = array();
293 foreach ($allentry_ids as $eachentry) {
294 $entry_ids[] = $eachentry[0];
295 }
296 } else { //archive a single article
297 $entry_ids = array($id);
298 }
299 foreach($entry_ids as $id) {
300 $this->store->archiveById($id, $this->user->getId());
301 Tools::logm('archive link #' . $id);
302 }
225 if ( Tools::isAjaxRequest() ) { 303 if ( Tools::isAjaxRequest() ) {
226 echo 1; 304 echo 1;
227 exit; 305 exit;
@@ -303,6 +381,26 @@ class Poche
303 $this->messages->add('s', _('The tag has been successfully deleted')); 381 $this->messages->add('s', _('The tag has been successfully deleted'));
304 Tools::redirect(); 382 Tools::redirect();
305 break; 383 break;
384
385 case 'reload_article' :
386 Tools::logm('reload article');
387 $id = $_GET['id'];
388 $entry = $this->store->retrieveOneById($id, $this->user->getId());
389 Tools::logm('reload url ' . $entry['url']);
390 $url = new Url(base64_encode($entry['url']));
391 $this->action('add', $url);
392 break;
393
394 /* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */
395 case 'random':
396 Tools::logm('get a random article');
397 if ($this->store->getRandomId($this->user->getId())) {
398 $id_array = $this->store->getRandomId($this->user->getId());
399 $id = $id_array[0];
400 Tools::redirect('?view=view&id=' . $id[0]);
401 Tools::logm('got the article with id ' . $id[0]);
402 }
403 break;
306 default: 404 default:
307 break; 405 break;
308 } 406 }
@@ -405,9 +503,12 @@ class Poche
405 } 503 }
406 504
407 # flattr checking 505 # flattr checking
408 $flattr = new FlattrItem(); 506 $flattr = NULL;
409 $flattr->checkItem($entry['url'], $entry['id']); 507 if (FLATTR) {
410 508 $flattr = new FlattrItem();
509 $flattr->checkItem($entry['url'], $entry['id']);
510 }
511
411 # tags 512 # tags
412 $tags = $this->store->retrieveTagsByEntry($entry['id']); 513 $tags = $this->store->retrieveTagsByEntry($entry['id']);
413 514
@@ -540,6 +641,8 @@ class Poche
540 Tools::redirect($referer); 641 Tools::redirect($referer);
541 } 642 }
542 $this->messages->add('e', _('login failed: bad login or password')); 643 $this->messages->add('e', _('login failed: bad login or password'));
644 // log login failure in web server log to allow fail2ban usage
645 error_log('user '.$login.' authentication failure');
543 Tools::logm('login failed'); 646 Tools::logm('login failed');
544 Tools::redirect(); 647 Tools::redirect();
545 } 648 }
@@ -625,7 +728,18 @@ class Poche
625 $urlsInserted[] = $url; //add 728 $urlsInserted[] = $url; //add
626 if (isset($record['tags']) && trim($record['tags'])) { 729 if (isset($record['tags']) && trim($record['tags'])) {
627 730
628 // @TODO: set tags 731 $tags = explode(',', $record['tags']);
732 foreach($tags as $tag) {
733 $entry_id = $id;
734 $tag_id = $this->store->retrieveTagByValue($tag);
735 if ($tag_id) {
736 $this->store->setTagToEntry($tag_id['id'], $entry_id);
737 } else {
738 $this->store->createTag($tag);
739 $tag_id = $this->store->retrieveTagByValue($tag);
740 $this->store->setTagToEntry($tag_id['id'], $entry_id);
741 }
742 }
629 743
630 } 744 }
631 } 745 }
@@ -640,7 +754,7 @@ class Poche
640 Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).'); 754 Tools::logm('Import of articles finished: '.$i.' articles added (w/o content if not provided).');
641 } 755 }
642 else { 756 else {
643 $this->messages->add('s', _('Did you forget to select a file?')); 757 $this->messages->add('e', _('Did you forget to select a file?'));
644 } 758 }
645 // file parsing finished here 759 // file parsing finished here
646 // now download article contents if any 760 // now download article contents if any
@@ -669,17 +783,23 @@ class Poche
669 $purifier = $this->_getPurifier(); 783 $purifier = $this->_getPurifier();
670 foreach($items as $item) { 784 foreach($items as $item) {
671 $url = new Url(base64_encode($item['url'])); 785 $url = new Url(base64_encode($item['url']));
672 Tools::logm('Fetching article ' . $item['id']); 786 if( $url->isCorrect() )
673 $content = Tools::getPageContent($url); 787 {
674 $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled')); 788 Tools::logm('Fetching article ' . $item['id']);
675 $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined')); 789 $content = Tools::getPageContent($url);
676 790 $title = (($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled'));
677 // clean content to prevent xss attack 791 $body = (($content['rss']['channel']['item']['description'] != '') ? $content['rss']['channel']['item']['description'] : _('Undefined'));
678 792
679 $title = $purifier->purify($title); 793 // clean content to prevent xss attack
680 $body = $purifier->purify($body); 794
681 $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId()); 795 $title = $purifier->purify($title);
682 Tools::logm('Article ' . $item['id'] . ' updated.'); 796 $body = $purifier->purify($body);
797 $this->store->updateContentAndTitle($item['id'], $title, $body, $this->user->getId());
798 Tools::logm('Article ' . $item['id'] . ' updated.');
799 } else
800 {
801 Tools::logm('Unvalid URL (' . $item['url'] .') to fetch for article ' . $item['id']);
802 }
683 } 803 }
684 } 804 }
685 } 805 }
@@ -748,10 +868,11 @@ class Poche
748 * 868 *
749 * @param $token 869 * @param $token
750 * @param $user_id 870 * @param $user_id
751 * @param $tag_id 871 * @param $tag_id if $type is 'tag', the id of the tag to generate feed for
752 * @param string $type 872 * @param string $type the type of feed to generate
873 * @param int $limit the maximum number of items (0 means all)
753 */ 874 */
754 public function generateFeeds($token, $user_id, $tag_id, $type = 'home') 875 public function generateFeeds($token, $user_id, $tag_id, $type = 'home', $limit = 0)
755 { 876 {
756 $allowed_types = array('home', 'fav', 'archive', 'tag'); 877 $allowed_types = array('home', 'fav', 'archive', 'tag');
757 $config = $this->store->getConfigUser($user_id); 878 $config = $this->store->getConfigUser($user_id);
@@ -778,8 +899,13 @@ class Poche
778 $entries = $this->store->getEntriesByView($type, $user_id); 899 $entries = $this->store->getEntriesByView($type, $user_id);
779 } 900 }
780 901
902 // if $limit is set to zero, use all entries
903 if (0 == $limit) {
904 $limit = count($entries);
905 }
781 if (count($entries) > 0) { 906 if (count($entries) > 0) {
782 foreach ($entries as $entry) { 907 for ($i = 0; $i < min(count($entries), $limit); $i++) {
908 $entry = $entries[$i];
783 $newItem = $feed->createNewItem(); 909 $newItem = $feed->createNewItem();
784 $newItem->setTitle($entry['title']); 910 $newItem->setTitle($entry['title']);
785 $newItem->setSource(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']); 911 $newItem->setSource(Tools::getPocheUrl() . '?view=view&amp;id=' . $entry['id']);