function fs_fetch_http_file($url, &$error) { $res = fs_create_http_conn($url); if ($res['status'] != 'ok') { $error = $res['status']; return null; } else { $http = $res['http']; $args = $res['args']; $error = $http->Open($args); if (!empty($error)) { return false; } $error = $http->SendRequest($args); if (!empty($error)) { return false; } $http->ReadReplyHeadersResponse($headers); if ($http->response_status != '200') { $error = sprintf(fs_r("Server returned error %s for %s"), "<b>{$http->response_status}</b>", "<b>{$url}</b>"); return false; } $content = ''; for (;;) { $body = ""; $error = $http->ReadReplyBody($body, 1000); if ($error != "" || strlen($body) == 0) { break; } $content .= $body; } return $content; } }
/** * Downloads a zip file containing the ip2country database and import it. */ function fs_update_ip2country_db($url, $file_type, $new_version) { $cant_write = fs_ip2c_database_writeable(); if ($cant_write != '') { return array('status' => 'error', 'message' => $cant_write); } require_once FS_ABS_PATH . '/php/utils.php'; $dir = $GLOBALS['FS_TEMP_DIR']; $tempName = @tempnam($dir, "fs_ip2c_"); if (!$tempName) { return array('status' => 'error', 'message' => fs_r('Error creating temporary file')); } $temp = @fopen($tempName, "w"); if (!$tempName) { return array('status' => 'error', 'message' => fs_r('Error creating temporary file')); } $res = fs_create_http_conn($url); $http = $res['http']; $args = $res['args']; $error = $http->Open($args); if (!empty($error)) { return array('status' => 'error', 'message' => sprintf(fs_r('Error opening connection to %s'), $url)); } $error = $http->SendRequest($args); if (!empty($error)) { return array('status' => 'error', 'message' => sprintf(fs_r('Error sending request to %s'), $url)); } $http->ReadReplyHeadersResponse($headers); if ($http->response_status != '200') { return array('status' => 'error', 'message' => sprintf(fs_r("Server returned error %s for %s"), "<b>{$http->response_status}</b>", "<b>{$url}</b>")); } $output = ob_get_clean(); // this is a little hack to keep outputing stuff while we download, it should help // with the server killing the script due to inactivity. echo "/* Downloading IP-to-country database. if you see this, your server didn't give this script enough time to complete.<br/>"; $content = ''; for (;;) { $body = ""; $error = $http->ReadReplyBody($body, 10000); if ($error != "") { return array('status' => 'error', 'message' => sprintf(fs_r('Error reading data from %s : %s'), $url, $error)); } echo "*"; if ($body == '') { break; } fwrite($temp, $body); } echo "*/"; ob_start(); echo $output; if ($file_type == 'bin') { return fs_install_bin_ip2c_database($new_version, $temp); } else { if ($file_type == 'zip') { $bin_file = ''; $res = fs_extract_zip_ip2c_database($tempName, $bin_file); if ($res == '') { $ok = fs_install_bin_ip2c_database($new_version, $bin_file); fs_clean_ip2c_temp(); return $ok; } else { return array('status' => 'error', 'message' => sprintf(fs_r("Error extracting IP-to-country database: %s"), $res)); } } else { return array('status' => 'error', 'message' => sprintf(fs_r('Unsupported file type : %s'), $file_type)); } } }