dn= $attrs['dn'];
$this->new= FALSE;
/* Load attributes */
foreach (array("cn", "dhcpHWAddress") as $attr){
if (isset($attrs[$attr][0])){
$this->$attr= $attrs[$attr][0];
}
}
/* Load options */
if (isset($attrs['dhcpOption'])){
foreach ($attrs['dhcpOption'] as $opt){
$idx= preg_replace('/\s.+$/', '', $opt);
$value= preg_replace('/^[^\s]+\s/', '', $opt);
$this->options[$idx]= $value;
}
}
/* Load statements */
if (isset($attrs['dhcpStatements'])){
foreach ($attrs['dhcpStatements'] as $opt){
$idx= preg_replace('/\s.+$/', '', $opt);
$value= preg_replace('/^[^\s]+\s/', '', $opt);
$this->statements[$idx]= $value;
}
}
} else {
/* We keep the parent dn here if it's new */
$this->dn= $attrs;
$this->new= TRUE;
}
/* Load network module */
$this->network= new dhcpNetwork();
$this->network->options= $this->options;
$this->network->statements= $this->statements;
$this->advanced= new dhcpAdvanced();
$this->advanced->options= $this->options;
$this->advanced->statements= $this->statements;
$this->advanced->setAutoStatements(array("fixed-address"));
/* Save CN for later reference */
$this->orig_cn= $this->cn;
}
function execute()
{
$smarty= get_smarty();
$smarty->assign("cn", $this->cn);
$smarty->assign("dhcpHWAddress", preg_replace('/^[^ ]+ /', '', $this->dhcpHWAddress));
/* Create fixed address */
if (isset($this->statements['fixed-address'])){
$smarty->assign("fixedaddr", $this->statements['fixed-address']);
} else {
$smarty->assign("fixedaddr", "");
}
/* Prepare hw type selector */
$hwtype= preg_replace('/\s.*$/', '', $this->dhcpHWAddress);
$smarty->assign("hwtype", $hwtype);
$smarty->assign("hwtypes", array("ethernet" => _("Ethernet"),
"fddi" => _("FDDI"),
"token-ring" => _("Token Ring")));
/* Show main page */
$display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE)).$this->network->execute();
/* Merge arrays for advanced view */
foreach (array("options", "statements") as $type){
$tmp= array_merge($this->$type, $this->network->$type);
$this->advanced->$type= $tmp;
}
$display.= $this->advanced->execute();
/* Merge back for removals */
foreach (array("options", "statements") as $type){
$this->$type= $this->advanced->$type;
$this->network->$type= $this->advanced->$type;
}
/* Add footer */
$display.= "
".
"
";
return ($display);
}
function remove_from_parent()
{
}
/* Save data to object */
function save_object()
{
plugin::save_object();
/* Save remaining attributes */
if (isset($_POST['hwtype'])){
/* Assemble hwAddress */
$this->dhcpHWAddress= $_POST['hwtype']." ".$_POST['dhcpHWAddress'];
$this->cn= validate($_POST['cn']);
/* Save fixed address */
if ($_POST['fixedaddr'] != ""){
$this->statements['fixed-address']= $_POST['fixedaddr'];
} else {
unset ($this->statements['fixed-address']);
}
}
/* Strip network objects */
foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
unset($this->options[$toberemoved]);
}
foreach (array("filename", "next-server") as $toberemoved){
unset($this->statements[$toberemoved]);
}
/* Save sub-objects */
$this->network->save_object();
$this->advanced->save_object();
/* Merge arrays for advanced view */
foreach (array("options", "statements") as $type){
$tmp= array_merge($this->$type, $this->network->$type);
$this->advanced->$type= $tmp;
}
}
/* Check values */
function check($cache)
{
$message= array();
/* All required fields are set? */
if ($this->cn == ""){
$message[]= _("Required field 'Name' is not filled.");
}
/* cn already used? */
if ($this->orig_cn != $this->cn || $this->new){
foreach($cache as $dn => $dummy){
if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
$message[]= _("The name for this host section is already used!");
break;
}
}
}
/* Check syntax of MAC address */
$check= preg_replace('/^[^\s]*\s/', '', $this->dhcpHWAddress);
if (!preg_match('/^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$/', $check)){
$message[]= _("The hardware address specified by you is not valid!");
}
/* Check external plugins */
$net= $this->network->check();
$adv= $this->advanced->check();
$message= array_merge($message, $net, $adv);
return $message;
}
/* Save to LDAP */
function save()
{
/* Merge arrays for network and advanced view */
foreach (array("options", "statements") as $type){
$tmp= array_merge($this->$type, $this->network->$type, $this->advanced->$type);
$this->$type= $tmp;
}
/* Add cn if we're new */
if ($this->new){
$this->dn= "cn=".$this->cn.",".$this->dn;
} else {
$this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
}
/* Assemble new entry - options */
$this->attrs['dhcpOption']= array();
if (isset ($this->options) && count ($this->options)){
foreach ($this->options as $key => $val){
$this->attrs['dhcpOption'][]= "$key $val";
}
}
/* Assemble new entry - statements */
$this->attrs['dhcpStatements']= array();
if (isset ($this->statements) && count ($this->statements)){
foreach ($this->statements as $key => $val){
$this->attrs['dhcpStatements'][]= "$key $val";
}
}
/* Move dn to the result */
$this->attrs['dn']= $this->dn;
$this->attrs['cn']= array($this->cn);
if ($this->dhcpHWAddress != ""){
$this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
} else {
$this->attrs['dhcpHWAddress']= array();
}
$this->attrs['objectClass']= array('top', 'dhcpHost');
$this->attrs['MODIFIED']= TRUE;
return ($this->attrs);
}
}
?>