fcgi_server.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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',
  22. 'test.php');
  23. $name = basename($file);
  24. return in_array($name,$exfiles);
  25. }
  26. private function parase_requri()
  27. {
  28. $method = strtolower(request_helper::method());
  29. if($method == 'get') {
  30. return;
  31. }
  32. $file = request_helper::req_uri();
  33. $ops = explode("?",$file);
  34. if(count($ops) == 2)
  35. {
  36. $squery = $ops[1];
  37. $params = preg_split('/&|=/', $squery);
  38. for ($i = 0; $i < count($params); ++$i) {
  39. $key = $params[$i];
  40. $val = $params[++$i];
  41. $_GET[$key] = $val;
  42. $_POST[$key] = $val;
  43. }
  44. }
  45. }
  46. private function do_clear($ar)
  47. {
  48. if(isset($ar) && is_array($ar))
  49. {
  50. foreach($ar as $key=>$value) {
  51. unset($ar[$key]);
  52. }
  53. }
  54. }
  55. private function clear_global()
  56. {
  57. $this->do_clear($_SESSION);
  58. $this->do_clear($_COOKIE);
  59. $this->do_clear($_POST);
  60. $this->do_clear($_GET);
  61. }
  62. public function run_looper()
  63. {
  64. DFAFilter::instance();
  65. require_once(BASE_ROOT_PATH.'/mobile/index.php');
  66. while(($ret = fcgi_accept()) >= 0)
  67. {
  68. $start = microtime(true);
  69. ob_start();
  70. $this->clear_global();
  71. performance_helper::clear();
  72. http_header::instance()->start();
  73. try
  74. {
  75. Log::start_sql_log();
  76. $this->parase_requri();
  77. init_request();
  78. init_cookie($_SERVER['HTTP_COOKIE']);
  79. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  80. Log::record($msg,Log::ERR);
  81. session::instance()->start();
  82. $file = request_helper::script_file();
  83. if(file_exists($file))
  84. {
  85. if(self::is_exclude($file))
  86. {
  87. include $file;
  88. }
  89. else
  90. {
  91. fcgi_header("Content-Type: text/html; charset=UTF-8");
  92. if(!isset($_GET['act'])) {
  93. $_GET['act'] = 'index';
  94. }
  95. if(!isset($_GET['op'])) {
  96. $_GET['op'] = 'index';
  97. }
  98. if(!isset($_POST['act'])) {
  99. $_POST['act'] = 'index';
  100. }
  101. if(!isset($_POST['op'])) {
  102. $_POST['op'] = 'index';
  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 (Exception $ex) {
  114. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  115. Log::record("run_looper exception catch code={$ex->getCode()} msg={$ex->getMessage()}",Log::ERR);
  116. }
  117. session::instance()->end();
  118. fcgi_headers_sent();
  119. $contents = ob_get_clean();
  120. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  121. Log::record($msg,Log::DEBUG);
  122. fcgi_echo($contents);
  123. //fcgi_finish();//单线程的情况下不需要调用
  124. $msg = sprintf("request time=%.6f",microtime(true) - $start);
  125. Log::record($msg,Log::DEBUG);
  126. }
  127. fcgi_fini();
  128. }
  129. }