trollolodn

#php: #CodeIgniter MY_Controller for RESTful json data

Oct 11th, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /*
  3. * Controller for RESTful
  4. */
  5. class MY_Controller extends CI_Controller{
  6.  
  7. private $_code = 200;
  8. private $_content_type = "application/json";
  9. private $_allow = true;
  10. private $_error_code = "";
  11.  
  12. function __construct(){
  13. parent::__construct();
  14. }
  15.  
  16. function get_referer(){
  17. return $_SERVER['HTTP_REFERER'];
  18. }
  19.  
  20. function is_method($method){
  21. if($_SERVER['REQUEST_METHOD'] == $method)
  22. return true;
  23. return false;
  24. }
  25.  
  26. function get_status_message(){
  27. $status = array(
  28. 100 => 'Continue',
  29. 101 => 'Switching Protocols',
  30. 200 => 'OK',
  31. 201 => 'Created',
  32. 202 => 'Accepted',
  33. 203 => 'Non-Authoritative Information',
  34. 204 => 'No Content',
  35. 205 => 'Reset Content',
  36. 206 => 'Partial Content',
  37. 300 => 'Multiple Choices',
  38. 301 => 'Moved Permanently',
  39. 302 => 'Found',
  40. 303 => 'See Other',
  41. 304 => 'Not Modified',
  42. 305 => 'Use Proxy',
  43. 306 => '(Unused)',
  44. 307 => 'Temporary Redirect',
  45. 400 => 'Bad Request',
  46. 401 => 'Unauthorized',
  47. 402 => 'Payment Required',
  48. 403 => 'Forbidden',
  49. 404 => 'Not Found',
  50. 405 => 'Method Not Allowed',
  51. 406 => 'Not Acceptable',
  52. 407 => 'Proxy Authentication Required',
  53. 408 => 'Request Timeout',
  54. 409 => 'Conflict',
  55. 410 => 'Gone',
  56. 411 => 'Length Required',
  57. 412 => 'Precondition Failed',
  58. 413 => 'Request Entity Too Large',
  59. 414 => 'Request-URI Too Long',
  60. 415 => 'Unsupported Media Type',
  61. 416 => 'Requested Range Not Satisfiable',
  62. 417 => 'Expectation Failed',
  63. 500 => 'Internal Server Error',
  64. 501 => 'Not Implemented',
  65. 502 => 'Bad Gateway',
  66. 503 => 'Service Unavailable',
  67. 504 => 'Gateway Timeout',
  68. 505 => 'HTTP Version Not Supported');
  69. return ($status[$this->_code]) ? $status[$this->_code] : $status[500];
  70. }
  71.  
  72. function response($data = '', $status = 200, $dataType = "json"){
  73. $this->_code = $status;
  74. $this->set_headers();
  75. if(is_array($data)){
  76. if($dataType == "json"){
  77. echo json_encode($data);
  78. }
  79. if($dataType == "print"){
  80. print_r($data);
  81. }
  82. } else{
  83. if($dataType == "json"){
  84. echo json_encode(array('result'=>$data));
  85. } else{
  86. echo $data;
  87. }
  88. }
  89. exit;
  90. }
  91.  
  92. function set_headers(){
  93. header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
  94. header("Content-Type:".$this->_content_type);
  95. }
  96.  
  97. function authenticate($method = 'GET'){
  98. if($this->is_method($method)){
  99. //Check ip allow on constant list
  100. $ip_access = $_SERVER['REMOTE_ADDR'];
  101. $ip_allow_arr = unserialize(IP_ALLOW);
  102.  
  103. if(in_array($ip_access, $ip_allow_arr)) {
  104. $this->_allow = true;
  105. }
  106.  
  107. return $this->_allow;
  108. } else{
  109. return false;
  110. }
  111. }
  112.  
  113. $this->_error_code = json_encode($code[$constant_name]);
  114.  
  115. return $this->_error_code;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment