fcgi_server.php 5.3 KB

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