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