#---------------------------------------------------------------------------------
require cfg_paths ;
#---------------------------------------------------------------------------------

$useropts{domain} = "https://$ENV{SERVER_NAME}" ;

#---------------------------------------------------------------------------------

use session ;
&check_session ;
&session_state ;
&config_set_vars;
&check_access;
&config_set_paths ;
&config_set_breadcrumbs ;
&config_common_html ;
# &check_ticket_inbox ;

#---------------------------------------------------------------------------------

sub check_ticket_inbox {
	
	# return unless $useropts{super}{$username} || $useropts{boss}{$username} || $useropts{it}{$username} ;
	
	my $xtra_sql_where = (!$useropts{super}{$username} && !$useropts{boss}{$username} && !$useropts{it}{$username} && !$useropts{tickets}{$username}) ? " AND staff_id = '$userid'" : '' ;

	&db_min_ro('tickets','COUNT(unique_id) AS ActiveTickets',"completed = '0' AND responded = '0'$xtra_sql_where",'','') ; 
	
	foreach my $count (keys %{$db{tickets}}) { 
		our $activetickets = "$db{tickets}{$count}{ActiveTickets}" ;
	}
    
} #------------------------------------------------------------------------------------

sub session_state {
	
	return if $ENV{SCRIPT_NAME} =~ m/index.pl/gi || $ENV{SCRIPT_NAME} =~ m/cron/gi || $ENV{SCRIPT_NAME} =~ m/get/gi ;

	unless ($session_state eq 'active') { print 'Status: 302 Moved', "\r\n", "Location: https://$ENV{SERVER_NAME}/", "\r\n\r\n" ; }
    
} #------------------------------------------------------------------------------------

sub config_session_state {

	# if ($ENV{SCRIPT_NAME} eq '/index.pl') { return ; }
	# if (($ENV{SCRIPT_NAME} =~ m/admin/g) or ($skip_relocate))   { $skip_relocate = 1 ; return ; }

	# if (($session_state eq 'expired') or ($session_state eq 'empty')) {
		# print 'Status: 302 Moved', "\r\n", "Location: http://$ENV{SERVER_NAME}/", "\r\n\r\n" ;
    # }
    
} #------------------------------------------------------------------------------------

sub check_access {
	
	# return unless $pixellot_limit{$username} || $events_limit{$username} || $is_operator || $usertype eq 'external' || $usertype eq 'schools_manager' ;
	return if $glod_user_level >= 5 ;
	# return if $glod_user_level >= 0 ;

	# my $path_prefix_ = '/cgi-bin/scripts/' ;
	my $path_prefix_ = $useropts{'scripts'} . '/' ;

	if ($events_limit{$username} || $glod_user_level >= 4){
		$exempt_script{$path_prefix_ . 'costings_detailed_report.pl'}               = 1 ;
		$exempt_script{$path_prefix_ . 'costings_summary_report.pl'}                = 1 ;
		$exempt_script{$path_prefix_ . 'events_detailed_report.pl'}                 = 1 ;
		$exempt_script{$path_prefix_ . 'events_summary_report.pl'}                  = 1 ;	
		$exempt_script{$path_prefix_ . 'operators_summary_report.pl'}               = 1 ;
		$exempt_script{$path_prefix_ . 'operators_report.pl'}                       = 1 ;
		$exempt_script{$path_prefix_ . 'event_quotes_report.pl'}                    = 1 ;
		$exempt_script{$path_prefix_ . 'pdf/event_quote_pdf.pl'}                    = 1 ;
		$exempt_script{$path_prefix_ . 'stock_report.pl'}                           = 1 ;
	}                                                                            

	$exempt_script{$path_prefix_ . 'index.pl'}                                      = 1 ;
	$exempt_script{$path_prefix_ . 'cameras.pl'}                                    = 1 if ($pixellot_limit{$username} || $events_limit{$username}) ;
	$exempt_script{$path_prefix_ . 'cameras_report.pl'}                             = 1 if ($pixellot_limit{$username} || $events_limit{$username}) ;
	$exempt_script{$path_prefix_ . 'delivered_report.pl'}                           = 1 if ($pixellot_limit{$username} || $events_limit{$username}) ;

	$exempt_query{''}       = 1 ;                                                
	$exempt_query{'list'}   = 1 ; 	

	if ($glod_user_level > 0) {  
		$exempt_script{$path_prefix_ . 'logistics_report.pl'}				        = 1 ;
		$exempt_script{$path_prefix_ . 'logistics_report_test.pl'}				    = 1 ;
		$exempt_script{$path_prefix_ . 'calendar.pl'}                               = 1 ;
		$exempt_script{$path_prefix_ . 'get/get_db_cal_events.pl'}                  = 1 ;
		$exempt_script{$path_prefix_ . 'fixed_systems_report.pl'}                   = 1 ;
		$exempt_script{$path_prefix_ . 'dialog/events.pl'}                          = 1 ;
		$exempt_script{$path_prefix_ . 'calibration_report.pl'}                     = 1 ;
		$exempt_script{$path_prefix_ . 'calibration_report_test.pl'}			    = 1 ;
		$exempt_script{$path_prefix_ . 'calibration_status_report.pl'}			    = 1 ;
		$exempt_script{$path_prefix_ . 'dialog/calibration_status_report_uploads.pl'}	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_logistics_item.pl'}		        = 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_poc.pl'}		                    = 1 ;
		$exempt_query{$ENV{QUERY_STRING}}                                           = 1 ;
	}                                                                            

	if ($usertype eq 'coding_provider') {
		$exempt_script{$path_prefix_ . 'get/get_analytics_charge_client.pl'}	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_analyst_from_coding_provider.pl'}	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_fixtures_charge_free_table_reason.pl'}	= 1 ;
		$exempt_script{$path_prefix_ . 'allfixtures_report.pl'}						= 1 ;
		$exempt_script{$path_prefix_ . 'all_fixtures_report.pl'}					= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_fixtures_analyst.pl'}			= 1 ;
	}
	if ($usertype eq 'analytics_client') {
		$exempt_script{$path_prefix_ . 'analytics_event_bookings.pl'}				= 1 ; 
	}

	if ($glod_user_level > 1) {                                                  
		$exempt_script{$path_prefix_ . 'event_quotes.pl'}                        	= 1 ;
		$exempt_script{$path_prefix_ . 'event_quotes_test.pl'}                   	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_event_quote_conflicts.pl'}       	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_event_quote_conflicts_test.pl'}  	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_venues_from_region.pl'}          	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_cities_from_region.pl'}          	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_delete_attach.pl'}               	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_roe.pl'}                         	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_city.pl'}                     	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_school.pl'}                   	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_sport_type.pl'}               	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_club.pl'}                     	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/add_operator.pl'}                 	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/event_email.pl'}                  	= 1 ;
		$exempt_script{$path_prefix_ . 'dialog/email.pl'}                        	= 1 ;
		$exempt_script{$path_prefix_ . 'pdf/event_details_pdf.pl'}               	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_poc_or_recipient_contact_nr.pl'}	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_country_from_region.pl'}			= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_region_country_from_client.pl'}		= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_sss_teams_from_client.pl'}       	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_teams_from_typeahead.pl'}       	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_regions_from_country.pl'}       	= 1 ;
		$exempt_script{$path_prefix_ . 'get/get_sss_teams_from_region.pl'}       	= 1 ; 
		$exempt_script{$path_prefix_ . 'get/get_region_from_city.pl'}       		= 1 ; 
		$exempt_script{$path_prefix_ . 'get/get_country_and_region_from_city.pl'}	= 1 ;  
		# $exempt_script{$path_prefix_ . 'get/get_cities_from_country.pl'}       	= 1 ;  
		$exempt_script{$path_prefix_ . 'set_up_groups_report.pl'}			        = 1 ;
		
		$exempt_query{$ENV{QUERY_STRING}}                                           = 1 ;
		$exempt_query{'edit'}                                                       = 1 ;
		$exempt_query{'add'}                                                        = 1 ;
		$exempt_query{'view'}                                                       = 1 ;
		$exempt_query{'copy'}                                                       = 1 ;
		$exempt_query{'search'}                                                     = 1 ;
	}                                                                            

	if ($glod_user_level > 2) {                                                  
		$exempt_script{$path_prefix_ . 'operators.pl'}                              = 1 ;
		# $exempt_script{$path_prefix_ . 'monitoring_report.pl'}				    = 1 ;
		$exempt_script{$path_prefix_ . 'sss_event_feedback.pl'}				        = 1 ;
		$exempt_script{$path_prefix_ . 'aisa_event_feedback.pl'}				    = 1 ;
		$exempt_script{$path_prefix_ . 'temp_calibrator_report.pl'}			        = 1 ;
		$exempt_script{$path_prefix_ . 'mobile_site_surveys_report.pl'}		        = 1 ;
		$exempt_script{$path_prefix_ . 'mobile_system_tracking_report.pl'}			= 1 ;
		$exempt_script{$path_prefix_ . 'troubleshoot_monitoring_report.pl'}			= 1 ;
	}
	
	if ($usertype eq 'support') {                                                  
		$exempt_script{$path_prefix_ . 'demos.pl'}	= 1 ;
	}
	
	if ($is_dcb_partner) {  
		%exempt_script = () ;
		$exempt_script{$path_prefix_ . 'index.pl'}                                  = 1 ;
		$exempt_script{$path_prefix_ . 'logistics_report.pl'}				        = 1 ;
	}  

	# print "Content-type: text/html\n\n";
	# print "SCRIPT_NAME=$ENV{SCRIPT_NAME}\n" ;
	# print "QUERY_STRING=$ENV{QUERY_STRING}\n" ;
	# print "path_prefix_=$path_prefix_ \n" ;
	# print "exempt_script=$exempt_script{$ENV{SCRIPT_NAME}} \n" ;

	unless ($exempt_script{$ENV{SCRIPT_NAME}})                                     { print "Content-type: text/html\n\n"; require '401.pm' ; exit ; }
	unless ($exempt_query{$ENV{QUERY_STRING}} || length($ENV{QUERY_STRING}) > 10)  { print "Content-type: text/html\n\n"; require '401.pm' ; exit ; }
    
} #------------------------------------------------------------------------------------

1;
