]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
Localisation updates from translatewiki.net
[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
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('form');
198     }
199
200     /**
201      * Gets any existing email address confirmations we're waiting for
202      *
203      * @return Confirm_address Email address confirmation for user, or null
204      */
205
206     function getConfirmation()
207     {
208         $user = common_current_user();
209
210         $confirm = new Confirm_address();
211
212         $confirm->user_id      = $user->id;
213         $confirm->address_type = 'email';
214
215         if ($confirm->find(true)) {
216             return $confirm;
217         } else {
218             return null;
219         }
220     }
221
222     /**
223      * Handle posts
224      *
225      * Since there are a lot of different options on the page, we
226      * figure out what we're supposed to do based on which button was
227      * pushed
228      *
229      * @return void
230      */
231
232     function handlePost()
233     {
234         // CSRF protection
235         $token = $this->trimmed('token');
236         if (!$token || $token != common_session_token()) {
237             $this->show_form(_('There was a problem with your session token. '.
238                                'Try again, please.'));
239             return;
240         }
241
242         if ($this->arg('save')) {
243             $this->savePreferences();
244         } else if ($this->arg('add')) {
245             $this->addAddress();
246         } else if ($this->arg('cancel')) {
247             $this->cancelConfirmation();
248         } else if ($this->arg('remove')) {
249             $this->removeAddress();
250         } else if ($this->arg('removeincoming')) {
251             $this->removeIncoming();
252         } else if ($this->arg('newincoming')) {
253             $this->newIncoming();
254         } else {
255             $this->showForm(_('Unexpected form submission.'));
256         }
257     }
258
259     /**
260      * Save email preferences
261      *
262      * @return void
263      */
264
265     function savePreferences()
266     {
267         $emailnotifysub   = $this->boolean('emailnotifysub');
268         $emailnotifyfav   = $this->boolean('emailnotifyfav');
269         $emailnotifymsg   = $this->boolean('emailnotifymsg');
270         $emailnotifynudge = $this->boolean('emailnotifynudge');
271         $emailnotifyattn  = $this->boolean('emailnotifyattn');
272         $emailmicroid     = $this->boolean('emailmicroid');
273         $emailpost        = $this->boolean('emailpost');
274
275         $user = common_current_user();
276
277         assert(!is_null($user)); // should already be checked
278
279         $user->query('BEGIN');
280
281         $original = clone($user);
282
283         $user->emailnotifysub   = $emailnotifysub;
284         $user->emailnotifyfav   = $emailnotifyfav;
285         $user->emailnotifymsg   = $emailnotifymsg;
286         $user->emailnotifynudge = $emailnotifynudge;
287         $user->emailnotifyattn  = $emailnotifyattn;
288         $user->emailmicroid     = $emailmicroid;
289         $user->emailpost        = $emailpost;
290
291         $result = $user->update($original);
292
293         if ($result === false) {
294             common_log_db_error($user, 'UPDATE', __FILE__);
295             $this->serverError(_('Couldn\'t update user.'));
296             return;
297         }
298
299         $user->query('COMMIT');
300
301         $this->showForm(_('Preferences saved.'), true);
302     }
303
304     /**
305      * Add the address passed in by the user
306      *
307      * @return void
308      */
309
310     function addAddress()
311     {
312         $user = common_current_user();
313
314         $email = $this->trimmed('email');
315
316         // Some validation
317
318         if (!$email) {
319             $this->showForm(_('No email address.'));
320             return;
321         }
322
323         $email = common_canonical_email($email);
324
325         if (!$email) {
326             $this->showForm(_('Cannot normalize that email address'));
327             return;
328         }
329         if (!Validate::email($email, common_config('email', 'check_domain'))) {
330             $this->showForm(_('Not a valid email address'));
331             return;
332         } else if ($user->email == $email) {
333             $this->showForm(_('That is already your email address.'));
334             return;
335         } else if ($this->emailExists($email)) {
336             $this->showForm(_('That email address already belongs '.
337                               'to another user.'));
338             return;
339         }
340
341         $confirm = new Confirm_address();
342
343         $confirm->address      = $email;
344         $confirm->address_type = 'email';
345         $confirm->user_id      = $user->id;
346         $confirm->code         = common_confirmation_code(64);
347
348         $result = $confirm->insert();
349
350         if ($result === false) {
351             common_log_db_error($confirm, 'INSERT', __FILE__);
352             $this->serverError(_('Couldn\'t insert confirmation code.'));
353             return;
354         }
355
356         mail_confirm_address($user, $confirm->code, $user->nickname, $email);
357
358         $msg = _('A confirmation code was sent to the email address you added. '.
359                  'Check your inbox (and spam box!) for the code and instructions '.
360                  'on how to use it.');
361
362         $this->showForm($msg, true);
363     }
364
365     /**
366      * Handle a request to cancel email confirmation
367      *
368      * @return void
369      */
370
371     function cancelConfirmation()
372     {
373         $email = $this->arg('email');
374
375         $confirm = $this->getConfirmation();
376
377         if (!$confirm) {
378             $this->showForm(_('No pending confirmation to cancel.'));
379             return;
380         }
381         if ($confirm->address != $email) {
382             $this->showForm(_('That is the wrong IM address.'));
383             return;
384         }
385
386         $result = $confirm->delete();
387
388         if (!$result) {
389             common_log_db_error($confirm, 'DELETE', __FILE__);
390             $this->serverError(_('Couldn\'t delete email confirmation.'));
391             return;
392         }
393
394         $this->showForm(_('Confirmation cancelled.'), true);
395     }
396
397     /**
398      * Handle a request to remove an address from the user's account
399      *
400      * @return void
401      */
402
403     function removeAddress()
404     {
405         $user = common_current_user();
406
407         $email = $this->arg('email');
408
409         // Maybe an old tab open...?
410
411         if ($user->email != $email) {
412             $this->showForm(_('That is not your email address.'));
413             return;
414         }
415
416         $user->query('BEGIN');
417
418         $original = clone($user);
419
420         $user->email = null;
421
422         $result = $user->updateKeys($original);
423
424         if (!$result) {
425             common_log_db_error($user, 'UPDATE', __FILE__);
426             $this->serverError(_('Couldn\'t update user.'));
427             return;
428         }
429         $user->query('COMMIT');
430
431         $this->showForm(_('The address was removed.'), true);
432     }
433
434     /**
435      * Handle a request to remove an incoming email address
436      *
437      * @return void
438      */
439
440     function removeIncoming()
441     {
442         $user = common_current_user();
443
444         if (!$user->incomingemail) {
445             $this->showForm(_('No incoming email address.'));
446             return;
447         }
448
449         $orig = clone($user);
450
451         $user->incomingemail = null;
452
453         if (!$user->updateKeys($orig)) {
454             common_log_db_error($user, 'UPDATE', __FILE__);
455             $this->serverError(_("Couldn't update user record."));
456         }
457
458         $this->showForm(_('Incoming email address removed.'), true);
459     }
460
461     /**
462      * Generate a new incoming email address
463      *
464      * @return void
465      */
466
467     function newIncoming()
468     {
469         $user = common_current_user();
470
471         $orig = clone($user);
472
473         $user->incomingemail = mail_new_incoming_address();
474
475         if (!$user->updateKeys($orig)) {
476             common_log_db_error($user, 'UPDATE', __FILE__);
477             $this->serverError(_("Couldn't update user record."));
478         }
479
480         $this->showForm(_('New incoming email address added.'), true);
481     }
482
483     /**
484      * Does another user already have this email address?
485      *
486      * Email addresses are unique for users.
487      *
488      * @param string $email Address to check
489      *
490      * @return boolean Whether the email already exists.
491      */
492
493     function emailExists($email)
494     {
495         $user = common_current_user();
496
497         $other = User::staticGet('email', $email);
498
499         if (!$other) {
500             return false;
501         } else {
502             return $other->id != $user->id;
503         }
504     }
505 }