fcgi_server.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 . '/helper/area_helper.php');
  9. class fcgi_server
  10. {
  11. static private $stInstance = NULL;
  12. static public function instance()
  13. {
  14. if(self::$stInstance == NULL) {
  15. self::$stInstance = new fcgi_server();
  16. }
  17. return self::$stInstance;
  18. }
  19. private function is_exclude($file)
  20. {
  21. static $exfiles = array('wxnotify.php','pub_wxnotify.php','alipay_notify_url.php','dispatch_notify.php','kdniao_notify.php',
  22. 'cmbpay_notify.php','cmbpay_sign.php','wxauthor.php','api/wxLogin/index.php','api/wxLogin/callback.php',
  23. 'test.php');
  24. $path = BASE_ROOT_PATH . '/mobile/';
  25. $file = str_replace($path,'',$file);
  26. return in_array($file,$exfiles);
  27. }
  28. private function parase_requri()
  29. {
  30. $method = strtolower(request_helper::method());
  31. if($method == 'get') {
  32. return;
  33. }
  34. $file = request_helper::req_uri();
  35. $ops = explode("?",$file);
  36. if(count($ops) == 2)
  37. {
  38. $squery = $ops[1];
  39. $params = preg_split('/&|=/', $squery);
  40. for ($i = 0; $i < count($params); ++$i) {
  41. $key = $params[$i];
  42. $val = $params[++$i];
  43. $_GET[$key] = $val;
  44. $_POST[$key] = $val;
  45. }
  46. }
  47. }
  48. private function clear_global()
  49. {
  50. $_SESSION = [];
  51. $_COOKIE = [];
  52. $_POST = [];
  53. $_GET = [];
  54. }
  55. public function run_looper()
  56. {
  57. DFAFilter::instance();
  58. area_helper::instance();
  59. require_once(BASE_ROOT_PATH.'/mobile/index.php');
  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. $this->parase_requri();
  71. init_request();
  72. init_cookie($_SERVER['HTTP_COOKIE']);
  73. $file = request_helper::script_file();
  74. session::instance()->start();
  75. $remote_addr = request_helper::remote_addr();
  76. Log::record("file={$file} remoteaddr={$remote_addr}",Log::DEBUG);
  77. if(file_exists($file))
  78. {
  79. if(self::is_exclude($file))
  80. {
  81. include $file;
  82. }
  83. else
  84. {
  85. fcgi_header("Content-Type: text/html; charset=UTF-8");
  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. Base::mobile_control();
  99. }
  100. }
  101. else
  102. {
  103. fcgi_header("Content-Type: text/html; charset=UTF-8");
  104. echo "no such file.";
  105. }
  106. }
  107. catch (Exception $ex) {
  108. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  109. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()} trace={$ex->getTraceAsString()}",Log::ERR);
  110. }
  111. session::instance()->end();
  112. fcgi_headers_sent();
  113. $contents = ob_get_clean();
  114. fcgi_echo($contents);
  115. Log::end_sql_log();
  116. //fcgi_finish();//单线程的情况下不需要调用
  117. $msg = sprintf("request time=%.6f\r\n\r\n",microtime(true) - $start);
  118. Log::record($msg,Log::DEBUG);
  119. }
  120. fcgi_fini();
  121. }
  122. }