Use short form array syntax everywhere
[friendica-addons.git] / xmpp / xmpp.php
1 <?php
2
3 /**
4  * Name: XMPP (Jabber)
5  * Description: Embedded XMPP (Jabber) client
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9 use Friendica\App;
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12
13 function xmpp_install()
14 {
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');
19 }
20
21 function xmpp_uninstall()
22 {
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');
27 }
28
29 function xmpp_plugin_settings_post()
30 {
31         if (!local_user() || (!x($_POST, 'xmpp-settings-submit'))) {
32                 return;
33         }
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']);
37
38         info(t('XMPP settings updated.') . EOL);
39 }
40
41 function xmpp_plugin_settings(App $a, &$s)
42 {
43         if (!local_user()) {
44                 return;
45         }
46
47         /* Add our stylesheet to the xmpp so we can make our settings look nice */
48
49         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/xmpp/xmpp.css' . '" media="all" />' . "\r\n";
50
51         /* Get the current state of our config variable */
52
53         $enabled = intval(PConfig::get(local_user(), 'xmpp', 'enabled'));
54         $enabled_checked = (($enabled) ? ' checked="checked" ' : '');
55
56         $individual = intval(PConfig::get(local_user(), 'xmpp', 'individual'));
57         $individual_checked = (($individual) ? ' checked="checked" ' : '');
58
59         $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
60
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>';
64         $s .= '</span>';
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>';
68         $s .= '</span>';
69
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>';
74
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>';
79         }
80
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>';
85         }
86
87         $s .= '</div>';
88
89         /* provide a submit button */
90
91         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="xmpp-settings-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
92 }
93
94 function xmpp_login()
95 {
96         if (!$_SESSION["allow_api"]) {
97                 $password = random_string(16);
98                 PConfig::set(local_user(), "xmpp", "password", $password);
99         }
100 }
101
102 function xmpp_plugin_admin(App $a, &$o)
103 {
104         $t = get_markup_template("admin.tpl", "addon/xmpp/");
105
106         $o = replace_macros($t, [
107                 '$submit' => t('Save Settings'),
108                 '$bosh_proxy' => ['bosh_proxy', t('Jabber BOSH host'), Config::get('xmpp', 'bosh_proxy'), ''],
109                 '$central_userbase' => ['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.')],
110         ]);
111 }
112
113 function xmpp_plugin_admin_post()
114 {
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);
120 }
121
122 function xmpp_script(App $a)
123 {
124         xmpp_converse($a);
125 }
126
127 function xmpp_converse(App $a)
128 {
129         if (!local_user()) {
130                 return;
131         }
132
133         if ($_GET["mode"] == "minimal") {
134                 return;
135         }
136
137         if ($a->is_mobile || $a->is_tablet) {
138                 return;
139         }
140
141         if (!PConfig::get(local_user(), "xmpp", "enabled")) {
142                 return;
143         }
144
145         if (in_array($a->query_string, ["admin/federation/"])) {
146                 return;
147         }
148
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";
151
152         if (Config::get("xmpp", "central_userbase") && !PConfig::get(local_user(), "xmpp", "individual")) {
153                 $bosh_proxy = Config::get("xmpp", "bosh_proxy");
154
155                 $password = PConfig::get(local_user(), "xmpp", "password", '', true);
156
157                 if ($password == "") {
158                         $password = random_string(16);
159                         PConfig::set(local_user(), "xmpp", "password", $password);
160                 }
161
162                 $jid = $a->user["nickname"] . "@" . $a->get_hostname() . "/converse-" . random_string(5);
163
164                 $auto_login = "auto_login: true,
165                         authentication: 'login',
166                         jid: '$jid',
167                         password: '$password',
168                         allow_logout: false,";
169         } else {
170                 $bosh_proxy = PConfig::get(local_user(), "xmpp", "bosh_proxy");
171
172                 $auto_login = "";
173         }
174
175         if ($bosh_proxy == "") {
176                 return;
177         }
178
179         if (in_array($a->argv[0], ["manage", "logout"])) {
180                 $additional_commands = "converse.user.logout();\n";
181         } else {
182                 $additional_commands = "";
183         }
184
185         $on_ready = "";
186
187         $initialize = "converse.initialize({
188                                         bosh_service_url: '$bosh_proxy',
189                                         keepalive: true,
190                                         message_carbons: false,
191                                         forward_messages: false,
192                                         play_sounds: true,
193                                         sounds_path: 'addon/xmpp/converse/sounds/',
194                                         roster_groups: false,
195                                         show_controlbox_by_default: false,
196                                         show_toolbar: true,
197                                         allow_contact_removal: false,
198                                         allow_registration: false,
199                                         hide_offline_users: true,
200                                         allow_chat_pending_contacts: false,
201                                         allow_dragresize: true,
202                                         auto_away: 0,
203                                         auto_xa: 0,
204                                         csi_waiting_time: 300,
205                                         auto_reconnect: true,
206                                         $auto_login
207                                         xhr_user_search: false
208                                 });\n";
209
210         $a->page['htmlhead'] .= "<script>
211                                         require(['converse'], function (converse) {
212                                                 $initialize
213                                                 converse.listen.on('ready', function (event) {
214                                                         $on_ready
215                                                 });
216                                                 $additional_commands
217                                         });
218                                 </script>";
219 }