controller.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. '3gp' => 'video/3gpp',
  37. 'ai' => 'application/postscript',
  38. 'aif' => 'audio/x-aiff',
  39. 'aifc' => 'audio/x-aiff',
  40. 'aiff' => 'audio/x-aiff',
  41. 'asc' => 'text/plain',
  42. 'atom' => 'application/atom+xml',
  43. 'au' => 'audio/basic',
  44. 'avi' => 'video/x-msvideo',
  45. 'bcpio' => 'application/x-bcpio',
  46. 'bin' => 'application/octet-stream',
  47. 'bmp' => 'image/bmp',
  48. 'cdf' => 'application/x-netcdf',
  49. 'cgm' => 'image/cgm',
  50. 'class' => 'application/octet-stream',
  51. 'cpio' => 'application/x-cpio',
  52. 'cpt' => 'application/mac-compactpro',
  53. 'csh' => 'application/x-csh',
  54. 'css' => 'text/css',
  55. 'dcr' => 'application/x-director',
  56. 'dif' => 'video/x-dv',
  57. 'dir' => 'application/x-director',
  58. 'djv' => 'image/vnd.djvu',
  59. 'djvu' => 'image/vnd.djvu',
  60. 'dll' => 'application/octet-stream',
  61. 'dmg' => 'application/octet-stream',
  62. 'dms' => 'application/octet-stream',
  63. 'doc' => 'application/msword',
  64. 'dtd' => 'application/xml-dtd',
  65. 'dv' => 'video/x-dv',
  66. 'dvi' => 'application/x-dvi',
  67. 'dxr' => 'application/x-director',
  68. 'eps' => 'application/postscript',
  69. 'etx' => 'text/x-setext',
  70. 'exe' => 'application/octet-stream',
  71. 'ez' => 'application/andrew-inset',
  72. 'flv' => 'video/x-flv',
  73. 'gif' => 'image/gif',
  74. 'gram' => 'application/srgs',
  75. 'grxml' => 'application/srgs+xml',
  76. 'gtar' => 'application/x-gtar',
  77. 'gz' => 'application/x-gzip',
  78. 'hdf' => 'application/x-hdf',
  79. 'hqx' => 'application/mac-binhex40',
  80. 'htm' => 'text/html',
  81. 'html' => 'text/html',
  82. 'ice' => 'x-conference/x-cooltalk',
  83. 'ico' => 'image/x-icon',
  84. 'ics' => 'text/calendar',
  85. 'ief' => 'image/ief',
  86. 'ifb' => 'text/calendar',
  87. 'iges' => 'model/iges',
  88. 'igs' => 'model/iges',
  89. 'jnlp' => 'application/x-java-jnlp-file',
  90. 'jp2' => 'image/jp2',
  91. 'jpe' => 'image/jpeg',
  92. 'jpeg' => 'image/jpeg',
  93. 'jpg' => 'image/jpeg',
  94. 'js' => 'application/x-javascript',
  95. 'kar' => 'audio/midi',
  96. 'latex' => 'application/x-latex',
  97. 'lha' => 'application/octet-stream',
  98. 'lzh' => 'application/octet-stream',
  99. 'm3u' => 'audio/x-mpegurl',
  100. 'm4a' => 'audio/mp4a-latm',
  101. 'm4p' => 'audio/mp4a-latm',
  102. 'm4u' => 'video/vnd.mpegurl',
  103. 'm4v' => 'video/x-m4v',
  104. 'mac' => 'image/x-macpaint',
  105. 'man' => 'application/x-troff-man',
  106. 'mathml' => 'application/mathml+xml',
  107. 'me' => 'application/x-troff-me',
  108. 'mesh' => 'model/mesh',
  109. 'mid' => 'audio/midi',
  110. 'midi' => 'audio/midi',
  111. 'mif' => 'application/vnd.mif',
  112. 'mov' => 'video/quicktime',
  113. 'movie' => 'video/x-sgi-movie',
  114. 'mp2' => 'audio/mpeg',
  115. 'mp3' => 'audio/mpeg',
  116. 'mp4' => 'video/mp4',
  117. 'mpe' => 'video/mpeg',
  118. 'mpeg' => 'video/mpeg',
  119. 'mpg' => 'video/mpeg',
  120. 'mpga' => 'audio/mpeg',
  121. 'ms' => 'application/x-troff-ms',
  122. 'msh' => 'model/mesh',
  123. 'mxu' => 'video/vnd.mpegurl',
  124. 'nc' => 'application/x-netcdf',
  125. 'oda' => 'application/oda',
  126. 'ogg' => 'application/ogg',
  127. 'ogv' => 'video/ogv',
  128. 'pbm' => 'image/x-portable-bitmap',
  129. 'pct' => 'image/pict',
  130. 'pdb' => 'chemical/x-pdb',
  131. 'pdf' => 'application/pdf',
  132. 'pgm' => 'image/x-portable-graymap',
  133. 'pgn' => 'application/x-chess-pgn',
  134. 'pic' => 'image/pict',
  135. 'pict' => 'image/pict',
  136. 'png' => 'image/png',
  137. 'pnm' => 'image/x-portable-anymap',
  138. 'pnt' => 'image/x-macpaint',
  139. 'pntg' => 'image/x-macpaint',
  140. 'ppm' => 'image/x-portable-pixmap',
  141. 'ppt' => 'application/vnd.ms-powerpoint',
  142. 'ps' => 'application/postscript',
  143. 'qt' => 'video/quicktime',
  144. 'qti' => 'image/x-quicktime',
  145. 'qtif' => 'image/x-quicktime',
  146. 'ra' => 'audio/x-pn-realaudio',
  147. 'ram' => 'audio/x-pn-realaudio',
  148. 'ras' => 'image/x-cmu-raster',
  149. 'rdf' => 'application/rdf+xml',
  150. 'rgb' => 'image/x-rgb',
  151. 'rm' => 'application/vnd.rn-realmedia',
  152. 'roff' => 'application/x-troff',
  153. 'rtf' => 'text/rtf',
  154. 'rtx' => 'text/richtext',
  155. 'sgm' => 'text/sgml',
  156. 'sgml' => 'text/sgml',
  157. 'sh' => 'application/x-sh',
  158. 'shar' => 'application/x-shar',
  159. 'silo' => 'model/mesh',
  160. 'sit' => 'application/x-stuffit',
  161. 'skd' => 'application/x-koan',
  162. 'skm' => 'application/x-koan',
  163. 'skp' => 'application/x-koan',
  164. 'skt' => 'application/x-koan',
  165. 'smi' => 'application/smil',
  166. 'smil' => 'application/smil',
  167. 'snd' => 'audio/basic',
  168. 'so' => 'application/octet-stream',
  169. 'spl' => 'application/x-futuresplash',
  170. 'src' => 'application/x-wais-source',
  171. 'sv4cpio' => 'application/x-sv4cpio',
  172. 'sv4crc' => 'application/x-sv4crc',
  173. 'svg' => 'image/svg+xml',
  174. 'swf' => 'application/x-shockwave-flash',
  175. 't' => 'application/x-troff',
  176. 'tar' => 'application/x-tar',
  177. 'tcl' => 'application/x-tcl',
  178. 'tex' => 'application/x-tex',
  179. 'texi' => 'application/x-texinfo',
  180. 'texinfo' => 'application/x-texinfo',
  181. 'tif' => 'image/tiff',
  182. 'tiff' => 'image/tiff',
  183. 'tr' => 'application/x-troff',
  184. 'tsv' => 'text/tab-separated-values',
  185. 'txt' => 'text/plain',
  186. 'ustar' => 'application/x-ustar',
  187. 'vcd' => 'application/x-cdlink',
  188. 'vrml' => 'model/vrml',
  189. 'vxml' => 'application/voicexml+xml',
  190. 'wav' => 'audio/x-wav',
  191. 'wbmp' => 'image/vnd.wap.wbmp',
  192. 'wbxml' => 'application/vnd.wap.wbxml',
  193. 'webm' => 'video/webm',
  194. 'wml' => 'text/vnd.wap.wml',
  195. 'wmlc' => 'application/vnd.wap.wmlc',
  196. 'wmls' => 'text/vnd.wap.wmlscript',
  197. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  198. 'wmv' => 'video/x-ms-wmv',
  199. 'wrl' => 'model/vrml',
  200. 'xbm' => 'image/x-xbitmap',
  201. 'xht' => 'application/xhtml+xml',
  202. 'xhtml' => 'application/xhtml+xml',
  203. 'xls' => 'application/vnd.ms-excel',
  204. 'xml' => 'application/xml',
  205. 'xpm' => 'image/x-xpixmap',
  206. 'xsl' => 'application/xml',
  207. 'xslt' => 'application/xslt+xml',
  208. 'xul' => 'application/vnd.mozilla.xul+xml',
  209. 'xwd' => 'image/x-xwindowdump',
  210. 'xyz' => 'chemical/x-xyz',
  211. 'zip' => 'application/zip'
  212. );
  213. $basename = basename($request);
  214. $ext = strtolower(substr($basename,strrpos($basename,'.')+1));
  215. if(isset($mime_types[$ext])){
  216. $this->content_type=$mime_types[$ext];
  217. }
  218. $direct = false;
  219. if(in_array($ext,explode('|',strtolower(DIRECT_EXT)))){
  220. $direct = true;
  221. }
  222. }
  223. }
  224. //开始处理
  225. $delete = false;
  226. if(count($purge = explode(PURGE_KEY.'/',$request,2))>1){
  227. $delete = true;
  228. $request = $purge[1];
  229. }
  230. $key = (NO_KEY)?$request:md5($request).'_'.strlen($request).'.'.$ext;
  231. $this->hit = false;
  232. $this->handle($request,$key,$delete,$direct);
  233. }
  234. /**
  235. * 获取内容并输出
  236. * 如果stroage里面不存在,则从URL里面获取
  237. * */
  238. private function handle($filename,$key,$delete = false,$direct = false){
  239. $content = '';
  240. if($this->succeed){
  241. $storage = storage::gethandle();
  242. if($delete){
  243. if(!$storage->exists($key)){
  244. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>'not exists')));
  245. }
  246. $return = $storage->delete($key);
  247. die(json_encode(array('purge'=>$filename,'key'=>$key,'success'=>$return)));
  248. }
  249. if($storage->exists($key) && !$direct){
  250. if(!NO_LOCATE && $url = $storage->url($key)){
  251. $this->locate($url);
  252. }
  253. $content = $storage->read($key);
  254. $this->hit = $key;
  255. if(empty($content)){
  256. $this->succeed = false;
  257. $this->error_type = 'empty_conent';
  258. }
  259. }else{
  260. //$content = @file_get_contents(BASE_URL.$filename);
  261. $content = lib::fetch_url(BASE_URL.$filename);
  262. if(!is_array($content) || count($content)<2){
  263. $this->succeed = false;
  264. $this->error_type = 'fetch_error';
  265. }elseif($content[0]==200){
  266. //返回200,才写入
  267. if(!$direct) $storage->write($key, $content[1]);
  268. }else{
  269. header('HTTP/1.1 '.$content[0]);
  270. }
  271. $content = $content[1];
  272. }
  273. }
  274. //显示内容
  275. $this->render($content);
  276. }
  277. /**
  278. * 输出结果,包括缓存控制等
  279. * */
  280. private function render($content=''){
  281. ob_end_clean();
  282. if(!$this->succeed){
  283. $this->error();
  284. return ;
  285. }else{
  286. if($this->hit){
  287. header('Layer-Cache: Hit;key='.$this->hit.';ENV='.RUN_ENV);
  288. }else{
  289. header('Layer-Cache: Miss;ENV='.RUN_ENV);
  290. }
  291. header("Expires: " . date("D, j M Y H:i:s GMT", time()+2592000));//缓存一月
  292. header('Content-type: '.$this->content_type);
  293. echo $content;
  294. }
  295. }
  296. private function locate($url){
  297. //302
  298. header("HTTP/1.1 302 Moved Temporarily");
  299. header("Location:".$url);
  300. die();
  301. }
  302. /**
  303. * 处理错误
  304. * */
  305. private function error(){
  306. $this->content_type = 'text/html';
  307. echo json_encode(array('error'=>$this->error_type));
  308. }
  309. }