Rest.inc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /****************************
  3. * File : Rest.inc.php
  4. * Author : Cygnusa
  5. * @2017
  6. ****************************/
  7. //SECURITY
  8. if ( ! defined('APPLICATION_PATH')) exit('No direct script access allowed');
  9. class REST {
  10. public $_allow = array();
  11. public $_content_type = "application/json";
  12. public $_request = array();
  13. public $_body = '';
  14. private $_method = "";
  15. private $_code = 200;
  16. public function __construct(){
  17. $this->inputs();
  18. }
  19. public function get_referer(){
  20. return $_SERVER['HTTP_REFERER'];
  21. }
  22. public function response($data,$status){
  23. $this->_code = ($status) ? $status : 200;
  24. $this->set_headers();
  25. $data['response_text']=$this->get_status_message();
  26. $data['status']=$this->_code;
  27. echo json_encode($data);
  28. unset($data);
  29. //unset($this);
  30. exit;
  31. }
  32. public function get_status_message(){
  33. $status = array(
  34. 100 => 'Continue',
  35. 101 => 'Switching Protocols',
  36. 200 => 'OK',
  37. 201 => 'Created',
  38. 202 => 'Accepted',
  39. 203 => 'Non-Authoritative Information',
  40. 204 => 'No Content',
  41. 205 => 'Reset Content',
  42. 206 => 'Partial Content',
  43. 300 => 'Multiple Choices',
  44. 301 => 'Moved Permanently',
  45. 302 => 'Found',
  46. 303 => 'See Other',
  47. 304 => 'Not Modified',
  48. 305 => 'Use Proxy',
  49. 306 => '(Unused)',
  50. 307 => 'Temporary Redirect',
  51. 400 => 'Bad Request',
  52. 401 => 'Unauthorized',
  53. 402 => 'Payment Required',
  54. 403 => 'Forbidden',
  55. 404 => 'Not Found',
  56. 405 => 'Method Not Allowed',
  57. 406 => 'Not Acceptable',
  58. 407 => 'Proxy Authentication Required',
  59. 408 => 'Request Timeout',
  60. 409 => 'Conflict',
  61. 410 => 'Gone',
  62. 411 => 'Length Required',
  63. 412 => 'Precondition Failed',
  64. 413 => 'Request Entity Too Large',
  65. 414 => 'Request-URI Too Long',
  66. 415 => 'Unsupported Media Type',
  67. 416 => 'Requested Range Not Satisfiable',
  68. 417 => 'Expectation Failed',
  69. 500 => 'Internal Server Error',
  70. 501 => 'Not Implemented',
  71. 502 => 'Bad Gateway',
  72. 503 => 'Service Unavailable',
  73. 504 => 'Gateway Timeout',
  74. 505 => 'HTTP Version Not Supported');
  75. return ($status[$this->_code])?$status[$this->_code]:$status[500];
  76. }
  77. public function get_request_method(){
  78. return $_SERVER['REQUEST_METHOD'];
  79. }
  80. private function inputs(){
  81. switch($this->get_request_method()){
  82. case "POST":
  83. $josnStr = $this->cleanInputs(file_get_contents('php://input'));
  84. $json = json_decode($josnStr, TRUE);
  85. $this->_request = (array) $json;
  86. if(count($this->_request)==0){
  87. $this->_request=$_REQUEST;
  88. unset($this->_request["request"]);
  89. }
  90. break;
  91. case "GET":
  92. case "DELETE":
  93. $josnStr = $this->cleanInputs(file_get_contents('php://input'));
  94. $json = json_decode($josnStr, TRUE);
  95. $this->_request = (array) $json;
  96. break;
  97. case "PUT":
  98. $this->_body = file_get_contents("php://input");
  99. //$this->_request = $this->cleanInputs($this->_body);
  100. break;
  101. default:
  102. $this->response('',406);
  103. break;
  104. }
  105. }
  106. private function cleanInputs($data){
  107. $clean_input = array();
  108. if(is_array($data)){
  109. foreach($data as $k => $v){
  110. $clean_input[$k] = $this->cleanInputs($v);
  111. }
  112. }else{
  113. if(get_magic_quotes_gpc()){
  114. $data = trim(stripslashes($data));
  115. }
  116. $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title)[^>]*+>#i', '', $data);
  117. //$data = strip_tags($data);
  118. $clean_input = trim($data);
  119. }
  120. return $clean_input;
  121. }
  122. private function set_headers(){
  123. header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
  124. header('Access-Control-Allow-Origin: *');
  125. header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  126. header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
  127. header("Content-Type:".$this->_content_type."; charset=utf-8");
  128. }
  129. }
  130. ?>