Files
MES/core/helper.php
T
2026-08-08 18:28:49 +08:00

27 lines
763 B
PHP

<?php
/**
* MES2 全局辅助函数
* 这些函数在全局命名空间中定义,可在视图和控制器中直接使用
*/
/**
* 安全输出函数(XSS 防护)
* 用法: <?= h($var) ?> 或 <?= h($var, '默认值') ?>
*/
function h($str, $default = '')
{
return htmlspecialchars((string)($str ?? $default), ENT_QUOTES, 'UTF-8');
}
/**
* 获取当前请求的 CSP nonce
* 由 index.php 在每次请求时生成并存入 $GLOBALS['csp_nonce']。
* 视图中为内联 <script>/<style> 或外部脚本打上 nonce 标记,
* 配合 index.php 中 CSP 头的 'nonce-***' 策略,可逐步淘汰 'unsafe-inline'。
* 用法:<script nonce="<?= csp_nonce() ?>"> ... </script>
*/
function csp_nonce()
{
return $GLOBALS['csp_nonce'] ?? '';
}