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