--- sites/all/modules/ubercart/uc_file/uc_file.ca.inc.copy	2009-04-30 12:24:08.000000000 -0400
+++ sites/all/modules/ubercart/uc_file/uc_file.ca.inc	2009-05-01 11:28:23.000000000 -0400
@@ -67,11 +67,6 @@ function uc_file_ca_predicate() {
     ),
   );
 
-  $order_args = array(
-    'order' => 'order',
-    'expiration' => 'expiration',
-  );
-
   // Notify the user when a file is granted.
   $configurations['uc_file_notify_grant_trigger'] = array(
     '#title' => t('Notify customer when a file is granted'),
@@ -95,6 +90,96 @@ function uc_file_ca_predicate() {
     ),
   );
 
+  // If order is canceled, revoke file permissions
+  $configurations['uc_file_cancel_order'] = array(
+    '#title' => t('Remove file access'),
+    '#class' => 'order',
+    '#trigger' => 'uc_order_status_update',
+    '#status' => 1,
+    '#conditions' => array(
+      '#operator' => 'AND',
+      '#conditions' => array(
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the order status is completed.'),
+          '#argument_map' => array(
+            'order' => 'order',
+          ),
+          '#settings' => array(
+            'order_status' => 'completed',
+          ),
+        ),
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the updated order status is canceled.'),
+          '#argument_map' => array(
+            'order' => 'updated_order',
+          ),
+          '#settings' => array(
+            'order_status' => 'canceled',
+          ),
+        ),
+      ),
+    ),
+    '#actions' => array(
+      array(
+        '#name' => 'uc_file_revoke_access',
+        '#title' => t('Remove file access for this order.'),
+        '#argument_map' => array(
+          'order' => 'updated_order',
+        ),
+      ),
+    ),
+  );
+  
+  // Renew files if order goes from canceled to completed
+  $configurations['uc_file_renew_file_access'] = array(
+    '#title' => t('Renew purchased files if order is reinstated'),
+    '#description' => t('Renew purchased files if the order status matches.'),
+    '#class' => 'renewal',
+    '#trigger' => 'uc_order_status_update',
+    '#status' => 1,
+    '#conditions' => array(
+      '#operator' => 'AND',
+      '#conditions' => array(
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the order status is canceled.'),
+          '#argument_map' => array(
+            'order' => 'order',
+          ),
+          '#settings' => array(
+            'order_status' => 'canceled',
+          ),
+        ),
+        array(
+          '#name' => 'uc_order_status_condition',
+          '#title' => t('If the updated order status is completed.'),
+          '#argument_map' => array(
+            'order' => 'updated_order',
+          ),
+          '#settings' => array(
+            'order_status' => 'completed',
+          ),
+        ),
+      ),
+    ),
+    '#actions' => array(
+      array(
+        '#name' => 'uc_file_order_renew',
+        '#title' => t('Update all file expirations for this order.'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+      ),
+    ),
+  );
+ 
+  $order_args = array(
+    'order' => 'order',
+    'expiration' => 'expiration',
+  );
+
   return $configurations;
 }
 
@@ -114,6 +199,19 @@ function uc_file_ca_action() {
       ),
     ),
   );
+  
+  // Revoke file access from an order
+  $actions['uc_file_revoke_access'] = array(
+    '#title' => t('Remove file access for this order.'),
+    '#category' => t('revoke'),
+    '#callback' => 'uc_file_revoke_access',
+    '#arguments' => array(
+      'order' => array(
+        '#entity' => 'uc_order',
+        '#title' => t('Order'),
+      ),
+    ),
+  );
 
   // Send an email to an order with a file expiration
   $actions['uc_file_order_email'] = array(
@@ -325,3 +423,49 @@ function uc_file_action_order_renew($ord
     ca_pull_trigger('uc_file_notify_grant', $order, $user_downloads);
   }
 }
+
+/**
+ * Callback function for action that revokes file access
+ */
+function uc_file_revoke_access($order) {
+  // Revoke file access for each item in the order.
+  if (count($order->products)) {
+    // Scan products for models matching downloads.
+    foreach ($order->products as $product) {
+      $files = db_query("SELECT * FROM {uc_file_products} AS fp ".
+        "INNER JOIN {uc_product_features} AS pf ON pf.pfid = fp.pfid ".
+        "INNER JOIN {uc_files} as f ON f.fid = fp.fid ".
+        "WHERE nid = %d", $product->nid);
+
+      while ($file = db_fetch_object($files)) {
+
+        // Either they match, or the file was set to any SKU.
+        if (!empty($file->model) && $file->model != $product->model) {
+          continue;
+        }  
+        $file_limits = uc_file_get_time_limit($file);
+        $file_limits['time_polarity'] = '-';
+        $file_access = db_fetch_object(db_query("SELECT * FROM {uc_file_users} WHERE fid = %d AND uid = %d", $file->fid, $order->uid));
+        // Get new expiration date and download limit for file, enter it into db
+        $expire = _uc_file_expiration_date($file_limits, $file_access->expiration);
+        $download = $file_access->download_limit - uc_file_get_download_limit($file);
+        $address_limit = $file_access->address_limit - uc_file_get_address_limit($file);
+        // If file is already expired, then don't change anything
+        if ($file_access->expiration > time()) {
+          // Update adress_limit, download_limit, and expiration
+          db_query("UPDATE {uc_file_users} SET expiration = %d, download_limit = %d, address_limit = %d WHERE fid = %d AND uid = %d", $expire, $download, $address_limit, $file->fid, $order->uid);
+          $user = user_load($order->uid);
+          if (is_dir(uc_file_qualify_file($file->filename))) {
+            $comment = t('User download permissions for the files in the directory %dir have been revoked.', array('%dir' => $file->filename));
+          }
+          else {
+            $comment = t('User download permissions for the file %file have been revoked.', array('%file' => basename($file->filename)));
+          }
+          uc_order_comment_save($order->order_id, $user->uid, $comment);
+          drupal_set_message(t('%user\'s download permissions for %file have been revoked', array('%user' => $user->name, '%file' => $file->name)));
+        }
+      }
+    }
+  }
+}
+
