Project

General

Profile

1 4307 leinfelder
<?php
2
/*
3
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
4
$Id: proxy.php 3650 2007-11-28 00:26:06Z rdewit $
5
$Name$
6
*/
7
8
////////////////////////////////////////////////////////////////////////////////
9
// Description:
10
// Script to redirect the request http://host/proxy.php?url=http://someUrl
11
// to http://someUrl .
12
//
13
// This script can be used to circumvent javascript's security requirements
14
// which prevent a URL from an external web site being called.
15
//
16
// Author: Nedjo Rogers
17
////////////////////////////////////////////////////////////////////////////////
18
19
// read in the variables
20
21
if(array_key_exists('HTTP_SERVERURL', $_SERVER)){
22
  $onlineresource=$_SERVER['HTTP_SERVERURL'];
23
}else{
24
  $onlineresource=$_REQUEST['url'];
25
}
26
$parsed = parse_url($onlineresource);
27
$host = @$parsed["host"];
28
$path = @$parsed["path"] . "?" . @$parsed["query"];
29
if(empty($host)) {
30
  $host = "localhost";
31
}
32
$port = @$parsed['port'];
33
if(empty($port)){
34
  $port="80";
35
}
36
$contenttype = @$_REQUEST['contenttype'];
37
if(empty($contenttype)) {
38
  $contenttype = "text/xml";
39
}
40
$data = @$GLOBALS["HTTP_RAW_POST_DATA"];
41
// define content type
42
header("Content-type: " . $contenttype);
43
44
if(empty($data)) {
45
  $result = send_request();
46
}
47
else {
48
  // post XML
49
  $posting = new HTTP_Client($host, $port, $data);
50
  $posting->set_path($path);
51
  $result = $posting->send_request();
52
}
53
54
// strip leading text from result and output result
55
$len=strlen($result);
56
$pos = strpos($result, "<");
57
if($pos > 1) {
58
  $result = substr($result, $pos, $len);
59
}
60
//$result = str_replace("xlink:","",$result);
61
echo $result;
62
63
// define class with functions to open socket and post XML
64
// from http://www.phpbuilder.com/annotate/message.php3?id=1013274 by Richard Hundt
65
66
class HTTP_Client {
67
  var $host;
68
  var $path;
69
  var $port;
70
  var $data;
71
  var $socket;
72
  var $errno;
73
  var $errstr;
74
  var $timeout;
75
  var $buf;
76
  var $result;
77
  var $agent_name = "MyAgent";
78
  //Constructor, timeout 30s
79
  function HTTP_Client($host, $port, $data, $timeout = 30) {
80
    $this->host = $host;
81
    $this->port = $port;
82
    $this->data = $data;
83
    $this->timeout = $timeout;
84
  }
85
86
  //Opens a connection
87
  function connect() {
88
    $this->socket = fsockopen($this->host,
89
      $this->port,
90
      $this->errno,
91
      $this->errstr,
92
      $this->timeout
93
      );
94
    if(!$this->socket)
95
      return false;
96
    else
97
      return true;
98
  }
99
100
  //Set the path
101
  function set_path($path) {
102
    $this->path = $path;
103
  }
104
105
  //Send request and clean up
106
  function send_request() {
107
    if(!$this->connect()) {
108
      return false;
109
    }
110
    else {
111
      $this->result = $this->request($this->data);
112
      return $this->result;
113
    }
114
  }
115
116
  function request($data) {
117
    $this->buf = "";
118
    fwrite($this->socket,
119
      "POST $this->path HTTP/1.0\r\n".
120
      "Host:$this->host\r\n".
121
      "User-Agent: $this->agent_name\r\n".
122
      "Content-Type: application/xml\r\n".
123
      "Content-Length: ".strlen($data).
124
      "\r\n".
125
      "\r\n".$data.
126
      "\r\n"
127
    );
128
129
    while(!feof($this->socket))
130
      $this->buf .= fgets($this->socket, 2048);
131
      $this->close();
132
      return $this->buf;
133
  }
134
135
136
  function close() {
137
    fclose($this->socket);
138
  }
139
}
140
141
142
143
function send_request() {
144
  global $onlineresource;
145
  $ch = curl_init();
146
  $timeout = 5; // set to zero for no timeout
147
148
  // fix to allow HTTPS connections with incorrect certificates
149
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
150
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
151
152
  curl_setopt ($ch, CURLOPT_URL,$onlineresource);
153
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
154
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
155
  curl_setopt ($ch, CURLOPT_ENCODING , "gzip, deflate");
156
157
  $file_contents = curl_exec($ch);
158
  curl_close($ch);
159
  $lines = array();
160
  $lines = explode("\n", $file_contents);
161
  if(!($response = $lines)) {
162
    echo "Unable to retrieve file '$service_request'";
163
  }
164
  $response = implode("",$response);
165
  return $response;
166
}
167
?>