]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
replace NULL with null
[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     function get_instructions() {
28         return _('You can send and receive notices through Jabber/GTalk [instant messages](%%doc.im%%). Configure your address and settings below.');
29     }
30
31     function show_form($msg=null, $success=false) {
32         $user = common_current_user();
33         $this->form_header(_('IM Settings'), $msg, $success);
34         common_element_start('form', array('method' => 'post',
35                                            'id' => 'imsettings',
36                                            'action' =>
37                                            common_local_url('imsettings')));
38         common_hidden('token', common_session_token());
39
40         common_element('h2', null, _('Address'));
41
42         if ($user->jabber) {
43             common_element_start('p');
44             common_element('span', 'address confirmed', $user->jabber);
45             common_element('span', 'input_instructions',
46                            _('Current confirmed Jabber/GTalk address.'));
47             common_hidden('jabber', $user->jabber);
48             common_element_end('p');
49             common_submit('remove', _('Remove'));
50         } else {
51             $confirm = $this->get_confirmation();
52             if ($confirm) {
53                 common_element_start('p');
54                 common_element('span', 'address unconfirmed', $confirm->address);
55                 common_element('span', 'input_instructions',
56                                 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()));
57                 common_hidden('jabber', $confirm->address);
58                 common_element_end('p');
59                 common_submit('cancel', _('Cancel'));
60             } else {
61                 common_input('jabber', _('IM Address'),
62                              ($this->arg('jabber')) ? $this->arg('jabber') : null,
63                          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()));
64                 common_submit('add', _('Add'));
65             }
66         }
67
68         common_element('h2', null, _('Preferences'));
69
70         common_checkbox('jabbernotify',
71                         _('Send me notices through Jabber/GTalk.'),
72                         $user->jabbernotify);
73         common_checkbox('updatefrompresence',
74                         _('Post a notice when my Jabber/GTalk status changes.'),
75                         $user->updatefrompresence);
76         common_checkbox('jabberreplies',
77                         _('Send me replies through Jabber/GTalk from people I\'m not subscribed to.'),
78                         $user->jabberreplies);
79         common_checkbox('jabbermicroid',
80                         _('Publish a MicroID for my Jabber/GTalk address.'),
81                         $user->jabbermicroid);
82         common_submit('save', _('Save'));
83
84         common_element_end('form');
85         common_show_footer();
86     }
87
88     function get_confirmation() {
89         $user = common_current_user();
90         $confirm = new Confirm_address();
91         $confirm->user_id = $user->id;
92         $confirm->address_type = 'jabber';
93         if ($confirm->find(TRUE)) {
94             return $confirm;
95         } else {
96             return null;
97         }
98     }
99
100     function handle_post() {
101
102         # CSRF protection
103         $token = $this->trimmed('token');
104         if (!$token || $token != common_session_token()) {
105             $this->show_form(_('There was a problem with your session token. Try again, please.'));
106             return;
107         }
108
109         if ($this->arg('save')) {
110             $this->save_preferences();
111         } else if ($this->arg('add')) {
112             $this->add_address();
113         } else if ($this->arg('cancel')) {
114             $this->cancel_confirmation();
115         } else if ($this->arg('remove')) {
116             $this->remove_address();
117         } else {
118             $this->show_form(_('Unexpected form submission.'));
119         }
120     }
121
122     function save_preferences() {
123
124         $jabbernotify = $this->boolean('jabbernotify');
125         $updatefrompresence = $this->boolean('updatefrompresence');
126         $jabberreplies = $this->boolean('jabberreplies');
127         $jabbermicroid = $this->boolean('jabbermicroid');
128
129         $user = common_current_user();
130
131         assert(!is_null($user)); # should already be checked
132
133         $user->query('BEGIN');
134
135         $original = clone($user);
136
137         $user->jabbernotify = $jabbernotify;
138         $user->updatefrompresence = $updatefrompresence;
139         $user->jabberreplies = $jabberreplies;
140         $user->jabbermicroid = $jabbermicroid;
141
142         $result = $user->update($original);
143
144         if ($result === FALSE) {
145             common_log_db_error($user, 'UPDATE', __FILE__);
146             common_server_error(_('Couldn\'t update user.'));
147             return;
148         }
149
150         $user->query('COMMIT');
151
152         $this->show_form(_('Preferences saved.'), true);
153     }
154
155     function add_address() {
156
157         $user = common_current_user();
158
159         $jabber = $this->trimmed('jabber');
160
161         # Some validation
162
163         if (!$jabber) {
164             $this->show_form(_('No Jabber ID.'));
165             return;
166         }
167
168         $jabber = jabber_normalize_jid($jabber);
169
170         if (!$jabber) {
171             $this->show_form(_('Cannot normalize that Jabber ID'));
172             return;
173         }
174         if (!jabber_valid_base_jid($jabber)) {
175             $this->show_form(_('Not a valid Jabber ID'));
176             return;
177         } else if ($user->jabber == $jabber) {
178             $this->show_form(_('That is already your Jabber ID.'));
179             return;
180         } else if ($this->jabber_exists($jabber)) {
181             $this->show_form(_('Jabber ID already belongs to another user.'));
182             return;
183         }
184
185           $confirm = new Confirm_address();
186            $confirm->address = $jabber;
187            $confirm->address_type = 'jabber';
188            $confirm->user_id = $user->id;
189            $confirm->code = common_confirmation_code(64);
190
191         $result = $confirm->insert();
192
193         if ($result === FALSE) {
194             common_log_db_error($confirm, 'INSERT', __FILE__);
195             common_server_error(_('Couldn\'t insert confirmation code.'));
196             return;
197         }
198
199         if (!common_config('queue', 'enabled')) {
200             jabber_confirm_address($confirm->code,
201                                    $user->nickname,
202                                    $jabber);
203         }
204
205         $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());
206
207         $this->show_form($msg, TRUE);
208     }
209
210     function cancel_confirmation() {
211         $jabber = $this->arg('jabber');
212         $confirm = $this->get_confirmation();
213         if (!$confirm) {
214             $this->show_form(_('No pending confirmation to cancel.'));
215             return;
216         }
217         if ($confirm->address != $jabber) {
218             $this->show_form(_('That is the wrong IM address.'));
219             return;
220         }
221
222         $result = $confirm->delete();
223
224         if (!$result) {
225             common_log_db_error($confirm, 'DELETE', __FILE__);
226             $this->server_error(_('Couldn\'t delete email confirmation.'));
227             return;
228         }
229
230         $this->show_form(_('Confirmation cancelled.'), TRUE);
231     }
232
233     function remove_address() {
234
235         $user = common_current_user();
236         $jabber = $this->arg('jabber');
237
238         # Maybe an old tab open...?
239
240         if ($user->jabber != $jabber) {
241             $this->show_form(_('That is not your Jabber ID.'));
242             return;
243         }
244
245         $user->query('BEGIN');
246         $original = clone($user);
247         $user->jabber = null;
248         $result = $user->updateKeys($original);
249         if (!$result) {
250             common_log_db_error($user, 'UPDATE', __FILE__);
251             common_server_error(_('Couldn\'t update user.'));
252             return;
253         }
254         $user->query('COMMIT');
255
256         # XXX: unsubscribe to the old address
257
258         $this->show_form(_('The address was removed.'), TRUE);
259     }
260
261     function jabber_exists($jabber) {
262         $user = common_current_user();
263         $other = User::staticGet('jabber', $jabber);
264         if (!$other) {
265             return false;
266         } else {
267             return $other->id != $user->id;
268         }
269     }
270 }