Is 'base64_encode' working good with videos in php?
Get the solution ↓↓↓I have a privet forms to give courses online that users can register and watching the video course Online after payment , And I need to produce the video without showing the original URL source by using this function :
// 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 "";
}
Now my function working Good even better until now , But I have read that using 'base64_encode' for videos maybe damage the original video , Well if that true What the better solution for this case ?
I appreciate your help .
Answer
Solution:
I have found a solution to this it's worked for me but I wonder if this a good secure solution ? so what I do that instead of using 'base64_encode' to encrypt my video URL source ,I tried to add this class bellow :
<?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();
}
}
And my 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();
}
?>
Finally I call my 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>
Now my video src look like : src"onlincu/response_video.php?video_id=AvpAkmjPf16HapWcRKDrdhRZWFeZTBox2LDHA3zeFnk="
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.