]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
csrf protection in userauthorization
[quix0rs-gnu-social.git] / actions / emailsettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class EmailsettingsAction extends SettingsAction {
25
26         function get_instructions() {
27                 return _('Manage how you get email from %%site.name%%.');
28         }
29
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',
35                                                                                    'action' =>
36                                                                                    common_local_url('emailsettings')));
37
38                 common_element('h2', NULL, _('Address'));
39
40                 if ($user->email) {
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'));
48                 } else {
49                         $confirm = $this->get_confirmation();
50                         if ($confirm) {
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'));
58                         } else {
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'));
63                         }
64                 }
65
66                 if ($user->email) {
67                         common_element('h2', NULL, _('Incoming email'));
68                         
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'));
76                         }
77                         
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'));
83                 }
84                 
85                 common_element('h2', NULL, _('Preferences'));
86
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.'),
92                                                 $user->emailpost);
93                 common_checkbox('emailmicroid',
94                                 _('Publish a MicroID for my email address.'),
95                                 $user->emailmicroid);
96
97                 common_submit('save', _('Save'));
98                 
99                 common_element_end('form');
100                 common_show_footer();
101         }
102
103         function get_confirmation() {
104                 $user = common_current_user();
105                 $confirm = new Confirm_address();
106                 $confirm->user_id = $user->id;
107                 $confirm->address_type = 'email';
108                 if ($confirm->find(TRUE)) {
109                         return $confirm;
110                 } else {
111                         return NULL;
112                 }
113         }
114
115         function handle_post() {
116
117                 if ($this->arg('save')) {
118                         $this->save_preferences();
119                 } else if ($this->arg('add')) {
120                         $this->add_address();
121                 } else if ($this->arg('cancel')) {
122                         $this->cancel_confirmation();
123                 } else if ($this->arg('remove')) {
124                         $this->remove_address();
125                 } else if ($this->arg('removeincoming')) {
126                         $this->remove_incoming();
127                 } else if ($this->arg('newincoming')) {
128                         $this->new_incoming();
129                 } else {
130                         $this->show_form(_('Unexpected form submission.'));
131                 }
132         }
133
134         function save_preferences() {
135
136                 $emailnotifysub = $this->boolean('emailnotifysub');
137                 $emailmicroid = $this->boolean('emailmicroid');
138                 $emailpost = $this->boolean('emailpost');
139
140                 $user = common_current_user();
141
142                 assert(!is_null($user)); # should already be checked
143
144                 $user->query('BEGIN');
145
146                 $original = clone($user);
147
148                 $user->emailnotifysub = $emailnotifysub;
149                 $user->emailmicroid = $emailmicroid;
150                 $user->emailpost = $emailpost;
151
152                 $result = $user->update($original);
153
154                 if ($result === FALSE) {
155                         common_log_db_error($user, 'UPDATE', __FILE__);
156                         common_server_error(_('Couldn\'t update user.'));
157                         return;
158                 }
159
160                 $user->query('COMMIT');
161
162                 $this->show_form(_('Preferences saved.'), true);
163         }
164
165         function add_address() {
166
167                 $user = common_current_user();
168
169                 $email = $this->trimmed('email');
170
171                 # Some validation
172
173                 if (!$email) {
174                         $this->show_form(_('No email address.'));
175                         return;
176                 }
177
178                 $email = common_canonical_email($email);
179
180                 if (!$email) {
181                     $this->show_form(_('Cannot normalize that email address'));
182                     return;
183                 }
184                 if (!Validate::email($email, true)) {
185                     $this->show_form(_('Not a valid email address'));
186                     return;
187                 } else if ($user->email == $email) {
188                     $this->show_form(_('That is already your email address.'));
189                     return;
190                 } else if ($this->email_exists($email)) {
191                     $this->show_form(_('That email address already belongs to another user.'));
192                     return;
193                 }
194
195                 $confirm = new Confirm_address();
196                 $confirm->address = $email;
197                 $confirm->address_type = 'email';
198                 $confirm->user_id = $user->id;
199                 $confirm->code = common_confirmation_code(64);
200
201                 $result = $confirm->insert();
202
203                 if ($result === FALSE) {
204                         common_log_db_error($confirm, 'INSERT', __FILE__);
205                         common_server_error(_('Couldn\'t insert confirmation code.'));
206                         return;
207                 }
208
209                 mail_confirm_address($confirm->code,
210                                                          $user->nickname,
211                                                          $email);
212
213                 $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.');
214
215                 $this->show_form($msg, TRUE);
216         }
217
218         function cancel_confirmation() {
219                 $email = $this->arg('email');
220                 $confirm = $this->get_confirmation();
221                 if (!$confirm) {
222                         $this->show_form(_('No pending confirmation to cancel.'));
223                         return;
224                 }
225                 if ($confirm->address != $email) {
226                         $this->show_form(_('That is the wrong IM address.'));
227                         return;
228                 }
229
230         $result = $confirm->delete();
231
232         if (!$result) {
233                         common_log_db_error($confirm, 'DELETE', __FILE__);
234             $this->server_error(_('Couldn\'t delete email confirmation.'));
235             return;
236         }
237
238         $this->show_form(_('Confirmation cancelled.'), TRUE);
239         }
240
241         function remove_address() {
242
243                 $user = common_current_user();
244                 $email = $this->arg('email');
245
246                 # Maybe an old tab open...?
247
248                 if ($user->email != $email) {
249                     $this->show_form(_('That is not your email address.'));
250                     return;
251                 }
252
253                 $user->query('BEGIN');
254                 $original = clone($user);
255                 $user->email = NULL;
256                 $result = $user->updateKeys($original);
257                 if (!$result) {
258                         common_log_db_error($user, 'UPDATE', __FILE__);
259                         common_server_error(_('Couldn\'t update user.'));
260                         return;
261                 }
262                 $user->query('COMMIT');
263
264                 $this->show_form(_('The address was removed.'), TRUE);
265         }
266
267         function remove_incoming() {
268                 $user = common_current_user();
269                 
270                 if (!$user->incomingemail) {
271                         $this->show_form(_('No incoming email address.'));
272                         return;
273                 }
274                 
275                 $orig = clone($user);
276                 $user->incomingemail = NULL;
277
278                 if (!$user->updateKeys($orig)) {
279                         common_log_db_error($user, 'UPDATE', __FILE__);
280                         $this->server_error(_("Couldn't update user record."));
281                 }
282                 
283                 $this->show_form(_('Incoming email address removed.'), TRUE);
284         }
285
286         function new_incoming() {
287                 $user = common_current_user();
288                 
289                 $orig = clone($user);
290                 $user->incomingemail = mail_new_incoming_address();
291                 
292                 if (!$user->updateKeys($orig)) {
293                         common_log_db_error($user, 'UPDATE', __FILE__);
294                         $this->server_error(_("Couldn't update user record."));
295                 }
296
297                 $this->show_form(_('New incoming email address added.'), TRUE);
298         }
299         
300         function email_exists($email) {
301                 $user = common_current_user();
302                 $other = User::staticGet('email', $email);
303                 if (!$other) {
304                         return false;
305                 } else {
306                         return $other->id != $user->id;
307                 }
308         }
309 }