controller.init.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Controller-controller 控制器基类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. require_once("request.init.php");
  12. require_once("validate.init.php");
  13. require_once("filter.init.php");
  14. class controllerInit extends filterInit{
  15. public $v; //视图模型对象
  16. /**
  17. * 初始化控制器,
  18. */
  19. public function __construct() {
  20. $this->filter(); //全局过滤函数,对GET POST数据自动过,InitPHP采取非常严格的数据过滤机制
  21. $this->set_token(); //生成全局TOKEN值,防止CRsf攻击
  22. }
  23. /**
  24. * 控制器 AJAX脚本输出
  25. * Controller中使用方法:$this->controller->ajax_return()
  26. * @param int $status 0:错误信息|1:正确信息
  27. * @param string $message 显示的信息
  28. * @param array $data 传输的信息
  29. * @param array $type 返回数据类型,json|xml|eval|jsonp
  30. * @return object
  31. */
  32. public function ajax_return($status, $message = '', $data = array(), $type = 'json') {
  33. $return_data = array('status' => $status, 'message' => $message, 'data' => $data);
  34. $type = strtolower($type);
  35. if ($type == 'json') {
  36. header("Content-type: application/json");
  37. exit(json_encode($return_data));
  38. } elseif ($type == 'xml') {
  39. header('Content-type: text/xml');
  40. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  41. $xml .= '<return>';
  42. $xml .= '<status>' .$status. '</status>';
  43. $xml .= '<message>' .$message. '</message>';
  44. $xml .= '<data>' .serialize($data). '</data>';
  45. $xml .= '</return>';
  46. exit($xml);
  47. } elseif ($type == "jsonp"){
  48. $callback = $this->get_gp('callback');
  49. $json_data = json_encode($return_data);
  50. if (is_string($callback) && isset($callback[0])) {
  51. exit("{$callback}({$json_data});");
  52. } else {
  53. exit($json_data);
  54. }
  55. } elseif ($type == 'eval') {
  56. exit($return_data);
  57. } else {
  58. }
  59. }
  60. /**
  61. * 控制器 重定向
  62. * Controller中使用方法:$this->controller->redirect($url, $time = 0)
  63. * @param string $url 跳转的URL路径
  64. * @param int $time 多少秒后跳转
  65. * @return
  66. */
  67. public function redirect($url, $time = 0) {
  68. if (!headers_sent()) {
  69. if ($time === 0) header("Location: ".$url);
  70. header("refresh:" . $time . ";url=" .$url. "");
  71. } else {
  72. exit("<meta http-equiv='Refresh' content='" . $time . ";URL=" .$url. "'>");
  73. }
  74. }
  75. /**
  76. * 返回404
  77. * Controller中使用方法:$this->controller->return404()
  78. * @return
  79. */
  80. public function return404() {
  81. header('HTTP/1.1 404 Not Found');
  82. header("status: 404 Not Found");
  83. return;
  84. }
  85. /**
  86. * 返回404
  87. * Controller中使用方法:$this->controller->return200()
  88. * @return
  89. */
  90. public function return200() {
  91. header("HTTP/1.1 200 OK");
  92. return;
  93. }
  94. /**
  95. * 返回500
  96. * Controller中使用方法:$this->controller->return500()
  97. * @return
  98. */
  99. public function return500() {
  100. header('HTTP/1.1 500 Internal Server Error');
  101. return;
  102. }
  103. /**
  104. * 返回403
  105. * Controller中使用方法:$this->controller->return403()
  106. * @return
  107. */
  108. public function return403() {
  109. header('HTTP/1.1 403 Forbidden');
  110. return;
  111. }
  112. /**
  113. * 返回405
  114. * Controller中使用方法:$this->controller->return405()
  115. * @return
  116. */
  117. public function return405() {
  118. header('HTTP/1.1 405 Method Not Allowed');
  119. return;
  120. }
  121. /**
  122. * 验证Service中$this->service->return_msg返回的结构
  123. * 是否正确。
  124. * Controller中使用方法:$this->controller->check_service_return()
  125. * @param array $data
  126. * @return boolean true|false
  127. */
  128. public function check_service_return($data) {
  129. if ($data[0] == true || $data[0] == 1) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * 类加载-获取全局TOKEN,防止CSRF攻击
  136. * Controller中使用方法:$this->controller->get_token()
  137. * @return
  138. */
  139. public function get_token() {
  140. return $_COOKIE['init_token'];
  141. }
  142. /**
  143. * 类加载-检测token值
  144. * Controller中使用方法:$this->controller->check_token($ispost = true)
  145. * @return
  146. */
  147. public function check_token($ispost = true) {
  148. if ($ispost && !$this->is_post()) return false;
  149. if ($this->get_gp('init_token') != $this->get_token()) return false;
  150. return true;
  151. }
  152. /**
  153. * 类加载-设置全局TOKEN,防止CSRF攻击
  154. * Controller中使用方法:$this->controller->set_token()
  155. * @return
  156. */
  157. private function set_token() {
  158. if (!$_COOKIE['init_token']) {
  159. $str = substr(md5(time(). $this->get_useragent()), 5, 8);
  160. setcookie("init_token", $str, NULL, '/');
  161. $_COOKIE['init_token'] = $str;
  162. }
  163. }
  164. }