3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2008-2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('GNUSOCIAL')) { exit(1); }
38 * @author Evan Prodromou <evan@status.net>
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
46 class EmailsettingsAction extends SettingsAction
51 * @return string Title of the page
55 // TRANS: Title for e-mail settings.
56 return _('Email settings');
60 * Instructions for use
62 * @return instructions for use
64 function getInstructions()
66 // XXX: For consistency of parameters in messages, this should be a
67 // regular parameters, replaced with sprintf().
68 // TRANS: E-mail settings page instructions.
69 // TRANS: %%site.name%% is the name of the site.
70 return _('Manage how you get email from %%site.name%%.');
73 function showScripts()
75 parent::showScripts();
76 $this->script('emailsettings.js');
77 $this->autofocus('email');
81 * Content area of the page
83 * Shows a form for adding and removing email addresses and setting
88 function showContent()
90 $user = $this->scoped->getUser();
92 $this->elementStart('form', array('method' => 'post',
93 'id' => 'form_settings_email',
94 'class' => 'form_settings',
96 common_local_url('emailsettings')));
97 $this->elementStart('fieldset');
98 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
99 // TRANS: Form legend for e-mail settings form.
100 $this->element('legend', null, _('Email address'));
101 $this->hidden('token', common_session_token());
104 $this->element('p', array('id' => 'form_confirmed'), $user->email);
105 // TRANS: Form note in e-mail settings form.
106 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
107 $this->hidden('email', $user->email);
108 // TRANS: Button label to remove a confirmed e-mail address.
109 $this->submit('remove', _m('BUTTON','Remove'));
112 $confirm = $this->getConfirmation();
113 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
114 $this->element('p', array('class' => 'form_note'),
115 // TRANS: Form note in e-mail settings form.
116 _('Awaiting confirmation on this address. '.
117 'Check your inbox (and spam box!) for a message '.
118 'with further instructions.'));
119 $this->hidden('email', $confirm->address);
120 // TRANS: Button label to cancel an e-mail address confirmation procedure.
121 $this->submit('cancel', _m('BUTTON','Cancel'));
122 } catch (NoResultException $e) {
123 $this->elementStart('ul', 'form_data');
124 $this->elementStart('li');
125 // TRANS: Field label for e-mail address input in e-mail settings form.
126 $this->input('email', _('Email address'),
127 $this->trimmed('email') ?: null,
128 // TRANS: Instructions for e-mail address input form. Do not translate
129 // TRANS: "example.org". It is one of the domain names reserved for
130 // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
131 // TRANS: Any other domain may be owned by a legitimate person or
132 // TRANS: organization.
133 _('Email address, like "UserName@example.org"'));
134 $this->elementEnd('li');
135 $this->elementEnd('ul');
136 // TRANS: Button label for adding an e-mail address in e-mail settings form.
137 $this->submit('add', _m('BUTTON','Add'));
140 $this->elementEnd('fieldset');
142 if (common_config('emailpost', 'enabled') && $user->email) {
143 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
144 // TRANS: Form legend for incoming e-mail settings form.
145 $this->element('legend', null, _('Incoming email'));
147 $this->elementStart('ul', 'form_data');
148 $this->elementStart('li');
149 $this->checkbox('emailpost',
150 // TRANS: Checkbox label in e-mail preferences form.
151 _('I want to post notices by email.'),
153 $this->elementEnd('li');
154 $this->elementEnd('ul');
156 // Our stylesheets make the form_data list items all floats, which
157 // creates lots of problems with trying to wrap divs around things.
158 // This should force a break before the next section, which needs
159 // to be separate so we can disable the things in it when the
161 $this->elementStart('div', array('style' => 'clear: both'));
162 $this->elementEnd('div');
164 $this->elementStart('div', array('id' => 'emailincoming'));
166 if ($user->incomingemail) {
167 $this->elementStart('p');
168 $this->element('span', 'address', $user->incomingemail);
169 // @todo XXX: Looks a little awkward in the UI.
170 // Something like "xxxx@identi.ca Send email ..". Needs improvement.
171 $this->element('span', 'input_instructions',
172 // TRANS: Form instructions for incoming e-mail form in e-mail settings.
173 _('Send email to this address to post new notices.'));
174 $this->elementEnd('p');
175 // TRANS: Button label for removing a set sender e-mail address to post notices from.
176 $this->submit('removeincoming', _m('BUTTON','Remove'));
179 $this->elementStart('p');
180 if ($user->incomingemail) {
181 // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
182 $msg = _('Make a new email address for posting to; '.
183 'cancels the old one.');
185 // TRANS: Instructions for incoming e-mail address input form.
186 $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
188 $this->element('span', 'input_instructions', $msg);
189 $this->elementEnd('p');
191 // TRANS: Button label for adding an e-mail address to send notices from.
192 $this->submit('newincoming', _m('BUTTON','New'));
194 $this->elementEnd('div'); // div#emailincoming
196 $this->elementEnd('fieldset');
199 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
200 // TRANS: Form legend for e-mail preferences form.
201 $this->element('legend', null, _('Email preferences'));
203 $this->elementStart('ul', 'form_data');
205 if (Event::handle('StartEmailFormData', array($this, $this->scoped))) {
206 $this->elementStart('li');
207 $this->checkbox('emailnotifysub',
208 // TRANS: Checkbox label in e-mail preferences form.
209 _('Send me notices of new subscriptions through email.'),
210 $user->emailnotifysub);
211 $this->elementEnd('li');
212 $this->elementStart('li');
213 $this->checkbox('emailnotifymsg',
214 // TRANS: Checkbox label in e-mail preferences form.
215 _('Send me email when someone sends me a private message.'),
216 $user->emailnotifymsg);
217 $this->elementEnd('li');
218 $this->elementStart('li');
219 $this->checkbox('emailnotifyattn',
220 // TRANS: Checkbox label in e-mail preferences form.
221 _('Send me email when someone sends me an "@-reply".'),
222 $user->emailnotifyattn);
223 $this->elementEnd('li');
224 $this->elementStart('li');
225 $this->checkbox('emailnotifynudge',
226 // TRANS: Checkbox label in e-mail preferences form.
227 _('Allow friends to nudge me and send me an email.'),
228 $user->emailnotifynudge);
229 $this->elementEnd('li');
230 Event::handle('EndEmailFormData', array($this, $this->scoped));
232 $this->elementEnd('ul');
233 // TRANS: Button label to save e-mail preferences.
234 $this->submit('save', _m('BUTTON','Save'));
235 $this->elementEnd('fieldset');
236 $this->elementEnd('fieldset');
237 $this->elementEnd('form');
241 * Gets any existing email address confirmations we're waiting for
243 * @return Confirm_address Email address confirmation for user, or null
245 function getConfirmation()
247 $confirm = new Confirm_address();
249 $confirm->user_id = $this->scoped->getID();
250 $confirm->address_type = 'email';
252 if ($confirm->find(true)) {
256 throw new NoResultException($confirm);
259 protected function doPost()
261 if ($this->arg('save')) {
262 return $this->savePreferences();
263 } else if ($this->arg('add')) {
264 return $this->addAddress();
265 } else if ($this->arg('cancel')) {
266 return $this->cancelConfirmation();
267 } else if ($this->arg('remove')) {
268 return $this->removeAddress();
269 } else if ($this->arg('removeincoming')) {
270 return $this->removeIncoming();
271 } else if ($this->arg('newincoming')) {
272 return $this->newIncoming();
275 // TRANS: Message given submitting a form with an unknown action in e-mail settings.
276 throw new ClientException(_('Unexpected form submission.'));
280 * Save email preferences
284 function savePreferences()
286 if (Event::handle('StartEmailSaveForm', array($this, $this->scoped))) {
287 $emailnotifysub = $this->booleanintstring('emailnotifysub');
288 $emailnotifymsg = $this->booleanintstring('emailnotifymsg');
289 $emailnotifynudge = $this->booleanintstring('emailnotifynudge');
290 $emailnotifyattn = $this->booleanintstring('emailnotifyattn');
291 $emailpost = $this->booleanintstring('emailpost');
293 $user = $this->scoped->getUser();
294 $user->query('BEGIN');
295 $original = clone($user);
297 $user->emailnotifysub = $emailnotifysub;
298 $user->emailnotifymsg = $emailnotifymsg;
299 $user->emailnotifynudge = $emailnotifynudge;
300 $user->emailnotifyattn = $emailnotifyattn;
301 $user->emailpost = $emailpost;
303 $result = $user->update($original);
305 if ($result === false) {
306 common_log_db_error($user, 'UPDATE', __FILE__);
307 $user->query('ROLLBACK');
308 // TRANS: Server error thrown on database error updating e-mail preferences.
309 throw new ServerException(_('Could not update user.'));
312 $user->query('COMMIT');
314 Event::handle('EndEmailSaveForm', array($this, $this->scoped));
316 // TRANS: Confirmation message for successful e-mail preferences save.
317 return _('Email preferences saved.');
321 * Add the address passed in by the user
325 function addAddress()
327 $user = $this->scoped->getUser();
329 $email = $this->trimmed('email');
334 // TRANS: Message given saving e-mail address without having provided one.
335 throw new ClientException(_('No email address.'));
338 $email = common_canonical_email($email);
341 // TRANS: Message given saving e-mail address that cannot be normalised.
342 throw new ClientException(_('Cannot normalize that email address.'));
344 if (!Validate::email($email, common_config('email', 'check_domain'))) {
345 // TRANS: Message given saving e-mail address that not valid.
346 throw new ClientException(_('Not a valid email address.'));
347 } else if ($user->email == $email) {
348 // TRANS: Message given saving e-mail address that is already set.
349 throw new ClientException(_('That is already your email address.'));
350 } else if ($this->emailExists($email)) {
351 // TRANS: Message given saving e-mail address that is already set for another user.
352 throw new ClientException(_('That email address already belongs to another user.'));
355 if (Event::handle('StartAddEmailAddress', array($user, $email))) {
357 $confirm = new Confirm_address();
359 $confirm->address = $email;
360 $confirm->address_type = 'email';
361 $confirm->user_id = $user->getID();
362 $confirm->code = common_confirmation_code(64);
364 $result = $confirm->insert();
366 if ($result === false) {
367 common_log_db_error($confirm, 'INSERT', __FILE__);
368 // TRANS: Server error thrown on database error adding e-mail confirmation code.
369 throw new ServerException(_('Could not insert confirmation code.'));
372 $confirm->sendConfirmation();
374 Event::handle('EndAddEmailAddress', array($user, $email));
377 // TRANS: Message given saving valid e-mail address that is to be confirmed.
378 return _('A confirmation code was sent to the email address you added. '.
379 'Check your inbox (and spam box!) for the code and instructions '.
380 'on how to use it.');
384 * Handle a request to cancel email confirmation
388 function cancelConfirmation()
390 $email = $this->trimmed('email');
393 $confirm = $this->getConfirmation();
394 if ($confirm->address !== $email) {
395 // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
396 throw new ClientException(_('That is the wrong email address.'));
398 } catch (NoResultException $e) {
399 // TRANS: Message given canceling e-mail address confirmation that is not pending.
400 throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
405 // TRANS: Message given after successfully canceling e-mail address confirmation.
406 return _('Email confirmation cancelled.');
410 * Handle a request to remove an address from the user's account
414 function removeAddress()
416 $user = common_current_user();
418 $email = $this->trimmed('email');
420 // Maybe an old tab open...?
421 if ($user->email !== $email) {
422 // TRANS: Message given trying to remove an e-mail address that is not
423 // TRANS: registered for the active user.
424 throw new ClientException(_('That is not your email address.'));
427 $original = clone($user);
429 // Throws exception on failure. Also performs it within a transaction.
430 $user->updateWithKeys($original);
432 // TRANS: Message given after successfully removing a registered e-mail address.
433 return _('The email address was removed.');
437 * Handle a request to remove an incoming email address
441 function removeIncoming()
443 $user = common_current_user();
445 if (empty($user->incomingemail)) {
446 // TRANS: Form validation error displayed when trying to remove an incoming e-mail address while no address has been set.
447 throw new AlreadyFulfilledException(_('No incoming email address.'));
450 $orig = clone($user);
451 $user->incomingemail = null;
452 $user->emailpost = 0;
453 // Throws exception on failure. Also performs it within a transaction.
454 $user->updateWithKeys($orig);
456 // TRANS: Message given after successfully removing an incoming e-mail address.
457 return _('Incoming email address removed.');
461 * Generate a new incoming email address
465 function newIncoming()
467 $user = common_current_user();
468 $orig = clone($user);
469 $user->incomingemail = mail_new_incoming_address();
470 $user->emailpost = 1;
471 // Throws exception on failure. Also performs it within a transaction.
472 $user->updateWithKeys($orig);
474 // TRANS: Message given after successfully adding an incoming e-mail address.
475 return _('New incoming email address added.');
479 * Does another user already have this email address?
481 * Email addresses are unique for users.
483 * @param string $email Address to check
485 * @return boolean Whether the email already exists.
488 function emailExists($email)
490 $user = common_current_user();
492 $other = User::getKV('email', $email);
494 if (!$other instanceof User) {
498 return $other->id != $user->id;