5 Jappix - An open social platform
6 This is a PHP BOSH proxy
8 -------------------------------------------------
10 This file is dual-licensed under the MIT license (see MIT.txt) and the AGPL license (see jappix/COPYING).
11 Authors: Vanaryon, Leberwurscht
16 define('JAPPIX_BASE', './jappix');
18 // Get the configuration
19 require_once('./jappix/php/functions.php');
20 require_once('./jappix/php/read-main.php');
21 require_once('./jappix/php/read-hosts.php');
23 // Optimize the page rendering
29 header('Status: 403 Forbidden', true, 403);
30 exit('HTTP/1.1 403 Forbidden');
34 $HOST_BOSH = HOST_BOSH;
35 if(isset($_GET['host_bosh']) && $_GET['host_bosh']) {
36 $host_bosh = $_GET['host_bosh'];
37 if (substr($host_bosh, 0, 7)==="http://" || substr($host_bosh, 0, 8)==="https://") {
38 $HOST_BOSH = $host_bosh;
43 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
45 header('Access-Control-Allow-Origin: *');
46 header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
47 header('Access-Control-Allow-Headers: Content-Type');
48 header('Access-Control-Max-Age: 31536000');
54 $data = file_get_contents('php://input');
59 header('Access-Control-Allow-Origin: *');
60 header('Access-Control-Allow-Headers: Content-Type');
66 else if(isset($_GET['data']) && $_GET['data'] && isset($_GET['callback']) && $_GET['callback']) {
68 $data = $_GET['data'];
69 $callback = $_GET['callback'];
74 header('Status: 400 Bad Request', true, 400);
75 exit('HTTP/1.1 400 Bad Request');
79 $headers = array('User-Agent: Jappix (BOSH PHP Proxy)', 'Connection: keep-alive', 'Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data));
81 // CURL is better if available
82 if(function_exists('curl_init'))
87 // CURL caused problems for me
90 // CURL stream functions
93 $connection = curl_init($HOST_BOSH);
95 // Set the CURL settings
96 curl_setopt($connection, CURLOPT_HEADER, 0);
97 curl_setopt($connection, CURLOPT_POST, 1);
98 curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
99 curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
100 curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
101 curl_setopt($connection, CURLOPT_VERBOSE, 0);
102 curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 30);
103 curl_setopt($connection, CURLOPT_TIMEOUT, 30);
104 curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
105 curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
106 curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
108 // Get the CURL output
109 $output = curl_exec($connection);
112 // Built-in stream functions
115 $parameters = array('http' => array(
121 $parameters['http']['header'] = $headers;
123 // Change default timeout
124 ini_set('default_socket_timeout', 30);
126 // Create the connection
127 $stream = @stream_context_create($parameters);
128 $connection = @fopen($HOST_BOSH, 'rb', false, $stream);
130 // Failed to connect!
131 if($connection == false) {
132 header('Status: 502 Proxy Error', true, 502);
133 exit('HTTP/1.1 502 Proxy Error');
136 // Allow stream blocking to handle incoming BOSH data
137 @stream_set_blocking($connection, true);
139 // Get the output content
140 $output = @stream_get_contents($connection);
144 header('Cache-Control: no-cache, must-revalidate');
145 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
148 if($method == 'POST') {
150 header('Content-Type: text/xml; charset=utf-8');
153 echo('<body xmlns=\'http://jabber.org/protocol/httpbind\' type=\'terminate\'/>');
159 if($method == 'GET') {
161 header('Content-type: application/json');
163 // Encode output to JSON
164 $json_output = json_encode($output);
166 if(($output == false) || ($output == '') || ($json_output == 'null'))
167 echo($callback.'({"reply":"<body xmlns=\'http:\/\/jabber.org\/protocol\/httpbind\' type=\'terminate\'\/>"});');
169 echo($callback.'({"reply":'.$json_output.'});');
172 // Close the connection
174 curl_close($connection);
176 @fclose($connection);