<?php

require 'browserhawk.php';

// This sample is provided under the terms of the BrowserHawk End User License
// Agreement.  Copyright (c) 2003-2010 cyScape, Inc. All rights reserved.
// 
// This example demonstrates how to use BrowserHawk to detect some of the
// more popular "extended" properties.  Extended properties are properties
// that can vary depending on how a user has configured their browser and
// system.
// 
// In addition, this sample demonstrates how to use BrowserHawk's built in
// support for automatic caching of extended property test results.  This
// caching of results makes it very convenient to test a user for a certain
// set of extended properties just once in their session, and to retreive
// the cached results from any pages where it is needed, without writing your
// own special logic to handle such caching.
// 
// The major benefit to the caching is that extended property tests, unlike
// "basic" property tests, require a round trip between the client and server.
// Therefore it is far more efficient to perform the test on the user's
// browser just once and then later retrieve the results from the cache on
// subsequent pages as needed.  BrowserHawk does not cache results of extended
// browser tests by default.  In the details below you will see exactly how to
// use the caching feature.
// 
// In this sample we check for disabled cookies, disabled JavaScript, screen
// width and height, available browser width and height, Flash version, and
// whether the user is on a Broadband connection.  If we already have tested
// this user during this session, the results are retrieved from the cache.
// Otherwise the browser is tested and the results are stored in the cache.
// 
// Feel free to use this sample as the basis for your own scripts that
// perform BrowserHawk testing, as it provides a good framework for doing so.
// You can easily expand or shrink this sample's functionality to meet your
// own needs by changing the parameters passed in to addProperties
// (see below).
// 
// Although we have made this sample very verbose with comments for
// educational purposes, note that it takes only a few lines of code to
// detect the extended properties -  1) use $options to set testing
// preferences, and 2) call bhextended($options) to obtain an
// ExtendedBrowserInfo instance where the test results are obtained.


  // To avoid an ugly exception, we check that the system executing this
  // example has a Professional or Enterprise Edition license as required
  // for this test.
  // Note: This is NOT needed in your pages, only for this generic sample.
  if (strpos($BrowserHawk->getVersion(), "Pro") === false &&
      strpos($BrowserHawk->getVersion(), "Ent") === false) {
    echo "Professional or 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;
  }

  // Now we tell BrowserHawk which tests we want it to perform on the
  // browser. If there are other settings you want to check as well, you can
  // just add them to this list.   See the ExtendedOptions class in the
  // BrowserHawk documentation for more information.
  $options->addProperties("PersistentCookies, SessionCookies, JavaScriptEnabled, Width, Height, WidthAvail, HeightAvail, Plugin_Flash, Broadband");

  // Now we set various preferences and options for how we want
  // BrowserHawk to perform its test.  See the ExtendedOptions class in
  // the documentation for details on options available such as
  // controlling the background color of the screen and page messages i.e.
  // "Checking your browser. Please wait...".
  // TIP: Set the BGCOLOR parameter below to your site's background color.
  // Otherwise you may notice a color flicker during the test.
  $options->setPageTitle("Please wait...");
  $options->setPageMessage("Checking your system. Please wait...");
  $options->setBodyTag("BGCOLOR=#FFFFFF");

  // Now we tell BrowserHawk we want it to cache the results of the test
  // in the user's session.  We do this simply by setting the
  // ExtendedOption CacheToken property.  The token name used can be
  // any valid session variable name.  NOTE: In order for this session
  // caching feature to work, you must have session tracking enabled.
  $options->setCacheToken("bhawk_results");

	// Above we are using "bhawk_results" as the cache token.  Whenver this
	// page runs again for this same user's session, or whenever any other
  // page using BrowserHawk runs again that also specifies this same cache
  // token, BrowserHawk will automaticall pull the results from the cache,
  // and NOT re-execute the test, even though the code below still calls
  // bhextended().  In this case bhextended() just retrives the cached
  // ExtendedBrowserInfo object from the user's session, instead of
  // retesting the browser.

  // Now call the bhextended() function which tells BrowserHawk that
  // we are finished setting our options and that it should perform the
  // browser test now.  If this is the first time this page is executing
  // under this user's session, BrowserHawk will actually perform the test
  // and then cache the results in the user's session object.
  // If this is not the first time this page is executing for the user's
  // session, or if a previous page performed an extended property test
  // and specified the same CacheToken value for the ExtendedOptions class,
  // then Browserhawk will NOT perform the test again. Rather, the call to
  // bhextended() will simply retrieve the ExtendedBrowserInfo from the
  // session.
  // NOTE: You should not attempt to access the cached object directly from
  // the session object, but rather by calling bhextended().  BrowserHawk
  // will return the cached ExtendedBrowserInfo object for you automatically.

  // Note: There may be situations where you want to force BrowserHawk to
  // retest a browser even though the information is already in the cache.
  // For example, let's say you test a browser for a required screen size
  // and the user's screen size is too small.  You then provide the user
  // with information on how to increase their screen size and provide a
  // link for them to retry the test to confirm their new settings are
  // correct so they can proceed with your site.  If you do not force a
  // clear of the cached information, BrowserHawk will continue to return
  // their original screen size test result.  Therefore in such situations,
  // you will want to call the options->setCacheForceRefresh() method and
  // pass in a value of true.  This tells BrowserHawk to retest the
  // browser even if cached information is already available.  The results
  // from the retest are then returned by BrowserHawk and stored in the
  // cache for reuse.

  $einfo = bhextended($options);
?>

<html>
<head>
	<title>BrowserHawk Sample - Performing Popular BrowserHawk Tests (PHP)</title>
</head>
<body><font face="arial">
<blockquote>
<h2>Some Popular BrowserHawk Test Results (PHP)</h2>

<font size=3>
This sample demonstrates how to perform some of the more popular browser tests.
<p>

It also demonstrates how BrowserHawk can automatically cache this
information in the user's session.  This way if the BrowserHawk test
results are needed on the same or subsequent page later, BrowerHawk can
just return the information to you from the session, rather than retesting
the browser.  See the source code for this sample for more information.

<p>
BrowserHawk results obtained from cache?

<?php if ($einfo->isFromCache()) { ?>
  <B><font color="green">YES, results are from cached BrowserHawk
     information in user's session</font></b>
<?php } else { ?>
  <B><font color="black">NO, results are NOT from the cache but will be on
     next <a href="?">retry</a></font></b>
<?php } ?>

<p>

Session cookies enabled? <?php boolprint($einfo->getSessionCookies()) ?><p>
Persistent cookies enabled? <?php boolprint($einfo->getPersistentCookies()) ?><p>
JavaScript enabled? <?php boolprint($einfo->getJavaScriptEnabled()) ?><p>
Screen resolution: <?php echo $einfo->getWidth() ?> x <?php echo $einfo->getHeight() ?><p>
Available browser window size: <?php echo $einfo->getWidthAvail() ?> x <?php echo $einfo->getHeightAvail() ?><p>
Flash plug-in version installed: <?php echo $einfo->getPluginFlash() ?><p>
Broadband connection? <?php boolprint($einfo->getBroadband()) ?><p>

<HR>

<p>
<b><a href="?bhRefresh=1">Retry this test, but clear the cached results to
force a retest of the browser</a></b><P>
<b><a href="popular.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>
</font>
</blockquote>
</body>
</html>
