1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-05 02:39:46 +02:00
This commit is contained in:
Daniel Neto 2024-12-03 14:56:56 -03:00
parent 51cb12d2ee
commit f38a9536da
3 changed files with 151 additions and 123 deletions

View file

@ -525,6 +525,25 @@ function getFFMPEGRemoteLog($keyword)
}
}
function stopFFMPEGRemote($keyword)
{
$obj = AVideoPlugin::getDataObjectIfEnabled('API');
if(!empty($obj) && !empty($obj->standAloneFFMPEG)){
$url = "{$obj->standAloneFFMPEG}";
$codeToExec = array('stop'=>1, 'keyword'=>$keyword, 'time'=>time());
$codeToExecEncrypted = encryptString(json_encode($codeToExec));
$url = addQueryStringParameter($url, 'APISecret', $obj->APISecret);
$url = addQueryStringParameter($url, 'codeToExecEncrypted', $codeToExecEncrypted);
_error_log("execFFMPEGAsyncOrRemote: URL $url");
return json_decode(url_get_contents($url));
}else{
return false;
}
}
// Function to find the process by keyword using the pid file
function findProcess($keyword)
{

View file

@ -1,4 +1,5 @@
<?php
/**
* FFMPEG Command Execution Script with API Secret Validation
* -----------------------------------------------------------
@ -87,25 +88,25 @@
* Replace `https://yourSite.com/` with your actual website URL.
*/
$global_timeLimit = 300;
$global_timeLimit = 300;
ini_set("memory_limit", -1);
ini_set('default_socket_timeout', $global_timeLimit);
set_time_limit($global_timeLimit);
ini_set('max_execution_time', $global_timeLimit);
ini_set("memory_limit", "-1");
ini_set("memory_limit", -1);
ini_set('default_socket_timeout', $global_timeLimit);
set_time_limit($global_timeLimit);
ini_set('max_execution_time', $global_timeLimit);
ini_set("memory_limit", "-1");
header('Content-Type: application/json');
header('Content-Type: application/json');
require_once __DIR__ . "/../../../objects/functionsStandAlone.php";
require_once __DIR__ . "/../../../objects/functionsStandAlone.php";
if (empty($streamerURL)) {
if (empty($streamerURL)) {
echo json_encode(['error' => true, 'message' => 'streamerURL not defined']);
exit;
}
}
function _decryptString($string)
{
function _decryptString($string)
{
global $global;
$url = "{$global['webSiteRootURL']}plugin/API/get.json.php?APIName=decryptString&string={$string}";
@ -120,10 +121,11 @@
}
return $json2;
return false;
}
}
// Function to safely get inputs from either command line or request
function getInput($key, $default = '') {
// Function to safely get inputs from either command line or request
function getInput($key, $default = '')
{
global $argv;
// Check if running from command line or HTTP request
@ -138,10 +140,11 @@
}
return $default;
}
}
// Validate and sanitize the ffmpegCommand
function sanitizeFFmpegCommand($command) {
// Validate and sanitize the ffmpegCommand
function sanitizeFFmpegCommand($command)
{
$allowedPrefixes = ['ffmpeg', '/usr/bin/ffmpeg', '/bin/ffmpeg'];
// Remove dangerous characters
@ -165,21 +168,21 @@
}
// Fetch and sanitize inputs
$codeToExecEncrypted = getInput('codeToExecEncrypted', '');
$codeToExec = _decryptString($codeToExecEncrypted);
// Fetch and sanitize inputs
$codeToExecEncrypted = getInput('codeToExecEncrypted', '');
$codeToExec = _decryptString($codeToExecEncrypted);
if (empty($codeToExec)) {
if (empty($codeToExec)) {
die('Invalid Request');
}
}
$ffmpegCommand = sanitizeFFmpegCommand($codeToExec->ffmpegCommand);
$keyword = preg_replace('/[^a-zA-Z0-9_-]/', '', $codeToExec->keyword);
$ffmpegCommand = sanitizeFFmpegCommand($codeToExec->ffmpegCommand);
$keyword = preg_replace('/[^a-zA-Z0-9_-]/', '', $codeToExec->keyword);
// Kill processes associated with the keyword
if (!empty($keyword)) {
// Kill processes associated with the keyword
if (!empty($keyword)) {
killProcessFromKeyword($keyword);
}
}
// Get the system's temporary directory
$tempDir = "{$global['systemRootPath']}videos/ffmpegLogs/";
@ -191,23 +194,33 @@ $tempDir = rtrim($tempDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
// Create a unique log file path
$logFile = "{$tempDir}ffmpeg_{$keyword}.log";
$time = time();
$modified = @filemtime($logFile);
$secondsAgo = $time - $obj->modified;
$isActive = $secondsAgo < 10;
if (!empty($codeToExec->log)) {
if (!empty($codeToExec->log)) {
$time = time();
$modified = @filemtime($logFile);
$secondsAgo = $time - $obj->modified;
$isActive = $secondsAgo < 10;
echo json_encode([
'error' => !file_exists($logFile),
'msg' => '',
'logFile' => $logFile,
'time' =>$time,
'modified' =>$modified,
'secondsAgo' =>$secondsAgo,
'isActive' =>$isActive,
'time' => $time,
'modified' => $modified,
'secondsAgo' => $secondsAgo,
'isActive' => $isActive,
]);
exit;
}else
} else if (!empty($codeToExec->stop) && !empty($keyword)) {
$cmd = "pkill -f 'ffmpeg.*$keyword'";
echo json_encode([
'error' => !file_exists($logFile),
'msg' => '',
'logFile' => $logFile,
'kill' => exec("pkill -f 'ffmpeg.*$keyword'"),
'keyword' => $keyword,
'unlink' => unlink($logFile),
]);
exit;
} else
// Validate that ffmpegCommand is not empty after sanitization
if (empty($ffmpegCommand)) {
echo json_encode([
@ -219,13 +232,13 @@ $isActive = $secondsAgo < 10;
}
// Redirect all output to the log file
$ffmpegCommand .= " > {$logFile} 2>&1";
// Redirect all output to the log file
$ffmpegCommand .= " > {$logFile} 2>&1";
// Debug output (optional)
error_log("Constructed FFMPEG Command [$keyword]: $ffmpegCommand");
// Debug output (optional)
error_log("Constructed FFMPEG Command [$keyword]: $ffmpegCommand");
try {
try {
$pid = execAsync($ffmpegCommand, $keyword);
echo json_encode([
'error' => false,
@ -234,13 +247,12 @@ $isActive = $secondsAgo < 10;
'pid' => $pid,
'logFile' => $logFile,
]);
} catch (Exception $e) {
} catch (Exception $e) {
echo json_encode([
'error' => true,
'msg' => 'Failed to execute command',
'errorMsg' => $e->getMessage(),
'logFile' => $logFile,
]);
}
exit;
}
exit;

View file

@ -1,10 +1,7 @@
<?php
includeConfigLog(__LINE__, basename(__FILE__));
require_once $global['systemRootPath'] . 'locale/function.php';
includeConfigLog(__LINE__, basename(__FILE__));
require_once $global['systemRootPath'] . 'objects/plugin.php';
includeConfigLog(__LINE__, basename(__FILE__));
abstract class PluginAbstract {