Rest.inc.php 4.4 KB

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