]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
dfdbe1bad066cac4dcb6f93156f3537fe1bcb699
[quix0rs-gnu-social.git] / actions / emailsettings.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35
36
37 /**
38  * Settings for email
39  *
40  * @category Settings
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
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     function title()
58     {
59         // TRANS: Title for e-mail settings.
60         return _('Email settings');
61     }
62
63     /**
64      * Instructions for use
65      *
66      * @return instructions for use
67      */
68     function getInstructions()
69     {
70         // XXX: For consistency of parameters in messages, this should be a
71         //      regular parameters, replaced with sprintf().
72         // TRANS: E-mail settings page instructions.
73         // TRANS: %%site.name%% is the name of the site.
74         return _('Manage how you get email from %%site.name%%.');
75     }
76
77     function showScripts()
78     {
79         parent::showScripts();
80         $this->script('emailsettings.js');
81         $this->autofocus('email');
82     }
83
84     /**
85      * Content area of the page
86      *
87      * Shows a form for adding and removing email addresses and setting
88      * email preferences.
89      *
90      * @return void
91      */
92     function showContent()
93     {
94         $user = $this->scoped->getUser();
95
96         $this->elementStart('form', array('method' => 'post',
97                                           'id' => 'form_settings_email',
98                                           'class' => 'form_settings',
99                                           'action' =>
100                                           common_local_url('emailsettings')));
101         $this->elementStart('fieldset');
102         $this->elementStart('fieldset', array('id' => 'settings_email_address'));
103         // TRANS: Form legend for e-mail settings form.
104         $this->element('legend', null, _('Email address'));
105         $this->hidden('token', common_session_token());
106
107         if ($user->email) {
108             $this->element('p', array('id' => 'form_confirmed'), $user->email);
109             // TRANS: Form note in e-mail settings form.
110             $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
111             $this->hidden('email', $user->email);
112             // TRANS: Button label to remove a confirmed e-mail address.
113             $this->submit('remove', _m('BUTTON','Remove'));
114         } else {
115             $confirm = $this->getConfirmation();
116             if ($confirm) {
117                 $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
118                 $this->element('p', array('class' => 'form_note'),
119                                         // TRANS: Form note in e-mail settings form.
120                                         _('Awaiting confirmation on this address. '.
121                                         'Check your inbox (and spam box!) for a message '.
122                                         'with further instructions.'));
123                 $this->hidden('email', $confirm->address);
124                 // TRANS: Button label to cancel an e-mail address confirmation procedure.
125                 $this->submit('cancel', _m('BUTTON','Cancel'));
126             } else {
127                 $this->elementStart('ul', 'form_data');
128                 $this->elementStart('li');
129                 // TRANS: Field label for e-mail address input in e-mail settings form.
130                 $this->input('email', _('Email address'),
131                              ($this->arg('email')) ? $this->arg('email') : null,
132                              // TRANS: Instructions for e-mail address input form. Do not translate
133                              // TRANS: "example.org". It is one of the domain names reserved for
134                              // TRANS: use in examples by http://www.rfc-editor.org/rfc/rfc2606.txt.
135                              // TRANS: Any other domain may be owned by a legitimate person or
136                              // TRANS: organization.
137                              _('Email address, like "UserName@example.org"'));
138                 $this->elementEnd('li');
139                 $this->elementEnd('ul');
140                 // TRANS: Button label for adding an e-mail address in e-mail settings form.
141                 $this->submit('add', _m('BUTTON','Add'));
142             }
143         }
144         $this->elementEnd('fieldset');
145
146        if (common_config('emailpost', 'enabled') && $user->email) {
147             $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
148             // TRANS: Form legend for incoming e-mail settings form.
149             $this->element('legend', null, _('Incoming email'));
150
151             $this->elementStart('ul', 'form_data');
152             $this->elementStart('li');
153             $this->checkbox('emailpost',
154                     // TRANS: Checkbox label in e-mail preferences form.
155                     _('I want to post notices by email.'),
156                     $user->emailpost);
157             $this->elementEnd('li');
158             $this->elementEnd('ul');
159
160             // Our stylesheets make the form_data list items all floats, which
161             // creates lots of problems with trying to wrap divs around things.
162             // This should force a break before the next section, which needs
163             // to be separate so we can disable the things in it when the
164             // checkbox is off.
165             $this->elementStart('div', array('style' => 'clear: both'));
166             $this->elementEnd('div');
167
168             $this->elementStart('div', array('id' => 'emailincoming'));
169
170             if ($user->incomingemail) {
171                 $this->elementStart('p');
172                 $this->element('span', 'address', $user->incomingemail);
173                 // @todo XXX: Looks a little awkward in the UI.
174                 //      Something like "xxxx@identi.ca  Send email ..". Needs improvement.
175                 $this->element('span', 'input_instructions',
176                                // TRANS: Form instructions for incoming e-mail form in e-mail settings.
177                                _('Send email to this address to post new notices.'));
178                 $this->elementEnd('p');
179                 // TRANS: Button label for removing a set sender e-mail address to post notices from.
180                 $this->submit('removeincoming', _m('BUTTON','Remove'));
181             }
182
183             $this->elementStart('p');
184             if ($user->incomingemail) {
185                 // TRANS: Instructions for incoming e-mail address input form, when an address has already been assigned.
186                 $msg = _('Make a new email address for posting to; '.
187                          'cancels the old one.');
188             } else {
189                 // TRANS: Instructions for incoming e-mail address input form.
190                 $msg = _('To send notices via email, we need to create a unique email address for you on this server:');
191             }
192             $this->element('span', 'input_instructions', $msg);
193             $this->elementEnd('p');
194
195             // TRANS: Button label for adding an e-mail address to send notices from.
196             $this->submit('newincoming', _m('BUTTON','New'));
197
198             $this->elementEnd('div'); // div#emailincoming
199
200             $this->elementEnd('fieldset');
201         }
202
203         $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
204         // TRANS: Form legend for e-mail preferences form.
205         $this->element('legend', null, _('Email preferences'));
206
207         $this->elementStart('ul', 'form_data');
208
209         if (Event::handle('StartEmailFormData', array($this, $this->scoped))) {
210             $this->elementStart('li');
211             $this->checkbox('emailnotifysub',
212                             // TRANS: Checkbox label in e-mail preferences form.
213                             _('Send me notices of new subscriptions through email.'),
214                             $user->emailnotifysub);
215             $this->elementEnd('li');
216             $this->elementStart('li');
217             $this->checkbox('emailnotifymsg',
218                             // TRANS: Checkbox label in e-mail preferences form.
219                             _('Send me email when someone sends me a private message.'),
220                             $user->emailnotifymsg);
221             $this->elementEnd('li');
222             $this->elementStart('li');
223             $this->checkbox('emailnotifyattn',
224                             // TRANS: Checkbox label in e-mail preferences form.
225                             _('Send me email when someone sends me an "@-reply".'),
226                             $user->emailnotifyattn);
227             $this->elementEnd('li');
228             $this->elementStart('li');
229             $this->checkbox('emailnotifynudge',
230                             // TRANS: Checkbox label in e-mail preferences form.
231                             _('Allow friends to nudge me and send me an email.'),
232                             $user->emailnotifynudge);
233             $this->elementEnd('li');
234             $this->elementStart('li');
235             $this->checkbox('emailmicroid',
236                             // TRANS: Checkbox label in e-mail preferences form.
237                             _('Publish a MicroID for my email address.'),
238                             $user->emailmicroid);
239             $this->elementEnd('li');
240             Event::handle('EndEmailFormData', array($this, $this->scoped));
241         }
242         $this->elementEnd('ul');
243         // TRANS: Button label to save e-mail preferences.
244         $this->submit('save', _m('BUTTON','Save'));
245         $this->elementEnd('fieldset');
246         $this->elementEnd('fieldset');
247         $this->elementEnd('form');
248     }
249
250     /**
251      * Gets any existing email address confirmations we're waiting for
252      *
253      * @return Confirm_address Email address confirmation for user, or null
254      */
255     function getConfirmation()
256     {
257         $user = common_current_user();
258
259         $confirm = new Confirm_address();
260
261         $confirm->user_id      = $user->id;
262         $confirm->address_type = 'email';
263
264         if ($confirm->find(true)) {
265             return $confirm;
266         } else {
267             return null;
268         }
269     }
270
271     /**
272      * Handle posts
273      *
274      * Since there are a lot of different options on the page, we
275      * figure out what we're supposed to do based on which button was
276      * pushed
277      *
278      * @return void
279      */
280     function handlePost()
281     {
282         // CSRF protection
283         $token = $this->trimmed('token');
284         if (!$token || $token != common_session_token()) {
285             // TRANS: Client error displayed when the session token does not match or is not given.
286             $this->show_form(_('There was a problem with your session token. '.
287                                'Try again, please.'));
288             return;
289         }
290
291         if ($this->arg('save')) {
292             $this->savePreferences();
293         } else if ($this->arg('add')) {
294             $this->addAddress();
295         } else if ($this->arg('cancel')) {
296             $this->cancelConfirmation();
297         } else if ($this->arg('remove')) {
298             $this->removeAddress();
299         } else if ($this->arg('removeincoming')) {
300             $this->removeIncoming();
301         } else if ($this->arg('newincoming')) {
302             $this->newIncoming();
303         } else {
304             // TRANS: Message given submitting a form with an unknown action in e-mail settings.
305             $this->showForm(_('Unexpected form submission.'));
306         }
307     }
308
309     /**
310      * Save email preferences
311      *
312      * @return void
313      */
314     function savePreferences()
315     {
316         $user = $this->scoped->getUser();
317
318         if (Event::handle('StartEmailSaveForm', array($this, $this->scoped))) {
319             $emailnotifysub   = $this->booleanintstring('emailnotifysub');
320             $emailnotifymsg   = $this->booleanintstring('emailnotifymsg');
321             $emailnotifynudge = $this->booleanintstring('emailnotifynudge');
322             $emailnotifyattn  = $this->booleanintstring('emailnotifyattn');
323             $emailmicroid     = $this->booleanintstring('emailmicroid');
324             $emailpost        = $this->booleanintstring('emailpost');
325
326             $user->query('BEGIN');
327
328             $original = clone($user);
329
330             $user->emailnotifysub   = $emailnotifysub;
331             $user->emailnotifymsg   = $emailnotifymsg;
332             $user->emailnotifynudge = $emailnotifynudge;
333             $user->emailnotifyattn  = $emailnotifyattn;
334             $user->emailmicroid     = $emailmicroid;
335             $user->emailpost        = $emailpost;
336
337             $result = $user->update($original);
338
339             if ($result === false) {
340                 common_log_db_error($user, 'UPDATE', __FILE__);
341                 $user->query('ROLLBACK');
342                 // TRANS: Server error thrown on database error updating e-mail preferences.
343                 $this->serverError(_('Could not update user.'));
344             }
345
346             $user->query('COMMIT');
347
348             Event::handle('EndEmailSaveForm', array($this, $this->scoped));
349
350             // TRANS: Confirmation message for successful e-mail preferences save.
351             $this->showForm(_('Email preferences saved.'), true);
352         }
353     }
354
355     /**
356      * Add the address passed in by the user
357      *
358      * @return void
359      */
360     function addAddress()
361     {
362         $user = common_current_user();
363
364         $email = $this->trimmed('email');
365
366         // Some validation
367
368         if (!$email) {
369             // TRANS: Message given saving e-mail address without having provided one.
370             $this->showForm(_('No email address.'));
371             return;
372         }
373
374         $email = common_canonical_email($email);
375
376         if (!$email) {
377             // TRANS: Message given saving e-mail address that cannot be normalised.
378             $this->showForm(_('Cannot normalize that email address.'));
379             return;
380         }
381         if (!Validate::email($email, common_config('email', 'check_domain'))) {
382             // TRANS: Message given saving e-mail address that not valid.
383             $this->showForm(_('Not a valid email address.'));
384             return;
385         } else if ($user->email == $email) {
386             // TRANS: Message given saving e-mail address that is already set.
387             $this->showForm(_('That is already your email address.'));
388             return;
389         } else if ($this->emailExists($email)) {
390             // TRANS: Message given saving e-mail address that is already set for another user.
391             $this->showForm(_('That email address already belongs '.
392                               'to another user.'));
393             return;
394         }
395
396         if (Event::handle('StartAddEmailAddress', array($user, $email))) {
397
398             $confirm = new Confirm_address();
399
400             $confirm->address      = $email;
401             $confirm->address_type = 'email';
402             $confirm->user_id      = $user->id;
403             $confirm->code         = common_confirmation_code(64);
404
405             $result = $confirm->insert();
406
407             if ($result === false) {
408                 common_log_db_error($confirm, 'INSERT', __FILE__);
409                 // TRANS: Server error thrown on database error adding e-mail confirmation code.
410                 $this->serverError(_('Could not insert confirmation code.'));
411             }
412
413             mail_confirm_address($user, $confirm->code, $user->nickname, $email);
414
415             Event::handle('EndAddEmailAddress', array($user, $email));
416         }
417
418         // TRANS: Message given saving valid e-mail address that is to be confirmed.
419         $msg = _('A confirmation code was sent to the email address you added. '.
420                  'Check your inbox (and spam box!) for the code and instructions '.
421                  'on how to use it.');
422
423         $this->showForm($msg, true);
424     }
425
426     /**
427      * Handle a request to cancel email confirmation
428      *
429      * @return void
430      */
431     function cancelConfirmation()
432     {
433         $email = $this->arg('email');
434
435         $confirm = $this->getConfirmation();
436
437         if (!$confirm) {
438             // TRANS: Message given canceling e-mail address confirmation that is not pending.
439             $this->showForm(_('No pending confirmation to cancel.'));
440             return;
441         }
442         if ($confirm->address != $email) {
443             // TRANS: Message given canceling e-mail address confirmation for the wrong e-mail address.
444             $this->showForm(_('That is the wrong email address.'));
445             return;
446         }
447
448         $result = $confirm->delete();
449
450         if (!$result) {
451             common_log_db_error($confirm, 'DELETE', __FILE__);
452             // TRANS: Server error thrown on database error canceling e-mail address confirmation.
453             $this->serverError(_('Could not delete email confirmation.'));
454         }
455
456         // TRANS: Message given after successfully canceling e-mail address confirmation.
457         $this->showForm(_('Email confirmation cancelled.'), true);
458     }
459
460     /**
461      * Handle a request to remove an address from the user's account
462      *
463      * @return void
464      */
465     function removeAddress()
466     {
467         $user = common_current_user();
468
469         $email = $this->arg('email');
470
471         // Maybe an old tab open...?
472
473         if ($user->email != $email) {
474             // TRANS: Message given trying to remove an e-mail address that is not
475             // TRANS: registered for the active user.
476             $this->showForm(_('That is not your email address.'));
477             return;
478         }
479
480         $original = clone($user);
481
482         $user->email = null;
483
484         // Throws exception on failure. Also performs it within a transaction.
485         $user->updateWithKeys($original);
486
487         // TRANS: Message given after successfully removing a registered e-mail address.
488         $this->showForm(_('The email address was removed.'), true);
489     }
490
491     /**
492      * Handle a request to remove an incoming email address
493      *
494      * @return void
495      */
496     function removeIncoming()
497     {
498         $user = common_current_user();
499
500         if (!$user->incomingemail) {
501             // TRANS: Form validation error displayed when trying to remove an incoming e-mail address while no address has been set.
502             $this->showForm(_('No incoming email address.'));
503             return;
504         }
505
506         $orig = clone($user);
507
508         $user->incomingemail = null;
509         $user->emailpost = 0;
510
511         // Throws exception on failure. Also performs it within a transaction.
512         $user->updateWithKeys($orig);
513
514         // TRANS: Message given after successfully removing an incoming e-mail address.
515         $this->showForm(_('Incoming email address removed.'), true);
516     }
517
518     /**
519      * Generate a new incoming email address
520      *
521      * @return void
522      */
523     function newIncoming()
524     {
525         $user = common_current_user();
526
527         $orig = clone($user);
528
529         $user->incomingemail = mail_new_incoming_address();
530         $user->emailpost = 1;
531
532         // Throws exception on failure. Also performs it within a transaction.
533         $user->updateWithKeys($orig);
534
535         // TRANS: Message given after successfully adding an incoming e-mail address.
536         $this->showForm(_('New incoming email address added.'), true);
537     }
538
539     /**
540      * Does another user already have this email address?
541      *
542      * Email addresses are unique for users.
543      *
544      * @param string $email Address to check
545      *
546      * @return boolean Whether the email already exists.
547      */
548
549     function emailExists($email)
550     {
551         $user = common_current_user();
552
553         $other = User::getKV('email', $email);
554
555         if (!$other) {
556             return false;
557         } else {
558             return $other->id != $user->id;
559         }
560     }
561 }