5 Jappix - An open social platform
6 This is the PHP BOSH proxy
8 -------------------------------------------------
12 Last revision: 15/01/12
17 define('JAPPIX_BASE', '..');
19 // Get the configuration
20 require_once('./functions.php');
21 require_once('./read-main.php');
22 require_once('./read-hosts.php');
24 // Optimize the page rendering
30 header('Status: 403 Forbidden', true, 403);
31 exit('HTTP/1.1 403 Forbidden');
35 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
37 header('Access-Control-Allow-Origin: *');
38 header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
39 header('Access-Control-Allow-Headers: Content-Type');
40 header('Access-Control-Max-Age: 31536000');
46 $data = file_get_contents('php://input');
51 header('Access-Control-Allow-Origin: *');
52 header('Access-Control-Allow-Headers: Content-Type');
58 else if(isset($_GET['data']) && $_GET['data'] && isset($_GET['callback']) && $_GET['callback']) {
60 $data = $_GET['data'];
61 $callback = $_GET['callback'];
66 header('Status: 400 Bad Request', true, 400);
67 exit('HTTP/1.1 400 Bad Request');
71 $headers = array('User-Agent: Jappix (BOSH PHP Proxy)', 'Connection: close', 'Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data));
73 // CURL is better if available
74 if(function_exists('curl_init'))
79 // CURL stream functions
82 $connection = curl_init(HOST_BOSH);
84 // Set the CURL settings
85 curl_setopt($connection, CURLOPT_HEADER, 0);
86 curl_setopt($connection, CURLOPT_POST, 1);
87 curl_setopt($connection, CURLOPT_POSTFIELDS, $data);
88 curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
89 curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
90 curl_setopt($connection, CURLOPT_VERBOSE, 0);
91 curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 30);
92 curl_setopt($connection, CURLOPT_TIMEOUT, 30);
93 curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
94 curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
95 curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
97 // Get the CURL output
98 $output = curl_exec($connection);
101 // Built-in stream functions
104 $parameters = array('http' => array(
110 $parameters['http']['header'] = $headers;
112 // Change default timeout
113 ini_set('default_socket_timeout', 30);
115 // Create the connection
116 $stream = @stream_context_create($parameters);
117 $connection = @fopen(HOST_BOSH, 'rb', false, $stream);
119 // Failed to connect!
120 if($connection == false) {
121 header('Status: 502 Proxy Error', true, 502);
122 exit('HTTP/1.1 502 Proxy Error');
125 // Allow stream blocking to handle incoming BOSH data
126 @stream_set_blocking($connection, true);
128 // Get the output content
129 $output = @stream_get_contents($connection);
133 header('Cache-Control: no-cache, must-revalidate');
134 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
137 if($method == 'POST') {
139 header('Content-Type: text/xml; charset=utf-8');
142 echo('<body xmlns=\'http://jabber.org/protocol/httpbind\' type=\'terminate\'/>');
148 if($method == 'GET') {
150 header('Content-type: application/json');
152 // Encode output to JSON
153 $json_output = json_encode($output);
155 if(($output == false) || ($output == '') || ($json_output == 'null'))
156 echo($callback.'({"reply":"<body xmlns=\'http:\/\/jabber.org\/protocol\/httpbind\' type=\'terminate\'\/>"});');
158 echo($callback.'({"reply":'.$json_output.'});');
161 // Close the connection
163 curl_close($connection);
165 @fclose($connection);