- สร้างโฟลเดอร์เก็บสคริปก่อน
mkdir -p /root/scripts
cd /root/scripts
2. สร้างไฟล์ใส่สคริป
nano manage_wp_settings.php
3. copy โค๊ดไปใส่ในไฟล์
<?php
// manage_wp_settings.php (Version 2.0 - Recursive Search)
// กำหนดค่าคอนฟิกที่ต้องการเพิ่ม/แก้ไขใน wp-config.php
$wp_config_defines = [
"define('DISALLOW_FILE_EDIT', true);",
"define('WP_DISABLE_FATAL_ERROR_HANDLER', true);"
];
// ตั้งค่าสำหรับ Database (options table)
$db_settings_to_update = [
'users_can_register' => '0',
'comment_registration' => '1',
'default_comment_status' => 'closed',
];
// เส้นทางหลักของไดเรกทอรีผู้ใช้ DirectAdmin
$users_dir = '/home/';
$report = "รายงานการอัปเดต WordPress Settings (ปิดลงทะเบียน/คอมเมนต์) โดยสคริปต์อัตโนมัติ:\n\n";
$report .= "วันที่: " . date('Y-m-d H:i:s') . "\n";
$report .= "-------------------------------------------------------\n\n";
echo "เริ่มต้นการสแกนและอัปเดตการตั้งค่า WordPress ในทุกเว็บไซต์ (รวมโฟลเดอร์ย่อย)...\n\n";
// <<< จุดที่แก้ไข 1: ฟังก์ชันใหม่สำหรับค้นหา wp-config.php ในโฟลเดอร์ย่อยทั้งหมด
/**
* ค้นหาไฟล์ wp-config.php ทั้งหมดในไดเรกทอรีที่กำหนด
* @param string $dir ไดเรกทอรีเริ่มต้น (เช่น /home/user/domains/domain.com/public_html)
* @return array รายการ path ของไฟล์ wp-config.php ที่พบ
*/
function find_wp_configs($dir) {
$wp_configs = [];
if (!is_dir($dir)) {
return [];
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if ($file->getFilename() === 'wp-config.php') {
$wp_configs[] = $file->getPathname();
}
}
return $wp_configs;
}
// Function สำหรับเชื่อมต่อ Database
function get_db_connection($db_host, $db_user, $db_password, $db_name) {
mysqli_report(MYSQLI_REPORT_OFF);
$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);
if ($mysqli->connect_error) {
return [null, $mysqli->connect_error];
}
$mysqli->set_charset("utf8mb4");
return [$mysqli, null];
}
$user_folders = @scandir($users_dir);
if (!$user_folders) {
$report .= "ข้อผิดพลาด: ไม่สามารถเข้าถึงไดเรกทอรีผู้ใช้งาน: {$users_dir} โปรดตรวจสอบสิทธิ์การเข้าถึง /home.\n";
die($report);
}
foreach ($user_folders as $user_folder) {
if ($user_folder == '.' || $user_folder == '..' || !is_dir($users_dir . $user_folder) || strpos($user_folder, '.') !== false) {
continue;
}
$domain_base_path = $users_dir . $user_folder . '/domains/';
if (!is_dir($domain_base_path)) {
continue;
}
echo "กำลังตรวจสอบผู้ใช้: **{$user_folder}**\n";
$report .= "--- ผู้ใช้: {$user_folder} ---\n";
$domain_folders = @scandir($domain_base_path);
if (!$domain_folders) {
continue;
}
foreach ($domain_folders as $domain_folder) {
if ($domain_folder == '.' || $domain_folder == '..' || !is_dir($domain_base_path . $domain_folder)) {
continue;
}
$public_html_path = $domain_base_path . $domain_folder . '/public_html/';
echo " - กำลังตรวจสอบโดเมน: **{$domain_folder}**\n";
$report .= " โดเมน: {$domain_folder}\n";
// <<< จุดที่แก้ไข 2: เรียกใช้ฟังก์ชันค้นหา wp-config.php
$wp_config_paths = find_wp_configs($public_html_path);
if (empty($wp_config_paths)) {
echo " - ไม่พบการติดตั้ง WordPress ในโดเมนนี้ (ข้าม).\n";
$report .= " - ไม่พบ wp-config.php (ข้าม).\n\n";
continue;
}
// <<< จุดที่แก้ไข 3: วนลูปจัดการทุก wp-config.php ที่เจอ
foreach ($wp_config_paths as $wp_config_path) {
$install_path = dirname($wp_config_path);
echo " - พบ WordPress ที่: {$install_path}\n";
$report_domain_detail = " - Path: {$install_path}\n";
// 1. จัดการ wp-config.php
$config_content = file_get_contents($wp_config_path);
$config_modified = false;
foreach ($wp_config_defines as $line_to_add) {
if (strpos($config_content, $line_to_add) === false) {
$insert_pos = strpos($config_content, "/* That's all, stop editing! Happy publishing. */");
if ($insert_pos !== false) {
$config_content = substr_replace($config_content, $line_to_add . "\n", $insert_pos, 0);
$config_modified = true;
$report_domain_detail .= " + เพิ่ม/แก้ไข wp-config.php: {$line_to_add}\n";
}
}
}
if ($config_modified) {
if (is_writable($wp_config_path)) {
file_put_contents($wp_config_path, $config_content);
echo " * อัปเดต wp-config.php สำเร็จ.\n";
$report_domain_detail .= " * อัปเดต wp-config.php สำเร็จ.\n";
} else {
echo " ! ไม่สามารถเขียนไฟล์ wp-config.php ได้.\n";
$report_domain_detail .= " ! ไม่สามารถเขียนไฟล์ wp-config.php ได้. (สิทธิ์ผิดพลาด)\n";
}
} else {
echo " * ไม่มีการเปลี่ยนแปลงใน wp-config.php.\n";
$report_domain_detail .= " * ไม่มีการเปลี่ยนแปลงใน wp-config.php.\n";
}
// 2. จัดการ Database
$db_name = $db_user = $db_password = '';
$db_host = 'localhost';
$table_prefix = 'wp_';
$config_lines = file($wp_config_path);
foreach ($config_lines as $line) {
if (preg_match("/define\(\s*'DB_NAME',\s*'(.*?)'\s*\);/", $line, $matches)) $db_name = $matches[1];
if (preg_match("/define\(\s*'DB_USER',\s*'(.*?)'\s*\);/", $line, $matches)) $db_user = $matches[1];
if (preg_match("/define\(\s*'DB_PASSWORD',\s*'(.*?)'\s*\);/", $line, $matches)) $db_password = $matches[1];
if (preg_match("/define\(\s*'DB_HOST',\s*'(.*?)'\s*\);/", $line, $matches)) $db_host = $matches[1];
if (preg_match("/\\\$table_prefix\s*=\s*'(.*?)';/", $line, $matches)) $table_prefix = $matches[1];
}
if ($db_name && $db_user && $db_password) {
$report_domain_detail .= " - ฐานข้อมูล: {$db_name} (Prefix: {$table_prefix})\n";
list($mysqli, $connect_error_msg) = get_db_connection($db_host, $db_user, $db_password, $db_name);
if ($mysqli) {
$db_modified = false;
foreach ($db_settings_to_update as $option_name => $option_value) {
$stmt = $mysqli->prepare("SELECT option_value FROM {$table_prefix}options WHERE option_name = ?");
if ($stmt) {
$stmt->bind_param("s", $option_name);
$stmt->execute();
$result = $stmt->get_result();
$current_value = $result->fetch_assoc()['option_value'] ?? null;
$stmt->close();
if ($current_value === null) {
$stmt_insert = $mysqli->prepare("INSERT INTO {$table_prefix}options (option_name, option_value, autoload) VALUES (?, ?, 'yes')");
if ($stmt_insert) {
$stmt_insert->bind_param("ss", $option_name, $option_value);
$stmt_insert->execute();
if ($stmt_insert->affected_rows > 0) {
$report_domain_detail .= " + เพิ่มค่า DB: {$option_name} = '{$option_value}'\n";
$db_modified = true;
}
$stmt_insert->close();
}
} elseif ($current_value != $option_value) {
$stmt_update = $mysqli->prepare("UPDATE {$table_prefix}options SET option_value = ? WHERE option_name = ?");
if ($stmt_update) {
$stmt_update->bind_param("ss", $option_value, $option_name);
$stmt_update->execute();
if ($stmt_update->affected_rows > 0) {
$report_domain_detail .= " + อัปเดตค่า DB: {$option_name} จาก '{$current_value}' เป็น '{$option_value}'\n";
$db_modified = true;
}
$stmt_update->close();
}
}
}
}
if (!$db_modified) {
$report_domain_detail .= " * ไม่มีการเปลี่ยนแปลงในฐานข้อมูล.\n";
}
$mysqli->close();
} else {
$report_domain_detail .= " ! ไม่สามารถเชื่อมต่อฐานข้อมูลได้: " . ($connect_error_msg ?: "ไม่ทราบสาเหตุ") . ".\n";
}
} else {
$report_domain_detail .= " - ไม่สามารถดึงข้อมูลฐานข้อมูล (ข้าม).\n";
}
$report .= $report_domain_detail . "\n";
}
}
$report .= "-------------------------------------------------------\n\n";
}
echo "\nการดำเนินการสคริปต์เสร็จสิ้น. กำลังเตรียมส่งอีเมลรายงาน...\n";
$report .= "\n\nการดำเนินการสคริปต์เสร็จสิ้น.";
// ส่งอีเมลแจ้งเตือน
$to = 'service24x7@hosttook.com';
$subject = 's3 รายงานการอัปเดต WordPress Settings (ปิดลงทะเบียน/คอมเมนต์)';
$headers = "From: s3 Automated Report <fail2ban@hosttook.com>\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
if (mail($to, $subject, $report, $headers)) {
echo "ส่งอีเมลรายงานสำเร็จไปยัง {$to}\n";
} else {
echo "เกิดข้อผิดพลาดในการส่งอีเมลรายงาน.\n";
}
?>
chmod +x /root/scripts/manage_wp_settings.php
chmod +x /root/scripts/manage_wp_settings.php
รันคำสั่ง
php /root/scripts/manage_wp_settings.php
หรือใส่ cronjob
## วิธีการเพิ่ม Crontab
- เปิด Terminal หรือ SSH เข้าไปยังเซิร์ฟเวอร์ของคุณ
- พิมพ์คำสั่งเพื่อแก้ไข crontab:Bash
crontab -eหากเป็นการเปิดครั้งแรก ระบบอาจจะถามให้คุณเลือกโปรแกรมสำหรับแก้ไขไฟล์ (editor) แนะนำให้เลือกnanoเพราะใช้งานง่ายที่สุดครับ - กด i เพื่อ insert ข้อความ
- คัดลอกคำสั่ง crontab ด้านบนไปวาง
- บันทึกและออกจาก editor:
- กด
ESC แล้วกด : wq จะเป็นการบันทึกแล้วออกจากโปรแกรม
- กด
- เมื่อบันทึกสำเร็จ คุณจะเห็นข้อความแจ้งว่า
crontab: installing new crontabเป็นอันเสร็จสิ้น
## คำอธิบายคำสั่ง
เพื่อให้เข้าใจมากขึ้น ผมจะอธิบายแต่ละส่วนของคำสั่งนะครับ 🧐
0 0 * * *คือการกำหนดเวลาทำงาน ซึ่งหมายถึง:0: นาทีที่ 00: ชั่วโมงที่ 0 (เที่ยงคืน)*: ทุกๆ วันในเดือน*: ทุกๆ เดือน*: ทุกๆ วันในสัปดาห์- รวมกันเป็น: “เวลาเที่ยงคืน ของทุกๆ วัน”
/usr/bin/phpคือการระบุ path แบบเต็มของโปรแกรม PHP interpreter (แนะนำให้ใช้ path เต็มเสมอใน cron job)- คุณสามารถตรวจสอบ path ที่ถูกต้องบนเซิร์ฟเวอร์ของคุณได้ด้วยคำสั่ง
which php
- คุณสามารถตรวจสอบ path ที่ถูกต้องบนเซิร์ฟเวอร์ของคุณได้ด้วยคำสั่ง
/root/scripts/manage_wp_settings.phpคือ path แบบเต็มไปยังไฟล์สคริปต์ของคุณ> /dev/null 2>&1เป็นส่วนเสริมที่มีประโยชน์มากครับ- มันหมายถึง “ไม่ต้องแสดงผลลัพธ์หรือ error ใดๆ ออกมา”
- เนื่องจากสคริปต์ของคุณมีการส่งอีเมลรายงานผลด้วยตัวเองอยู่แล้ว เราจึงไม่จำเป็นต้องให้ cron job ส่งอีเมลแจ้งเตือนการทำงานซ้ำซ้อนอีกครับ การทำแบบนี้จะช่วยให้ mailbox ของคุณไม่รกครับ