]> 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 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 [instant messages](%%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                 // TRANS: Form note in IM settings form.
115                 $this->element('p', 'form_note',
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 address set for the site.
128                                    sprintf(_('Awaiting confirmation on this address. '.
129                                              'Check your %s account for a '.
130                                              'message with further instructions. '.
131                                              '(Did you add %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                     $this->input('screenname', _('IM address'),
141                                  ($this->arg('screenname')) ? $this->arg('screenname') : null,
142                                  sprintf(_('%s screenname.'),
143                                          $transport_info['display']));
144                     $this->elementEnd('li');
145                     $this->elementEnd('ul');
146                     // TRANS: Button label for adding an IM address in IM settings form.
147                     $this->submit('add', _m('BUTTON','Add'));
148                 }
149             }
150             $this->elementEnd('fieldset');
151             $this->elementEnd('form');
152         }
153
154         if($user_im_prefs_by_transport)
155         {
156             $this->elementStart('form', array('method' => 'post',
157                                               'id' => 'form_settings_im',
158                                               'class' => 'form_settings',
159                                               'action' =>
160                                               common_local_url('imsettings')));
161             $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
162             // TRANS: Header for IM preferences form.
163             $this->element('legend', null, _('IM Preferences'));
164             $this->hidden('token', common_session_token());
165             $this->elementStart('table');
166             $this->elementStart('tr');
167             foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
168             {
169                 $this->element('th', null, $transports[$transport]['display']);
170             }
171             $this->elementEnd('tr');
172             $preferences = array(
173                 // TRANS: Checkbox label in IM preferences form.
174                 array('name'=>'notify', 'description'=>_('Send me notices')),
175                 // TRANS: Checkbox label in IM preferences form.
176                 array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
177                 // TRANS: Checkbox label in IM preferences form.
178                 array('name'=>'replies', 'description'=>_('Send me replies '.
179                               'from people I\'m not subscribed to.')),
180                 // TRANS: Checkbox label in IM preferences form.
181                 array('name'=>'microid', 'description'=>_('Publish a MicroID'))
182             );
183             foreach($preferences as $preference)
184             {
185                 $this->elementStart('tr');
186                 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
187                 {
188                     $preference_name = $preference['name'];
189                     $this->elementStart('td');
190                     $this->checkbox($transport . '_' . $preference['name'],
191                                 $preference['description'],
192                                 $user_im_prefs->$preference_name);
193                     $this->elementEnd('td');
194                 }
195                 $this->elementEnd('tr');
196             }
197             $this->elementEnd('table');
198             // TRANS: Button label to save IM preferences.
199             $this->submit('save', _m('BUTTON','Save'));
200             $this->elementEnd('fieldset');
201             $this->elementEnd('form');
202         }
203     }
204
205     /**
206      * Get a confirmation code for this user
207      *
208      * @return Confirm_address address object for this user
209      */
210 <<<<<<< HEAD
211
212     function getConfirmation($transport)
213 =======
214     function getConfirmation()
215 >>>>>>> 0.9.x
216     {
217         $user = common_current_user();
218
219         $confirm = new Confirm_address();
220
221         $confirm->user_id      = $user->id;
222         $confirm->address_type = $transport;
223
224         if ($confirm->find(true)) {
225             return $confirm;
226         } else {
227             return null;
228         }
229     }
230
231     /**
232      * Handle posts to this form
233      *
234      * Based on the button that was pressed, muxes out to other functions
235      * to do the actual task requested.
236      *
237      * All sub-functions reload the form with a message -- success or failure.
238      *
239      * @return void
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 Instant Messaging 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     function savePreferences()
274     {
275         $user = common_current_user();
276
277         $user_im_prefs = new User_im_prefs();
278         $user_im_prefs->query('BEGIN');
279         $user_im_prefs->user_id = $user->id;
280         if($user_im_prefs->find() && $user_im_prefs->fetch())
281         {
282             $preferences = array('notify', 'updatefrompresence', 'replies', 'microid');
283             do
284             {
285                 $original = clone($user_im_prefs);
286                 $new = clone($user_im_prefs);
287                 foreach($preferences as $preference)
288                 {
289                     $new->$preference = $this->boolean($new->transport . '_' . $preference);
290                 }
291                 $result = $new->update($original);
292
293                 if ($result === false) {
294                     common_log_db_error($user, 'UPDATE', __FILE__);
295                     // TRANS: Server error thrown on database error updating IM preferences.
296                     $this->serverError(_('Couldn\'t update IM preferences.'));
297                     return;
298                 }
299             }while($user_im_prefs->fetch());
300         }
301         $user_im_prefs->query('COMMIT');
302         // TRANS: Confirmation message for successful IM preferences save.
303         $this->showForm(_('Preferences saved.'), true);
304     }
305
306     /**
307      * Sends a confirmation to the address given
308      *
309      * Stores a confirmation record and sends out a
310      * message with the confirmation info.
311      *
312      * @return void
313      */
314     function addAddress()
315     {
316         $user = common_current_user();
317
318         $screenname = $this->trimmed('screenname');
319         $transport = $this->trimmed('transport');
320
321         // Some validation
322
323         if (!$screenname) {
324             // TRANS: Message given saving IM address without having provided one.
325             $this->showForm(_('No screenname.'));
326             return;
327         }
328
329         if (!$transport) {
330             $this->showForm(_('No transport.'));
331             return;
332         }
333
334         Event::handle('NormalizeImScreenname', array($transport, &$screenname));
335
336         if (!$screenname) {
337             // TRANS: Message given saving IM address that cannot be normalised.
338             $this->showForm(_('Cannot normalize that screenname'));
339             return;
340         }
341         $valid = false;
342         Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
343         if (!$valid) {
344             // TRANS: Message given saving IM address that not valid.
345             $this->showForm(_('Not a valid screenname'));
346             return;
347         } else if ($this->screennameExists($transport, $screenname)) {
348             // TRANS: Message given saving IM address that is already set for another user.
349             $this->showForm(_('Screenname already belongs to another user.'));
350             return;
351         }
352
353         $confirm = new Confirm_address();
354
355         $confirm->address      = $screenname;
356         $confirm->address_type = $transport;
357         $confirm->user_id      = $user->id;
358         $confirm->code         = common_confirmation_code(64);
359         $confirm->sent         = common_sql_now();
360         $confirm->claimed      = common_sql_now();
361
362         $result = $confirm->insert();
363
364         if ($result === false) {
365             common_log_db_error($confirm, 'INSERT', __FILE__);
366             // TRANS: Server error thrown on database error adding Instant Messaging confirmation code.
367             $this->serverError(_('Could not insert confirmation code.'));
368             return;
369         }
370
371         Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $user));
372
373         // TRANS: Message given saving valid IM address that is to be confirmed.
374         $msg = _('A confirmation code was sent '.
375                          'to the IM address you added.');
376
377         $this->showForm($msg, true);
378     }
379
380     /**
381      * Cancel a confirmation
382      *
383      * If a confirmation exists, cancel it.
384      *
385      * @return void
386      */
387     function cancelConfirmation()
388     {
389         $screenname = $this->trimmed('screenname');
390         $transport = $this->trimmed('transport');
391
392         $confirm = $this->getConfirmation($transport);
393
394         if (!$confirm) {
395             // TRANS: Message given canceling Instant Messaging address confirmation that is not pending.
396             $this->showForm(_('No pending confirmation to cancel.'));
397             return;
398         }
399         if ($confirm->address != $screenname) {
400             // TRANS: Message given canceling IM address confirmation for the wrong IM address.
401             $this->showForm(_('That is the wrong IM address.'));
402             return;
403         }
404
405         $result = $confirm->delete();
406
407         if (!$result) {
408             common_log_db_error($confirm, 'DELETE', __FILE__);
409             // TRANS: Server error thrown on database error canceling IM address confirmation.
410             $this->serverError(_('Couldn\'t delete confirmation.'));
411             return;
412         }
413
414         // TRANS: Message given after successfully canceling IM address confirmation.
415         $this->showForm(_('IM confirmation cancelled.'), true);
416     }
417
418     /**
419      * Remove an address
420      *
421      * If the user has a confirmed address, remove it.
422      *
423      * @return void
424      */
425     function removeAddress()
426     {
427         $user = common_current_user();
428
429         $screenname = $this->trimmed('screenname');
430         $transport = $this->trimmed('transport');
431
432         // Maybe an old tab open...?
433
434         $user_im_prefs = new User_im_prefs();
435         $user_im_prefs->user_id = $user->id;
436         if(! ($user_im_prefs->find() && $user_im_prefs->fetch())) {
437             // TRANS: Message given trying to remove an IM address that is not
438             // TRANS: registered for the active user.
439             $this->showForm(_('That is not your screenname.'));
440             return;
441         }
442
443         $result = $user_im_prefs->delete();
444
445         if (!$result) {
446             common_log_db_error($user, 'UPDATE', __FILE__);
447             // TRANS: Server error thrown on database error removing a registered IM address.
448             $this->serverError(_('Couldn\'t update user im prefs.'));
449             $this->serverError(_('Couldn\'t update user.'));
450             return;
451         }
452
453         // XXX: unsubscribe to the old address
454
455         // TRANS: Message given after successfully removing a registered Instant Messaging address.
456         $this->showForm(_('The IM address was removed.'), true);
457     }
458
459     /**
460      * Does this screenname exist?
461      *
462      * Checks if we already have another user with this address.
463      *
464      * @param string $transport Transport to check
465      * @param string $screenname Screenname to check
466      *
467      * @return boolean whether the screenname exists
468      */
469
470     function screennameExists($transport, $screenname)
471     {
472         $user = common_current_user();
473
474         $user_im_prefs = new User_im_prefs();
475         $user_im_prefs->transport = $transport;
476         $user_im_prefs->screenname = $screenname;
477         if($user_im_prefs->find() && $user_im_prefs->fetch()){
478             return true;
479         }else{
480             return false;
481         }
482     }
483 }