/lib/ezc/SystemInformation/src/readers/windows.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 235 lines · 118 code · 15 blank · 102 comment · 5 complexity · cda5aa4614f903e988d8af91990256a8 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcSystemInfoWindowsReader class
  4. *
  5. * @package SystemInformation
  6. * @version //autogen//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Provide functionality to read system information from Windows systems.
  12. *
  13. * This reader try to scan Windows system parameters on initialization and fill in
  14. * correspondent values. CPU parameters are taken from Windows registry.
  15. * Memory size received using functions in php_win32ps.dll PHP extension.
  16. *
  17. * @package SystemInformation
  18. * @version //autogentag//
  19. */
  20. class ezcSystemInfoWindowsReader extends ezcSystemInfoReader
  21. {
  22. /**
  23. * Contains true if ezcSystemInfoReader object initialized
  24. * and system info successfully taken.
  25. *
  26. * @var bool
  27. */
  28. private $isValid = false;
  29. /**
  30. * Contains string that represents reader in messages and exceptions.
  31. *
  32. * @var string
  33. */
  34. protected $readerName = 'Windows system info reader';
  35. /**
  36. * Stores properties that fetched form system once during construction.
  37. *
  38. * Read-only after initialization. If property set to true than it contains valid
  39. * value. Otherwise property is not set.
  40. *
  41. * Propertyes could be
  42. * 'cpu_count'
  43. * 'cpu_type'
  44. * 'cpu_speed'
  45. * 'memory_size'
  46. *
  47. * @var array(string)
  48. */
  49. private $validProperties = array();
  50. /**
  51. * Contains the amount of CPUs in system.
  52. *
  53. * @var int
  54. */
  55. protected $cpuCount = null;
  56. /**
  57. * Contains the strings that represent type of CPU,
  58. * for each CPU in sysytem. Type is taken directly
  59. * from the OS and can vary a lot.
  60. *
  61. * @var array(string)
  62. */
  63. protected $cpuType = null;
  64. /**
  65. * Contains the speed of each CPU in MHz.
  66. *
  67. * @var array(float)
  68. */
  69. protected $cpuSpeed = null;
  70. /**
  71. * Contains the amount of system memory the OS has, the value is in bytes.
  72. *
  73. * @var int
  74. */
  75. protected $memorySize = null;
  76. /**
  77. * Constructs ezcSystemInfoReader object and fill it with system information.
  78. *
  79. * @throws ezcSystemInfoReaderCantScanOSException
  80. * If system variables can't be received from OS.
  81. */
  82. public function __construct()
  83. {
  84. if ( !$this->getOsInfo() )
  85. {
  86. throw new ezcSystemInfoReaderCantScanOSException( "<{$this->readerName}>: can't scan OS for system values." );
  87. }
  88. }
  89. /**
  90. * Scans the OS and fills in the information internally.
  91. */
  92. private function init()
  93. {
  94. $this->getOsInfo();
  95. }
  96. /**
  97. * Returns true if the property $propertyName holds a valid value and false otherwise.
  98. *
  99. * @param string $propertyName
  100. * @return bool
  101. */
  102. public function isValid( $propertyName )
  103. {
  104. return true;
  105. }
  106. /**
  107. * Scans the OS and fills in the information internally.
  108. * Returns true if it was able to scan the system or false if it failed.
  109. *
  110. * @param string $dmesgPath path to the source of system information in OS
  111. * @return bool
  112. */
  113. private function getOsInfo()
  114. {
  115. // query contents of CentralProcessor section.
  116. $output = shell_exec( "reg query HKLM\\HARDWARE\\DESCRIPTION\\SYSTEM\\CentralProcessor" );
  117. $outputStrings = explode( "\n", $output );
  118. // In first two items of output strings we have the signature of reg.exe utility
  119. // and path to CentralProcessor section than list of subsections paths follows.
  120. // One subsection represent info for one CPU.
  121. // Name of each subsection is index of CPU starting from 0.
  122. if ( is_array( $outputStrings ) && count( $outputStrings ) > 2 )
  123. {
  124. $this->cpuCount = count( $outputStrings ) - 2; // cpuCount is amount of subsections, output header skipped.
  125. for ( $i = 0; $i < $this->cpuCount; $i++ )
  126. {
  127. $output = shell_exec( "reg query HKLM\\HARDWARE\\DESCRIPTION\\SYSTEM\\CentralProcessor\\$i /v ProcessorNameString" );
  128. preg_match( "/ProcessorNameString\s*\S*\s*(.*)/", $output, $matches );
  129. if ( isset( $matches[1] ) )
  130. {
  131. $this->cpuType[] = $matches[1];
  132. $this->validProperties['cpuType'] = $this->cpuType;
  133. }
  134. unset( $matches );
  135. $output = shell_exec( "reg query HKLM\\HARDWARE\\DESCRIPTION\\SYSTEM\\CentralProcessor\\$i /v ~MHz" );
  136. preg_match( "/~MHz\s*\S*\s*(\S*)/", $output, $matches );
  137. if ( isset( $matches[1] ) )
  138. {
  139. $this->cpuSpeed[] = (float)hexdec( $matches[1] ).'.0'; // force to be float value
  140. $this->validProperties['cpu_count'] = $this->cpuCount;
  141. $this->validProperties['cpu_speed'] = $this->cpuSpeed;
  142. }
  143. unset( $matches );
  144. }
  145. }
  146. // if no php_win32ps.dll extension installed than scanning of
  147. // Total Physical memory is not supported.
  148. // It's could be implemented on WinXP and Win2003 using call to
  149. // Windows Management Instrumentation (WMI) service like "wmic memphysical"
  150. // (should be researched in details) or with help of some free third party
  151. // utility like psinfo.exe from SysInternals ( www.sysinternals.com ).
  152. if ( ezcBaseFeatures::hasExtensionSupport( 'win32ps' ) )
  153. {
  154. $memInfo = win32_ps_stat_mem();
  155. $this->memorySize = $memInfo['total_phys'] * $memInfo['unit'];
  156. $this->validProperties['memory_size'] = $this->memorySize;
  157. }
  158. return true;
  159. }
  160. /**
  161. * Returns count of CPUs in system.
  162. *
  163. * If the CPU speed could not be read null is returned.
  164. *
  165. * @return int with count of CPUs in system or null.
  166. */
  167. public function getCpuCount()
  168. {
  169. return $this->cpuCount;
  170. }
  171. /**
  172. * Returns string with CPU speed.
  173. *
  174. * If the CPU speed could not be read null is returned.
  175. *
  176. * @return string
  177. */
  178. public function cpuSpeed()
  179. {
  180. if ( !is_array( $this->cpuSpeed ) || count( $this->cpuSpeed ) == 0 )
  181. {
  182. return null;
  183. }
  184. $result = null;
  185. foreach ( $this->cpuSpeed as $speed )
  186. {
  187. $result += $speed;
  188. }
  189. $result = $result / count( $this->cpuSpeed );
  190. return $result;
  191. }
  192. /**
  193. * Returns string with CPU type.
  194. *
  195. * If the CPU type could not be read null is returned.
  196. *
  197. * @return string
  198. */
  199. public function cpuType()
  200. {
  201. return $this->cpuType[0];
  202. }
  203. /**
  204. * Returns memory size in bytes.
  205. *
  206. * If the memory size could not be read null is returned.
  207. *
  208. * @return int
  209. */
  210. public function memorySize()
  211. {
  212. return $this->memorySize;
  213. }
  214. }
  215. ?>