<?php
require_once($_SERVER['APPL_PHYSICAL_PATH'].'/inc/CSException.class.php');
require_once($_SERVER['APPL_PHYSICAL_PATH'].'/inc/system.inc.php');
require_once($_SERVER['APPL_PHYSICAL_PATH'].'/inc/CSIniFile.class.php');
require_once($_SERVER['APPL_PHYSICAL_PATH'].'/inc/CSDatabase.class.php');
define('CSARG_ISSET', 0);
define('CSARG_EMPTY', 1);
define('CSERR_THROW', 0);
define('CSERR_ERROR', 1);
// Flags for CS::Check
define('CSA_ISSET', 0x0001);
define('CSA_EMPTY', 0x0002);
define('CSA_THROW', 0x0004);
define('CSA_IP4', 0x0008);
define('CSA_EMAIL', 0x0010);
define('CSA_THREADCNT', 0x0020);
define('CSA_PATH', 0x0040);
class CS
{
private $ini;
public function getIni() { return $this->ini; }
private $db;
public function getDb() { return $this->db; }
public function __construct($iniFile)
{
try {
$this->ini = new CSIniFile($iniFile);
$this->db = new CSDatabase(
$this->ini->getKey('db', 'server'),
$this->ini->getKey('db', 'username'),
$this->ini->getKey('db', 'password'),
$this->ini->getKey('db', 'database'),
$this->ini->getKey('db', 'port')
);
}
catch (Exception $e) {
throw $e; // Throw it back to the caller
}
}
public function __destruct() { }
// GetOption
// Reads the database for the specified parameter, and returns it.
//
public function GetOption($p)
{
$rec = $this->db->GetOneRecord('SELECT `'.$p.'` FROM `options`');
if (!$rec)
throw new Exception('CS::GetOption(): could not read option');
return $rec[$p];
}
// #####################################################################################
// Static methods, available globally
public static function PrintCopyright() // Phase this out
{
CS::PrintHtmlCopyright();
}
public static function PrintHtmlCopyright()
{
echo '<div class="Copyright">Copyright © 2006-2008 Alasdair McWilliam.</p>';
}
public static function PrintHtmlHeader($pageTitle = FALSE, $customHeaders = FALSE, $usingFrameset = FALSE, $noCachePragma = TRUE)
{
global $CS_URL_PREPEND;
$hBuf = array();
//
// Our doctype depends on whether this is a frame layout
//
if ($usingFrameset) {
$hBuf[] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
} else {
$hBuf[] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
}
//
// Now we output generic contents
$hBuf[] = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
$hBuf[] = '<head>';
$hBuf[] = '<!-- Network Configuration Store (NCS) Version '.CS_VERSION.' -->';
if (!empty($pageTitle)) {
$hBuf[] = '<title>'.$pageTitle.' - NCS</title>';
} else {
$hBuf[] = '<title>Network Configuration Store</title>';
}
$hBuf[] = '<link rel="stylesheet" type="text/css" href="'.$CS_URL_PREPEND.'assets/ncs.css" />';
$hBuf[] = '<script type="text/javascript" src="'.$CS_URL_PREPEND.'assets/ncs.js"></script>';
$hBuf[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
if ($noCachePragma) {
$hBuf[] = '<meta http-equiv="Pragma" content="no-cache" />';
}
//
// Load any additional per-page headers
if (is_array($customHeaders))
{
foreach($customHeaders as $html)
$hBuf[] = $html;
}
//
// Complete the header
$hBuf[] = '</head>';
//
// Print it !
foreach($hBuf as $html)
echo "$html\n";
}
private static function DisplayException($e)
{
// See if we need to send any HTML headers.
if (!headers_sent())
{
CS::PrintHtmlHeader(($e instanceof CSException) ? 'Application Error' : 'System Error');
echo '<body id="Main">';
}
// If this is an instance of CSException, it's a friendly NCS error.
// Otherwise it's a system exception and needs debugging.
//
if ($e instanceof CSException)
{
echo '<h1>An error has occured...</h1>';
echo '<p><strong>'.$e->getMessage().'</strong></p>';
}
else
{
echo '<h1 class="Warning">Internal System Error!</h1>';
echo '<p><strong>'.$e->getMessage().'</strong></p>';
echo '<table width="100%">'
.'<tr class="Odd"><td><strong>Requested Resource</strong></td><td>'.$_SERVER['SCRIPT_NAME'].'</td></tr>'
.'<tr class="Oven"><td><strong>Exception Source</strong></td><td>'.$e->getFile().':'.$e->getLine().'</td></tr>'
.'<tr class="Odd"><td><strong>Traceback</strong></td><td>'.nl2br($e->getTraceAsString()).'</td></tr>'
.'</table>';
echo '<p>Please report the above exception backtrace to your system administrator with'
.'a description of the action you attempted to perform.</p>';
}
echo '<p><a href="javascript:history.back(1);">Back</a></p>';
echo '</body></html>';
exit(); // Aieeee...! What a world...
}
public static function Abort($thisException)
{
CS::DisplayException($thisException);
}
public static function Check(&$arg, $argFlags, $throwMessage = null)
{
if ($argFlags & CSA_EMPTY)
{
if (!empty($arg))
return true;
}
elseif ($argFlags & CSA_ISSET)
{
if (isset($arg))
return true;
}
elseif ($argFlags & CSA_IP4)
{
if (!empty($arg) && ip2long($arg))
return true;
}
elseif ($argFlags & CSA_EMAIL)
{
if (!empty($arg) && strrpos($arg, '@') !== false)
return true;
}
elseif ($argFlags & CSA_THREADCNT)
{
if (!empty($arg) && ($arg >= 1) && ($arg <= 50))
return true;
}
elseif ($argFlags & CSA_PATH)
{
if (!empty($arg)) // FIXME
return true;
}
else
{
throw new Exception('CS::Check() called with invalid argFlags');
}
if ($argFlags & CSA_THROW)
throw new Exception(!empty($throwMessage) ? $throwMessage : 'An unspecified argument has failed validation.');
return false;
}
public static function CheckPageArg(&$Arg, $CheckLevel = CSARG_ISSET, $Action = CSERR_THROW)
{
throw new Exception('CS::CheckPageArg() called but is obsolete');
}
public static function GetFileContents($file)
{
// This is the succesor to ReadFile
if (file_exists($file))
{
$data = file($file);
if ($data)
return $data;
}
throw new CSFileReadException($file);
}
public static function GetVersionInfo($GetBuild = FALSE, $EchoIt = FALSE)
{
$version = CS_VERSION;
$build = '';
$branch = '';
if ($GetBuild)
{
try {
$tag = CS::GetFileContents(dirname($_SERVER['APPL_PHYSICAL_PATH']).'/.build');
$tags = explode(':', trim($tag[0]));
$branch = $tags[0];
$build = $tags[1];
}
catch (Exception $e) { } // Silence CSFileReadException
}
return array($version, $build, $branch);
}
}
try {
$CS = new CS(dirname($_SERVER['APPL_PHYSICAL_PATH']).'/db.ini');
} catch (Exception $e) {
CS::Abort(new Exception('FATAL ERROR - Cannot load NCS - '.$e->getMessage()));
}
?>