<?php 
require_once($_SERVER['DOCUMENT_ROOT']."/klickit/connection.php");
require_once($_SERVER['DOCUMENT_ROOT']."/klickit/content/access.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/klickit/content/main.php");

include ('main.php');

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'UNAUTHORISED ACCESS';
    exit;
} else {

    if($_SERVER['PHP_AUTH_USER']== 'klickit' AND $_SERVER['PHP_AUTH_PW']=="Klickit@2022"){

        if($_POST['service']=='auth'){
            login($_POST['username'],$_POST['password']);
        }elseif($_POST['service']=='ip'){
            print(json_encode(getip()));
        }elseif($_POST['service']=='register'){
            $db = getConnection();
            $name = mysqli_real_escape_string($db, $_REQUEST['fullname']);
            $email = mysqli_real_escape_string($db, $_REQUEST['email']);
            $tell = mysqli_real_escape_string($db, $_REQUEST['tell']);
            $password = mysqli_real_escape_string($db, $_REQUEST['password']);
            $school = mysqli_real_escape_string($db, $_REQUEST['school']);
            $type = mysqli_real_escape_string($db, $_REQUEST['user_type']);
            

            addCustomer($name, $email, $tell, $password, $type, $school);
        }elseif($_POST['service']=='content'){
            print(json_encode(getGrades()));
        }
    // 
    }else{

    header('HTTP/1.0 401 Unauthorized');
    echo 'UNAUTHORISED ACCESS';
    }
}
//echo "success";
?>