<?
	define("BANNERCACHE", "includes/bannercache.inc");
	define("LAYOUTDIR", "layouts/");
	define("BANNERIMG", "bannerimg.jpg");

	$target = basename($_SERVER['PHP_SELF'], ".php");

	$bannerImages = getBannerImages( BANNERCACHE, LAYOUTDIR, BANNERIMG );
	// Seth: To see how this works, uncomment the following line
	// echo "<pre>"; print_r($bannerImages); echo "</pre>"; die();
	// The above, if uncommented, should list all BANNERIMG files found one level beneath LAYOUTDIR
	// Having done so, the script will exit.

	$bannerimg = (count($bannerImages)) ? $bannerImages[rand(0,count($bannerImages)-1)] : false;
	if ( ($target == "layouts2") && isset($_REQUEST['m']) ) {
		$bannerimg = sprintf("layouts/%s/%s", $_REQUEST['m'], BANNERIMG);
	}

    // START PRORAIL/BAYRAIL OVERRIDES //
    {
        $now = time();
        $prorailstart = strtotime("July 30, 2023");
        $prorailend   = strtotime("June 1, 2024");

        // Quick-Abort:  Uncomment the following line
        // $now = $prorailend;

        if ( ($now > $prorailstart) && ($now < $prorailend) ){
            // These are the special ProRail settings
            $css = "prorail.css";
            $banner = "includes/banner.prorail2020.inc";
            $sitenav = "includes/sitenav.prorail2020.inc";
        }
        else {
            // These are the standard BayRail settings
            $css = "bayrails.css";
            $banner = "includes/banner.inc";
            $sitenav = "includes/sitenav.inc";
        }
    }
    // SEDIRREVO LIARYAB/LIARORP TRATS //    


/*
 * if the data file doesn't exist, make it.
 * then, if the file does exist, return it's eval()
 *
 * Translation: If the cache file exists, use it.
 * In other words ... if new banner images have been added to the layouts directories,
 *     the cache file must be rebuilt to include them. To do that, DELETE the cache file
 *     and this routine will rebuild it with all files called $needle
 */
function getBannerImages( $cache, $topdir, $needle ) {
	$images = array();
	if ( !file_exists( $cache ) ) {
		if (($dh = opendir( $topdir )) !== false ) {
			while ( $file = readdir($dh) ) {
				if ( is_dir( $topdir.$file ) && file_exists( $topdir.$file."/".$needle) )
					$images[] = $topdir.$file."/".$needle;
			}
			closedir( $dh );
		}
		file_put_contents( $cache, var_export($images, true)."\n" );
	}

	if ( file_exists( $cache ) ) {
		$s = file_get_contents($cache);
		eval('$images = ' . $s . ';');
	}
	return( $images );
}
?>
