]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
Merge remote branch 'gitorious/0.9.x' 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                              // TRANS: Do not translate "example.org". It is one of the domain names reserved for use in examples by
141                              // TRANS: http://www.rfc-editor.org/rfc/rfc2606.txt. Any other domain may be owned by a legitimate
142                              // TRANS: person or organization.
143                              sprintf(_('Jabber or GTalk address, '.
144                                        'like "UserName@example.org". '.
145                                        'First, make sure to add %s to your '.
146                                        'buddy list in your IM client or on GTalk.'),
147                                      jabber_daemon_address()));
148                 $this->elementEnd('li');
149                 $this->elementEnd('ul');
150                 // TRANS: Button label for adding an IM address in IM settings form.
151                 $this->submit('add', _m('BUTTON','Add'));
152             }
153         }
154         $this->elementEnd('fieldset');
155         
156         $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
157         // TRANS: Form legend for IM preferences form.
158         $this->element('legend', null, _('IM preferences'));
159         $this->elementStart('ul', 'form_data');
160         $this->elementStart('li');
161         $this->checkbox('jabbernotify',
162                         // TRANS: Checkbox label in IM preferences form.
163                         _('Send me notices through Jabber/GTalk.'),
164                         $user->jabbernotify);
165         $this->elementEnd('li');
166         $this->elementStart('li');
167         $this->checkbox('updatefrompresence',
168                         // TRANS: Checkbox label in IM preferences form.
169                         _('Post a notice when my Jabber/GTalk status changes.'),
170                         $user->updatefrompresence);
171         $this->elementEnd('li');
172         $this->elementStart('li');
173         $this->checkbox('jabberreplies',
174                         // TRANS: Checkbox label in IM preferences form.
175                         _('Send me replies through Jabber/GTalk '.
176                           'from people I\'m not subscribed to.'),
177                         $user->jabberreplies);
178         $this->elementEnd('li');
179         $this->elementStart('li');
180         $this->checkbox('jabbermicroid',
181                         // TRANS: Checkbox label in IM preferences form.
182                         _('Publish a MicroID for my Jabber/GTalk address.'),
183                         $user->jabbermicroid);
184         $this->elementEnd('li');
185         $this->elementEnd('ul');
186         // TRANS: Button label to save IM preferences.
187         $this->submit('save', _m('BUTTON','Save'));
188         $this->elementEnd('fieldset');
189         $this->elementEnd('form');
190     }
191
192     /**
193      * Get a confirmation code for this user
194      *
195      * @return Confirm_address address object for this user
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 = 'jabber';
206
207         if ($confirm->find(true)) {
208             return $confirm;
209         } else {
210             return null;
211         }
212     }
213
214     /**
215      * Handle posts to this form
216      *
217      * Based on the button that was pressed, muxes out to other functions
218      * to do the actual task requested.
219      *
220      * All sub-functions reload the form with a message -- success or failure.
221      *
222      * @return void
223      */
224
225     function handlePost()
226     {
227         // CSRF protection
228         $token = $this->trimmed('token');
229         if (!$token || $token != common_session_token()) {
230             $this->showForm(_('There was a problem with your session token. '.
231                               'Try again, please.'));
232             return;
233         }
234
235         if ($this->arg('save')) {
236             $this->savePreferences();
237         } else if ($this->arg('add')) {
238             $this->addAddress();
239         } else if ($this->arg('cancel')) {
240             $this->cancelConfirmation();
241         } else if ($this->arg('remove')) {
242             $this->removeAddress();
243         } else {
244             // TRANS: Message given submitting a form with an unknown action in IM settings.
245             $this->showForm(_('Unexpected form submission.'));
246         }
247     }
248
249     /**
250      * Save user's Jabber preferences
251      *
252      * These are the checkboxes at the bottom of the page. They're used to
253      * set different settings
254      *
255      * @return void
256      */
257
258     function savePreferences()
259     {
260         $jabbernotify       = $this->boolean('jabbernotify');
261         $updatefrompresence = $this->boolean('updatefrompresence');
262         $jabberreplies      = $this->boolean('jabberreplies');
263         $jabbermicroid      = $this->boolean('jabbermicroid');
264
265         $user = common_current_user();
266
267         assert(!is_null($user)); // should already be checked
268
269         $user->query('BEGIN');
270
271         $original = clone($user);
272
273         $user->jabbernotify       = $jabbernotify;
274         $user->updatefrompresence = $updatefrompresence;
275         $user->jabberreplies      = $jabberreplies;
276         $user->jabbermicroid      = $jabbermicroid;
277
278         $result = $user->update($original);
279
280         if ($result === false) {
281             common_log_db_error($user, 'UPDATE', __FILE__);
282             // TRANS: Server error thrown on database error updating IM preferences.
283             $this->serverError(_('Couldn\'t update user.'));
284             return;
285         }
286
287         $user->query('COMMIT');
288
289         // TRANS: Confirmation message for successful IM preferences save.
290         $this->showForm(_('Preferences saved.'), true);
291     }
292
293     /**
294      * Sends a confirmation to the address given
295      *
296      * Stores a confirmation record and sends out a
297      * Jabber message with the confirmation info.
298      *
299      * @return void
300      */
301
302     function addAddress()
303     {
304         $user = common_current_user();
305
306         $jabber = $this->trimmed('jabber');
307
308         // Some validation
309
310         if (!$jabber) {
311             // TRANS: Message given saving IM address without having provided one.
312             $this->showForm(_('No Jabber ID.'));
313             return;
314         }
315
316         $jabber = jabber_normalize_jid($jabber);
317
318         if (!$jabber) {
319             // TRANS: Message given saving IM address that cannot be normalised.
320             $this->showForm(_('Cannot normalize that Jabber ID'));
321             return;
322         }
323         if (!jabber_valid_base_jid($jabber, common_config('email', 'domain_check'))) {
324             // TRANS: Message given saving IM address that not valid.
325             $this->showForm(_('Not a valid Jabber ID'));
326             return;
327         } else if ($user->jabber == $jabber) {
328             // TRANS: Message given saving IM address that is already set.
329             $this->showForm(_('That is already your Jabber ID.'));
330             return;
331         } else if ($this->jabberExists($jabber)) {
332             // TRANS: Message given saving IM address that is already set for another user.
333             $this->showForm(_('Jabber ID already belongs to another user.'));
334             return;
335         }
336
337         $confirm = new Confirm_address();
338
339         $confirm->address      = $jabber;
340         $confirm->address_type = 'jabber';
341         $confirm->user_id      = $user->id;
342         $confirm->code         = common_confirmation_code(64);
343         $confirm->sent         = common_sql_now();
344         $confirm->claimed      = common_sql_now();
345
346         $result = $confirm->insert();
347
348         if ($result === false) {
349             common_log_db_error($confirm, 'INSERT', __FILE__);
350             // TRANS: Server error thrown on database error adding IM confirmation code.
351             $this->serverError(_('Couldn\'t insert confirmation code.'));
352             return;
353         }
354
355         jabber_confirm_address($confirm->code,
356                                $user->nickname,
357                                $jabber);
358
359         // TRANS: Message given saving valid IM address that is to be confirmed.
360         // TRANS: %s is the IM address set for the site.
361         $msg = sprintf(_('A confirmation code was sent '.
362                          'to the IM address you added. '.
363                          'You must approve %s for '.
364                          'sending messages to you.'),
365                        jabber_daemon_address());
366
367         $this->showForm($msg, true);
368     }
369
370     /**
371      * Cancel a confirmation
372      *
373      * If a confirmation exists, cancel it.
374      *
375      * @return void
376      */
377
378     function cancelConfirmation()
379     {
380         $jabber = $this->arg('jabber');
381
382         $confirm = $this->getConfirmation();
383
384         if (!$confirm) {
385             // TRANS: Message given canceling IM address confirmation that is not pending.
386             $this->showForm(_('No pending confirmation to cancel.'));
387             return;
388         }
389         if ($confirm->address != $jabber) {
390             // TRANS: Message given canceling IM address confirmation for the wrong IM address.
391             $this->showForm(_('That is the wrong IM address.'));
392             return;
393         }
394
395         $result = $confirm->delete();
396
397         if (!$result) {
398             common_log_db_error($confirm, 'DELETE', __FILE__);
399             // TRANS: Server error thrown on database error canceling IM address confirmation.
400             $this->serverError(_('Couldn\'t delete IM confirmation.'));
401             return;
402         }
403
404         // TRANS: Message given after successfully canceling IM address confirmation.
405         $this->showForm(_('IM confirmation cancelled.'), true);
406     }
407
408     /**
409      * Remove an address
410      *
411      * If the user has a confirmed address, remove it.
412      *
413      * @return void
414      */
415
416     function removeAddress()
417     {
418         $user = common_current_user();
419
420         $jabber = $this->arg('jabber');
421
422         // Maybe an old tab open...?
423
424         if ($user->jabber != $jabber) {
425             // TRANS: Message given trying to remove an IM address that is not
426             // TRANS: registered for the active user.
427             $this->showForm(_('That is not your Jabber ID.'));
428             return;
429         }
430
431         $user->query('BEGIN');
432
433         $original = clone($user);
434
435         $user->jabber = null;
436
437         $result = $user->updateKeys($original);
438
439         if (!$result) {
440             common_log_db_error($user, 'UPDATE', __FILE__);
441             // TRANS: Server error thrown on database error removing a registered IM address.
442             $this->serverError(_('Couldn\'t update user.'));
443             return;
444         }
445         $user->query('COMMIT');
446
447         // XXX: unsubscribe to the old address
448
449         // TRANS: Message given after successfully removing a registered IM address.
450         $this->showForm(_('The IM address was removed.'), true);
451     }
452
453     /**
454      * Does this Jabber ID exist?
455      *
456      * Checks if we already have another user with this address.
457      *
458      * @param string $jabber Address to check
459      *
460      * @return boolean whether the Jabber ID exists
461      */
462
463     function jabberExists($jabber)
464     {
465         $user = common_current_user();
466
467         $other = User::staticGet('jabber', $jabber);
468
469         if (!$other) {
470             return false;
471         } else {
472             return $other->id != $user->id;
473         }
474     }
475 }