<?php
// #######################################################
// Author: Martin Haase / DAASI International GmbH / TextGrid
// Creation date: 2010-09-23
// Modification date: 2010-09-03
// Version: 0.1
// #######################################################

mb_internal_encoding("UTF-8");

class WebUtils {
  // Global variables


  public function __construct() {

  }

  public function printAuthSuccess($heading,$loginname,$rbachash,$slc) {

    $this->printHeader($heading);

    echo "<meta name=\"remote_principal\" content=\"".$rbachash["remote_user"]."\"/>\n";
    echo "<meta name=\"rbac_session_status\" content=\"".$rbachash["scstatus"]."\"/>\n";
    echo "<meta name=\"rbac_sessionid\" content=\"".$rbachash["Sid"]."\"/>\n";
    if (!$slc["slcmode"]) { 
      echo "<meta name=\"ePPNplusSID\" content=\"".$rbachash["remote_user"]."|".$rbachash["Sid"]."\"/>\n";
    }
    echo "<style type=\"text/css\">";
    echo "#d123 {display:none;}";
    echo "</style>";
    echo "<script type=\"text/javascript\">\n";
    echo "<!--\n";
    echo "function toggle (target) {\n";
    echo "var obj=document.getElementById(target);\n";
    echo "obj.style.display=\"block\";\n";
    echo "}\n";
    echo "-->\n";
    echo "</script>\n";

    echo "</head>\n<body>\n";
    echo "<h2>Authentication Succeeded</h2>\n"; 

    echo "<p>You were successfully authenticated with User ID '<b>" . $rbachash["remote_user"] . "</b>'. You may now access remote resources using the TextGrid Lab. This window can be closed.</p>\n";

    // Details -----------------------------------------------
    echo "<p>More <a href=\"javascript:toggle('d123')\">Details</a>.</p>\n";
    echo "<div id=\"d123\"> <h2>Authentication Details</h2>\n";
    echo "<table><tr><td>TgAuth Instance</td><td>". $rbachash["rbacbase"] ."</td></tr>\n";
    echo "<tr><td>Shibboleth Identity ProviderID</td><td>". $rbachash["identity_provider"]  ."</td></tr>\n";
    echo "<tr><td>Login Name</td><td>". (isset($loginname) ? $loginname : "only known at your home organization") ."</td></tr>\n";
    echo "<tr><td>User ID Attribute Name</td><td>". $rbachash["identified_user"]['authnmethod'] ."</td></tr>\n";
    echo "<tr><td>User ID Value </td><td>".$rbachash["remote_user"]."</td></tr>\n";
    //    echo "<tr><td>Given Name(s)</td><td>".$givennames."</td></tr>\n";
    //    echo "<tr><td>Surname(s)</td><td>".$surnames."</td></tr>\n";
    //    echo "<tr><td>Common Name(s)</td><td>".$cns."</td></tr>\n";
    //    echo "<tr><td>Mail(s)</td><td>".$mails."</td></tr>\n";
    //    echo "<tr><td>Organisation(s)</td><td>".$organisations."</td></tr>\n";
    echo "<tr><td>TgAuth Session ID</td><td>".$rbachash["Sid"]."</td></tr></table>\n";
    echo "</div>";
    
    if ($slc["slcmode"]) {
      echo "SLCButtons here";
    }    

    echo "\n</body>\n</html>";

  }

  public function printAuthFailure($heading,$detail,$loginname,$rbachash) {

    $this->printHeader("Authentication failed");
    
    if (isset($rbachash)) {
      echo "<meta name=\"remote_principal\" content=\"". $rbachash["remote_user"]."\"/>\n";
      echo "<meta name=\"rbac_session_status\" content=\"". $rbachash["scstatus"]."\"/>\n";
      echo "<meta name=\"rbac_sessionid\" content=\"". $rbachash["Sid"]."\"/>\n";
    }
    echo "<style type=\"text/css\">";
    echo "#d123 {display:none;}";
    echo "</style>";
    echo "<script type=\"text/javascript\">\n";
    echo "<!--\n";
    echo "function toggle (target) {\n";
    echo "  var obj=document.getElementById(target);\n";
    echo "  obj.style.display=\"block\";\n";
    echo "}\n";
    echo "-->\n";
    echo "</script>\n";
    
    echo "</head>\n\n<body>\n";

    echo "<h2>Authentication Failure</h2>\n";
    echo "<h3>$heading</h3>\n";
    echo "<p>The Authentication system could not authenticate you.</p>\n";

    echo "<p>More <a href=\"javascript:toggle('d123')\">Details</a>.</p>\n";
    echo "<div id=\"d123\">"; 
    echo "<h2>Error Details</h2>\n";
    echo "<p>". $detail ."</p>" ;
    echo "<h2>Authentication Details</h2>\n";
    echo "<table>\n";
    echo "<tr><td>Login Name</td><td>". $loginname ."</td></tr>\n";
    if (isset($rbachash["rbacbase"])) {
      echo "<tr><td>TgAuth Instance</td><td>". $rbachash["rbacbase"] ."</td></tr>\n";
    }
    if (isset($rbachash["identity_provider"])) {
      echo "<tr><td>Shibboleth Identity ProviderID</td><td>". $rbachash["identity_provider"]  ."</td></tr>\n";
    }
    if (isset($rbachash["$identified_user"]['authnmethod'] )) {
      echo "<tr><td>User ID Attribute Name</td><td>". $rbachash["identified_user"]['authnmethod'] ."</td></tr>\n";
    }
    if (isset($rbachash["remote_user"])) {
      echo "<tr><td>User ID Value </td><td>".$rbachash["remote_user"]."</td></tr>\n";
    }
    if (isset($rbachash["Sid"])) {
      echo "<tr><td>TgAuth Session ID</td><td>".$rbachash["Sid"]."</td></tr>\n";
    }
    echo "</table>\n";
    echo "</div>";

    echo "<p>If not indicated otherwise in the <a href=\"javascript:toggle('d123')\">details</a>, it could be that some service is not responding temporarily. In this case, please <a href=\"javascript:history.back()\">go back</a> or re-open the TextGridLab and try again.</p>";
    echo "<p>If the problem persists, please report this bug together with its time of occurence (" . date("Y-m-d H:i:s") .  "). In the TextGridLab, choose 'Help-&gt;Report Bug'.</p>" ;
    echo "\n</body>\n</html>";
  }

  public function printHeader ($title) {
    header('Content-Type: text/html; charset=utf-8');
    echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
    echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n";
    echo "<head>\n";
    echo "<title>" . $title . "</title>\n";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n";
  }

}

?>