]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
fix registration bug - fatal error when sending email confirmation
[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                 common_hidden('token', common_session_token());
38
39                 common_element('h2', NULL, _('Address'));
40
41                 if ($user->email) {
42                         common_element_start('p');
43                         common_element('span', 'address confirmed', $user->email);
44                         common_element('span', 'input_instructions',
45                                        _('Current confirmed email address.'));
46                         common_hidden('email', $user->email);
47                         common_element_end('p');
48                         common_submit('remove', _('Remove'));
49                 } else {
50                         $confirm = $this->get_confirmation();
51                         if ($confirm) {
52                                 common_element_start('p');
53                                 common_element('span', 'address unconfirmed', $confirm->address);
54                                 common_element('span', 'input_instructions',
55                                                            _('Awaiting confirmation on this address. Check your inbox (and spam box!) for a message with further instructions.'));
56                                 common_hidden('email', $confirm->address);
57                                 common_element_end('p');
58                                 common_submit('cancel', _('Cancel'));
59                         } else {
60                                 common_input('email', _('Email Address'),
61                                                          ($this->arg('email')) ? $this->arg('email') : NULL,
62                                                          _('Email address, like "UserName@example.org"'));
63                                 common_submit('add', _('Add'));
64                         }
65                 }
66
67                 if ($user->email) {
68                         common_element('h2', NULL, _('Incoming email'));
69                         
70                         if ($user->incomingemail) {
71                                 common_element_start('p');
72                                 common_element('span', 'address', $user->incomingemail);
73                                 common_element('span', 'input_instructions',
74                                                            _('Send email to this address to post new notices.'));
75                                 common_element_end('p');
76                                 common_submit('removeincoming', _('Remove'));
77                         }
78                         
79                         common_element_start('p');
80                         common_element('span', 'input_instructions',
81                                                    _('Make a new email address for posting to; cancels the old one.'));
82                         common_element_end('p');
83                         common_submit('newincoming', _('New'));
84                 }
85                 
86                 common_element('h2', NULL, _('Preferences'));
87
88                 common_checkbox('emailnotifysub',
89                                 _('Send me notices of new subscriptions through email.'),
90                                 $user->emailnotifysub);
91                 common_checkbox('emailnotifyfav',
92                                 _('Send me email when someone adds my notice as a favorite.'),
93                                 $user->emailnotifyfav);
94                 common_checkbox('emailnotifymsg',
95                                 _('Send me email when someone sends me a private message.'),
96                                 $user->emailnotifymsg);
97                 common_checkbox('emailnotifynudge',
98                                 _('Allow friends to nudge me and send me an email.'),
99                                 $user->emailnotifynudge);
100                 common_checkbox('emailpost',
101                                                 _('I want to post notices by email.'),
102                                                 $user->emailpost);
103                 common_checkbox('emailmicroid',
104                                 _('Publish a MicroID for my email address.'),
105                                 $user->emailmicroid);
106
107                 common_submit('save', _('Save'));
108                 
109                 common_element_end('form');
110                 common_show_footer();
111         }
112
113         function get_confirmation() {
114                 $user = common_current_user();
115                 $confirm = new Confirm_address();
116                 $confirm->user_id = $user->id;
117                 $confirm->address_type = 'email';
118                 if ($confirm->find(TRUE)) {
119                         return $confirm;
120                 } else {
121                         return NULL;
122                 }
123         }
124
125         function handle_post() {
126
127                 # CSRF protection
128                 $token = $this->trimmed('token');
129                 if (!$token || $token != common_session_token()) {
130                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
131                         return;
132                 }
133
134                 if ($this->arg('save')) {
135                         $this->save_preferences();
136                 } else if ($this->arg('add')) {
137                         $this->add_address();
138                 } else if ($this->arg('cancel')) {
139                         $this->cancel_confirmation();
140                 } else if ($this->arg('remove')) {
141                         $this->remove_address();
142                 } else if ($this->arg('removeincoming')) {
143                         $this->remove_incoming();
144                 } else if ($this->arg('newincoming')) {
145                         $this->new_incoming();
146                 } else {
147                         $this->show_form(_('Unexpected form submission.'));
148                 }
149         }
150
151         function save_preferences() {
152
153                 $emailnotifysub = $this->boolean('emailnotifysub');
154                 $emailnotifyfav = $this->boolean('emailnotifyfav');
155                 $emailnotifymsg = $this->boolean('emailnotifymsg');
156                 $emailnotifynudge = $this->boolean('emailnotifynudge');
157                 $emailmicroid = $this->boolean('emailmicroid');
158                 $emailpost = $this->boolean('emailpost');
159
160                 $user = common_current_user();
161
162                 assert(!is_null($user)); # should already be checked
163
164                 $user->query('BEGIN');
165
166                 $original = clone($user);
167
168                 $user->emailnotifysub = $emailnotifysub;
169                 $user->emailnotifyfav = $emailnotifyfav;
170                 $user->emailnotifymsg = $emailnotifymsg;
171                 $user->emailnotifynudge = $emailnotifynudge;
172                 $user->emailmicroid = $emailmicroid;
173                 $user->emailpost = $emailpost;
174
175                 $result = $user->update($original);
176
177                 if ($result === FALSE) {
178                         common_log_db_error($user, 'UPDATE', __FILE__);
179                         common_server_error(_('Couldn\'t update user.'));
180                         return;
181                 }
182
183                 $user->query('COMMIT');
184
185                 $this->show_form(_('Preferences saved.'), true);
186         }
187
188         function add_address() {
189
190                 $user = common_current_user();
191
192                 $email = $this->trimmed('email');
193
194                 # Some validation
195
196                 if (!$email) {
197                         $this->show_form(_('No email address.'));
198                         return;
199                 }
200
201                 $email = common_canonical_email($email);
202
203                 if (!$email) {
204                     $this->show_form(_('Cannot normalize that email address'));
205                     return;
206                 }
207                 if (!Validate::email($email, true)) {
208                     $this->show_form(_('Not a valid email address'));
209                     return;
210                 } else if ($user->email == $email) {
211                     $this->show_form(_('That is already your email address.'));
212                     return;
213                 } else if ($this->email_exists($email)) {
214                     $this->show_form(_('That email address already belongs to another user.'));
215                     return;
216                 }
217
218                 $confirm = new Confirm_address();
219                 $confirm->address = $email;
220                 $confirm->address_type = 'email';
221                 $confirm->user_id = $user->id;
222                 $confirm->code = common_confirmation_code(64);
223
224                 $result = $confirm->insert();
225
226                 if ($result === FALSE) {
227                         common_log_db_error($confirm, 'INSERT', __FILE__);
228                         common_server_error(_('Couldn\'t insert confirmation code.'));
229                         return;
230                 }
231
232                 mail_confirm_address($user, $confirm->code, $user->nickname, $email);
233
234                 $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.');
235
236                 $this->show_form($msg, TRUE);
237         }
238
239         function cancel_confirmation() {
240                 $email = $this->arg('email');
241                 $confirm = $this->get_confirmation();
242                 if (!$confirm) {
243                         $this->show_form(_('No pending confirmation to cancel.'));
244                         return;
245                 }
246                 if ($confirm->address != $email) {
247                         $this->show_form(_('That is the wrong IM address.'));
248                         return;
249                 }
250
251         $result = $confirm->delete();
252
253         if (!$result) {
254                         common_log_db_error($confirm, 'DELETE', __FILE__);
255             $this->server_error(_('Couldn\'t delete email confirmation.'));
256             return;
257         }
258
259         $this->show_form(_('Confirmation cancelled.'), TRUE);
260         }
261
262         function remove_address() {
263
264                 $user = common_current_user();
265                 $email = $this->arg('email');
266
267                 # Maybe an old tab open...?
268
269                 if ($user->email != $email) {
270                     $this->show_form(_('That is not your email address.'));
271                     return;
272                 }
273
274                 $user->query('BEGIN');
275                 $original = clone($user);
276                 $user->email = NULL;
277                 $result = $user->updateKeys($original);
278                 if (!$result) {
279                         common_log_db_error($user, 'UPDATE', __FILE__);
280                         common_server_error(_('Couldn\'t update user.'));
281                         return;
282                 }
283                 $user->query('COMMIT');
284
285                 $this->show_form(_('The address was removed.'), TRUE);
286         }
287
288         function remove_incoming() {
289                 $user = common_current_user();
290                 
291                 if (!$user->incomingemail) {
292                         $this->show_form(_('No incoming email address.'));
293                         return;
294                 }
295                 
296                 $orig = clone($user);
297                 $user->incomingemail = NULL;
298
299                 if (!$user->updateKeys($orig)) {
300                         common_log_db_error($user, 'UPDATE', __FILE__);
301                         $this->server_error(_("Couldn't update user record."));
302                 }
303                 
304                 $this->show_form(_('Incoming email address removed.'), TRUE);
305         }
306
307         function new_incoming() {
308                 $user = common_current_user();
309                 
310                 $orig = clone($user);
311                 $user->incomingemail = mail_new_incoming_address();
312                 
313                 if (!$user->updateKeys($orig)) {
314                         common_log_db_error($user, 'UPDATE', __FILE__);
315                         $this->server_error(_("Couldn't update user record."));
316                 }
317
318                 $this->show_form(_('New incoming email address added.'), TRUE);
319         }
320         
321         function email_exists($email) {
322                 $user = common_current_user();
323                 $other = User::staticGet('email', $email);
324                 if (!$other) {
325                         return false;
326                 } else {
327                         return $other->id != $user->id;
328                 }
329         }
330 }