<?php

$upload_path = '/srv/www.itvilla.ee/htdocs/';

ini_set('upload_tmp_dir', '/srv/www.itvilla.ee/tmp/');
if (!count($_FILES) && $_SERVER['REMOTE_ADDR'] == '37.157.76.211') {
# debug form
?>
<form enctype="multipart/form-data" action="https://extinfo.uniscada.eu//upload.php" method="POST">
	<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
	<input type="hidden" name="mac" value="support/pyapp/1234567890ac" />
	File: <input name="file" type="file" />
	<input type="submit" value="Upload" />
</form>


<?php
} else {
	if (!$_FILES['file']['error'] && $_FILES['file']['size'] && $_POST['mac']) {
#		print_r($_FILES);
#		print_r($_POST);
		if (!is_dir($upload_path . $_POST['mac']) && !preg_match("/\.\./", $_POST['mac'])) {
			# mkdir($upload_path . $_POST['mac'], 0744, true);
			print "ignored";
			unlink($_FILES['file']['tmp_name']);
		} else {
			$uploaded_file = $_FILES['file']['tmp_name'];
			$result_full_path = $upload_path . $_POST['mac'] . "/" . $_FILES['file']['name'];
			if (move_uploaded_file($uploaded_file, $result_full_path)) {
				#mail("neeme@takis.ee", 
				#	"file uploaded", 
				#	"From: "  . $_SERVER['REMOTE_ADDR']. "\n" 
				#		. "File: " . $_POST['mac'] . "/" . $_FILES['file']['name'] . "\n"
				#		. "Last 100 lines:\n" 
				#		. "---\n"
				#		. `zcat $result_full_path | tail -n 100`. "\n"
				#		. "---\n",
				#	"From: support@itvilla.ee");
				mail("urmas@auv.ee", "file uploaded", "From: "  . $_SERVER['REMOTE_ADDR']. "\nFile: " . $_POST['mac'] . "/" . $_FILES['file']['name'] . "\n" . print_r($_FILES, 1), "From: support@itvilla.ee");
				print "ok";
			} else {
				mail("neeme@takis.ee",
					"file uploaded, move failed!",
					"From: "  . $_SERVER['REMOTE_ADDR']. "\n" 
						. "File: " . $_POST['mac'] . "/" . $_FILES['file']['name'] . "\n"
						. "Couldn't move \"" . $uploaded_file . "\" to \"" . $result_full_path . "\"\n"
						. "Last 100 lines:\n" 
						. "---\n"
						. `zcat $uploaded_file | tail -n 100` . "\n"
						. "---\n",

						"From: support@itvilla.ee");
			}
		}
	}
}
?>