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