]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
Merge commit 'holizz/0.8.x-fasterjs' into 0.8.x
[quix0rs-gnu-social.git] / actions / imsettings.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!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  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
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         return _('IM Settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
70         return _('You can send and receive notices through '.
71                  'Jabber/GTalk [instant messages](%%doc.im%%). '.
72                  'Configure your address and settings below.');
73     }
74
75     /**
76      * Content area of the page
77      *
78      * We make different sections of the form for the different kinds of
79      * functions, and have submit buttons with different names. These
80      * are muxed by handlePost() to see what the user really wants to do.
81      *
82      * @return void
83      */
84
85     function showContent()
86     {
87         if (!common_config('xmpp', 'enabled')) {
88             $this->element('div', array('class' => 'error'),
89                            _('IM is not available.'));
90             return;
91         }
92
93         $user = common_current_user();
94         $this->elementStart('form', array('method' => 'post',
95                                           'id' => 'form_settings_im',
96                                           'class' => 'form_settings',
97                                           'action' =>
98                                           common_local_url('imsettings')));
99         $this->elementStart('fieldset', array('id' => 'settings_im_address'));
100         $this->element('legend', null, _('Address'));
101         $this->hidden('token', common_session_token());
102
103         if ($user->jabber) {
104             $this->element('p', 'form_confirmed', $user->jabber);
105             $this->element('p', 'form_note',
106                            _('Current confirmed Jabber/GTalk address.'));
107             $this->hidden('jabber', $user->jabber);
108             $this->submit('remove', _('Remove'));
109         } else {
110             $confirm = $this->getConfirmation();
111             if ($confirm) {
112                 $this->element('p', 'form_unconfirmed', $confirm->address);
113                 $this->element('p', 'form_note',
114                                sprintf(_('Awaiting confirmation on this address. '.
115                                          'Check your Jabber/GTalk account for a '.
116                                          'message with further instructions. '.
117                                          '(Did you add %s to your buddy list?)'),
118                                        jabber_daemon_address()));
119                 $this->hidden('jabber', $confirm->address);
120                 $this->submit('cancel', _('Cancel'));
121             } else {
122                 $this->elementStart('ul', 'form_data');
123                 $this->elementStart('li');
124                 $this->input('jabber', _('IM Address'),
125                              ($this->arg('jabber')) ? $this->arg('jabber') : null,
126                              sprintf(_('Jabber or GTalk address, '.
127                                        'like "UserName@example.org". '.
128                                        'First, make sure to add %s to your '.
129                                        'buddy list in your IM client or on GTalk.'),
130                                      jabber_daemon_address()));
131                 $this->elementEnd('li');
132                 $this->elementEnd('ul');
133                 $this->submit('add', _('Add'));
134             }
135         }
136         $this->elementEnd('fieldset');
137         
138         $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
139         $this->element('legend', null, _('Preferences'));
140         $this->elementStart('ul', 'form_data');
141         $this->elementStart('li');
142         $this->checkbox('jabbernotify',
143                         _('Send me notices through Jabber/GTalk.'),
144                         $user->jabbernotify);
145         $this->elementEnd('li');
146         $this->elementStart('li');
147         $this->checkbox('updatefrompresence',
148                         _('Post a notice when my Jabber/GTalk status changes.'),
149                         $user->updatefrompresence);
150         $this->elementEnd('li');
151         $this->elementStart('li');
152         $this->checkbox('jabberreplies',
153                         _('Send me replies through Jabber/GTalk '.
154                           'from people I\'m not subscribed to.'),
155                         $user->jabberreplies);
156         $this->elementEnd('li');
157         $this->elementStart('li');
158         $this->checkbox('jabbermicroid',
159                         _('Publish a MicroID for my Jabber/GTalk address.'),
160                         $user->jabbermicroid);
161         $this->elementEnd('li');
162         $this->elementEnd('ul');
163         $this->submit('save', _('Save'));
164         $this->elementEnd('fieldset');
165         $this->elementEnd('form');
166     }
167
168     /**
169      * Get a confirmation code for this user
170      *
171      * @return Confirm_address address object for this user
172      */
173
174     function getConfirmation()
175     {
176         $user = common_current_user();
177
178         $confirm = new Confirm_address();
179
180         $confirm->user_id      = $user->id;
181         $confirm->address_type = 'jabber';
182
183         if ($confirm->find(true)) {
184             return $confirm;
185         } else {
186             return null;
187         }
188     }
189
190     /**
191      * Handle posts to this form
192      *
193      * Based on the button that was pressed, muxes out to other functions
194      * to do the actual task requested.
195      *
196      * All sub-functions reload the form with a message -- success or failure.
197      *
198      * @return void
199      */
200
201     function handlePost()
202     {
203         // CSRF protection
204         $token = $this->trimmed('token');
205         if (!$token || $token != common_session_token()) {
206             $this->showForm(_('There was a problem with your session token. '.
207                               'Try again, please.'));
208             return;
209         }
210
211         if ($this->arg('save')) {
212             $this->savePreferences();
213         } else if ($this->arg('add')) {
214             $this->addAddress();
215         } else if ($this->arg('cancel')) {
216             $this->cancelConfirmation();
217         } else if ($this->arg('remove')) {
218             $this->removeAddress();
219         } else {
220             $this->showForm(_('Unexpected form submission.'));
221         }
222     }
223
224     /**
225      * Save user's Jabber preferences
226      *
227      * These are the checkboxes at the bottom of the page. They're used to
228      * set different settings
229      *
230      * @return void
231      */
232
233     function savePreferences()
234     {
235
236         $jabbernotify       = $this->boolean('jabbernotify');
237         $updatefrompresence = $this->boolean('updatefrompresence');
238         $jabberreplies      = $this->boolean('jabberreplies');
239         $jabbermicroid      = $this->boolean('jabbermicroid');
240
241         $user = common_current_user();
242
243         assert(!is_null($user)); // should already be checked
244
245         $user->query('BEGIN');
246
247         $original = clone($user);
248
249         $user->jabbernotify       = $jabbernotify;
250         $user->updatefrompresence = $updatefrompresence;
251         $user->jabberreplies      = $jabberreplies;
252         $user->jabbermicroid      = $jabbermicroid;
253
254         $result = $user->update($original);
255
256         if ($result === false) {
257             common_log_db_error($user, 'UPDATE', __FILE__);
258             $this->serverError(_('Couldn\'t update user.'));
259             return;
260         }
261
262         $user->query('COMMIT');
263
264         $this->showForm(_('Preferences saved.'), true);
265     }
266
267     /**
268      * Sends a confirmation to the address given
269      *
270      * Stores a confirmation record and sends out a
271      * Jabber message with the confirmation info.
272      *
273      * @return void
274      */
275
276     function addAddress()
277     {
278         $user = common_current_user();
279
280         $jabber = $this->trimmed('jabber');
281
282         // Some validation
283
284         if (!$jabber) {
285             $this->showForm(_('No Jabber ID.'));
286             return;
287         }
288
289         $jabber = jabber_normalize_jid($jabber);
290
291         if (!$jabber) {
292             $this->showForm(_('Cannot normalize that Jabber ID'));
293             return;
294         }
295         if (!jabber_valid_base_jid($jabber)) {
296             $this->showForm(_('Not a valid Jabber ID'));
297             return;
298         } else if ($user->jabber == $jabber) {
299             $this->showForm(_('That is already your Jabber ID.'));
300             return;
301         } else if ($this->jabberExists($jabber)) {
302             $this->showForm(_('Jabber ID already belongs to another user.'));
303             return;
304         }
305
306         $confirm = new Confirm_address();
307
308         $confirm->address      = $jabber;
309         $confirm->address_type = 'jabber';
310         $confirm->user_id      = $user->id;
311         $confirm->code         = common_confirmation_code(64);
312
313         $result = $confirm->insert();
314
315         if ($result === false) {
316             common_log_db_error($confirm, 'INSERT', __FILE__);
317             $this->serverError(_('Couldn\'t insert confirmation code.'));
318             return;
319         }
320
321         if (!common_config('queue', 'enabled')) {
322             jabber_confirm_address($confirm->code,
323                                    $user->nickname,
324                                    $jabber);
325         }
326
327         $msg = sprintf(_('A confirmation code was sent '.
328                          'to the IM address you added. '.
329                          'You must approve %s for '.
330                          'sending messages to you.'),
331                        jabber_daemon_address());
332
333         $this->showForm($msg, true);
334     }
335
336     /**
337      * Cancel a confirmation
338      *
339      * If a confirmation exists, cancel it.
340      *
341      * @return void
342      */
343
344     function cancelConfirmation()
345     {
346         $jabber = $this->arg('jabber');
347
348         $confirm = $this->getConfirmation();
349
350         if (!$confirm) {
351             $this->showForm(_('No pending confirmation to cancel.'));
352             return;
353         }
354         if ($confirm->address != $jabber) {
355             $this->showForm(_('That is the wrong IM address.'));
356             return;
357         }
358
359         $result = $confirm->delete();
360
361         if (!$result) {
362             common_log_db_error($confirm, 'DELETE', __FILE__);
363             $this->serverError(_('Couldn\'t delete email confirmation.'));
364             return;
365         }
366
367         $this->showForm(_('Confirmation cancelled.'), true);
368     }
369
370     /**
371      * Remove an address
372      *
373      * If the user has a confirmed address, remove it.
374      *
375      * @return void
376      */
377
378     function removeAddress()
379     {
380         $user = common_current_user();
381
382         $jabber = $this->arg('jabber');
383
384         // Maybe an old tab open...?
385
386         if ($user->jabber != $jabber) {
387             $this->showForm(_('That is not your Jabber ID.'));
388             return;
389         }
390
391         $user->query('BEGIN');
392
393         $original = clone($user);
394
395         $user->jabber = null;
396
397         $result = $user->updateKeys($original);
398
399         if (!$result) {
400             common_log_db_error($user, 'UPDATE', __FILE__);
401             $this->serverError(_('Couldn\'t update user.'));
402             return;
403         }
404         $user->query('COMMIT');
405
406         // XXX: unsubscribe to the old address
407
408         $this->showForm(_('The address was removed.'), true);
409     }
410
411     /**
412      * Does this Jabber ID exist?
413      *
414      * Checks if we already have another user with this address.
415      *
416      * @param string $jabber Address to check
417      *
418      * @return boolean whether the Jabber ID exists
419      */
420
421     function jabberExists($jabber)
422     {
423         $user = common_current_user();
424
425         $other = User::staticGet('jabber', $jabber);
426
427         if (!$other) {
428             return false;
429         } else {
430             return $other->id != $user->id;
431         }
432     }
433 }