1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?php /*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2025 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net, so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: André L F S Bacci <ae php.net> |
+----------------------------------------------------------------------+
# Description
This command line utility test if an file is valid standalone XML file,
accepting undefined entities references. If an directory is informed,
the test is applied in all .xml files in directory and sub directories.
This tool also cares for directories marked with .xmlfragmentdir, so
theses files are tested in relaxed semantics for XML Fragments. */
ini_set( 'display_errors' , 1 );
ini_set( 'display_startup_errors' , 1 );
error_reporting( E_ALL );
if ( count( $argv ) < 2 )
print_usage_exit( $argv[0] );
array_shift( $argv );
$dos2unix = false;
foreach( $argv as & $arg )
{
if ( $arg == "--dos2unix" )
{
$dos2unix = true;
$arg = null;
}
}
$argv = array_filter( $argv );
foreach( $argv as $arg )
{
if ( file_exists( $arg ) )
{
if ( is_file( $arg ) )
testFile( $arg );
if ( is_dir( $arg ) )
testDir( $arg );
continue;
}
echo "Path does not exist: $arg\n";
}
function print_usage_exit( $cmd )
{
fwrite( STDERR , " Wrong paramater count. Usage:\n" );
fwrite( STDERR , " {$cmd} [--dos2unix] path\n" );
exit;
}
function setup( string & $prefix , string & $suffix , string & $extra )
{
// Undefined entities generate TWO different error messages on libxml
// - "Entity '?' not defined" (for entity inside elements)
// - "Extra content at the end of the document" (entity outside elements)
$inside = "<x>&ZZZ;</x>";
$outside = "<x/>&ZZZ;";
$doc = new DOMDocument();
$doc->recover = true;
$doc->resolveExternals = false;
$doc->substituteEntities = false;
libxml_use_internal_errors( true );
$doc->loadXML( $inside );
$message = trim( libxml_get_errors()[0]->message );
$message = str_replace( "ZZZ" , "\f" , $message );
[ $prefix , $suffix ] = explode( "\f" , $message );
libxml_clear_errors();
$doc->loadXML( $outside );
$extra = trim( libxml_get_errors()[0]->message );
libxml_clear_errors();
}
function testFile( string $filename , bool $fragment = false )
{
$contents = file_get_contents( $filename );
if ( str_starts_with( $contents , b"\xEF\xBB\xBF" ) )
{
echo "Wrong XML file:\n";
echo " Issue: XML file with BOM. Several tools may misbehave.\n";
echo " Path: $filename\n";
echo " Hint: You can try autofix this with 'doc-base/scripts/broken.php --dos2unix langdir'.\n";
echo "\n";
autofix_dos2unix( $filename );
}
if ( PHP_EOL == "\n" && str_contains( $contents , "\r") )
{
echo "Wrong XML file:\n";
echo " Issue: XML file contains \\r. Several tools may misbehave.\n";
echo " Path: $filename\n";
echo " Hint: You can try autofix this with 'doc-base/scripts/broken.php --dos2unix langdir'.\n";
echo "\n";
autofix_dos2unix( $filename );
}
static $prefix = "", $suffix = "", $extra = "";
if ( $extra == "" )
setup( $prefix , $suffix , $extra );
$doc = new DOMDocument();
$doc->recover = true;
$doc->resolveExternals = false;
$doc->substituteEntities = false;
libxml_use_internal_errors( true );
if ( $fragment )
$contents = "<f>{$contents}</f>";
$doc->loadXML( $contents );
$errors = libxml_get_errors();
libxml_clear_errors();
foreach( $errors as $error )
{
$message = trim( $error->message );
$hintFragDir = false;
if ( str_starts_with( $message , $prefix ) && str_ends_with( $message , $suffix ) )
continue;
//if ( $message == $extra ) // Disabled as unnecessary. Also, this indicates that some
// continue; // some entity reference is used at an unusual position.
if ( $message == $extra )
$hintFragDir = true;
$lin = $error->line;
$col = $error->column;
echo "Broken XML file:\n";
echo " Issue: $message\n";
echo " Path: $filename [$lin,$col]\n";
if ( $hintFragDir )
echo " Hint: Copy the .xmlfragmentdir file, or check for misplaced entity references (outside any enclosing tag).\n";
echo "\n";
return;
}
}
function testDir( string $dir )
{
$dir = realpath( $dir );
$files = scandir( $dir );
$fragment = false;
$subdirs = [];
foreach( $files as $file )
{
if ( $file == ".xmlfragmentdir" )
{
$fragment = true;
continue;
}
if ( $file[0] == "." )
continue;
$fullpath = realpath( "$dir/$file" );
if ( is_dir ( $fullpath ) )
{
$subdirs[] = $fullpath;
continue;
}
if ( str_ends_with( $fullpath , ".xml" ) )
testFile( $fullpath , $fragment );
}
foreach( $subdirs as $dir )
testDir( $dir );
}
function autofix_dos2unix( string $filename )
{
if ( $GLOBALS['dos2unix'] )
{
$cmd = "dos2unix -r " . escapeshellarg( $filename );
system( $cmd );
echo "\n";
}
}
|