Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
index.php
1 <?php
2 /**
3  * Testing from the command line:
4  * function getallheaders(){return array('X-Gentics' => 'X');};
5  * https url example: https://google.com/adsense
6  *
7  */
8 
9 require_once "settings.conf.php";
10 require_once "http.inc.php";
11 
12 $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
13 
14 $request = array(
15  'method' => $_SERVER['REQUEST_METHOD'],
16  'protocol' => $_SERVER['SERVER_PROTOCOL'],
17  'headers' => getallheaders(),
18  // multipart/form-data should work when the directives
19  // in the .htaccess are working to prevent PHP from parsing
20  // our data. This is done by a hack
21  'payload' => file_get_contents('php://input'),
22 );
23 
24 // Check if the header X-proxyphp-Content-Type was set by our mod-rewrite rule.
25 // If yes: restore the original Content-Type header.
26 if (isset($request['headers']['X-proxyphp-Content-Type'])) {
27  $request['headers']['Content-Type'] = $request['headers']['X-proxyphp-Content-Type'];
28  unset($request['headers']['X-proxyphp-Content-Type']);
29 }
30 
31 $url = $_SERVER['REQUEST_URI'];
32 // use URL from GET-parameters
33 if ( $reset_url == true ) $url = $_GET['url'];
34 
35 if (strpos($url, $PROXYNAME) === 0) {
36  $url = substr($url, strlen($PROXYNAME));
37 }
38 
39 // Make sure that the URL starts with a / ofr security reasons.
40 if ($url[0] !== '/') {
41  $url = '/' . $url;
42 }
43 
44 // Unset some headers which shouldn't get forwarded
45 if (isset($request['headers']['If-None-Match'])){
46  unset($request['headers']['If-None-Match']);
47 }
48 
49 if (isset($request['headers']['If-Modified-Since'])){
50  unset($request['headers']['If-Modified-Since']);
51 }
52 
53 if (isset($request['headers']['If-Modified-Since'])){
54  unset($request['headers']['If-Modified-Since']);
55 }
56 
57 // Add parameters to the query URL if specified
58 if (!empty($HTTP_URL_ADD_QUERY_PARAMETERS) && strpos($url, '?') === false){
59  $url = $url . '?' . $HTTP_URL_ADD_QUERY_PARAMETERS;
60 }
61 else {
62  $url = $url . '&' . $HTTP_URL_ADD_QUERY_PARAMETERS;
63 }
64 
65 // Remove slash at the end of $CMS_SERVERHOST if there is one
66 if (substr($url, -1) === '/') {
67  $CMS_SERVERHOST = substr($CMS_SERVERHOST, 0, -1);
68 }
69 
70 $request['url'] = $CMS_SERVERHOST . $url;
71 
72 $response = http_request($request);
73 
74 // Note HEAD does not always work even if specified...
75 // We use HEAD for Linkchecking so we do a 2nd request.
76 if ($_SERVER['REQUEST_METHOD'] === 'HEAD' && (int)$response['status'] >= 400) {
77 
78  $request['method'] = 'GET';
79  fpassthru($response['socket']);
80  fclose($response['socket']);
81  $response = http_request($request);
82 }
83 else {
84  $n_redirects = 0;
85  // Follow redirections until we got a page finally, but only max $HTTP_MAX_REDIRECTS times.
86  while (in_array($response['status'], array(301, 302, 307))) {
87  // We got a redirection request from the remote server,
88  // let's follow the trail and see what happens...
89  // We don't check if the URL is an external now, because the response
90  // should be trustworthy.
91  // We handle HTTP 301, 302 and 307
92  // See: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
93 
94  // Close the old socket
95  fclose($response['socket']);
96 
97  if ($n_redirects++ == $HTTP_MAX_REDIRECTS) {
98  myErrorHandler('Too many redirects (' . $n_redirects . '), exiting...');
99  }
100 
101  if (empty($response['headers']['Location'])) {
102  myErrorHandler("Got redirection request by the remote server, but the redirection URL is empty.");
103  }
104 
105  $n_redirects++;
106 
107  $url = $response['headers']['Location'];
108 
109  // For some unknown reason we got the new URL with the proxyname
110  // prepended back, so we have to remove that part of the URL.
111  // parse_url: http://php.net/manual/de/function.parse-url.php
112  $parsedURL = parse_url($url);
113  $url_path = $parsedURL['path'];
114 
115  // Check if the redirection URL starts with our proxyname
116  if (substr($url_path, 0, strlen($PROXYNAME)) === $PROXYNAME) {
117  $url_path = substr($url_path, strlen($PROXYNAME));
118 
119  // Now build the new URL
120  $url =
121  $parsedURL['scheme'] . '://' .
122  $parsedURL['host'] .
123  $url_path .
124  (empty($parsedURL['query']) ? '' : '?' . $parsedURL['query']);
125  }
126 
127  $request['url'] = $url;
128  $response = http_request($request);
129  }
130 }
131 
132 // Forward the response code to our client
133 // this sets the response code.
134 // we don't use http_response_code() as that only works for PHP >= 5.4
135 header('HTTP/1.0 ' . $response['status']);
136 
137 // forward each returned header...
138 foreach ($response['headers'] as $key => $value) {
139 
140  if (strtolower($key) == 'content-length') {
141  // There is no need to specify a content length since we don't do keep
142  // alive, and this can cause problems for integration (e.g. gzip output,
143  // which would change the content length)
144  // Note: overriding with header('Content-length:') will set
145  // the content-length to zero for some reason
146  continue;
147  }
148 
149  header($key . ': ' . $value);
150 }
151 
152 header('Connection: close');
153 
154 // output the contents if any
155 if (null !== $response['socket']) {
156  fpassthru($response['socket']);
157  fclose($response['socket']);
158 }
159 
160 function myErrorHandler($msg)
161 {
162  // 500 could be misleading...
163  // Should we return a special Error when a proxy error occurs?
164  header("HTTP/1.0 500 Internal Error");
165  die("Gentics Aloha Editor AJAX Gateway Error: $msg");
166 }
167