wp_kses_bad_protocol_once()
云策文档标注
概述
wp_kses_bad_protocol_once() 函数用于清理内容中的不良协议和其他字符,通过检查字符串开头的 URL 协议并处理空白和 HTML 实体来实现。
关键要点
- 函数用于内容安全过滤,防止恶意协议注入。
- 接受三个参数:$content(必需,要检查的内容)、$allowed_protocols(必需,允许的协议数组)、$count(可选,递归调用深度,默认值为1)。
- 返回清理后的字符串内容。
- 内部使用正则表达式和 wp_kses_bad_protocol_once2() 辅助函数进行协议验证。
- 支持递归处理,特别是针对 'feed:' 协议,以避免无限循环。
代码示例
function wp_kses_bad_protocol_once( $content, $allowed_protocols, $count = 1 ) {
$content = preg_replace( '/(&#*58(?![;0-9])|&#*3a(?![;a-f0-9]))/i', '$1;', $content );
$content2 = preg_split( '/:|&#*58;|&#*3a;|:/i', $content, 2 );
if ( isset( $content2[1] ) && ! preg_match( '%/?%', $content2[0] ) ) {
$content = trim( $content2[1] );
$protocol = wp_kses_bad_protocol_once2( $content2[0], $allowed_protocols );
if ( 'feed:' === $protocol ) {
if ( $count > 2 ) {
return '';
}
$content = wp_kses_bad_protocol_once( $content, $allowed_protocols, ++$count );
if ( empty( $content ) ) {
return $content;
}
}
$content = $protocol . $content;
}
return $content;
}注意事项
- 函数是 wp_kses_bad_protocol() 的辅助函数,用于单次协议清理。
- 在 WordPress 1.0.0 版本中引入,位于 wp-includes/kses.php 文件中。
- 使用时需确保 $allowed_protocols 参数正确设置,以定义允许的 URL 协议列表。
原文内容
Sanitizes content from bad protocols and other characters.
Description
This function searches for URL protocols at the beginning of the string, while handling whitespace and HTML entities.
Parameters
$contentstringrequired-
Content to check for bad protocols.
$allowed_protocolsstring[]required-
Array of allowed URL protocols.
$countintoptional-
Depth of call recursion to this function.
Default:
1
Source
function wp_kses_bad_protocol_once( $content, $allowed_protocols, $count = 1 ) {
$content = preg_replace( '/(�*58(?![;0-9])|�*3a(?![;a-f0-9]))/i', '$1;', $content );
$content2 = preg_split( '/:|�*58;|�*3a;|:/i', $content, 2 );
if ( isset( $content2[1] ) && ! preg_match( '%/?%', $content2[0] ) ) {
$content = trim( $content2[1] );
$protocol = wp_kses_bad_protocol_once2( $content2[0], $allowed_protocols );
if ( 'feed:' === $protocol ) {
if ( $count > 2 ) {
return '';
}
$content = wp_kses_bad_protocol_once( $content, $allowed_protocols, ++$count );
if ( empty( $content ) ) {
return $content;
}
}
$content = $protocol . $content;
}
return $content;
}
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |