Annotation of /trunk/download.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?php |
| 2 : | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | ||
| 3 : | |||
| 4 : | /** | ||
| 5 : | * Download Manager - Download Script | ||
| 6 : | * | ||
| 7 : | * Simple download manager for Joomla! CMS | ||
| 8 : | * Download Script for ionFiles | ||
| 9 : | * | ||
| 10 : | * PHP versions 4 and 5 | ||
| 11 : | * | ||
| 12 : | * | ||
| 13 : | * @category CategoryName | ||
| 14 : | * @package PackageName | ||
| 15 : | * @author jordan DeLozier <http://www.codecall.net> | ||
| 16 : | * @copyright 1997-2005 CodeCall / Jordan DeLozier | ||
| 17 : | * @link http://www.codecall.net | ||
| 18 : | */ | ||
| 19 : | |||
| 20 : | /* | ||
| 21 : | * Place includes, constant defines and $_GLOBAL settings here. | ||
| 22 : | * Make sure they have appropriate docblocks to avoid phpDocumentor | ||
| 23 : | * construing they are documented by the page-level docblock. | ||
| 24 : | ***********************************************************************/ | ||
| 25 : | |||
| 26 : | // Mod below - commented out unneeded crap | ||
| 27 : | |||
| 28 : | # Disable all warnings | ||
| 29 : | #------------------------------------ | ||
| 30 : | /*error_reporting(0); | ||
| 31 : | |||
| 32 : | # Get our file download information | ||
| 33 : | #------------------------------------ | ||
| 34 : | $file = $_GET['file']; | ||
| 35 : | $download = $_GET['download']; | ||
| 36 : | |||
| 37 : | # Run Checks | ||
| 38 : | #---------------------------- | ||
| 39 : | if ($file == "") { | ||
| 40 : | print "Error: No File Selected"; | ||
| 41 : | die(); | ||
| 42 : | }*/ | ||
| 43 : | |||
| 44 : | |||
| 45 : | // Mod blow, added function header | ||
| 46 : | function startDownload($file, $download) { | ||
| 47 : | |||
| 48 : | # Make sure program execution doesn't time out | ||
| 49 : | #------------------------------- | ||
| 50 : | set_time_limit(0); | ||
| 51 : | |||
| 52 : | # Get the filename to be downloaded | ||
| 53 : | #---------------------------------- | ||
| 54 : | $file = stripslashes($file); | ||
| 55 : | $file = htmlspecialchars($file); | ||
| 56 : | |||
| 57 : | # file extension | ||
| 58 : | #----------------------------------- | ||
| 59 : | $fext = strtolower(substr(strrchr($file,"."),1)); | ||
| 60 : | |||
| 61 : | # Get the File Size | ||
| 62 : | #----------------------------------- | ||
| 63 : | //$fsize = remotefsize($file); | ||
| 64 : | |||
| 65 : | # Get the mime type | ||
| 66 : | #------------------------------------ | ||
| 67 : | $mtype = getmtype($fext); | ||
| 68 : | |||
| 69 : | # Check to see if the file is a link or download | ||
| 70 : | #------------------------------------------------- | ||
| 71 : | if ($download == 1) { | ||
| 72 : | header("Pragma: no-cache"); | ||
| 73 : | header("Expires: 0"); | ||
| 74 : | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | ||
| 75 : | header("Cache-Control: public"); | ||
| 76 : | header("Content-Description: File Transfer"); | ||
| 77 : | header("Content-type: $mtype"); | ||
| 78 : | header("Content-Transfer-Encoding: binary"); | ||
| 79 : | header("Content-Disposition: attachment; filename=\"".basename($file)."\""); | ||
| 80 : | |||
| 81 : | |||
| 82 : | if (ini_get('allow_url_fopen')) { | ||
| 83 : | if (extension_loaded('curl')) { | ||
| 84 : | header("Content-Length: " . remotefsize($file)); | ||
| 85 : | } | ||
| 86 : | } | ||
| 87 : | |||
| 88 : | header("Accept-Ranges: bytes"); | ||
| 89 : | @readfile("$file"); | ||
| 90 : | |||
| 91 : | } | ||
| 92 : | else { | ||
| 93 : | # Forward the user to the file | ||
| 94 : | #-------------------------------------------------- | ||
| 95 : | header("Location: " .$file); | ||
| 96 : | } | ||
| 97 : | |||
| 98 : | } | ||
| 99 : | //////////////////////////////////////////////////////////// | ||
| 100 : | // Get the mtype | ||
| 101 : | //////////////////////////////////////////////////////////// | ||
| 102 : | function getmtype($fext) { | ||
| 103 : | |||
| 104 : | # Mime Types | ||
| 105 : | #------------------------------- | ||
| 106 : | $mine_types = array ( | ||
| 107 : | |||
| 108 : | # archives | ||
| 109 : | #------------------------------- | ||
| 110 : | 'zip' => 'application/zip', | ||
| 111 : | |||
| 112 : | # documents | ||
| 113 : | #------------------------------- | ||
| 114 : | 'pdf' => 'application/pdf', | ||
| 115 : | 'doc' => 'application/msword', | ||
| 116 : | 'xls' => 'application/vnd.ms-excel', | ||
| 117 : | 'ppt' => 'application/vnd.ms-powerpoint', | ||
| 118 : | |||
| 119 : | # executables | ||
| 120 : | #------------------------------- | ||
| 121 : | 'exe' => 'application/octet-stream', | ||
| 122 : | |||
| 123 : | # images | ||
| 124 : | #------------------------------- | ||
| 125 : | 'gif' => 'image/gif', | ||
| 126 : | 'png' => 'image/png', | ||
| 127 : | 'jpg' => 'image/jpeg', | ||
| 128 : | 'jpeg' => 'image/jpeg', | ||
| 129 : | |||
| 130 : | # audio | ||
| 131 : | #------------------------------- | ||
| 132 : | 'mp3' => 'audio/mpeg', | ||
| 133 : | 'wav' => 'audio/x-wav', | ||
| 134 : | |||
| 135 : | # video | ||
| 136 : | #------------------------------- | ||
| 137 : | 'mpeg' => 'video/mpeg', | ||
| 138 : | 'mpg' => 'video/mpeg', | ||
| 139 : | 'mpe' => 'video/mpeg', | ||
| 140 : | 'mov' => 'video/quicktime', | ||
| 141 : | 'avi' => 'video/x-msvideo' | ||
| 142 : | |||
| 143 : | ); | ||
| 144 : | |||
| 145 : | # Find the Mime Type | ||
| 146 : | #------------------------------- | ||
| 147 : | if ($mine_types[$fext] == '') { | ||
| 148 : | $mtype = ''; | ||
| 149 : | |||
| 150 : | # mime type is not set, get from server settings | ||
| 151 : | #------------------------------- | ||
| 152 : | if (function_exists('mime_content_type')) { | ||
| 153 : | $mtype = mime_content_type($file); | ||
| 154 : | } | ||
| 155 : | else | ||
| 156 : | { | ||
| 157 : | $mtype = "application/force-download"; | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | } | ||
| 161 : | else { | ||
| 162 : | # get mime type defined by admin | ||
| 163 : | #------------------------------- | ||
| 164 : | $mtype = $mine_types[$fext]; | ||
| 165 : | } | ||
| 166 : | |||
| 167 : | # Return our Type | ||
| 168 : | #------------------------------- | ||
| 169 : | return $mtype; | ||
| 170 : | } | ||
| 171 : | |||
| 172 : | //////////////////////////////////////////////////////////// | ||
| 173 : | // Filesize of remote file | ||
| 174 : | // Grab the filesize of a file hosted on another server | ||
| 175 : | //////////////////////////////////////////////////////////// | ||
| 176 : | function remotefsize($url, $user = "", $pw = "") { | ||
| 177 : | ob_start(); | ||
| 178 : | $ch = curl_init($url); | ||
| 179 : | curl_setopt($ch, CURLOPT_HEADER, 1); | ||
| 180 : | curl_setopt($ch, CURLOPT_NOBODY, 1); | ||
| 181 : | |||
| 182 : | if(!empty($user) && !empty($pw)) | ||
| 183 : | |||
| 184 : | { | ||
| 185 : | $headers = array('Authorization: Basic ' . base64_encode("$user:$pw")); | ||
| 186 : | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | ||
| 187 : | } | ||
| 188 : | |||
| 189 : | |||
| 190 : | $ok = curl_exec($ch); | ||
| 191 : | curl_close($ch); | ||
| 192 : | $head = ob_get_contents(); | ||
| 193 : | ob_end_clean(); | ||
| 194 : | |||
| 195 : | $regex = '/Content-Length:\s([0-9].+?)\s/'; | ||
| 196 : | $count = preg_match($regex, $head, $matches); | ||
| 197 : | |||
| 198 : | return isset($matches[1]) ? $matches[1] : "unknown"; | ||
| 199 : | } | ||
| 200 : | |||
| 201 : | |||
| 202 : | // Old Code - PHP 5 only | ||
| 203 : | /* function remotefsize($url) { | ||
| 204 : | |||
| 205 : | if (ini_get('allow_url_fopen')) { | ||
| 206 : | $schemed = parse_url($url); | ||
| 207 : | $sch = $schemed['scheme']; | ||
| 208 : | |||
| 209 : | //$sch = parse_url($url, PHP_URL_SCHEME); | ||
| 210 : | |||
| 211 : | if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) { | ||
| 212 : | return false; | ||
| 213 : | } | ||
| 214 : | if (($sch == "http") || ($sch == "https")) { | ||
| 215 : | $headers = get_headers($url, 1); | ||
| 216 : | if ((!array_key_exists("Content-Length", $headers))) { return false; } | ||
| 217 : | return $headers["Content-Length"]; | ||
| 218 : | } | ||
| 219 : | if (($sch == "ftp") || ($sch == "ftps")) { | ||
| 220 : | $parsed = parse_url($url); | ||
| 221 : | $server = $parsed['host']; | ||
| 222 : | $port = $parsed['port']; | ||
| 223 : | $path = $parsed['path']; | ||
| 224 : | $user = $parsed['user']; | ||
| 225 : | $pass = $parsed['pass']; | ||
| 226 : | |||
| 227 : | /* PHP 5.0++ Code | ||
| 228 : | $server = parse_url($url, PHP_URL_HOST); | ||
| 229 : | $port = parse_url($url, PHP_URL_PORT); | ||
| 230 : | $path = parse_url($url, PHP_URL_PATH); | ||
| 231 : | $user = parse_url($url, PHP_URL_USER); | ||
| 232 : | $pass = parse_url($url, PHP_URL_PASS); | ||
| 233 : | |||
| 234 : | if ((!$server) || (!$path)) { return false; } | ||
| 235 : | if (!$port) { $port = 21; } | ||
| 236 : | if (!$user) { $user = "anonymous"; } | ||
| 237 : | if (!$pass) { $pass = "phpos@"; } | ||
| 238 : | switch ($sch) { | ||
| 239 : | case "ftp": | ||
| 240 : | $ftpid = ftp_connect($server, $port); | ||
| 241 : | break; | ||
| 242 : | case "ftps": | ||
| 243 : | $ftpid = ftp_ssl_connect($server, $port); | ||
| 244 : | break; | ||
| 245 : | } | ||
| 246 : | if (!$ftpid) { return false; } | ||
| 247 : | $login = ftp_login($ftpid, $user, $pass); | ||
| 248 : | if (!$login) { return false; } | ||
| 249 : | $ftpsize = ftp_size($ftpid, $path); | ||
| 250 : | ftp_close($ftpid); | ||
| 251 : | if ($ftpsize == -1) { return false; } | ||
| 252 : | return $ftpsize; | ||
| 253 : | } | ||
| 254 : | } | ||
| 255 : | else | ||
| 256 : | { | ||
| 257 : | return false; | ||
| 258 : | } | ||
| 259 : | } */ | ||
| 260 : | |||
| 261 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

