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