annotate vendor/pear/pear-core-minimal/src/OS/Guess.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2 /**
Chris@5 3 * The OS_Guess class
Chris@5 4 *
Chris@5 5 * PHP versions 4 and 5
Chris@5 6 *
Chris@5 7 * @category pear
Chris@5 8 * @package PEAR
Chris@5 9 * @author Stig Bakken <ssb@php.net>
Chris@5 10 * @author Gregory Beaver <cellog@php.net>
Chris@5 11 * @copyright 1997-2009 The Authors
Chris@5 12 * @license http://opensource.org/licenses/bsd-license.php New BSD License
Chris@5 13 * @link http://pear.php.net/package/PEAR
Chris@5 14 * @since File available since PEAR 0.1
Chris@5 15 */
Chris@5 16
Chris@5 17 // {{{ uname examples
Chris@5 18
Chris@5 19 // php_uname() without args returns the same as 'uname -a', or a PHP-custom
Chris@5 20 // string for Windows.
Chris@5 21 // PHP versions prior to 4.3 return the uname of the host where PHP was built,
Chris@5 22 // as of 4.3 it returns the uname of the host running the PHP code.
Chris@5 23 //
Chris@5 24 // PC RedHat Linux 7.1:
Chris@5 25 // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown
Chris@5 26 //
Chris@5 27 // PC Debian Potato:
Chris@5 28 // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown
Chris@5 29 //
Chris@5 30 // PC FreeBSD 3.3:
Chris@5 31 // 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@5 32 //
Chris@5 33 // PC FreeBSD 4.3:
Chris@5 34 // 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@5 35 //
Chris@5 36 // PC FreeBSD 4.5:
Chris@5 37 // 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@5 38 //
Chris@5 39 // PC FreeBSD 4.5 w/uname from GNU shellutils:
Chris@5 40 // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb i386 unknown
Chris@5 41 //
Chris@5 42 // HP 9000/712 HP-UX 10:
Chris@5 43 // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license
Chris@5 44 //
Chris@5 45 // HP 9000/712 HP-UX 10 w/uname from GNU shellutils:
Chris@5 46 // HP-UX host B.10.10 A 9000/712 unknown
Chris@5 47 //
Chris@5 48 // IBM RS6000/550 AIX 4.3:
Chris@5 49 // AIX host 3 4 000003531C00
Chris@5 50 //
Chris@5 51 // AIX 4.3 w/uname from GNU shellutils:
Chris@5 52 // AIX host 3 4 000003531C00 unknown
Chris@5 53 //
Chris@5 54 // SGI Onyx IRIX 6.5 w/uname from GNU shellutils:
Chris@5 55 // IRIX64 host 6.5 01091820 IP19 mips
Chris@5 56 //
Chris@5 57 // SGI Onyx IRIX 6.5:
Chris@5 58 // IRIX64 host 6.5 01091820 IP19
Chris@5 59 //
Chris@5 60 // SparcStation 20 Solaris 8 w/uname from GNU shellutils:
Chris@5 61 // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc
Chris@5 62 //
Chris@5 63 // SparcStation 20 Solaris 8:
Chris@5 64 // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20
Chris@5 65 //
Chris@5 66 // Mac OS X (Darwin)
Chris@5 67 // 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@5 68 //
Chris@5 69 // Mac OS X early versions
Chris@5 70 //
Chris@5 71
Chris@5 72 // }}}
Chris@5 73
Chris@5 74 /* TODO:
Chris@5 75 * - define endianness, to allow matchSignature("bigend") etc.
Chris@5 76 */
Chris@5 77
Chris@5 78 /**
Chris@5 79 * Retrieves information about the current operating system
Chris@5 80 *
Chris@5 81 * This class uses php_uname() to grok information about the current OS
Chris@5 82 *
Chris@5 83 * @category pear
Chris@5 84 * @package PEAR
Chris@5 85 * @author Stig Bakken <ssb@php.net>
Chris@5 86 * @author Gregory Beaver <cellog@php.net>
Chris@5 87 * @copyright 1997-2009 The Authors
Chris@5 88 * @license http://opensource.org/licenses/bsd-license.php New BSD License
Chris@5 89 * @version Release: @package_version@
Chris@5 90 * @link http://pear.php.net/package/PEAR
Chris@5 91 * @since Class available since Release 0.1
Chris@5 92 */
Chris@5 93 class OS_Guess
Chris@5 94 {
Chris@5 95 var $sysname;
Chris@5 96 var $nodename;
Chris@5 97 var $cpu;
Chris@5 98 var $release;
Chris@5 99 var $extra;
Chris@5 100
Chris@5 101 function __construct($uname = null)
Chris@5 102 {
Chris@5 103 list($this->sysname,
Chris@5 104 $this->release,
Chris@5 105 $this->cpu,
Chris@5 106 $this->extra,
Chris@5 107 $this->nodename) = $this->parseSignature($uname);
Chris@5 108 }
Chris@5 109
Chris@5 110 function parseSignature($uname = null)
Chris@5 111 {
Chris@5 112 static $sysmap = array(
Chris@5 113 'HP-UX' => 'hpux',
Chris@5 114 'IRIX64' => 'irix',
Chris@5 115 );
Chris@5 116 static $cpumap = array(
Chris@5 117 'i586' => 'i386',
Chris@5 118 'i686' => 'i386',
Chris@5 119 'ppc' => 'powerpc',
Chris@5 120 );
Chris@5 121 if ($uname === null) {
Chris@5 122 $uname = php_uname();
Chris@5 123 }
Chris@5 124 $parts = preg_split('/\s+/', trim($uname));
Chris@5 125 $n = count($parts);
Chris@5 126
Chris@5 127 $release = $machine = $cpu = '';
Chris@5 128 $sysname = $parts[0];
Chris@5 129 $nodename = $parts[1];
Chris@5 130 $cpu = $parts[$n-1];
Chris@5 131 $extra = '';
Chris@5 132 if ($cpu == 'unknown') {
Chris@5 133 $cpu = $parts[$n - 2];
Chris@5 134 }
Chris@5 135
Chris@5 136 switch ($sysname) {
Chris@5 137 case 'AIX' :
Chris@5 138 $release = "$parts[3].$parts[2]";
Chris@5 139 break;
Chris@5 140 case 'Windows' :
Chris@5 141 switch ($parts[1]) {
Chris@5 142 case '95/98':
Chris@5 143 $release = '9x';
Chris@5 144 break;
Chris@5 145 default:
Chris@5 146 $release = $parts[1];
Chris@5 147 break;
Chris@5 148 }
Chris@5 149 $cpu = 'i386';
Chris@5 150 break;
Chris@5 151 case 'Linux' :
Chris@5 152 $extra = $this->_detectGlibcVersion();
Chris@5 153 // use only the first two digits from the kernel version
Chris@5 154 $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
Chris@5 155 break;
Chris@5 156 case 'Mac' :
Chris@5 157 $sysname = 'darwin';
Chris@5 158 $nodename = $parts[2];
Chris@5 159 $release = $parts[3];
Chris@5 160 if ($cpu == 'Macintosh') {
Chris@5 161 if ($parts[$n - 2] == 'Power') {
Chris@5 162 $cpu = 'powerpc';
Chris@5 163 }
Chris@5 164 }
Chris@5 165 break;
Chris@5 166 case 'Darwin' :
Chris@5 167 if ($cpu == 'Macintosh') {
Chris@5 168 if ($parts[$n - 2] == 'Power') {
Chris@5 169 $cpu = 'powerpc';
Chris@5 170 }
Chris@5 171 }
Chris@5 172 $release = preg_replace('/^([0-9]+\.[0-9]+).*/', '\1', $parts[2]);
Chris@5 173 break;
Chris@5 174 default:
Chris@5 175 $release = preg_replace('/-.*/', '', $parts[2]);
Chris@5 176 break;
Chris@5 177 }
Chris@5 178
Chris@5 179 if (isset($sysmap[$sysname])) {
Chris@5 180 $sysname = $sysmap[$sysname];
Chris@5 181 } else {
Chris@5 182 $sysname = strtolower($sysname);
Chris@5 183 }
Chris@5 184 if (isset($cpumap[$cpu])) {
Chris@5 185 $cpu = $cpumap[$cpu];
Chris@5 186 }
Chris@5 187 return array($sysname, $release, $cpu, $extra, $nodename);
Chris@5 188 }
Chris@5 189
Chris@5 190 function _detectGlibcVersion()
Chris@5 191 {
Chris@5 192 static $glibc = false;
Chris@5 193 if ($glibc !== false) {
Chris@5 194 return $glibc; // no need to run this multiple times
Chris@5 195 }
Chris@5 196 $major = $minor = 0;
Chris@5 197 include_once "System.php";
Chris@5 198 // Use glibc's <features.h> header file to
Chris@5 199 // get major and minor version number:
Chris@5 200 if (@file_exists('/usr/include/features.h') &&
Chris@5 201 @is_readable('/usr/include/features.h')) {
Chris@5 202 if (!@file_exists('/usr/bin/cpp') || !@is_executable('/usr/bin/cpp')) {
Chris@5 203 $features_file = fopen('/usr/include/features.h', 'rb');
Chris@5 204 while (!feof($features_file)) {
Chris@5 205 $line = fgets($features_file, 8192);
Chris@5 206 if (!$line || (strpos($line, '#define') === false)) {
Chris@5 207 continue;
Chris@5 208 }
Chris@5 209 if (strpos($line, '__GLIBC__')) {
Chris@5 210 // major version number #define __GLIBC__ version
Chris@5 211 $line = preg_split('/\s+/', $line);
Chris@5 212 $glibc_major = trim($line[2]);
Chris@5 213 if (isset($glibc_minor)) {
Chris@5 214 break;
Chris@5 215 }
Chris@5 216 continue;
Chris@5 217 }
Chris@5 218
Chris@5 219 if (strpos($line, '__GLIBC_MINOR__')) {
Chris@5 220 // got the minor version number
Chris@5 221 // #define __GLIBC_MINOR__ version
Chris@5 222 $line = preg_split('/\s+/', $line);
Chris@5 223 $glibc_minor = trim($line[2]);
Chris@5 224 if (isset($glibc_major)) {
Chris@5 225 break;
Chris@5 226 }
Chris@5 227 continue;
Chris@5 228 }
Chris@5 229 }
Chris@5 230 fclose($features_file);
Chris@5 231 if (!isset($glibc_major) || !isset($glibc_minor)) {
Chris@5 232 return $glibc = '';
Chris@5 233 }
Chris@5 234 return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ;
Chris@5 235 } // no cpp
Chris@5 236
Chris@5 237 $tmpfile = System::mktemp("glibctest");
Chris@5 238 $fp = fopen($tmpfile, "w");
Chris@5 239 fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
Chris@5 240 fclose($fp);
Chris@5 241 $cpp = popen("/usr/bin/cpp $tmpfile", "r");
Chris@5 242 while ($line = fgets($cpp, 1024)) {
Chris@5 243 if ($line{0} == '#' || trim($line) == '') {
Chris@5 244 continue;
Chris@5 245 }
Chris@5 246
Chris@5 247 if (list($major, $minor) = explode(' ', trim($line))) {
Chris@5 248 break;
Chris@5 249 }
Chris@5 250 }
Chris@5 251 pclose($cpp);
Chris@5 252 unlink($tmpfile);
Chris@5 253 } // features.h
Chris@5 254
Chris@5 255 if (!($major && $minor) && @is_link('/lib/libc.so.6')) {
Chris@5 256 // Let's try reading the libc.so.6 symlink
Chris@5 257 if (preg_match('/^libc-(.*)\.so$/', basename(readlink('/lib/libc.so.6')), $matches)) {
Chris@5 258 list($major, $minor) = explode('.', $matches[1]);
Chris@5 259 }
Chris@5 260 }
Chris@5 261
Chris@5 262 if (!($major && $minor)) {
Chris@5 263 return $glibc = '';
Chris@5 264 }
Chris@5 265
Chris@5 266 return $glibc = "glibc{$major}.{$minor}";
Chris@5 267 }
Chris@5 268
Chris@5 269 function getSignature()
Chris@5 270 {
Chris@5 271 if (empty($this->extra)) {
Chris@5 272 return "{$this->sysname}-{$this->release}-{$this->cpu}";
Chris@5 273 }
Chris@5 274 return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}";
Chris@5 275 }
Chris@5 276
Chris@5 277 function getSysname()
Chris@5 278 {
Chris@5 279 return $this->sysname;
Chris@5 280 }
Chris@5 281
Chris@5 282 function getNodename()
Chris@5 283 {
Chris@5 284 return $this->nodename;
Chris@5 285 }
Chris@5 286
Chris@5 287 function getCpu()
Chris@5 288 {
Chris@5 289 return $this->cpu;
Chris@5 290 }
Chris@5 291
Chris@5 292 function getRelease()
Chris@5 293 {
Chris@5 294 return $this->release;
Chris@5 295 }
Chris@5 296
Chris@5 297 function getExtra()
Chris@5 298 {
Chris@5 299 return $this->extra;
Chris@5 300 }
Chris@5 301
Chris@5 302 function matchSignature($match)
Chris@5 303 {
Chris@5 304 $fragments = is_array($match) ? $match : explode('-', $match);
Chris@5 305 $n = count($fragments);
Chris@5 306 $matches = 0;
Chris@5 307 if ($n > 0) {
Chris@5 308 $matches += $this->_matchFragment($fragments[0], $this->sysname);
Chris@5 309 }
Chris@5 310 if ($n > 1) {
Chris@5 311 $matches += $this->_matchFragment($fragments[1], $this->release);
Chris@5 312 }
Chris@5 313 if ($n > 2) {
Chris@5 314 $matches += $this->_matchFragment($fragments[2], $this->cpu);
Chris@5 315 }
Chris@5 316 if ($n > 3) {
Chris@5 317 $matches += $this->_matchFragment($fragments[3], $this->extra);
Chris@5 318 }
Chris@5 319 return ($matches == $n);
Chris@5 320 }
Chris@5 321
Chris@5 322 function _matchFragment($fragment, $value)
Chris@5 323 {
Chris@5 324 if (strcspn($fragment, '*?') < strlen($fragment)) {
Chris@5 325 $reg = '/^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '\\z/';
Chris@5 326 return preg_match($reg, $value);
Chris@5 327 }
Chris@5 328 return ($fragment == '*' || !strcasecmp($fragment, $value));
Chris@5 329 }
Chris@5 330
Chris@5 331 }
Chris@5 332 /*
Chris@5 333 * Local Variables:
Chris@5 334 * indent-tabs-mode: nil
Chris@5 335 * c-basic-offset: 4
Chris@5 336 * End:
Chris@5 337 */