]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/imsettings.php
d8680c98b4dc4daa98a797dd104142f982cb0147
[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
24 class ImsettingsAction extends SettingsAction {
25
26         function show_top($arr) {
27                 $msg = $arr[0];
28                 $success = $arr[1];
29                 if ($msg) {
30                         $this->message($msg, $success);
31                 } else {
32                         common_element('div', 'instructions',
33                                                    _t('You can send and receive notices through '.
34                                                           'Jabber/GTalk instant messages. Configure '.
35                                                           'your address and settings below.'));
36                 }
37                 $this->settings_menu();
38         }
39
40         function show_form($msg=NULL, $success=false) {
41                 $user = common_current_user();
42                 common_show_header(_t('IM settings'), NULL, array($msg, $success),
43                                                    array($this, 'show_top'));
44
45                 common_element_start('form', array('method' => 'POST',
46                                                                                    'id' => 'imsettings',
47                                                                                    'action' =>
48                                                                                    common_local_url('imsettings')));
49                 # too much common patterns here... abstractable?
50                 common_input('jabber', _t('IM Address'),
51                                          ($this->arg('jabber')) ? $this->arg('jabber') : $user->jabber,
52                                          _t('Jabber or GTalk address, like "UserName@example.org"'));
53                 common_checkbox('jabbernotify',
54                                 _t('Send me notices through Jabber/GTalk.'));
55                 common_checkbox('updatefrompresence',
56                                 _t('Post a notice when my Jabber/GTalk status changes.'));
57                 common_submit('submit', _t('Save'));
58                 common_element_end('form');
59                 common_show_footer();
60         }
61
62         function handle_post() {
63
64                 $jabber = jabber_normalize_jid($this->trimmed('jabber'));
65                 $jabbernotify = $this->boolean('jabbernotify');
66                 $updatefrompresence = $this->boolean('updatefrompresence');
67
68                 if (!jabber_valid_base_jid($jabber)) {
69                         $this->show_form(_('Not a valid Jabber ID'));
70                         return;
71                 } else if ($this->jabber_exists($jabber)) {
72                         $this->show_form(_('Not a valid Jabber ID'));
73                         return;
74                 }
75
76                 # Some validation
77
78                 $user = common_current_user();
79
80                 assert(!is_null($user)); # should already be checked
81
82                 $user->query('BEGIN');
83
84                 $original = clone($user);
85
86                 $user->jabber = $jabber;
87                 $user->jabbernotify = $jabbernotify;
88                 $user->updatefrompresence = $updatefrompresence;
89
90                 $result = $user->updateKeys($original); # For key columns
91
92                 if ($result === FALSE) {
93                         common_log_db_error($user, 'UPDATE', __FILE__);
94                         common_server_error(_t('Couldnt update user.'));
95                         return;
96                 }
97
98                 $result = $user->update($original); # For non-key columns
99
100                 if ($result === FALSE) {
101                         common_log_db_error($user, 'UPDATE', __FILE__);
102                         common_server_error(_t('Couldnt update user.'));
103                         return;
104                 }
105
106                 $user->query('COMMIT');
107
108                 $this->show_form(_t('Settings saved.'), TRUE);
109         }
110
111         function jabber_exists($jabber) {
112                 $user = common_current_user();
113                 $other = User::staticGet('jabber', $jabber);
114                 if (!$other) {
115                         return false;
116                 } else {
117                         return $other->id != $user->id;
118                 }
119         }
120 }