diff options
author | Stig Bakken <ssb@php.net> | 2002-05-03 13:13:51 +0000 |
---|---|---|
committer | Stig Bakken <ssb@php.net> | 2002-05-03 13:13:51 +0000 |
commit | bd4f1f90d0320f72fa8987b7c4508aff64b23d16 (patch) | |
tree | fcb99b49cfdf872d2901df30ae3967234ed13bb8 /pear/OS/Guess.php | |
parent | e7f7fb1781fcb260aa04be709f700126072292d1 (diff) | |
download | php-git-bd4f1f90d0320f72fa8987b7c4508aff64b23d16.tar.gz |
* switched from static to instance model
* added matching method with glob support, some examples:
$os->matchSignature('linux');
$os->matchSignature('linux-2.4*);
$os->matchSignature('linux-*-i?86');
Diffstat (limited to 'pear/OS/Guess.php')
-rw-r--r-- | pear/OS/Guess.php | 182 |
1 files changed, 133 insertions, 49 deletions
diff --git a/pear/OS/Guess.php b/pear/OS/Guess.php index 95205b7c30..a98f41df69 100644 --- a/pear/OS/Guess.php +++ b/pear/OS/Guess.php @@ -19,9 +19,67 @@ // // $Id$ +// {{{ uname examples + +// php_uname() without args returns the same as 'uname -a', or a PHP-custom +// string for Windows. +// PHP versions prior to 4.3 return the uname of the host where PHP was built, +// as of 4.3 it returns the uname of the host running the PHP code. +// +// PC RedHat Linux 7.1: +// Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown +// +// PC Debian Potato: +// Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown +// +// PC FreeBSD 3.3: +// 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 +// +// PC FreeBSD 4.3: +// 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 +// +// PC FreeBSD 4.5: +// 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 +// +// PC FreeBSD 4.5 w/uname from GNU shellutils: +// FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb i386 unknown +// +// HP 9000/712 HP-UX 10: +// HP-UX iq B.10.10 A 9000/712 2008429113 two-user license +// +// HP 9000/712 HP-UX 10 w/uname from GNU shellutils: +// HP-UX host B.10.10 A 9000/712 unknown +// +// IBM RS6000/550 AIX 4.3: +// AIX host 3 4 000003531C00 +// +// AIX 4.3 w/uname from GNU shellutils: +// AIX host 3 4 000003531C00 unknown +// +// SGI Onyx IRIX 6.5 w/uname from GNU shellutils: +// IRIX64 host 6.5 01091820 IP19 mips +// +// SGI Onyx IRIX 6.5: +// IRIX64 host 6.5 01091820 IP19 +// +// SparcStation 20 Solaris 8 w/uname from GNU shellutils: +// SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc +// +// SparcStation 20 Solaris 8: +// SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20 +// + +// }}} + class OS_Guess { - function guess() + var $sysname; + var $nodename; + var $cpu; + var $release; + var $extra; + + function OS_Guess() { static $sysmap = array( 'HP-UX' => 'hpux', @@ -36,71 +94,97 @@ class OS_Guess $parts = preg_split('/\s+/', trim($uname)); $n = count($parts); - $release = $fullversion = $machine = $processor = ''; + $this->release = $this->machine = $this->cpu = ''; - $sysname = $parts[0]; + $this->sysname = $parts[0]; $nodename = $parts[1]; - $cpu = $parts[$n-1]; - if ($cpu == 'unknown') { - $cpu = $parts[$n-2]; + $this->cpu = $parts[$n-1]; + if ($this->cpu == 'unknown') { + $this->cpu = $parts[$n-2]; } - // php_uname() function without args returns the same as 'uname -a' - // some examples: - // - // RedHat Linux 7.1: - // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown - // - // Debian Potato: - // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown - // - // FreeBSD 3.3: - // 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 - // - // FreeBSD 4.3: - // 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 - // - // FreeBSD 4.5: - // 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 - // - // FreeBSD 4.5 w/uname from GNU shellutils: - // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb i386 unknown - // - // HP-UX 10: - // HP-UX host B.10.10 A 9000/712 unknown - // - // AIX 4.3: - // AIX host 3 4 000003531C00 - // - // IRIX 6.5: - // IRIX64 host 6.5 01091820 IP19 mips - // - // Solaris 8: - // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc - // - switch ($sysname) { + switch ($this->sysname) { case 'AIX': - $release = "$parts[3].$parts[2]"; + $this->release = "$parts[3].$parts[2]"; break; case 'Windows': - $release = $parts[3]; + $this->release = $parts[3]; break; default: - $release = preg_replace('/-.*/', '', $parts[2]); + $this->release = preg_replace('/-.*/', '', $parts[2]); break; } - if (isset($sysmap[$sysname])) { - $sysname = $sysmap[$sysname]; + if (isset($sysmap[$this->sysname])) { + $this->sysname = $sysmap[$this->sysname]; } else { - $sysname = strtolower($sysname); + $this->sysname = strtolower($this->sysname); } - if (isset($cpumap[$cpu])) { - $cpu = $cpumap[$cpu]; + if (isset($cpumap[$this->cpu])) { + $this->cpu = $cpumap[$this->cpu]; } - return "$sysname-$release-$cpu"; } + + function getSignature() + { + return "{$this->sysname}-{$this->release}-{$this->cpu}"; + } + + function getSysname() + { + return $this->sysname; + } + + function getNodename() + { + return $this->nodename; + } + + function getCpu() + { + return $this->cpu; + } + + function getRelease() + { + return $this->release; + } + + function getExtra() + { + return $this->extra; + } + + function matchSignature($match) + { + $fragments = explode('-', $match); + $n = count($fragments); + $matches = 0; + if ($n > 0) { + $matches += $this->_matchFragment($fragments[0], $this->sysname); + } + if ($n > 1) { + $matches += $this->_matchFragment($fragments[1], $this->release); + } + if ($n > 2) { + $matches += $this->_matchFragment($fragments[2], $this->cpu); + } + if ($n > 3) { + $matches += $this->_matchFragment($fragments[3], $this->extra); + } + return ($matches == $n); + } + + function _matchFragment($fragment, $value) + { + if (strcspn($fragment, '*?') < strlen($fragment)) { + $preg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '$/i'; + return preg_match($preg, $value); + } + return ($fragment == '*' || !strcasecmp($fragment, $value)); + } + } /* |