WorkerManAdmin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
  3. require_once WORKERMAN_ROOT_DIR . 'applications/Common/Protocols/Http.php';
  4. /**
  5. *
  6. * WorkerMan 管理后台
  7. * HTTP协议
  8. *
  9. * @author walkor <worker-man@qq.com>
  10. */
  11. class WorkerManAdmin extends Man\Core\SocketWorker
  12. {
  13. /**
  14. * 资源类型
  15. * @var array
  16. */
  17. protected static $typeMap = array(
  18. 'js' => 'text/javascript',
  19. 'png' => 'image/png',
  20. 'jpg' => 'image/jpeg',
  21. 'gif' => 'image/gif',
  22. 'png' => 'image/png',
  23. 'css' => 'text/css',
  24. );
  25. public function onStart()
  26. {
  27. App\Common\Protocols\HttpCache::init();
  28. }
  29. /**
  30. * 确定数据是否接收完整
  31. * @see Man\Core.SocketWorker::dealInput()
  32. */
  33. public function dealInput($recv_str)
  34. {
  35. return App\Common\Protocols\http_input($recv_str);
  36. }
  37. /**
  38. * 数据接收完整后处理业务逻辑
  39. * @see Man\Core.SocketWorker::dealProcess()
  40. */
  41. public function dealProcess($recv_str)
  42. {
  43. // http请求处理开始。解析http协议,生成$_POST $_GET $_COOKIE
  44. App\Common\Protocols\http_start($recv_str);
  45. // 开启session
  46. App\Common\Protocols\session_start();
  47. // 缓冲输出
  48. ob_start();
  49. // 请求的文件
  50. $file = $_SERVER['REQUEST_URI'];
  51. $pos = strpos($file, '?');
  52. if($pos !== false)
  53. {
  54. // 去掉文件名后面的querystring
  55. $file = substr($_SERVER['REQUEST_URI'], 0, $pos);
  56. }
  57. // 得到文件真实路径
  58. $file = WORKERMAN_ROOT_DIR . 'applications/WorkerManAdmin/'.$file;
  59. if(!is_file($file))
  60. {
  61. // 从定向到index.php
  62. $file = WORKERMAN_ROOT_DIR . 'applications/WorkerManAdmin/index.php';
  63. }
  64. // 请求的文件存在
  65. if(is_file($file))
  66. {
  67. $extension = pathinfo($file, PATHINFO_EXTENSION);
  68. // 如果请求的是php文件
  69. if($extension == 'php')
  70. {
  71. // 载入php文件
  72. try
  73. {
  74. include $file;
  75. }
  76. catch(\Exception $e)
  77. {
  78. // 如果不是exit
  79. if($e->getMessage() != 'jump_exit')
  80. {
  81. echo $e;
  82. }
  83. }
  84. }
  85. // 请求的是静态资源文件
  86. else
  87. {
  88. if(isset(self::$typeMap[$extension]))
  89. {
  90. App\Common\Protocols\header('Content-Type: '. self::$typeMap[$extension]);
  91. }
  92. // 发送文件
  93. readfile($file);
  94. }
  95. }
  96. else
  97. {
  98. echo 'index.php missed';
  99. }
  100. $content = ob_get_clean();
  101. $buffer = App\Common\Protocols\http_end($content);
  102. $this->sendToClient($buffer);
  103. }
  104. }