Downloadsvn - Noah.org

Downloadsvn

From Noah.org

Jump to: navigation, search

This allows you to download files directly from SVN. It should give you a download and not open in the browser.

This is probably not very secure.

Click to download: downloadsvn.php

<?php
 
/**
 * This grabs a file from SVN and forces a download (instead of displaying it in
 * the browser). This assumes that 'svn cat' will work on the given filename. This
 * is a hack to force most browsers to download and save a file instead of trying
 * to display the file contents. This is especially helpful with PDF and image
 * files when you want to just download the file and not view it. This usually
 * works with IE and Mozilla. Based on http://elouai.com/force-download.php
 *
 * @package
 * @version $Id: downloadsvn.php 137 2007-12-13 03:23:39Z root $
 * @copyright Copyright 2007
 * @author Noah Spurrier
 * @license Public domain, free of licenses and restrictions
 * vi:ts=4:sw=4:expandtab:ft=php:
 */
 
// Use either src= or url=, I don't care. Both should work.
if (isset($_GET['src']))
{
    $url = $_GET['src'];
}
elseif (isset($_GET['url']))
{
    $url = $_GET['url'];
}
else
{
    echo "<html><title>ERROR</title><body>ERROR: No given src URL.</body></html>";
    exit;
}
 
// If plain 'svn cat' fails then try again using 'svn cat
// --config-dir=/tmp'. Plain 'svn cat' worked fine for months
// then just stopped.
// Adding --config-dir=/tmp is a hack that fixed it, but
// I only want to use it if necessary. I wish I knew what
// the root cause was.
$cmd = 'svn cat "' . escapeshellarg($url) . '"';
exec ($cmd, $output, $return_var);
if ($return_var != 0)
{
    $cmd = "svn cat --config-dir=/tmp " . escapeshellarg($url);
    exec ($cmd, $output, $return_var);
}
if ($return_var != 0)
{
    echo "<html><title>File not found</title><body>ERROR: include could not read the given src URL using 'svn cat'.\ncmd: $cmd\nreturn code: $return_var\noutput: " . join("\n", $output) . "\n</body></html>";
    exit;
}
$output = join("\n", $output);
 
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');
 
// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($url,"."),1));
 
switch( $file_extension )
{
    case "pdf": $ctype="application/pdf"; break;
    case "exe": $ctype="application/octet-stream"; break;
    case "zip": $ctype="application/zip"; break;
    case "doc": $ctype="application/msword"; break;
    case "xls": $ctype="application/vnd.ms-excel"; break;
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($url)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".count($output));
#readfile("$url");
echo $output;
exit();
?>
-->