<?php
ini_set
('include_path','/usr/share/phpinclude');
require_once(
'dom_functions.inc');
require_once(
'xml_doctype.inc');
require_once(
'cspfilter_class.php');
include_once(
'idna_convert.class.php');

$http_host $_SERVER['HTTP_HOST'];
// above should be sanitized
// because we allow base tag to be set in the form
$thisdir $http_host dirname($_SERVER['PHP_SELF']);
// might be broken under IIS with ISAPI??
if (! isset($_SERVER['HTTPS'])) {
   
$thisdir 'http://' $thisdir;
   } elseif (
strcmp($_SERVER['HTTPS'],'off') == 0) {
   
$thisdir 'http://' $thisdir;
   } else {
   
$thisdir 'https://' $thisdir;
   }
   
$def_csp = Array();
if (isset(
$_POST['allow'])) {
   
$foo trim($_POST['allow']);
   if (
strlen($foo) > 0) {
      
$def_csp['allow'] = $foo;
      } else {
      
$def_csp['allow'] = 'none';
      }
   } else {
   
$def_csp['allow'] = 'none';
   }
   
if (isset(
$_POST['img-src'])) {
   
$foo trim($_POST['img-src']);
   if (
strlen($foo) > 0) {
      
$def_csp['img-src'] = $foo;
      }
   }
   
if (isset(
$_POST['media-src'])) {
   
$foo trim($_POST['media-src']);
   if (
strlen($foo) > 0) {
      
$def_csp['media-src'] = $foo;
      }
   }
   
if (isset(
$_POST['script-src'])) {
   
$foo trim($_POST['script-src']);
   if (
strlen($foo) > 0) {
      
$def_csp['script-src'] = $foo;
      }
   }
   
if (isset(
$_POST['object-src'])) {
   
$foo trim($_POST['object-src']);
   if (
strlen($foo) > 0) {
      
$def_csp['object-src'] = $foo;
      }
   }
   
if (isset(
$_POST['frame-src'])) {
   
$foo trim($_POST['frame-src']);
   if (
strlen($foo) > 0) {
      
$def_csp['frame-src'] = $foo;
      }
   }
   
$preStyle "font-family: Monaco, 'Lucida Mono', 'Lucida Mono EFOP', 'Lucida Mono EF', Courier, monospace; color: blue;";
   
$dom = new DOMDocument('1.0','UTF-8');
$dom->preserveWhiteSpace false;
$dom->formatOutput true;

if (isset(
$_POST['forcehtml'])) {
   
$myPage = new docType($dom,'text/html');
   } else {
   
$myPage = new docType($dom);
   }

$sanitized = new cspfilter($dom);
$sanitized->httphost $_SERVER['HTTP_HOST'];

$xmlHtml $myPage->document();
$xmlHead $dom->createElement('head');
$xmlHtml->appendChild($xmlHead);
$xmlBody $dom->createElement('body');
$xmlBody->setAttribute('style','background-color: #eeeeee;');
$xmlHtml->appendChild($xmlBody);
pageTitle($dom,$xmlHead,'script test');
$def_base='';
if (isset(
$_POST['base'])) {
   
$def_base trim($_POST['base']);
   if (
strlen($def_base) > 0) {
      
$xmlBase $dom->createElement('base');
      
$xmlBase->setAttribute('href',$def_base);
      
$xmlHead->appendChild($xmlBase);
      }
   }
   
$def_test='';
if (isset(
$_POST['foo'])) {
   
$def_test=trim($_POST['foo']);
   
$import_prepare '<div>' $def_test '</div>';
   
   
$newDom = new DOMDocument('1.0','UTF-8');
   
$newDom->loadXML($import_prepare);
   
$elements $newDom->getElementsByTagName('div');
   
$imported_div $elements->item(0);
   
   
$testDiv $dom->importNode($imported_div,true);
   
$testDiv->setAttribute('id','formPostData');
   }

// body content
$xmlPar $dom->createElement('p','Test page for ');
addLink($dom,$xmlPar,$thisdir '/','CSP Output Filter Class');
$xmlBody->appendChild($xmlPar);

$xmlForm $dom->createElement('form');
$xmlForm->setAttribute('id','form');
$xmlForm->setAttribute('method','post');
$xmlForm->setAttribute('action',$thisdir '/dom_script_test.php');
$xmlBody->appendChild($xmlForm);

$cspDiv $dom->createElement('div');
$cspDiv->setAttribute('id','cspsettings');
$cspDiv->setAttribute('style','float: left;');
$source = Array('allow','img-src','media-src','script-src','object-src','frame-src');
$inputDiv $dom->createElement('div');
$inputDiv->setAttribute('style','text-align: right;');
for (
$i=0$i<5$i++) {
   
$foo $source[$i];
   
addLabel($dom,$inputDiv,'',$source[$i] . ':','inp_' $source[$i]);
   if (isset(
$def_csp[$foo])) {
      
$val=$def_csp[$foo];
      } else {
      
$val='';
      }
   
addTextInput($dom,$inputDiv,'inp_' $source[$i],$source[$i],'80',$val);
   
addBr($dom,$inputDiv);
   }
$cspDiv->appendChild($inputDiv);
$xmlForm->appendChild($cspDiv);
$clear $dom->createElement('div');
$clear->setAttribute('style','clear: both;');
$xmlForm->appendChild($clear);
addHr($dom,$xmlForm,'','width: 50%;');

$htmlSet $dom->createElement('div');
$htmlSet->setAttribute('id','htmlsettings');
$htmlSet->setAttribute('style','float: left;');
$xmlDiv $dom->createElement('div');
$xmlDiv->setAttribute('style','float: left;');
if (
$myPage->usexml == 1) {
   
addCheckboxInput($dom,$xmlDiv,'forcehtml','forcehtml','1');
   } else {
   
addCheckboxInput($dom,$xmlDiv,'forcehtml','forcehtml','1',true);
   }
addLabel($dom,$xmlDiv,'','Force HTML 4.1','forcehtml',$class='',$style='');
$htmlSet->appendChild($xmlDiv);

$xmlDiv $dom->createElement('div','&#160;');
$xmlDiv->setAttribute('style','float: left; width: 2em;');
$htmlSet->appendChild($xmlDiv);

$xmlDiv $dom->createElement('div');
$xmlDiv->setAttribute('style','float: left;');
addLabel($dom,$xmlDiv,'','Contents of base tag href attribute: ','inp_base');
addTextInput($dom,$xmlDiv,'inp_base','base','40',$def_base);
$htmlSet->appendChild($xmlDiv);
$xmlForm->appendChild($htmlSet);

$clear $dom->createElement('div');
$clear->setAttribute('style','clear: both;');
$xmlForm->appendChild($clear);

$xmlDiv  $dom->createElement('div');
$xmlPar $dom->createElement('p','In order to preserve UTF8 characters, the textarea below is imported in to the DOM via loadXML() opposed to loadHTML().');
$xmlDiv->appendChild($xmlPar);
$xmlPar $dom->createElement('p','Enter some well-formed xhtml here:');
$xmlDiv->appendChild($xmlPar);

addTextArea($dom,$xmlDiv,'textarea','foo',$def_test,'20','80','','width: 65%; height: 14em;');
addBr($dom,$xmlDiv);
addSubmit($dom,$xmlDiv,'submit','submit','Submit');
$xmlForm->appendChild($xmlDiv);

if (isset(
$testDiv)) {
   
$xmlBody->appendChild($testDiv);
   }
   
addHr($dom,$xmlBody);

$xmlPar $dom->createElement('p');
addLink($dom,$xmlPar,$thisdir '/cspfilter_class.phps','Filter class source');
$xmlBody->appendChild($xmlPar);
$xmlPar $dom->createElement('p');
addLink($dom,$xmlPar,$thisdir '/dom_script_test.phps','This page php source');
$xmlBody->appendChild($xmlPar);

$sanitized->httphost $http_host;
$sanitized->cspHeader true;
$sanitized->csp['allow'] = $def_csp['allow'];
if (isset(
$def_csp['img-src'])) {
   
$sanitized->csp['img-src'] = $def_csp['img-src'];
   }
if (isset(
$def_csp['media-src'])) {
   
$sanitized->csp['media-src'] = $def_csp['media-src'];
   }
if (isset(
$def_csp['script-src'])) {
   
$sanitized->csp['script-src'] = $def_csp['script-src'];
   }
if (isset(
$def_csp['object-src'])) {
   
$sanitized->csp['object-src'] = $def_csp['object-src'];
   }
if (isset(
$def_csp['frame-src'])) {
   
$sanitized->csp['frame-src'] = $def_csp['frame-src'];
   }
$sanitized->csp['style-src'] = 'self';   

$sanitized->processData();






$elements $dom->getElementsByTagName('div');
foreach (
$elements as $element) {
   if (
$element->hasAttribute('id')) {
      
$divId trim($element->getAttribute('id'));
      if (
$divId == 'formPostData') {
         
$userInput $element;
         }
      }
   }
if (isset(
$userInput)) {
   if (
$userInput->hasChildNodes()) {
      
$preXML = new DOMDocument('1.0','UTF-8');
      
$preXML->preserveWhiteSpace false;
      
$preXML->formatOutput true;
      
$children $userInput->childNodes;
      foreach (
$children as $child) {
         
$fooNode $preXML->importNode($child,true);
         
$preXML->appendChild($fooNode);
         }
      if (
$myPage->usexml == 1) {
         
$bar $preXML->saveXML();
         
$btype 'xml';
         } else {
         
$bar $preXML->saveHTML();
         
$btype 'html';
         }
      
$bar preg_replace('/<\?xml[^>]*>\n/','',$bar,1);
      
$bar preg_replace('/^\s*$/','',$bar);
      
$bar preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/''\n'$bar);
      
$userSource $dom->createElement('pre',$bar);
      
$userSource->setAttribute('style',$preStyle);
      
$xmlHeader $dom->createElement('h3','Input after CSP Output Filtering (as $btype)');

      
//$elements = $dom->getElementsByTagName('body');
      //$xmlBody = $elements->item(0);
      
$xmlBody->appendChild($xmlHeader);
      
$xmlBody->appendChild($userSource);
      }
   }
   
$errorReport $sanitized->displayViolationReport();
$xmlHeader $dom->createElement('h3','Policy Violation Report');
$preReport $dom->createElement('pre',$errorReport);
$preReport->setAttribute('style',$preStyle);
//$elements = $dom->getElementsByTagName('body');
//$xmlBody = $elements->item(0);
$xmlBody->appendChild($xmlHeader);
$xmlBody->appendChild($preReport);


$myPage->sendpage();
?>