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

Dealing with invalid xml tag ("<br>" without closing) in vml comments #1181

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Formula Parser: Wrong line count for stuff like "MyOtherSheet!A:D" [#1215](https://github.com/PHPOffice/PhpSpreadsheet/issues/1215)
- Call garbage collector after removing a column to prevent stale cached values
- Trying to remove a column that doesn't exist deletes the latest column
- Deal with VML-Comments containing an new-line written by Excel 2013
- Keep big integer as integer instead of lossely casting to float [#874](https://github.com/PHPOffice/PhpSpreadsheet/pull/874)
- Fix branch pruning handling of non boolean conditions [#1167](https://github.com/PHPOffice/PhpSpreadsheet/pull/1167)
- Fix ODS Reader when no DC namespace are defined [#1182](https://github.com/PHPOffice/PhpSpreadsheet/pull/1182)
Expand Down
58 changes: 56 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Reader;

use DOMDocument;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
use PhpOffice\PhpSpreadsheet\NamedRange;
Expand Down Expand Up @@ -900,8 +901,9 @@ public function load($pFilename)
$relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath);

try {
$vmlCommentsFile = simplexml_load_string(
$this->securityScanner->scan($this->getFromZipArchive($zip, $relPath)),
$secureXml = $this->securityScanner->scan($this->getFromZipArchive($zip, $relPath));
$vmlCommentsFile = $this->simplexml_load_string_of_html_too(
$secureXml,
'SimpleXMLElement',
Settings::getLibXmlLoaderOptions()
);
Expand Down Expand Up @@ -2037,11 +2039,63 @@ private function getWorkbookBaseName(ZipArchive $zip)
$workbookBasename = $basename;
}


break;
}
}
}

return $workbookBasename;
}


/**
* Tries to load a string as XML and HTML.
* @throws \Exception
*/
protected function simplexml_load_string_of_html_too($string, $class_name = "SimpleXMLElement", $options = 0, $ns = "", $is_prefix = false) {

try {
$xml = $this->simplexml_load_string($string, $class_name, $options, $ns, $is_prefix);
} catch (\Exception $e) {
$dom = new DOMDocument;
$rc = $dom->loadHTML($string, $options);
if(false === $rc) {
throw $e;
}

$xml = simplexml_import_dom($dom, $class_name);
}

return $xml;
}

/**
* @param $string
* @param $class_name
* @param $options
* @param $ns
* @param $is_prefix
* @return SimpleXMLElement
* @throws \Exception
*/
protected function simplexml_load_string($string, $class_name, $options, $ns, $is_prefix): SimpleXMLElement
{
libxml_clear_errors();

$previous = libxml_use_internal_errors(true);
$xml = simplexml_load_string($string, $class_name, $options, $ns, $is_prefix);
libxml_use_internal_errors($previous);

if (false === $xml) {
$message = '';
foreach (libxml_get_errors() as $error) {
$trimmedMsg = trim($error->message);
$message .= "$trimmedMsg on line: $error->line, column: $error->column.\n";
}
libxml_clear_errors();
throw new \Exception($message);
}
return $xml;
}
}