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