static function getURLContent($url, &$msgerr = NULL, $rec = 0)
{
global $CFG_GLPI;
$content = "";
$taburl = parse_url($url);
// Connection directe
if (empty($CFG_GLPI["proxy_name"])) {
$hostscheme = '';
$defaultport = 80;
// Manage standard HTTPS port : scheme detection or port 443
if (isset($taburl["scheme"]) && $taburl["scheme"] == 'https' || isset($taburl["port"]) && $taburl["port"] == '443') {
$hostscheme = 'ssl://';
$defaultport = 443;
}
if ($fp = @fsockopen($hostscheme . $taburl["host"], isset($taburl["port"]) ? $taburl["port"] : $defaultport, $errno, $errstr, 1)) {
if (isset($taburl["path"]) && $taburl["path"] != '/') {
$toget = $taburl["path"];
if (isset($taburl["query"])) {
$toget .= '?' . $taburl["query"];
}
// retrieve path + args
$request = "GET {$toget} HTTP/1.1\r\n";
} else {
$request = "GET / HTTP/1.1\r\n";
}
$request .= "Host: " . $taburl["host"] . "\r\n";
} else {
if (isset($msgerr)) {
//TRANS: %s is the error string
$msgerr = sprintf(__('Connection failed. If you use a proxy, please configure it. (%s)'), $errstr);
}
return "";
}
} else {
// Connection using proxy
$fp = fsockopen($CFG_GLPI["proxy_name"], $CFG_GLPI["proxy_port"], $errno, $errstr, 1);
if ($fp) {
$request = "GET {$url} HTTP/1.1\r\n";
$request .= "Host: " . $taburl["host"] . "\r\n";
if (!empty($CFG_GLPI["proxy_user"])) {
$request .= "Proxy-Authorization: Basic " . base64_encode($CFG_GLPI["proxy_user"] . ":" . self::decrypt($CFG_GLPI["proxy_passwd"], GLPIKEY)) . "\r\n";
}
} else {
if (isset($msgerr)) {
//TRANS: %s is the error string
$msgerr = sprintf(__('Failed to connect to the proxy server (%s)'), $errstr);
}
return "";
}
}
$request .= "User-Agent: GLPI/" . trim($CFG_GLPI["version"]) . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($fp, $request);
$header = true;
$redir = false;
$errstr = "";
while (!feof($fp)) {
if ($buf = fgets($fp, 1024)) {
if ($header) {
if (strlen(trim($buf)) == 0) {
// Empty line = end of header
$header = false;
} else {
if ($redir && preg_match("/^Location: (.*)\$/", $buf, $rep)) {
if ($rec < 9) {
$desturl = trim($rep[1]);
$taburl2 = parse_url($desturl);
if (isset($taburl2['host'])) {
// Redirect to another host
return self::getURLContent($desturl, $errstr, $rec + 1);
}
// redirect to same host
return self::getURLContent((isset($taburl['scheme']) ? $taburl['scheme'] : 'http') . "://" . $taburl['host'] . (isset($taburl['port']) ? ':' . $taburl['port'] : '') . $desturl, $errstr, $rec + 1);
}
$errstr = "Too deep";
break;
} else {
if (preg_match("/^HTTP.*200.*OK/", $buf)) {
// HTTP 200 = OK
} else {
if (preg_match("/^HTTP.*302/", $buf)) {
// HTTP 302 = Moved Temporarily
$redir = true;
} else {
if (preg_match("/^HTTP/", $buf)) {
// Other HTTP status = error
$errstr = trim($buf);
break;
}
}
}
}
}
} else {
// Body
$content .= $buf;
}
}
}
// eof
fclose($fp);
if (empty($content) && isset($msgerr)) {
if (empty($errstr)) {
$msgerr = __('No data available on the web site');
} else {
//TRANS: %s is the error string
$msgerr = sprintf(__('Impossible to connect to site (%s)'), $errstr);
}
}
return $content;
}