Project

General

Profile

Download (11.9 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
</head>
124

    
125
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
126
<form action="wizard.php" method="post">
127
<input type="hidden" name="xml" value="<?= $xml ?>">
128
<input type="hidden" name="stepid" value="<?= $stepid ?>">
129
<?php if ($savemsg) print_info_box($savemsg); ?>
130

    
131
<center>
132

    
133
&nbsp;<br>
134

    
135
<a href="http://<?= getenv("HTTP_HOST") ?>"><img border="0" src="/logo.gif"></a>
136
<p>
137

    
138
<table width="600" cellspacing="0" cellpadding="3">
139
    <!-- wizard goes here -->
140
    <tr><td>&nbsp;</td></tr>
141
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
142
    <?php
143
	if(!$pkg['step'][$stepid]['disableheader'])
144
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
145
    ?>
146

    
147
    <?php
148
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
149
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
150

    
151
		    $value = $field['value'];
152
		    $name  = $field['name'];
153

    
154
		    $name = ereg_replace(" ", "", $name);
155
		    $name = strtolower($name);
156

    
157
		    if($field['bindstofield'] <> "") {
158
				$arraynum = "";
159
				$field_conv = "";
160
				$field_split = split("->", $field['bindstofield']);
161
				// arraynum is used in cases where there is an array of the same field
162
				// name such as dnsserver (2 of them)
163
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
164
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
165
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
166
					eval($toeval);
167
					if ($field['type'] == "checkbox") {
168
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
169
						eval($toeval);
170
					}
171
		    }
172

    
173
		    if(!$field['combinefieldsend'])
174
			echo "<tr>";
175

    
176
		    if ($field['type'] == "input") {
177
			if(!$field['dontdisplayname']) {
178
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
179
				echo fixup_string($field['name']);
180
				echo ":</td>\n";
181
			}
182
			if(!$field['dontcombinecells'])
183
				echo "<td class=\"vtable\">\n";
184
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'>\n";
185
		    } else if ($field['type'] == "password") {
186
			if(!$field['dontdisplayname']) {
187
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
188
				echo fixup_string($field['name']);
189
				echo ":</td>\n";
190
			}
191
			if(!$field['dontcombinecells'])
192
				echo "<td class=\"vtable\">";
193
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
194
		    } else if ($field['type'] == "select") {
195
			if(!$field['dontdisplayname']) {
196
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
197
				echo fixup_string($field['name']);
198
				echo ":</td>\n";
199
			}
200
			// XXX: TODO: set $selected
201
			if($field['size']) $size = " size='" . $field['size'] . "' ";
202
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
203
			if(!$field['dontcombinecells'])
204
				echo "<td class=\"vtable\">\n";
205
			$onchange = "";
206
			foreach ($field['options']['option'] as $opt) {
207
				if($opt['enablefields'] <> "") {
208
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
209
				}
210
			}
211
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
212
			foreach ($field['options']['option'] as $opt) {
213
				$selected = "";
214
				if($value == $opt['value']) $selected = " SELECTED";
215
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
216
			}
217
			echo "</select>\n";
218
		    } else if ($field['type'] == "textarea") {
219
			if(!$field['dontdisplayname']) {
220
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
221
				echo fixup_string($field['name']);
222
				echo ":</td>";
223
			}
224
			if(!$field['dontcombinecells'])
225
				echo "<td class=\"vtable\">";
226
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
227
		    } else if ($field['type'] == "submit") {
228
			echo "<td>&nbsp;<br></td></tr>";
229
			echo "<tr><td colspan='2'><center>";
230
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
231
		    } else if ($field['type'] == "listtopic") {
232
			echo "<td>&nbsp;</td><tr>";
233
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
234
		    } else if ($field['type'] == "checkbox") {
235
			if(!$field['dontdisplayname']) {
236
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
237
				echo $field['name'];
238
				echo ":</td>";
239
			}
240
			$checked = "";
241
			if($value <> "") $checked = " CHECKED";
242
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked . ">\n";
243
		    }
244

    
245
		    if($field['typehint'] <> "") {
246
			echo $field['typehint'];
247
		    }
248

    
249
		    if($field['description'] <> "") {
250
			echo "<br>" . $field['description'];
251
			echo "</td>";
252
		    }
253

    
254
		    if(!$field['combinefieldsbegin'])
255
			 echo "</tr>\n";
256
		}
257
	}
258
    ?>
259
</table>
260

    
261
</form>
262
</body>
263
</html>
264

    
265
<?php
266

    
267
$fieldnames_array = Array();
268
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
269
	// create a fieldname loop that can be used with javascript
270
	// hide and enable features.
271
	echo "\n<script language=\"JavaScript\">\n";
272
	echo "function disableall() {\n";
273
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
274
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
275
			if(!$field['donotdisable'] <> "") {
276
				array_push($fieldnames_array, $field['name']);
277
				$fieldname = ereg_replace(" ", "", $field['name']);
278
				$fieldname = strtolower($fieldname);
279
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
280
			}
281
		}
282
	}
283
	echo "}\ndisableall();\n";
284
	echo "function enableitems(selectedindex) {\n";
285
	echo "disableall();\n";
286
	$idcounter = 0;
287
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
288
		echo "\tswitch(selectedindex) {\n";
289
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
290
			if($field['options']['option'] <> "") {
291
				foreach ($field['options']['option'] as $opt) {
292
					if($opt['enablefields'] <> "") {
293
						echo "\t\tcase " . $idcounter . ":\n";
294
						$enablefields_split = split(",", $opt['enablefields']);
295
						foreach ($enablefields_split as $efs) {
296
							$fieldname = ereg_replace(" ", "", $efs);
297
							$fieldname = strtolower($fieldname);
298
							if($fieldname <> "") {
299
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
300
								echo $onchange;
301
							}
302
						}
303
						echo "\t\t\tbreak;\n";
304
					}
305
					$idcounter = $idcounter + 1;
306
				}
307
			}
308
		}
309
		echo "\t}\n";
310
	}
311
	echo "}\n";
312
	echo "disableall();\n";
313
	echo "</script>\n\n";
314
}
315

    
316

    
317
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
318
	// handle after form display event.
319
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
320
}
321

    
322
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
323
	// handle after form display event.
324
        echo "\n<script language=\"JavaScript\">\n";
325
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
326
	echo "</script>\n\n";
327
}
328

    
329
/*
330
 *  HELPER FUNCTIONS
331
 */
332

    
333
function fixup_string($string) {
334
	global $config;
335
	// fixup #1: $myurl -> http[s]://ip_address:port/
336
	$https = "";
337
	$port = "";
338
	$urlport = "";
339
	$port = $config['system']['webguiport'];
340
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
341
	if($config['system']['webguiproto'] == "https") $https = "s";
342
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
343
	$newstring = str_replace("\$myurl", $myurl, $string);
344
	$string = $newstring;
345
	// fixup #2: $wanip
346
	$curwanip = get_current_wan_address();
347
	$newstring = str_replace("\$wanip", $curwanip, $string);
348
	$string = $newstring;
349
	// fixup #3: $lanip
350
	$lancfg = $config['interfaces']['lan'];
351
	$lanip = $lancfg['ipaddr'];
352
	$newstring = str_replace("\$lanip", $lanip, $string);
353
	$string = $newstring;
354
	// fixup #4: fix'r'up here.
355
	return $newstring;
356
}
357

    
358
?>
359

    
(100-100/100)