access.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. session_start();
  3. if (isset($_POST["video"])) {
  4. include("../connectSQL_Component.php");
  5. $sql = "SELECT * FROM [BIMComponents].[dbo].[Video_Table] WHERE [id] = '" . $_POST["video"] . "'";
  6. $fetchResult = sqlsrv_query($conn, $sql);
  7. $file_name = "";
  8. $file_path = "";
  9. $title = "";
  10. while ($row = sqlsrv_fetch_array($fetchResult)) {
  11. $file_name = $row["file_name"];
  12. $file_path = $row["file_path"];
  13. $title = $row["title"];
  14. }
  15. $token = "../../." . $file_path;
  16. sqlsrv_close($conn);
  17. if (file_exists($token)) {
  18. $stream = new VideoStream($token);
  19. $stream->start();
  20. } else {
  21. echo "Error: File Does not exists";
  22. }
  23. }
  24. class VideoStream
  25. {
  26. private $path = "";
  27. private $stream = "";
  28. private $buffer = 102400;
  29. private $start = -1;
  30. private $end = -1;
  31. private $size = 0;
  32. function __construct($filePath)
  33. {
  34. $this->path = $filePath;
  35. }
  36. /**
  37. * Open stream
  38. */
  39. private function open()
  40. {
  41. if (!($this->stream = fopen($this->path, 'rb'))) {
  42. die('Could not open stream for reading');
  43. }
  44. }
  45. /**
  46. * Set proper header to serve the video content
  47. */
  48. private function setHeader()
  49. {
  50. ob_get_clean();
  51. header("Content-Type: video/mp4");
  52. header("Cache-Control: max-age=2592000, public");
  53. header("Expires: " . gmdate('D, d M Y H:i:s', time() + 2592000) . ' GMT');
  54. header("Last-Modified: " . gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT');
  55. $this->start = 0;
  56. $this->size = filesize($this->path);
  57. $this->end = $this->size - 1;
  58. header("Accept-Ranges: 0-" . $this->end);
  59. if (isset($_SERVER['HTTP_RANGE'])) {
  60. $c_start = $this->start;
  61. $c_end = $this->end;
  62. list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  63. if (strpos($range, ',') !== false) {
  64. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  65. header("Content-Range: bytes $this->start-$this->end/$this->size");
  66. exit;
  67. }
  68. if ($range == '-') {
  69. $c_start = $this->size - substr($range, 1);
  70. } else {
  71. $range = explode('-', $range);
  72. $c_start = $range[0];
  73. $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
  74. }
  75. $c_end = ($c_end > $this->end) ? $this->end : $c_end;
  76. if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
  77. header('HTTP/1.1 416 Requested Range Not Satisfiable');
  78. header("Content-Range: bytes $this->start-$this->end/$this->size");
  79. exit;
  80. }
  81. $this->start = $c_start;
  82. $this->end = $c_end;
  83. $length = $this->end - $this->start + 1;
  84. fseek($this->stream, $this->start);
  85. header('HTTP/1.1 206 Partial Content');
  86. header("Content-Length: " . $length);
  87. header("Content-Range: bytes $this->start-$this->end/" . $this->size);
  88. } else {
  89. header("Content-Length: " . $this->size);
  90. }
  91. }
  92. /**
  93. * close curretly opened stream
  94. */
  95. private function end()
  96. {
  97. fclose($this->stream);
  98. exit;
  99. }
  100. /**
  101. * perform the streaming of calculated range
  102. */
  103. private function stream()
  104. {
  105. $i = $this->start;
  106. set_time_limit(0);
  107. while (!feof($this->stream) && $i <= $this->end) {
  108. $bytesToRead = $this->buffer;
  109. if (($i + $bytesToRead) > $this->end) {
  110. $bytesToRead = $this->end - $i + 1;
  111. }
  112. $data = fread($this->stream, $bytesToRead);
  113. echo $data;
  114. flush();
  115. $i += $bytesToRead;
  116. }
  117. }
  118. /**
  119. * Start streaming video content
  120. */
  121. function start()
  122. {
  123. $this->open();
  124. $this->setHeader();
  125. $this->stream();
  126. $this->end();
  127. }
  128. }