@TerritoryOfPeace
Изучаю php

Почему нельзя перемотать видео когда используешь php роутер?

Есть роутер:

<?php

session_start();



function get($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'GET' ){ route($route, $path_to_include); }
}
function post($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ route($route, $path_to_include); }
}
function put($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'PUT' ){ route($route, $path_to_include); }
}
function patch($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'PATCH' ){ route($route, $path_to_include); }
}
function delete($route, $path_to_include){
  if( $_SERVER['REQUEST_METHOD'] == 'DELETE' ){ route($route, $path_to_include); }
}
function any($route, $path_to_include){ route($route, $path_to_include); }
function route($route, $path_to_include){
  $ROOT = $_SERVER['DOCUMENT_ROOT'];
  if($route == "/404"){
    include_once("$ROOT/$path_to_include");
    exit();
  }
  $request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
  $request_url = rtrim($request_url, '/');
  $request_url = strtok($request_url, '?');

  // Добавляем проверку наличия видеофайла в корневом каталоге
  $video_path = "$ROOT/$request_url";
 if (file_exists($video_path) && is_file($video_path)) {
    $mime_type = mime_content_type($video_path);
    if ($mime_type == "video/webm" || $mime_type == "video/mp4") {
      header("Content-Type: $mime_type");
      readfile($video_path);
      exit();
    }
  }

  $route_parts = explode('/', $route);
  $request_url_parts = explode('/', $request_url);
  array_shift($route_parts);
  array_shift($request_url_parts);
  if( $route_parts[0] == '' && count($request_url_parts) == 0 ){
    include_once("$ROOT/$path_to_include");
    exit();
  }
  if( count($route_parts) != count($request_url_parts) ){ return; }
  $parameters = [];
  for( $__i__ = 0; $__i__ < count($route_parts); $__i__++ ){
    $route_part = $route_parts[$__i__];
    if( preg_match("/^[$]/", $route_part) ){
      $route_part = ltrim($route_part, '$');
      array_push($parameters, $request_url_parts[$__i__]);
      $$route_part=$request_url_parts[$__i__];
    }
    else if( $route_parts[$__i__] != $request_url_parts[$__i__] ){
      return;
    }
  }
  include_once("$ROOT/$path_to_include");
  exit();
}

function out($text){echo htmlspecialchars($text);}
function set_csrf(){
  if( ! isset($_SESSION["csrf"]) ){ $_SESSION["csrf"] = bin2hex(random_bytes(50)); }
  echo '<input type="hidden" name="csrf" value="'.$_SESSION["csrf"].'">';
}
function is_csrf_valid(){
  if( ! isset($_SESSION['csrf']) || ! isset($_POST['csrf'])){ return false; }
  if( $_SESSION['csrf'] != $_POST['csrf']){ return false; }
  return true;
}


Почему из-за него я не могу перематывать видео в самом простом HTML плеере? Без роутера на сервере все работает.
  • Вопрос задан
  • 109 просмотров
Решения вопроса 1
@TerritoryOfPeace Автор вопроса
Изучаю php
function route($route, $path_to_include){
$ROOT = $_SERVER['DOCUMENT_ROOT'];
if($route == "/404"){
include_once("$ROOT/$path_to_include");
exit();
}

$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
$request_url = rtrim($request_url, '/');
$request_url = strtok($request_url, '?');

// Проверяем наличие видеофайла в корневом каталоге
$video_path = "$ROOT/$request_url";
if (file_exists($video_path) && is_file($video_path)) {
$mime_type = mime_content_type($video_path);
if ($mime_type == "video/webm" || $mime_type == "video/mp4") {
header("Content-Type: $mime_type");
$filesize = filesize($video_path);
$offset = 0;
$length = $filesize;
if (isset($_SERVER['HTTP_RANGE'])) {
$partialContent = true;
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = ($matches[2] ?? $filesize - 1) - $offset + 1;
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length - 1) . '/' . $filesize);
} else {
$partialContent = false;
header('Content-Length: ' . $filesize);
}
header('Accept-Ranges: bytes');
$file = fopen($video_path, 'rb');
fseek($file, $offset);
while (!feof($file) && ($pos = ftell($file)) <= ($offset + $length - 1)) {
if ($pos + 1024 * 16 > $offset + $length) {
echo fread($file, $offset + $length - $pos);
} else {
echo fread($file, 1024 * 16);
}
flush();
}
fclose($file);
exit();
}
}

// Обработка остальных маршрутов
$route_parts = explode('/', $route);
$request_url_parts = explode('/', $request_url);
array_shift($route_parts);
array_shift($request_url_parts);
if( $route_parts[0] == '' && count($request_url_parts) == 0 ){
include_once("$ROOT/$path_to_include");
exit();
}
if( count($route_parts) != count($request_url_parts) ){ return; }
$parameters = [];
for( $__i__ = 0; $__i__ < count($route_parts); $__i__++ ){
$route_part = $route_parts[$__i__];
if( preg_match("/^[$]/", $route_part) ){
$route_part = ltrim($route_part, '$');
array_push($parameters, $request_url_parts[$__i__]);
$$route_part=$request_url_parts[$__i__];
}
else if( $route_parts[$__i__] != $request_url_parts[$__i__] ){
return;
}
}
include_once("$ROOT/$path_to_include");
exit();
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@humoured
Вы всё на свете найдёте в коробке с карандашами
Потому что перемотка видеофайла, как и возобновляемое скачивания, работает при помощи заголовка Content-Range, который функции readfile() безразличен.
Гляньте хотя бы вот это: https://gist.github.com/kosinix/4cf0d432638817888149
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы