PHP_SMART_HOME_V3/library/ApiController.php

38 lines
901 B
PHP
Raw Normal View History

2020-04-24 16:37:05 +00:00
<?php
2020-04-24 19:22:24 +00:00
class ApiController {
private $input;
private $authenticated;
2020-04-24 16:37:05 +00:00
2020-04-24 16:58:00 +00:00
function __construct() {
2020-04-24 19:22:24 +00:00
$this->authenticated = false;
$input = file_get_contents('php://input');
if(empty($input)){
$this->input = NULL;
}else{
$this->input = json_decode($input, true);
if(json_last_error() != JSON_ERROR_NONE){
throw new Exception("Invalid request payload", 400);
}
}
}
2020-04-24 16:37:05 +00:00
function requireAuth(){
2020-04-24 19:22:24 +00:00
if (isset($this->headers['HTTP_AUTHORIZATION'])) {
// TODO: call appropriate class/method
$authManager = new AuthManager();
$this->authenticated = $authManager>validateToken($this->headers['HTTP_AUTHORIZATION']);
if(!$this->authenticated){
throw new Exception("Auth required", 401);
}
2020-04-24 16:37:05 +00:00
} else {
2020-04-24 19:22:24 +00:00
throw new Exception("Auth required", 401);
2020-04-24 16:37:05 +00:00
}
2020-04-24 19:22:24 +00:00
}
2020-04-24 16:37:05 +00:00
2020-04-24 17:05:18 +00:00
function response($data = [], $httpCode = '200'){
2020-04-24 19:22:24 +00:00
http_response_code($httpCode);
echo json_encode($data);
2020-04-24 16:37:05 +00:00
}
2020-04-24 19:22:24 +00:00
}