<?php

error_reporting(1);
ini_set('max_execution_time', 0);
ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

if (!session_start()) {
    session_start();
}

define('siteurl', 'https://www.chcentertirur.in/');
date_default_timezone_set('Asia/Kolkata');

function dblink()
{
    $server = 'localhost';
    $user   = 'gdhhsxmy_chcentertirur';
    $pwd    = 'chcentertirur';
    $dbname = 'gdhhsxmy_chcentertirur';




    $con = mysqli_connect($server, $user, $pwd, $dbname);

    if (!$con) {
        die('Connection failed: ' . mysqli_connect_error());
    }

    return $con;
}

function closeConnection($con)
{
    if ($con) {
        mysqli_close($con); // Close the connection
    }
}
define('SALT', '7f850b261dfa7105ce670612a350e6c2d3494f5c');
define('APIKEY', '1a8d0e55-afaa-45f1-9c47-b75868edcb9e');
define('PHONE', '9597403366');
define('DESCRIPTION', 'SHADE CHARITABLE TRUST Fund Collection');
define('CITY', 'tirur');
define('ZIPCODE', '630501');
define('EMAIL', 'chcentertirur@gmail.com');

define('DB_USERNAME', 'gdhhsxmy_chcentertirur');
define('DB_PASSWORD', 'chcentertirur');
define('DB_NAME', 'gdhhsxmy_chcentertirur');

function get_option_without_select($table, $where, $field = 'name')
{
    $datais = [];
    $data   = findQuery("SELECT $field, id FROM $table WHERE $where");

    if (count($data) > 0) {
        foreach ($data as $row) {
            $datais[$row['id']] = $row[$field];
        }
    }

    return $datais;
}

function encrypt($string)
{
    $ciphering      = 'AES-128-CTR';
    $iv_length      = openssl_cipher_iv_length($ciphering);
    $options        = 0;
    $encryption_iv  = '1234567891011121';
    $encryption_key = 'GeeksforGeeks';

    return $encryption = openssl_encrypt($string, $ciphering, $encryption_key, $options, $encryption_iv);
}

function decrypt($encryption)
{
    $ciphering      = 'AES-128-CTR';
    $decryption_iv  = '1234567891011121';
    $options        = 0;
    $decryption_key = 'GeeksforGeeks';

    return $decryption = openssl_decrypt($encryption, $ciphering, $decryption_key, $options, $decryption_iv);
}

function saveArray($table, $arr, $id = 0, $link = '')
{
    $fieldis   = keyvalueis($arr);
    $valueis   = keyvalueis($arr, 1);
    $stordata  = implode(',', $valueis);
    $fieldname = implode(',', $fieldis);

    if (!$link) {
        $link = dblink();
    }

    if ($id == 0) {
        $sql = "INSERT INTO $table($fieldname) VALUES($stordata)";
        mysqli_query($link, $sql) or die(mysqli_error($link));
        $lastid = mysqli_insert_id($link);
        closeConnection($link);

        return $lastid;
    } else {
        updateArray($table, $fieldis, $valueis, $id);
        closeConnection($link);

        return $id;
    }
}

function keyvalueis($arr, $t = 0)
{
    $rarry = [];

    foreach ($arr as $key => $value) {
        $value = "'" . mysql_ready($value) . "'";

        if ($t == 0) {
            $value = $key;
        }
        array_push($rarry, $value);
    }

    return $rarry;
}

function mysql_ready($var)
{
    $link = dblink();
    $var  = mysqli_real_escape_string($link, $var);

    return $var;
}

function updateArray($table, $f, $v, $id)
{
    $link = dblink();
    for ($i = 0; $i < count($f); $i++) {
        $key    = $f[$i];
        $uvalue = $v[$i];
        $query  = "UPDATE $table SET $key = $uvalue WHERE id = $id";
        mysqli_query($link, $query) or die(mysqli_error($link));
    }
    closeConnection($link);
}

function updateQuery($query)
{
    $link = dblink();
    mysqli_query($link, $query) or die(mysqli_error($link));
    closeConnection($link);
}

function findQuery($sql = '', $link = '')
{
    $fieldtype  = [];
    $fieldvalue = [];
    $type       = [];
    $data       = [];

    if (!$link) {
        $link = dblink();
    }

    $rs           = mysqli_query($link, $sql) or die(mysqli_error($link));
    $countrow     = mysqli_num_rows($rs);
    $countoffield = mysqli_num_fields($rs);
    $finfo        = $rs->fetch_fields();

    foreach ($finfo as $val) {
        array_push($fieldtype, $val->name);
        array_push($type, $val->type);
    }

    for ($i = 0; $i < $countrow; $i++) {
        $fieldvalue[$i] = mysqli_fetch_row($rs);
        $data[$i]       = array_combine($fieldtype, $fieldvalue[$i]);
    }

    closeConnection($link);

    return $data;
}

function get_option($table, $field = 'name', $where = 'id != 0')
{
    $datais = [];
    $data   = findQuery("SELECT $field, id FROM $table WHERE $where");

    $datais[0] = 'Select'; // Add the "Select" option first
    for ($i = 0; $i < count($data); $i++) {
        $datais[$data[$i]['id']] = $data[$i][$field];
    }

    return $datais;
}

function array_options($data = null, $selected = 0, $str = 0)
{
    $html = '';

    foreach ($data as $key => $value) {
        if ($str) {
            $key = $value;
        }

        if ($key == $selected) {
            $html .= "<option value='" . htmlspecialchars($key) . "' selected=selected>" . htmlspecialchars($value) . '</option>';
        } else {
            $html .= "<option value='" . htmlspecialchars($key) . "'>" . htmlspecialchars($value) . '</option>';
        }
    }

    return $html;
}
