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