@ -1,5 +1,14 @@
<?php
<?php
class UrlHelper {
class UrlHelper {
static $fetch_last_error;
static $fetch_last_error_code;
static $fetch_last_error_content;
static $fetch_last_content_type;
static $fetch_last_modified;
static $fetch_effective_url;
static $fetch_effective_ip_addr;
static $fetch_curl_used;
static function build_url($parts) {
static function build_url($parts) {
$tmp = $parts['scheme'] . "://" . $parts['host'];
$tmp = $parts['scheme'] . "://" . $parts['host'];
@ -158,6 +167,7 @@ class UrlHelper {
public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false,
public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false,
4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) {
4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) {
/*
global $fetch_last_error;
global $fetch_last_error;
global $fetch_last_error_code;
global $fetch_last_error_code;
global $fetch_last_error_content;
global $fetch_last_error_content;
@ -166,15 +176,16 @@ class UrlHelper {
global $fetch_effective_url;
global $fetch_effective_url;
global $fetch_effective_ip_addr;
global $fetch_effective_ip_addr;
global $fetch_curl_used;
global $fetch_curl_used;
*/
$fetch_last_error = false;
self:: $fetch_last_error = false;
$fetch_last_error_code = -1;
self:: $fetch_last_error_code = -1;
$fetch_last_error_content = "";
self:: $fetch_last_error_content = "";
$fetch_last_content_type = "";
self:: $fetch_last_content_type = "";
$fetch_curl_used = false;
self:: $fetch_curl_used = false;
$fetch_last_modified = "";
self:: $fetch_last_modified = "";
$fetch_effective_url = "";
self:: $fetch_effective_url = "";
$fetch_effective_ip_addr = "";
self:: $fetch_effective_ip_addr = "";
if (!is_array($options)) {
if (!is_array($options)) {
@ -219,7 +230,7 @@ class UrlHelper {
$url = self::validate($url, true);
$url = self::validate($url, true);
if (!$url) {
if (!$url) {
$fetch_last_error = "Requested URL failed extended validation.";
self:: $fetch_last_error = "Requested URL failed extended validation.";
return false;
return false;
}
}
@ -227,13 +238,13 @@ class UrlHelper {
$ip_addr = gethostbyname($url_host);
$ip_addr = gethostbyname($url_host);
if (!$ip_addr || strpos($ip_addr, "127.") === 0) {
if (!$ip_addr || strpos($ip_addr, "127.") === 0) {
$fetch_last_error = "URL hostname failed to resolve or resolved to a loopback address ($ip_addr)";
self:: $fetch_last_error = "URL hostname failed to resolve or resolved to a loopback address ($ip_addr)";
return false;
return false;
}
}
if (function_exists('curl_init') & & !ini_get("open_basedir")) {
if (function_exists('curl_init') & & !ini_get("open_basedir")) {
$fetch_curl_used = true;
self:: $fetch_curl_used = true;
$ch = curl_init($url);
$ch = curl_init($url);
@ -306,13 +317,13 @@ class UrlHelper {
list ($key, $value) = explode(": ", $header);
list ($key, $value) = explode(": ", $header);
if (strtolower($key) == "last-modified") {
if (strtolower($key) == "last-modified") {
$fetch_last_modified = $value;
self:: $fetch_last_modified = $value;
}
}
}
}
if (substr(strtolower($header), 0, 7) == 'http/1.') {
if (substr(strtolower($header), 0, 7) == 'http/1.') {
$fetch_last_error_code = (int) substr($header, 9, 3);
self:: $fetch_last_error_code = (int) substr($header, 9, 3);
$fetch_last_error = $header;
self:: $fetch_last_error = $header;
}
}
}
}
@ -322,39 +333,39 @@ class UrlHelper {
}
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
self:: $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$fetch_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
self:: $fetch_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if (!self::validate($fetch_effective_url, true)) {
if (!self::validate(self:: $fetch_effective_url, true)) {
$fetch_last_error = "URL received after redirection failed extended validation.";
self:: $fetch_last_error = "URL received after redirection failed extended validation.";
return false;
return false;
}
}
$fetch_effective_ip_addr = gethostbyname(parse_url($fetch_effective_url, PHP_URL_HOST));
self:: $fetch_effective_ip_addr = gethostbyname(parse_url(self:: $fetch_effective_url, PHP_URL_HOST));
if (!$fetch_effective_ip_addr || strpos($fetch_effective_ip_addr, "127.") === 0) {
if (!self:: $fetch_effective_ip_addr || strpos(self:: $fetch_effective_ip_addr, "127.") === 0) {
$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address ($fetch_effective_ip_addr)";
self:: $fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self:: $fetch_effective_ip_addr." )";
return false;
return false;
}
}
$fetch_last_error_code = $http_code;
self:: $fetch_last_error_code = $http_code;
if ($http_code != 200 || $type & & strpos($fetch_last_content_type, "$type") === false) {
if ($http_code != 200 || $type & & strpos(self:: $fetch_last_content_type, "$type") === false) {
if (curl_errno($ch) != 0) {
if (curl_errno($ch) != 0) {
$fetch_last_error .= "; " . curl_errno($ch) . " " . curl_error($ch);
self:: $fetch_last_error .= "; " . curl_errno($ch) . " " . curl_error($ch);
}
}
$fetch_last_error_content = $contents;
self:: $fetch_last_error_content = $contents;
curl_close($ch);
curl_close($ch);
return false;
return false;
}
}
if (!$contents) {
if (!$contents) {
$fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
self:: $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
curl_close($ch);
curl_close($ch);
return false;
return false;
}
}
@ -372,7 +383,7 @@ class UrlHelper {
return $contents;
return $contents;
} else {
} else {
$fetch_curl_used = false;
self:: $fetch_curl_used = false;
if ($login & & $pass){
if ($login & & $pass){
$url_parts = array();
$url_parts = array();
@ -417,18 +428,18 @@ class UrlHelper {
$old_error = error_get_last();
$old_error = error_get_last();
$fetch_effective_url = self::resolve_redirects($url, $timeout ? $timeout : Config::get(Config::FILE_FETCH_CONNECT_TIMEOUT));
self:: $fetch_effective_url = self::resolve_redirects($url, $timeout ? $timeout : Config::get(Config::FILE_FETCH_CONNECT_TIMEOUT));
if (!self::validate($fetch_effective_url, true)) {
if (!self::validate(self:: $fetch_effective_url, true)) {
$fetch_last_error = "URL received after redirection failed extended validation.";
self:: $fetch_last_error = "URL received after redirection failed extended validation.";
return false;
return false;
}
}
$fetch_effective_ip_addr = gethostbyname(parse_url($fetch_effective_url, PHP_URL_HOST));
self:: $fetch_effective_ip_addr = gethostbyname(parse_url(self:: $fetch_effective_url, PHP_URL_HOST));
if (!$fetch_effective_ip_addr || strpos($fetch_effective_ip_addr, "127.") === 0) {
if (!self:: $fetch_effective_ip_addr || strpos(self:: $fetch_effective_ip_addr, "127.") === 0) {
$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address ($fetch_effective_ip_addr)";
self:: $fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self:: $fetch_effective_ip_addr." )";
return false;
return false;
}
}
@ -442,30 +453,30 @@ class UrlHelper {
$key = strtolower($key);
$key = strtolower($key);
if ($key == 'content-type') {
if ($key == 'content-type') {
$fetch_last_content_type = $value;
self:: $fetch_last_content_type = $value;
// don't abort here b/c there might be more than one
// don't abort here b/c there might be more than one
// e.g. if we were being redirected -- last one is the right one
// e.g. if we were being redirected -- last one is the right one
} else if ($key == 'last-modified') {
} else if ($key == 'last-modified') {
$fetch_last_modified = $value;
self:: $fetch_last_modified = $value;
} else if ($key == 'location') {
} else if ($key == 'location') {
$fetch_effective_url = $value;
self:: $fetch_effective_url = $value;
}
}
}
}
if (substr(strtolower($header), 0, 7) == 'http/1.') {
if (substr(strtolower($header), 0, 7) == 'http/1.') {
$fetch_last_error_code = (int) substr($header, 9, 3);
self:: $fetch_last_error_code = (int) substr($header, 9, 3);
$fetch_last_error = $header;
self:: $fetch_last_error = $header;
}
}
}
}
if ($fetch_last_error_code != 200) {
if (self:: $fetch_last_error_code != 200) {
$error = error_get_last();
$error = error_get_last();
if ($error['message'] != $old_error['message']) {
if ($error['message'] != $old_error['message']) {
$fetch_last_error .= "; " . $error["message"];
self:: $fetch_last_error .= "; " . $error["message"];
}
}
$fetch_last_error_content = $data;
self:: $fetch_last_error_content = $data;
return false;
return false;
}
}