Guess.php 10 KB

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