This module was working fine for me, but then for some reason it started counting images from image module as downloads, even though I was excluding jpegs. I updated image module tonight and I wonder if that has somehow changed things.

Anyhow, I investigated the code and noticed that it was using strpos, substr and array searching to locate the file extension. I think image module may confuse this since it tags files .preview.jpg, .thumbnail.jpg and so on. Because it introduces extra dots the extensions become thumbnail.jpg, preview.jpg, and so on.

My work around was to use a regex to detect the file extension, using code as follows as a replacement in the count function:

function download_count_file_download($filename) {

  $extensions = trim(variable_get('download_count_excluded_file_extensions', ''));

  if ( $extensions ) {
    $extensions = ereg_replace(' +', '|', preg_quote($extensions));
    $pattern = '@\.'.$extensions.'$@i';
    if ( preg_match($pattern, $filename) ) {
      return;
	}
  }

This is now working for me and the above regex will only look for extensions at the end of the filename, irrespective of how many . are used!

This also gets around the need to explode the list in to an array and do an array search, so there may be small performance gain?

Not sure if this is something you want to consider for inclusion in the module?

Otherwise it seems to be working just fine

Comments

Stuart Greenfield’s picture

Doh. Shouldn't stay up so late playing with Drupal and PHP!

The pattern above will probably work, but it's wrong because I missed the ( ) around the extensions.

Should be

$pattern = '@\.('.$extensions.')$@i';
Chill35’s picture

Assigned: Unassigned » Chill35

Excellent fix. Will put that in locally, test it and commit.

Thank you!

Chill35’s picture

Title: Started counting image downloads... » Files with dots in names not properly excluded
Status: Needs review » Fixed

Now fixed in both Drupal 4.7 and Drupal 5 versions.

Anonymous’s picture

Status: Fixed » Closed (fixed)
mr.j’s picture

Status: Closed (fixed) » Reviewed & tested by the community

Even though the current 5.1 dev download is dated May 19, and this was fixed April 24, the fix has not been applied to the 5.1 dev branch.

I just experienced the same bug after downloading and installing.

Chill35’s picture

Status: Reviewed & tested by the community » Closed (fixed)