( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
@mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "Pw4TheBrand!";
$dbname = "thebrand";
header('Content-Type: application/json');
$logDir = __DIR__ . '/v/uploads/converted';
if (!is_dir($logDir)) { @mkdir($logDir, 0777, true); }
$errorLogPath = $logDir . '/transfer_php_errors.log';
@ini_set('log_errors','1');
@ini_set('error_log',$errorLogPath);
$phpErrFp = @fopen($errorLogPath, 'a');
function logErr($fp, $type, $data = []) { if ($fp) { $row = array_merge(["ts" => date('c'), "type" => $type], $data); @fwrite($fp, json_encode($row) . PHP_EOL); } }
set_error_handler(function($severity, $message, $file, $line) use ($phpErrFp) { logErr($phpErrFp, 'php_error', ["severity" => $severity, "message" => $message, "file" => $file, "line" => $line]); });
set_exception_handler(function($ex) use ($phpErrFp) { logErr($phpErrFp, 'php_exception', ["message" => $ex->getMessage(), "file" => $ex->getFile(), "line" => $ex->getLine(), "code" => $ex->getCode()]); });
register_shutdown_function(function() use ($phpErrFp) { $e = error_get_last(); if ($e) { logErr($phpErrFp, 'php_fatal', ["type" => $e['type'], "message" => $e['message'], "file" => $e['file'], "line" => $e['line']]); } if ($phpErrFp) { @fclose($phpErrFp); } });
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
http_response_code(500);
echo json_encode(["success" => false, "message" => "db_connect_error"]);
exit;
}
$conn->set_charset("utf8");
$catalogid = 152;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 100;
if ($limit < 1) { $limit = 100; }
$stmt = $conn->prepare("SELECT id, title FROM profilepicture WHERE catalogid = ? AND converted = 0 AND public = 1 AND is_deleted = 0 AND isdisplay = 13 ORDER BY id DESC LIMIT ?");
$stmt->bind_param("ii", $catalogid, $limit);
// continuous pagination until exhausted
$maxBatches = isset($_GET['max_batches']) ? (int)$_GET['max_batches'] : 1000;
if ($maxBatches < 1) { $maxBatches = 1000; }
$stmt2 = $conn->prepare("SELECT layers3, layers2 FROM layerorder WHERE themeid = ? ORDER BY id DESC LIMIT 1");
$stmt3 = $conn->prepare("UPDATE profilepicture SET converted = 1 WHERE id = ?");
$stmt4 = $conn->prepare("UPDATE profilepicture SET converted = 3 WHERE id = ?");
$dir = __DIR__ . '/v/uploads/converted';
if (!is_dir($dir)) { @mkdir($dir, 0777, true); }
$stream = isset($_GET['stream']) ? (int)$_GET['stream'] : 0;
if ($stream === 1) { header('Content-Type: text/plain'); @ini_set('output_buffering','0'); @ini_set('zlib.output_compression','0'); @ini_set('implicit_flush','1'); ob_implicit_flush(true); }
$progressPath = $dir . '/transfer_progress.log';
$checkpointPath = $dir . '/transfer_checkpoint.json';
$progressFp = @fopen($progressPath, 'a');
function logp($fp, $event, $payload = []) { if ($fp) { $row = array_merge(["ts" => date('c'), "event" => $event], $payload); @fwrite($fp, json_encode($row) . PHP_EOL); } }
function out($stream, $msg) { if ($stream === 1) { echo $msg . "\n"; if (function_exists('flush')) { @flush(); } if (function_exists('ob_flush')) { @ob_flush(); } } }
$processed = [];
$errors = [];
$batchesRun = 0;
$lastThemeId = null;
while (true) {
if ($batchesRun >= $maxBatches) { break; }
logp($progressFp, 'batch_begin', ["batch" => $batchesRun + 1]);
out($stream, 'batch_begin ' . ($batchesRun + 1));
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) { break; }
$batchesRun++;
while ($row = $result->fetch_assoc()) {
$themeid = (int)$row['id'];
$titleStr = (string)$row['title'];
$lastThemeId = $themeid;
logp($progressFp, 'row_begin', ["themeid" => $themeid, "title" => $titleStr]);
out($stream, 'row_begin ' . $themeid);
$stmt2->bind_param("i", $themeid);
$stmt2->execute();
$res2 = $stmt2->get_result();
$layers = $res2->fetch_assoc();
if (!$layers) {
$errors[] = ["themeid" => $themeid, "error" => "no_layerorder_record"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "no_layerorder_record"]);
out($stream, 'error no_layerorder_record ' . $themeid);
continue;
}
$l3 = isset($layers['layers3']) ? trim((string)$layers['layers3']) : '';
$l2 = isset($layers['layers2']) ? trim((string)$layers['layers2']) : '';
$templateJson = $l3 !== '' ? $l3 : $l2;
if ($templateJson === '') {
$errors[] = ["themeid" => $themeid, "error" => "empty_layers_fields"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "empty_layers_fields"]);
out($stream, 'error empty_layers_fields ' . $themeid);
continue;
}
$decoded = json_decode($templateJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$errors[] = ["themeid" => $themeid, "error" => "invalid_json_in_layers"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "invalid_json_in_layers"]);
out($stream, 'error invalid_json_in_layers ' . $themeid);
$stmt4->bind_param("i", $themeid);
$stmt4->execute();
continue;
}
$basename = sha1($themeid . ':' . $titleStr);
$path = $dir . '/' . $basename . '.json';
$ok = file_put_contents($path, $templateJson) !== false;
if ($ok && file_exists($path) && filesize($path) > 2) {
$written = file_get_contents($path);
$verified = ($written !== false) && (md5($written) === md5($templateJson));
if ($verified) {
$stmt3->bind_param("i", $themeid);
$stmt3->execute();
$processed[] = ["themeid" => $themeid, "filename" => $basename . '.json', "path" => 'v/uploads/converted/' . $basename . '.json'];
logp($progressFp, 'row_done', ["themeid" => $themeid, "filename" => $basename . '.json']);
out($stream, 'row_done ' . $themeid);
} else {
$errors[] = ["themeid" => $themeid, "error" => "verification_failed"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "verification_failed"]);
out($stream, 'error verification_failed ' . $themeid);
}
} else {
$errors[] = ["themeid" => $themeid, "error" => "write_failed"];
logp($progressFp, 'error', ["themeid" => $themeid, "error" => "write_failed"]);
out($stream, 'error write_failed ' . $themeid);
}
@file_put_contents($checkpointPath, json_encode(["last_themeid" => $lastThemeId, "count_processed" => count($processed), "count_errors" => count($errors), "batches_run" => $batchesRun]));
}
}
$stmt2->close();
$stmt3->close();
$stmt4->close();
$stmt->close();
$conn->close();
if ($progressFp) { @fclose($progressFp); }
echo json_encode([
"success" => true,
"count_processed" => count($processed),
"processed" => $processed,
"count_errors" => count($errors),
"errors" => $errors,
"batches_run" => $batchesRun
]);
?>