<?php

require 'browserhawk.php';

// This file is provided under the terms of the BrowserHawk4J license
// agreement.  Copyright (c) 2005-2006 cyScape, Inc. All rights reserved.

// One of the most challenging aspects of deploying Java applets is
// guaranteeing that your visitor's browser has Java installed, enabled,
// and of a particular version and vendor that are known to work with
// your applet. BrowserHawk makes this process easy by enabling you to
// detect, before sending your applet to the client, what the client's
// capabilities and settings are. 

// Unfortunately Java detection isn't as easy as a simple on/off switch
// because of the various settings and variables involved. This sample
// therefore is designed to walk you through the process and explain how
// to make the most of BrowserHawk when detecting your visitor's Java
// capabilities within their browser.

// This sample tests the various properties involved in sniffing Java,
// and based on the returned values, reports the client's Java
// capabilities. You should feel free to use this sample as the basis for
// how you sniff Java in your own application.

// First we make sure the system executing this example has an
// Enterprise Edition license as required for this test.
// Note: This is NOT needed in your pages, only for this sample.
if (strpos($BrowserHawk->getVersion(), "Ent") === false) {
  echo "Enterprise License Required.  ";
  echo "You are using " . $BrowserHawk->getVersion() . ".  ";
  echo "See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.";
  return;
}
  
$options->addProperties("JavaScriptEnabled, JavaEnabled, Plugin_JavaVer, JavaVersion, JavaVendor, MSJVMBuild");
$options->setPageMessage("Please wait while we check your browser...");
$options->setPageTitle("Please wait...");

$einfo = bhextended($options);
?>

<html>
<head><title>BrowserHawk Java Test Page (PHP)</title></head>

<body bgcolor=white>
<font face="helvetica">

<h2>BrowserHawk Java Test Page (PHP)</h2>

Java test results as detected by 
<a href="http://www.cyscape.com">BrowserHawk</a>:<br>

<?php
// The returned value is the UNSUPPORTED constant if the browser isn't
// Netscape, Mozilla, Firefox, IE, Opera, Safari, or AOL Explorer.

// The PHP Java bridge doesn't let you compare complex Java objects for
// equality, so we'll see if their toString() forms match.

if ($einfo->toString() === $UNSUPPORTED->toString()) { ?>
  <big>No extended information is available.  Only Netscape,
  Mozilla, Firefox, IE, Opera, Safari, and AOL Explorer clients are
  supported.</big>

<?php } else if ($einfo->getJavaScriptEnabled() === false) { ?>
  <big>No Java detection information is available.  This client has
  JavaScript disabled.</big>

<?php } else if (!isSupportedBrowser($info)) { ?>
  <big>No Java detection is available.<br>For Java detection your browser has
  to be IE Windows 3+ (except exactly 4.00), Mozilla, Firefox, Safari 1.2+, 
  Netscape 4.0+, Opera 7+, or AOL Explorer.</big>

<?php } else { ?>

<br>

<table>
<tr><th align="left">Property</th><th>Value</th></tr>
<tr><td width="150">JavaEnabled</font></td>
    <td><?php boolprint($einfo->getJavaEnabled()) ?></td></tr>
<tr><td width="150">Plugin_JavaVer</font></td>
    <td><?php echo $einfo->getPluginJavaVer() ?></td></tr>
<tr><td width="150">JavaVersion</font></td>
    <td><?php echo $einfo->getJavaVersion() ?></td></tr>
<tr><td width="150">JavaVendor</font></td>
    <td><?php echo $einfo->getJavaVendor() ?></td></tr>
<tr><td width="150">MSJVMBuild</font></td>
    <td><?php echo $einfo->getMSJVMBuild() ?></td></tr>
</table>

<br>
<b>Conclusion:</b>
<p>

  <?php
    $javaVendor = $einfo->getJavaVendor();
    $javaVersion = $einfo->getJavaVersion();
    $pluginJavaVer = $einfo->getPluginJavaVer();
    $msjvmBuild = $einfo->getMSJVMBuild();
    if ($pluginJavaVer == "") $pluginJavaVer = null;
    if ($msjvmBuild == "") $msjvmBuild = null;

    $developerWarning =  // added to some error message reports
      "<br><br>" .
      "(Note for the developer: It's also possible that this condition is " .
      "occurring because the JVMDetector.class file is not being found. " .
      "This .class file is distributed with the BrowserHawk files and " .
      "should be placed into the same directory where your web script " .
      "is running. Or, you can place it into another web directory " .
      "and use BrowserHawk's \$options->setJVMCodeBase() to tell " .
      "BrowserHawk what the path to the web directory should be for applet " .
      "downloads. The web server's access logs can reveal if the class " .
      "file is being found and downloaded or not.)";


    // Java disabled and non-empty version is a strong indication Java's disabled
    if (!($einfo->getJavaEnabled()) && $pluginJavaVer != null) { ?>
      JavaEnabled returned false while Plugin_JavaVer detection returned a legitimate
      value.<br>This indicates you have the Java Plugin <?php echo $pluginJavaVer ?> installed
      but it's currently disabled.

  <?php } else if (!($einfo->getJavaEnabled()) && $msjvmBuild != null) { ?>
      JavaEnabled returned false while MSJVMBuild detection returned a legitimate
      value.<br>This indicates you have the the MSJVM Build <?php echo $msjvmBuild ?> installed
      but it's currently disabled.

  <?php } else if ($javaVersion == "-3") { ?>
      JavaVersion returned -3.<br>This indicates you don't have the Java Plugin
      installed and configured to run applets.

  <?php } else if (!($einfo->getJavaEnabled()) /* implicitly pluginJavaVer and msjvmBuild are null */) { ?>
      JavaEnabled returned false and Plugin_JavaVer/MSJVMBuild detection returned no legitimate
      value.<br>This indicates you probably have no Java installed.

  <?php } else if ($javaVersion == "-1") { ?>
      JavaEnabled returned true while JavaVersion returned -1.<br>This indicates you
      most likely have a very old JVM which can't be accessed from JavaScript.
      <?php echo $developerWarning ?>

  <?php } else if ($javaVersion == "-2" && $pluginJavaVer != null) { ?>
      JavaEnabled returned true, JavaVersion returned -2, and Plugin_JavaVer
      detection returned a legitimate value.<br>This indicates you have the Java Plugin
      <?php echo $pluginJavaVer ?> installed but scripting of applets is explicitly disabled
      so it's not possible to confirm the behavior of an applet.
      <?php echo $developerWarning ?>

  <?php } else if ($javaVersion == "-2" && $msjvmBuild != null) { ?>
      JavaEnabled returned true, JavaVersion returned -2, and MSJVMBuild
      detection returned a legitimate value.<br>This indicates you have the MSJVM Build
      <?php echo $msjvmBuild ?> installed but scripting of applets is explicitly disabled
      so it's not possible to confirm the behavior of an applet.
      <?php echo $developerWarning ?>

  <?php } else if ($javaVersion == "-2" /* implicitly pluginJavaVer and msjvmBuild are null */) { ?>
      JavaEnabled returned true, JavaVersion returned -2, and Plugin_JavaVer/MSJVMBuild
      detection returned no legitimate value.<br>This indicates you don't have any
      Java installed.
      <?php echo $developerWarning ?>

  <?php } else if ($javaVersion != null && !($javaVersion == "")) /* javaVendor exists if javaVersion exists */ { ?>
      JavaVersion and JavaVendor indicate you have Java <?php echo $javaVersion ?> from <?php echo $javaVendor ?>
      ready to run applets under <?php echo $info->getBrowser() ?> <?php echo $info->getVersion() ?>
      on <?php echo $info->getPlatform() ?>.
      <?php
        // If the first 3 chars don't match between javaVersion and pluginJavaVer,
        // add a note about what plugin might be available.
        if ($pluginJavaVer != null && strncmp($javaVersion, $pluginJavaVer, 3) != 0) {
          echo "<br>You also have the Java Plugin version " . $pluginJavaVer . " at your disposal.";
        }
      ?>
  <?php } ?>

<?php } ?>

<P>
<b><a href="java.php">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other PHP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font>
</body>
</html>

<?php
  function isSupportedBrowser($info) {
    $browser = $info->getBrowser();
    $version = $info->getVersion();
    $platform = $info->getPlatform();

    if ($browser == "Mozilla") {
      return true;
    }
    if ($browser == "Firefox") {
      return true;
    }
    if ($browser == "Safari" && $version >= 1.2) {
      return true;
    }
    if ($browser == "Netscape" && $version >= 4) {
      return true;
    }
    if ($browser == "Opera" && $version >= 7) {
      return true;
    }
    if ($browser == "IE" && strpos($platform, "Win") >= 0
            && $version >= 3 && $version != 4.0) {
      return true;
    }
    return false;
  }
?>
