Project

General

Profile

Download (12.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
require("xmlparse_pkg.inc");
33

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

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

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

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

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

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

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

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

    
93
$title          = $pkg['step'][$stepid]['title'];
94
$description    = $pkg['step'][$stepid]['description'];
95

    
96
function update_config_field($field, $updatetext, $unset, $arraynum) {
97
	global $config;
98
	$field_split = split("->",$field);
99
	foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
100
	if($field_conv == "") return;
101
	if($unset <> "") {
102
		$text = "unset(\$config" . $field_conv . ");";
103
		eval($text);
104
		$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
105
		eval($text);
106
	} else {
107
		if($arraynum <> "") {
108
			$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
109
		} else {
110
			$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
111
		}
112
		eval($text);
113
	}
114
}
115

    
116
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
117
	// handle before form display event.
118
        // good for modifying posted values, etc.
119
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
120
}
121

    
122
?>
123
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
124
<html>
125
<head>
126
<title><?=gentitle_pkg($title);?></title>
127
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
128
<link href="gui.css" rel="stylesheet" type="text/css">
129

    
130
<script Language="JavaScript">
131
<!--
132
function FieldValidate(userinput, regexp, message)
133
{
134
        if(!userinput.match(regexp))
135
                alert(message);
136
}
137
//-->
138
</script>
139

    
140
</head>
141

    
142
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
143
<form action="wizard.php" method="post">
144
<input type="hidden" name="xml" value="<?= $xml ?>">
145
<input type="hidden" name="stepid" value="<?= $stepid ?>">
146
<?php if ($savemsg) print_info_box($savemsg); ?>
147

    
148
<center>
149

    
150
&nbsp;<br>
151

    
152
<a href="/"><img border="0" src="/logo.gif"></a>
153
<p>
154

    
155
<table width="600" cellspacing="0" cellpadding="3">
156
    <!-- wizard goes here -->
157
    <tr><td>&nbsp;</td></tr>
158
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
159
    <?php
160
	if(!$pkg['step'][$stepid]['disableheader'])
161
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
162
    ?>
163

    
164
    <?php
165
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
166
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
167

    
168
		    $value = $field['value'];
169
		    $name  = $field['name'];
170

    
171
		    $name = ereg_replace(" ", "", $name);
172
		    $name = strtolower($name);
173

    
174
		    if($field['bindstofield'] <> "") {
175
				$arraynum = "";
176
				$field_conv = "";
177
				$field_split = split("->", $field['bindstofield']);
178
				// arraynum is used in cases where there is an array of the same field
179
				// name such as dnsserver (2 of them)
180
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
181
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
182
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
183
					eval($toeval);
184
					if ($field['type'] == "checkbox") {
185
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
186
						eval($toeval);
187
					}
188
		    }
189

    
190
		    if(!$field['combinefieldsend'])
191
			echo "<tr>";
192

    
193
		    if ($field['type'] == "input") {
194
			if(!$field['dontdisplayname']) {
195
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
196
				echo fixup_string($field['name']);
197
				echo ":</td>\n";
198
			}
199
			if(!$field['dontcombinecells'])
200
				echo "<td class=\"vtable\">\n";
201

    
202
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'";
203
			if($field['validate'])
204
				echo " onChange='FieldValidate(this.value, \"{$field['validate']}\", \"{$field['message']}\");'";
205
			echo ">\n";
206
		    } else if ($field['type'] == "password") {
207
			if(!$field['dontdisplayname']) {
208
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
209
				echo fixup_string($field['name']);
210
				echo ":</td>\n";
211
			}
212
			if(!$field['dontcombinecells'])
213
				echo "<td class=\"vtable\">";
214
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
215
		    } else if ($field['type'] == "select") {
216
			if(!$field['dontdisplayname']) {
217
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
218
				echo fixup_string($field['name']);
219
				echo ":</td>\n";
220
			}
221
			// XXX: TODO: set $selected
222
			if($field['size']) $size = " size='" . $field['size'] . "' ";
223
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
224
			if(!$field['dontcombinecells'])
225
				echo "<td class=\"vtable\">\n";
226
			$onchange = "";
227
			foreach ($field['options']['option'] as $opt) {
228
				if($opt['enablefields'] <> "") {
229
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
230
				}
231
			}
232
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
233
			foreach ($field['options']['option'] as $opt) {
234
				$selected = "";
235
				if($value == $opt['value']) $selected = " SELECTED";
236
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
237
			}
238
			echo "</select>\n";
239
		    } else if ($field['type'] == "textarea") {
240
			if(!$field['dontdisplayname']) {
241
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
242
				echo fixup_string($field['name']);
243
				echo ":</td>";
244
			}
245
			if(!$field['dontcombinecells'])
246
				echo "<td class=\"vtable\">";
247
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
248
		    } else if ($field['type'] == "submit") {
249
			echo "<td>&nbsp;<br></td></tr>";
250
			echo "<tr><td colspan='2'><center>";
251
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
252
		    } else if ($field['type'] == "listtopic") {
253
			echo "<td>&nbsp;</td><tr>";
254
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
255
		    } else if ($field['type'] == "checkbox") {
256
			if(!$field['dontdisplayname']) {
257
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
258
				echo $field['name'];
259
				echo ":</td>";
260
			}
261
			$checked = "";
262
			if($value <> "") $checked = " CHECKED";
263
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked . ">\n";
264
		    }
265

    
266
		    if($field['typehint'] <> "") {
267
			echo $field['typehint'];
268
		    }
269

    
270
		    if($field['description'] <> "") {
271
			echo "<br>" . $field['description'];
272
			echo "</td>";
273
		    }
274

    
275
		    if(!$field['combinefieldsbegin'])
276
			 echo "</tr>\n";
277
		}
278
	}
279
    ?>
280
</table>
281

    
282
</form>
283
</body>
284
</html>
285

    
286
<?php
287

    
288
$fieldnames_array = Array();
289
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
290
	// create a fieldname loop that can be used with javascript
291
	// hide and enable features.
292
	echo "\n<script language=\"JavaScript\">\n";
293
	echo "function disableall() {\n";
294
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
295
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
296
			if(!$field['donotdisable'] <> "") {
297
				array_push($fieldnames_array, $field['name']);
298
				$fieldname = ereg_replace(" ", "", $field['name']);
299
				$fieldname = strtolower($fieldname);
300
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
301
			}
302
		}
303
	}
304
	echo "}\ndisableall();\n";
305
	echo "function enableitems(selectedindex) {\n";
306
	echo "disableall();\n";
307
	$idcounter = 0;
308
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
309
		echo "\tswitch(selectedindex) {\n";
310
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
311
			if($field['options']['option'] <> "") {
312
				foreach ($field['options']['option'] as $opt) {
313
					if($opt['enablefields'] <> "") {
314
						echo "\t\tcase " . $idcounter . ":\n";
315
						$enablefields_split = split(",", $opt['enablefields']);
316
						foreach ($enablefields_split as $efs) {
317
							$fieldname = ereg_replace(" ", "", $efs);
318
							$fieldname = strtolower($fieldname);
319
							if($fieldname <> "") {
320
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
321
								echo $onchange;
322
							}
323
						}
324
						echo "\t\t\tbreak;\n";
325
					}
326
					$idcounter = $idcounter + 1;
327
				}
328
			}
329
		}
330
		echo "\t}\n";
331
	}
332
	echo "}\n";
333
	echo "disableall();\n";
334
	echo "</script>\n\n";
335
}
336

    
337

    
338
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
339
	// handle after form display event.
340
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
341
}
342

    
343
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
344
	// handle after form display event.
345
        echo "\n<script language=\"JavaScript\">\n";
346
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
347
	echo "</script>\n\n";
348
}
349

    
350
/*
351
 *  HELPER FUNCTIONS
352
 */
353

    
354
function fixup_string($string) {
355
	global $config;
356
	// fixup #1: $myurl -> http[s]://ip_address:port/
357
	$https = "";
358
	$port = "";
359
	$urlport = "";
360
	$port = $config['system']['webguiport'];
361
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
362
	if($config['system']['webguiproto'] == "https") $https = "s";
363
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
364
	$newstring = str_replace("\$myurl", $myurl, $string);
365
	$string = $newstring;
366
	// fixup #2: $wanip
367
	$curwanip = get_current_wan_address();
368
	$newstring = str_replace("\$wanip", $curwanip, $string);
369
	$string = $newstring;
370
	// fixup #3: $lanip
371
	$lancfg = $config['interfaces']['lan'];
372
	$lanip = $lancfg['ipaddr'];
373
	$newstring = str_replace("\$lanip", $lanip, $string);
374
	$string = $newstring;
375
	// fixup #4: fix'r'up here.
376
	return $newstring;
377
}
378

    
379
?>
380

    
(108-108/109)