]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
DocAction now extends ManagedAction
[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('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * Settings for Jabber/XMPP integration
34  *
35  * @category Settings
36  * @package  StatusNet
37  * @author   Evan Prodromou <evan@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
39  * @link     http://status.net/
40  *
41  * @see      SettingsAction
42  */
43
44 class ImsettingsAction extends SettingsAction
45 {
46     /**
47      * Title of the page
48      *
49      * @return string Title of the page
50      */
51     function title()
52     {
53         // TRANS: Title for Instant Messaging settings.
54         return _('IM settings');
55     }
56
57     /**
58      * Instructions for use
59      *
60      * @return instructions for use
61      */
62     function getInstructions()
63     {
64         // TRANS: Instant messaging settings page instructions.
65         // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
66         // TRANS: the order and formatting of link text and link should remain unchanged.
67         return _('You can send and receive notices through '.
68                  '[instant messaging](%%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     function showContent()
82     {
83         $transports = array();
84         Event::handle('GetImTransports', array(&$transports));
85         if (! $transports) {
86             $this->element('div', array('class' => 'error'),
87                            // TRANS: Message given in the IM settings if IM is not enabled on the site.
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             // TRANS: Form legend for IM settings form.
105             $this->element('legend', null, $transport_info['display']);
106             $this->hidden('token', common_session_token());
107             $this->hidden('transport', $transport);
108
109             if ($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $transport, 'user_id' => $user->id) )) {
110                 $user_im_prefs_by_transport[$transport] = $user_im_prefs;
111                 $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
112                 $this->element('p', 'form_note',
113                                // TRANS: Form note in IM settings form. %s is the type of IM address that was confirmed.
114                                sprintf(_('Current confirmed %s address.'),$transport_info['display']));
115                 $this->hidden('screenname', $user_im_prefs->screenname);
116                 // TRANS: Button label to remove a confirmed IM address.
117                 $this->submit('remove', _m('BUTTON','Remove'));
118             } else {
119                 try {
120                     $confirm = $this->getConfirmation($transport);
121                     $this->element('p', 'form_unconfirmed', $confirm->address);
122                     // TRANS: Form note in IM settings form.
123                     $this->element('p', 'form_note',
124                                    // TRANS: Form note in IM settings form.
125                                    // TRANS: %s is the IM service name, %2$s is the IM address set.
126                                    sprintf(_('Awaiting confirmation on this address. '.
127                                              'Check your %1$s account for a '.
128                                              'message with further instructions. '.
129                                              '(Did you add %2$s to your buddy list?)'),
130                                              $transport_info['display'],
131                                              $transport_info['daemonScreenname']));
132                     $this->hidden('screenname', $confirm->address);
133                     // TRANS: Button label to cancel an IM address confirmation procedure.
134                     $this->submit('cancel', _m('BUTTON','Cancel'));
135                 } catch (NoResultException $e) {
136                     $this->elementStart('ul', 'form_data');
137                     $this->elementStart('li');
138                     // TRANS: Field label for IM address.
139                     $this->input('screenname', _('IM address'),
140                                  ($this->arg('screenname')) ? $this->arg('screenname') : null,
141                                  // TRANS: Field title for IM address. %s is the IM service name.
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             );
181             foreach($preferences as $preference)
182             {
183                 $this->elementStart('tr');
184                 foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
185                 {
186                     $preference_name = $preference['name'];
187                     $this->elementStart('td');
188                     $this->checkbox($transport . '_' . $preference['name'],
189                                 $preference['description'],
190                                 $user_im_prefs->$preference_name);
191                     $this->elementEnd('td');
192                 }
193                 $this->elementEnd('tr');
194             }
195             $this->elementEnd('table');
196             // TRANS: Button label to save IM preferences.
197             $this->submit('save', _m('BUTTON','Save'));
198             $this->elementEnd('fieldset');
199             $this->elementEnd('form');
200         }
201     }
202
203     /**
204      * Get a confirmation code for this user
205      *
206      * @return Confirm_address address object for this user
207      */
208     function getConfirmation($transport)
209     {
210         $confirm = new Confirm_address();
211
212         $confirm->user_id      = $this->scoped->getID();
213         $confirm->address_type = $transport;
214
215         if ($confirm->find(true)) {
216             return $confirm;
217         }
218
219         throw new NoResultException($confirm);
220     }
221
222     protected function doPost()
223     {
224         if ($this->arg('save')) {
225             return $this->savePreferences();
226         } else if ($this->arg('add')) {
227             return $this->addAddress();
228         } else if ($this->arg('cancel')) {
229             return $this->cancelConfirmation();
230         } else if ($this->arg('remove')) {
231             return $this->removeAddress();
232         }
233         // TRANS: Message given submitting a form with an unknown action in Instant Messaging settings.
234         throw new ClientException(_('Unexpected form submission.'));
235     }
236
237     /**
238      * Save user's XMPP preferences
239      *
240      * These are the checkboxes at the bottom of the page. They're used to
241      * set different settings
242      *
243      * @return void
244      */
245     function savePreferences()
246     {
247         $user_im_prefs = new User_im_prefs();
248         $user_im_prefs->query('BEGIN');
249         $user_im_prefs->user_id = $this->scoped->getID();
250         if($user_im_prefs->find() && $user_im_prefs->fetch())
251         {
252             $preferences = array('notify', 'updatefrompresence', 'replies');
253             do
254             {
255                 $original = clone($user_im_prefs);
256                 $new = clone($user_im_prefs);
257                 foreach($preferences as $preference)
258                 {
259                     $new->$preference = $this->boolean($new->transport . '_' . $preference);
260                 }
261                 $result = $new->update($original);
262
263                 if ($result === false) {
264                     common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
265                     // TRANS: Server error thrown on database error updating IM preferences.
266                     throw new ServerException(_('Could not update IM preferences.'));
267                 }
268             }while($user_im_prefs->fetch());
269         }
270         $user_im_prefs->query('COMMIT');
271         // TRANS: Confirmation message for successful IM preferences save.
272         return _('Preferences saved.');
273     }
274
275     /**
276      * Sends a confirmation to the address given
277      *
278      * Stores a confirmation record and sends out a
279      * message with the confirmation info.
280      *
281      * @return void
282      */
283     function addAddress()
284     {
285         $screenname = $this->trimmed('screenname');
286         $transport = $this->trimmed('transport');
287
288         // Some validation
289
290         if (empty($screenname)) {
291             // TRANS: Message given saving IM address without having provided one.
292             throw new ClientException(_('No screenname.'));
293         }
294
295         if (empty($transport)) {
296             // TRANS: Form validation error when no transport is available setting an IM address.
297             throw new ClientException(_('No transport.'));
298         }
299
300         Event::handle('NormalizeImScreenname', array($transport, &$screenname));
301
302         if (empty($screenname)) {
303             // TRANS: Message given saving IM address that cannot be normalised.
304             throw new ClientException(_('Cannot normalize that screenname.'));
305         }
306         $valid = false;
307         Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
308         if (!$valid) {
309             // TRANS: Message given saving IM address that not valid.
310             throw new ClientException(_('Not a valid screenname.'));
311         } else if ($this->screennameExists($transport, $screenname)) {
312             // TRANS: Message given saving IM address that is already set for another user.
313             throw new ClientException(_('Screenname already belongs to another user.'));
314         }
315
316         $confirm = new Confirm_address();
317
318         $confirm->address      = $screenname;
319         $confirm->address_type = $transport;
320         $confirm->user_id      = $this->scoped->getID();
321         $confirm->code         = common_confirmation_code(64);
322         $confirm->sent         = common_sql_now();
323         $confirm->claimed      = common_sql_now();
324
325         $result = $confirm->insert();
326
327         if ($result === false) {
328             common_log_db_error($confirm, 'INSERT', __FILE__);
329             // TRANS: Server error thrown on database error adding Instant Messaging confirmation code.
330             $this->serverError(_('Could not insert confirmation code.'));
331         }
332
333         Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $this->scoped));
334
335         // TRANS: Message given saving valid IM address that is to be confirmed.
336         return _('A confirmation code was sent to the IM address you added.');
337     }
338
339     /**
340      * Cancel a confirmation
341      *
342      * If a confirmation exists, cancel it.
343      *
344      * @return void
345      */
346     function cancelConfirmation()
347     {
348         $screenname = $this->trimmed('screenname');
349         $transport = $this->trimmed('transport');
350
351         try {
352             $confirm = $this->getConfirmation($transport);
353             if ($confirm->address != $screenname) {
354                 // TRANS: Message given canceling IM address confirmation for the wrong IM address.
355                 throw new ClientException(_('That is the wrong IM address.'));
356             }
357         } catch (NoResultException $e) {
358             // TRANS: Message given canceling Instant Messaging address confirmation that is not pending.
359             throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
360         }
361
362         $result = $confirm->delete();
363
364         if ($result === false) {
365             common_log_db_error($confirm, 'DELETE', __FILE__);
366             // TRANS: Server error thrown on database error canceling IM address confirmation.
367             throw new ServerException(_('Could not delete confirmation.'));
368         }
369
370         // TRANS: Message given after successfully canceling IM address confirmation.
371         return _('IM confirmation cancelled.');
372     }
373
374     /**
375      * Remove an address
376      *
377      * If the user has a confirmed address, remove it.
378      *
379      * @return void
380      */
381     function removeAddress()
382     {
383         $screenname = $this->trimmed('screenname');
384         $transport = $this->trimmed('transport');
385
386         // Maybe an old tab open...?
387
388         $user_im_prefs = new User_im_prefs();
389         $user_im_prefs->user_id = $this->scoped->getID();
390         $user_im_prefs->transport = $transport;
391         if (!$user_im_prefs->find(true)) {
392             // TRANS: Message given trying to remove an IM address that is not
393             // TRANS: registered for the active user.
394             throw new AlreadyFulfilledException(_('There were no preferences stored for this transport.'));
395         }
396
397         $result = $user_im_prefs->delete();
398
399         if ($result === false) {
400             common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
401             // TRANS: Server error thrown on database error removing a registered IM address.
402             throw new ServerException(_('Could not update user IM preferences.'));
403         }
404
405         // XXX: unsubscribe to the old address
406
407         // TRANS: Message given after successfully removing a registered Instant Messaging address.
408         return _('The IM address was removed.');
409     }
410
411     /**
412      * Does this screenname exist?
413      *
414      * Checks if we already have another user with this address.
415      *
416      * @param string $transport Transport to check
417      * @param string $screenname Screenname to check
418      *
419      * @return boolean whether the screenname exists
420      */
421
422     function screennameExists($transport, $screenname)
423     {
424         $user_im_prefs = new User_im_prefs();
425         $user_im_prefs->transport = $transport;
426         $user_im_prefs->screenname = $screenname;
427         return $user_im_prefs->find(true) ? true : false;
428     }
429 }