<?php

// make sure the required arguments are there
$missingArgs array_diff(array('transformer','dappName'), array_keys($_REQUEST));
if (
sizeof($missingArgs)) {
  echo 
'Missing arguments: '.join(', ',$missingArgs);
  exit;
}

// get the arguments passed in
$transformerName  $_REQUEST['transformer'];
$dappName         $_REQUEST['dappName'];
$urlToApplyDappTo = isset($_REQUEST['applyToUrl']) ?
                    
$_REQUEST['applyToUrl'] :
                    
null;
$variableArgs     = array();
foreach (
$_REQUEST as $reqVar => $reqVal) {
  if (
preg_match('/^variableArg_(.+)/',$reqVar,$matches) && strlen($reqVal)) {
    
$variableArgs[$reqVar] = $reqVal;
  }
}

$extraArgs = array();
foreach (
$_GET as $getVar => $getVal) {
  if (
preg_match('/^extraArg_(.*)/',$getVar,$matches))
    
$extraArgs[$matches[1]] = $getVal;
}

// if extra arguments were passed, and they are arguments that we permit to be
// passed through to Dapper, we handle them here (e.g. username and password)
$allowedPassThruArgs = array('username','password','encryptionAlg');
$passThruArgs = array();
foreach (
$allowedPassThruArgs as $allowedArg)
  if (!empty(
$_REQUEST[$allowedArg]))
    
$passThruArgs[$allowedArg] = $_REQUEST[$allowedArg];

// make sure the transformer exists
if (!file_exists($transformerName.'.php')) {
  echo 
'Transformer does not exist';
  exit;
}

// include the right transformer
require_once($transformerName.'.php');

// instantiate the transformer
$transformer = new $transformerName($dappName$urlToApplyDappTo$variableArgs$passThruArgs);

// deal with extra arguments
if (sizeof($extraArgs))
  
$transformer->setExtraArgs($extraArgs);

// execute the transformation
$transformer->transform();

?>