emailsettings.php 15 KB
Newer Older
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
5 6 7 8 9
 * Settings for email
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12 13 14 15 16 17 18 19 20
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 22
 *
 * @category  Settings
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @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 33
    exit(1);
}
34

35
require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36 37 38 39 40

/**
 * Settings for email
 *
 * @category Settings
41
 * @package  StatusNet
42 43
 * @author   Evan Prodromou <evan@status.net>
 * @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 48
 *
 * @see      Widget
 */
49

50
class EmailsettingsAction extends AccountSettingsAction
51
{
52 53 54 55 56
    /**
     * Title of the page
     *
     * @return string Title of the page
     */
57

58 59 60 61 62 63 64 65 66 67 68 69
    function title()
    {
        return _('Email Settings');
    }

    /**
     * Instructions for use
     *
     * @return instructions for use
     */

    function getInstructions()
70
    {
71 72 73
        return _('Manage how you get email from %%site.name%%.');
    }

74 75 76 77 78 79 80 81 82 83
    /**
     * Content area of the page
     *
     * Shows a form for adding and removing email addresses and setting
     * email preferences.
     *
     * @return void
     */

    function showContent()
84
    {
85
        $user = common_current_user();
86

87
        $this->elementStart('form', array('method' => 'post',
sarven's avatar
sarven committed
88 89
                                          'id' => 'form_settings_email',
                                          'class' => 'form_settings',
90 91
                                          'action' =>
                                          common_local_url('emailsettings')));
92

sarven's avatar
sarven committed
93
        $this->elementStart('fieldset', array('id' => 'settings_email_address'));
sarven's avatar
sarven committed
94 95
        $this->element('legend', null, _('Address'));
        $this->hidden('token', common_session_token());
96 97

        if ($user->email) {
sarven's avatar
sarven committed
98
            $this->element('p', array('id' => 'form_confirmed'), $user->email);
sarven's avatar
sarven committed
99
            $this->element('p', array('class' => 'form_note'), _('Current confirmed email address.'));
100 101
            $this->hidden('email', $user->email);
            $this->submit('remove', _('Remove'));
102
        } else {
103
            $confirm = $this->getConfirmation();
104
            if ($confirm) {
sarven's avatar
sarven committed
105
                $this->element('p', array('id' => 'form_unconfirmed'), $confirm->address);
106
                $this->element('p', array('class' => 'form_note'),
sarven's avatar
sarven committed
107 108 109
                                        _('Awaiting confirmation on this address. '.
                                        'Check your inbox (and spam box!) for a message '.
                                        'with further instructions.'));
110 111
                $this->hidden('email', $confirm->address);
                $this->submit('cancel', _('Cancel'));
112
            } else {
sarven's avatar
sarven committed
113
                $this->elementStart('ul', 'form_data');
sarven's avatar
sarven committed
114
                $this->elementStart('li');
115
                $this->input('email', _('Email Address'),
Evan Prodromou's avatar
Evan Prodromou committed
116
                             ($this->arg('email')) ? $this->arg('email') : null,
117
                             _('Email address, like "UserName@example.org"'));
sarven's avatar
sarven committed
118 119
                $this->elementEnd('li');
                $this->elementEnd('ul');
120
                $this->submit('add', _('Add'));
121 122
            }
        }
sarven's avatar
sarven committed
123
        $this->elementEnd('fieldset');
124

125
       if (common_config('emailpost', 'enabled') && $user->email) {
sarven's avatar
sarven committed
126
            $this->elementStart('fieldset', array('id' => 'settings_email_incoming'));
sarven's avatar
sarven committed
127
            $this->element('legend',_('Incoming email'));
128
            if ($user->incomingemail) {
129 130 131
                $this->elementStart('p');
                $this->element('span', 'address', $user->incomingemail);
                $this->element('span', 'input_instructions',
132
                               _('Send email to this address to post new notices.'));
133 134
                $this->elementEnd('p');
                $this->submit('removeincoming', _('Remove'));
135
            }
136

137 138
            $this->elementStart('p');
            $this->element('span', 'input_instructions',
139 140
                           _('Make a new email address for posting to; '.
                             'cancels the old one.'));
141 142
            $this->elementEnd('p');
            $this->submit('newincoming', _('New'));
sarven's avatar
sarven committed
143
            $this->elementEnd('fieldset');
144
        }
145

sarven's avatar
sarven committed
146 147
        $this->elementStart('fieldset', array('id' => 'settings_email_preferences'));
        $this->element('legend', null, _('Preferences'));
148

sarven's avatar
sarven committed
149
        $this->elementStart('ul', 'form_data');
sarven's avatar
sarven committed
150
        $this->elementStart('li');
151
        $this->checkbox('emailnotifysub',
152 153
                        _('Send me notices of new subscriptions through email.'),
                        $user->emailnotifysub);
sarven's avatar
sarven committed
154 155
        $this->elementEnd('li');
        $this->elementStart('li');
156
        $this->checkbox('emailnotifyfav',
157 158
                        _('Send me email when someone '.
                          'adds my notice as a favorite.'),
159
                        $user->emailnotifyfav);
sarven's avatar
sarven committed
160 161
        $this->elementEnd('li');
        $this->elementStart('li');
162
        $this->checkbox('emailnotifymsg',
163 164
                        _('Send me email when someone sends me a private message.'),
                        $user->emailnotifymsg);
sarven's avatar
sarven committed
165 166
        $this->elementEnd('li');
        $this->elementStart('li');
167 168 169 170 171
        $this->checkbox('emailnotifyattn',
                        _('Send me email when someone sends me an "@-reply".'),
                        $user->emailnotifyattn);
        $this->elementEnd('li');
        $this->elementStart('li');
172
        $this->checkbox('emailnotifynudge',
173 174
                        _('Allow friends to nudge me and send me an email.'),
                        $user->emailnotifynudge);
sarven's avatar
sarven committed
175
        $this->elementEnd('li');
176 177 178 179 180 181 182
        if (common_config('emailpost', 'enabled')) {
            $this->elementStart('li');
            $this->checkbox('emailpost',
                            _('I want to post notices by email.'),
                            $user->emailpost);
            $this->elementEnd('li');
        }
sarven's avatar
sarven committed
183
        $this->elementStart('li');
184
        $this->checkbox('emailmicroid',
185 186
                        _('Publish a MicroID for my email address.'),
                        $user->emailmicroid);
sarven's avatar
sarven committed
187 188
        $this->elementEnd('li');
        $this->elementEnd('ul');
189
        $this->submit('save', _('Save'));
sarven's avatar
sarven committed
190
        $this->elementEnd('fieldset');
191
        $this->elementEnd('form');
192 193
    }

194 195 196 197 198 199 200
    /**
     * Gets any existing email address confirmations we're waiting for
     *
     * @return Confirm_address Email address confirmation for user, or null
     */

    function getConfirmation()
201
    {
202
        $user = common_current_user();
203

204
        $confirm = new Confirm_address();
205 206

        $confirm->user_id      = $user->id;
207
        $confirm->address_type = 'email';
208

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
209
        if ($confirm->find(true)) {
210 211
            return $confirm;
        } else {
Evan Prodromou's avatar
Evan Prodromou committed
212
            return null;
213 214 215
        }
    }

216 217 218 219 220 221 222 223 224 225 226
    /**
     * Handle posts
     *
     * Since there are a lot of different options on the page, we
     * figure out what we're supposed to do based on which button was
     * pushed
     *
     * @return void
     */

    function handlePost()
227
    {
228
        // CSRF protection
229 230
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
231 232
            $this->show_form(_('There was a problem with your session token. '.
                               'Try again, please.'));
233 234 235 236
            return;
        }

        if ($this->arg('save')) {
237
            $this->savePreferences();
238
        } else if ($this->arg('add')) {
239
            $this->addAddress();
240
        } else if ($this->arg('cancel')) {
241
            $this->cancelConfirmation();
242
        } else if ($this->arg('remove')) {
243
            $this->removeAddress();
244
        } else if ($this->arg('removeincoming')) {
245
            $this->removeIncoming();
246
        } else if ($this->arg('newincoming')) {
247
            $this->newIncoming();
248
        } else {
249
            $this->showForm(_('Unexpected form submission.'));
250 251 252
        }
    }

253 254 255 256 257
    /**
     * Save email preferences
     *
     * @return void
     */
258

259 260 261 262 263
    function savePreferences()
    {
        $emailnotifysub   = $this->boolean('emailnotifysub');
        $emailnotifyfav   = $this->boolean('emailnotifyfav');
        $emailnotifymsg   = $this->boolean('emailnotifymsg');
264
        $emailnotifynudge = $this->boolean('emailnotifynudge');
265
        $emailnotifyattn  = $this->boolean('emailnotifyattn');
266 267
        $emailmicroid     = $this->boolean('emailmicroid');
        $emailpost        = $this->boolean('emailpost');
268 269 270

        $user = common_current_user();

271
        assert(!is_null($user)); // should already be checked
272 273 274 275 276

        $user->query('BEGIN');

        $original = clone($user);

277 278 279
        $user->emailnotifysub   = $emailnotifysub;
        $user->emailnotifyfav   = $emailnotifyfav;
        $user->emailnotifymsg   = $emailnotifymsg;
280
        $user->emailnotifynudge = $emailnotifynudge;
281
        $user->emailnotifyattn  = $emailnotifyattn;
282 283
        $user->emailmicroid     = $emailmicroid;
        $user->emailpost        = $emailpost;
284 285 286

        $result = $user->update($original);

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
287
        if ($result === false) {
288
            common_log_db_error($user, 'UPDATE', __FILE__);
289
            $this->serverError(_('Couldn\'t update user.'));
290 291 292 293 294
            return;
        }

        $user->query('COMMIT');

295
        $this->showForm(_('Preferences saved.'), true);
296 297
    }

298 299 300 301 302
    /**
     * Add the address passed in by the user
     *
     * @return void
     */
303

304 305
    function addAddress()
    {
306 307 308 309
        $user = common_current_user();

        $email = $this->trimmed('email');

310
        // Some validation
311 312

        if (!$email) {
313
            $this->showForm(_('No email address.'));
314 315 316 317 318 319
            return;
        }

        $email = common_canonical_email($email);

        if (!$email) {
320
            $this->showForm(_('Cannot normalize that email address'));
321 322 323
            return;
        }
        if (!Validate::email($email, true)) {
324
            $this->showForm(_('Not a valid email address'));
325 326
            return;
        } else if ($user->email == $email) {
327
            $this->showForm(_('That is already your email address.'));
328
            return;
329 330 331
        } else if ($this->emailExists($email)) {
            $this->showForm(_('That email address already belongs '.
                              'to another user.'));
332 333 334
            return;
        }

335 336 337 338 339 340
        $confirm = new Confirm_address();

        $confirm->address      = $email;
        $confirm->address_type = 'email';
        $confirm->user_id      = $user->id;
        $confirm->code         = common_confirmation_code(64);
341 342 343

        $result = $confirm->insert();

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
344
        if ($result === false) {
345
            common_log_db_error($confirm, 'INSERT', __FILE__);
346
            $this->serverError(_('Couldn\'t insert confirmation code.'));
347 348 349 350 351
            return;
        }

        mail_confirm_address($user, $confirm->code, $user->nickname, $email);

352 353 354
        $msg = _('A confirmation code was sent to the email address you added. '.
                 'Check your inbox (and spam box!) for the code and instructions '.
                 'on how to use it.');
355

356
        $this->showForm($msg, true);
357 358
    }

359 360 361 362 363 364 365
    /**
     * Handle a request to cancel email confirmation
     *
     * @return void
     */

    function cancelConfirmation()
366
    {
367
        $email = $this->arg('email');
368 369 370

        $confirm = $this->getConfirmation();

371
        if (!$confirm) {
372
            $this->showForm(_('No pending confirmation to cancel.'));
373 374 375
            return;
        }
        if ($confirm->address != $email) {
376
            $this->showForm(_('That is the wrong IM address.'));
377 378
            return;
        }
379 380 381 382

        $result = $confirm->delete();

        if (!$result) {
383
            common_log_db_error($confirm, 'DELETE', __FILE__);
384
            $this->serverError(_('Couldn\'t delete email confirmation.'));
385 386 387
            return;
        }

388
        $this->showForm(_('Confirmation cancelled.'), true);
389 390
    }

391 392 393 394 395
    /**
     * Handle a request to remove an address from the user's account
     *
     * @return void
     */
396

397 398
    function removeAddress()
    {
399
        $user = common_current_user();
400

401 402
        $email = $this->arg('email');

403
        // Maybe an old tab open...?
404 405

        if ($user->email != $email) {
406
            $this->showForm(_('That is not your email address.'));
407 408 409 410
            return;
        }

        $user->query('BEGIN');
411

412
        $original = clone($user);
413

Evan Prodromou's avatar
Evan Prodromou committed
414
        $user->email = null;
415

416
        $result = $user->updateKeys($original);
417

418 419
        if (!$result) {
            common_log_db_error($user, 'UPDATE', __FILE__);
420
            $this->serverError(_('Couldn\'t update user.'));
421 422 423 424
            return;
        }
        $user->query('COMMIT');

425
        $this->showForm(_('The address was removed.'), true);
426 427
    }

428 429 430 431 432 433 434
    /**
     * Handle a request to remove an incoming email address
     *
     * @return void
     */

    function removeIncoming()
435
    {
436
        $user = common_current_user();
437

438
        if (!$user->incomingemail) {
439
            $this->showForm(_('No incoming email address.'));
440 441
            return;
        }
442

443
        $orig = clone($user);
444

Evan Prodromou's avatar
Evan Prodromou committed
445
        $user->incomingemail = null;
446 447 448

        if (!$user->updateKeys($orig)) {
            common_log_db_error($user, 'UPDATE', __FILE__);
449
            $this->serverError(_("Couldn't update user record."));
450
        }
451 452

        $this->showForm(_('Incoming email address removed.'), true);
453 454
    }

455 456 457 458 459 460 461
    /**
     * Generate a new incoming email address
     *
     * @return void
     */

    function newIncoming()
462
    {
463
        $user = common_current_user();
464

465
        $orig = clone($user);
466

467
        $user->incomingemail = mail_new_incoming_address();
468

469 470
        if (!$user->updateKeys($orig)) {
            common_log_db_error($user, 'UPDATE', __FILE__);
471
            $this->serverError(_("Couldn't update user record."));
472 473
        }

474
        $this->showForm(_('New incoming email address added.'), true);
475
    }
476 477 478 479 480 481 482 483 484 485 486 487

    /**
     * Does another user already have this email address?
     *
     * Email addresses are unique for users.
     *
     * @param string $email Address to check
     *
     * @return boolean Whether the email already exists.
     */

    function emailExists($email)
488
    {
489
        $user = common_current_user();
490

491
        $other = User::staticGet('email', $email);
492

493 494 495 496 497 498
        if (!$other) {
            return false;
        } else {
            return $other->id != $user->id;
        }
    }
499
}