PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/check/check_integrity_inc.php

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