<?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://keralaneetmodelexam.in/');


$server   = 'localhost';
$username = 'gdhhsxmy_dopa';
$password = 'gdhhsxmy_dopa';
$database = 'gdhhsxmy_dopa';




define('DB_USER', $username);
define('DB_PASS', $password);
define('DB_NAME', $database);
define('DB_SERVER', 'localhost');



function dblink()
{
    $server = 'localhost';
    $user   = DB_USER;
    $pwd    = DB_PASS;
    $dbname = DB_NAME;

    return $con = mysqli_connect('localhost', $user, $pwd, $dbname);
}



function get_option_without_select($table, $were, $field = 'name')
{
    //$datais[0] = '';
    $data = findQuery("select $field, id from $table where $were");
    for ($i = 0; $i < count($data); $i++) {
        $datais[$data[$i]['id']] = $data[$i][$field];
    }

    return $datais;
}

function get_IP_address()
{
    foreach (['HTTP_CLIENT_IP',
                   'HTTP_X_FORWARDED_FOR',
                   'HTTP_X_FORWARDED',
                   'HTTP_X_CLUSTER_CLIENT_IP',
                   'HTTP_FORWARDED_FOR',
                   'HTTP_FORWARDED',
                   'REMOTE_ADDR'] as $key) {
        if (array_key_exists($key, $_SERVER) == true) {
            foreach (explode(',', $_SERVER[$key]) as $IPaddress) {
                $IPaddress = trim($IPaddress); // Just to be safe

                if (filter_var(
                    $IPaddress,
                    FILTER_VALIDATE_IP,
                    FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
                )
                    != false) {
                    return $IPaddress;
                }
            }
        }
    }
}

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));

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

        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)
{
    // No need to check for magic quotes anymore
    $link = dblink();
    // Use mysqli_real_escape_string to escape input
    $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());
    }
}



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








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

    if (!$link) {
        $link = dblink();
    }
    $rs = mysqli_query($link, $sql) or die(mysqli_error());


    $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]);
    }

    return $data;
}









function get_option($table, $field = 'name', $were = 'id!=0')
{
    //$datais[0] = 'Select';

    $datais[$data[0]['id']] = 'Select';



    $data = findQuery("select $field,id from $table where $were order by id asc");
    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='" . $key . "' selected=selected>" . $value . '</option>';
        } else {
            $html .= "<option value='" . $key . "' >" . $value . '</option>';
        }
    }

    return $html;
}
