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