]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
Merge branch 'master' of /var/www/trunk into uiredesign
[quix0rs-gnu-social.git] / actions / imsettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/jabber.php');
24
25 class ImsettingsAction extends SettingsAction
26 {
27
28     function get_instructions()
29     {
30         return _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
31     }
32
33     function show_form($msg=null, $success=false)
34     {
35         $user = common_current_user();
36         $this->form_header(_('IM Settings'), $msg, $success);
37         $this->elementStart('form', array('method' => 'post',
38                                            'id' => 'imsettings',
39                                            'action' =>
40                                            common_local_url('imsettings')));
41         $this->hidden('token', common_session_token());
42
43         $this->element('h2', null, _('Address'));
44
45         if ($user->jabber) {
46             $this->elementStart('p');
47             $this->element('span', 'address confirmed', $user->jabber);
48             $this->element('span', 'input_instructions',
49                            _('Current confirmed Jabber/GTalk address.'));
50             $this->hidden('jabber', $user->jabber);
51             $this->elementEnd('p');
52             $this->submit('remove', _('Remove'));
53         } else {
54             $confirm = $this->get_confirmation();
55             if ($confirm) {
56                 $this->elementStart('p');
57                 $this->element('span', 'address unconfirmed', $confirm->address);
58                 $this->element('span', 'input_instructions',
59                                 sprintf(_('Awaiting confirmation on this address. Check your Jabber/GTalk account for a message with further instructions. (Did you add %s to your buddy list?)'), jabber_daemon_address()));
60                 $this->hidden('jabber', $confirm->address);
61                 $this->elementEnd('p');
62                 $this->submit('cancel', _('Cancel'));
63             } else {
64                 $this->input('jabber', _('IM Address'),
65                              ($this->arg('jabber')) ? $this->arg('jabber') : null,
66                          sprintf(_('Jabber or GTalk address, like "UserName@example.org". First, make sure to add %s to your buddy list in your IM client or on GTalk.'), jabber_daemon_address()));
67                 $this->submit('add', _('Add'));
68             }
69         }
70
71         $this->element('h2', null, _('Preferences'));
72
73         $this->checkbox('jabbernotify',
74                         _('Send me notices through Jabber/GTalk.'),
75                         $user->jabbernotify);
76         $this->checkbox('updatefrompresence',
77                         _('Post a notice when my Jabber/GTalk status changes.'),
78                         $user->updatefrompresence);
79         $this->checkbox('jabberreplies',
80                         _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
81                         $user->jabberreplies);
82         $this->checkbox('jabbermicroid',
83                         _('Publish a MicroID for my Jabber/GTalk address.'),
84                         $user->jabbermicroid);
85         $this->submit('save', _('Save'));
86
87         $this->elementEnd('form');
88         common_show_footer();
89     }
90
91     function get_confirmation()
92     {
93         $user = common_current_user();
94         $confirm = new Confirm_address();
95         $confirm->user_id = $user->id;
96         $confirm->address_type = 'jabber';
97         if ($confirm->find(true)) {
98             return $confirm;
99         } else {
100             return null;
101         }
102     }
103
104     function handle_post()
105     {
106
107         # CSRF protection
108         $token = $this->trimmed('token');
109         if (!$token || $token != common_session_token()) {
110             $this->show_form(_('There was a problem with your session token. Try again, please.'));
111             return;
112         }
113
114         if ($this->arg('save')) {
115             $this->save_preferences();
116         } else if ($this->arg('add')) {
117             $this->add_address();
118         } else if ($this->arg('cancel')) {
119             $this->cancel_confirmation();
120         } else if ($this->arg('remove')) {
121             $this->remove_address();
122         } else {
123             $this->show_form(_('Unexpected form submission.'));
124         }
125     }
126
127     function save_preferences()
128     {
129
130         $jabbernotify = $this->boolean('jabbernotify');
131         $updatefrompresence = $this->boolean('updatefrompresence');
132         $jabberreplies = $this->boolean('jabberreplies');
133         $jabbermicroid = $this->boolean('jabbermicroid');
134
135         $user = common_current_user();
136
137         assert(!is_null($user)); # should already be checked
138
139         $user->query('BEGIN');
140
141         $original = clone($user);
142
143         $user->jabbernotify = $jabbernotify;
144         $user->updatefrompresence = $updatefrompresence;
145         $user->jabberreplies = $jabberreplies;
146         $user->jabbermicroid = $jabbermicroid;
147
148         $result = $user->update($original);
149
150         if ($result === false) {
151             common_log_db_error($user, 'UPDATE', __FILE__);
152             $this->serverError(_('Couldn\'t update user.'));
153             return;
154         }
155
156         $user->query('COMMIT');
157
158         $this->show_form(_('Preferences saved.'), true);
159     }
160
161     function add_address()
162     {
163
164         $user = common_current_user();
165
166         $jabber = $this->trimmed('jabber');
167
168         # Some validation
169
170         if (!$jabber) {
171             $this->show_form(_('No Jabber ID.'));
172             return;
173         }
174
175         $jabber = jabber_normalize_jid($jabber);
176
177         if (!$jabber) {
178             $this->show_form(_('Cannot normalize that Jabber ID'));
179             return;
180         }
181         if (!jabber_valid_base_jid($jabber)) {
182             $this->show_form(_('Not a valid Jabber ID'));
183             return;
184         } else if ($user->jabber == $jabber) {
185             $this->show_form(_('That is already your Jabber ID.'));
186             return;
187         } else if ($this->jabber_exists($jabber)) {
188             $this->show_form(_('Jabber ID already belongs to another user.'));
189             return;
190         }
191
192           $confirm = new Confirm_address();
193            $confirm->address = $jabber;
194            $confirm->address_type = 'jabber';
195            $confirm->user_id = $user->id;
196            $confirm->code = common_confirmation_code(64);
197
198         $result = $confirm->insert();
199
200         if ($result === false) {
201             common_log_db_error($confirm, 'INSERT', __FILE__);
202             $this->serverError(_('Couldn\'t insert confirmation code.'));
203             return;
204         }
205
206         if (!common_config('queue', 'enabled')) {
207             jabber_confirm_address($confirm->code,
208                                    $user->nickname,
209                                    $jabber);
210         }
211
212         $msg = sprintf(_('A confirmation code was sent to the IM address you added. You must approve %s for sending messages to you.'), jabber_daemon_address());
213
214         $this->show_form($msg, true);
215     }
216
217     function cancel_confirmation()
218     {
219         $jabber = $this->arg('jabber');
220         $confirm = $this->get_confirmation();
221         if (!$confirm) {
222             $this->show_form(_('No pending confirmation to cancel.'));
223             return;
224         }
225         if ($confirm->address != $jabber) {
226             $this->show_form(_('That is the wrong IM address.'));
227             return;
228         }
229
230         $result = $confirm->delete();
231
232         if (!$result) {
233             common_log_db_error($confirm, 'DELETE', __FILE__);
234             $this->serverError(_('Couldn\'t delete email confirmation.'));
235             return;
236         }
237
238         $this->show_form(_('Confirmation cancelled.'), true);
239     }
240
241     function remove_address()
242     {
243
244         $user = common_current_user();
245         $jabber = $this->arg('jabber');
246
247         # Maybe an old tab open...?
248
249         if ($user->jabber != $jabber) {
250             $this->show_form(_('That is not your Jabber ID.'));
251             return;
252         }
253
254         $user->query('BEGIN');
255         $original = clone($user);
256         $user->jabber = null;
257         $result = $user->updateKeys($original);
258         if (!$result) {
259             common_log_db_error($user, 'UPDATE', __FILE__);
260             $this->serverError(_('Couldn\'t update user.'));
261             return;
262         }
263         $user->query('COMMIT');
264
265         # XXX: unsubscribe to the old address
266
267         $this->show_form(_('The address was removed.'), true);
268     }
269
270     function jabber_exists($jabber)
271     {
272         $user = common_current_user();
273         $other = User::staticGet('jabber', $jabber);
274         if (!$other) {
275             return false;
276         } else {
277             return $other->id != $user->id;
278         }
279     }
280 }