4 * Description: Embedded XMPP (Jabber) client
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
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');
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');
23 function xmpp_plugin_settings_post($a,$post) {
24 if(! local_user() || (! x($_POST,'xmpp-settings-submit')))
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']);
30 info( t('XMPP settings updated.') . EOL);
33 function xmpp_plugin_settings(&$a,&$s) {
38 /* Add our stylesheet to the xmpp so we can make our settings look nice */
40 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
42 /* Get the current state of our config variable */
44 $enabled = intval(get_pconfig(local_user(),'xmpp','enabled'));
45 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
47 $individual = intval(get_pconfig(local_user(),'xmpp','individual'));
48 $individual_checked = (($individual) ? ' checked="checked" ' : '');
50 $bosh_proxy = get_pconfig(local_user(),"xmpp","bosh_proxy");
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>';
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>';
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>';
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>';
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>';
80 /* provide a submit button */
82 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
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);
93 function xmpp_plugin_admin(&$a, &$o){
94 $t = get_markup_template("admin.tpl", "addon/xmpp/");
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.')),
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 );
111 function xmpp_script(&$a,&$s) {
112 xmpp_converse($a,$s);
115 function xmpp_converse(&$a,&$s) {
119 if ($_GET["mode"] == "minimal")
122 if ($a->is_mobile || $a->is_tablet)
125 if (!get_pconfig(local_user(),"xmpp","enabled"))
128 if (in_array($a->query_string, array("admin/federation/")))
131 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />'."\n";
132 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>'."\n";
134 if (get_config("xmpp", "central_userbase") AND !get_pconfig(local_user(),"xmpp","individual")) {
135 $bosh_proxy = get_config("xmpp", "bosh_proxy");
137 $password = get_pconfig(local_user(), "xmpp", "password");
139 if ($password == "") {
140 $password = substr(random_string(),0,16);
141 set_pconfig(local_user(), "xmpp", "password", $password);
144 $jid = $a->user["nickname"]."@".$a->get_hostname()."/converse-".substr(random_string(),0,5);;
146 $auto_login = "auto_login: true,
147 authentication: 'login',
149 password: '$password',
150 allow_logout: false,";
152 $bosh_proxy = get_pconfig(local_user(), "xmpp", "bosh_proxy");
157 if ($bosh_proxy == "")
160 if (in_array($a->argv[0], array("manage", "logout")))
161 $additional_commands = "converse.user.logout();\n";
163 $additional_commands = "";
167 $initialize = "converse.initialize({
168 bosh_service_url: '$bosh_proxy',
170 message_carbons: false,
171 forward_messages: false,
173 sounds_path: 'addon/xmpp/converse/sounds/',
174 roster_groups: false,
175 show_controlbox_by_default: false,
177 allow_contact_removal: false,
178 allow_registration: false,
179 hide_offline_users: true,
180 allow_chat_pending_contacts: false,
181 allow_dragresize: true,
184 csi_waiting_time: 300,
185 auto_reconnect: true,
187 xhr_user_search: false
190 $a->page['htmlhead'] .= "<script>
191 require(['converse'], function (converse) {
193 converse.listen.on('ready', function (event) {