Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Nonaktifkan pelaporan error untuk menjaga kebersihan output
- error_reporting(0);
- ini_set('display_errors', 0);
- /**
- * Kelas PasswordHash portabel untuk PHP.
- * Diadaptasi dari kelas phpass WordPress untuk penggunaan mandiri.
- */
- class PasswordHash {
- var $itoa64;
- var $iteration_count_log2;
- var $portable_hashes;
- var $random_state;
- function __construct($iteration_count_log2, $portable_hashes) {
- $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
- if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) {
- $iteration_count_log2 = 8;
- }
- $this->iteration_count_log2 = $iteration_count_log2;
- $this->portable_hashes = $portable_hashes;
- $this->random_state = microtime();
- if (function_exists('getmypid')) {
- $this->random_state .= getmypid();
- }
- }
- function get_random_bytes($count) {
- $output = '';
- if (is_readable('/dev/urandom') && ($fh = @fopen('/dev/urandom', 'rb'))) {
- $output = fread($fh, $count);
- fclose($fh);
- }
- if (strlen($output) < $count) {
- $output = '';
- for ($i = 0; $i < $count; $i += 16) {
- $this->random_state = md5(microtime() . $this->random_state);
- $output .= pack('H*', md5($this->random_state));
- }
- $output = substr($output, 0, $count);
- }
- return $output;
- }
- function encode64($input, $count) {
- $output = '';
- $i = 0;
- do {
- $value = ord($input[$i++]);
- $output .= $this->itoa64[$value & 0x3f];
- if ($i < $count) {
- $value |= ord($input[$i]) << 8;
- }
- $output .= $this->itoa64[($value >> 6) & 0x3f];
- if ($i++ >= $count) {
- break;
- }
- if ($i < $count) {
- $value |= ord($input[$i]) << 16;
- }
- $output .= $this->itoa64[($value >> 12) & 0x3f];
- if ($i++ >= $count) {
- break;
- }
- $output .= $this->itoa64[($value >> 18) & 0x3f];
- } while ($i < $count);
- return $output;
- }
- function gensalt_private($input) {
- $output = '$P$';
- $output .= $this->itoa64[min($this->iteration_count_log2 + 5, 30)];
- $output .= $this->encode64($input, 6);
- return $output;
- }
- function crypt_private($password, $setting) {
- $output = '*0';
- if (substr($setting, 0, 2) == $output) {
- $output = '*1';
- }
- if (substr($setting, 0, 3) != '$P$') {
- return $output;
- }
- $count_log2 = strpos($this->itoa64, $setting[3]);
- if ($count_log2 < 7 || $count_log2 > 30) {
- return $output;
- }
- $count = 1 << $count_log2;
- $salt = substr($setting, 4, 8);
- if (strlen($salt) != 8) {
- return $output;
- }
- $hash = md5($salt . $password, TRUE);
- do {
- $hash = md5($hash . $password, TRUE);
- } while (--$count);
- $output = substr($setting, 0, 12);
- $output .= $this->encode64($hash, 16);
- return $output;
- }
- function gensalt_extended($input) {
- $count_log2 = min($this->iteration_count_log2 + 8, 24);
- $count = (1 << $count_log2) - 1;
- $output = '_';
- $output .= $this->itoa64[$count & 0x3f];
- $output .= $this->itoa64[($count >> 6) & 0x3f];
- $output .= $this->itoa64[($count >> 12) & 0x3f];
- $output .= $this->itoa64[($count >> 18) & 0x3f];
- $output .= $this->encode64($input, 3);
- return $output;
- }
- function gensalt_blowfish($input) {
- $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- $output = '$2a$';
- $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
- $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
- $output .= '$';
- $i = 0;
- do {
- $c1 = ord($input[$i++]);
- $output .= $itoa64[$c1 >> 2];
- $c1 = ($c1 & 0x03) << 4;
- if ($i >= 16) {
- $output .= $itoa64[$c1];
- break;
- }
- $c2 = ord($input[$i++]);
- $c1 |= $c2 >> 4;
- $output .= $itoa64[$c1];
- $c1 = ($c2 & 0x0f) << 2;
- $c2 = ord($input[$i++]);
- $c1 |= $c2 >> 6;
- $output .= $itoa64[$c1];
- $output .= $itoa64[$c2 & 0x3f];
- } while (1);
- return $output;
- }
- function HashPassword($password) {
- $random = '';
- if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
- $random = $this->get_random_bytes(16);
- $hash = crypt($password, $this->gensalt_blowfish($random));
- if (strlen($hash) == 60) {
- return $hash;
- }
- }
- if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
- if (strlen($random) < 3) {
- $random = $this->get_random_bytes(3);
- }
- $hash = crypt($password, $this->gensalt_extended($random));
- if (strlen($hash) == 20) {
- return $hash;
- }
- }
- if (strlen($random) < 6) {
- $random = $this->get_random_bytes(6);
- }
- $hash = $this->crypt_private($password, $this->gensalt_private($random));
- if (strlen($hash) == 34) {
- return $hash;
- }
- return '*';
- }
- }
- $message = '';
- $message_type = ''; // 'success' or 'error'
- $db_config_message = '';
- $db_config_type = 'info';
- // --- Otomatis Deteksi Konfigurasi DB ---
- $db_host = $db_user = $db_pass = $db_name = null;
- $config_file_path = __DIR__ . '/wp-config.php';
- if (file_exists($config_file_path) && is_readable($config_file_path)) {
- $config_content = file_get_contents($config_file_path);
- function extract_db_config($define, $content) {
- if (preg_match("/define\(\s*['\"]" . $define . "['\"]\s*,\s*['\"]([^']*)['\"]\s*\)/", $content, $matches)) {
- return $matches[1];
- }
- return null;
- }
- $db_name = extract_db_config('DB_NAME', $config_content);
- $db_user = extract_db_config('DB_USER', $config_content);
- $db_pass = extract_db_config('DB_PASSWORD', $config_content);
- $db_host = extract_db_config('DB_HOST', $config_content);
- if ($db_name && $db_user && $db_host) {
- $db_config_message = "Konfigurasi database berhasil dideteksi dari wp-config.php.";
- $db_config_type = 'success';
- } else {
- $db_config_message = "Gagal mendeteksi semua detail database dari wp-config.php. Pastikan file tersebut ada, dapat dibaca, dan formatnya benar.";
- $db_config_type = 'error';
- }
- } else {
- $db_config_message = "File wp-config.php tidak ditemukan di direktori ini. Letakkan skrip ini di direktori root WordPress Anda (di mana wp-config.php berada).";
- $db_config_type = 'error';
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if ($db_config_type === 'error') {
- $message = $db_config_message;
- $message_type = 'error';
- } else {
- // Ambil dan bersihkan data dari form
- $admin_user = trim(preg_replace('/[^a-zA-Z0-9_]/', '', $_POST['admin_user']));
- $admin_pass = trim($_POST['admin_pass']);
- $admin_email = trim($_POST['admin_email']);
- // Validasi input
- if (empty($admin_user) || empty($admin_pass) || empty($admin_email)) {
- $message = 'Detail admin (username, password, email) wajib diisi.';
- $message_type = 'error';
- } elseif (!filter_var($admin_email, FILTER_VALIDATE_EMAIL)) {
- $message = 'Format email tidak valid.';
- $message_type = 'error';
- } else {
- $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
- if ($conn->connect_error) {
- $message = "Koneksi Gagal: " . $conn->connect_error;
- $message_type = 'error';
- } else {
- $prefix = '';
- $result = $conn->query("SHOW TABLES LIKE '%users'");
- if ($result && $result->num_rows > 0) {
- $row = $result->fetch_array();
- $users_table = $row[0];
- $prefix = substr($users_table, 0, strpos($users_table, 'users'));
- } else {
- $message = "Tidak dapat menemukan tabel 'users'. Pastikan detail database benar.";
- $message_type = 'error';
- }
- if (empty($message)) {
- $users_table_name = $prefix . 'users';
- $usermeta_table_name = $prefix . 'usermeta';
- $stmt_check = $conn->prepare("SELECT ID FROM $users_table_name WHERE user_login = ? OR user_email = ?");
- $stmt_check->bind_param('ss', $admin_user, $admin_email);
- $stmt_check->execute();
- $stmt_check->store_result();
- if ($stmt_check->num_rows > 0) {
- $message = "Username atau email sudah terdaftar.";
- $message_type = 'error';
- } else {
- $hasher = new PasswordHash(8, true);
- $hashed_password = $hasher->HashPassword($admin_pass);
- $stmt_users = $conn->prepare(
- "INSERT INTO $users_table_name (user_login, user_pass, user_nicename, user_email, user_registered, user_status, display_name) VALUES (?, ?, ?, ?, NOW(), 0, ?)"
- );
- $stmt_users->bind_param('sssss', $admin_user, $hashed_password, $admin_user, $admin_email, $admin_user);
- if ($stmt_users->execute()) {
- $new_user_id = $stmt_users->insert_id;
- $capabilities = 'a:1:{s:13:"administrator";b:1;}';
- $stmt_meta1 = $conn->prepare("INSERT INTO $usermeta_table_name (user_id, meta_key, meta_value) VALUES (?, ?, ?)");
- $meta_key_cap = $prefix . 'capabilities';
- $stmt_meta1->bind_param('iss', $new_user_id, $meta_key_cap, $capabilities);
- $stmt_meta1->execute();
- $stmt_meta1->close();
- $stmt_meta2 = $conn->prepare("INSERT INTO $usermeta_table_name (user_id, meta_key, meta_value) VALUES (?, ?, ?)");
- $meta_key_level = $prefix . 'user_level';
- $user_level = '10';
- $stmt_meta2->bind_param('iss', $new_user_id, $meta_key_level, $user_level);
- $stmt_meta2->execute();
- $stmt_meta2->close();
- $message = "Admin user '<strong>$admin_user</strong>' berhasil dibuat! Silakan login dan HAPUS FILE INI SEKARANG.";
- $message_type = 'success';
- } else {
- $message = "Gagal membuat user: " . $stmt_users->error;
- $message_type = 'error';
- }
- $stmt_users->close();
- }
- $stmt_check->close();
- }
- $conn->close();
- }
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="id">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>WordPress Admin Creator</title>
- <script src="https://cdn.tailwindcss.com"></script>
- <style>
- body {
- font-family: 'Inter', sans-serif;
- }
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
- </style>
- </head>
- <body class="bg-gray-100 flex items-center justify-center min-h-screen py-12">
- <div class="w-full max-w-lg mx-auto bg-white rounded-xl shadow-lg p-8 md:p-10">
- <div class="text-center mb-8">
- <h1 class="text-3xl font-bold text-gray-800">WordPress Admin Creator</h1>
- <p class="text-gray-500 mt-2">Buat user admin baru jika Anda kehilangan akses.</p>
- </div>
- <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded-md mb-6" role="alert">
- <p class="font-bold">Peringatan Keamanan!</p>
- <p>Setelah selesai, <strong class="underline">SEGERA HAPUS</strong> file ini dari server Anda untuk melindungi situs Anda.</p>
- </div>
- <?php if (!empty($message)): ?>
- <div class="p-4 mb-6 rounded-md <?php echo ($message_type === 'success') ? 'bg-green-100 border-l-4 border-green-500 text-green-700' : 'bg-red-100 border-l-4 border-red-500 text-red-700'; ?>">
- <p><?php echo $message; ?></p>
- </div>
- <?php endif; ?>
- <div class="mb-6">
- <h2 class="text-lg font-semibold text-gray-700 border-b pb-2 mb-4">Status Konfigurasi Database</h2>
- <?php
- $alert_class = 'bg-blue-100 border-blue-500 text-blue-700'; // Default
- if ($db_config_type === 'success') {
- $alert_class = 'bg-green-100 border-green-500 text-green-700';
- } elseif ($db_config_type === 'error') {
- $alert_class = 'bg-red-100 border-red-500 text-red-700';
- }
- ?>
- <div class="p-4 rounded-md border-l-4 <?php echo $alert_class; ?>">
- <p><?php echo $db_config_message; ?></p>
- <?php if ($db_config_type === 'success'): ?>
- <ul class="list-disc list-inside mt-2 text-sm">
- <li><strong>DB Host:</strong> <?php echo htmlspecialchars($db_host); ?></li>
- <li><strong>DB Name:</strong> <?php echo htmlspecialchars($db_name); ?></li>
- <li><strong>DB User:</strong> <?php echo htmlspecialchars($db_user); ?></li>
- </ul>
- <?php endif; ?>
- </div>
- </div>
- <form action="" method="POST" <?php if ($db_config_type === 'error') echo 'style="display:none;"'; ?>>
- <div>
- <h2 class="text-lg font-semibold text-gray-700 border-b pb-2 mb-4">Detail Admin Baru</h2>
- <div class="space-y-4">
- <div>
- <label for="admin_user" class="block text-sm font-medium text-gray-700">Username Admin</label>
- <input type="text" name="admin_user" id="admin_user" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" required>
- </div>
- <div>
- <label for="admin_pass" class="block text-sm font-medium text-gray-700">Password Admin</label>
- <input type="password" name="admin_pass" id="admin_pass" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" required>
- </div>
- <div>
- <label for="admin_email" class="block text-sm font-medium text-gray-700">Email Admin</label>
- <input type="email" name="admin_email" id="admin_email" class="mt-1 block w-full px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" required>
- </div>
- </div>
- </div>
- <div class="mt-8">
- <button type="submit" class="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors">
- Buat Admin
- </button>
- </div>
- </form>
- </div>
- </body>
- </html>
Advertisement