Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. class REST {
  4.  
  5. public $_allow = array();
  6. public $_content_type = "application/json";
  7. public $_request = array();
  8.  
  9. private $_method = "";
  10. protected $_endpoint = "";
  11. protected $_verb = "";
  12. protected $_args = Array();
  13. private $_code = 200;
  14.  
  15. public function __construct(){
  16. $this->inputs();
  17. }
  18.  
  19. public function get_referer(){
  20. return $_SERVER['HTTP_REFERER'];
  21. }
  22.  
  23. public function response($data,$status){
  24. $this->_code = ($status)?$status:200;
  25. $this->set_headers();
  26. echo $data;
  27. exit;
  28. }
  29.  
  30. private function get_status_message(){
  31. $status = array(
  32. 100 => 'Continue',
  33. 101 => 'Switching Protocols',
  34. 200 => 'OK',
  35. 201 => 'Created',
  36. 202 => 'Accepted',
  37. 203 => 'Non-Authoritative Information',
  38. 204 => 'No Content',
  39. 205 => 'Reset Content',
  40. 206 => 'Partial Content',
  41. 300 => 'Multiple Choices',
  42. 301 => 'Moved Permanently',
  43. 302 => 'Found',
  44. 303 => 'See Other',
  45. 304 => 'Not Modified',
  46. 305 => 'Use Proxy',
  47. 306 => '(Unused)',
  48. 307 => 'Temporary Redirect',
  49. 400 => 'Bad Request',
  50. 401 => 'Unauthorized',
  51. 402 => 'Payment Required',
  52. 403 => 'Forbidden',
  53. 404 => 'Not Found',
  54. 405 => 'Method Not Allowed',
  55. 406 => 'Not Acceptable',
  56. 407 => 'Proxy Authentication Required',
  57. 408 => 'Request Timeout',
  58. 409 => 'Conflict',
  59. 410 => 'Gone',
  60. 411 => 'Length Required',
  61. 412 => 'Precondition Failed',
  62. 413 => 'Request Entity Too Large',
  63. 414 => 'Request-URI Too Long',
  64. 415 => 'Unsupported Media Type',
  65. 416 => 'Requested Range Not Satisfiable',
  66. 417 => 'Expectation Failed',
  67. 500 => 'Internal Server Error',
  68. 501 => 'Not Implemented',
  69. 502 => 'Bad Gateway',
  70. 503 => 'Service Unavailable',
  71. 504 => 'Gateway Timeout',
  72. 505 => 'HTTP Version Not Supported');
  73. return ($status[$this->_code])?$status[$this->_code]:$status[500];
  74. }
  75.  
  76. public function get_request_method(){
  77. return $_SERVER['REQUEST_METHOD'];
  78. }
  79.  
  80. private function inputs(){
  81. $this->_args = explode('/', rtrim($_REQUEST['rquest'], '/'));
  82. $this->_endpoint = array_shift($this->_args);
  83.  
  84. switch($this->get_request_method()){
  85. case "POST":
  86. $this->_request = file_get_contents("php://input") ;
  87. $this->_request = $this->cleanInputs($this->_request);
  88. break;
  89. case "GET":
  90. $this->_request = $this->cleanInputs($_GET);
  91. break;
  92. case "DELETE":
  93. $this->_request = $this->cleanInputs($_GET);
  94. break;
  95. case "PUT":
  96. $this->_request = file_get_contents("php://input") ;
  97. $this->_request = $this->cleanInputs($this->_request);
  98. break;
  99. default:
  100. $this->response('',406);
  101. break;
  102. }
  103. }
  104.  
  105. private function cleanInputs($data){
  106. $clean_input = array();
  107. if(is_array($data)){
  108. foreach($data as $k => $v){
  109. $clean_input[$k] = $this->cleanInputs($v);
  110. }
  111. } else {
  112. if(get_magic_quotes_gpc()){
  113. $data = trim(stripslashes($data));
  114. }
  115. $data = strip_tags($data);
  116. $clean_input = trim($data);
  117. }
  118. return $clean_input;
  119. }
  120.  
  121. private function set_headers(){
  122. header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
  123. header("Content-Type:".$this->_content_type);
  124. }
  125.  
  126. }
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement