M3u8.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class M3u8 extends CI_Controller {
  4. function __construct(){
  5. parent::__construct();
  6. //防盗链
  7. if(!is_referer(1)){
  8. Header("HTTP/1.1 404 Not Found");
  9. exit('The path is not correct');
  10. }
  11. }
  12. //m3u8列表
  13. public function index($uri=''){
  14. $m3u8path = sys_auth(str_replace('.m3u8', '', $uri),1);
  15. if(empty($m3u8path)){
  16. Header("HTTP/1.1 404 Not Found");
  17. exit('File address error');
  18. }
  19. $Mu_Path = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  20. $m3u8path = $m3u8path;
  21. if(!file_exists($Mu_Path.$m3u8path)){
  22. Header("HTTP/1.1 404 Not Found");
  23. exit('404');
  24. }
  25. //获取实际路径
  26. $varr = explode('/', $m3u8path);
  27. array_pop($varr);
  28. $vpath = implode('/',$varr).'/';
  29. $data = file_get_contents($Mu_Path.$m3u8path);
  30. $marr = explode("\n", $data);
  31. for ($i=0; $i < count($marr); $i++) {
  32. if(substr($marr[$i],-3) == '.ts'){
  33. $yts = str_replace("\r", "", $marr[$i]);
  34. if(Mu_Tsext == ''){
  35. $xts = 'ts/'.sys_auth($vpath.$yts).'.ts';
  36. }else{
  37. $xts = 'ts/'.sys_auth($vpath.$yts).'.'.Mu_Tsext;
  38. }
  39. $data = str_replace($yts, $xts, $data);
  40. }
  41. }
  42. $xkey = sys_auth($vpath.'key.key');
  43. $data = str_replace('URI="key.key"', 'URI="key/'.$xkey.'.key"', $data);
  44. header('Content-type: application/vnd.apple.mpegURL');
  45. header('Content-disposition: attachment; filename='.$uri);
  46. echo $data;
  47. }
  48. //秘钥
  49. public function key($uri=''){
  50. $keypath = sys_auth(str_replace('.key', '', $uri),1);
  51. if(empty($keypath)){
  52. Header("HTTP/1.1 404 Not Found");
  53. exit('File address error');
  54. }
  55. $Mu_Path = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  56. $key_path = $Mu_Path.$keypath;
  57. if(!file_exists($key_path)){
  58. Header("HTTP/1.1 404 Not Found");
  59. exit('404');
  60. }
  61. $data = file_get_contents($key_path);
  62. echo $data;
  63. }
  64. //ts分片
  65. public function ts($uri=''){
  66. if(Mu_Tsext == ''){
  67. $tspath = sys_auth(str_replace('.ts', '', $uri),1);
  68. }else{
  69. $tspath = sys_auth(str_replace('.'.Mu_Tsext, '', $uri),1);
  70. }
  71. if(empty($tspath)){
  72. Header("HTTP/1.1 404 Not Found");
  73. exit('File address error');
  74. }
  75. $Mu_Path = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  76. $ts_path = $Mu_Path.$tspath;
  77. if(!file_exists($ts_path)){
  78. Header("HTTP/1.1 404 Not Found");
  79. exit('404');
  80. }
  81. $filesize = sprintf("%u", filesize($ts_path));
  82. header('Content-type: video/mp2t');
  83. header('Content-length: '.$filesize);
  84. header('Content-disposition: attachment; filename='.$uri);
  85. $data = file_get_contents($ts_path);
  86. echo $data;
  87. }
  88. //图片
  89. public function pic($uri=''){
  90. $picpath = sys_auth(str_replace(array('.jpg','.gif'), '', $uri),1);
  91. if(empty($picpath)){
  92. Header("HTTP/1.1 404 Not Found");
  93. exit('File address error');
  94. }
  95. $Mu_Path = substr(Mu_Path,0,2) == './' ? FCPATH.substr(Mu_Path,2) : Mu_Path;
  96. $pic_path = $Mu_Path.$picpath;
  97. if(!file_exists($pic_path)){
  98. Header("HTTP/1.1 404 Not Found");
  99. exit('404');
  100. }
  101. $image = file_get_contents($pic_path);
  102. if(strpos($uri, '.jpg')){
  103. header('Content-type: image/jpg');
  104. }else{
  105. header('Content-type: image/gif');
  106. }
  107. echo $image;
  108. }
  109. }