<?php

$url = '';
$file_name ='';

if (isset ($_REQUEST["url"])) {
	$url = $_REQUEST["url"];
}

if (isset ($_REQUEST["file"])) {
	$file_name = $_REQUEST["file"];
}
else {
	$file_name = 'tempfile';
}

echo mycurl ($url, $file_name);

function mycurl ($url, $file_name) {
	$output = '';
	if ($url) {
		$url = preg_replace ('/^http.*:\/\//', '', $url);
		$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
		$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
		$header[] = "Cache-Control: max-age=0";
		$header[] = "Connection: keep-alive";
		$header[] = "Keep-Alive: 300";
		$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
		$header[] = "Accept-Language: en-us,en;q=0.5";
		//~ $header[] = "Pragma: ";

		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0");
		curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt ($ch, CURLOPT_REFERER, 'https://duckduckgo.com/');
		curl_setopt ($ch, CURLOPT_ENCODING, 'gzip,deflate');
		curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
		$response = curl_exec ($ch);

		curl_close($ch);

		if ($response) {
			if (file_put_contents ($file_name, $response)) {
				$output .= "curl: file saved to '".$file_name."'\n";
				$output .= "curl: file size ".strlen ($response)." bytes\n";
			}
			else {
				$output .= "curl: cannot save file to '/$file_name': Permission denied\n";
			}
		}
		else {
			$output .= "curl: no response\n";
		}
	}
	else {
		$output .= "curl: no URL\n";
	}
	return $output;
}
?>
