Project

General

Profile

Download (16.3 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, $field['type']);
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, $field_type) {
101
	global $config;
102
	$field_split = split("->",$field);
103
	foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
104
	if($field_conv == "") return;
105
	if($field_type == "checkbox" and $updatetext <> "on") {
106
		/*
107
		    item is a checkbox, it should have the value "on"
108
		    if it was checked
109
                */
110
		$text = "unset(\$config" . $field_conv . ");";
111
		eval($text);
112
		return;
113
	}    
114
	if($unset <> "") {
115
		$text = "unset(\$config" . $field_conv . ");";
116
		eval($text);
117
		$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
118
		eval($text);
119
	} else {
120
		if($arraynum <> "") {
121
			$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
122
		} else {
123
			$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
124
		}
125
		eval($text);
126
	}
127
}
128

    
129
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
130
	// handle before form display event.
131
        // good for modifying posted values, etc.
132
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
133
}
134

    
135
?>
136
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
137
<html>
138
<head>
139
<title><?=gentitle_pkg($title);?></title>
140
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
141
<link href="gui.css" rel="stylesheet" type="text/css">
142
</head>
143

    
144
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
145
<?php if($pkg['step'][$stepid]['fields']['field'] <> "") { ?>
146
<script language="JavaScript">
147
<!--
148
function enablechange() {
149
<?php
150
        foreach($pkg['step'][$stepid]['fields']['field'] as $field) {
151
                if(isset($field['enablefields']) or isset($field['checkenablefields'])) {
152
                        print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
153
                        if(isset($field['enablefields'])) {
154
                                $enablefields = explode(',', $field['enablefields']);
155
                                foreach($enablefields as $enablefield) {
156
                                        $enablefield = strtolower($enablefield);
157
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 1;' . "\n";
158
                                }
159
                        }
160
                        if(isset($field['checkenablefields'])) {
161
                                $checkenablefields = explode(',', $field['checkenablefields']);
162
                                foreach($checkenablefields as $checkenablefield) {
163
                                        $checkenablefield = strtolower($checkenablefield);
164
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 0;' . "\n";
165
                                }
166
                        }
167
                        print "\t" . '} else {' . "\n";
168
                        if(isset($field['enablefields'])) {
169
                                foreach($enablefields as $enablefield) {
170
                                        $enablefield = strtolower($enablefield);
171
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 0;' . "\n";
172
                                }
173
                        }
174
                        if(isset($field['checkenablefields'])) {
175
                                foreach($checkenablefields as $checkenablefield) {
176
                                        $checkenablefield = strtolower($checkenablefield);
177
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 1;' . "\n";
178
                                }
179
                        }   
180
                        print "\t" . '}' . "\n";
181
                }
182
        }
183
?>
184
}
185
//-->
186
</script>
187
<?php } ?>
188

    
189
<form action="wizard.php" method="post" name="iform" id="iform">
190
<input type="hidden" name="xml" value="<?= $xml ?>">
191
<input type="hidden" name="stepid" value="<?= $stepid ?>">
192
<?php if ($savemsg) print_info_box($savemsg); ?>
193

    
194
<center>
195

    
196
&nbsp;<br>
197

    
198
<a href="/"><img border="0" src="/logo.gif"></a>
199
<p>
200

    
201
<table width="600" cellspacing="0" cellpadding="3">
202
    <!-- wizard goes here -->
203
    <tr><td>&nbsp;</td></tr>
204
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
205
    <?php
206
	if(!$pkg['step'][$stepid]['disableheader'])
207
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
208
    ?>
209

    
210
    <?php
211
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
212
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
213

    
214
		    $value = $field['value'];
215
		    $name  = $field['name'];
216

    
217
		    $name = ereg_replace(" ", "", $name);
218
		    $name = strtolower($name);
219

    
220
		    if($field['bindstofield'] <> "") {
221
				$arraynum = "";
222
				$field_conv = "";
223
				$field_split = split("->", $field['bindstofield']);
224
				// arraynum is used in cases where there is an array of the same field
225
				// name such as dnsserver (2 of them)
226
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
227
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
228
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
229
					eval($toeval);
230
					if ($field['type'] == "checkbox") {
231
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
232
						eval($toeval);
233
					}
234
		    }
235

    
236
		    if(!$field['combinefieldsend'])
237
			echo "<tr>";
238

    
239
		    if ($field['type'] == "input") {
240
			if(!$field['dontdisplayname']) {
241
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
242
				echo fixup_string($field['name']);
243
				echo ":</td>\n";
244
			}
245
			if(!$field['dontcombinecells'])
246
				echo "<td class=\"vtable\">\n";
247

    
248
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'";
249
			if($field['validate'])
250
				echo " onChange='FieldValidate(this.value, \"{$field['validate']}\", \"{$field['message']}\");'";
251
			echo ">\n";
252
		    } else if ($field['type'] == "password") {
253
			if(!$field['dontdisplayname']) {
254
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
255
				echo fixup_string($field['name']);
256
				echo ":</td>\n";
257
			}
258
			if(!$field['dontcombinecells'])
259
				echo "<td class=\"vtable\">";
260
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
261
		    } else if ($field['type'] == "select") {
262
			if(!$field['dontdisplayname']) {
263
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
264
				echo fixup_string($field['name']);
265
				echo ":</td>\n";
266
			}
267
			// XXX: TODO: set $selected
268
			if($field['size']) $size = " size='" . $field['size'] . "' ";
269
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
270
			if(!$field['dontcombinecells'])
271
				echo "<td class=\"vtable\">\n";
272
			$onchange = "";
273
			foreach ($field['options']['option'] as $opt) {
274
				if($opt['enablefields'] <> "") {
275
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
276
				}
277
			}
278
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
279
			foreach ($field['options']['option'] as $opt) {
280
				$selected = "";
281
				if($value == $opt['value']) $selected = " SELECTED";
282
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
283
			}
284
			echo "</select>\n";
285
		    } else if ($field['type'] == "textarea") {
286
			if(!$field['dontdisplayname']) {
287
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
288
				echo fixup_string($field['name']);
289
				echo ":</td>";
290
			}
291
			if(!$field['dontcombinecells'])
292
				echo "<td class=\"vtable\">";
293
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
294
		    } else if ($field['type'] == "submit") {
295
			echo "<td>&nbsp;<br></td></tr>";
296
			echo "<tr><td colspan='2'><center>";
297
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
298
		    } else if ($field['type'] == "listtopic") {
299
			echo "<td>&nbsp;</td><tr>";
300
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
301
		    } else if ($field['type'] == "subnet_select") {
302
			if(!$field['dontdisplayname']) {
303
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
304
				echo fixup_string($field['name']);
305
				echo ":</td>";
306
			}
307
			if(!$field['dontcombinecells'])
308
				echo "<td class=\"vtable\">";
309
			echo "<select name='{$name}'>\n";
310
			for($x=1; $x<33; $x++) {
311
				$CHECKED = "";
312
				if($value == $x) $CHECKED = " SELECTED";
313
				if($x <> 31)
314
					echo "<option value='{$x}' {$CHECKED}>{$x}</option>\n";
315
			}
316
			echo "</select>\n";
317
		    } else if ($field['type'] == "timezone_select") {
318
			if(!$field['dontdisplayname']) {
319
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
320
				echo fixup_string($field['name']);
321
				echo ":</td>";
322
			}
323
			if(!$field['dontcombinecells'])
324
				echo "<td class=\"vtable\">";
325
			echo "<select name='{$name}'>\n";
326
			foreach ($timezonelist as $tz) {
327
				$SELECTED = "";
328
				if ($value == $tz) $SELECTED = " SELECTED";
329
				echo "<option value='" . htmlspecialchars($tz) . "' {$SELECTED}>";
330
				echo htmlspecialchars($tz);
331
				echo "</option>\n";
332
			}
333
			echo "</select>\n";
334
		    } else if ($field['type'] == "checkbox") {
335
			if(!$field['dontdisplayname']) {
336
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
337
				echo $field['name'];
338
				echo ":</td>";
339
			}
340
			$checked = "";
341
			if($value <> "") $checked = " CHECKED";
342
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked;
343
			if(isset($field['enablefields']) or isset($field['checkenablefields'])) echo " onClick=\"enablechange()\"";
344
			echo ">\n";
345
		    }
346

    
347
		    if($field['typehint'] <> "") {
348
			echo $field['typehint'];
349
		    }
350

    
351
		    if($field['description'] <> "") {
352
			echo "<br>" . $field['description'];
353
			echo "</td>";
354
		    }
355

    
356
		    if(!$field['combinefieldsbegin'])
357
			 echo "</tr>\n";
358

    
359
		    if($field['warning'] <> "") {
360
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
361
		    }
362

    
363
		}
364
	}
365
    ?>
366
</table>
367

    
368
</form>
369
</body>
370
</html>
371

    
372
<?php
373

    
374
$fieldnames_array = Array();
375
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
376
	// create a fieldname loop that can be used with javascript
377
	// hide and enable features.
378
	echo "\n<script language=\"JavaScript\">\n";
379
	echo "function disableall() {\n";
380
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
381
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
382
			if(!$field['donotdisable'] <> "") {
383
				array_push($fieldnames_array, $field['name']);
384
				$fieldname = ereg_replace(" ", "", $field['name']);
385
				$fieldname = strtolower($fieldname);
386
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
387
			}
388
		}
389
	}
390
	echo "}\ndisableall();\n";
391
	echo "function enableitems(selectedindex) {\n";
392
	echo "disableall();\n";
393
	$idcounter = 0;
394
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
395
		echo "\tswitch(selectedindex) {\n";
396
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
397
			if($field['options']['option'] <> "") {
398
				foreach ($field['options']['option'] as $opt) {
399
					if($opt['enablefields'] <> "") {
400
						echo "\t\tcase " . $idcounter . ":\n";
401
						$enablefields_split = split(",", $opt['enablefields']);
402
						foreach ($enablefields_split as $efs) {
403
							$fieldname = ereg_replace(" ", "", $efs);
404
							$fieldname = strtolower($fieldname);
405
							if($fieldname <> "") {
406
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
407
								echo $onchange;
408
							}
409
						}
410
						echo "\t\t\tbreak;\n";
411
					}
412
					$idcounter = $idcounter + 1;
413
				}
414
			}
415
		}
416
		echo "\t}\n";
417
	}
418
	echo "}\n";
419
	echo "disableall();\n";
420
	echo "</script>\n\n";
421
}
422

    
423

    
424
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
425
	// handle after form display event.
426
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
427
}
428

    
429
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
430
	// handle after form display event.
431
        echo "\n<script language=\"JavaScript\">\n";
432
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
433
	echo "</script>\n\n";
434
}
435

    
436
/*
437
 *  HELPER FUNCTIONS
438
 */
439

    
440
function fixup_string($string) {
441
	global $config, $myurl;
442
	// fixup #1: $myurl -> http[s]://ip_address:port/
443
	$https = "";
444
	$port = "";
445
	$urlport = "";
446
	$port = $config['system']['webguiport'];
447
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
448
	if($config['system']['webguiproto'] == "https") $https = "s";
449
	//$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
450
        $myurl = "http" . $https . "://" . $config['interfaces']['lan']['ip'];
451
	$newstring = str_replace("\$myurl", $myurl, $string);
452
	$string = $newstring;
453
	// fixup #2: $wanip
454
	$curwanip = get_current_wan_address();
455
	$newstring = str_replace("\$wanip", $curwanip, $string);
456
	$string = $newstring;
457
	// fixup #3: $lanip
458
	$lancfg = $config['interfaces']['lan'];
459
	$lanip = $lancfg['ipaddr'];
460
	$newstring = str_replace("\$lanip", $lanip, $string);
461
	$string = $newstring;
462
	// fixup #4: fix'r'up here.
463
	return $newstring;
464
}
465

    
466
function is_timezone($elt) {
467
	return !preg_match("/\/$/", $elt);
468
}
469

    
470
?>
(116-116/117)