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