feat: add panel-validated CAPTCHA dataset collector

- validate labels through real panel login attempts
  - append confirmed samples safely to the OCR dataset
  - add dependency-free mobile-friendly collector UI
  - support Persian and Arabic digit normalization
  - fix PHP 8.5 captcha image corruption
  - add integration tests and documentation
This commit is contained in:
Meghdad committed 2026-07-28 03:52:28 +03:30
1 parent d5e974957e
commit 64b481bdb0
6 files changed
+1616 -1859

No files matched your search

+249
View File
@@ -0,0 +1,249 @@
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background: #09111d;
color: #eaf1fb;
--panel: #111d2d;
--line: #26364b;
--muted: #91a2b8;
--accent: #67e8b8;
--accent-dark: #073c31;
--danger: #ff9a9a;
}
* {
box-sizing: border-box;
}
body {
min-height: 100dvh;
margin: 0;
background:
radial-gradient(circle at top, rgba(46, 91, 126, .28), transparent 36rem),
#09111d;
}
button,
input {
font: inherit;
}
.shell {
width: min(100% - 2rem, 42rem);
margin: 0 auto;
padding: max(1.25rem, env(safe-area-inset-top)) 0 max(2rem, env(safe-area-inset-bottom));
}
.topbar {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 1rem;
margin-bottom: 1.2rem;
}
.eyebrow {
margin: 0 0 .2rem;
color: var(--accent);
font-size: .75rem;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
h1,
h2,
p {
margin-top: 0;
}
h1 {
margin-bottom: 0;
font-size: clamp(1.55rem, 6vw, 2.2rem);
}
.counter {
flex: 0 0 auto;
text-align: right;
}
.counter strong {
display: block;
color: var(--accent);
font-size: 1.5rem;
line-height: 1;
}
.counter span,
.privacy {
color: var(--muted);
font-size: .78rem;
}
.status {
min-height: 2.8rem;
margin-bottom: .9rem;
padding: .75rem .9rem;
border: 1px solid rgba(103, 232, 184, .24);
border-radius: .8rem;
background: rgba(103, 232, 184, .08);
color: #c7f9e8;
}
.status.error {
border-color: rgba(255, 154, 154, .3);
background: rgba(255, 99, 99, .09);
color: var(--danger);
}
.card {
padding: clamp(1rem, 4vw, 1.6rem);
border: 1px solid var(--line);
border-radius: 1.15rem;
background: rgba(17, 29, 45, .96);
box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, .28);
}
.image-stage {
display: grid;
min-height: 9.5rem;
place-items: center;
margin-bottom: 1.2rem;
border: 1px solid var(--line);
border-radius: .9rem;
background: #f3f6fa;
overflow: hidden;
}
.image-stage img {
width: min(100%, 27.5rem);
height: auto;
image-rendering: auto;
}
label {
display: block;
margin-bottom: .45rem;
color: var(--muted);
font-size: .86rem;
font-weight: 700;
}
.entry-row {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: .7rem;
}
input {
min-width: 0;
height: 3.35rem;
padding: 0 .95rem;
border: 1px solid #39506c;
border-radius: .75rem;
outline: 0;
background: #09131f;
color: #fff;
font-size: 1.25rem;
font-variant-numeric: tabular-nums;
letter-spacing: .08em;
}
input:focus {
border-color: var(--accent);
box-shadow: 0 0 0 .2rem rgba(103, 232, 184, .12);
}
button,
.button-link {
min-height: 3.35rem;
padding: 0 1rem;
border: 0;
border-radius: .75rem;
cursor: pointer;
font-weight: 800;
text-decoration: none;
}
button:disabled {
cursor: wait;
opacity: .55;
}
.primary {
background: var(--accent);
color: var(--accent-dark);
}
.secondary {
width: 100%;
min-height: 2.8rem;
margin-top: .7rem;
border: 1px solid var(--line);
background: transparent;
color: var(--muted);
}
.button-link {
display: inline-flex;
align-items: center;
}
.unavailable {
padding: 2rem 1rem;
text-align: center;
}
.unavailable p {
color: var(--muted);
}
.privacy {
margin: .9rem 0 0;
text-align: center;
}
[hidden] {
display: none !important;
}
@media (max-width: 560px) {
.shell {
width: min(100% - 1rem, 42rem);
padding-top: max(.7rem, env(safe-area-inset-top));
}
.topbar {
align-items: center;
margin-bottom: .7rem;
}
.counter span {
display: none;
}
.card {
padding: .8rem;
border-radius: .9rem;
}
.image-stage {
min-height: 7.7rem;
margin-bottom: .8rem;
}
.entry-row {
grid-template-columns: minmax(0, 1fr) 7.3rem;
gap: .45rem;
}
input,
button {
height: 3.2rem;
}
.primary {
padding: 0 .65rem;
font-size: .88rem;
}
}
+117
View File
@@ -0,0 +1,117 @@
(() => {
const answerForm = document.querySelector('#answer-form');
const nextForm = document.querySelector('#next-form');
const input = document.querySelector('#label');
const image = document.querySelector('#captcha-image');
const status = document.querySelector('#status');
const savedCount = document.querySelector('#saved-count');
const submitButton = document.querySelector('#submit-button');
let busy = false;
if (!answerForm || !nextForm || !input || !image || !status || !submitButton) {
return;
}
const digitMap = {
'۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4',
'۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9',
'٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4',
'٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9',
};
const showStatus = (message, isError = false) => {
status.hidden = false;
status.textContent = message;
status.classList.toggle('error', isError);
};
const setBusy = value => {
busy = value;
submitButton.disabled = value;
nextForm.querySelector('button').disabled = value;
};
const applyChallenge = challenge => {
if (!challenge) {
answerForm.hidden = true;
image.closest('.image-stage').hidden = true;
nextForm.querySelector('button').textContent = 'Try another captcha';
return;
}
answerForm.hidden = false;
image.closest('.image-stage').hidden = false;
nextForm.querySelector('button').textContent = 'New captcha';
answerForm.elements.challenge_id.value = challenge.id;
image.src = `${challenge.image_url}&nonce=${Date.now()}`;
input.value = '';
input.focus({preventScroll: true});
};
const submit = async form => {
if (busy) return;
setBusy(true);
try {
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form),
headers: {'Accept': 'application/json'},
});
let payload;
try {
payload = await response.json();
} catch {
throw new Error('The server returned an invalid response.');
}
if (!response.ok) {
const problem = new Error(payload.error || 'The request failed.');
problem.payload = payload;
throw problem;
}
if (savedCount) {
savedCount.textContent = new Intl.NumberFormat().format(
payload.saved_this_session
);
}
applyChallenge(payload.challenge);
showStatus(
payload.warning
? `${payload.message} ${payload.warning}`
: payload.message,
Boolean(payload.warning),
);
} catch (problem) {
if (
problem.payload
&& Object.prototype.hasOwnProperty.call(problem.payload, 'challenge')
) {
applyChallenge(problem.payload.challenge);
}
showStatus(problem.message || 'Connection error. Refresh and try again.', true);
if (!answerForm.hidden) {
input.focus({preventScroll: true});
input.select();
}
} finally {
setBusy(false);
}
};
input.addEventListener('input', () => {
input.value = Array.from(
input.value,
character => digitMap[character] ?? character,
).join('').replace(/[^0-9]/g, '');
});
answerForm.addEventListener('submit', event => {
event.preventDefault();
submit(answerForm);
});
nextForm.addEventListener('submit', event => {
event.preventDefault();
submit(nextForm);
});
input.focus();
})();
+681
View File
@@ -0,0 +1,681 @@
<?php
declare(strict_types=1);
const DATASET_COLUMNS = ['filename', 'label', 'batch_id', 'verified', 'hash'];
const MAX_PANEL_RESPONSE_BYTES = 2_000_000;
const CHALLENGE_MAX_AGE = 240;
final class CollectorError extends RuntimeException
{
public function __construct(
string $message,
public readonly int $status = 400,
) {
parent::__construct($message);
}
}
final class PanelResponse
{
/**
* @param array<string, list<string>> $headers
*/
public function __construct(
public readonly int $status,
public readonly array $headers,
public readonly string $body,
) {
}
public function header(string $name): ?string
{
$values = $this->headers[strtolower($name)] ?? [];
return $values[0] ?? null;
}
/** @return list<string> */
public function headers(string $name): array
{
return $this->headers[strtolower($name)] ?? [];
}
}
function collectorConfig(): array
{
$configuredDataset = $_SERVER['OCR_DATASET_DIR'] ?? getenv('OCR_DATASET_DIR') ?: '';
$timeoutValue = $_SERVER['PANEL_REQUEST_TIMEOUT']
?? getenv('PANEL_REQUEST_TIMEOUT')
?: '10';
$timeout = filter_var($timeoutValue, FILTER_VALIDATE_INT);
return [
'dataset' => rtrim(
$configuredDataset !== '' ? $configuredDataset : dirname(__DIR__) . '/dataset',
DIRECTORY_SEPARATOR,
),
'panel_base_url' => rtrim(
$_SERVER['PANEL_BASE_URL']
?? getenv('PANEL_BASE_URL')
?: 'https://panel.mvphub.ir',
'/',
),
'panel_username' => (string) (
$_SERVER['PANEL_LOGIN_USERNAME']
?? getenv('PANEL_LOGIN_USERNAME')
?: 'username'
),
'panel_password' => (string) (
$_SERVER['PANEL_LOGIN_PASSWORD']
?? getenv('PANEL_LOGIN_PASSWORD')
?: 'password'
),
'timeout' => $timeout === false ? 10 : max(1, min(60, $timeout)),
];
}
function sendSecurityHeaders(): void
{
header_remove('X-Powered-By');
header('Cache-Control: no-store, max-age=0');
header('Pragma: no-cache');
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: DENY');
header("Content-Security-Policy: default-src 'self'; img-src 'self'; style-src 'self'; script-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'");
header('Referrer-Policy: no-referrer');
header('Permissions-Policy: camera=(), microphone=(), geolocation=()');
}
function h(string|int|null $value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
function jsonResponse(array $payload, int $status = 200): never
{
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
exit;
}
function redirect(string $location): never
{
header('Location: ' . $location, true, 303);
exit;
}
function wantsJson(): bool
{
return str_contains(
strtolower((string) ($_SERVER['HTTP_ACCEPT'] ?? '')),
'application/json',
);
}
function scalarRequestValue(string $key, int $maxLength): ?string
{
$value = $_POST[$key] ?? $_GET[$key] ?? null;
return is_string($value) && strlen($value) <= $maxLength ? $value : null;
}
function scalarPostValue(string $key, int $maxLength): ?string
{
$value = $_POST[$key] ?? null;
return is_string($value) && strlen($value) <= $maxLength ? $value : null;
}
function localCsrfToken(): string
{
if (!isset($_SESSION['local_csrf']) || !is_string($_SESSION['local_csrf'])) {
$_SESSION['local_csrf'] = bin2hex(random_bytes(32));
}
return $_SESSION['local_csrf'];
}
function requireLocalCsrf(): void
{
$submitted = scalarPostValue('csrf_token', 64);
if ($submitted === null || !hash_equals(localCsrfToken(), $submitted)) {
throw new CollectorError('This page has expired. Refresh it and try again.', 403);
}
}
function normalizeAnswer(string $answer): string
{
$answer = strtr(trim($answer), [
'۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4',
'۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
'٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4',
'٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9',
]);
if ($answer === '' || strlen($answer) > 16 || preg_match('/^[0-9]+$/D', $answer) !== 1) {
throw new CollectorError('Enter digits only.', 422);
}
return $answer;
}
function panelUrl(array $config, string $path): string
{
$base = $config['panel_base_url'];
$parts = parse_url($base);
if (
$parts === false
|| !isset($parts['scheme'], $parts['host'])
|| !in_array(strtolower((string) $parts['scheme']), ['http', 'https'], true)
|| isset($parts['user'])
|| isset($parts['pass'])
|| isset($parts['query'])
|| isset($parts['fragment'])
) {
throw new CollectorError('PANEL_BASE_URL must be a valid HTTP or HTTPS origin.', 500);
}
return $base . '/' . ltrim($path, '/');
}
/**
* @param list<string> $headers
*/
function panelRequest(
array $config,
string $method,
string $path,
array $headers = [],
string $body = '',
): PanelResponse {
foreach ($headers as $header) {
if (str_contains($header, "\r") || str_contains($header, "\n")) {
throw new CollectorError('Unsafe outbound panel header.', 500);
}
}
$headers[] = 'Accept: text/html, image/*;q=0.9, */*;q=0.5';
$headers[] = 'User-Agent: tci-ocr-panel-collector/1.0';
$options = [
'http' => [
'method' => strtoupper($method),
'header' => implode("\r\n", $headers),
'content' => $body,
'ignore_errors' => true,
'follow_location' => 0,
'timeout' => $config['timeout'],
'protocol_version' => 1.1,
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
],
];
$stream = @fopen(panelUrl($config, $path), 'rb', false, stream_context_create($options));
if ($stream === false) {
$details = error_get_last();
$message = is_array($details) ? (string) ($details['message'] ?? '') : '';
error_log('Panel request failed: ' . $message);
throw new CollectorError('Could not connect to the panel.', 502);
}
try {
$responseBody = stream_get_contents($stream, MAX_PANEL_RESPONSE_BYTES + 1);
$metadata = stream_get_meta_data($stream);
} finally {
fclose($stream);
}
if ($responseBody === false || ($metadata['timed_out'] ?? false) === true) {
throw new CollectorError('The panel request timed out.', 504);
}
if (strlen($responseBody) > MAX_PANEL_RESPONSE_BYTES) {
throw new CollectorError('The panel returned an unexpectedly large response.', 502);
}
$rawHeaders = $metadata['wrapper_data'] ?? [];
if (!is_array($rawHeaders)) {
throw new CollectorError('The panel returned malformed HTTP headers.', 502);
}
$status = 0;
$parsedHeaders = [];
foreach ($rawHeaders as $line) {
if (!is_string($line)) {
continue;
}
if (preg_match('#^HTTP/\S+\s+(\d{3})#i', $line, $matches) === 1) {
$status = (int) $matches[1];
$parsedHeaders = [];
continue;
}
$separator = strpos($line, ':');
if ($separator === false) {
continue;
}
$name = substr($line, 0, $separator);
$value = substr($line, $separator + 1);
if (trim($name) === '') {
continue;
}
$parsedHeaders[strtolower(trim($name))][] = trim($value);
}
if ($status === 0) {
throw new CollectorError('The panel returned a response without an HTTP status.', 502);
}
return new PanelResponse($status, $parsedHeaders, $responseBody);
}
function extractPanelCookies(PanelResponse $response): string
{
$cookies = [];
foreach ($response->headers('set-cookie') as $header) {
$pair = trim(explode(';', $header, 2)[0]);
$separator = strpos($pair, '=');
if ($separator === false) {
continue;
}
$name = substr($pair, 0, $separator);
$value = substr($pair, $separator + 1);
if (
preg_match('/^[A-Za-z0-9_.-]+$/D', $name) !== 1
|| preg_match('/[\x00-\x20\x7f;]/', $value) === 1
) {
continue;
}
$cookies[$name] = $value;
}
if ($cookies === []) {
throw new CollectorError('The panel did not create a login session.', 502);
}
$parts = [];
foreach ($cookies as $name => $value) {
$parts[] = $name . '=' . $value;
}
return implode('; ', $parts);
}
function extractPanelCsrf(string $html): string
{
$patterns = [
'/<input\b[^>]*\bname=(["\'])csrf_token\1[^>]*\bvalue=(["\'])(.*?)\2[^>]*>/is',
'/<input\b[^>]*\bvalue=(["\'])(.*?)\1[^>]*\bname=(["\'])csrf_token\3[^>]*>/is',
];
foreach ($patterns as $index => $pattern) {
if (preg_match($pattern, $html, $matches) === 1) {
$value = $index === 0 ? $matches[3] : $matches[2];
$token = html_entity_decode($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (preg_match('/^[a-f0-9]{64}$/D', $token) === 1) {
return $token;
}
}
}
throw new CollectorError('Could not find the panel login token.', 502);
}
function imageType(string $bytes, ?string $contentType): array
{
$mediaType = strtolower(trim(explode(';', (string) $contentType, 2)[0]));
$types = [
'image/jpeg' => ['extension' => '.jpg', 'magic' => str_starts_with($bytes, "\xff\xd8\xff")],
'image/png' => ['extension' => '.png', 'magic' => str_starts_with($bytes, "\x89PNG\r\n\x1a\n")],
'image/gif' => ['extension' => '.gif', 'magic' => str_starts_with($bytes, 'GIF87a') || str_starts_with($bytes, 'GIF89a')],
'image/bmp' => ['extension' => '.bmp', 'magic' => str_starts_with($bytes, 'BM')],
'image/webp' => [
'extension' => '.webp',
'magic' => strlen($bytes) >= 12
&& substr($bytes, 0, 4) === 'RIFF'
&& substr($bytes, 8, 4) === 'WEBP',
],
];
$type = $types[$mediaType] ?? null;
if ($type === null || $type['magic'] !== true) {
throw new CollectorError('The panel returned an unsupported captcha image.', 502);
}
return ['mime' => $mediaType, 'extension' => $type['extension']];
}
function fetchPanelChallenge(array $config): array
{
$login = panelRequest($config, 'GET', '/panel');
if ($login->status !== 200) {
throw new CollectorError('The panel login page is unavailable.', 502);
}
$cookie = extractPanelCookies($login);
$upstreamCsrf = extractPanelCsrf($login->body);
$captcha = panelRequest(
$config,
'GET',
'/captcha?nonce=' . rawurlencode(bin2hex(random_bytes(8))),
['Cookie: ' . $cookie],
);
if ($captcha->status !== 200 || $captcha->body === '') {
throw new CollectorError('The panel captcha is unavailable.', 502);
}
$type = imageType($captcha->body, $captcha->header('content-type'));
return [
'id' => bin2hex(random_bytes(16)),
'cookie' => $cookie,
'upstream_csrf' => $upstreamCsrf,
'image' => $captcha->body,
'mime' => $type['mime'],
'extension' => $type['extension'],
'issued_at' => time(),
];
}
function challengeIsUsable(mixed $challenge): bool
{
return is_array($challenge)
&& isset(
$challenge['id'],
$challenge['cookie'],
$challenge['upstream_csrf'],
$challenge['image'],
$challenge['mime'],
$challenge['extension'],
$challenge['issued_at'],
)
&& is_string($challenge['id'])
&& is_string($challenge['cookie'])
&& is_string($challenge['upstream_csrf'])
&& is_string($challenge['image'])
&& is_string($challenge['mime'])
&& is_string($challenge['extension'])
&& is_int($challenge['issued_at'])
&& (time() - $challenge['issued_at']) <= CHALLENGE_MAX_AGE;
}
function ensurePanelChallenge(array $config, bool $force = false): array
{
$current = $_SESSION['panel_challenge'] ?? null;
if (!$force && challengeIsUsable($current)) {
return $current;
}
unset($_SESSION['panel_challenge']);
$challenge = fetchPanelChallenge($config);
$_SESSION['panel_challenge'] = $challenge;
return $challenge;
}
function challengeImageUrl(array $challenge): string
{
return '?action=captcha&id=' . rawurlencode($challenge['id']);
}
function challengePayload(array $challenge): array
{
return [
'id' => $challenge['id'],
'image_url' => challengeImageUrl($challenge),
];
}
function servePendingCaptcha(): never
{
$id = scalarRequestValue('id', 64) ?? '';
$challenge = $_SESSION['panel_challenge'] ?? null;
if (
!challengeIsUsable($challenge)
|| $id === ''
|| !hash_equals($challenge['id'], $id)
) {
throw new CollectorError('Captcha not found or expired.', 404);
}
header('Content-Type: ' . $challenge['mime']);
header('Content-Length: ' . strlen($challenge['image']));
echo $challenge['image'];
exit;
}
function validatePendingChallenge(array $config, string $challengeId, string $answer): array
{
$challenge = $_SESSION['panel_challenge'] ?? null;
if (
!challengeIsUsable($challenge)
|| $challengeId === ''
|| !hash_equals($challenge['id'], $challengeId)
) {
throw new CollectorError('This captcha has expired. Load a new one.', 409);
}
// A panel captcha can be used only once. Discard it before the network request
// so an ambiguous timeout can never cause a guessed label to be submitted twice.
unset($_SESSION['panel_challenge']);
$body = http_build_query([
'username' => $config['panel_username'],
'password' => $config['panel_password'],
'captcha' => $answer,
'csrf_token' => $challenge['upstream_csrf'],
], '', '&', PHP_QUERY_RFC3986);
$response = panelRequest(
$config,
'POST',
'/login',
[
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($body),
'Cookie: ' . $challenge['cookie'],
],
$body,
);
$location = $response->header('location');
$locationPath = $location === null ? null : parse_url($location, PHP_URL_PATH);
if ($response->status === 303 && $locationPath === '/panel') {
return storeValidatedSample($config, $challenge, $answer);
}
if (
$response->status === 401
&& str_contains($response->body, 'کد امنیتی اشتباه است یا اعتبار آن به پایان رسیده است')
) {
return ['stored' => false, 'rejected' => true];
}
if (
$response->status === 401
&& str_contains($response->body, 'نام کاربری یا رمز عبور صحیح نیست')
) {
throw new CollectorError(
'The panel rejected the configured username or password.',
502,
);
}
throw new CollectorError(
'The panel returned an unexpected login response; nothing was saved.',
502,
);
}
function outcomeMessage(string $outcome): string
{
return match ($outcome) {
'saved' => 'Correct — saved.',
'duplicate' => 'Correct — already saved.',
'rejected' => 'Rejected — showing the next captcha.',
'skipped' => 'New captcha loaded.',
default => 'Ready.',
};
}
function ensureDirectory(string $path): void
{
if (!is_dir($path) && !@mkdir($path, 0775, true) && !is_dir($path)) {
throw new CollectorError("Cannot create dataset directory: {$path}", 500);
}
}
function writeAtomic(string $path, string $bytes): void
{
ensureDirectory(dirname($path));
$temporary = dirname($path) . DIRECTORY_SEPARATOR
. '.' . basename($path) . '.' . bin2hex(random_bytes(6)) . '.tmp';
$handle = @fopen($temporary, 'xb');
if ($handle === false) {
throw new CollectorError('Cannot create a temporary dataset file.', 500);
}
$completed = false;
try {
try {
$written = 0;
$length = strlen($bytes);
while ($written < $length) {
$count = fwrite($handle, substr($bytes, $written));
if ($count === false || $count === 0) {
throw new CollectorError('Could not write the captcha image.', 500);
}
$written += $count;
}
if (!fflush($handle) || (function_exists('fsync') && !fsync($handle))) {
throw new CollectorError('Could not safely flush the captcha image.', 500);
}
} finally {
fclose($handle);
}
if (!@rename($temporary, $path)) {
throw new CollectorError('Could not finalize the captcha image.', 500);
}
$completed = true;
} finally {
if (!$completed && is_file($temporary)) {
@unlink($temporary);
}
}
}
function csvContainsFilename($handle, string $filename): bool
{
rewind($handle);
fgetcsv($handle, null, ',', '"', '');
while (($row = fgetcsv($handle, null, ',', '"', '')) !== false) {
if (($row[0] ?? null) === $filename) {
return true;
}
}
return false;
}
function validateOrCreateCsvHeader($handle): void
{
$stats = fstat($handle);
if ($stats === false) {
throw new CollectorError('Could not inspect labels.csv.', 500);
}
if ((int) $stats['size'] === 0) {
if (fputcsv($handle, DATASET_COLUMNS, ',', '"', '', "\n") === false) {
throw new CollectorError('Could not initialize labels.csv.', 500);
}
return;
}
rewind($handle);
$header = fgetcsv($handle, null, ',', '"', '');
if ($header !== DATASET_COLUMNS) {
throw new CollectorError('labels.csv has an incompatible header.', 409);
}
}
function appendCsvRow($handle, array $row): void
{
if (fseek($handle, 0, SEEK_END) !== 0) {
throw new CollectorError('Could not seek to the end of labels.csv.', 500);
}
$position = ftell($handle);
if ($position === false) {
throw new CollectorError('Could not inspect labels.csv.', 500);
}
if ($position > 0) {
if (fseek($handle, -1, SEEK_END) !== 0) {
throw new CollectorError('Could not inspect the end of labels.csv.', 500);
}
$last = fread($handle, 1);
if (fseek($handle, 0, SEEK_END) !== 0) {
throw new CollectorError('Could not seek to the end of labels.csv.', 500);
}
if ($last !== "\n" && fwrite($handle, "\n") !== 1) {
throw new CollectorError('Could not repair the labels.csv line ending.', 500);
}
}
if (fputcsv($handle, $row, ',', '"', '', "\n") === false) {
throw new CollectorError('Could not append to labels.csv.', 500);
}
if (!fflush($handle) || (function_exists('fsync') && !fsync($handle))) {
throw new CollectorError('Could not safely flush labels.csv.', 500);
}
}
function storeValidatedSample(array $config, array $challenge, string $label): array
{
$dataset = $config['dataset'];
ensureDirectory($dataset);
$hash = hash('sha256', $challenge['image']);
$relative = 'images/panel/' . substr($hash, 0, 2) . '/'
. $hash . $challenge['extension'];
$imagePath = $dataset . DIRECTORY_SEPARATOR
. str_replace('/', DIRECTORY_SEPARATOR, $relative);
$marker = $dataset . DIRECTORY_SEPARATOR . '.panel-samples'
. DIRECTORY_SEPARATOR . substr($hash, 0, 2)
. DIRECTORY_SEPARATOR . $hash . '.done';
$csvPath = $dataset . DIRECTORY_SEPARATOR . 'labels.csv';
$lockPath = $dataset . DIRECTORY_SEPARATOR . '.panel-collector.lock';
$lock = @fopen($lockPath, 'c');
if ($lock === false || !flock($lock, LOCK_EX)) {
if (is_resource($lock)) {
fclose($lock);
}
throw new CollectorError('Could not lock the dataset for writing.', 503);
}
try {
$csv = @fopen($csvPath, 'c+b');
if ($csv === false) {
throw new CollectorError('Could not open labels.csv for writing.', 500);
}
try {
validateOrCreateCsvHeader($csv);
$csvChecked = false;
if (is_file($marker)) {
$csvChecked = true;
if (csvContainsFilename($csv, $relative)) {
return ['stored' => false, 'hash' => $hash, 'filename' => $relative];
}
if (!@unlink($marker)) {
throw new CollectorError('Could not repair stale panel sample state.', 500);
}
}
if (is_file($imagePath)) {
$existingHash = @hash_file('sha256', $imagePath);
if (!is_string($existingHash) || !hash_equals($hash, $existingHash)) {
throw new CollectorError('An existing captcha file failed its hash check.', 500);
}
if (!$csvChecked && csvContainsFilename($csv, $relative)) {
writeAtomic($marker, "saved\n");
return ['stored' => false, 'hash' => $hash, 'filename' => $relative];
}
} else {
writeAtomic($imagePath, $challenge['image']);
}
appendCsvRow($csv, [$relative, $label, 'panel', '1', $hash]);
writeAtomic($marker, "saved\n");
} finally {
fclose($csv);
}
} finally {
flock($lock, LOCK_UN);
fclose($lock);
}
return ['stored' => true, 'hash' => $hash, 'filename' => $relative];
}
+186 -1586
View File
File diff suppressed because it is too large. Load diff