Location: PHPKode > scripts > Database metadata > class.backbone.inc.php
<?php
/**
 * This class should be included in each file.
 *
 * @author H.F.N. den Boer <hide@address.com>
 * @copyright Copyright 2005 - 2006, IMS - the Netherlands <http://www.denboer-ims.nl>
 * @version 1.0.0
 * @package nl.denboer-ims.imslib
 */

/**
 * This class holds application config settings and contains
 * often used utility methods, arranges for security, multy language facilities,
 * data handling etc. In short, it is really the backbone of a webapplication.
 *
 * @package nl.denboer-ims.imslib
 */
class imslib_backBone
{

	/*******************************************************************************
	* Properties
	********************************************************************************/

	/**
	 * This array, used as a hashlist, contains relevant database settings
	 *
	 * @var array with properties database
	 * @access public
	 */
	public $database;

	/**
	 * This array, used as a hashlist, contains relevant info about the head
	 *
	 * Used keys: See install.html
	 *
	 * @var array with relevant URL's
	 * @access public
	 */
	public $head;

	/**
	 * Bool value to indicate that we work in a dev environment
	 *
	 * @var int
	 * @access private
	 */
	private $_isLocal;

	/*******************************************************************************
	* Methods - general
	********************************************************************************/

	/**
	 * Constructor.
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
 		global $local;
		//	Init class en members
 		$this->database = array();
 		$this->_isLocal = isset($local) ? $local : false;
		$this->_dbReads = 0;
		$this->_dbWrites = 0;

 		//	Set some default values
 		$this->head = array();
		$this->head["Author"] = "Nico den Boer, hide@address.com";
		$this->head["Copyright"] = "Copyright 2005 - 2006 IMS, http//www.denboer-ims.nl";
		$this->head["StylesheetScreen"] = "./styles.css";
		$this->head["StylesheetPrint"] = "./print.css";
	}

	/**
	 * Destructor, closes database connection and de-initializes properties
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @return void
	 */
	public function __destruct()
	{
		$this->database["connection"]->close();
		unset($this->head);
		unset($this->database);
	}

	/**
	 * Indicates that we work from localhost
	 *
	 * @since version 1.0.0
	 * @access public
	 * @author H.F.N. den Boer <hide@address.com>
	 * @return bool
	 * @todo find a better way to determine if we are working from a local machine
	 */
	public function isLocal()
	{
		return $this->_isLocal;
	}

	/**
	 * Prints the full head section
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @return void
	 */
	public function printHead ()
	{
		$target = $_SERVER["PHP_SELF"];
		$charset = "utf-8";
		$language = "en";

		/*******************************************************************************
		* Make sure we have no cache at the browser side
		********************************************************************************/
		header("Pragma: public");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

		//SERVER_PROTOCOL HTTP/1.1
		$protocol = explode("/" , $_SERVER["SERVER_PROTOCOL"]);
		$protocol = (float)$protocol[1];
		if ($protocol == 1.1)
		{
			//	HTTP 1.1
			header("Cache-Control: no-cache, no-store, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0");
			header("Cache-Control: private", false); // required for certain browsers
			//	private, must-revalidate, no-store, post-check=0, pre-check=0, max-age=0
		}

		//	HTTP 1.0
		if ($protocol == 1.0)
			header("Pragma: no-cache");

		/**
		 * $_ENV["SERVER_PROTOCOL"] can return a value like HTTP/1.1.
		 * However, MS IE can switch off HTTP 1.1
		 * So, we can never trust this environment var...
		 */

		/*******************************************************************************
		* Print the top + head section of the page
		********************************************************************************/
		echo "<?xml version=\"1.0\" encoding=\"$charset\"?>";
		echo "\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">";
		echo "\n<html xml:lang=\"$language\" lang=\"$language\" xmlns=\"http://www.w3.org/1999/xhtml\">";

		echo "\n<head>";
		echo sprintf("\n\t<title>%s</title>", $this->head["Title"]);
		echo sprintf("\n\t<meta http-equiv=\"content-type\" content=\"text/html; charset=\"%s\" />",
			$charset);

		echo sprintf( "\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" media=\"screen\" />",
			$this->head["StylesheetScreen"]);
		echo sprintf("\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" media=\"print\" />",
			$this->head["StylesheetPrint"]);
		echo "\n</head>";
		echo "\n\t<body>";
	}

	/**
	 * Prints the footer section
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @return void
	 */
	public function printFooter ()
	{
		$this->database["connection"]->close();
		echo ("\n</body></html>");
	}

	/**
	 * Method to a clean string, which is the result of GPC's (Get/Post/Cookie) and
	 * <ul>
	 * <li>make them usefull for f.e. SQL statements and PHP</li>
	 * <li>clean garbage code (attempts to corrupt incoming data)</li>
	 * </ul>
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param string $string2clean
	 * @param bool $stripTags if true, all tags except b, i, u will be stripped
	 * @param bool $convert2entity if true, convert special HTML characters like > to entity codes
	 * @param bool $fromPost if true, get posted parameter
	 * @return void
	 */
	public function getCleanGPCString ($string2clean, $stripTags = true, $convert2entity = false, $fromPost = true)
	{
		if ($fromPost)
		{
			if (isset($_POST[$string2clean]))
				$retVal = $_POST[$string2clean];
			elseif (isset($_GET[$string2clean]))
				$retVal = $_GET[$string2clean];
			else
				$retVal = "";
		}
		else
			$retVal = $string2clean;
		if (strncmp($string2clean, "chk", 3) == 0)
			$retVal = $retVal == "true" || $retVal == "1" ? "1" : "0";

		//	Strip HTML and PHP tags and allow <b><i><u> only
		if ($stripTags)
			$retVal = strip_tags($retVal, "<b><i><u>");

		//	Convert special HTML characters like > to entity codes
		if ($convert2entity)
			$retVal = htmlspecialchars($retVal);

		return $retVal;
	}


	/*******************************************************************************
	* Methods - data
	********************************************************************************/
	/**
	 * Method to convert parameter to a SQL parameter, ready to insert/update
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param mixed $par
	 * @param bool $forMySql, optional
	 * @return mixed
	 */
	public function getSqlParameter ($par, $forMySql = true)
	{
		return $this->database["writer"]->getSqlParameter($par);
	}

	/**
	 * Method to clean exec a SQL command and handle it nicely
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param string $sql
	 * @return int. If the SQL statement is an INSERT, the inserted ID, else the number of affected records.
	 */
	public function execSql($sql)
	{
		return $this->database["writer"]->execSql($sql);
	}

	/**
	 * Function to exec a SQL script
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param array $commands
	 * @return bool for success
	 */
	public function execScript ($commands)
	{
		$retVal = is_array($commands);	//	Get script lines

		if ($retVal)
		{
			reset($commands);
			$value = current($commands);
			$cnt = count($commands);
			for ($i = 0; $i <= $cnt; $i++)
			{
				if (!$value) continue;
				$this->execSql($value);
				$value = next($commands);
			}
		}
		return $retVal;
	}

	/**
	 * Method to get a resultset and handle common errors nicely
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param string $sql
	 * @param object $dataReader. Passed by reference. Will be set with an instance of imslib_dataReader.
	 * @return bool
	 */
	public function getDataSet($sql, &$dataReader)
	{
		if (!$this->database["connection"]->isOpen())
			$sql = "";

		$dataReader = new imslib_dataReader($sql);
		$retVal = $dataReader->hasNext();

		return $retVal;
	}

	/**
	 * Method to get one row
	 *
	 * @since version 1.0.0
	 * @author H.F.N. den Boer <hide@address.com>
	 * @access public
	 * @param string $sql
	 * @param string $field of which content should be retrieved
	 * @return mixed
	 */
	public function getOneRow($sql, $field = null)
	{
		$dataReader = null;
		$row = null;
		if (!$this->database["connection"]->isOpen())
			$retVal = null;
		else
		{
			$retVal = $this->getDataSet($sql, $dataReader);
			unset($sql);
		}

		if ($retVal)
			$retVal = ($dataReader->getCount() == 1);

		if ($retVal)
			$row = $dataReader->getNext();

		if ($retVal && isset($field))
			$row = $dataReader->getCleanDbString($row[$field]);

		unset($dataReader);

		return $row;
	}
}

/*******************************************************************************
* Initialise backbone
********************************************************************************/
$imslib_Backbone = new imslib_backBone();

/**
 * Classes for interaction with MySql
 */
require_once("classes.mySql.inc.php");

?>
Return current item: Database metadata