<?php
	header('Cache-Control: no-cache, must-revalidate');
	header('Expires: Wed, 09 Sep 2009 09:09:09 GMT');
	if (!@chdir('nqog')) die('fuck');
	@mkdir('.hsh');
	
	$ts1 = 0;
	$ts2 = microtime(true);
	if (isset($_GET['ts']))
	{
		$tp = intval($_GET['ts']) .'.tp';
		file_put_contents($tp, $ts2);
		die();
	}
	if (isset($_POST) &&
		isset($_POST['tp']))
	{
		$tp = intval($_POST['tp']);
		$path = $tp. '.tp';
		if (file_exists($path))
		{
			$ts1 = intval(file_get_contents($path));
			unlink($path);

			$tl = intval($_POST['tl']);
			if ($tl > 0)
				$ts1 += ($tl - $tp) / 500;
		}
	}

?><!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;
	margin: .5em 0;
}
html {
	font-family: sans-serif;
}
.ok {
	color: #490;
}
.ng {
	color: #a30;
}
#onebox {
	display: none;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	width: 100%;
	height: 100%;
	background: #fff;
}
#onebtn {
	font-size: 2em;
	padding: 1em;
	margin: 2em;
	text-align: center;
	background: #eee;
	border: .1em solid #ddd;
	border-radius: .2em;
}
</style></head><body>
	<div id="tb">
		<p>put files into the boxes below</p>
		<p>any boxes are fine, any order is fine, but more than 15 files at once and i'll <strong>die</strong></p>
		<p><img src="new.gif" style="height: 1.8em; margin: -.3em 0 -.5em 0" /> you can drag and drop multiple files into one box</p>
		<p>press any of the submit buttons, any button is fine</p>
	</div>
	<form method="post" enctype="multipart/form-data" id="uf" action="?">
		<input type="hidden" name="tp" id="tp" />
		<input type="hidden" name="tl" id="tl" />
		<input type="submit" name="u1" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="file" name="f[]" multiple="multiple" />
		<input type="submit" name="u2" />
		<p>
			put a comment or your SSN here:<br />
			<textarea name="tx"></textarea>
		</p>
		<p>supported music formats: flac/mp3/ogg/m4a/aac/wav/tak/mod/xm/it/s3m/nsf/ftm/spc/sid</p>
	</form>
	<div id="onebox">
		<div id="onebtn">
			click here to send a file to ed
		</div>
	</div>
	<script>
var done = false;
document.domain = 'ocv.me';
var h = document.getElementById('tb');
var f = document.getElementById('uf');
f.onsubmit = function()
{
	var ts = new Date().getTime();
	h.innerHTML = '<h1>Connecting...</h1>';
	document.getElementById('tp').value = ts;

	var xhr = window.XMLHttpRequest ?
		new XMLHttpRequest() :
		new ActiveXObject("Microsoft.XMLHTTP");
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4)
		{
			if (done) return; done = true;
			document.getElementById('tl').value = new Date().getTime();
			h.innerHTML = '<h1>Uploading...</h1>';
			f.submit();
		}
	};
	xhr.open('GET', '?ts=' + ts);
	xhr.send();
	setTimeout(function()
	{
		if (done) return; done = true;
		h.innerHTML = '<h1>Uploading (fallback)...</h1>';
		f.submit();
	}, 1000);
	//return true;
	return false;
};

if (window.location.href.indexOf('?1') >= 0)
	document.getElementById('onebox').style.display = 'block';

var isub, ifile = undefined;
document.getElementById('onebtn').onclick = function()
{
	if (ifile !== undefined && ifile.files.length > 0)
	{
		document.getElementById('onebtn').innerHTML = 'sending file...';
		isub.click();
		return;
	}

	var inputs = document.getElementsByTagName('input');
	for (var a = 0; a < inputs.length; a++)
	{
		if (inputs[a].getAttribute('type') == 'submit')
			isub = inputs[a];

		if (inputs[a].getAttribute('type') == 'file')
			ifile = inputs[a];
	}
	ifile.click();
	awaitfile();
};

function awaitfile()
{
	if (ifile.files.length == 0)
	{
		setTimeout(awaitfile, 250);
		return;
	}
	document.getElementById('onebtn').innerHTML = 'file selected!<br />click here to confirm and start sending';
}

</script>
<?php
	$htmlrepltab = [
		'&' => '&amp;',
		'<' => '&lt;',
		'>' => '&gt;'
	];
	function htmlesc($in)
	{
		global $htmlrepltab;
		return strtr($in, $htmlrepltab);
	}

	$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']))
	{
		echo "<h2>upload report:</h2>\n";

		if (!isset($_FILES['f']['name']) ||
			!isset($_FILES['f']['name'][0]))
		{
			die('filename not set (wait what)');
		}
		$fd = array();
		$fc = count($_FILES['f']['name']);
		$fk = array_keys($_FILES['f']);
		$num_ups = 0;
		for ($i = 0; $i < $fc; $i++)
		{
			foreach ($fk as $k)
			{
				$fd[$i][$k] = $_FILES['f'][$k][$i];
			}
			if ($fd[$i]['error'] === 0)
				$num_ups += 1;
		}

		//echo '<pre>'; print_r($fd); die('</pre>');
		if ($num_ups > 15)
			die('whoa dude way too many files at once ' . $num_ups);

		foreach ($fd as $f)
		{
			$err = $f['error'];
			if ($err > 0)
			{
				if ($err == UPLOAD_ERR_INI_SIZE) echo 'too thicc';
				elseif ($err == UPLOAD_ERR_FORM_SIZE) echo 'too thicc';
				elseif ($err == UPLOAD_ERR_PARTIAL) echo 'lost some of this';
				elseif ($err == UPLOAD_ERR_NO_FILE) continue;
				else echo 'server fuckup';

				echo (': <span="ng">' . $f['name'] .'</span><br />');
				continue;
			}
			$sum = sha1_file($f['tmp_name']);
			//print_r($f);
			$fn = $f['name'];
			$tx .= "({$sum}) = ({$f['size']}) = [{$fn}] = ";
			if ($fn === '-')
				$fn = 'curl-stdin';

			$fn = preg_replace('/[^a-zA-Z0-9]/', '_', $fn);

			if (strlen($fn) > 1)
			{
				$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 == "opus" ||
						$ext == "flac" ||
						$ext == "wav" ||
						$ext == "webm" ||
						$ext == "zip" ||
						$ext == "7z" ||
						$ext == "rar" ||
						$ext == "tgz")
					{
						$fn = substr($fn, 0, $ofs) .'.'. substr($fn, $ofs + 1);
					}
				}
				if (file_exists($fn))
				{
					$ofs = max(strrpos($fn, '_'), strrpos($fn, '.'));
					if ($ofs > 0)
					{
						$fn1 = substr($fn, 0, $ofs);
						$fn2 = substr($fn, $ofs);
					}
					else
					{
						$fn1 = $fn;
						$fn2 = '';
					}
					$ofs = 1;
					while (file_exists($fn))
					{
						if ($ofs > strlen($sum) - 2) break;
						$fn = $fn1 .'-'. substr($sum, 0, $ofs) . $fn2;
						$ofs += 1;
					}
				}

				$tx .= "[{$fn}]\n";
				$len += $f['size'];
				$sumf = ".hsh/{$sum}";
				
				if (file_exists($sumf))
				{
					symlink(trim(fgets(fopen($sumf, 'r'))), $fn); 
					unlink($f['tmp_name']);
				}
				else
					move_uploaded_file($f['tmp_name'], $fn);

				file_put_contents($sumf, "{$fn}\n", FILE_APPEND);
				//echo ('file saved successfully' .'<br />');

				//$efn = rawurlencode($f['name']);
				$efn = htmlesc($f['name']);
				echo("saved file: <span class=\"ok\">{$efn}</span><br />\n");
			}
			else
			echo ('bad filename or other error' .'<br />');
		}
	
		function b2h($v)
		{
			$s = 'B';
			while ($v >= 1024)
			{
					 if ($s ==  'B') $s = 'kB';
				else if ($s == 'kB') $s = 'MB';
				else if ($s == 'MB') $s = 'GB';
				else break;
				$v /= 1024;
			}
			return round($v,2).' '.$s;
		}

		$hlen = b2h($len);
		$hspd = 'UNKNOWN kB';
		$tx .= '(byte) = ['.$len."]\n";
		$tx .= '(size) = ['.$hlen."]\n";
		if ($ts1 > 0)
		{
			$spd = $len / ($ts2 - $ts1);
			$hspd = b2h($spd);

			$tx .= '(time1) = ['.$ts1."]\n";
			$tx .= '(time2) = ['.$ts2."]\n";
			$tx .= '(bps) = ['.$spd."]\n";
			$tx .= '(spd) = ['.$hspd."]\n";
		}
		echo <<<DONGS
<script>
	document.getElementById('tb').innerHTML =
		'<h1>you upped {$hlen} at {$hspd}/s</h1>';
</script>
DONGS;
	}
	if ($msg != '')
		$tx .= "\n".$msg;
	
	$tfn = "$td";
	while (file_exists($tfn.'.txt'))
		$tfn = $tfn.'_';

	if ($len > 0 || $msg != '')
		file_put_contents("$tfn.txt", $tx);
?>
<br />
<small>private file uploader by ed &lt;irc.rizon.net&gt;</small><br />
<small>design by <a href="http://www.motherfuckingwebsite.com/">mfw</a></small>

<?php if($_SERVER['REMOTE_ADDR'] == '182.161.103'){?>
<?php } ?>
</body></html>
