PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Guess.php

Documentation is available at Guess.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Guess.php,v 1.13.4.1 2004/10/19 04:15:56 cellog Exp $
  21.  
  22. // {{{ uname examples
  23.  
  24. // php_uname() without args returns the same as 'uname -a', or a PHP-custom
  25. // string for Windows.
  26. // PHP versions prior to 4.3 return the uname of the host where PHP was built,
  27. // as of 4.3 it returns the uname of the host running the PHP code.
  28. //
  29. // PC RedHat Linux 7.1:
  30. // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown
  31. //
  32. // PC Debian Potato:
  33. // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown
  34. //
  35. // PC FreeBSD 3.3:
  36. // 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
  37. //
  38. // PC FreeBSD 4.3:
  39. // 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
  40. //
  41. // PC FreeBSD 4.5:
  42. // 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
  43. //
  44. // PC FreeBSD 4.5 w/uname from GNU shellutils:
  45. // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb  i386 unknown
  46. //
  47. // HP 9000/712 HP-UX 10:
  48. // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license
  49. //
  50. // HP 9000/712 HP-UX 10 w/uname from GNU shellutils:
  51. // HP-UX host B.10.10 A 9000/712 unknown
  52. //
  53. // IBM RS6000/550 AIX 4.3:
  54. // AIX host 3 4 000003531C00
  55. //
  56. // AIX 4.3 w/uname from GNU shellutils:
  57. // AIX host 3 4 000003531C00 unknown
  58. //
  59. // SGI Onyx IRIX 6.5 w/uname from GNU shellutils:
  60. // IRIX64 host 6.5 01091820 IP19 mips
  61. //
  62. // SGI Onyx IRIX 6.5:
  63. // IRIX64 host 6.5 01091820 IP19
  64. //
  65. // SparcStation 20 Solaris 8 w/uname from GNU shellutils:
  66. // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc
  67. //
  68. // SparcStation 20 Solaris 8:
  69. // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20
  70. //
  71. // Mac OS X (Darwin)
  72. // 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
  73. //
  74. // Mac OS X early versions
  75. // 
  76.  
  77. // }}}
  78.  
  79. /* TODO:
  80.  * - define endianness, to allow matchSignature("bigend") etc.
  81.  */
  82.  
  83. class OS_Guess
  84. {
  85.     var $sysname;
  86.     var $nodename;
  87.     var $cpu;
  88.     var $release;
  89.     var $extra;
  90.  
  91.     function OS_Guess($uname = null)
  92.     {
  93.         list($this->sysname,
  94.              $this->release,
  95.              $this->cpu,
  96.              $this->extra,
  97.              $this->nodename$this->parseSignature($uname);
  98.     }
  99.  
  100.     function parseSignature($uname = null)
  101.     {
  102.         static $sysmap = array(
  103.             'HP-UX' => 'hpux',
  104.             'IRIX64' => 'irix',
  105.         );
  106.         static $cpumap = array(
  107.             'i586' => 'i386',
  108.             'i686' => 'i386',
  109.             'ppc' => 'powerpc',
  110.         );
  111.         if ($uname === null{
  112.             $uname = php_uname();
  113.         }
  114.         $parts = split('[[:space:]]+'trim($uname));
  115.         $n count($parts);
  116.  
  117.         $release $machine $cpu '';
  118.         $sysname $parts[0];
  119.         $nodename $parts[1];
  120.         $cpu $parts[$n-1];
  121.         $extra '';
  122.         if ($cpu == 'unknown'{
  123.             $cpu $parts[$n-2];
  124.         }
  125.  
  126.         switch ($sysname{
  127.             case 'AIX':
  128.                 $release = "$parts[3].$parts[2]";
  129.                 break;
  130.             case 'Windows':
  131.                 switch ($parts[1]{
  132.                     case '95/98':
  133.                         $release '9x';
  134.                         break;
  135.                     default:
  136.                         $release $parts[1];
  137.                         break;
  138.                 }
  139.                 $cpu 'i386';
  140.                 break;
  141.             case 'Linux':
  142.                 $extra $this->_detectGlibcVersion();
  143.                 // use only the first two digits from the kernel version
  144.                 $release ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*''\1'$parts[2]);
  145.                 break;
  146.             case 'Mac' :
  147.                 $sysname 'darwin';
  148.                 $nodename $parts[2];
  149.                 $release $parts[3];
  150.                 if ($cpu == 'Macintosh'{
  151.                     if ($parts[$n - 2== 'Power'{
  152.                         $cpu 'powerpc';
  153.                     }
  154.                 }
  155.                 break;
  156.             case 'Darwin' :
  157.                 if ($cpu == 'Macintosh'{
  158.                     if ($parts[$n - 2== 'Power'{
  159.                         $cpu 'powerpc';
  160.                     }
  161.                 }
  162.                 $release ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*''\1'$parts[2]);
  163.                 break;
  164.             default:
  165.                 $release ereg_replace('-.*'''$parts[2]);
  166.                 break;
  167.         }
  168.  
  169.  
  170.         if (isset($sysmap[$sysname])) {
  171.             $sysname $sysmap[$sysname];
  172.         else {
  173.             $sysname strtolower($sysname);
  174.         }
  175.         if (isset($cpumap[$cpu])) {
  176.             $cpu $cpumap[$cpu];
  177.         }
  178.         return array($sysname$release$cpu$extra$nodename);
  179.     }
  180.  
  181.     function _detectGlibcVersion()
  182.     {
  183.         // Use glibc's <features.h> header file to
  184.         // get major and minor version number:
  185.         include_once "System.php";
  186.         $tmpfile System::mktemp("glibctest");
  187.         $fp fopen($tmpfile"w");
  188.         fwrite($fp"#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
  189.         fclose($fp);
  190.         $cpp popen("/usr/bin/cpp $tmpfile""r");
  191.         $major $minor = 0;
  192.         while ($line fgets($cpp1024)) {
  193.             if ($line{0== '#' || trim($line== ''{
  194.                 continue;
  195.             }
  196.             if (list($major$minorexplode(' 'trim($line))) {
  197.                 break;
  198.             }
  199.         }
  200.         pclose($cpp);
  201.         unlink($tmpfile);
  202.         if (!($major && $minor&& is_link('/lib/libc.so.6')) {
  203.             // Let's try reading the libc.so.6 symlink
  204.             if (ereg('^libc-([.*])\.so$'basename(readlink('/lib/libc.so.6'))$matches)) {
  205.                 list($major$minorexplode('.'$matches);
  206.             }
  207.         }
  208.         if (!($major && $minor)) {
  209.             return '';
  210.         }
  211.         return "glibc{$major}.{$minor}";
  212.     }
  213.  
  214.     function getSignature()
  215.     {
  216.         if (empty($this->extra)) {
  217.             return "{$this->sysname}-{$this->release}-{$this->cpu}";
  218.         }
  219.         return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}";
  220.     }
  221.  
  222.     function getSysname()
  223.     {
  224.         return $this->sysname;
  225.     }
  226.  
  227.     function getNodename()
  228.     {
  229.         return $this->nodename;
  230.     }
  231.  
  232.     function getCpu()
  233.     {
  234.         return $this->cpu;
  235.     }
  236.  
  237.     function getRelease()
  238.     {
  239.         return $this->release;
  240.     }
  241.  
  242.     function getExtra()
  243.     {
  244.         return $this->extra;
  245.     }
  246.  
  247.     function matchSignature($match)
  248.     {
  249.         if (is_array($match)) {
  250.             $fragments = $match;
  251.         } else {
  252.             $fragments = explode('-', $match);
  253.         }
  254.         $n = count($fragments);
  255.         $matches = 0;
  256.         if ($n > 0) {
  257.             $matches += $this->_matchFragment($fragments[0]$this->sysname);
  258.         }
  259.         if ($n > 1) {
  260.             $matches += $this->_matchFragment($fragments[1]$this->release);
  261.         }
  262.         if ($n > 2) {
  263.             $matches += $this->_matchFragment($fragments[2]$this->cpu);
  264.         }
  265.         if ($n > 3) {
  266.             $matches += $this->_matchFragment($fragments[3]$this->extra);
  267.         }
  268.         return ($matches == $n);
  269.     }
  270.  
  271.     function _matchFragment($fragment, $value)
  272.     {
  273.         if (strcspn($fragment, '*?') < strlen($fragment)) {
  274.             $reg = '^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '$';
  275.             return eregi($reg, $value);
  276.         }
  277.         return ($fragment == '*' || !strcasecmp($fragment, $value));
  278.     }
  279.  
  280. }
  281. /*
  282.  * Local Variables:
  283.  * indent-tabs-mode: nil
  284.  * c-basic-offset: 4
  285.  * End:
  286.  */

Documentation generated on Mon, 11 Mar 2019 14:23:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.