Skip to content
Snippets Groups Projects

Resolve "Fix Downloading of files"

Merged Dominik Hebeler requested to merge 20-fix-downloading-of-files into master
3 files
+ 85
30
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -181,6 +181,12 @@ class ProxyController extends Controller
$answer = Cache::get($hash);
}
if(!empty($answer["error"])){
if($answer["error"] === CURLE_ABORTED_BY_CALLBACK){
return response(view("errors.413"), 413);
}
}
if ($result === null) {
return $this->streamFile($targetUrl);
} else {
@@ -194,14 +200,9 @@ class ProxyController extends Controller
$contentEncoding = stripos($contentTypeHeader, "charset=") !== false ? trim(substr($contentTypeHeader, stripos($contentTypeHeader, "charset=") + 8)) : null;
$contentEncoding = rtrim($contentEncoding, ";");
if (isset($answer["headers"]["content-disposition"])) {
if (stripos($answer["headers"]["content-disposition"], "filename=") === false) {
$basename = basename(parse_url($targetUrl, PHP_URL_PATH));
$newHeader = $answer["headers"]["content-disposition"];
$newHeader = trim($newHeader);
$newHeader = rtrim($newHeader, ";");
$newHeader .= "; filename=" . $basename;
$result["headers"]["content-disposition"] = $newHeader;
}
// File Downloads aren't working anymore within an IFrame.
// We will show the user a page to download the File
return response(view("errors.413"), 413);
}
$body = base64_decode($answer["body"]);
switch ($contentType) {
@@ -240,6 +241,7 @@ class ProxyController extends Controller
case 'application/x-www-form-urlencoded':
case 'application/zip':
case 'binary/octet-stream':
case 'application/vnd.android.package-archive':
# Nothing to do with Images: Just return them
break;
case 'text/css':
@@ -262,7 +264,24 @@ class ProxyController extends Controller
->withHeaders($answer["headers"]);
}
private function streamFile($url)
public function streamFile(Request $request, $password, $id, $url){
/**
* Forms of proxied webpages might aswell Post to this URL
* Those need to be denied
*/
$check = md5(env('PROXY_PASSWORD') . date('dmy') . $request->ip());
if(!$request->filled("force-download") && $request->input("check", "") === $check){
abort(405);
}
$targetUrl = str_replace("<<SLASH>>", "/", $url);
$targetUrl = str_rot13(base64_decode($targetUrl));
if (strpos($targetUrl, URL::to('/')) === 0) {
return redirect($targetUrl);
}
return $this->streamResponse($targetUrl);
}
private function streamResponse($url)
{
$headers = get_headers($url, 1);
@@ -276,30 +295,28 @@ class ProxyController extends Controller
# Add the Filename if it's not set:
if (!isset($headers["Content-Disposition"])) {
$headers["Content-Disposition"] = "inline; filename=\"" . $filename . "\"";
}elseif(preg_match("/filename=\"{0,1}(.*?)(\"|\s|$)/", $headers["Content-Disposition"], $matches)){
$filename = $matches[1];
}
$response = new StreamedResponse(function () use ($url) {
return response()->streamDownload(function() use ($url){
# We are gonna stream a large file
$wh = fopen('php://output', 'r+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 50000);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 5);
curl_setopt($ch, CURLOPT_FILE, $wh); // Data will be sent to our stream ;-)
curl_exec($ch);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data){
echo($data);
});
curL_exec($ch);
curl_close($ch);
// Don't forget to close the "file" / stream
fclose($wh);
}, 200, $headers);
$response->send();
return $response;
}, $filename, $headers);
}
public function proxifyUrl($url, $password = null, $key, $topLevel)
Loading