ringkasan dan summary petani

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-05-04 15:07:56 +07:00
co-authored by Copilot
parent 61f72e4352
commit 69d09e74e2
17 changed files with 933 additions and 38 deletions
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\DB;
class ValidSql implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$sql = trim($value);
if (empty($sql)) {
$fail('Query tidak boleh kosong.');
return;
}
if (!str_starts_with(strtolower($sql), 'select')) {
$fail('Query harus diawali dengan SELECT.');
return;
}
$forbiddenCommands = ['insert', 'update', 'delete', 'drop', 'alter', 'create', 'truncate', 'replace', 'grant', 'revoke'];
foreach ($forbiddenCommands as $command) {
if (stripos($sql, $command) !== false) {
$fail("Query tidak boleh mengandung perintah '$command'.");
return;
}
}
if (strpos($sql, ';') !== false) {
$fail('Query tidak boleh mengandung multiple statements.');
return;
}
$pattern = '/^select\s+.+\s+from\s+\w+/i';
if (!preg_match($pattern, $sql)) {
$fail('Query tidak memiliki format SQL yang valid.');
return;
}
// try {
// DB::beginTransaction();
// $wrapped = "SELECT * FROM ({$sql}) as x LIMIT 1";
// DB::select($wrapped);
// DB::rollBack();
// } catch (\Throwable $e) {
// DB::rollBack();
// $fail("Query tidak valid: " . $e->getMessage());
// }
}
}