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('STATUSNET') && !defined('LACONICA')) {
42 * @author Evan Prodromou <evan@status.net>
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
50 class EmailsettingsAction extends SettingsAction
55 * @return string Title of the page
59 // TRANS: Title for e-mail settings.
60 return _('Email settings');
64 * Instructions for use
66 * @return instructions for use
68 function getInstructions()
70 // XXX: For consistency of parameters in messages, this should be a
71 // regular parameters, replaced with sprintf().
72 // TRANS: E-mail settings page instructions.
73 // TRANS: %%site.name%% is the name of the site.
74 return _('Manage how you get email from %%site.name%%.');
77 function showScripts()
79 parent::showScripts();
80 $this->script('emailsettings.js');
81 $this->autofocus('email');
85 * Content area of the page
87 * Shows a form for adding and removing email addresses and setting
92 function showContent()
94 $user = common_current_user();
96 $this->elementStart('form', array('method' => 'post',
97 'id' => 'form_settings_email',
98 'class' => 'form_settings',
100 common_local_url('emailsettings')));
101 $this->elementStart('fieldset');
102 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
103 // TRANS: Form legend for e-mail settings form.
104 $this->element('legend', null, _('Email address'));
105 $this->hidden('token', common_session_token());
108 $this->element('p', array('id' => 'form_confirmed'), $user->email);
109 // TRANS: Form note in e-mail settings form.
110 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
111 $this->hidden('email', $user->email);
112 // TRANS: Button label to remove a confirmed e-mail address.
113 $this->submit('remove', _m('BUTTON','Remove'));
115 $confirm = $this->getConfirmation();
117 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
118 $this->element('p', array('class' => 'form_note'),
119 // TRANS: Form note in e-mail settings form.
120 _('Awaiting confirmation on this address. '.
121 'Check your inbox (and spam box!) for a message '.
122 'with further instructions.'));
123 $this->hidden('email', $confirm->address);
124 // TRANS: Button label to cancel an e-mail address confirmation procedure.
125 $this->submit('cancel', _m('BUTTON','Cancel'));
127 $this->elementStart('ul', 'form_data');
128 $this->elementStart('li');
129 // TRANS: Field label for e-mail address input in e-mail settings form.
130 $this->input('email', _('Email address'),
131 ($this->arg('email')) ? $this->arg('email') : null,
132 // TRANS: Instructions for e-mail address input form. Do not translate
133 // TRANS: "example.org". It is one of the domain names reserved for
134 // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
135 // TRANS: Any other domain may be owned by a legitimate person or
136 // TRANS: organization.
137 _('Email address, like "UserName@example.org"'));
138 $this->elementEnd('li');
139 $this->elementEnd('ul');
140 // TRANS: Button label for adding an e-mail address in e-mail settings form.
141 $this->submit('add', _m('BUTTON','Add'));
144 $this->elementEnd('fieldset');
146 if (common_config('emailpost', 'enabled') && $user->email) {
147 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
148 // TRANS: Form legend for incoming e-mail settings form.
149 $this->element('legend', null, _('Incoming email'));
151 $this->elementStart('ul', 'form_data');
152 $this->elementStart('li');
153 $this->checkbox('emailpost',
154 // TRANS: Checkbox label in e-mail preferences form.
155 _('I want to post notices by email.'),
157 $this->elementEnd('li');
158 $this->elementEnd('ul');
160 // Our stylesheets make the form_data list items all floats, which
161 // creates lots of problems with trying to wrap divs around things.
162 // This should force a break before the next section, which needs
163 // to be separate so we can disable the things in it when the
165 $this->elementStart('div', array('style' => 'clear: both'));
166 $this->elementEnd('div');
168 $this->elementStart('div', array('id' => 'emailincoming'));
170 if ($user->incomingemail) {
171 $this->elementStart('p');
172 $this->element('span', 'address', $user->incomingemail);
173 // @todo XXX: Looks a little awkward in the UI.
174 // Something like "xxxx@identi.ca Send email ..". Needs improvement.
175 $this->element('span', 'input_instructions',
176 // TRANS: Form instructions for incoming e-mail form in e-mail settings.
177 _('Send email to this address to post new notices.'));
178 $this->elementEnd('p');
179 // TRANS: Button label for removing a set sender e-mail address to post notices from.
180 $this->submit('removeincoming', _m('BUTTON','Remove'));
183 $this->elementStart('p');
184 if ($user->incomingemail) {
185 // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
186 $msg = _('Make a new email address for posting to; '.
187 'cancels the old one.');
189 // TRANS: Instructions for incoming e-mail address input form.
190 $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
192 $this->element('span', 'input_instructions', $msg);
193 $this->elementEnd('p');
195 // TRANS: Button label for adding an e-mail address to send notices from.
196 $this->submit('newincoming', _m('BUTTON','New'));
198 $this->elementEnd('div'); // div#emailincoming
200 $this->elementEnd('fieldset');
203 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
204 // TRANS: Form legend for e-mail preferences form.
205 $this->element('legend', null, _('Email preferences'));
207 $this->elementStart('ul', 'form_data');
209 if (Event::handle('StartEmailFormData', array($this))) {
210 $this->elementStart('li');
211 $this->checkbox('emailnotifysub',
212 // TRANS: Checkbox label in e-mail preferences form.
213 _('Send me notices of new subscriptions through email.'),
214 $user->emailnotifysub);
215 $this->elementEnd('li');
216 $this->elementStart('li');
217 $this->checkbox('emailnotifyfav',
218 // TRANS: Checkbox label in e-mail preferences form.
219 _('Send me email when someone '.
220 'adds my notice as a favorite.'),
221 $user->emailnotifyfav);
222 $this->elementEnd('li');
223 $this->elementStart('li');
224 $this->checkbox('emailnotifymsg',
225 // TRANS: Checkbox label in e-mail preferences form.
226 _('Send me email when someone sends me a private message.'),
227 $user->emailnotifymsg);
228 $this->elementEnd('li');
229 $this->elementStart('li');
230 $this->checkbox('emailnotifyattn',
231 // TRANS: Checkbox label in e-mail preferences form.
232 _('Send me email when someone sends me an "@-reply".'),
233 $user->emailnotifyattn);
234 $this->elementEnd('li');
235 $this->elementStart('li');
236 $this->checkbox('emailnotifynudge',
237 // TRANS: Checkbox label in e-mail preferences form.
238 _('Allow friends to nudge me and send me an email.'),
239 $user->emailnotifynudge);
240 $this->elementEnd('li');
241 $this->elementStart('li');
242 $this->checkbox('emailmicroid',
243 // TRANS: Checkbox label in e-mail preferences form.
244 _('Publish a MicroID for my email address.'),
245 $user->emailmicroid);
246 $this->elementEnd('li');
247 Event::handle('EndEmailFormData', array($this));
249 $this->elementEnd('ul');
250 // TRANS: Button label to save e-mail preferences.
251 $this->submit('save', _m('BUTTON','Save'));
252 $this->elementEnd('fieldset');
253 $this->elementEnd('fieldset');
254 $this->elementEnd('form');
258 * Gets any existing email address confirmations we're waiting for
260 * @return Confirm_address Email address confirmation for user, or null
262 function getConfirmation()
264 $user = common_current_user();
266 $confirm = new Confirm_address();
268 $confirm->user_id = $user->id;
269 $confirm->address_type = 'email';
271 if ($confirm->find(true)) {
281 * Since there are a lot of different options on the page, we
282 * figure out what we're supposed to do based on which button was
287 function handlePost()
290 $token = $this->trimmed('token');
291 if (!$token || $token != common_session_token()) {
292 // TRANS: Client error displayed when the session token does not match or is not given.
293 $this->show_form(_('There was a problem with your session token. '.
294 'Try again, please.'));
298 if ($this->arg('save')) {
299 $this->savePreferences();
300 } else if ($this->arg('add')) {
302 } else if ($this->arg('cancel')) {
303 $this->cancelConfirmation();
304 } else if ($this->arg('remove')) {
305 $this->removeAddress();
306 } else if ($this->arg('removeincoming')) {
307 $this->removeIncoming();
308 } else if ($this->arg('newincoming')) {
309 $this->newIncoming();
311 // TRANS: Message given submitting a form with an unknown action in e-mail settings.
312 $this->showForm(_('Unexpected form submission.'));
317 * Save email preferences
321 function savePreferences()
323 $user = common_current_user();
325 if (Event::handle('StartEmailSaveForm', array($this, &$user))) {
326 $emailnotifysub = $this->boolean('emailnotifysub');
327 $emailnotifyfav = $this->boolean('emailnotifyfav');
328 $emailnotifymsg = $this->boolean('emailnotifymsg');
329 $emailnotifynudge = $this->boolean('emailnotifynudge');
330 $emailnotifyattn = $this->boolean('emailnotifyattn');
331 $emailmicroid = $this->boolean('emailmicroid');
332 $emailpost = $this->boolean('emailpost');
334 assert(!is_null($user)); // should already be checked
336 $user->query('BEGIN');
338 $original = clone($user);
340 $user->emailnotifysub = $emailnotifysub;
341 $user->emailnotifyfav = $emailnotifyfav;
342 $user->emailnotifymsg = $emailnotifymsg;
343 $user->emailnotifynudge = $emailnotifynudge;
344 $user->emailnotifyattn = $emailnotifyattn;
345 $user->emailmicroid = $emailmicroid;
346 $user->emailpost = $emailpost;
348 $result = $user->update($original);
350 if ($result === false) {
351 common_log_db_error($user, 'UPDATE', __FILE__);
352 // TRANS: Server error thrown on database error updating e-mail preferences.
353 $this->serverError(_('Could not update user.'));
357 $user->query('COMMIT');
359 Event::handle('EndEmailSaveForm', array($this));
361 // TRANS: Confirmation message for successful e-mail preferences save.
362 $this->showForm(_('Email preferences saved.'), true);
367 * Add the address passed in by the user
371 function addAddress()
373 $user = common_current_user();
375 $email = $this->trimmed('email');
380 // TRANS: Message given saving e-mail address without having provided one.
381 $this->showForm(_('No email address.'));
385 $email = common_canonical_email($email);
388 // TRANS: Message given saving e-mail address that cannot be normalised.
389 $this->showForm(_('Cannot normalize that email address.'));
392 if (!Validate::email($email, common_config('email', 'check_domain'))) {
393 // TRANS: Message given saving e-mail address that not valid.
394 $this->showForm(_('Not a valid email address.'));
396 } else if ($user->email == $email) {
397 // TRANS: Message given saving e-mail address that is already set.
398 $this->showForm(_('That is already your email address.'));
400 } else if ($this->emailExists($email)) {
401 // TRANS: Message given saving e-mail address that is already set for another user.
402 $this->showForm(_('That email address already belongs '.
403 'to another user.'));
407 if (Event::handle('StartAddEmailAddress', array($user, $email))) {
409 $confirm = new Confirm_address();
411 $confirm->address = $email;
412 $confirm->address_type = 'email';
413 $confirm->user_id = $user->id;
414 $confirm->code = common_confirmation_code(64);
416 $result = $confirm->insert();
418 if ($result === false) {
419 common_log_db_error($confirm, 'INSERT', __FILE__);
420 // TRANS: Server error thrown on database error adding e-mail confirmation code.
421 $this->serverError(_('Could not insert confirmation code.'));
425 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
427 Event::handle('EndAddEmailAddress', array($user, $email));
430 // TRANS: Message given saving valid e-mail address that is to be confirmed.
431 $msg = _('A confirmation code was sent to the email address you added. '.
432 'Check your inbox (and spam box!) for the code and instructions '.
433 'on how to use it.');
435 $this->showForm($msg, true);
439 * Handle a request to cancel email confirmation
443 function cancelConfirmation()
445 $email = $this->arg('email');
447 $confirm = $this->getConfirmation();
450 // TRANS: Message given canceling e-mail address confirmation that is not pending.
451 $this->showForm(_('No pending confirmation to cancel.'));
454 if ($confirm->address != $email) {
455 // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
456 $this->showForm(_('That is the wrong email address.'));
460 $result = $confirm->delete();
463 common_log_db_error($confirm, 'DELETE', __FILE__);
464 // TRANS: Server error thrown on database error canceling e-mail address confirmation.
465 $this->serverError(_('Could not delete email confirmation.'));
469 // TRANS: Message given after successfully canceling e-mail address confirmation.
470 $this->showForm(_('Email confirmation cancelled.'), true);
474 * Handle a request to remove an address from the user's account
478 function removeAddress()
480 $user = common_current_user();
482 $email = $this->arg('email');
484 // Maybe an old tab open...?
486 if ($user->email != $email) {
487 // TRANS: Message given trying to remove an e-mail address that is not
488 // TRANS: registered for the active user.
489 $this->showForm(_('That is not your email address.'));
493 $user->query('BEGIN');
495 $original = clone($user);
499 $result = $user->updateKeys($original);
502 common_log_db_error($user, 'UPDATE', __FILE__);
503 // TRANS: Server error thrown on database error removing a registered e-mail address.
504 $this->serverError(_('Could not update user.'));
507 $user->query('COMMIT');
509 // TRANS: Message given after successfully removing a registered e-mail address.
510 $this->showForm(_('The email address was removed.'), true);
514 * Handle a request to remove an incoming email address
518 function removeIncoming()
520 $user = common_current_user();
522 if (!$user->incomingemail) {
523 // TRANS: Form validation error displayed when trying to remove an incoming e-mail address while no address has been set.
524 $this->showForm(_('No incoming email address.'));
528 $orig = clone($user);
530 $user->incomingemail = null;
531 $user->emailpost = 0;
533 if (!$user->updateKeys($orig)) {
534 common_log_db_error($user, 'UPDATE', __FILE__);
535 // TRANS: Server error thrown on database error removing incoming e-mail address.
536 $this->serverError(_('Could not update user record.'));
539 // TRANS: Message given after successfully removing an incoming e-mail address.
540 $this->showForm(_('Incoming email address removed.'), true);
544 * Generate a new incoming email address
548 function newIncoming()
550 $user = common_current_user();
552 $orig = clone($user);
554 $user->incomingemail = mail_new_incoming_address();
555 $user->emailpost = 1;
557 if (!$user->updateKeys($orig)) {
558 common_log_db_error($user, 'UPDATE', __FILE__);
559 // TRANS: Server error thrown on database error adding incoming e-mail address.
560 $this->serverError(_('Could not update user record.'));
563 // TRANS: Message given after successfully adding an incoming e-mail address.
564 $this->showForm(_('New incoming email address added.'), true);
568 * Does another user already have this email address?
570 * Email addresses are unique for users.
572 * @param string $email Address to check
574 * @return boolean Whether the email already exists.
577 function emailExists($email)
579 $user = common_current_user();
581 $other = User::staticGet('email', $email);
586 return $other->id != $user->id;