]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
Merge branch 'apinamespace' into 0.9.x
[quix0rs-gnu-social.git] / actions / imsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Settings for Jabber/XMPP integration
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   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/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 require_once INSTALLDIR.'/lib/jabber.php';
36
37 /**
38  * Settings for Jabber/XMPP integration
39  *
40  * @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  * @see      SettingsAction
47  */
48
49 class ImsettingsAction extends ConnectSettingsAction
50 {
51     /**
52      * Title of the page
53      *
54      * @return string Title of the page
55      */
56
57     function title()
58     {
59         // TRANS: Title for instance messaging settings.
60         return _('IM settings');
61     }
62
63     /**
64      * Instructions for use
65      *
66      * @return instructions for use
67      */
68
69     function getInstructions()
70     {
71         // TRANS: Instant messaging settings page instructions.
72         // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
73         // TRANS: the order and formatting of link text and link should remain unchanged.
74         return _('You can send and receive notices through '.
75                  'Jabber/GTalk [instant messages](%%doc.im%%). '.
76                  'Configure your address and settings below.');
77     }
78
79     /**
80      * Content area of the page
81      *
82      * We make different sections of the form for the different kinds of
83      * functions, and have submit buttons with different names. These
84      * are muxed by handlePost() to see what the user really wants to do.
85      *
86      * @return void
87      */
88
89     function showContent()
90     {
91         if (!common_config('xmpp', 'enabled')) {
92             $this->element('div', array('class' => 'error'),
93                            // TRANS: Message given in the IM settings if XMPP is not enabled on the site.
94                            _('IM is not available.'));
95             return;
96         }
97
98         $user = common_current_user();
99         $this->elementStart('form', array('method' => 'post',
100                                           'id' => 'form_settings_im',
101                                           'class' => 'form_settings',
102                                           'action' =>
103                                           common_local_url('imsettings')));
104         $this->elementStart('fieldset', array('id' => 'settings_im_address'));
105         // TRANS: Form legend for IM settings form.
106         $this->element('legend', null, _('IM address'));
107         $this->hidden('token', common_session_token());
108
109         if ($user->jabber) {
110             $this->element('p', 'form_confirmed', $user->jabber);
111             // TRANS: Form note in IM settings form.
112             $this->element('p', 'form_note',
113                            _('Current confirmed Jabber/GTalk address.'));
114             $this->hidden('jabber', $user->jabber);
115             // TRANS: Button label to remove a confirmed IM address.
116             $this->submit('remove', _m('BUTTON','Remove'));
117         } else {
118             $confirm = $this->getConfirmation();
119             if ($confirm) {
120                 $this->element('p', 'form_unconfirmed', $confirm->address);
121                 $this->element('p', 'form_note',
122                                // TRANS: Form note in IM settings form.
123                                // TRANS: %s is the IM address set for the site.
124                                sprintf(_('Awaiting confirmation on this address. '.
125                                          'Check your Jabber/GTalk account for a '.
126                                          'message with further instructions. '.
127                                          '(Did you add %s to your buddy list?)'),
128                                        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 {
133                 $this->elementStart('ul', 'form_data');
134                 $this->elementStart('li');
135                 // TRANS: Field label for IM address input in IM settings form.
136                 $this->input('jabber', _('IM address'),
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                              sprintf(_('Jabber or GTalk address, '.
141                                        'like "UserName@example.org". '.
142                                        'First, make sure to add %s to your '.
143                                        'buddy list in your IM client or on GTalk.'),
144                                      jabber_daemon_address()));
145                 $this->elementEnd('li');
146                 $this->elementEnd('ul');
147                 // TRANS: Button label for adding an IM address in IM settings form.
148                 $this->submit('add', _m('BUTTON','Add'));
149             }
150         }
151         $this->elementEnd('fieldset');
152         
153         $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
154         // TRANS: Form legend for IM preferences form.
155         $this->element('legend', null, _('IM preferences'));
156         $this->elementStart('ul', 'form_data');
157         $this->elementStart('li');
158         $this->checkbox('jabbernotify',
159                         // TRANS: Checkbox label in IM preferences form.
160                         _('Send me notices through Jabber/GTalk.'),
161                         $user->jabbernotify);
162         $this->elementEnd('li');
163         $this->elementStart('li');
164         $this->checkbox('updatefrompresence',
165                         // TRANS: Checkbox label in IM preferences form.
166                         _('Post a notice when my Jabber/GTalk status changes.'),
167                         $user->updatefrompresence);
168         $this->elementEnd('li');
169         $this->elementStart('li');
170         $this->checkbox('jabberreplies',
171                         // TRANS: Checkbox label in IM preferences form.
172                         _('Send me replies through Jabber/GTalk '.
173                           'from people I\'m not subscribed to.'),
174                         $user->jabberreplies);
175         $this->elementEnd('li');
176         $this->elementStart('li');
177         $this->checkbox('jabbermicroid',
178                         // TRANS: Checkbox label in IM preferences form.
179                         _('Publish a MicroID for my Jabber/GTalk address.'),
180                         $user->jabbermicroid);
181         $this->elementEnd('li');
182         $this->elementEnd('ul');
183         // TRANS: Button label to save IM preferences.
184         $this->submit('save', _m('BUTTON','Save'));
185         $this->elementEnd('fieldset');
186         $this->elementEnd('form');
187     }
188
189     /**
190      * Get a confirmation code for this user
191      *
192      * @return Confirm_address address object for this user
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 = 'jabber';
203
204         if ($confirm->find(true)) {
205             return $confirm;
206         } else {
207             return null;
208         }
209     }
210
211     /**
212      * Handle posts to this form
213      *
214      * Based on the button that was pressed, muxes out to other functions
215      * to do the actual task requested.
216      *
217      * All sub-functions reload the form with a message -- success or failure.
218      *
219      * @return void
220      */
221
222     function handlePost()
223     {
224         // CSRF protection
225         $token = $this->trimmed('token');
226         if (!$token || $token != common_session_token()) {
227             $this->showForm(_('There was a problem with your session token. '.
228                               'Try again, please.'));
229             return;
230         }
231
232         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      * Save user's Jabber preferences
248      *
249      * These are the checkboxes at the bottom of the page. They're used to
250      * set different settings
251      *
252      * @return void
253      */
254
255     function savePreferences()
256     {
257         $jabbernotify       = $this->boolean('jabbernotify');
258         $updatefrompresence = $this->boolean('updatefrompresence');
259         $jabberreplies      = $this->boolean('jabberreplies');
260         $jabbermicroid      = $this->boolean('jabbermicroid');
261
262         $user = common_current_user();
263
264         assert(!is_null($user)); // should already be checked
265
266         $user->query('BEGIN');
267
268         $original = clone($user);
269
270         $user->jabbernotify       = $jabbernotify;
271         $user->updatefrompresence = $updatefrompresence;
272         $user->jabberreplies      = $jabberreplies;
273         $user->jabbermicroid      = $jabbermicroid;
274
275         $result = $user->update($original);
276
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             return;
282         }
283
284         $user->query('COMMIT');
285
286         // TRANS: Confirmation message for successful IM preferences save.
287         $this->showForm(_('Preferences saved.'), true);
288     }
289
290     /**
291      * Sends a confirmation to the address given
292      *
293      * Stores a confirmation record and sends out a
294      * Jabber message with the confirmation info.
295      *
296      * @return void
297      */
298
299     function addAddress()
300     {
301         $user = common_current_user();
302
303         $jabber = $this->trimmed('jabber');
304
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             return;
311         }
312
313         $jabber = jabber_normalize_jid($jabber);
314
315         if (!$jabber) {
316             // TRANS: Message given saving IM address that cannot be normalised.
317             $this->showForm(_('Cannot normalize that Jabber ID'));
318             return;
319         }
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             return;
324         } 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             return;
332         }
333
334         $confirm = new Confirm_address();
335
336         $confirm->address      = $jabber;
337         $confirm->address_type = 'jabber';
338         $confirm->user_id      = $user->id;
339         $confirm->code         = common_confirmation_code(64);
340         $confirm->sent         = common_sql_now();
341         $confirm->claimed      = common_sql_now();
342
343         $result = $confirm->insert();
344
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             return;
350         }
351
352         jabber_confirm_address($confirm->code,
353                                $user->nickname,
354                                $jabber);
355
356         // TRANS: Message given saving valid IM address that is to be confirmed.
357         // TRANS: %s is the IM address set for the site.
358         $msg = sprintf(_('A confirmation code was sent '.
359                          'to the IM address you added. '.
360                          'You must approve %s for '.
361                          'sending messages to you.'),
362                        jabber_daemon_address());
363
364         $this->showForm($msg, true);
365     }
366
367     /**
368      * Cancel a confirmation
369      *
370      * If a confirmation exists, cancel it.
371      *
372      * @return void
373      */
374
375     function cancelConfirmation()
376     {
377         $jabber = $this->arg('jabber');
378
379         $confirm = $this->getConfirmation();
380
381         if (!$confirm) {
382             // TRANS: Message given canceling IM address confirmation that is not pending.
383             $this->showForm(_('No pending confirmation to cancel.'));
384             return;
385         }
386         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             return;
390         }
391
392         $result = $confirm->delete();
393
394         if (!$result) {
395             common_log_db_error($confirm, 'DELETE', __FILE__);
396             // TRANS: Server error thrown on database error canceling IM address confirmation.
397             $this->serverError(_('Couldn\'t delete IM confirmation.'));
398             return;
399         }
400
401         // TRANS: Message given after successfully canceling IM address confirmation.
402         $this->showForm(_('IM confirmation cancelled.'), true);
403     }
404
405     /**
406      * Remove an address
407      *
408      * If the user has a confirmed address, remove it.
409      *
410      * @return void
411      */
412
413     function removeAddress()
414     {
415         $user = common_current_user();
416
417         $jabber = $this->arg('jabber');
418
419         // Maybe an old tab open...?
420
421         if ($user->jabber != $jabber) {
422             // TRANS: Message given trying to remove an IM address that is not
423             // TRANS: registered for the active user.
424             $this->showForm(_('That is not your Jabber ID.'));
425             return;
426         }
427
428         $user->query('BEGIN');
429
430         $original = clone($user);
431
432         $user->jabber = null;
433
434         $result = $user->updateKeys($original);
435
436         if (!$result) {
437             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             return;
441         }
442         $user->query('COMMIT');
443
444         // XXX: unsubscribe to the old address
445
446         // TRANS: Message given after successfully removing a registered IM address.
447         $this->showForm(_('The IM address was removed.'), true);
448     }
449
450     /**
451      * Does this Jabber ID exist?
452      *
453      * Checks if we already have another user with this address.
454      *
455      * @param string $jabber Address to check
456      *
457      * @return boolean whether the Jabber ID exists
458      */
459
460     function jabberExists($jabber)
461     {
462         $user = common_current_user();
463
464         $other = User::staticGet('jabber', $jabber);
465
466         if (!$other) {
467             return false;
468         } else {
469             return $other->id != $user->id;
470         }
471     }
472 }