Plugin Directory

Changeset 944960


Ignore:
Timestamp:
07/08/2014 02:08:33 PM (12 years ago)
Author:
deepaks
Message:

New Version of Plugin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • hide-real-download-path/trunk/hide-download-path.php

    r652802 r944960  
    44Plugin URI: http://xlab.co.in
    55Description: This plugin help you to hide download path of files on your website. You can allow download of files without showing exact download path of file on your server and make if more secure. It also allow to restrict hot linking of files.
    6 Version: 1.0
     6Version: 1.5
    77Author: Deepak Sihag
    88Author URI: http://xlab.co.in/hide-download-path-of-file-wordpress-plugin/
     
    1010*/
    1111?>
     12
    1213<?php
    13 
    14 function hide_download_path () {
    15    
    16    
    17     global $wpdb;
    18    
    19     $table_name = $wpdb->prefix . "download_settings";
    20    
    21     /* Check and create TICKER table if doesn't exist */
    22     if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    23 
    24         $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
     14    function hide_download_path ()
     15    {
     16        global $wpdb;
     17        $table_name = $wpdb->prefix . "download_settings";
     18       
     19        /* Check and create TICKER table if doesn't exist */
     20        if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
     21        {
     22            $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
    2523            `id` int(11) NOT NULL AUTO_INCREMENT,
    2624            `allowed_referred` varchar(50) NULL,
     
    2826            `log_downloads` int(1) NOT NULL DEFAULT '1',
    2927            PRIMARY KEY (`id`)
    30         ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
    31      
    32         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    33         dbDelta($sql);
    34        
    35         $baseDir = "Your Base path here";
    36         $rows_affected = $wpdb->insert( $table_name, array( 'base_dir' => $baseDir ) );
    37      
    38         add_option("hide_download_path", "1.0"); //set version for table description
     28            ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
     29
     30            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
     31            dbDelta($sql);
     32            $baseDir = "Your Base path here";
     33            $rows_affected = $wpdb->insert( $table_name, array( 'base_dir' => $baseDir ) );
     34            add_option("hide_download_path", "1.0"); //set version for table description
     35        }
    3936    }
    40    
    41 }
    42 
    43 /* Call plugin installation function */
    44 register_activation_hook(__FILE__,'hide_download_path');
    45 
    46 
    47 function pluginUninstall() {
    48   global $wpdb;
    49   $thetable = $wpdb->prefix."download_settings";
    50   //Delete any options that's stored also?
    51   //delete_option('wp_yourplugin_version');
    52   $wpdb->query("DROP TABLE IF EXISTS $thetable");
    53 }
    54 
    55 register_deactivation_hook( __FILE__, 'pluginUninstall');
    56 
    57 
    58 /* Add Plugin Menu Start */
    59 function ticker_menu() {
    60   add_options_page('Hide Download Link Settings', 'Hide Download Link', 'manage_options', 'hide-link-settings', 'download_settings_main');
    61 }
    62 
    63 add_action('admin_menu', 'ticker_menu');
    64 /* Add Plugin Menu Ends */
    65 
    66 function updateSettings()
    67 {
    68    
    69     global $wpdb;
    70    
    71     if(rtrim($_POST["txtBaseDir"] == ""))
    72     {
    73         return "0";   
     37
     38    /* Call plugin installation function */
     39    register_activation_hook(__FILE__,'hide_download_path');
     40
     41    function pluginUninstall()
     42    {
     43        global $wpdb;
     44        $thetable = $wpdb->prefix."download_settings";
     45        //Delete any options that's stored also?
     46        //delete_option('wp_yourplugin_version');
     47        $wpdb->query("DROP TABLE IF EXISTS $thetable");
    7448    }
    75    
    76     if(isset($_POST['chk_log']))
    77     {
    78         $log_download = 1;
     49
     50    register_deactivation_hook( __FILE__, 'pluginUninstall');
     51
     52    /* Add Plugin Menu Start */
     53    function ticker_menu()
     54    {
     55        add_options_page('Hide Download Link Settings', 'Hide Download Link', 'manage_options', 'hide-link-settings', 'download_settings_main');
    7956    }
    80     else
    81     {
    82         $log_download = 0;
     57    add_action('admin_menu', 'ticker_menu');
     58
     59    /* Add Plugin Menu Ends */
     60    function updateSettings()
     61    {
     62        global $wpdb;
     63        if(rtrim($_POST["txtBaseDir"] == ""))
     64        {
     65            return "0";   
     66        }
     67
     68        if(isset($_POST['chk_log']))
     69        {
     70            $log_download = 1;
     71        }
     72        else
     73        {
     74            $log_download = 0;
     75        }
     76        $result = $wpdb->query("UPDATE $wpdb->prefix"."download_settings SET `allowed_referred`='".mysql_real_escape_string($_POST["txtReferred"])."', `base_dir`='".$_POST["txtBaseDir"]."', `log_downloads`='".$log_download."'");
     77        return $result;
    8378    }
    84        
    85     $result = $wpdb->query("UPDATE $wpdb->prefix"."download_settings SET `allowed_referred`='".mysql_real_escape_string($_POST["txtReferred"])."', `base_dir`='".$_POST["txtBaseDir"]."', `log_downloads`='".$log_download."'");
    86    
    87     return $result;
    88 }
    89 
    90 
    91 function download_settings_main()
    92 {
    93     global $wpdb;
    94     define( 'PLUGINNAME_URL', plugin_dir_url(__FILE__) );
    95    
    96     if($_POST)
    97     {
     79
     80
     81
     82    function download_settings_main()
     83    {
     84        global $wpdb;
     85        define( 'PLUGINNAME_URL', plugin_dir_url(__FILE__) );
     86        if($_POST)
     87        {
    9888            $result = updateSettings();
    9989            if($result)
    10090            {
    101                 $message = "Hurray, All set to start.";   
     91                $message = "<div class='updated'>Settings are saved. You must create a new download page now and add shortcode.</div>";   
    10292            }
    10393            else
    10494            {
    105                 $message = "Opps Jack, Something unexpected has occured, Please try again.";//"";
     95                $message = "<div class='error'>Something went wrong while saving data, please try again.</div>";//"";
    10696            }           
    107     }
    108    
    109    
    110     echo "<h2>" . __( 'Download Settings', 'tickerimp_trdom' ) . "</h2>";
    111    
    112     $qry_settings = "SELECT * FROM ".$wpdb->prefix . "download_settings";
    113     $existing_settings = $wpdb->get_results($qry_settings);
    114    
    115     ?>
    116    
     97        }
     98 
     99        echo "<div class='wrap'>";
     100        echo "<h2>" . __( 'Download Settings', 'tickerimp_trdom' ) . "</h2>";
     101 
     102        $qry_settings = "SELECT * FROM ".$wpdb->prefix . "download_settings";
     103        $existing_settings = $wpdb->get_results($qry_settings);
     104?>
     105
    117106        <div class="message">
    118107            <?php echo $message; ?>
    119108        </div>   
     109
     110        <div id="main-container" class="postbox-container metabox-holder" style="width:75%;">
     111            <div style="margin-right:16px;">
     112        <div class="postbox">
     113            <h3 style="cursor:default;"><span>Download Settings</span></h3>
     114            <div class="inside">
     115                            <p></p>
     116                <form method="post" id="frm_settings">
     117                    <?php if(!empty($existing_settings)) {?>
     118                        <table style="border: solid 0px red; width: 100%;">
     119                            <tr style="height: 35px;">
     120                                <td colspan="4" class="table-heading">Download Settings</td>
     121                            </tr>
     122                            <tr>
     123                                <td class="td-label">Allowed Referred: </td>
     124                                <td class="td-text"><input name="txtReferred" type="text" id="txtReferred" class="download-text" width="300" value="<?php echo $existing_settings[0]->allowed_referred; ?>" /></td>
     125                                <td class="td-label">Base Dir: </td>
     126                                <td class="td-text"><input name="txtBaseDir" type="text" id="txtBaseDir" class="download-text" value="<?php echo $existing_settings[0]->base_dir; ?>" /></td>
     127                            </tr>
     128                            <tr>
     129                                <td class="td-label">Log downloads: </td>
     130                                <td class="td-text" colspan="3" style="text-align: left;"><input type="checkbox" name="chk_log" id="chk_log" <?php if($existing_settings[0]->log_downloads == "1") {echo " Checked ";} ; ?> /> </td>                   
     131                            </tr>
     132                            <tr>
     133                                <td colspan="4" style="text-align: right;">
     134                                    <input type="submit" name="btn_saveSettings" id="btn_saveSettings" class="button-primary" value="Click To Save Settings" />
     135                                </td>
     136                            </tr>
     137                        </table>
     138                    <?php } else { "Something went wrong while plugin activation. Make sure you have database modification rights."; } ?>
     139                </form>
     140        <p>Please follow these instructions to configure settings:</p>
     141        <ol>
     142            <li>Create a directory on your server</li>
     143            <li>'Base Path' is root path of your download directory where all files are hosted. Root path on your server is <code><?php echo get_home_path(); ?></code> followed by path to directory created in step 1<br />
     144            For example if your WordPress installation is in public_html directory and your have created directory named 'files' in 'wp-content' then your base path is <code><?php echo get_home_path(); ?>/wp-content/files/</code></li>
     145            <li>If you want to restrict download from any specific domain only, enter domain name (without http://www) in 'Allowed Referred' else leave it blank.<br />If you add <code><?php echo $_SERVER['SERVER_NAME']; ?></code> in 'Allowed Referred' download will be only allowed from this website.</li>
     146            <li>Uncheck 'Log downloads' if you don't want to keep a track of files downloaded. I would recommend to kep it checked, this will help you to track download of your files.</li>
     147            <li>Save settings</li>
     148        </ol>                 
     149            </div> <!-- .inside -->
     150        </div> <!-- .postbox -->
     151    </div></div>
     152       
     153        <div id="side-container" class="postbox-container metabox-holder" style="width:25%;">
     154        <div class="postbox">
     155            <h3 style="cursor:default;"><span>Do you like this Plugin?</span></h3>
     156            <div class="inside">
     157                <p>Please consider a donation.</p>
     158                <div style="text-align:center">
     159                    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
     160                    <input type="hidden" name="cmd" value="_donations">
     161                    <input type="hidden" name="business" value="[email protected]">
     162                    <input type="hidden" name="lc" value="US">
     163                    <input type="hidden" name="item_name" value="Hide File download path plugin">
     164                    <input type="hidden" name="no_note" value="0">
     165                    <input type="hidden" name="currency_code" value="USD">
     166                    <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest">
     167                    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
     168                    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
     169                    </form>
     170                </div>
     171                <p>
     172                If you wish to help then contact <a href="https://twitter.com/cvedovini">@cvedovini</a> on Twitter or use that <a href="http://vedovini.net/contact/">contact form</a>.</p>
     173            </div> <!-- .inside -->
     174        </div> <!-- .postbox -->
     175
     176    </div>       
    120177   
    121178        <link rel='stylesheet' href='<?php echo PLUGINNAME_URL; ?>/css/download-style.css' type='text/css' media='all' />
     
    123180        <div id="div_addTicker" style="display: ''; width: 1024px; border: solid 1px; margin-bottom: 30px; background-color: lightYellow; border-color: #E6DB55; margin: 5px 70px 30px; border-radius: 3px; padding: 5px;">
    124181           
    125             <form method="post" id="frm_settings">
    126             <?php if(!empty($existing_settings)) {?>
    127                 <table style="border: solid 0px red; width: 100%;">
    128                     <tr style="height: 35px;">
    129                         <td colspan="4" class="table-heading">Download Settings</td>
    130                     </tr>
    131                     <tr>
    132                         <td class="td-label">Allowed Referred: </td>
    133                         <td class="td-text"><input name="txtReferred" type="text" id="txtReferred" class="download-text" width="300" value="<?php echo $existing_settings[0]->allowed_referred; ?>" /></td>
    134                         <td class="td-label">Base Dir: </td>
    135                         <td class="td-text"><input name="txtBaseDir" type="text" id="txtBaseDir" class="download-text" value="<?php echo $existing_settings[0]->base_dir; ?>" /></td>
    136                     </tr>
    137                     <tr>
    138                         <td class="td-label">Log downloads: </td>
    139                         <td class="td-text" colspan="3" style="text-align: left;"><input type="checkbox" name="chk_log" id="chk_log" <?php if($existing_settings[0]->log_downloads == "1") {echo " Checked ";} ; ?> /> </td>                   
    140                     </tr>
    141                     <tr>
    142                         <td colspan="4" style="text-align: right;">
    143                             <input type="submit" name="btn_saveSettings" id="btn_saveSettings" class="button-primary" value="Click To Save Settings" />
    144                         </td>
    145                     </tr>
    146                 </table>
    147             <?php } else { "Jack, Something seems went wrong while activation. Make sure you have DB rights."; } ?>
    148             </form>
    149182        </div>
    150183         
    151     <?php           
     184    <?php   
     185    echo "</div>";         
    152186}
    153187
    154188
    155 /* Function to display performance page starts */
    156 
    157 function download_link_page()
    158 {
    159     global $wpdb;
    160     define( 'PLUGINNAME_URL', plugin_dir_url(__FILE__) );
    161 
    162     $querystr = "SELECT * FROM ".$wpdb->prefix . "download_settings";
    163     $settings = $wpdb->get_results($querystr);
    164 
    165     $allowed_referred = "";
    166     $base_dir = "";
    167     $log_downloads = true;
    168 
    169     if(!empty($settings))
    170     {
    171         foreach($settings as $setting)
    172         {
    173             if(rtrim($setting->allowed_referred) != "")
    174             {
    175                 $allowed_referred =  $setting->allowed_referred;
    176             }
    177            
    178             if(rtrim($setting->base_dir) != "")
    179             {
    180                 $base_dir =  $setting->base_dir;
    181             }           
    182 
    183             if($setting->log_downloads == "0")
    184             {
    185                 $base_dir =  false;
    186             }           
    187         }
    188     }
     189    /* Function to display performance page starts */
     190    function download_link_page()
     191   {
     192       global $wpdb;
     193       define( 'PLUGINNAME_URL', plugin_dir_url(__FILE__) );
     194       $querystr = "SELECT * FROM ".$wpdb->prefix . "download_settings";
     195       $settings = $wpdb->get_results($querystr);
     196       $allowed_referred = "";
     197       $base_dir = "";
     198       $log_downloads = true;
     199   
     200       if(!empty($settings))
     201       {
     202           foreach($settings as $setting)
     203           {
     204               if(rtrim($setting->allowed_referred) != "")
     205               {
     206                   $allowed_referred =  $setting->allowed_referred;
     207               }
     208               if(rtrim($setting->base_dir) != "")
     209               {
     210                   $base_dir =  $setting->base_dir;
     211               }           
     212               if($setting->log_downloads == "0")
     213               {
     214               $base_dir =  false;
     215           }           
     216       }
     217   }
    189218   
    190219    // Allow direct file download (hotlinking)?
    191     // Empty - allow hotlinking
    192     // If set to nonempty value (Example: example.com) will only allow downloads when referrer contains this text
    193     define('ALLOWED_REFERRER', $allowed_referred);
    194 
     220   // Empty - allow hotlinking
     221   // If set to nonempty value (Example: example.com) will only allow downloads when referrer contains this text
     222   define('ALLOWED_REFERRER', $allowed_referred);
    195223    // Download folder, i.e. folder where you keep all files for download.
    196     // MUST end with slash (i.e. "/" )
    197     define('BASE_DIR',$base_dir);
    198 
    199     // log downloads?  true/false
    200     define('LOG_DOWNLOADS',$log_downloads);
    201 
     224   // MUST end with slash (i.e. "/" )
     225   define('BASE_DIR',$base_dir);
     226
     227   // log downloads?  true/false
     228   define('LOG_DOWNLOADS',$log_downloads);
    202229    // log file name
    203     define('LOG_FILE','downloads.log');
    204 
     230   define('LOG_FILE','downloads.log');
    205231    // Allowed extensions list in format 'extension' => 'mime type'
    206     // If myme type is set to empty string then script will try to detect mime type
    207     // itself, which would only work if you have Mimetype or Fileinfo extensions
    208     // installed on server.
     232   // If myme type is set to empty string then script will try to detect mime type
     233   // itself, which would only work if you have Mimetype or Fileinfo extensions
     234   // installed on server.
    209235    $allowed_ext = array (
    210 
    211         // archives
    212         'zip' => 'application/zip',
    213 
    214           // documents
    215         'pdf' => 'application/pdf',
    216         'doc' => 'application/msword',
    217         'xls' => 'application/vnd.ms-excel',
    218         'ppt' => 'application/vnd.ms-powerpoint',
    219  
    220         // executables
    221         'exe' => 'application/octet-stream',
    222 
    223         // images
    224         'gif' => 'image/gif',
    225         'png' => 'image/png',
    226         'jpg' => 'image/jpeg',
    227         'jpeg' => 'image/jpeg',
    228 
    229         // audio
    230         'mp3' => 'audio/mpeg',
    231         'wav' => 'audio/x-wav',
    232        
    233         // video
    234         'mpeg' => 'video/mpeg',
    235         'mpg' => 'video/mpeg',
    236         'mpe' => 'video/mpeg',
    237         'mov' => 'video/quicktime',
    238         'avi' => 'video/x-msvideo'
    239     );
    240 
    241 
    242 
    243     ####################################################################
    244     ###  DO NOT CHANGE BELOW
    245     ####################################################################
    246 
     236       // archives
     237       'zip' => 'application/zip',
     238       // documents
     239       'pdf' => 'application/pdf',
     240       'doc' => 'application/msword',
     241       'xls' => 'application/vnd.ms-excel',
     242       'ppt' => 'application/vnd.ms-powerpoint',
     243       'xlsx' => 'application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',         
     244       // executables
     245       'exe' => 'application/octet-stream',
     246       // images
     247       'gif' => 'image/gif',
     248       'png' => 'image/png',
     249       'jpg' => 'image/jpeg',
     250       'jpeg' => 'image/jpeg',
     251       // audio
     252       'mp3' => 'audio/mpeg',
     253       'wav' => 'audio/x-wav',
     254       // video
     255       'mpeg' => 'video/mpeg',
     256       'mpg' => 'video/mpeg',
     257       'mpe' => 'video/mpeg',
     258       'mov' => 'video/quicktime',
     259       'avi' => 'video/x-msvideo'
     260   );
     261
     262   ####################################################################
     263   ###  DO NOT CHANGE BELOW
     264   ####################################################################
    247265    // If hotlinking not allowed then make hackers think there are some server problems
    248     if (ALLOWED_REFERRER !== ''
    249     && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false)
    250     ) {
    251        
    252         if(strtoupper($_SERVER['HTTP_REFERER']) != home_url())
    253         {
    254             $referredBy = strtoupper($_SERVER['HTTP_REFERER']);
    255             $parent   = strtoupper(home_url());
    256             $pos = strpos($referredBy, $parent);
    257 
     266   if (ALLOWED_REFERRER !== '' && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false))
     267   {
     268       if(strtoupper($_SERVER['HTTP_REFERER']) != home_url())
     269       {
     270           $referredBy = strtoupper($_SERVER['HTTP_REFERER']);
     271           $parent   = strtoupper(home_url());
     272           $pos = strpos($referredBy, $parent);
    258273            if ($pos === false) {
    259                 die("Internal server error. Please contact system administrator.");
    260             } else {
    261 
    262             }
    263         }
     274               die("Internal server error. Please contact system administrator.");
     275           }
     276           else
     277           {
     278           
     279           }
     280       }
     281   }
     282    // Make sure program execution doesn't time out
     283   // Set maximum script execution time in seconds (0 means no limit)
     284   set_time_limit(0);
     285   
     286   if (!isset($_GET['f']) || empty($_GET['f']))
     287   {
     288       die("Sorry No File is specified to download.");
     289   }
     290   
     291   // Nullbyte hack fix
     292   if (strpos($_GET['f'], "\0") !== FALSE) die('');
     293    // Get real file name.
     294   // Remove any path info to avoid hacking by adding relative path, etc.
     295   $fname = basename($_GET['f']);
     296    // get full file path (including subfolders)
     297   $file_path = '';
     298   $file_path = find_file(BASE_DIR, $fname, $file_path);
     299   if (!is_file($file_path)) {
     300       die("File does not exist. Make sure you specified correct file name.");
     301   }
     302   else
     303   {
     304
     305   }
     306    // file size in bytes
     307   $fsize = filesize($file_path);
     308    // file extension
     309   $fext = strtolower(substr(strrchr($fname,"."),1));
     310    // check if allowed extension
     311   if (!array_key_exists($fext, $allowed_ext)) {
     312       die("Not allowed file type.");
     313   }
     314    // get mime type
     315   if ($allowed_ext[$fext] == '') {
     316       $mtype = '';
     317       // mime type is not set, get from server settings
     318       if (function_exists('mime_content_type')) {
     319           $mtype = mime_content_type($file_path);
     320       }
     321       else if (function_exists('finfo_file')) {
     322           $finfo = finfo_open(FILEINFO_MIME); // return mime type
     323           $mtype = finfo_file($finfo, $file_path);
     324           finfo_close($finfo); 
     325       }
     326       if ($mtype == '') {
     327           $mtype = "application/force-download";
     328       }
     329   }
     330   else {
     331       // get mime type defined by admin
     332       $mtype = $allowed_ext[$fext];
     333   }
     334    // Browser will try to save file with this filename, regardless original filename.
     335   // You can override it if needed.
     336
     337  if (!isset($_GET['f']) || empty($_GET['f'])) {
     338       $asfname = $fname;
     339   }
     340   else {
     341       // remove some bad chars
     342       $asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['f']);
     343       if ($asfname === '') $asfname = 'NoName';
     344   }
     345
     346if ($fd = fopen ($file_path, "r")) {
     347    $fsize = filesize($file_path);
     348    $path_parts = pathinfo($file_path);
     349    $ext = strtolower($path_parts["extension"]);
     350
     351    header("Content-type: $mtype"); // add here more headers for diff. extensions
     352    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
     353 
     354    header("Content-length: $fsize");
     355    header("Cache-control: private"); //use this to open files directly
     356   
     357while(ob_get_level() > 0)
     358{
     359    @ob_end_clean();
     360}
     361   
     362    while(!feof($fd)) {
     363        $buffer = fread($fd, 2048);
     364        echo $buffer;
    264365    }
    265 
    266     // Make sure program execution doesn't time out
    267     // Set maximum script execution time in seconds (0 means no limit)
    268     set_time_limit(0);
    269    
    270     if (!isset($_GET['f']) || empty($_GET['f'])) {
    271     die("Sorry No File is specified to download.");
    272     }
    273    
    274     // Nullbyte hack fix
    275     if (strpos($_GET['f'], "\0") !== FALSE) die('');
    276 
    277     // Get real file name.
    278     // Remove any path info to avoid hacking by adding relative path, etc.
    279     $fname = basename($_GET['f']);
     366}
     367fclose ($fd);
     368
     369
     370
     371
     372    // log downloads
     373
     374    if (!LOG_DOWNLOADS) die();
     375
     376
     377
     378    $f = @fopen(LOG_FILE, 'a+');
     379
     380    if ($f) {
     381
     382        @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
     383
     384        @fclose($f);
     385
     386    }   
     387
     388}
    280389
    281390    // Check if the file exists
    282391    // Check in subfolders too
    283     function find_file ($dirname, $fname, &$file_path) {
     392    function find_file ($dirname, $fname, $file_path)
     393    {
     394        //echo $dirname .'<br>'. $fname .'<br>'. $file_path;
    284395       
    285396        $dir = opendir($dirname);
    286        
    287397        while ($file = readdir($dir)) {
    288398            if (empty($file_path) && $file != '.' && $file != '..') {
     
    293403                if (file_exists($dirname.'/'.$fname)) {
    294404                    $file_path = $dirname.'/'.$fname;
    295                     return;
     405                    //echo 'OK FOUND LETS RETURN '.$file_path."<br>";
     406                    return $file_path;
    296407                }
    297408            }
    298409            }
    299410        }
    300 
    301411    } // find_file
    302412
    303     // get full file path (including subfolders)
    304     $file_path = '';
    305     find_file(BASE_DIR, $fname, $file_path);
    306    
    307     if (!is_file($file_path)) {
    308         die("File does not exist. Make sure you specified correct file name.");
    309     }
    310 
    311     // file size in bytes
    312     $fsize = filesize($file_path);
    313 
    314     // file extension
    315     $fext = strtolower(substr(strrchr($fname,"."),1));
    316 
    317     // check if allowed extension
    318     if (!array_key_exists($fext, $allowed_ext)) {
    319         die("Not allowed file type.");
    320     }
    321 
    322     // get mime type
    323     if ($allowed_ext[$fext] == '') {
    324         $mtype = '';
    325         // mime type is not set, get from server settings
    326         if (function_exists('mime_content_type')) {
    327             $mtype = mime_content_type($file_path);
    328         }
    329         else if (function_exists('finfo_file')) {
    330             $finfo = finfo_open(FILEINFO_MIME); // return mime type
    331             $mtype = finfo_file($finfo, $file_path);
    332             finfo_close($finfo); 
    333         }
    334         if ($mtype == '') {
    335             $mtype = "application/force-download";
    336         }
    337     }
    338     else {
    339         // get mime type defined by admin
    340         $mtype = $allowed_ext[$fext];
    341     }
    342 
    343     // Browser will try to save file with this filename, regardless original filename.
    344     // You can override it if needed.
    345 
    346     if (!isset($_GET['fc']) || empty($_GET['fc'])) {
    347         $asfname = $fname;
    348     }
    349     else {
    350         // remove some bad chars
    351         $asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['fc']);
    352         if ($asfname === '') $asfname = 'NoName';
    353     }
    354 
    355     // set headers
    356     header("Pragma: public");
    357     header("Expires: 0");
    358     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    359     header("Cache-Control: public");
    360     header("Content-Description: File Transfer");
    361     header("Content-Type: $mtype");
    362     header("Content-Disposition: attachment; filename=\"$asfname\"");
    363     header("Content-Transfer-Encoding: binary");
    364     header("Content-Length: " . $fsize);
    365 
    366     // download
    367     // @readfile($file_path);
    368     $file = @fopen($file_path,"rb");
    369     if ($file) {
    370         while(!feof($file)) {
    371             print(fread($file, 1024*8));
    372             flush();
    373             if (connection_status()!=0) {
    374             @fclose($file);
    375             die();
    376         }
    377     }
    378     @fclose($file);
    379     }
    380 
    381     // log downloads
    382     if (!LOG_DOWNLOADS) die();
    383 
    384     $f = @fopen(LOG_FILE, 'a+');
    385     if ($f) {
    386         @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
    387         @fclose($f);
    388     }   
    389 }
    390 
    391413    add_shortcode( 'download_page', 'download_link_page' );
     414
    392415 
     416
    393417?>
Note: See TracChangeset for help on using the changeset viewer.