Project

General

Profile

Download (16 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

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

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

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

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

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

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

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

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

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

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

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

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

    
134
$pgtitle = $title;
135
include("head.inc");
136

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

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

    
188
<center>
189

    
190
&nbsp;<br>
191

    
192
<a href="/"><img border="0" src="/logo.gif"></a>
193
<p>
194

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

    
204
    <?php
205
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
206
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
207

    
208
		    $value = $field['value'];
209
		    $name  = $field['name'];
210

    
211
		    $name = ereg_replace(" ", "", $name);
212
		    $name = strtolower($name);
213

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

    
230
		    if(!$field['combinefieldsend'])
231
			echo "<tr>";
232

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

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

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

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

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

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

    
356
		}
357
	}
358
    ?>
359
</table>
360

    
361
</form>
362
</body>
363
</html>
364

    
365
<?php
366

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

    
416

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

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

    
429
/*
430
 *  HELPER FUNCTIONS
431
 */
432

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

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

    
460
?>
(126-126/127)