Php 乔姆拉';JInput将HTML与每个过滤器分离
我试图在Joomla2.5中将HTML文本安全地保存到数据库中,所以我使用JInput获取表单数据 根据,有HTML筛选器: HTML-返回一个字符串,其中HTML实体和标记完好无损,以 过滤器中的白色或黑色列表 根据,有以下过滤器应(逻辑上,此处未解释)传递HTML标记: 原始、HTML、安全\u HTML 在JInput用于过滤的代码中,没有安全的HTML过滤器。我不知道它在一个文档中做了什么,为什么在另一个文档中缺少原始过滤器。除此之外,所有这些过滤器都会剥离HTML标记 仅使用$u POST:Php 乔姆拉';JInput将HTML与每个过滤器分离,php,joomla,joomla2.5,Php,Joomla,Joomla2.5,我试图在Joomla2.5中将HTML文本安全地保存到数据库中,所以我使用JInput获取表单数据 根据,有HTML筛选器: HTML-返回一个字符串,其中HTML实体和标记完好无损,以 过滤器中的白色或黑色列表 根据,有以下过滤器应(逻辑上,此处未解释)传递HTML标记: 原始、HTML、安全\u HTML 在JInput用于过滤的代码中,没有安全的HTML过滤器。我不知道它在一个文档中做了什么,为什么在另一个文档中缺少原始过滤器。除此之外,所有这些过滤器都会剥离HTML标记 仅使用$u PO
$_POST['shortDescription'];
返回
<b>Hello <i>world</i></b>
一切都归来了
Hello world
没有HTML标签。那是干什么用的?当我需要安全地存储HTML时,如何使用它?我使用以下方法:
public function getHtmlInput($htmlText)
{
$input_options = JFilterInput::getInstance(
array(
'img','p','a','u','i','b','strong','span','div','ul','li','ol','h1','h2','h3','h4','h5',
'table','tr','td','th','tbody','theader','tfooter','br'
),
array(
'src','width','height','alt','style','href','rel','target','align','valign','border','cellpading',
'cellspacing','title','id','class'
)
);
$postData = new JInput($_POST, array('filter' => $input_options));
return $postData->get($htmlText, '', 'HTML');
}
用法:
$this->getHtmlInput('documentation');
我希望这在Joomla 3中得到解决…这是一篇老文章,但我想我会投入2美分,因为这可能会帮助人们找到这篇文章,寻找解决方案 使用html编辑器,它仍然使用html过滤器剥离html。为了解决这个问题,我使用数组作为过滤器,然后对结果进行内爆 轻松呼吸。您应该这样做:
$jinput = JFactory::getApplication()->input;
$html = JComponentHelper::filterText($jinput->post->get('shortDescription', '', 'raw'));
(在Joomla 3.x的上下文中,JInputFilter
实例的默认配置是在白名单模式下运行,白名单标记和属性的空数组,也就是说,最严格的HTML过滤模式可以有效地去除所有内容
这显然不是开箱即用的,但它选择了安全性而不是方便性,让开发人员有意识地决定放松安全性,通过使用备用JInputFilter实例接受接收内容中的标记和属性,或者:
A) 带有指定的标签白名单(@Jon在自己的答案中最终做了什么)
或
B) 配置为在黑名单模式下运行
$filter = JInputFilter::getInstance([], [], 1, 1);
另外,除非禁用$xsauto选项(请参见下面的用法),否则Joomla将强制执行以下黑名单,而不管JInputFilter实例配置为哪种模式:
标记:“applet”、“body”、“bgsound”、“base”、“basefont”、“embed”、“frame”、“frameset”、“head”、“html”、“id”、“iframe”、“ilayer”、“layer”、“link”、“meta”、“name”、“object”、“script”、“style”、“title”、“xml”
属性:'action'、'background'、'codebase'、'dynsrc'、'lowsrc'
以下是JFilterInput::getInstance
方法的使用信息供参考:
/**
* Returns an input filter object, only creating it if it doesn't already exist.
*
* @param array $tagsArray List of user-defined tags
* @param array $attrArray List of user-defined attributes
* @param integer $tagsMethod WhiteList method = 0, BlackList method = 1
* @param integer $attrMethod WhiteList method = 0, BlackList method = 1
* @param integer $xssAuto Only auto clean essentials = 0, Allow clean blacklisted tags/attr = 1
* @param integer $stripUSC Strip 4-byte unicode characters = 1, no strip = 0, ask the database driver = -1
*
* @return JFilterInput The JFilterInput object.
*
* @since 11.1
*/
public static function &getInstance($tagsArray = array(), $attrArray = array(), $tagsMethod = 0, $attrMethod = 0, $xssAuto = 1, $stripUSC = -1)
Joomla还在管理界面的全局配置页面的文本过滤器选项卡上提供可配置的过滤规则。在这里,您可以配置操作模式,以及根据每个用户组筛选的标记和属性。要在您自己的代码中利用这一点,请按照@xavip的回答使用
jComponentThelper::filterText()
方法。我刚刚在Joomla中检查了这个!3.2.2而且“$my_var=$jinput->get('my_field'、'my default'、'html');”似乎仍然会错误地剥离html。我将深入研究Joomla代码,虽然这实际上是用这种方法绕过Joomla内置保护。要非常小心,不要相信使用HTML编辑器的人。否则,您将开放自己来托管各种恶意内容。@JohnRix是正确的,我将非常粗略地允许某人从面向前端的表单提交html。我的解决方案假设这是一个管理方面的事情,您知道它不会是恶意的。
$filter = JInputFilter::getInstance([], [], 1, 1);
/**
* Returns an input filter object, only creating it if it doesn't already exist.
*
* @param array $tagsArray List of user-defined tags
* @param array $attrArray List of user-defined attributes
* @param integer $tagsMethod WhiteList method = 0, BlackList method = 1
* @param integer $attrMethod WhiteList method = 0, BlackList method = 1
* @param integer $xssAuto Only auto clean essentials = 0, Allow clean blacklisted tags/attr = 1
* @param integer $stripUSC Strip 4-byte unicode characters = 1, no strip = 0, ask the database driver = -1
*
* @return JFilterInput The JFilterInput object.
*
* @since 11.1
*/
public static function &getInstance($tagsArray = array(), $attrArray = array(), $tagsMethod = 0, $attrMethod = 0, $xssAuto = 1, $stripUSC = -1)