";
}
}
return $ret;
}
/**
* getMenu - resturns HTML code for sidebar menu
*
* @param string $menu currently displayed menu
* @param string $activelink link that will be marked as selected
* @param string $type index or admin
* @return string
*/
function getMenu($menu = '', $activelink = '', $type) {
if (!isset($menu)) { return ""; };
while(list($key, $val) = each($menu)) {
$bCont = TRUE;
$q = $this->GetBetween($key,"q=","&m=");
if ($this->configarray["queryarray"][$q]["notforlibclient"] == 1 && $this->configarray["serverlist"][$this->GETVars['server']]["libraryclient"] == 1) {
$bCont = FALSE;
}
$key = $_SERVER['PHP_SELF']."?".$key;
if ($type == "index") {
$key .= "&s=".$this->GETVars['server'];
}
if ($val == "Admin") {
$key = "admin.php";
}else if ($val == "TSM Monitor") {
$key = "index.php";
}
if ($val == "trennlinie") {
$links .= " \n";
} else if ($bCont) {
if (!stristr($key,$activelink)) {
$links .= "$val\n";
} else {
$links .= "
$val
\n";
}
}
}
return $links;
}
/**
* getMessage - returns HTML code for global system message
*
* @return string
*/
function getMessage() {
return "
System Message:
".$this->message."
";
}
/**
* getInfo - returns HTML Code for Infoboxes with information about current query
*
* @return string
*/
function getInfo() {
$label = $this->configarray["queryarray"][$this->GETVars['qq']]["label"];
$info = $this->configarray["queryarray"][$this->GETVars['qq']]["info"];
if ($info != "") {
$ret = "
".$label.":
".$info;
$ret .= "
";
return $ret;
}
}
/**
* getTableheader - generates and returns headers for query result HTML tables.
*
* @return string
*/
function getTableheader() {
$tableheader="
";
$orderby = $this->configarray["queryarray"][$this->GETVars['qq']]["orderby"];
$orderdir = $this->GETVars['orderdir'];
$this->page = $_GET['page'];
if ($orderdir == "asc") {
$sonew="desc";
} else if ($orderdir == "desc") {
$sonew="asc";
}
$isAdmin = strstr($_SERVER['PHP_SELF'], 'admin.php');
if ($isAdmin) {
$sql = "SHOW COLUMNS FROM cfg_".$_GET['q'];
} else {
$sql = "SHOW COLUMNS FROM res_".$this->configarray["queryarray"][$this->GETVars['qq']]["name"]."_".$this->GETVars['server'];
}
$fieldnames = $this->fetchArrayDB($sql, $this->conn);
// If table has more than one column
if (sizeof($fieldnames) > 1) {
foreach ($fieldnames as $col) {
if ($col['Field'] != "timestamp" && $col['Field'] != "id") {
$name = $col['Field'];
$arrow = "";
if (($this->GETVars['ob'] == $name && $this->GETVars['ob']!="") || ($this->GETVars['ob']=="" && $orderby!="" && $orderby == $name)) {
$link = "href='".$_SERVER['PHP_SELF']."?q=".$this->GETVars['qq']."&m=".$this->GETVars['menu']."&sort=".$name."&page=".$this->page."&so=".$sonew."&s=".$this->GETVars['server']."'";
if ($orderdir == "asc") {
$arrow = "⇑";
} else if ($orderdir == "desc") {
$arrow = "⇓";
}
} else {
$arrow = "";
$link = "href='".$_SERVER['PHP_SELF']."?q=".$this->GETVars['qq']."&m=".$this->GETVars['menu']."&sort=".$name."&page=".$this->page."&s=".$this->GETVars['server']."'";
}
$tableheader = $tableheader."
\n";
}
return $out;
}
/**
* generateTimetableHeader - returns HTML code for timetable header (display of hours)
*
* @param string $startpunkt I forgot that one
* @param string $FirstCol first field of result table
* @return string
*/
function generateTimetableHeader($startpunkt = '', $FirstCol = '') {
$header = $FirstCol["label"];
$out= "
";
return $out;
}
/**
* getConfigArray - queries the DB and generates the global config array
*
* @return array
*/
function getConfigArray() {
$retArray = array();
// Navigation
$query = "SELECT * from cfg_mainmenu";
$mainmenutablerows = $this->fetchArrayDB($query, $this->conn);
$ret = array();
$menuarray = array();
$mainmenuarray = array();
while (list ($key, $val) = each ($mainmenutablerows)) {
$menuname = $val['name'];
$menulabel = $val['label'];
$url = "q=overview&m=".$menuname;
$mainmenuarray[$url] = $menulabel;
}
$menuarrayxml = $queryconfigarray["navigation"]["mainmenuitem"];
$mainmenuarrayxml = $menuarrayxml;
$mainmenuarray["trennlinie"] = "trennlinie";
$mainmenuarray["q=polldstat&m=main"] = "Polling Daemon Log";
$mainmenuarray["q=serverlist&m=main"] = "Change Server";
if ($_SESSION["logindata"]["role"] == "admin") $mainmenuarray["admin"] = "Admin";
$mainmenuarray["q=logout"] = "Logout";
$menuarray["main"] = $mainmenuarray;
$query = "SELECT * from cfg_mainmenu";
$mainmenutablerows = $this->fetchArrayDB($query, $this->conn);
$query = "SELECT * from cfg_queries";
$querytablerows = $this->fetchArrayDB($query, $this->conn);
while (list ($key, $val) = each ($mainmenutablerows)) {
$menuname = $val['name'];
$menulabel = $val['label'];
$submenuarray = array();
$submenuarray[""] = "<---";
$query = "SELECT * from cfg_queries where parent='".$menuname."'";
$querytablerows = $this->fetchArrayDB($query, $this->conn);
while (list ($subkey, $submenuitem) = each ($querytablerows)) {
$submenuitem_name = $submenuitem['name'];
$submenuitem_label = $submenuitem['label'];
$url = "q=".$submenuitem_name."&m=".$menuname;
$submenuarray[$url] = $submenuitem_label;
}
$submenuarray["trennlinie"] = "trennlinie";
$submenuarray["q=polldstat&m=".$submenu['name']] = "Polling Daemon Log";
$submenuarray["q=serverlist&m=".$submenu['name']] = "Change Server";
if ($_SESSION["logindata"]["role"] == "admin") $submenuarray["admin"] = "Admin";
$submenuarray["q=logout"] = "Logout";
$menuarray[$menuname] = $submenuarray;
}
$retArray["menuarray"] = $menuarray;
// Admin Backend Menu
$adminmenuarray = array();
$adminmenuarray["q=config&m=main"] = "General";
$adminmenuarray["q=users&m=main"] = "Users";
$adminmenuarray["q=groups&m=main"] = "Groups";
$adminmenuarray["q=servers&m=main"] = "Servers";
$adminmenuarray["q=mainmenu&m=main"] = "Mainmenu";
$adminmenuarray["q=queries&m=main"] = "Queries";
$adminmenuarray["trennlinie"] = "trennlinie";
$adminmenuarray["q=settings&m=main"] = "Settings";
$adminmenuarray["trennlinie2"] = "trennlinie";
$adminmenuarray["tsmmonitor"] = "TSM Monitor";
$adminmenuarray["q=logout"] = "Logout";
$retArray["adminmenuarray"] = $adminmenuarray;
// Overview Boxes
$ret = array();
$query = "SELECT * from cfg_overviewboxes order by sortorder asc";
$queryoverviewboxes = $this->fetchArrayDB($query, $this->conn);
while (list ($subkey, $box) = each ($queryoverviewboxes)) {
$query = "SELECT * from cfg_overviewqueries where parent='".$box['name']."' order by sortorder asc";
$queryoverview = $this->fetchArrayDB($query, $this->conn);
$temp = array ();
//print_r($queryoverview);
while (list ($subkey, $ovquery) = each ($queryoverview)) {
$ovquery['header'] = $queryoverview['name'];
$temp[] = (array)$ovquery;
}
$ret[$box['name']] = $temp;
}
$retArray["infoboxarray"] = $ret;
// Queries
$dbret = array();
$query = "SELECT * from cfg_queries";
$querytablerows = $this->fetchArrayDB($query, $this->conn);
while (list ($subkey, $queryrow) = each ($querytablerows)) {
$dbret[$queryrow['name']] = (array)$queryrow;
}
$retArray["queryarray"] = $dbret;
// General settings
$query = "SELECT * from cfg_config";
$rows = $this->fetchArrayDB($query, $this->conn);
$ret = array();
foreach ($rows as $key => $val) {
$ret[$val['confkey']] = $val['confval'];
}
$retArray["settings"] = $ret;
// Set Stylesheet
$query = "SELECT stylesheet from cfg_users where username='".$_SESSION["logindata"]["user"]."'";
$row = $this->fetchArrayDB($query, $this->conn);
$retArray["stylesheet"] = $row[0]['stylesheet'];
// Colors
$query = "SELECT * from cfg_colors";
$rows = $this->fetchArrayDB($query, $this->conn);
$ret = array();
while (list ($key, $val) = each ($rows)) {
$ret[$val['name']] = $val['value'];
}
$retArray["colorsarray"] = $ret;
// Servers
$query = "SELECT * from cfg_servers";
$rows = $this->fetchArrayDB($query, $this->conn);
$ret = array();
while (list ($key, $val) = each ($rows)) {
$ret[$val['servername']] = (array)$val;
if ($val['default'] == 1) {
$retArray["defaultserver"] = $val['servername'];
}
}
$retArray["serverlist"] = $ret;
return $retArray;
}
/**
* findPath - find a external program in the search path
*
* @param string $binary the external program to search for
* @param string $search_path the search path in which to look for the external program
* @return string the full path to the external program or empty string if not found
*/
function findPath($binary, $search_path) {
foreach ($search_path as $path) {
if ((file_exists($path . "/" . $binary)) && (is_readable($path . "/" . $binary))) {
return($path . "/" . $binary);
}
}
}
/**
* connectDB - establish a DB connection via ADODB
*
* @param string $host the hostname of the DB server
* @param string $port the portnumber for the DB connection
* @param string $user the username for the DB connection
* @param string $pass the password for the DB connection
* @param string $db_name the name of the DB
* @param string $db_type the type of the DB (currently only 'mysql')
* @param string $retr the number attempts for the DB connection before a failure is reported
* @return ADOConnection DB connection ID or error code if connection failed
*/
function connectDB($host, $port = "3306", $user, $pass, $db_name, $db_type, $retr = 20) {
$try = 0;
$hostport = $host . ":" . $port;
$this->conn = NewADOConnection($db_type);
while ($try <= $retries) {
if ($this->conn->PConnect($hostport,$user,$pass,$db_name)) {
return($this->conn);
}
$try++;
usleep(50000);
}
die("FATAL: Cannot connect to database server on '$host':'$port'. Please make sure you have specified a valid database name in 'includes/config.php'\n");
return 0;
}
/**
* closeDB - close an open DB connection
*
* @param ADOConnection $DBconn DB connection ID to be closed
* @return string
*/
function closeDB($DBconn = FALSE) {
if ($DBconn) {
return $DBconn->Close();
}
}
/**
* execDB - execute a SQL statement against the DB via ADODB
*
* @param string $sql SQL statement to execute
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @return ADORecordSet
*/
function execDB($sql, $DBconn = FALSE) {
// $DBconn->debug = true;
$sql = $this->sanitizeSQL($sql);
$recordSet = &$DBconn->Execute($sql);
if (($recordSet) || ($DBconn->ErrorNo() == 0)) {
return($recordSet);
} else {
echo "
Database Error (".$DBconn->ErrorNo().")
\n
".$DBconn->ErrorMsg()."
";
exit;
}
}
/**
* fetchCellDB - execute a SQL query against the DB via ADODB and
* return only the first column of the fist row found
* or a specified column of the fist row found
*
* @param string $sql SQL statement to execute
* @param $column_name Column name to use instead of the first column
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @return string Content of the cell as a single variable
*/
function fetchCellDB($sql, $column_name, $DBconn = FALSE) {
//$DBconn->debug = true;
$sql = $this->sanitizeSQL($sql);
if ($column_name != '') {
$DBconn->SetFetchMode(ADODB_FETCH_ASSOC);
} else {
$DBconn->SetFetchMode(ADODB_FETCH_NUM);
}
$recordSet = $DBconn->Execute($sql);
if (($recordSet) || ($DBconn->ErrorNo() == 0)) {
if (!$recordSet->EOF) {
if ($column_name != '') {
$column = $recordSet->fields[$column_name];
}else{
$column = $recordSet->fields[0];
}
$recordSet->close();
return($column);
}
} else {
echo "
Database Error (".$DBconn->ErrorNo().")
\n
".$DBconn->ErrorMsg()."
";
exit;
}
}
/**
* fetchRowDB - execute a SQL query against the DB via ADODB
* and return only the first row found
*
* @param string $sql SQL statement to execute
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @return array First row of results as an associative array
*/
function fetchRowDB($sql, $DBconn = FALSE) {
//$DBconn->debug = true;
$sql = $this->sanitizeSQL($sql);
$DBconn->SetFetchMode(ADODB_FETCH_ASSOC);
$recordSet = $DBconn->Execute($sql);
if (($recordSet) || ($DBconn->ErrorNo() == 0)) {
if (!$recordSet->EOF) {
$recordFields = $recordSet->fields;
$recordSet->close();
return($recordFields);
}
} else {
echo "
Database Error (".$DBconn->ErrorNo().")
\n
".$DBconn->ErrorMsg()."
";
exit;
}
}
/**
* fetchArrayDB - execute a SQL query against the DB via ADODB
* and return results in an associative array.
*
* @param string $sql SQL statement to execute
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @return array All results in an associative array
*/
function fetchArrayDB($sql, $DBconn = FALSE) {
//$DBconn->debug = true;
$sql = $this->sanitizeSQL($sql);
$recordArray = array();
$DBconn->SetFetchMode(ADODB_FETCH_ASSOC);
$recordSet = &$DBconn->Execute($sql);
if (($recordSet) || ($DBconn->ErrorNo() == 0)) {
while ((!$recordSet->EOF) && ($recordSet)) {
$recordArray{sizeof($recordArray)} = $recordSet->fields;
$recordSet->MoveNext();
}
$recordSet->close();
return($recordArray);
} else {
echo "
Database Error (".$DBconn->ErrorNo().")
\n
".$DBconn->ErrorMsg()."
";
exit;
}
}
/**
* $this->fetchSplitArrayDB - execute a SQL query against the DB via ADODB
* and return results in an associative array.
*
* @param string $sql SQL statement to execute
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @param string $rows_per_page number of rows per page a result will have
* @return array All results in an associative array
*/
function fetchSplitArrayDB($sql, $DBconn = FALSE, $rows_per_page = '20') {
// $DBconn->debug = true;
$this->page = intval($_GET['page']);
$sql = $this->sanitizeSQL($sql);
$recordArray = array();
$DBconn->SetFetchMode(ADODB_FETCH_ASSOC);
$recordSet = &$DBconn->Execute($sql);
if (($recordSet) || ($DBconn->ErrorNo() == 0)) {
$total_rows = $recordSet->RecordCount($recordSet);
$this->max_pages = ceil($total_rows/$rows_per_page);
if($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
$offset = $rows_per_page * ($this->page-1);
$endset = $offset + $rows_per_page;
$recordSet->Move($offset);
while (($recordSet->CurrentRow() < $endset) && ($recordSet->CurrentRow() < $total_rows) && ($recordSet)) {
$recordArray{sizeof($recordArray)} = $recordSet->fields;
$recordSet->MoveNext();
}
$recordSet->close();
return($recordArray);
} else {
echo "
Database Error (".$DBconn->ErrorNo().")
\n
".$DBconn->ErrorMsg()."
";
exit;
}
}
/**
* updateDB - execute a SQL update statement against the DB via ADODB
* to update a record. If the record is not found, an insert
* statement is generated and executed.
*
* @param string $table The name of the table containing the record to be updated
* @param array $cells An array of columnname/value pairs of the record to be updated
* @param string $keys Name of the primary key
* @param boolean $autoquote Use intelligent auto-quoting
* @param ADOConnection $DBconn DB connection ID to run the SQL against
* @return string Auto-increment ID if insert was performed
*/
function updateDB($table, $cells, $keys, $DBconn = FALSE, $autoquote = TRUE) {
//$DBconn->debug = true;
$DBconn->Replace($table, $cells, $keys, $autoquote);
return $DBconn->Insert_ID();
}
/**
* sanitizeSQL - removes unwanted chars in values passed for use in
* SQL statements
*
* @param string $sql SQL expression to sanitize
* @return string
*/
function sanitizeSQL($sql) {
$sql = str_replace(";", "\;", $sql);
$sql = str_replace("\n", "", $sql);
$sql = str_replace("\r", "", $sql);
$sql = str_replace("\t", " ", $sql);
return $sql;
}
}
?>