5 * Description: Embedded XMPP (Jabber) client
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
13 function xmpp_install()
15 register_hook('plugin_settings', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings');
16 register_hook('plugin_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings_post');
17 register_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
18 register_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
21 function xmpp_uninstall()
23 unregister_hook('plugin_settings', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings');
24 unregister_hook('plugin_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_plugin_settings_post');
25 unregister_hook('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
26 unregister_hook('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
29 function xmpp_plugin_settings_post()
31 if (!local_user() || (!x($_POST, 'xmpp-settings-submit'))) {
34 PConfig::set(local_user(), 'xmpp', 'enabled', intval($_POST['xmpp_enabled']));
35 PConfig::set(local_user(), 'xmpp', 'individual', intval($_POST['xmpp_individual']));
36 PConfig::set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy']);
38 info(t('XMPP settings updated.') . EOL);
41 function xmpp_plugin_settings(App $a, &$s)
47 /* Add our stylesheet to the xmpp so we can make our settings look nice */
49 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
51 /* Get the current state of our config variable */
53 $enabled = intval(PConfig::get(local_user(), 'xmpp', 'enabled'));
54 $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
56 $individual = intval(PConfig::get(local_user(), 'xmpp', 'individual'));
57 $individual_checked = (($individual) ? ' checked="checked" ' : '');
59 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
61 /* Add some HTML to the existing form */
62 $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
63 $s .= '<h3>' . t('XMPP-Chat (Jabber)') . '</h3>';
65 $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
66 $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
67 $s .= '<h3>' . t('XMPP-Chat (Jabber)') . '</h3>';
70 $s .= '<div id="xmpp-settings-wrapper">';
71 $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . t('Enable Webchat') . '</label>';
72 $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
73 $s .= '<div class="clear"></div>';
75 if (Config::get("xmpp", "central_userbase")) {
76 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . t('Individual Credentials') . '</label>';
77 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
78 $s .= '<div class="clear"></div>';
81 if (!Config::get("xmpp", "central_userbase") || PConfig::get(local_user(), "xmpp", "individual")) {
82 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . t('Jabber BOSH host') . '</label>';
83 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
84 $s .= '<div class="clear"></div>';
89 /* provide a submit button */
91 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
96 if (!$_SESSION["allow_api"]) {
97 $password = substr(random_string(),0,16);
98 PConfig::set(local_user(), "xmpp", "password", $password);
102 function xmpp_plugin_admin(App $a, &$o)
104 $t = get_markup_template("admin.tpl", "addon/xmpp/");
106 $o = replace_macros($t, array(
107 '$submit' => t('Save Settings'),
108 '$bosh_proxy' => array('bosh_proxy', t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''),
109 '$central_userbase' => array('central_userbase', t('Use central userbase'), Config::get('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.')),
113 function xmpp_plugin_admin_post()
115 $bosh_proxy = ((x($_POST, 'bosh_proxy')) ? trim($_POST['bosh_proxy']) : '');
116 $central_userbase = ((x($_POST, 'central_userbase')) ? intval($_POST['central_userbase']) : false);
117 Config::set('xmpp', 'bosh_proxy', $bosh_proxy);
118 Config::set('xmpp', 'central_userbase', $central_userbase);
119 info(t('Settings updated.') . EOL);
122 function xmpp_script(App $a)
127 function xmpp_converse(App $a)
133 if ($_GET["mode"] == "minimal") {
137 if ($a->is_mobile || $a->is_tablet) {
141 if (!PConfig::get(local_user(), "xmpp", "enabled")) {
145 if (in_array($a->query_string, array("admin/federation/"))) {
149 $a->page['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
150 $a->page['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
152 if (Config::get("xmpp", "central_userbase") && !PConfig::get(local_user(), "xmpp", "individual")) {
153 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
155 $password = PConfig::get(local_user(), "xmpp", "password");
157 if ($password == "") {
158 $password = random_string(16);
159 PConfig::set(local_user(), "xmpp", "password", $password);
162 $jid = $a->user["nickname"] . "@" . $a->get_hostname() . "/converse-" . random_string(5);
164 $auto_login = "auto_login: true,
165 authentication: 'login',
167 password: '$password',
168 allow_logout: false,";
170 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
175 if ($bosh_proxy == "") {
179 if (in_array($a->argv[0], array("manage", "logout"))) {
180 $additional_commands = "converse.user.logout();\n";
182 $additional_commands = "";
187 $initialize = "converse.initialize({
188 bosh_service_url: '$bosh_proxy',
190 message_carbons: false,
191 forward_messages: false,
193 sounds_path: 'addon/xmpp/converse/sounds/',
194 roster_groups: false,
195 show_controlbox_by_default: false,
197 allow_contact_removal: false,
198 allow_registration: false,
199 hide_offline_users: true,
200 allow_chat_pending_contacts: false,
201 allow_dragresize: true,
204 csi_waiting_time: 300,
205 auto_reconnect: true,
207 xhr_user_search: false
210 $a->page['htmlhead'] .= "<script>
211 require(['converse'], function (converse) {
213 converse.listen.on('ready', function (event) {