Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text should be escaped as it's being parsed into XML files #972

Closed
c00 opened this issue Jan 3, 2017 · 5 comments
Closed

Text should be escaped as it's being parsed into XML files #972

c00 opened this issue Jan 3, 2017 · 5 comments

Comments

@c00
Copy link

c00 commented Jan 3, 2017

In at least the Section::addText() function, data is not being escaped properly. Adding text that would be considered invalid in XML will result in a corrupt Word output file.

In the following example the unescaped ampersand is causing the corruption.

$word = new PhpWord();
$section = $word->addSection();

//This will cause a corrupt docx file.
$section->addText("This is bad & not good.");

//This will work fine
$section->addText(htmlentities("This is alright & good.", ENT_XML1));

//This also works.
$section->addText("This is okay & tedious.");

$objWriter = IOFactory::createWriter($word, 'Word2007');
$objWriter->save("C:\\foo\\bar.docx");

Opening the resulting file will result in a parsing error.

The solution would be to auto-escape all text added through addText() functions. Possibly other functions as well

@ldurfee
Copy link

ldurfee commented Jan 4, 2017

I believe I am having this same issue.

@ldurfee
Copy link

ldurfee commented Jan 4, 2017

I can confirm that this is the issue I am having. I wrapped the data in htmlentities and it then would open in Word.

@c00
Copy link
Author

c00 commented Jan 5, 2017

Great, that means I'm not alone. I couldn't easily find the place in the code where to change this, I hope one of the contributors can do that!

@alder
Copy link

alder commented Feb 24, 2017

Confirm - I am having the same issue with setValue() in TemplateProcessor. Temporary solution - wrap string data in htmlentities().

@troosan
Copy link
Contributor

troosan commented Jun 27, 2017

If you first call

Settings::setOutputEscapingEnabled(true);

The xml entities will get escaped.
The default value for this is (unfortunately) false for backward compatibility reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants