PEAR PHP Cross Reference Developer Tools

Source: /OS/Guess.php - 338 lines - 10652 bytes - Summary - Text - Print

Description: The OS_Guess class PHP versions 4 and 5

   1  <?php
   2  /**
   3   * The OS_Guess class
   4   *
   5   * PHP versions 4 and 5
   6   *
   7   * @category   pear
   8   * @package    PEAR
   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
  16   */
  17  
  18  // {{{ uname examples
  19  
  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.
  24  //
  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
  27  //
  28  // PC Debian Potato:
  29  // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown
  30  //
  31  // PC FreeBSD 3.3:
  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
  33  //
  34  // PC FreeBSD 4.3:
  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
  36  //
  37  // PC FreeBSD 4.5:
  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
  39  //
  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
  42  //
  43  // HP 9000/712 HP-UX 10:
  44  // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license
  45  //
  46  // HP 9000/712 HP-UX 10 w/uname from GNU shellutils:
  47  // HP-UX host B.10.10 A 9000/712 unknown
  48  //
  49  // IBM RS6000/550 AIX 4.3:
  50  // AIX host 3 4 000003531C00
  51  //
  52  // AIX 4.3 w/uname from GNU shellutils:
  53  // AIX host 3 4 000003531C00 unknown
  54  //
  55  // SGI Onyx IRIX 6.5 w/uname from GNU shellutils:
  56  // IRIX64 host 6.5 01091820 IP19 mips
  57  //
  58  // SGI Onyx IRIX 6.5:
  59  // IRIX64 host 6.5 01091820 IP19
  60  //
  61  // SparcStation 20 Solaris 8 w/uname from GNU shellutils:
  62  // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc
  63  //
  64  // SparcStation 20 Solaris 8:
  65  // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20
  66  //
  67  // Mac OS X (Darwin)
  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
  69  //
  70  // Mac OS X early versions
  71  //
  72  
  73  // }}}
  74  
  75  /* TODO:
  76   * - define endianness, to allow matchSignature("bigend") etc.
  77   */
  78  
  79  /**
  80   * Retrieves information about the current operating system
  81   *
  82   * This class uses php_uname() to grok information about the current OS
  83   *
  84   * @category   pear
  85   * @package    PEAR
  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
  93   */
  94  class OS_Guess
  95  {
  96      var $sysname;
  97      var $nodename;
  98      var $cpu;
  99      var $release;
 100      var $extra;
 101  
 102      function OS_Guess($uname = null)
 103      {
 104          list($this->sysname,
 105               $this->release,
 106               $this->cpu,
 107               $this->extra,
 108               $this->nodename) = $this->parseSignature($uname);
 109      }
 110  
 111      function parseSignature($uname = null)
 112      {
 113          static $sysmap = array(
 114              'HP-UX' => 'hpux',
 115              'IRIX64' => 'irix',
 116          );
 117          static $cpumap = array(
 118              'i586' => 'i386',
 119              'i686' => 'i386',
 120              'ppc' => 'powerpc',
 121          );
 122          if ($uname === null) {
 123              $uname = php_uname();
 124          }
 125          $parts = preg_split('/\s+/', trim($uname));
 126          $n = count($parts);
 127  
 128          $release  = $machine = $cpu = '';
 129          $sysname  = $parts[0];
 130          $nodename = $parts[1];
 131          $cpu      = $parts[$n-1];
 132          $extra = '';
 133          if ($cpu == 'unknown') {
 134              $cpu = $parts[$n - 2];
 135          }
 136  
 137          switch ($sysname) {
 138              case 'AIX' :
 139                  $release = "$parts[3].$parts[2]";
 140                  break;
 141              case 'Windows' :
 142                  switch ($parts[1]) {
 143                      case '95/98':
 144                          $release = '9x';
 145                          break;
 146                      default:
 147                          $release = $parts[1];
 148                          break;
 149                  }
 150                  $cpu = 'i386';
 151                  break;
 152              case 'Linux' :
 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]);
 156                  break;
 157              case 'Mac' :
 158                  $sysname = 'darwin';
 159                  $nodename = $parts[2];
 160                  $release = $parts[3];
 161                  if ($cpu == 'Macintosh') {
 162                      if ($parts[$n - 2] == 'Power') {
 163                          $cpu = 'powerpc';
 164                      }
 165                  }
 166                  break;
 167              case 'Darwin' :
 168                  if ($cpu == 'Macintosh') {
 169                      if ($parts[$n - 2] == 'Power') {
 170                          $cpu = 'powerpc';
 171                      }
 172                  }
 173                  $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
 174                  break;
 175              default:
 176                  $release = preg_replace('/-.*/', '', $parts[2]);
 177                  break;
 178          }
 179  
 180          if (isset($sysmap[$sysname])) {
 181              $sysname = $sysmap[$sysname];
 182          } else {
 183              $sysname = strtolower($sysname);
 184          }
 185          if (isset($cpumap[$cpu])) {
 186              $cpu = $cpumap[$cpu];
 187          }
 188          return array($sysname, $release, $cpu, $extra, $nodename);
 189      }
 190  
 191      function _detectGlibcVersion()
 192      {
 193          static $glibc = false;
 194          if ($glibc !== false) {
 195              return $glibc; // no need to run this multiple times
 196          }
 197          $major = $minor = 0;
 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)) {
 208                          continue;
 209                      }
 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)) {
 215                              break;
 216                          }
 217                          continue;
 218                      }
 219  
 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)) {
 226                              break;
 227                          }
 228                          continue;
 229                      }
 230                  }
 231                  fclose($features_file);
 232                  if (!isset($glibc_major) || !isset($glibc_minor)) {
 233                      return $glibc = '';
 234                  }
 235                  return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ;
 236              } // no cpp
 237  
 238              $tmpfile = System::mktemp("glibctest");
 239              $fp = fopen($tmpfile, "w");
 240              fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
 241              fclose($fp);
 242              $cpp = popen("/usr/bin/cpp $tmpfile", "r");
 243              while ($line = fgets($cpp, 1024)) {
 244                  if ($line{0} == '#' || trim($line) == '') {
 245                      continue;
 246                  }
 247  
 248                  if (list($major, $minor) = explode(' ', trim($line))) {
 249                      break;
 250                  }
 251              }
 252              pclose($cpp);
 253              unlink($tmpfile);
 254          } // features.h
 255  
 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]);
 260              }
 261          }
 262  
 263          if (!($major && $minor)) {
 264              return $glibc = '';
 265          }
 266  
 267          return $glibc = "glibc{$major}.{$minor}";
 268      }
 269  
 270      function getSignature()
 271      {
 272          if (empty($this->extra)) {
 273              return "{$this->sysname}-{$this->release}-{$this->cpu}";
 274          }
 275          return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}";
 276      }
 277  
 278      function getSysname()
 279      {
 280          return $this->sysname;
 281      }
 282  
 283      function getNodename()
 284      {
 285          return $this->nodename;
 286      }
 287  
 288      function getCpu()
 289      {
 290          return $this->cpu;
 291      }
 292  
 293      function getRelease()
 294      {
 295          return $this->release;
 296      }
 297  
 298      function getExtra()
 299      {
 300          return $this->extra;
 301      }
 302  
 303      function matchSignature($match)
 304      {
 305          $fragments = is_array($match) ? $match : explode('-', $match);
 306          $n = count($fragments);
 307          $matches = 0;
 308          if ($n > 0) {
 309              $matches += $this->_matchFragment($fragments[0], $this->sysname);
 310          }
 311          if ($n > 1) {
 312              $matches += $this->_matchFragment($fragments[1], $this->release);
 313          }
 314          if ($n > 2) {
 315              $matches += $this->_matchFragment($fragments[2], $this->cpu);
 316          }
 317          if ($n > 3) {
 318              $matches += $this->_matchFragment($fragments[3], $this->extra);
 319          }
 320          return ($matches == $n);
 321      }
 322  
 323      function _matchFragment($fragment, $value)
 324      {
 325          if (strcspn($fragment, '*?') < strlen($fragment)) {
 326              $reg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '\\z/';
 327              return preg_match($reg, $value);
 328          }
 329          return ($fragment == '*' || !strcasecmp($fragment, $value));
 330      }
 331  
 332  }
 333  /*
 334   * Local Variables:
 335   * indent-tabs-mode: nil
 336   * c-basic-offset: 4
 337   * End:
 338   */

title

Description

title

Description

title

Description

title

title

Body