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')));
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' => 'email_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' => 'email_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_datas');
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');
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');
147 $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
148 $this->element('legend', null, _('Preferences'));
150 $this->elementStart('ul', 'form_datas');
151 $this->elementStart('li');
152 $this->checkbox('emailnotifysub',
153 _('Send me notices of new subscriptions through email.'),
154 $user->emailnotifysub);
155 $this->elementEnd('li');
156 $this->elementStart('li');
157 $this->checkbox('emailnotifyfav',
158 _('Send me email when someone '.
159 'adds my notice as a favorite.'),
160 $user->emailnotifyfav);
161 $this->elementEnd('li');
162 $this->elementStart('li');
163 $this->checkbox('emailnotifymsg',
164 _('Send me email when someone sends me a private message.'),
165 $user->emailnotifymsg);
166 $this->elementEnd('li');
167 $this->elementStart('li');
168 $this->checkbox('emailnotifynudge',
169 _('Allow friends to nudge me and send me an email.'),
170 $user->emailnotifynudge);
171 $this->elementEnd('li');
172 $this->elementStart('li');
173 $this->checkbox('emailpost',
174 _('I want to post notices by email.'),
176 $this->elementEnd('li');
177 $this->elementStart('li');
178 $this->checkbox('emailmicroid',
179 _('Publish a MicroID for my email address.'),
180 $user->emailmicroid);
181 $this->elementEnd('li');
182 $this->elementEnd('ul');
183 $this->submit('save', _('Save'));
184 $this->elementEnd('fieldset');
185 $this->elementEnd('form');
189 * Gets any existing email address confirmations we're waiting for
191 * @return Confirm_address Email address confirmation for user, or null
194 function getConfirmation()
196 $user = common_current_user();
198 $confirm = new Confirm_address();
200 $confirm->user_id = $user->id;
201 $confirm->address_type = 'email';
203 if ($confirm->find(true)) {
213 * Since there are a lot of different options on the page, we
214 * figure out what we're supposed to do based on which button was
220 function handlePost()
223 $token = $this->trimmed('token');
224 if (!$token || $token != common_session_token()) {
225 $this->show_form(_('There was a problem with your session token. '.
226 'Try again, please.'));
230 if ($this->arg('save')) {
231 $this->savePreferences();
232 } else if ($this->arg('add')) {
234 } else if ($this->arg('cancel')) {
235 $this->cancelConfirmation();
236 } else if ($this->arg('remove')) {
237 $this->removeAddress();
238 } else if ($this->arg('removeincoming')) {
239 $this->removeIncoming();
240 } else if ($this->arg('newincoming')) {
241 $this->newIncoming();
243 $this->showForm(_('Unexpected form submission.'));
248 * Save email preferences
253 function savePreferences()
255 $emailnotifysub = $this->boolean('emailnotifysub');
256 $emailnotifyfav = $this->boolean('emailnotifyfav');
257 $emailnotifymsg = $this->boolean('emailnotifymsg');
258 $emailnotifynudge = $this->boolean('emailnotifynudge');
259 $emailmicroid = $this->boolean('emailmicroid');
260 $emailpost = $this->boolean('emailpost');
262 $user = common_current_user();
264 assert(!is_null($user)); // should already be checked
266 $user->query('BEGIN');
268 $original = clone($user);
270 $user->emailnotifysub = $emailnotifysub;
271 $user->emailnotifyfav = $emailnotifyfav;
272 $user->emailnotifymsg = $emailnotifymsg;
273 $user->emailnotifynudge = $emailnotifynudge;
274 $user->emailmicroid = $emailmicroid;
275 $user->emailpost = $emailpost;
277 $result = $user->update($original);
279 if ($result === false) {
280 common_log_db_error($user, 'UPDATE', __FILE__);
281 $this->serverError(_('Couldn\'t update user.'));
285 $user->query('COMMIT');
287 $this->showForm(_('Preferences saved.'), true);
291 * Add the address passed in by the user
296 function addAddress()
298 $user = common_current_user();
300 $email = $this->trimmed('email');
305 $this->showForm(_('No email address.'));
309 $email = common_canonical_email($email);
312 $this->showForm(_('Cannot normalize that email address'));
315 if (!Validate::email($email, true)) {
316 $this->showForm(_('Not a valid email address'));
318 } else if ($user->email == $email) {
319 $this->showForm(_('That is already your email address.'));
321 } else if ($this->emailExists($email)) {
322 $this->showForm(_('That email address already belongs '.
323 'to another user.'));
327 $confirm = new Confirm_address();
329 $confirm->address = $email;
330 $confirm->address_type = 'email';
331 $confirm->user_id = $user->id;
332 $confirm->code = common_confirmation_code(64);
334 $result = $confirm->insert();
336 if ($result === false) {
337 common_log_db_error($confirm, 'INSERT', __FILE__);
338 $this->serverError(_('Couldn\'t insert confirmation code.'));
342 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
344 $msg = _('A confirmation code was sent to the email address you added. '.
345 'Check your inbox (and spam box!) for the code and instructions '.
346 'on how to use it.');
348 $this->showForm($msg, true);
352 * Handle a request to cancel email confirmation
357 function cancelConfirmation()
359 $email = $this->arg('email');
361 $confirm = $this->getConfirmation();
364 $this->showForm(_('No pending confirmation to cancel.'));
367 if ($confirm->address != $email) {
368 $this->showForm(_('That is the wrong IM address.'));
372 $result = $confirm->delete();
375 common_log_db_error($confirm, 'DELETE', __FILE__);
376 $this->serverError(_('Couldn\'t delete email confirmation.'));
380 $this->showForm(_('Confirmation cancelled.'), true);
384 * Handle a request to remove an address from the user's account
389 function removeAddress()
391 $user = common_current_user();
393 $email = $this->arg('email');
395 // Maybe an old tab open...?
397 if ($user->email != $email) {
398 $this->showForm(_('That is not your email address.'));
402 $user->query('BEGIN');
404 $original = clone($user);
408 $result = $user->updateKeys($original);
411 common_log_db_error($user, 'UPDATE', __FILE__);
412 $this->serverError(_('Couldn\'t update user.'));
415 $user->query('COMMIT');
417 $this->showForm(_('The address was removed.'), true);
421 * Handle a request to remove an incoming email address
426 function removeIncoming()
428 $user = common_current_user();
430 if (!$user->incomingemail) {
431 $this->showForm(_('No incoming email address.'));
435 $orig = clone($user);
437 $user->incomingemail = null;
439 if (!$user->updateKeys($orig)) {
440 common_log_db_error($user, 'UPDATE', __FILE__);
441 $this->serverError(_("Couldn't update user record."));
444 $this->showForm(_('Incoming email address removed.'), true);
448 * Generate a new incoming email address
453 function newIncoming()
455 $user = common_current_user();
457 $orig = clone($user);
459 $user->incomingemail = mail_new_incoming_address();
461 if (!$user->updateKeys($orig)) {
462 common_log_db_error($user, 'UPDATE', __FILE__);
463 $this->serverError(_("Couldn't update user record."));
466 $this->showForm(_('New incoming email address added.'), true);
470 * Does another user already have this email address?
472 * Email addresses are unique for users.
474 * @param string $email Address to check
476 * @return boolean Whether the email already exists.
479 function emailExists($email)
481 $user = common_current_user();
483 $other = User::staticGet('email', $email);
488 return $other->id != $user->id;