fcgi_server.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. 'test.php'];
  27. $path = BASE_ROOT_PATH . '/mobile/';
  28. $file = str_replace($path,'',$file);
  29. return in_array($file,$exfiles);
  30. }
  31. private function parase_requri()
  32. {
  33. $method = strtolower(request_helper::method());
  34. if($method == 'get') {
  35. return;
  36. }
  37. $file = request_helper::req_uri();
  38. $ops = explode("?",$file);
  39. if(count($ops) == 2)
  40. {
  41. $squery = $ops[1];
  42. $params = preg_split('/&|=/', $squery);
  43. for ($i = 0; $i < count($params); ++$i) {
  44. $key = $params[$i];
  45. $val = $params[++$i];
  46. $_GET[$key] = $val;
  47. $_POST[$key] = $val;
  48. }
  49. }
  50. }
  51. private function clear_global()
  52. {
  53. $_SESSION = [];
  54. $_COOKIE = [];
  55. $_POST = [];
  56. $_GET = [];
  57. }
  58. public function handle_error($level, $message, $file, $line)
  59. {
  60. if($level == E_NOTICE) return;
  61. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  62. $backtrace = debug_backtrace();
  63. foreach ($backtrace as $item) {
  64. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  65. }
  66. Log::record($trace,Log::ERR);
  67. }
  68. public function run_looper()
  69. {
  70. Log::record(__FUNCTION__,Log::DEBUG);
  71. // DFAFilter::instance();
  72. area_helper::instance();
  73. set_error_handler([$this, 'handle_error']);
  74. Log::record('Waiting......',Log::DEBUG);
  75. while(($ret = fcgi_accept()) >= 0)
  76. {
  77. $start = microtime(true);
  78. ob_start();
  79. $this->clear_global();
  80. performance_helper::clear();
  81. http_header::instance()->start();
  82. try
  83. {
  84. Log::start_sql_log();
  85. $this->parase_requri();
  86. init_request();
  87. init_cookie($_SERVER['HTTP_COOKIE']);
  88. $file = request_helper::script_file();
  89. session::instance()->start();
  90. Log::record("member_id=" . session_helper::memberid(),Log::DEBUG);
  91. if(file_exists($file))
  92. {
  93. fcgi_header("Content-Type: text/html; charset=UTF-8");
  94. if(self::is_exclude($file)) {
  95. include $file;
  96. }
  97. else
  98. {
  99. if(!isset($_GET['act'])) {
  100. $_GET['act'] = 'index';
  101. }
  102. if(!isset($_GET['op'])) {
  103. $_GET['op'] = 'index';
  104. }
  105. if(!isset($_POST['act'])) {
  106. $_POST['act'] = 'index';
  107. }
  108. if(!isset($_POST['op'])) {
  109. $_POST['op'] = 'index';
  110. }
  111. Base::mobile_control();
  112. }
  113. }
  114. else
  115. {
  116. fcgi_header("Content-Type: text/html; charset=UTF-8");
  117. echo "no such file.";
  118. }
  119. }
  120. catch (UnloginException $ex) {
  121. mobileControl::outerr(errcode::ErrUnLogin,errcode::msg(errcode::ErrUnLogin));
  122. }
  123. catch (Exception $ex) {
  124. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  125. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()} trace={$ex->getTraceAsString()}",Log::ERR);
  126. }
  127. session::instance()->end();
  128. fcgi_headers_sent();
  129. $contents = ob_get_clean();
  130. fcgi_echo($contents);
  131. Log::end_sql_log();
  132. //fcgi_finish();//单线程的情况下不需要调用
  133. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  134. Log::record($msg ,Log::DEBUG);
  135. }
  136. fcgi_fini();
  137. Log::record('Waiting......',Log::DEBUG);
  138. }
  139. }