



<?php
session_start();
session_regenerate_id(true);  //create a new session id to prevent hijacking

// define('BASEPATH', true); //access connection script if you omit this line file will be blank
// require '../../db/config.php';
require_once '../config.php';
require_once '../functions/enDecpt.php';


// echo "<script>alert('Hello 01')</script>";  
$dbHandler = DbHandler();
$id = $_GET['id'];
// echo "<script>console.log($id)</script>";  

if (isset($_SESSION['admin'])) {
    // echo "<script>console.log('Hello 03')</script>";  

    //check if image exists
    $fetchdoc = $dbHandler->prepare('SELECT fname FROM tbl_blog WHERE id=?');
    $fetchdoc->execute([base64_url_decode($id)]);

    // $record = $fetchdoc->fetch();
    $record = $fetchdoc->fetch(PDO::FETCH_ASSOC);
    $imageUrl = '../blog/upload/' . $record['fname'];
    //delete image from folder
    unlink($imageUrl);
    $sql = 'DELETE FROM tbl_blog WHERE id = ?';
    $remove_post = $dbHandler->prepare($sql);
    $remove_post->execute([base64_url_decode($id)]);
    // exit();
    header("Location: ../Blog/blog-manager.php");
    // echo "<script>window.location='./withdraw.php'</script>";
} else {
    header("Location: ./logout.php");
    echo "<script>window.location='./logout.php'</script>";
}

?>
