<?php

chdir(__DIR__ . '/..');

require 'framework/controller_base.class.php';
require 'framework/model_base.class.php';
require 'framework/registry.class.php';
require 'framework/config.php';
require 'framework/mysql.class.php';
require 'framework/router.class.php';
require 'framework/functions.php';

$registry = new registry;
$registry->db = Mysql::getInstance();

function get($var) {
	global $registry;
	return $registry->{$var};
}

require 'config/flags.php';
require 'config/settings.php';
$registry->siteroot = $siteroot;

session_start();
//ob_start();

$sess_id = session_id();
if ($_GET && $_GET['sid'])
    session_id($_GET['sid']);

// CUSTOMER/USER INFO 
if(isset($_POST['fname'])){
    $_SESSION["fname"] = $_POST['fname'];
} elseif(isset($_POST['deliv_name'])) {
	$_SESSION["fname"] = $_POST['deliv_name'];    
} elseif(isset($_POST['customer_name'])) {
	$_SESSION["fname"] = $_POST['customer_name'];
}
if(isset($_POST['email'])){
    $_SESSION["email"] = $_POST['email'];
} elseif(isset($_POST['deliv_email'])) {
	$_SESSION["email"] = $_POST['deliv_email']; 
}
if(isset($_POST['phone'])){
    $_SESSION["phone"] = $_POST['phone'];
} elseif(isset($_POST['deliv_phone'])) {
	$_SESSION["phone"] = $_POST['deliv_phone'];     
}
if(isset($_POST['loyalty_phone'])){
    $_SESSION["loyalty_phone"] = $_POST['loyalty_phone'];
} 
if(isset($_POST['customer_id'])){
    $_SESSION["customer_id"] = $_POST['customer_id'];
}
// LIST OF LOCATIONS
if(isset($_POST['locations'])){
	$_SESSION["locations"] = $_POST['locations'];
}
// BUSINESS LOCATION ID (UNIQUE)
if(isset($_POST['location_override']) && $_POST['location_override'] !="No Loc Override"){
	// This gets set if the User changes the Locations while viewing Available Times
	$_SESSION["location"] = $_POST['location_override'];
	$_SESSION["location_override"] = $_POST['location_override'];
} else {
	if(isset($_POST['location'])){
   		$_SESSION["location"] = $_POST['location'];
	}
}
// BUSINESS LOCATION NAME
if(isset($_POST['location_name_override']) && $_POST['location_name_override'] != "No LocN Override"){
	// This gets set if the User changes the Locations while viewing Available Times
	$_SESSION["location_name"] = $_POST['location_name_override'];
	$_SESSION["location_name_override"] = $_POST['location_name_override'];
} else {
	if(isset($_POST['location_name'])){
   		$_SESSION["location_name"] = $_POST['location_name'];
	}
}
// ORDER INFO
if(isset($_POST['order_type'])){
    $_SESSION["order_type"] = $_POST['order_type'];
} elseif(isset($_POST['order_type_pickup'])) {
	$_SESSION["order_type"] = $_POST['order_type_pickup'];
} elseif(isset($_POST['order_type_delivery'])) {
	$_SESSION["order_type"] = $_POST['order_type_delivery'];
} elseif(isset($_POST['order_type_dinein'])) {
	$_SESSION["order_type"] = $_POST['order_type_dinein'];	
}			
// TIMES AND DATES START HERE
// ASAP OR CUSTOM TIME RADIO OPTIONS
if(isset($_POST['selected_choose_time'])){
	$_SESSION["selected_choose_time"] = $_POST['selected_choose_time'];
} 
if(isset($_POST['pickup_date'])){
	$_SESSION["pickup_date"] = $_POST['pickup_date']; // Typically today's date and for ASAP option
} elseif(isset($_POST['date_chosen'])) {
	$_SESSION["pickup_date"] = $_POST['date_chosen'];  // Typically a custom date
}
// I DON'T THINK WE USE THIS ONE ANY MORE
if(isset($_POST['pickup_times'])){
	$_SESSION["pickup_times"] = $_POST['pickup_times'];
}  
// THIS IS THE MASTER ASAP PICKUP/ORDER TIME IN RFC FORMAT
if(isset($_POST['pickup_time'])){
	$_SESSION["pickup_time"] = $_POST['pickup_time'];
} elseif(isset($_POST['pickup_asap'])) {
	$_SESSION["pickup_time"] = $_POST['pickup_asap'];    
}
// THIS IS THE MASTER CUSTOM PICKUP/ORDER TIME IN RFC FORMAT
if(isset($_POST['custom_time'])){
	$_SESSION["custom_time"] = $_POST['custom_time']; 
}
// RFC FORMAT
if(isset($_POST['datetime_chosen'])) {
	$_SESSION["datetime_chosen"] = $_POST['datetime_chosen'];    
}
// DAY OF THE WEEK
if(isset($_POST['pickup_day'])){
	$_SESSION["pickup_day"] = $_POST['pickup_day'];
} 
// TIMES AND DATES END HERE

if(isset($_POST['table_number'])){
	$_SESSION["table_number"] = $_POST['table_number'];
} elseif(isset($_POST['table_number_value'])) {
	$_SESSION["table_number"] = $_POST['table_number_value'];    
}           
if(isset($_POST['tip_percentage']) && $_POST['tip_percentage'] > 0 ){
	$_SESSION["tip_percentage"] = $_POST['tip_percentage'];
	$_SESSION["tip_custom"] = '';
}        
if(isset($_POST['tip_custom']) && $_POST['tip_custom'] > 0){
	$_SESSION["tip_custom"] = $_POST['tip_custom'];
	$_SESSION["tip_percentage"] = '';
} else {
	if(isset($_POST['tip_custom']) && $_POST['tip_custom'] == 0){	
		$_SESSION["tip_custom"] = 0;
		//$_SESSION["tip_percentage"] = '';
	}	
}                    
//if(isset($_POST['tip_in_cents']) && $_POST['tip_in_cents'] > 0){
//	$_SESSION["tip_in_cents"] = $_POST['tip_in_cents'];
//} else {
//	if(isset($_POST['tip_in_cents']) && $_POST['tip_in_cents'] == 0){	
//		$_SESSION["tip_in_cents"] = 0;
//	}	
//}                   
if(isset($_POST['order_total_in_dollars']) && $_POST['order_total_in_dollars'] > 0){
	$_SESSION["order_total_in_dollars"] = $_POST['order_total_in_dollars'];
}      
if(isset($_POST['ip_address'])){
	$_SESSION["ip_address"] = $_POST['ip_address'];
}    
if(isset($_POST['is_pay_with_apple_pay'])){
	$_SESSION["is_pay_with_apple_pay"] = $_POST['is_pay_with_apple_pay'];
}    
if(isset($_POST['is_pay_with_virtual_balance'])){
	$_SESSION["is_pay_with_virtual_balance"] = $_POST['is_pay_with_virtual_balance'];
}    
if(isset($_POST['virtual_balance_amount'])){
	$_SESSION["virtual_balance_amount"] = $_POST['virtual_balance_amount'];
}    
// See discount below also
if(isset($_POST['discount_stars'])){
	$_SESSION["discount_stars"] = $_POST['discount_stars'];
}    
// CUSTOMER PICKUP INFO OR PICKUP PERSON INFO
if(isset($_POST['curbside_checkbox']) && $_POST['curbside_checkbox'] ==1){
    $_SESSION["curbside_checkbox"] = 1;
}      
if(isset($_POST['curbside_info'])){
    $_SESSION["curbside_info"] = $_POST['curbside_info'];
} else {
	if($_SESSION['user_id']){
		$_SESSION["curbside_info"] = $_SESSION['user']['color'] . " " . $_SESSION['user']['make'] . " " . $_SESSION['user']['model'];		
	} 
}     
if(isset($_POST['contactless'])){
    $_SESSION["contactless"] = $_POST['contactless'];
} else {
	if($_SESSION['user_id']){
		$_SESSION["contactless"] = $_SESSION['user']['contactless'];		
	} 	
}    
if(isset($_POST['pp_checkbox']) && $_POST['pp_checkbox'] ==1){
    $_SESSION["pp_checkbox"] = 1;
} 
if(isset($_POST['pp_name'])){
    $_SESSION["pp_name"] = $_POST['pp_name'];
}     
if(isset($_POST['pp_email'])){
    $_SESSION["pp_email"] = $_POST['pp_email'];
}     
if(isset($_POST['pp_phone'])){
    $_SESSION["pp_phone"] = $_POST['pp_phone'];
}    
// SHIPPING AND/OR DELIVERY ADDRESS & INFO
if(isset($_POST['deliv_email'])){
    $_SESSION["deliv_email"] = $_POST['deliv_email'];
}      
if(isset($_POST['deliv_phone'])){
    $_SESSION["deliv_phone"] = $_POST['deliv_phone'];
}    
if(isset($_POST['delivery_notes'])){
    $_SESSION["delivery_notes"] = $_POST['delivery_notes'];
}    
if(isset($_POST['delivery'])){
	$_SESSION["delivery"] = $_POST['delivery'];
}    
if(isset($_POST['address1'])){
    $_SESSION["address1"] = $_POST['address1'];
}
if(isset($_POST['address2'])){
    $_SESSION["address2"] = $_POST['address2'];
}
if(isset($_POST['city'])){
    $_SESSION["city"] = $_POST['city'];
}
if(isset($_POST['state'])){	
	$_SESSION["state"] = $_POST['state'];
}
if(isset($_POST['zip'])){
    $_SESSION["zip"] = $_POST['zip'];
}
if(isset($_POST['hometype'])){
    $_SESSION["hometype"] = $_POST['hometype'];
}
// OTHER ORDER SETTINGS
if(isset($_POST['entercode'])){
    $_SESSION["entercode"] = $_POST['entercode'];
}
// If it's a full discount, ie 100% or a value > order total, then we should set tax to $0
if(isset($_POST['full_discount'])){
    $_SESSION["full_discount"] = $_POST['full_discount'];
}
if(isset($_POST['discount'])){
    $_SESSION["discount"] = $_POST['discount'];
}
if(isset($_POST['tax'])){
    $_SESSION["tax"] = $_POST['tax'];
}
if(isset($_POST['note'])){
	$_SESSION["note"] = $_POST['note'];
}     
if(isset($_POST['customer_note'])){
    $_SESSION["customer_note"] = $_POST['customer_note'];  
}    

// TIMESLOTS, ETC.
if(isset($_POST['timeslot_location'])){
    $_SESSION["timeslot_location"] = $_POST['timeslot_location'];
}
if(isset($_POST['timeslot_day'])){
    $_SESSION["timeslot_day"] = $_POST['timeslot_day'];
}
if(isset($_POST['timeslot'])){
    $_SESSION["timeslot"] = $_POST['timeslot'];
}
if(isset($_POST['timeslot_id'])){
    $_SESSION["timeslot_id"] = $_POST['timeslot_id'];
}
if(isset($_GET['lid'])){
    $_SESSION["lid"] = $_GET['lid'];
}
if(isset($_GET['plan'])){
    $_SESSION["plan"] = $_GET['plan'];
}

if (isset($_POST['token'])) {
	$_SESSION["token"] = $_POST['token'];
}

$_SESSION['customer_ip'] = $ipaddress;

// extend cookie life time to allow persistance of
// login state on iPhone Home Screen Web Apps.
$cookieLifetime = 31 * 24 * 60 * 60; // 31 days
//setcookie(session_name(), $sess_id, time() + $cookieLifetime); // Older method
// MORE MODERN SECURE METHOD - FOR PASSING PEN TESTS
setcookie(session_name(), $sess_id, [
	'expires' => time() + $cookieLifetime,
	'path' => '/',
	'secure' => true,
	'httponly' => true,
	'samesite' => 'Strict',
]);

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
	$registry->loggedin = 1;
}

function __autoload($class_name) {
	$filename = $class_name . '.php';
	$file = 'models/' . $filename;

	if (file_exists($file) == false)
	{
		return false;
	}
	include ($file);
}

//function my_autoloader($class_name){
//	$filename = $class_name . '.php';
//	$file = 'models/' . $filename;
//
//	if (file_exists($file) == false) {
//		return false;
//	}
//	include($file);
//}

function modules_autoloader($class_name){
	//$modules = ["listing","blog"];
	$filename = $class_name . '.php';
	$modulesdir = "modules";
	$modules = array_filter(glob($modulesdir."/*"), 'is_dir');
	foreach ($modules as $module) {
		$module = substr($module, 8);
		// echo $module . "<br />";
		$file = 'modules/' . $module . '/model/' . $filename;
		if (file_exists($file) == false) {
			return false;
		}
		require_once($file);	
	}
	//print_r($dirs);
	//echo $class_name; // blogModel
	//exit;
}
//spl_autoload_register('my_autoloader');
spl_autoload_register('__autoload');
spl_autoload_register('modules_autoloader');