Project

General

Profile

Download (18.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    wizard.php
5
    Copyright (C) 2004 Scott Ullrich
6
    All rights reserved.
7

    
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10

    
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13

    
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17

    
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29

    
30
require("guiconfig.inc");
31

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

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

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

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

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

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

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

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

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

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

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

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

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

    
145
?>
146
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
147
<?php if($pkg['step'][$stepid]['fields']['field'] <> "") { ?>
148
<script language="JavaScript">
149
<!--
150

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

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

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

    
203
<center>
204

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

    
212
?>
213

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

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

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

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

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

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

    
260
		    if ($field['type'] == "input") {
261
			if(!$field['dontdisplayname']) {
262
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
263
				echo fixup_string($field['name']);
264
				echo ":</td>\n";
265
			}
266
			if(!$field['dontcombinecells'])
267
				echo "<td class=\"vtable\">\n";
268

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

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

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

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

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

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

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

    
450
</body>
451
</html>
452

    
453
<?php
454

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

    
504

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

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

    
517
/*
518
 *  HELPER FUNCTIONS
519
 */
520

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

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

    
562
?>
(154-154/155)