• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

2.0.18 Image Download

x2STePx

Chevereto Member
Please note i have only tried this on 2.0.18, but don't worry im sure this will work on anything. Also note i have done this for people using the "Union" Theme by Piers - Excellent theme Piers!

Code for the DEFAULT 2.1.1 is towards the bottom!

Open up your favorite editor, paste and save this as: class.chip_download.php
Code:
<?php
/*
|-----------------
| Author:    Life.Object
| E-Mail:    life.object@gmail.com
| Website:    http://www.tutorialchip.com/
| Help:        http://www.tutorialchip.com/php-download-file-script/
| Version:    1.0
| Released: November 28, 2010
| Updated: November 28, 2010
|------------------
*/

class chip_download {
    
    /*
    |---------------------------
    | Properties
    |---------------------------
    */
    
    private $download_hook = array();
    
    private $args = array(
                        'download_path'            =>    NULL,
                        'file'                    =>    NULL,                        
                        'extension_check'        =>    TRUE,
                        'referrer_check'        =>    FALSE,    
                        'referrer'                =>    NULL,                    
                    );
    
    private $allowed_extensions = array(
                        
                    
                          /* Images */
                          'gif'    => 'image/gif',
                          'png'    => 'image/png',
                          'jpg'    => 'image/jpeg',
                          'jpeg'    => 'image/jpeg'
                    
                    );
    

    /*
    |---------------------------
    | Constructor
    |
    | @public
    | @param array $args
    | @param array $allowed_extensions
    |
    |---------------------------
    */
    
    public function __construct( $args = array(), $allowed_extensions = array()  ) {
        
        $this->set_args( $args );
        $this->set_allowed_extensions( $allowed_extensions );
                        
    }
    
    /*
    |---------------------------
    | Print variable in readable format
    |
    | @public
    | @param string|array|object $var
    |
    |---------------------------
    */
    
    public function chip_print( $var ) { 
        
        echo "<pre>";
        print_r($var);
            echo "</pre>";
    
    }
    
    /*
    |---------------------------
    | Update default arguments
    | It will update default array of class i.e $args
    |
    | @private
    | @param array $args - input arguments
    | @param array $defatuls - default arguments 
    | @return array
    |
    |---------------------------
    */
    
    private function chip_parse_args( $args = array(), $defaults = array() ) { 
        return array_merge( $defaults, $args );     
    }
    
    /*
    |---------------------------
    | Get extension and name of file
    |
    | @private
    | @param string $file_name 
    | @return array - having file_name and file_ext
    |
    |---------------------------
    */
    
    private function chip_extension($file_name) {
        $temp = array();
        $temp['file_name'] = strtolower( substr( $file_name, 0, strripos( $file_name, '.' ) ) );
        $temp['file_extension'] = strtolower( substr( $file_name, strripos( $file_name, '.' ) + 1 ) );
        return $temp;
    }
    
    /*
    |---------------------------
    | Set default arguments
    | It will set default array of class i.e $args
    |
    | @private
    | @param array $args
    | @return 0
    |
    |---------------------------
    */
    
    private function set_args( $args = array() ) { 
        
        $defaults = $this->get_args();
        $args = $this->chip_parse_args( $args, $defaults );
        $this->args = $args;     
    }
    
    /*
    |---------------------------
    | Get default arguments
    | It will get default array of class i.e $args
    |
    | @public
    | @return array
    |
    |---------------------------
    */
    
    public function get_args() { 
        return $this->args;     
    }
    
    /*
    |---------------------------
    | Set default allowed extensions
    | It will set default array of class i.e $allowed_extensions
    |
    | @private
    | @param array $allowed_extensions
    | @return 0
    |
    |---------------------------
    */
    
    private function set_allowed_extensions( $allowed_extensions = array() ) { 
        
        $defaults = $this->get_allowed_extensions();
        $allowed_extensions = array_unique( $this->chip_parse_args( $allowed_extensions, $defaults ) );
        $this->allowed_extensions = $allowed_extensions;     
    
    }
    
    /*
    |---------------------------
    | Get default allowed extensions
    | It will get default array of class i.e $allowed_extensions
    |
    | @public
    | @return array
    |
    |---------------------------
    */
    
    public function get_allowed_extensions() { 
        return $this->allowed_extensions;     
    }
    
    /*
    |---------------------------
    | Set Mimi Type
    | It will set default array of class i.e $allowed_extensions
    |
    | @private
    | @param string $file_path
    ! @return string
    |
    |---------------------------
    */
    
    private function set_mime_type( $file_path ) { 
        
        /* by Function - mime_content_type */
        if( function_exists( 'mime_content_type' ) ) {
            $file_mime_type = @mime_content_type( $file_path );
        }
        
        /* by Function - mime_content_type */
        else if( function_exists( 'finfo_file' ) ) {
            
            $finfo = @finfo_open(FILEINFO_MIME);
            $file_mime_type = @finfo_file($finfo, $file_path);
            finfo_close($finfo);  
        
        }
        
        /* Default - FALSE */
        else {
            $file_mime_type = FALSE;
         }
         
         return $file_mime_type;     
    
    }
    
    /*
    |---------------------------
    | Get Mimi Type
    | It will set default array of class i.e $allowed_extensions
    |
    | @public
    | @param string $file_path
    ! @return string
    |
    |---------------------------
    */
    
    public function get_mime_type( $file_path ) { 
        return $this->set_mime_type( $file_path );     
    }
    
    /*
    |---------------------------
    | Pre Download Hook
    |
    | @private
    | @return 0
    |
    |---------------------------
    */
    
    private function set_download_hook() { 
        
        /* Allowed Extensions */
        $allowed_extensions = $this->get_allowed_extensions();
        
        /* Arguments */
        $args = $this->get_args();        
        
        /* Extract Arguments */
        extract($args);
        
        /* File Path */
        $file_path = $download_path . $file;
        $this->download_hook['file_path'] = $file_path;
        
        /* File and File Path Validation */
        if( empty( $file ) || !file_exists( $file_path ) ) {
            $this->download_hook['download'] = FALSE;
            $this->download_hook['message'] = "Invalid File or File Path.";
            return 0;
        }
        
        /* File Name and Extension */
        $nameext = $this->chip_extension($file);
        $file_name = $nameext['file_name'];
        $file_extension = $nameext['file_extension'];
        
        $this->download_hook['file'] = $file;
        $this->download_hook['file_name'] = $file_name;
        $this->download_hook['file_extension'] = $file_extension;

        /* Allowed Extension - Validation */
        if ( $extension_check == TRUE && !array_key_exists( $file_extension, $allowed_extensions ) ) {
          $this->download_hook['download'] = FALSE;
          $this->download_hook['message'] = "File is not allowed to download"; 
          return 0;
        }
        
        /* Referrer - Validation */        
        if ( $referrer_check == TRUE && !empty($referrer) && strpos( strtoupper( $_SERVER['HTTP_REFERER'] ), strtoupper( $referrer ) ) === FALSE ) {
            $this->download_hook['download'] = FALSE;
             $this->download_hook['message'] = "Internal server error - Please contact system administrator";
            return 0;
        }
        
        /* File Size in Bytes */
        $file_size = filesize($file_path);
        $this->download_hook['file_size'] = $file_size;
        
        /* File Mime Type - Auto, Manual, Default */
        $file_mime_type = $this->get_mime_type( $file_path );        
        if( empty( $file_mime_type ) ) {
            
            $file_mime_type = $allowed_extensions[$file_extension];
            if( empty( $file_mime_type ) ) {
                $file_mime_type = "application/force-download";
            }
        
        }        
        
        $this->download_hook['file_mime_type'] = $file_mime_type;
        
        $this->download_hook['download'] = TRUE;
        $this->download_hook['message'] = "File is ready to download";
        return 0;        
    
    }
    
    /*
    |---------------------------
    | Download Hook
    | Allows you to do some action before download
    |
    | @public
    | @return array
    |
    |---------------------------
    */
    
    public function get_download_hook() { 
        $this->set_download_hook();
        return $this->download_hook;
    }
    
    /*
    |---------------------------
    | Post Download Hook
    |
    | @private
    | @return array
    |
    |---------------------------
    */
    
    private function set_post_download_hook() { 
        return $this->download_hook;
    }
    
    /*
    |---------------------------
    | Download
    | Start download stream
    |
    | @public
    | @return 0
    |
    |---------------------------
    */
    
    public function set_download() { 
        
        /* Download Hook */
        $download_hook = $this->set_post_download_hook();
        
        /* Extract */
        extract($download_hook);
        
        /* Recheck */
        if( $download_hook['download'] != TRUE ) {
            echo "File is not allowed to download";
            return 0;
        }
        
        /* Execution Time Unlimited */
        set_time_limit(0);
        
        /*
        |----------------
        | Header
        | Forcing a download using readfile()
        |----------------
        */
        
        header('Content-Description: File Transfer');
        header('Content-Type: ' . $file_mime_type);
        header('Content-Disposition: attachment; filename=' . $file);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . $file_size);
        ob_clean();
        flush();
        readfile($file_path);
        exit;
        
    }
    
    /*
    |---------------------------
    | Download
    | Start download stream
    |
    | @public
    | @return array
    |
    |---------------------------
    */
    
    public function get_download() { 
        $this->set_download();
        exit;
    }

    /*
    |---------------------------
    | Destructor
    |---------------------------
    */
    
    public function __destruct() {
    }
}
?>

Put that new file in your "includes" directory.

Same thing as before, open up editor paste and save this as: download.php
Code:
<?php
/*
|-----------------
| Chip Download Class
|------------------
*/

require("includes/class.chip_download.php");

/*
|-----------------
| Class Instance
|------------------
*/

$download_path = "images/";
$file = $_REQUEST['f'];

$args = array(
        'download_path'        =>    $download_path,
        'file'                =>    $file,        
        'extension_check'    =>    TRUE,
        'referrer_check'    =>    FALSE,
        'referrer'            =>    NULL,
        );
$download = new chip_download( $args );

/*
|-----------------
| Pre Download Hook
|------------------
*/

$download_hook = $download->get_download_hook();
//$download->chip_print($download_hook);
//exit;

/*
|-----------------
| Download
|------------------
*/

if( $download_hook['download'] == TRUE ) {

    /* You can write your logic before proceeding to download */
    
    /* Let's download file */
    $download->get_download();

}

?>

Now, to link the images to download :)
Like i said above, i have done this ONLY on Piers theme Union... So idk where you wanna put it if your using another theme or default, but you should put it on view.php

Change this:
Code:
        <div id="content-right-image-info">
     <h3>Image Information</h3></br>
      <div id="content-inner"> <h2 id="viewing">Image Name: <a href="<?php show_img_url(); ?>" target="_blank"><?php show_img_filename(); ?></a></h2> </div>
      <div id="content-inner"><h2 id="viewing">Image Size: <span id="imgWeight"><?php show_img_weight(); ?></span></h2> </div>
      <div id="content-inner"><h2 id="viewing">Image Resolution: <span id="imgDimentions"><?php show_img_dimentions(); ?></span> </h2> </div>
    </div>

To this:
Code:
        <div id="content-right-image-info">
     <h3>Image Information</h3></br>
      <div id="content-inner"> <h2 id="viewing">Image Name: <a href="<?php show_img_url(); ?>" target="_blank"><?php show_img_filename(); ?></a></h2> </div>
      <div id="content-inner"><h2 id="viewing">Image Size: <span id="imgWeight"><?php show_img_weight(); ?></span></h2> </div>
      <div id="content-inner"><h2 id="viewing">Image Resolution: <span id="imgDimentions"><?php show_img_dimentions(); ?></span> </h2> </div>
      <div id="content-inner"><h2 id="viewing">Download Image: <a href="download.php?f=<?php show_img_filename(); ?>" target="_blank"><?php show_img_filename(); ?></a> </h2> </div>
    </div>

Here's for the 2.1.1 DEFAULT view.php

Change this:
Code:
            <div class="image-tools-section" id="show_directly">
                <h3><?php show_lang_txt('txt_show_directly'); ?> <span><?php show_lang_txt('txt_show_directly_desc'); ?></span></h3>
                <div class="input-item"><label for="viewer-link"><?php show_lang_txt('txt_show_viewer_link'); ?>:</label> <input type="text" id="viewer-link" value="<?php show_img_viewer(); ?>" /></div>
                <div class="input-item"><label for="direct-link"><?php show_lang_txt('txt_show_directly_link'); ?>:</label> <input type="text" id="direct-link" value="<?php show_img_url(); ?>" /></div>
                <?php if(is_config_short_url() && is_user_preference_short_url()) : ?>
                <div class="input-item"><label for="short-url"><?php show_lang_txt('txt_show_directly_shorturl'); ?>:</label> <input type="text" id="short-url" value="<?php show_short_url(); ?>" /></div>
                <?php endif; ?>
                <div class="input-item"><label for="html-image"><?php show_lang_txt('txt_show_directly_html'); ?>:</label> <input type="text" id="html-image" value="<?php show_img_html(); ?>" /></div>
                <div class="input-item"><label for="bb-code"><?php show_lang_txt('txt_thumb_plus_link_bbcode'); ?>:</label> <input type="text" id="bb-code" value="<?php show_img_bbcode(); ?>" /></div>
            </div>

To this:
Code:
            <div class="image-tools-section" id="show_directly">
                <h3><?php show_lang_txt('txt_show_directly'); ?> <span><?php show_lang_txt('txt_show_directly_desc'); ?></span></h3>
                <div class="input-item"><label for="viewer-link"><?php show_lang_txt('txt_show_viewer_link'); ?>:</label> <input type="text" id="viewer-link" value="<?php show_img_viewer(); ?>" /></div>
                <div class="input-item"><label for="direct-link"><?php show_lang_txt('txt_show_directly_link'); ?>:</label> <input type="text" id="direct-link" value="<?php show_img_url(); ?>" /></div>
                <?php if(is_config_short_url() && is_user_preference_short_url()) : ?>
                <div class="input-item"><label for="short-url"><?php show_lang_txt('txt_show_directly_shorturl'); ?>:</label> <input type="text" id="short-url" value="<?php show_short_url(); ?>" /></div>
                <?php endif; ?>
                <div class="input-item"><label for="html-image"><?php show_lang_txt('txt_show_directly_html'); ?>:</label> <input type="text" id="html-image" value="<?php show_img_html(); ?>" /></div>
                <div class="input-item"><label for="bb-code"><?php show_lang_txt('txt_thumb_plus_link_bbcode'); ?>:</label> <input type="text" id="bb-code" value="<?php show_img_bbcode(); ?>" /></div>
                <div class="input-item"><label for="bb-code">Download Image:</label> <a href="download.php?f=<?php show_img_filename(); ?>" target="_blank"><?php show_img_filename(); ?></a></div>
            </div>


Enjoy - x2STePx
 
Back
Top