<?php
session_start();
include('../config/dbcon.php');

function getAll($table)
{
    global $con;
    $query = "SELECT * FROM $table";
    return $query_run = mysqli_query($con, $query);
}

function getByID($table, $id)
{
    global $con;
    $query = "SELECT * FROM $table WHERE id='$id' ";
    return $query_run = mysqli_query($con, $query);
}

function getAllActive($table)
{
    global $con;
    $query = "SELECT * FROM $table WHERE status='0' ";
    return $query_run = mysqli_query($con, $query);
}
function getAllCat($table, $catid)
{
    global $con;
    return $catid;
    //$query = "SELECT * FROM $table WHERE category_id='$catid' ";
    //return $query_run = mysqli_query($con, $query);
}
function redirect($url,$message)
{
    $_SESSION['message'] = $message;
    header('Location: '.$url);
    exit();
}
function getAllOrders()
{
    global $con;
    $query = "SELECT * FROM orders WHERE status IN ('1', '2') ORDER BY id DESC";
    return mysqli_query($con, $query);
}
function checkTrackingNoValid($trackingNo)
{
    global $con;
    $query = "SELECT * FROM orders WHERE tracking_no='$trackingNo' ";
    return $query_run = mysqli_query($con, $query);
}
function getOrderHistory(){
    global $con;
    $query = "SELECT * FROM orders WHERE status IN (3, 4)  ORDER BY id DESC";
    return $query_run = mysqli_query($con, $query);
}
function enSifre($password) {
    $key = '122748';
    $cipher = "aes-256-cbc";
    $iv_length = openssl_cipher_iv_length($cipher);
    $fixed_iv = "245FCFVC245FCFVC"; // Use a fixed IV (16 bytes for AES-256-CBC)

    // Use the fixed IV in encryption
    $encrypted = openssl_encrypt($password, $cipher, $key, OPENSSL_RAW_DATA, $fixed_iv);
    if ($encrypted === false) {
        die('Encryption failed');
    }
    return base64_encode($fixed_iv . $encrypted);
}

function deSifre($encryptedPassword) {
    $key = '122748';
    $cipher = "aes-256-cbc";
    $iv_length = openssl_cipher_iv_length($cipher);
    $fixed_iv = "245FCFVC245FCFVC"; // Use the same fixed IV used for encryption

    // Decode the base64-encoded encrypted password
    $decoded = base64_decode($encryptedPassword);
    if ($decoded === false) {
        die('Failed to base64 decode the encrypted password');
    }

    // Check if the decoded data has the correct length
    if (strlen($decoded) < $iv_length) {
        die('Invalid encrypted data format');
    }

    // Extract the IV and encrypted data
    $iv = substr($decoded, 0, $iv_length);
    $encrypted = substr($decoded, $iv_length);

    // Attempt to decrypt the encrypted data using the extracted IV
    $decrypted = openssl_decrypt($encrypted, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    if ($decrypted === false) {
        die('Decryption failed');
    }

    return $decrypted;
}

?>