Skip to content
Snippets Groups Projects
WebUtils.class.php 15 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?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() {
      }
    
    
    Martin Haase's avatar
    Martin Haase committed
      public function printAuthSuccess($heading,$loginname,$rbachash,$slc) {
    
    Martin Haase's avatar
    Martin Haase committed
        $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";
    
    Martin Haase's avatar
    Martin Haase committed
          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";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<div style=\"font-face: Arial;\"";
    
    Martin Haase's avatar
    Martin Haase committed
        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 "SLCMODE YES";
          $isSLCScompatible = $this->scanEntitlements($slc);
          if ($isSLCScompatible) {
    	echo "SLCcompatible YES";
    	$this->showCertificateButtons($slc,$rbachash);
          } else {
    	$this->showCertificateInfoButton($slc,$rbachash);
          }
    
    Martin Haase's avatar
    Martin Haase committed
        }    
    
    
    Martin Haase's avatar
    Martin Haase committed
        echo "</div>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "\n</body>\n</html>";
    
      }
    
    
      function showCertificateButtons ($slc,$rbachash) {
        echo "<form method=\"get\" action=\"". $slc['slcPortalDelegationURL'] ."\">\n";
        echo "<input type=\"hidden\" name=\"userDetails\" value=\"" . $rbachash['remote_user'] . "|" .$rbachash['Sid'] ."|". $rbachash['authZinstance'] . "\">\n";
        echo "<input type=\"submit\" value=\"Request Certificate\">\n";
        echo "</form>\n\n";
        
        echo "<form method=\"post\" action=\"". $slc['slcNoDelegationURL'] ."\">\n";
        echo "<input type=\"hidden\" name=\"ePPNplusSID\" value=\"" . $rbachash['remote_user'] . "|" .$rbachash['Sid'] . "\">\n";
        echo "<input type=\"submit\" value=\"Use Existing Certificate\"/>\n";
        echo "</form>\n";
      }
    
    Martin Haase's avatar
    Martin Haase committed
    
    
      function showCertificateInfoButton ($slc,$rbachash) {
        echo "<br/><br/>Your account does not include certificate support.";
        echo "<form method=\"post\" action=\"". $slc['slcNoDelegationURL'] ."\">\n";
        echo "<input type=\"hidden\" name=\"ePPNplusSID\" value=\"" . $rbachash['remote_user'] . "|" .$rbachash['Sid'] . "\">\n";
        echo "<input type=\"submit\" value=\"Work without Certificate\"/>\n";
        echo "</form>\n";
      }
    
    
      function scanEntitlements ($slc) {
        if (isset($_SERVER[$slc['slcEntitlementAttributeName']])) {
          $entitlements = $_SERVER[$slc['slcEntitlementAttributeName']];
          $arrEntitlements = explode( ";", $entitlements);
          foreach ($arrEntitlements as $ent) {
    	if ($ent === $slc['slcEntitlementAttributeValue']) {
    	  return TRUE;
    	}
          }
        }
        return FALSE;
    
    Martin Haase's avatar
    Martin Haase committed
      }
    
    Martin Haase's avatar
    Martin Haase committed
    
      public function printAuthFailure($heading,$detail,$loginname,$rbachash) {
    
        $this->printHeader("Authentication failed");
    
    Martin Haase's avatar
    Martin Haase committed
          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";
    
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<h2>Authentication Failure</h2>\n";
        echo "<h3>$heading</h3>\n";
    
        echo "<p>The Authentication system could not authenticate you.</p>\n";
    
    
    Martin Haase's avatar
    Martin Haase committed
        //    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";
    
    Martin Haase's avatar
    Martin Haase committed
        if (isset($rbachash["rbacbase"])) {
          echo "<tr><td>TgAuth Instance</td><td>". $rbachash["rbacbase"] ."</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        if (isset($rbachash["identity_provider"])) {
          echo "<tr><td>Shibboleth Identity ProviderID</td><td>". $rbachash["identity_provider"]  ."</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        if (isset($rbachash["identified_user"]['authnmethod'] )) {
    
    Martin Haase's avatar
    Martin Haase committed
          echo "<tr><td>User ID Attribute Name</td><td>". $rbachash["identified_user"]['authnmethod'] ."</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        if (isset($rbachash["remote_user"])) {
          echo "<tr><td>User ID Value </td><td>".$rbachash["remote_user"]."</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        if (isset($rbachash["Sid"])) {
          echo "<tr><td>TgAuth Session ID</td><td>".$rbachash["Sid"]."</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        //    echo "</div>";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<p>If not indicated otherwise in the error details, 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) {
    
    Martin Haase's avatar
    Martin Haase committed
        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";
      }
    
    
    Martin Haase's avatar
    Martin Haase committed
      public function printAttributeForm ( $existingAttrs, $providedAttrs, $attrMap, $Sid, $authZinstance, $remote_user, $ToUversion, $ToUtext) {
    
        if (isset ($providedAttrs)) {
          $loginmode = TRUE;
          $title = "Please Complete your User Attributes";
        } else {
          $loginmode = FALSE;
          $title = "Your User Attributes for Modification";
        }
    
        $this->printHeader ($title);
    
        echo "<meta name=\"remote_principal\" content=\"".$remote_user."\"/>\n";
        echo "<meta name=\"rbac_sessionid\" content=\"".$Sid."\"/>\n";
    
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"./scripts/help.css\" media=\"all\"/>\n";
        echo "<script src=\"./scripts/jquery-1.5.2.min.js\" type=\"text/javascript\"></script>\n";
        echo "<script src=\"./scripts/help.js\" type=\"text/javascript\"></script>\n";
    
        echo "<style type=\"text/css\">";
        echo "#d123 {display:none;}";
        echo "</style>";
        echo "<script type=\"text/javascript\">\n";
        echo "<!--\n";
        echo "function toggleToU (target) {\n";
        echo "var obj=document.getElementById(target);\n";
        echo "obj.style.display=\"block\";\n";
        echo "}\n";
        echo "-->\n";
        echo "</script>\n";
    
    
        echo "<script type=\"text/javascript\">\n";
        echo "function chkFormular () {\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "    if (!document.Formular.ToUversion.checked) {\n";
        echo "        alert(\"You must accept the Terms of Use in order to proceed.\");\n";
        echo "        document.Formular.ToUversion.focus();\n";
        echo "        return false;\n";
        echo "    }\n";
    
    
        foreach ($existingAttrs as $a) {
    
    Martin Haase's avatar
    Martin Haase committed
          if ($a->mandatory && $a->name != "ToUversion") {
    
    	echo "    if (document.Formular.". $a->name . ".value == \"\") {\n";
    
    Martin Haase's avatar
    Martin Haase committed
    	echo "        alert(\"Please specify your ".$a->displayname . "\\n(".$a->description . ").\");\n";
    
    	echo "        document.Formular.". $a->name . ".focus();\n";
    	echo "        return false;\n";
    	echo "    }\n";
          }
        }
        echo "    if (document.Formular.mail.value.indexOf(\"@\") == -1 ){\n";
        echo "        alert(\"Please specify your valid E-Mail-Address.\");\n";
        echo "        document.Formular.mail.focus();\n";
        echo "        return false;\n";
        echo "    }\n";
        
        echo "}\n"; // end of chkFormular
    
        echo "</script>";
    
        echo "</head>\n<body>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<div style=\"font-face: Arial;\">";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<h2 style=\"text-align: center;\">$title</h2>\n";
    
        //    echo "This is what we got for sure:". serialize ($providedAttrs);
        //    echo $remote_user .serialize ( $attrMap );
    
    
        echo "<form name=\"Formular\" action=\"PutAttributes.php\" method=\"post\" onsubmit=\"return chkFormular()\">\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<table style=\"margin-left: auto; margin-right: auto;\">\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<colgroup> <col align=\"left\" span=\"3\"></col></colgroup>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        $oldToUversion = "";
    
        foreach ($existingAttrs as $a) {
    
    Martin Haase's avatar
    Martin Haase committed
          if ($a->name == "ToUversion") {
    	$oldToUversion = $a->value;
    	continue;
          }
    
          $oldval = "";
          if (isset ($a->value)) {
    	$oldval = $a->value;
          }
    
    Martin Haase's avatar
    Martin Haase committed
          $providedreadonly = "";
    
          if (isset($attrMap[$a->name]) && isset($providedAttrs[$attrMap[$a->name]])) {
    	$oldval = $providedAttrs[$attrMap[$a->name]];
    
    	if (stripos($remote_user, "@textgrid.de") === FALSE) {
    	  $providedreadonly = " readonly=\"readonly\"";
    	}
    
          }
          if ($this->isBoolean ($a)) {
    	if ($oldval === "TRUE") {
    	  $checked = "checked=\"checked\"";
    	} else {
    	  $checked = "";
    	}
          }
          if ($a->mandatory) {
    	$mandatory = "<span style=\"color:red\">*</span>";
          } else {
    	$mandatory = "";
          }
    
          echo "<tr><td>".$a->displayname." ${mandatory}</td>";
          if ($this->isBoolean ($a)) {
    
    	echo "<td><input type=\"checkbox\" name=\"". $a->name . "\" value=\"on\" ${checked}/></td>\n";
    
          } else if ($this->isISO3166format ($a)) { 
    	if ($oldval === "") {$oldval = "DE";} // pre-assume German Scholars
    	echo "<td>";	
    	$this->formatISO3166list( $a->name, $oldval);
    	echo "</td>\n";
          } else { // assume string
    
    Martin Haase's avatar
    Martin Haase committed
    	echo "<td><input type=\"text\" size=\"50\" name=\"". $a->name . "\" value=\"${oldval}\"${providedreadonly}/></td>\n";
    
    Martin Haase's avatar
    Martin Haase committed
          echo "<td><a href=\"javascript:displayhelp('".$a->displayname."','". $a->description ."')\"><img src=\"./scripts/help.png\" border=\"0\" height=\"25px\" alt=\"?\"></a>";
          echo "</td></tr>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        
        $checked = "";
        if ($ToUversion == $oldToUversion) {
          $checked = "checked=\"checked\"";
        }
        echo "<tr><td></td><td colspan=\"2\"><div style=\"text-align: right;\"><input type=\"checkbox\" name=\"ToUversion\" value=\"${ToUversion}\" ${checked}/>\n";
    
        echo " I accept the <a href=\"javascript:toggleToU('d123')\">TextGrid Terms of Use</a>.</div></td></tr>";
        echo "<tr><td colspan=\"3\"><div id=\"d123\">";
    
        echo $ToUtext;
    
        echo "</div></td></tr>\n";
    
    
        echo "<input type=\"hidden\" name=\"loginmode\" value=\"" . $loginmode . "\" />\n";
        echo "<input type=\"hidden\" name=\"remote_user\" value=\"" . $remote_user . "\" />\n";
        echo "<input type=\"hidden\" name=\"Sid\" value=\"" . $Sid . "\" />\n";
        echo "<input type=\"hidden\" name=\"authZinstance\" value=\"" . $authZinstance . "\" />\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<tr><td></td><td colspan=\"2\"><div style=\"text-align: right;\"><input type=\"reset\" value=\"Cancel\">";
        echo "<input type=\"submit\" value=\"Submit\"></div></td></tr>";
        echo "</table>\n";
    
        echo "</form>\n";
    
    Martin Haase's avatar
    Martin Haase committed
        echo "</div>";
    
        echo "\n</body>\n</html>";
      }
      
      public function printSetAttributesSuccess ($thedisplayname) {
        $this->printHeader("Successfully updated User Attributes");
        echo "</head><body>\n";
        echo "Thank you, ${thedisplayname}, your user attributes are now up to date.<br/>\n";
        echo "You can close this dialogue.\n";
        echo "\n</body>\n</html>";    
      }
    
    
      public function isBoolean ($attr) {
        // TODO this is a hack yet
        if ($attr->name === "agreesearch") {
          return TRUE;
        } else {
          return FALSE;
        }
      }
    
      public function isISO3166format ($attr) {
        // TODO this is a hack yet
        if ($attr->name === "citizenship" || $attr->name === "country") {
          return TRUE;
        } else {
          return FALSE;
        }
      }
      
      public function formatISO3166list ( $listname, $preselected ) {
        $list = $this->list_iso3166();
    
    
    Martin Haase's avatar
    Martin Haase committed
        echo "<select name=\"${listname}\" style=\"width: 200px;\" size=\"1\">\n";
    
        foreach ($list as $code => $name) {
          if ($code === $preselected) {
    	$selected = "selected=\"selected\"";
          }	else {
    	$selected = "";
          }
          echo "<option ${selected} value=\"${code}\">${name}</option>\n";
        }
        echo "</select>";
      } 
    
      // taken from http://code.google.com/p/hm2k/source/browse/trunk/code/php/functions/iso3166.php
      function list_iso3166($key=false) { // Returns a list of ISO 3166 alpha 2 country codes and names in an array
    
        // Make a static copy of this file on your server
        $url='http://www.iso.org/iso/iso3166_en_code_lists.txt';
    
        // Path/Filename of the local copy (if present)
        $file='iso3166_en_code_lists.txt';
    
        // Can we find a local copy?
        if (!file_exists($file)) { $file=$url; }
    
        // Put the data into a list...
        $list=@file_get_contents($file);
    
        // No list? Return
        if (empty($list)) { return; }
    
        // Process list
        $lines=explode("\n",$list);
        $list=array();
        foreach ($lines as $line) {
          $line=explode(';',$line);
          if (isset($line[1])) {
    	$list[trim($line[1])]=trim($line[0]);
          }
        }
    
        // Shift first line (info line) off the array
        array_shift($list);
    
        //Return either entire list as array or just a key.         
        return isset($list[$key])?$list[$key]:$list;
      }
    
    
    Martin Haase's avatar
    Martin Haase committed
    
    ?>