PhyrePanel-mirror/web/app/ShellApi.php

49 lines
1.1 KiB
PHP
Raw Normal View History

2024-04-22 11:14:05 +00:00
<?php
namespace App;
2024-04-30 23:04:24 +00:00
use Illuminate\Support\Str;
2024-04-22 11:14:05 +00:00
class ShellApi
{
2024-04-30 23:04:24 +00:00
public static function safeDelete($pathOrFile, $whiteListedPaths = [])
2024-04-22 11:14:05 +00:00
{
2024-04-30 23:04:24 +00:00
if (empty($whiteListedPaths)) {
throw new \Exception('Whitelist paths cannot be empty');
}
2024-04-30 23:14:27 +00:00
$canIDeleteFile = false;
2024-04-30 23:04:24 +00:00
foreach ($whiteListedPaths as $whiteListedPath) {
2024-04-30 23:14:27 +00:00
if (Str::of($pathOrFile)->startsWith($whiteListedPath)) {
$canIDeleteFile = true;
break;
2024-04-30 23:04:24 +00:00
}
}
2024-04-30 23:14:27 +00:00
if (!$canIDeleteFile) {
throw new \Exception('Cannot delete this path:' . $pathOrFile . '. Allowed paths are:' . implode(',', $whiteListedPaths));
2024-04-22 11:14:05 +00:00
}
2024-04-30 22:55:07 +00:00
$exec = shell_exec('rm -rf ' . $pathOrFile);
2024-04-22 11:14:05 +00:00
2024-04-30 22:55:07 +00:00
return $exec;
2024-04-22 11:14:05 +00:00
}
2024-04-30 22:55:07 +00:00
public static function exec($command, $argsArray = [])
2024-04-22 11:14:05 +00:00
{
$args = '';
if (! empty($argsArray)) {
foreach ($argsArray as $arg) {
$args .= escapeshellarg($arg).' ';
}
}
2024-04-30 22:55:07 +00:00
$fullCommand = $command.' '.$args;
2024-04-22 11:14:05 +00:00
2024-04-30 22:55:07 +00:00
$execOutput = shell_exec($fullCommand);
2024-04-22 11:14:05 +00:00
return $execOutput;
}
2024-04-30 22:55:07 +00:00
2024-04-22 11:14:05 +00:00
}