controller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
  3. class controller{
  4. public $content_type;
  5. public $succeed ;
  6. public $error_type;
  7. private $hit = false;
  8. public function __construct($request = ''){
  9. $this->content_type = 'text/html';
  10. $this->error_type = 0;
  11. $this->succeed = TRUE;
  12. $request = ltrim($request,'/');
  13. //检测环境
  14. if(!RUN_ENV){
  15. $this->error_type = 'no_run_env';
  16. $this->succeed = FALSE;
  17. }
  18. //请求为空
  19. elseif($request === '' && WELCOME_DOC){
  20. //显示欢迎页面
  21. view::show('welcome');
  22. return ;
  23. }
  24. else{
  25. //检查防盗链
  26. $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
  27. if(ALLOW_REGX && !preg_match('/'.ALLOW_REGX.'/i',$referer)){
  28. $this->error_type = 'not_allowed_domain';
  29. $this->succeed = FALSE;
  30. }else{
  31. //匹配文件后缀
  32. $mime_types = array(
  33. 'jpg' => 'image/jpeg',
  34. 'jpeg' => 'image/jpeg',
  35. 'gif' => 'image/gif',
  36. 'png' => 'image/png',
  37. 'ico' => 'image/jpeg',
  38. 'css' => 'text/css',
  39. 'txt' => 'text/plain',
  40. 'js' => 'text/javascript',
  41. 'html' => 'text/html',
  42. 'htm' => 'text/html',
  43. 'php' => 'text/html',
  44. 'asp' => 'text/html',
  45. 'rss' => 'application/atom+xml',
  46. 'json' => 'application/json',
  47. 'ogg' => 'audio/ogg',
  48. 'pdf' => 'application/pdf',
  49. 'xml' => 'text/xml',
  50. 'zip' => 'application/zip',
  51. 'rar' => 'application/octet-stream',
  52. 'exe' => 'application/octet-stream',
  53. 'chm' => 'application/octet-stream',
  54. 'gz' => 'application/gzip',
  55. 'gzip' => 'application/gzip',
  56. 'wav' => 'audio/vnd.wave',
  57. 'mp3' => 'audio/mp3',
  58. 'mp4' => 'video/mp4',
  59. 'flv' => 'video/x-flv',
  60. );
  61. $basename = basename($request);
  62. $ext = strtolower(substr($basename,strrpos($basename,'.')+1));
  63. if(isset($mime_types[$ext])){
  64. $this->content_type=$mime_types[$ext];
  65. }
  66. $direct = false;
  67. if(in_array($ext,explode('|',strtolower(DIRECT_EXT)))){
  68. $direct = true;
  69. }
  70. }
  71. }
  72. //开始处理
  73. $delete = false;
  74. if(count($purge = explode(PURGE_KEY.'/',$request,2))>1){
  75. $delete = true;
  76. $request = $purge[1];
  77. }
  78. $key = md5($request).'_'.strlen($request).'.cache';
  79. $this->hit = $key;
  80. $this->handle($request,$key,$delete,$direct);
  81. }
  82. /**
  83. * 获取内容并输出
  84. * 如果stroage里面不存在,则从URL里面获取
  85. * */
  86. private function handle($filename,$key,$delete = false,$direct = false){
  87. $content = '';
  88. if($this->succeed){
  89. $storage = storage::gethandle();
  90. if($delete){
  91. if(!$storage->exists($key)){
  92. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>'not exists')));
  93. }
  94. $return = $storage->delete($key);
  95. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>$return)));
  96. }
  97. if($storage->exists($key) && !$direct){
  98. if($url = $storage->url($key)){
  99. $this->locate($url);
  100. }
  101. $content = $storage->read($key);
  102. if(empty($content)){
  103. $this->succeed = false;
  104. $this->error_type = 'empty_conent';
  105. }
  106. }else{
  107. //$content = @file_get_contents(BASE_URL.$filename);
  108. $content = lib::fetch_url(BASE_URL.$filename);
  109. if(!is_array($content) || count($content)<2){
  110. $this->succeed = false;
  111. $this->error_type = 'fetch_error';
  112. }elseif($content[0]==200){
  113. //返回200,才写入
  114. if(!$direct) $storage->write($key, $content[1]);
  115. }else{
  116. header('HTTP/1.1 '.$content[0]);
  117. }
  118. $content = $content[1];
  119. }
  120. }
  121. //显示内容
  122. $this->render($content);
  123. }
  124. /**
  125. * 输出结果,包括缓存控制等
  126. * */
  127. private function render($content=''){
  128. ob_end_clean();
  129. if(!$this->succeed){
  130. $this->error();
  131. return ;
  132. }else{
  133. if($this->hit){
  134. header('Layer-Cache: Hit;key='.$this->hit.';ENV='.RUN_ENV);
  135. }else{
  136. header('Layer-Cache: Miss;ENV='.RUN_ENV);
  137. }
  138. header("Expires: " . date("D, j M Y H:i:s GMT", time()+2592000));//缓存一月
  139. header('Content-type: '.$this->content_type);
  140. echo $content;
  141. }
  142. }
  143. private function loacte($url){
  144. //302
  145. header("HTTP/1.1 302 Moved Temporarily");
  146. header("Location:".$url);
  147. }
  148. /**
  149. * 处理错误
  150. * */
  151. private function error(){
  152. $this->content_type = 'text/html';
  153. echo json_encode(array('error'=>$this->error_type));
  154. }
  155. }