fcgi_server.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. DFAFilter::instance();
  71. area_helper::instance();
  72. set_error_handler([$this, 'handle_error']);
  73. while(($ret = fcgi_accept()) >= 0)
  74. {
  75. $start = microtime(true);
  76. ob_start();
  77. $this->clear_global();
  78. performance_helper::clear();
  79. http_header::instance()->start();
  80. try
  81. {
  82. Log::start_sql_log();
  83. $this->parase_requri();
  84. init_request();
  85. init_cookie($_SERVER['HTTP_COOKIE']);
  86. $file = request_helper::script_file();
  87. session::instance()->start();
  88. Log::record("member_id=" . session_helper::memberid(),Log::DEBUG);
  89. if(file_exists($file))
  90. {
  91. fcgi_header("Content-Type: text/html; charset=UTF-8");
  92. if(self::is_exclude($file)) {
  93. include $file;
  94. }
  95. else
  96. {
  97. if(!isset($_GET['act'])) {
  98. $_GET['act'] = 'index';
  99. }
  100. if(!isset($_GET['op'])) {
  101. $_GET['op'] = 'index';
  102. }
  103. if(!isset($_POST['act'])) {
  104. $_POST['act'] = 'index';
  105. }
  106. if(!isset($_POST['op'])) {
  107. $_POST['op'] = 'index';
  108. }
  109. Base::mobile_control();
  110. }
  111. }
  112. else
  113. {
  114. fcgi_header("Content-Type: text/html; charset=UTF-8");
  115. echo "no such file.";
  116. }
  117. }
  118. catch (UnloginException $ex) {
  119. mobileControl::outerr(errcode::ErrUnLogin,errcode::msg(errcode::ErrUnLogin));
  120. }
  121. catch (Exception $ex) {
  122. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  123. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()} trace={$ex->getTraceAsString()}",Log::ERR);
  124. }
  125. session::instance()->end();
  126. fcgi_headers_sent();
  127. $contents = ob_get_clean();
  128. fcgi_echo($contents);
  129. Log::end_sql_log();
  130. //fcgi_finish();//单线程的情况下不需要调用
  131. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  132. Log::record($msg ,Log::DEBUG);
  133. }
  134. fcgi_fini();
  135. }
  136. }