fcgi_server.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/3/10
  6. * Time: 下午9:08
  7. */
  8. class fcgi_server
  9. {
  10. static private $stInstance = NULL;
  11. static public function instance()
  12. {
  13. if(self::$stInstance == NULL) {
  14. self::$stInstance = new fcgi_server();
  15. }
  16. return self::$stInstance;
  17. }
  18. private function is_exclude($file)
  19. {
  20. static $exfiles = array('wxnotify.php','alipay_notify_url.php','dispatch_notify.php','kdniao_notify.php',
  21. 'cmbpay_notify.php','cmbpay_sign.php','wxauthor.php','api/wxLogin/index.php','api/wxLogin/callback.php',
  22. 'test.php');
  23. $path = BASE_ROOT_PATH . '/mobile/';
  24. $file = str_replace($path,'',$file);
  25. return in_array($file,$exfiles);
  26. }
  27. private function parase_requri()
  28. {
  29. $method = strtolower(request_helper::method());
  30. if($method == 'get') {
  31. return;
  32. }
  33. $file = request_helper::req_uri();
  34. $ops = explode("?",$file);
  35. if(count($ops) == 2)
  36. {
  37. $squery = $ops[1];
  38. $params = preg_split('/&|=/', $squery);
  39. for ($i = 0; $i < count($params); ++$i) {
  40. $key = $params[$i];
  41. $val = $params[++$i];
  42. $_GET[$key] = $val;
  43. $_POST[$key] = $val;
  44. }
  45. }
  46. }
  47. // private function do_clear($ar)
  48. // {
  49. // if(isset($ar) && is_array($ar))
  50. // {
  51. // foreach($ar as $key=>$value) {
  52. // unset($ar[$key]);
  53. // }
  54. //
  55. // }
  56. // }
  57. private function clear_global()
  58. {
  59. $_SESSION = [];
  60. $_COOKIE = [];
  61. $_POST = [];
  62. $_GET = [];
  63. // $this->do_clear($_SESSION);
  64. // $this->do_clear($_COOKIE);
  65. // $this->do_clear($_POST);
  66. // $this->do_clear($_GET);
  67. }
  68. public function run_looper()
  69. {
  70. DFAFilter::instance();
  71. require_once(BASE_ROOT_PATH.'/mobile/index.php');
  72. while(($ret = fcgi_accept()) >= 0)
  73. {
  74. $start = microtime(true);
  75. ob_start();
  76. $this->clear_global();
  77. performance_helper::clear();
  78. http_header::instance()->start();
  79. try
  80. {
  81. Log::start_sql_log();
  82. $this->parase_requri();
  83. init_request();
  84. init_cookie($_SERVER['HTTP_COOKIE']);
  85. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  86. Log::record($msg,Log::DEBUG);
  87. session::instance()->start();
  88. $file = request_helper::script_file();
  89. $remote_addr = request_helper::remote_addr();
  90. Log::record("file={$file} remoteaddr={$remote_addr}",Log::DEBUG);
  91. if(file_exists($file))
  92. {
  93. if(self::is_exclude($file))
  94. {
  95. include $file;
  96. }
  97. else
  98. {
  99. fcgi_header("Content-Type: text/html; charset=UTF-8");
  100. if(!isset($_GET['act'])) {
  101. $_GET['act'] = 'index';
  102. }
  103. if(!isset($_GET['op'])) {
  104. $_GET['op'] = 'index';
  105. }
  106. if(!isset($_POST['act'])) {
  107. $_POST['act'] = 'index';
  108. }
  109. if(!isset($_POST['op'])) {
  110. $_POST['op'] = 'index';
  111. }
  112. Base::mobile_control();
  113. }
  114. }
  115. else
  116. {
  117. fcgi_header("Content-Type: text/html; charset=UTF-8");
  118. echo "no such file.";
  119. }
  120. }
  121. catch (Exception $ex) {
  122. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  123. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()}",Log::ERR);
  124. }
  125. session::instance()->end();
  126. fcgi_headers_sent();
  127. $contents = ob_get_clean();
  128. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  129. Log::record($msg,Log::DEBUG);
  130. Log::record("content={$contents}",Log::DEBUG);
  131. fcgi_echo($contents);
  132. //fcgi_finish();//单线程的情况下不需要调用
  133. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  134. Log::record($msg,Log::DEBUG);
  135. }
  136. fcgi_fini();
  137. }
  138. }