<?php session_start();
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
/**
 * Program adodbExample.php
 */
include_once 'adodb5214/adodb.inc.php';
// $emailTitle  = 'Pollux Software Solutions';
// $emailTitle2 = 'Pollux Software Solutions';
$siteurl = 'https://www.srsainvestments.com';
$emailTitle = 'SRSA Investment LLC';
// $emailTitle2 = 'vaigai Taxi';
// $contact_from_email = "support@polluxamerica.com";
// $contact_from_name  = 'Pollux Software Solutions';
// $contact_from_email = "arunvicky153@gmail.com";
$contact_from_email = "noreply@srsainvestments.com";
$contact_from_name  = 'SRSA Investment LLC';
$email_host         = 'mail.srsainvestments.com';
$email_username     = 'noreply@srsainvestments.com';
$email_userpassword = ')etOQbXFGjsr';
$logo_url = "assets/images/logo-dark.png";
$contact_to_email = 'info@srsainvestments.com';
// $contact_to_email = 'jrdev123@yopmail.com';
$thankByName = "SRSA Investment LLC";
if ($_SERVER['HTTP_HOST'] == 'localhost') {
    $host     = "localhost";
    $username = "root";
    $password = "";
    $dbname   = "srsa";
/*test captcha*/
    $googlecaptcha_sitekey   = '6LeyO7grAAAAABpYtXqiGNYTn8MjVhvWm_RlK-Ni';
    $googlecaptcha_secretkey = '6LeyO7grAAAAAAAxLB-b0BUhaB6cpM86gIFV0D3o';
    $allowemailnotsent = 0;
    $allownotinsert = 0;
} else {
    $host = "localhost";
    // $dbname = "nawemorg_esgconf2023";
    // $username = "nawemorg_NwmEcg23";
    // $password = "^j}0QqgxXD3!";
    $dbname   = "bwjqfxmy_srsa";
    $username = "bwjqfxmy_agnirajsrsa";
    $password = "^j}0QqgxXD3!";
    // $googlecaptcha_sitekey = '6Leik3MpAAAAABBNK6qPa6Beogq7ufU5SxedqRR-';
    // $googlecaptcha_secretkey = '6Leik3MpAAAAAMv0OCsarP0GBcM7ewnBpDzXNy4z';
	$googlecaptcha_sitekey   = '6LeyO7grAAAAABpYtXqiGNYTn8MjVhvWm_RlK-Ni';
    $googlecaptcha_secretkey = '6LeyO7grAAAAAAAxLB-b0BUhaB6cpM86gIFV0D3o';
    $allowemailnotsent = 0;
    $allownotinsert    = 0;
}
$DB = newADOConnection('mysqli');
//$DB->debug=true;
$DB->connect($host, $username, $password, $dbname) or die("Can't Connect to the Database. Please Check DB Connection");
$DB->EXECUTE("set names 'utf8'");
$mysqli_con = mysqli_connect('127.0.0.1', $username, $password, $dbname);
if (! $mysqli_con) {die("Connection failed: " . mysqli_connect_error());
    echo ' not connected';}
$DB->debug = false;
function MRES_FILTER($data)
{
    global $mysqli_con;
    $data = trim($data);
    if ($data != "") {
        return mysqli_real_escape_string($mysqli_con, $data);
    } else {
        return $data;
    }
}
function AS_FILTER($data)
{
    global $mysqli_con;
    $data = trim($data);
    if ($data != "") {
        return addslashes($data);
    } else {
        return $data;
    }
}
function SS_FILTER($data)
{
    global $mysqli_con;
    $data = trim($data);
    if ($data != "") {
        return stripslashes($data);
    } else {
        return $data;
    }
}
function jRInsert($table, $data)
{
    global $mysqli_con;
    global $DB;
    $colval     = "";
    $colloop    = 0;
    $insdataarr = [];
    foreach ($data as $key => $val) {
        if ($colloop > 0) {
            $colval .= ", ";
        }
        $colval .= "`$key`=?";
        $insdataarr[] = $val;
        $colloop++;
    }
    if ($colval != '' && $table != '') {
        $query = "insert into $table set $colval";
        $DB->EXECUTE($query, $insdataarr);
        $insertedId = $DB->insert_Id();
        if ($insertedId > 0) {
        } else {
        }
        return $insertedId;
    } else {
        return 0;
    }
}
function jRUpdate($table, $data, $wherearr)
{
    global $DB, $mysqli_con;
    $insdataarr   = [];
    $colval       = "";
    $whereval     = "";
    $colloop      = 0;
    $eherecolloop = 0;
    foreach ($data as $key => $val) {
        if ($colloop > 0) {
            $colval .= ", ";
        }
        $colval .= "`$key`=?";
        $insdataarr[] = $val;
        $colloop++;
    }
    foreach ($wherearr as $val) {
        $whereval .= "$val";
    }
    if ($colval != '' && $table != '') {
        $query = "update $table set $colval $whereval";
        $DB->EXECUTE($query, $insdataarr);
        $affectedrows = $DB->Affected_Rows();
        return $affectedrows;
    } else {
        return 0;
    }
}
function validate_recaptcha($response)
{
    global $googlecaptcha_secretkey;
    // Verifying the user's response (https://developers.google.com/recaptcha/docs/verify)
    $verifyURL = 'https://www.google.com/recaptcha/api/siteverify';
    // Collect and build POST data
    $post_data = http_build_query(
        [
            'secret'   => $googlecaptcha_secretkey,
            'response' => $response,
            'remoteip' => (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR']),
        ]
    );
    // Send data on the best possible way
    if (function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec')) {
        // Use cURL to get data 10x faster than using file_get_contents or other methods
        $ch = curl_init($verifyURL);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json', 'Content-type: application/x-www-form-urlencoded']);
        $response = curl_exec($ch);
        curl_close($ch);
    } else {
        // If server not have active cURL module, use file_get_contents
        $opts = ['http' => [
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $post_data,
        ],
        ];
        $context  = stream_context_create($opts);
        $response = file_get_contents($verifyURL, false, $context);
    }
    // Verify all reponses and avoid PHP errors
    if ($response) {
        $result = json_decode($response);
        if ($result->success === true) {
            return true;
        } else {
            return false;
        }
    }
    // Dead end
    return false;
}
function uploadDocument($file, $uploadDir = "uploads/", $prefix = "file")
{
    // Allowed file types
    $allowedTypes = ['pdf', 'doc', 'docx'];
    // Extract file details
    $fileName = basename($file["name"]);
    $fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
    $fileTmp  = $file["tmp_name"];
    $fileSize = $file["size"];
    // Check if the file type is allowed
    if (! in_array($fileType, $allowedTypes)) {
        return "Error: Only PDF, DOC, and DOCX files are allowed.";
    }
    // Check file size (max 5MB)
    if ($fileSize > 5 * 1024 * 1024) {
        return "Error: File size exceeds 5MB.";
    }
    // Ensure the upload directory exists
    if (! is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }
    // Generate unique file name
    $newFileName        = uniqid() . "_" . $fileName;
    $file_name_modified = preg_replace('/[^A-Za-z0-9]/', '', $prefix) . "_" . substr(preg_replace('/[^A-Za-z0-9]/', '', $newFileName), 0, 20);
    $filetypexp         = pathinfo($fileName, PATHINFO_EXTENSION);
    $filePath           = $uploadDir . '/' . $file_name_modified . '.' . $filetypexp;
    // Move the file to the upload directory
    if (move_uploaded_file($fileTmp, $filePath)) {
        return $filePath;
    } else {
        return "Error: File upload failed.";
    }
}

function validate_fields_no_html($post_data, $fields) {
    foreach ($fields as $field) {
        if (isset($post_data[$field])) {
            $value = $post_data[$field];
            
            // Check if it's an array (like multi-select)
            if (is_array($value)) {
                foreach ($value as $v) {
                    if ($v !== strip_tags($v)) {
                        return $field; // Return field name with HTML tags
                    }
                }
            } else {
                if ($value !== strip_tags($value)) {
                    return $field; // Return field name with HTML tags
                }
            }
        }
    }
    return true; // All specified fields are clean
}


function validate_fields_no_html_with_labels($post_data, $fields_map) {
    foreach ($fields_map as $field => $label) {
        if (isset($post_data[$field])) {
            $value = $post_data[$field];

            // Check if it's an array (like multi-select)
            if (is_array($value)) {
                foreach ($value as $v) {
                    if ($v !== strip_tags($v)) {
                        return $label; // Return friendly label
                    }
                }
            } else {
                if ($value !== strip_tags($value)) {
                    return $label; // Return friendly label
                }
            }
        }
    }
    return true; // All specified fields are clean
}
