87 lines
2.2 KiB
PHP
87 lines
2.2 KiB
PHP
<?php
|
|
class RestAPI {
|
|
private $_content_type="application/json";
|
|
protected $_request = array();
|
|
|
|
private $_code = 200;
|
|
|
|
public function __construct() {
|
|
$this->init_request();
|
|
}
|
|
|
|
private function init_request() {
|
|
switch($_SERVER['REQUEST_METHOD']) {
|
|
case "POST":
|
|
$this->_request = $this->cleanup_request($_POST);
|
|
break;
|
|
case "GET":
|
|
case "DELETE":
|
|
$this->_request = $this->cleanup_request($_GET);
|
|
break;
|
|
case "PUT":
|
|
parse_str(file_get_contents("php://input"),$this->_request);
|
|
$this->_request = $this->cleanup_request($this->_request);
|
|
break;
|
|
default:
|
|
$this->response('',406);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private function cleanup_request($data){
|
|
$clean_input = array();
|
|
|
|
if(is_array($data)) {
|
|
foreach($data as $k => $v) {
|
|
$clean_input[$k] = $this->cleanup_request($v);
|
|
}
|
|
} else {
|
|
if(get_magic_quotes_gpc()) {
|
|
$data = trim(stripslashes($data));
|
|
}
|
|
|
|
$data = strip_tags($data);
|
|
$clean_input = trim($data);
|
|
}
|
|
return $clean_input == 'null' ? null : $clean_input;
|
|
}
|
|
|
|
protected function response($data, $http_status=200) {
|
|
$this->send_headers($http_status);
|
|
|
|
if(http_response_code() == 200) {
|
|
if(is_array($data)) {
|
|
echo json_encode($data);
|
|
} else {
|
|
echo $data;
|
|
}
|
|
}
|
|
|
|
exit;
|
|
}
|
|
|
|
private function send_headers($http_status) {
|
|
http_response_code($http_status);
|
|
|
|
if($http_status == 200) {
|
|
header("Content-Type:" . "application/json");
|
|
}
|
|
}
|
|
|
|
public function process_api() {
|
|
$func = strtolower(trim(str_replace("/","",$_REQUEST['action'])));
|
|
|
|
if((int)method_exists($this,$func) > 0) {
|
|
$this->$func();
|
|
} else {
|
|
$this->response('',404);
|
|
}
|
|
}
|
|
|
|
protected function get_db_connection() {
|
|
return new PDO("pgsql:host=localhost;dbname=accountant", "accountant", "accountant");
|
|
}
|
|
}
|
|
?>
|
|
|