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