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