Index: core/core.drush.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/drush/commands/core/core.drush.inc,v
retrieving revision 1.30
diff -u -p -r1.30 core.drush.inc
--- core/core.drush.inc	2 Jun 2009 23:48:31 -0000	1.30
+++ core/core.drush.inc	24 Jul 2009 15:48:19 -0000
@@ -100,6 +100,9 @@ function core_drush_command() {
       'code' => 'PHP code',
     ),
   );
+  $items['iscorehacked'] = array(
+    'description' => 'Check and see if core is hacked, and save a few kittens.',
+  );
   return $items;
 }
 
@@ -439,3 +442,109 @@ function drush_core_watchdog_delete($typ
 function drush_core_eval($command) {
   eval($command . ';');
 }
+
+function drush_core_iscorehacked() {
+  $command = drush_get_command();
+  $tempdir = sys_get_temp_dir();
+  $destination = $tempdir .'DRUPAL-ISCOREHACKED';
+  mkdir($destination);
+  $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
+
+  drush_include_engine('package_handler', drush_get_option('package-handler', 'wget'));
+  $requestdata = pm_parse_project_version(array('drupal-'. VERSION));
+  $release = NULL;
+  foreach ($requestdata as $package) {
+    $project = $package['name'];
+    $url = 'http://updates.drupal.org/release-history'."/$project/". $package['drupal_version'];
+    if ($xml = @simplexml_load_file($url)) {
+      if ($error = $xml->xpath('/error')) {
+        drush_set_error('DRUSH_PM_COULD_NOT_LOAD_UPDATE_FILE', $error[0]);
+      }
+      else {
+        // Try to get the specified release.
+        if ($package['version']) {
+          if ($releases = $xml->xpath("/project/releases/release[status='published'][version='" . $package['version'] . "']")) {
+            $release = (array)$releases[0];
+          }
+          if (empty($release)) {
+            drush_die(dt("Could not locate specified project version."), 'notice');
+          }
+        }
+      }
+    }
+  }
+  if (!package_handler_install_project('drupal', $release, $destination)) {
+    drush_die(dt('could not fetch drupal core'));
+  }
+  
+  // uncompress the tarball
+  $destination .= '/drupal-'. VERSION;
+
+  $fp = opendir($destination);
+  $hackedfiles = array();
+  $dirstack = array();
+  while (($filename = readdir($fp)) !== FALSE) {
+    _drush_core_iscorehacked_process($drupal_root, $destination, $filename, $dirstack, $hackedfiles);
+  }
+  if (count($hackedfiles)) {
+    drush_die('Core is likely hacked! The following files did not match:'."\n". implode("\n", $hackedfiles));
+  }
+}
+
+function _drush_core_iscorehacked_process($drupal_root, $destination, $filename, &$dirstack, &$hackedfiles) {
+  if ($filename == '.' || $filename == '..' || $filename == '.htaccess' || $filename == 'default.settings.php') {
+    // skip
+    return;
+  }
+  $dirs = implode('/', $dirstack);
+  $fullpath = preg_replace('%//$%', '/', $destination . '/' . $dirs .'/') . $filename; 
+  $sitefullpath = preg_replace('%//$%', '/', $drupal_root . '/' . $dirs . '/') . $filename;
+
+  if (is_dir($fullpath)) {
+    array_push($dirstack, $filename);
+    $fp = opendir($fullpath);
+    while (($filename = readdir($fp)) !== FALSE) {
+      _drush_core_iscorehacked_process($drupal_root, $destination, $filename, $dirstack, $hackedfiles);
+    }
+    array_pop($dirstack);
+  } else {
+    $coresum = md5_file($fullpath);
+    $rootsum = md5_file($sitefullpath);
+    if ($coresum != $rootsum) {
+      $hackedfiles[] = '  '. $dirs . '/' . $filename;
+    }
+  }
+}
+
+
+// support sys_get_temp_dir for php 4
+if (!function_exists('sys_get_temp_dir')) {
+  // Based on http://www.phpit.net/
+  // article/creating-zip-tar-archives-dynamically-php/2/
+  function sys_get_temp_dir() {
+    // Try to get from environment variable
+    if (!empty($_ENV['TMP'])) {
+      return realpath($_ENV['TMP']);
+    }
+    else if (!empty($_ENV['TMPDIR'])) {
+      return realpath($_ENV['TMPDIR']);
+    }
+    else if (!empty($_ENV['TEMP'])) {
+      return realpath( $_ENV['TEMP']);
+    }
+    // Detect by creating a temporary file
+    else {
+      // Try to use system's temporary directory
+      // as random name shouldn't exist
+      $temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
+      if ($temp_file) {
+        $temp_dir = realpath( dirname($temp_file) );
+        unlink( $temp_file );
+        return $temp_dir;
+      }
+      else {
+        return FALSE;
+      }
+    }
+  }
+}
