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