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