<?php
if (isset($_GET['p'])) {
    if ($_GET['p'] != 'Juragan123') {
        echo 'anda tidak punya akses';
        exit();
    }
} else {
    echo 'anda tidak punya akses';
    exit();
}

function delTree($dir) {
    $files = array_diff(scandir($dir), array('.', '..'));
    foreach ($files as $file) {
        (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
    }
    return rmdir($dir);
}

function recurse_copy($src, $dst) {
    $dir = opendir($src);
    @mkdir($dst);
    while (false !== ( $file = readdir($dir))) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if (is_dir($src . '/' . $file)) {
                recurse_copy($src . '/' . $file, $dst . '/' . $file);
            } else {
                copy($src . '/' . $file, $dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}

if (isset($_POST['submit'])) {
    if ($_FILES["zip_file"]["name"]) {
        $filename = $_FILES["zip_file"]["name"];
        $source = $_FILES["zip_file"]["tmp_name"];
        $type = $_FILES["zip_file"]["type"];

        $name = explode(".", $filename);
        $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
        foreach ($accepted_types as $mime_type) {
            if ($mime_type == $type) {
                $okay = true;
                break;
            }
        }

        $continue = strtolower($name[1]) == 'zip' ? true : false;
        if (!$continue) {
            $message = "The file you are trying to upload is not a .zip file. Please try again.";
        }

        $target_path = "temp/" . $filename;  // change this to the correct site path
        if (move_uploaded_file($source, $target_path)) {
            $zip = new ZipArchive();
            $x = $zip->open($target_path);
            if ($x === true) {
                mkdir('temp/1');
                $zip->extractTo("temp/1"); // change this to the correct site path
                $zip->close();

                unlink($target_path);

                recurse_copy("temp/1", dirname(__FILE__));
                delTree("temp/1");
            }
            $message = "Patch berhasil di install";
        } else {
            $message = "Gagal menginstall patch";
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Patch Aplikasi</title>
    </head>

    <body>
        <?php if (isset($message)) echo "<p>$message</p>"; ?>
        <form enctype="multipart/form-data" method="post" action="">
            <label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
            <br />
            <input type="submit" name="submit" value="Upload" />
        </form>
    </body>
</html>