suppress_errors(true); $wpdb->hide_errors(); } $step = sanitize_text_field($_POST['step'] ?? ''); $jobId = sanitize_text_field($_POST['job_id'] ?? ''); // Path sanitization: sanitize_text_field first, then normalize $zipPath = isset($_POST['zip_path']) ? wp_normalize_path(sanitize_text_field(wp_unslash($_POST['zip_path']))) : ''; // Path traversal koruması if (!empty($zipPath) && defined('ALLDAMI_TEMP_DIR')) { $allowedDir = wp_normalize_path(ALLDAMI_TEMP_DIR); if (strpos($zipPath, $allowedDir) !== 0) { wp_send_json_error(['message' => 'Geçersiz dosya yolu: Güvenlik ihlali']); } } try { // Chunk upload işlemi if ($step === 'upload') { handleChunkUpload(); exit; } $progress = new ProgressState(); // Init adımı if ($step === 'init') { if (empty($zipPath)) { wp_send_json_error(['message' => 'DEBUG: Zip Path boş geldi! Lütfen sayfayı yenilemeden tekrar yükleme yapın.']); } if (!file_exists($zipPath)) { wp_send_json_error(['message' => 'DEBUG: Dosya bulunamadı: ' . $zipPath]); } $config = new ChunkConfig(true); $jobId = $progress->init('import', $config); // Security Token Oluştur $importToken = bin2hex(random_bytes(32)); $tokenCreatedAt = time(); // Token expiry için // Settings'i ve token'ı state'e kaydet $settings = Settings::getInstance(); $progress->setResumePointer([ 'zip_path' => $zipPath, 'import_token' => $importToken, 'import_token_created_at' => $tokenCreatedAt, 'settings' => [ 'security_import_token_enabled' => $settings->get('security_import_token_enabled', true) ] ]); $importManager = new ImportManager($progress, $zipPath); $result = $importManager->initialize(); wp_send_json_success([ 'job_id' => $jobId, 'import_token' => $importToken, // Frontend'e döndür 'next_step' => $result['next_step'], 'progress' => $progress->getProgress() ]); } // Devam eden job if (!$progress->load($jobId)) { wp_send_json_error(['message' => 'Geçersiz job ID.']); } $pointer = $progress->getResumePointer(); $zipPath = $pointer['zip_path'] ?? $zipPath; $importManager = new ImportManager($progress, $zipPath); // Debug: Log which step is about to be processed error_log('WP Cloner: Processing step = ' . $step . ' for job = ' . $jobId); switch ($step) { case 'extract': $result = $importManager->extract(); break; case 'preflight': $result = $importManager->preflight(); break; case 'db_import': error_log('AJAX: Case db_import triggered'); $result = $importManager->importDbChunk(); break; case 'file_restore': $result = $importManager->restoreFilesChunk(); break; case 'replace': $result = $importManager->processReplacements(); break; case 'finalize': $result = $importManager->finalize(); break; default: wp_send_json_error(['message' => 'Geçersiz adım: ' . $step]); } if (isset($result['status']) && $result['status'] === 'completed') { wp_send_json_success([ 'status' => 'completed', 'progress' => $progress->getProgress() ]); } else { wp_send_json_success([ 'job_id' => $jobId, 'next_step' => $result['next_step'] ?? null, 'progress' => $progress->getProgress() ]); } } catch (\Throwable $e) { $msg = 'FATAL ERROR: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine(); error_log($msg); if (isset($progress) && $progress instanceof ProgressState) { $progress->setError($msg); } wp_send_json_error([ 'message' => $msg, 'progress' => isset($progress) ? $progress->getProgress() : null ]); } catch (\Exception $e) { $msg = 'EXCEPTION: ' . $e->getMessage(); error_log($msg); if (isset($progress) && $progress instanceof ProgressState) { $progress->setError($msg); } wp_send_json_error([ 'message' => $msg, 'progress' => isset($progress) ? $progress->getProgress() : null ]); } /** * Chunk upload işleyici */ function handleChunkUpload(): void { if (!isset($_FILES['chunk'])) { wp_send_json_error(['message' => 'Dosya chunk bulunamadı.']); } $uploadId = sanitize_text_field($_POST['upload_id'] ?? ''); $chunkIndex = (int) ($_POST['chunk_index'] ?? 0); $totalChunks = (int) ($_POST['total_chunks'] ?? 0); $filename = sanitize_file_name($_POST['filename'] ?? 'backup.zip'); if (empty($uploadId)) { wp_send_json_error(['message' => 'Upload ID gerekli.']); } // Geçici dizin $tempDir = ALLDAMI_TEMP_DIR . 'uploads/' . $uploadId . '/'; if (!is_dir($tempDir)) { wp_mkdir_p($tempDir); } // Chunk'ı kaydet - WordPress uyumlu yöntem $chunkPath = $tempDir . 'chunk_' . str_pad($chunkIndex, 5, '0', STR_PAD_LEFT); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents file_put_contents($chunkPath, file_get_contents($_FILES['chunk']['tmp_name'])); // Tüm chunk'lar yüklendi mi? if ($chunkIndex + 1 >= $totalChunks) { // Chunk'ları birleştir $finalPath = ALLDAMI_TEMP_DIR . $uploadId . '_' . $filename; $finalFile = fopen($finalPath, 'wb'); for ($i = 0; $i < $totalChunks; $i++) { $chunkFile = $tempDir . 'chunk_' . str_pad($i, 5, '0', STR_PAD_LEFT); if (file_exists($chunkFile)) { fwrite($finalFile, file_get_contents($chunkFile)); @unlink($chunkFile); } } fclose($finalFile); // Temp dizini temizle @rmdir($tempDir); wp_send_json_success([ 'completed' => true, 'file_path' => $finalPath, 'filename' => $filename ]); } else { wp_send_json_success([ 'completed' => false, 'chunk_index' => $chunkIndex ]); } }