Repository navigation
Expand file tree
/
Copy pathmigrateUserBoards.php
More file actions
683 lines (571 loc) · 21.2 KB
/
Copy pathmigrateUserBoards.php
File metadata and controls
683 lines (571 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
<?php
/**
* Migrates pre-4.3 user boards into the current layout.
*
* Old boards were standalone copies of the uploader living in
* user/boards/<uri>/, each with its own config.php. They now become a line in
* data/boards.log plus a boards/<uri>/ directory served by the main script.
*
* The source is only read, never modified, so a failed run leaves the old
* boards untouched and a partially migrated board can be removed from the
* admin's Boards page and migrated again.
*
* Usage:
* php migrateUserBoards.php [--dry-run] [--source=path/to/user/boards]
* php migrateUserBoards.php --thumbnails [--dry-run] [--source=...]
*
* --thumbnails only copies thumbnails that are missing from boards migrated
* earlier, for boards whose old thumbnails had no prefix.
*/
if (PHP_SAPI !== 'cli') {
http_response_code(403);
exit("This script may only be run from the command line.\n");
}
define('ROOT_DIR', __DIR__);
define('GLOBAL_DATA_DIR', ROOT_DIR . '/data/');
define('DATA_DIR', GLOBAL_DATA_DIR);
require __DIR__ . '/code/TwintailUploader/include.php';
require __DIR__ . '/autoloader.php';
use TwintailUploader\Classes\board;
use TwintailUploader\Classes\boardRepository;
use TwintailUploader\Classes\themeManager;
use function TwintailUploader\Functions\generatePasswordHash;
/** Board fields the old config.php could not express */
const DROPPED_SETTINGS = ['deletionPassword', 'passwordRequired', 'maxAmountOfFiles', 'maxUploadSize', 'maxTotalSize'];
/** URIs that would collide with the repository layout or with routes */
const RESERVED_URIS = ['index', 'admin', 'api', 'boards', 'code', 'data', 'lang', 'src', 'static', 'templates', 'thmb', 'user'];
/**
* Sentinel for a value we refuse to evaluate (marks it "skip this key").
*
* Declared up here with the others because `const` is not hoisted the way a
* function declaration is: the migration loop below runs before any statement
* further down the file, so a sentinel defined next to the parser that uses it
* would not exist yet when the parser first ran.
*/
const MIGRATE_COMPLEX_VALUE = "\0__complex__\0";
$options = parseOptions($argv);
$conf = require ROOT_DIR . '/config.php';
$sourceDir = rtrim($options['source'] ?? (ROOT_DIR . '/user/boards'), '/');
$boardsRoot = ROOT_DIR . '/' . $conf['boardsDir'];
if (!is_dir($sourceDir)) {
exit("Nothing to migrate: $sourceDir does not exist.\n");
}
if ($options['dry-run']) {
echo "DRY RUN — nothing will be written.\n\n";
}
$boardRepository = new boardRepository(GLOBAL_DATA_DIR . 'boards.log');
$migrated = 0;
$skipped = 0;
foreach (findOldBoards($sourceDir) as $uri => $oldConfigFile) {
echo "[$uri]\n";
$result = $options['thumbnails']
? copyMissingThumbnails($uri, $oldConfigFile, $conf, $boardsRoot, $boardRepository, $options['dry-run'])
: migrateBoard($uri, $oldConfigFile, $conf, $boardsRoot, $boardRepository, $options['dry-run']);
foreach ($result['messages'] as $message) {
echo " $message\n";
}
$result['migrated'] ? $migrated++ : $skipped++;
echo "\n";
}
echo ($options['thumbnails'] ? 'Updated' : 'Migrated') . " $migrated board(s), skipped $skipped.\n";
if ($migrated > 0 && !$options['dry-run'] && !$options['thumbnails']) {
echo "The old boards were left in place — remove $sourceDir once you've checked the new ones.\n";
}
/**
* @return array<string,string> board uri => path to its old config.php
*/
function findOldBoards(string $sourceDir): array {
$boards = [];
foreach (scandir($sourceDir) ?: [] as $entry) {
if ($entry === '.' || $entry === '..') continue;
$configFile = $sourceDir . '/' . $entry . '/config.php';
if (is_dir($sourceDir . '/' . $entry) && file_exists($configFile)) {
$boards[$entry] = $configFile;
}
}
ksort($boards);
return $boards;
}
/**
* @return array{migrated:bool,messages:string[]}
*/
function migrateBoard(string $uri, string $oldConfigFile, array $conf, string $boardsRoot, boardRepository $boardRepository, bool $dryRun): array {
$messages = [];
if (!preg_match('/^[A-Za-z0-9_-]{1,16}$/', $uri)) {
return ['migrated' => false, 'messages' => ['SKIPPED: the directory name is not a usable board URL (letters, numbers, dashes and underscores, up to 16 characters).']];
}
if (in_array(strtolower($uri), RESERVED_URIS, true)) {
return ['migrated' => false, 'messages' => ['SKIPPED: that board URL is reserved. Rename the directory and run again.']];
}
if ($boardRepository->uriExists($uri)) {
return ['migrated' => false, 'messages' => ['SKIPPED: already registered in data/boards.log.']];
}
$boardDir = $boardsRoot . $uri;
if (is_dir($boardDir)) {
return ['migrated' => false, 'messages' => ["SKIPPED: $boardDir already exists."]];
}
$oldConf = loadOldConfig($oldConfigFile);
if ($oldConf === null) {
return ['migrated' => false, 'messages' => ['SKIPPED: could not read the old config.php.']];
}
// The prefix is baked into every stored file name, so a prefix we can't
// reproduce safely means the files would stop resolving
$prefix = (string) ($oldConf['prefix'] ?? '');
if (!preg_match('/^[A-Za-z0-9_-]{0,10}$/', $prefix)) {
return ['migrated' => false, 'messages' => ["SKIPPED: the file prefix " . var_export($prefix, true) . " isn't safe to reuse. Rename the files and set a plain prefix first."]];
}
$oldBoardDir = dirname($oldConfigFile);
$oldUploadDir = $oldBoardDir . '/' . ($oldConf['uploadDir'] ?? 'src/');
$oldThumbDir = $oldBoardDir . '/' . ($oldConf['thumbDir'] ?? 'thmb/');
$oldLogFile = $oldBoardDir . '/' . ($oldConf['logFile'] ?? 'userPosts.block');
$entries = readOldLog($oldLogFile, $messages);
$board = new board([
(string) $boardRepository->getNextID(),
$uri,
cleanText((string) ($oldConf['boardTitle'] ?? $uri), 64) ?: $uri,
cleanText((string) ($oldConf['boardSubTitle'] ?? ''), 256),
hashOwnerPassword((string) ($oldConf['adminPassword'] ?? ''), $messages),
bin2hex(random_bytes(16)),
(string) @filemtime($oldConfigFile) ?: (string) time(),
'',
!empty($oldConf['boardListed']) ? '1' : '0',
'0',
!empty($oldConf['commentRequired']) ? '1' : '0',
cleanText((string) ($oldConf['defaultComment'] ?? ''), 128),
$prefix,
// the old config named a theme file; keep it only if that file is
// still installed, and start the board with no custom palette
installedTheme((string) ($oldConf['defaultTheme'] ?? '')),
'',
]);
$bans = collectOldBans($oldConf);
$messages[] = count($entries) . ' upload(s), ' . count($bans) . ' ban(s).';
if ($prefix === '') {
$messages[] = 'NOTE: the board has no file prefix, so its files keep their bare "<id>.<ext>" names.';
}
reportDroppedSettings($oldConf, $conf, $messages);
if ($dryRun) {
$messages[] = "would create $boardDir and register it as [" . $uri . '].';
return ['migrated' => true, 'messages' => $messages];
}
if (!scaffold($boardDir, $conf, $messages)) {
return ['migrated' => false, 'messages' => $messages];
}
[$copied, $skipped] = copyDirContents($oldUploadDir, $boardDir . '/' . rtrim($conf['uploadDir'], '/'), $conf['allowedExtensions'] ?? [], $conf['extensionsToBeConvertedToText'] ?? []);
$messages[] = "copied $copied file(s) into " . $conf['uploadDir'];
if ($skipped > 0) {
$messages[] = "NOTE: skipped $skipped file(s) with a disallowed extension (would have been web-served).";
}
$thumbs = copyThumbnails($oldThumbDir, $boardDir . '/' . rtrim($conf['thumbDir'], '/'), $entries, $prefix, $conf);
$messages[] = "copied $thumbs thumbnail(s), renamed to the current scheme.";
writeLog($boardDir . '/data/' . $conf['logFile'], $entries);
file_put_contents($boardDir . '/data/' . $conf['counterFile'], (string) highestID($entries));
file_put_contents($boardDir . '/data/banlist.dat', $bans ? implode("\n", $bans) . "\n" : '');
if (!$boardRepository->add($board)) {
return ['migrated' => false, 'messages' => array_merge($messages, ['FAILED: could not register the board in data/boards.log.'])];
}
$messages[] = "migrated to $boardDir";
return ['migrated' => true, 'messages' => $messages];
}
/**
* The old config.php is owner-supplied code, so it is never require()'d — that
* would run whatever the owner put in it with the migrating admin's privileges.
* Instead the `return [...]` literal is parsed statically: only string, number,
* bool, null and (nested) arrays of those are read; anything else — a function
* call, an expression, a variable — is skipped, never evaluated.
*/
function loadOldConfig(string $configFile): ?array {
$contents = file_get_contents($configFile);
if ($contents === false) {
return null;
}
$parsed = parseReturnedArray($contents);
return is_array($parsed) ? $parsed : null;
}
/**
* Statically parses the array literal a PHP file returns, without executing it.
*/
function parseReturnedArray(string $code): ?array {
$stream = [];
foreach (token_get_all($code) as $token) {
if (is_array($token)) {
if (in_array($token[0], [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG], true)) {
continue;
}
$stream[] = [$token[0], $token[1]];
} else {
$stream[] = [null, $token];
}
}
$n = count($stream);
$pos = 0;
while ($pos < $n && $stream[$pos][0] !== T_RETURN) {
$pos++;
}
if ($pos >= $n) {
return null;
}
$pos++;
// tolerate `return $conf = [ ... ]` as well as `return [ ... ]`
if ($pos < $n && $stream[$pos][0] === T_VARIABLE && ($pos + 1) < $n && $stream[$pos + 1][1] === '=') {
$pos += 2;
}
$value = parseConfigValue($stream, $pos);
return is_array($value) ? $value : null;
}
/** Reads one literal value at $pos, advancing it. Returns the sentinel if complex. */
function parseConfigValue(array $stream, int &$pos) {
$n = count($stream);
if ($pos >= $n) {
return MIGRATE_COMPLEX_VALUE;
}
[$id, $text] = $stream[$pos];
// array literal — [ ... ] or array( ... )
if ($text === '[' || $id === T_ARRAY) {
$close = ']';
if ($id === T_ARRAY) {
$pos++; // skip 'array'
$close = ')';
}
$pos++; // skip opening bracket
$result = [];
$autoIndex = 0;
while ($pos < $n && $stream[$pos][1] !== $close) {
$key = parseConfigValue($stream, $pos);
$keyed = false;
$value = $key;
if ($pos < $n && $stream[$pos][0] === T_DOUBLE_ARROW) {
$keyed = true;
$pos++; // skip =>
$value = parseConfigValue($stream, $pos);
}
// A clean element leaves $pos on a separator. Anything else means we
// only parsed the prefix of an expression (e.g. 20*1024) — skip the
// rest and drop the element rather than storing a wrong value.
if ($pos < $n && $stream[$pos][1] !== ',' && $stream[$pos][1] !== $close) {
skipConfigValue($stream, $pos);
} elseif ($keyed) {
if ((is_string($key) || is_int($key)) && $value !== MIGRATE_COMPLEX_VALUE) {
$result[$key] = $value;
}
} elseif ($value !== MIGRATE_COMPLEX_VALUE) {
$result[$autoIndex++] = $value;
} else {
$autoIndex++;
}
if ($pos < $n && $stream[$pos][1] === ',') {
$pos++;
} else {
break;
}
}
if ($pos < $n && $stream[$pos][1] === $close) {
$pos++;
}
return $result;
}
if ($id === T_CONSTANT_ENCAPSED_STRING) {
$pos++;
return unquotePhpString($text);
}
if ($id === T_LNUMBER) {
$pos++;
return (int) $text;
}
if ($id === T_DNUMBER) {
$pos++;
return (float) $text;
}
if ($id === T_STRING) {
$lower = strtolower($text);
if ($lower === 'true' || $lower === 'false') {
$pos++;
return $lower === 'true';
}
if ($lower === 'null') {
$pos++;
return null;
}
}
if ($text === '-' && ($pos + 1) < $n && in_array($stream[$pos + 1][0], [T_LNUMBER, T_DNUMBER], true)) {
$num = $stream[$pos + 1];
$pos += 2;
return $num[0] === T_LNUMBER ? -(int) $num[1] : -(float) $num[1];
}
// Anything else (function call, expression, constant) — skip it unevaluated.
skipConfigValue($stream, $pos);
return MIGRATE_COMPLEX_VALUE;
}
/** Advances $pos past a value we won't evaluate, to the next top-level ',' or close. */
function skipConfigValue(array $stream, int &$pos): void {
$n = count($stream);
$depth = 0;
while ($pos < $n) {
$tok = $stream[$pos][1];
if ($tok === '[' || $tok === '(' || $tok === '{') {
$depth++;
} elseif ($tok === ']' || $tok === ')' || $tok === '}') {
if ($depth === 0) {
return;
}
$depth--;
} elseif ($tok === ',' && $depth === 0) {
return;
}
$pos++;
}
}
/** Decodes a single- or double-quoted PHP string literal token. */
function unquotePhpString(string $literal): string {
if (strlen($literal) < 2) {
return $literal;
}
$quote = $literal[0];
$inner = substr($literal, 1, -1);
if ($quote === "'") {
return str_replace(['\\\\', "\\'"], ['\\', "'"], $inner);
}
return stripcslashes($inner);
}
/**
* Reads the old log, which uses the same nine "<>" fields as souko.log, and
* re-hashes the deletion passwords — the old boards stored them in the clear,
* and password_verify() would reject every one of them.
*
* @return array<int,string[]> parsed entries, newest first
*/
function readOldLog(string $logFile, array &$messages): array {
if (!file_exists($logFile)) {
$messages[] = 'NOTE: no upload log found, the board will start empty.';
return [];
}
$entries = [];
$rehashed = 0;
$malformed = 0;
foreach (file($logFile, FILE_IGNORE_NEW_LINES) ?: [] as $line) {
if (trim($line) === '') continue;
$fields = explode('<>', $line);
if (count($fields) !== 9) {
$malformed++;
continue;
}
if ($fields[7] !== '' && !isHashed($fields[7])) {
$fields[7] = generatePasswordHash($fields[7]);
$rehashed++;
}
$entries[] = $fields;
}
if ($rehashed > 0) {
$messages[] = "re-hashed $rehashed plaintext deletion password(s).";
}
if ($malformed > 0) {
$messages[] = "WARNING: dropped $malformed malformed log line(s).";
}
return $entries;
}
function isHashed(string $password): bool {
return password_get_info($password)['algo'] !== null;
}
function hashOwnerPassword(string $oldPassword, array &$messages): string {
if ($oldPassword === '') {
$messages[] = 'WARNING: the old board had no admin password. Set one from the admin Boards page before handing it back to its owner.';
return generatePasswordHash(bin2hex(random_bytes(16)));
}
$messages[] = 'owner password carried over (the old one still works).';
return generatePasswordHash($oldPassword);
}
/**
* The old denylist and hardBanList both become plain board bans — the new
* per-board ban list is the only ban scope a board owner has.
*
* @return string[]
*/
function collectOldBans(array $oldConf): array {
$bans = [];
foreach (array_merge($oldConf['denylist'] ?? [], $oldConf['hardBanList'] ?? []) as $ip) {
$ip = trim((string) $ip);
// 0.0.0.0 was the placeholder the old config shipped with
if ($ip === '' || $ip === '0.0.0.0' || !filter_var($ip, FILTER_VALIDATE_IP)) {
continue;
}
$bans[$ip] = true;
}
return array_keys($bans);
}
/**
* The old board's theme, if the current instance still ships it. Anything else
* is dropped so a board can't name a stylesheet that isn't there.
*/
function installedTheme(string $themeName): string {
if ($themeName === '') {
return '';
}
$themeManager = new themeManager(ROOT_DIR . '/static/css/themes', 'static/css/themes');
return in_array($themeName, $themeManager->getThemeNames(), true) ? $themeName : '';
}
/**
* Points out old per-board settings the current layout has no place for, so
* whoever runs this knows what changed rather than finding out later.
*/
function reportDroppedSettings(array $oldConf, array $conf, array &$messages): void {
$dropped = [];
foreach (DROPPED_SETTINGS as $key) {
if (isset($oldConf[$key])) {
$dropped[] = $key;
}
}
if ($dropped) {
$messages[] = 'NOTE: these old settings have no equivalent and were not carried over: ' . implode(', ', $dropped) . '.';
}
// old limits were bytes, the current per-board limits are instance-wide and in megabytes
if (isset($oldConf['maxAmountOfFiles']) && (int) $oldConf['maxAmountOfFiles'] > (int) $conf['boardMaxAmountOfFiles']) {
$messages[] = 'NOTE: the old board allowed ' . (int) $oldConf['maxAmountOfFiles'] . ' files, boardMaxAmountOfFiles is ' . (int) $conf['boardMaxAmountOfFiles'] . '.';
}
}
function scaffold(string $boardDir, array $conf, array &$messages): bool {
$directories = [
$boardDir,
$boardDir . '/' . rtrim($conf['uploadDir'], '/'),
$boardDir . '/' . rtrim($conf['thumbDir'], '/'),
$boardDir . '/data',
];
foreach ($directories as $directory) {
if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
$messages[] = "FAILED: could not create $directory.";
return false;
}
}
file_put_contents($boardDir . '/data/.htaccess', "Order Deny,Allow\nDeny from all\n");
file_put_contents($boardDir . '/data/banned_hashes.dat', '');
$stub = "<?php\n"
. "// Generated board stub — hands the request to the main uploader script.\n"
. "\$boardUri = basename(__DIR__);\n"
. "require dirname(__DIR__, 2) . '/" . $conf['mainScript'] . "';\n";
return file_put_contents($boardDir . '/index.php', $stub) !== false;
}
/**
* Copies a directory's files, skipping any extension that isn't on the upload
* whitelist or that should be served as text — the target is web-served, so a
* stray .php/.svg in an old src/ must not carry over as executable/renderable.
*
* @return array{0:int,1:int} [copied, skipped]
*/
function copyDirContents(string $sourceDir, string $targetDir, array $allowedExtensions, array $blockedExtensions): array {
if (!is_dir($sourceDir)) {
return [0, 0];
}
$allowed = array_map('strtolower', $allowedExtensions);
$blocked = array_map('strtolower', $blockedExtensions);
$copied = 0;
$skipped = 0;
foreach (scandir($sourceDir) ?: [] as $entry) {
$source = $sourceDir . '/' . $entry;
if ($entry === '.' || $entry === '..' || !is_file($source)) {
continue;
}
$ext = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
if ($ext === '' || !in_array($ext, $allowed, true) || in_array($ext, $blocked, true)) {
$skipped++;
continue;
}
if (copy($source, $targetDir . '/' . $entry)) {
$copied++;
}
}
return [$copied, $skipped];
}
/**
* Copies the thumbnails an already migrated board is missing, using the
* prefix the registry gives it and the IDs from its own log. Nothing else
* about the board is touched.
*
* @return array{migrated:bool,messages:string[]}
*/
function copyMissingThumbnails(string $uri, string $oldConfigFile, array $conf, string $boardsRoot, boardRepository $boardRepository, bool $dryRun): array {
$board = $boardRepository->getByUri($uri);
$boardDir = $boardsRoot . $uri;
$logFile = $boardDir . '/data/' . $conf['logFile'];
if ($board === null || !is_dir($boardDir) || !file_exists($logFile)) {
return ['migrated' => false, 'messages' => ['SKIPPED: not migrated yet, run without --thumbnails first.']];
}
$oldConf = loadOldConfig($oldConfigFile);
if ($oldConf === null) {
return ['migrated' => false, 'messages' => ['SKIPPED: could not read the old config.php.']];
}
$entries = [];
foreach (file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [] as $line) {
$entries[] = explode('<>', $line);
}
$thumbs = copyThumbnails(
dirname($oldConfigFile) . '/' . ($oldConf['thumbDir'] ?? 'thmb/'),
$boardDir . '/' . rtrim($conf['thumbDir'], '/'),
$entries,
$board->getPrefix(),
$conf,
$dryRun
);
return ['migrated' => true, 'messages' => [($dryRun ? 'would copy ' : 'copied ') . "$thumbs missing thumbnail(s)."]];
}
/**
* Old thumbnails were "<prefix><id>_thumb.<source extension>" but always held
* JPEG data; the current scheme is "<prefix><id><thumb_suffix>.<thumbnailExtension>".
* Older boards left the prefix off the thumbnail, so that name is tried too.
* A thumbnail already in place is never overwritten.
*/
function copyThumbnails(string $sourceDir, string $targetDir, array $entries, string $prefix, array $conf, bool $dryRun = false): int {
if (!is_dir($sourceDir)) {
return 0;
}
$copied = 0;
foreach ($entries as $fields) {
$id = sprintf('%03d', (int) $fields[0]);
$matches = glob($sourceDir . '/' . $prefix . $id . '_thumb.*') ?: [];
if (!$matches && $prefix !== '') {
$matches = glob($sourceDir . '/' . $id . '_thumb.*') ?: [];
}
if (!$matches) {
continue;
}
$newName = $prefix . $id . $conf['thumb_suffix'] . '.' . $conf['thumbnailExtension'];
if (file_exists($targetDir . '/' . $newName)) {
continue;
}
if ($dryRun || copy($matches[0], $targetDir . '/' . $newName)) {
$copied++;
}
}
return $copied;
}
function writeLog(string $logFile, array $entries): void {
$lines = '';
foreach ($entries as $fields) {
$lines .= implode('<>', $fields) . "\n";
}
file_put_contents($logFile, $lines, LOCK_EX);
}
function highestID(array $entries): int {
$highest = 0;
foreach ($entries as $fields) {
$highest = max($highest, (int) $fields[0]);
}
return $highest;
}
function cleanText(string $value, int $maxLength): string {
$value = strip_tags(str_replace(["\r\n", "\r", "\n", "\t", "\0"], '', $value));
return trim(mb_substr($value, 0, $maxLength));
}
function parseOptions(array $argv): array {
$options = ['dry-run' => false, 'thumbnails' => false, 'source' => null];
foreach (array_slice($argv, 1) as $argument) {
if ($argument === '--dry-run') {
$options['dry-run'] = true;
} elseif ($argument === '--thumbnails') {
$options['thumbnails'] = true;
} elseif (str_starts_with($argument, '--source=')) {
$options['source'] = substr($argument, strlen('--source='));
} else {
exit("Unknown option: $argument\nUsage: php migrateUserBoards.php [--dry-run] [--thumbnails] [--source=path]\n");
}
}
return $options;
}