<?php
// xmllint_validation.php
// RJM Programming
// June, 2024
 $vsnone='none';
 $prefn='Please enter either HTML or XML file to validate ...';
 $pth='';
 $rn='' .rand(1,78477554);
 $results='';
 $precontents='';
 $contents='';
 $fn='';
 if (!isset($_GET['htmlfile']) && isset($_GET['content'])) {
   $_GET['htmlfile']=$_GET['content'];
 }
 if (!isset($_POST['htmlfile']) && isset($_POST['content'])) {
   $_POST['htmlfile']=$_POST['content'];
 } 
 if (isset($_GET['htmlfile'])) {
   if (substr(trim(urldecode($_GET['htmlfile'])) . ' ', 0, 1) == '<') {
   $contents=trim(str_replace('+',' ',urldecode($_GET['htmlfile'])));
   if (strpos(strtolower(explode('>', $contents)[0]), 'html') !== false) {
   $fn='/tmp/html_' . $rn . '.html';
   } else if (strpos(strtolower(explode('>', $contents)[0]), 'xml') !== false) {
   $fn='/tmp/xml_' . $rn . '.xml';
   } 
   file_put_contents($fn, $contents);
   } else {
   $fn=str_replace('+',' ',urldecode($_GET['htmlfile']));
   }
   if ($fn != '') {
   if (strpos($fn, 'localhost') !== false) {
     $rhs=explode('localhost' . explode('/', explode('localhost', $fn)[1])[0], $fn)[1];
     $fn=$_SERVER['DOCUMENT_ROOT'] . $rhs;
     $pth='/usr/bin/';
   } else if (strpos($fn, 'rjmprogramming.com.au') !== false) {
     $rhs=explode('rjmprogramming.com.au' . explode('/', explode('rjmprogramming.com.au', $fn)[1])[0], $fn)[1];
     $fn=$_SERVER['DOCUMENT_ROOT'] . $rhs;
   } else if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { 
     $pth='/usr/bin/';
   } 
   }
   if (file_exists($fn)) {
   if ($contents == '') {
     $contents=file_get_contents($fn);
   }
   $prefn=$fn;
   $fn=realpath($fn);
 if (strpos(strtolower($fn), '.xml') !== false) {
   $vsnone='block';
   $results=shell_exec($pth . 'xmllint --valid --noout ' . $fn . ' 2> ' . ' ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   if (file_exists(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err')) {
     $results.="\n";
     $results.=file_get_contents(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
     unlink(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   }
 } else if (strpos(strtolower($fn), '.htm') !== false) {
   $vsnone='block';
   $results=shell_exec($pth . 'xmllint --html --valid --noout ' . $fn . ' 2> ' . ' ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   if (file_exists(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err')) {
     $results.="\n";
     $results.=file_get_contents(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
     unlink(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   }
 }
   if (strpos($fn, '/tmp/') !== false) { unlink($fn);  }
   } else {
   $contents='';
   if (strpos($fn, '/tmp/') !== false) { unlink($fn);  }
   }
 } else if (isset($_POST['htmlfile'])) {
   if (substr(trim(urldecode($_POST['htmlfile'])) . ' ', 0, 1) == '<') {
   $contents=trim(str_replace('+',' ',urldecode($_POST['htmlfile'])));
   if (strpos(strtolower(explode('>', $contents)[0]), 'html') !== false) {
   $fn='/tmp/html_' . $rn . '.html';
   } else if (strpos(strtolower(explode('>', $contents)[0]), 'xml') !== false) {
   $fn='/tmp/xml_' . $rn . '.xml';
   } 
   file_put_contents($fn, $contents);
   } else {
   $fn=str_replace('+',' ',urldecode($_POST['htmlfile']));
   }
   if ($fn != '') {
   if (strpos($fn, 'localhost') !== false) {
     $rhs=explode('localhost' . explode('/', explode('localhost', $fn)[1])[0], $fn)[1];
     $fn=$_SERVER['DOCUMENT_ROOT'] . $rhs;
     $pth='/usr/bin/';
   } else if (strpos($fn, 'rjmprogramming.com.au') !== false) {
     $rhs=explode('rjmprogramming.com.au' . explode('/', explode('rjmprogramming.com.au', $fn)[1])[0], $fn)[1];
     $fn=$_SERVER['DOCUMENT_ROOT'] . $rhs;
   } else if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) { 
     $pth='/usr/bin/';
   }
   }
   if (file_exists($fn)) {
   $prefn=$fn;
   if ($contents == '') {
     $contents=file_get_contents($fn);
   }
   $fn=realpath($fn);
 if (strpos(strtolower($fn), '.xml') !== false) {
   $vsnone='block';
   $results=shell_exec($pth . 'xmllint --valid --noout ' . $fn . ' 2> ' . ' ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   if (file_exists(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err')) {
     $results.="\n";
     $results.=file_get_contents(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
     unlink(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   }
 } else if (strpos(strtolower($fn), '.htm') !== false) {
   $vsnone='block';
   $results=shell_exec($pth . 'xmllint --html --valid --noout ' . $fn . ' 2> ' . ' ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   if (file_exists(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err')) {
     $results.="\n";
     $results.=file_get_contents(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
     //file_put_contents('xxx.xxx', $results);
     unlink(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'xmllint.err');
   }
 }
   if (strpos($fn, '/tmp/') !== false) { unlink($fn);  }
   } else {
   $contents='';
   if (strpos($fn, '/tmp/') !== false) { unlink($fn);  }
   }
 }
 if ($contents != '') {
   $lines=explode("\n", $contents);
   for ($ii=1; $ii<=sizeof($lines); $ii++) {
     $precontents.='' . $ii . "\n";
   }
 }
?>
<html>
<head>
<title>XML Lint Usage - RJM Programming - June, 2024</title>
<meta charset='UTF-8'/>
<script type='text/javascript'>
// /home/virtfs/rjmprogr/usr/local/cpanel/3rdparty/bin/xmllint --html --valid --noout 
var beforebih=''; //(iois.srcdoc.toLowerCase().indexOf('<body') == -1 ? iois.srcdoc : '' + iois.srcdoc.replace('<BODY','<body').replace('</BODY>','</body>').split('<body')[1].split('</body>')[0].split('</body>')[0].replace(iois.srcdoc.replace('<BODY','<body').replace('</BODY>','</body>').split('<body')[1].split('</body>')[0].split('>')[0] + '>',''));
var afterbih='';
var s1=null, s2=null;

// Thanks to https://stackoverflow.com/questions/7108270/scrolling-2-different-elements-in-same-time
function select_scroll_1(e) { s2.scrollTop = s1.scrollTop; }
function select_scroll_2(e) { s1.scrollTop = s2.scrollTop; }

function onl() {
const sanitizer = new Sanitizer();  // Default sanitizer;

// Get the frame and its Document object
const frame_element = document.getElementById("userFrame");
const unsanitized_frame_tree = frame_element.contentWindow.document;
console.log(unsanitized_frame_tree);
console.log(frame_element.contentWindow.document.body.innerHTML.length);

// Sanitize the document tree and update the frame.
const sanitized_frame_tree = sanitizer.sanitize(unsanitized_frame_tree);
console.log(sanitized_frame_tree);
frame_element.replaceChildren(sanitized_frame_tree);
console.log(frame_element.contentWindow.document.body.innerHTML.length);
}

function ifixit(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto != null) {
     if (aconto.document) { aconto = aconto.document; }
     if (aconto.body != null) {
const unsanitized_string = (iois.srcdoc.toLowerCase().indexOf('<body') == -1 ? iois.srcdoc : '' + iois.srcdoc.replace('<BODY','<body').replace('</BODY>','</body>').split('<body')[1].split('</body>')[0].split('</body>')[0].replace(iois.srcdoc.replace('<BODY','<body').replace('</BODY>','</body>').split('<body')[1].split('</body>')[0].split('>')[0] + '>',''));  // Unsanitized string of HTML
beforebih=unsanitized_string.replace(/\>/g,'&gt;').replace(/\</g,'&lt;');
document.getElementById('before').innerHTML=beforebih;
const sanitizer = new Sanitizer();  // Default sanitizer;

// Sanitize the string
const sanitizedBody = sanitizer.sanitizeFor("body", unsanitized_string);

//We can verify the returned element type, and view sanitized HTML in string form:
console.log(sanitizedBody instanceof HTMLBodyElement);
// true
console.log(sanitizedBody.innerHTML)
// "abc  def"

// At some point later…

// Get the element to update. This must be a div to match our sanitizeFor() context.
// Set its content to be the children of our sanitized element.
iois.replaceChildren(sanitizedBody.children);
//console.log(aconto);
afterbih=aconto.body.innerHTML.replace(/\>/g,'&gt;').replace(/\</g,'&lt;');
document.getElementById('after').innerHTML=afterbih;
     }
    }
  }
}

function trythis() {
  var newsd=prompt('Optionally enter new iframe content to sanitize.', beforebih.replace(/\&gt\;/g,'>').replace(/\&lt\;/g,'<'));
  if (newsd) {
    if (newsd != beforebih.replace(/\&gt\;/g,'>').replace(/\&lt\;/g,'<')) {
      document.getElementById("userFrame").srcdoc=newsd;
    }
  }
}

function ronecheck() {
   if (document.getElementById('result1').innerHTML != '') {
      document.getElementById('incoming').innerHTML=document.getElementById('result1').value;
      document.getElementById('result1').innerHTML='';
      document.getElementById('result1').value='';
      document.getElementById('myhxfile').name='xx';
      document.getElementById('mysub').click();
   }
}

function pcheckit(iois) {
   var paconto = (iois.contentWindow || iois.contentDocument);
   if (paconto != null) {
   if (paconto.document) { paconto = paconto.document; }
   if (paconto.body != null) {
         paconto.getElementsByTagName('h1')[0].style.opacity='0.0';
         if (1 == 1) { paconto.getElementsByTagName('h3')[0].style.opacity='0.0'; }
         if (1 == 1) { paconto.getElementsByTagName('h4')[0].style.opacity='0.0'; }
   }
   }
}

setInterval(ronecheck, 3000);
</script>
</head>
<body style=background-color:rgb(250,250,250); onload="s1 = document.getElementById('preincoming'); s2 = document.getElementById('incoming'); s1.addEventListener('scroll', select_scroll_1, false); s2.addEventListener('scroll', select_scroll_2, false);" data-onload='onl();'>
<h1>XML Lint Validation</h1>
<h3>RJM Programming - June, 2024</h3>
<form onsubmit=" if (document.getElementById('myhxfile').value == '') { document.getElementById('myhxfile').name='xx';  }  return true;" action=./xmllint_validation.php method=POST target=_self>
<table style=width:95%;background-color:rgba(200,200,200,0.5); border=5>
<tr><th colspan=2 style=display:table-cell;overflow-y:hidden;text-align:center;><span style=vertical-align:top;margin-top:10px;display:inline-block;>XML Lint validation of</span>&nbsp;<input style=width:50%;vertical-align:top;margin-top:10px; onblur="if (this.value.length > 0) { document.getElementById('mysub').click();  }" name=htmlfile id=myhxfile placeholder="<?php echo str_replace('>','&gt;',str_replace('<','&lt;',$prefn)); ?>" value=""></input>&nbsp;<span id=scbi style=background-color:yellow;><iframe onload=pcheckit(this); scrolling=no frameborder=0 id=cbi data-type=file data-accept="text/html" style="background-color:yellow;z-index:1;display:inline-block;width:173px;height:228px;margin-top:-194px;" src="/HTMLCSS/client_browsing.htm?d=690751964842271&straighttext=y"></iframe></span></th></tr>
<tr><th>Data to validate</th><th>XML Lint results</th></tr>
<tr><td style=vertical-align:top;><textarea style=font-size:8px;display:inline-block;overflow-x:clip;text-wrap:nowrap;text-align:right; id=preincoming><?php echo str_replace('>','&gt;',str_replace('<','&lt;',$precontents)); ?></textarea><textarea onblur="if (this.value.length > 0 && '<?echo $fn; ?>' == '') { document.getElementById('mysub').click();  }" style=font-size:8px;display:inline-block;overflow-x:clip;text-wrap:nowrap; name=content id=incoming><?php echo str_replace('>','&gt;',str_replace('<','&lt;',$contents)); ?></textarea></td><td style=vertical-align:top;><textarea id=outgoing><?php echo str_replace('>','&gt;',str_replace('<','&lt;',$results)); ?></textarea></td></tr>
<tr><td></td><td><input type=submit id=mysub style=background-color:lightgreen;display:<?php echo $vsnone; ?> value=Validate></input></td></tr>
</table>
</form>
<div id=txtresultout></div>
<div id=resultout></div>
<div id=result></div>
<textarea style=display:none; id=result1></textarea>
<textarea style=display:none; id=result2></textarea>
<!--div id=resultav></div-->
<textarea style=display:none; id=thewords></textarea>
<input id=outputname type=hidden value=''></input>
<input id=cto type=hidden value=''></input>
</body>
</html>
