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