<?php

function denied(){ die('<h1>403 forbiddena</h1>'); }
function buglol(){ die('<h1>fuckdamn shitcunts</h1>'); }
function sploit($m){ die('<h1>you tryin ter sploit me brah</h1><h2>'.$m.'</h2>'); }

header('Content-Type: text/html; charset=UTF-8');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 09 Jan 2009 09:09:09 GMT');
date_default_timezone_set('UTC');

if (isset($_SERVER['HTTP_REFERER']))
{
	$ref = $_SERVER['HTTP_REFERER'];
	if (preg_match('@^https?://(.*\.)?(reddit\.com)@', $ref))
	{
		$url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://');
		$url .= $_SERVER['HTTP_HOST'];
		$url .= '/nope/';
		header('Location: ' . $url);
		echo 'ayy';
		die();
	}
}

function dec($a) { return rawurldecode($a); }
function enc($a) { return str_replace('%2F', '/', rawurlencode($a)); }
function car($a,$b) {return (strncmp($a,$b,strlen($b))===0);}
function cdr($a,$b) {return (strlen($a)>=strlen($b)&&strcmp(substr($a,-strlen($b)),$b)===0);}

$path = $_SERVER['REQUEST_URI'];
$path = explode('?',$path);
	if (count($path) == 1) $path = '';
elseif (count($path) == 2) $path = $path[1];
else {
	print_r($path);
	sploit('1)'.$path);
}
$orgpath = $path;
$relpath = dec($path);
//$self = realpath(dirname(__FILE__));
$self = get_included_files()[0];
$self = realpath(dirname($self));
$abspath = realpath($relpath);

$okay = false;
$safetargets = [ $self, '/web/www/foo.moe/.priv/lootbag/done/' ];
foreach ($safetargets as $safetarget)
	if (car($abspath, $safetarget))
		$okay = true;

if (!$okay) sploit('2)'.$orgpath);
#if (!car($abspath, $self)) sploit('2)'.$orgpath);
$pathp = $relpath;
if (strpos($pathp, 'rtorren') !== false) sploit('4)'.$orgpath);
if (strpos($pathp, '"') !== false) sploit('5)'.$orgpath);

$files = 0;
$dir = new RecursiveDirectoryIterator($pathp);
$it = new RecursiveIteratorIterator($dir,
	RecursiveIteratorIterator::LEAVES_ONLY,
	RecursiveIteratorIterator::CATCH_GET_CHILD);
foreach ($it as $n => $o)
{
	// $e = ltrim($n, './');
	$e = $n;  // should be safe thanks to realpath check
	if (strpos($e, '.torrent') !== false) die('sorry, this folder is off-limits to you');
	if (cdr($e, '.php')) die('sorry, this folder is off-limits to you');
	if (cdr($e, '.')) continue;
	$okay = false;
	foreach ($safetargets as $safetarget)
		if (car(realpath($e), $safetarget))
			$okay = true;
	if (realpath($e) == $self)
		die('nope');
	if (!$okay)
		die('we are lost');
		// die('we are lost<br />'.$e."<br />".realpath($e)."<br />".$self);
	// echo "$e<br />";
	$files++;
}
if ($files <= 0) die('No files were found.');
#if ($files > 100) die('Too many files! Calm down bro');

#die('abs:'.$abspath);
$zipname = substr($abspath, strrpos($abspath, '/'));
if ($zipname[0] == '/')
	$zipname = substr($zipname, 1);
$basedir = substr($abspath, 0, strrpos($abspath, '/'));
if (strpos($zipname, '"') !== false) sploit('6)'.$zipname);
if (strpos($basedir, '"') !== false) sploit('7)'.$basedir);
chdir($basedir) or die('ch:'); #.$basedir);
#die("zn:$zipname");

$stream = popen("/usr/bin/zip -q -r - \"$zipname\"", "r");
if ($stream)
{
	header('Content-Type: application/x-zip');
	header('Content-Disposition: attachment; filename="' . $zipname . '.zip"');
	fpassthru($stream);
	fclose($stream);
}
else echo "error";
