Project

General

Profile

Download (13.6 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
    wizard.php
6
    Copyright (C) 2004 Scott Ullrich
7
    All rights reserved.
8

    
9
    Redistribution and use in source and binary forms, with or without
10
    modification, are permitted provided that the following conditions are met:
11

    
12
    1. Redistributions of source code must retain the above copyright notice,
13
       this list of conditions and the following disclaimer.
14

    
15
    2. Redistributions in binary form must reproduce the above copyright
16
       notice, this list of conditions and the following disclaimer in the
17
       documentation and/or other materials provided with the distribution.
18

    
19
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
    POSSIBILITY OF SUCH DAMAGE.
29
*/
30

    
31
require("guiconfig.inc");
32
require("xmlparse_pkg.inc");
33

    
34
function gentitle_pkg($pgname) {
35
	global $config;
36
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
37
}
38

    
39
$stepid = $_GET['stepid'];
40
if (isset($_POST['stepid']))
41
    $stepid = $_POST['stepid'];
42
if (!$stepid) $stepid = "0";
43

    
44
// XXX: Make this input safe.
45
$xml = $_GET['xml'];
46
if($_POST['xml']) $xml = $_POST['xml'];
47

    
48
if($xml == "") {
49
	$xml = "not_defined";
50
	print_info_box_np("ERROR:  Could not open " . $xml . ".");
51
	die;
52
} else {
53
	if (file_exists("{$g['www_path']}/wizards/{$xml}"))
54
		$pkg = parse_xml_config_pkg("{$g['www_path']}/wizards/" . $xml, "pfsensewizard");
55
	else {
56
		print_info_box_np("ERROR:  Could not open " . $xml . ".");
57
		die;
58
	}
59
}
60

    
61
$title          = $pkg['step'][$stepid]['title'];
62
$description    = $pkg['step'][$stepid]['description'];
63
$totalsteps     = $pkg['totalsteps'];
64

    
65
exec('/usr/bin/tar -tzf /usr/share/zoneinfo.tgz', $timezonelist);
66
$timezonelist = array_filter($timezonelist, 'is_timezone');
67
sort($timezonelist);
68

    
69
if($pkg['step'][$stepid]['stepsubmitbeforesave']) {
70
		eval($pkg['step'][$stepid]['stepsubmitbeforesave']);
71
}
72

    
73
if ($_POST) {
74
    foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
75
        if($field['bindstofield'] <> "" and $field['type'] <> "submit") {
76
		$fieldname = $field['name'];
77
		$unset_fields = "";
78
		$fieldname = ereg_replace(" ", "", $fieldname);
79
		$fieldname = strtolower($fieldname);
80
		// update field with posted values.
81
                if($field['unsetfield'] <> "") $unset_fields = "yes";
82
		if($field['arraynum'] <> "") $arraynum = $field['arraynum'];
83
		if($field['bindstofield'])
84
			update_config_field( $field['bindstofield'], $_POST[$fieldname], $unset_fields, $arraynum);
85
        }
86

    
87
    }
88
    // run custom php code embedded in xml config.
89
    if($pkg['step'][$stepid]['stepsubmitphpaction'] <> "") {
90
		eval($pkg['step'][$stepid]['stepsubmitphpaction']);
91
    }
92
	write_config();
93
    $stepid++;
94
    if($stepid > $totalsteps) $stepid = $totalsteps;
95
}
96

    
97
$title          = $pkg['step'][$stepid]['title'];
98
$description    = $pkg['step'][$stepid]['description'];
99

    
100
function update_config_field($field, $updatetext, $unset, $arraynum) {
101
	global $config;
102
	$field_split = split("->",$field);
103
	foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
104
	if($field_conv == "") return;
105
	if($unset <> "") {
106
		$text = "unset(\$config" . $field_conv . ");";
107
		eval($text);
108
		$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
109
		eval($text);
110
	} else {
111
		if($arraynum <> "") {
112
			$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
113
		} else {
114
			$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
115
		}
116
		eval($text);
117
	}
118
}
119

    
120
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
121
	// handle before form display event.
122
        // good for modifying posted values, etc.
123
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
124
}
125

    
126
?>
127
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
128
<html>
129
<head>
130
<title><?=gentitle_pkg($title);?></title>
131
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
132
<link href="gui.css" rel="stylesheet" type="text/css">
133

    
134
<script Language="JavaScript">
135
<!--
136
function FieldValidate(userinput, regexp, message)
137
{
138
        if(!userinput.match(regexp))
139
                alert(message);
140
}
141
//-->
142
</script>
143

    
144
</head>
145

    
146
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
147
<form action="wizard.php" method="post">
148
<input type="hidden" name="xml" value="<?= $xml ?>">
149
<input type="hidden" name="stepid" value="<?= $stepid ?>">
150
<?php if ($savemsg) print_info_box($savemsg); ?>
151

    
152
<center>
153

    
154
&nbsp;<br>
155

    
156
<a href="/"><img border="0" src="/logo.gif"></a>
157
<p>
158

    
159
<table width="600" cellspacing="0" cellpadding="3">
160
    <!-- wizard goes here -->
161
    <tr><td>&nbsp;</td></tr>
162
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
163
    <?php
164
	if(!$pkg['step'][$stepid]['disableheader'])
165
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
166
    ?>
167

    
168
    <?php
169
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
170
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
171

    
172
		    $value = $field['value'];
173
		    $name  = $field['name'];
174

    
175
		    $name = ereg_replace(" ", "", $name);
176
		    $name = strtolower($name);
177

    
178
		    if($field['bindstofield'] <> "") {
179
				$arraynum = "";
180
				$field_conv = "";
181
				$field_split = split("->", $field['bindstofield']);
182
				// arraynum is used in cases where there is an array of the same field
183
				// name such as dnsserver (2 of them)
184
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
185
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
186
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
187
					eval($toeval);
188
					if ($field['type'] == "checkbox") {
189
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
190
						eval($toeval);
191
					}
192
		    }
193

    
194
		    if(!$field['combinefieldsend'])
195
			echo "<tr>";
196

    
197
		    if ($field['type'] == "input") {
198
			if(!$field['dontdisplayname']) {
199
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
200
				echo fixup_string($field['name']);
201
				echo ":</td>\n";
202
			}
203
			if(!$field['dontcombinecells'])
204
				echo "<td class=\"vtable\">\n";
205

    
206
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'";
207
			if($field['validate'])
208
				echo " onChange='FieldValidate(this.value, \"{$field['validate']}\", \"{$field['message']}\");'";
209
			echo ">\n";
210
		    } else if ($field['type'] == "password") {
211
			if(!$field['dontdisplayname']) {
212
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
213
				echo fixup_string($field['name']);
214
				echo ":</td>\n";
215
			}
216
			if(!$field['dontcombinecells'])
217
				echo "<td class=\"vtable\">";
218
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
219
		    } else if ($field['type'] == "select") {
220
			if(!$field['dontdisplayname']) {
221
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
222
				echo fixup_string($field['name']);
223
				echo ":</td>\n";
224
			}
225
			// XXX: TODO: set $selected
226
			if($field['size']) $size = " size='" . $field['size'] . "' ";
227
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
228
			if(!$field['dontcombinecells'])
229
				echo "<td class=\"vtable\">\n";
230
			$onchange = "";
231
			foreach ($field['options']['option'] as $opt) {
232
				if($opt['enablefields'] <> "") {
233
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
234
				}
235
			}
236
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
237
			foreach ($field['options']['option'] as $opt) {
238
				$selected = "";
239
				if($value == $opt['value']) $selected = " SELECTED";
240
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
241
			}
242
			echo "</select>\n";
243
		    } else if ($field['type'] == "textarea") {
244
			if(!$field['dontdisplayname']) {
245
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
246
				echo fixup_string($field['name']);
247
				echo ":</td>";
248
			}
249
			if(!$field['dontcombinecells'])
250
				echo "<td class=\"vtable\">";
251
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
252
		    } else if ($field['type'] == "submit") {
253
			echo "<td>&nbsp;<br></td></tr>";
254
			echo "<tr><td colspan='2'><center>";
255
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
256
		    } else if ($field['type'] == "listtopic") {
257
			echo "<td>&nbsp;</td><tr>";
258
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
259
		    } else if ($field['type'] == "subnet_select") {
260
			if(!$field['dontdisplayname']) {
261
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
262
				echo fixup_string($field['name']);
263
				echo ":</td>";
264
			}
265
			if(!$field['dontcombinecells'])
266
				echo "<td class=\"vtable\">";
267
			echo "<select name='{$name}'>\n";
268
			for($x=1; $x<33; $x++) {
269
				$CHECKED = "";
270
				if($value == $x) $CHECKED = " SELECTED";
271
				if($x <> 31)
272
					echo "<option value='{$x}' {$CHECKED}>{$x}</option>\n";
273
			}
274
			echo "</select>\n";
275
		    } else if ($field['type'] == "timezone_select") {
276
			if(!$field['dontdisplayname']) {
277
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
278
				echo fixup_string($field['name']);
279
				echo ":</td>";
280
			}
281
			if(!$field['dontcombinecells'])
282
				echo "<td class=\"vtable\">";
283
			echo "<select name='{$name}'>\n";
284
			foreach ($timezonelist as $tz) {
285
				$SELECTED = "";
286
				if ($value == $tz) $SELECTED = " SELECTED";
287
				echo "<option value='" . htmlspecialchars($tz) . "' {$SELECTED}>";
288
				echo htmlspecialchars($tz);
289
				echo "</option>\n";
290
			}
291
			echo "</select>\n";
292
		    } else if ($field['type'] == "checkbox") {
293
			if(!$field['dontdisplayname']) {
294
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
295
				echo $field['name'];
296
				echo ":</td>";
297
			}
298
			$checked = "";
299
			if($value <> "") $checked = " CHECKED";
300
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked . ">\n";
301
		    }
302

    
303
		    if($field['typehint'] <> "") {
304
			echo $field['typehint'];
305
		    }
306

    
307
		    if($field['description'] <> "") {
308
			echo "<br>" . $field['description'];
309
			echo "</td>";
310
		    }
311

    
312
		    if(!$field['combinefieldsbegin'])
313
			 echo "</tr>\n";
314
		}
315
	}
316
    ?>
317
</table>
318

    
319
</form>
320
</body>
321
</html>
322

    
323
<?php
324

    
325
$fieldnames_array = Array();
326
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
327
	// create a fieldname loop that can be used with javascript
328
	// hide and enable features.
329
	echo "\n<script language=\"JavaScript\">\n";
330
	echo "function disableall() {\n";
331
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
332
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
333
			if(!$field['donotdisable'] <> "") {
334
				array_push($fieldnames_array, $field['name']);
335
				$fieldname = ereg_replace(" ", "", $field['name']);
336
				$fieldname = strtolower($fieldname);
337
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
338
			}
339
		}
340
	}
341
	echo "}\ndisableall();\n";
342
	echo "function enableitems(selectedindex) {\n";
343
	echo "disableall();\n";
344
	$idcounter = 0;
345
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
346
		echo "\tswitch(selectedindex) {\n";
347
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
348
			if($field['options']['option'] <> "") {
349
				foreach ($field['options']['option'] as $opt) {
350
					if($opt['enablefields'] <> "") {
351
						echo "\t\tcase " . $idcounter . ":\n";
352
						$enablefields_split = split(",", $opt['enablefields']);
353
						foreach ($enablefields_split as $efs) {
354
							$fieldname = ereg_replace(" ", "", $efs);
355
							$fieldname = strtolower($fieldname);
356
							if($fieldname <> "") {
357
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
358
								echo $onchange;
359
							}
360
						}
361
						echo "\t\t\tbreak;\n";
362
					}
363
					$idcounter = $idcounter + 1;
364
				}
365
			}
366
		}
367
		echo "\t}\n";
368
	}
369
	echo "}\n";
370
	echo "disableall();\n";
371
	echo "</script>\n\n";
372
}
373

    
374

    
375
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
376
	// handle after form display event.
377
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
378
}
379

    
380
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
381
	// handle after form display event.
382
        echo "\n<script language=\"JavaScript\">\n";
383
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
384
	echo "</script>\n\n";
385
}
386

    
387
/*
388
 *  HELPER FUNCTIONS
389
 */
390

    
391
function fixup_string($string) {
392
	global $config, $myurl;
393
	// fixup #1: $myurl -> http[s]://ip_address:port/
394
	$https = "";
395
	$port = "";
396
	$urlport = "";
397
	$port = $config['system']['webguiport'];
398
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
399
	if($config['system']['webguiproto'] == "https") $https = "s";
400
	//$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
401
        $myurl = "http" . $https . "://" . $config['lan']['ip'];
402
	$newstring = str_replace("\$myurl", $myurl, $string);
403
	$string = $newstring;
404
	// fixup #2: $wanip
405
	$curwanip = get_current_wan_address();
406
	$newstring = str_replace("\$wanip", $curwanip, $string);
407
	$string = $newstring;
408
	// fixup #3: $lanip
409
	$lancfg = $config['interfaces']['lan'];
410
	$lanip = $lancfg['ipaddr'];
411
	$newstring = str_replace("\$lanip", $lanip, $string);
412
	$string = $newstring;
413
	// fixup #4: fix'r'up here.
414
	return $newstring;
415
}
416

    
417
function is_timezone($elt) {
418
	return !preg_match("/\/$/", $elt);
419
}
420

    
421
?>
(111-111/112)