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('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 return _('Email Settings');
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 return _('Manage how you get email from %%site.name%%.');
75 * Content area of the page
77 * Shows a form for adding and removing email addresses and setting
83 function showContent()
85 $user = common_current_user();
87 $this->elementStart('form', array('method' => 'post',
88 'id' => 'form_settings_email',
89 'class' => 'form_settings',
91 common_local_url('emailsettings')));
93 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
94 $this->element('legend', null, _('Address'));
95 $this->hidden('token', common_session_token());
98 $this->element('p', array('id' => 'form_confirmed'), $user->email);
99 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
100 $this->hidden('email', $user->email);
101 $this->submit('remove', _('Remove'));
103 $confirm = $this->getConfirmation();
105 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
106 $this->element('p', array('class' => 'form_note'),
107 _('Awaiting confirmation on this address. '.
108 'Check your inbox (and spam box!) for a message '.
109 'with further instructions.'));
110 $this->hidden('email', $confirm->address);
111 $this->submit('cancel', _('Cancel'));
113 $this->elementStart('ul', 'form_data');
114 $this->elementStart('li');
115 $this->input('email', _('Email Address'),
116 ($this->arg('email')) ? $this->arg('email') : null,
117 _('Email address, like "UserName@example.org"'));
118 $this->elementEnd('li');
119 $this->elementEnd('ul');
120 $this->submit('add', _('Add'));
123 $this->elementEnd('fieldset');
125 if (common_config('emailpost', 'enabled') && $user->email) {
126 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
127 $this->element('legend',_('Incoming email'));
128 if ($user->incomingemail) {
129 $this->elementStart('p');
130 $this->element('span', 'address', $user->incomingemail);
131 $this->element('span', 'input_instructions',
132 _('Send email to this address to post new notices.'));
133 $this->elementEnd('p');
134 $this->submit('removeincoming', _('Remove'));
137 $this->elementStart('p');
138 $this->element('span', 'input_instructions',
139 _('Make a new email address for posting to; '.
140 'cancels the old one.'));
141 $this->elementEnd('p');
142 $this->submit('newincoming', _('New'));
143 $this->elementEnd('fieldset');
146 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
147 $this->element('legend', null, _('Preferences'));
149 $this->elementStart('ul', 'form_data');
150 $this->elementStart('li');
151 $this->checkbox('emailnotifysub',
152 _('Send me notices of new subscriptions through email.'),
153 $user->emailnotifysub);
154 $this->elementEnd('li');
155 $this->elementStart('li');
156 $this->checkbox('emailnotifyfav',
157 _('Send me email when someone '.
158 'adds my notice as a favorite.'),
159 $user->emailnotifyfav);
160 $this->elementEnd('li');
161 $this->elementStart('li');
162 $this->checkbox('emailnotifymsg',
163 _('Send me email when someone sends me a private message.'),
164 $user->emailnotifymsg);
165 $this->elementEnd('li');
166 $this->elementStart('li');
167 $this->checkbox('emailnotifyattn',
168 _('Send me email when someone sends me an "@-reply".'),
169 $user->emailnotifyattn);
170 $this->elementEnd('li');
171 $this->elementStart('li');
172 $this->checkbox('emailnotifynudge',
173 _('Allow friends to nudge me and send me an email.'),
174 $user->emailnotifynudge);
175 $this->elementEnd('li');
176 if (common_config('emailpost', 'enabled')) {
177 $this->elementStart('li');
178 $this->checkbox('emailpost',
179 _('I want to post notices by email.'),
181 $this->elementEnd('li');
183 $this->elementStart('li');
184 $this->checkbox('emailmicroid',
185 _('Publish a MicroID for my email address.'),
186 $user->emailmicroid);
187 $this->elementEnd('li');
188 $this->elementEnd('ul');
189 $this->submit('save', _('Save'));
190 $this->elementEnd('fieldset');
191 $this->elementEnd('form');
195 * Gets any existing email address confirmations we're waiting for
197 * @return Confirm_address Email address confirmation for user, or null
200 function getConfirmation()
202 $user = common_current_user();
204 $confirm = new Confirm_address();
206 $confirm->user_id = $user->id;
207 $confirm->address_type = 'email';
209 if ($confirm->find(true)) {
219 * Since there are a lot of different options on the page, we
220 * figure out what we're supposed to do based on which button was
226 function handlePost()
229 $token = $this->trimmed('token');
230 if (!$token || $token != common_session_token()) {
231 $this->show_form(_('There was a problem with your session token. '.
232 'Try again, please.'));
236 if ($this->arg('save')) {
237 $this->savePreferences();
238 } else if ($this->arg('add')) {
240 } else if ($this->arg('cancel')) {
241 $this->cancelConfirmation();
242 } else if ($this->arg('remove')) {
243 $this->removeAddress();
244 } else if ($this->arg('removeincoming')) {
245 $this->removeIncoming();
246 } else if ($this->arg('newincoming')) {
247 $this->newIncoming();
249 $this->showForm(_('Unexpected form submission.'));
254 * Save email preferences
259 function savePreferences()
261 $emailnotifysub = $this->boolean('emailnotifysub');
262 $emailnotifyfav = $this->boolean('emailnotifyfav');
263 $emailnotifymsg = $this->boolean('emailnotifymsg');
264 $emailnotifynudge = $this->boolean('emailnotifynudge');
265 $emailnotifyattn = $this->boolean('emailnotifyattn');
266 $emailmicroid = $this->boolean('emailmicroid');
267 $emailpost = $this->boolean('emailpost');
269 $user = common_current_user();
271 assert(!is_null($user)); // should already be checked
273 $user->query('BEGIN');
275 $original = clone($user);
277 $user->emailnotifysub = $emailnotifysub;
278 $user->emailnotifyfav = $emailnotifyfav;
279 $user->emailnotifymsg = $emailnotifymsg;
280 $user->emailnotifynudge = $emailnotifynudge;
281 $user->emailnotifyattn = $emailnotifyattn;
282 $user->emailmicroid = $emailmicroid;
283 $user->emailpost = $emailpost;
285 $result = $user->update($original);
287 if ($result === false) {
288 common_log_db_error($user, 'UPDATE', __FILE__);
289 $this->serverError(_('Couldn\'t update user.'));
293 $user->query('COMMIT');
295 $this->showForm(_('Preferences saved.'), true);
299 * Add the address passed in by the user
304 function addAddress()
306 $user = common_current_user();
308 $email = $this->trimmed('email');
313 $this->showForm(_('No email address.'));
317 $email = common_canonical_email($email);
320 $this->showForm(_('Cannot normalize that email address'));
323 if (!Validate::email($email, true)) {
324 $this->showForm(_('Not a valid email address'));
326 } else if ($user->email == $email) {
327 $this->showForm(_('That is already your email address.'));
329 } else if ($this->emailExists($email)) {
330 $this->showForm(_('That email address already belongs '.
331 'to another user.'));
335 $confirm = new Confirm_address();
337 $confirm->address = $email;
338 $confirm->address_type = 'email';
339 $confirm->user_id = $user->id;
340 $confirm->code = common_confirmation_code(64);
342 $result = $confirm->insert();
344 if ($result === false) {
345 common_log_db_error($confirm, 'INSERT', __FILE__);
346 $this->serverError(_('Couldn\'t insert confirmation code.'));
350 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
352 $msg = _('A confirmation code was sent to the email address you added. '.
353 'Check your inbox (and spam box!) for the code and instructions '.
354 'on how to use it.');
356 $this->showForm($msg, true);
360 * Handle a request to cancel email confirmation
365 function cancelConfirmation()
367 $email = $this->arg('email');
369 $confirm = $this->getConfirmation();
372 $this->showForm(_('No pending confirmation to cancel.'));
375 if ($confirm->address != $email) {
376 $this->showForm(_('That is the wrong IM address.'));
380 $result = $confirm->delete();
383 common_log_db_error($confirm, 'DELETE', __FILE__);
384 $this->serverError(_('Couldn\'t delete email confirmation.'));
388 $this->showForm(_('Confirmation cancelled.'), true);
392 * Handle a request to remove an address from the user's account
397 function removeAddress()
399 $user = common_current_user();
401 $email = $this->arg('email');
403 // Maybe an old tab open...?
405 if ($user->email != $email) {
406 $this->showForm(_('That is not your email address.'));
410 $user->query('BEGIN');
412 $original = clone($user);
416 $result = $user->updateKeys($original);
419 common_log_db_error($user, 'UPDATE', __FILE__);
420 $this->serverError(_('Couldn\'t update user.'));
423 $user->query('COMMIT');
425 $this->showForm(_('The address was removed.'), true);
429 * Handle a request to remove an incoming email address
434 function removeIncoming()
436 $user = common_current_user();
438 if (!$user->incomingemail) {
439 $this->showForm(_('No incoming email address.'));
443 $orig = clone($user);
445 $user->incomingemail = null;
447 if (!$user->updateKeys($orig)) {
448 common_log_db_error($user, 'UPDATE', __FILE__);
449 $this->serverError(_("Couldn't update user record."));
452 $this->showForm(_('Incoming email address removed.'), true);
456 * Generate a new incoming email address
461 function newIncoming()
463 $user = common_current_user();
465 $orig = clone($user);
467 $user->incomingemail = mail_new_incoming_address();
469 if (!$user->updateKeys($orig)) {
470 common_log_db_error($user, 'UPDATE', __FILE__);
471 $this->serverError(_("Couldn't update user record."));
474 $this->showForm(_('New incoming email address added.'), true);
478 * Does another user already have this email address?
480 * Email addresses are unique for users.
482 * @param string $email Address to check
484 * @return boolean Whether the email already exists.
487 function emailExists($email)
489 $user = common_current_user();
491 $other = User::staticGet('email', $email);
496 return $other->id != $user->id;