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
27 function get_instructions()
29 return _('Manage how you get email from %%site.name%%.');
32 function show_form($msg=null, $success=false)
34 $user = common_current_user();
35 $this->form_header(_('Email Settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
37 'id' => 'emailsettings',
39 common_local_url('emailsettings')));
40 common_hidden('token', common_session_token());
42 common_element('h2', null, _('Address'));
45 common_element_start('p');
46 common_element('span', 'address confirmed', $user->email);
47 common_element('span', 'input_instructions',
48 _('Current confirmed email address.'));
49 common_hidden('email', $user->email);
50 common_element_end('p');
51 common_submit('remove', _('Remove'));
53 $confirm = $this->get_confirmation();
55 common_element_start('p');
56 common_element('span', 'address unconfirmed', $confirm->address);
57 common_element('span', 'input_instructions',
58 _('Awaiting confirmation on this address. Check your inbox (and spam box!) for a message with further instructions.'));
59 common_hidden('email', $confirm->address);
60 common_element_end('p');
61 common_submit('cancel', _('Cancel'));
63 common_input('email', _('Email Address'),
64 ($this->arg('email')) ? $this->arg('email') : null,
65 _('Email address, like "UserName@example.org"'));
66 common_submit('add', _('Add'));
71 common_element('h2', null, _('Incoming email'));
73 if ($user->incomingemail) {
74 common_element_start('p');
75 common_element('span', 'address', $user->incomingemail);
76 common_element('span', 'input_instructions',
77 _('Send email to this address to post new notices.'));
78 common_element_end('p');
79 common_submit('removeincoming', _('Remove'));
82 common_element_start('p');
83 common_element('span', 'input_instructions',
84 _('Make a new email address for posting to; cancels the old one.'));
85 common_element_end('p');
86 common_submit('newincoming', _('New'));
89 common_element('h2', null, _('Preferences'));
91 common_checkbox('emailnotifysub',
92 _('Send me notices of new subscriptions through email.'),
93 $user->emailnotifysub);
94 common_checkbox('emailnotifyfav',
95 _('Send me email when someone adds my notice as a favorite.'),
96 $user->emailnotifyfav);
97 common_checkbox('emailnotifymsg',
98 _('Send me email when someone sends me a private message.'),
99 $user->emailnotifymsg);
100 common_checkbox('emailnotifynudge',
101 _('Allow friends to nudge me and send me an email.'),
102 $user->emailnotifynudge);
103 common_checkbox('emailpost',
104 _('I want to post notices by email.'),
106 common_checkbox('emailmicroid',
107 _('Publish a MicroID for my email address.'),
108 $user->emailmicroid);
110 common_submit('save', _('Save'));
112 common_element_end('form');
113 common_show_footer();
116 function get_confirmation()
118 $user = common_current_user();
119 $confirm = new Confirm_address();
120 $confirm->user_id = $user->id;
121 $confirm->address_type = 'email';
122 if ($confirm->find(true)) {
129 function handle_post()
133 $token = $this->trimmed('token');
134 if (!$token || $token != common_session_token()) {
135 $this->show_form(_('There was a problem with your session token. Try again, please.'));
139 if ($this->arg('save')) {
140 $this->save_preferences();
141 } else if ($this->arg('add')) {
142 $this->add_address();
143 } else if ($this->arg('cancel')) {
144 $this->cancel_confirmation();
145 } else if ($this->arg('remove')) {
146 $this->remove_address();
147 } else if ($this->arg('removeincoming')) {
148 $this->remove_incoming();
149 } else if ($this->arg('newincoming')) {
150 $this->new_incoming();
152 $this->show_form(_('Unexpected form submission.'));
156 function save_preferences()
159 $emailnotifysub = $this->boolean('emailnotifysub');
160 $emailnotifyfav = $this->boolean('emailnotifyfav');
161 $emailnotifymsg = $this->boolean('emailnotifymsg');
162 $emailnotifynudge = $this->boolean('emailnotifynudge');
163 $emailmicroid = $this->boolean('emailmicroid');
164 $emailpost = $this->boolean('emailpost');
166 $user = common_current_user();
168 assert(!is_null($user)); # should already be checked
170 $user->query('BEGIN');
172 $original = clone($user);
174 $user->emailnotifysub = $emailnotifysub;
175 $user->emailnotifyfav = $emailnotifyfav;
176 $user->emailnotifymsg = $emailnotifymsg;
177 $user->emailnotifynudge = $emailnotifynudge;
178 $user->emailmicroid = $emailmicroid;
179 $user->emailpost = $emailpost;
181 $result = $user->update($original);
183 if ($result === false) {
184 common_log_db_error($user, 'UPDATE', __FILE__);
185 common_server_error(_('Couldn\'t update user.'));
189 $user->query('COMMIT');
191 $this->show_form(_('Preferences saved.'), true);
194 function add_address()
197 $user = common_current_user();
199 $email = $this->trimmed('email');
204 $this->show_form(_('No email address.'));
208 $email = common_canonical_email($email);
211 $this->show_form(_('Cannot normalize that email address'));
214 if (!Validate::email($email, true)) {
215 $this->show_form(_('Not a valid email address'));
217 } else if ($user->email == $email) {
218 $this->show_form(_('That is already your email address.'));
220 } else if ($this->email_exists($email)) {
221 $this->show_form(_('That email address already belongs to another user.'));
225 $confirm = new Confirm_address();
226 $confirm->address = $email;
227 $confirm->address_type = 'email';
228 $confirm->user_id = $user->id;
229 $confirm->code = common_confirmation_code(64);
231 $result = $confirm->insert();
233 if ($result === false) {
234 common_log_db_error($confirm, 'INSERT', __FILE__);
235 common_server_error(_('Couldn\'t insert confirmation code.'));
239 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
241 $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.');
243 $this->show_form($msg, true);
246 function cancel_confirmation()
248 $email = $this->arg('email');
249 $confirm = $this->get_confirmation();
251 $this->show_form(_('No pending confirmation to cancel.'));
254 if ($confirm->address != $email) {
255 $this->show_form(_('That is the wrong IM address.'));
259 $result = $confirm->delete();
262 common_log_db_error($confirm, 'DELETE', __FILE__);
263 $this->server_error(_('Couldn\'t delete email confirmation.'));
267 $this->show_form(_('Confirmation cancelled.'), true);
270 function remove_address()
273 $user = common_current_user();
274 $email = $this->arg('email');
276 # Maybe an old tab open...?
278 if ($user->email != $email) {
279 $this->show_form(_('That is not your email address.'));
283 $user->query('BEGIN');
284 $original = clone($user);
286 $result = $user->updateKeys($original);
288 common_log_db_error($user, 'UPDATE', __FILE__);
289 common_server_error(_('Couldn\'t update user.'));
292 $user->query('COMMIT');
294 $this->show_form(_('The address was removed.'), true);
297 function remove_incoming()
299 $user = common_current_user();
301 if (!$user->incomingemail) {
302 $this->show_form(_('No incoming email address.'));
306 $orig = clone($user);
307 $user->incomingemail = null;
309 if (!$user->updateKeys($orig)) {
310 common_log_db_error($user, 'UPDATE', __FILE__);
311 $this->server_error(_("Couldn't update user record."));
314 $this->show_form(_('Incoming email address removed.'), true);
317 function new_incoming()
319 $user = common_current_user();
321 $orig = clone($user);
322 $user->incomingemail = mail_new_incoming_address();
324 if (!$user->updateKeys($orig)) {
325 common_log_db_error($user, 'UPDATE', __FILE__);
326 $this->server_error(_("Couldn't update user record."));
329 $this->show_form(_('New incoming email address added.'), true);
332 function email_exists($email)
334 $user = common_current_user();
335 $other = User::staticGet('email', $email);
339 return $other->id != $user->id;