1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2004-2007 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class dhcpNetwork extends plugin
22 {
23 /* Used attributes */
24 var $options= array();
25 var $statements= array();
27 /* attribute list for save action */
28 var $attributes= array();
29 var $objectclasses= array();
31 function dhcpNetwork()
32 {
33 /* This is always an account */
34 $this->is_account= TRUE;
35 }
37 function execute()
38 {
39 /* Check for iteraction */
40 if (isset($_POST['add_dns']) && $_POST['addserver'] != ""){
41 if (!preg_match('/^[0-9a-z.-]+$/', $_POST['addserver'])){
42 print_red(_("The name of the DNS server your're going to add is not valid!"));
43 } else {
44 $servers= array();
45 if (isset($this->options['domain-name-servers'])){
46 foreach(split(",", $this->options['domain-name-servers']) as $val){
47 $servers[$val]= $val;
48 }
49 }
50 $servers[$_POST['addserver']]= $_POST['addserver'];
51 $tmp= "";
52 foreach($servers as $val){
53 $tmp.= $val.",";
54 }
55 $this->options['domain-name-servers']= preg_replace('/,$/', '', $tmp);
56 }
57 }
58 if (isset($_POST['delete_dns']) && isset($_POST['dnsserver'])){
59 $tmp= preg_replace("/(\s*,\s*)?".$_POST['dnsserver']."/i", '',
60 $this->options['domain-name-servers']);
61 $tmp= preg_replace("/(\s*)?,(\s*)?$/", '', $tmp);
62 if ($tmp != ""){
63 $this->options['domain-name-servers']= $tmp;
64 } else {
65 unset($this->options['domain-name-servers']);
66 }
67 }
69 /* Show main page */
70 $smarty= get_smarty();
72 /*
73 * Assemble options
74 */
76 /* Router */
77 if (isset($this->options['routers'])){
78 $smarty->assign("routers", $this->options['routers']);
79 } else {
80 $smarty->assign("routers", "");
81 }
83 /* DNS */
84 if (isset($this->options['domain-name'])){
85 $smarty->assign("domain", trim($this->options['domain-name'], '"'));
86 } else {
87 $smarty->assign("domain", "");
88 }
89 if (isset($this->options['domain-name-servers'])){
90 $servers= array();
91 foreach(split(",", $this->options['domain-name-servers']) as $val){
92 $servers[$val]= $val;
93 }
94 $smarty->assign("dnsservers", $servers);
95 } else {
96 $smarty->assign("dnsservers", "");
97 }
99 /* Netmask / Broadcast */
100 if (isset($this->options['subnet-mask'])){
101 $this->options['subnet-mask']= normalize_netmask($this->options['subnet-mask']);
102 $smarty->assign("subnet_mask", $this->options['subnet-mask']);
103 } else {
104 $smarty->assign("subnet_mask", "");
105 }
106 if (isset($this->options['broadcast-address'])){
107 $smarty->assign("broadcast_address", $this->options['broadcast-address']);
108 } else {
109 $smarty->assign("broadcast_address", "");
110 }
112 /* Boot stuff */
113 if (isset($this->statements['filename'])){
114 $smarty->assign("filename", trim($this->statements['filename'], '"'));
115 } else {
116 $smarty->assign("filename", "");
117 }
118 if (isset($this->statements['next-server'])){
119 $smarty->assign("nextserver", trim($this->statements['next-server'], '"'));
120 } else {
121 $smarty->assign("nextserver", "");
122 }
124 /* Set flags */
125 $smarty->assign("autohost", "");
126 if (isset($this->statements['get-lease-hostnames'])){
127 if (preg_match('/^(true|on|yes)$/', $this->statements['get-lease-hostnames'])){
128 $smarty->assign("autohost", "checked");
129 }
130 }
131 $smarty->assign("autohostdecl", "");
132 if (isset($this->statements['use-host-decl-names'])){
133 if (preg_match('/^(true|on|yes)$/', $this->statements['use-host-decl-names'])){
134 $smarty->assign("autohostdecl", "checked");
135 }
136 }
138 return $smarty->fetch(get_template_path('dhcp_network.tpl', TRUE));
139 }
141 function remove_from_parent()
142 {
143 }
146 /* Save data to object */
147 function save_object()
148 {
149 /* Only save, if we are "active" */
150 if (isset($_POST['routers'])){
152 /*
153 * Assemble options
154 */
156 /* Options */
157 foreach (array("routers" => "routers", "domain-name" => "domain") as $key => $val){
159 if ($_POST["$val"] == ''){
160 unset($this->options["$key"]);
161 } else {
162 $this->options["$key"]= $_POST["$val"];
163 }
164 }
166 /* Statements */
167 foreach (array("filename" => "filename", "next-server" => "nextserver") as $key => $val){
168 if ($_POST["$val"] == ''){
169 unset($this->statements["$key"]);
170 } else {
171 $this->statements["$key"]= '"'.$_POST["$val"].'"';
172 }
173 }
175 /* Netmask / Broadcast */
176 foreach (array("subnet-mask" => "nm", "broadcast-address" => "bc")
177 as $key => $val){
179 $tmp= "";
180 for ($i= 0; $i<4; $i++){
181 $tmp.= $_POST["$val$i"].".";
182 }
183 if ($tmp == "...."){
184 unset($this->options["$key"]);
185 } else {
186 $this->options["$key"]= preg_replace('/\.$/', '', $tmp);
187 }
188 }
190 /* Flags */
191 if (isset ($_POST['autohost'])){
192 $this->statements['get-lease-hostnames']= "true";
193 } else {
194 unset($this->statements['get-lease-hostnames']);
195 }
196 if (isset ($_POST['autohostdecl'])){
197 $this->statements['use-host-decl-names']= "on";
198 } else {
199 unset($this->statements['use-host-decl-names']);
200 }
201 }
202 }
205 /* Check values */
206 function check()
207 {
208 $message= array();
210 /* Check netmask and broadcast */
211 foreach(array("subnet-mask" => _("Netmask"), "broadcast-address" => _("Broadcast")) as $key => $typ){
212 if (!isset($this->options["$key"])){
213 continue;
214 }
215 $tmp= preg_replace('/^[^\s]+\s/', '', $this->options["$key"]);
217 list($n0,$n1,$n2,$n3)= split('\.', $tmp);
218 for ($i= 0; $i<4; $i++){
219 $name= "n$i";
220 if (preg_match('/^[0-9]+$/', $$name)){
221 $val= (int)($$name);
222 if ($val < 0 || $val > 255){
223 $message[]= sprintf(_("Error in definition of '%s'!"), $typ);
224 break;
225 }
226 } else {
227 $message[]= sprintf(_("Illegal characters in definition of '%s'!"), $typ);
228 break;
229 }
230 }
231 }
233 #FIXME: There are some more things we could test -> valid netmask, range
234 return $message;
235 }
238 /* Save to LDAP */
239 function save()
240 {
241 }
243 }
245 ?>