Project

General

Profile

Download (18.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

    
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
	
114
	if($field_type == "interfaces_selection") {
115
		$text = "unset(\$config" . $field_conv . ");";
116
		eval($text);
117
		$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
118
		eval($text);
119
		return;
120
	}
121
	
122
	if($unset <> "") {
123
		$text = "unset(\$config" . $field_conv . ");";
124
		eval($text);
125
		$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
126
		eval($text);
127
	} else {
128
		if($arraynum <> "") {
129
			$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
130
		} else {
131
			$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
132
		}
133
		eval($text);
134
	}
135
}
136

    
137
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
138
	// handle before form display event.
139
        // good for modifying posted values, etc.
140
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
141
}
142

    
143
$pgtitle = $title;
144
include("head.inc");
145

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

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

    
197
<center>
198

    
199
&nbsp;<br>
200

    
201
<a href="/"><img border="0" src="./themes/<?= $g['theme']; ?>/images/logo.gif"></a>
202
<p>
203

    
204
<div style="width:700px;background-color:#ffffff" id="roundme">
205
<table bgcolor="#ffffff" width="600" cellspacing="0" cellpadding="3">
206
    <!-- wizard goes here -->
207
    <tr><td>&nbsp;</td></tr>
208
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
209
    <?php
210
	if(!$pkg['step'][$stepid]['disableheader'])
211
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
212
    ?>
213

    
214
    <?php
215
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
216
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
217

    
218
		    $value = $field['value'];
219
		    $name  = $field['name'];
220

    
221
		    $name = ereg_replace(" ", "", $name);
222
		    $name = strtolower($name);
223

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

    
240
		    if(!$field['combinefieldsend'])
241
			echo "<tr>";
242

    
243
		    if ($field['type'] == "input") {
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\">\n";
251

    
252
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'";
253
			if($field['validate'])
254
				echo " onChange='FieldValidate(this.value, \"{$field['validate']}\", \"{$field['message']}\");'";
255
			echo ">\n";
256
		    } else if($field['type'] == "interfaces_selection") {
257
			$size = "";
258
			$multiple = "";
259
			$name = strtolower($name);
260
			echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
261
			echo fixup_string($field['name']) . "\n";
262
			echo "</td>";
263
			echo "<td class=\"vtable\">\n";
264
			if($field['size'] <> "") $size = " size=\"" . $field['size'] . "\"";
265
			if($field['multiple'] <> "" and $field['multiple'] <> "0") {
266
			  $multiple = " multiple=\"multiple\"";
267
			  $name .= "[]";
268
			}
269
			echo "<select name='" . $name . "'" . $size . $multiple . ">\n";
270
			if($field['add_to_interfaces_selection'] <> "") {
271
				$SELECTED = "";
272
				if($field['add_to_interfaces_selection'] == $value) $SELECTED = " SELECTED";
273
				echo "<option value='" . $field['add_to_interfaces_selection'] . "'" . $SELECTED . ">" . $field['add_to_interfaces_selection'] . "</option>\n";
274
			}
275
			$interfaces = &$config['interfaces'];
276
			if($field['all_interfaces'] <> "") {
277
				$ints = split(" ", `/sbin/ifconfig -l`);
278
				$interfaces = array();
279
				foreach ($ints as $int) {
280
					$interfaces[]['descr'] = $int;
281
					$interfaces[] = $int;
282
				}
283
			}
284
			foreach ($interfaces as $ifname => $iface) {
285
			  if ($iface['descr'])
286
				  $ifdescr = $iface['descr'];
287
			  else
288
				  $ifdescr = strtoupper($ifname);
289
			  $ifname = $iface['descr'];
290
			  $ip = "";
291
			  if($field['all_interfaces'] <> "") {
292
				$ifdescr = $iface;
293
				$ip = " " . find_interface_ip($iface);
294
			  }
295
			  $SELECTED = "";
296
			  if($value == $ifdescr) $SELECTED = " SELECTED";
297
			  $to_echo =  "<option value='" . $ifdescr . "'" . $SELECTED . ">" . $ifdescr . $ip . "</option>\n";
298
			  $to_echo .= "<!-- {$value} -->";
299
			  $canecho = 0;
300
			  if($field['interface_filter'] <> "") {
301
				if(stristr($iface, $field['interface_filter']) == true)
302
					$canecho = 1;
303
			  } else {
304
				$canecho = 1;
305
			  }
306
			  if($canecho == 1) 
307
				echo $to_echo;
308
			}
309
				echo "</select>\n";
310
			} else if ($field['type'] == "password") {
311
			if(!$field['dontdisplayname']) {
312
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
313
				echo fixup_string($field['name']);
314
				echo ":</td>\n";
315
			}
316
			if(!$field['dontcombinecells'])
317
				echo "<td class=\"vtable\">";
318
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
319
		    } else if ($field['type'] == "select") {
320
			if(!$field['dontdisplayname']) {
321
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
322
				echo fixup_string($field['name']);
323
				echo ":</td>\n";
324
			}
325
			if($field['size']) $size = " size='" . $field['size'] . "' ";
326
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
327
			if(!$field['dontcombinecells'])
328
				echo "<td class=\"vtable\">\n";
329
			$onchange = "";
330
			foreach ($field['options']['option'] as $opt) {
331
				if($opt['enablefields'] <> "") {
332
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
333
				}
334
			}
335
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
336
			foreach ($field['options']['option'] as $opt) {
337
				$selected = "";
338
				if($value == $opt['value']) $selected = " SELECTED";
339
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
340
			}
341
			echo "</select>\n";
342
		    } else if ($field['type'] == "textarea") {
343
			if(!$field['dontdisplayname']) {
344
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
345
				echo fixup_string($field['name']);
346
				echo ":</td>";
347
			}
348
			if(!$field['dontcombinecells'])
349
				echo "<td class=\"vtable\">";
350
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
351
		    } else if ($field['type'] == "submit") {
352
			echo "<td>&nbsp;<br></td></tr>";
353
			echo "<tr><td colspan='2'><center>";
354
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
355
		    } else if ($field['type'] == "listtopic") {
356
			echo "<td>&nbsp;</td><tr>";
357
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
358
		    } else if ($field['type'] == "subnet_select") {
359
			if(!$field['dontdisplayname']) {
360
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
361
				echo fixup_string($field['name']);
362
				echo ":</td>";
363
			}
364
			if(!$field['dontcombinecells'])
365
				echo "<td class=\"vtable\">";
366
			echo "<select name='{$name}'>\n";
367
			for($x=1; $x<33; $x++) {
368
				$CHECKED = "";
369
				if($value == $x) $CHECKED = " SELECTED";
370
				if($x <> 31)
371
					echo "<option value='{$x}' {$CHECKED}>{$x}</option>\n";
372
			}
373
			echo "</select>\n";
374
		    } else if ($field['type'] == "timezone_select") {
375
			if(!$field['dontdisplayname']) {
376
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
377
				echo fixup_string($field['name']);
378
				echo ":</td>";
379
			}
380
			if(!$field['dontcombinecells'])
381
				echo "<td class=\"vtable\">";
382
			echo "<select name='{$name}'>\n";
383
			foreach ($timezonelist as $tz) {
384
				$SELECTED = "";
385
				if ($value == $tz) $SELECTED = " SELECTED";
386
				echo "<option value='" . htmlspecialchars($tz) . "' {$SELECTED}>";
387
				echo htmlspecialchars($tz);
388
				echo "</option>\n";
389
			}
390
			echo "</select>\n";
391
		    } else if ($field['type'] == "checkbox") {
392
			if(!$field['dontdisplayname']) {
393
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
394
				echo $field['name'];
395
				echo ":</td>";
396
			}
397
			$checked = "";
398
			if($value <> "") $checked = " CHECKED";
399
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked;
400
			if(isset($field['enablefields']) or isset($field['checkenablefields'])) echo " onClick=\"enablechange()\"";
401
			echo ">\n";
402
		    }
403

    
404
		    if($field['typehint'] <> "") {
405
			echo $field['typehint'];
406
		    }
407

    
408
		    if($field['description'] <> "") {
409
			echo "<br>" . $field['description'];
410
			echo "</td>";
411
		    }
412

    
413
		    if(!$field['combinefieldsbegin'])
414
			 echo "</tr>\n";
415

    
416
		    if($field['warning'] <> "") {
417
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
418
		    }
419

    
420
		}
421
	}
422
    ?>
423
</table>
424
<br>&nbsp;
425
</div>
426
</form>
427

    
428
<script type="text/javascript">
429
NiftyCheck();
430
Rounded("div#roundme","all","#333333","#FFFFFF","smooth");
431
</script>
432

    
433
</body>
434
</html>
435

    
436
<?php
437

    
438
$fieldnames_array = Array();
439
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
440
	// create a fieldname loop that can be used with javascript
441
	// hide and enable features.
442
	echo "\n<script language=\"JavaScript\">\n";
443
	echo "function disableall() {\n";
444
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
445
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
446
			if(!$field['donotdisable'] <> "") {
447
				array_push($fieldnames_array, $field['name']);
448
				$fieldname = ereg_replace(" ", "", $field['name']);
449
				$fieldname = strtolower($fieldname);
450
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
451
			}
452
		}
453
	}
454
	echo "}\ndisableall();\n";
455
	echo "function enableitems(selectedindex) {\n";
456
	echo "disableall();\n";
457
	$idcounter = 0;
458
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
459
		echo "\tswitch(selectedindex) {\n";
460
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
461
			if($field['options']['option'] <> "") {
462
				foreach ($field['options']['option'] as $opt) {
463
					if($opt['enablefields'] <> "") {
464
						echo "\t\tcase " . $idcounter . ":\n";
465
						$enablefields_split = split(",", $opt['enablefields']);
466
						foreach ($enablefields_split as $efs) {
467
							$fieldname = ereg_replace(" ", "", $efs);
468
							$fieldname = strtolower($fieldname);
469
							if($fieldname <> "") {
470
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
471
								echo $onchange;
472
							}
473
						}
474
						echo "\t\t\tbreak;\n";
475
					}
476
					$idcounter = $idcounter + 1;
477
				}
478
			}
479
		}
480
		echo "\t}\n";
481
	}
482
	echo "}\n";
483
	echo "disableall();\n";
484
	echo "</script>\n\n";
485
}
486

    
487

    
488
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
489
	// handle after form display event.
490
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
491
}
492

    
493
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
494
	// handle after form display event.
495
        echo "\n<script language=\"JavaScript\">\n";
496
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
497
	echo "</script>\n\n";
498
}
499

    
500
/*
501
 *  HELPER FUNCTIONS
502
 */
503

    
504
function fixup_string($string) {
505
	global $config, $myurl;
506
	$newstring = $string;
507
	// fixup #1: $myurl -> http[s]://ip_address:port/
508
	$https = "";
509
	$port = $config['system']['webguiport'];
510
	if($port <> "443" and $port <> "80")
511
		$urlport = ":" . $port;
512
	else
513
		$urlport = "";
514
	if($config['system']['webguiproto'] == "https")
515
		$https = "s";
516
    $myurl = "http" . $https . "://" . $config['interfaces']['lan']['ipaddr'] . $urlport;
517
	$newstring = str_replace("\$myurl", $myurl, $newstring);
518
	// fixup #2: $wanip
519
	$curwanip = get_current_wan_address();
520
	$newstring = str_replace("\$wanip", $curwanip, $newstring);
521
	// fixup #3: $lanip
522
	$lanip = $config['interfaces']['lan']['ipaddr'];
523
	$newstring = str_replace("\$lanip", $lanip, $newstring);
524
	// fixup #4: fix'r'up here.
525
	return $newstring;
526
}
527

    
528
function is_timezone($elt) {
529
	return !preg_match("/\/$/", $elt);
530
}
531

    
532
?>
(142-142/143)