summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBill Marquette <billm@pfsense.org>2008-07-24 03:53:11 +0000
committerBill Marquette <billm@pfsense.org>2008-07-24 03:53:11 +0000
commit9140757d593279628f6e102cbd896fe7a708d494 (patch)
treef0070ebb0dfc4060d604f6e204e637e888c2487e
parent96e00c2ff57ee8edc17efd201c95380eadbb3ffe (diff)
downloadpfsense-9140757d593279628f6e102cbd896fe7a708d494.zip
pfsense-9140757d593279628f6e102cbd896fe7a708d494.tar.gz
DRY up the code a tad, download_file_with_progress_bar isn't related to
packages only anymore, move into pfsense-utils and allow for multiple file write functions (firmware upgrades use a different one than our standard)
-rw-r--r--etc/inc/pfsense-utils.inc62
-rw-r--r--etc/inc/pkg-utils.inc61
-rwxr-xr-xusr/local/www/system_firmware_auto.php43
3 files changed, 65 insertions, 101 deletions
diff --git a/etc/inc/pfsense-utils.inc b/etc/inc/pfsense-utils.inc
index 5fcf7e9..d891a41 100644
--- a/etc/inc/pfsense-utils.inc
+++ b/etc/inc/pfsense-utils.inc
@@ -4074,4 +4074,66 @@ $span_begin = "";
return $descriptions;
}
}
+function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
+ global $ch, $fout, $file_size, $downloaded;
+ $file_size = 1;
+ $downloaded = 1;
+ /* open destination file */
+ $fout = fopen($destination_file, "wb");
+
+ /*
+ * Originally by Author: Keyvan Minoukadeh
+ * Modified by Scott Ullrich to return Content-Length size
+ */
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url_file);
+ curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
+ curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
+ curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
+
+ curl_exec($ch);
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if($fout)
+ fclose($fout);
+ curl_close($ch);
+ return ($http_code == 200) ? true : $http_code;
+}
+
+function read_header($ch, $string) {
+ global $file_size, $fout;
+ $length = strlen($string);
+ $regs = "";
+ ereg("(Content-Length:) (.*)", $string, $regs);
+ if($regs[2] <> "") {
+ $file_size = intval($regs[2]);
+ }
+ ob_flush();
+ return $length;
+}
+
+function read_body($ch, $string) {
+ global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
+ $length = strlen($string);
+ $downloaded += intval($length);
+ $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
+ $downloadProgress = 100 - $downloadProgress;
+ if($lastseen <> $downloadProgress and $downloadProgress < 101) {
+ if($sendto == "status") {
+ $tostatus = $static_status . $downloadProgress . "%";
+ update_status($tostatus);
+ } else {
+ $tooutput = $static_output . $downloadProgress . "%";
+ update_output_window($tooutput);
+ }
+ update_progress_bar($downloadProgress);
+ $lastseen = $downloadProgress;
+ }
+ if($fout)
+ fwrite($fout, $string);
+ ob_flush();
+ return $length;
+}
+
?>
diff --git a/etc/inc/pkg-utils.inc b/etc/inc/pkg-utils.inc
index d62313a..c45a0a9 100644
--- a/etc/inc/pkg-utils.inc
+++ b/etc/inc/pkg-utils.inc
@@ -372,67 +372,6 @@ function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url =
return true;
}
-function download_file_with_progress_bar($url_file, $destination_file) {
- global $ch, $fout, $file_size, $downloaded, $pkg_interface;
- $file_size = 1;
- $downloaded = 1;
- /* open destination file */
- $fout = fopen($destination_file, "wb");
-
- /*
- * Originally by Author: Keyvan Minoukadeh
- * Modified by Scott Ullrich to return Content-Length size
- */
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url_file);
- curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
- curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
- curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
-
- curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if($fout)
- fclose($fout);
- curl_close($ch);
- return ($http_code == 200) ? true : $http_code;
-}
-
-function read_header($ch, $string) {
- global $file_size, $fout;
- $length = strlen($string);
- $regs = "";
- ereg("(Content-Length:) (.*)", $string, $regs);
- if($regs[2] <> "") {
- $file_size = intval($regs[2]);
- }
- ob_flush();
- return $length;
-}
-
-function read_body($ch, $string) {
- global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $pkg_interface;
- $length = strlen($string);
- $downloaded += intval($length);
- $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
- $downloadProgress = 100 - $downloadProgress;
- if($lastseen <> $downloadProgress and $downloadProgress < 101) {
- if($sendto == "status") {
- $tostatus = $static_status . $downloadProgress . "%";
- update_status($tostatus);
- } else {
- $tooutput = $static_output . $downloadProgress . "%";
- update_output_window($tooutput);
- }
- update_progress_bar($downloadProgress);
- $lastseen = $downloadProgress;
- }
- if($fout)
- fwrite($fout, $string);
- ob_flush();
- return $length;
-}
-
function install_package($package, $pkg_info = "") {
global $g, $config, $pkg_interface, $fd_log, $static_output, $pkg_interface, $restart_sync;
if($pkg_interface == "console")
diff --git a/usr/local/www/system_firmware_auto.php b/usr/local/www/system_firmware_auto.php
index 87d8650..426232f 100755
--- a/usr/local/www/system_firmware_auto.php
+++ b/usr/local/www/system_firmware_auto.php
@@ -122,7 +122,7 @@ if($current_installed_pfsense_version <> $latest_version)
if($needs_system_upgrade == true) {
update_status("Downloading updates ...");
- $status = download_file_with_progress_bar("{$updater_url}/latest.tgz", "/tmp/latest.tgz");
+ $status = download_file_with_progress_bar("{$updater_url}/latest.tgz", "/tmp/latest.tgz", "read_body_firmware");
$status = download_file_with_progress_bar("{$updater_url}/latest.tgz.sha256", "/tmp/latest.tgz.sha256");
update_output_window("{$g['product_name']} download complete.");
}
@@ -176,44 +176,7 @@ if($downloaded_latest_tgz_sha256 <> $upgrade_latest_tgz_sha256) {
Helper functions
*/
-function download_file_with_progress_bar($url_file, $destination_file) {
- global $ch, $fout, $file_size, $downloaded, $counter;
- $file_size = 1;
- $downloaded = 1;
- /* open destination file */
- $fout = fopen($destination_file, "wb");
-
- /*
- Originally by Author: Keyvan Minoukadeh
- Modified by Scott Ullrich to return Content-Length size
- */
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url_file);
- curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
- curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
- curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
-
- curl_exec($ch);
- fclose($fout);
- return 1;
-
- if ($error = curl_error($ch)) {
- return -1;
- }
-}
-
-function read_header($ch, $string) {
- global $file_size, $ch, $fout;
- $length = strlen($string);
- ereg("(Content-Length:) (.*)", $string, $regs);
- if($regs[2] <> "") {
- $file_size = intval($regs[2]);
- }
- return $length;
-}
-
-function read_body($ch, $string) {
+function read_body_firmware($ch, $string) {
global $fout, $file_size, $downloaded, $counter, $version, $latest_version, $current_installed_pfsense_version;
$length = strlen($string);
$downloaded += intval($length);
@@ -240,4 +203,4 @@ function read_body($ch, $string) {
return $length;
}
-?> \ No newline at end of file
+?>
OpenPOWER on IntegriCloud