Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
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
require("xmlparse_pkg.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
            $pkg = parse_xml_config_pkg("/usr/local/www/wizards/" . $xml, "pfsensewizard");
53
}
54

    
55
$title          = $pkg['step'][$stepid]['title'];
56
$description    = $pkg['step'][$stepid]['description'];
57
$totalsteps     = $pkg['totalsteps'];
58

    
59
if($pkg['step'][$stepid]['stepsubmitbeforesave']) {
60
		eval($pkg['step'][$stepid]['stepsubmitbeforesave']);
61
}
62

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

    
77
    }
78
    // run custom php code embedded in xml config.
79
    if($pkg['step'][$stepid]['stepsubmitphpaction'] <> "") {
80
		eval($pkg['step'][$stepid]['stepsubmitphpaction']);
81
    }
82
	write_config();
83
    $stepid++;
84
    if($stepid > $totalsteps) $stepid = $totalsteps;
85
}
86

    
87
$title          = $pkg['step'][$stepid]['title'];
88
$description    = $pkg['step'][$stepid]['description'];
89

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

    
110
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
111
	// handle before form display event.
112
        // good for modifying posted values, etc.
113
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
114
}
115

    
116
?>
117
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
118
<html>
119
<head>
120
<title><?=gentitle_pkg($title);?></title>
121
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
122
<link href="gui.css" rel="stylesheet" type="text/css">
123

    
124
<script Language="JavaScript">
125
<!-- 
126
function FieldValidate(userinput, regexp, message)
127
{
128
        if(!userinput.match(regexp))
129
                alert(message);
130
}
131
//-->
132
</script>
133

    
134
</head>
135

    
136
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
137
<form action="wizard.php" method="post">
138
<input type="hidden" name="xml" value="<?= $xml ?>">
139
<input type="hidden" name="stepid" value="<?= $stepid ?>">
140
<?php if ($savemsg) print_info_box($savemsg); ?>
141

    
142
<center>
143

    
144
&nbsp;<br>
145

    
146
<a href="/"><img border="0" src="/logo.gif"></a>
147
<p>
148

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

    
158
    <?php
159
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
160
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
161

    
162
		    $value = $field['value'];
163
		    $name  = $field['name'];
164

    
165
		    $name = ereg_replace(" ", "", $name);
166
		    $name = strtolower($name);
167

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

    
184
		    if(!$field['combinefieldsend'])
185
			echo "<tr>";
186

    
187
		    if ($field['type'] == "input") {
188
			if(!$field['dontdisplayname']) {
189
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
190
				echo fixup_string($field['name']);
191
				echo ":</td>\n";
192
			}
193
			if(!$field['dontcombinecells'])
194
				echo "<td class=\"vtable\">\n";
195

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

    
260
		    if($field['typehint'] <> "") {
261
			echo $field['typehint'];
262
		    }
263

    
264
		    if($field['description'] <> "") {
265
			echo "<br>" . $field['description'];
266
			echo "</td>";
267
		    }
268

    
269
		    if(!$field['combinefieldsbegin'])
270
			 echo "</tr>\n";
271
		}
272
	}
273
    ?>
274
</table>
275

    
276
</form>
277
</body>
278
</html>
279

    
280
<?php
281

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

    
331

    
332
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
333
	// handle after form display event.
334
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
335
}
336

    
337
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
338
	// handle after form display event.
339
        echo "\n<script language=\"JavaScript\">\n";
340
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
341
	echo "</script>\n\n";
342
}
343

    
344
/*
345
 *  HELPER FUNCTIONS
346
 */
347

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

    
373
?>
374

    
(106-106/106)