<?php

if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
	if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
		$ajaxRequest = $_SERVER['HTTP_X_REQUESTED_WITH'];
	}else{
		$ajaxRequest = null;
	}
}else{
	$ajaxRequest = null;
}

if(!is_null($ajaxRequest)){
	function create_cookies(){
		if (session_status() == PHP_SESSION_NONE){
			session_start();
		}
		if(!isset($_COOKIE['goauth_hash']) && isset($_SESSION['goauth_hash'])){
			$hash = $_SESSION['goauth_hash'];
            if(isset($_SERVER['HTTPS']) && 'on'){
                setcookie('goauth_hash', $hash, strtotime('+1 year'), '/', false, false, true);
            }else {
                setcookie('goauth_hash', $hash, strtotime('+1 year'), '/');
            }
		}else{
			if(isset($_COOKIE['goauth_hash'])){
				$hash = $_COOKIE['goauth_hash'];
                if(isset($_SERVER['HTTPS']) && 'on'){
                    setcookie('goauth_hash', $hash, strtotime('+1 year'), '/', false, false, true);
                }else {
                    setcookie('goauth_hash', $hash, strtotime('+1 year'), '/');
                }
			}
		}
		if(session_status() === PHP_SESSION_ACTIVE){
			session_write_close();
		}
	}

	function remove_cookies(){
		if (session_status() == PHP_SESSION_NONE){
			session_start();
		}
		if(isset($_SESSION['token'])){
			unset($_SESSION['token']);
		}
		if(isset($_SESSION['refresh'])){
			unset($_SESSION['refresh']);
		}
                if(session_status() === PHP_SESSION_ACTIVE){
                        session_write_close();
                }
	}

	if(isset($_GET['func'])){
		switch($_GET['func']){
			case 'create':
				create_cookies();
				break;
			case 'remove':
				remove_cookies();
				break;
			default:
				return false;
		}
	}
}else{
	return false;
}

?>
