fcgi_server.php 4.7 KB

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