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