| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd/admin/config/ |
Upload File : |
<?php
// Aktifkan error reporting hanya untuk development
if ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_NAME'] == '127.0.0.1') {
error_reporting(E_ALL);
ini_set('display_errors', 1);
} else {
error_reporting(0);
ini_set('display_errors', 0);
}
// Security Headers
function setSecurityHeaders() {
// Proteksi XSS
header('X-XSS-Protection: 1; mode=block');
// Mencegah MIME-type sniffing
header('X-Content-Type-Options: nosniff');
// Proteksi Clickjacking
header('X-Frame-Options: SAMEORIGIN');
// Referrer Policy
header('Referrer-Policy: strict-origin-when-cross-origin');
// Content Security Policy (CSP)
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://unpkg.com https://code.jquery.com https://cdn.ckeditor.com; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self';");
}
// CSRF Protection
function generateCSRFToken() {
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function validateCSRFToken($token) {
if (!isset($_SESSION['csrf_token']) || $token !== $_SESSION['csrf_token']) {
die('Invalid CSRF token');
}
return true;
}
// Rate Limiting
function checkRateLimit($action, $maxAttempts = 5, $timeWindow = 300) {
$ip = $_SERVER['REMOTE_ADDR'];
$key = "rate_limit_{$action}_{$ip}";
if (!isset($_SESSION[$key])) {
$_SESSION[$key] = [
'attempts' => 1,
'first_attempt' => time()
];
return true;
}
$data = $_SESSION[$key];
// Reset jika waktu sudah lewat
if (time() - $data['first_attempt'] > $timeWindow) {
$_SESSION[$key] = [
'attempts' => 1,
'first_attempt' => time()
];
return true;
}
// Cek apakah melebihi batas
if ($data['attempts'] >= $maxAttempts) {
return false;
}
// Increment attempts
$_SESSION[$key]['attempts']++;
return true;
}
// Input Validation
function validateInput($data, $type = 'string', $options = []) {
$data = trim($data);
$data = stripslashes($data);
switch ($type) {
case 'email':
return filter_var($data, FILTER_VALIDATE_EMAIL);
case 'int':
return filter_var($data, FILTER_VALIDATE_INT);
case 'url':
return filter_var($data, FILTER_VALIDATE_URL);
case 'alphanumeric':
return preg_match('/^[a-zA-Z0-9\s]+$/', $data) ? $data : false;
case 'name':
return preg_match('/^[a-zA-Z\s\.\']+$/', $data) ? $data : false;
case 'phone':
return preg_match('/^[0-9\+\-\s]+$/', $data) ? $data : false;
case 'string':
default:
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
}
// SQL Injection Prevention
function sanitizeSQL($conn, $input) {
return mysqli_real_escape_string($conn, $input);
}
// Prepared Statement Helper
function executeQuery($conn, $sql, $params = [], $types = '') {
$stmt = $conn->prepare($sql);
if (!$stmt) {
error_log("Prepare failed: " . $conn->error);
return false;
}
if (!empty($params)) {
$stmt->bind_param($types, ...$params);
}
if (!$stmt->execute()) {
error_log("Execute failed: " . $stmt->error);
return false;
}
return $stmt;
}
// XSS Prevention
function escapeOutput($data) {
if (is_array($data)) {
return array_map('escapeOutput', $data);
}
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
// Honeypot untuk Anti Spam
function generateHoneypot() {
$honeypot_name = 'hp_' . substr(md5(rand()), 0, 10);
$_SESSION['honeypot_name'] = $honeypot_name;
$html = '<div style="display:none !important; position:absolute !important; left:-9999px !important;">';
$html .= '<input type="text" name="' . $honeypot_name . '" autocomplete="off" tabindex="-1">';
$html .= '</div>';
return $html;
}
function validateHoneypot() {
$honeypot_name = $_SESSION['honeypot_name'] ?? '';
if ($honeypot_name && !empty($_POST[$honeypot_name])) {
return false; // Bot terdeteksi
}
return true;
}
// Time-based form submission (Anti bot)
function checkFormTime($minTime = 3) {
if (!isset($_SESSION['form_start_time'])) {
return false;
}
$elapsed = time() - $_SESSION['form_start_time'];
return $elapsed >= $minTime;
}
// Captcha sederhana
function generateSimpleCaptcha() {
$num1 = rand(1, 9);
$num2 = rand(1, 9);
$operator = ['+', '-', '*'][rand(0, 2)];
switch ($operator) {
case '+': $result = $num1 + $num2; break;
case '-': $result = $num1 - $num2; break;
case '*': $result = $num1 * $num2; break;
}
$_SESSION['captcha_result'] = $result;
return "$num1 $operator $num2 = ?";
}
function validateCaptcha($answer) {
return isset($_SESSION['captcha_result']) &&
(int)$answer === (int)$_SESSION['captcha_result'];
}
// Logging untuk aktivitas mencurigakan
function logSuspiciousActivity($type, $details) {
$logFile = '../logs/suspicious_activity.log';
$timestamp = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$logEntry = "[$timestamp] IP: $ip | Type: $type | Details: $details | UA: $userAgent\n";
if (!file_exists('../logs')) {
mkdir('../logs', 0755, true);
}
file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
}
?>