We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

imsettings.php 16.2 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5 6 7 8 9
 * Settings for Jabber/XMPP integration
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
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
 * @author    Evan Prodromou <evan@status.net>
25
 * @copyright 2008-2009 StatusNet, Inc.
26
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27
 * @link      http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
28 29
 */

30
if (!defined('STATUSNET') && !defined('LACONICA')) {
31 32
    exit(1);
}
Evan Prodromou's avatar
Evan Prodromou committed
33

34
require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 36 37 38 39 40
require_once INSTALLDIR.'/lib/jabber.php';

/**
 * Settings for Jabber/XMPP integration
 *
 * @category Settings
41
 * @package  StatusNet
42
 * @author   Evan Prodromou <evan@status.net>
43
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44
 * @link     http://status.net/
45 46 47
 *
 * @see      SettingsAction
 */
Evan Prodromou's avatar
Evan Prodromou committed
48

49
class ImsettingsAction extends ConnectSettingsAction
50
{
51 52 53 54 55 56 57 58
    /**
     * Title of the page
     *
     * @return string Title of the page
     */

    function title()
    {
59
        // TRANS: Title for instance messaging settings.
60
        return _('IM settings');
61 62 63 64 65 66 67
    }

    /**
     * Instructions for use
     *
     * @return instructions for use
     */
Evan Prodromou's avatar
Evan Prodromou committed
68

69
    function getInstructions()
70
    {
71 72 73
        // TRANS: Instant messaging settings page instructions.
        // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
        // TRANS: the order and formatting of link text and link should remain unchanged.
74 75 76
        return _('You can send and receive notices through '.
                 'Jabber/GTalk [instant messages](%%doc.im%%). '.
                 'Configure your address and settings below.');
77 78
    }

79 80 81 82 83 84 85 86 87 88 89
    /**
     * Content area of the page
     *
     * We make different sections of the form for the different kinds of
     * functions, and have submit buttons with different names. These
     * are muxed by handlePost() to see what the user really wants to do.
     *
     * @return void
     */

    function showContent()
90
    {
91 92
        if (!common_config('xmpp', 'enabled')) {
            $this->element('div', array('class' => 'error'),
93
                           // TRANS: Message given in the IM settings if XMPP is not enabled on the site.
94 95 96 97
                           _('IM is not available.'));
            return;
        }

98
        $user = common_current_user();
99
        $this->elementStart('form', array('method' => 'post',
sarven's avatar
sarven committed
100 101
                                          'id' => 'form_settings_im',
                                          'class' => 'form_settings',
102 103
                                          'action' =>
                                          common_local_url('imsettings')));
sarven's avatar
sarven committed
104
        $this->elementStart('fieldset', array('id' => 'settings_im_address'));
105 106
        // TRANS: Form legend for IM settings form.
        $this->element('legend', null, _('IM address'));
107
        $this->hidden('token', common_session_token());
108 109

        if ($user->jabber) {
sarven's avatar
sarven committed
110
            $this->element('p', 'form_confirmed', $user->jabber);
111
            // TRANS: Form note in IM settings form.
sarven's avatar
sarven committed
112
            $this->element('p', 'form_note',
113
                           _('Current confirmed Jabber/GTalk address.'));
114
            $this->hidden('jabber', $user->jabber);
115 116
            // TRANS: Button label to remove a confirmed IM address.
            $this->submit('remove', _m('BUTTON','Remove'));
117
        } else {
118
            $confirm = $this->getConfirmation();
119
            if ($confirm) {
sarven's avatar
sarven committed
120 121
                $this->element('p', 'form_unconfirmed', $confirm->address);
                $this->element('p', 'form_note',
122 123
                               // TRANS: Form note in IM settings form.
                               // TRANS: %s is the IM address set for the site.
124 125 126 127 128
                               sprintf(_('Awaiting confirmation on this address. '.
                                         'Check your Jabber/GTalk account for a '.
                                         'message with further instructions. '.
                                         '(Did you add %s to your buddy list?)'),
                                       jabber_daemon_address()));
129
                $this->hidden('jabber', $confirm->address);
130
                // TRANS: Button label to cancel an IM address confirmation procedure.
131
                $this->submit('cancel', _m('BUTTON','Cancel'));
132
            } else {
sarven's avatar
sarven committed
133
                $this->elementStart('ul', 'form_data');
sarven's avatar
sarven committed
134
                $this->elementStart('li');
135
                // TRANS: Field label for IM address input in IM settings form.
136
                $this->input('jabber', _('IM address'),
Evan Prodromou's avatar
Evan Prodromou committed
137
                             ($this->arg('jabber')) ? $this->arg('jabber') : null,
138
                             // TRANS: IM address input field instructions in IM settings form.
139
                             // TRANS: %s is the IM address set for the site.
140 141 142 143 144
                             sprintf(_('Jabber or GTalk address, '.
                                       'like "UserName@example.org". '.
                                       'First, make sure to add %s to your '.
                                       'buddy list in your IM client or on GTalk.'),
                                     jabber_daemon_address()));
sarven's avatar
sarven committed
145 146
                $this->elementEnd('li');
                $this->elementEnd('ul');
147 148
                // TRANS: Button label for adding an IM address in IM settings form.
                $this->submit('add', _m('BUTTON','Add'));
149 150
            }
        }
sarven's avatar
sarven committed
151 152 153
        $this->elementEnd('fieldset');
        
        $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
154 155
        // TRANS: Form legend for IM preferences form.
        $this->element('legend', null, _('IM preferences'));
sarven's avatar
sarven committed
156
        $this->elementStart('ul', 'form_data');
sarven's avatar
sarven committed
157
        $this->elementStart('li');
158
        $this->checkbox('jabbernotify',
159
                        // TRANS: Checkbox label in IM preferences form.
160 161
                        _('Send me notices through Jabber/GTalk.'),
                        $user->jabbernotify);
sarven's avatar
sarven committed
162 163
        $this->elementEnd('li');
        $this->elementStart('li');
164
        $this->checkbox('updatefrompresence',
165
                        // TRANS: Checkbox label in IM preferences form.
166 167
                        _('Post a notice when my Jabber/GTalk status changes.'),
                        $user->updatefrompresence);
sarven's avatar
sarven committed
168 169
        $this->elementEnd('li');
        $this->elementStart('li');
170
        $this->checkbox('jabberreplies',
171
                        // TRANS: Checkbox label in IM preferences form.
172 173
                        _('Send me replies through Jabber/GTalk '.
                          'from people I\'m not subscribed to.'),
174
                        $user->jabberreplies);
sarven's avatar
sarven committed
175 176
        $this->elementEnd('li');
        $this->elementStart('li');
177
        $this->checkbox('jabbermicroid',
178
                        // TRANS: Checkbox label in IM preferences form.
179 180
                        _('Publish a MicroID for my Jabber/GTalk address.'),
                        $user->jabbermicroid);
sarven's avatar
sarven committed
181 182
        $this->elementEnd('li');
        $this->elementEnd('ul');
183 184
        // TRANS: Button label to save IM preferences.
        $this->submit('save', _m('BUTTON','Save'));
sarven's avatar
sarven committed
185
        $this->elementEnd('fieldset');
186
        $this->elementEnd('form');
187 188
    }

189 190 191 192 193 194 195
    /**
     * Get a confirmation code for this user
     *
     * @return Confirm_address address object for this user
     */

    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 = 'jabber';
203

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
204
        if ($confirm->find(true)) {
205 206
            return $confirm;
        } else {
Evan Prodromou's avatar
Evan Prodromou committed
207
            return null;
208 209 210
        }
    }

211 212 213 214 215 216 217 218 219 220 221 222
    /**
     * Handle posts to this form
     *
     * Based on the button that was pressed, muxes out to other functions
     * to do the actual task requested.
     *
     * All sub-functions reload the form with a message -- success or failure.
     *
     * @return void
     */

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

        if ($this->arg('save')) {
233
            $this->savePreferences();
234
        } else if ($this->arg('add')) {
235
            $this->addAddress();
236
        } else if ($this->arg('cancel')) {
237
            $this->cancelConfirmation();
238
        } else if ($this->arg('remove')) {
239
            $this->removeAddress();
240
        } else {
241
            // TRANS: Message given submitting a form with an unknown action in IM settings.
242
            $this->showForm(_('Unexpected form submission.'));
243 244 245
        }
    }

246 247 248 249 250 251 252 253 254 255
    /**
     * Save user's Jabber preferences
     *
     * These are the checkboxes at the bottom of the page. They're used to
     * set different settings
     *
     * @return void
     */

    function savePreferences()
256
    {
257
        $jabbernotify       = $this->boolean('jabbernotify');
258
        $updatefrompresence = $this->boolean('updatefrompresence');
259 260
        $jabberreplies      = $this->boolean('jabberreplies');
        $jabbermicroid      = $this->boolean('jabbermicroid');
261 262 263

        $user = common_current_user();

264
        assert(!is_null($user)); // should already be checked
265 266 267 268 269

        $user->query('BEGIN');

        $original = clone($user);

270
        $user->jabbernotify       = $jabbernotify;
271
        $user->updatefrompresence = $updatefrompresence;
272 273
        $user->jabberreplies      = $jabberreplies;
        $user->jabbermicroid      = $jabbermicroid;
274 275 276

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

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
277
        if ($result === false) {
278
            common_log_db_error($user, 'UPDATE', __FILE__);
279
            // TRANS: Server error thrown on database error updating IM preferences.
280
            $this->serverError(_('Couldn\'t update user.'));
281 282 283 284 285
            return;
        }

        $user->query('COMMIT');

286
        // TRANS: Confirmation message for successful IM preferences save.
287
        $this->showForm(_('Preferences saved.'), true);
288 289
    }

290 291 292 293 294 295 296 297
    /**
     * Sends a confirmation to the address given
     *
     * Stores a confirmation record and sends out a
     * Jabber message with the confirmation info.
     *
     * @return void
     */
298

299 300
    function addAddress()
    {
301 302 303 304
        $user = common_current_user();

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

305
        // Some validation
306 307

        if (!$jabber) {
308
            // TRANS: Message given saving IM address without having provided one.
309
            $this->showForm(_('No Jabber ID.'));
310 311 312 313 314 315
            return;
        }

        $jabber = jabber_normalize_jid($jabber);

        if (!$jabber) {
316
            // TRANS: Message given saving IM address that cannot be normalised.
317
            $this->showForm(_('Cannot normalize that Jabber ID'));
318 319
            return;
        }
320
        if (!jabber_valid_base_jid($jabber, common_config('email', 'domain_check'))) {
321
            // TRANS: Message given saving IM address that not valid.
322
            $this->showForm(_('Not a valid Jabber ID'));
323 324
            return;
        } else if ($user->jabber == $jabber) {
325
            // TRANS: Message given saving IM address that is already set.
326
            $this->showForm(_('That is already your Jabber ID.'));
327
            return;
328
        } else if ($this->jabberExists($jabber)) {
329
            // TRANS: Message given saving IM address that is already set for another user.
330
            $this->showForm(_('Jabber ID already belongs to another user.'));
331 332 333
            return;
        }

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

        $confirm->address      = $jabber;
        $confirm->address_type = 'jabber';
        $confirm->user_id      = $user->id;
        $confirm->code         = common_confirmation_code(64);
340 341
        $confirm->sent         = common_sql_now();
        $confirm->claimed      = common_sql_now();
342 343 344

        $result = $confirm->insert();

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
345
        if ($result === false) {
346
            common_log_db_error($confirm, 'INSERT', __FILE__);
347
            // TRANS: Server error thrown on database error adding IM confirmation code.
348
            $this->serverError(_('Couldn\'t insert confirmation code.'));
349 350 351
            return;
        }

352 353 354
        jabber_confirm_address($confirm->code,
                               $user->nickname,
                               $jabber);
355

356 357
        // TRANS: Message given saving valid IM address that is to be confirmed.
        // TRANS: %s is the IM address set for the site.
358 359 360 361 362
        $msg = sprintf(_('A confirmation code was sent '.
                         'to the IM address you added. '.
                         'You must approve %s for '.
                         'sending messages to you.'),
                       jabber_daemon_address());
363

364
        $this->showForm($msg, true);
365 366
    }

367 368 369 370 371 372 373 374 375
    /**
     * Cancel a confirmation
     *
     * If a confirmation exists, cancel it.
     *
     * @return void
     */

    function cancelConfirmation()
376
    {
377
        $jabber = $this->arg('jabber');
378 379 380

        $confirm = $this->getConfirmation();

381
        if (!$confirm) {
382
            // TRANS: Message given canceling IM address confirmation that is not pending.
383
            $this->showForm(_('No pending confirmation to cancel.'));
384 385 386
            return;
        }
        if ($confirm->address != $jabber) {
387
            // TRANS: Message given canceling IM address confirmation for the wrong IM address.
388
            $this->showForm(_('That is the wrong IM address.'));
389 390
            return;
        }
391 392 393 394

        $result = $confirm->delete();

        if (!$result) {
395
            common_log_db_error($confirm, 'DELETE', __FILE__);
396 397
            // TRANS: Server error thrown on database error canceling IM address confirmation.
            $this->serverError(_('Couldn\'t delete IM confirmation.'));
398 399 400
            return;
        }

401 402
        // TRANS: Message given after successfully canceling IM address confirmation.
        $this->showForm(_('IM confirmation cancelled.'), true);
403 404
    }

405 406 407 408 409 410 411
    /**
     * Remove an address
     *
     * If the user has a confirmed address, remove it.
     *
     * @return void
     */
412

413 414
    function removeAddress()
    {
415
        $user = common_current_user();
416

417 418
        $jabber = $this->arg('jabber');

419
        // Maybe an old tab open...?
420 421

        if ($user->jabber != $jabber) {
422 423
            // TRANS: Message given trying to remove an IM address that is not
            // TRANS: registered for the active user.
424
            $this->showForm(_('That is not your Jabber ID.'));
425 426 427 428
            return;
        }

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

430
        $original = clone($user);
431

Evan Prodromou's avatar
Evan Prodromou committed
432
        $user->jabber = null;
433

434
        $result = $user->updateKeys($original);
435

436 437
        if (!$result) {
            common_log_db_error($user, 'UPDATE', __FILE__);
438
            // TRANS: Server error thrown on database error removing a registered IM address.
439
            $this->serverError(_('Couldn\'t update user.'));
440 441 442 443
            return;
        }
        $user->query('COMMIT');

444
        // XXX: unsubscribe to the old address
445

446 447
        // TRANS: Message given after successfully removing a registered IM address.
        $this->showForm(_('The IM address was removed.'), true);
448 449
    }

450 451 452 453 454 455 456 457 458 459 460
    /**
     * Does this Jabber ID exist?
     *
     * Checks if we already have another user with this address.
     *
     * @param string $jabber Address to check
     *
     * @return boolean whether the Jabber ID exists
     */

    function jabberExists($jabber)
461
    {
462
        $user = common_current_user();
463

464
        $other = User::staticGet('jabber', $jabber);
465

466 467 468 469 470 471
        if (!$other) {
            return false;
        } else {
            return $other->id != $user->id;
        }
    }
Evan Prodromou's avatar
Evan Prodromou committed
472
}