fcgi_server.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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','test.php');
  21. $name = basename($file);
  22. return in_array($name,$exfiles);
  23. }
  24. private function parase_requri()
  25. {
  26. $method = strtolower(request_helper::method());
  27. if($method == 'get') {
  28. return;
  29. }
  30. $file = request_helper::req_uri();
  31. $ops = explode("?",$file);
  32. if(count($ops) == 2)
  33. {
  34. $squery = $ops[1];
  35. $params = preg_split('/&|=/', $squery);
  36. for ($i = 0; $i < count($params); ++$i) {
  37. $key = $params[$i];
  38. $val = $params[++$i];
  39. $_GET[$key] = $val;
  40. $_POST[$key] = $val;
  41. }
  42. }
  43. }
  44. public function run_looper()
  45. {
  46. require_once(BASE_ROOT_PATH.'/mobile/index.php');
  47. Base::mobile_init();
  48. while(($ret = fcgi_accept()) >= 0)
  49. {
  50. ob_start();
  51. performance_helper::clear();
  52. http_header::instance()->start();
  53. try
  54. {
  55. Log::start_sql_log();
  56. Log::record("req_uri = " . request_helper::req_uri(),Log::DEBUG);
  57. $this->parase_requri();
  58. init_request();
  59. init_cookie($_SERVER['HTTP_COOKIE']);
  60. session::instance()->start();
  61. $file = request_helper::script_file();
  62. if(file_exists($file))
  63. {
  64. if(self::is_exclude($file)) {
  65. include $file;
  66. }
  67. else
  68. {
  69. fcgi_header("Content-Type: text/html; charset=UTF-8");
  70. if(!array_key_exists('act',$_GET)) {
  71. $_GET['act'] = 'index';
  72. }
  73. if(!array_key_exists('op',$_GET)) {
  74. $_GET['op'] = 'index';
  75. }
  76. if(!array_key_exists('act',$_POST)) {
  77. $_POST['act'] = 'index';
  78. }
  79. if(!array_key_exists('op',$_POST)) {
  80. $_POST['op'] = 'index';
  81. }
  82. Base::mobile_control();
  83. }
  84. }
  85. else
  86. {
  87. fcgi_header("Content-Type: text/html; charset=UTF-8");
  88. echo "no such file.";
  89. }
  90. }
  91. catch (Exception $ex) {
  92. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  93. }
  94. fcgi_headers_sent();
  95. $contents = ob_get_clean();
  96. fcgi_echo($contents);
  97. //Log::record("return msg={$contents}",Log::DEBUG);
  98. session::instance()->end();
  99. fcgi_finish();
  100. }
  101. fcgi_fini();
  102. }
  103. }