diff --git a/commerce_file.module b/commerce_file.module old mode 100644 new mode 100755 index 1bfed5c..2748cd3 --- a/commerce_file.module +++ b/commerce_file.module @@ -74,8 +74,19 @@ function commerce_file_admin_paths() { * Menu callback; download a single file entity. */ function commerce_file_download_page($file) { + // Get the license id from the file query. + $url_query = drupal_get_query_parameters(); + $license_id = (isset($url_query['license_id']))? $url_query['license_id'] : ''; + $license = entity_load_single('commerce_license', $license_id); + $license_uid = $license->uid; + $can_download = commerce_file_can_download($license, $file); + + // If the license exists and hits the download limit. + if (!$can_download) { + commerce_file_generate_download_limit_message($license_uid); + } // Ensure there is a valid token to download this file. - if (!isset($_GET['token']) || $_GET['token'] !== commerce_file_get_download_token($file)) { + if (!isset($_GET['token']) || $_GET['token'] !== commerce_file_get_download_token($file, $license_id)) { return MENU_ACCESS_DENIED; } // If the file does not exist it can cause problems with file_transfer(). @@ -270,6 +282,13 @@ function commerce_file_access($op, $file, $account = NULL) { // Look for a valid license. $license = commerce_file_get_license($op, $file, $account); + + // Send download error if no license is found. + if ($op == 'download' && empty($license)) { + $uid = $account->uid; + commerce_file_generate_download_limit_message($uid); + } + return !empty($license); } @@ -365,7 +384,7 @@ function commerce_file_get_license($op, $file, $account = NULL) { } } } - + return $licenses[$file->fid]; } @@ -612,8 +631,17 @@ function commerce_file_exit($destination = NULL) { $file = commerce_file_get_request_file(); $enable_limit = variable_get('commerce_file_enable_download_limit', FALSE); if ($file && $enable_limit) { - // The license is already in the commerce_file_get_license() static cache. - $license = commerce_file_get_license('download', $file); + // Load the license from the licence id in the file url query. + $url_query = drupal_get_query_parameters(); + if (!empty($url_query) && isset($url_query['license_id'])) { + $license = entity_load_single('commerce_license', $url_query['license_id']); + } + else { + // Fallback if there is no file url query or license id. + // The license is already in the commerce_file_get_license() static cache. + $license = commerce_file_get_license('download', $file); + } + // Skip logging if the admin is downloading another user's file. if ($license && $license->uid == $GLOBALS['user']->uid) { commerce_file_download_log_insert($license, $file); @@ -786,7 +814,7 @@ function commerce_file_field_formatter_settings_form($field, $instance, $view_mo '#type' => 'checkbox', '#title' => t('Check access'), '#description' => t('Confirms that the user has a file license.'), - '#default_value' => $settings['show_icon'], + '#default_value' => $settings['check_access'], ); } @@ -819,8 +847,29 @@ function commerce_file_field_formatter_view($entity_type, $entity, $field, $inst if ($entity_type != 'commerce_product') { return $element; } + // Grab the license_id if the display provides it. + if (isset($display['license_id'])) { + $license_id = $display['license_id']; + } + elseif (isset($display['views_row_id'])) { + // Check to see if the license id is in the current views row and grab it. + $views_row_id = $display['views_row_id']; + // Make sure the base field is license_id. + $base_field = (isset($display['views_view']->base_field) && $display['views_view']->base_field == 'license_id')? $display['views_view']->base_field : ''; + if (!empty($views_row_id) && !empty($base_field) && isset($display['views_view']->result[$views_row_id]->$base_field)) { + $license_id = $display['views_view']->result[$views_row_id]->$base_field; + } + } + + // Load the license from the row's license id. + if (isset($license_id)) { + $license = entity_load_single('commerce_license', $license_id); + } + else { + // Fallback: Grab the first license of a file. + $license = commerce_file_get_product_license($entity); + } - $license = commerce_file_get_product_license($entity); $access = TRUE; if (!empty($settings['check_access'])) { $access = ($license || commerce_file_bypass_license_control()); @@ -855,7 +904,13 @@ function commerce_file_field_formatter_view($entity_type, $entity, $field, $inst function theme_commerce_file_download_link($variables) { $file = $variables['file']; $uri = array('path' => "file/{$file->fid}/download", 'options' => array()); - $uri['options']['query']['token'] = commerce_file_get_download_token($file); + if (isset($variables['license']) && !empty($variables['license'])) { + $uri['options']['query']['token'] = commerce_file_get_download_token($file, $variables['license']->license_id); + $uri['options']['query']['license_id'] = $variables['license']->license_id; + } + else { + $uri['options']['query']['token'] = commerce_file_get_download_token($file); + } // Set options as per anchor format described at // http://microformats.org/wiki/file-format-examples $uri['options']['attributes']['type'] = $file->filemime . '; length=' . $file->filesize; @@ -899,7 +954,19 @@ function theme_commerce_file_download_link($variables) { * @return string * A CSRF token string. */ -function commerce_file_get_download_token($file) { +function commerce_file_get_download_token($file, $license_id = '') { $identifier = !empty($GLOBALS['user']->uid) ? $GLOBALS['user']->sid : ip_address(); - return drupal_hmac_base64("file/$file->fid/download", $identifier . drupal_get_private_key() . drupal_get_hash_salt()); + return drupal_hmac_base64("file/$file->fid/download", $identifier . $license_id . drupal_get_private_key() . drupal_get_hash_salt()); +} + +/** + * Generate message when file hits the download limit. + * + * @param string $uid + * A user's id. + */ +function commerce_file_generate_download_limit_message($uid) { + // Stop the download and reload the user's Files page. + drupal_set_message(t('This file has hit the download limit. If you would like to continue downloading this file, please purchase more licenses from the store.'), 'error', FALSE); + drupal_goto('user/' . $uid . '/my-files'); } diff --git a/plugins/license_type/CommerceLicenseFile.class.php b/plugins/license_type/CommerceLicenseFile.class.php old mode 100644 new mode 100755 index 90312a2..f0041ab --- a/plugins/license_type/CommerceLicenseFile.class.php +++ b/plugins/license_type/CommerceLicenseFile.class.php @@ -18,6 +18,7 @@ class CommerceLicenseFile extends CommerceLicenseBase { public function accessDetails() { // Display the files. $product = $this->wrapper->product->value(); + $license_id = $this->wrapper->license_id->value(); $display = array( 'label' => 'hidden', 'type' => 'commerce_file', @@ -27,6 +28,8 @@ class CommerceLicenseFile extends CommerceLicenseBase { // point in performing that check. 'check_access' => FALSE, ), + // Pass the license id to the file formatter. + 'license_id' => $license_id, ); $output = field_view_field('commerce_product', $product, 'commerce_file', $display); return drupal_render($output); @@ -39,8 +42,12 @@ class CommerceLicenseFile extends CommerceLicenseBase { // Store the uid in the session. The file access function will use it // if the user is anonymous, allowing the download to proceed. $_SESSION['commerce_license_uid'] = $this->uid; + $account = user_load($_SESSION['commerce_license_uid']); $product = $this->wrapper->product->value(); + $license_id = $this->wrapper->license_id->value(); + $license = entity_load_single('commerce_license', $license_id); + $message = t('Thank you for purchasing %product.', array('%product' => $product->title)) . '
'; $message .= t('Download now:'); return $message . $this->accessDetails();