forum.coppermine-gallery.net

No Support => Modifications/Add-Ons/Hacks => Mods: Miscellaneous => Topic started by: adrianbj on July 05, 2005, 02:26:28 am

Title: Modification to create image download link
Post by: adrianbj on July 05, 2005, 02:26:28 am
First of all, I tried to add this post to modifications, but I didn't have rights to post a new topic, so here goes. Something that has always bugged me a little about Coppermine was that clicking on the normal size image spawns a new window containing the fullsize version. I think it would be better (especially for non-tech users) that there is a download link to the full version. Sometimes these images can be many MB in size and a user may not realize this until their browser starts to download it. I have made a modification myself to address this and thought I'd share it with anyone else who might be interested.

I used to use version 1.3 and so the code to be modified was in the displayimage.php file, but I have just installed 1.41 and it now appears that you have to make this mod to the theme.php file.
Here are my changes:

Code: [Select]
// The original content of theme.php (note this is only present in the sample theme)

    if ($mime_content['content']=='image') {
        if (isset($image_size['reduced'])) {
            $winsizeX = $CURRENT_PIC_DATA['pwidth']+5;  //the +'s are the mysterious FF and IE paddings
            $winsizeY = $CURRENT_PIC_DATA['pheight']+3; //the +'s are the mysterious FF and IE paddings
            $pic_html = "<a href=\"javascript:;\" onclick=\"MM_openBrWindow('displayimage.php?pid=$pid&amp;fullsize=1','" . uniqid(rand()) . "','scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
            $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
            $pic_html .= "<img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
            $pic_html .= "</a>\n";


// My modification below creates a download link showing the size of the image and prevents opening of fullsize image in new window

    if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
      $pic_html .= "</a>\n";

You will also need to create a new file called download.php in the root of your coppermine installation with these contents

Code: [Select]
<?php
     
if ($_REQUEST ['getfile']){
        
$file $_REQUEST ['getfile'];
     
     }

$save_as_name basename($file);   
ini_set('session.cache_limiter''');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/octet-stream");
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");

readfile($file);

?>

Anyway, hope that is useful to someone.

Adrian


Title: Re: Modification to create image download link
Post by: HanOverFist on July 05, 2005, 08:27:09 pm
everything seems to work but when I click the link I'm getting strange DL problems.
file sizes are over 1.5 meg do not fully DL
The DL dialog box finishes transfering the file but when trying to open the file I get a corrupt or truncated file error message.
Server setting?
CPG 1.3.3
Title: Re: Modification to create image download link
Post by: HanOverFist on July 11, 2005, 08:36:25 pm
I would love to get a reply here.
cpg133 with this mod truncates every file over 1.9 megs at 2,000,000 bytes.
cpg141 with this mod truncates every file over 1.9 megs at 2,000,000 bytes.
Everytime.
Is this a php server setting that is doing this.
It would be great to get this resolved.
Is there any more info you need from me for a response?

Jay
Title: Re: Modification to create image download link
Post by: Nibbler on July 11, 2005, 09:37:34 pm
Read the comments in the php manual for readfile() (http://php.net/readfile).
Title: Re: Modification to create image download link
Post by: HanOverFist on July 11, 2005, 10:12:41 pm
thanks nibbler

That is what I was looking for.
now where do I insert this bit of code?

<?php
function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

}
?>



In the new download.php file that I created on line 15? (replacing the instance of readfile?)
or do I make a new php file out of it called readfile.php?

tia

Jay
Title: Re: Modification to create image download link
Post by: Nibbler on July 11, 2005, 10:15:09 pm
You place the function definition somewhere in the file before the call to read($file), and replace the call to read($file) with readfile_chunked($file);
Title: Re: Modification to create image download link
Post by: HanOverFist on July 11, 2005, 10:22:06 pm
sweeeeet

Thank you
Title: Re: Modification to create image download link
Post by: serhathakan on July 15, 2005, 01:04:04 am
i am using 1.3.2 and the original code is different and

when i change it to

Code: [Select]
if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
    $pic_html .= "</a>\n";



it giving error

and my original code is



Code: [Select]
if ($mime_content['content']=='image') {
        if (isset($image_size['reduced'])) {
            $winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
            $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
            $pic_html = "<a href=\"javascript:;\" onClick=\"MM_openBrWindow('displayimage.php?pid=$pid&fullsize=1','" . uniqid(rand()) . "','scrollbars=yes,toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
            $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
            $pic_html .= "<img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
            $pic_html .= "</a>\n";
Title: Re: Modification to create image download link
Post by: Nibbler on July 15, 2005, 01:15:23 am
Are we allowed to know the error that it gives you ?
Title: Re: Modification to create image download link
Post by: serhathakan on July 15, 2005, 01:52:56 am
sure here is the code

Code: [Select]
Parse error: parse error, unexpected $ in /home/tasarim/public_html/galeri/displayimage.php on line 646
Title: Re: Modification to create image download link
Post by: Loki66 on July 16, 2005, 12:59:53 am
Is there a way to protect the image using this download ? Is it possible to put images in protected directories ?

Thanks,

L
Title: Re: Modification to create image download link
Post by: serhathakan on July 16, 2005, 02:34:28 am
no not protected
chmod 777
Title: Re: Modification to create image download link
Post by: adrianbj on August 05, 2005, 08:43:45 am
Well thanks everyone for improving on my mod. I didn't have any download problems on my server with my php settings so I didn't realize the need for the chunkfile way of doing things. I have my the changes to my setup also.

Cheers,

A.
Title: Re: Modification to create image download link
Post by: BT-loader on August 25, 2005, 10:01:06 pm
I´ve just installed this mod/hack but it seems that there is an error when saving the image to my hard drive.  :\'(
The image is saved as "document" without any extension, why is that?

(I´m using 1.3.3)

EDIT:  Nevermind, i finally got it to work.

Thanks for a great mod.  ;D
Title: Re: Modification to create image download link
Post by: Joachim Müller on August 26, 2005, 08:06:30 am
EDIT:  Nevermind, i finally got it to work.
Then why don't you tell others who might have the same issues you were having? Be a giver instead of always being a taker.
Title: Re: Modification to create image download link
Post by: BT-loader on August 26, 2005, 02:38:32 pm
Quote from: GauGau
Then why don't you tell others who might have the same issues you were having? Be a giver instead of always being a taker.
It was a stupid thing really, i just forgot to copy all the code.  :-[

Quote from: adrianbj
// My modification below creates a download link showing the size of the image and prevents opening of fullsize image in new window

    if ($mime_content['content']=='image') {
    if (isset($image_size['reduced'])) {
        $filesize = $CURRENT_PIC_DATA['filesize'];
$filesizecorr = intval($filesize/1024);
$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
        $winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
$picturefs_url = str_replace("normal_", "", $picture_url);
        $pic_title = $lang_display_image_php['view_fs'] . "\n==============\n" . $pic_title;
        $pic_html .= "<center><img src=\"" . $picture_url . "\" class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /></center><br />";
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
    $pic_html .= "</a>\n";
Quote
Title: Re: Modification to create image download link
Post by: mylogon on August 30, 2005, 10:47:39 pm
Any thought on how to modify the code for pictures that are resized and therefore will not cause either the "Normal" view or create the download link because they do not trigger that section?

Anyone have any ideas?
Title: Re: Modification to create image download link
Post by: shniz123 on September 03, 2005, 07:34:57 pm
Works like a champ! Great work.
Title: Re: Modification to create image download link
Post by: outpatient on September 18, 2005, 08:09:29 pm
Hey kids,

I'm looking for a mod to stop my videos from playing automatically and to offer a download link as an option.  I know there was one that worked just for the videos, because I've used it before, only I can't for the life of me, find it.  ::)

Would this mod make all images available for download? Can someone point me in the right direction?

I'd really appreciate it, thanks


eta: nevermind, I found it!  ;) here (http://forum.coppermine-gallery.net/index.php?topic=9367.0)
Title: Re: Modification to create image download link
Post by: EtaBeta on January 21, 2006, 05:52:02 pm
Hi!
Nice mod!
Is it possible to allow ONLY registerd users to download? How??? I am very iterested to this modification because iI don' want all the visitors be able to download pictures from my gallery.
Thanks to all
Title: Re: Modification to create image download link
Post by: FIREBOX on February 03, 2006, 09:14:18 pm
1.
its a great mod i am on cpg 135 when i installed the code the link download the preview not the full size image

2.
also is there a mod for only allowing registered users to use the link



thanks
Title: Re: Modification to create image download link
Post by: Nallan on February 27, 2006, 01:25:15 am
Is it possible to allow ONLY registerd users to download? How??? I am very iterested to this modification because iI don' want all the visitors be able to download pictures from my gallery.

I'm also very interested in this issue!!
Title: Re: Modification to create image download link
Post by: adrianbj on February 27, 2006, 01:45:37 am
Well I just tried this and it seems to work fine - now only logged in users will see the download link

Add the if statement and the corresponding close } around the download link - like this:

Code: [Select]
if (USER_ID){
$pic_html .= "<center><a href=\"download.php?getfile=$picturefs_url\">Click here to save the fullsize ($filesizecorr KB) image directly to your hard drive<br /></a><br /><br /></center>";
}


As for the issue of downloading the preview - sorry I have no idea

While I am here, I thought I may as well also post the revised version of the download file which includes the chunked file issue brought up long ago:

Code: [Select]
<?php
     
if ($_REQUEST ['getfile']){
        
$file $_REQUEST ['getfile'];
     
     }

$save_as_name basename($file);   
ini_set('session.cache_limiter''');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/octet-stream");
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");

function 
readfile_chunked($filename,$retbytes=true) {
  
$chunksize 1*(1024*1024); // how many bytes per chunk
  
$buffer '';
  
$cnt =0;
  
// $handle = fopen($filename, 'rb');
  
$handle fopen($filename'rb');
  if (
$handle === false) {
      return 
false;
  }
  while (!
feof($handle)) {
      
$buffer fread($handle$chunksize);
      echo 
$buffer;
       
ob_flush();
      
flush();
      if (
$retbytes) {
          
$cnt += strlen($buffer);
      }
  }
      
$status fclose($handle);
  if (
$retbytes && $status) {
      return 
$cnt// return num. bytes delivered like readfile() does.
  
}
  return 
$status;

}

readfile_chunked($file);
//readfile($file);

?>

Hope that helps someone.

Cheers,
Adrian
Title: Re: Modification to create image download link
Post by: Darkwoods on March 01, 2006, 09:38:43 pm
very nice mod  :) is it possible to have the full link on the thumbnails page? instead of having it on displayimage.php
Title: Re: Modification to create image download link
Post by: adrianbj on March 02, 2006, 05:11:19 am
very nice mod  :) is it possible to have the full link on the thumbnails page? instead of having it on displayimage.php

OK - here's a quick hack to get what you want:

In the functions.inc.php file in the includes directory, find the build_caption at around line 801. In the code below, notice the 4 most indented lines which are also comment with '//download link' at the end. These are the 4 new lines of code that need to be added.
Hope that does what you are looking for.


Code: [Select]
function build_caption(&$rowset,$must_have=array())
{
    global $CONFIG, $THEME_DIR;
    global $album_date_fmt, $lastcom_date_fmt, $lastup_date_fmt, $lasthit_date_fmt, $cat;
    global $lang_get_pic_data, $lang_meta_album_names, $lang_errors;

    foreach ($rowset as $key => $row) {
        $caption='';
$pic_url =  get_pic_url($row, 'thumb'); //download link
$downloadfilename = str_replace("thumb_", "", $pic_url); //download link
$filesizecorr = $row['filesize'] >> 10 . $lang_byte_units[1]; //download link
$caption .= "<span class=\"thumb_title\"><a href=\"download.php?getfile=$downloadfilename\">Save fullsize ($filesizecorr KB) image to your HDD</a></span>"; //download link
        if ($CONFIG['display_filename']) {
          $caption .='<span class="thumb_filename">' . $row['filename'] . '</span>';
        }

Title: Re: Modification to create image download link
Post by: Darkwoods on March 02, 2006, 03:12:08 pm
thanks its working   ;D you maked my day

thx
Title: Re: Modification to create image download link
Post by: Darkwoods on March 02, 2006, 07:31:10 pm
does this work on all type of files? because its just work with image files not mp3 or others files  :-[
Title: Re: Modification to create image download link
Post by: adrianbj on March 02, 2006, 08:58:18 pm
does this work on all type of files? because its just work with image files not mp3 or others files  :-[

Good point - I don't have anything but images in my library so it hasn't been an issue.

I see two major problems here:

1) thumbnails for mp3's and other none image files are not relative to the file in question, they are simply a generic image, so i have to figure out a different way to find the filename.
2) the download script has a Content-Type: application/octet-stream which I thought might handle most things, but obviously not. I have this part sorted already, but I will have to work on the first bit.

Will get back to you when I have something that is working.
Title: Re: Modification to create image download link
Post by: adrianbj on March 02, 2006, 09:06:23 pm
Hi All,

I am afraid I have noticed a VERY serious security issue with the download.php script. Here is my quick hack to prevent any misuse.

Add these lines to the download.php file, directly after the: $save_as_name = basename($file);

Code: [Select]
$dirpath = dirname($file);
$pathcheck = substr_count($dirpath, 'albums/userpics');

if($pathcheck >= 1){


and then just before the final ?> at the end of the file, add these lines:

Code: [Select]
else{
print 'You do not have permission to download that file';
}

It basically checks to make sure that the file the user is trying to download is actually in the albums/userpics folder.

I think this should prevent the security issue, but I am no expert - maybe someone else can comment on this!
Title: Re: Modification to create image download link
Post by: Joachim Müller on March 03, 2006, 08:52:51 am
having a file like download.php is a huge security issue, it must be extremely hardened against possible exploits. You might want to take a look at the code I posted in http://forum.coppermine-gallery.net/index.php?topic=28216.msg130742#msg130742 - you explicitely allow file types there.
Title: Re: Modification to create image download link
Post by: adrianbj on March 05, 2006, 06:17:48 am
GauGau - thanks for that - I was actually just thinking about doing it by file types today - seems like a good solution - might even be nice to combine the file type and folder check. I would definitely recommend to everyone using this hack to use the file type checking code that you linked to. Thanks!
Title: Re: Modification to create image download link
Post by: Sir Doyle on April 10, 2006, 05:16:10 pm
hi,
This is great Mod
But it does not work for another theme
Can you please tell how can I install this Mod in Eyeball theme?

Thanks
Title: Re: Modification to create image download link
Post by: spedmetal on April 15, 2006, 12:51:27 am
Hi!
Nice mod!
Is it possible to allow ONLY registerd users to download? How??? I am very iterested to this modification because iI don' want all the visitors be able to download pictures from my gallery.
Thanks to all


Yes does this only work with the sample theme? Also I have cm1.4.4 installed and cant even get back to the sample theme.

@adrianbj

the code you posted on that still goes in the theme.php file in the sample folder correct? If so were?
Title: Re: Modification to create image download link
Post by: bennettsweb on April 17, 2006, 07:42:26 am
I was able to impliment this code in 1.4.4 by specifying the
Code: [Select]
function theme_html_picture() in my custom theme.php.  I used the same function as the sample theme.php except for the bit of code in the first post.  I decided to keep the link to the fullsize image when the image is clicked and include the download link below the image. 

You could also modify themes.inc.php to apply the mod to all themes that do not have the function specified if I understand the code (I'm still pretty green in php)

Thanks for the help! -Bennett
Title: Re: Modification to create image download link
Post by: ir803 on January 14, 2007, 08:59:59 am
this sounds like a great idea, and looks like something i can use, all i need now is to know Which logged in user downloaded which file, is that something that can be integrated in to this hack or would this have to be a separate issue??
Title: Re: Modification to create image download link
Post by: adrianbj on January 14, 2007, 09:15:40 am
ir803,

I have in fact mod my gallery to include a feature to record who has downloaded what. The mod also adds a new category called 'Most Downloaded'. Unfortunately I have also made many many other mods to my gallery since doing this, so it won't be easy to send you all the code changes, which also include changes to the database. I have also started collecting the user's country during registration, so I can see what country each image is being downloaded from. I really should put this mods together at some point, but not sure when I will get a chance to I'm afraid. I'll see what I can do though. BTW, I have also made the download script way more secure by initiating the downloads by picture ID, rather than filename.
Adrian
Title: Re: Modification to create image download link
Post by: ir803 on January 14, 2007, 10:01:24 am
THAT sounds amazing mate could you send me a link to your site so I can see it in action, I wil wait for to to get a chance to see if you can put the code together coz I am very new to PHP and I wouldn't know where to start to take out the relevent bits of sode.
Title: Re: Modification to create image download link
Post by: adrianbj on January 16, 2007, 06:15:25 pm
The gallery is at http://ian.umces.edu/imagelibrary/

You will see that only registered users can download the fullsize version.

Here are a list of the features that might be of interest to you.

1) When you go to register you'll notice that I collect the user's country.
2) Notice the 'Most Downloaded' option in the menu. This doesn't list who has downloaded what (I don't think this should ever be presented publicly), but the information is being collected and stored in a new database table, so I can see who has downloaded what.
3) I have also integrated the lightbox mod and made the zip download also only available to registered users.

One of the problems you would find applying my changes is that I have also applied the 'Search Engine Friendly' URL mod, as well as swapping all links over to the PID, rather than the POS and made endless other code changes, including improvements to the SEF mod and the entire search process so they all actually work.

As I said, I will see what I can do about getting together the required changes, but I think I might be beyond making that a viable option as there have been so many other changes to the code. Please don't expect anything anytime soon though. I am still finishing off the upload and automatic thumbnail generation for vector files (SVG, EPS etc) and TIFFs, PSDs etc and movies and so don't really have any spare time at the moment. At least it will give you some ideas on possible options should you decide to try to mod the code yourself.
Title: Re: Modification to create image download link
Post by: ir803 on January 16, 2007, 11:35:42 pm
Sounds good, I'll be patient, I'm still picking up the basics at the moment. do you privately have a record of specifically who (which user) downloaded each image as this would be the most important aspect. BTW this is the site I would be putting it on http://www.ghsimages.co.uk/ (http://www.ghsimages.co.uk/)
Title: Re: Modification to create image download link
Post by: adrianbj on January 16, 2007, 11:45:28 pm
Sounds good, I'll be patient, I'm still picking up the basics at the moment. do you privately have a record of specifically who (which user) downloaded each image as this would be the most important aspect. BTW this is the site I would be putting it on http://www.ghsimages.co.uk/ (http://www.ghsimages.co.uk/)

The new table 'downloads' has these fields

pid    
user_id    
datetime    

so to answer your question, yes I do have access to who has downloaded which photos (pid), and the date/time they were downloaded. By linking the user_id to the users table I can find out their name, country and state.
Title: Re: Modification to create image download link
Post by: ir803 on January 17, 2007, 01:29:48 pm
FANTASTIC  :D
Title: Re: Modification to create image download link
Post by: adrianbj on January 18, 2007, 02:58:25 am
You might also want to look at this plugin I stumbled across: http://forum.coppermine-gallery.net/index.php?topic=36322.0

Might be an easier way to set up what I have hacked without all the problems associated with my other modifications.
Title: Re: Modification to create image download link
Post by: nisx91 on March 02, 2008, 08:19:08 am
I cant find any theme where i can put this code :(
Title: Re: Modification to create image download link
Post by: Joachim Müller on March 02, 2008, 09:31:41 am
Use your custom theme. If a section that needs editing doesn't exist in your custom theme, copy the corresponding section from themes/sample/theme.php