Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function rglob($pattern='*', $flags = 0, $path=false)
- {
- if (!$path) { $path=dirname($pattern).DIRECTORY_SEPARATOR; }
- $pattern=basename($pattern);
- $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
- $files=glob($path.$pattern, $flags);
- foreach ($paths as $path) {
- $files=array_merge($files,rglob($pattern, $flags, $path));
- }
- return $files;
- }
- function myurlencode($string) {
- $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
- $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
- return str_replace($entities, $replacements, urlencode($string));
- }
- function disp_files($pattern, $path='/data/tf/gecko')
- {
- $files = rglob($pattern, 0, $path);
- echo "<table border=1><tr><td>FileName</td><td>List</td></tr>";
- foreach ( $files as $file)
- {
- if (preg_match('/finished$/',dirname($file)))
- {
- continue;
- }
- $file = str_replace("%2F", "/", $file);
- echo "<tr><td>".basename($file)."</td><td><a href=\"rar".$file."\">List</a></td></tr>";
- }
- echo "</table>";
- }
- function detectUserAgent() {
- if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
- return "Other";
- $uas = $_SERVER['HTTP_USER_AGENT'];
- if (preg_match("@Opera/@", $uas))
- return "Opera";
- if (preg_match("@Firefox/@", $uas))
- return "Firefox";
- if (preg_match("@Chrome/@", $uas))
- return "Chrome";
- if (preg_match("@MSIE ([0-9.]+);@", $uas, $matches)) {
- if (((float)$matches[1]) >= 7.0)
- return "IE";
- }
- return "Other";
- }
- function format_bytes($size) {
- $units = array(' B', ' KB', ' MB', ' GB', ' TB');
- for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
- return round($size, 2).$units[$i];
- }
- function crunch_uri($params)
- {
- global $keywords;
- $cur_item = array_shift($params);
- switch ($cur_item)
- {
- case 'rar':
- $response['rar']['set'] = true;
- $response['rar']['param'] = urldecode(array_shift($params));
- break;
- case 'ex':
- $response['ex']['set'] = true;
- $response['ex']['param'] = urldecode(array_shift($params));
- break;
- case 'list':
- $response['list']['set'] = true;
- break;
- }
- if(!empty($params) && $params[0] != "")
- {
- $response += crunch_uri($params, $method);
- }
- return $response;
- }
- $keywords[] = 'rar';
- $keywords[] = 'ex';
- $keywords[] = 'list';
- $params = explode( '/', $_SERVER['REQUEST_URI']);
- while (strtolower(array_shift($params)) != 'www'): endwhile;
- if(!empty($params) && $params[0] != "")
- {
- $crunched_uri = crunch_uri($params,$method);
- }
- else
- {
- $crunched_uri['list']['set'] = true;
- }
- if (! isset($crunched_uri['rar']['param']))
- {
- disp_files('*.rar');
- }
- else
- {
- $filename=$crunched_uri['rar']['param'];
- $rar_file = rar_open($filename);
- if (isset($crunched_uri['ex']['param']))
- {
- $returnfile=$crunched_uri['ex']['param'];
- $formatRFC2231 = 'Content-Disposition: attachment; filename*=UTF-8\'\'%s';
- $formatDef = 'Content-Disposition: attachment; filename="%s"';
- $orfilename = urldecode($returnfile);
- $entry = $rar_file->getEntry($orfilename);
- $filesize = $entry->getUnpackedSize();
- $fp = $entry->getStream();
- $rar_file->close();
- switch (detectUserAgent()) {
- case "Opera":
- case "Firefox":
- $orfilename = rawurlencode($orfilename);
- $format = $formatRFC2231;
- break;
- case "IE":
- case "Chrome":
- $orfilename = rawurlencode($orfilename);
- $format = $formatDef;
- break;
- default:
- if (function_exists('iconv'))
- $orfilename =
- @iconv("UTF-8", "ISO-8859-1//TRANSLIT", $orfilename);
- $format = $formatDef;
- }
- header(sprintf($format, $orfilename));
- //cannot send error messages from now on (headers already sent)
- //replace by real content type, perhaps infering from the file extension
- $contentType = "application/octet-stream";
- header("Content-Type: $contentType");
- header("Content-Transfer-Encoding: binary");
- header("Content-Length: $filesize");
- if ($_SERVER['REQUEST_METHOD'] == "HEAD")
- die();
- while (!feof($fp)) {
- $s = @fread($fp, 8192);
- if ($s === false)
- break; //useless to send error messages
- echo $s;
- }
- }
- else {
- $file_list = rar_list($rar_file);
- # print_r($file_list);
- echo "<table border=1><tr><td>Filename</td><td>Size</td><td>F</td><td>D</td></tr>";
- foreach($file_list as $file)
- {
- $name= $file->getName();
- $size = format_bytes($file->getUnpackedSize());
- $isfolder = $file->isDirectory();
- echo "<tr><td>$name</td><td>$size</td><td>$isfolder</td><td><a href=\"rar/".myurlencode($filename)."/ex/".myurlencode($name)."\">D</a></td></tr>";
- }
- echo "</table>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement