Line 1... |
Line 1... |
1 |
<?php |
1 |
<?php |
2 |
// +----------------------------------------------------------------------+ |
2 |
// +----------------------------------------------------------------------+ |
3 |
// | PHP Version 4 | |
3 |
// | PHP Version 4 | |
4 |
// +----------------------------------------------------------------------+ |
4 |
// +----------------------------------------------------------------------+ |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
5 |
// | Copyright (c) 1997-2004 The PHP Group | |
6 |
// +----------------------------------------------------------------------+ |
6 |
// +----------------------------------------------------------------------+ |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
7 |
// | This source file is subject to version 3.0 of the PHP license, | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
8 |
// | that is bundled with this package in the file LICENSE, and is | |
9 |
// | available at through the world-wide-web at | |
9 |
// | available at through the world-wide-web at | |
10 |
// | http://www.php.net/license/3_0.txt. | |
10 |
// | http://www.php.net/license/3_0.txt. | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
11 |
// | If you did not receive a copy of the PHP license and are unable to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
12 |
// | obtain it through the world-wide-web, please send a note to | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
13 |
// | license@php.net so we can mail you a copy immediately. | |
14 |
// +----------------------------------------------------------------------+ |
14 |
// +----------------------------------------------------------------------+ |
15 |
// | Authors: Philippe Jausions <Philippe.Jausions@11abacus.com> | |
15 |
// | Authors: Philippe Jausions <Philippe.Jausions@11abacus.com> | |
16 |
// | Aidan Lister <aidan@php.net> | |
16 |
// | Aidan Lister <aidan@php.net> | |
17 |
// +----------------------------------------------------------------------+ |
17 |
// +----------------------------------------------------------------------+ |
18 |
// |
18 |
// |
19 |
// $Id: version_compare.php,v 1.13 2005/08/01 12:21:14 aidan Exp $ |
19 |
// $Id: version_compare.php,v 1.13 2005/08/01 12:21:14 aidan Exp $ |
20 |
|
20 |
|
21 |
|
21 |
|
22 |
/** |
22 |
/** |
23 |
* Replace version_compare() |
23 |
* Replace version_compare() |
24 |
* |
24 |
* |
25 |
* @category PHP |
25 |
* @category PHP |
26 |
* @package PHP_Compat |
26 |
* @package PHP_Compat |
27 |
* @link http://php.net/function.version_compare |
27 |
* @link http://php.net/function.version_compare |
28 |
* @author Philippe Jausions <Philippe.Jausions@11abacus.com> |
28 |
* @author Philippe Jausions <Philippe.Jausions@11abacus.com> |
29 |
* @author Aidan Lister <aidan@php.net> |
29 |
* @author Aidan Lister <aidan@php.net> |
30 |
* @version $Revision: 1.13 $ |
30 |
* @version $Revision: 1.13 $ |
31 |
* @since PHP 4.1.0 |
31 |
* @since PHP 4.1.0 |
32 |
* @require PHP 4.0.5 (user_error) |
32 |
* @require PHP 4.0.5 (user_error) |
33 |
*/ |
33 |
*/ |
34 |
if (!function_exists('version_compare')) { |
34 |
if (!function_exists('version_compare')) { |
35 |
function version_compare($version1, $version2, $operator = '<') |
35 |
function version_compare($version1, $version2, $operator = '<') |
36 |
{ |
36 |
{ |
37 |
// Check input |
37 |
// Check input |
38 |
if (!is_scalar($version1)) { |
38 |
if (!is_scalar($version1)) { |
39 |
user_error('version_compare() expects parameter 1 to be string, ' . |
39 |
user_error('version_compare() expects parameter 1 to be string, ' . |
40 |
gettype($version1) . ' given', E_USER_WARNING); |
40 |
gettype($version1) . ' given', E_USER_WARNING); |
41 |
return; |
41 |
return; |
42 |
} |
42 |
} |
43 |
|
43 |
|
44 |
if (!is_scalar($version2)) { |
44 |
if (!is_scalar($version2)) { |
45 |
user_error('version_compare() expects parameter 2 to be string, ' . |
45 |
user_error('version_compare() expects parameter 2 to be string, ' . |
46 |
gettype($version2) . ' given', E_USER_WARNING); |
46 |
gettype($version2) . ' given', E_USER_WARNING); |
47 |
return; |
47 |
return; |
48 |
} |
48 |
} |
49 |
|
49 |
|
50 |
if (!is_scalar($operator)) { |
50 |
if (!is_scalar($operator)) { |
51 |
user_error('version_compare() expects parameter 3 to be string, ' . |
51 |
user_error('version_compare() expects parameter 3 to be string, ' . |
52 |
gettype($operator) . ' given', E_USER_WARNING); |
52 |
gettype($operator) . ' given', E_USER_WARNING); |
53 |
return; |
53 |
return; |
54 |
} |
54 |
} |
55 |
|
55 |
|
56 |
// Standardise versions |
56 |
// Standardise versions |
57 |
$v1 = explode('.', |
57 |
$v1 = explode('.', |
58 |
str_replace('..', '.', |
58 |
str_replace('..', '.', |
59 |
preg_replace('/([^0-9\.]+)/', '.$1.', |
59 |
preg_replace('/([^0-9\.]+)/', '.$1.', |
60 |
str_replace(array('-', '_', '+'), '.', |
60 |
str_replace(array('-', '_', '+'), '.', |
61 |
trim($version1))))); |
61 |
trim($version1))))); |
62 |
|
62 |
|
63 |
$v2 = explode('.', |
63 |
$v2 = explode('.', |
64 |
str_replace('..', '.', |
64 |
str_replace('..', '.', |
65 |
preg_replace('/([^0-9\.]+)/', '.$1.', |
65 |
preg_replace('/([^0-9\.]+)/', '.$1.', |
66 |
str_replace(array('-', '_', '+'), '.', |
66 |
str_replace(array('-', '_', '+'), '.', |
67 |
trim($version2))))); |
67 |
trim($version2))))); |
68 |
|
68 |
|
69 |
// Replace empty entries at the start of the array |
69 |
// Replace empty entries at the start of the array |
70 |
while (empty($v1[0]) && array_shift($v1)) {} |
70 |
while (empty($v1[0]) && array_shift($v1)) {} |
71 |
while (empty($v2[0]) && array_shift($v2)) {} |
71 |
while (empty($v2[0]) && array_shift($v2)) {} |
72 |
|
72 |
|
73 |
// Release state order |
73 |
// Release state order |
74 |
// '#' stands for any number |
74 |
// '#' stands for any number |
75 |
$versions = array( |
75 |
$versions = array( |
76 |
'dev' => 0, |
76 |
'dev' => 0, |
77 |
'alpha' => 1, |
77 |
'alpha' => 1, |
78 |
'a' => 1, |
78 |
'a' => 1, |
79 |
'beta' => 2, |
79 |
'beta' => 2, |
80 |
'b' => 2, |
80 |
'b' => 2, |
81 |
'RC' => 3, |
81 |
'RC' => 3, |
82 |
'#' => 4, |
82 |
'#' => 4, |
83 |
'p' => 5, |
83 |
'p' => 5, |
84 |
'pl' => 5); |
84 |
'pl' => 5); |
85 |
|
85 |
|
86 |
// Loop through each segment in the version string |
86 |
// Loop through each segment in the version string |
87 |
$compare = 0; |
87 |
$compare = 0; |
88 |
for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) { |
88 |
for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) { |
89 |
if ($v1[$i] == $v2[$i]) { |
89 |
if ($v1[$i] == $v2[$i]) { |
90 |
continue; |
90 |
continue; |
91 |
} |
91 |
} |
92 |
$i1 = $v1[$i]; |
92 |
$i1 = $v1[$i]; |
93 |
$i2 = $v2[$i]; |
93 |
$i2 = $v2[$i]; |
94 |
if (is_numeric($i1) && is_numeric($i2)) { |
94 |
if (is_numeric($i1) && is_numeric($i2)) { |
95 |
$compare = ($i1 < $i2) ? -1 : 1; |
95 |
$compare = ($i1 < $i2) ? -1 : 1; |
96 |
break; |
96 |
break; |
97 |
} |
97 |
} |
98 |
|
98 |
|
99 |
// We use the position of '#' in the versions list |
99 |
// We use the position of '#' in the versions list |
100 |
// for numbers... (so take care of # in original string) |
100 |
// for numbers... (so take care of # in original string) |
101 |
if ($i1 == '#') { |
101 |
if ($i1 == '#') { |
102 |
$i1 = ''; |
102 |
$i1 = ''; |
103 |
} elseif (is_numeric($i1)) { |
103 |
} elseif (is_numeric($i1)) { |
104 |
$i1 = '#'; |
104 |
$i1 = '#'; |
105 |
} |
105 |
} |
106 |
|
106 |
|
107 |
if ($i2 == '#') { |
107 |
if ($i2 == '#') { |
108 |
$i2 = ''; |
108 |
$i2 = ''; |
109 |
} elseif (is_numeric($i2)) { |
109 |
} elseif (is_numeric($i2)) { |
110 |
$i2 = '#'; |
110 |
$i2 = '#'; |
111 |
} |
111 |
} |
112 |
|
112 |
|
113 |
if (isset($versions[$i1]) && isset($versions[$i2])) { |
113 |
if (isset($versions[$i1]) && isset($versions[$i2])) { |
114 |
$compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1; |
114 |
$compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1; |
115 |
} elseif (isset($versions[$i1])) { |
115 |
} elseif (isset($versions[$i1])) { |
116 |
$compare = 1; |
116 |
$compare = 1; |
117 |
} elseif (isset($versions[$i2])) { |
117 |
} elseif (isset($versions[$i2])) { |
118 |
$compare = -1; |
118 |
$compare = -1; |
119 |
} else { |
119 |
} else { |
120 |
$compare = 0; |
120 |
$compare = 0; |
121 |
} |
121 |
} |
122 |
|
122 |
|
123 |
break; |
123 |
break; |
124 |
} |
124 |
} |
125 |
|
125 |
|
126 |
// If previous loop didn't find anything, compare the "extra" segments |
126 |
// If previous loop didn't find anything, compare the "extra" segments |
127 |
if ($compare == 0) { |
127 |
if ($compare == 0) { |
128 |
if (count($v2) > count($v1)) { |
128 |
if (count($v2) > count($v1)) { |
129 |
if (isset($versions[$v2[$i]])) { |
129 |
if (isset($versions[$v2[$i]])) { |
130 |
$compare = ($versions[$v2[$i]] < 4) ? 1 : -1; |
130 |
$compare = ($versions[$v2[$i]] < 4) ? 1 : -1; |
131 |
} else { |
131 |
} else { |
132 |
$compare = -1; |
132 |
$compare = -1; |
133 |
} |
133 |
} |
134 |
} elseif (count($v2) < count($v1)) { |
134 |
} elseif (count($v2) < count($v1)) { |
135 |
if (isset($versions[$v1[$i]])) { |
135 |
if (isset($versions[$v1[$i]])) { |
136 |
$compare = ($versions[$v1[$i]] < 4) ? -1 : 1; |
136 |
$compare = ($versions[$v1[$i]] < 4) ? -1 : 1; |
137 |
} else { |
137 |
} else { |
138 |
$compare = 1; |
138 |
$compare = 1; |
139 |
} |
139 |
} |
140 |
} |
140 |
} |
141 |
} |
141 |
} |
142 |
|
142 |
|
143 |
// Compare the versions |
143 |
// Compare the versions |
144 |
if (func_num_args() > 2) { |
144 |
if (func_num_args() > 2) { |
145 |
switch ($operator) { |
145 |
switch ($operator) { |
146 |
case '>': |
146 |
case '>': |
147 |
case 'gt': |
147 |
case 'gt': |
148 |
return (bool) ($compare > 0); |
148 |
return (bool) ($compare > 0); |
149 |
break; |
149 |
break; |
150 |
case '>=': |
150 |
case '>=': |
151 |
case 'ge': |
151 |
case 'ge': |
152 |
return (bool) ($compare >= 0); |
152 |
return (bool) ($compare >= 0); |
153 |
break; |
153 |
break; |
154 |
case '<=': |
154 |
case '<=': |
155 |
case 'le': |
155 |
case 'le': |
156 |
return (bool) ($compare <= 0); |
156 |
return (bool) ($compare <= 0); |
157 |
break; |
157 |
break; |
158 |
case '==': |
158 |
case '==': |
159 |
case '=': |
159 |
case '=': |
160 |
case 'eq': |
160 |
case 'eq': |
161 |
return (bool) ($compare == 0); |
161 |
return (bool) ($compare == 0); |
162 |
break; |
162 |
break; |
163 |
case '<>': |
163 |
case '<>': |
164 |
case '!=': |
164 |
case '!=': |
165 |
case 'ne': |
165 |
case 'ne': |
166 |
return (bool) ($compare != 0); |
166 |
return (bool) ($compare != 0); |
167 |
break; |
167 |
break; |
168 |
case '': |
168 |
case '': |
169 |
case '<': |
169 |
case '<': |
170 |
case 'lt': |
170 |
case 'lt': |
171 |
return (bool) ($compare < 0); |
171 |
return (bool) ($compare < 0); |
172 |
break; |
172 |
break; |
173 |
default: |
173 |
default: |
174 |
return; |
174 |
return; |
175 |
} |
175 |
} |
176 |
} |
176 |
} |
177 |
|
177 |
|
178 |
return $compare; |
178 |
return $compare; |
179 |
} |
179 |
} |
180 |
} |
180 |
} |
181 |
|
181 |
|
182 |
?> |
182 |
?> |
183 |
|
183 |
|