Project

General

Profile

Download (16.1 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
</head>
134

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

    
180
<form action="wizard.php" method="post" name="iform" id="iform">
181
<input type="hidden" name="xml" value="<?= $xml ?>">
182
<input type="hidden" name="stepid" value="<?= $stepid ?>">
183
<?php if ($savemsg) print_info_box($savemsg); ?>
184

    
185
<center>
186

    
187
&nbsp;<br>
188

    
189
<a href="/"><img border="0" src="/logo.gif"></a>
190
<p>
191

    
192
<table width="600" cellspacing="0" cellpadding="3">
193
    <!-- wizard goes here -->
194
    <tr><td>&nbsp;</td></tr>
195
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
196
    <?php
197
	if(!$pkg['step'][$stepid]['disableheader'])
198
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
199
    ?>
200

    
201
    <?php
202
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
203
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
204

    
205
		    $value = $field['value'];
206
		    $name  = $field['name'];
207

    
208
		    $name = ereg_replace(" ", "", $name);
209
		    $name = strtolower($name);
210

    
211
		    if($field['bindstofield'] <> "") {
212
				$arraynum = "";
213
				$field_conv = "";
214
				$field_split = split("->", $field['bindstofield']);
215
				// arraynum is used in cases where there is an array of the same field
216
				// name such as dnsserver (2 of them)
217
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
218
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
219
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
220
					eval($toeval);
221
					if ($field['type'] == "checkbox") {
222
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
223
						eval($toeval);
224
					}
225
		    }
226

    
227
		    if(!$field['combinefieldsend'])
228
			echo "<tr>";
229

    
230
		    if ($field['type'] == "input") {
231
			if(!$field['dontdisplayname']) {
232
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
233
				echo fixup_string($field['name']);
234
				echo ":</td>\n";
235
			}
236
			if(!$field['dontcombinecells'])
237
				echo "<td class=\"vtable\">\n";
238

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

    
338
		    if($field['typehint'] <> "") {
339
			echo $field['typehint'];
340
		    }
341

    
342
		    if($field['description'] <> "") {
343
			echo "<br>" . $field['description'];
344
			echo "</td>";
345
		    }
346

    
347
		    if(!$field['combinefieldsbegin'])
348
			 echo "</tr>\n";
349

    
350
		    if($field['warning'] <> "") {
351
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
352
		    }
353

    
354
		}
355
	}
356
    ?>
357
</table>
358

    
359
</form>
360
</body>
361
</html>
362

    
363
<?php
364

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

    
414

    
415
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
416
	// handle after form display event.
417
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
418
}
419

    
420
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
421
	// handle after form display event.
422
        echo "\n<script language=\"JavaScript\">\n";
423
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
424
	echo "</script>\n\n";
425
}
426

    
427
/*
428
 *  HELPER FUNCTIONS
429
 */
430

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

    
457
function is_timezone($elt) {
458
	return !preg_match("/\/$/", $elt);
459
}
460

    
461
?>
(114-114/115)