05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

What you do with a processing instruction is up to you. One trick is to embed <strong>PHP</strong><br />

code in an XML document and, as you parse that document, execute the <strong>PHP</strong> code<br />

with the eval( ) function. Example 11-5 does just that. Of course, you have to trust<br />

the documents you’re processing if you eval( ) code in them. eval( ) will run any<br />

code given to it—even code that destroys files or mails passwords to a hacker.<br />

Example 11-5. Processing instruction handler<br />

function processing_instruction($inParser, $inTarget, $inCode) {<br />

if ($inTarget === 'php') {<br />

eval($inCode);<br />

}<br />

}<br />

Entity Handlers<br />

Entities in XML are placeholders. XML provides five standard entities (&amp;, &gt;,<br />

&lt;, &quot;, and &apos;), but XML documents can define their own entities. Most<br />

entity definitions do not trigger events, and the XML parser expands most entities in<br />

documents before calling the other handlers.<br />

Two types of entities, external and unparsed, have special support in <strong>PHP</strong>’s XML<br />

library. An external entity is one whose replacement text is identified by a filename<br />

or URL rather than explicitly given in the XML file. You can define a handler to be<br />

called for occurrences of external entities in character data, but it’s up to you to parse<br />

the contents of the file or URL yourself if that’s what you want.<br />

An unparsed entity must be accompanied by a notation declaration, and while you<br />

can define handlers for declarations of unparsed entities and notations, occurrences<br />

of unparsed entities are deleted from the text before the character data handler is<br />

called.<br />

External entities<br />

External entity references allow XML documents to include other XML documents.<br />

Typically, an external entity reference handler opens the referenced file, parses the<br />

file, and includes the results in the current document. Set the handler with xml_set_<br />

external_entity_ref_handler( ), which takes in a reference to the XML parser and<br />

the name of the handler function:<br />

xml_set_external_entity_ref_handler(parser, handler);<br />

The external entity reference handler takes five parameters: the parser triggering the<br />

handler, the entity’s name, the base URI for resolving the identifier of the entity<br />

(which is currently always empty), the system identifier (such as the filename), and<br />

the public identifier for the entity, as defined in the entity’s declaration:<br />

$ok = my_ext_entity_handler(parser, entity, base, system, public);<br />

268 | Chapter 11: XML<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!