From: Janusz Dobrowolski Date: Fri, 17 Sep 2010 09:01:35 +0000 (+0000) Subject: External url reading made independent of php configuration. X-Git-Tag: v2.4.2~19^2~623 X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;ds=sidebyside;h=6108acadfe6369362f469909f8dac47c17d21322;p=fa-stable.git External url reading made independent of php configuration. --- diff --git a/gl/includes/db/gl_db_rates.inc b/gl/includes/db/gl_db_rates.inc index 5db9ba24..ceaddcc7 100644 --- a/gl/includes/db/gl_db_rates.inc +++ b/gl/includes/db/gl_db_rates.inc @@ -9,6 +9,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License here . ***********************************************************************/ +include_once($path_to_root . "/includes/remote_url.inc"); //--------------------------------------------------------------------------------------------- function get_exchange_rate($rate_id) { @@ -123,18 +124,7 @@ function get_extern_rate($curr_b, $provider = 'ECB', $date) } while( ($contents == '') && $retry--); } else { - $handle = @fopen("http://".$site.$filename, 'rb'); - if ($handle) { - do - { - $data = @fread( $handle, 4096 ); - if ( strlen ( $data ) == 0 ) - break; - $contents .= $data; // with this syntax only text will be translated, whole text with htmlspecialchars($data) - } - while (true); - @fclose( $handle ); - } // end handle + $contents = url_get_contents("http://".$site.$filename); } if (!$contents) { display_warning(_("Cannot retrieve currency rate from $provider page. Please set the rate manually.")); diff --git a/includes/packages.inc b/includes/packages.inc index 247c25da..da379b81 100644 --- a/includes/packages.inc +++ b/includes/packages.inc @@ -10,6 +10,7 @@ See the License here . ***********************************************************************/ include_once($path_to_root. "/includes/archive.inc"); +include_once($path_to_root. "/includes/remote_url.inc"); define('PKG_CACHE_PATH', $path_to_root.'/modules/_cache'); define('PUBKEY_PATH', $path_to_root); @@ -290,10 +291,10 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey $refresh = true; do{ if (!file_exists($loclist)) { - copy(REPO_URL.'/Release.gz', $loclist); + url_copy(REPO_URL.'/Release.gz', $loclist); $refresh = false; } - $sig = file_get_contents(REPO_URL.'/Release.sig', 'rb'); + $sig = url_get_contents(REPO_URL.'/Release.sig'); $data = file_get_contents($loclist); $cert = file_get_contents(PUBKEY_PATH.'/FA.pem'); if (!openssl_verify($data, $sig, $cert)) { @@ -323,7 +324,7 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey $refresh = true; do{ if (!file_exists($locindex)) { - copy($remoteindex, $locindex); + url_copy($remoteindex, $locindex); $refresh = false; } if ($parms['SHA1sum'] != sha1_file($locindex)) { // check subdir index consistency @@ -359,7 +360,7 @@ function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey //download package to temp directory if ($download) { $locname = "$path_to_root/tmp/".$pkg['Filename'].'.pkg'; - copy($pkgfullname, $locname); + url_copy($pkgfullname, $locname); // checking sha1 hash is expensive proces, so chekc the package // consistency just before downloading if ($pkg['SHA1sum'] != sha1_file($locname)) { diff --git a/includes/remote_url.inc b/includes/remote_url.inc new file mode 100644 index 00000000..8b033806 --- /dev/null +++ b/includes/remote_url.inc @@ -0,0 +1,88 @@ +. +***********************************************************************/ + +/* + Read content of remote url via http. + Does not require curl php extension nor allow_url_fopen=1. +*/ +function url_get_contents($url, $timeout=10) +{ + // get the host name and url path + $parsedUrl = parse_url($url); + + $host = $parsedUrl['host']; + + if (isset($parsedUrl['path'])) { + $path = $parsedUrl['path']; + } else { + // the url is pointing to the host like http://www.mysite.com + $path = '/'; + } + + if (isset($parsedUrl['query'])) { + $path .= '?' . $parsedUrl['query']; + } + + if (isset($parsedUrl['port'])) { + $port = $parsedUrl['port']; + } else { + // most sites use port 80 + $port = '80'; + } + + $response = ''; + + // connect to the remote server + $fp = @fsockopen($host, $port, $errno, $errstr, $timeout ); + if( !$fp ) { + return null; + } else { + // send the necessary headers to get the file + fputs($fp, "GET $path HTTP/1.0\r\n" . + "Host: $host\r\n". + (isset($parsedUrl['pass']) ? "Authorization: Basic ". + base64_encode($parsedUrl['user'].':'.$parsedUrl['pass'])."\r\n" : ''). + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" . + "Accept: */*\r\n" . + "Accept-Language: en-us,en;q=0.5\r\n" . + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" . + "Keep-Alive: 300\r\n" . + "Connection: keep-alive\r\n" . + "Referer: http://$host\r\n\r\n"); + + // retrieve the response from the remote server + $len =0; + while ($line = fread($fp, 4096)) { + $response .= $line; +// if ($host=='localhost') +// stream_set_blocking($fp, 0); // just after connection switch to nonblocking mode + } + fclose( $fp ); + + if (!strpos($response, "200 OK\r\n")) + return null; + // strip the headers + $pos = strpos($response, "\r\n\r\n"); + + $response = substr($response, $pos + 4); + } + + // return the file content + return $response; +} + +function url_copy($from, $to, $timeout=10) +{ + $f = fopen($to, 'wb'); + fwrite($f, url_get_contents($from, $timeout)); + fclose($f); +}