1--TEST-- 2libxml_set_external_entity_loader() variation: resolve externals and entities 3--SKIPIF-- 4<?php if (!extension_loaded('dom')) die('skip'); ?> 5--FILE-- 6<?php 7chdir(__DIR__); 8$xml = <<<XML 9<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar"> 10<foo>bar&fooz;</foo> 11XML; 12 13$dtd = <<<DTD 14<!ELEMENT foo (#PCDATA)> 15<!ENTITY % fooentity PUBLIC 16 "-//FOO/ENTITY" 17 "fooentity.ent"> 18%fooentity; 19DTD; 20 21$entity = <<<ENT 22<!ENTITY fooz "baz"> 23ENT; 24 25libxml_set_external_entity_loader( 26 function ($public, $system, $context) use($dtd,$entity){ 27 static $first = true; 28 var_dump($public); 29 var_dump($system); 30 var_dump($context); 31 $f = fopen("php://temp", "r+"); 32 fwrite($f, $first ? $dtd : $entity); 33 $first = false; 34 rewind($f); 35 return $f; 36 } 37); 38 39$dd = new DOMDocument; 40$dd->substituteEntities = true; 41$dd->resolveExternals = true; 42$r = $dd->loadXML($xml); 43var_dump($dd->validate()); 44 45echo "Done.\n"; 46--EXPECTF-- 47string(10) "-//FOO/BAR" 48string(25) "http://example.com/foobar" 49array(4) { 50 ["directory"]=> 51 string(%d) "%s" 52 ["intSubName"]=> 53 string(3) "foo" 54 ["extSubURI"]=> 55 string(25) "http://example.com/foobar" 56 ["extSubSystem"]=> 57 string(10) "-//FOO/BAR" 58} 59string(13) "-//FOO/ENTITY" 60string(32) "http://example.com/fooentity.ent" 61array(4) { 62 ["directory"]=> 63 string(%d) "%s" 64 ["intSubName"]=> 65 string(3) "foo" 66 ["extSubURI"]=> 67 string(25) "http://example.com/foobar" 68 ["extSubSystem"]=> 69 string(10) "-//FOO/BAR" 70} 71bool(true) 72Done. 73