fcgi_server.php 5.2 KB

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