Project

General

Profile

« Previous | Next » 

Revision a76c1c45

Added by Jim Pingle about 15 years ago

Beef up the version comparison code. This should let us compare several combinations of local and remote versions and properly determine if the current version is older or the same as the remote version.

View differences:

etc/inc/pfsense-utils.inc
1793 1793
	fclose($fd);
1794 1794
}
1795 1795

  
1796
function version_compare_dates($a, $b) {
1797
	$a_time = strtotime($a);
1798
	$b_time = strtotime($b);
1799

  
1800
	if ((!$a_time) || (!$b_time)) {
1801
		return FALSE;
1802
	} else {
1803
		if ($a < $b)
1804
			return -1;
1805
		elseif ($a == $b)
1806
			return 0;
1807
		else
1808
			return 1;
1809
	}
1810
}
1811
function version_get_string_value($a) {
1812
	$strs = array(
1813
		0 => "ALPHA-ALPHA",
1814
		2 => "ALPHA",
1815
		3 => "BETA",
1816
		4 => "B",
1817
		5 => "RC",
1818
		6 => "RELEASE"
1819
	);
1820
	$major = 0;
1821
	$minor = 0;
1822
	foreach ($strs as $num => $str) {
1823
		if (substr($a, 0, strlen($str)) == $str) {
1824
			$major = $num;
1825
			$n = substr($a, strlen($str));
1826
			if (is_numeric($n))
1827
				$minor = $n;
1828
			break;
1829
		}
1830
	}
1831
	return "{$major}.{$minor}";
1832
}
1833
function version_compare_string($a, $b) {
1834
	return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
1835
}
1836
function version_compare_numeric($a, $b) {
1837
	$a_arr = explode('.', rtrim($a, '.0'));
1838
	$b_arr = explode('.', rtrim($b, '.0'));
1839

  
1840
	foreach ($a_arr as $n => $val) {
1841
		if (array_key_exists($n, $b_arr)) {
1842
			// So far so good, both have values at this minor version level. Compare.
1843
			if ($val > $b_arr[$n])
1844
				return 1;
1845
			elseif ($val < $b_arr[$n])
1846
				return -1;
1847
		} else {
1848
			// a is greater, since b doesn't have any minor version here.
1849
			return 1;
1850
		}
1851
	}
1852
	if (count($b_arr) > count($a_arr)) {
1853
		// b is longer than a, so it must be greater.
1854
		return -1;
1855
	} else {
1856
		// Both a and b are of equal length and value.
1857
		return 0;
1858
	}
1859
}
1860
function pfs_version_compare($cur_time, $cur_text, $remote) {
1861
	// First try date compare
1862
	$v = version_compare_dates($cur_time, $b);
1863
	if ($v === FALSE) {
1864
		// If that fails, try to compare by string
1865
		// Before anything else, simply test if the strings are equal
1866
		if ($cur_text == $remote)
1867
			return 0;
1868
		list($cur_num, $cur_str) = explode('-', $cur_text);
1869
		list($rem_num, $rem_str) = explode('-', $remote);
1870

  
1871
		// First try to compare the numeric parts of the version string.
1872
		$v = version_compare_numeric($cur_num, $rem_num);
1873

  
1874
		// If the numeric parts are the same, compare the string parts.
1875
		if ($v == 0)
1876
			return version_compare_string($cur_str, $rem_str);
1877
	}
1878
	return $v;
1879
}
1880

  
1796 1881
?>
usr/local/www/system_firmware_check.php
119 119
else
120 120
	$updater_url = $g['update_url'];
121 121
$needs_system_upgrade = false;
122
$static_text .= "Downloading new version information...";
122 123
download_file_with_progress_bar("{$updater_url}/version", "/tmp/{$g['product_name']}_version");
123
$latest_version = str_replace("\n", "", @file_get_contents("/tmp/{$g['product_name']}_version"));
124
$static_text .= "done.\n";
125
if(!$latest_version) {
126
	$static_text .= "Unable to check for updates.\n";
124
$remote_version = trim(@file_get_contents("/tmp/{$g['product_name']}_version"));
125
$static_text .= "done.\\n";
126
if (!$remote_version) {
127
	$static_text .= "Unable to check for updates.\\n";
127 128
	if(isset($curcfg['alturl']['enable']))
128
		$static_text .= "Could not contact custom update server.\n";
129
		$static_text .= "Could not contact custom update server.\\n";
129 130
	else
130
		$static_text .= "Could not contact {$g['product_name']} update server {$updater_url}.\n";
131
		$static_text .= "Could not contact {$g['product_name']} update server {$updater_url}.\\n";
131 132
} else {
132
	$static_text .= "Downloading current version information...";
133
	$static_text .= "Obtaining current version information...";
133 134
	update_output_window($static_text);
134
	$current_installed_pfsense_version = str_replace("\n", "", file_get_contents("/etc/version.buildtime"));
135
	$current_installed_pfsense = strtotime($current_installed_pfsense_version);
136
	$latest_build_version = strtotime($latest_version);
137
	$static_text .= "done\n";
135

  
136
	$current_installed_buildtime = trim(file_get_contents("/etc/version.buildtime"));
137
	$current_installed_version = trim(file_get_contents("/etc/version"));
138

  
139
	$static_text .= "done\\n";
138 140
	update_output_window($static_text);
139
	if(!$latest_build_version) {
141

  
142
	if (pfs_version_compare($current_installed_buildtime, $current_installed_version, $remote_version) == -1) {
140 143
		$needs_system_upgrade = true;
141 144
	} else {
142
		if($current_installed_pfsense < $latest_build_version) {
143
			$needs_system_upgrade = true;
144
		} else {
145
			$static_text .= "You are on the latest version.\n";
146
		}
145
		$static_text .= "\\nYou are on the latest version.\\n";
147 146
	}
148 147
}
148

  
149 149
update_output_window($static_text);
150 150
if ($needs_system_upgrade == false) {
151 151
	require("fend.inc");
......
154 154

  
155 155
echo "\n<script>$('invokeupgrade').style.visibility = 'visible';</script>";
156 156
$txt  = "A new version is now available \\n\\n";
157
$txt .= "Current version: {$current_installed_pfsense_version}\\n";
158
$txt .= "New version:     {$latest_version}\\n\\n";
159
$txt .= "Update source:   {$updater_url}\\n";
157
$txt .= "Current version: {$current_installed_version}\\n";
158
$txt .= "       Built On: {$current_installed_buildtime}\\n";
159
$txt .= "    New version: {$remote_version}\\n\\n";
160
$txt .= "  Update source: {$updater_url}\\n";
160 161
update_output_window($txt);
161 162
?>
162 163
</form>

Also available in: Unified diff