b2evolution PHP Cross Reference Blogging Systems

Source: /inc/tools/model/_system.funcs.php - 516 lines - 12420 bytes - Summary - Text - Print

Description: This file implements the system diagnostics support functions. b2evolution - {@link http://b2evolution.net/} Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   1  <?php
   2  /**
   3   * This file implements the system diagnostics support functions.
   4   *
   5   * b2evolution - {@link http://b2evolution.net/}
   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal Open Source relicensing agreement:
  11   * }}
  12   *
  13   * @package admin
  14   *
  15   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  16   * @author fplanque: Francois PLANQUE.
  17   *
  18   * @version $Id: _system.funcs.php 6136 2014-03-08 07:59:48Z manuel $
  19   */
  20  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  21  
  22  /**
  23   * Collect system stats for display on the "About this system" page
  24   *
  25   * @return array
  26   */
  27  function get_system_stats()
  28  {
  29      global $evo_charset, $DB, $Settings, $cache_path;
  30  
  31      static $system_stats = array();
  32  
  33      if( !empty($system_stats) )
  34      {
  35          return $system_stats;
  36      }
  37  
  38      // b2evo config choices:
  39      $system_stats['mediadir_status'] = system_check_dir('media'); // If error, then the host is potentially borked
  40      $system_stats['install_removed'] = system_check_install_removed();
  41      $system_stats['evo_charset'] = $evo_charset;
  42      $system_stats['evo_blog_count'] = count( system_get_blog_IDs( false ) );
  43  
  44      // Caching:
  45      $system_stats['cachedir_status'] = system_check_dir('cache'); // If error, then the host is potentially borked
  46      $system_stats['cachedir_size'] = get_dirsize_recursive( $cache_path );
  47      $system_stats['general_pagecache_enabled'] = $Settings->get( 'general_cache_enabled' );
  48      $system_stats['blog_pagecaches_enabled'] = count( system_get_blog_IDs( true ) );
  49  
  50      // Database:
  51      $system_stats['db_version'] = $DB->get_version();    // MySQL version
  52      $system_stats['db_utf8'] = system_check_db_utf8();
  53  
  54      // PHP:
  55      list( $uid, $uname ) = system_check_process_user();
  56      $system_stats['php_uid'] = $uid;
  57      $system_stats['php_uname'] = $uname;    // Potential unsecure hosts will use names like 'nobody', 'www-data'
  58      list( $gid, $gname ) = system_check_process_group();
  59      $system_stats['php_gid'] = $gid;
  60      $system_stats['php_gname'] = $gname;    // Potential unsecure hosts will use names like 'nobody', 'www-data'
  61      $system_stats['php_version'] = PHP_VERSION;
  62      $system_stats['php_reg_globals'] = ini_get('register_globals');
  63      $system_stats['php_allow_url_include'] = ini_get('allow_url_include');
  64      $system_stats['php_allow_url_fopen'] = ini_get('allow_url_fopen');
  65      // TODO php_magic quotes
  66      $system_stats['php_upload_max'] = system_check_upload_max_filesize();
  67      $system_stats['php_post_max'] = system_check_post_max_size();
  68      $system_stats['php_memory'] = system_check_memory_limit(); // how much room does b2evo have to move?
  69      $system_stats['php_mbstring'] = extension_loaded('mbstring');
  70      $system_stats['php_xml'] = extension_loaded('xml');
  71      $system_stats['php_imap'] = extension_loaded('imap');
  72      $system_stats['php_opcode_cache'] = get_active_opcode_cache();
  73  
  74      // GD:
  75      $system_stats['gd_version'] = system_check_gd_version();
  76  
  77      return $system_stats;
  78  }
  79  
  80  
  81  /**
  82   * Check if a directory is ready for operation, i-e writable by PHP.
  83   *
  84   * @return integer result code, 0 means 'ok'
  85   */
  86  function system_check_dir( $directory = 'media', $relative_path = NULL )
  87  {
  88      global $media_path, $cache_path;
  89  
  90      switch( $directory )
  91      {
  92          case 'cache':
  93              $path = $cache_path;
  94              break;
  95  
  96          case 'media':
  97              $path = $media_path;
  98              break;
  99  
 100          default:
 101              return 1;
 102      }
 103  
 104      if( $relative_path != NULL )
 105      {
 106          $path .= $relative_path;
 107      }
 108  
 109      if( ! is_dir( $path ) )
 110      {
 111          return 2;
 112      }
 113      elseif( ! is_readable( $path ) )
 114      {
 115          return 3;
 116      }
 117      elseif( ! is_writable( $path ) )
 118      {
 119          return 4;
 120      }
 121      else
 122      {
 123          $tempfile_path = $path.'temp.tmp';
 124          if( !@touch( $tempfile_path ) || !@unlink( $tempfile_path ) )
 125          {
 126              return 5;
 127          }
 128      }
 129  
 130      if( $directory == 'cache' && $relative_path != NULL )
 131      { // Create .htaccess file with deny rules
 132          if( ! create_htaccess_deny( $cache_path ) )
 133          {
 134              return 6;
 135          }
 136      }
 137  
 138      return 0;
 139  }
 140  
 141  
 142  /**
 143   * Get corresponding status and message for the system_check_dir code.
 144   *
 145   * @param integer system_check_dir result code
 146   * @param string before message
 147   */
 148  function system_get_result( $check_dir_code, $before_msg = '' )
 149  {
 150      $status = ( $check_dir_code == 0 ) ? 'ok' : 'error';
 151      $system_results = array(
 152      // fp> note: you can add statuses but not change existing ones.
 153          0 => T_( 'OK' ),
 154          1 => T_( 'Unknown directory' ),
 155          2 => T_( 'The directory doesn\'t exist.' ),
 156          3 => T_( 'The directory is not readable.' ),
 157          4 => T_( 'The directory is not writable.' ),
 158          5 => T_( 'No permission to create/delete file in directory!' ),
 159          6 => T_( 'No permission to create .htaccess file in directory!' ) );
 160      return array( $status, $before_msg.$system_results[$check_dir_code] );
 161  }
 162  
 163  
 164  /**
 165   * Create cache/ and  /cache/plugins/ folders
 166   *
 167   * @return boolean false if cache/ folder not exists or has limited editing permission, true otherwise
 168   */
 169  function system_create_cache_folder()
 170  {
 171      global $cache_path;
 172      // create /cache folder
 173      mkdir_r( $cache_path );
 174      // check /cache folder
 175      if( system_check_dir( 'cache' ) > 0 )
 176      {
 177          return false;
 178      }
 179  
 180      // create /cache/plugins/ folder
 181      mkdir_r( $cache_path.'plugins/' );
 182      return true;
 183  }
 184  
 185  
 186  /**
 187   * Get blog ids
 188   *
 189   * @param boolean true to get only those blogs where cache is enabled
 190   * @return array blog ids
 191   */
 192  function system_get_blog_IDs( $only_cache_enabled )
 193  {
 194      global $DB;
 195      $query = 'SELECT blog_ID FROM T_blogs';
 196      if( $only_cache_enabled )
 197      {
 198          $query .= ' INNER JOIN T_coll_settings ON
 199                                          ( blog_ID = cset_coll_ID
 200                                      AND cset_name = "cache_enabled"
 201                                      AND cset_value = "1" )';
 202      }
 203      return $DB->get_col( $query );
 204  }
 205  
 206  
 207  /**
 208   * Check if the given blog cache directory is ready for operation
 209   *
 210   * @param mixed blog ID, or NULL to check the general cache
 211   * @param boolean true if function should try to repair the corresponding cache folder, false otherwise
 212   * @return mixed false if the corresponding setting is disabled, or array( status, message ).
 213   */
 214  function system_check_blog_cache( $blog_ID = NULL, $repair = false )
 215  {
 216      global $Settings;
 217      load_class( '_core/model/_pagecache.class.php', 'PageCache' );
 218  
 219      $Blog = NULL;
 220      $result = NULL;
 221      if( $blog_ID == NULL )
 222      {
 223          if( $Settings->get( 'general_cache_enabled' ) )
 224          {
 225              $result = system_check_dir( 'cache', 'general/' );
 226              $before_msg = T_( 'General cache' ).': ';
 227          }
 228      }
 229      else
 230      {
 231          $BlogCache = & get_BlogCache();
 232          $Blog = $BlogCache->get_by_ID( $blog_ID );
 233          if( $Blog->get_setting( 'cache_enabled' ) )
 234          {
 235              $result = system_check_dir( 'cache', 'c'.$blog_ID.'/' );
 236              $before_msg = sprintf( T_( '%s cache' ).': ', $Blog->get( 'shortname' ) );
 237          }
 238      }
 239  
 240      if( !isset( $result ) )
 241      {
 242          return false;
 243      }
 244  
 245      if( !$repair || ( $result == 0 ) )
 246      {
 247          return system_get_result( $result/*, $before_msg*/ );
 248      }
 249  
 250      // try to repair the corresponding cache folder
 251      $PageCache = new PageCache( $Blog );
 252      $PageCache->cache_delete();
 253      $PageCache->cache_create();
 254      return system_check_blog_cache( $blog_ID, false );
 255  }
 256  
 257  
 258  function system_check_caches( $repair = true )
 259  {
 260      global $DB;
 261  
 262      // Check cache/ folder
 263      $result = system_check_dir( 'cache' );
 264      if( $result > 0 )
 265      { // error with cache/ folder
 266          $failed = true;
 267          if( $repair && ( $result == 2 ) )
 268          { // if cache folder not exists, and should repair, then try to create it
 269              $failed = ( $failed && !system_create_cache_folder() );
 270          }
 271          if( $failed )
 272          { // could/should not repair
 273              list( $status, $message ) = system_get_result( $result, T_( 'Cache folder error' ).': ' );
 274              return array( $message );
 275          }
 276      }
 277  
 278      $error_messages = array();
 279      if( ( $result = system_check_blog_cache( NULL, $repair ) ) !== false )
 280      { // general cache folder should exists
 281          list( $status, $message ) = $result;
 282          if( $status != 'ok' )
 283          {
 284              $error_messages[] = T_( 'General cache folder error' ).': '.$message;
 285          }
 286      }
 287  
 288      $cache_enabled_blogs = system_get_blog_IDs( true );
 289      $BlogCache = & get_BlogCache();
 290      foreach( $cache_enabled_blogs as $blog_ID )
 291      { // blog's cache folder should exists
 292          if( ( $result = system_check_blog_cache( $blog_ID, $repair ) ) !== false )
 293          {
 294              list( $status, $message ) = $result;
 295              if( $status != 'ok' )
 296              {
 297                  $Blog = $BlogCache->get_by_ID( $blog_ID );
 298                  $error_messages[] = sprintf( T_( '&laquo;%s&raquo; page cache folder' ),  $Blog->get( 'shortname' ) ).': '.$message;
 299              }
 300          }
 301      }
 302  
 303      return $error_messages;
 304  }
 305  
 306  
 307  /**
 308   * Initialize cache settings and folders (during install or upgrade)
 309   */
 310  function system_init_caches()
 311  {
 312      global $cache_path, $Settings, $DB;
 313  
 314      // create /cache and /cache/plugins/ folders
 315      if( !system_create_cache_folder() )
 316      {
 317          return false;
 318      }
 319  
 320      $Settings->set( 'newblog_cache_enabled', true );
 321      set_cache_enabled( 'general_cache_enabled', true );
 322      $existing_blogs = system_get_blog_IDs( false );
 323      foreach( $existing_blogs as $blog_ID )
 324      {
 325          set_cache_enabled( 'cache_enabled', true, $blog_ID );
 326      }
 327      return true;
 328  }
 329  
 330  
 331  /**
 332   * @return boolean true if install directory has been removed
 333   */
 334  function system_check_install_removed()
 335  {
 336      global $basepath, $install_subdir;
 337      return ! is_dir( $basepath.$install_subdir );
 338  }
 339  
 340  
 341  /**
 342   * @return boolean true if DB supports UTF8
 343   */
 344  function system_check_db_utf8()
 345  {
 346      global $DB;
 347  
 348      $save_show_errors = $DB->show_errors;
 349      $save_halt_on_error = $DB->halt_on_error;
 350      $last_error = $DB->last_error;
 351      $error = $DB->error;
 352      // Blatantly ignore any error generated by SET NAMES...
 353      $DB->show_errors = false;
 354      $DB->halt_on_error = false;
 355      if( $DB->query( 'SET NAMES utf8' ) === false )
 356      {
 357          $ok = false;
 358      }
 359      else
 360      {
 361          $ok = true;
 362      }
 363      $DB->show_errors = $save_show_errors;
 364      $DB->halt_on_error = $save_halt_on_error;
 365      $DB->last_error = $last_error;
 366      $DB->error = $error;
 367  
 368      return $ok;
 369  }
 370  
 371  
 372  /**
 373   * @return array {id,name,name+id}
 374   */
 375  function system_check_process_user()
 376  {
 377      $process_uid = NULL;
 378      $process_user = NULL;
 379      if( function_exists('posix_geteuid') )
 380      {
 381          $process_uid = posix_geteuid();
 382  
 383          if( function_exists('posix_getpwuid')
 384              && ($process_user = posix_getpwuid($process_uid)) )
 385          {
 386              $process_user = $process_user['name'];
 387          }
 388  
 389          $running_as = sprintf( '%s (uid %s)',
 390              ($process_user ? $process_user : '?'), (!is_null($process_uid) ? $process_uid : '?') );
 391      }
 392      else
 393      {
 394          $running_as = '('.T_('Unknown').')';
 395      }
 396  
 397      return array( $process_uid, $process_user, $running_as );
 398  }
 399  
 400  
 401  
 402  /**
 403   * @return array {id,name,name+id}
 404   */
 405  function system_check_process_group()
 406  {
 407      $process_gid = null;
 408      $process_group = null;
 409      if( function_exists('posix_getegid') )
 410      {
 411          $process_gid = posix_getegid();
 412  
 413          if( function_exists('posix_getgrgid')
 414              && ($process_group = posix_getgrgid($process_gid)) )
 415          {
 416              $process_group = $process_group['name'];
 417          }
 418  
 419          $running_as = sprintf( '%s (gid %s)',
 420              ($process_group ? $process_group : '?'), (!is_null($process_gid) ? $process_gid : '?') );
 421      }
 422      else
 423      {
 424          $running_as = '('.T_('Unknown').')';
 425      }
 426  
 427      return array( $process_gid, $process_group, $running_as );
 428  }
 429  
 430  
 431  /**
 432   * @return integer
 433   */
 434  function system_check_upload_max_filesize()
 435  {
 436      return get_php_bytes_size( ini_get('upload_max_filesize') );
 437  }
 438  
 439  /**
 440   * @return integer
 441   */
 442  function system_check_post_max_size()
 443  {
 444      return get_php_bytes_size( ini_get('post_max_size') );
 445  }
 446  
 447  /**
 448   * @return integer
 449   */
 450  function system_check_memory_limit()
 451  {
 452      return get_php_bytes_size( ini_get('memory_limit') );
 453  }
 454  
 455  
 456  /**
 457   * @return string
 458   */
 459  function system_check_gd_version()
 460  {
 461      if( ! function_exists( 'gd_info' ) )
 462      {
 463          return NULL;
 464      }
 465  
 466      $gd_info = gd_info();
 467      $gd_version = $gd_info['GD Version'];
 468  
 469      return $gd_version;
 470  }
 471  
 472  /**
 473   * @return integer
 474   */
 475  function system_check_max_execution_time()
 476  {
 477      $max_execution_time = ini_get('max_execution_time');
 478  
 479      return $max_execution_time;
 480  }
 481  
 482  
 483  /**
 484   * Get how much bytes php ini value takes
 485   *
 486   * @param string PHP ini value,
 487   *    Examples:
 488   *         912 - 912 bytes
 489   *          4K - 4 Kilobytes
 490   *         13M - 13 Megabytes
 491   *          8G - 8 Gigabytes
 492   * @return integer Bytes
 493   */
 494  function get_php_bytes_size( $php_ini_value )
 495  {
 496      if( (string) intval( $php_ini_value ) === (string) $php_ini_value )
 497      { // Bytes
 498          return $php_ini_value;
 499      }
 500      elseif( strpos( $php_ini_value, 'K' ) !== false )
 501      { // Kilobytes
 502          return intval( $php_ini_value ) * 1024;
 503      }
 504      elseif( strpos( $php_ini_value, 'M' ) !== false  )
 505      { // Megabytes
 506          return intval( $php_ini_value ) * 1024 * 1024;
 507      }
 508      elseif( strpos( $php_ini_value, 'G' ) !== false  )
 509      { // Gigabytes
 510          return intval( $php_ini_value ) * 1024 * 1024 * 1024;
 511      }
 512  
 513      // Unknown format
 514      return $php_ini_value;
 515  }
 516  ?>

title

Description

title

Description

title

Description

title

title

Body