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