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->autofocus('email');
86 * Content area of the page
88 * Shows a form for adding and removing email addresses and setting
94 function showContent()
96 $user = common_current_user();
98 $this->elementStart('form', array('method' => 'post',
99 'id' => 'form_settings_email',
100 'class' => 'form_settings',
102 common_local_url('emailsettings')));
103 $this->elementStart('fieldset');
104 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
105 // TRANS: Form legend for e-mail settings form.
106 $this->element('legend', null, _('Email address'));
107 $this->hidden('token', common_session_token());
110 $this->element('p', array('id' => 'form_confirmed'), $user->email);
111 // TRANS: Form note in e-mail settings form.
112 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
113 $this->hidden('email', $user->email);
114 // TRANS: Button label to remove a confirmed e-mail address.
115 $this->submit('remove', _m('BUTTON','Remove'));
117 $confirm = $this->getConfirmation();
119 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
120 // TRANS: Form note in e-mail settings form.
121 $this->element('p', array('class' => 'form_note'),
122 _('Awaiting confirmation on this address. '.
123 'Check your inbox (and spam box!) for a message '.
124 'with further instructions.'));
125 $this->hidden('email', $confirm->address);
126 // TRANS: Button label to cancel an e-mail address confirmation procedure.
127 $this->submit('cancel', _m('BUTTON','Cancel'));
129 $this->elementStart('ul', 'form_data');
130 $this->elementStart('li');
131 // TRANS: Field label for e-mail address input in e-mail settings form.
132 $this->input('email', _('Email address'),
133 ($this->arg('email')) ? $this->arg('email') : null,
134 // TRANS: Instructions for e-mail address input form. Do not translate
135 // TRANS: "example.org". It is one of the domain names reserved for
136 // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
137 // TRANS: Any other domain may be owned by a legitimate person or
138 // TRANS: organization.
139 _('Email address, like "UserName@example.org"'));
140 $this->elementEnd('li');
141 $this->elementEnd('ul');
142 // TRANS: Button label for adding an e-mail address in e-mail settings form.
143 $this->submit('add', _m('BUTTON','Add'));
146 $this->elementEnd('fieldset');
148 if (common_config('emailpost', 'enabled') && $user->email) {
149 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
150 // TRANS: Form legend for incoming e-mail settings form.
151 $this->element('legend', null, _('Incoming email'));
153 $this->elementStart('ul', 'form_data');
154 $this->elementStart('li');
155 $this->checkbox('emailpost',
156 // TRANS: Checkbox label in e-mail preferences form.
157 _('I want to post notices by email.'),
159 $this->elementEnd('li');
160 $this->elementEnd('ul');
162 if ($user->incomingemail) {
163 $this->elementStart('p');
164 $this->element('span', 'address', $user->incomingemail);
165 // XXX: Looks a little awkward in the UI.
166 // Something like "xxxx@identi.ca Send email ..". Needs improvement.
167 $this->element('span', 'input_instructions',
168 // TRANS: Form instructions for incoming e-mail form in e-mail settings.
169 _('Send email to this address to post new notices.'));
170 $this->elementEnd('p');
171 // TRANS: Button label for removing a set sender e-mail address to post notices from.
172 $this->submit('removeincoming', _m('BUTTON','Remove'));
175 $this->elementStart('p');
176 if ($user->incomingemail) {
177 // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
178 $msg = _('Make a new email address for posting to; '.
179 'cancels the old one.');
181 // TRANS: Instructions for incoming e-mail address input form.
182 $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
184 $this->element('span', 'input_instructions', $msg);
185 $this->elementEnd('p');
187 // TRANS: Button label for adding an e-mail address to send notices from.
188 $this->submit('newincoming', _m('BUTTON','New'));
189 $this->elementEnd('fieldset');
192 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
193 // TRANS: Form legend for e-mail preferences form.
194 $this->element('legend', null, _('Email preferences'));
196 $this->elementStart('ul', 'form_data');
198 if (Event::handle('StartEmailFormData', array($this))) {
199 $this->elementStart('li');
200 $this->checkbox('emailnotifysub',
201 // TRANS: Checkbox label in e-mail preferences form.
202 _('Send me notices of new subscriptions through email.'),
203 $user->emailnotifysub);
204 $this->elementEnd('li');
205 $this->elementStart('li');
206 $this->checkbox('emailnotifyfav',
207 // TRANS: Checkbox label in e-mail preferences form.
208 _('Send me email when someone '.
209 'adds my notice as a favorite.'),
210 $user->emailnotifyfav);
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 $this->elementStart('li');
231 $this->checkbox('emailmicroid',
232 // TRANS: Checkbox label in e-mail preferences form.
233 _('Publish a MicroID for my email address.'),
234 $user->emailmicroid);
235 $this->elementEnd('li');
236 Event::handle('EndEmailFormData', array($this));
238 $this->elementEnd('ul');
239 // TRANS: Button label to save e-mail preferences.
240 $this->submit('save', _m('BUTTON','Save'));
241 $this->elementEnd('fieldset');
242 $this->elementEnd('fieldset');
243 $this->elementEnd('form');
247 * Gets any existing email address confirmations we're waiting for
249 * @return Confirm_address Email address confirmation for user, or null
252 function getConfirmation()
254 $user = common_current_user();
256 $confirm = new Confirm_address();
258 $confirm->user_id = $user->id;
259 $confirm->address_type = 'email';
261 if ($confirm->find(true)) {
271 * Since there are a lot of different options on the page, we
272 * figure out what we're supposed to do based on which button was
278 function handlePost()
281 $token = $this->trimmed('token');
282 if (!$token || $token != common_session_token()) {
283 $this->show_form(_('There was a problem with your session token. '.
284 'Try again, please.'));
288 if ($this->arg('save')) {
289 $this->savePreferences();
290 } else if ($this->arg('add')) {
292 } else if ($this->arg('cancel')) {
293 $this->cancelConfirmation();
294 } else if ($this->arg('remove')) {
295 $this->removeAddress();
296 } else if ($this->arg('removeincoming')) {
297 $this->removeIncoming();
298 } else if ($this->arg('newincoming')) {
299 $this->newIncoming();
301 // TRANS: Message given submitting a form with an unknown action in e-mail settings.
302 $this->showForm(_('Unexpected form submission.'));
307 * Save email preferences
312 function savePreferences()
314 $user = common_current_user();
316 if (Event::handle('StartEmailSaveForm', array($this, &$user))) {
318 $emailnotifysub = $this->boolean('emailnotifysub');
319 $emailnotifyfav = $this->boolean('emailnotifyfav');
320 $emailnotifymsg = $this->boolean('emailnotifymsg');
321 $emailnotifynudge = $this->boolean('emailnotifynudge');
322 $emailnotifyattn = $this->boolean('emailnotifyattn');
323 $emailmicroid = $this->boolean('emailmicroid');
324 $emailpost = $this->boolean('emailpost');
326 assert(!is_null($user)); // should already be checked
328 $user->query('BEGIN');
330 $original = clone($user);
332 $user->emailnotifysub = $emailnotifysub;
333 $user->emailnotifyfav = $emailnotifyfav;
334 $user->emailnotifymsg = $emailnotifymsg;
335 $user->emailnotifynudge = $emailnotifynudge;
336 $user->emailnotifyattn = $emailnotifyattn;
337 $user->emailmicroid = $emailmicroid;
338 $user->emailpost = $emailpost;
340 $result = $user->update($original);
342 if ($result === false) {
343 common_log_db_error($user, 'UPDATE', __FILE__);
344 // TRANS: Server error thrown on database error updating e-mail preferences.
345 $this->serverError(_('Couldn\'t update user.'));
349 $user->query('COMMIT');
351 Event::handle('EndEmailSaveForm', array($this));
353 // TRANS: Confirmation message for successful e-mail preferences save.
354 $this->showForm(_('Email preferences saved.'), true);
359 * Add the address passed in by the user
364 function addAddress()
366 $user = common_current_user();
368 $email = $this->trimmed('email');
373 // TRANS: Message given saving e-mail address without having provided one.
374 $this->showForm(_('No email address.'));
378 $email = common_canonical_email($email);
381 // TRANS: Message given saving e-mail address that cannot be normalised.
382 $this->showForm(_('Cannot normalize that email address'));
385 if (!Validate::email($email, common_config('email', 'check_domain'))) {
386 // TRANS: Message given saving e-mail address that not valid.
387 $this->showForm(_('Not a valid email address.'));
389 } else if ($user->email == $email) {
390 // TRANS: Message given saving e-mail address that is already set.
391 $this->showForm(_('That is already your email address.'));
393 } else if ($this->emailExists($email)) {
394 // TRANS: Message given saving e-mail address that is already set for another user.
395 $this->showForm(_('That email address already belongs '.
396 'to another user.'));
400 $confirm = new Confirm_address();
402 $confirm->address = $email;
403 $confirm->address_type = 'email';
404 $confirm->user_id = $user->id;
405 $confirm->code = common_confirmation_code(64);
407 $result = $confirm->insert();
409 if ($result === false) {
410 common_log_db_error($confirm, 'INSERT', __FILE__);
411 // TRANS: Server error thrown on database error adding e-mail confirmation code.
412 $this->serverError(_('Couldn\'t insert confirmation code.'));
416 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
418 // TRANS: Message given saving valid e-mail address that is to be confirmed.
419 $msg = _('A confirmation code was sent to the email address you added. '.
420 'Check your inbox (and spam box!) for the code and instructions '.
421 'on how to use it.');
423 $this->showForm($msg, true);
427 * Handle a request to cancel email confirmation
432 function cancelConfirmation()
434 $email = $this->arg('email');
436 $confirm = $this->getConfirmation();
439 // TRANS: Message given canceling e-mail address confirmation that is not pending.
440 $this->showForm(_('No pending confirmation to cancel.'));
443 if ($confirm->address != $email) {
444 // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
445 $this->showForm(_('That is the wrong email address.'));
449 $result = $confirm->delete();
452 common_log_db_error($confirm, 'DELETE', __FILE__);
453 // TRANS: Server error thrown on database error canceling e-mail address confirmation.
454 $this->serverError(_('Couldn\'t delete email confirmation.'));
458 // TRANS: Message given after successfully canceling e-mail address confirmation.
459 $this->showForm(_('Email confirmation cancelled.'), true);
463 * Handle a request to remove an address from the user's account
468 function removeAddress()
470 $user = common_current_user();
472 $email = $this->arg('email');
474 // Maybe an old tab open...?
476 if ($user->email != $email) {
477 // TRANS: Message given trying to remove an e-mail address that is not
478 // TRANS: registered for the active user.
479 $this->showForm(_('That is not your email address.'));
483 $user->query('BEGIN');
485 $original = clone($user);
489 $result = $user->updateKeys($original);
492 common_log_db_error($user, 'UPDATE', __FILE__);
493 // TRANS: Server error thrown on database error removing a registered e-mail address.
494 $this->serverError(_('Couldn\'t update user.'));
497 $user->query('COMMIT');
499 // TRANS: Message given after successfully removing a registered e-mail address.
500 $this->showForm(_('The email address was removed.'), true);
504 * Handle a request to remove an incoming email address
509 function removeIncoming()
511 $user = common_current_user();
513 if (!$user->incomingemail) {
514 $this->showForm(_('No incoming email address.'));
518 $orig = clone($user);
520 $user->incomingemail = null;
522 if (!$user->updateKeys($orig)) {
523 common_log_db_error($user, 'UPDATE', __FILE__);
524 // TRANS: Server error thrown on database error removing incoming e-mail address.
525 $this->serverError(_("Couldn't update user record."));
528 // TRANS: Message given after successfully removing an incoming e-mail address.
529 $this->showForm(_('Incoming email address removed.'), true);
533 * Generate a new incoming email address
538 function newIncoming()
540 $user = common_current_user();
542 $orig = clone($user);
544 $user->incomingemail = mail_new_incoming_address();
546 if (!$user->updateKeys($orig)) {
547 common_log_db_error($user, 'UPDATE', __FILE__);
548 // TRANS: Server error thrown on database error adding incoming e-mail address.
549 $this->serverError(_("Couldn't update user record."));
552 // TRANS: Message given after successfully adding an incoming e-mail address.
553 $this->showForm(_('New incoming email address added.'), true);
557 * Does another user already have this email address?
559 * Email addresses are unique for users.
561 * @param string $email Address to check
563 * @return boolean Whether the email already exists.
566 function emailExists($email)
568 $user = common_current_user();
570 $other = User::staticGet('email', $email);
575 return $other->id != $user->id;