3 * Laconica, 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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/settingsaction.php';
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @author Zach Copley <zach@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
50 class EmailsettingsAction extends SettingsAction
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')));
92 $this->elementStart('fieldset');
93 $this->element('legend', null, _('Email settings'));
94 $this->hidden('token', common_session_token());
96 $this->elementStart('div', array('id' => 'settings_email_address'));
97 $this->element('h2', null, _('Address'));
100 $this->element('p', array('id' => 'email_confirmed', $user->email));
101 $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
102 $this->hidden('email', $user->email);
103 $this->submit('remove', _('Remove'));
105 $confirm = $this->getConfirmation();
107 $this->element('p', array('id' => 'email_unconfirmed'), $confirm->address);
108 $this->element('p', array('class' => 'form_note'),
109 _('Awaiting confirmation on this address. '.
110 'Check your inbox (and spam box!) for a message '.
111 'with further instructions.'));
112 $this->hidden('email', $confirm->address);
113 $this->submit('cancel', _('Cancel'));
115 $this->elementStart('ul', 'form_datas');
116 $this->elementStart('li');
117 $this->input('email', _('Email Address'),
118 ($this->arg('email')) ? $this->arg('email') : null,
119 _('Email address, like "UserName@example.org"'));
120 $this->elementEnd('li');
121 $this->elementEnd('ul');
122 $this->submit('add', _('Add'));
125 $this->elementEnd('div');
127 $this->elementStart('div', array('id' => 'settings_email_incoming'));
129 $this->element('h2', null, _('Incoming email'));
131 if ($user->incomingemail) {
132 $this->elementStart('p');
133 $this->element('span', 'address', $user->incomingemail);
134 $this->element('span', 'input_instructions',
135 _('Send email to this address to post new notices.'));
136 $this->elementEnd('p');
137 $this->submit('removeincoming', _('Remove'));
140 $this->elementStart('p');
141 $this->element('span', 'input_instructions',
142 _('Make a new email address for posting to; '.
143 'cancels the old one.'));
144 $this->elementEnd('p');
145 $this->submit('newincoming', _('New'));
147 $this->elementEnd('div');
149 $this->elementStart('div', array('id' => 'settings_email_preferences'));
150 $this->element('h2', null, _('Preferences'));
152 $this->elementStart('ul', 'form_datas');
153 $this->elementStart('li');
154 $this->checkbox('emailnotifysub',
155 _('Send me notices of new subscriptions through email.'),
156 $user->emailnotifysub);
157 $this->elementEnd('li');
158 $this->elementStart('li');
159 $this->checkbox('emailnotifyfav',
160 _('Send me email when someone '.
161 'adds my notice as a favorite.'),
162 $user->emailnotifyfav);
163 $this->elementEnd('li');
164 $this->elementStart('li');
165 $this->checkbox('emailnotifymsg',
166 _('Send me email when someone sends me a private message.'),
167 $user->emailnotifymsg);
168 $this->elementEnd('li');
169 $this->elementStart('li');
170 $this->checkbox('emailnotifynudge',
171 _('Allow friends to nudge me and send me an email.'),
172 $user->emailnotifynudge);
173 $this->elementEnd('li');
174 $this->elementStart('li');
175 $this->checkbox('emailpost',
176 _('I want to post notices by email.'),
178 $this->elementEnd('li');
179 $this->elementStart('li');
180 $this->checkbox('emailmicroid',
181 _('Publish a MicroID for my email address.'),
182 $user->emailmicroid);
183 $this->elementEnd('li');
184 $this->elementEnd('ul');
185 $this->elementEnd('div');
187 $this->submit('save', _('Save'));
189 $this->elementEnd('fieldset');
190 $this->elementEnd('form');
194 * Gets any existing email address confirmations we're waiting for
196 * @return Confirm_address Email address confirmation for user, or null
199 function getConfirmation()
201 $user = common_current_user();
203 $confirm = new Confirm_address();
205 $confirm->user_id = $user->id;
206 $confirm->address_type = 'email';
208 if ($confirm->find(true)) {
218 * Since there are a lot of different options on the page, we
219 * figure out what we're supposed to do based on which button was
225 function handlePost()
228 $token = $this->trimmed('token');
229 if (!$token || $token != common_session_token()) {
230 $this->show_form(_('There was a problem with your session token. '.
231 'Try again, please.'));
235 if ($this->arg('save')) {
236 $this->savePreferences();
237 } else if ($this->arg('add')) {
239 } else if ($this->arg('cancel')) {
240 $this->cancelConfirmation();
241 } else if ($this->arg('remove')) {
242 $this->removeAddress();
243 } else if ($this->arg('removeincoming')) {
244 $this->removeIncoming();
245 } else if ($this->arg('newincoming')) {
246 $this->newIncoming();
248 $this->showForm(_('Unexpected form submission.'));
253 * Save email preferences
258 function savePreferences()
260 $emailnotifysub = $this->boolean('emailnotifysub');
261 $emailnotifyfav = $this->boolean('emailnotifyfav');
262 $emailnotifymsg = $this->boolean('emailnotifymsg');
263 $emailnotifynudge = $this->boolean('emailnotifynudge');
264 $emailmicroid = $this->boolean('emailmicroid');
265 $emailpost = $this->boolean('emailpost');
267 $user = common_current_user();
269 assert(!is_null($user)); // should already be checked
271 $user->query('BEGIN');
273 $original = clone($user);
275 $user->emailnotifysub = $emailnotifysub;
276 $user->emailnotifyfav = $emailnotifyfav;
277 $user->emailnotifymsg = $emailnotifymsg;
278 $user->emailnotifynudge = $emailnotifynudge;
279 $user->emailmicroid = $emailmicroid;
280 $user->emailpost = $emailpost;
282 $result = $user->update($original);
284 if ($result === false) {
285 common_log_db_error($user, 'UPDATE', __FILE__);
286 $this->serverError(_('Couldn\'t update user.'));
290 $user->query('COMMIT');
292 $this->showForm(_('Preferences saved.'), true);
296 * Add the address passed in by the user
301 function addAddress()
303 $user = common_current_user();
305 $email = $this->trimmed('email');
310 $this->showForm(_('No email address.'));
314 $email = common_canonical_email($email);
317 $this->showForm(_('Cannot normalize that email address'));
320 if (!Validate::email($email, true)) {
321 $this->showForm(_('Not a valid email address'));
323 } else if ($user->email == $email) {
324 $this->showForm(_('That is already your email address.'));
326 } else if ($this->emailExists($email)) {
327 $this->showForm(_('That email address already belongs '.
328 'to another user.'));
332 $confirm = new Confirm_address();
334 $confirm->address = $email;
335 $confirm->address_type = 'email';
336 $confirm->user_id = $user->id;
337 $confirm->code = common_confirmation_code(64);
339 $result = $confirm->insert();
341 if ($result === false) {
342 common_log_db_error($confirm, 'INSERT', __FILE__);
343 $this->serverError(_('Couldn\'t insert confirmation code.'));
347 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
349 $msg = _('A confirmation code was sent to the email address you added. '.
350 'Check your inbox (and spam box!) for the code and instructions '.
351 'on how to use it.');
353 $this->showForm($msg, true);
357 * Handle a request to cancel email confirmation
362 function cancelConfirmation()
364 $email = $this->arg('email');
366 $confirm = $this->getConfirmation();
369 $this->showForm(_('No pending confirmation to cancel.'));
372 if ($confirm->address != $email) {
373 $this->showForm(_('That is the wrong IM address.'));
377 $result = $confirm->delete();
380 common_log_db_error($confirm, 'DELETE', __FILE__);
381 $this->serverError(_('Couldn\'t delete email confirmation.'));
385 $this->showForm(_('Confirmation cancelled.'), true);
389 * Handle a request to remove an address from the user's account
394 function removeAddress()
396 $user = common_current_user();
398 $email = $this->arg('email');
400 // Maybe an old tab open...?
402 if ($user->email != $email) {
403 $this->showForm(_('That is not your email address.'));
407 $user->query('BEGIN');
409 $original = clone($user);
413 $result = $user->updateKeys($original);
416 common_log_db_error($user, 'UPDATE', __FILE__);
417 $this->serverError(_('Couldn\'t update user.'));
420 $user->query('COMMIT');
422 $this->showForm(_('The address was removed.'), true);
426 * Handle a request to remove an incoming email address
431 function removeIncoming()
433 $user = common_current_user();
435 if (!$user->incomingemail) {
436 $this->showForm(_('No incoming email address.'));
440 $orig = clone($user);
442 $user->incomingemail = null;
444 if (!$user->updateKeys($orig)) {
445 common_log_db_error($user, 'UPDATE', __FILE__);
446 $this->serverError(_("Couldn't update user record."));
449 $this->showForm(_('Incoming email address removed.'), true);
453 * Generate a new incoming email address
458 function newIncoming()
460 $user = common_current_user();
462 $orig = clone($user);
464 $user->incomingemail = mail_new_incoming_address();
466 if (!$user->updateKeys($orig)) {
467 common_log_db_error($user, 'UPDATE', __FILE__);
468 $this->serverError(_("Couldn't update user record."));
471 $this->showForm(_('New incoming email address added.'), true);
475 * Does another user already have this email address?
477 * Email addresses are unique for users.
479 * @param string $email Address to check
481 * @return boolean Whether the email already exists.
484 function emailExists($email)
486 $user = common_current_user();
488 $other = User::staticGet('email', $email);
493 return $other->id != $user->id;