XMPP: User configurations
[friendica-addons.git] / xmpp / xmpp.php
1 <?php
2 /**
3  * Name: XMPP (Jabber)
4  * Description: Embedded XMPP (Jabber) client
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8
9 function xmpp_install() {
10         register_hook('plugin_settings', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings');
11         register_hook('plugin_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings_post');
12         register_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
13         register_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
14 }
15
16 function xmpp_uninstall() {
17         unregister_hook('plugin_settings', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings');
18         unregister_hook('plugin_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings_post');
19         unregister_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
20         unregister_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
21 }
22
23 function xmpp_plugin_settings_post($a,$post) {
24         if(! local_user() || (! x($_POST,'xmpp-settings-submit')))
25                 return;
26         set_pconfig(local_user(),'xmpp','enabled',intval($_POST['xmpp_enabled']));
27         set_pconfig(local_user(),'xmpp','individual',intval($_POST['xmpp_individual']));
28         set_pconfig(local_user(),'xmpp','bosh_proxy',$_POST['xmpp_bosh_proxy']);
29
30         info( t('XMPP settings updated.') . EOL);
31 }
32
33 function xmpp_plugin_settings(&$a,&$s) {
34
35         if(! local_user())
36                 return;
37
38         /* Add our stylesheet to the xmpp so we can make our settings look nice */
39
40         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
41
42         /* Get the current state of our config variable */
43
44         $enabled = intval(get_pconfig(local_user(),'xmpp','enabled'));
45         $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
46
47         $individual = intval(get_pconfig(local_user(),'xmpp','individual'));
48         $individual_checked = (($individual) ? ' checked="checked" ' : '');
49
50         $bosh_proxy = get_pconfig(local_user(),"xmpp","bosh_proxy");
51
52         /* Add some HTML to the existing form */
53         $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
54         $s .= '<h3>' . t('XMPP-Chat (Jabber)') . '</h3>';
55         $s .= '</span>';
56         $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
57         $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
58         $s .= '<h3>' . t('XMPP-Chat (Jabber)') . '</h3>';
59         $s .= '</span>';
60
61         $s .= '<div id="xmpp-settings-wrapper">';
62         $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . t('Enable Webchat') . '</label>';
63         $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
64         $s .= '<div class="clear"></div>';
65
66         if (get_config("xmpp", "central_userbase")) {
67                 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . t('Individual Credentials') . '</label>';
68                 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
69                 $s .= '<div class="clear"></div>';
70         }
71
72         if (!get_config("xmpp", "central_userbase") OR get_pconfig(local_user(),"xmpp","individual")) {
73                 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">'.t('Jabber BOSH host').'</label>';
74                 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="'.$bosh_proxy.'" />';
75                 $s .= '<div class="clear"></div>';
76         }
77
78         $s .= '</div>';
79
80         /* provide a submit button */
81
82         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
83
84 }
85
86 function xmpp_login($a,$b) {
87         if (!$_SESSION["allow_api"]) {
88                 $password = substr(random_string(),0,16);
89                 set_pconfig(local_user(), "xmpp", "password", $password);
90         }
91 }
92
93 function xmpp_plugin_admin(&$a, &$o){
94         $t = get_markup_template("admin.tpl", "addon/xmpp/");
95
96         $o = replace_macros($t, array(
97                 '$submit' => t('Save Settings'),
98                 '$bosh_proxy'       => array('bosh_proxy', t('Jabber BOSH host'),            get_config('xmpp', 'bosh_proxy'), ''),
99                 '$central_userbase' => array('central_userbase', t('Use central userbase'), get_config('xmpp', 'central_userbase'), t('If enabled, users will automatically login to an ejabberd server that has to be installed on this machine with synchronized credentials via the "auth_ejabberd.php" script.')),
100         ));
101 }
102
103 function xmpp_plugin_admin_post(&$a){
104         $bosh_proxy       = ((x($_POST,'bosh_proxy')) ?       trim($_POST['bosh_proxy']) : '');
105         $central_userbase = ((x($_POST,'central_userbase')) ? intval($_POST['central_userbase']) : false);
106         set_config('xmpp','bosh_proxy',$bosh_proxy);
107         set_config('xmpp','central_userbase',$central_userbase);
108         info( t('Settings updated.'). EOL );
109 }
110
111 function xmpp_script(&$a,&$s) {
112         xmpp_converse($a,$s);
113 }
114
115 function xmpp_converse(&$a,&$s) {
116         if (!local_user())
117                 return;
118
119         if ($_GET["mode"] == "minimal")
120                 return;
121
122         if ($a->is_mobile || $a->is_tablet)
123                 return;
124
125         if (!get_pconfig(local_user(),"xmpp","enabled"))
126                 return;
127
128         $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />'."\n";
129         $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>'."\n";
130
131         if (get_config("xmpp", "central_userbase") AND !get_pconfig(local_user(),"xmpp","individual")) {
132                 $bosh_proxy = get_config("xmpp", "bosh_proxy");
133
134                 $password = get_pconfig(local_user(), "xmpp", "password");
135
136                 if ($password == "") {
137                         $password = substr(random_string(),0,16);
138                         set_pconfig(local_user(), "xmpp", "password", $password);
139                 }
140
141                 $jid = $a->user["nickname"]."@".$a->get_hostname()."/converse-".substr(random_string(),0,5);;
142
143                 $auto_login = "auto_login: true,
144                         authentication: 'login',
145                         jid: '$jid',
146                         password: '$password',
147                         allow_logout: false,";
148         } else {
149                 $bosh_proxy = get_pconfig(local_user(), "xmpp", "bosh_proxy");
150
151                 $auto_login = "";
152         }
153
154         if ($bosh_proxy == "")
155                 return;
156
157         if (in_array($a->argv[0], array("manage", "logout")))
158                 $additional_commands = "converse.user.logout();\n";
159         else
160                 $additional_commands = "";
161
162         $on_ready = "";
163
164         $initialize = "converse.initialize({
165                                         bosh_service_url: '$bosh_proxy',
166                                         keepalive: true,
167                                         message_carbons: false,
168                                         forward_messages: false,
169                                         play_sounds: true,
170                                         sounds_path: 'addon/xmpp/converse/sounds/',
171                                         roster_groups: false,
172                                         show_controlbox_by_default: false,
173                                         show_toolbar: true,
174                                         allow_contact_removal: false,
175                                         allow_registration: false,
176                                         hide_offline_users: true,
177                                         allow_chat_pending_contacts: false,
178                                         allow_dragresize: true,
179                                         auto_away: 0,
180                                         auto_xa: 0,
181                                         csi_waiting_time: 300,
182                                         auto_reconnect: true,
183                                         $auto_login
184                                         xhr_user_search: false
185                                 });\n";
186
187         $a->page['htmlhead'] .= "<script>
188                                         require(['converse'], function (converse) {
189                                                 $initialize
190                                                 converse.listen.on('ready', function (event) {
191                                                         $on_ready
192                                                 });
193                                                 $additional_commands
194                                         });
195                                 </script>";
196 }
197 ?>