<?php
chdir('../../');
require_once 'autoload.php';

$uni = new university_controller;
$info = $uni->index();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['csrf_token']) && isset($_POST['csrf_form_id']) && $waf->csrf_validate($_POST['csrf_form_id'], $_POST['csrf_token'], $utility->action_name())) {

switch ($_POST['asset']) {
case 'logo':

$ext = $utility->extension($_FILES);
$file = 'logo_'.time();
$upload_status = $utility->uploader('storage/images/misc/', $_FILES, $file, 'png,jpeg,jpg');
$file = $file.'.'.$ext;

if ($upload_status == "OK") {

$utility->delete_file('storage/images/misc/'.$info['logo']);

$data['logo'] = $file;

$uni->update($data);

$utility->setMessage("success", "Logo updated successfully");
header("location:../settings");

}else{

$utility->setMessage("warning", $upload_status);
header("location:../settings");
}

break;

case 'icon':

$ext = $utility->extension($_FILES);
$file = 'icon_'.time();
$upload_status = $utility->uploader('storage/images/misc/', $_FILES, $file, 'png,jpeg,jpg');
$file = $file.'.'.$ext;

if ($upload_status == "OK") {

$utility->delete_file('storage/images/misc/'.$info['icon']);

$data['icon'] = $file;

$uni->update($data);

$utility->setMessage("success", "Icon updated successfully");
header("location:../settings");

}else{

$utility->setMessage("warning", $upload_status);
header("location:../settings");
}


break;

default:
header("location:../settings");
break;
}

}else{

header("location:../settings");

}
?>
