Project

General

Profile

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

    
152
function  FieldValidate(userinput,  regexp,  message) 
153
{ 
154
                if(!userinput.match(regexp)) 
155
                                alert(message); 
156
} 
157

    
158
function enablechange() {
159
<?php
160
        foreach($pkg['step'][$stepid]['fields']['field'] as $field) {
161
                if(isset($field['enablefields']) or isset($field['checkenablefields'])) {
162
                        print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
163
                        if(isset($field['enablefields'])) {
164
                                $enablefields = explode(',', $field['enablefields']);
165
                                foreach($enablefields as $enablefield) {
166
                                        $enablefield = strtolower($enablefield);
167
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 1;' . "\n";
168
                                }
169
                        }
170
                        if(isset($field['checkenablefields'])) {
171
                                $checkenablefields = explode(',', $field['checkenablefields']);
172
                                foreach($checkenablefields as $checkenablefield) {
173
                                        $checkenablefield = strtolower($checkenablefield);
174
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 0;' . "\n";
175
                                }
176
                        }
177
                        print "\t" . '} else {' . "\n";
178
                        if(isset($field['enablefields'])) {
179
                                foreach($enablefields as $enablefield) {
180
                                        $enablefield = strtolower($enablefield);
181
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 0;' . "\n";
182
                                }
183
                        }
184
                        if(isset($field['checkenablefields'])) {
185
                                foreach($checkenablefields as $checkenablefield) {
186
                                        $checkenablefield = strtolower($checkenablefield);
187
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 1;' . "\n";
188
                                }
189
                        }   
190
                        print "\t" . '}' . "\n";
191
                }
192
        }
193
?>
194
}
195
//-->
196
</script>
197
<?php } ?>
198

    
199
<form action="wizard.php" method="post" name="iform" id="iform">
200
<input type="hidden" name="xml" value="<?= $xml ?>">
201
<input type="hidden" name="stepid" value="<?= $stepid ?>">
202
<?php if ($savemsg) print_info_box($savemsg); ?>
203

    
204
<center>
205

    
206
&nbsp;<br>
207
<?php
208
	if($title == "Reload in progress")
209
		$ip = "http://{$config['interfaces']['lan']['ipaddr']}";
210
	else
211
		$ip = "/";
212

    
213
?>
214

    
215
<a href="<?php echo $ip; ?>"><img border="0" src="./themes/<?= $g['theme']; ?>/images/logo.gif"></a>
216
<p>
217
<div style="width:700px;background-color:#ffffff" id="roundme">
218
<table bgcolor="#ffffff" width="600" cellspacing="0" cellpadding="3">
219
    <!-- wizard goes here -->
220
    <tr><td>&nbsp;</td></tr>
221
    <tr><td colspan='2'>
222
<?php	if ($_GET['message'] != "") {
223
		print_info_box($_GET['message']);
224
	}
225
?></td></tr>
226
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
227
    <?php
228
	if(!$pkg['step'][$stepid]['disableheader'])
229
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
230
    ?>
231

    
232
    <?php
233
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
234
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
235

    
236
		    $value = $field['value'];
237
		    $name  = $field['name'];
238

    
239
		    $name = ereg_replace(" ", "", $name);
240
		    $name = strtolower($name);
241

    
242
		    if($field['bindstofield'] <> "") {
243
				$arraynum = "";
244
				$field_conv = "";
245
				$field_split = split("->", $field['bindstofield']);
246
				// arraynum is used in cases where there is an array of the same field
247
				// name such as dnsserver (2 of them)
248
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
249
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
250
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
251
					eval($toeval);
252
					if ($field['type'] == "checkbox") {
253
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
254
						eval($toeval);
255
					}
256
		    }
257

    
258
		    if(!$field['combinefieldsend'])
259
			echo "<tr>";
260

    
261
		    if ($field['type'] == "input") {
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
			if(!$field['dontcombinecells'])
268
				echo "<td class=\"vtable\">\n";
269

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

    
422
		    if($field['typehint'] <> "") {
423
			echo $field['typehint'];
424
		    }
425

    
426
		    if($field['description'] <> "") {
427
			echo "<br>" . $field['description'];
428
			echo "</td>";
429
		    }
430

    
431
		    if(!$field['combinefieldsbegin'])
432
			 echo "</tr>\n";
433

    
434
		    if($field['warning'] <> "") {
435
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
436
		    }
437

    
438
		}
439
	}
440
    ?>
441
</table>
442
<br>&nbsp;
443
</div>
444
</form>
445

    
446
<script type="text/javascript">
447
NiftyCheck();
448
Rounded("div#roundme","all","#333333","#FFFFFF","smooth");
449
</script>
450

    
451
</body>
452
</html>
453

    
454
<?php
455

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

    
505

    
506
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
507
	// handle after form display event.
508
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
509
}
510

    
511
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
512
	// handle after form display event.
513
        echo "\n<script language=\"JavaScript\">\n";
514
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
515
	echo "</script>\n\n";
516
}
517

    
518
/*
519
 *  HELPER FUNCTIONS
520
 */
521

    
522
function fixup_string($string) {
523
	global $config, $myurl;
524
	$newstring = $string;
525
	// fixup #1: $myurl -> http[s]://ip_address:port/
526
	switch($config['system']['webguiproto']) {
527
		case "http":
528
			$proto = "http";
529
			break;
530
		case "https":
531
			$proto = "https";
532
			break;
533
		default:
534
			$proto = "http";
535
			break;
536
	}
537
	$port = $config['system']['webguiport'];
538
	if($port != "") {
539
		if(($port == "443" and $proto != "https") or ($port == "80" and $proto != "http")) {
540
			$urlport = ":" . $port;
541
		} elseif ($port != "80" and $port != "443") {
542
			$urlport = ":" . $port;
543
		} else {
544
			$urlport = "";
545
		}
546
	}
547
	$myurl = $proto . "://" . $config['interfaces']['lan']['ipaddr'] . $urlport . "/";
548
	$newstring = str_replace("\$myurl", $myurl, $newstring);
549
	// fixup #2: $wanip
550
	$curwanip = get_current_wan_address();
551
	$newstring = str_replace("\$wanip", $curwanip, $newstring);
552
	// fixup #3: $lanip
553
	$lanip = $config['interfaces']['lan']['ipaddr'];
554
	$newstring = str_replace("\$lanip", $lanip, $newstring);
555
	// fixup #4: fix'r'up here.
556
	return $newstring;
557
}
558

    
559
function is_timezone($elt) {
560
	return !preg_match("/\/$/", $elt);
561
}
562

    
563
?>
(152-152/153)