<?php
require_once('session.php');
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    header("HTTP/1.1 403 Forbidden");
    echo "Access forbidden. Use HTTPS.";
    exit();
}
if($u['cuid'] == 1){ 
    if (isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) {
        $users = $u['user'];
        $title = $_POST['instansi'];
        $keyword = $_POST['keyword'];
        $deskripsi = $_POST['deskripsi'];
        $news = $_POST['news'];
        $pengumuman = $_POST['pengumuman'];
        $unik = $_POST['unik'];

        $date = date('Y-m-d H:i:s');
        $kode = date('YdmHis');
    $tipe_gambar = array('image/jpeg', 'image/png', 'image/gif'); // Sesuaikan dengan jenis tipe gambar yang diizinkan

    $gbr = $_FILES['image']['name'];
    $ukuran = $_FILES['image']['size'];
    $tipe = $_FILES['image']['type'];
    $error = $_FILES['image']['error'];
    $explode = explode('.', $gbr);
    $extensi = $explode[count($explode) - 1];
    $newname = 'logo_' . $users . '_' . $kode . '.' . $extensi;

    $favicon = $_FILES['favicon']['name'];
    $explodes = explode('.', $favicon);
    $extensis = $explodes[count($explodes) - 1];
    $newnames = 'favicon.png';
    $upload_dir = "../../upload/";

    if ($_FILES['image']['size'] <= 50012000) {
        if ($gbr != "" && $error == 0) {
            $fileMimeType = mime_content_type($_FILES['image']['tmp_name']);
            $allowedMimeTypes = array('image/jpeg', 'image/png', 'image/gif');

            if (in_array($fileMimeType, $allowedMimeTypes)) {
                move_uploaded_file($_FILES['favicon']['tmp_name'], $upload_dir . $newnames);
                move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $newname);
                $query = mysqli_query($conn, "UPDATE `tb_seo` SET `image` = '$newname', `instansi` = '$title', `keyword` = '$keyword', `deskripsi` ='$deskripsi', `news` ='$news', `pengumuman` ='$pengumuman', `unik` ='$unik' WHERE cuid = 1") or die(mysqli_error());
                header('location:../setting/?notif=1');
                exit();
            } else {
                header('location:../setting/?notif=3');
                exit();
            }
        } else {
            move_uploaded_file($_FILES['favicon']['tmp_name'], $upload_dir . $newnames);
            $query = mysqli_query($conn, "UPDATE `tb_seo` SET `instansi` = '$title', `keyword` = '$keyword', `deskripsi` ='$deskripsi', `news` ='$news', `pengumuman` ='$pengumuman', `unik` ='$unik' WHERE cuid = 1") or die(mysqli_error());
            header('location:../setting/?notif=1');
            exit();
        }
    } else if ($_FILES['image']['size'] >= 50012000 || $_FILES['image']['size'] == 0) {
        header('location:../setting/?notif=2');
        exit();
    }
} else {
    // CSRF token is invalid, handle the error
    echo "Ngapain Bang Ngetamper Data Kah?";
    exit();
}
} else { echo 'BOKEP CXFTEAMS'; }
