<?php /* http://coding.binon.net/dhtml/cr_test.php - (c) cybaer@binon.net - 081219 */

 
define('XHTML',TRUE);
 
define('XHTML_W3C_NOTICE',TRUE);
 include(
'basiclib.php');

 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 
$sMarkupFile=setext($_SERVER['SCRIPT_FILENAME'],'xhtml');
 
$sParser=get_request_data('parser','GET');
 
$bShowW3CNotice=strbool(get_request_data('w3c','GET',NULL,(XHTML_W3C_NOTICE)?'on':'off'));
 
$bShowSource=strbool(get_request_data('source','GET'));

 
// ---------------------------------------------------------------------------

 // At first: ignore some really stupid idiots and their bots
 
if(is_url($sParser)) {
  
header($_SERVER['SERVER_PROTOCOL'].' 503 Service Unavailable'); 
  
header('Connection: close');
  die();
 }

 if(!
is_file($sMarkupFile)) {
  
header('Content-Type: text/html');
  echo 
'<title>Fehler</title>Dateifehler: '.htmlentities(substr($sMarkupFile,strlen(HTTP_ROOT))).' wurde nicht gefunden!';
 } else {

  
// ---------------------------------------------------------------------------

  
if($bShowSource) {

   
header('Content-Type: text/html;charset=iso-8859-1');

   switch(
$sParser) {
    case 
'xhtml'  highlight_string(getMarkup($sMarkupFile,$bShowW3CNotice)); die();
    case 
'(x)html'highlight_string(downgradeXHTMLFile($sMarkupFile,$bShowW3CNotice)); die();
    case 
'html'   highlight_string(convertXHTMLFile($sMarkupFile,$bShowW3CNotice)); die();
    default       : 
highlight_file($_SERVER['SCRIPT_FILENAME']); die();
   }
  }

  
// ---------------------------------------------------------------------------

  
header('Vary: Accept');

  
// ---------------------------------------------------------------------------

  
if(($sParser && $sParser=='xhtml') || (!$sParser && instr(@ifsetor($_SERVER['HTTP_ACCEPT']),'application/xhtml+xml'))) {

   
header('Content-Type: application/xhtml+xml;charset=iso-8859-1');
   echo 
getMarkup($sMarkupFile,$bShowW3CNotice);

  } else if(
$sParser=='(x)html') {

   
header('Content-Type: text/html;charset=iso-8859-1');
   echo 
downgradeXHTMLFile($sMarkupFile,$bShowW3CNotice);

  } else if(
$sParser!='none') {

   
header('Content-Type: text/html;charset=iso-8859-1');
   echo 
convertXHTMLFile($sMarkupFile,$bShowW3CNotice);

  } else {

   
header('Content-Type: text/plain; charset=iso-8859-1');
   echo 
html_entity_decode(strip_tags(str_replace('<strong>XHTML 1.1</strong>',
                                                  
'Text (PHP strip_tags & html_entity_decode)',
                                                  
getMarkup($sMarkupFile,$bShowW3CNotice))),ENT_QUOTES,'ISO-8859-1');

  }

  
// ---------------------------------------------------------------------------

 
}

 
// ===========================================================================

 
function downgradeXHTMLFile($sXHTMLFile,$bAddW3CNotice=FALSE) {
  return 
str_replace(array(
                           
'<strong id="w3cNotationParsingType">XHTML 1.1</strong>',
                           
'DTD XHTML 1.1',
                           
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
                   
),array(
                           
'<strong id="w3cNotationParsingType">HTML</strong> (<a href="http://www.w3.org/MarkUp/2004/xhtml-faq#texthtml">backwards compatible XHTML 1.0</a>)',
                           
'DTD XHTML 1.0 Strict',
                           
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
                          
),getMarkup($sXHTMLFile,$bAddW3CNotice));
 }

 
// ---------------------------------------------------------------------------

 
function convertXHTMLFile($sXHTMLFile,$bAddW3CNotice=FALSE) {
  return 
xhtml2html(getMarkup($sXHTMLFile,$bAddW3CNotice),
                                      
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
                                      array(
                                            
'<strong id="w3cNotationParsingType">XHTML 1.1</strong>'
                                          
=>'<strong id="w3cNotationParsingType">HTML 4.01 strict</strong>'
                                            
));
 }

 
// ---------------------------------------------------------------------------

 
function getMarkup($sFile,$bAddNotice=XHTML_W3C_NOTICE) {
  if(
$bAddNotice && XHTML_W3C_NOTICE!==FALSE) {
   
$sNotice='
<table id="w3cNotation" border="0"><tbody><tr>
 <td>
  <a href="http://validator.w3.org/check?uri=http%3A%2F%2F{ph:ServerName}{ph:HttpScriptFileEsc}%3Fparser=html%26w3c=off&amp;ss=1"><img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Strict" height="31" width="88" /></a><br />
  Erzwinge Parser:<br />
  <a href="{ph:ScriptName}?parser=xhtml" title="Valides XHTML 1.1 / XML-Parser">XHTML 1.1</a><br />
  <a href="{ph:ScriptName}?parser=%28x%29html" title="Valides XHTML 1.0 / HTML-Parser">(X)HTML</a><br />
  <a href="{ph:ScriptName}?parser=html" title="Valides HTML 4.01 Strict / HTML-Parser">HTML 4.01</a><br />
  <a href="{ph:ScriptName}?parser=none" rel="nofollow" title="Markup wird entfernt / Text">Nur Text</a><br />
  Zeige Quelltext:<br />
  <a href="{ph:ScriptName}?parser=xhtml&amp;source=on&amp;w3c=off" rel="nofollow" title="XHTML-1.1-Quelltext (unver&auml;ndert)">XHTML 1.1</a><br />
  <a href="{ph:ScriptName}?parser=%28x%29html&amp;source=on&amp;w3c=off" rel="nofollow" title="XHTML-1.0-strict-Quelltext">(X)HTML</a><br />
  <a href="{ph:ScriptName}?parser=html&amp;source=on&amp;w3c=off" rel="nofollow" title="HTML-4.01-Quelltext">HTML 4.01</a><br />
  <a href="{ph:ScriptName}?source=on" title="Quelltext des PHP-Scripts">PHP-Script</a><br />
 </td><td>
  <p>Zu Testzwecken wurde dieses Dokument in <a href="{ph:HttpMarkupFile}" rel="nofollow" title="Original-Dokument XHTML 1.1 codiert (f&uuml;r XML-Parser)">XHTML 1.1</a>
  codiert!</p>

  <p>An Clients, die den XHTML-Mime-Type <code>application/xhtml+xml</code>
  im Accept-Header senden (z.B. Firefox, Safari, Opera &gt;=7),
  wird das Dokument im Original (als "echtes" XHTML) mittels eines
  <a href="{ph:ScriptName}?source=on">PHP-Scripts</a> durchgereicht, und
  vom XML-Parser des Browsers verarbeitet. Andernfalls (z.B. IE &lt;9)
  werden die XHTML-Elemente entfernt, ein HTML-Doctype erg&auml;nzt,
  das Dokument als HTML gesendet (Mime-Type <code>text/html</code>),
  und vom HTML-Parser des Browser verarbeitet.</p>
 </td>
 <td><p>This document has been coded in <a href="{ph:HttpMarkupFile}" rel="nofollow" title="Original document coded in XHTML 1.1 for XML parsers">XHTML 1.1</a>
  for test purposes!</p>

  <p>To clients sending the XHTML mime-type accept-header <code>application/xhtml+xml</code>
  (i.e. Firefox, Safari, Opera &gt;=7), it will be passed through a
  <a href="{ph:ScriptName}?source=on">PHP script</a> as it is (as "real"
  XHTML) and parsed by the XML parser of the browser. Otherwise (i.e.
  IE &lt;9) the XHTML elements will be removed, a HTML doctype will be
  added, the document will be sent as HTML (mime-type <code>text/html</code>)
  to be parsed by the HTML
  parser of the browser.</p>
 </td><td>
  <a href="http://validator.w3.org/check?uri=http%3A%2F%2F{ph:ServerName}{ph:HttpMarkupFileEsc}&amp;ss=1"><img src="http://www.w3.org/Icons/valid-xhtml11-blue" alt="Valid XHTML 1.1" height="31" width="88" /></a><br />
  Force parser:<br />
  <a href="{ph:ScriptName}?parser=xhtml" title="Valid XHTML 1.1 / XML parser">XHTML 1.1</a><br />
  <a href="{ph:ScriptName}?parser=%28x%29html" title="Valid XHTML 1.0 strict / HTML parser">(X)HTML</a><br />
  <a href="{ph:ScriptName}?parser=html" title="Valid HTML 4.01 strict / HTML parser">HTML 4.01</a><br />
  <a href="{ph:ScriptName}?parser=none" rel="nofollow" title="Removing markup / text">Text only</a><br />
  Show source:<br />
  <a href="{ph:ScriptName}?parser=xhtml&amp;source=on&amp;w3c=off" rel="nofollow" title="XHTML 1.1 source (unchanged)">XHTML 1.1</a><br />
  <a href="{ph:ScriptName}?parser=%28x%29html&amp;source=on&amp;w3c=off" rel="nofollow" title="XHTML 1.0 strict source">(X)HTML</a><br />
  <a href="{ph:ScriptName}?parser=html&amp;source=on&amp;w3c=off" rel="nofollow" title="HTML 4.01 strict source">HTML 4.01</a><br />
  <a href="{ph:ScriptName}?source=on" title="Source of PHP script">PHP script</a><br />
 </td>
</tr><tr>
 <td colspan="4">
  Dieses Dokument wurde von ihrem Browser verarbeitet als/This document was parsed by your browser as:<br />
  <strong id="w3cNotationParsingType">XHTML 1.1</strong> (<a href="http://validator.w3.org/check?uri=referer&amp;ss=1">W3C-Validator</a>)
 </td>
</tr></tbody></table>
'
;
  
$sNotice=fill_template($sNotice,array(
                                        
'ServerName'=>$_SERVER['SERVER_NAME'],
                                        
'ScriptName'=>realBasename(array_value(parse_url(SCRIPT_URI),'path')),
                                        
'HttpScriptFileEsc'=>urlencode(array_value(parse_url(SCRIPT_URI),'path')),
                                        
'HttpMarkupFile'=>substr($sFile,strlen(HTTP_ROOT)),
                                        
'HttpMarkupFileEsc'=>urlencode(substr($sFile,strlen(HTTP_ROOT)))
                                       ),
'{ph:*}');
  return 
str_replace('</body>',$sNotice.'</body>',get_contents($sFile));
 } else {
  return 
get_contents($sFile);
 }
}

?>