• Revista PROGRAMAR: Já está disponível a edição #53 da revista programar. Faz já o download aqui!

kingless

[PHP]Download Script

7 mensagens neste tópico

Criei este script de downloads a algumas semanas atrás e como esta em testes não disponibilizei antes, anyway aqui fica o código

Download:

http://www.kingless.net/download.php?filename=download.php

Source Code:

http://www.kingless.net/download.php?filename=download.php;source

Demo:

O que vais usar para fazer download do script e/ou ver o source code xp

<?php
/**
* @version $Id: download.php,v 1.5 10-03-2007 19:01:07 kingless Exp $
* @package Download Script!
* @copyright Copyright (c) 2007 by Luis Filipe (http://www.kingless.net).
* 
* Download Script is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Download Script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
* @license http://www.gnu.org/copyleft/gpl.txt
*
* @version Download Script 1.5
* @author Luis Filipe (kingless) - <blastelf@gmail.com>
*/

define( 'ROOT_PATH', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );

/**
* Pasta onde ficam os ficheiros disponiveis para download
*/
define( 'DOWNLOAD_PATH', ROOT_PATH );

/**
* true ou false
* Se GESHI for igual a false será utilizado o hightlight padrão do PHP
* Se quiser utilizar o highlight do GeSHi faça download do GeSHi em
* http://qbnz.com/highlighter/ ou 
* http://www.kingless.net/download.php?filename=GeSHi-1.0.7.17.tar.gz
*/
define( 'GESHI', false );

/**
* Pasta onde esta lozalicado o GeSHi
*/
define( 'GESHI_PATH', ROOT_PATH.DS.'geshi' ); //Padrão

/**
* Pasta onde estão as linguagens suportadas pelo GeSHi 
*/
define( 'GESHI_LANG_PATH', GESHI_PATH.DS.'geshi' ); //Padrão

$file = new file( @$_GET['filename'] );
$file->download();

class file {

var $msg = '';
var $source = '';
var $file;
/** Disabilita o highlight para certos tipos de ficheiros */
var $highlight_disable = 'zip|exe|tar|rar|gif|jpeg|jpg|png';

function file ( $name = '' ) {

	if(empty( $name )) {
		die( $this->form() );
	}

	Header( 'Content-Type: text/html; charset=iso-8859-1' );

	if($file = explode( ';', $name )) {
		@list( $file, $this->source ) = $file;
	}

	$this->file = $file;

	if(!$this->is_valid( $file )) {
		die( $this->msg );
	}
}

function is_valid( $file ) {

	switch( $file ) {
		case eregi( '\.\.\/', $file ) OR eregi( '\.\/', $file ):
			$this->msg = "Tentativa de Hack";
			break;
		case eregi( '\.htaccess', $file ):
			 $this->msg = "Não foi possivel fazer download do ficheiro \"$file\"";
			 break;
		case !file_exists( DOWNLOAD_PATH.DS.$file ):
			 $this->msg =  "O ficheiro \"$file\" não existe!";
			 break;
		default:
			 return true;
	}

	return false;
}

function form () {

	$form[] = '<html><head><title>Download Script 1.5</title></head><body><center><form action="" method="get">';
	$form[] = 'Ficheiro: <input style="border: 1px solid silver;" type="text" name="filename" value="" />';
	$form[] = '<input style="border: 1px solid silver;" type="submit" name="Download" value="Download" />';
	$form[] = '</form></center></body></html>';

	return implode( "\r\n", $form );
}

function download() {
	$filepath = DOWNLOAD_PATH.DS.$this->file;

	if(ereg( '^source$', $this->source )) {
		return $this->highlight_file( $filepath );
	}

	header("Pragma: public");
	header('Cache-Control:  no-cache, must-revalidate');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime( $filepath )) . ' GMT');
	header('Accept-Ranges: bytes');
	header('Content-Type: '. $this->mime_content_type( $this->file ));
	header('Content-Disposition: attachment; filename="'. $this->file .'";');
	header('Content-Transfer-Encoding: binary');
	header('Content-Length: ' . filesize( $filepath ));
	header('Connection: close');	
	readfile( $filepath );
}

function highlight_file( $file ) {

	if(!eregi('\.('.$this->highlight_disable .')$', $file )) {

		if(!GESHI) {
			return highlight_file( $file );
		}

		include_once( GESHI_PATH.DS.'geshi.php' );

		if(!is_readable( $file )) {
			return false;
		}

		$source = file_get_contents( $file );

		if (get_magic_quotes_gpc()) {
			$source = stripslashes( $source );
		}

		$lang = GeSHi::get_language_name_from_extension(substr(strrchr( $file, '.' ), 1 ));

		$geshi =& new GeSHi( trim($source), $lang );

		$geshi->set_language_path( GESHI_LANG_PATH );

		$geshi->set_header_type( GESHI_HEADER_DIV );

		$geshi->enable_classes();
    
		$geshi->enable_line_numbers( GESHI_FANCY_LINE_NUMBERS, 5);

		$geshi->set_overall_style('color: #000066; border: 1px solid #336abe; background-color: #f2f2f2;', true);
    
		$geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #163d77;', 'font-weight: bold; color: #163d77;', true);
		$geshi->set_code_style('color: #000020;', 'color: #000020;');

		$geshi->set_link_styles(GESHI_LINK, 'color: #163d77;');
		$geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');

		$geshi->set_header_content('Source Code do ficheiro "'. $this->file .'" <div style="position:absolute; right: 20px; top: 11px; border: 0px; margin: 0px; padding: 0px; text-align: right;"> Gerado em <TIME> segundos </div>' );
		$geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #fcfcfd; font-size: 70%; font-weight: bold; background-color: #4582DF; border-bottom: 1px solid #d0d0d0; padding: 2px;');

		$geshi->set_footer_content('Copyright © 2007 Download Script by <a href="http://www.kingless.net/" style="text-decoration: none; color: #fcfcfd; font-family: Verdana, Arial, sans-serif;">kingless</a>. Todos os direitos reservados.');
		$geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #fcfcfd; font-size: 70%; font-weight: bold; background-color: #4582DF; border-top: 1px solid #d0d0d0; padding: 2px;');

		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt" lang="pt">
		<head>
		<title>Source Code do ficheiro '. $this->file .'</title>
		<meta name="Copyright" content="Copyright (c) 2007 kingless (http://www.kingless.net)" />
		<meta name="Generator" content="GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter)" />';

		echo '<style type="text/css">';

		echo $geshi->get_stylesheet();

		echo '</style>';

		echo '</head><body>';

		echo $geshi->parse_code();

		echo '</body></html>';

		return true;
	}
	return false;
}

function mime_content_type( $file ) {

$filetype = substr(strrchr( $file, '.' ), 1 );

$mimetypes = array(
		"ez" => "application/andrew-inset",
		"rar" => "application/archive-rar",
		"atom" => "application/atom+xml",
		"ccad" => "application/clariscad",
		"drw" => "application/drafting",
		"tsp" => "application/dsptype",
		"dxf" => "application/dxf",
		"unv" => "application/i-deas",
		"hqx" => "application/mac-binhex40",
		"cpt" => "application/mac-compactpro",
		"pps" => "application/mspowerpoint",
		"doc" => "application/msword",
		"bin" => "application/octet-stream",
		"oda" => "application/oda",
		"pdf" => "application/pdf",
		"ps" => "application/postscript",
		"prt" => "application/pro_eng",
		"set" => "application/set",
		"smil" => "application/smil",
		"sol" => "application/solids",
		"vda" => "application/vda",
		"mif" => "application/vnd.mif",
		"xlw" => "application/vnd.ms-excel",
		"ppt" => "application/vnd.ms-powerpoint",
		"wbxml" => "application/vnd.wap.wbxml",
		"wmlc" => "application/vnd.wap.wmlc",
		"wmlsc" => "application/vnd.wap.wmlscriptc",
		"bcpio" => "application/x-bcpio",
		"vcd" => "application/x-cdlink",
		"pgn" => "application/x-chess-pgn",
		"cpio" => "application/x-cpio",
		"csh" => "application/x-csh",
		"dxr" => "application/x-director",
		"dvi" => "application/x-dvi",
		"pre" => "application/x-freelance",
		"spl" => "application/x-futuresplash",
		"gtar" => "application/x-gtar",
		"gz" => "application/x-gzip",
		"hdf" => "application/x-hdf",
		"php" => "application/x-httpd-php",
		"phps" => "application/x-httpd-php-source",
		"ipx" => "application/x-ipix",
		"ips" => "application/x-ipscript",
		"js" => "application/x-javascript",
		"skp" => "application/x-koan",
		"latex" => "application/x-latex",
		"lsp" => "application/x-lisp",
		"scm" => "application/x-lotusscreencam",
		"cdf" => "application/x-netcdf",
		"sh" => "application/x-sh",
		"shar" => "application/x-shar",
		"flv" => "application/x-shockwave-flash",
		"sit" => "application/x-stuffit",
		"sv4cpio" => "application/x-sv4cpio",
		"sv4crc" => "application/x-sv4crc",
		"tar" => "application/x-tar",
		"tcl" => "application/x-tcl",
		"tex" => "application/x-tex",
		"texinfo" => "application/x-texinfo",
		"t" => "application/x-troff",
		"man" => "application/x-troff-man",
		"me" => "application/x-troff-me",
		"ms" => "application/x-troff-ms",
		"ustar" => "application/x-ustar",
		"src" => "application/x-wais-source",
		"xhtml" => "application/xhtml+xml",
		"zip" => "application/zip",
		"tsi" => "audio/TSP-audio",
		"au" => "audio/basic",
		"mid" => "audio/midi",
		"mpga" => "audio/mpeg",
		"aiff" => "audio/x-aiff",
		"m3u" => "audio/x-mpegurl",
		"ram" => "audio/x-pn-realaudio",
		"rpm" => "application/x-rpm",
		"ra" => "audio/x-realaudio",
		"wav" => "audio/x-wav",
		"wma" => "audio/x-wma",
		"pdb" => "chemical/x-pdb",
		"xyz" => "chemical/x-xyz",
		"bmp" => "image/bmp",
		"gif" => "image/gif",
		"ief" => "image/ief",
		"jpg" => "image/jpeg",
		"png" => "image/png",
		"tiff" => "image/tiff",
		"djv" => "image/vnd.djvu",
		"wbmp" => "image/vnd.wap.wbmp",
		"ras" => "image/x-cmu-raster",
		"pnm" => "image/x-portable-anymap",
		"pbm" => "image/x-portable-bitmap",
		"pgm" => "image/x-portable-graymap",
		"ppm" => "image/x-portable-pixmap",
		"rgb" => "image/x-rgb",
		"xbm" => "image/x-xbitmap",
		"xpm" => "image/x-xpixmap",
		"xwd" => "image/x-xwindowdump",
		"igs" => "model/iges",
		"silo" => "model/mesh",
		"wrl" => "model/vrml",
		"css" => "text/css",
		"htm" => "text/html",
		"c" => "text/plain",
		"rtx" => "text/richtext",
		"rtf" => "text/rtf",
		"sgml" => "text/sgml",
		"tsv" => "text/tab-separated-values",
		"wml" => "text/vnd.wap.wml",
		"wmls" => "text/vnd.wap.wmlscript",
		"etx" => "text/x-setext",
		"xml" => "text/xml",
		"mpg" => "video/mpeg",
		"qt" => "video/quicktime",
		"mxu" => "video/vnd.mpegurl",
		"fli" => "video/x-fli",
		"wmv" => "video/x-msvideo",
		"movie" => "video/x-sgi-movie",
		"ice" => "x-conference/x-cooltalk"
		 );

 	return implode( '', array_keys( array_flip( $mimetypes ), $filetype ));
       }

}
?> 

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Não á demo para testes ? Não percebi muito bem a função disto

Este script serve para fazer download de ficheiros e visualizar o código de fonte

Demo:

http://www.kingless.net/download.php?filename=define.php;source '>http://www.kingless.net/download.php?filename=define.php;source 

http://www.kingless.net/download.php?filename=define.php

EDIT:

Alterei a descrição do script para facilitar a compreensão do que o script faz

http://www.kingless.net/index.php?fileinfo=ZG93bmxvYWQ=

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Em Firefox 2.0 tens um bug de apresentação, no tempo de geração de página.

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Download Script 1.5 Final

Já esta disponível para download a versão final do Download Script 1.5.

Download:

http://www.kingless.net/download.php?filename=download.php'>http://www.kingless.net/download.php?filename=download.php

Source Code:

http://www.kingless.net/download.php?filename=download.php'>http://www.kingless.net/download.php?filename=download.php;source

Patch:

http://www.kingless.net/download.php?filename=patch.download.diff'>http://www.kingless.net/download.php?filename=patch.download.diff

Foi adicionada uma form HTML ao script...

Exemplo da Form:

http://www.kingless.net/download.php

0

Partilhar esta mensagem


Link para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!


Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.


Entrar Agora