PageRenderTime 37ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/OS/Guess.php

https://github.com/chregu/fluxcms
PHP | 343 lines | 216 code | 21 blank | 106 comment | 52 complexity | 9fd18f0d9afe7f70f8ecf4438472eaeb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * The OS_Guess class
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.0 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category pear
  14. * @package PEAR
  15. * @author Stig Bakken <ssb@php.net>
  16. * @author Gregory Beaver <cellog@php.net>
  17. * @copyright 1997-2005 The PHP Group
  18. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  19. * @version CVS: $Id$
  20. * @link http://pear.php.net/package/PEAR
  21. * @since File available since PEAR 0.1
  22. */
  23. // {{{ uname examples
  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. /* TODO:
  78. * - define endianness, to allow matchSignature("bigend") etc.
  79. */
  80. /**
  81. * Retrieves information about the current operating system
  82. *
  83. * This class uses php_uname() to grok information about the current OS
  84. *
  85. * @category pear
  86. * @package PEAR
  87. * @author Stig Bakken <ssb@php.net>
  88. * @author Gregory Beaver <cellog@php.net>
  89. * @copyright 1997-2005 The PHP Group
  90. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  91. * @version Release: 1.4.6
  92. * @link http://pear.php.net/package/PEAR
  93. * @since Class available since Release 0.1
  94. */
  95. class OS_Guess
  96. {
  97. var $sysname;
  98. var $nodename;
  99. var $cpu;
  100. var $release;
  101. var $extra;
  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. function parseSignature($uname = null)
  111. {
  112. static $sysmap = array(
  113. 'HP-UX' => 'hpux',
  114. 'IRIX64' => 'irix',
  115. );
  116. static $cpumap = array(
  117. 'i586' => 'i386',
  118. 'i686' => 'i386',
  119. 'ppc' => 'powerpc',
  120. );
  121. if ($uname === null) {
  122. $uname = php_uname();
  123. }
  124. $parts = split('[[:space:]]+', trim($uname));
  125. $n = count($parts);
  126. $release = $machine = $cpu = '';
  127. $sysname = $parts[0];
  128. $nodename = $parts[1];
  129. $cpu = $parts[$n-1];
  130. $extra = '';
  131. if ($cpu == 'unknown') {
  132. $cpu = $parts[$n-2];
  133. }
  134. switch ($sysname) {
  135. case 'AIX' :
  136. $release = "$parts[3].$parts[2]";
  137. break;
  138. case 'Windows' :
  139. switch ($parts[1]) {
  140. case '95/98':
  141. $release = '9x';
  142. break;
  143. default:
  144. $release = $parts[1];
  145. break;
  146. }
  147. $cpu = 'i386';
  148. break;
  149. case 'Linux' :
  150. $extra = $this->_detectGlibcVersion();
  151. // use only the first two digits from the kernel version
  152. $release = ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*', '\1', $parts[2]);
  153. break;
  154. case 'Mac' :
  155. $sysname = 'darwin';
  156. $nodename = $parts[2];
  157. $release = $parts[3];
  158. if ($cpu == 'Macintosh') {
  159. if ($parts[$n - 2] == 'Power') {
  160. $cpu = 'powerpc';
  161. }
  162. }
  163. break;
  164. case 'Darwin' :
  165. if ($cpu == 'Macintosh') {
  166. if ($parts[$n - 2] == 'Power') {
  167. $cpu = 'powerpc';
  168. }
  169. }
  170. $release = ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*', '\1', $parts[2]);
  171. break;
  172. default:
  173. $release = ereg_replace('-.*', '', $parts[2]);
  174. break;
  175. }
  176. if (isset($sysmap[$sysname])) {
  177. $sysname = $sysmap[$sysname];
  178. } else {
  179. $sysname = strtolower($sysname);
  180. }
  181. if (isset($cpumap[$cpu])) {
  182. $cpu = $cpumap[$cpu];
  183. }
  184. return array($sysname, $release, $cpu, $extra, $nodename);
  185. }
  186. function _detectGlibcVersion()
  187. {
  188. static $glibc = false;
  189. if ($glibc !== false) {
  190. return $glibc; // no need to run this multiple times
  191. }
  192. include_once "System.php";
  193. if (!file_exists('/usr/bin/cpp') || !is_executable('/usr/bin/cpp')) {
  194. // Use glibc's <features.h> header file to
  195. // get major and minor version number:
  196. if ($features_file = @fopen('/usr/include/features.h', 'rb') ) {
  197. while (!feof($features_file)) {
  198. $line = fgets($features_file, 8192);
  199. if (!$line || (strpos($line, '#define') === false)) {
  200. continue;
  201. }
  202. if (strpos($line, '__GLIBC__')) {
  203. // major version number #define __GLIBC__ version
  204. $line = preg_split('/\s+/', $line);
  205. $glibc_major = trim($line[2]);
  206. if (isset($glibc_minor)) {
  207. break;
  208. }
  209. continue;
  210. }
  211. if (strpos($line, '__GLIBC_MINOR__')) {
  212. // got the minor version number
  213. // #define __GLIBC_MINOR__ version
  214. $line = preg_split('/\s+/', $line);
  215. $glibc_minor = trim($line[2]);
  216. if (isset($glibc_major)) {
  217. break;
  218. }
  219. continue;
  220. }
  221. }
  222. fclose($features_file);
  223. if (!isset($glibc_major) || !isset($glibc_minor)) {
  224. return $glibc = '';
  225. }
  226. return $glibc = 'glibc' . trim($glibc_major) . "." . trim($glibc_minor) ;
  227. }
  228. return $glibc = '';
  229. }
  230. $tmpfile = System::mktemp("glibctest");
  231. $fp = fopen($tmpfile, "w");
  232. fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
  233. fclose($fp);
  234. $cpp = popen("/usr/bin/cpp $tmpfile", "r");
  235. $major = $minor = 0;
  236. while ($line = fgets($cpp, 1024)) {
  237. if ($line{0} == '#' || trim($line) == '') {
  238. continue;
  239. }
  240. if (list($major, $minor) = explode(' ', trim($line))) {
  241. break;
  242. }
  243. }
  244. pclose($cpp);
  245. unlink($tmpfile);
  246. if (!($major && $minor) && is_link('/lib/libc.so.6')) {
  247. // Let's try reading the libc.so.6 symlink
  248. if (ereg('^libc-([.*])\.so$', basename(readlink('/lib/libc.so.6')), $matches)) {
  249. list($major, $minor) = explode('.', $matches);
  250. }
  251. }
  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. if (is_array($match)) {
  287. $fragments = $match;
  288. } else {
  289. $fragments = explode('-', $match);
  290. }
  291. $n = count($fragments);
  292. $matches = 0;
  293. if ($n > 0) {
  294. $matches += $this->_matchFragment($fragments[0], $this->sysname);
  295. }
  296. if ($n > 1) {
  297. $matches += $this->_matchFragment($fragments[1], $this->release);
  298. }
  299. if ($n > 2) {
  300. $matches += $this->_matchFragment($fragments[2], $this->cpu);
  301. }
  302. if ($n > 3) {
  303. $matches += $this->_matchFragment($fragments[3], $this->extra);
  304. }
  305. return ($matches == $n);
  306. }
  307. function _matchFragment($fragment, $value)
  308. {
  309. if (strcspn($fragment, '*?') < strlen($fragment)) {
  310. $reg = '^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '$';
  311. return eregi($reg, $value);
  312. }
  313. return ($fragment == '*' || !strcasecmp($fragment, $value));
  314. }
  315. }
  316. /*
  317. * Local Variables:
  318. * indent-tabs-mode: nil
  319. * c-basic-offset: 4
  320. * End:
  321. */
  322. ?>