| 1 |
<?php
|
| 2 |
// $Id: filerequest.module,v 1.2.2.2 2006/03/26 15:08:08 elmuerte Exp $
|
| 3 |
|
| 4 |
function filerequest_help($section) {
|
| 5 |
switch ($section) {
|
| 6 |
case 'admin/modules#description':
|
| 7 |
return t('Properly handles file requests for download method "private". Also provides anti leeching features.');
|
| 8 |
}
|
| 9 |
}
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Override the menu for system/files to capture the private.
|
| 13 |
*/
|
| 14 |
function filerequest_menu($may_cache) {
|
| 15 |
$items = array();
|
| 16 |
|
| 17 |
//if ($may_cache) { // never cache to overrule the builtin one
|
| 18 |
$items[] = array('path' => 'system/files', 'title' => t('file download'),
|
| 19 |
'callback' => '_filerequest_download',
|
| 20 |
'access' => TRUE,
|
| 21 |
'weight' => 10,
|
| 22 |
'type' => MENU_CALLBACK);
|
| 23 |
//}
|
| 24 |
|
| 25 |
return $items;
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Show settings. Used for anti-leech and config file generation
|
| 30 |
*/
|
| 31 |
function filerequest_settings() {
|
| 32 |
$form['settings'] = array(
|
| 33 |
'#type' => 'fieldset',
|
| 34 |
'#title' => t('filerequest settings'),
|
| 35 |
);
|
| 36 |
|
| 37 |
$form['settings']['filereq_antileech_enabled'] = array(
|
| 38 |
'#type' => 'checkbox',
|
| 39 |
'#title' => t('Anti-leech enabled'),
|
| 40 |
'#return_value' => 1,
|
| 41 |
'#default_value' => variable_get('filereq_antileech_enabled', true),
|
| 42 |
'#description' => t('Prevent other sites from directly linking to files'),
|
| 43 |
);
|
| 44 |
|
| 45 |
$form['settings']['filereq_antileech_regex'] = array(
|
| 46 |
'#type' => 'textfield',
|
| 47 |
'#title' => t('Valid host'),
|
| 48 |
'#default_value' => variable_get('filereq_antileech_regex', preg_quote($_SERVER["HTTP_HOST"])),
|
| 49 |
'#size' => 72,
|
| 50 |
'#maxlength' => 65535,
|
| 51 |
'#description' => t('A regular expression defining hosts that are allowed to directly link to the file. This should at least cover the current host.'),
|
| 52 |
);
|
| 53 |
|
| 54 |
$opts[0] = "Normal";
|
| 55 |
$opts[1] = "Add a watermark";
|
| 56 |
$opts[2] = "Replace the image";
|
| 57 |
$form['settings']['filereq_antileech_image_mode'] = array(
|
| 58 |
'#type' => 'select',
|
| 59 |
'#title' => t('Image mode'),
|
| 60 |
'#default_value' => variable_get('filereq_antileech_image_mode', 0),
|
| 61 |
'#options' => $opts,
|
| 62 |
'#description' => t('Special behavior when leeching images.'),
|
| 63 |
);
|
| 64 |
|
| 65 |
$form['settings']['filereq_antileech_image_file'] = array(
|
| 66 |
'#type' => 'textfield',
|
| 67 |
'#title' => t('Image file'),
|
| 68 |
'#default_value' => variable_get('filereq_antileech_image_file', ""),
|
| 69 |
'#size' => 72,
|
| 70 |
'#maxlength' => 65535,
|
| 71 |
'#description' => t('The image to used when special image handling is enabled. This path is relative to the drupal base directory. <br />In case of the watermark mode the image has to be a 24 bit PNG file. Hint: make use of the alpha channels for PNGs.'),
|
| 72 |
);
|
| 73 |
|
| 74 |
$form['config'] = array(
|
| 75 |
'#type' => 'fieldset',
|
| 76 |
'#title' => t('modules/filerequest/throttle.config.php'),
|
| 77 |
'#description' => t('Use the following configuration if you added the .htaccess lines as provided in the README.TXT .')
|
| 78 |
);
|
| 79 |
|
| 80 |
$form['config'][] = array(
|
| 81 |
'#value' => "<textarea rows=\"7\" cols=\"72\" readonly=\"readonly\" wrap=\"off\"><?php\n\$config[\"filereq_antileech_enabled\"] = ".variable_get('filereq_antileech_enabled', true).";\n\$config[\"filereq_antileech_regex\"] = \"".addslashes(variable_get('filereq_antileech_regex', preg_quote($_SERVER["HTTP_HOST"])))."\";\n\$config[\"filereq_fspath\"] = \"".addslashes(file_create_path())."\";\n\$config[\"filereq_antileech_image_mode\"] = ".variable_get('filereq_antileech_image_mode', 0).";\n\$config[\"filereq_antileech_image_file\"] = \"".addslashes(variable_get('filereq_antileech_image_file', "modules/filerequest/watermark.png"))."\";\n?></textarea>"
|
| 82 |
);
|
| 83 |
|
| 84 |
return $form;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Process the download request. If it's a file leecher redirect it to the
|
| 89 |
* associated node, otherwise return an access_denied error.
|
| 90 |
*/
|
| 91 |
function _filerequest_download() {
|
| 92 |
define("__DRUPAL_BASE_DIR", dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR);
|
| 93 |
require_once("downloadhandler.php");
|
| 94 |
|
| 95 |
$config["filename"] = $_GET['file'];
|
| 96 |
$config["filereq_antileech_enabled"] = variable_get('filereq_antileech_enabled', true);
|
| 97 |
$config["filereq_antileech_regex"] = variable_get('filereq_antileech_regex', preg_quote($_SERVER["HTTP_HOST"]));
|
| 98 |
$config["filereq_antileech_image_mode"] = variable_get('filereq_antileech_image_mode', 0);
|
| 99 |
$config["filereq_antileech_image_file"] = variable_get('filereq_antileech_image_file', "modules/filerequest/watermark.png");
|
| 100 |
|
| 101 |
if (!__fr_can_download_file($config)) define("__FILEREQ_LEECH", $file);
|
| 102 |
|
| 103 |
if (defined("__FILEREQ_LEECH"))
|
| 104 |
{
|
| 105 |
$result = db_result(db_query("SELECT nid FROM {files} WHERE filepath = '%s'", $config["filename"]));
|
| 106 |
if ($result !== false)
|
| 107 |
{
|
| 108 |
drupal_goto("node/".$result);
|
| 109 |
exit;
|
| 110 |
}
|
| 111 |
drupal_access_denied();
|
| 112 |
return;
|
| 113 |
}
|
| 114 |
$config["filename"] = file_create_path($config["filename"]);
|
| 115 |
if (file_exists($config["filename"])) {
|
| 116 |
$list = module_list();
|
| 117 |
foreach ($list as $module) {
|
| 118 |
$headers = module_invoke($module, 'file_download', $_GET['file']);
|
| 119 |
if (in_array(-1, $headers)) {
|
| 120 |
return drupal_access_denied();
|
| 121 |
}
|
| 122 |
elseif (is_array($headers)) {
|
| 123 |
// make sure no caching headers are set
|
| 124 |
header("Cache-Control:");
|
| 125 |
header("Pragma:");
|
| 126 |
header("Expires:");
|
| 127 |
__fr_process_download($config["filename"], preg_match("#(\?|&)download(&|$)#", $_SERVER["REQUEST_URI"]), $config["watermark"]);
|
| 128 |
exit();
|
| 129 |
}
|
| 130 |
}
|
| 131 |
}
|
| 132 |
drupal_not_found();
|
| 133 |
}
|