--- imagefield_crop_file.inc	Thu Feb 12 06:37:28 2009
+++ imagefield_crop_file.inc	Sat Apr 04 15:08:34 2009
@@ -1,43 +1,63 @@
-<?php
-
-/**
- * @file
- * hook_file and imagefield_crop file functions.
- */
-function imagefield_crop_file_insert($file) {
-  // we copy the file as is, for quality cropping.
-  if (imagefield_file_is_image($file)) { 
-    $newfile = drupal_clone($file);
-    file_copy($newfile->filepath, imagefield_crop_file_admin_crop_display_path($newfile));
-  }
-}
-
-function imagefield_crop_file_update(&$file) {
-  // update admin thumbnail
-  imagefield_file_insert($file);
-
-  // flush imagecache
-  module_invoke('imagecache', 'image_flush', $file->filepath);
-
-  // update timestamp, see http://drupal.org/node/353405
-  $file->timestamp = REQUEST_TIME;
-  drupal_write_record('files', $file, 'fid');
-}
-
-function imagefield_crop_file_delete($file) {
-  // delete admin thumbnail.
-  if (imagefield_file_is_image($file))
-    file_delete(imagefield_crop_file_admin_crop_display_path($file));
-}
-
-// create the path to the file to be used for displaying the crop interface.
-function imagefield_crop_file_admin_crop_display_path($file) {
-  $file = (object)$file;
-  return $file->filepath .'.crop_display.jpg';
-}
-
-// Original file to crop from
-function imagefield_crop_file_admin_original_path($file) {
-  // the displayed and original are the same. Yay Jcrop!
-  return imagefield_crop_file_admin_crop_display_path($file);
-}
+<?php
+
+/**
+ * @file
+ * hook_file and imagefield_crop file functions.
+ */
+function imagefield_crop_file_insert($file) {
+  // we copy the file as is, for quality cropping.
+  if (imagefield_file_is_image($file)) { 
+    $newfile = drupal_clone($file);
+    file_copy($newfile->filepath, imagefield_crop_file_admin_crop_display_path($newfile));
+  }
+}
+
+function imagefield_crop_file_update(&$file) {
+  // update admin thumbnail
+  imagefield_file_insert($file);
+
+  // flush imagecache
+  module_invoke('imagecache', 'image_flush', $file->filepath);
+
+  // update timestamp, see http://drupal.org/node/353405
+  $file->timestamp = REQUEST_TIME;
+  drupal_write_record('files', $file, 'fid');
+}
+
+function imagefield_crop_file_delete($file) {
+  // delete admin thumbnail.
+  if (imagefield_file_is_image($file))
+    file_delete(imagefield_crop_file_admin_crop_display_path($file));
+}
+
+// create the path to the file to be used for displaying the crop interface.
+function imagefield_crop_file_admin_crop_display_path($file) {
+  $file = (object)$file;
+  return $file->filepath .'.crop_display.jpg';
+}
+
+// Original file to crop from
+function imagefield_crop_file_admin_original_path($file) {
+  // the displayed and original are the same. Yay Jcrop!
+  return imagefield_crop_file_admin_crop_display_path($file);
+}
+
+function imagefield_crop_file_download($filepath) {
+  // Return headers for crop display if private files are enabled.
+  if (strpos($filepath, 'crop_display') !== FALSE) {
+    $original_path = str_replace('crop_display.jpg', '', $filepath);
+    $original_full_path = file_create_path($original_path);
+    $display_full_path = file_create_path($filepath);
+
+    // Allow access to temporary images, since they're not yet associated
+    // with a node. If not temporary, check access on the original file.
+    $status = db_result(db_query("SELECT status FROM {files} WHERE filepath = '%s'", $original_full_path));
+    $access = ($status == 0 || module_invoke_all('file_download', $original_path));
+    if ($access && $info = getimagesize($display_full_path)) {
+      return array(
+        'Content-Type: ' . $info['mime'],
+        'Content-Length: ' . filesize($display_full_path)
+      );
+    }
+  }
+}
