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
47--EXPECTF--
48string(10) "-//FOO/BAR"
49string(25) "http://example.com/foobar"
50array(4) {
51  ["directory"]=>
52  string(%d) "%s"
53  ["intSubName"]=>
54  string(3) "foo"
55  ["extSubURI"]=>
56  string(25) "http://example.com/foobar"
57  ["extSubSystem"]=>
58  string(10) "-//FOO/BAR"
59}
60string(13) "-//FOO/ENTITY"
61string(32) "http://example.com/fooentity.ent"
62array(4) {
63  ["directory"]=>
64  string(%d) "%s"
65  ["intSubName"]=>
66  string(3) "foo"
67  ["extSubURI"]=>
68  string(25) "http://example.com/foobar"
69  ["extSubSystem"]=>
70  string(10) "-//FOO/BAR"
71}
72bool(true)
73Done.
74