00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 include_once( 'lib/ezutils/classes/ezsys.php' );
00055
00056 class eZSysInfo
00057 {
00058
00059
00060
00061 function eZSysInfo()
00062 {
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 function attributes()
00074 {
00075 return array( 'is_valid',
00076 'cpu_type',
00077 'cpu_unit',
00078 'cpu_speed',
00079 'memory_size' );
00080 }
00081
00082
00083
00084
00085
00086 function hasAttribute( $name )
00087 {
00088 return in_array( $name, $this->attributes() );
00089 }
00090
00091
00092
00093
00094
00095 function &attribute( $name )
00096 {
00097 if ( $name == 'is_valid' )
00098 return $this->IsValid;
00099 else if ( $name == 'cpu_type' )
00100 return $this->CPUType;
00101 else if ( $name == 'cpu_unit' )
00102 return $this->CPUUnit;
00103 else if ( $name == 'cpu_speed' )
00104 return $this->CPUSpeed;
00105 else if ( $name == 'memory_size' )
00106 return $this->MemorySize;
00107 else
00108 {
00109 eZDebug::writeError( "Attribute '$name' does not exist", 'eZSysInfo::attribute' );
00110 $retValue = null;
00111 return $retValue;
00112 }
00113 }
00114
00115
00116
00117
00118 function isValid()
00119 {
00120 return $this->IsValid;
00121 }
00122
00123
00124
00125
00126
00127
00128 function cpuType()
00129 {
00130 return $this->CPUType;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 function cpuSpeed()
00140 {
00141 return $this->CPUSpeed;
00142 }
00143
00144
00145
00146
00147
00148
00149 function memorySize()
00150 {
00151 return $this->MemorySize;
00152 }
00153
00154
00155
00156
00157
00158 function scan()
00159 {
00160 $this->IsValid = false;
00161 $this->CPUSpeed = false;
00162 $this->CPUType = false;
00163 $this->CPUUnit = false;
00164 $this->MemorySize = false;
00165
00166 $sys =& eZSys::instance();
00167 $osType = $sys->osType();
00168
00169 if ( $osType == 'win32' )
00170 {
00171
00172
00173 }
00174 else if ( $osType == 'mac' )
00175 {
00176
00177 $this->IsValid = $this->scanDMesg();
00178 return $this->IsValid;
00179 }
00180 else if ( $osType == 'unix' )
00181 {
00182
00183 $osName = $sys->osName();
00184 if ( $osName == 'linux' )
00185 {
00186 $this->IsValid = $this->scanProc();
00187 return $this->IsValid;
00188 }
00189 else if ( $osName == 'freebsd' )
00190 {
00191 $this->IsValid = $this->scanDMesg();
00192 return $this->IsValid;
00193 }
00194 else
00195 {
00196
00197
00198
00199 if ( $this->scanProc() )
00200 {
00201 $this->IsValid = true;
00202 return true;
00203 }
00204
00205 if ( $this->scanDMesg() )
00206 {
00207 $this->IsValid = true;
00208 return true;
00209 }
00210 }
00211 }
00212 return false;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 function scanProc( $cpuinfoPath = false, $meminfoPath = false )
00224 {
00225 if ( !$cpuinfoPath )
00226 $cpuinfoPath = '/proc/cpuinfo';
00227 if ( !$meminfoPath )
00228 $meminfoPath = '/proc/meminfo';
00229
00230 if ( !file_exists( $cpuinfoPath ) )
00231 return false;
00232 if ( !file_exists( $meminfoPath ) )
00233 return false;
00234
00235 $fileLines = file( $cpuinfoPath );
00236 foreach ( $fileLines as $line )
00237 {
00238 if ( substr( $line, 0, 7 ) == 'cpu MHz' )
00239 {
00240 $cpu = trim( substr( $line, 11, strlen( $line ) - 11 ) );
00241 $this->CPUSpeed = $cpu;
00242 $this->CPUUnit = 'MHz';
00243 }
00244 if ( substr( $line, 0, 10 ) == 'model name' )
00245 {
00246 $system = trim( substr( $line, 13, strlen( $line ) - 13 ) );
00247 $this->CPUType = $system;
00248 }
00249 if ( $this->CPUSpeed !== false and
00250 $this->CPUType !== false and
00251 $this->CPUUnit !== false )
00252 break;
00253 }
00254
00255 $fileLines = file( $meminfoPath );
00256 foreach ( $fileLines as $line )
00257 {
00258 if ( substr( $line, 0, 8 ) == 'MemTotal' )
00259 {
00260 $mem = trim( substr( $line, 11, strlen( $line ) - 11 ) );
00261 $memBytes = $mem;
00262 if ( preg_match( "#^([0-9]+) *([a-zA-Z]+)#", $mem, $matches ) )
00263 {
00264 $memBytes = (int)$matches[1];
00265 $unit = strtolower( $matches[2] );
00266 if ( $unit == 'kb' )
00267 {
00268 $memBytes *= 1024;
00269 }
00270 else if ( $unit == 'mb' )
00271 {
00272 $memBytes *= 1024*1024;
00273 }
00274 else if ( $unit == 'gb' )
00275 {
00276 $memBytes *= 1024*1024*1024;
00277 }
00278 }
00279 else
00280 {
00281 $memBytes = (int)$memBytes;
00282 }
00283 $this->MemorySize = $memBytes;
00284 }
00285 if ( $this->MemorySize !== false )
00286 break;
00287 }
00288
00289 return true;
00290 }
00291
00292
00293
00294
00295
00296
00297
00298 function scanDMesg( $dmesgPath = false )
00299 {
00300 if ( !$dmesgPath )
00301 $dmesgPath = '/var/run/dmesg.boot';
00302 if ( !file_exists( $dmesgPath ) )
00303 return false;
00304 $fileLines = file( $dmesgPath );
00305 foreach ( $fileLines as $line )
00306 {
00307 if ( substr( $line, 0, 3 ) == 'CPU' )
00308 {
00309 $system = trim( substr( $line, 4, strlen( $line ) - 4 ) );
00310 $cpu = false;
00311 $cpuunit = false;
00312 if ( preg_match( "#^(.+)\\((.+)(MHz) +([^)]+)\\)#", $system, $matches ) )
00313 {
00314 $system = trim( $matches[1] ) . ' (' . trim( $matches[4] ) . ')';
00315 $cpu = $matches[2];
00316 $cpuunit = $matches[3];
00317 }
00318 $this->CPUSpeed = $cpu;
00319 $this->CPUType = $system;
00320 $this->CPUUnit = $cpuunit;
00321 }
00322 if ( substr( $line, 0, 11 ) == 'real memory' )
00323 {
00324 $mem = trim( substr( $line, 12, strlen( $line ) - 12 ) );
00325 $memBytes = $mem;
00326 if ( preg_match( "#^= *([0-9]+)#", $mem, $matches ) )
00327 {
00328 $memBytes = $matches[1];
00329 }
00330 $memBytes = (int)$memBytes;
00331 $this->MemorySize = $memBytes;
00332 }
00333 if ( $this->CPUSpeed !== false and
00334 $this->CPUType !== false and
00335 $this->CPUUnit !== false and
00336 $this->MemorySize !== false )
00337 break;
00338
00339 }
00340 return true;
00341 }
00342
00343
00344 var $IsValid = false;
00345 var $CPUSpeed = false;
00346 var $CPUType = false;
00347 var $CPUUnit = false;
00348 var $MemorySize = false;
00349 }
00350
00351 ?>