#!/usr/bin/env php findHashes((string) $base_dir, $ignore_regex, $excluded_directories, $debug_callback); } catch (DirectoryNotFoundException | InvalidArgumentException $e) { printf('ERROR: %s' . PHP_EOL, $e->getMessage()); exit(6); } if ($create) { $output = ''; foreach ($file_array as $filename => $md5) { $output .= "$md5 $filename" . PHP_EOL; } if (!$quiet) { print 'Writing ' . cacti_sizeof($file_array) . " entries to $md5_file" . PHP_EOL; } if (!$confirm && file_exists($md5_file)) { printf('ERROR: MD5 file \'%s\' exists, but not --confirm to overwrite', $md5_file); exit(3); } try { $filesystem = new CactiFilesystem(); $filesystem->writeFile($md5_file, $output); } catch (Symfony\Component\Filesystem\Exception\IOExceptionInterface) { printf('ERROR: Failed to write to MD5 file \'%s\'' . PHP_EOL, $md5_file); exit(4); } } else { if (!file_exists($md5_file)) { printf('ERROR: MD5 file \'%s\' is missing, cannot verify' . PHP_EOL, $md5_file); exit(5); } $contents = file_get_contents($md5_file, false); if ($contents === false) { printf('ERROR: Failed to read from MD5 file \'%s\'' . PHP_EOL, $md5_file); exit(6); } $contents = explode("\n",$contents); $line = 0; $verify_array = []; foreach ($contents as $md5) { $line++; if (strlen($md5)) { if ($md5[32] != ' ') { printf('ERROR: Failed to parse line %d:' . PHP_EOL . ' %s' . PHP_EOL, $line, $md5); exit(7); } $filename = trim(substr($md5,33)); $verify_array[$filename] = substr($md5,0,32); } } $all_keys = array_unique(array_merge(array_keys($file_array),array_keys($verify_array))); foreach ($all_keys as $filename) { $hash_read = sprintf('%32s','Missing'); if (array_key_exists($filename, $file_array)) { $hash_read = $file_array[$filename]; } $hash_file = sprintf('%32s','Missing'); if (array_key_exists($filename, $verify_array)) { $hash_file = $verify_array[$filename]; } if ($hash_read != $hash_file) { if ($quiet) { exit(8); } print "$filename: FAILED" . PHP_EOL; if ($debug || $show_hash) { print " Read: [$hash_read]" . PHP_EOL; print " File: [$hash_file]" . PHP_EOL; } } } } /** * display_version - displays version information * * @return void */ function display_version() : void { $version = get_cacti_cli_version(); print "Cacti md5sum Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL; } /** * display_version - displays help information * * @return void */ function display_help() : void { display_version(); print PHP_EOL; print 'usage: md5sum.php [option] [filename]' . PHP_EOL . PHP_EOL; print 'Options:' . PHP_EOL; print '--create When specified used creates a file containing the md5 hash' . PHP_EOL; print ' followed by the name. Otherwise, the file is verified' . PHP_EOL; print '--debug logs additional output to the screen to aid in diagnosing' . PHP_EOL; print ' potential issues' . PHP_EOL . PHP_EOL; print '--basedir When specified, sets the base directory to search from. If' . PHP_EOL; print ' not specified, defaults to the directory above this script' . PHP_EOL; print '--quiet When specified, quiet mode only returns an exit value that' . PHP_EOL; print ' corresponds to the point of exit. Suppresses debug option' . PHP_EOL; print '--show When specified, adds extra output to the verify mode which' . PHP_EOL; print ' shows both the stored and computed hash value that failed' . PHP_EOL; print ' to match' . PHP_EOL . PHP_EOL; print 'When no filename is passed, .md5sum is assumed. Only one filename allowed' . PHP_EOL; }