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')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
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 AccountSettingsAction
55 * @return string Title of the page
60 // TRANS: Title for e-mail settings.
61 return _('Email settings');
65 * Instructions for use
67 * @return instructions for use
70 function getInstructions()
72 // XXX: For consistency of parameters in messages, this should be a
73 // regular parameters, replaced with sprintf().
74 // TRANS: E-mail settings page instructions.
75 // TRANS: %%site.name%% is the name of the site.
76 return _('Manage how you get email from %%site.name%%.');
79 function showScripts()
81 parent::showScripts();
82 $this->script('emailsettings.js');
83 $this->autofocus('email');
87 * Content area of the page
89 * Shows a form for adding and removing email addresses and setting
95 function showContent()
97 $user = common_current_user();
99 $this->elementStart('form', array('method' => 'post',
100 'id' => 'form_settings_email',
101 'class' => 'form_settings',
103 common_local_url('emailsettings')));
104 $this->elementStart('fieldset');
105 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
106 // TRANS: Form legend for e-mail settings form.
107 $this->element('legend', null, _('Email address'));
108 $this->hidden('token', common_session_token());
111 $this->element('p', array('id' => 'form_confirmed'), $user->email);
112 // TRANS: Form note in e-mail settings form.
113 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
114 $this->hidden('email', $user->email);
115 // TRANS: Button label to remove a confirmed e-mail address.
116 $this->submit('remove', _m('BUTTON','Remove'));
118 $confirm = $this->getConfirmation();
120 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
121 // TRANS: Form note in e-mail settings form.
122 $this->element('p', array('class' => 'form_note'),
123 _('Awaiting confirmation on this address. '.
124 'Check your inbox (and spam box!) for a message '.
125 'with further instructions.'));
126 $this->hidden('email', $confirm->address);
127 // TRANS: Button label to cancel an e-mail address confirmation procedure.
128 $this->submit('cancel', _m('BUTTON','Cancel'));
130 $this->elementStart('ul', 'form_data');
131 $this->elementStart('li');
132 // TRANS: Field label for e-mail address input in e-mail settings form.
133 $this->input('email', _('Email address'),
134 ($this->arg('email')) ? $this->arg('email') : null,
135 // TRANS: Instructions for e-mail address input form. Do not translate
136 // TRANS: "example.org". It is one of the domain names reserved for
137 // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
138 // TRANS: Any other domain may be owned by a legitimate person or
139 // TRANS: organization.
140 _('Email address, like "UserName@example.org"'));
141 $this->elementEnd('li');
142 $this->elementEnd('ul');
143 // TRANS: Button label for adding an e-mail address in e-mail settings form.
144 $this->submit('add', _m('BUTTON','Add'));
147 $this->elementEnd('fieldset');
149 if (common_config('emailpost', 'enabled') && $user->email) {
150 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
151 // TRANS: Form legend for incoming e-mail settings form.
152 $this->element('legend', null, _('Incoming email'));
154 $this->elementStart('ul', 'form_data');
155 $this->elementStart('li');
156 $this->checkbox('emailpost',
157 // TRANS: Checkbox label in e-mail preferences form.
158 _('I want to post notices by email.'),
160 $this->elementEnd('li');
161 $this->elementEnd('ul');
163 // Our stylesheets make the form_data list items all floats, which
164 // creates lots of problems with trying to wrap divs around things.
165 // This should force a break before the next section, which needs
166 // to be separate so we can disable the things in it when the
168 $this->elementStart('div', array('style' => 'clear: both'));
169 $this->elementEnd('div');
171 $this->elementStart('div', array('id' => 'emailincoming'));
173 if ($user->incomingemail) {
174 $this->elementStart('p');
175 $this->element('span', 'address', $user->incomingemail);
176 // XXX: Looks a little awkward in the UI.
177 // Something like "xxxx@identi.ca Send email ..". Needs improvement.
178 $this->element('span', 'input_instructions',
179 // TRANS: Form instructions for incoming e-mail form in e-mail settings.
180 _('Send email to this address to post new notices.'));
181 $this->elementEnd('p');
182 // TRANS: Button label for removing a set sender e-mail address to post notices from.
183 $this->submit('removeincoming', _m('BUTTON','Remove'));
186 $this->elementStart('p');
187 if ($user->incomingemail) {
188 // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
189 $msg = _('Make a new email address for posting to; '.
190 'cancels the old one.');
192 // TRANS: Instructions for incoming e-mail address input form.
193 $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
195 $this->element('span', 'input_instructions', $msg);
196 $this->elementEnd('p');
198 // TRANS: Button label for adding an e-mail address to send notices from.
199 $this->submit('newincoming', _m('BUTTON','New'));
201 $this->elementEnd('div'); // div#emailincoming
203 $this->elementEnd('fieldset');
206 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
207 // TRANS: Form legend for e-mail preferences form.
208 $this->element('legend', null, _('Email preferences'));
210 $this->elementStart('ul', 'form_data');
212 if (Event::handle('StartEmailFormData', array($this))) {
213 $this->elementStart('li');
214 $this->checkbox('emailnotifysub',
215 // TRANS: Checkbox label in e-mail preferences form.
216 _('Send me notices of new subscriptions through email.'),
217 $user->emailnotifysub);
218 $this->elementEnd('li');
219 $this->elementStart('li');
220 $this->checkbox('emailnotifyfav',
221 // TRANS: Checkbox label in e-mail preferences form.
222 _('Send me email when someone '.
223 'adds my notice as a favorite.'),
224 $user->emailnotifyfav);
225 $this->elementEnd('li');
226 $this->elementStart('li');
227 $this->checkbox('emailnotifymsg',
228 // TRANS: Checkbox label in e-mail preferences form.
229 _('Send me email when someone sends me a private message.'),
230 $user->emailnotifymsg);
231 $this->elementEnd('li');
232 $this->elementStart('li');
233 $this->checkbox('emailnotifyattn',
234 // TRANS: Checkbox label in e-mail preferences form.
235 _('Send me email when someone sends me an "@-reply".'),
236 $user->emailnotifyattn);
237 $this->elementEnd('li');
238 $this->elementStart('li');
239 $this->checkbox('emailnotifynudge',
240 // TRANS: Checkbox label in e-mail preferences form.
241 _('Allow friends to nudge me and send me an email.'),
242 $user->emailnotifynudge);
243 $this->elementEnd('li');
244 $this->elementStart('li');
245 $this->checkbox('emailmicroid',
246 // TRANS: Checkbox label in e-mail preferences form.
247 _('Publish a MicroID for my email address.'),
248 $user->emailmicroid);
249 $this->elementEnd('li');
250 Event::handle('EndEmailFormData', array($this));
252 $this->elementEnd('ul');
253 // TRANS: Button label to save e-mail preferences.
254 $this->submit('save', _m('BUTTON','Save'));
255 $this->elementEnd('fieldset');
256 $this->elementEnd('fieldset');
257 $this->elementEnd('form');
261 * Gets any existing email address confirmations we're waiting for
263 * @return Confirm_address Email address confirmation for user, or null
266 function getConfirmation()
268 $user = common_current_user();
270 $confirm = new Confirm_address();
272 $confirm->user_id = $user->id;
273 $confirm->address_type = 'email';
275 if ($confirm->find(true)) {
285 * Since there are a lot of different options on the page, we
286 * figure out what we're supposed to do based on which button was
292 function handlePost()
295 $token = $this->trimmed('token');
296 if (!$token || $token != common_session_token()) {
297 $this->show_form(_('There was a problem with your session token. '.
298 'Try again, please.'));
302 if ($this->arg('save')) {
303 $this->savePreferences();
304 } else if ($this->arg('add')) {
306 } else if ($this->arg('cancel')) {
307 $this->cancelConfirmation();
308 } else if ($this->arg('remove')) {
309 $this->removeAddress();
310 } else if ($this->arg('removeincoming')) {
311 $this->removeIncoming();
312 } else if ($this->arg('newincoming')) {
313 $this->newIncoming();
315 // TRANS: Message given submitting a form with an unknown action in e-mail settings.
316 $this->showForm(_('Unexpected form submission.'));
321 * Save email preferences
326 function savePreferences()
328 $user = common_current_user();
330 if (Event::handle('StartEmailSaveForm', array($this, &$user))) {
332 $emailnotifysub = $this->boolean('emailnotifysub');
333 $emailnotifyfav = $this->boolean('emailnotifyfav');
334 $emailnotifymsg = $this->boolean('emailnotifymsg');
335 $emailnotifynudge = $this->boolean('emailnotifynudge');
336 $emailnotifyattn = $this->boolean('emailnotifyattn');
337 $emailmicroid = $this->boolean('emailmicroid');
338 $emailpost = $this->boolean('emailpost');
340 assert(!is_null($user)); // should already be checked
342 $user->query('BEGIN');
344 $original = clone($user);
346 $user->emailnotifysub = $emailnotifysub;
347 $user->emailnotifyfav = $emailnotifyfav;
348 $user->emailnotifymsg = $emailnotifymsg;
349 $user->emailnotifynudge = $emailnotifynudge;
350 $user->emailnotifyattn = $emailnotifyattn;
351 $user->emailmicroid = $emailmicroid;
352 $user->emailpost = $emailpost;
354 $result = $user->update($original);
356 if ($result === false) {
357 common_log_db_error($user, 'UPDATE', __FILE__);
358 // TRANS: Server error thrown on database error updating e-mail preferences.
359 $this->serverError(_('Couldn\'t update user.'));
363 $user->query('COMMIT');
365 Event::handle('EndEmailSaveForm', array($this));
367 // TRANS: Confirmation message for successful e-mail preferences save.
368 $this->showForm(_('Email preferences saved.'), true);
373 * Add the address passed in by the user
378 function addAddress()
380 $user = common_current_user();
382 $email = $this->trimmed('email');
387 // TRANS: Message given saving e-mail address without having provided one.
388 $this->showForm(_('No email address.'));
392 $email = common_canonical_email($email);
395 // TRANS: Message given saving e-mail address that cannot be normalised.
396 $this->showForm(_('Cannot normalize that email address'));
399 if (!Validate::email($email, common_config('email', 'check_domain'))) {
400 // TRANS: Message given saving e-mail address that not valid.
401 $this->showForm(_('Not a valid email address.'));
403 } else if ($user->email == $email) {
404 // TRANS: Message given saving e-mail address that is already set.
405 $this->showForm(_('That is already your email address.'));
407 } else if ($this->emailExists($email)) {
408 // TRANS: Message given saving e-mail address that is already set for another user.
409 $this->showForm(_('That email address already belongs '.
410 'to another user.'));
414 $confirm = new Confirm_address();
416 $confirm->address = $email;
417 $confirm->address_type = 'email';
418 $confirm->user_id = $user->id;
419 $confirm->code = common_confirmation_code(64);
421 $result = $confirm->insert();
423 if ($result === false) {
424 common_log_db_error($confirm, 'INSERT', __FILE__);
425 // TRANS: Server error thrown on database error adding e-mail confirmation code.
426 $this->serverError(_('Couldn\'t insert confirmation code.'));
430 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
432 // TRANS: Message given saving valid e-mail address that is to be confirmed.
433 $msg = _('A confirmation code was sent to the email address you added. '.
434 'Check your inbox (and spam box!) for the code and instructions '.
435 'on how to use it.');
437 $this->showForm($msg, true);
441 * Handle a request to cancel email confirmation
446 function cancelConfirmation()
448 $email = $this->arg('email');
450 $confirm = $this->getConfirmation();
453 // TRANS: Message given canceling e-mail address confirmation that is not pending.
454 $this->showForm(_('No pending confirmation to cancel.'));
457 if ($confirm->address != $email) {
458 // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
459 $this->showForm(_('That is the wrong email address.'));
463 $result = $confirm->delete();
466 common_log_db_error($confirm, 'DELETE', __FILE__);
467 // TRANS: Server error thrown on database error canceling e-mail address confirmation.
468 $this->serverError(_('Couldn\'t delete email confirmation.'));
472 // TRANS: Message given after successfully canceling e-mail address confirmation.
473 $this->showForm(_('Email confirmation cancelled.'), true);
477 * Handle a request to remove an address from the user's account
482 function removeAddress()
484 $user = common_current_user();
486 $email = $this->arg('email');
488 // Maybe an old tab open...?
490 if ($user->email != $email) {
491 // TRANS: Message given trying to remove an e-mail address that is not
492 // TRANS: registered for the active user.
493 $this->showForm(_('That is not your email address.'));
497 $user->query('BEGIN');
499 $original = clone($user);
503 $result = $user->updateKeys($original);
506 common_log_db_error($user, 'UPDATE', __FILE__);
507 // TRANS: Server error thrown on database error removing a registered e-mail address.
508 $this->serverError(_('Couldn\'t update user.'));
511 $user->query('COMMIT');
513 // TRANS: Message given after successfully removing a registered e-mail address.
514 $this->showForm(_('The email address was removed.'), true);
518 * Handle a request to remove an incoming email address
523 function removeIncoming()
525 $user = common_current_user();
527 if (!$user->incomingemail) {
528 $this->showForm(_('No incoming email address.'));
532 $orig = clone($user);
534 $user->incomingemail = null;
535 $user->emailpost = 0;
537 if (!$user->updateKeys($orig)) {
538 common_log_db_error($user, 'UPDATE', __FILE__);
539 // TRANS: Server error thrown on database error removing incoming e-mail address.
540 $this->serverError(_("Couldn't update user record."));
543 // TRANS: Message given after successfully removing an incoming e-mail address.
544 $this->showForm(_('Incoming email address removed.'), true);
548 * Generate a new incoming email address
553 function newIncoming()
555 $user = common_current_user();
557 $orig = clone($user);
559 $user->incomingemail = mail_new_incoming_address();
560 $user->emailpost = 1;
562 if (!$user->updateKeys($orig)) {
563 common_log_db_error($user, 'UPDATE', __FILE__);
564 // TRANS: Server error thrown on database error adding incoming e-mail address.
565 $this->serverError(_("Couldn't update user record."));
568 // TRANS: Message given after successfully adding an incoming e-mail address.
569 $this->showForm(_('New incoming email address added.'), true);
573 * Does another user already have this email address?
575 * Email addresses are unique for users.
577 * @param string $email Address to check
579 * @return boolean Whether the email already exists.
582 function emailExists($email)
584 $user = common_current_user();
586 $other = User::staticGet('email', $email);
591 return $other->id != $user->id;