]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/proxy.php
Preparation to make the automatic authentication work (hopefully) reliable
[friendica-addons.git] / jappixmini / proxy.php
1 <?php
2
3 /*
4
5 Jappix - An open social platform
6 This is a PHP BOSH proxy
7
8 -------------------------------------------------
9
10 This file is dual-licensed under the MIT license (see MIT.txt) and the AGPL license (see jappix/COPYING).
11 Authors: Vanaryon, Leberwurscht
12
13 */
14
15 // PHP base
16 define('JAPPIX_BASE', './jappix');
17
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');
22
23 // Optimize the page rendering
24 hideErrors();
25 compressThis();
26
27 // Not allowed?
28 if(!BOSHProxy()) {
29         header('Status: 403 Forbidden', true, 403);
30         exit('HTTP/1.1 403 Forbidden');
31 }
32
33 // custom BOSH host
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;
39         }
40 }
41
42 // OPTIONS method?
43 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
44         // CORS headers
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');
49         
50         exit;
51 }
52
53 // Read POST content
54 $data = file_get_contents('php://input');
55
56 // POST method?
57 if($data) {
58         // CORS headers
59         header('Access-Control-Allow-Origin: *');
60         header('Access-Control-Allow-Headers: Content-Type');
61         
62         $method = 'POST';
63 }
64
65 // GET method?
66 else if(isset($_GET['data']) && $_GET['data'] && isset($_GET['callback']) && $_GET['callback']) {
67         $method = 'GET';
68         $data = $_GET['data'];
69         $callback = $_GET['callback'];
70 }
71
72 // Invalid method?
73 else {
74         header('Status: 400 Bad Request', true, 400);
75         exit('HTTP/1.1 400 Bad Request');
76 }
77
78 // HTTP headers
79 $headers = array('User-Agent: Jappix (BOSH PHP Proxy)', 'Connection: keep-alive', 'Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data));
80
81 // CURL is better if available
82 if(function_exists('curl_init'))
83         $use_curl = true;
84 else
85         $use_curl = false;
86
87 // CURL caused problems for me
88 $use_curl = false;
89
90 // CURL stream functions
91 if($use_curl) {
92         // Initialize CURL
93         $connection = curl_init($HOST_BOSH);
94         
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);
107         
108         // Get the CURL output
109         $output = curl_exec($connection);
110 }
111
112 // Built-in stream functions
113 else {
114         // HTTP parameters
115         $parameters = array('http' => array(
116                                         'method' => 'POST',
117                                         'content' => $data
118                                       )
119                       );
120
121         $parameters['http']['header'] = $headers;
122
123         // Change default timeout
124         ini_set('default_socket_timeout', 30);
125
126         // Create the connection
127         $stream = @stream_context_create($parameters);
128         $connection = @fopen($HOST_BOSH, 'rb', false, $stream);
129
130         // Failed to connect!
131         if($connection == false) {
132                 header('Status: 502 Proxy Error', true, 502);
133                 exit('HTTP/1.1 502 Proxy Error');
134         }
135
136         // Allow stream blocking to handle incoming BOSH data
137         @stream_set_blocking($connection, true);
138
139         // Get the output content
140         $output = @stream_get_contents($connection);
141 }
142
143 // Cache headers
144 header('Cache-Control: no-cache, must-revalidate');
145 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
146
147 // POST output
148 if($method == 'POST') {
149         // XML header
150         header('Content-Type: text/xml; charset=utf-8');
151         
152         if(!$output)
153                 echo('<body xmlns=\'http://jabber.org/protocol/httpbind\' type=\'terminate\'/>');
154         else
155                 echo($output);
156 }
157
158 // GET output
159 if($method == 'GET') {
160         // JSON header
161         header('Content-type: application/json');
162         
163         // Encode output to JSON
164         $json_output = json_encode($output);
165         
166         if(($output == false) || ($output == '') || ($json_output == 'null'))
167                 echo($callback.'({"reply":"<body xmlns=\'http:\/\/jabber.org\/protocol\/httpbind\' type=\'terminate\'\/>"});');
168         else
169                 echo($callback.'({"reply":'.$json_output.'});');
170 }
171
172 // Close the connection
173 if($use_curl)
174         curl_close($connection);
175 else
176         @fclose($connection);
177
178 ?>