<?php

require_once('tilib.php');
require_once('lib/nusoap.php');

$global_file = false;

function saveToSession( $stuff ) {
  global $global_file;
  if ($fp = fopen($global_file, 'a+')) {
    LG($stuff);
    fwrite($fp,$stuff);
    fclose($fp);
  }
}

LG("Starting up...");

class imsx_codeMinor {
  var $imsx_codeMinorFieldName, $imsx_codeMinorFieldValue;
  function imsx_codeMinorField ($imsx_codeMinorFieldName, $imsx_codeMinorFieldValue) {
    $this->imsx_codeMinorFieldName = $imsx_codeMinorFieldName;
    $this->imsx_codeMinorFieldValue = $imsx_codeMinorFieldValue;
  }
}

class imsx_codeMinorField {
  var $imsx_codeMinorFieldName, $imsx_codeMinorFieldValue;
  function imsx_codeMinorField ($imsx_codeMinorFieldName, $imsx_codeMinorFieldValue) {
    $this->imsx_codeMinorFieldName = $imsx_codeMinorFieldName;
    $this->imsx_codeMinorFieldValue = $imsx_codeMinorFieldValue;
  }
}

// Define the method as a PHP function
function ProxyToolLaunch($ProxyToolLaunch) {

    global $global_session;
    global $global_file;

    $x = getSession();
    LG("\n------------ Starting ProxyTool Launch -----------\n\n");
    LG("Got session: $global_session");
    $global_file = "session/session-$global_session.txt";
    LG("Creating new session in: $global_file");
    // Wipe the file out
    if ( $fp = fopen($global_file,"w") ) {
	fwrite($fp,"Establishing session in $global_file at ".date('l dS \of F Y h:i:s A')."\n");
	fclose($fp);
    }

    
    $pathback = getPathBack();
    LG("Path = $pathback\n");

    $toolpath = str_replace("DoLaunchRequest.php", "LMSTool.php?session=$global_session", getPathBack());
     LG("Tool Path= $toolpath \n");

    ob_start(saveToSession);
    echo("ProxyToolLaunch\n");
    var_dump($ProxyToolLaunch);
    echo("\n");
    ob_end_clean();

    // Produce a reasonable return value
    $retval = 
	array ( 
	     'Response' =>
	     array (
	       'LaunchDirective' => 
		  array (
			'PerformRedirectAction' => $toolpath
		  )
	     ),
	     'HeaderInfoResponse' =>
	     array (
               'imsx_version' => '1.0',
               'imsx_messageIdentifier' => 'MID',
               'imsx_statusInfo' => array(
                    'imsx_codeMajor' => 'Major Fred',
                    'imsx_severity' => 'Severe Fred',
                    'imsx_messageRefIdentifier' => 'Message ReFred',
                    'imsx_operationRefIdentifier' => 'Operation Ref Ident - Fred',
		    'imsx_codeMinor' => array (
		        'imsx_codeMinorField' => array (
			     'imsx_codeMinorFieldName' => 'Floyd',
			     'imsx_codeMinorFieldValue' => 'Pink'
		         )
                    ),
/*
		     new imsx_codeMinor('Field1','pink'),
*/
/*
                    'imsx_codeMinor' => 
                        array (
			  new imsx_codeMinorField('Field1','pink')
                        ),  // end of imsx_codeMinor
*/

/*
                    'imsx_codeMinor' => 
                        array (
			  'imsx_codeMinorField' => 'Field1',
		          'imsx_codeMinorValue' => 'pink'
                        ),  // end of imsx_codeMinor
*/
                    'imsx_description' => 'The IMSX Description'
                 ) // End of imx_statusInfo

// SecurityProfile is not serializing - SimpleSecurityProfile is a extension and
// It is not clear how to represent the extension in this format
// Leaving it in is harmless - but nothing ever makes it past serializaton
// perhaps I should forxe it with soapval.   Hmmm.
/*
                 ,
		 'SecurityProfile' => array (
                        "tns:MAC" => "00:ff:ff:ff:00:00"
		 )
*/
	      ) // End of HeaderInfoResponse
         ) ;

    ob_start(saveToSession);
    echo("\n------- Return Value --------\n");
    var_dump($retval);
    echo("\n");
    ob_end_clean();

    return $retval;
}

global $debug;

// Turn this on if you want massive debugging on soap_server
// The debu outout shows up on the client (testlaunch.php)
// $debug = 999;

$server = new soap_server("TIRLaunchSyncSingle.wsdl");

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>
