fcgi_server.php 5.4 KB

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