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 ImsettingsAction 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'));
66 common_element('h2', NULL, _('Preferences'));
68 common_checkbox('emailnotifysub',
69 _('Send me notices of new subscriptions through email.'),
70 $user->emailnotifysub);
72 common_submit('save', _('Save'));
74 common_element_end('form');
78 function get_confirmation() {
79 $user = common_current_user();
80 $confirm = new Confirm_address();
81 $confirm->user_id = $user->id;
82 $confirm->address_type = 'email';
83 if ($confirm->find(TRUE)) {
90 function handle_post() {
92 if ($this->arg('save')) {
93 $this->save_preferences();
94 } else if ($this->arg('add')) {
96 } else if ($this->arg('cancel')) {
97 $this->cancel_confirmation();
98 } else if ($this->arg('remove')) {
99 $this->remove_address();
101 $this->show_form(_('Unexpected form submission.'));
105 function save_preferences() {
107 $emailnotifysub = $this->boolean('emailnotifysub');
109 $user = common_current_user();
111 assert(!is_null($user)); # should already be checked
113 $user->query('BEGIN');
115 $original = clone($user);
117 $user->emailnotifysub = $emailnotifysub;
119 $result = $user->update($original);
121 if ($result === FALSE) {
122 common_log_db_error($user, 'UPDATE', __FILE__);
123 common_server_error(_('Couldn\'t update user.'));
127 $user->query('COMMIT');
129 $this->show_form(_('Preferences saved.'), true);
132 function add_address() {
134 $user = common_current_user();
136 $email = $this->trimmed('email');
141 $this->show_form(_('No email address.'));
145 $email = common_normalize_email($email);
148 $this->show_form(_('Cannot normalize that email address'));
151 if (!Validate::email($email, true)) {
152 $this->show_form(_('Not a valid email address'));
154 } else if ($user->email == $email) {
155 $this->show_form(_('That is already your email address.'));
157 } else if ($this->email_exists($email)) {
158 $this->show_form(_('That email address already belongs to another user.'));
162 $confirm = new Confirm_address();
163 $confirm->address = $email;
164 $confirm->address_type = 'email';
165 $confirm->user_id = $user->id;
166 $confirm->code = common_confirmation_code(64);
168 $result = $confirm->insert();
170 if ($result === FALSE) {
171 common_log_db_error($confirm, 'INSERT', __FILE__);
172 common_server_error(_('Couldn\'t insert confirmation code.'));
176 $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.');
178 $this->show_form($msg, TRUE);
181 function cancel_confirmation() {
182 $email = $this->arg('email');
183 $confirm = $this->get_confirmation();
185 $this->show_form(_('No pending confirmation to cancel.'));
188 if ($confirm->address != $email) {
189 $this->show_form(_('That is the wrong IM address.'));
193 $result = $confirm->delete();
196 common_log_db_error($confirm, 'DELETE', __FILE__);
197 $this->server_error(_('Couldn\'t delete email confirmation.'));
201 $this->show_form(_('Confirmation cancelled.'), TRUE);
204 function remove_address() {
206 $user = common_current_user();
207 $email = $this->arg('email');
209 # Maybe an old tab open...?
211 if ($user->email != $email) {
212 $this->show_form(_('That is not your email address.'));
216 $user->query('BEGIN');
217 $original = clone($user);
219 $result = $user->updateKeys($original);
221 common_log_db_error($user, 'UPDATE', __FILE__);
222 common_server_error(_('Couldn\'t update user.'));
225 $user->query('COMMIT');
227 # XXX: unsubscribe to the old address
229 $this->show_form(_('The address was removed.'), TRUE);
232 function email_exists($email) {
233 $user = common_current_user();
234 $other = User::staticGet('email', $email);
238 return $other->id != $user->id;