Project

General

Profile

Download (27.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	wizard.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	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
16
 *		the documentation and/or other materials provided with the
17
 *		distribution.
18
 *
19
 *	3. All advertising materials mentioning features or use of this software
20
 *		must display the following acknowledgment:
21
 *		"This product includes software developed by the pfSense Project
22
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *	4. The names "pfSense" and "pfSense Project" must not be used to
25
 *		 endorse or promote products derived from this software without
26
 *		 prior written permission. For written permission, please contact
27
 *		 coreteam@pfsense.org.
28
 *
29
 *	5. Products derived from this software may not be called "pfSense"
30
 *		nor may "pfSense" appear in their names without prior written
31
 *		permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35
 *
36
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *	====================================================================
53
 *
54
 */
55

    
56
##|+PRIV
57
##|*IDENT=page-pfsensewizardsubsystem
58
##|*NAME=pfSense wizard subsystem
59
##|*DESCR=Allow access to the 'pfSense wizard subsystem' page.
60
##|*MATCH=wizard.php*
61
##|-PRIV
62

    
63
require("globals.inc");
64
require("guiconfig.inc");
65
require("functions.inc");
66
require_once("filter.inc");
67
require("shaper.inc");
68
require_once("rrd.inc");
69
require_once("system.inc");
70

    
71
// This causes the step #, field type and field name to be printed at the top of the page
72
define(DEBUG, false);
73

    
74
global $g;
75

    
76
$stepid = htmlspecialchars($_GET['stepid']);
77
if (isset($_POST['stepid'])) {
78
	$stepid = htmlspecialchars($_POST['stepid']);
79
}
80

    
81
if (!$stepid) {
82
	$stepid = "0";
83
}
84

    
85
$xml = htmlspecialchars($_GET['xml']);
86
if ($_POST['xml']) {
87
	$xml = htmlspecialchars($_POST['xml']);
88
}
89

    
90
if (empty($xml)) {
91
	$xml = "not_defined";
92
	print_info_box(sprintf(gettext("ERROR:  Could not open %s."), $xml));
93
	die;
94
} else {
95
	$wizard_xml_prefix = "{$g['www_path']}/wizards";
96
	$wizard_full_path = "{$wizard_xml_prefix}/{$xml}";
97
	if (substr_compare(realpath($wizard_full_path), $wizard_xml_prefix, 0, strlen($wizard_xml_prefix))) {
98
		print_info_box(gettext("ERROR: Invalid path specified."));
99
		die;
100
	}
101
	if (file_exists($wizard_full_path)) {
102
		$pkg = parse_xml_config_pkg($wizard_full_path, "pfsensewizard");
103
	} else {
104
		print_info_box(sprintf(gettext("ERROR:  Could not open %s."), $xml));
105
		die;
106
	}
107
}
108

    
109
if (!is_array($pkg)) {
110
	print_info_box(sprintf(gettext("ERROR: Could not parse %s/wizards/%s file."), $g['www_path'], $xml));
111
	die;
112
}
113

    
114
$title	   = preg_replace("/pfSense/i", $g['product_name'], $pkg['step'][$stepid]['title']);
115
$description = preg_replace("/pfSense/i", $g['product_name'], $pkg['step'][$stepid]['description']);
116
$totalsteps	 = $pkg['totalsteps'];
117

    
118
if ($pkg['includefile']) {
119
	require_once($pkg['includefile']);
120
}
121

    
122
if ($pkg['step'][$stepid]['includefile']) {
123
	require_once($pkg['step'][$stepid]['includefile']);
124
}
125

    
126
if ($pkg['step'][$stepid]['stepsubmitbeforesave']) {
127
	eval($pkg['step'][$stepid]['stepsubmitbeforesave']);
128
}
129

    
130
if ($_POST && !$input_errors) {
131
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
132
		if (!empty($field['bindstofield']) and $field['type'] != "submit") {
133
			$fieldname = $field['name'];
134
			$fieldname = str_replace(" ", "", $fieldname);
135
			$fieldname = strtolower($fieldname);
136
			// update field with posted values.
137
			if ($field['unsetfield'] != "") {
138
				$unset_fields = "yes";
139
			} else {
140
				$unset_fields = "";
141
			}
142

    
143
			if ($field['arraynum'] != "") {
144
				$arraynum = $field['arraynum'];
145
			} else {
146
				$arraynum = "";
147
			}
148

    
149
			update_config_field($field['bindstofield'], $_POST[$fieldname], $unset_fields, $arraynum, $field['type']);
150
		}
151

    
152
	}
153
	// run custom php code embedded in xml config.
154
	if ($pkg['step'][$stepid]['stepsubmitphpaction'] != "") {
155
		eval($pkg['step'][$stepid]['stepsubmitphpaction']);
156
	}
157
	if (!$input_errors) {
158
		write_config();
159
	}
160

    
161
	$stepid++;
162
	if ($stepid > $totalsteps) {
163
		$stepid = $totalsteps;
164
	}
165
}
166

    
167
function update_config_field($field, $updatetext, $unset, $arraynum, $field_type) {
168
	global $config;
169
	$field_split = explode("->", $field);
170
	foreach ($field_split as $f) {
171
		$field_conv .= "['" . $f . "']";
172
	}
173
	if ($field_conv == "") {
174
		return;
175
	}
176
	if ($arraynum != "") {
177
		$field_conv .= "[" . $arraynum . "]";
178
	}
179
	if (($field_type == "checkbox" and $updatetext != "on") || $updatetext == "") {
180
		/*
181
		 * item is a checkbox, it should have the value "on"
182
		 * if it was checked
183
		 */
184
		$var = "\$config{$field_conv}";
185
		$text = "if (isset({$var})) unset({$var});";
186
		eval($text);
187
		return;
188
	}
189

    
190
	if ($field_type == "interfaces_selection") {
191
		$var = "\$config{$field_conv}";
192
		$text = "if (isset({$var})) unset({$var});";
193
		$text .= "\$config" . $field_conv . " = \"" . $updatetext . "\";";
194
		eval($text);
195
		return;
196
	}
197

    
198
	if ($unset == "yes") {
199
		$text = "unset(\$config" . $field_conv . ");";
200
		eval($text);
201
	}
202
	$text = "\$config" . $field_conv . " = \"" . addslashes($updatetext) . "\";";
203
	eval($text);
204
}
205

    
206
$title	   = preg_replace("/pfSense/i", $g['product_name'], $pkg['step'][$stepid]['title']);
207
$description = preg_replace("/pfSense/i", $g['product_name'], $pkg['step'][$stepid]['description']);
208

    
209
// handle before form display event.
210
do {
211
	$oldstepid = $stepid;
212
	if ($pkg['step'][$stepid]['stepbeforeformdisplay'] != "") {
213
		eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
214
	}
215
} while ($oldstepid != $stepid);
216

    
217
$pgtitle = array(gettext("Wizard"), gettext($pkg['step'][0]['title']));	//First step is main title of the wizard in the breadcrumb
218
$pgtitle[] = ($stepid > 0 ? gettext($pkg['step'][$stepid]['title']):'&nbsp;');		//Following steps are sub-level breadcrumbs.
219
$shortcut_section = "Wizard";
220
include("head.inc");
221

    
222
if ($pkg['step'][$stepid]['fields']['field'] != "") { ?>
223
<script type="text/javascript">
224
//<![CDATA[
225

    
226

    
227
	function FieldValidate(userinput, regexp, message) {
228
		if (!userinput.match(regexp)) {
229
			alert(message);
230
		}
231
	}
232

    
233
	function enablechange() {
234

    
235
	<?php
236

    
237
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
238
			if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
239
				print "\t" . 'if ( $("#" + "' . strtolower($field['name']) . '").prop("checked") ) {' . "\n";
240

    
241
				if (isset($field['enablefields'])) {
242
					$enablefields = explode(',', $field['enablefields']);
243
					foreach ($enablefields as $enablefield) {
244
						$enablefield = strtolower($enablefield);
245
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", false);' . "\n";
246
					}
247
				}
248

    
249
				if (isset($field['checkenablefields'])) {
250
					$checkenablefields = explode(',', $field['checkenablefields']);
251
					foreach ($checkenablefields as $checkenablefield) {
252
						$checkenablefield = strtolower($checkenablefield);
253
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", true);' . "\n";
254
					}
255
				}
256

    
257
				print "\t" . '} else {' . "\n";
258
				if (isset($field['enablefields'])) {
259
					$enablefields = explode(',', $field['enablefields']);
260
					foreach ($enablefields as $enablefield) {
261
						$enablefield = strtolower($enablefield);
262
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", true);' . "\n";
263

    
264
					}
265
				}
266

    
267
			if (isset($field['checkdisablefields'])) {
268
				$checkenablefields = explode(',', $field['checkdisablefields']);
269
				foreach ($checkenablefields as $checkenablefield) {
270
					$checkenablefield = strtolower($checkenablefield);
271
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", false);' . "\n";
272
					}
273
				}
274

    
275
				print "\t" . '}' . "\n";
276
			}
277
		}
278
	?>
279

    
280
	}
281

    
282
	function disablechange() {
283
	<?php
284
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
285
			if (isset($field['disablefields']) or isset($field['checkdisablefields'])) {
286

    
287
				print "\t" . 'if ( $("#" + "' . strtolower($field['name']) . '").prop("checked") ) {' . "\n";
288

    
289
				if (isset($field['disablefields'])) {
290
					$enablefields = explode(',', $field['disablefields']);
291
					foreach ($enablefields as $enablefield) {
292
						$enablefield = strtolower($enablefield);
293

    
294
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", true);' . "\n";
295
					}
296
				}
297
				if (isset($field['checkdisablefields'])) {
298
					$checkenablefields = explode(',', $field['checkdisablefields']);
299
					foreach ($checkenablefields as $checkenablefield) {
300
						$checkenablefield = strtolower($checkenablefield);
301
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", true);' . "\n";
302
					}
303
				}
304
				print "\t" . '} else {' . "\n";
305
				if (isset($field['disablefields'])) {
306
					$enablefields = explode(',', $field['disablefields']);
307
					foreach ($enablefields as $enablefield) {
308
						$enablefield = strtolower($enablefield);
309
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", false);' . "\n";
310
					}
311
				}
312
				if (isset($field['checkdisablefields'])) {
313
					$checkenablefields = explode(',', $field['checkdisablefields']);
314
					foreach ($checkenablefields as $checkenablefield) {
315
						$checkenablefield = strtolower($checkenablefield);
316
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", false);' . "\n";
317
					}
318
				}
319
				print "\t" . '}' . "\n";
320
			}
321
		}
322
	?>
323
	}
324

    
325
	function showchange() {
326
<?php
327
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
328
			if (isset($field['showfields'])) {
329
				print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
330
				if (isset($field['showfields'])) {
331
					$showfields = explode(',', $field['showfields']);
332
					foreach ($showfields as $showfield) {
333
						$showfield = strtolower($showfield);
334
						//print "\t\t" . 'document.iform.' . $showfield . ".display =\"none\";\n";
335
						print "\t\t $('#". $showfield . "').hide();";
336
					}
337
				}
338
				print "\t" . '} else {' . "\n";
339
				if (isset($field['showfields'])) {
340
					$showfields = explode(',', $field['showfields']);
341
					foreach ($showfields as $showfield) {
342
						$showfield = strtolower($showfield);
343
						#print "\t\t" . 'document.iform.' . $showfield . ".display =\"\";\n";
344
						print "\t\t $('#". $showfield . "').show();";
345
					}
346
				}
347
				print "\t" . '}' . "\n";
348
			}
349
		}
350
?>
351
	}
352

    
353
//]]>
354
</script>
355
<?php }
356

    
357
function fixup_string($string) {
358
	global $config, $g, $myurl, $title;
359
	$newstring = $string;
360
	// fixup #1: $myurl -> http[s]://ip_address:port/
361
	switch ($config['system']['webgui']['protocol']) {
362
		case "http":
363
			$proto = "http";
364
			break;
365
		case "https":
366
			$proto = "https";
367
			break;
368
		default:
369
			$proto = "http";
370
			break;
371
	}
372
	$port = $config['system']['webgui']['port'];
373
	if ($port != "") {
374
		if (($port == "443" and $proto != "https") or ($port == "80" and $proto != "http")) {
375
			$urlport = ":" . $port;
376
		} elseif ($port != "80" and $port != "443") {
377
			$urlport = ":" . $port;
378
		} else {
379
			$urlport = "";
380
		}
381
	}
382

    
383
	$http_host = $_SERVER['HTTP_HOST'];
384
	$urlhost = $http_host;
385
	// If finishing the setup wizard, check if accessing on a LAN or WAN address that changed
386
	if ($title == "Reload in progress") {
387
		if (is_ipaddr($urlhost)) {
388
			$host_if = find_ip_interface($urlhost);
389
			if ($host_if) {
390
				$host_if = convert_real_interface_to_friendly_interface_name($host_if);
391
				if ($host_if && is_ipaddr($config['interfaces'][$host_if]['ipaddr'])) {
392
					$urlhost = $config['interfaces'][$host_if]['ipaddr'];
393
				}
394
			}
395
		} else if ($urlhost == $config['system']['hostname']) {
396
			$urlhost = $config['wizardtemp']['system']['hostname'];
397
		} else if ($urlhost == $config['system']['hostname'] . '.' . $config['system']['domain']) {
398
			$urlhost = $config['wizardtemp']['system']['hostname'] . '.' . $config['wizardtemp']['system']['domain'];
399
		}
400
	}
401

    
402
	if ($urlhost != $http_host) {
403
		file_put_contents("{$g['tmp_path']}/setupwizard_lastreferrer", $proto . "://" . $http_host . $urlport . $_SERVER['REQUEST_URI']);
404
	}
405

    
406
	$myurl = $proto . "://" . $urlhost . $urlport . "/";
407

    
408
	if (strstr($newstring, "\$myurl")) {
409
		$newstring = str_replace("\$myurl", $myurl, $newstring);
410
	}
411
	// fixup #2: $wanip
412
	if (strstr($newstring, "\$wanip")) {
413
		$curwanip = get_interface_ip();
414
		$newstring = str_replace("\$wanip", $curwanip, $newstring);
415
	}
416
	// fixup #3: $lanip
417
	if (strstr($newstring, "\$lanip")) {
418
		$lanip = get_interface_ip("lan");
419
		$newstring = str_replace("\$lanip", $lanip, $newstring);
420
	}
421
	// fixup #4: fix'r'up here.
422
	return $newstring;
423
}
424

    
425
function is_timezone($elt) {
426
	return !preg_match("/\/$/", $elt);
427
}
428

    
429
if ($title == "Reload in progress") {
430
	$ip = fixup_string("\$myurl");
431
} else {
432
	$ip = "/";
433
}
434

    
435
if ($input_errors) {
436
	print_input_errors($input_errors);
437
}
438
if ($savemsg) {
439
	print_info_box($savemsg, 'success');
440
}
441
if ($_GET['message'] != "") {
442
	print_info_box(htmlspecialchars($_GET['message']));
443
}
444
if ($_POST['message'] != "") {
445
	print_info_box(htmlspecialchars($_POST['message']));
446
}
447

    
448
$completion = ($stepid == 0) ? 0:($stepid * 100) / ($totalsteps -1);
449
?>
450

    
451
<!-- Present the pfSense logo -->
452
<div class="text-center"><p><a href="<?=$ip?>"><img src="logo-black.png" alt="logo-black" style="border:0px; vertical-align:middle" height="45" width="180" /></a></p></div><br /><br/>
453

    
454
<!-- Draw a progress bar to show step progress -->
455
<div class="progress">
456
	<div class="progress-bar" role="progressbar" aria-valuenow="<?=$completion?>" aria-valuemin="0" aria-valuemax="100" style="width:<?=$completion?>%">
457
	</div>
458
</div>
459

    
460
<?php
461

    
462
$form = new Form(false);
463

    
464
$form->addGlobal(new Form_Input(
465
	'stepid',
466
	null,
467
	'hidden',
468
	$stepid
469
));
470

    
471
$form->addGlobal(new Form_Input(
472
	'xml',
473
	null,
474
	'hidden',
475
	$xml
476
));
477

    
478
$section = new Form_Section(fixup_string($title));
479

    
480
if ($description) {
481
	$section->addInput(new Form_StaticText(
482
		null,
483
		fixup_string($description)
484
	));
485
}
486

    
487
$inputaliases = array();
488
if ($pkg['step'][$stepid]['fields']['field'] != "") {
489
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
490

    
491
		$value = $field['value'];
492
		$name  = $field['name'];
493

    
494
		$name = preg_replace("/\s+/", "", $name);
495
		$name = strtolower($name);
496

    
497
		if ($field['bindstofield'] != "") {
498
			$arraynum = "";
499
			$field_conv = "";
500
			$field_split = explode("->", $field['bindstofield']);
501
			// arraynum is used in cases where there is an array of the same field
502
			// name such as dnsserver (2 of them)
503
			if ($field['arraynum'] != "") {
504
				$arraynum = "[" . $field['arraynum'] . "]";
505
			}
506

    
507
			foreach ($field_split as $f) {
508
				$field_conv .= "['" . $f . "']";
509
			}
510

    
511
			if ($field['type'] == "checkbox") {
512
				$toeval = "if (isset(\$config" . $field_conv . $arraynum . ")) { \$value = \$config" . $field_conv . $arraynum . "; if (empty(\$value)) \$value = true; }";
513
			} else {
514
				$toeval = "if (isset(\$config" . $field_conv . $arraynum . ")) \$value = \$config" . $field_conv . $arraynum . ";";
515
			}
516

    
517
			eval($toeval);
518
		}
519

    
520

    
521
		if (DEBUG) {
522
			print('Step: ' . $pkg['step'][$stepid]['id'] . ', Field: ' . $field['type'] . ', Name: ' . $name . '<br />');
523
		}
524

    
525
		switch ($field['type']) {
526
			case "input":
527
				if ($field['displayname']) {
528
					$etitle = $field['displayname'];
529

    
530
				} else if (!$field['dontdisplayname']) {
531
					$etitle =  fixup_string($field['name']);
532
				}
533

    
534
				$section->addInput(new Form_Input(
535
					$name,
536
					$etitle,
537
					'text',
538
					$value
539
				))->setHelp($field['description'])
540
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")":"");
541

    
542
				break;
543
			case "text":
544
				$section->addInput(new Form_StaticText(
545
					null,
546
					$field['description']
547
				));
548

    
549
				break;
550
			case "inputalias":
551
				if ($field['displayname']) {
552
					$etitle = $field['displayname'];
553

    
554
				} else if (!$field['dontdisplayname']) {
555
					$etitle =  fixup_string($field['name']);
556
				}
557

    
558
				$onchange = "";
559

    
560
				if ($field['validate']) {
561
					$onchange="FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")";
562
				}
563

    
564
				$section->addInput(new Form_Input(
565
					$name,
566
					$etitle,
567
					'text',
568
					$value
569
				))->setAttribute('autocomplete', 'off')
570
				  ->setOnchange($onchange)
571
				  ->setHelp($field['description']);
572

    
573
				break;
574
			case "interfaces_selection":
575
			case "interface_select":
576

    
577
				$name = strtolower($name);
578
				$options = array();
579
				$selected = array();
580

    
581
				$etitle = (fixup_string($field['displayname'])) ? $field['displayname'] : $field['name'];
582

    
583
				if (($field['multiple'] != "") && ($field['multiple'] != "0")) {
584
					$multiple = true;
585
				} else {
586
					$multiple = false;
587
				}
588

    
589
				if ($field['add_to_interfaces_selection'] != "") {
590
					if ($field['add_to_interfaces_selection'] == $value) {
591
						array_push($selected, $value);
592
					}
593

    
594
					$options[$field['add_to_interfaces_selection']] = $field['add_to_interfaces_selection'];
595
				}
596

    
597
				if ($field['type'] == "interface_select") {
598
					$interfaces = get_interface_list();
599
				} else {
600
					$interfaces = get_configured_interface_with_descr();
601
				}
602

    
603
				foreach ($interfaces as $ifname => $iface) {
604
					if ($field['type'] == "interface_select") {
605
						$iface = $ifname;
606
						if ($iface['mac']) {
607
							$iface .= " ({$iface['mac']})";
608
						}
609
					}
610

    
611
					if ($value == $ifname) {
612
						array_push($selected, $value);
613
					}
614

    
615
					$canecho = 0;
616
					if ($field['interface_filter'] != "") {
617
						if (stristr($ifname, $field['interface_filter']) == true) {
618
							$canecho = 1;
619
						}
620
					} else {
621
						$canecho = 1;
622
					}
623

    
624
					if ($canecho == 1) {
625
						$options[$ifname] = $iface;
626
					}
627
				}
628

    
629
				$section->addInput(new Form_Select(
630
					$name,
631
					$etitle,
632
					($multiple) ? $selected:$selected[0],
633
					$options,
634
					$multiple
635
				))->setHelp($field['description']);
636

    
637
				break;
638
			case "password":
639
				if ($field['displayname']) {
640
					$etitle = $field['displayname'];
641
				} else if (!$field['dontdisplayname']) {
642
					$etitle =  fixup_string($field['name']);
643
				}
644

    
645
				$section->addInput(new Form_Input(
646
					$name,
647
					$etitle,
648
					'password',
649
					$value
650
				))->setHelp($field['description'])
651
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] ."\")":"");
652

    
653
				break;
654
			case "certca_selection":
655
				$options = array();
656
				$selected = "";
657

    
658
				$name = strtolower($name);
659

    
660
				$etitle = (fixup_string($field['displayname']) ? $field['displayname'] : $field['name']);
661

    
662
				if ($field['add_to_certca_selection'] != "") {
663
					if ($field['add_to_certca_selection'] == $value) {
664
						$selected = $value;
665
					}
666

    
667
					$options[$field['add_to_certca_selection']] = $field['add_to_certca_selection'];
668
				}
669

    
670
				foreach ($config['ca'] as $ca) {
671
					$caname = htmlspecialchars($ca['descr']);
672

    
673
					if ($value == $caname) {
674
						$selected = $value;
675
					}
676

    
677
					$canecho = 0;
678
					if ($field['certca_filter'] != "") {
679
						if (stristr($caname, $field['certca_filter']) == true) {
680
							$canecho = 1;
681
						}
682
					} else {
683
						$canecho = 1;
684
					}
685
					if ($canecho == 1) {
686
						$options[$ca['refid']] = $caname;
687
					}
688
				}
689

    
690
				$section->addInput(new Form_Select(
691
					$name,
692
					$etitle,
693
					$selected,
694
					$options
695
				))->setHelp($field['description']);
696

    
697
				break;
698
			case "cert_selection":
699
				$options = array();
700
				$selected = array();
701

    
702
				$multiple = false;
703
				$name = strtolower($name);
704

    
705
				$etitle = (fixup_string($field['displayname']) ? $field['displayname'] : $field['name']);
706

    
707
				if ($field['add_to_cert_selection'] != "") {
708
					if ($field['add_to_cert_selection'] == $value) {
709
						array_push($selected, $value);
710
					}
711

    
712
					$options[$field['add_to_cert_selection']] = $field['add_to_cert_selection'];
713
				}
714

    
715
				foreach ($config['cert'] as $ca) {
716
					if (stristr($ca['descr'], "webconf")) {
717
						continue;
718
					}
719

    
720
					$caname = htmlspecialchars($ca['descr']);
721

    
722
					if ($value == $caname) {
723
						array_push($selected, $value);
724
					}
725

    
726

    
727
					$canecho = 0;
728
					if ($field['cert_filter'] != "") {
729
						if (stristr($caname, $field['cert_filter']) == true) {
730
							$canecho = 1;
731
						}
732
					} else {
733
						$canecho = 1;
734
					}
735

    
736
					if ($canecho == 1) {
737
						$options[$ca['refid']] = $caname;
738
					}
739
				}
740

    
741
				$section->addInput(new Form_Select(
742
					$name,
743
					$etitle,
744
					($multiple) ? $selected:$selected[0],
745
					$options,
746
					$multiple
747
				))->setHelp($field['description']);
748

    
749
				break;
750
			case "select":
751
				if ($field['displayname']) {
752
					$etitle = $field['displayname'];
753
				} else if (!$field['dontdisplayname']) {
754
					$etitle =  fixup_string($field['name']);
755
				}
756

    
757
				if ($field['size']) {
758
					$size = " size='" . $field['size'] . "' ";
759
				}
760

    
761
				$multiple = ($field['multiple'] == "yes");
762

    
763
				$onchange = "";
764
				foreach ($field['options']['option'] as $opt) {
765
					if ($opt['enablefields'] != "") {
766
						$onchange = "Javascript:enableitems(this.selectedIndex);";
767
					}
768
				}
769

    
770
				$options = array();
771
				$selected = array();
772

    
773
				foreach ($field['options']['option'] as $opt) {
774
					if ($value == $opt['value']) {
775
						array_push($selected, $value);
776
					}
777

    
778
					if ($opt['displayname']) {
779
						$options[$opt['value']] = $opt['displayname'];
780
					} else {
781
						$options[$opt['value']] = $opt['name'];
782
					}
783

    
784
				}
785

    
786
				$section->addInput(new Form_Select(
787
					$name,
788
					$etitle,
789
					($multiple) ? $selected:$selected[0],
790
					$options,
791
					$multiple
792
				))->setHelp($field['description'])->setOnchange($onchange);
793

    
794
				break;
795
			case "textarea":
796
				if ($field['displayname']) {
797
					$etitle = $field['displayname'];
798
				} else if (!$field['dontdisplayname']) {
799
					$etitle =  fixup_string($field['name']);
800
				}
801

    
802
				$section->addInput(new Form_Textarea(
803
					$name,
804
					$etitle,
805
					$value
806
				))->setHelp($field['description'])
807
				  ->setAttribute('rows', $field['rows'])
808
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")":"");
809

    
810
				break;
811
			case "submit":
812
				$form->addGlobal(new Form_Button(
813
					$name,
814
					$field['name']
815
				));
816

    
817
				break;
818
			case "listtopic":
819
				$form->add($section);
820
				$section = new Form_Section($field['name']);
821

    
822
				break;
823
			case "subnet_select":
824
				if ($field['displayname']) {
825
					$etitle = $field['displayname'];
826
				} else /* if (!$field['dontdisplayname']) */ {
827
					$etitle =  fixup_string($field['name']);
828
				}
829

    
830
				$section->addInput(new Form_Select(
831
					$name,
832
					$etitle,
833
					$value,
834
					array_combine(range(32, 1, -1), range(32, 1, -1))
835
				))->setHelp($field['description']);
836

    
837
				break;
838
			case "timezone_select":
839
				$timezonelist = system_get_timezone_list();
840

    
841
				/* kill carriage returns */
842
				for ($x = 0; $x < count($timezonelist); $x++) {
843
					$timezonelist[$x] = str_replace("\n", "", $timezonelist[$x]);
844
				}
845

    
846
				if ($field['displayname']) {
847
					$etitle = $field['displayname'];
848
				} else if (!$field['dontdisplayname']) {
849
					$etitle =  fixup_string($field['name']);
850
				}
851

    
852
				if (!$field['dontcombinecells']) {
853
					//echo "<td class=\"vtable\">";
854
				}
855

    
856
				$section->addInput(new Form_Select(
857
					$name,
858
					$etitle,
859
					($value == "") ? $g['default_timezone'] : $value,
860
					array_combine($timezonelist, $timezonelist)
861
				))->setHelp($field['description']);
862

    
863
				break;
864
			case "checkbox":
865
				if ($field['displayname']) {
866
					$etitle = $field['displayname'];
867

    
868
				} else if (!$field['dontdisplayname']) {
869
					$etitle =  fixup_string($field['name']);
870
				}
871

    
872
				if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
873
					$onclick = "enablechange()";
874
				} else if (isset($field['disablefields']) or isset($field['checkdisablefields'])) {
875
					$onclick = "disablechange()";
876
				}
877

    
878
				$section->addInput(new Form_Checkbox(
879
					$name,
880
					$etitle,
881
					$field['typehint'],
882
					($value != ""),
883
					'on'
884
				))->setHelp($field['description'])
885
				  ->setOnclick($onclick);
886

    
887
				break;
888
		} // e-o-switch
889
	} // e-o-foreach(package)
890
} // e-o- if(we have fields)
891

    
892
$form->add($section);
893
print($form);
894
?>
895

    
896
<script type="text/javascript">
897
//<![CDATA[
898

    
899
		if (typeof ext_change != 'undefined') {
900
			ext_change();
901
		}
902
		if (typeof proto_change != 'undefined') {
903
			ext_change();
904
		}
905
		if (typeof proto_change != 'undefined') {
906
			proto_change();
907
		}
908

    
909
	<?php
910
		$isfirst = 0;
911
		$aliases = "";
912
		$addrisfirst = 0;
913
		$aliasesaddr = "";
914
		if ($config['aliases']['alias'] != "" and is_array($config['aliases']['alias'])) {
915
			foreach ($config['aliases']['alias'] as $alias_name) {
916
				if ($isfirst == 1) {
917
					$aliases .= ",";
918
				}
919
				$aliases .= "'" . $alias_name['name'] . "'";
920
				$isfirst = 1;
921
			}
922
		}
923
	?>
924

    
925
		var customarray=new Array(<?=$aliases; ?>);
926

    
927
		window.onload = function () {
928

    
929
<?php
930
		$counter = 0;
931
		foreach ($inputaliases as $alias) {
932
?>
933
			$('#' + '<?=$alias;?>').autocomplete({
934
				source: customarray
935
			});
936
<?php
937
		}
938
?>
939
	}
940

    
941
//]]>
942
</script>
943

    
944
<?php
945

    
946
$fieldnames_array = Array();
947
if ($pkg['step'][$stepid]['disableallfieldsbydefault'] != "") {
948
	// create a fieldname loop that can be used with javascript
949
	// hide and enable features.
950
	echo "\n<script type=\"text/javascript\">\n";
951
	echo "//<![CDATA[\n";
952
	echo "function disableall() {\n";
953
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
954
		if ($field['type'] != "submit" and $field['type'] != "listtopic") {
955
			if (!$field['donotdisable'] != "") {
956
				array_push($fieldnames_array, $field['name']);
957
				$fieldname = preg_replace("/\s+/", "", $field['name']);
958
				$fieldname = strtolower($fieldname);
959
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
960
			}
961
		}
962
	}
963
	echo "}\ndisableall();\n";
964
	echo "function enableitems(selectedindex) {\n";
965
	echo "disableall();\n";
966
	$idcounter = 0;
967
	if ($pkg['step'][$stepid]['fields']['field'] != "") {
968
		echo "\tswitch (selectedindex) {\n";
969
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
970
			if ($field['options']['option'] != "") {
971
				foreach ($field['options']['option'] as $opt) {
972
					if ($opt['enablefields'] != "") {
973
						echo "\t\tcase " . $idcounter . ":\n";
974
						$enablefields_split = explode(",", $opt['enablefields']);
975
						foreach ($enablefields_split as $efs) {
976
							$fieldname = preg_replace("/\s+/", "", $efs);
977
							$fieldname = strtolower($fieldname);
978
							if ($fieldname != "") {
979
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
980
								echo $onchange;
981
							}
982
						}
983
						echo "\t\t\tbreak;\n";
984
					}
985
					$idcounter = $idcounter + 1;
986
				}
987
			}
988
		}
989
		echo "\t}\n";
990
	}
991
	echo "}\n";
992
	echo "//]]>\n";
993
	echo "</script>\n\n";
994
}
995
?>
996

    
997
<script type="text/javascript">
998
//<![CDATA[
999
events.push(function() {
1000
	enablechange();
1001
	disablechange();
1002
	showchange();
1003
});
1004
//]]>
1005
</script>
1006

    
1007
<?php
1008
if ($pkg['step'][$stepid]['stepafterformdisplay'] != "") {
1009
	// handle after form display event.
1010
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
1011
}
1012

    
1013
if ($pkg['step'][$stepid]['javascriptafterformdisplay'] != "") {
1014
	// handle after form display event.
1015
	echo "\n<script type=\"text/javascript\">\n";
1016
	echo "//<![CDATA[\n";
1017
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
1018
	echo "//]]>\n";
1019
	echo "</script>\n\n";
1020
}
1021

    
1022
include("foot.inc");
(228-228/229)