1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| <?php
<?php
// proxy http
// auteur: Marc Gauthier
// 02/09/2009
// les erreurs dans les log du serveur
// http://www.papygeek.com/download/53/
// Transfer-Encoding: chunked
//
// Didier Richard - IGN - dérivation pour publication
// (c) IGN 2010
// License : http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
// ----------------------------------------------------------
// global variables :
$debug= 1;
$debug_html= 0;
$sUrl= '';
$sReponse= '';
$chunked= -1;
// via un proxy d'entreprise/IP
$proxy_host= '';
$proxy_port= 80;
$content_types= array(
'application/vnd.google-earth.kml+xml',
'application/vnd.google-earth.kml',
//'application/vnd.google-earth.kmz', # TODO needs to unzip response ...
'application/vnd.ogc.se_xml',
'application/vnd.ogc.wms_xml',
'application/vnd.ogc.wfs_xml',
'application/vnd.ogc.gml',
'application/soap+xml',
'application/xml',
'text/xml',
'text/plain',
'text/html');
// ----------------------------------------------------------
//
// écriture d'un message de log
function carp($msg) {
global $debug;
global $debug_html;
if ($debug) {
if ($debug_html) {
print "__FILE__.:$msg.<br>\n";
}
error_log(__FILE__.": $msg", 0);
}
}
//
// écriture d'un message de log et erreur http
function confess($msg) {
carp($msg);
header("HTTP/1.0 500 $msg");
exit;
}
//
// mode chunk
function unchunk($result) {
return preg_replace_callback(
'/(?:(?:\r\n|\n)|^)([0-9A-F]+)(?:\r\n|\n){1,2}(.*?)'.
'((?:\r\n|\n)(?:[0-9A-F]+(?:\r\n|\n))|$)/si',
create_function(
'$matches',
'return hexdec($matches[1]) == strlen($matches[2]) ? $matches[2] : $matches[0];'
),
$result
);
}
//
// la fonction proxy
// 2 grandes étapes:
// - émission de la requête
// - réception de la réponse
function proxy($sUrl) {
global $proxy_host, $proxy_port, $chunked, $content_types;
//
// analyse de l'url et construction de la requête
$sUrl= urldecode($sUrl);
$referrer= $_SERVER["HTTP_REFERER"];
$userAgent= $_SERVER["HTTP_USER_AGENT"];
$aUrl= @parse_url($sUrl);
if (!isset($aUrl['scheme'])) {
//try absolute and relative path:
confess("proxy scheme missing");
}
// construction de la requete
$acceptH= $_SERVER['HTTP_ACCEPT'];
$pragmaH= $_SERVER['HTTP_PRAGMA'];
$cacheControlH= $_SERVER['HTTP_CACHE_CONTROL'];
$acceptLanguageH= $_SERVER['HTTP_ACCEPT_CHARSET'];
$keepAliveH= $_SERVER['HTTP_KEEP_ALIVE'];
$acceptEncodingH= $_SERVER['HTTP_ACCEPT_ENCODING'];
$connectionH= $_SERVER['HTTP_CONNECTION'];
$SOAPActionH= $_SERVER['HTTP_SOAPACTION'];
$acceptCharsetH= $_SERVER['HTTP_ACCEPT_CHARSET'];
$gppKeyH= $_SERVER['HTTP_GPPKEY'];#if gppKey in HTTP Header
$Hs= "Host: ".$aUrl['host'].($aUrl['port']? ":".$aUrl['port'] : "")."\r\n"
. (strlen($acceptH)>0? "Accept: ".$acceptH : join(",", $content_types))."\r\n"
. (strlen($pragmaH)>0? "Pragma: ".$pragmaH."\r\n" : "")
. (strlen($cacheControlH)>0? "Cache-Control: ".$cacheControlH."\r\n" : "")
. (strlen($acceptLanguageH)>0? "Accept-Language: ".$acceptLanguageH."\r\n" : "")
. (strlen($keepAliveH)>0? "Keep-Alive: ".$keepAliveH."\r\n" : "")
. (strlen($acceptEncodingH)>0? "Accept-Encoding: ".$acceptEncodingH."\r\n" : "")
. (strlen($connectionH)>0? "Connection: ".$connectionH."\r\n" : "")
. (strlen($SOAPActionH)>0? "SOAPAction: ".$SOAPActionH."\r\n" : "")
. (strlen($acceptCharsetH)>0? "Accept-Charset: ".$acceptCharsetH."\r\n" : "")
. (strlen($gppKeyH)>0? "gppkey: ".$gppKeyH."\r\n" : "")
. (strlen($referrer)>0? "Referer: ".$referrer."\r\n" : "")
. (strlen($userAgent)>0? "User-Agent: ".$userAgent."\r\n" : "")
;
carp($Hs);
if ($_SERVER["REQUEST_METHOD"]==='GET') {
$sReq= "GET $sUrl HTTP/1.0\r\n"
. $Hs
;
} else {
if ($_SERVER["REQUEST_METHOD"]==='POST') {
$data= '';
if (count($_POST)){
while (list($key, $val)= each($_POST)){
$data.="$key : $val\n";
}
} else {
$data= trim(file_get_contents('php://input'));
}
$sReq= "POST $sUrl HTTP/1.0\r\n"
. $Hs
. "Content-Type: text/xml\r\n"
. "Content-length: ".strlen($data)."\r\n"
. "\r\n"
. $data
;
}
}
$sReq.= "\r\n";
if (empty($proxy_host)) {
$host= $aUrl["host"];
$port= $aUrl["port"] ? $aUrl["port"] : 80;
} else {
$host= $proxy_host;
$port= $proxy_port;
}
// envoi de la requête
carp("host:$host port:$port url:$sUrl");
$fp= @fsockopen($host, $port, $errno, $errstr, 5);
if (!$fp) {
confess("fsockopen failed: $errstr ($errno)");
}
carp("sReq:$sReq");
fwrite($fp, $sReq);
// attente de la réponse
$headers= '';
$sReponse= '';
ob_start();
while (!feof($fp)) {
$sReponse.= fread($fp, 4096);
}
fclose ($fp);
$eoh= strpos($sReponse, "\r\n\r\n");
$headers= substr($sReponse, 0, $eoh);
$sReponse= substr($sReponse, $eoh+4);
$Hs= preg_split('/(?:\r\n|\n)/', $headers);
carp("Hs=[".count($hs)."]");
for ($i= 0, $l= count($hs); $i<$l; $i++) {
if (preg_match('/^Content-Length/i', $hs[$i])) {
continue;
}
if (preg_match('/^Transfer-Encoding: chunked/i', $hs[$i])) {
// Transfer-Encoding: chunked
carp("chunked response");
$sReponse= unchunck($sReponse);
continue;
}
#carp("header=[$hs[$i]]");
header($hs[$i]);
}
header("Content-Length: ".strlen($sReponse));
print $sReponse;
}
// ----------------------------------------------------------
// programme principal:
// on accept que GET/POST (pour l'instant)
if (($_SERVER["REQUEST_METHOD"]==='GET' or $_SERVER["REQUEST_METHOD"]==='POST') &&
(isset($_REQUEST["url"]) && strlen($_REQUEST["url"])>0)) {
$sUrl= substr($_SERVER["QUERY_STRING"],4);
carp("GET url:$sUrl");
proxy($sUrl);
exit;
}
// on ne traite pas la demande :
carp("REQUEST_METHOD:".$_SERVER["REQUEST_METHOD"]." QUERY_STRING:".$_SERVER["QUERY_STRING"] );
if ($debug) {
phpinfo(INFO_VARIABLES);
}
exit;
// ----------------------------------------------------------
?> |
Partager