<?php
require_once('Transformer.php');

class 
XSL extends Transformer {
  
  public function 
transform() {
    
    
// make sure the user suplied a url for the XSL
    
if (empty($this->extraArgs['xslUrl']))
      throw new 
MissingArgumentsException(array('xslUrl'));

    
// get the xsl url
    
$xslUrl $this->extraArgs['xslUrl'];
    
    
// security check the xsl url
    
if (!preg_match('!^http://!',$xslUrl))
      throw new 
Exception("Invalid URL: ".$xslUrl);
    
    
// grab the XSL contents
    
$xslContents = @file_get_contents($xslUrl);
    if (!
$xslContents) {
      throw new 
Exception("Unable to get contents of stylesheet at: ".$xslUrl);
    }

    
$xp      = new XsltProcessor();
    
$xsl     = new DomDocument;    
    if (!@
$xsl->loadXML($xslContents))
      throw new 
Exception("Invalid XML in stylesheet: ".$xslUrl);

    
$xp->importStylesheet($xsl);
    
$xml     = new DomDocument;
    
$xml->loadXML($this->xmlContents);
    
    if (
$html $xp->transformToXML($xml)) {
      echo 
$html;
    } else {
      throw new 
Exception('XSL transformation failed.');
    }
    
  }
  
  public static function 
getDetails() {
    
$details = array();
    
    
$details['args']['xslUrl']  = array('type'     => USER_FREEFORM,
                                        
'required' => true,
                                        
'display'  => 'URL of XSL Stylesheet');
    
    
$details['description'] = 'Transforms the Dapp output using an XSL stylesheet';
    
    return 
$details;
  }
}
?>