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() {
27 return _('Manage how you get email from %%site.name%%.');
30 function show_form($msg=NULL, $success=false) {
31 $user = common_current_user();
32 $this->form_header(_('Email Settings'), $msg, $success);
33 common_element_start('form', array('method' => 'post',
34 'id' => 'emailsettings',
36 common_local_url('emailsettings')));
38 common_element('h2', NULL, _('Address'));
41 common_element_start('p');
42 common_element('span', 'address confirmed', $user->email);
43 common_element('span', 'input_instructions',
44 _('Current confirmed email address.'));
45 common_hidden('email', $user->email);
46 common_element_end('p');
47 common_submit('remove', _('Remove'));
49 $confirm = $this->get_confirmation();
51 common_element_start('p');
52 common_element('span', 'address unconfirmed', $confirm->address);
53 common_element('span', 'input_instructions',
54 _('Awaiting confirmation on this address. Check your inbox (and spam box!) for a message with further instructions.'));
55 common_hidden('email', $confirm->address);
56 common_element_end('p');
57 common_submit('cancel', _('Cancel'));
59 common_input('email', _('Email Address'),
60 ($this->arg('email')) ? $this->arg('email') : NULL,
61 _('Email address, like "UserName@example.org"'));
62 common_submit('add', _('Add'));
67 common_element('h2', NULL, _('Incoming email'));
69 if ($user->incomingemail) {
70 common_element_start('p');
71 common_element('span', 'address', $user->incomingemail);
72 common_element('span', 'input_instructions',
73 _('Send email to this address to post new notices.'));
74 common_element_end('p');
75 common_submit('removeincoming', _('Remove'));
78 common_element_start('p');
79 common_element('span', 'input_instructions',
80 _('Make a new email address for posting to; cancels the old one.'));
81 common_element_end('p');
82 common_submit('newincoming', _('New'));
85 common_element('h2', NULL, _('Preferences'));
87 common_checkbox('emailnotifysub',
88 _('Send me notices of new subscriptions through email.'),
89 $user->emailnotifysub);
90 common_checkbox('emailpost',
91 _('I want to post notices by email.'),
94 common_submit('save', _('Save'));
96 common_element_end('form');
100 function get_confirmation() {
101 $user = common_current_user();
102 $confirm = new Confirm_address();
103 $confirm->user_id = $user->id;
104 $confirm->address_type = 'email';
105 if ($confirm->find(TRUE)) {
112 function handle_post() {
114 if ($this->arg('save')) {
115 $this->save_preferences();
116 } else if ($this->arg('add')) {
117 $this->add_address();
118 } else if ($this->arg('cancel')) {
119 $this->cancel_confirmation();
120 } else if ($this->arg('remove')) {
121 $this->remove_address();
122 } else if ($this->arg('removeincoming')) {
123 $this->remove_incoming();
124 } else if ($this->arg('newincoming')) {
125 $this->new_incoming();
127 $this->show_form(_('Unexpected form submission.'));
131 function save_preferences() {
133 $emailnotifysub = $this->boolean('emailnotifysub');
134 $emailpost = $this->boolean('emailpost');
136 $user = common_current_user();
138 assert(!is_null($user)); # should already be checked
140 $user->query('BEGIN');
142 $original = clone($user);
144 $user->emailnotifysub = $emailnotifysub;
145 $user->emailpost = $emailpost;
147 $result = $user->update($original);
149 if ($result === FALSE) {
150 common_log_db_error($user, 'UPDATE', __FILE__);
151 common_server_error(_('Couldn\'t update user.'));
155 $user->query('COMMIT');
157 $this->show_form(_('Preferences saved.'), true);
160 function add_address() {
162 $user = common_current_user();
164 $email = $this->trimmed('email');
169 $this->show_form(_('No email address.'));
173 $email = common_canonical_email($email);
176 $this->show_form(_('Cannot normalize that email address'));
179 if (!Validate::email($email, true)) {
180 $this->show_form(_('Not a valid email address'));
182 } else if ($user->email == $email) {
183 $this->show_form(_('That is already your email address.'));
185 } else if ($this->email_exists($email)) {
186 $this->show_form(_('That email address already belongs to another user.'));
190 $confirm = new Confirm_address();
191 $confirm->address = $email;
192 $confirm->address_type = 'email';
193 $confirm->user_id = $user->id;
194 $confirm->code = common_confirmation_code(64);
196 $result = $confirm->insert();
198 if ($result === FALSE) {
199 common_log_db_error($confirm, 'INSERT', __FILE__);
200 common_server_error(_('Couldn\'t insert confirmation code.'));
204 mail_confirm_address($confirm->code,
208 $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.');
210 $this->show_form($msg, TRUE);
213 function cancel_confirmation() {
214 $email = $this->arg('email');
215 $confirm = $this->get_confirmation();
217 $this->show_form(_('No pending confirmation to cancel.'));
220 if ($confirm->address != $email) {
221 $this->show_form(_('That is the wrong IM address.'));
225 $result = $confirm->delete();
228 common_log_db_error($confirm, 'DELETE', __FILE__);
229 $this->server_error(_('Couldn\'t delete email confirmation.'));
233 $this->show_form(_('Confirmation cancelled.'), TRUE);
236 function remove_address() {
238 $user = common_current_user();
239 $email = $this->arg('email');
241 # Maybe an old tab open...?
243 if ($user->email != $email) {
244 $this->show_form(_('That is not your email address.'));
248 $user->query('BEGIN');
249 $original = clone($user);
251 $result = $user->updateKeys($original);
253 common_log_db_error($user, 'UPDATE', __FILE__);
254 common_server_error(_('Couldn\'t update user.'));
257 $user->query('COMMIT');
259 $this->show_form(_('The address was removed.'), TRUE);
262 function remove_incoming() {
263 $user = common_current_user();
265 if (!$user->incomingemail) {
266 $this->show_form(_('No incoming email address.'));
270 $orig = clone($user);
271 $user->incomingemail = NULL;
273 if (!$user->updateKeys($orig)) {
274 common_log_db_error($user, 'UPDATE', __FILE__);
275 $this->server_error(_("Couldn't update user record."));
278 $this->show_form(_('Incoming email address removed.'), TRUE);
281 function new_incoming() {
282 $user = common_current_user();
284 $orig = clone($user);
285 $user->incomingemail = mail_new_incoming_address();
287 if (!$user->updateKeys($orig)) {
288 common_log_db_error($user, 'UPDATE', __FILE__);
289 $this->server_error(_("Couldn't update user record."));
292 $this->show_form(_('New incoming email address added.'), TRUE);
295 function email_exists($email) {
296 $user = common_current_user();
297 $other = User::staticGet('email', $email);
301 return $other->id != $user->id;