[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/admin/check/ -> check_integrity_inc.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package MantisBT
  19   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  20   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  21   * @link http://www.mantisbt.org
  22   *
  23   * @uses check_api.php
  24   * @uses config_api.php
  25   */
  26  
  27  if ( !defined( 'CHECK_INTEGRITY_INC_ALLOW' ) ) {
  28      return;
  29  }
  30  
  31  /**
  32   * MantisBT Check API
  33   */
  34  require_once ( 'check_api.php' );
  35  require_api( 'config_api.php' );
  36  
  37  $t_this_directory = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
  38  if( file_exists( $t_this_directory . 'integrity_release_blobs.php' ) ) {
  39      require_once( $t_this_directory . 'integrity_release_blobs.php' );
  40  }
  41  if( file_exists( $t_this_directory . 'integrity_commit_blobs.php' ) ) {
  42      require_once( $t_this_directory . 'integrity_commit_blobs.php' );
  43  }
  44  
  45  function create_git_object_hash( $p_file ) {
  46      $t_hash_context = hash_init( 'sha1' );
  47      hash_update( $t_hash_context, 'blob ' . filesize( $p_file ) . "\x00" );
  48      hash_update_file( $t_hash_context, $p_file );
  49      $t_object_hash = hash_final( $t_hash_context );
  50      return $t_object_hash;
  51  }
  52  
  53  function get_release_containing_object_hash( $p_filename, $p_object_hash ) {
  54      global $g_integrity_release_blobs;
  55      if( !isset( $g_integrity_release_blobs ) ) {
  56          return null;
  57      }
  58      foreach( $g_integrity_release_blobs as $t_tag => $t_blobs ) {
  59          if( array_key_exists( $p_filename, $t_blobs ) ) {
  60              if( $t_blobs[$p_filename] == $p_object_hash ) {
  61                  return $t_tag;
  62              }
  63          }
  64      }
  65      return null;
  66  }
  67  
  68  function get_commit_containing_object_hash( $p_filename, $p_object_hash ) {
  69      global $g_integrity_commit_blobs;
  70      if( !isset( $g_integrity_commit_blobs ) ) {
  71          return null;
  72      }
  73      if( array_key_exists( $p_filename, $g_integrity_commit_blobs ) ) {
  74          $t_blobs = $g_integrity_commit_blobs[$p_filename];
  75          if( array_key_exists( $p_object_hash, $t_blobs ) ) {
  76              return $t_blobs[$p_object_hash];
  77          }
  78      }
  79      return null;
  80  }
  81  
  82  function check_file_integrity_recursive( $p_directory, $p_base_directory, $p_relative_path_prefix = '', $p_ignore_files = array() ) {
  83      global $g_integrity_blobs, $g_integrity_release_blobs;
  84      if( $t_handle = opendir( $p_directory ) ) {
  85          while( false !== ( $t_file = readdir( $t_handle ) ) ) {
  86              if( $t_file == '.' || $t_file == '..' ) {
  87                  continue;
  88              }
  89              $t_file_absolute = $p_directory . $t_file;
  90              $t_file_relative = preg_replace( '@^' . preg_quote( $p_base_directory, '@' ) . '@', '', $t_file_absolute );
  91              $t_file_relative = $p_relative_path_prefix . $t_file_relative;
  92              $t_file_relative = strtr( $t_file_relative, '\\', '/' );
  93              $t_file_relative = ltrim( $t_file_relative, '/' );
  94              if( is_dir( $t_file_absolute ) ) {
  95                  if( in_array( $t_file_relative . '/', $p_ignore_files ) ) {
  96                      continue;
  97                  }
  98                  check_file_integrity_recursive( $t_file_absolute . DIRECTORY_SEPARATOR, $p_base_directory, $p_relative_path_prefix, $p_ignore_files );
  99              } else if( is_file( $t_file_absolute ) ) {
 100                  if( in_array( $t_file_relative, $p_ignore_files ) ) {
 101                      continue;
 102                  }
 103                  $t_file_hash = create_git_object_hash( $t_file_absolute );
 104                  $t_integrity_ok = false;
 105                  $t_integrity_info = 'This file does not originate from any official MantisBT release or snapshot.';
 106                  $t_release = get_release_containing_object_hash( $t_file_relative, $t_file_hash );
 107                  if( $t_release !== null ) {
 108                      $t_integrity_ok = true;
 109                      $t_release_sanitised = htmlentities( $t_release );
 110                      $t_integrity_info = "Matches file from release <a href=\"http://git.mantisbt.org/?p=mantisbt.git;a=commit;h=release-$t_release_sanitised\">$t_release_sanitised</a>.";
 111                  } else {
 112                      $t_commit = get_commit_containing_object_hash( $t_file_relative, $t_file_hash );
 113                      if( $t_commit !== null ) {
 114                          $t_integrity_ok = true;
 115                          $t_commit_sanitised = htmlentities( $t_commit );
 116                          $t_integrity_info = "Matches file introduced or modified in commit <a href=\"http://git.mantisbt.org/?p=mantisbt.git;a=commit;h=$t_commit_sanitised\">$t_commit_sanitised</a>.";
 117                      }
 118                  }
 119                  check_print_test_warn_row(
 120                      htmlentities( $t_file_absolute ),
 121                      $t_integrity_ok,
 122                      $t_integrity_info
 123                  );
 124              }
 125          }
 126      }
 127  }
 128  
 129  check_print_section_header_row( 'Integrity' );
 130  
 131  $t_can_perform_integrity_check = isset( $g_integrity_release_blobs ) && isset( $g_integrity_commit_blobs );
 132  check_print_test_warn_row(
 133      'Reference integrity blob hashes are available for verifying the integrity of this MantisBT installation',
 134      $t_can_perform_integrity_check,
 135      array( false => 'Ensure integrity_release_blobs.php and/or integrity_commit_blobs.php are available.' )
 136  );
 137  
 138  if( !$t_can_perform_integrity_check ) {
 139      return;
 140  }
 141  
 142  $t_absolute_base_dir = realpath( config_get_global( 'absolute_path' ) ) . DIRECTORY_SEPARATOR;
 143  $t_ignore_files = array(
 144      '.git/',
 145      'admin/integrity_commit_blobs.php',
 146      'admin/integrity_release_blobs.php',
 147      'core/',
 148      'lang/',
 149      'library/',
 150      'plugins/',
 151      'config_inc.php',
 152      'custom_constants_inc.php',
 153      'custom_functions_inc.php',
 154      'custom_relationships_inc.php',
 155      'custom_strings_inc.php',
 156      'mantis_offline.php'
 157  );
 158  check_file_integrity_recursive( $t_absolute_base_dir, $t_absolute_base_dir, '', $t_ignore_files );
 159  
 160  $t_base_dir = realpath( config_get_global( 'core_path' ) ) . DIRECTORY_SEPARATOR;
 161  $t_ignore_files = array(
 162      'core/classes/'
 163  );
 164  check_file_integrity_recursive( $t_base_dir, $t_base_dir, 'core/', $t_ignore_files );
 165  
 166  $t_base_dir = realpath( config_get_global( 'class_path' ) ) . DIRECTORY_SEPARATOR;
 167  check_file_integrity_recursive( $t_base_dir, $t_base_dir, 'core/classes/' );
 168  
 169  $t_base_dir = realpath( config_get_global( 'library_path' ) ) . DIRECTORY_SEPARATOR;
 170  $t_ignore_files = array(
 171      'library/jpgraph/',
 172      'library/FirePHPCore/'
 173  );
 174  check_file_integrity_recursive( $t_base_dir, $t_base_dir, 'library/', $t_ignore_files );
 175  
 176  $t_base_dir = realpath( config_get_global( 'language_path' ) ) . DIRECTORY_SEPARATOR;
 177  check_file_integrity_recursive( $t_base_dir, $t_base_dir, 'lang/' );
 178  
 179  $t_builtin_plugins = array(
 180      'MantisCoreFormatting',
 181      'MantisGraph',
 182      'XmlImportExport'
 183  );
 184  $t_plugins_dir = $t_absolute_base_dir . 'plugins' . DIRECTORY_SEPARATOR;
 185  foreach( $t_builtin_plugins as $t_builtin_plugin) {
 186      $t_base_dir = $t_plugins_dir . $t_builtin_plugin . DIRECTORY_SEPARATOR;
 187      check_file_integrity_recursive( $t_base_dir, $t_base_dir, 'plugins/' . $t_builtin_plugin . DIRECTORY_SEPARATOR );
 188  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7