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