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