views:

304

answers:

1

I'm trying to use the following code and it still strips out all the tags. Am I doing something wrong? I'm using the newest V1.10

$allowed_tags = array('img', 'object', 'param', 'embed', 'a', 'href', 'p', 'br', 'em', 'strong', 'li', 'ol', 'span');
$allowed_attributes = array('style', 'src', 'alt', 'href', 'width', 'height', 'value', 'name', 'type', 'embed', 'quality', 'pluginspage');
Zend_Loader::loadClass('Zend_Filter_StripTags');
$html_filter = new Zend_Filter_StripTags($allowed_tags, $allowed_attributes);

$post = $html_filter->filter($this->_request->getPost('post'));

For a test case I've been using the same string, this is what's going in

<p><span style="background-color: #333399; color: #ff9900; text-decoration: underline;"><em><strong>This is a test</strong></em></span></p>

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><sub><span style="background-color: #ffffff;"><span style="color: #000000;">asdf</span></span></sub></em></strong></span></p>

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;"><sup>asdf</sup></span></span></em></strong></span></p>

<p><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;"><img title="Cool" src="../../../public/scripts/tinymce/plugins/emotions/img/smiley-cool.gif" border="0" alt="Cool" />asdf</span></span></em></strong></span></p>

<ul>

<li><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;">sadf</span></span></em></strong></span></li>

</ul>

<ol>

<li><span style="background-color: #333399; color: #ff9900;"><strong><em><span style="background-color: #ffffff;"><span style="color: #000000;">asdf</span></span></em></strong></span></li>

</ol>

This is what comes out

This is a test

asdf

asdf

asdf

sadf

asdf


Alternatively, perhaps there's something else wrong, as I just tried this:

$post = strip_tags($this->_request->getPost('elm1'), '<img><object><param><embed><a><href><p><br><em><strong><li><ol><span>');

And it stripped out everything as well. Perhaps there is a setting in PHP that I'm missing?

A: 

According to the API Doc for the StripTag Filter, the constructor signature is

void   __construct  ([string|array|Zend_Config $options = null]) 

So it should work with

$html_filter = new Zend_Filter_StripTags(array(
  'allowTags' => $allowed_tags, 
  'allowAttribs' => $allowed_attributes
);

In earlier versions of Zend Framework (1.8.4) you had to do

$html_filter = new Zend_Filter_StripTags($allowed_tags, $allowed_attributes);

All versions should support:

$html_filter = new Zend_Filter_StripTags;
$html_filter->setAttributesAllowed($allowed_attributes);
$html_filter->setTagsAllowed($allowed_tags);

Internally, StripTags works with str_replace and preg_replace. So even if someone added strip_tags() to the list of disallowed functions in your php.ini, the filter should work.

I've tried with your example code and it worked.

Gordon
I had already tried your second way, I'll give the first a shot.
Jhorra
Neither of those work for me.
Jhorra
@Jhorra Can you be more specific about what happens when you do this? What is not working? Are you sure `$this->_request->getPost('post')` is a string?
Gordon
See my post above, couldn't use the comment box as it wouldn't hold all the text
Jhorra
@Jhorra can you add the code to your question instead of to an answer please? Helps prevent clutter.
Gordon
Sorry, it wouldn't take it all in the comment section.
Jhorra
@Jhorra are you sure you are on 1.10? Can you do a `var_dump(Zend_Version::VERSION);` please?
Gordon
I'm giving you credit for the assist, it is some kind of setting on my test set up. I uploaded it to the live server and it filtered just like it should.
Jhorra
@Jhorra do you have error_reporting(-1) enabled on your test system and ZF set to display errors? Maybe there is someone in the error logs.
Gordon
I did end up using your first example for the constructor as I believe it did change in 1.10 to that format instead of what I was originally using.
Jhorra
@Jhorra have a look at the sourcecode for 1.10. There is a fallback in the constructor, so you can use the old instantiation as well. I have no clue why it's not working for you though.
Gordon
Something with my dev box, on the live server it works fine.
Jhorra