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