Guess.php 11 KB

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