]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
Update to forms and email settings
[quix0rs-gnu-social.git] / actions / emailsettings.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Settings for email
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Zach Copley <zach@controlyourself.ca>
26  * @copyright 2008-2009 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/settingsaction.php';
36
37 /**
38  * Settings for email
39  *
40  * @category Settings
41  * @package  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @author   Zach Copley <zach@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  *
47  * @see      Widget
48  */
49
50 class EmailsettingsAction extends SettingsAction
51 {
52     /**
53      * Title of the page
54      *
55      * @return string Title of the page
56      */
57
58     function title()
59     {
60         return _('Email Settings');
61     }
62
63     /**
64      * Instructions for use
65      *
66      * @return instructions for use
67      */
68
69     function getInstructions()
70     {
71         return _('Manage how you get email from %%site.name%%.');
72     }
73
74     /**
75      * Content area of the page
76      *
77      * Shows a form for adding and removing email addresses and setting
78      * email preferences.
79      *
80      * @return void
81      */
82
83     function showContent()
84     {
85         $user = common_current_user();
86
87         $this->elementStart('form', array('method' => 'post',
88                                           'id' => 'form_settings_email',
89                                           'class' => 'form_settings',
90                                           'action' =>
91                                           common_local_url('emailsettings')));
92         $this->elementStart('fieldset');
93         $this->element('legend', null, _('Email settings'));
94         $this->hidden('token', common_session_token());
95
96         $this->elementStart('div', array('id' => 'settings_email_address'));
97         $this->element('h2', null, _('Address'));
98
99         if ($user->email) {
100             $this->element('p', array('id' => 'email_confirmed', $user->email));
101             $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
102             $this->hidden('email', $user->email);
103             $this->submit('remove', _('Remove'));
104         } else {
105             $confirm = $this->getConfirmation();
106             if ($confirm) {
107                 $this->element('p', array('id' => 'email_unconfirmed'), $confirm->address);
108                 $this->element('p', array('class' => 'form_note'), 
109                                         _('Awaiting confirmation on this address. '.
110                                         'Check your inbox (and spam box!) for a message '.
111                                         'with further instructions.'));
112                 $this->hidden('email', $confirm->address);
113                 $this->submit('cancel', _('Cancel'));
114             } else {
115                 $this->elementStart('ul', 'form_datas');
116                 $this->elementStart('li');
117                 $this->input('email', _('Email Address'),
118                              ($this->arg('email')) ? $this->arg('email') : null,
119                              _('Email address, like "UserName@example.org"'));
120                 $this->elementEnd('li');
121                 $this->elementEnd('ul');
122                 $this->submit('add', _('Add'));
123             }
124         }
125         $this->elementEnd('div');
126
127         $this->elementStart('div', array('id' => 'settings_email_incoming'));
128         if ($user->email) {
129             $this->element('h2', null, _('Incoming email'));
130
131             if ($user->incomingemail) {
132                 $this->elementStart('p');
133                 $this->element('span', 'address', $user->incomingemail);
134                 $this->element('span', 'input_instructions',
135                                _('Send email to this address to post new notices.'));
136                 $this->elementEnd('p');
137                 $this->submit('removeincoming', _('Remove'));
138             }
139
140             $this->elementStart('p');
141             $this->element('span', 'input_instructions',
142                            _('Make a new email address for posting to; '.
143                              'cancels the old one.'));
144             $this->elementEnd('p');
145             $this->submit('newincoming', _('New'));
146         }
147         $this->elementEnd('div');
148
149         $this->elementStart('div', array('id' => 'settings_email_preferences'));
150         $this->element('h2', null, _('Preferences'));
151
152         $this->elementStart('ul', 'form_datas');
153         $this->elementStart('li');
154         $this->checkbox('emailnotifysub',
155                         _('Send me notices of new subscriptions through email.'),
156                         $user->emailnotifysub);
157         $this->elementEnd('li');
158         $this->elementStart('li');
159         $this->checkbox('emailnotifyfav',
160                         _('Send me email when someone '.
161                           'adds my notice as a favorite.'),
162                         $user->emailnotifyfav);
163         $this->elementEnd('li');
164         $this->elementStart('li');
165         $this->checkbox('emailnotifymsg',
166                         _('Send me email when someone sends me a private message.'),
167                         $user->emailnotifymsg);
168         $this->elementEnd('li');
169         $this->elementStart('li');
170         $this->checkbox('emailnotifynudge',
171                         _('Allow friends to nudge me and send me an email.'),
172                         $user->emailnotifynudge);
173         $this->elementEnd('li');
174         $this->elementStart('li');
175         $this->checkbox('emailpost',
176                         _('I want to post notices by email.'),
177                         $user->emailpost);
178         $this->elementEnd('li');
179         $this->elementStart('li');
180         $this->checkbox('emailmicroid',
181                         _('Publish a MicroID for my email address.'),
182                         $user->emailmicroid);
183         $this->elementEnd('li');
184         $this->elementEnd('ul');
185         $this->elementEnd('div');
186
187         $this->submit('save', _('Save'));
188
189         $this->elementEnd('fieldset');
190         $this->elementEnd('form');
191     }
192
193     /**
194      * Gets any existing email address confirmations we're waiting for
195      *
196      * @return Confirm_address Email address confirmation for user, or null
197      */
198
199     function getConfirmation()
200     {
201         $user = common_current_user();
202
203         $confirm = new Confirm_address();
204
205         $confirm->user_id      = $user->id;
206         $confirm->address_type = 'email';
207
208         if ($confirm->find(true)) {
209             return $confirm;
210         } else {
211             return null;
212         }
213     }
214
215     /**
216      * Handle posts
217      *
218      * Since there are a lot of different options on the page, we
219      * figure out what we're supposed to do based on which button was
220      * pushed
221      *
222      * @return void
223      */
224
225     function handlePost()
226     {
227         // CSRF protection
228         $token = $this->trimmed('token');
229         if (!$token || $token != common_session_token()) {
230             $this->show_form(_('There was a problem with your session token. '.
231                                'Try again, please.'));
232             return;
233         }
234
235         if ($this->arg('save')) {
236             $this->savePreferences();
237         } else if ($this->arg('add')) {
238             $this->addAddress();
239         } else if ($this->arg('cancel')) {
240             $this->cancelConfirmation();
241         } else if ($this->arg('remove')) {
242             $this->removeAddress();
243         } else if ($this->arg('removeincoming')) {
244             $this->removeIncoming();
245         } else if ($this->arg('newincoming')) {
246             $this->newIncoming();
247         } else {
248             $this->showForm(_('Unexpected form submission.'));
249         }
250     }
251
252     /**
253      * Save email preferences
254      *
255      * @return void
256      */
257
258     function savePreferences()
259     {
260         $emailnotifysub   = $this->boolean('emailnotifysub');
261         $emailnotifyfav   = $this->boolean('emailnotifyfav');
262         $emailnotifymsg   = $this->boolean('emailnotifymsg');
263         $emailnotifynudge = $this->boolean('emailnotifynudge');
264         $emailmicroid     = $this->boolean('emailmicroid');
265         $emailpost        = $this->boolean('emailpost');
266
267         $user = common_current_user();
268
269         assert(!is_null($user)); // should already be checked
270
271         $user->query('BEGIN');
272
273         $original = clone($user);
274
275         $user->emailnotifysub   = $emailnotifysub;
276         $user->emailnotifyfav   = $emailnotifyfav;
277         $user->emailnotifymsg   = $emailnotifymsg;
278         $user->emailnotifynudge = $emailnotifynudge;
279         $user->emailmicroid     = $emailmicroid;
280         $user->emailpost        = $emailpost;
281
282         $result = $user->update($original);
283
284         if ($result === false) {
285             common_log_db_error($user, 'UPDATE', __FILE__);
286             $this->serverError(_('Couldn\'t update user.'));
287             return;
288         }
289
290         $user->query('COMMIT');
291
292         $this->showForm(_('Preferences saved.'), true);
293     }
294
295     /**
296      * Add the address passed in by the user
297      *
298      * @return void
299      */
300
301     function addAddress()
302     {
303         $user = common_current_user();
304
305         $email = $this->trimmed('email');
306
307         // Some validation
308
309         if (!$email) {
310             $this->showForm(_('No email address.'));
311             return;
312         }
313
314         $email = common_canonical_email($email);
315
316         if (!$email) {
317             $this->showForm(_('Cannot normalize that email address'));
318             return;
319         }
320         if (!Validate::email($email, true)) {
321             $this->showForm(_('Not a valid email address'));
322             return;
323         } else if ($user->email == $email) {
324             $this->showForm(_('That is already your email address.'));
325             return;
326         } else if ($this->emailExists($email)) {
327             $this->showForm(_('That email address already belongs '.
328                               'to another user.'));
329             return;
330         }
331
332         $confirm = new Confirm_address();
333
334         $confirm->address      = $email;
335         $confirm->address_type = 'email';
336         $confirm->user_id      = $user->id;
337         $confirm->code         = common_confirmation_code(64);
338
339         $result = $confirm->insert();
340
341         if ($result === false) {
342             common_log_db_error($confirm, 'INSERT', __FILE__);
343             $this->serverError(_('Couldn\'t insert confirmation code.'));
344             return;
345         }
346
347         mail_confirm_address($user, $confirm->code, $user->nickname, $email);
348
349         $msg = _('A confirmation code was sent to the email address you added. '.
350                  'Check your inbox (and spam box!) for the code and instructions '.
351                  'on how to use it.');
352
353         $this->showForm($msg, true);
354     }
355
356     /**
357      * Handle a request to cancel email confirmation
358      *
359      * @return void
360      */
361
362     function cancelConfirmation()
363     {
364         $email = $this->arg('email');
365
366         $confirm = $this->getConfirmation();
367
368         if (!$confirm) {
369             $this->showForm(_('No pending confirmation to cancel.'));
370             return;
371         }
372         if ($confirm->address != $email) {
373             $this->showForm(_('That is the wrong IM address.'));
374             return;
375         }
376
377         $result = $confirm->delete();
378
379         if (!$result) {
380             common_log_db_error($confirm, 'DELETE', __FILE__);
381             $this->serverError(_('Couldn\'t delete email confirmation.'));
382             return;
383         }
384
385         $this->showForm(_('Confirmation cancelled.'), true);
386     }
387
388     /**
389      * Handle a request to remove an address from the user's account
390      *
391      * @return void
392      */
393
394     function removeAddress()
395     {
396         $user = common_current_user();
397
398         $email = $this->arg('email');
399
400         // Maybe an old tab open...?
401
402         if ($user->email != $email) {
403             $this->showForm(_('That is not your email address.'));
404             return;
405         }
406
407         $user->query('BEGIN');
408
409         $original = clone($user);
410
411         $user->email = null;
412
413         $result = $user->updateKeys($original);
414
415         if (!$result) {
416             common_log_db_error($user, 'UPDATE', __FILE__);
417             $this->serverError(_('Couldn\'t update user.'));
418             return;
419         }
420         $user->query('COMMIT');
421
422         $this->showForm(_('The address was removed.'), true);
423     }
424
425     /**
426      * Handle a request to remove an incoming email address
427      *
428      * @return void
429      */
430
431     function removeIncoming()
432     {
433         $user = common_current_user();
434
435         if (!$user->incomingemail) {
436             $this->showForm(_('No incoming email address.'));
437             return;
438         }
439
440         $orig = clone($user);
441
442         $user->incomingemail = null;
443
444         if (!$user->updateKeys($orig)) {
445             common_log_db_error($user, 'UPDATE', __FILE__);
446             $this->serverError(_("Couldn't update user record."));
447         }
448
449         $this->showForm(_('Incoming email address removed.'), true);
450     }
451
452     /**
453      * Generate a new incoming email address
454      *
455      * @return void
456      */
457
458     function newIncoming()
459     {
460         $user = common_current_user();
461
462         $orig = clone($user);
463
464         $user->incomingemail = mail_new_incoming_address();
465
466         if (!$user->updateKeys($orig)) {
467             common_log_db_error($user, 'UPDATE', __FILE__);
468             $this->serverError(_("Couldn't update user record."));
469         }
470
471         $this->showForm(_('New incoming email address added.'), true);
472     }
473
474     /**
475      * Does another user already have this email address?
476      *
477      * Email addresses are unique for users.
478      *
479      * @param string $email Address to check
480      *
481      * @return boolean Whether the email already exists.
482      */
483
484     function emailExists($email)
485     {
486         $user = common_current_user();
487
488         $other = User::staticGet('email', $email);
489
490         if (!$other) {
491             return false;
492         } else {
493             return $other->id != $user->id;
494         }
495     }
496 }