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 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%%.');
74 function showScripts()
76 parent::showScripts();
77 $this->autofocus('email');
81 * Content area of the page
83 * Shows a form for adding and removing email addresses and setting
89 function showContent()
91 $user = common_current_user();
93 $this->elementStart('form', array('method' => 'post',
94 'id' => 'form_settings_email',
95 'class' => 'form_settings',
97 common_local_url('emailsettings')));
99 $this->elementStart('fieldset', array('id' => 'settings_email_address'));
100 $this->element('legend', null, _('Address'));
101 $this->hidden('token', common_session_token());
104 $this->element('p', array('id' => 'form_confirmed'), $user->email);
105 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
106 $this->hidden('email', $user->email);
107 $this->submit('remove', _('Remove'));
109 $confirm = $this->getConfirmation();
111 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
112 $this->element('p', array('class' => 'form_note'),
113 _('Awaiting confirmation on this address. '.
114 'Check your inbox (and spam box!) for a message '.
115 'with further instructions.'));
116 $this->hidden('email', $confirm->address);
117 $this->submit('cancel', _('Cancel'));
119 $this->elementStart('ul', 'form_data');
120 $this->elementStart('li');
121 $this->input('email', _('Email Address'),
122 ($this->arg('email')) ? $this->arg('email') : null,
123 _('Email address, like "UserName@example.org"'));
124 $this->elementEnd('li');
125 $this->elementEnd('ul');
126 $this->submit('add', _('Add'));
129 $this->elementEnd('fieldset');
131 if (common_config('emailpost', 'enabled') && $user->email) {
132 $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
133 $this->element('legend',_('Incoming email'));
134 if ($user->incomingemail) {
135 $this->elementStart('p');
136 $this->element('span', 'address', $user->incomingemail);
137 $this->element('span', 'input_instructions',
138 _('Send email to this address to post new notices.'));
139 $this->elementEnd('p');
140 $this->submit('removeincoming', _('Remove'));
143 $this->elementStart('p');
144 $this->element('span', 'input_instructions',
145 _('Make a new email address for posting to; '.
146 'cancels the old one.'));
147 $this->elementEnd('p');
148 $this->submit('newincoming', _('New'));
149 $this->elementEnd('fieldset');
152 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
153 $this->element('legend', null, _('Preferences'));
155 $this->elementStart('ul', 'form_data');
156 $this->elementStart('li');
157 $this->checkbox('emailnotifysub',
158 _('Send me notices of new subscriptions through email.'),
159 $user->emailnotifysub);
160 $this->elementEnd('li');
161 $this->elementStart('li');
162 $this->checkbox('emailnotifyfav',
163 _('Send me email when someone '.
164 'adds my notice as a favorite.'),
165 $user->emailnotifyfav);
166 $this->elementEnd('li');
167 $this->elementStart('li');
168 $this->checkbox('emailnotifymsg',
169 _('Send me email when someone sends me a private message.'),
170 $user->emailnotifymsg);
171 $this->elementEnd('li');
172 $this->elementStart('li');
173 $this->checkbox('emailnotifyattn',
174 _('Send me email when someone sends me an "@-reply".'),
175 $user->emailnotifyattn);
176 $this->elementEnd('li');
177 $this->elementStart('li');
178 $this->checkbox('emailnotifynudge',
179 _('Allow friends to nudge me and send me an email.'),
180 $user->emailnotifynudge);
181 $this->elementEnd('li');
182 if (common_config('emailpost', 'enabled')) {
183 $this->elementStart('li');
184 $this->checkbox('emailpost',
185 _('I want to post notices by email.'),
187 $this->elementEnd('li');
189 $this->elementStart('li');
190 $this->checkbox('emailmicroid',
191 _('Publish a MicroID for my email address.'),
192 $user->emailmicroid);
193 $this->elementEnd('li');
194 $this->elementEnd('ul');
195 $this->submit('save', _('Save'));
196 $this->elementEnd('fieldset');
197 $this->elementEnd('form');
201 * Gets any existing email address confirmations we're waiting for
203 * @return Confirm_address Email address confirmation for user, or null
206 function getConfirmation()
208 $user = common_current_user();
210 $confirm = new Confirm_address();
212 $confirm->user_id = $user->id;
213 $confirm->address_type = 'email';
215 if ($confirm->find(true)) {
225 * Since there are a lot of different options on the page, we
226 * figure out what we're supposed to do based on which button was
232 function handlePost()
235 $token = $this->trimmed('token');
236 if (!$token || $token != common_session_token()) {
237 $this->show_form(_('There was a problem with your session token. '.
238 'Try again, please.'));
242 if ($this->arg('save')) {
243 $this->savePreferences();
244 } else if ($this->arg('add')) {
246 } else if ($this->arg('cancel')) {
247 $this->cancelConfirmation();
248 } else if ($this->arg('remove')) {
249 $this->removeAddress();
250 } else if ($this->arg('removeincoming')) {
251 $this->removeIncoming();
252 } else if ($this->arg('newincoming')) {
253 $this->newIncoming();
255 $this->showForm(_('Unexpected form submission.'));
260 * Save email preferences
265 function savePreferences()
267 $emailnotifysub = $this->boolean('emailnotifysub');
268 $emailnotifyfav = $this->boolean('emailnotifyfav');
269 $emailnotifymsg = $this->boolean('emailnotifymsg');
270 $emailnotifynudge = $this->boolean('emailnotifynudge');
271 $emailnotifyattn = $this->boolean('emailnotifyattn');
272 $emailmicroid = $this->boolean('emailmicroid');
273 $emailpost = $this->boolean('emailpost');
275 $user = common_current_user();
277 assert(!is_null($user)); // should already be checked
279 $user->query('BEGIN');
281 $original = clone($user);
283 $user->emailnotifysub = $emailnotifysub;
284 $user->emailnotifyfav = $emailnotifyfav;
285 $user->emailnotifymsg = $emailnotifymsg;
286 $user->emailnotifynudge = $emailnotifynudge;
287 $user->emailnotifyattn = $emailnotifyattn;
288 $user->emailmicroid = $emailmicroid;
289 $user->emailpost = $emailpost;
291 $result = $user->update($original);
293 if ($result === false) {
294 common_log_db_error($user, 'UPDATE', __FILE__);
295 $this->serverError(_('Couldn\'t update user.'));
299 $user->query('COMMIT');
301 $this->showForm(_('Preferences saved.'), true);
305 * Add the address passed in by the user
310 function addAddress()
312 $user = common_current_user();
314 $email = $this->trimmed('email');
319 $this->showForm(_('No email address.'));
323 $email = common_canonical_email($email);
326 $this->showForm(_('Cannot normalize that email address'));
329 if (!Validate::email($email, common_config('email', 'check_domain'))) {
330 $this->showForm(_('Not a valid email address'));
332 } else if ($user->email == $email) {
333 $this->showForm(_('That is already your email address.'));
335 } else if ($this->emailExists($email)) {
336 $this->showForm(_('That email address already belongs '.
337 'to another user.'));
341 $confirm = new Confirm_address();
343 $confirm->address = $email;
344 $confirm->address_type = 'email';
345 $confirm->user_id = $user->id;
346 $confirm->code = common_confirmation_code(64);
348 $result = $confirm->insert();
350 if ($result === false) {
351 common_log_db_error($confirm, 'INSERT', __FILE__);
352 $this->serverError(_('Couldn\'t insert confirmation code.'));
356 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
358 $msg = _('A confirmation code was sent to the email address you added. '.
359 'Check your inbox (and spam box!) for the code and instructions '.
360 'on how to use it.');
362 $this->showForm($msg, true);
366 * Handle a request to cancel email confirmation
371 function cancelConfirmation()
373 $email = $this->arg('email');
375 $confirm = $this->getConfirmation();
378 $this->showForm(_('No pending confirmation to cancel.'));
381 if ($confirm->address != $email) {
382 $this->showForm(_('That is the wrong IM address.'));
386 $result = $confirm->delete();
389 common_log_db_error($confirm, 'DELETE', __FILE__);
390 $this->serverError(_('Couldn\'t delete email confirmation.'));
394 $this->showForm(_('Confirmation cancelled.'), true);
398 * Handle a request to remove an address from the user's account
403 function removeAddress()
405 $user = common_current_user();
407 $email = $this->arg('email');
409 // Maybe an old tab open...?
411 if ($user->email != $email) {
412 $this->showForm(_('That is not your email address.'));
416 $user->query('BEGIN');
418 $original = clone($user);
422 $result = $user->updateKeys($original);
425 common_log_db_error($user, 'UPDATE', __FILE__);
426 $this->serverError(_('Couldn\'t update user.'));
429 $user->query('COMMIT');
431 $this->showForm(_('The address was removed.'), true);
435 * Handle a request to remove an incoming email address
440 function removeIncoming()
442 $user = common_current_user();
444 if (!$user->incomingemail) {
445 $this->showForm(_('No incoming email address.'));
449 $orig = clone($user);
451 $user->incomingemail = null;
453 if (!$user->updateKeys($orig)) {
454 common_log_db_error($user, 'UPDATE', __FILE__);
455 $this->serverError(_("Couldn't update user record."));
458 $this->showForm(_('Incoming email address removed.'), true);
462 * Generate a new incoming email address
467 function newIncoming()
469 $user = common_current_user();
471 $orig = clone($user);
473 $user->incomingemail = mail_new_incoming_address();
475 if (!$user->updateKeys($orig)) {
476 common_log_db_error($user, 'UPDATE', __FILE__);
477 $this->serverError(_("Couldn't update user record."));
480 $this->showForm(_('New incoming email address added.'), true);
484 * Does another user already have this email address?
486 * Email addresses are unique for users.
488 * @param string $email Address to check
490 * @return boolean Whether the email already exists.
493 function emailExists($email)
495 $user = common_current_user();
497 $other = User::staticGet('email', $email);
502 return $other->id != $user->id;