Skip to content

Commit

Permalink
Replace shell_exec() with Helper::shellExec()
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Jan 5, 2024
1 parent 2bfdc99 commit 8a52a85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/CleanTmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct()
*/
public function handle()
{
shell_exec('find '.\Helper::getTempDir().' -mtime +7 -type f -name '.\Helper::getTempFilePrefix().'* -exec rm -r -f {} \;');
\Helper::shellExec('find '.\Helper::getTempDir().' -mtime +7 -type f -name '.\Helper::getTempFilePrefix().'* -exec rm -r -f {} \;');

$this->comment("Done");
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/SystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function status(Request $request)
// Check if cache files are writable.
$non_writable_cache_file = '';
if (function_exists('shell_exec')) {
$non_writable_cache_file = shell_exec('find '.base_path('storage/framework/cache/data/').' -type f | xargs -I {} sh -c \'[ ! -w "{}" ] && echo {}\' 2>&1 | head -n 1');
$non_writable_cache_file = \Helper::shellExec('find '.base_path('storage/framework/cache/data/').' -type f | xargs -I {} sh -c \'[ ! -w "{}" ] && echo {}\' 2>&1 | head -n 1');
$non_writable_cache_file = trim($non_writable_cache_file ?? '');
// Leave only one line (in case head -n 1 does not work)
$non_writable_cache_file = preg_replace("#[\r\n].+#m", '', $non_writable_cache_file);
Expand Down Expand Up @@ -97,7 +97,7 @@ public function status(Request $request)
$running_commands = 0;

try {
$processes = preg_split("/[\r\n]/", shell_exec("ps aux | grep '{$command_identifier}'"));
$processes = preg_split("/[\r\n]/", \Helper::shellExec("ps aux | grep '{$command_identifier}'"));
$pids = [];
foreach ($processes as $process) {
$process = trim($process);
Expand Down
21 changes: 17 additions & 4 deletions app/Misc/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ public static function getRunningProcesses($search = '')
$pids = [];

try {
$processes = preg_split("/[\r\n]/", shell_exec("ps aux | grep '".$search."'"));
$processes = preg_split("/[\r\n]/", \Helper::shellExec("ps aux | grep '".$search."'"));
foreach ($processes as $process) {
$process = trim($process);
preg_match("/^[\S]+\s+([\d]+)\s+/", $process, $m);
Expand Down Expand Up @@ -1921,7 +1921,7 @@ public static function checkRequiredExtensions()
if (self::isConsole() || !function_exists('shell_exec')) {
$pcntl_enabled = extension_loaded('pcntl');
} else {
$pcntl_enabled = preg_match("/enable/m", shell_exec("php -i | grep pcntl") ?? '');
$pcntl_enabled = preg_match("/enable/m", \Helper::shellExec("php -i | grep pcntl") ?? '');
}
$php_extensions['pcntl (console PHP)'] = $pcntl_enabled;

Expand All @@ -1935,8 +1935,8 @@ public static function checkRequiredFunctions()
'proc_open (PHP)' => function_exists('proc_open'),
'fpassthru (PHP)' => function_exists('fpassthru'),
'symlink (PHP)' => function_exists('symlink'),
'pcntl_signal (console PHP)' => function_exists('shell_exec') ? (int)shell_exec('php -r "echo (int)function_exists(\'pcntl_signal\');"') : false,
'ps (shell)' => function_exists('shell_exec') ? shell_exec('ps') : false,
'pcntl_signal (console PHP)' => function_exists('shell_exec') ? (int)\Helper::shellExec('php -r "echo (int)function_exists(\'pcntl_signal\');"') : false,
'ps (shell)' => function_exists('shell_exec') ? \Helper::shellExec('ps') : false,
];
}

Expand Down Expand Up @@ -2120,4 +2120,17 @@ public static function sanitizeDatepickerDatetime($datetime)
{
return str_replace('T', ' ', $datetime);
}

// To catch possible exception:
// shell_exec(): Unable to execute
public static function shellExec($command)
{
try {
return shell_exec($command);
} catch (\Exception $e) {
self::logException($e, '\Helper::shellExec() - ');
}

return '';
}
}

0 comments on commit 8a52a85

Please sign in to comment.