PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Michael A. Peters  >  CSP Filter  >  index.php  >  Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example Script
Class: CSP Filter
Filter HTML based on Content Security Policy
Author: By
Last change: Example updated
Date: 12 years ago
Size: 1,007 bytes
 

Contents

Class file image Download
<?php
require_once('cspfilter_class.php');
$htmlfile = "README.html";
$buffer = file_get_contents($htmlfile);

$domdoc = new DOMDocument("1.0","utf-8");
$domdoc->preserveWhiteSpace = false;
$domdoc->formatOutput = true;
$domdoc->loadHTML($buffer);

$filter = new cspfilter($domdoc);
$filter->csp['allow'] = 'none';
$filter->csp['img-src'] = 'www.w3.org';

$filter->processData();
$filter->makeCSP();

// ignore this part of the source - I'm modifying the README.html
// content to insert actual example of policy violation report
$report = $filter->displayViolationReport();
$reportNode = $domdoc->createElement("pre",$report);
$reportNode->setAttribute("class","cspreport");
$elements = $domdoc->getElementsByTagName("pre");
foreach (
$elements as $element) {
   if (
$element->hasAttribute('id')) {
     
$id = $element->getAttribute('id');
      if (
$id == 'treport') {
        
$element->parentNode->replaceChild($reportNode,$element);
         }
      }
   }
// end ignore

print $domdoc->saveHTML();
?>

For more information send a message to info at phpclasses dot org.