<?php
// dapps can take a long time!
ini_set('default_socket_timeout',300);

define('DAPP_FIELD',100);
define('DAPP_ATTRIBUTE',101);
define('DAPP_FIELD_OR_ATTRIBUTE',102);
define('DAPP_FIELD_MULTI',103);
define('DAPP_FIELD_OR_ATTRIBUTE_MULTI',104);
define('USER_FREEFORM',200);
define('USER_SELECT',201);
define('USER_CHECKBOX',202);
define('DAPPINFO_NAME'301);
define('USERINFO_EMAIL'401);
abstract class 
Transformer {

  protected 
$xmlContents;
  protected 
$extraArgs;
  protected 
$dappName;
  protected 
$xmlUrl;
  protected 
$dapp;

  public function 
Transformer($dappName$urlToApplyDappTo=null$variableArgs=array(), $passThruArgs=array(), $fetch=true) {
    
$this->dappName $dappName;
    
    
// construct the RunDapp url
    
$variableArgsString '';
    foreach (
$variableArgs as $vArg => $vVal) {
      
$variableArgsString .= "$vArg=".rawurlencode($vVal).'&';
    }
    
    
$xmlUrl "http://www.dapper.net/RunDapp".
              
"?dappName=".$dappName.
              (isset(
$urlToApplyDappTo) ?
               
"&applyToUrl=".rawurlencode($urlToApplyDappTo) :
               
'').
              
'&'.
              
$variableArgsString.
              
"v=1";

    
// if there are extra arguments we blindly passing through, append them now
    
if (sizeof($passThruArgs)) {
      foreach (
$passThruArgs as $argName => $argValue) {
        
$xmlUrl .= '&'.$argName.'='.rawurlencode($argValue);
      }
    }
    
    
$this->xmlUrl $xmlUrl;
    
    
// some transformers do not want us to fetch the xml
    
if ($fetch) {
      
// we want to set some custom headers on the GET, so we create a context
      
$context stream_context_create(
                   array(
'http' =>
                           array(
                             
'header' => 'Referer: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."\r\n".
                                         
'User-Agent: Dapper Transformer ('.get_class($this).")\r\n".
                                         (!empty(
$_SERVER['REMOTE_ADDR']) ? 
                                         
'Dapper-Remote-IP: '.$_SERVER['REMOTE_ADDR']."\r\n" :
                                         
'')
                           )
                        )
                 );

      
$this->xmlContents file_get_contents($xmlUrlfalse$context);
      if (!
$this->xmlContents) {
        throw new 
Exception("Unable to get XML from $xmlUrl");
      }
    }
  }
 
  public function 
getDappName() {
    return 
$this->dappName;
  }
  
  public function 
getDapp() {
    if (!
$this->dapp) {
      require_once(
$_SERVER['DOCUMENT_ROOT'].'/../includes/Dapp.php');
      
$this->dapp Dapp::loadDapp($this->getDappName());
    }
    
    return 
$this->dapp;
  }

  public function 
setExtraArgs($extraArgs) {
    
$this->extraArgs $extraArgs;
  }

  public function 
previewHelper() {
    
ob_start();
    
$this->transform();
    
$transformerOutput ob_get_contents();
    
ob_end_clean();
    return 
$transformerOutput;
  }
  
  public function 
preview() {
    echo 
$this->previewHelper();
  }

  
// this function must be implemented by subclasses
  
public abstract function transform();  
  
  
// this function must be implemented by subclasses
  // it is used to supply information about the properties of the transformer
  // it should return an array with keys like 'description' and 'argMapping'
  
public abstract static function getDetails();
  

}

?>