fcgi_server.php 5.2 KB

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