Skip to content

Commit

Permalink
MDL-70896 mod: libxml_disable_entity_loader is deprecated in php 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Feb 15, 2021
1 parent 41037ef commit 1e8375d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
25 changes: 21 additions & 4 deletions mod/imscp/locallib.php
Expand Up @@ -117,11 +117,11 @@ function imscp_parse_structure($imscp, $context) {
*/
function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
$doc = new DOMDocument();
$oldentities = libxml_disable_entity_loader(true);
$oldentities = imscp_libxml_disable_entity_loader(true);
if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
return null;
}
libxml_disable_entity_loader($oldentities);
imscp_libxml_disable_entity_loader($oldentities);

// We put this fake URL as base in order to detect path changes caused by xml:base attributes.
$doc->documentURI = 'http://grrr/';
Expand Down Expand Up @@ -221,11 +221,11 @@ function imscp_recursive_href($manifestfilename, $imscp, $context) {
}

$doc = new DOMDocument();
$oldentities = libxml_disable_entity_loader(true);
$oldentities = imscp_libxml_disable_entity_loader(true);
if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
return null;
}
libxml_disable_entity_loader($oldentities);
imscp_libxml_disable_entity_loader($oldentities);

$xmlresources = $doc->getElementsByTagName('resource');
foreach ($xmlresources as $res) {
Expand Down Expand Up @@ -274,6 +274,23 @@ function imscp_recursive_item($xmlitem, $level, $resources) {
);
}

/**
* Wrapper for function libxml_disable_entity_loader() deprecated in PHP 8
*
* Method was deprecated in PHP 8 and it shows deprecation message. However it is still
* required in the previous versions on PHP. While Moodle supports both PHP 7 and 8 we need to keep it.
* @see https://php.watch/versions/8.0/libxml_disable_entity_loader-deprecation
*
* @param bool $value
* @return bool
*/
function imscp_libxml_disable_entity_loader(bool $value): bool {
if (PHP_VERSION_ID < 80000) {
return (bool)libxml_disable_entity_loader($value);
}
return true;
}

/**
* File browsing support class
*
Expand Down
21 changes: 19 additions & 2 deletions mod/lti/locallib.php
Expand Up @@ -4384,7 +4384,7 @@ function lti_load_cartridge($url, $map, $propertiesmap = array()) {

// TODO MDL-46023 Replace this code with a call to the new library.
$origerrors = libxml_use_internal_errors(true);
$origentity = libxml_disable_entity_loader(true);
$origentity = lti_libxml_disable_entity_loader(true);
libxml_clear_errors();

$document = new DOMDocument();
Expand All @@ -4396,7 +4396,7 @@ function lti_load_cartridge($url, $map, $propertiesmap = array()) {

libxml_clear_errors();
libxml_use_internal_errors($origerrors);
libxml_disable_entity_loader($origentity);
lti_libxml_disable_entity_loader($origentity);

if (count($errors) > 0) {
$message = 'Failed to load cartridge.';
Expand Down Expand Up @@ -4481,3 +4481,20 @@ function lti_new_access_token($typeid, $scopes) {

}


/**
* Wrapper for function libxml_disable_entity_loader() deprecated in PHP 8
*
* Method was deprecated in PHP 8 and it shows deprecation message. However it is still
* required in the previous versions on PHP. While Moodle supports both PHP 7 and 8 we need to keep it.
* @see https://php.watch/versions/8.0/libxml_disable_entity_loader-deprecation
*
* @param bool $value
* @return bool
*/
function lti_libxml_disable_entity_loader(bool $value): bool {
if (PHP_VERSION_ID < 80000) {
return (bool)libxml_disable_entity_loader($value);
}
return true;
}
6 changes: 3 additions & 3 deletions mod/lti/service.php
Expand Up @@ -70,13 +70,13 @@
}

// TODO MDL-46023 Replace this code with a call to the new library.
$origentity = libxml_disable_entity_loader(true);
$origentity = lti_libxml_disable_entity_loader(true);
$xml = simplexml_load_string($rawbody);
if (!$xml) {
libxml_disable_entity_loader($origentity);
lti_libxml_disable_entity_loader($origentity);
throw new Exception('Invalid XML content');
}
libxml_disable_entity_loader($origentity);
lti_libxml_disable_entity_loader($origentity);

$body = $xml->imsx_POXBody;
foreach ($body->children() as $child) {
Expand Down

0 comments on commit 1e8375d

Please sign in to comment.