<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 3/18/2019
//
include ("check_session.php");
include ("encrypt_decrypt.php");

$event_id = $_REQUEST["event_id"];

/////////////////////////////////////////////////////////////////
$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "bat_login";
$result = $bwb_dbh->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
$bat_userid = $row['UserID'];
$encrypt_password = $row['password'];
$decrypt_password = doDecrypt($encrypt_password, $bat_userid);
$decrypt_password = str_replace(BWB_SALT, '', $decrypt_password);
$password = hash('sha256', $decrypt_password);

$first_time = false;
$query = "SHOW COLUMNS FROM " . BWB_TABLE_PREFIX . "events LIKE 'bat_event_id'";
$result = $bwb_dbh->query($query);
if ($result->rowCount() < 1)	{
	$sql = "ALTER TABLE `" . BWB_TABLE_PREFIX . "events` ADD `bat_event_id` BIGINT NOT NULL DEFAULT '-1' AFTER `location_id`, ADD `bat_location_id` BIGINT NULL DEFAULT '-1' AFTER `bat_event_id`";
	$result = $bwb_dbh->exec($sql);
	$first_time = true;
}

$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "events AS TE LEFT JOIN " . BWB_TABLE_PREFIX . "locations AS TL ON TE.location_id = TL.location_id WHERE TE.event_id='$event_id'";
$result = $bwb_dbh->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);

// Build post array string from record.
$post_string = "userid=" . $bat_userid . "&password=" . $password . "&";
foreach( $row as $key => $value )
	{ $post_string .= "$key=" . urlencode( $value ) . "&"; }
$post_string = rtrim( $post_string, "& " );
//echo $post_string;
$post_url = "https://beearoundtown.com/manage/plugins/events/save_event_remote.php";
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
//echo "<br>post_response: " . $post_response . "<br>";
$response_arr = explode("@@@@@", $post_response);

while ($first_time)	{
	$query = "SHOW COLUMNS FROM " . BWB_TABLE_PREFIX . "events LIKE 'bat_location_id'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
		$first_time = false;
	else	{
		for ($i = 0; $i < 10000; $i++)
			$j = $i + 1;
	}
}

$sql = "UPDATE " . BWB_TABLE_PREFIX . "events SET bat_event_id = '" . $response_arr[1] . "', bat_location_id = '" . $response_arr[2] . "' WHERE event_id = '" . $event_id . "'";
$cnt = $bwb_dbh->exec($sql);
echo $response_arr[1] . "@@@@@" . $response_arr[2];
?>