Sindbad~EG File Manager
<?php
require_once 'config/config.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
die('Access denied. Please <a href="login.php">login</a> first.');
}
$db = Database::getInstance()->getConnection();
$success = [];
$errors = [];
$recreatedCodes = [];
// Include the code generation functions from codes.php
function generateTrackingCode($type = 'member') {
$prefix = $type === 'member' ? 'MEM' : 'USR';
return $prefix . date('Y') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
}
function generateBarcode($code) {
if (!extension_loaded('gd')) {
return generateBarcodeSVG($code);
}
try {
$width = 200;
$height = 50;
$image = imagecreate($width, $height);
if (!$image) {
return generateBarcodeSVG($code);
}
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
$codeLength = strlen($code);
$x = 10;
for ($i = 0; $i < $codeLength && $x < 180; $i++) {
$char = ord($code[$i]);
$pattern = $char % 4;
for ($j = 0; $j < 3 && $x < 180; $j++) {
$barWidth = ($pattern & (1 << $j)) ? 3 : 1;
imagefilledrectangle($image, $x, 5, $x + $barWidth, 35, $black);
$x += $barWidth + 1;
}
}
imagestring($image, 2, 10, 37, substr($code, 0, 20), $black);
ob_start();
imagepng($image);
$imageData = ob_get_contents();
ob_end_clean();
imagedestroy($image);
if ($imageData) {
return "data:image/png;base64," . base64_encode($imageData);
} else {
return generateBarcodeSVG($code);
}
} catch (Exception $e) {
return generateBarcodeSVG($code);
}
}
function generateBarcodeSVG($code) {
$svg = '<?xml version="1.0" encoding="UTF-8"?>
<svg width="200" height="50" xmlns="http://www.w3.org/2000/svg">
<rect width="200" height="50" fill="white"/>
<g fill="black">';
$x = 10;
$codeLength = strlen($code);
for ($i = 0; $i < $codeLength && $x < 180; $i++) {
$char = ord($code[$i]);
$pattern = $char % 4;
for ($j = 0; $j < 3 && $x < 180; $j++) {
$barWidth = ($pattern & (1 << $j)) ? 3 : 1;
$svg .= '<rect x="' . $x . '" y="5" width="' . $barWidth . '" height="30"/>';
$x += $barWidth + 1;
}
}
$svg .= '</g>
<text x="100" y="45" text-anchor="middle" font-family="monospace" font-size="8" fill="black">' . htmlspecialchars(substr($code, 0, 20)) . '</text>
</svg>';
return "data:image/svg+xml;base64," . base64_encode($svg);
}
function generateQRCode($code) {
if (!extension_loaded('gd')) {
return generateQRCodeSVG($code);
}
try {
$size = 100;
$image = imagecreate($size, $size);
if (!$image) {
return generateQRCodeSVG($code);
}
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
$codeHash = md5($code);
$blockSize = 5;
imagefilledrectangle($image, 5, 5, 30, 30, $black);
imagefilledrectangle($image, 70, 5, 95, 30, $black);
imagefilledrectangle($image, 5, 70, 30, 95, $black);
imagefilledrectangle($image, 10, 10, 25, 25, $white);
imagefilledrectangle($image, 75, 10, 90, 25, $white);
imagefilledrectangle($image, 10, 75, 25, 90, $white);
imagefilledrectangle($image, 15, 15, 20, 20, $black);
imagefilledrectangle($image, 80, 15, 85, 20, $black);
imagefilledrectangle($image, 15, 80, 20, 85, $black);
for ($i = 0; $i < 32; $i++) {
$hexChar = hexdec($codeHash[$i]);
for ($bit = 0; $bit < 4; $bit++) {
if ($hexChar & (1 << $bit)) {
$x = 35 + (($i * 2 + $bit) % 6) * $blockSize;
$y = 35 + floor(($i * 2 + $bit) / 6) * $blockSize;
if ($x < 95 && $y < 95) {
imagefilledrectangle($image, $x, $y, $x + $blockSize - 1, $y + $blockSize - 1, $black);
}
}
}
}
ob_start();
imagepng($image);
$imageData = ob_get_contents();
ob_end_clean();
imagedestroy($image);
if ($imageData) {
return "data:image/png;base64," . base64_encode($imageData);
} else {
return generateQRCodeSVG($code);
}
} catch (Exception $e) {
return generateQRCodeSVG($code);
}
}
function generateQRCodeSVG($code) {
$codeHash = md5($code);
$svg = '<?xml version="1.0" encoding="UTF-8"?>
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="white"/>
<g fill="black">
<rect x="5" y="5" width="25" height="25"/>
<rect x="70" y="5" width="25" height="25"/>
<rect x="5" y="70" width="25" height="25"/>
<rect x="10" y="10" width="15" height="15" fill="white"/>
<rect x="75" y="10" width="15" height="15" fill="white"/>
<rect x="10" y="75" width="15" height="15" fill="white"/>
<rect x="15" y="15" width="5" height="5" fill="black"/>
<rect x="80" y="15" width="5" height="5" fill="black"/>
<rect x="15" y="80" width="5" height="5" fill="black"/>';
$blockSize = 5;
for ($i = 0; $i < 32; $i++) {
$hexChar = hexdec($codeHash[$i]);
for ($bit = 0; $bit < 4; $bit++) {
if ($hexChar & (1 << $bit)) {
$x = 35 + (($i * 2 + $bit) % 6) * $blockSize;
$y = 35 + floor(($i * 2 + $bit) / 6) * $blockSize;
if ($x < 95 && $y < 95) {
$svg .= '<rect x="' . $x . '" y="' . $y . '" width="' . $blockSize . '" height="' . $blockSize . '"/>';
}
}
}
}
$svg .= '</g></svg>';
return "data:image/svg+xml;base64," . base64_encode($svg);
}
echo "<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Regenerate All Member Codes</title>
<script src='https://cdn.tailwindcss.com'></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'>
</head>
<body class='bg-gray-100'>
<div class='container mx-auto px-4 py-8 max-w-6xl'>
<div class='bg-white rounded-lg shadow-lg p-8'>
<h1 class='text-3xl font-bold text-gray-800 mb-6'>
<i class='fas fa-sync-alt'></i> Regenerate All Member Codes
</h1>";
try {
// Get all existing codes
$stmt = $db->query("SELECT * FROM memberuser_codes ORDER BY code_type, created_at");
$existingCodes = $stmt->fetchAll(PDO::FETCH_ASSOC);
$memberCodesCount = 0;
$userCodesCount = 0;
foreach ($existingCodes as $oldCode) {
if ($oldCode['code_type'] === 'member') {
$memberCodesCount++;
} else {
$userCodesCount++;
}
}
echo "<div class='mb-6 p-4 bg-blue-100 border-l-4 border-blue-500'>
<p class='text-blue-800'>Found <strong>$memberCodesCount</strong> member codes and <strong>$userCodesCount</strong> user codes.</p>
<p class='text-blue-800 mt-2'>Regenerating all codes with proper barcode and QR code data...</p>
</div>";
$db->beginTransaction();
// Process each existing code
foreach ($existingCodes as $oldCode) {
try {
// Generate new tracking code
do {
$newTrackingCode = generateTrackingCode($oldCode['code_type']);
$checkStmt = $db->prepare("SELECT id FROM memberuser_codes WHERE tracking_code = :code");
$checkStmt->execute(['code' => $newTrackingCode]);
} while ($checkStmt->fetch());
// Generate new barcode and QR code
$newBarcode = generateBarcode($newTrackingCode);
$newQrcode = generateQRCode($newTrackingCode);
// Delete old code
$deleteStmt = $db->prepare("DELETE FROM memberuser_codes WHERE id = :id");
$deleteStmt->execute(['id' => $oldCode['id']]);
// Insert new code with same details but new tracking code and images
$insertStmt = $db->prepare("
INSERT INTO memberuser_codes (
code, description, member_id, user_id, event_id, code_type, tracking_code,
barcode, qrcode, created_by, expires_at, max_usage, is_active, usage_count
) VALUES (
:code, :description, :member_id, :user_id, :event_id, :code_type, :tracking_code,
:barcode, :qrcode, :created_by, :expires_at, :max_usage, :is_active, :usage_count
)
");
$insertStmt->execute([
'code' => $oldCode['code'],
'description' => $oldCode['description'],
'member_id' => $oldCode['member_id'],
'user_id' => $oldCode['user_id'],
'event_id' => $oldCode['event_id'],
'code_type' => $oldCode['code_type'],
'tracking_code' => $newTrackingCode,
'barcode' => $newBarcode,
'qrcode' => $newQrcode,
'created_by' => $oldCode['created_by'],
'expires_at' => $oldCode['expires_at'],
'max_usage' => $oldCode['max_usage'],
'is_active' => $oldCode['is_active'],
'usage_count' => $oldCode['usage_count']
]);
$recreatedCodes[] = [
'old_tracking' => $oldCode['tracking_code'],
'new_tracking' => $newTrackingCode,
'type' => $oldCode['code_type'],
'barcode_length' => strlen($newBarcode),
'qrcode_length' => strlen($newQrcode)
];
} catch (Exception $e) {
$errors[] = "Failed to regenerate code " . $oldCode['tracking_code'] . ": " . $e->getMessage();
}
}
$db->commit();
echo "<div class='mb-6 p-4 bg-green-100 border-l-4 border-green-500'>
<h3 class='font-bold text-green-800 mb-2'><i class='fas fa-check-circle'></i> Success!</h3>
<p class='text-green-800'>Successfully regenerated <strong>" . count($recreatedCodes) . "</strong> codes.</p>
</div>";
// Show summary table
if (!empty($recreatedCodes)) {
echo "<div class='mb-6'>
<h2 class='text-xl font-bold mb-4'>Regenerated Codes Summary:</h2>
<div class='overflow-x-auto'>
<table class='w-full border-collapse border border-gray-300'>
<thead class='bg-gray-100'>
<tr>
<th class='border border-gray-300 px-4 py-2'>Old Tracking Code</th>
<th class='border border-gray-300 px-4 py-2'>New Tracking Code</th>
<th class='border border-gray-300 px-4 py-2'>Type</th>
<th class='border border-gray-300 px-4 py-2'>Barcode Size</th>
<th class='border border-gray-300 px-4 py-2'>QR Code Size</th>
</tr>
</thead>
<tbody>";
foreach ($recreatedCodes as $code) {
$typeColor = $code['type'] === 'member' ? 'text-blue-600' : 'text-purple-600';
$barcodeStatus = $code['barcode_length'] > 255 ? 'text-green-600 font-bold' : 'text-red-600';
$qrcodeStatus = $code['qrcode_length'] > 255 ? 'text-green-600 font-bold' : 'text-red-600';
echo "<tr>
<td class='border border-gray-300 px-4 py-2 font-mono text-sm'>" . htmlspecialchars($code['old_tracking']) . "</td>
<td class='border border-gray-300 px-4 py-2 font-mono text-sm font-bold'>" . htmlspecialchars($code['new_tracking']) . "</td>
<td class='border border-gray-300 px-4 py-2 $typeColor'>" . ucfirst($code['type']) . "</td>
<td class='border border-gray-300 px-4 py-2 $barcodeStatus'>" . number_format($code['barcode_length']) . " chars</td>
<td class='border border-gray-300 px-4 py-2 $qrcodeStatus'>" . number_format($code['qrcode_length']) . " chars</td>
</tr>";
}
echo "</tbody></table></div></div>";
}
} catch (Exception $e) {
if ($db->inTransaction()) {
$db->rollBack();
}
echo "<div class='p-4 bg-red-100 border-l-4 border-red-500'>
<h3 class='font-bold text-red-800 mb-2'><i class='fas fa-exclamation-circle'></i> Error!</h3>
<p class='text-red-800'>" . htmlspecialchars($e->getMessage()) . "</p>
</div>";
}
if (!empty($errors)) {
echo "<div class='mt-4 p-4 bg-red-100 border-l-4 border-red-500'>
<h3 class='font-bold text-red-800 mb-2'>Errors:</h3>
<ul class='list-disc list-inside text-red-800'>";
foreach ($errors as $error) {
echo "<li>" . htmlspecialchars($error) . "</li>";
}
echo "</ul></div>";
}
echo "<div class='mt-6 p-4 bg-yellow-100 border-l-4 border-yellow-500'>
<h3 class='font-bold text-yellow-800 mb-2'><i class='fas fa-info-circle'></i> Important Notes:</h3>
<ul class='list-disc list-inside text-yellow-800'>
<li>All codes have been assigned <strong>new tracking codes</strong></li>
<li>Barcode and QR code images are now <strong>full size</strong> (not truncated)</li>
<li>All other properties (description, expiry, usage count) have been preserved</li>
<li>You can now view any code and see proper barcode/QR images</li>
</ul>
</div>";
echo "<div class='mt-6 flex gap-4'>
<a href='modules/membership/codes.php' class='px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition'>
<i class='fas fa-qrcode mr-2'></i>View Member Codes
</a>
<a href='dashboard.php' class='px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition'>
<i class='fas fa-home mr-2'></i>Back to Dashboard
</a>
</div>";
echo "</div></div>
</body>
</html>";
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists