]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/bosh.php
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / php / bosh.php
1 <?php
2
3 /*
4
5 Jappix - An open social platform
6 This is the PHP BOSH proxy
7
8 -------------------------------------------------
9
10 License: AGPL
11 Author: Vanaryon
12 Last revision: 15/01/12
13
14 */
15
16 // PHP base
17 define('JAPPIX_BASE', '..');
18
19 // Get the configuration
20 require_once('./functions.php');
21 require_once('./read-main.php');
22 require_once('./read-hosts.php');
23
24 // Optimize the page rendering
25 hideErrors();
26 compressThis();
27
28 // Not allowed?
29 if(!BOSHProxy()) {
30         header('Status: 403 Forbidden', true, 403);
31         exit('HTTP/1.1 403 Forbidden');
32 }
33
34 // OPTIONS method?
35 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
36         // CORS headers
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');
41         
42         exit;
43 }
44
45 // Read POST content
46 $data = file_get_contents('php://input');
47
48 // POST method?
49 if($data) {
50         // CORS headers
51         header('Access-Control-Allow-Origin: *');
52         header('Access-Control-Allow-Headers: Content-Type');
53         
54         $method = 'POST';
55 }
56
57 // GET method?
58 else if(isset($_GET['data']) && $_GET['data'] && isset($_GET['callback']) && $_GET['callback']) {
59         $method = 'GET';
60         $data = $_GET['data'];
61         $callback = $_GET['callback'];
62 }
63
64 // Invalid method?
65 else {
66         header('Status: 400 Bad Request', true, 400);
67         exit('HTTP/1.1 400 Bad Request');
68 }
69
70 // HTTP headers
71 $headers = array('User-Agent: Jappix (BOSH PHP Proxy)', 'Connection: close', 'Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data));
72
73 // CURL is better if available
74 if(function_exists('curl_init'))
75         $use_curl = true;
76 else
77         $use_curl = false;
78
79 // CURL stream functions
80 if($use_curl) {
81         // Initialize CURL
82         $connection = curl_init(HOST_BOSH);
83         
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);
96         
97         // Get the CURL output
98         $output = curl_exec($connection);
99 }
100
101 // Built-in stream functions
102 else {
103         // HTTP parameters
104         $parameters = array('http' => array(
105                                         'method' => 'POST',
106                                         'content' => $data
107                                       )
108                       );
109
110         $parameters['http']['header'] = $headers;
111
112         // Change default timeout
113         ini_set('default_socket_timeout', 30);
114
115         // Create the connection
116         $stream = @stream_context_create($parameters);
117         $connection = @fopen(HOST_BOSH, 'rb', false, $stream);
118
119         // Failed to connect!
120         if($connection == false) {
121                 header('Status: 502 Proxy Error', true, 502);
122                 exit('HTTP/1.1 502 Proxy Error');
123         }
124
125         // Allow stream blocking to handle incoming BOSH data
126         @stream_set_blocking($connection, true);
127
128         // Get the output content
129         $output = @stream_get_contents($connection);
130 }
131
132 // Cache headers
133 header('Cache-Control: no-cache, must-revalidate');
134 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
135
136 // POST output
137 if($method == 'POST') {
138         // XML header
139         header('Content-Type: text/xml; charset=utf-8');
140         
141         if(!$output)
142                 echo('<body xmlns=\'http://jabber.org/protocol/httpbind\' type=\'terminate\'/>');
143         else
144                 echo($output);
145 }
146
147 // GET output
148 if($method == 'GET') {
149         // JSON header
150         header('Content-type: application/json');
151         
152         // Encode output to JSON
153         $json_output = json_encode($output);
154         
155         if(($output == false) || ($output == '') || ($json_output == 'null'))
156                 echo($callback.'({"reply":"<body xmlns=\'http:\/\/jabber.org\/protocol\/httpbind\' type=\'terminate\'\/>"});');
157         else
158                 echo($callback.'({"reply":'.$json_output.'});');
159 }
160
161 // Close the connection
162 if($use_curl)
163         curl_close($connection);
164 else
165         @fclose($connection);
166
167 ?>