bcast-bridge/web/include/db_Bridge.class.php

127 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2006-12-11 21:36:44 +01:00
<?php
class Bridge {
private $inDB = false;
public $id;
public $name;
public $ctrlip = "0.0.0.0";
public $basemac = "00:00:00:00:00:00";
private function fromRow($row) {
$retval = new Bridge();
if ($row) {
$retval->inDB = true;
$retval->id = (int)$row['id'];
$retval->name = (string)$row['name'];
$retval->ctrlip = (string)$row['ctrlip'];
$retval->basemac = (string)$row['basemac'];
}
return $retval;
}
private function toSql() {
return "name = '".mysql_real_escape_string($this->name)."',
ctrlip = INET_ATON('".mysql_real_escape_string($this->ctrlip)."'),
basemac = '".mysql_real_escape_string($this->basemac)."'";
}
function load($id) {
$dbh = Database::getInstance();
$sql = "SELECT id, name, INET_NTOA(ctrlip) AS ctrlip, basemac
FROM bridge
WHERE id = '{$id}'";
$dbh->query($sql);
return self::fromRow($dbh->fetch_assoc());
}
function save() {
$dbh = Database::getInstance();
if ($this->inDB) {
$sql = "UPDATE bridge SET ".$this->toSql()."
WHERE id = '{$this->id}'";
$dbh->query($sql);
} else {
$sql = "INSERT bridge SET ".$this->toSql();
$dbh->query($sql);
$this->iNDB = true;
$this->id = $dbh->insert_id();
}
}
function delete($id) {
$dbh = Database::getInstance();
$sql = "DELETE FROM bridge
WHERE id = '{$id}'";
$dbh->query($sql);
$trunks = Trunk::getAll($id);
foreach ($trunks as $trunk)
Trunk::delete($trunk->id);
}
function getAll() {
$dbh = Database::getInstance();
$sql = "SELECT id, name, INET_NTOA(ctrlip) AS ctrlip, basemac
FROM bridge
ORDER BY name";
$dbh->query($sql);
$retval = array();
while ($row = $dbh->fetch_assoc())
$retval[$row['id']] = self::fromRow($row);
return $retval;
}
private function inc_mac($mac) {
$parts = explode(':', $mac);
foreach ($parts as $num => $part)
$parts[$num] = hexdec($part);
$inc = true;
for ($i = 5; $i >= 0; $i--) {
if ($inc)
$parts[$i]++;
if ($parts[$i] >= 0x100) {
$parts[$i] = 0x00;
$inc = true;
} else {
$inc = false;
}
}
foreach ($parts as $num => $part) {
$parts[$num] = strtoupper(dechex($part));
if ($part < 0x10)
$parts[$num] = "0".$parts[$num];
}
return implode(':', $parts);
}
function reorder_macs() {
$mac = $this->basemac;
$trunks = Trunk::getAll($this->id);
foreach ($trunks as $trunk) {
$vlans = Vlan::getAll($trunk->id);
foreach ($vlans as $vlan) {
$mac = self::inc_mac($mac);
$vlan->mac = $mac;
$vlan->save();
}
}
}
}
?>