<?php
	header('Cache-Control: no-cache, must-revalidate');
	header('Expires: Wed, 09 Sep 2009 09:09:09 GMT');
	
	if (isset($_REQUEST['ts']))
	{
		$cval = microtime(true) - 396.25792;
		setcookie('timestamp', $cval);
		echo '<h1>upload started at ['.$cval.']</h1>';
		die();
	}
	$ts1 = 0;
	$ts2 = microtime(true);
	if (isset($_FILES) &&
		isset($_FILES['f']) &&
		isset($_COOKIE['timestamp']))
	{
		$ts1 = floatval($_COOKIE['timestamp']) + 396.25792;
		setcookie('timestamp', '', time() - 3600);
	}

?><!DOCTYPE html><html><head>
	<meta charset="utf-8" />
	<title>upper</title>
	<meta name="viewport" content="width:device-width, initial-scale=1" />
	<style>
input {
	display: block;
}
</style></head><body>
	<div id="tb">
		<h1>okay fine i'll add a fucking description jesus christ</h1>
		<p>put files into the boxes below</p>
		<p>any boxes are fine, any order is fine, any number is fine</p>
		<p>press any of the submit buttons, any button is fine</p>
		<h1>there was that so goddamn hard</h1>
		<h1>fuck</h1>
	</div>
	<form method="post" enctype="multipart/form-data" id="uf">
		<input type="submit" name="u1" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="file" name="f[]" />
		<input type="submit" name="u2" />
		<textarea name="tx"></textarea>
	</form>
	<script>
var done = false;
document.domain = 'ocv.me';
var h = document.getElementById('tb');
var f = document.getElementById('uf');
f.addEventListener('submit', function()
{
	h.innerHTML = '<h1>Connecting...</h1>';
	var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4)
		{
			if (done) return;
			done = true;
			h.innerHTML = '<h1>Uploading...</h1>';
			h.innerHTML = xhr.responseText;
			f.submit();
		}
	};
	xhr.open('GET', '?ts=' + new Date().getTime());
	xhr.send();
	setTimeout(function()
	{
		if (done) return;
		h.innerHTML = '<h1>Uploading (fallback)...</h1>';
		done = true;
		f.submit();
	}, 1000);
	return false;
});
</script>
<?php
	$len = 0;
	$msg = '';
	$td = time();
	$tx = $_SERVER['REMOTE_ADDR']."\n";

	if (isset($_POST) && isset($_POST['tx']))
		$msg = $_POST['tx'];

	if (isset($_FILES) && isset($_FILES['f']))
	{
		if (!isset($_FILES['f']['name']) ||
			!isset($_FILES['f']['name'][0]))
		{
			die('filename not set (what tue fuck)');
		}
		$fd = array();
		$fc = count($_FILES['f']['name']);
		$fk = array_keys($_FILES['f']);
		for ($i = 0; $i < $fc; $i++)
		{
			foreach ($fk as $k)
			{
				$fd[$i][$k] = $_FILES['f'][$k][$i];
			}
		}
		foreach ($fd as $f)
		{
			if ($f['error'] > 0)
			{
				echo ('error: ' + $f['error'] .'<br />');
				continue;
			}
			//print_r($f);
			$fn = $f['name'];
			$tx .= '('.$f['size'].') = ['.$fn."]\n";
			$fn = preg_replace('/[^a-zA-Z0-9]/', '_', $fn);
			if (strlen($fn) > 1)
			{
				$fn = "inc/$fn";
				$ofs = strrpos($fn, '_');
				if ($ofs > 0 && $ofs < strlen($fn))
				{
					$ext = strtolower(substr($fn, $ofs + 1));
					if ($ext == "jpg" ||
						$ext == "png" ||
						$ext == "gif" ||
						$ext == "txt" ||
						$ext == "exe" ||
						$ext == "mp3" ||
						$ext == "aac" ||
						$ext == "m4a" ||
						$ext == "ogg" ||
						$ext == "flac" ||
						$ext == "zip" ||
						$ext == "7z" ||
						$ext == "rar" ||
						$ext == "tgz")
					{
						$fn = substr($fn, 0, $ofs) .'.'. substr($fn, $ofs + 1);
					}
				}
				$len += $f['size'];
				move_uploaded_file($f['tmp_name'], $fn);
				echo ('file saved successfully' .'<br />');
			}
			else
			echo ('bad filename or other error' .'<br />');
		}
	}

	$tx .= '(size_t) = ['.$len."]\n";
	if ($ts1 > 0)
	{
		$s1 = $len / ($ts2 - $ts1);
		$sv = $s1;
		$su = 'B';
		while ($sv >= 1024)
		{
			     if ($su ==  'B') $su = 'kB';
			else if ($su == 'kB') $su = 'MB';
			else if ($su == 'MB') $su = 'GB';
			else break;
			$sv /= 1024;
		}
		$tx .= '(time1) = ['.$ts1."]\n";
		$tx .= '(time2) = ['.$ts2."]\n";
		$tx .= '(bps) = ['.$s1."]\n";
		$tx .= '(spd) = ['.round($sv,2).' '.$su."]\n";
	}
	if ($msg != '')
		$tx .= "\n".$msg;
	
	file_put_contents("inc/$td.txt", $tx);
	echo 'private file uploader';
?>
</body></html>
