3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
24 class EmailsettingsAction extends SettingsAction {
26 function get_instructions()
28 return _('Manage how you get email from %%site.name%%.');
31 function show_form($msg=null, $success=false)
33 $user = common_current_user();
34 $this->form_header(_('Email Settings'), $msg, $success);
35 common_element_start('form', array('method' => 'post',
36 'id' => 'emailsettings',
38 common_local_url('emailsettings')));
39 common_hidden('token', common_session_token());
41 common_element('h2', null, _('Address'));
44 common_element_start('p');
45 common_element('span', 'address confirmed', $user->email);
46 common_element('span', 'input_instructions',
47 _('Current confirmed email address.'));
48 common_hidden('email', $user->email);
49 common_element_end('p');
50 common_submit('remove', _('Remove'));
52 $confirm = $this->get_confirmation();
54 common_element_start('p');
55 common_element('span', 'address unconfirmed', $confirm->address);
56 common_element('span', 'input_instructions',
57 _('Awaiting confirmation on this address. Check your inbox (and spam box!) for a message with further instructions.'));
58 common_hidden('email', $confirm->address);
59 common_element_end('p');
60 common_submit('cancel', _('Cancel'));
62 common_input('email', _('Email Address'),
63 ($this->arg('email')) ? $this->arg('email') : null,
64 _('Email address, like "UserName@example.org"'));
65 common_submit('add', _('Add'));
70 common_element('h2', null, _('Incoming email'));
72 if ($user->incomingemail) {
73 common_element_start('p');
74 common_element('span', 'address', $user->incomingemail);
75 common_element('span', 'input_instructions',
76 _('Send email to this address to post new notices.'));
77 common_element_end('p');
78 common_submit('removeincoming', _('Remove'));
81 common_element_start('p');
82 common_element('span', 'input_instructions',
83 _('Make a new email address for posting to; cancels the old one.'));
84 common_element_end('p');
85 common_submit('newincoming', _('New'));
88 common_element('h2', null, _('Preferences'));
90 common_checkbox('emailnotifysub',
91 _('Send me notices of new subscriptions through email.'),
92 $user->emailnotifysub);
93 common_checkbox('emailnotifyfav',
94 _('Send me email when someone adds my notice as a favorite.'),
95 $user->emailnotifyfav);
96 common_checkbox('emailnotifymsg',
97 _('Send me email when someone sends me a private message.'),
98 $user->emailnotifymsg);
99 common_checkbox('emailnotifynudge',
100 _('Allow friends to nudge me and send me an email.'),
101 $user->emailnotifynudge);
102 common_checkbox('emailpost',
103 _('I want to post notices by email.'),
105 common_checkbox('emailmicroid',
106 _('Publish a MicroID for my email address.'),
107 $user->emailmicroid);
109 common_submit('save', _('Save'));
111 common_element_end('form');
112 common_show_footer();
115 function get_confirmation()
117 $user = common_current_user();
118 $confirm = new Confirm_address();
119 $confirm->user_id = $user->id;
120 $confirm->address_type = 'email';
121 if ($confirm->find(true)) {
128 function handle_post()
132 $token = $this->trimmed('token');
133 if (!$token || $token != common_session_token()) {
134 $this->show_form(_('There was a problem with your session token. Try again, please.'));
138 if ($this->arg('save')) {
139 $this->save_preferences();
140 } else if ($this->arg('add')) {
141 $this->add_address();
142 } else if ($this->arg('cancel')) {
143 $this->cancel_confirmation();
144 } else if ($this->arg('remove')) {
145 $this->remove_address();
146 } else if ($this->arg('removeincoming')) {
147 $this->remove_incoming();
148 } else if ($this->arg('newincoming')) {
149 $this->new_incoming();
151 $this->show_form(_('Unexpected form submission.'));
155 function save_preferences()
158 $emailnotifysub = $this->boolean('emailnotifysub');
159 $emailnotifyfav = $this->boolean('emailnotifyfav');
160 $emailnotifymsg = $this->boolean('emailnotifymsg');
161 $emailnotifynudge = $this->boolean('emailnotifynudge');
162 $emailmicroid = $this->boolean('emailmicroid');
163 $emailpost = $this->boolean('emailpost');
165 $user = common_current_user();
167 assert(!is_null($user)); # should already be checked
169 $user->query('BEGIN');
171 $original = clone($user);
173 $user->emailnotifysub = $emailnotifysub;
174 $user->emailnotifyfav = $emailnotifyfav;
175 $user->emailnotifymsg = $emailnotifymsg;
176 $user->emailnotifynudge = $emailnotifynudge;
177 $user->emailmicroid = $emailmicroid;
178 $user->emailpost = $emailpost;
180 $result = $user->update($original);
182 if ($result === false) {
183 common_log_db_error($user, 'UPDATE', __FILE__);
184 common_server_error(_('Couldn\'t update user.'));
188 $user->query('COMMIT');
190 $this->show_form(_('Preferences saved.'), true);
193 function add_address()
196 $user = common_current_user();
198 $email = $this->trimmed('email');
203 $this->show_form(_('No email address.'));
207 $email = common_canonical_email($email);
210 $this->show_form(_('Cannot normalize that email address'));
213 if (!Validate::email($email, true)) {
214 $this->show_form(_('Not a valid email address'));
216 } else if ($user->email == $email) {
217 $this->show_form(_('That is already your email address.'));
219 } else if ($this->email_exists($email)) {
220 $this->show_form(_('That email address already belongs to another user.'));
224 $confirm = new Confirm_address();
225 $confirm->address = $email;
226 $confirm->address_type = 'email';
227 $confirm->user_id = $user->id;
228 $confirm->code = common_confirmation_code(64);
230 $result = $confirm->insert();
232 if ($result === false) {
233 common_log_db_error($confirm, 'INSERT', __FILE__);
234 common_server_error(_('Couldn\'t insert confirmation code.'));
238 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
240 $msg = _('A confirmation code was sent to the email address you added. Check your inbox (and spam box!) for the code and instructions on how to use it.');
242 $this->show_form($msg, true);
245 function cancel_confirmation()
247 $email = $this->arg('email');
248 $confirm = $this->get_confirmation();
250 $this->show_form(_('No pending confirmation to cancel.'));
253 if ($confirm->address != $email) {
254 $this->show_form(_('That is the wrong IM address.'));
258 $result = $confirm->delete();
261 common_log_db_error($confirm, 'DELETE', __FILE__);
262 $this->server_error(_('Couldn\'t delete email confirmation.'));
266 $this->show_form(_('Confirmation cancelled.'), true);
269 function remove_address()
272 $user = common_current_user();
273 $email = $this->arg('email');
275 # Maybe an old tab open...?
277 if ($user->email != $email) {
278 $this->show_form(_('That is not your email address.'));
282 $user->query('BEGIN');
283 $original = clone($user);
285 $result = $user->updateKeys($original);
287 common_log_db_error($user, 'UPDATE', __FILE__);
288 common_server_error(_('Couldn\'t update user.'));
291 $user->query('COMMIT');
293 $this->show_form(_('The address was removed.'), true);
296 function remove_incoming()
298 $user = common_current_user();
300 if (!$user->incomingemail) {
301 $this->show_form(_('No incoming email address.'));
305 $orig = clone($user);
306 $user->incomingemail = null;
308 if (!$user->updateKeys($orig)) {
309 common_log_db_error($user, 'UPDATE', __FILE__);
310 $this->server_error(_("Couldn't update user record."));
313 $this->show_form(_('Incoming email address removed.'), true);
316 function new_incoming()
318 $user = common_current_user();
320 $orig = clone($user);
321 $user->incomingemail = mail_new_incoming_address();
323 if (!$user->updateKeys($orig)) {
324 common_log_db_error($user, 'UPDATE', __FILE__);
325 $this->server_error(_("Couldn't update user record."));
328 $this->show_form(_('New incoming email address added.'), true);
331 function email_exists($email)
333 $user = common_current_user();
334 $other = User::staticGet('email', $email);
338 return $other->id != $user->id;