[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \ingroup Misc 5 * 6 * This class is a bunch of helper methods that will calculate the MD5 of the files within 7 * the given list of folders, generate and write a PHP array to disk and compare a reference 8 * array with pre-calculated MD5 hashes of files against the current MD5 hashes of those files 9 * in disk. 10 * 11 * This is not a general purpose class so if you're confused about its description 12 * it probably is not what you're looking for. 13 */ 14 class IntegrityChecker 15 { 16 17 /** 18 * Given an array with folder paths, generate another associative array where the key is the path and file 19 * name and the value is the MD5 hash of the contents of the file. 20 * 21 * @param directories Array containing the paths of the directories that should be checked. If no parameter is specified 22 * it defaults to "class", "templates/admin", "templates/wizard", "templates/rss", "templates/summary", "templates/default", 23 * all of them under the PLOG_CLASS_PATH base folder 24 * @param recursive Whether to recursively process the given folders 25 * @param includeDirs Determines whether the folder names and paths should be included in the output array, defaults to 'false' 26 * @param ignore Array containing file patterns that will be ignored and not included in the output array 27 * @return An associative array 28 */ 29 function generateMD5Folder( $directories = Array( "class", "templates/admin", "templates/wizard", "templates/rss", "templates/summary", "templates/default" ), $recursive = true, $includeDirs = false, $ignore = Array( "*.svn", ".DS_Store" )) 30 { 31 $result = Array(); 32 foreach( $directories as $directory ) { 33 $result = array_merge( $result, IntegrityChecker::directoryToMD5Array( $directory, $recursive, $includeDirs, $ignore )); 34 } 35 36 return( $result ); 37 } 38 39 /** 40 * Generates an MD5 array, optinally recursively, of the given single folder. 41 * 42 * @param directory 43 * @param recursive 44 * @param includeDirs 45 * @param ignore 46 * @return An associative array 47 * @see IntegrityChecker::generateMD5Folder() 48 */ 49 function directoryToMD5Array( $directory, $recursive = true, $includeDirs = false, $ignore = Array( "*.svn" )) 50 { 51 $array_items = array(); 52 if ($handle = opendir($directory)) { 53 while (false !== ($file = readdir($handle))) { 54 if ($file != "." && $file != ".." && !IntegrityChecker::canIgnore( $file, $ignore )) { 55 if (is_dir($directory. "/" . $file)) { 56 if($recursive) { 57 $array_items = array_merge($array_items, IntegrityChecker::directoryToMD5Array($directory. "/" . $file, $recursive, $includeDirs, $ignore )); 58 } 59 if( $includeDirs ) { 60 $file = $directory . "/" . $file; 61 $array_items[preg_replace("/\/\//si", "/", $file)] = 0; 62 } 63 } 64 else { 65 $file = $directory . "/" . $file; 66 $array_items[preg_replace("/\/\//si", "/", $file)] = md5(file_get_contents(preg_replace("/\/\//si", "/", $file))); 67 } 68 } 69 } 70 closedir($handle); 71 } 72 return $array_items; 73 } 74 75 /** 76 * @return Returns true if the given file matches any of the patterns in the $ignore array 77 * @param file 78 * @param ignore 79 * @private 80 */ 81 function canIgnore( $file, $ignore ) 82 { 83 lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" ); 84 85 $result = false; 86 foreach( $ignore as $pattern ) { 87 if( Glob::myFnMatch( $pattern, $file )) { 88 $result = true; 89 break; 90 } 91 } 92 93 return( $result ); 94 } 95 96 /** 97 * Writes the list of files and MD5 hashes to the destination file. If no destination file is given 98 * PLOG_CLASS_PATH/install/files.properties.php is used 99 * 100 * @param folders Array containing the names of the folders to include 101 * @param dest Name of the destination file 102 */ 103 function writeMD5ListToFile( $folders = Array( "class", "templates/admin", "templates/wizard", "templates/rss", "templates/summary", "templates/default" ), $dest = "" ) 104 { 105 lt_include( PLOG_CLASS_PATH."class/file/file.class.php" ); 106 107 if( $dest == "" ) 108 $dest = PLOG_CLASS_PATH."install/files.properties.php"; 109 110 $currentData = IntegrityChecker::generateMD5Folder( $folders ); 111 112 // open the file and write the headers 113 $file = new File( $dest ); 114 $file->open( "w+" ); 115 $file->write( "<?php\n" ); 116 $file->write( "\$data = Array(\n"); 117 118 $line = 1; 119 foreach( $currentData as $f => $md5 ) { 120 $file->write( "\"$f\" => \"$md5\""); 121 if( $line < count($currentData )) 122 $file->write( "," ); 123 $file->write( "\n" ); 124 $line++; 125 } 126 127 // write the footer and close the file 128 $file->write( ");\n"); 129 $file->write( "?>" ); 130 $file->close(); 131 132 return( true ); 133 } 134 135 /** 136 * Given the $reference reference array and a list of folders, this method 137 * will calculate the MD5 hashes of the given folders and compare them against the ones 138 * in the reference array. 139 * 140 * @param reference 141 * @param folders 142 * @return An associative array containing the name of the files whose MD5 hash does not match 143 * the current one 144 */ 145 function checkIntegrity( $reference, $folders = Array( "class", "templates/admin", "templates/wizard", "templates/rss", "templates/summary", "templates/default" ) ) 146 { 147 $currentData = IntegrityChecker::generateMD5Folder( $folders ); 148 149 foreach( $currentData as $file => $md5 ) { 150 if( isset( $reference[$file] )) { 151 if( $reference[$file] == $md5 ) 152 unset( $reference[$file]); 153 } 154 } 155 156 return( $reference ); 157 } 158 } 159 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |