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