<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require("config.php");
if (!isset($_SESSION["adminuser".$l1])){
	header("location:adminlogin.php");
}	
 
	if ($_FILES['file']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['file']['tmp_name'])) { 
        $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        if ($extension !== 'upg') {
            echo "Only .upg files are allowed.";
			exit();
        } else {
            if(move_uploaded_file($_FILES['file']['tmp_name'], './'.$_FILES['file']['name'])){
				$encoded_file=file_get_contents($_FILES['file']['name']);
				$decoded_file=base64_decode($encoded_file);
				$filename = pathinfo($_FILES['file']['name'], PATHINFO_FILENAME);
				file_put_contents($filename . '.php', $decoded_file);
				unlink(htmlspecialchars(basename($_FILES["file"]["name"])));
				$file = fopen($filename.'.php', 'r');
				$first_18_characters = fread($file, 18);
				fclose($file);
				$expected_string = "<?php //socraftupg";
			    $universal_string = "<?php //eslunivupg";
				if ($first_18_characters === $expected_string || $first_18_characters === $universal_string) {
					echo ("OK");
					exit();
				} else {
					unlink(htmlspecialchars(basename($_FILES["file"]["name"])));
					echo("Not a valid upgrade file!");
					exit();
				}				
				echo "OK";
			}
        }
    } else {
        echo "File upload failed.";
		exit();
    }
?>
