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