";
diff --git a/includes/adodb.php b/includes/adodb.php
new file mode 100644
index 0000000..855ade7
--- /dev/null
+++ b/includes/adodb.php
@@ -0,0 +1,239 @@
+.
+
+************************************************************************
+*/
+
+/**
+ *
+ * adodb.php, TSM Monitor
+ *
+ * DB Stuff for TSM Monitor
+ *
+ * @author Frank Fegert, Michael Clemens
+ * @package tsmmonitor
+ */
+
+
+/**
+ *
+ * Class ADOdb
+ *
+ */
+
+class ADOdb {
+
+
+ var $conn;
+
+ /**
+ * constructor - establishes 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 0
+ */
+ function ADOdb($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)) {
+ $this->conn = $this->conn;
+ return 0;
+ }
+ $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
+ *
+ * @return string
+ */
+ function closeDB() {
+ if ($this->conn) {
+ return $this->conn->Close();
+ }
+ }
+
+
+ /**
+ * execDB - execute a SQL statement against the DB via ADODB
+ *
+ * @param string $sql SQL statement to execute
+ * @return ADORecordSet
+ */
+ function execDB($sql) {
+ // $this->conn->debug = true;
+ $sql = $this->sanitizeSQL($sql);
+
+ $recordSet = &$this->conn->Execute($sql);
+ if (($recordSet) || ($this->conn->ErrorNo() == 0)) {
+ return($recordSet);
+ } else {
+ echo "Database Error (".$this->conn->ErrorNo().")
\n".$this->conn->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
+ * @return string Content of the cell as a single variable
+ */
+ function fetchCellDB($sql, $column_name) {
+ //$this->conn->debug = true;
+ $sql = $this->sanitizeSQL($sql);
+
+ if ($column_name != '') {
+ $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);
+ } else {
+ $this->conn->SetFetchMode(ADODB_FETCH_NUM);
+ }
+ $recordSet = $this->conn->Execute($sql);
+
+ if (($recordSet) || ($this->conn->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 (".$this->conn->ErrorNo().")
\n".$this->conn->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
+ * @return array First row of results as an associative array
+ */
+ function fetchRowDB($sql) {
+ //$this->conn->debug = true;
+ $sql = $this->sanitizeSQL($sql);
+
+ $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);
+ $recordSet = $this->conn->Execute($sql);
+
+ if (($recordSet) || ($this->conn->ErrorNo() == 0)) {
+ if (!$recordSet->EOF) {
+ $recordFields = $recordSet->fields;
+ $recordSet->close();
+
+ return($recordFields);
+ }
+ } else {
+ echo "Database Error (".$this->conn->ErrorNo().")
\n".$this->conn->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
+ * @return array All results in an associative array
+ */
+ function fetchArrayDB($sql) {
+ //$this->conn->debug = true;
+ $sql = $this->sanitizeSQL($sql);
+
+ $recordArray = array();
+ $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);
+ $recordSet = &$this->conn->Execute($sql);
+
+ if (($recordSet) || ($this->conn->ErrorNo() == 0)) {
+ while ((!$recordSet->EOF) && ($recordSet)) {
+ $recordArray{sizeof($recordArray)} = $recordSet->fields;
+ $recordSet->MoveNext();
+ }
+ $recordSet->close();
+ return($recordArray);
+ } else {
+ echo "Database Error (".$this->conn->ErrorNo().")
\n".$this->conn->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 $this->conn DB connection ID to run the SQL against
+ * @return string Auto-increment ID if insert was performed
+ */
+ function updateDB($table, $cells, $keys, $autoquote = TRUE) {
+ //$this->conn->debug = true;
+ $this->conn->Replace($table, $cells, $keys, $autoquote);
+
+ return $this->conn->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;
+ }
+
+}
+
+?>
diff --git a/includes/global.php b/includes/global.php
index b5db00f..cc69205 100644
--- a/includes/global.php
+++ b/includes/global.php
@@ -84,25 +84,25 @@ header("Pragma: no-cache");
// ** Include generic code and external libraries ** //
include ($config["library_path"] . "/adodb5/adodb.inc.php");
+include_once($config["include_path"] . "/adodb.php");
include_once($config["include_path"] . "/tsmmonitor.php");
include_once($config["include_path"] . "/polld.php");
-// ** instantiate TSMMonitor Class ** //
-$tsmmonitor = new TSMMonitor();
-
// ** Connect to the database ** //
-//$conn = connectDB($db_host, $db_port, $db_user, $db_password, $db_name, $db_type);
-$tsmmonitor->conn = $tsmmonitor->connectDB($db_host, $db_port, $db_user, $db_password, $db_name, $db_type);
+$adodb = new ADOdb($db_host, $db_port, $db_user, $db_password, $db_name, $db_type);
+
+// ** instantiate TSMMonitor Class ** //
+$tsmmonitor = new TSMMonitor($adodb);
// check to see if this is a new installation
-$version = $tsmmonitor->fetchCellDB("select confval from cfg_config where confkey='version'", '', $tsmmonitor->conn);
+$version = $adodb->fetchCellDB("select confval from cfg_config where confkey='version'", '', $tsmmonitor->conn);
if ($version != $config["tsm_monitor_version"] && basename($_SERVER['REQUEST_URI']) != 'install.php') {
header("Location: install.php");
exit;
}
// ** Initialize PHP session ** //
//initialize();
-$tsmmonitor->initialize();
+//$tsmmonitor->initialize();
// ** Include generic code and external libraries ** //
// ... more includes here
diff --git a/includes/tsmmonitor.php b/includes/tsmmonitor.php
index fa9f9fc..a4edfe1 100644
--- a/includes/tsmmonitor.php
+++ b/includes/tsmmonitor.php
@@ -33,7 +33,7 @@
/**
*
- * Class
+ * Class TSMMonitor
*
*/
@@ -50,18 +50,19 @@ class TSMMonitor {
var $submenu;
var $adminmenu;
var $message;
- var $conn;
+ var $adodb;
/**
- * initialize - This function us called every time index.php is refreshed
+ * TSMMonitor - constructor
*
*/
- function initialize() {
+ function TSMMonitor($adodb) {
+ $this->adodb = $adodb;
session_name("tsmmonitordev");
session_start();
@@ -142,7 +143,7 @@ class TSMMonitor {
if (($_POST['Poll'] == "Poll Now!" || $_SESSION['timemachine']['date'] == "") && $this->queryarray[$this->GETVars['qq']]["polltype"]=="snapshot" || $_POST['s'] != "" && $this->GETVars['qq'] != "overview" && $this->GETVars['qq'] != "index") {
$qtable = $this->configarray["queryarray"][$this->GETVars['qq']]["name"];
$sql = "SELECT MAX(TimeStamp) FROM res_".$qtable."_".$this->GETVars["server"];
- $res = $this->fetchArrayDB($sql, $this->conn);
+ $res = $this->adodb->fetchArrayDB($sql);
$resarr = (array)$res[0];
$_SESSION['timemachine']['date'] = $resarr["MAX(TimeStamp)"];
$_SESSION['timemachine']['time'] = $resarr["MAX(TimeStamp)"];
@@ -163,6 +164,49 @@ class TSMMonitor {
+ /**
+ * $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 string $rows_per_page number of rows per page a result will have
+ * @return array All results in an associative array
+ */
+ function fetchSplitArrayDB($sql, $rows_per_page = '20') {
+ // $this->conn->debug = true;
+ $this->page = intval($_GET['page']);
+
+ $sql = $this->adodb->sanitizeSQL($sql);
+
+ $recordArray = array();
+ $this->adodb->conn->SetFetchMode(ADODB_FETCH_ASSOC);
+ $recordSet = $this->adodb->conn->Execute($sql);
+
+ if (($recordSet) || ($this->adodb->conn->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 (".$this->conn->ErrorNo().")
\n".$this->conn->ErrorMsg()."
";
+ exit;
+ }
+ }
+
+
+
/**
* showPageNavigation - generates a clickable navigation bar for sql results
* splitted by function fetchSplitArrayDB
@@ -428,7 +472,7 @@ class TSMMonitor {
} else {
$sql = "SHOW COLUMNS FROM res_".$this->configarray["queryarray"][$this->GETVars['qq']]["name"]."_".$this->GETVars['server'];
}
- $fieldnames = $this->fetchArrayDB($sql, $this->conn);
+ $fieldnames = $this->adodb->fetchArrayDB($sql);
// If table has more than one column
if (sizeof($fieldnames) > 1) {
@@ -483,7 +527,7 @@ class TSMMonitor {
if ($user != "" && $pass != "") {
$sql = "SELECT password, role from cfg_users where username='".$user."'";
- $ret = $this->fetchArrayDB($sql, $this->conn);
+ $ret = $this->adodb->fetchArrayDB($sql);
if ($ret[0] != "" && $ret[0]['password'] == md5($pass)) {
$_SESSION["logindata"]["role"] = $ret[0]['role'];
@@ -561,7 +605,7 @@ class TSMMonitor {
$timestampquery = " WHERE timestamp between ".$startofday." and ".$endofday;
$sql = "SELECT distinct timestamp from res_".$qtable."_".$server.$timestampquery;
- $ret = $this->fetchArrayDB($sql, $this->conn);
+ $ret = $this->adodb->fetchArrayDB($sql);
return $ret;
@@ -582,7 +626,7 @@ class TSMMonitor {
$qtable = $this->configarray["queryarray"][$this->GETVars['qq']]["name"];
$sql = "SELECT MAX(TimeStamp) from res_".$qtable."_".$server;
- $ret = $this->fetchArrayDB($sql, $this->conn);
+ $ret = $this->adodb->fetchArrayDB($sql);
$ret = (array)$ret[0];
return $ret["MAX(TimeStamp)"];
@@ -601,7 +645,7 @@ class TSMMonitor {
$sqlth = "SELECT * from ".$tablename." LIMIT 1";
- $sqlresth = $this->fetchArrayDB($sqlth, $this->conn);
+ $sqlresth = $this->adodb->fetchArrayDB($sqlth);
$columnnames = "";
// get all table fields to be selected
@@ -644,7 +688,7 @@ class TSMMonitor {
if ($sqlres) $this->message = $sql;
$i = 1;
- $rs = $this->fetchArrayDB($sql, $this->conn);
+ $rs = $this->adodb->fetchArrayDB($sql);
foreach ($rs as $row) {
if ($type=="list") {
@@ -669,7 +713,7 @@ class TSMMonitor {
$outp .= "\n";
} else {
- $outp = $this->fetchArrayDB($sql, $this->conn);
+ $outp = $this->adodb->fetchArrayDB($sql);
var_dump($outp);
}
}
@@ -752,7 +796,7 @@ class TSMMonitor {
if ($bContinue) {
if ($type == "table") {
$i = 1;
- $rs = $this->fetchSplitArrayDB($sql,$this->conn,20);
+ $rs = $this->fetchSplitArrayDB($sql,20);
foreach ($rs as $row) {
$color = "";
@@ -791,10 +835,10 @@ class TSMMonitor {
}
}
else if ($type == "verticaltable") {
- $outp = $this->fetchArrayDB($sql, $this->conn);
+ $outp = $this->adodb->fetchArrayDB($sql);
}
else if ($type == "timetable") {
- $sqlres = $this->fetchArrayDB($sql, $this->conn);
+ $sqlres = $this->adodb->fetchArrayDB($sql);
$outp = array();;
foreach ($sqlres as $row) {
$rowarray2 = array();
@@ -838,7 +882,7 @@ class TSMMonitor {
$arrop = $searcharr["op"];
}
$sql = "SHOW COLUMNS FROM res_".$this->configarray["queryarray"][$this->GETVars['qq']]["name"]."_".$this->GETVars['server'];
- $fieldnames = $this->fetchArrayDB($sql, $this->conn);
+ $fieldnames = $this->adodb->fetchArrayDB($sql);
// Build Field Name Combobox
$fieldbox = "