Project

General

Profile

Download (18.4 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
<?php
201
	if($title == "Reload in progress")
202
		$ip = "http://{$config['interfaces']['lan']['ipaddr']}";
203
	else
204
		$ip = "/";
205
?>
206

    
207
<a href="<?php echo $ip; ?>"><img border="0" src="./themes/<?= $g['theme']; ?>/images/logo.gif"></a>
208
<p>
209

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

    
220
    <?php
221
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
222
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
223

    
224
		    $value = $field['value'];
225
		    $name  = $field['name'];
226

    
227
		    $name = ereg_replace(" ", "", $name);
228
		    $name = strtolower($name);
229

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

    
246
		    if(!$field['combinefieldsend'])
247
			echo "<tr>";
248

    
249
		    if ($field['type'] == "input") {
250
			if(!$field['dontdisplayname']) {
251
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
252
				echo fixup_string($field['name']);
253
				echo ":</td>\n";
254
			}
255
			if(!$field['dontcombinecells'])
256
				echo "<td class=\"vtable\">\n";
257

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

    
410
		    if($field['typehint'] <> "") {
411
			echo $field['typehint'];
412
		    }
413

    
414
		    if($field['description'] <> "") {
415
			echo "<br>" . $field['description'];
416
			echo "</td>";
417
		    }
418

    
419
		    if(!$field['combinefieldsbegin'])
420
			 echo "</tr>\n";
421

    
422
		    if($field['warning'] <> "") {
423
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
424
		    }
425

    
426
		}
427
	}
428
    ?>
429
</table>
430
<br>&nbsp;
431
</div>
432
</form>
433

    
434
<script type="text/javascript">
435
NiftyCheck();
436
Rounded("div#roundme","all","#333333","#FFFFFF","smooth");
437
</script>
438

    
439
</body>
440
</html>
441

    
442
<?php
443

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

    
493

    
494
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
495
	// handle after form display event.
496
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
497
}
498

    
499
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
500
	// handle after form display event.
501
        echo "\n<script language=\"JavaScript\">\n";
502
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
503
	echo "</script>\n\n";
504
}
505

    
506
/*
507
 *  HELPER FUNCTIONS
508
 */
509

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

    
534
function is_timezone($elt) {
535
	return !preg_match("/\/$/", $elt);
536
}
537

    
538
?>
(145-145/146)