<?php
/**
* Script to update role checks across admin files
* This will update role checks to include director role alongside admin
*/
// Basic security check
session_start();
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
header('Location: login.php');
exit;
}
// Include necessary files
require_once 'includes/functions.php';
require_once '../config/database.php';
// Define patterns and replacements
$patterns = [
// Role check pattern 1 - Direct equality
'/if\s*\(\s*\$_SESSION\[\'role\'\]\s*==\s*\'admin\'\s*\)/' => 'if ($_SESSION[\'role\'] == \'admin\' || $_SESSION[\'role\'] == \'director\')',
// Role check pattern 2 - With isset
'/if\s*\(\s*isset\s*\(\s*\$_SESSION\[\'user_id\'\]\s*\)\s*&&\s*\$_SESSION\[\'role\'\]\s*==\s*\'admin\'\s*\)/' =>
'if (isset($_SESSION[\'user_id\']) && ($_SESSION[\'role\'] == \'admin\' || $_SESSION[\'role\'] == \'director\'))',
// Role check pattern 3 - Strict equality
'/if\s*\(\s*\$_SESSION\[\'role\'\]\s*===\s*\'admin\'\s*\)/' => 'if ($_SESSION[\'role\'] === \'admin\' || $_SESSION[\'role\'] === \'director\')',
// Role check pattern 4 - Not equal
'/if\s*\(\s*\$_SESSION\[\'role\'\]\s*!=\s*\'admin\'\s*\)/' => 'if ($_SESSION[\'role\'] != \'admin\' && $_SESSION[\'role\'] != \'director\')',
// Role check pattern 5 - Not strict equal
'/if\s*\(\s*\$_SESSION\[\'role\'\]\s*!==\s*\'admin\'\s*\)/' => 'if ($_SESSION[\'role\'] !== \'admin\' && $_SESSION[\'role\'] !== \'director\')',
// Role check pattern 6 - Complex with isset check
'/if\s*\(\s*!isset\s*\(\s*\$_SESSION\[\'user_id\'\]\s*\)\s*\|\|\s*\$_SESSION\[\'role\'\]\s*!==?\s*\'admin\'\s*\)/' =>
'if (!isset($_SESSION[\'user_id\']) || ($_SESSION[\'role\'] !== \'admin\' && $_SESSION[\'role\'] !== \'director\'))',
// Role check pattern 7 - Complex with isset for role
'/if\s*\(\s*!isset\s*\(\s*\$_SESSION\[\'role\'\]\s*\)\s*\|\|\s*\$_SESSION\[\'role\'\]\s*!==?\s*\'admin\'\s*\)/' =>
'if (!isset($_SESSION[\'role\']) || ($_SESSION[\'role\'] !== \'admin\' && $_SESSION[\'role\'] !== \'director\'))',
// Role check pattern 8 - Complex with multiple isset checks and != instead of !==
'/if\s*\(\s*!isset\s*\(\s*\$_SESSION\[\'user_id\'\]\s*\)\s*\|\|\s*!isset\s*\(\s*\$_SESSION\[\'role\'\]\s*\)\s*\|\|\s*\$_SESSION\[\'role\'\]\s*!=\s*\'admin\'\s*\)/' =>
'if (!isset($_SESSION[\'user_id\']) || !isset($_SESSION[\'role\']) || ($_SESSION[\'role\'] != \'admin\' && $_SESSION[\'role\'] != \'director\'))',
];
// Get directory path
$dir = __DIR__;
$updatedFiles = [];
$errorFiles = [];
// Process PHP files
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $file) {
// Skip directories and non-PHP files
if ($file->isDir() || $file->getExtension() !== 'php' || $file->getBasename() === 'update_director_privileges.php') {
continue;
}
$filePath = $file->getRealPath();
$content = file_get_contents($filePath);
$originalContent = $content;
// Apply replacements
foreach ($patterns as $pattern => $replacement) {
$content = preg_replace($pattern, $replacement, $content);
}
// Check if there were changes
if ($content !== $originalContent) {
// Try to update the file
try {
if (file_put_contents($filePath, $content)) {
$updatedFiles[] = str_replace($dir, '', $filePath);
} else {
$errorFiles[] = str_replace($dir, '', $filePath) . ' (write error)';
}
} catch (Exception $e) {
$errorFiles[] = str_replace($dir, '', $filePath) . ' (' . $e->getMessage() . ')';
}
}
}
// Display results
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Director Privileges</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<div class="card shadow">
<div class="card-header bg-primary text-white">
<h3 class="mb-0">Update Director Privileges</h3>
</div>
<div class="card-body">
<div class="alert alert-info">
<p><strong>Task completed!</strong></p>
<p>This script has updated admin files to grant director role the same privileges as admin role.</p>
</div>
<h5>Updated Files (<?php echo count($updatedFiles); ?>):</h5>
<?php if (!empty($updatedFiles)): ?>
<ul class="list-group mb-4">
<?php foreach ($updatedFiles as $file): ?>
<li class="list-group-item"><?php echo htmlspecialchars($file); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p class="text-muted">No files were updated.</p>
<?php endif; ?>
<?php if (!empty($errorFiles)): ?>
<h5>Error Files (<?php echo count($errorFiles); ?>):</h5>
<div class="alert alert-danger">
<ul class="mb-0">
<?php foreach ($errorFiles as $file): ?>
<li><?php echo htmlspecialchars($file); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<p>Remember to manually check any files that may have custom role checking logic.</p>
<div class="mt-4">
<a href="index.php" class="btn btn-primary">Return to Admin Dashboard</a>
</div>
</div>
</div>
</div>
</body>
</html>