<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;
require_once __DIR__ . '/vendor/autoload.php';
try {
    $connection = new AMQPStreamConnection('52.77.231.145', 5672, 'guest', 'guest');
} catch (Exception $e) {
    creteLogs(__FILE__, __LINE__, "rabbit mq not connect" . $e->getMessage());
}
function cmdExperianComm($serviceUrl, $requestParams)
{
    global $javaUrlconnect;
    $webURL = 'http://' . $javaUrlconnect . '/RESTfulExample/rest/services/' . $serviceUrl;
    // Server\
    creteLogs("experian-flow.php", __LINE__, $requestParams . $serviceUrl);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $webURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParams);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    // we want headers
    curl_setopt($ch, CURLOPT_NOBODY, false);
    // we don't need body
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $response = curl_exec($ch);
    if ($response === false) {
        $msg = 'Curl error: ' . curl_error($ch);
        creteLogs("experian-flow.php", __LINE__, $msg);
    }
    curl_close($ch);
    return $response;
}
$fetch_user = "******";
$result = mysql_query_with_throw($fetch_user);
$update_array = array();
while ($row = mysql_fetch_array($result)) {
    $user_id = $row['user_id'];
    $user_mobile = $row['mobile'];
    $user_email = $row['email'];
    $user_name = $row['name'];
    // send sms and email to user and others
    $usermessage = 'Dear ' . $user_name . ', Please log into www.clearmydues.com with OTP to get your free credit report.';
    $file_list = array(array("path" => $filePath1, "file_name" => $id_name));
    $to = array($user_email);
    $cc = array("");
    $bcc = array("");
    $subject = "Registration Validation Pending - Clearmydues";
    $body = '<p>
         Dear ' . $user_name . ',<br><br> Thanks for registering with clearmydues. Please login with OTP to get your free credit report.
         </p>';
    send_mail_through_queue($to, $cc, $bcc, $subject, $body, $file_list);
    $sendsmstouser = sendsms($user_mobile, $usermessage);
    // update status in job table
    $update_user = "******";
    $update_user_check = mysql_query_with_throw($update_user);
    $update_user_check_set = mysql_fetch_assoc($update_user_check);
    if (!$update_user_check_set['otp_status']) {
        // insert userid and status to 1
        creteLogs(__FILE__, __LINE__, "send email to user " . $user_id . "about otp 1 time");
        $insert_data = "insert into tbl_jobstatus (`user_id`, `otp_status`) \n                    values ({$user_id}, 1);";
        $insert_data_status = mysql_query_with_throw($insert_data);
    }
}
 // check city
 if (!preg_match($addressRegex, $city)) {
     $errors[] = "Please fill city name in valid format(Use only characters,digits,/ and -).";
 }
 if (strlen($flatno) > 39) {
     $errors[] = "Flat-No is greater then permissible limit. Please splited into appropiate boxes.";
 }
 if (empty($errors)) {
     if (trim($email)) {
         //$rand_str = mt_rand(1000000,9999999);
         $rand_str = 12398;
         // Code here
         $query_score = "INSERT INTO `tbl_getscore` ( `user_id` , `full_name` , `email_id` , `dob` , `gender` , \n\t\t\t\t\t`email_verify` , `flatno` , `building`, `road` , `city` , `state` ,`stateid` , `pincode`,\n\t\t\t\t\t `mobile` , `telephone` ,\n\t\t\t\t\t  `pan` , `created_date`, `first_name` ,\n\t\t\t\t\t  `middle_name` , `last_name`)\n\t\t\t\t\t\t\t\t\t\t      VALUES ('{$user_id}', '{$full_name}', '{$email_id}', '{$dob}', '{$genderid}',  '{$rand_str}',\n\t\t\t\t\t\t\t\t\t\t       '{$flatno}', '{$building}', '{$road}', '{$city}', '{$state}', '{$stateid}', '{$pincode}', '{$mobile}', \n\t\t\t\t\t\t\t\t\t\t        '{$telephone}', '{$pan}', '{$date}', '{$firstname}', '{$middlename}', '{$lastname}')";
         $exe_score = mysql_query_with_throw($query_score);
         $insertid = mysql_insert_id();
         creteLogs(__FILE__, __LINE__, "enter score table UserId:" . $user_id . "Scoreid:" . $score_id . "Session_user_id:" . $_SESSION['GTUserID']);
         // Send email
         //send_verify_email ($firstname, $email, $rand_str);
         //header("location:index.php?p=get-verified&id=$insertid");     // Email verification step is skipped for right now...
         header("location:index.php?p=experian-flow&verified=1&score_id={$insertid}");
     }
 } else {
     //report the errors
     if (!empty($errors)) {
         echo '<div id="gt-formfeedback"><b><font size="4">WHOOPS! Please review the following issues:</font></b><ul>';
         foreach ($errors as $msg) {
             echo "<li>{$msg}</li>";
         }
         // end of foreach
         echo '</ul></div>';
     }
function push_to_queue($purpose, $message)
{
    $requestParams = array("purpose" => $purpose, "message" => $message);
    $msg = json_encode($requestParams);
    require_once __DIR__ . '/vendor/autoload.php';
    try {
        global $rabbit_ip, $rabbit_port, $rabbit_user, $rabbit_pswd;
        $connection = new AMQPStreamConnection($rabbit_ip, $rabbit_port, $rabbit_user, $rabbit_pswd);
    } catch (Exception $e) {
        creteLogs(__FILE__, __LINE__, "rabbit mq not connect" . $e->getMessage());
        $success = FALSE;
        return $success;
    }
    global $queue_name, $exchange_name, $bit_mandatory, $bit_immediate, $durable, $passive, $exclusive, $auto_delete;
    $success = "";
    try {
        $channel = $connection->channel();
        $channel->queue_declare($queue_name, $passive, $durable, $exclusive, $auto_delete);
        $msg = new AMQPMessage($msg);
        $channel->basic_publish($msg, $exchange_name, $queue_name, $bit_mandatory, $bit_immediate);
        $success = TRUE;
        $channel->close();
        $connection->close();
    } catch (Exception $e) {
        $success = FALSE;
    }
    return $success;
}