PHP highlight XML source code

Source: Internet
Author: User
It explains how external entities direct to the processor to include and parse other documents, how PIs is handled, and how to determine the credibility of the code containing PIs. XML documents (xmltest. xml and xmltest2.xml) that can be used by this example are listed after this example. External entity example? Php $ filexmltest. xml; functiontrustedFile ($ fil

It explains how external entities direct to the processor to include and parse other documents, how PIs is handled, and how to determine the credibility of the code containing PIs.

XML documents (xmltest. xml and xmltest2.xml) that can be used by this example are listed after this example.

External entity example

$ File = "xmltest. xml ";

Function trustedFile ($ file ){
// Only trust local files owned by ourselves
If (! Eregi ("^ ([a-z]): //", $ file)
& Fileowner ($ file) = getmyuid ()){
Return true;
}
Return false;
}

Function startElement ($ parser, $ name, $ attribs ){
Print "<$ name ";
If (sizeof ($ attribs )){
While (list ($ k, $ v) = each ($ attribs )){
Print "$ k = \" color = \ "#990000 \"> $ v \"";
}
}
Print "> ";
}

Function endElement ($ parser, $ name ){
Print" ";
}

Function characterData ($ parser, $ data ){
Print"$ Data";
}

Function PIHandler ($ parser, $ target, $ data ){
Switch (strtolower ($ target )){
Case "php ":
Global $ parser_file;
// If the parsed document is "trusted", we say it is safe
// To execute PHP code inside it. If not, display the code
// Instead.
If (trustedFile ($ parser_file [$ parser]) {
Eval ($ data );
} Else {
Printf ("Untrusted PHP code:% S",
Htmlspecialchars ($ data ));
}
Break;
}
}

Function defaultHandler ($ parser, $ data ){
If (substr ($ data, 0, 1) = "&" & substr ($ data,-1, 1) = ";"){
Printf ('% s ',
Htmlspecialchars ($ data ));
} Else {
Printf ('% s ',
Htmlspecialchars ($ data ));
}
}

Function externalEntityRefHandler ($ parser, $ openEntityNames, $ base, $ systemId,
$ PublicId ){
If ($ systemId ){
If (! List ($ parser, $ fp) = new_xml_parser ($ systemId )){
Printf ("cocould not open entity % s at % s \ n", $ openEntityNames,
$ SystemId );
Return false;
}
While ($ data = fread ($ fp, 4096 )){
If (! Xml_parse ($ parser, $ data, feof ($ fp ))){
Printf ("XML error: % s at line % d while parsing entity % s \ n ",
Xml_error_string (xml_get_error_code ($ parser )),
Xml_get_current_line_number ($ parser), $ openEntityNames );
Xml_parser_free ($ parser );
Return false;
}
}
Xml_parser_free ($ parser );
Return true;
}
Return false;
}

Function new_xml_parser ($ file ){
Global $ parser_file;

$ Xml_parser = xml_parser_create ();
Xml_parser_set_option ($ xml_parser, XML_OPTION_CASE_FOLDING, 1 );
Xml_set_element_handler ($ xml_parser, "startElement", "endElement ");
Xml_set_character_data_handler ($ xml_parser, "characterData ");
Xml_set_processing_instruction_handler ($ xml_parser, "PIHandler ");
Xml_set_default_handler ($ xml_parser, "defaultHandler ");
Xml_set_external_entity_ref_handler ($ xml_parser, "externalEntityRefHandler ");

If (! ($ Fp = @ fopen ($ file, "r "))){
Return false;
}
If (! Is_array ($ parser_file )){
Settype ($ parser_file, "array ");
}
$ Parser_file [$ xml_parser] = $ file;
Return array ($ xml_parser, $ fp );
}

If (! (List ($ xml_parser, $ fp) = new_xml_parser ($ file ))){
Die ("cocould not open XML input ");
}

Print"

";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
print "
";
Print "parse complete \ n ";
Xml_parser_free ($ xml_parser );
?>

Xmltest. xml




]>

Title & plainEntity;




A1 B1 C1
A2 C2
A3 B3 C3




& SystemEntity;


About this Document





The following documents will be called by the xmltest. xml file: xmltest2.xml



]>


& TestEnt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.