External url reading made independent of php configuration.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 17 Sep 2010 09:01:35 +0000 (09:01 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 17 Sep 2010 09:01:35 +0000 (09:01 +0000)
gl/includes/db/gl_db_rates.inc
includes/packages.inc
includes/remote_url.inc [new file with mode: 0644]

index 5db9ba2443568970e8dd473c7a37321bd14d3431..ceaddcc7966bf476f57cb8c4b62ce19820268e17 100644 (file)
@@ -9,6 +9,7 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+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."));
index 247c25da036e317cafed4dbdd5293cbff48228a3..da379b8100729632e62a6a5aa85a2bb9e2afe928 100644 (file)
@@ -10,6 +10,7 @@
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 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 (file)
index 0000000..8b03380
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+
+/*
+       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);
+}