yii2数据库输出问题
Problem with output from the database yii2
我在数据库中有我想要输出的文件的路径。
喜欢:
<audio src="/yii2-biblioteca/frontend/web/uploads/audio/lya1.mp3" controls type="audio/mpeg">
我正在使用:
<?=HtmlPurifier::process($model->audio)?>
用于输出。
我对图像使用了同样的东西,没关系,它可以工作,但对于音频和 pdf 嵌入就没那么多了。
在 pdf 开始工作时,我用 js 功能更改了一些东西,这不会产生负面影响。我都改回原来好的时候了,现在不行了
pdf 示例:<embed src="/yii2-biblioteca/frontend/web/uploads/pdf/dying.pdf" type="application/pdf" width="100%" height="100%" />
Yii2 的 HTMLPurifier 包装器需要 a second argument:
echo HtmlPurifier::process($html, [
// options go here
]);
对于 <embed>
,您应该可以使用 HTML.SafeEmbed 设置:
echo HtmlPurifier::process($html, [
'HTML.SafeEmbed' => true,
]);
不幸的是,对于 <audio>
,这里的根本问题是 HTML Purifier 不支持 HTML5,这将使添加变得更加复杂。
有 user-supplied patches to allow HTML Purifier to understand HTML5,但据我所知,none 已经过审核,因此很难说这会对您网站的安全造成什么影响。 (可以说,HTML 带有用户空间提供的 HTML5 定义的净化器仍然比没有 HTML 净化器要好。)
我已经给出了一些关于如何让 HTML Purifier(库本身,而不是它的 Yii2 包装器)只知道 another question 上的 <audio>
标签的粗略说明。引用相关文章:
You'll have to look at the "Customize!" end-user documentation, where it will tell you how to add tags and attributes that HTML Purifier is not aware of.
To quote the most vivid code example from the linked documentation
(this code teaches HTML Purifier about the <form>
tag):
Time for some code:
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
$config->set('HTML.DefinitionRev', 1);
$config->set('Cache.DefinitionImpl', null); // remove this later!
$def = $config->getHTMLDefinition(true);
[...]
$form = $def->addElement(
'form', // name
'Block', // content set
'Flow', // allowed children
'Common', // attribute collection
array( // attributes
'action*' => 'URI',
'method' => 'Enum#get|post',
'name' => 'ID'
)
);
$form->excludes = array('form' => true);
Each of the parameters corresponds to one of the questions we asked. Notice that we added an asterisk to the end of the action attribute to
indicate that it is required. If someone specifies a form without that
attribute, the tag will be axed. Also, the extra line at the end is a
special extra declaration that prevents forms from being nested within
each other.
按照这些说明进行净化程序后
知道 <audio>
,将标签 <audio>
添加到您的配置中
白名单将起作用。
因此,简而言之,如果您希望能够仅净化 <audio>
个标签而不完全丢失它们,您将不得不对标签的功能进行一些研究并将信息添加到HTML净化器。
如果您不想从头开始编写代码,您可以根据在 xemlock/htmlpurifier-html5's HTML5Definition.php
file 中找到的内容编写代码。
我在数据库中有我想要输出的文件的路径。 喜欢:
<audio src="/yii2-biblioteca/frontend/web/uploads/audio/lya1.mp3" controls type="audio/mpeg">
我正在使用:
<?=HtmlPurifier::process($model->audio)?>
用于输出。
我对图像使用了同样的东西,没关系,它可以工作,但对于音频和 pdf 嵌入就没那么多了。 在 pdf 开始工作时,我用 js 功能更改了一些东西,这不会产生负面影响。我都改回原来好的时候了,现在不行了
pdf 示例:<embed src="/yii2-biblioteca/frontend/web/uploads/pdf/dying.pdf" type="application/pdf" width="100%" height="100%" />
Yii2 的 HTMLPurifier 包装器需要 a second argument:
echo HtmlPurifier::process($html, [
// options go here
]);
对于 <embed>
,您应该可以使用 HTML.SafeEmbed 设置:
echo HtmlPurifier::process($html, [
'HTML.SafeEmbed' => true,
]);
不幸的是,对于 <audio>
,这里的根本问题是 HTML Purifier 不支持 HTML5,这将使添加变得更加复杂。
有 user-supplied patches to allow HTML Purifier to understand HTML5,但据我所知,none 已经过审核,因此很难说这会对您网站的安全造成什么影响。 (可以说,HTML 带有用户空间提供的 HTML5 定义的净化器仍然比没有 HTML 净化器要好。)
我已经给出了一些关于如何让 HTML Purifier(库本身,而不是它的 Yii2 包装器)只知道 another question 上的 <audio>
标签的粗略说明。引用相关文章:
You'll have to look at the "Customize!" end-user documentation, where it will tell you how to add tags and attributes that HTML Purifier is not aware of.
To quote the most vivid code example from the linked documentation (this code teaches HTML Purifier about the
<form>
tag):Time for some code:
$config = HTMLPurifier_Config::createDefault(); $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial'); $config->set('HTML.DefinitionRev', 1); $config->set('Cache.DefinitionImpl', null); // remove this later! $def = $config->getHTMLDefinition(true); [...] $form = $def->addElement( 'form', // name 'Block', // content set 'Flow', // allowed children 'Common', // attribute collection array( // attributes 'action*' => 'URI', 'method' => 'Enum#get|post', 'name' => 'ID' ) ); $form->excludes = array('form' => true);
Each of the parameters corresponds to one of the questions we asked. Notice that we added an asterisk to the end of the action attribute to indicate that it is required. If someone specifies a form without that attribute, the tag will be axed. Also, the extra line at the end is a special extra declaration that prevents forms from being nested within each other.
按照这些说明进行净化程序后 知道
<audio>
,将标签<audio>
添加到您的配置中 白名单将起作用。
因此,简而言之,如果您希望能够仅净化 <audio>
个标签而不完全丢失它们,您将不得不对标签的功能进行一些研究并将信息添加到HTML净化器。
如果您不想从头开始编写代码,您可以根据在 xemlock/htmlpurifier-html5's HTML5Definition.php
file 中找到的内容编写代码。