[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezutils/classes/ -> ezsysinfo.php (source)

   1  <?php
   2  //
   3  // Definition of eZSysInfo class
   4  //
   5  // Created on: <29-Sep-2004 12:43:57 jb>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezsysinfo.php
  30  */
  31  
  32  /*!
  33    \class eZSysInfo ezsysinfo.php
  34    \brief Provides common information on the running system
  35  
  36    The following information can be queried:
  37    - CPU Type (e.g Pentium) - cpuType()
  38    - CPU Speed (e.g 1000) - cpuSpeed()
  39    - CPU Unit (e.g. MHz) - cpuUnit()
  40    - Memory Size in bytes (e.g. 528424960) - memorySize()
  41  
  42    \code
  43    $info = new eZSysInfo();
  44    $info->scan();
  45    print( $info->cpuType() . "\n" );
  46    \endcode
  47  
  48    \note This class supports the 'attribute' system and be used directly as a template variable.
  49    \note It uses eZSys to figure out the OS type.
  50  */
  51  
  52  include_once ( 'lib/ezutils/classes/ezsys.php' );
  53  
  54  class eZSysInfo
  55  {
  56      /*!
  57       Constructor
  58      */
  59      function eZSysInfo()
  60      {
  61      }
  62  
  63      /*!
  64       \return An array with available attributes.
  65       The available attributes:
  66       - cpu_type - cpuType()
  67       - cpu_unit - cpuUnit()
  68       - cpu_speed - cpuSpeed()
  69       - memory_size - memorySize()
  70      */
  71      function attributes()
  72      {
  73          return array( 'is_valid',
  74                        'cpu_type',
  75                        'cpu_unit',
  76                        'cpu_speed',
  77                        'memory_size' );
  78      }
  79  
  80      /*!
  81       \return \c true if the attribute named \a $name exists.
  82       See attributes() for a list of available attributes.
  83      */
  84      function hasAttribute( $name )
  85      {
  86          return in_array( $name, $this->attributes() );
  87      }
  88  
  89      /*!
  90       \return The value of the attribute named \a $name, or \c null if it does not exist.
  91       See attributes() for a list of available attributes.
  92      */
  93      function &attribute( $name )
  94      {
  95          if ( $name == 'is_valid' )
  96              return $this->IsValid;
  97          else if ( $name == 'cpu_type' )
  98              return $this->CPUType;
  99          else if ( $name == 'cpu_unit' )
 100              return $this->CPUUnit;
 101          else if ( $name == 'cpu_speed' )
 102              return $this->CPUSpeed;
 103          else if ( $name == 'memory_size' )
 104              return $this->MemorySize;
 105          else
 106          {
 107              eZDebug::writeError( "Attribute '$name' does not exist", 'eZSysInfo::attribute' );
 108              $retValue = null;
 109              return $retValue;
 110          }
 111      }
 112  
 113      /*!
 114       \return \c true if the system has been scanned correctly.
 115      */
 116      function isValid()
 117      {
 118          return $this->IsValid;
 119      }
 120  
 121      /*!
 122       Contains the type of CPU, the type is taken directly from the OS
 123       and can vary a lot.
 124       \return The type as a string or \c false if no type was found.
 125      */
 126      function cpuType()
 127      {
 128          return $this->CPUType;
 129      }
 130  
 131      /*!
 132       Contains the speed of CPU, the type is taken directly from the OS
 133       and can vary a lot. The speed is just a number so use cpuUnit()
 134       to get the proper unit (e.g MHz).
 135       \return The speed as a string or \c false if no type was found.
 136      */
 137      function cpuSpeed()
 138      {
 139          return $this->CPUSpeed;
 140      }
 141  
 142      /*!
 143       Contains the amount of system memory the OS has, the value is
 144       in bytes.
 145       \return The type as a number \c false if no type was found.
 146      */
 147      function memorySize()
 148      {
 149          return $this->MemorySize;
 150      }
 151  
 152      /*!
 153       Scans the system depending on the OS and fills in the information internally.
 154       \return \c true if it was able to scan the system or \c false if it failed.
 155      */
 156      function scan()
 157      {
 158          $this->IsValid = false;
 159          $this->CPUSpeed = false;
 160          $this->CPUType = false;
 161          $this->CPUUnit = false;
 162          $this->MemorySize = false;
 163  
 164          $sys =& eZSys::instance();
 165          $osType = $sys->osType();
 166  
 167          if ( $osType == 'win32' )
 168          {
 169              // Windows (win32) is not supported yet
 170              // eZDebug::writeWarning( "System scan for Windows (win32) machines not supported yet" );
 171          }
 172          else if ( $osType == 'mac' )
 173          {
 174              // Mac means FreeBSD type of structure?
 175              $this->IsValid = $this->scanDMesg();
 176              return $this->IsValid;
 177          }
 178          else if ( $osType == 'unix' )
 179          {
 180              // Now determine specific 'Unix' type
 181              $osName = $sys->osName();
 182              if ( $osName == 'linux' )
 183              {
 184                  $this->IsValid = $this->scanProc();
 185                  return $this->IsValid;
 186              }
 187              else if ( $osName == 'freebsd' )
 188              {
 189                  $this->IsValid = $this->scanDMesg();
 190                  return $this->IsValid;
 191              }
 192              else
 193              {
 194                  // Not known so we just try the various scanners
 195                  //
 196                  // /proc for Linux systems
 197                  if ( $this->scanProc() )
 198                  {
 199                      $this->IsValid = true;
 200                      return true;
 201                  }
 202                  // dmesg.boot for FreeBSD systems
 203                  if ( $this->scanDMesg() )
 204                  {
 205                      $this->IsValid = true;
 206                      return true;
 207                  }
 208              }
 209          }
 210          return false;
 211      }
 212  
 213      /*!
 214       \private
 215       Scans the /proc/cpuinfo and /proc/meminfo files for CPU
 216       and memory information.
 217       If this files are unavailable or could not be read it will return \c false.
 218       \param $cpuinfoPath The path to the cpuinfo file, if \c false it uses '/proc/cpuinfo' which should be sufficient.
 219       \param $meminfoPath The path to the meminfo file, if \c false it uses '/proc/meminfo' which should be sufficient.
 220      */
 221      function scanProc( $cpuinfoPath = false, $meminfoPath = false )
 222      {
 223          if ( !$cpuinfoPath )
 224              $cpuinfoPath = '/proc/cpuinfo';
 225          if ( !$meminfoPath )
 226              $meminfoPath = '/proc/meminfo';
 227  
 228          if ( !file_exists( $cpuinfoPath ) )
 229              return false;
 230          if ( !file_exists( $meminfoPath ) )
 231              return false;
 232  
 233          $fileLines = file( $cpuinfoPath );
 234          foreach ( $fileLines as $line )
 235          {
 236              if ( substr( $line, 0, 7 ) == 'cpu MHz' )
 237              {
 238                  $cpu = trim( substr( $line, 11, strlen( $line ) - 11 ) );
 239                  $this->CPUSpeed = $cpu;
 240                  $this->CPUUnit = 'MHz';
 241              }
 242              if ( substr( $line, 0, 10 ) == 'model name' )
 243              {
 244                  $system = trim( substr( $line, 13, strlen( $line ) - 13 ) );
 245                  $this->CPUType = $system;
 246              }
 247              if ( $this->CPUSpeed !== false and
 248                   $this->CPUType !== false and
 249                   $this->CPUUnit !== false )
 250                  break;
 251          }
 252  
 253          $fileLines = file( $meminfoPath );
 254          foreach ( $fileLines as $line )
 255          {
 256              if ( substr( $line, 0, 8 ) == 'MemTotal' )
 257              {
 258                  $mem = trim( substr( $line, 11, strlen( $line ) - 11 ) );
 259                  $memBytes = $mem;
 260                  if ( preg_match( "#^([0-9]+) *([a-zA-Z]+)#", $mem, $matches ) )
 261                  {
 262                      $memBytes = (int)$matches[1];
 263                      $unit = strtolower( $matches[2] );
 264                      if ( $unit == 'kb' )
 265                      {
 266                          $memBytes *= 1024;
 267                      }
 268                      else if ( $unit == 'mb' )
 269                      {
 270                          $memBytes *= 1024*1024;
 271                      }
 272                      else if ( $unit == 'gb' )
 273                      {
 274                          $memBytes *= 1024*1024*1024;
 275                      }
 276                  }
 277                  else
 278                  {
 279                      $memBytes = (int)$memBytes;
 280                  }
 281                  $this->MemorySize = $memBytes;
 282              }
 283              if ( $this->MemorySize !== false )
 284                  break;
 285          }
 286  
 287          return true;
 288      }
 289  
 290      /*!
 291       \private
 292       Scans the dmesg.boot file which is created by the kernel.
 293       If this files are unavailable or could not be read it will return \c false.
 294       \param $dmesgPath The path to the dmesg file, if \c false it uses '/var/run/dmesg.boot' which should be sufficient.
 295      */
 296      function scanDMesg( $dmesgPath = false )
 297      {
 298          if ( !$dmesgPath )
 299              $dmesgPath = '/var/run/dmesg.boot';
 300          if ( !file_exists( $dmesgPath ) )
 301              return false;
 302          $fileLines = file( $dmesgPath );
 303          foreach ( $fileLines as $line )
 304          {
 305              if ( substr( $line, 0, 3 ) == 'CPU' )
 306              {
 307                  $system = trim( substr( $line, 4, strlen( $line ) - 4 ) );
 308                  $cpu = false;
 309                  $cpuunit = false;
 310                  if ( preg_match( "#^(.+)\\((.+)(MHz) +([^)]+)\\)#", $system, $matches ) )
 311                  {
 312                      $system = trim( $matches[1] ) . ' (' . trim( $matches[4] ) . ')';
 313                      $cpu = $matches[2];
 314                      $cpuunit = $matches[3];
 315                  }
 316                  $this->CPUSpeed = $cpu;
 317                  $this->CPUType = $system;
 318                  $this->CPUUnit = $cpuunit;
 319              }
 320              if ( substr( $line, 0, 11 ) == 'real memory' )
 321              {
 322                  $mem = trim( substr( $line, 12, strlen( $line ) - 12 ) );
 323                  $memBytes = $mem;
 324                  if ( preg_match( "#^= *([0-9]+)#", $mem, $matches ) )
 325                  {
 326                      $memBytes = $matches[1];
 327                  }
 328                  $memBytes = (int)$memBytes;
 329                  $this->MemorySize = $memBytes;
 330              }
 331              if ( $this->CPUSpeed !== false and
 332                   $this->CPUType !== false and
 333                   $this->CPUUnit !== false and
 334                   $this->MemorySize !== false )
 335                  break;
 336  
 337          }
 338          return true;
 339      }
 340  
 341      /// \privatesection
 342      var $IsValid = false;
 343      var $CPUSpeed = false;
 344      var $CPUType = false;
 345      var $CPUUnit = false;
 346      var $MemorySize = false;
 347  }
 348  
 349  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7