Project

General

Profile

Download (16.2 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
	if($unset <> "") {
114
		$text = "unset(\$config" . $field_conv . ");";
115
		eval($text);
116
		$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
117
		eval($text);
118
	} else {
119
		if($arraynum <> "") {
120
			$text = "\$config" . $field_conv . "[" . $arraynum . "] = \"" . $updatetext . "\";";
121
		} else {
122
			$text = "\$config" . $field_conv . " = \"" . $updatetext . "\";";
123
		}
124
		eval($text);
125
	}
126
}
127

    
128
if($pkg['step'][$stepid]['stepbeforeformdisplay'] <> "") {
129
	// handle before form display event.
130
        // good for modifying posted values, etc.
131
	eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
132
}
133

    
134
$pgtitle = $title;
135
include("head.inc");
136

    
137
?>
138
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
139
<?php if($pkg['step'][$stepid]['fields']['field'] <> "") { ?>
140
<script language="JavaScript">
141
<!--
142
function enablechange() {
143
<?php
144
        foreach($pkg['step'][$stepid]['fields']['field'] as $field) {
145
                if(isset($field['enablefields']) or isset($field['checkenablefields'])) {
146
                        print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
147
                        if(isset($field['enablefields'])) {
148
                                $enablefields = explode(',', $field['enablefields']);
149
                                foreach($enablefields as $enablefield) {
150
                                        $enablefield = strtolower($enablefield);
151
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 1;' . "\n";
152
                                }
153
                        }
154
                        if(isset($field['checkenablefields'])) {
155
                                $checkenablefields = explode(',', $field['checkenablefields']);
156
                                foreach($checkenablefields as $checkenablefield) {
157
                                        $checkenablefield = strtolower($checkenablefield);
158
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 0;' . "\n";
159
                                }
160
                        }
161
                        print "\t" . '} else {' . "\n";
162
                        if(isset($field['enablefields'])) {
163
                                foreach($enablefields as $enablefield) {
164
                                        $enablefield = strtolower($enablefield);
165
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 0;' . "\n";
166
                                }
167
                        }
168
                        if(isset($field['checkenablefields'])) {
169
                                foreach($checkenablefields as $checkenablefield) {
170
                                        $checkenablefield = strtolower($checkenablefield);
171
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 1;' . "\n";
172
                                }
173
                        }   
174
                        print "\t" . '}' . "\n";
175
                }
176
        }
177
?>
178
}
179
//-->
180
</script>
181
<?php } ?>
182

    
183
<form action="wizard.php" method="post" name="iform" id="iform">
184
<input type="hidden" name="xml" value="<?= $xml ?>">
185
<input type="hidden" name="stepid" value="<?= $stepid ?>">
186
<?php if ($savemsg) print_info_box($savemsg); ?>
187

    
188
<center>
189

    
190
&nbsp;<br>
191

    
192
<a href="/"><img border="0" src="./themes/<?= $g['theme']; ?>/images/logo.gif"></a>
193
<p>
194

    
195
<div style="width:700px;background-color:#ffffff" id="roundme">
196
<table bgcolor="#ffffff" width="600" cellspacing="0" cellpadding="3">
197
    <!-- wizard goes here -->
198
    <tr><td>&nbsp;</td></tr>
199
    <tr><td colspan='2'><center><b><?= fixup_string($description) ?></b></center></td></tr><tr><td>&nbsp;</td></tr>
200
    <?php
201
	if(!$pkg['step'][$stepid]['disableheader'])
202
		echo "<tr><td colspan=\"2\" class=\"listtopic\">" . fixup_string($title) . "</td></tr>";
203
    ?>
204

    
205
    <?php
206
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
207
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
208

    
209
		    $value = $field['value'];
210
		    $name  = $field['name'];
211

    
212
		    $name = ereg_replace(" ", "", $name);
213
		    $name = strtolower($name);
214

    
215
		    if($field['bindstofield'] <> "") {
216
				$arraynum = "";
217
				$field_conv = "";
218
				$field_split = split("->", $field['bindstofield']);
219
				// arraynum is used in cases where there is an array of the same field
220
				// name such as dnsserver (2 of them)
221
				if($field['arraynum'] <> "") $arraynum = "[" . $field['arraynum'] . "]";
222
				foreach ($field_split as $f) $field_conv .= "['" . $f . "']";
223
					$toeval = "\$value = \$config" . $field_conv . $arraynum . ";";
224
					eval($toeval);
225
					if ($field['type'] == "checkbox") {
226
						$toeval = "if(isset(\$config" . $field_conv . $arraynum . ")) \$value = \" CHECKED\";";
227
						eval($toeval);
228
					}
229
		    }
230

    
231
		    if(!$field['combinefieldsend'])
232
			echo "<tr>";
233

    
234
		    if ($field['type'] == "input") {
235
			if(!$field['dontdisplayname']) {
236
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
237
				echo fixup_string($field['name']);
238
				echo ":</td>\n";
239
			}
240
			if(!$field['dontcombinecells'])
241
				echo "<td class=\"vtable\">\n";
242

    
243
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "'";
244
			if($field['validate'])
245
				echo " onChange='FieldValidate(this.value, \"{$field['validate']}\", \"{$field['message']}\");'";
246
			echo ">\n";
247
		    } else if ($field['type'] == "password") {
248
			if(!$field['dontdisplayname']) {
249
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
250
				echo fixup_string($field['name']);
251
				echo ":</td>\n";
252
			}
253
			if(!$field['dontcombinecells'])
254
				echo "<td class=\"vtable\">";
255
			echo "<input id='" . $name . "' name='" . $name . "' value='" . $value . "' type='password'>\n";
256
		    } else if ($field['type'] == "select") {
257
			if(!$field['dontdisplayname']) {
258
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
259
				echo fixup_string($field['name']);
260
				echo ":</td>\n";
261
			}
262
			if($field['size']) $size = " size='" . $field['size'] . "' ";
263
			if($field['multiple'] == "yes") $multiple = "MULTIPLE ";
264
			if(!$field['dontcombinecells'])
265
				echo "<td class=\"vtable\">\n";
266
			$onchange = "";
267
			foreach ($field['options']['option'] as $opt) {
268
				if($opt['enablefields'] <> "") {
269
					$onchange = "onchange=\"enableitems(this.selectedIndex);\" ";
270
				}
271
			}
272
			echo "<select " . $onchange . $multiple . $size . "id='" . $name . "' name='" . $name . "'>\n";
273
			foreach ($field['options']['option'] as $opt) {
274
				$selected = "";
275
				if($value == $opt['value']) $selected = " SELECTED";
276
			    echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
277
			}
278
			echo "</select>\n";
279
		    } else if ($field['type'] == "textarea") {
280
			if(!$field['dontdisplayname']) {
281
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
282
				echo fixup_string($field['name']);
283
				echo ":</td>";
284
			}
285
			if(!$field['dontcombinecells'])
286
				echo "<td class=\"vtable\">";
287
			echo "<textarea id='" . $name . "' name='" . $name . ">" . $value . "</textarea>\n";
288
		    } else if ($field['type'] == "submit") {
289
			echo "<td>&nbsp;<br></td></tr>";
290
			echo "<tr><td colspan='2'><center>";
291
			echo "<input type='submit' name='" . $name . "' value='" . $field['name'] . "'>\n";
292
		    } else if ($field['type'] == "listtopic") {
293
			echo "<td>&nbsp;</td><tr>";
294
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $field['name'] . "<br></td>\n";
295
		    } else if ($field['type'] == "subnet_select") {
296
			if(!$field['dontdisplayname']) {
297
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
298
				echo fixup_string($field['name']);
299
				echo ":</td>";
300
			}
301
			if(!$field['dontcombinecells'])
302
				echo "<td class=\"vtable\">";
303
			echo "<select name='{$name}'>\n";
304
			for($x=1; $x<33; $x++) {
305
				$CHECKED = "";
306
				if($value == $x) $CHECKED = " SELECTED";
307
				if($x <> 31)
308
					echo "<option value='{$x}' {$CHECKED}>{$x}</option>\n";
309
			}
310
			echo "</select>\n";
311
		    } else if ($field['type'] == "timezone_select") {
312
			if(!$field['dontdisplayname']) {
313
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
314
				echo fixup_string($field['name']);
315
				echo ":</td>";
316
			}
317
			if(!$field['dontcombinecells'])
318
				echo "<td class=\"vtable\">";
319
			echo "<select name='{$name}'>\n";
320
			foreach ($timezonelist as $tz) {
321
				$SELECTED = "";
322
				if ($value == $tz) $SELECTED = " SELECTED";
323
				echo "<option value='" . htmlspecialchars($tz) . "' {$SELECTED}>";
324
				echo htmlspecialchars($tz);
325
				echo "</option>\n";
326
			}
327
			echo "</select>\n";
328
		    } else if ($field['type'] == "checkbox") {
329
			if(!$field['dontdisplayname']) {
330
				echo "<td width=\"22%\" align=\"right\" class=\"vncellreq\">\n";
331
				echo $field['name'];
332
				echo ":</td>";
333
			}
334
			$checked = "";
335
			if($value <> "") $checked = " CHECKED";
336
			echo "<td class=\"vtable\"><input type='checkbox' id='" . $name . "' name='" . $name . "' " . $checked;
337
			if(isset($field['enablefields']) or isset($field['checkenablefields'])) echo " onClick=\"enablechange()\"";
338
			echo ">\n";
339
		    }
340

    
341
		    if($field['typehint'] <> "") {
342
			echo $field['typehint'];
343
		    }
344

    
345
		    if($field['description'] <> "") {
346
			echo "<br>" . $field['description'];
347
			echo "</td>";
348
		    }
349

    
350
		    if(!$field['combinefieldsbegin'])
351
			 echo "</tr>\n";
352

    
353
		    if($field['warning'] <> "") {
354
			echo "<br><b><font color=\"red\">" . $field['warning'] . "</font></b>";
355
		    }
356

    
357
		}
358
	}
359
    ?>
360
</table>
361
<br>&nbsp;
362
</div>
363
</form>
364

    
365
<script type="text/javascript">
366
NiftyCheck();
367
Rounded("div#roundme","all","#333333","#FFFFFF","smooth");
368
</script>
369

    
370
</body>
371
</html>
372

    
373
<?php
374

    
375
$fieldnames_array = Array();
376
if($pkg['step'][$stepid]['disableallfieldsbydefault'] <> "") {
377
	// create a fieldname loop that can be used with javascript
378
	// hide and enable features.
379
	echo "\n<script language=\"JavaScript\">\n";
380
	echo "function disableall() {\n";
381
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
382
		if($field['type'] <> "submit" and $field['type'] <> "listtopic") {
383
			if(!$field['donotdisable'] <> "") {
384
				array_push($fieldnames_array, $field['name']);
385
				$fieldname = ereg_replace(" ", "", $field['name']);
386
				$fieldname = strtolower($fieldname);
387
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
388
			}
389
		}
390
	}
391
	echo "}\ndisableall();\n";
392
	echo "function enableitems(selectedindex) {\n";
393
	echo "disableall();\n";
394
	$idcounter = 0;
395
	if($pkg['step'][$stepid]['fields']['field'] <> "") {
396
		echo "\tswitch(selectedindex) {\n";
397
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
398
			if($field['options']['option'] <> "") {
399
				foreach ($field['options']['option'] as $opt) {
400
					if($opt['enablefields'] <> "") {
401
						echo "\t\tcase " . $idcounter . ":\n";
402
						$enablefields_split = split(",", $opt['enablefields']);
403
						foreach ($enablefields_split as $efs) {
404
							$fieldname = ereg_replace(" ", "", $efs);
405
							$fieldname = strtolower($fieldname);
406
							if($fieldname <> "") {
407
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
408
								echo $onchange;
409
							}
410
						}
411
						echo "\t\t\tbreak;\n";
412
					}
413
					$idcounter = $idcounter + 1;
414
				}
415
			}
416
		}
417
		echo "\t}\n";
418
	}
419
	echo "}\n";
420
	echo "disableall();\n";
421
	echo "</script>\n\n";
422
}
423

    
424

    
425
if($pkg['step'][$stepid]['stepafterformdisplay'] <> "") {
426
	// handle after form display event.
427
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
428
}
429

    
430
if($pkg['step'][$stepid]['javascriptafterformdisplay'] <> "") {
431
	// handle after form display event.
432
        echo "\n<script language=\"JavaScript\">\n";
433
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
434
	echo "</script>\n\n";
435
}
436

    
437
/*
438
 *  HELPER FUNCTIONS
439
 */
440

    
441
function fixup_string($string) {
442
	global $config, $myurl;
443
	$newstring = $string;
444
	// fixup #1: $myurl -> http[s]://ip_address:port/
445
	$https = "";
446
	$port = $config['system']['webguiport'];
447
	if($port <> "443" and $port <> "80")
448
		$urlport = ":" . $port;
449
	else
450
		$urlport = "";
451
	if($config['system']['webguiproto'] == "https")
452
		$https = "s";
453
    $myurl = "http" . $https . "://" . $config['interfaces']['lan']['ipaddr'] . $urlport;
454
	$newstring = str_replace("\$myurl", $myurl, $newstring);
455
	// fixup #2: $wanip
456
	$curwanip = get_current_wan_address();
457
	$newstring = str_replace("\$wanip", $curwanip, $newstring);
458
	// fixup #3: $lanip
459
	$lanip = $config['interfaces']['lan']['ipaddr'];
460
	$newstring = str_replace("\$lanip", $lanip, $newstring);
461
	// fixup #4: fix'r'up here.
462
	return $newstring;
463
}
464

    
465
function is_timezone($elt) {
466
	return !preg_match("/\/$/", $elt);
467
}
468

    
469
?>
(136-136/137)