Class Dbflib
Class Dbflib
php
// IPplan v4.92b
//
//
//
// You should have received a copy of the GNU General Public License
//
// versions of php
require_once(dirname(__FILE__)."/config.php");
define("DBF_API_VER", "1");
// generic errors
//$myError[100] =
class IPplanDbf {
var $ds;
var $expr="RLIKE";
var $size=0;
var $dhcp=0;
function IPplanDbf() {
// create a connection id
$this->ds = &ADONewConnection(DBF_TYPE);
$this->ds->debug = DBF_DEBUG;
if (DBF_PERSISTENT) {
// Fix issue with Reverse DNS imports that use nested database calls
$this->ds->autoRollback = FALSE;
if ($this->ds->
return $this->ds;
else {
if ($this->ds->
$this->ds->SetFetchMode(ADODB_FETCH_ASSOC);
return $this->ds;
unset($this->ds);
return false;
function DbfTransactionStart() {
if (DBF_TRANSACTIONS)
$this->ds->BeginTrans();
function DbfTransactionEnd() {
if (DBF_TRANSACTIONS)
$this->ds->CommitTrans();
}
function DbfTransactionRollback() {
if (DBF_TRANSACTIONS)
$this->ds->RollBackTrans();
// add a new ipaddress entry in the ipaddr table if it does not already exist
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $ipaddr)) {
// is too small!
$row["event"]=132;
$row["user"]=getAuthUsername();
$row["action"]="delete ip details";
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($ipaddr);
$this->ds->Execute("DELETE FROM ipaddr
ipaddr=$ipaddr") and
ipaddr=$ipaddr") and
$this->AuditLog($row);
// add a new ipaddress entry in the ipaddr table if it does not already exist
$userid = getAuthUsername();
// RE 30/3/2005 - should use FOR UPDATE on SELECT, but don't know compat?
// THIS GETS DONE TWICE!!! ONCE FOR AUDITLOG AND AGAIN HERE!
if($this->ds->GetRow("SELECT ipaddr
FROM ipaddr
SET userinf=".$this->ds->qstr($user).",
location=".$this->ds->qstr($location).",
telno=".$this->ds->qstr($telno).",
macaddr=".$this->ds->qstr($macaddr).",
descrip=".$this->ds->qstr($descrip).",
hname=".$this->ds->qstr($hname).",
lastmod=".$this->ds->DBTimeStamp(time()).",
userid=".$this->ds->qstr($userid)."
ipaddr=$ipaddr");
else {
VALUES
(".$this->ds->qstr($user).",
".$this->ds->qstr($location).",
".$this->ds->qstr($telno).",
".$this->ds->qstr($macaddr).",
".$this->ds->qstr($descrip).",
".$this->ds->qstr($hname).",
$baseindex,
$ipaddr,
".$this->ds->DBTimeStamp(time()).",
".$this->ds->qstr($userid).")");
}
// always try to update record - record could not exist, which
// to add DNS or nmap records - do not add record if info is empty - waste?
// to ipaddradd table
if($this->ds->GetRow("SELECT ipaddr
FROM ipaddradd
SET info=".$this->ds->qstr($info)."
ipaddr=$ipaddr");
else {
if (!empty($info)) {
VALUES
(".$this->ds->qstr($info).",
$baseindex,
$ipaddr)");
}
}
$userid = getAuthUsername();
SET $field=".$this->ds->qstr($value).",
lastmod=".$this->ds->DBTimeStamp(time()).",
userid=".$this->ds->qstr($userid)."
return $this->ds->Affected_Rows();
if (is_array($ipaddr)) {
foreach($ipaddr as $value) {
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $value)) {
// is too small!
$row["event"]=130;
$row["user"]=getAuthUsername();
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($value);
$this->AuditLog($row);
if ($user) {
if (!$this->
if ($location) {
if (!$this->
$location))
if ($descrip) {
if (!$this->
$descrip))
if ($telno) {
if (!$this->
if ($macaddr) {
if (!$this->
if ($hname) {
if (!$this->
$this->
AuditLog(sprintf
(my_
("User %s modified ip details %s index %u"),
getAuthUsername(),
inet_ntoa($value), $baseindex));
else {
// lock the row for update - at the same time get current row
if($row=$this->GetIPDetails($baseindex, $ipaddr)) {
// is too small!
$row["event"]=130;
$row["user"]=getAuthUsername();
$row["baseindex"]=$baseindex;
$row["ip"]=inet_ntoa($ipaddr);
$this->AuditLog($row);
"descrip"=>$descrip, "hname"=>$hname));
}
}
$userid = getAuthUsername();
VALUES
($baseaddr, $subnetsize,
".$this->ds->qstr($descrip).",
".$this->ds->qstr($grp).",
$dhcp,
$cust,
".$this->ds->qstr($userid).",
".$this->ds->DBTimeStamp(time()).")");
return $this->ds->Insert_ID();
else {
$temprow = $result->FetchRow();
return $temprow["baseindex"];
base.customer=customer.customer AND
customer.admingrp=usergrp.grp AND
usergrp.userid=".$this->ds->qstr($userid));
if ($row = $result->FetchRow()) {
return $row["admingrp"];
else
return 0;
function TestCustomerCreate($userid) {
// could use GetRow here
usergrp.grp=grp.grp AND
grp.createcust=".$this->ds->qstr('Y'));
if ($row = $result->FetchRow()) {
return $row["grp"];
else
return 0;
function GetBaseGrp($baseindex) {
FROM base
WHERE baseindex=$baseindex");
if ($row = $result->FetchRow()) {
return $row["admingrp"];
else
return 0;
}
// delete a subnet
function DeleteSubnet($baseindex) {
WHERE baseindex=$baseindex");
return $result;
function grpSQL($grps) {
foreach($grps as $value) {
$string .= $this->ds->qstr($value).",";
return $this->ds->
FROM base
baseaddr + subnetsize - 1) OR
$baseaddr+$subnetsize >
customer=$cust");
FROM bounds
WHERE grp=".$this->ds->qstr($grp));
$row = $result->FetchRow();
return 1;
FROM bounds
grp=".$this->ds->qstr($grp));
if ($result->FetchRow()) {
return 1;
return 0;
// need groups here as some users may not see all customers
$sql = "";
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
}
}
return $this->ds->
base.descrip, customer.custdescrip,
$sqlswipmod AS swipmod
base.baseaddr + base.subnetsize - 1) OR
$baseaddr+$subnetsize >
base.customer = customer.customer
$sql");
$sql = "";
if ($descrip) {
if ($this->size > 0) {
$sql.=" AND base.subnetsize > $this->size ";
return $this->ds->
FROM base
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=netrange.customer
WHERE base.customer=$cust
$sql
ORDER BY
base.baseaddr");
// not all fields are used, but get them anyway so function is
// more generic
$sql = "";
if ($descrip) {
$sql=$this->mySearchSql("base.descrip", $this->expr, $descrip);
if ($this->size > 0) {
if ($cust == 0) {
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
return $this->ds->
customer.custdescrip, customer.customer
WHERE netrange.areaindex=$areaindex
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=customer.customer
$sql
ORDER BY
base.baseaddr");
else {
return $this->ds->
WHERE netrange.areaindex=$areaindex
AND netrange.rangeaddr+netrange.rangesize-1
AND base.customer=$cust
AND netrange.customer=$cust
$sql
ORDER BY
base.baseaddr");
// not all fields are used, but get them anyway so function is
// more generic
$sql = "";
if ($descrip) {
$sql=$this->mySearchSql("base.descrip", $this->expr, $descrip);
if ($this->size > 0) {
if ($this->dhcp > 0) {
if ($cust == 0) {
if (!empty($grps)) {
if (!$this->TestGrpsAdmin($grps)) {
return $this->ds->
customer.custdescrip, customer.customer
AND base.customer=customer.customer
$sql
ORDER BY
base.baseaddr");
else {
return $this->ds->
FROM base
$sql
AND base.customer=$cust
ORDER BY
base.baseaddr");
function GetBaseFromIndex($baseindex) {
baseopt
FROM base
WHERE baseindex=$baseindex");
}
// find base address info from a ip
// query looks odd, but record may not exist, but would still
descrip, baseaddr
FROM base
baseaddr+subnetsize-1 AND
customer=$cust");
FROM ipaddr
ORDER BY
ipaddr");
}
// get one ip record from a subnet based on a baseindex and ipaddr
FROM ipaddr
// update the poll date of the address - will add a record if required
SET lastpol=".$this->ds->DBTimeStamp(time())."
ipaddr=$ipaddr");
if ($this->ds->Affected_Rows() == 0) {
VALUES
(".$this->ds->qstr("").",
".$this->ds->qstr("").",
".$this->ds->qstr("").",
".$this->ds->qstr("").",
$baseindex,
$ipaddr,
".$this->ds->DBTimeStamp(time()).",
".$this->ds->DBTimeStamp(time()).",
".$this->ds->qstr("POLLER").")");
function TestGrpsAdmin($grps) {
if (!empty($grps)) {
$result = $this->GetGrpsInfo($grps);
if ($row["grpopt"] & 1)
return TRUE;
return FALSE;
function GetGrps() {
return $this->ds->Execute("SELECT grpdescrip, grp
FROM grp
ORDER BY grpdescrip");
// don't pass empty grps array as you will generate database error
function GetGrpsInfo($grps) {
$sql = $this->grpSQL($grps);
return $this->ds->
FROM grp
function GetCustomer($sql="") {
if (empty($sql)) {
FROM customer
ORDER BY custdescrip");
else {
FROM customer
WHERE $sql
ORDER BY custdescrip");
function GetCustomerInfo($cust) {
return $this->ds->Execute("SELECT *
FROM custinfo
WHERE customer=$cust");
function GetCustomerDNSInfo($cust) {
FROM revdns
WHERE customer=$cust
ORDER BY horder");
function GetCustomerDescrip($cust) {
FROM customer
WHERE customer=$cust");
return $row["custdescrip"];
}
function GetCustomerGrp($cust) {
if ($cust == 0) {
return $this->ds->
FROM customer
ORDER BY custdescrip");
else {
return $this->ds->
FROM customer
WHERE customer=$cust");
return $this->ds->
FROM netrange
WHERE areaindex=$areaindex
ORDER BY rangeaddr");
if (!$rangeindex) {
return $this->ds->
FROM netrange
WHERE customer=$cust
ORDER BY rangeaddr");
else {
return $this->ds->
FROM netrange
WHERE rangeindex=$rangeindex
ORDER BY rangeaddr");
if ($areaindex==0) {
FROM area
WHERE customer=$cust
ORDER BY areaaddr");
else if ($areaindex==-1) {
netrange.areaindex=area.areaindex
ORDER BY area.areaaddr");
else {
FROM area
WHERE areaindex=$areaindex
ORDER BY areaaddr");
$sql="";
if (is_array($ip)) {
foreach($ip as $value) {
$sql .= "$value,";
}
}
else {
$sql = "$ip,";
$result =
FROM ipaddr
GROUP BY lastmod");
$row = $result->FetchRow();
return $row["md5str"];
else {
FROM ipaddr
$md5str="";
$md5str .= $row["lastmod"];
return md5($md5str);
}
// eg array("event"=>100)
function AuditLog($message) {
if (AUDIT) {
if (is_string($message)) {
VALUES
(".$this->ds->qstr(substr($message,0,254)).",
".$this->ds->qstr(getAuthUsername()).",
".$this->ds->DBTimeStamp(time()).")");
else if (is_array($message)) {
$newmsg="";
foreach($msgarr as $value) {
VALUES
(".$this->ds->qstr(substr($value,0,254)).",
".$this->ds->qstr(getAuthUsername()).",
".$this->ds->DBTimeStamp(time()).")");
if (EXT_FUNCTION) {
user_trigger($message);
// build an sql query for the search - $var contain field, type is expression,
$sql="";
if (!empty($search)) {
switch ($expr) {
case "NLIKE":
break;
case "EXACT":
$sql="$var = ".$this->ds->qstr("$search");
break;
case "RLIKE":
return "";
if (DBF_TYPE=="mysql" or DBF_TYPE=="maxsql") {
break;
if (DBF_TYPE=="postgres7") {
$sql="$var ~ ".$this->ds->qstr("$search");
break;
case "NRLIKE":
return "";
if (DBF_TYPE=="mysql" or DBF_TYPE=="maxsql") {
break;
if (DBF_TYPE=="postgres7") {
case "LIKE":
break;
case "END":
break;
default:
if ($addand) {
return $sql;
// form variables
var $cust;
var $areaindex;
var $rangeindex;
var $searchin = 0;
var $grps;
var $start;
var $end;
var $subnetsize = 0; // used for searching over a netrange - called from findfree
var $err = 0;
function SetGrps($grps) {
$this->grps = $grps;
function SetIPaddr($ipaddr) {
$this->ipaddr = $ipaddr;
function SetSearchIn($searchin) {
$this->searchin = $searchin;
}
function SetDescrip($descrip) {
$this->descrip = $descrip;
function SetSubnetSize($subnetsize) {
$this->subnetsize = $subnetsize;
$this->cust = $cust;
$this->rangeindex = $rangeindex;
$this->areaindex = $areaindex;
if ($this->rangeindex) {
$row = $result->FetchRow();
$this->start = inet_ntoa($row["rangeaddr"]);
$this->end =
else {
if ($this->ipaddr) {
if ($this->subnetsize) {
$this->start = $this->ipaddr;
$this->end = inet_ntoa(inet_aton($this->ipaddr)+$this->subnetsize-1);
else {
if (testIP($this->start) or testIP($this->end)) {
return FALSE;
else {
$this->start = DEFAULTROUTE;
$this->end = ALLNETS;
$startnum = inet_aton($this->start);
$endnum = inet_aton($this->end);
if ($this->cust == 0) {
$this->custdescrip = "All";
else {
$this->custdescrip = $this->GetCustomerDescrip($this->cust);
if (strtolower($this->custdescrip) == "all")
$this->cust = 0;
$result =
$result =
$this->GetBaseFromArea($this->areaindex, $this->descrip,
$this->cust, $this->grps);
else {
if ($this->searchin == 0) {
$result =
$this->cust, $this->grps);
}
else {
if ($this->cust == 0) {
$result =
$this->GetDuplicateSubnetAll($startnum, 1,
$this->grps);
else {
$result =
$this->GetDuplicateSubnet($startnum, 1,
$this->cust);
return $result;
?>