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