简体   繁体   English

Joomla的JInput在每个过滤器上都会删除HTML

[英]Joomla's JInput strips HTML with every filter

I'm trying to save HTML text to database safely in Joomla 2.5, so I'm using JInput to get the form data. 我正在尝试在Joomla 2.5中安全地将HTML文本保存到数据库,所以我使用JInput来获取表单数据。

According to developer.joomla.org , there is HTML filter: 根据developer.joomla.org ,有HTML过滤器:

HTML - Returns a string with HTML entities and tags intact, subject to the white or black lists in the filter. HTML - 根据过滤器中的白名单或黑名单,返回包含HTML实体和标签的字符串。

According to docs.joomla.org , there are these filter which should (logically. They are not explained there) pass HTML tags: 根据docs.joomla.org ,有这些过滤器应该(逻辑上。它们没有在那里解释)传递HTML标签:

RAW, HTML, SAFE_HTML RAW,HTML,SAFE_HTML

At the code JFilterInput::clean which JInput uses for filtering, there is no SAFE_HTML filter. 在JInput用于过滤的代码JFilterInput :: clean中 ,没有SAFE_HTML过滤器。 I don't know what it is doing in one documentation and why RAW filter is missing in another. 我不知道它在一个文档中做了什么以及为什么另一个文件中缺少RAW过滤器。 Apart from that, all these filters strip HTML tags anyway. 除此之外,所有这些过滤器无论如何都会剥离HTML标签。

With just $_POST: 只需$ _POST:

$_POST['shortDescription'];

returns 回报

<b>Hello <i>world</i></b>

When I use JInput: 当我使用JInput时:

$input->get('shortDescription', '', 'RAW');
$input->get('shortDescription', '', 'HTML');
$input->get('shortDescription', '', 'SAFE_HTML');

all returns just 所有回报都是

Hello world

without HTML tags. 没有HTML标签。 What is it for then? 那是什么呢? How to use it when I need to store HTML safely? 当我需要安全地存储HTML时如何使用它?

I bypased it with this method: 我用这种方法做了它:

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');
}

Usage: 用法:

$this->getHtmlInput('documentation');

I hope this is solved in Joomla 3... 我希望在Joomla 3中解决这个问题......

You should do this: 你应该做这个:

$jinput = JFactory::getApplication()->input;
$html = JComponentHelper::filterText($jinput->post->get('shortDescription', '', 'raw'));

This is an old post but I figured I would throw my 2 cents in as it might help people finding this post searching for a solution. 这是一个老帖子,但我想我会投入2美分,因为它可能会帮助人们找到这篇文章寻找解决方案。

Using an html editor it still strips the html with using the HTML filter. 使用HTML编辑器,它仍然使用HTML过滤器剥离html。 To get around it I use ARRAY as the filter instead and then just implode the result. 为了解决它,我使用ARRAY作为过滤器,然后只是破坏结果。

Easy bo breazy. 容易bo breazy。

(In the context of Joomla 3.x) The default configuration of a JInputFilter instance is to operate in whitelisting mode, with empty arrays of whitelisted tags and attributes ie. (在Joomla 3.x的上下文中) JInputFilter实例的默认配置是在白名单模式下运行,带有空列的白名单标签和属性,即。 the most restrictive possible mode of HTML filtering that effectively gets rid of everything. HTML过滤最有限的可能模式,有效地摆脱了一切。

This clearly isn't that useful out of the box, but it is opting for security over convenience, and leaving it up to developers to make a conscious decision to relax the security to accept tags and attributes in the received content by using an alternate JInputFilter instance, either: 这显然不是那么有用,但它选择安全性而不是方便,并且让开发人员通过使用备用JInputFilter做出有意识的决定来放松安全性以接受接收内容中的标记和属性。例如,要么:

A) with a specified whitelist of tags (what @Jon ultimately did in his own answer) A)带有指定的标签白名单(@Jon最终在他自己的答案中做了什么)

$filter = JInputFilter::getInstance(array('img', ...), array('src', ...));

or 要么

B) configured to operate in blacklist mode B)配置为以黑名单模式运行

$filter = JInputFilter::getInstance([], [], 1, 1);

As an aside, unless you disable the $xssAuto option (see usage below), Joomla will enforce the following blacklists irrespective of which mode the JInputFilter instance is configured with: 顺便说一下,除非你禁用$ xssAuto选项(参见下面的用法),否则Joomla将强制执行以下黑名单,而不管JInputFilter实例配置的模式是什么:

Tags : 'applet', 'body', 'bgsound', 'base', 'basefont', 'embed', 'frame', 'frameset', 'head', 'html', 'id', 'iframe', 'ilayer', 'layer', 'link', 'meta', 'name', 'object', 'script', 'style', 'title', 'xml' 标签 :'applet','body','bgsound','base','basefont','embed','frame','frameset','head','html','id','iframe', 'ilayer','layer','link','meta','name','object','script','style','title','xml'

Attributes : 'action', 'background', 'codebase', 'dynsrc', 'lowsrc' 属性 :'action','background','codebase','dynsrc','lowsrc'

For reference, here is the usage information for the JFilterInput::getInstance method: 供参考,以下是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 also provides configurable filtering rules on the Text Filters tab of the Global Configuration page in the administration interface. Joomla还在管理界面的“全局配置”页面的“文本过滤器”选项卡上提供可配置的过滤规则。 Here, you can configure the mode of operation, as well as the tags and attributes to be filtered on a per user group basis. 在这里,您可以配置操作模式,以及基于每个用户组过滤的标签和属性。 To take advantage of this in your own code, use the JComponentHelper::filterText() method, per @xavip's answer. 要在您自己的代码中利用这一点,请根据@ xavip的答案使用JComponentHelper::filterText()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM