首页 > 解决方案 > 'base64_encode' 是否适用于 php 中的视频?

问题描述

我有一个女贞表格在线提供课程,用户可以在付款后注册并在线观看视频课程,并且我需要使用此功能制作视频而不显示原始 URL 源:

// Get video view tag
function GetVideoViewTag(&$fld, $val, $tooltip = false)
{
    global $Page;
    if (!EmptyString($val)) {
        $val = $fld->htmlDecode($val);
        if ($fld->DataType == DATATYPE_BLOB) {
            $wrknames = [$val];
            $wrkfiles = [$val];
        } elseif ($fld->UploadMultiple) {
            $wrknames = explode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $val);
            $wrkfiles = explode(Config("MULTIPLE_UPLOAD_SEPARATOR"), $fld->htmlDecode($fld->Upload->DbValue));
        } else {
            $wrknames = [$val];
            $wrkfiles = [$fld->htmlDecode($fld->Upload->DbValue)];
        }
        $multiple = (count($wrkfiles) > 1);
        $href = $tooltip ? "" : $fld->HrefValue;
        $isLazy = $tooltip ? false : IsLazy();
        $tags = "";
        $wrkcnt = 0;
        foreach ($wrkfiles as $wrkfile) {
            $tag = "";
            if (
                $Page && ($Page->TableType == "REPORT" &&
                ($Page->isExport("excel") && Config("USE_PHPEXCEL") ||
                $Page->isExport("word") && Config("USE_PHPWORD")) ||
                $Page->TableType != "REPORT" && ($Page->CustomExport == "pdf" || $Page->CustomExport == "email"))
            ) {
                $fn = GetFileTempImage($fld, $wrkfile);
            } else {
                $fn = GetFileUploadUrl($fld, $wrkfile, $fld->Resize);
            }
            $fi = base64_encode(file_get_contents('http://localhost/onlincu/files/'.$wrkfile));
                if ($href == "" && !$fld->UseColorbox) {
                    if ($fn != "") {
                        if ($isLazy) {
                           $tag = '<center><video style="width: 100%; height: 380px;" autoplay controls controlsList="nodownload"><source src="data:video/mp4;base64,'.$fi.'" type="video/mp4" data-src="' . $fn . '"' . $fld->viewAttributes() . '></video></center>';
                         } else {
                           $tag = '<center><video style="width: 100%; height: 380px;" autoplay controls controlsList="nodownload"><source src="data:video/mp4;base64,'.$fi.'" type="video/mp4" data-src="' . $fn . '"' . $fld->viewAttributes() . '></video></center>';
                         }
                    }
                } else {
                    if ($fld->UploadMultiple && ContainsString($href, '%u')) {
                        $fld->HrefValue = str_replace('%u', GetFileUploadUrl($fld, $wrkfile), $href);
                    }
                    if ($fn != "") {
                        if ($isLazy) {
                            $tag = '<center><video style="width: 100%; height: 380px;" autoplay controls controlsList="nodownload"><source src="data:video/mp4;base64,'.$fi.'" type="video/mp4" data-src="' . $fn . '"' . $fld->viewAttributes() . '></video></center>';
                        } else {
                            $tag = '<center><video style="width: 100%; height: 380px;" autoplay controls controlsList="nodownload"><source src="data:video/mp4;base64,'.$fi.'" type="video/mp4" data-src="' . $fn . '"' . $fld->viewAttributes() . '></video></center>';
                         }
                    }
                }
            if ($tag != "") {
                if ($multiple) {
                    $tags .= '<div class="p-1">' . $tag . '</div>';
                } else {
                    $tags .= $tag;
                }
            }
            $wrkcnt += 1;
        }
        if ($multiple && $tags != "") {
            $tags = '<div class="d-flex flex-row">' . $tags . '</div>';
        }
        return $tags;
    }
    return "";
}

现在我的功能工作得更好,直到现在,但我已经读到对视频使用'base64_encode'可能会损坏原始视频,那么如果这是真的,对于这种情况有什么更好的解决方案?

我感谢您的帮助 。

标签: php

解决方案


我找到了一个解决方案,它对我有用,但我想知道这是否是一个好的安全解决方案?所以我这样做而不是使用'base64_encode'来加密我的视频URL源,我试图在下面添加这个类:

<?php
//VideoStream.php

class VideoStream
{
    private $path = "";
    private $stream = "";
    private $iv = "";
    private $buffer = 102400;
    private $start  = -1;
    private $end    = -1;
    private $size   = 0;
 
    function __construct($filePath) 
    {
        $this->$iv = str_repeat('z', 16);
        $this->path = openssl_decrypt($filePath, "aes128", session_id(),0,$this->$iv);
    }
     
    /**
     * Open stream
     */
    private function open()
    {
        if (!($this->stream = fopen($this->path, 'rb'))) {
            die('Could not open stream for reading');
        }
         
    }
     
    /**
     * Set proper header to serve the video content
     */
    private function setHeader()
    {
        ob_get_clean();
        header("Content-Type: video/mp4");        
        header("Cache-Control: max-age=2592000, private");
        header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
        header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
        $this->start = 0;
        $this->size  = filesize($this->path);
        $this->end   = $this->size - 1;
        header("Accept-Ranges: 0-".$this->end);
         
        if (isset($_SERVER['HTTP_RANGE'])) {
  
            $c_start = $this->start;
            $c_end = $this->end;
 
            list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
            if (strpos($range, ',') !== false) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header("Content-Range: bytes $this->start-$this->end/$this->size");
                exit;
            }
            if ($range == '-') {
                $c_start = $this->size - substr($range, 1);
            }else{
                $range = explode('-', $range);
                $c_start = $range[0];
                 
                $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
            }
            $c_end = ($c_end > $this->end) ? $this->end : $c_end;
            if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header("Content-Range: bytes $this->start-$this->end/$this->size");
                exit;
            }
            $this->start = $c_start;
            $this->end = $c_end;
            $length = $this->end - $this->start + 1;
            fseek($this->stream, $this->start);
            header('HTTP/1.1 206 Partial Content');
            header("Content-Length: ".$length);
            header("Content-Range: bytes $this->start-$this->end/".$this->size);
        }
        else
        {
            header("Content-Length: ".$this->size);
        }  
         
    }
    
    /**
     * close curretly opened stream
     */
    private function end()
    {
        fclose($this->stream);
        exit;
    }
     
    /**
     * perform the streaming of calculated range
     */
    private function stream()
    {
        $i = $this->start;
        set_time_limit(0);
        while(!feof($this->stream) && $i <= $this->end) {
            $bytesToRead = $this->buffer;
            if(($i+$bytesToRead) > $this->end) {
                $bytesToRead = $this->end - $i + 1;
            }
            $data = fread($this->stream, $bytesToRead);
            echo $data;
            flush();
            $i += $bytesToRead;
        }
    }
     
    /**
     * Start streaming video content
     */
    function start()
    {
        $this->open();
        $this->setHeader();
        $this->stream();
        $this->end();
    }
}

还有我的 response_video.php :

<?php
//response_video.php

header("location: /onlincu/");
session_start();
// Start the session
if(isset($_GET["video_id"])&& isset($_GET["token"])){
       $token_id = $_GET["token"];
       $Vid_id = $_GET["video_id"];
       $iv = str_repeat('z', 16);
       $token = openssl_decrypt($Vid_id, "aes128", $token_id,0,$iv);
       $path = openssl_encrypt("files/".$token, "aes128", session_id(),0,$iv);
       require_once'VideoStream.php';
       $stream = new VideoStream($path);
       $stream->start();
}
?>

最后我打电话给我的 response_video.php :

<?php 
//index.php
$TokenValue = 'my-token-value';
$myVideo = $Page->videofile->getViewValue() //my-Video-name-value
$iv = str_repeat('z', 16);
$path=openssl_encrypt($myVideo, "aes128", $TokenValue,0,$iv);
?>
<video oncontextmenu="return false;" id="myVideo" width="100%" height="550px" autoplay controls controlsList="nodownload">
</video>
<script> 
var vid = document.getElementById("myVideo"); 
vid.src='response_video.php?video_id=<?= $path?>&token=<?= $TokenValue?>';
vid.type = 'video/mp4';
</script>    

现在我的视频 src 看起来像: src"onlincu/response_video.php?video_id=AvpAkmjPf16HapWcRKDrdhRZWFeZTBox2LDHA3zeFnk="


推荐阅读