fcgi_server.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/3/10
  6. * Time: 下午9:08
  7. */
  8. require_once (BASE_ROOT_PATH . '/mobile/index.php');
  9. require_once (BASE_ROOT_PATH . '/helper/area_helper.php');
  10. require_once (BASE_CORE_PATH . '/framework/function/http.php');
  11. class fcgi_server
  12. {
  13. static private $stInstance = NULL;
  14. static public function instance()
  15. {
  16. if(self::$stInstance == NULL) {
  17. self::$stInstance = new fcgi_server();
  18. }
  19. return self::$stInstance;
  20. }
  21. private function is_exclude($file)
  22. {
  23. static $exfiles = ['web_wxnotify.php',
  24. 'wxnotify.php','pub_wxnotify.php','alipay_notify_url.php','dispatch_notify.php','kdniao_notify.php',
  25. 'cmbpay_notify.php','cmbpay_sign.php','wxauthor.php','api/wxLogin/index.php','api/wxLogin/callback.php',
  26. 'signature.php',
  27. 'refill_suhc.php','refill_beixt.php'
  28. ];
  29. $path = BASE_ROOT_PATH . '/mobile/';
  30. $file = str_replace($path,'',$file);
  31. return in_array($file,$exfiles);
  32. }
  33. private function parase_requri()
  34. {
  35. $method = strtolower(request_helper::method());
  36. if($method == 'get') {
  37. return;
  38. }
  39. $file = request_helper::req_uri();
  40. $ops = explode("?",$file);
  41. if(count($ops) == 2)
  42. {
  43. $squery = $ops[1];
  44. $params = preg_split('/&|=/', $squery);
  45. for ($i = 0; $i < count($params); ++$i) {
  46. $key = $params[$i];
  47. $val = $params[++$i];
  48. $_GET[$key] = $val;
  49. $_POST[$key] = $val;
  50. }
  51. }
  52. }
  53. private function clear_global()
  54. {
  55. $_SESSION = [];
  56. $_COOKIE = [];
  57. $_POST = [];
  58. $_GET = [];
  59. }
  60. public function handle_error($level, $message, $file, $line)
  61. {
  62. if($level == E_NOTICE) return;
  63. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  64. $backtrace = debug_backtrace();
  65. foreach ($backtrace as $item) {
  66. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  67. }
  68. Log::record($trace,Log::ERR);
  69. }
  70. public function run_looper()
  71. {
  72. Log::record(__FUNCTION__,Log::DEBUG);
  73. DFAFilter::instance();
  74. area_helper::instance();
  75. set_error_handler([$this, 'handle_error']);
  76. Log::record('Waiting......',Log::DEBUG);
  77. while(($ret = fcgi_accept()) >= 0)
  78. {
  79. $start = microtime(true);
  80. ob_start();
  81. $this->clear_global();
  82. performance_helper::clear();
  83. http_header::instance()->start();
  84. try
  85. {
  86. Log::start_sql_log();
  87. $this->parase_requri();
  88. init_request();
  89. init_cookie($_SERVER['HTTP_COOKIE']);
  90. $file = request_helper::script_file();
  91. if(file_exists($file))
  92. {
  93. if(defined('CROSS_DOAMIN') && CROSS_DOAMIN == true) {
  94. $host = 'http://localhost:3333';
  95. fcgi_header("Content-Type: text/html; charset=UTF-8");
  96. fcgi_header("Access-Control-Allow-Credentials: true");
  97. fcgi_header("Access-Control-Allow-Origin: {$host}");
  98. fcgi_header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,PATCH');
  99. }
  100. if(self::is_exclude($file)) {
  101. include $file;
  102. }
  103. else
  104. {
  105. if(!isset($_GET['act'])) {
  106. $_GET['act'] = 'index';
  107. }
  108. if(!isset($_GET['op'])) {
  109. $_GET['op'] = 'index';
  110. }
  111. if(!isset($_POST['act'])) {
  112. $_POST['act'] = 'index';
  113. }
  114. if(!isset($_POST['op'])) {
  115. $_POST['op'] = 'index';
  116. }
  117. //部分控制器不需要使用session.
  118. $act = $_GET['act'];
  119. if($act != 'refill') {
  120. session::instance()->start();
  121. Log::record("member_id=" . session_helper::memberid(),Log::DEBUG);
  122. }
  123. Base::mobile_control();
  124. }
  125. }
  126. else
  127. {
  128. fcgi_header("Content-Type: text/html; charset=UTF-8");
  129. echo "no such file.";
  130. }
  131. }
  132. catch (UnloginException $ex) {
  133. mobileControl::outerr(errcode::ErrUnLogin,errcode::msg(errcode::ErrUnLogin));
  134. }
  135. catch (Exception $ex) {
  136. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  137. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()} trace={$ex->getTraceAsString()}",Log::ERR);
  138. }
  139. session::instance()->end();
  140. fcgi_headers_sent();
  141. $contents = ob_get_clean();
  142. fcgi_echo($contents);
  143. Log::end_sql_log();
  144. Log::record("content={$contents}",Log::DEBUG);
  145. //fcgi_finish();//单线程的情况下不需要调用
  146. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  147. Log::record($msg ,Log::DEBUG);
  148. }
  149. fcgi_fini();
  150. Log::record('Waiting quit......',Log::DEBUG);
  151. }
  152. }