fcgi_server.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. } else {
  67. fcgi_header("Content-Type: text/html; charset=UTF-8");
  68. Base::mobile_control();
  69. }
  70. }
  71. else
  72. {
  73. fcgi_header("Content-Type: text/html; charset=UTF-8");
  74. echo "no such file.";
  75. }
  76. }
  77. catch (Exception $ex) {
  78. mobileControl::outerr($ex->getCode(),$ex->getMessage());
  79. }
  80. fcgi_headers_sent();
  81. $contents = ob_get_clean();
  82. fcgi_echo($contents);
  83. Log::record("return msg={$contents}",Log::DEBUG);
  84. session::instance()->end();
  85. fcgi_finish();
  86. }
  87. fcgi_fini();
  88. }
  89. }