controller.php 4.4 KB

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