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