fcgi_server.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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','refill_bxtwt.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. foreach ($params as $pair)
  46. {
  47. $kv = explode('=', $pair);
  48. $count = count($kv);
  49. if($count === 1) {
  50. $key = $kv[0];
  51. $val = "";
  52. }
  53. elseif($count === 2) {
  54. $key = $kv[0];
  55. $val = urldecode($kv[1]);
  56. }
  57. else {
  58. continue;
  59. }
  60. $_GET[$key] = $val;
  61. $_POST[$key] = $val;
  62. Log::record("{$key}:{$val}",Log::DEBUG);
  63. }
  64. }
  65. }
  66. private function clear_global()
  67. {
  68. $_SESSION = [];
  69. $_COOKIE = [];
  70. $_POST = [];
  71. $_GET = [];
  72. }
  73. public function handle_error($level, $message, $file, $line)
  74. {
  75. if($level == E_NOTICE) return;
  76. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  77. $backtrace = debug_backtrace();
  78. foreach ($backtrace as $item) {
  79. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  80. }
  81. Log::record($trace,Log::ERR);
  82. }
  83. public function run_looper()
  84. {
  85. Log::record(__FUNCTION__,Log::DEBUG);
  86. DFAFilter::instance();
  87. area_helper::instance();
  88. set_error_handler([$this, 'handle_error']);
  89. Log::record('Waiting......',Log::DEBUG);
  90. while(($ret = fcgi_accept()) >= 0)
  91. {
  92. $start = microtime(true);
  93. ob_start();
  94. $this->clear_global();
  95. performance_helper::clear();
  96. http_header::instance()->start();
  97. try
  98. {
  99. Log::start_sql_log();
  100. $this->parase_requri();
  101. init_request();
  102. init_cookie($_SERVER['HTTP_COOKIE']);
  103. $file = request_helper::script_file();
  104. if(file_exists($file))
  105. {
  106. if(defined('CROSS_DOAMIN') && CROSS_DOAMIN == true) {
  107. $host = 'http://localhost:3333';
  108. fcgi_header("Content-Type: text/html; charset=UTF-8");
  109. fcgi_header("Access-Control-Allow-Credentials: true");
  110. fcgi_header("Access-Control-Allow-Origin: {$host}");
  111. fcgi_header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,PATCH');
  112. }
  113. if(self::is_exclude($file)) {
  114. include $file;
  115. }
  116. else
  117. {
  118. if(!isset($_GET['act'])) {
  119. $_GET['act'] = 'index';
  120. }
  121. if(!isset($_GET['op'])) {
  122. $_GET['op'] = 'index';
  123. }
  124. if(!isset($_POST['act'])) {
  125. $_POST['act'] = 'index';
  126. }
  127. if(!isset($_POST['op'])) {
  128. $_POST['op'] = 'index';
  129. }
  130. //部分控制器不需要使用session.
  131. $act = $_GET['act'];
  132. if($act != 'refill') {
  133. session::instance()->start();
  134. Log::record("member_id=" . session_helper::memberid(),Log::DEBUG);
  135. }
  136. Base::mobile_control();
  137. }
  138. }
  139. else
  140. {
  141. fcgi_header("Content-Type: text/html; charset=UTF-8");
  142. echo "no such file.";
  143. }
  144. }
  145. catch (UnloginException $ex) {
  146. mobileControl::outerr(errcode::ErrUnLogin,errcode::msg(errcode::ErrUnLogin));
  147. }
  148. catch (Exception $ex) {
  149. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  150. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()} trace={$ex->getTraceAsString()}",Log::ERR);
  151. }
  152. session::instance()->end();
  153. fcgi_headers_sent();
  154. $contents = ob_get_clean();
  155. fcgi_echo($contents);
  156. Log::end_sql_log();
  157. Log::record("content={$contents}",Log::DEBUG);
  158. //fcgi_finish();//单线程的情况下不需要调用
  159. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  160. // Log::record($msg ,Log::DEBUG);
  161. }
  162. fcgi_fini();
  163. Log::record('Waiting quit......',Log::DEBUG);
  164. }
  165. }