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