controller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // $temp = array();
  33. // if(preg_match('/\.(jpg|jpeg|png|pdf|gif|css|js|zip)$/i', $request,$temp)===1){//暂时先就这几种
  34. // //http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types
  35. // switch($temp[1]){
  36. // case 'jpg':{$this->content_type="image/jpeg";}break;
  37. // case 'gif':{$this->content_type="image/gif";}break;
  38. // case 'png':{$this->content_type="image/png";}break;
  39. // case 'css':{$this->content_type="text/css";}break;
  40. // case 'js':{$this->content_type="text/javascript";}break;
  41. // }
  42. // }
  43. $mime_types = array(
  44. 'jpg' => 'image/jpeg',
  45. 'gif' => 'image/gif',
  46. 'png' => 'image/png',
  47. 'css' => 'text/css',
  48. 'txt' => 'text/plain',
  49. 'js' => 'text/javascript',
  50. 'html' => 'text/html',
  51. 'htm' => 'text/htm',
  52. 'rss' => 'application/atom+xml',
  53. 'json' => 'application/json',
  54. 'ogg' => 'audio/ogg',
  55. 'pdf' => 'application/pdf',
  56. 'xml' => 'text/xml',
  57. 'zip' => 'application/zip',
  58. 'rar' => 'application/octet-stream',
  59. 'gz' => 'application/gzip',
  60. 'gzip' => 'application/gzip',
  61. 'wav' => 'audio/vnd.wave',
  62. 'mp3' => 'audio/mp3',
  63. 'mp4' => 'video/mp4',
  64. 'flv' => 'video/x-flv',
  65. );
  66. $basename = basename($request);
  67. $ext = strtolower(substr($basename,strrpos($basename,'.')+1));
  68. if(isset($mime_types[$ext])){
  69. $this->content_type=$mime_types[$ext];
  70. }
  71. }
  72. }
  73. //开始处理
  74. $delete = false;
  75. if(count($purge = explode(PURGE_KEY.'/',$request,2))>1){
  76. $delete = true;
  77. $request = $purge[1];
  78. }
  79. $key = md5($request).'_'.strlen($request).'.cache';
  80. $this->hit = $key;
  81. $this->handle($request,$key,$delete);
  82. }
  83. /**
  84. * 获取内容并输出
  85. * 如果stroage里面不存在,则从URL里面获取
  86. * */
  87. private function handle($filename,$key,$delete = false){
  88. $content = '';
  89. if($this->succeed){
  90. $storage = storage::gethandle();
  91. if($delete){
  92. if(!$storage->exists($key)){
  93. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>'not exists')));
  94. }
  95. $return = $storage->delete($key);
  96. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>$return)));
  97. }
  98. if($storage->exists($key)){
  99. if($url = $storage->url($key)){
  100. $this->locate($url);
  101. }
  102. $content = $storage->read($key);
  103. }else{
  104. //$content = @file_get_contents(BASE_URL.$filename);
  105. $content = lib::fetch_url(BASE_URL.$filename);
  106. $storage->write($key, $content);
  107. }
  108. if(empty($content)){
  109. $this->error_type = 'empty_content';
  110. $this->succeed = FALSE;
  111. }else{
  112. //这里应该有更多的检查
  113. }
  114. }
  115. //显示内容
  116. $this->render($content);
  117. }
  118. /**
  119. * 输出结果,包括缓存控制等
  120. * */
  121. private function render($content=''){
  122. ob_end_clean();
  123. if(!$this->succeed){
  124. $this->error();
  125. return ;
  126. }else{
  127. if($this->hit){
  128. header('Layer-Cache: Hit;key='.$this->hit);
  129. }else{
  130. header('Layer-Cache: Miss');
  131. }
  132. header("Expires: " . date("D, j M Y H:i:s GMT", time()+2592000));//缓存一月
  133. header('Content-type: '.$this->content_type);
  134. echo $content;
  135. }
  136. }
  137. private function loacte($url){
  138. //302
  139. header("HTTP/1.1 302 Moved Temporarily");
  140. header("Location:".$url);
  141. }
  142. /**
  143. * 处理错误
  144. * */
  145. private function error(){
  146. $this->content_type = 'text/html';
  147. echo json_encode(array('error'=>$this->error_type));
  148. }
  149. }