<html>
<head>
    <title>AppsAnywhere embed test page</title>
    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimal-ui" />
    <meta http-equiv="cleartype" content="on" />
    <meta name="HandheldFriendly" content="True" />
    <meta name="MobileOptimized" content="320" />

    <script src="sha.js"></script>
    <script src="https://cdn.tailwindcss.com?plugins=forms"></script>
</head>
<body class="text-gray-700 flex min-h-screen">
<main class="p-6">
    <?php

    $host = ''; // Your AppsAnywhere url

    $courseId = '1'; // Identifier for this course
    $courseCode = 'ENG-101'; // Display name for this course
    $username = 'adam.willcock'; // The username, matching the SSO or LDAP user
    $usernameField = 'lis_person_sourcedid'; // This field name can be configured in the AppsAnywhere Embed, defaults to 'lis_person_sourcedid'

    // Comma separated list of roles, replace as required e.g. 'urn:lti:role:ims/lis/Administrator,urn:lti:role:ims/lis/Instructor'
    //$roles = 'urn:lti:instrole:ims/lis/Student';// If neither admin nor instructor then the list is read-only
    //$roles = 'urn:lti:role:ims/lis/Instructor'; // 'urn:lti:role:ims/lis/Instructor' or 'urn:lti:role:ims/lis/Administrator' can edit and associate app lists
    $roles = 'urn:lti:role:ims/lis/Administrator'; // Provides the same level of admin as Instructor, however this is meant for global admins

    $consumerKey = ''; // The consumer key copied from the AppsAnywhere Embed
    $keySecret = ''; // The secret key copied from the AppsAnywhere Embed
    ?>

    <form action="<?php echo "https://{$host}/lti/application/embed" ?>" method="post" target="output_frame" id="form" class="flex flex-col gap-6">

        <?php

        // Params required to be sent alongside 'oauth_signature'
        $params = [
            'custom_appsanywhere_custom_embed_course_code' => $courseCode,
            'custom_appsanywhere_custom_embed_course_id' => $courseId,
            'roles' => $roles,
            'oauth_consumer_key' => $consumerKey,
            $usernameField => $username,
            'oauth_timestamp' => time(), // The current timestamp for OAuth
            'oauth_nonce' => bin2hex(random_bytes(32)), // An OAuth nonce (limited to 96 characters)
            'tool_consumer_info_product_family_code' => 'Test-System', // Free to be any value you like
        ];
        ksort($params); // Params must be ordered by key

        $method = 'POST&';
        $baseUrl = rawurlencode("https://{$host}/lti/application/embed") . '&';

        // Parse params
        $paramString = '';
        foreach ($params as $key => $value) {
            $paramString .= rawurlencode($key) . '=' . rawurlencode($value) . '&';
        }
        $paramString = rawurlencode(rtrim($paramString, '&'));

        // Create base string for oauth signature, note the use of '&' with the secret
        $baseString = $method . $baseUrl . $paramString;
        $signature = base64_encode(hash_hmac('sha1', $baseString, $keySecret . '&', true));
        ?>

        <h3>Current details:</h3>
        <ul>
            <li>Host: <?php echo $host ?></li>
            <li>Username: <?php echo $username ?></li>
            <li>Roles: <?php echo $roles ?></li>
            <li>Course ID: <?php echo $courseId ?></li>
            <li>Course code: <?php echo $courseCode ?></li>
        </ul>

        <input type="hidden" name="tool_consumer_info_product_family_code" value="<?php echo $params['tool_consumer_info_product_family_code'] ?>" />
        <input type="hidden" name="custom_appsanywhere_custom_embed_course_id" value="<?php echo $params['custom_appsanywhere_custom_embed_course_id'] ?>" />
        <input type="hidden" name="custom_appsanywhere_custom_embed_course_code" value="<?php echo $params['custom_appsanywhere_custom_embed_course_code'] ?>" />
        <input type="hidden" name="<?php echo $usernameField ?>" value="<?php echo $params[$usernameField] ?>" />
        <input type="hidden" name="roles" value="<?php echo $params['roles'] ?>" />

        <input type="hidden" name="oauth_consumer_key" value="<?php echo $params['oauth_consumer_key'] ?>" />
        <input type="hidden" name="oauth_timestamp" value="<?php echo $params['oauth_timestamp'] ?>" />
        <input type="hidden" name="oauth_nonce" value="<?php echo $params['oauth_nonce'] ?>" />
        <input type="hidden" name="oauth_signature" value="<?php echo $signature ?>" />


        <button type="submit" class="bg-indigo-600 text-white rounded px-4 py-3">Submit</button>
    </form>
</main>
<iframe name="output_frame" src="" id="output_frame" frameborder="0" class="flex-grow" title="Contains the AppsAnywhere Embed for test purposes when the form is submitted."></iframe>
</body>
</html>
