]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
c93cbfae0d74e423cd54ba4c1441f3d177abe06a
[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('emailpost',
92                                                 _('I want to post notices by email.'),
93                                                 $user->emailpost);
94                 common_checkbox('emailmicroid',
95                                 _('Publish a MicroID for my email address.'),
96                                 $user->emailmicroid);
97
98                 common_submit('save', _('Save'));
99                 
100                 common_element_end('form');
101                 common_show_footer();
102         }
103
104         function get_confirmation() {
105                 $user = common_current_user();
106                 $confirm = new Confirm_address();
107                 $confirm->user_id = $user->id;
108                 $confirm->address_type = 'email';
109                 if ($confirm->find(TRUE)) {
110                         return $confirm;
111                 } else {
112                         return NULL;
113                 }
114         }
115
116         function handle_post() {
117
118                 # CSRF protection
119                 $token = $this->trimmed('token');
120                 if (!$token || $token != common_session_token()) {
121                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
122                         return;
123                 }
124
125                 if ($this->arg('save')) {
126                         $this->save_preferences();
127                 } else if ($this->arg('add')) {
128                         $this->add_address();
129                 } else if ($this->arg('cancel')) {
130                         $this->cancel_confirmation();
131                 } else if ($this->arg('remove')) {
132                         $this->remove_address();
133                 } else if ($this->arg('removeincoming')) {
134                         $this->remove_incoming();
135                 } else if ($this->arg('newincoming')) {
136                         $this->new_incoming();
137                 } else {
138                         $this->show_form(_('Unexpected form submission.'));
139                 }
140         }
141
142         function save_preferences() {
143
144                 $emailnotifysub = $this->boolean('emailnotifysub');
145                 $emailmicroid = $this->boolean('emailmicroid');
146                 $emailpost = $this->boolean('emailpost');
147
148                 $user = common_current_user();
149
150                 assert(!is_null($user)); # should already be checked
151
152                 $user->query('BEGIN');
153
154                 $original = clone($user);
155
156                 $user->emailnotifysub = $emailnotifysub;
157                 $user->emailmicroid = $emailmicroid;
158                 $user->emailpost = $emailpost;
159
160                 $result = $user->update($original);
161
162                 if ($result === FALSE) {
163                         common_log_db_error($user, 'UPDATE', __FILE__);
164                         common_server_error(_('Couldn\'t update user.'));
165                         return;
166                 }
167
168                 $user->query('COMMIT');
169
170                 $this->show_form(_('Preferences saved.'), true);
171         }
172
173         function add_address() {
174
175                 $user = common_current_user();
176
177                 $email = $this->trimmed('email');
178
179                 # Some validation
180
181                 if (!$email) {
182                         $this->show_form(_('No email address.'));
183                         return;
184                 }
185
186                 $email = common_canonical_email($email);
187
188                 if (!$email) {
189                     $this->show_form(_('Cannot normalize that email address'));
190                     return;
191                 }
192                 if (!Validate::email($email, true)) {
193                     $this->show_form(_('Not a valid email address'));
194                     return;
195                 } else if ($user->email == $email) {
196                     $this->show_form(_('That is already your email address.'));
197                     return;
198                 } else if ($this->email_exists($email)) {
199                     $this->show_form(_('That email address already belongs to another user.'));
200                     return;
201                 }
202
203                 $confirm = new Confirm_address();
204                 $confirm->address = $email;
205                 $confirm->address_type = 'email';
206                 $confirm->user_id = $user->id;
207                 $confirm->code = common_confirmation_code(64);
208
209                 $result = $confirm->insert();
210
211                 if ($result === FALSE) {
212                         common_log_db_error($confirm, 'INSERT', __FILE__);
213                         common_server_error(_('Couldn\'t insert confirmation code.'));
214                         return;
215                 }
216
217                 mail_confirm_address($confirm->code,
218                                                          $user->nickname,
219                                                          $email);
220
221                 $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.');
222
223                 $this->show_form($msg, TRUE);
224         }
225
226         function cancel_confirmation() {
227                 $email = $this->arg('email');
228                 $confirm = $this->get_confirmation();
229                 if (!$confirm) {
230                         $this->show_form(_('No pending confirmation to cancel.'));
231                         return;
232                 }
233                 if ($confirm->address != $email) {
234                         $this->show_form(_('That is the wrong IM address.'));
235                         return;
236                 }
237
238         $result = $confirm->delete();
239
240         if (!$result) {
241                         common_log_db_error($confirm, 'DELETE', __FILE__);
242             $this->server_error(_('Couldn\'t delete email confirmation.'));
243             return;
244         }
245
246         $this->show_form(_('Confirmation cancelled.'), TRUE);
247         }
248
249         function remove_address() {
250
251                 $user = common_current_user();
252                 $email = $this->arg('email');
253
254                 # Maybe an old tab open...?
255
256                 if ($user->email != $email) {
257                     $this->show_form(_('That is not your email address.'));
258                     return;
259                 }
260
261                 $user->query('BEGIN');
262                 $original = clone($user);
263                 $user->email = NULL;
264                 $result = $user->updateKeys($original);
265                 if (!$result) {
266                         common_log_db_error($user, 'UPDATE', __FILE__);
267                         common_server_error(_('Couldn\'t update user.'));
268                         return;
269                 }
270                 $user->query('COMMIT');
271
272                 $this->show_form(_('The address was removed.'), TRUE);
273         }
274
275         function remove_incoming() {
276                 $user = common_current_user();
277                 
278                 if (!$user->incomingemail) {
279                         $this->show_form(_('No incoming email address.'));
280                         return;
281                 }
282                 
283                 $orig = clone($user);
284                 $user->incomingemail = NULL;
285
286                 if (!$user->updateKeys($orig)) {
287                         common_log_db_error($user, 'UPDATE', __FILE__);
288                         $this->server_error(_("Couldn't update user record."));
289                 }
290                 
291                 $this->show_form(_('Incoming email address removed.'), TRUE);
292         }
293
294         function new_incoming() {
295                 $user = common_current_user();
296                 
297                 $orig = clone($user);
298                 $user->incomingemail = mail_new_incoming_address();
299                 
300                 if (!$user->updateKeys($orig)) {
301                         common_log_db_error($user, 'UPDATE', __FILE__);
302                         $this->server_error(_("Couldn't update user record."));
303                 }
304
305                 $this->show_form(_('New incoming email address added.'), TRUE);
306         }
307         
308         function email_exists($email) {
309                 $user = common_current_user();
310                 $other = User::staticGet('email', $email);
311                 if (!$other) {
312                         return false;
313                 } else {
314                         return $other->id != $user->id;
315                 }
316         }
317 }