133 lines
3.8 KiB
PHP
133 lines
3.8 KiB
PHP
<?php
|
||
/**
|
||
* Fix S3 URLs - исправляет URL кодирование для файлов с русскими символами
|
||
*/
|
||
|
||
ini_set('memory_limit', '512M');
|
||
set_time_limit(0);
|
||
date_default_timezone_set('Europe/Moscow');
|
||
|
||
$ROOT = '/var/www/fastuser/data/www/crm.clientright.ru/';
|
||
require_once $ROOT . 'config.inc.php';
|
||
|
||
// CLI options
|
||
$opts = getopt('', [
|
||
'limit::',
|
||
'offset::',
|
||
'dry-run::'
|
||
]);
|
||
|
||
$limit = isset($opts['limit']) ? (int)$opts['limit'] : 100;
|
||
$offset = isset($opts['offset']) ? (int)$opts['offset'] : 0;
|
||
$dryRun = isset($opts['dry-run']) ? (int)$opts['dry-run'] !== 0 : true;
|
||
|
||
// Database connection
|
||
$mysqli = new mysqli($dbconfig['db_server'], $dbconfig['db_username'], $dbconfig['db_password'], $dbconfig['db_name']);
|
||
if ($mysqli->connect_error) {
|
||
die("Connection failed: " . $mysqli->connect_error);
|
||
}
|
||
$mysqli->set_charset("utf8");
|
||
|
||
// Logging
|
||
$logDir = $ROOT . 'logs';
|
||
if (!is_dir($logDir)) mkdir($logDir, 0755, true);
|
||
$logFile = $logDir . '/fix_s3_urls.log';
|
||
|
||
function logln($msg) {
|
||
global $logFile;
|
||
$line = '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL;
|
||
file_put_contents($logFile, $line, FILE_APPEND | LOCK_EX);
|
||
echo $line;
|
||
}
|
||
|
||
logln("Starting S3 URL fix");
|
||
logln("Parameters: limit=$limit, offset=$offset, dry-run=" . ($dryRun ? 'true' : 'false'));
|
||
|
||
// Find External files with potential URL encoding issues
|
||
$query = "SELECT notesid, filename, s3_key, s3_bucket
|
||
FROM vtiger_notes
|
||
WHERE filelocationtype = 'E'
|
||
AND s3_key IS NOT NULL
|
||
AND filename LIKE '%s3.twcstorage.ru%'
|
||
LIMIT $limit OFFSET $offset";
|
||
|
||
$result = $mysqli->query($query);
|
||
if (!$result) {
|
||
die("Query failed: " . $mysqli->error);
|
||
}
|
||
|
||
$records = [];
|
||
while ($row = $result->fetch_assoc()) {
|
||
$records[] = $row;
|
||
}
|
||
|
||
logln("Found " . count($records) . " records to fix (offset=$offset, limit=$limit)");
|
||
|
||
if (empty($records)) {
|
||
logln("No records to fix.");
|
||
exit(0);
|
||
}
|
||
|
||
$fixed = 0;
|
||
$errors = 0;
|
||
|
||
foreach ($records as $record) {
|
||
$notesid = $record['notesid'];
|
||
$currentFilename = $record['filename'];
|
||
$s3Key = $record['s3_key'];
|
||
$s3Bucket = $record['s3_bucket'];
|
||
|
||
// Properly encode the S3 key for URL
|
||
$encodedS3Key = implode('/', array_map('rawurlencode', explode('/', $s3Key)));
|
||
$newUrl = "https://s3.twcstorage.ru/$s3Bucket/$encodedS3Key";
|
||
|
||
// Check if URL needs fixing
|
||
if ($currentFilename === $newUrl) {
|
||
continue; // Already correct
|
||
}
|
||
|
||
logln("Processing notesid=$notesid");
|
||
logln(" Old URL: $currentFilename");
|
||
logln(" New URL: $newUrl");
|
||
|
||
if (!$dryRun) {
|
||
// Create backup
|
||
$backupDir = $ROOT . 'crm_extensions/file_storage/backups/';
|
||
if (!is_dir($backupDir)) mkdir($backupDir, 0755, true);
|
||
|
||
$backupFile = $backupDir . 'fix_url_backup_' . $notesid . '_' . date('Ymd_His') . '.json';
|
||
$backup = [
|
||
'notesid' => $notesid,
|
||
'original_filename' => $currentFilename,
|
||
'new_filename' => $newUrl,
|
||
'timestamp' => date('c')
|
||
];
|
||
file_put_contents($backupFile, json_encode($backup, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||
|
||
// Update database
|
||
$updateQuery = "UPDATE vtiger_notes SET filename = ? WHERE notesid = ?";
|
||
$stmt = $mysqli->prepare($updateQuery);
|
||
$stmt->bind_param('si', $newUrl, $notesid);
|
||
|
||
if ($stmt->execute()) {
|
||
logln("FIXED notesid=$notesid");
|
||
$fixed++;
|
||
} else {
|
||
logln("ERROR fixing notesid=$notesid: " . $stmt->error);
|
||
$errors++;
|
||
}
|
||
$stmt->close();
|
||
} else {
|
||
logln("DRY-RUN: Would fix notesid=$notesid");
|
||
$fixed++;
|
||
}
|
||
}
|
||
|
||
logln("S3 URL fix completed:");
|
||
logln("- Fixed: $fixed");
|
||
logln("- Errors: $errors");
|
||
logln("- Mode: " . ($dryRun ? "DRY-RUN" : "LIVE"));
|
||
|
||
$mysqli->close();
|
||
?>
|