Helical Alpha Code

ginclude()


<?

/*

name: ginclude.php
author: keith beckman
date: 122805
url: http://alphahelical.com/code/misc/ginclude
description: ginclude provides a simple function which facilitates the use
  common elements throughout a website, and ease of migration. As long as
  each page includes ginclude.php, which should reside in the document root,
  it has access to any files in the specified general include directory.
  
  Additionally, the ninclude() function allows _n_ directories to be
  available for inclusion. Directories are specified as key-value pairs in
  the $directories array.
  
  An added benefit is that document root information is less likely to be
  leaked to the page source, since failed ginclude()s and ninclude()s give
  only internally-identifying information.
  
*/


// Specify directories as key-value pairs.
// Whatever you do, DON'T change a key you are using!!!
$directories = array(
 1 => $_SERVER['DOCUMENT_ROOT'].'/gen-inc',
 );

// Loop through $directories . . .
$i=1;
foreach($directories as $dir) {
 
 // . . . build constant names . . .
 $dirName = 'GINC_DIR'.$i;
 
 // . . . and set constants. We use constants to preclude
 // accidental reset, however unlikely it may be.
 define($dirName,$dir);
 }

// The one you'll use the most. It's basically shorthand for
// ninclude($fn,1), and the fallback in case ninclude can't
// find a file.
function ginclude($fn) {

 // Check if the file exists . . .
 if(file_exists(GINC_DIR1."/$fn")) {

  // . . .and if it does, include it.
  include(GINC_DIR1."/$fn");
  }
 
 // If it doesn't . . .
 else {

  // . . . spit out a comment.
  gincUnAvail($fn,'ginclude','1');
  }
 }

// The Swiss Army Include() . . .
function ninclude($fn,$dirID) {

 // Build the constant name . . .
 $dirName = 'GINC_DIR'.$dirID;
 
 // . . . and get the constant value . . .
 
 // . . . if, of course, it exists.
 if(defined($dirName)) {
  $dir = constant($dirName);
  }
 
 // If not . . .
 else {

  // . . . give a warning . . .
  echo("\n\n<!-- '$dirID' is not a valid directory identifier. -->\n\n");
  
  // . . . and quit.
  return TRUE;
  }

 // If the file exists . . .
 if(file_exists($dir."/$fn")) {
 
  // . . . include it.
  include($dir."/$fn");
  }
 else {
 
  // Otherwise, slip a comment . . .
  gincUnAvail($fn,'ninclude',$dirID);
  
  // . . . and try ginclude().
  ginclude($fn);
  
  }
 
 }

// Boring warn function:
function gincUnAvail($fn,$func,$id) {
 
 // Just warn in HTML with $fn, which function, and which
 // directory key (not whole directory, for security's sake.
 echo("\n\n<!-- file '$fn' not available to $func() in directory '$id' -->\n\n");
 }

?>

Generated by GNU enscript 1.6.1 and enscriptclean.

ginclude | Download Source | View Source