Changes

Jump to navigation Jump to search
Line 307: Line 307:     
</syntaxhighlight>
 
</syntaxhighlight>
 +
Things to notice are the use of the "Stash" to communicate between the controller and the .ep file (mainly by pointers to data structures) and the need to pass the $c data structure through the routines as a parameter.
 +
 +
The Main_Save procedure is stolen almost entirely from the original formMagick code:<syntaxhighlight lang="perl">
 +
sub Main_Save ($){
 +
 +
    ##Pull CGI object from parameters array
 +
    my $q = shift;
 +
 +
    ###Build Hash of config parameters to update from cgi submit
 +
    my $dhcpd_status    = $q->param ('dhcp_enable');
 +
    my $dhcpd_winscustom    = $q->param ('dhcp_winscustom');
 +
    my $dhcpd_check    = $q->param ('dhcp_check');
 +
    my $dhcpd_start    = $q->param ('dhcp_start');
 +
    my $dhcpd_end        = $q->param ('dhcp_end');
 +
    my $dhcpd_winsserver    = $q->param ('dhcp_winsserver');
 +
    my $dhcpd_leasetime    = $q->param ('dhcp_leasetime');
 +
    my $dhcpd_dnscustom    = $q->param ('dhcp_dnscustom');
 +
    my $dhcpd_dns1server    = $q->param ('dhcp_dns1server');
 +
    my $dhcpd_dns2server    = $q->param ('dhcp_dns2server');
 +
    my $dhcpd_dns3server    = $q->param ('dhcp_dns3server');
 +
    my $dhcpd_gatewaycustom = $q->param ('dhcp_gatewaycustom');
 +
    my $dhcpd_gateway    = $q->param ('dhcp_gateway');
 +
     
 +
    ###Update SME configuration dbase
 +
    my $dbh_sme = esmith::ConfigDB->open('/home/e-smith/db/configuration');
 +
 +
    ##Initiate get method --> create record object
 +
    my $sme_record  = $dbh_sme->get('dhcpd');
 +
    #get localip of server
 +
    my $local_ip    = $dbh_sme->get_value('LocalIP');
 +
 +
    ##Set status of service
 +
    $sme_record->set_prop('status', $dhcpd_status);
 +
    $sme_record->set_prop('check' , $dhcpd_check);
 +
    $sme_record->set_prop('winscustom', $dhcpd_winscustom);
 +
    $sme_record->set_prop('leasetime' , $dhcpd_leasetime);
 +
    $sme_record->set_prop('dnscustom' , $dhcpd_dnscustom);
 +
    $sme_record->set_prop('gatewaycustom' , $dhcpd_gatewaycustom);
 +
 +
    #checkip to the dhcpserver, perform the save in DB configuration or display an error if value != of a valid ip or if dhcp_start is greater than dhcp_end
 +
   
 +
   
 +
    if ($dhcpd_status eq "enabled")
 +
    {
 +
        if ( isValidIP ($dhcpd_start) && isValidIP ($dhcpd_end))
 +
        {
 +
            #check if $dhcpd_start is greater than $dhcpd_end and if yes, display an error message.
 +
         
 +
            if (inet_aton($dhcpd_start) ge inet_aton($dhcpd_end))
 +
            {
 +
            return $q->l('dhcpd_DHCP_START_GREATER_DHCP_END_ERRORS') . ' (' . $dhcpd_start . '/' . $dhcpd_end .')';
 +
            }
 +
            elsif ( ( (inet_aton($dhcpd_start) le inet_aton($local_ip) ) && ( inet_aton($dhcpd_end)) ge inet_aton($local_ip) ) )
 +
            {
 +
            #display an error if the range of dhcp server include the ip of the server address
 +
            return $q->l('dhcpd_DHCP_RANGE_MUST_NOT_INCLUDE_SERVER_IP') . ' (' . $local_ip . ')';
 +
            }
 +
            else
 +
            {
 +
            #set value
 +
            my $dhcpd_start = cleanIP($dhcpd_start);
 +
            my $dhcpd_end = cleanIP($dhcpd_end);
 +
 +
            $sme_record->set_prop('end', $dhcpd_end);
 +
            $sme_record->set_prop('start', $dhcpd_start);
 +
            }
 +
        } 
 +
        #if $dhcpd_start or $dhcpd_end are not valid ip then display an error
 +
        else
 +
        {
 +
        return $q->l('dhcpd_DHCP_RANGE_WITH_BAD_IP') . ' (' . $dhcpd_start . '/' . $dhcpd_end .')';
 +
        } 
 +
    } 
 +
     
 +
    #checkip to the winserver perform the save in DB configuration or display an error if value != of a valid ip
 +
    if ($dhcpd_winscustom eq "enabled")
 +
    {
 +
 +
        if ( isValidIP ($dhcpd_winsserver) )
 +
        {
 +
        #set value
 +
        my $dhcpd_winsserver = cleanIP($dhcpd_winsserver);
 +
        $dbh_sme->set_prop('smb','WINSServer', $dhcpd_winsserver);
 +
        } 
 +
        else
 +
        {
 +
        #if $dhcpd_winsserver is not valid ip then display an error
 +
        return $q->l('dhcpd_WINSSERVER_BAD_IP') . ' (' . $dhcpd_winsserver .')';
 +
        }
 +
    }
 +
    elsif ($dhcpd_winscustom eq "disabled")
 +
    {
 +
        my $delws = $dbh_sme->get('smb');
 +
        $delws->delete_prop('WINSServer');
 +
    }
 +
 +
    #checkip to the dnsserver custom, perform the save in DB configuration or display an error if value != of a valid ip
 +
    if ($dhcpd_dnscustom eq "enabled")
 +
    {
 +
        #check if $dhcpd_dns1server and ( $dhcpd_dns2server are valid ip or $dhcpd_dns2server = null )
 +
        if ( isValidIP ($dhcpd_dns1server) &&  (isValidIP($dhcpd_dns2server) || ( $dhcpd_dns2server eq "")  ) &&  (isValidIP($dhcpd_dns3server) || ( $dhcpd_dns3server eq "")  )  )
 +
        {
 +
        #set value
 +
        my $dhcpd_dns1server = cleanIP($dhcpd_dns1server);
 +
        $sme_record->set_prop('dns1server' , $dhcpd_dns1server);
 +
        my $dhcpd_dns2server = cleanIP($dhcpd_dns2server);
 +
        $sme_record->set_prop('dns2server' , $dhcpd_dns2server);
 +
        my $dhcpd_dns3server = cleanIP($dhcpd_dns3server);
 +
        $sme_record->set_prop('dns3server' , $dhcpd_dns3server);
 +
        }
 +
        else
 +
        {
 +
        ##if $dhcpd_dns1server or $dhcpd_dns2server or $dhcpd_dns3server are not valid ip then display an error
 +
        return $q->l('dhcpd_DNS_SERVER_WITH_BAD_IP') . ' (' . $dhcpd_dns1server . '/' . $dhcpd_dns2server . '/' . $dhcpd_dns3server .')';
 +
        }
 +
    }
 +
 +
    #checkip to the gateway_custom perform the save in DB configuration or display an error if value != of a valid ip
 +
    if ($dhcpd_gatewaycustom eq "enabled")
 +
    {
 +
        if ( isValidIP ($dhcpd_gateway) )
 +
        {
 +
        #set value
 +
        my $dhcpd_gateway = cleanIP($dhcpd_gateway);
 +
        $sme_record->set_prop('gateway' , $dhcpd_gateway);
 +
        } 
 +
        else
 +
        {       
 +
        #if $dhcpd_gateway is not valid ip then display an error
 +
        return $q->l('dhcpd_GATEWAY_BAD_IP') . ' (' . $dhcpd_gateway .')';
 +
        }
 +
    }   
 +
   
 +
 +
    # - 4 expand templates
 +
    # changed to new sme standard signal-event
 +
    system ("/sbin/e-smith/signal-event","workgroup-update") == 0
 +
                or die "Error while saving settings: $!";
 +
  return 'ok';
 +
    exit;
 +
}
 +
</syntaxhighlight>The Message names had to have "dhcpd_" added to them, the localise routine name needed editing. $q replaces $c in other routines for legacy reasons.  Note the return string is either an error message OR the "ok" string.
    
==The Template Files (dhcpd.html.ep and partials)==
 
==The Template Files (dhcpd.html.ep and partials)==

Navigation menu