Merge pull request #1007 from annando/notice-info
[friendica-addons.git/.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  * Status: Unsupported
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14 use Friendica\Util\Strings;
15
16 function xmpp_install()
17 {
18         Hook::register('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
19         Hook::register('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
20         Hook::register('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
21         Hook::register('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
22 }
23
24 function xmpp_uninstall()
25 {
26         Hook::unregister('addon_settings', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings');
27         Hook::unregister('addon_settings_post', 'addon/xmpp/xmpp.php', 'xmpp_addon_settings_post');
28         Hook::unregister('page_end', 'addon/xmpp/xmpp.php', 'xmpp_script');
29         Hook::unregister('logged_in', 'addon/xmpp/xmpp.php', 'xmpp_login');
30 }
31
32 function xmpp_addon_settings_post()
33 {
34         if (!local_user() || empty($_POST['xmpp-settings-submit'])) {
35                 return;
36         }
37
38         DI::pConfig()->set(local_user(), 'xmpp', 'enabled', $_POST['xmpp_enabled'] ?? false);
39         DI::pConfig()->set(local_user(), 'xmpp', 'individual', $_POST['xmpp_individual'] ?? false);
40         DI::pConfig()->set(local_user(), 'xmpp', 'bosh_proxy', $_POST['xmpp_bosh_proxy'] ?? '');
41 }
42
43 function xmpp_addon_settings(App $a, &$s)
44 {
45         if (!local_user()) {
46                 return;
47         }
48
49         /* Add our stylesheet to the xmpp so we can make our settings look nice */
50
51         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
52
53         /* Get the current state of our config variable */
54
55         $enabled = intval(DI::pConfig()->get(local_user(), 'xmpp', 'enabled'));
56         $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
57
58         $individual = intval(DI::pConfig()->get(local_user(), 'xmpp', 'individual'));
59         $individual_checked = (($individual) ? ' checked="checked" ' : '');
60
61         $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
62
63         /* Add some HTML to the existing form */
64         $s .= '<span id="settings_xmpp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
65         $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
66         $s .= '</span>';
67         $s .= '<div id="settings_xmpp_expanded" class="settings-block" style="display: none;">';
68         $s .= '<span class="fakelink" onclick="openClose(\'settings_xmpp_expanded\'); openClose(\'settings_xmpp_inflated\');">';
69         $s .= '<h3>' . DI::l10n()->t('XMPP-Chat (Jabber)') . '</h3>';
70         $s .= '</span>';
71
72         $s .= '<div id="xmpp-settings-wrapper">';
73         $s .= '<label id="xmpp-enabled-label" for="xmpp-enabled">' . DI::l10n()->t('Enable Webchat') . '</label>';
74         $s .= '<input id="xmpp-enabled" type="checkbox" name="xmpp_enabled" value="1" ' . $enabled_checked . '/>';
75         $s .= '<div class="clear"></div>';
76
77         if (DI::config()->get("xmpp", "central_userbase")) {
78                 $s .= '<label id="xmpp-individual-label" for="xmpp-individual">' . DI::l10n()->t('Individual Credentials') . '</label>';
79                 $s .= '<input id="xmpp-individual" type="checkbox" name="xmpp_individual" value="1" ' . $individual_checked . '/>';
80                 $s .= '<div class="clear"></div>';
81         }
82
83         if (!DI::config()->get("xmpp", "central_userbase") || DI::pConfig()->get(local_user(), "xmpp", "individual")) {
84                 $s .= '<label id="xmpp-bosh-proxy-label" for="xmpp-bosh-proxy">' . DI::l10n()->t('Jabber BOSH host') . '</label>';
85                 $s .= ' <input id="xmpp-bosh-proxy" type="text" name="xmpp_bosh_proxy" value="' . $bosh_proxy . '" />';
86                 $s .= '<div class="clear"></div>';
87         }
88
89         $s .= '</div>';
90
91         /* provide a submit button */
92
93         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
94 }
95
96 function xmpp_login()
97 {
98         if (empty($_SESSION['allow_api'])) {
99                 $password = Strings::getRandomHex(16);
100                 DI::pConfig()->set(local_user(), 'xmpp', 'password', $password);
101         }
102 }
103
104 function xmpp_addon_admin(App $a, &$o)
105 {
106         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/xmpp/');
107
108         $o = Renderer::replaceMacros($t, [
109                 '$submit' => DI::l10n()->t('Save Settings'),
110                 '$bosh_proxy' => ['bosh_proxy', DI::l10n()->t('Jabber BOSH host'), DI::config()->get('xmpp', 'bosh_proxy'), ''],
111                 '$central_userbase' => ['central_userbase', DI::l10n()->t('Use central userbase'), DI::config()->get('xmpp', 'central_userbase'), DI::l10n()->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.')],
112         ]);
113 }
114
115 function xmpp_addon_admin_post()
116 {
117         $bosh_proxy = (!empty($_POST['bosh_proxy']) ? trim($_POST['bosh_proxy']) : '');
118         $central_userbase = (!empty($_POST['central_userbase']) ? intval($_POST['central_userbase']) : false);
119
120         DI::config()->set('xmpp', 'bosh_proxy', $bosh_proxy);
121         DI::config()->set('xmpp', 'central_userbase', $central_userbase);
122 }
123
124 function xmpp_script(App $a)
125 {
126         xmpp_converse($a);
127 }
128
129 function xmpp_converse(App $a)
130 {
131         if (!local_user()) {
132                 return;
133         }
134
135         if (($_GET['mode'] ?? '') == 'minimal') {
136                 return;
137         }
138
139         if (DI::mode()->isMobile() || DI::mode()->isMobile()) {
140                 return;
141         }
142
143         if (!DI::pConfig()->get(local_user(), "xmpp", "enabled")) {
144                 return;
145         }
146
147         if (in_array(DI::args()->getQueryString(), ["admin/federation/"])) {
148                 return;
149         }
150
151         DI::page()['htmlhead'] .= '<link type="text/css" rel="stylesheet" media="screen" href="addon/xmpp/converse/css/converse.css" />' . "\n";
152         DI::page()['htmlhead'] .= '<script src="addon/xmpp/converse/builds/converse.min.js"></script>' . "\n";
153
154         if (DI::config()->get("xmpp", "central_userbase") && !DI::pConfig()->get(local_user(), "xmpp", "individual")) {
155                 $bosh_proxy = DI::config()->get("xmpp", "bosh_proxy");
156
157                 $password = DI::pConfig()->get(local_user(), "xmpp", "password", '', true);
158
159                 if ($password == "") {
160                         $password = Strings::getRandomHex(16);
161                         DI::pConfig()->set(local_user(), "xmpp", "password", $password);
162                 }
163
164                 $jid = $a->user["nickname"] . "@" . DI::baseUrl()->getHostname() . "/converse-" . Strings::getRandomHex(5);
165
166                 $auto_login = "auto_login: true,
167                         authentication: 'login',
168                         jid: '$jid',
169                         password: '$password',
170                         allow_logout: false,";
171         } else {
172                 $bosh_proxy = DI::pConfig()->get(local_user(), "xmpp", "bosh_proxy");
173
174                 $auto_login = "";
175         }
176
177         if ($bosh_proxy == "") {
178                 return;
179         }
180
181         if (in_array($a->argv[0], ["delegation", "logout"])) {
182                 $additional_commands = "converse.user.logout();\n";
183         } else {
184                 $additional_commands = "";
185         }
186
187         $on_ready = "";
188
189         $initialize = "converse.initialize({
190                                         bosh_service_url: '$bosh_proxy',
191                                         keepalive: true,
192                                         message_carbons: false,
193                                         forward_messages: false,
194                                         play_sounds: true,
195                                         sounds_path: 'addon/xmpp/converse/sounds/',
196                                         roster_groups: false,
197                                         show_controlbox_by_default: false,
198                                         show_toolbar: true,
199                                         allow_contact_removal: false,
200                                         allow_registration: false,
201                                         hide_offline_users: true,
202                                         allow_chat_pending_contacts: false,
203                                         allow_dragresize: true,
204                                         auto_away: 0,
205                                         auto_xa: 0,
206                                         csi_waiting_time: 300,
207                                         auto_reconnect: true,
208                                         $auto_login
209                                         xhr_user_search: false
210                                 });\n";
211
212         DI::page()['htmlhead'] .= "<script>
213                                         require(['converse'], function (converse) {
214                                                 $initialize
215                                                 converse.listen.on('ready', function (event) {
216                                                         $on_ready
217                                                 });
218                                                 $additional_commands
219                                         });
220                                 </script>";
221 }