Chris@18: Chris@18: * @author Gregory Beaver Chris@18: * @copyright 1997-2009 The Authors Chris@18: * @license http://opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @link http://pear.php.net/package/PEAR Chris@18: * @since File available since PEAR 0.1 Chris@18: */ Chris@18: Chris@18: // {{{ uname examples Chris@18: Chris@18: // php_uname() without args returns the same as 'uname -a', or a PHP-custom Chris@18: // string for Windows. Chris@18: // PHP versions prior to 4.3 return the uname of the host where PHP was built, Chris@18: // as of 4.3 it returns the uname of the host running the PHP code. Chris@18: // Chris@18: // PC RedHat Linux 7.1: Chris@18: // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown Chris@18: // Chris@18: // PC Debian Potato: Chris@18: // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown Chris@18: // Chris@18: // PC FreeBSD 3.3: Chris@18: // 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 Chris@18: // Chris@18: // PC FreeBSD 4.3: Chris@18: // 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 Chris@18: // Chris@18: // PC FreeBSD 4.5: Chris@18: // 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 Chris@18: // Chris@18: // PC FreeBSD 4.5 w/uname from GNU shellutils: Chris@18: // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb i386 unknown Chris@18: // Chris@18: // HP 9000/712 HP-UX 10: Chris@18: // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license Chris@18: // Chris@18: // HP 9000/712 HP-UX 10 w/uname from GNU shellutils: Chris@18: // HP-UX host B.10.10 A 9000/712 unknown Chris@18: // Chris@18: // IBM RS6000/550 AIX 4.3: Chris@18: // AIX host 3 4 000003531C00 Chris@18: // Chris@18: // AIX 4.3 w/uname from GNU shellutils: Chris@18: // AIX host 3 4 000003531C00 unknown Chris@18: // Chris@18: // SGI Onyx IRIX 6.5 w/uname from GNU shellutils: Chris@18: // IRIX64 host 6.5 01091820 IP19 mips Chris@18: // Chris@18: // SGI Onyx IRIX 6.5: Chris@18: // IRIX64 host 6.5 01091820 IP19 Chris@18: // Chris@18: // SparcStation 20 Solaris 8 w/uname from GNU shellutils: Chris@18: // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc Chris@18: // Chris@18: // SparcStation 20 Solaris 8: Chris@18: // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20 Chris@18: // Chris@18: // Mac OS X (Darwin) Chris@18: // 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 Chris@18: // Chris@18: // Mac OS X early versions Chris@18: // Chris@18: Chris@18: // }}} Chris@18: Chris@18: /* TODO: Chris@18: * - define endianness, to allow matchSignature("bigend") etc. Chris@18: */ Chris@18: Chris@18: /** Chris@18: * Retrieves information about the current operating system Chris@18: * Chris@18: * This class uses php_uname() to grok information about the current OS Chris@18: * Chris@18: * @category pear Chris@18: * @package PEAR Chris@18: * @author Stig Bakken Chris@18: * @author Gregory Beaver Chris@18: * @copyright 1997-2009 The Authors Chris@18: * @license http://opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @version Release: @package_version@ Chris@18: * @link http://pear.php.net/package/PEAR Chris@18: * @since Class available since Release 0.1 Chris@18: */ Chris@18: class OS_Guess Chris@18: { Chris@18: var $sysname; Chris@18: var $nodename; Chris@18: var $cpu; Chris@18: var $release; Chris@18: var $extra; Chris@18: Chris@18: function __construct($uname = null) Chris@18: { Chris@18: list($this->sysname, Chris@18: $this->release, Chris@18: $this->cpu, Chris@18: $this->extra, Chris@18: $this->nodename) = $this->parseSignature($uname); Chris@18: } Chris@18: Chris@18: function parseSignature($uname = null) Chris@18: { Chris@18: static $sysmap = array( Chris@18: 'HP-UX' => 'hpux', Chris@18: 'IRIX64' => 'irix', Chris@18: ); Chris@18: static $cpumap = array( Chris@18: 'i586' => 'i386', Chris@18: 'i686' => 'i386', Chris@18: 'ppc' => 'powerpc', Chris@18: ); Chris@18: if ($uname === null) { Chris@18: $uname = php_uname(); Chris@18: } Chris@18: $parts = preg_split('/\s+/', trim($uname)); Chris@18: $n = count($parts); Chris@18: Chris@18: $release = $machine = $cpu = ''; Chris@18: $sysname = $parts[0]; Chris@18: $nodename = $parts[1]; Chris@18: $cpu = $parts[$n-1]; Chris@18: $extra = ''; Chris@18: if ($cpu == 'unknown') { Chris@18: $cpu = $parts[$n - 2]; Chris@18: } Chris@18: Chris@18: switch ($sysname) { Chris@18: case 'AIX' : Chris@18: $release = "$parts[3].$parts[2]"; Chris@18: break; Chris@18: case 'Windows' : Chris@18: switch ($parts[1]) { Chris@18: case '95/98': Chris@18: $release = '9x'; Chris@18: break; Chris@18: default: Chris@18: $release = $parts[1]; Chris@18: break; Chris@18: } Chris@18: $cpu = 'i386'; Chris@18: break; Chris@18: case 'Linux' : Chris@18: $extra = $this->_detectGlibcVersion(); Chris@18: // use only the first two digits from the kernel version Chris@18: $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]); Chris@18: break; Chris@18: case 'Mac' : Chris@18: $sysname = 'darwin'; Chris@18: $nodename = $parts[2]; Chris@18: $release = $parts[3]; Chris@18: if ($cpu == 'Macintosh') { Chris@18: if ($parts[$n - 2] == 'Power') { Chris@18: $cpu = 'powerpc'; Chris@18: } Chris@18: } Chris@18: break; Chris@18: case 'Darwin' : Chris@18: if ($cpu == 'Macintosh') { Chris@18: if ($parts[$n - 2] == 'Power') { Chris@18: $cpu = 'powerpc'; Chris@18: } Chris@18: } Chris@18: $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]); Chris@18: break; Chris@18: default: Chris@18: $release = preg_replace('/-.*/', '', $parts[2]); Chris@18: break; Chris@18: } Chris@18: Chris@18: if (isset($sysmap[$sysname])) { Chris@18: $sysname = $sysmap[$sysname]; Chris@18: } else { Chris@18: $sysname = strtolower($sysname); Chris@18: } Chris@18: if (isset($cpumap[$cpu])) { Chris@18: $cpu = $cpumap[$cpu]; Chris@18: } Chris@18: return array($sysname, $release, $cpu, $extra, $nodename); Chris@18: } Chris@18: Chris@18: function _detectGlibcVersion() Chris@18: { Chris@18: static $glibc = false; Chris@18: if ($glibc !== false) { Chris@18: return $glibc; // no need to run this multiple times Chris@18: } Chris@18: $major = $minor = 0; Chris@18: include_once "System.php"; Chris@18: // Use glibc's header file to Chris@18: // get major and minor version number: Chris@18: if (@file_exists('/usr/include/features.h') && Chris@18: @is_readable('/usr/include/features.h')) { Chris@18: if (!@file_exists('/usr/bin/cpp') || !@is_executable('/usr/bin/cpp')) { Chris@18: $features_file = fopen('/usr/include/features.h', 'rb'); Chris@18: while (!feof($features_file)) { Chris@18: $line = fgets($features_file, 8192); Chris@18: if (!$line || (strpos($line, '#define') === false)) { Chris@18: continue; Chris@18: } Chris@18: if (strpos($line, '__GLIBC__')) { Chris@18: // major version number #define __GLIBC__ version Chris@18: $line = preg_split('/\s+/', $line); Chris@18: $glibc_major = trim($line[2]); Chris@18: if (isset($glibc_minor)) { Chris@18: break; Chris@18: } Chris@18: continue; Chris@18: } Chris@18: Chris@18: if (strpos($line, '__GLIBC_MINOR__')) { Chris@18: // got the minor version number Chris@18: // #define __GLIBC_MINOR__ version Chris@18: $line = preg_split('/\s+/', $line); Chris@18: $glibc_minor = trim($line[2]); Chris@18: if (isset($glibc_major)) { Chris@18: break; Chris@18: } Chris@18: continue; Chris@18: } Chris@18: } Chris@18: fclose($features_file); Chris@18: if (!isset($glibc_major) || !isset($glibc_minor)) { Chris@18: return $glibc = ''; Chris@18: } Chris@18: return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ; Chris@18: } // no cpp Chris@18: Chris@18: $tmpfile = System::mktemp("glibctest"); Chris@18: $fp = fopen($tmpfile, "w"); Chris@18: fwrite($fp, "#include \n__GLIBC__ __GLIBC_MINOR__\n"); Chris@18: fclose($fp); Chris@18: $cpp = popen("/usr/bin/cpp $tmpfile", "r"); Chris@18: while ($line = fgets($cpp, 1024)) { Chris@18: if ($line{0} == '#' || trim($line) == '') { Chris@18: continue; Chris@18: } Chris@18: Chris@18: if (list($major, $minor) = explode(' ', trim($line))) { Chris@18: break; Chris@18: } Chris@18: } Chris@18: pclose($cpp); Chris@18: unlink($tmpfile); Chris@18: } // features.h Chris@18: Chris@18: if (!($major && $minor) && @is_link('/lib/libc.so.6')) { Chris@18: // Let's try reading the libc.so.6 symlink Chris@18: if (preg_match('/^libc-(.*)\.so$/', basename(readlink('/lib/libc.so.6')), $matches)) { Chris@18: list($major, $minor) = explode('.', $matches[1]); Chris@18: } Chris@18: } Chris@18: Chris@18: if (!($major && $minor)) { Chris@18: return $glibc = ''; Chris@18: } Chris@18: Chris@18: return $glibc = "glibc{$major}.{$minor}"; Chris@18: } Chris@18: Chris@18: function getSignature() Chris@18: { Chris@18: if (empty($this->extra)) { Chris@18: return "{$this->sysname}-{$this->release}-{$this->cpu}"; Chris@18: } Chris@18: return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}"; Chris@18: } Chris@18: Chris@18: function getSysname() Chris@18: { Chris@18: return $this->sysname; Chris@18: } Chris@18: Chris@18: function getNodename() Chris@18: { Chris@18: return $this->nodename; Chris@18: } Chris@18: Chris@18: function getCpu() Chris@18: { Chris@18: return $this->cpu; Chris@18: } Chris@18: Chris@18: function getRelease() Chris@18: { Chris@18: return $this->release; Chris@18: } Chris@18: Chris@18: function getExtra() Chris@18: { Chris@18: return $this->extra; Chris@18: } Chris@18: Chris@18: function matchSignature($match) Chris@18: { Chris@18: $fragments = is_array($match) ? $match : explode('-', $match); Chris@18: $n = count($fragments); Chris@18: $matches = 0; Chris@18: if ($n > 0) { Chris@18: $matches += $this->_matchFragment($fragments[0], $this->sysname); Chris@18: } Chris@18: if ($n > 1) { Chris@18: $matches += $this->_matchFragment($fragments[1], $this->release); Chris@18: } Chris@18: if ($n > 2) { Chris@18: $matches += $this->_matchFragment($fragments[2], $this->cpu); Chris@18: } Chris@18: if ($n > 3) { Chris@18: $matches += $this->_matchFragment($fragments[3], $this->extra); Chris@18: } Chris@18: return ($matches == $n); Chris@18: } Chris@18: Chris@18: function _matchFragment($fragment, $value) Chris@18: { Chris@18: if (strcspn($fragment, '*?') < strlen($fragment)) { Chris@18: $reg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '\\z/'; Chris@18: return preg_match($reg, $value); Chris@18: } Chris@18: return ($fragment == '*' || !strcasecmp($fragment, $value)); Chris@18: } Chris@18: Chris@18: } Chris@18: /* Chris@18: * Local Variables: Chris@18: * indent-tabs-mode: nil Chris@18: * c-basic-offset: 4 Chris@18: * End: Chris@18: */