mirror of
https://github.com/PhyreApps/PhyrePanel.git
synced 2024-11-22 15:40:25 +00:00
48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
class ShellApi
|
|
{
|
|
public static function safeDelete($pathOrFile, $whiteListedPaths = [])
|
|
{
|
|
if (empty($whiteListedPaths)) {
|
|
throw new \Exception('Whitelist paths cannot be empty');
|
|
}
|
|
|
|
$canIDeleteFile = false;
|
|
foreach ($whiteListedPaths as $whiteListedPath) {
|
|
if (Str::of($pathOrFile)->startsWith($whiteListedPath)) {
|
|
$canIDeleteFile = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$canIDeleteFile) {
|
|
throw new \Exception('Cannot delete this path:' . $pathOrFile . '. Allowed paths are:' . implode(',', $whiteListedPaths));
|
|
}
|
|
|
|
$exec = shell_exec('rm -rf ' . $pathOrFile);
|
|
|
|
return $exec;
|
|
}
|
|
|
|
public static function exec($command, $argsArray = [])
|
|
{
|
|
$args = '';
|
|
if (! empty($argsArray)) {
|
|
foreach ($argsArray as $arg) {
|
|
$args .= escapeshellarg($arg).' ';
|
|
}
|
|
}
|
|
|
|
$fullCommand = $command.' '.$args;
|
|
|
|
$execOutput = shell_exec($fullCommand);
|
|
|
|
return $execOutput;
|
|
}
|
|
|
|
}
|