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')));
98 $this->elementStart('fieldset');
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('fieldset');
198 $this->elementEnd('form');
202 * Gets any existing email address confirmations we're waiting for
204 * @return Confirm_address Email address confirmation for user, or null
207 function getConfirmation()
209 $user = common_current_user();
211 $confirm = new Confirm_address();
213 $confirm->user_id = $user->id;
214 $confirm->address_type = 'email';
216 if ($confirm->find(true)) {
226 * Since there are a lot of different options on the page, we
227 * figure out what we're supposed to do based on which button was
233 function handlePost()
236 $token = $this->trimmed('token');
237 if (!$token || $token != common_session_token()) {
238 $this->show_form(_('There was a problem with your session token. '.
239 'Try again, please.'));
243 if ($this->arg('save')) {
244 $this->savePreferences();
245 } else if ($this->arg('add')) {
247 } else if ($this->arg('cancel')) {
248 $this->cancelConfirmation();
249 } else if ($this->arg('remove')) {
250 $this->removeAddress();
251 } else if ($this->arg('removeincoming')) {
252 $this->removeIncoming();
253 } else if ($this->arg('newincoming')) {
254 $this->newIncoming();
256 $this->showForm(_('Unexpected form submission.'));
261 * Save email preferences
266 function savePreferences()
268 $emailnotifysub = $this->boolean('emailnotifysub');
269 $emailnotifyfav = $this->boolean('emailnotifyfav');
270 $emailnotifymsg = $this->boolean('emailnotifymsg');
271 $emailnotifynudge = $this->boolean('emailnotifynudge');
272 $emailnotifyattn = $this->boolean('emailnotifyattn');
273 $emailmicroid = $this->boolean('emailmicroid');
274 $emailpost = $this->boolean('emailpost');
276 $user = common_current_user();
278 assert(!is_null($user)); // should already be checked
280 $user->query('BEGIN');
282 $original = clone($user);
284 $user->emailnotifysub = $emailnotifysub;
285 $user->emailnotifyfav = $emailnotifyfav;
286 $user->emailnotifymsg = $emailnotifymsg;
287 $user->emailnotifynudge = $emailnotifynudge;
288 $user->emailnotifyattn = $emailnotifyattn;
289 $user->emailmicroid = $emailmicroid;
290 $user->emailpost = $emailpost;
292 $result = $user->update($original);
294 if ($result === false) {
295 common_log_db_error($user, 'UPDATE', __FILE__);
296 $this->serverError(_('Couldn\'t update user.'));
300 $user->query('COMMIT');
302 $this->showForm(_('Preferences saved.'), true);
306 * Add the address passed in by the user
311 function addAddress()
313 $user = common_current_user();
315 $email = $this->trimmed('email');
320 $this->showForm(_('No email address.'));
324 $email = common_canonical_email($email);
327 $this->showForm(_('Cannot normalize that email address'));
330 if (!Validate::email($email, common_config('email', 'check_domain'))) {
331 $this->showForm(_('Not a valid email address'));
333 } else if ($user->email == $email) {
334 $this->showForm(_('That is already your email address.'));
336 } else if ($this->emailExists($email)) {
337 $this->showForm(_('That email address already belongs '.
338 'to another user.'));
342 $confirm = new Confirm_address();
344 $confirm->address = $email;
345 $confirm->address_type = 'email';
346 $confirm->user_id = $user->id;
347 $confirm->code = common_confirmation_code(64);
349 $result = $confirm->insert();
351 if ($result === false) {
352 common_log_db_error($confirm, 'INSERT', __FILE__);
353 $this->serverError(_('Couldn\'t insert confirmation code.'));
357 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
359 $msg = _('A confirmation code was sent to the email address you added. '.
360 'Check your inbox (and spam box!) for the code and instructions '.
361 'on how to use it.');
363 $this->showForm($msg, true);
367 * Handle a request to cancel email confirmation
372 function cancelConfirmation()
374 $email = $this->arg('email');
376 $confirm = $this->getConfirmation();
379 $this->showForm(_('No pending confirmation to cancel.'));
382 if ($confirm->address != $email) {
383 $this->showForm(_('That is the wrong IM address.'));
387 $result = $confirm->delete();
390 common_log_db_error($confirm, 'DELETE', __FILE__);
391 $this->serverError(_('Couldn\'t delete email confirmation.'));
395 $this->showForm(_('Confirmation cancelled.'), true);
399 * Handle a request to remove an address from the user's account
404 function removeAddress()
406 $user = common_current_user();
408 $email = $this->arg('email');
410 // Maybe an old tab open...?
412 if ($user->email != $email) {
413 $this->showForm(_('That is not your email address.'));
417 $user->query('BEGIN');
419 $original = clone($user);
423 $result = $user->updateKeys($original);
426 common_log_db_error($user, 'UPDATE', __FILE__);
427 $this->serverError(_('Couldn\'t update user.'));
430 $user->query('COMMIT');
432 $this->showForm(_('The address was removed.'), true);
436 * Handle a request to remove an incoming email address
441 function removeIncoming()
443 $user = common_current_user();
445 if (!$user->incomingemail) {
446 $this->showForm(_('No incoming email address.'));
450 $orig = clone($user);
452 $user->incomingemail = null;
454 if (!$user->updateKeys($orig)) {
455 common_log_db_error($user, 'UPDATE', __FILE__);
456 $this->serverError(_("Couldn't update user record."));
459 $this->showForm(_('Incoming email address removed.'), true);
463 * Generate a new incoming email address
468 function newIncoming()
470 $user = common_current_user();
472 $orig = clone($user);
474 $user->incomingemail = mail_new_incoming_address();
476 if (!$user->updateKeys($orig)) {
477 common_log_db_error($user, 'UPDATE', __FILE__);
478 $this->serverError(_("Couldn't update user record."));
481 $this->showForm(_('New incoming email address added.'), true);
485 * Does another user already have this email address?
487 * Email addresses are unique for users.
489 * @param string $email Address to check
491 * @return boolean Whether the email already exists.
494 function emailExists($email)
496 $user = common_current_user();
498 $other = User::staticGet('email', $email);
503 return $other->id != $user->id;