<?php

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

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
	// We are not caring much.
	LG("Error\n");
}

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

    LG("\n------------ Starting X   ProxyTool Launch -----------\n\n");
    
    ob_start(LG);
    echo("ProxyToolLaunch\n");
    var_dump($ProxyToolLaunch);
    echo("\n");

    echo("ImsHeader\n");
    var_dump($ImsHeader);
    echo("\n");
    ob_end_clean();

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

    $toolpath = str_replace("EndPointLaunch.php", "EndPointTool.php?x=1", getPathBack());
    LG("Tool Path= $toolpath \n");

    //  **** REPLACE THIS CODE WITH WHATEVER CDE YOU NEED TO SET UP THE SESSION
    //  **** AND PRODUCE A URL TO SEND A USER TO YOUR TOOL WITH THAT SESSION

    // Parse the input parameters - a sample of the input parameters is at the 
    // Bottom of this message
    if (  $ProxyToolLaunch["ContextualSettings"] ) {
       if ( $ProxyToolLaunch["ContextualSettings"]["LaunchProfile"] ) {
         $lidVal  = $ProxyToolLaunch["ContextualSettings"]["LaunchProfile"]["LaunchIdentifier"];
         LG("LaunchIdentifier = $lidVal\n");
         $roleVal = $ProxyToolLaunch["ContextualSettings"]["LaunchProfile"]["Role"];
         LG("Role = $roleVal\n");
       }
       if ( $ProxyToolLaunch["ContextualSettings"]["DeliveryContextProfile"] ) {
         if ( $ProxyToolLaunch["ContextualSettings"]["DeliveryContextProfile"]["IMSSourcedid"] ) {
           $contextVal = $ProxyToolLaunch["ContextualSettings"]["DeliveryContextProfile"]["IMSSourcedid"]["SourceID"];
           LG("Context (SiteId) = $contextVal\n");
         }
       }
       if ( $ProxyToolLaunch["ContextualSettings"]["UserProfile"] ) {
         if ( $ProxyToolLaunch["ContextualSettings"]["UserProfile"]["IMSSourcedid"] ) {
           $eidVal = $ProxyToolLaunch["ContextualSettings"]["UserProfile"]["IMSSourcedid"]["SourceID"];
           LG("Enterprise ID = $eidVal\n");
         }
         $idVal = $ProxyToolLaunch["ContextualSettings"]["UserProfile"]["Userid"];
         LG("User ID = $idVal\n");
         $fnVal = $ProxyToolLaunch["ContextualSettings"]["UserProfile"]["FirstName"];
         LG("First Name = $fnVal\n");
         $lnVal = $ProxyToolLaunch["ContextualSettings"]["UserProfile"]["LasttName"];
         LG("Last Name = $lnVal\n");
      }
    }
    
    $parms = "";
    if ( isset($contextVal) ) $parms .= "&SiteId=".$contextVal;
    if ( isset($lidVal) ) $parms .= "&LaunchIdentifier=".$lidVal;
    if ( isset($roleVal) ) $parms .=  "&Role=".$roleVal;
    if ( isset($eidVal) ) $parms .= "&EID=".$eidVal;
    if ( isset($idVal) ) $parms .= "&Userid=".$idVal;
    if ( isset($fnVal) ) $parms .= "&FirstName=".$fnVal;
    if ( isset($lnVal) ) $parms .= "&LastName=".$lnVal;

    // *** END OF CODE YOU ARE SUPPOSED TO REPLACE

    $toolpath .= htmlspecialchars($parms);
    LG("Tool Path= $toolpath \n");

LG("HO THERE\n");
    // 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' => 'success',
                    'imsx_severity' => 'status',
                    'imsx_messageRefIdentifier' => '234',
                    'imsx_operationRefIdentifier' => '567',
                    'imsx_description' => 'Happy Interoperating',
		    'imsx_codeMinor' => array (
		        'imsx_codeMinorField' => array (
			     'imsx_codeMinorFieldName' => 'Floyd',
			     'imsx_codeMinorFieldValue' => 'Pink'
		         )
                    )
                 ) // End of imx_statusInfo
	      ) // End of HeaderInfoResponse
         ) ;

LG("HI THERE\n");

    ob_start(LG);
    echo("Returning\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 debug 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);

/* What the input parameters to this look like 

ProxyToolLaunch
array(3) {
  ["CoreSettings"]=>
  array(2) {
    ["ProxyTool"]=>
    array(4) {
      ["ProxyToolName"]=>
      string(18) "Loop Back Endpoint"
      ["ProxyToolVersion"]=>
      array(2) {
        ["major"]=>
        string(1) "1"
        ["minor"]=>
        string(1) "0"
      }
      ["IMSTIVersion"]=>
      array(2) {
        ["major"]=>
        string(1) "1"
        ["minor"]=>
        string(1) "0"
      }
      ["ProxyToolDescription"]=>
      string(9) "DO A LOOP"
    }
    ["TIR"]=>
    array(2) {
      ["LaunchService"]=>
      array(3) {
        ["ServiceName"]=>
        string(32) "sakai-target-tool launch service"
        ["ServiceLocation"]=>
        string(57) "http://localhost:8081/~csev/imsti-test/EndPointLaunch.php"
        ["ServiceProfileName"]=>
        string(20) "SimpleOutcomeProfile"
      }
      ["OutcomeService"]=>
      array(3) {
        ["ServiceName"]=>
        string(19) "SakaiOutcomeService"
        ["ServiceLocation"]=>
        string(77) "http://localhost:8080/sakai-imsti-tool/services/TIROutcomeServiceSyncSoapPort"
        ["ServiceProfileName"]=>
        string(20) "SimpleOutcomeProfile"
      }
    }
  }
  ["ContextualSettings"]=>
  array(4) {
    ["LaunchProfile"]=>
    array(3) {
      ["LaunchIdentifier"]=>
      string(36) "53634116-1a44-455e-0070-7def31c08153"
      ["Role"]=>
      string(5) "admin"
      ["LaunchType"]=>
      string(4) "test"
    }
    ["DeliveryContextProfile"]=>
    array(4) {
      ["IMSSourcedid"]=>
      array(2) {
        ["SourceName"]=>
        string(12) "sakaiContext"
        ["SourceID"]=>
        string(36) "a78e0f1f-08e7-48c2-0011-01461b356193"
      }
      ["Name"]=>
      string(5) "sadfg"
      ["ContextDescription"]=>
      string(0) ""
      ["Ancillary"]=>
      string(395) "<?xml version="1.0" encoding="UTF-8"?>
<ti:sakai xmlns:ti="http://www.sakaiproject.org/ti"><ti:sakaiContext>a78e0f1f-08e7-48c2-0011-01461b356193</ti:sakaiContext><ti:sessionId>6c35b1b5-c946-40de-0059-22bbb1d902b2</ti:sessionId><ti:placementId>7e78f8c9-227f-4e54-808f-6285dd3b0382</ti:placementId><ti:toolId>0</ti:toolId><ti:instanceId>0</ti:instanceId><ti:resourceid>1</ti:resourceid></ti:sakai>"
    }
    ["AcclipProfile"]=>
    array(1) {
      ["Ancillary"]=>
      string(0) ""
    }
    ["UserProfile"]=>
    array(5) {
      ["IMSSourcedid"]=>
      array(2) {
        ["SourceName"]=>
        string(3) "eid"
        ["SourceID"]=>
        string(4) "csev"
      }
      ["Userid"]=>
      string(36) "5d379004-9917-422d-0006-ab401d0e0933"
      ["FirstName"]=>
      string(0) ""
      ["LastName"]=>
      string(0) ""
      ["Ancillary"]=>
      string(0) ""
    }
  }
  ["ToolSettings"]=>
  array(1) {
    ["Ancillary"]=>
    string(51) "<resourceIdentifier><id>1</id></resourceIdentifier>"
  }
}

ImsHeader
array(3) {
  ["imsx_version"]=>
  string(0) ""
  ["imsx_messageIdentifier"]=>
  string(36) "e3d0c807-0781-4496-006b-bfc4adb3abc8"
  ["SecurityProfile"]=>
  array(1) {
    ["MAC"]=>
    string(24) "6Bf1QJPb48MhvKUhSpoIfw=="
  }
}

*/

?>
