9 * @author Stig Bakken <ssb@php.net>
10 * @author Gregory Beaver <cellog@php.net>
11 * @copyright 1997-2009 The Authors
12 * @license http://opensource.org/licenses/bsd-license.php New BSD License
13 * @version CVS: $Id: Guess.php 313023 2011-07-06 19:17:11Z dufuz $
14 * @link http://pear.php.net/package/PEAR
15 * @since File available since PEAR 0.1
20 // php_uname() without args returns the same as 'uname -a', or a PHP-custom
21 // string for Windows.
22 // PHP versions prior to 4.3 return the uname of the host where PHP was built,
23 // as of 4.3 it returns the uname of the host running the PHP code.
25 // PC RedHat Linux 7.1:
26 // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown
29 // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown
32 // FreeBSD host.example.com 3.3-STABLE FreeBSD 3.3-STABLE #0: Mon Feb 21 00:42:31 CET 2000 root@example.com:/usr/src/sys/compile/CONFIG i386
35 // FreeBSD host.example.com 4.3-RELEASE FreeBSD 4.3-RELEASE #1: Mon Jun 25 11:19:43 EDT 2001 root@example.com:/usr/src/sys/compile/CONFIG i386
38 // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb 6 23:59:23 CET 2002 root@example.com:/usr/src/sys/compile/CONFIG i386
40 // PC FreeBSD 4.5 w/uname from GNU shellutils:
41 // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb i386 unknown
43 // HP 9000/712 HP-UX 10:
44 // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license
46 // HP 9000/712 HP-UX 10 w/uname from GNU shellutils:
47 // HP-UX host B.10.10 A 9000/712 unknown
49 // IBM RS6000/550 AIX 4.3:
50 // AIX host 3 4 000003531C00
52 // AIX 4.3 w/uname from GNU shellutils:
53 // AIX host 3 4 000003531C00 unknown
55 // SGI Onyx IRIX 6.5 w/uname from GNU shellutils:
56 // IRIX64 host 6.5 01091820 IP19 mips
59 // IRIX64 host 6.5 01091820 IP19
61 // SparcStation 20 Solaris 8 w/uname from GNU shellutils:
62 // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc
64 // SparcStation 20 Solaris 8:
65 // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20
68 // Darwin home-eden.local 7.5.0 Darwin Kernel Version 7.5.0: Thu Aug 5 19:26:16 PDT 2004; root:xnu/xnu-517.7.21.obj~3/RELEASE_PPC Power Macintosh
70 // Mac OS X early versions
76 * - define endianness, to allow matchSignature("bigend") etc.
80 * Retrieves information about the current operating system
82 * This class uses php_uname() to grok information about the current OS
86 * @author Stig Bakken <ssb@php.net>
87 * @author Gregory Beaver <cellog@php.net>
88 * @copyright 1997-2009 The Authors
89 * @license http://opensource.org/licenses/bsd-license.php New BSD License
90 * @version Release: 1.9.4
91 * @link http://pear.php.net/package/PEAR
92 * @since Class available since Release 0.1
102 function OS_Guess($uname = null)
108 $this->nodename) = $this->parseSignature($uname);
111 function parseSignature($uname = null)
113 static $sysmap = array(
117 static $cpumap = array(
122 if ($uname === null) {
123 $uname = php_uname();
125 $parts = preg_split('/\s+/', trim($uname));
128 $release = $machine = $cpu = '';
129 $sysname = $parts[0];
130 $nodename = $parts[1];
133 if ($cpu == 'unknown') {
134 $cpu = $parts[$n - 2];
139 $release = "$parts[3].$parts[2]";
147 $release = $parts[1];
153 $extra = $this->_detectGlibcVersion();
154 // use only the first two digits from the kernel version
155 $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
159 $nodename = $parts[2];
160 $release = $parts[3];
161 if ($cpu == 'Macintosh') {
162 if ($parts[$n - 2] == 'Power') {
168 if ($cpu == 'Macintosh') {
169 if ($parts[$n - 2] == 'Power') {
173 $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
176 $release = preg_replace('/-.*/', '', $parts[2]);
180 if (isset($sysmap[$sysname])) {
181 $sysname = $sysmap[$sysname];
183 $sysname = strtolower($sysname);
185 if (isset($cpumap[$cpu])) {
186 $cpu = $cpumap[$cpu];
188 return array($sysname, $release, $cpu, $extra, $nodename);
191 function _detectGlibcVersion()
193 static $glibc = false;
194 if ($glibc !== false) {
195 return $glibc; // no need to run this multiple times
198 include_once "System.php";
199 // Use glibc's <features.h> header file to
200 // get major and minor version number:
201 if (@file_exists('/usr/include/features.h') &&
202 @is_readable('/usr/include/features.h')) {
203 if (!@file_exists('/usr/bin/cpp') || !@is_executable('/usr/bin/cpp')) {
204 $features_file = fopen('/usr/include/features.h', 'rb');
205 while (!feof($features_file)) {
206 $line = fgets($features_file, 8192);
207 if (!$line || (strpos($line, '#define') === false)) {
210 if (strpos($line, '__GLIBC__')) {
211 // major version number #define __GLIBC__ version
212 $line = preg_split('/\s+/', $line);
213 $glibc_major = trim($line[2]);
214 if (isset($glibc_minor)) {
220 if (strpos($line, '__GLIBC_MINOR__')) {
221 // got the minor version number
222 // #define __GLIBC_MINOR__ version
223 $line = preg_split('/\s+/', $line);
224 $glibc_minor = trim($line[2]);
225 if (isset($glibc_major)) {
231 fclose($features_file);
232 if (!isset($glibc_major) || !isset($glibc_minor)) {
235 return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ;
238 $tmpfile = System::mktemp("glibctest");
239 $fp = fopen($tmpfile, "w");
240 fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
242 $cpp = popen("/usr/bin/cpp $tmpfile", "r");
243 while ($line = fgets($cpp, 1024)) {
244 if ($line{0} == '#' || trim($line) == '') {
248 if (list($major, $minor) = explode(' ', trim($line))) {
256 if (!($major && $minor) && @is_link('/lib/libc.so.6')) {
257 // Let's try reading the libc.so.6 symlink
258 if (preg_match('/^libc-(.*)\.so$/', basename(readlink('/lib/libc.so.6')), $matches)) {
259 list($major, $minor) = explode('.', $matches[1]);
263 if (!($major && $minor)) {
267 return $glibc = "glibc{$major}.{$minor}";
270 function getSignature()
272 if (empty($this->extra)) {
273 return "{$this->sysname}-{$this->release}-{$this->cpu}";
275 return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}";
278 function getSysname()
280 return $this->sysname;
283 function getNodename()
285 return $this->nodename;
293 function getRelease()
295 return $this->release;
303 function matchSignature($match)
305 $fragments = is_array($match) ? $match : explode('-', $match);
306 $n = count($fragments);
309 $matches += $this->_matchFragment($fragments[0], $this->sysname);
312 $matches += $this->_matchFragment($fragments[1], $this->release);
315 $matches += $this->_matchFragment($fragments[2], $this->cpu);
318 $matches += $this->_matchFragment($fragments[3], $this->extra);
320 return ($matches == $n);
323 function _matchFragment($fragment, $value)
325 if (strcspn($fragment, '*?') < strlen($fragment)) {
326 $reg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '\\z/';
327 return preg_match($reg, $value);
329 return ($fragment == '*' || !strcasecmp($fragment, $value));
335 * indent-tabs-mode: nil