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