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. class fcgi_server
  10. {
  11. static private $stInstance = NULL;
  12. static public function instance()
  13. {
  14. if(self::$stInstance == NULL) {
  15. self::$stInstance = new fcgi_server();
  16. }
  17. return self::$stInstance;
  18. }
  19. private function is_exclude($file)
  20. {
  21. static $exfiles = array('wxnotify.php','pub_wxnotify.php','alipay_notify_url.php','dispatch_notify.php','kdniao_notify.php',
  22. 'cmbpay_notify.php','cmbpay_sign.php','wxauthor.php','api/wxLogin/index.php','api/wxLogin/callback.php',
  23. 'test.php');
  24. $path = BASE_ROOT_PATH . '/mobile/';
  25. $file = str_replace($path,'',$file);
  26. return in_array($file,$exfiles);
  27. }
  28. private function parase_requri()
  29. {
  30. $method = strtolower(request_helper::method());
  31. if($method == 'get') {
  32. return;
  33. }
  34. $file = request_helper::req_uri();
  35. $ops = explode("?",$file);
  36. if(count($ops) == 2)
  37. {
  38. $squery = $ops[1];
  39. $params = preg_split('/&|=/', $squery);
  40. for ($i = 0; $i < count($params); ++$i) {
  41. $key = $params[$i];
  42. $val = $params[++$i];
  43. $_GET[$key] = $val;
  44. $_POST[$key] = $val;
  45. }
  46. }
  47. }
  48. private function clear_global()
  49. {
  50. $_SESSION = [];
  51. $_COOKIE = [];
  52. $_POST = [];
  53. $_GET = [];
  54. }
  55. public function handle_error($level, $message, $file, $line)
  56. {
  57. if($level == E_NOTICE) return;
  58. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  59. $backtrace = debug_backtrace();
  60. foreach ($backtrace as $item) {
  61. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  62. }
  63. Log::record($trace,Log::ERR);
  64. }
  65. public function run_looper()
  66. {
  67. DFAFilter::instance();
  68. area_helper::instance();
  69. set_error_handler([$this, 'handle_error']);
  70. require_once(BASE_ROOT_PATH . '/mobile/index.php');
  71. while(($ret = fcgi_accept()) >= 0)
  72. {
  73. $start = microtime(true);
  74. ob_start();
  75. $this->clear_global();
  76. performance_helper::clear();
  77. http_header::instance()->start();
  78. try
  79. {
  80. Log::start_sql_log();
  81. $this->parase_requri();
  82. init_request();
  83. init_cookie($_SERVER['HTTP_COOKIE']);
  84. $file = request_helper::script_file();
  85. session::instance()->start();
  86. Log::record("member_id=" . session_helper::memberid(),Log::DEBUG);
  87. if(file_exists($file))
  88. {
  89. if(self::is_exclude($file))
  90. {
  91. include $file;
  92. }
  93. else
  94. {
  95. fcgi_header("Content-Type: text/html; charset=UTF-8");
  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. }