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' => 'emailsettings',
90 common_local_url('emailsettings')));
92 $this->hidden('token', common_session_token());
94 $this->element('h2', null, _('Address'));
97 $this->elementStart('p');
98 $this->element('span', 'address confirmed', $user->email);
99 $this->element('span', 'input_instructions',
100 _('Current confirmed email address.'));
101 $this->hidden('email', $user->email);
102 $this->elementEnd('p');
103 $this->submit('remove', _('Remove'));
105 $confirm = $this->getConfirmation();
107 $this->elementStart('p');
108 $this->element('span', 'address unconfirmed', $confirm->address);
109 $this->element('span', 'input_instructions',
110 _('Awaiting confirmation on this address. '.
111 'Check your inbox (and spam box!) for a message '.
112 'with further instructions.'));
113 $this->hidden('email', $confirm->address);
114 $this->elementEnd('p');
115 $this->submit('cancel', _('Cancel'));
117 $this->input('email', _('Email Address'),
118 ($this->arg('email')) ? $this->arg('email') : null,
119 _('Email address, like "UserName@example.org"'));
120 $this->submit('add', _('Add'));
125 $this->element('h2', null, _('Incoming email'));
127 if ($user->incomingemail) {
128 $this->elementStart('p');
129 $this->element('span', 'address', $user->incomingemail);
130 $this->element('span', 'input_instructions',
131 _('Send email to this address to post new notices.'));
132 $this->elementEnd('p');
133 $this->submit('removeincoming', _('Remove'));
136 $this->elementStart('p');
137 $this->element('span', 'input_instructions',
138 _('Make a new email address for posting to; '.
139 'cancels the old one.'));
140 $this->elementEnd('p');
141 $this->submit('newincoming', _('New'));
144 $this->element('h2', null, _('Preferences'));
146 $this->checkbox('emailnotifysub',
147 _('Send me notices of new subscriptions through email.'),
148 $user->emailnotifysub);
149 $this->checkbox('emailnotifyfav',
150 _('Send me email when someone '.
151 'adds my notice as a favorite.'),
152 $user->emailnotifyfav);
153 $this->checkbox('emailnotifymsg',
154 _('Send me email when someone sends me a private message.'),
155 $user->emailnotifymsg);
156 $this->checkbox('emailnotifynudge',
157 _('Allow friends to nudge me and send me an email.'),
158 $user->emailnotifynudge);
159 $this->checkbox('emailpost',
160 _('I want to post notices by email.'),
162 $this->checkbox('emailmicroid',
163 _('Publish a MicroID for my email address.'),
164 $user->emailmicroid);
166 $this->submit('save', _('Save'));
168 $this->elementEnd('form');
172 * Gets any existing email address confirmations we're waiting for
174 * @return Confirm_address Email address confirmation for user, or null
177 function getConfirmation()
179 $user = common_current_user();
181 $confirm = new Confirm_address();
183 $confirm->user_id = $user->id;
184 $confirm->address_type = 'email';
186 if ($confirm->find(true)) {
196 * Since there are a lot of different options on the page, we
197 * figure out what we're supposed to do based on which button was
203 function handlePost()
206 $token = $this->trimmed('token');
207 if (!$token || $token != common_session_token()) {
208 $this->show_form(_('There was a problem with your session token. '.
209 'Try again, please.'));
213 if ($this->arg('save')) {
214 $this->savePreferences();
215 } else if ($this->arg('add')) {
217 } else if ($this->arg('cancel')) {
218 $this->cancelConfirmation();
219 } else if ($this->arg('remove')) {
220 $this->removeAddress();
221 } else if ($this->arg('removeincoming')) {
222 $this->removeIncoming();
223 } else if ($this->arg('newincoming')) {
224 $this->newIncoming();
226 $this->showForm(_('Unexpected form submission.'));
231 * Save email preferences
236 function savePreferences()
238 $emailnotifysub = $this->boolean('emailnotifysub');
239 $emailnotifyfav = $this->boolean('emailnotifyfav');
240 $emailnotifymsg = $this->boolean('emailnotifymsg');
241 $emailnotifynudge = $this->boolean('emailnotifynudge');
242 $emailmicroid = $this->boolean('emailmicroid');
243 $emailpost = $this->boolean('emailpost');
245 $user = common_current_user();
247 assert(!is_null($user)); // should already be checked
249 $user->query('BEGIN');
251 $original = clone($user);
253 $user->emailnotifysub = $emailnotifysub;
254 $user->emailnotifyfav = $emailnotifyfav;
255 $user->emailnotifymsg = $emailnotifymsg;
256 $user->emailnotifynudge = $emailnotifynudge;
257 $user->emailmicroid = $emailmicroid;
258 $user->emailpost = $emailpost;
260 $result = $user->update($original);
262 if ($result === false) {
263 common_log_db_error($user, 'UPDATE', __FILE__);
264 $this->serverError(_('Couldn\'t update user.'));
268 $user->query('COMMIT');
270 $this->showForm(_('Preferences saved.'), true);
274 * Add the address passed in by the user
279 function addAddress()
281 $user = common_current_user();
283 $email = $this->trimmed('email');
288 $this->showForm(_('No email address.'));
292 $email = common_canonical_email($email);
295 $this->showForm(_('Cannot normalize that email address'));
298 if (!Validate::email($email, true)) {
299 $this->showForm(_('Not a valid email address'));
301 } else if ($user->email == $email) {
302 $this->showForm(_('That is already your email address.'));
304 } else if ($this->emailExists($email)) {
305 $this->showForm(_('That email address already belongs '.
306 'to another user.'));
310 $confirm = new Confirm_address();
312 $confirm->address = $email;
313 $confirm->address_type = 'email';
314 $confirm->user_id = $user->id;
315 $confirm->code = common_confirmation_code(64);
317 $result = $confirm->insert();
319 if ($result === false) {
320 common_log_db_error($confirm, 'INSERT', __FILE__);
321 $this->serverError(_('Couldn\'t insert confirmation code.'));
325 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
327 $msg = _('A confirmation code was sent to the email address you added. '.
328 'Check your inbox (and spam box!) for the code and instructions '.
329 'on how to use it.');
331 $this->showForm($msg, true);
335 * Handle a request to cancel email confirmation
340 function cancelConfirmation()
342 $email = $this->arg('email');
344 $confirm = $this->getConfirmation();
347 $this->showForm(_('No pending confirmation to cancel.'));
350 if ($confirm->address != $email) {
351 $this->showForm(_('That is the wrong IM address.'));
355 $result = $confirm->delete();
358 common_log_db_error($confirm, 'DELETE', __FILE__);
359 $this->serverError(_('Couldn\'t delete email confirmation.'));
363 $this->showForm(_('Confirmation cancelled.'), true);
367 * Handle a request to remove an address from the user's account
372 function removeAddress()
374 $user = common_current_user();
376 $email = $this->arg('email');
378 // Maybe an old tab open...?
380 if ($user->email != $email) {
381 $this->showForm(_('That is not your email address.'));
385 $user->query('BEGIN');
387 $original = clone($user);
391 $result = $user->updateKeys($original);
394 common_log_db_error($user, 'UPDATE', __FILE__);
395 $this->serverError(_('Couldn\'t update user.'));
398 $user->query('COMMIT');
400 $this->showForm(_('The address was removed.'), true);
404 * Handle a request to remove an incoming email address
409 function removeIncoming()
411 $user = common_current_user();
413 if (!$user->incomingemail) {
414 $this->showForm(_('No incoming email address.'));
418 $orig = clone($user);
420 $user->incomingemail = null;
422 if (!$user->updateKeys($orig)) {
423 common_log_db_error($user, 'UPDATE', __FILE__);
424 $this->serverError(_("Couldn't update user record."));
427 $this->showForm(_('Incoming email address removed.'), true);
431 * Generate a new incoming email address
436 function newIncoming()
438 $user = common_current_user();
440 $orig = clone($user);
442 $user->incomingemail = mail_new_incoming_address();
444 if (!$user->updateKeys($orig)) {
445 common_log_db_error($user, 'UPDATE', __FILE__);
446 $this->serverError(_("Couldn't update user record."));
449 $this->showForm(_('New incoming email address added.'), true);
453 * Does another user already have this email address?
455 * Email addresses are unique for users.
457 * @param string $email Address to check
459 * @return boolean Whether the email already exists.
462 function emailExists($email)
464 $user = common_current_user();
466 $other = User::staticGet('email', $email);
471 return $other->id != $user->id;