39669-vm/patch_center_subjects.py
2026-04-16 16:39:04 +00:00

43 lines
2.4 KiB
Python

import re
with open('center_subjects.php', 'r', encoding='utf-8') as f:
content = f.read()
# Protect POST update action
post_handler = r"if \(\$_SERVER\['REQUEST_METHOD'\] === 'POST' && isset\(\$_POST\['action'\]\) && \$_POST\['action'\] === 'update_subjects'\) \{"
post_handler_new = r"""if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_subjects') {
if (!is_super_admin()) {
$errors['form'] = 'عذراً، تعديل المواد متاح للمشرف العام فقط.';
} else {"""
content = content.replace(post_handler, post_handler_new)
# Find the try/catch block to close the else
try_block = r""" update_application_subjects\(\(int\) \$application\['id'\], \$selected_subjects_ids\);.*?\} catch \(Throwable \$e\) \{.*?\}"""
try_block_new = r""" update_application_subjects((int) $application['id'], $selected_subjects_ids);
set_flash('success', 'تم تحديث المواد الدراسية للمركز بنجاح.');
$selectedCycleIdStr = $selectedCycleId > 0 ? '&cycle=' . $selectedCycleId : '';
header('Location: center_subjects.php?id=' . $application['id'] . $selectedCycleIdStr);
exit;
} catch (Throwable $e) {
$errors['form'] = 'تعذر حفظ البيانات. يرجى المحاولة لاحقاً.';
}
}"""
content = re.sub(r" update_application_subjects\(\(int\) \$application\['id'\], \$selected_subjects_ids\);.*?\} catch \(Throwable \$e\) \{.*?\}", try_block_new, content, flags=re.DOTALL)
# Disable checkboxes and hide button if not super admin
checkbox = r'<input class="form-check-input ms-0 me-2 mt-1 float-end" type="checkbox" name="subjects\[\]"'
checkbox_new = r'<input class="form-check-input ms-0 me-2 mt-1 float-end" type="checkbox" name="subjects[]" <?= is_super_admin() ? "" : "disabled" ?>'
content = re.sub(checkbox, checkbox_new, content)
btn = r'<button type="submit" class="btn btn-dark px-4">حفظ المواد الدراسية</button>'
btn_new = r"""<?php if (is_super_admin()): ?>
<button type="submit" class="btn btn-dark px-4">حفظ المواد الدراسية</button>
<?php else: ?>
<div class="alert alert-warning mb-0">يمكن للمشرف العام فقط تعديل هذه القائمة.</div>
<?php endif; ?>"""
content = content.replace(btn, btn_new)
with open('center_subjects.php', 'w', encoding='utf-8') as f:
f.write(content)