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