<?php /*
   +------------------------------------------------------------------+
   | MikeCherim.com                                                   |
   | PHP: Style Changer                                               |
   | PHP Hypertext Preprocessor                                       |
   | Copyright Sept 2006                                              |
   | Use with attribution by visible link please!                     |
   | Attribute to: <a href="http://green-beast.com/">Mike Cherim</a>  |
   +------------------------------------------------------------------+
*/
?>

<?php // THIS IS PHP PART 1 OF 3 *************************************
/*
*  INSTRUCTIONS: Place this first part at the very top of your page I mean
*  the very top, above the Document Type Declaration (DTD), even above
*  any whitespace. This is required to properly send the cookie. As a
*  recommended option, add it to a separate file and include it.
*/
?>
<?php                               
// PHP STYLE CHANGER CONFIGURATION
######################################################################
// Begin configuration section

   
$cookieName = "GimmeStyle";       // Choose a name for your cookie

   
$defName    = "Default Style";    // Enter a default CSS title
   
$altName    = "Alternate Style";  // Enter an alternate CSS title
                                     // ADD more titles as needed

   
$cssPath    = "styles/";          // Path to CSS relative to page
                                     // Leave blank for root-level CSS

   
$defStyle   = "default.css";      // Defatult CSS file name
   
$altStyle   = "alternate.css";    // Alternate CSS file name
                                     // Add more CSS files as needed


// No need to edit below this point unless **ADD**ing files
######################################################################
/*
*  First we need to determine if the user has the CookiesOk test cookie
*/
if (!isset($_COOKIE['CookiesOk']) && !isset($_GET['PSC'])) {
// Of course it's not set, even on return visits. Here it is set on load
     
setcookie('CookiesOk', '1');

/*
*  But a refresh is needed, the passed vaiable makes sure this doesn't loop
*  if cookies are not allowed. This was the tricky part and I got some help
*  from "ClamCrusher" at SitePoint for this block! And, of course I can't
*  forget PHP.net (my brain away from home)
*/
    
header('Location: '.htmlentities($_SERVER['PHP_SELF']).'?PSC');
   exit;

/*
*  If a user doesn't allow cookies and the test cookie isn't set, you can
*  pass some thing on optionally, else some variables are generated
*/
} elseif (!isset($_COOKIE['CookiesOk']) && isset($_GET['PSC'])) {
   
$commentLead  = "<!--";    // Use to hide link/links for non-cookie users, begin
   
$commentTail  = "-->";     // Use to hide link/links for non-cookie users, end

// Normally, the idea isn't to display a message but just remove the function
   
$nocookieMsg  = "<p>OPTIONAL no cookies message or leave null</p>";

//  Else the cookie is set and the script can execute
} else {
   
$commentLead  = "";
   
$commentTail  = "";
   
$nocookieMsg  = "";
}

/*
*  Let's define the default style in case cookies aren't supported or for
*  use upon initial landing, before a selection is made
*/
   
$cssFile  = "$defStyle";

// Got the style cookie? Cool, this will return the data to the user
if(isset($_COOKIE["".$cookieName.""]) && !isset($_GET["".$cookieName.""])) {

// Redefine the user's default style from the cookie data (from last visit)
   
$cssFile  = $_COOKIE["".$cookieName.""];
  }

/*
*  The user may not already have the cookie, but it doesn't mean
*  they can't have one, all they have to do is click the link.
*  The file must exist ([A] the first method of preventing XSS)
*/
if(isset($_GET["".$cookieName.""]) && file_exists(''.$cssPath.''.$_GET["".$cookieName.""].'')) {
  
setcookie("".$cookieName."", "".$_GET["".$cookieName.""]."", time()+60*60*24*30, '/');
   
$cssFile = "".$_GET["".$cookieName.""]."";
}

// Available styles array, this is also to prevent XSS (see below)
   
$pcsStyles  = array( ''.$defStyle.'',''.$altStyle.'', ); // **ADD** more to array as needed

/*
*  Let's make sure the file is in the array, and here's how the second method
*  of preventing XSS works by demanding an exact match of index definitions.
*  If it doesn't match, they get the default style only
*/
if(!in_array($cssFile, $pcsStyles) ) {
   
$cssFile = $defStyle;
}

/*
*  Let's give the style a common name for good browsers like Firefox
*  (this could be done with associative array as well), if this wasn't
*  a special experiment page, the selected style could be seen in
*  View - Page Style. This block also negotiates link elements, etc.
*  If **ADD**ing style sheets, you'll have to provide the style link as
*  a list of muliple links, one for each file name. You'll use } elseif {
*  for each but the last. Switching between two styles with a single link
*  will no longer be sufficient (see NOTE below)
*/
if($cssFile == "$altStyle") {
   
$cssName          = "$altName";
   
$show_link        = "$defStyle";
   
$name_link        = "$defName";
} else {
   
$cssName          = "$defName";
   
$show_link        = "$altStyle";
   
$name_link        = "$altName";
}

// Here the variables are combined for forming the dynamic style changer's link unless **ADD***ing (see NOTE below)
   
$styleLink = "<ul>
                   <li><a href=\""
.htmlentities($_SERVER['PHP_SELF'])."?".$cookieName."=".$show_link."\">Click this Link to Select the ".$name_link."</a></li>
                 </ul>"
;
/*
*  O  NOTE: If offering multiple style sheets, more than 2, **ADD** mutiple links
*  R  instead of a single link like that shown above. Add links as to block below as
*     needed and uncomment by removing the two forward slashes before:  $styleLink =
*/
// $styleLink = "<ul>
//                 <li><a href=\"".htmlentities($_SERVER['PHP_SELF'])."?".$cookieName."=".$defStyle."\">Click this Link to Select the ".$defName."</a></li>
//                 <li><a href=\"".htmlentities($_SERVER['PHP_SELF'])."?".$cookieName."=".$altStyle."\">Click this Link to Select the ".$altName."</a></li>
//                 <li><a href=\"".htmlentities($_SERVER['PHP_SELF'])."?".$cookieName."=".$thirdStyle."\">Click this Link to Select the ".$thirdName."</a></li>
//               </ul>";

// This creates the import statement
   
$pscImport = "<style type=\"text/css\" media=\"screen\" title=\"$cssName\">  
  @import \"$cssPath$cssFile\";
</style>"
;

// Lastly everything is put together into one tiny code snippet
    
$pscLink = "".$commentLead."".$styleLink."".$commentTail."".$nocookieMsg."";

/*
*  End of main script (1 of 3) - This can be "included" at the very top of every
*  page if in separate file. This would actually be recommended
*/
?>






<?php // THIS IS PHP PART 2 OF 3 *************************************
/*
*  INSTRUCTIONS: This section is the CSS import statement. Simply
*  place this in the <head></head> of the document
*/
?>
     <?php echo(''.$pscImport.''); ?>







<?php // THIS IS PHP PART 3 OF 3 *************************************
/*
*  INSTRUCTIONS: This echoes the link or list of links if you **ADD**ed
*  more than two style sheets. Place this snippet in the <body></body>
*  of the document where you'd like the style link or list to display
*/
?>
      <?php echo(''.$pscLink.''); ?>