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