Project

General

Profile

Download (29.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * wizard.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-pfsensewizardsubsystem
26
##|*NAME=pfSense wizard subsystem
27
##|*DESCR=Allow access to the 'pfSense wizard subsystem' page.
28
##|*MATCH=wizard.php*
29
##|-PRIV
30

    
31
require_once("globals.inc");
32
require_once("guiconfig.inc");
33
require_once("functions.inc");
34
require_once("filter.inc");
35
require_once("shaper.inc");
36
require_once("rrd.inc");
37
require_once("system.inc");
38

    
39
// This causes the step #, field type and field name to be printed at the top of the page
40
define('DEBUG', false);
41

    
42
global $g;
43

    
44
$stepid = htmlspecialchars($_REQUEST['stepid']);
45

    
46

    
47
if (!$stepid) {
48
	$stepid = "0";
49
}
50

    
51
$xml = htmlspecialchars($_REQUEST['xml']);
52

    
53
if (empty($xml)) {
54
	$xml = "not_defined";
55
	print_info_box(sprintf(gettext("Could not open %s."), $xml), 'danger');
56
	die;
57
} else {
58
	$wizard_xml_prefix = "{$g['www_path']}/wizards";
59
	$wizard_full_path = "{$wizard_xml_prefix}/{$xml}";
60
	if (substr_compare(realpath($wizard_full_path), $wizard_xml_prefix, 0, strlen($wizard_xml_prefix))) {
61
		print_info_box(gettext("Invalid path specified."), 'danger');
62
		die;
63
	}
64
	if (file_exists($wizard_full_path)) {
65
		$pkg = parse_xml_config_pkg($wizard_full_path, "pfsensewizard");
66
	} else {
67
		print_info_box(sprintf(gettext("Could not open %s."), $xml), 'danger');
68
		die;
69
	}
70
}
71

    
72
if (!is_array($pkg)) {
73
	print_info_box(sprintf(gettext('Could not parse %1$s/wizards/%2$s file.'), $g['www_path'], $xml), 'danger');
74
	die;
75
}
76

    
77
$totalsteps = $pkg['totalsteps'];
78

    
79
if ($pkg['includefile']) {
80
	require_once($pkg['includefile']);
81
}
82

    
83
if ($pkg['step'][$stepid]['includefile']) {
84
	require_once($pkg['step'][$stepid]['includefile']);
85
}
86

    
87
if ($pkg['step'][$stepid]['stepsubmitbeforesave']) {
88
	eval($pkg['step'][$stepid]['stepsubmitbeforesave']);
89
}
90

    
91
if ($_POST && !$input_errors) {
92
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
93
		if (!empty($field['bindstofield']) and $field['type'] != "submit") {
94
			$fieldname = $field['name'];
95
			$fieldname = str_replace(" ", "", $fieldname);
96
			$fieldname = strtolower($fieldname);
97
			// update field with posted values.
98
			if ($field['unsetfield'] != "") {
99
				$unset_fields = "yes";
100
			} else {
101
				$unset_fields = "";
102
			}
103

    
104
			if ($field['arraynum'] != "") {
105
				$arraynum = $field['arraynum'];
106
			} else {
107
				$arraynum = "";
108
			}
109

    
110
			update_config_field($field['bindstofield'], $_POST[$fieldname], $unset_fields, $arraynum, $field['type']);
111
		}
112

    
113
	}
114
	// run custom php code embedded in xml config.
115
	if ($pkg['step'][$stepid]['stepsubmitphpaction'] != "") {
116
		eval($pkg['step'][$stepid]['stepsubmitphpaction']);
117
	}
118
	if (!$input_errors) {
119
		write_config(gettext("Configuration changed via the wizard subsystem."));
120
	}
121

    
122
	$stepid++;
123
}
124

    
125
while (!empty($pkg['step'][$stepid]['skip_flavors'])) {
126
	$skip = false;
127
	foreach (explode(',', $pkg['step'][$stepid]['skip_flavors']) as $flavor) {
128
		if ($flavor == $g['default-config-flavor']) {
129
			$skip = true;
130
			break;
131
		}
132
	}
133
	if ($skip) {
134
		$stepid++;
135
	} else {
136
		break;
137
	}
138
}
139

    
140
if ($stepid > $totalsteps) {
141
	$stepid = $totalsteps;
142
}
143

    
144
// Convert a string containing a text version of a PHP array into a real $config array
145
// that can then be created. e.g.: config_array_from_str("['apple']['orange']['pear']['banana']");
146
function config_array_from_str( $text) {
147
	$t = str_replace("[", "", $text);	// Remove '['
148
	$t = str_replace("'", "", $t);		// Remove '
149
	$t = str_replace("\"", "", $t);		// Remove "
150
	$t = str_replace("]", " ", $t);		// Convert ] to space
151
	$a = explode(" ", trim($t));
152
	init_config_arr($a);
153
}
154

    
155
function update_config_field($field, $updatetext, $unset, $arraynum, $field_type) {
156
	global $config;
157
	$field_split = explode("->", $field);
158
	$thisvar = null;
159
	foreach ($field_split as $f) {
160
		$field_conv .= "['" . $f . "']";
161
	}
162
	if ($field_conv == "") {
163
		return;
164
	}
165
	if ($arraynum != "") {
166
		$field_conv .= "[" . $arraynum . "]";
167
	}
168
	if (($field_type == "checkbox" and $updatetext != "on") || $updatetext == "") {
169
		/*
170
		 * item is a checkbox, it should have the value "on"
171
		 * if it was checked
172
		 */
173
		$var = "\$config{$field_conv}";
174
		$text = "if (isset({$var})) unset({$var});";
175
		eval($text);
176
		return;
177
	}
178

    
179
	if ($field_type == "interfaces_selection") {
180
		$var = "\$config{$field_conv}";
181
		$text = "if (isset({$var})) unset({$var});";
182
		$text .= "\$thisvar = &\$config" . $field_conv . ";";
183
		eval($text);
184
		$thisvar = $updatetext;
185
		return;
186
	}
187

    
188
	if ($field_type == "select") {
189
		if (is_array($updatetext)) {
190
			$updatetext = implode(',', $updatetext);
191
		}
192
	}
193

    
194
	if ($unset == "yes") {
195
		$text = "unset(\$config" . $field_conv . ");";
196
		eval($text);
197
	}
198

    
199
	// Verify that the needed $config element exists. If not, create it
200
	$tsttext = 'return (isset($config' . $field_conv . '));';
201

    
202
	if (!eval($tsttext)) {
203
		config_array_from_str($field_conv);
204
	}
205

    
206
	$text .= "\$thisvar = &\$config" . $field_conv . ";";
207
	eval($text);
208

    
209
	$thisvar = $updatetext;
210
}
211

    
212
$title	   = $pkg['step'][$stepid]['title'];
213
$description = $pkg['step'][$stepid]['description'];
214

    
215
// handle before form display event.
216
do {
217
	$oldstepid = $stepid;
218
	if ($pkg['step'][$stepid]['stepbeforeformdisplay'] != "") {
219
		eval($pkg['step'][$stepid]['stepbeforeformdisplay']);
220
	}
221
} while ($oldstepid != $stepid);
222

    
223
$pgtitle = array(gettext("Wizard"), gettext($pkg['step'][0]['title']));	//First step is main title of the wizard in the breadcrumb
224
$pglinks = array("", "wizard.php?xml=" . $xml);
225
$pgtitle[] = ($stepid > 0 ? gettext($pkg['step'][$stepid]['title']):'&nbsp;');		//Following steps are sub-level breadcrumbs.
226
$pglinks[] = ($stepid > 0 ? "wizard.php?xml=" . $xml . "&stepid=" . $stepid:'&nbsp;');
227
$shortcut_section = "Wizard";
228
include("head.inc");
229

    
230
if ($pkg['step'][$stepid]['fields']['field'] != "") { ?>
231
<script type="text/javascript">
232
//<![CDATA[
233

    
234

    
235
	function FieldValidate(userinput, regexp, message) {
236
		if (!userinput.match(regexp)) {
237
			alert(message);
238
		}
239
	}
240

    
241
	function enablechange() {
242

    
243
	<?php
244

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

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

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

    
265
				print "\t" . '} else {' . "\n";
266
				if (isset($field['enablefields'])) {
267
					$enablefields = explode(',', $field['enablefields']);
268
					foreach ($enablefields as $enablefield) {
269
						$enablefield = strtolower($enablefield);
270
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", true);' . "\n";
271

    
272
					}
273
				}
274

    
275
			if (isset($field['checkdisablefields'])) {
276
				$checkenablefields = explode(',', $field['checkdisablefields']);
277
				foreach ($checkenablefields as $checkenablefield) {
278
					$checkenablefield = strtolower($checkenablefield);
279
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", false);' . "\n";
280
					}
281
				}
282

    
283
				print "\t" . '}' . "\n";
284
			}
285
		}
286
	?>
287

    
288
	}
289

    
290
	function disablechange() {
291
	<?php
292
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
293
			if (isset($field['disablefields']) or isset($field['checkdisablefields'])) {
294

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

    
297
				if (isset($field['disablefields'])) {
298
					$enablefields = explode(',', $field['disablefields']);
299
					foreach ($enablefields as $enablefield) {
300
						$enablefield = strtolower($enablefield);
301

    
302
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", true);' . "\n";
303
					}
304
				}
305
				if (isset($field['checkdisablefields'])) {
306
					$checkenablefields = explode(',', $field['checkdisablefields']);
307
					foreach ($checkenablefields as $checkenablefield) {
308
						$checkenablefield = strtolower($checkenablefield);
309
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", true);' . "\n";
310
					}
311
				}
312
				print "\t" . '} else {' . "\n";
313
				if (isset($field['disablefields'])) {
314
					$enablefields = explode(',', $field['disablefields']);
315
					foreach ($enablefields as $enablefield) {
316
						$enablefield = strtolower($enablefield);
317
						print "\t\t" . '$("#" + "' . $enablefield . '").prop("disabled", false);' . "\n";
318
					}
319
				}
320
				if (isset($field['checkdisablefields'])) {
321
					$checkenablefields = explode(',', $field['checkdisablefields']);
322
					foreach ($checkenablefields as $checkenablefield) {
323
						$checkenablefield = strtolower($checkenablefield);
324
						print "\t\t" . '$("#" + "' . $checkenablefield . '").prop("checked", false);' . "\n";
325
					}
326
				}
327
				print "\t" . '}' . "\n";
328
			}
329
		}
330
	?>
331
	}
332

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

    
361
//]]>
362
</script>
363
<?php }
364

    
365
function fixup_string($string) {
366
	global $g, $myurl, $title;
367
	$newstring = $string;
368
	// fixup #1: $myurl -> http[s]://ip_address:port/
369
	switch (config_get_path('system/webgui/protocol')) {
370
		case "http":
371
			$proto = "http";
372
			break;
373
		case "https":
374
			$proto = "https";
375
			break;
376
		default:
377
			$proto = "http";
378
			break;
379
	}
380
	$port = config_get_path('system/webgui/port', "");
381
	if ($port != "") {
382
		if (($port == "443" and $proto != "https") or ($port == "80" and $proto != "http")) {
383
			$urlport = ":" . $port;
384
		} elseif ($port != "80" and $port != "443") {
385
			$urlport = ":" . $port;
386
		} else {
387
			$urlport = "";
388
		}
389
	}
390

    
391
	$http_host = $_SERVER['HTTP_HOST'];
392
	$urlhost = $http_host;
393
	$hostname = config_get_path('system/hostname');
394
	$fqdn = $hostname . '.' . config_get_path('system/domain');
395
	$wizard_hostname = config_get_path('wizardtemp/system/hostname');
396
	$wizard_fqdn = $wizard_hostname . '.' . config_get_path('wizardtempsystem/domain');
397
	// If finishing the setup wizard, check if accessing on a LAN or WAN address that changed
398
	if ($title == "Reload in progress") {
399
		if (is_ipaddr($urlhost)) {
400
			$host_if = find_ip_interface($urlhost);
401
			if ($host_if) {
402
				$host_if = convert_real_interface_to_friendly_interface_name($host_if);
403
				$host_if_ip = config_get_path("interfaces/{$host_if}/ipaddr");
404
				if ($host_if && is_ipaddr($host_if_ip)) {
405
					$urlhost = $host_if_ip;
406
				}
407
			}
408
		} else if ($urlhost == $hostname) {
409
			$urlhost = $wizard_hostname;
410
		} else if ($urlhost == $fqdn)  {
411
			$urlhost = $wizard_fqdn;
412
		}
413
	}
414

    
415
	if ($urlhost != $http_host) {
416
		file_put_contents("{$g['tmp_path']}/setupwizard_lastreferrer", $proto . "://" . $http_host . $urlport . $_SERVER['REQUEST_URI']);
417
	}
418

    
419
	$myurl = $proto . "://" . $urlhost . $urlport . "/";
420

    
421
	if (strstr($newstring, "\$myurl")) {
422
		$newstring = str_replace("\$myurl", $myurl, $newstring);
423
	}
424
	// fixup #2: $wanip
425
	if (strstr($newstring, "\$wanip")) {
426
		$curwanip = get_interface_ip();
427
		$newstring = str_replace("\$wanip", $curwanip, $newstring);
428
	}
429
	// fixup #3: $lanip
430
	if (strstr($newstring, "\$lanip")) {
431
		$lanip = get_interface_ip("lan");
432
		$newstring = str_replace("\$lanip", $lanip, $newstring);
433
	}
434
	// fixup #4: fix'r'up here.
435
	return $newstring;
436
}
437

    
438
function is_timezone($elt) {
439
	return !preg_match("/\/$/", $elt);
440
}
441

    
442
if ($title == "Reload in progress") {
443
	$ip = fixup_string("\$myurl");
444
} else {
445
	$ip = "/";
446
}
447

    
448
if ($input_errors) {
449
	print_input_errors($input_errors);
450
}
451
if ($savemsg) {
452
	print_info_box($savemsg, 'success');
453
}
454
if ($_REQUEST['message'] != "") {
455
	print_info_box(htmlspecialchars($_REQUEST['message']));
456
}
457

    
458
$completion = ($stepid == 0) ? 0:($stepid * 100) / ($totalsteps -1);
459
$pbclass = ($completion == 100) ? "progress-bar progress-bar-success":"progress-bar progress-bar-danger";
460
?>
461

    
462
<!-- Draw a progress bar to show step progress -->
463
<div class="progress">
464
	<div class="<?=$pbclass?>" role="progressbar" aria-valuenow="<?=$completion?>" aria-valuemin="0" aria-valuemax="100" style="width:<?=$completion?>%; line-height: 15px;">
465
		<?php print(sprintf(gettext("Step %s of %s"), $stepid, $totalsteps-1)); ?>
466
	</div>
467
</div>
468
<br />
469

    
470
<?php
471

    
472
$form = new Form(false);
473

    
474
$form->addGlobal(new Form_Input(
475
	'stepid',
476
	null,
477
	'hidden',
478
	$stepid
479
));
480

    
481
$form->addGlobal(new Form_Input(
482
	'xml',
483
	null,
484
	'hidden',
485
	$xml
486
));
487

    
488
$section = new Form_Section(fixup_string($title));
489

    
490
if ($description) {
491
	$section->addInput(new Form_StaticText(
492
		null,
493
		fixup_string($description)
494
	));
495
}
496

    
497
$inputaliases = array();
498
if ($pkg['step'][$stepid]['fields']['field'] != "") {
499
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
500

    
501
		$value = $field['value'];
502
		$name  = $field['name'];
503

    
504
		$name = preg_replace("/\s+/", "", $name);
505
		$name = strtolower($name);
506

    
507
		if ($field['bindstofield'] != "") {
508
			$arraynum = "";
509
			$field_conv = "";
510
			$field_split = explode("->", $field['bindstofield']);
511
			// arraynum is used in cases where there is an array of the same field
512
			// name such as dnsserver (2 of them)
513
			if ($field['arraynum'] != "") {
514
				$arraynum = "[" . $field['arraynum'] . "]";
515
			}
516

    
517
			foreach ($field_split as $f) {
518
				$field_conv .= "['" . $f . "']";
519
			}
520

    
521
			if ($field['type'] == "checkbox") {
522
				$toeval = "if (isset(\$config" . $field_conv . $arraynum . ")) { \$value = \$config" . $field_conv . $arraynum . "; if (empty(\$value)) \$value = true; }";
523
			} else {
524
				$toeval = "if (isset(\$config" . $field_conv . $arraynum . ")) \$value = \$config" . $field_conv . $arraynum . ";";
525
			}
526

    
527
			eval($toeval);
528
		}
529

    
530

    
531
		if (DEBUG) {
532
			print('Step: ' . $pkg['step'][$stepid]['id'] . ', Field: ' . $field['type'] . ', Name: ' . $name . '<br />');
533
		}
534

    
535
		switch ($field['type']) {
536
			case "input":
537
				if ($field['displayname']) {
538
					$etitle = $field['displayname'];
539

    
540
				} else if (!$field['dontdisplayname']) {
541
					$etitle =  fixup_string($field['name']);
542
				}
543

    
544
				$section->addInput(new Form_Input(
545
					$name,
546
					$etitle,
547
					'text',
548
					$value
549
				))->setHelp($field['description'])
550
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")":"");
551

    
552
				break;
553
			case "text":
554
				$section->addInput(new Form_StaticText(
555
					null,
556
					$field['description']
557
				));
558

    
559
				break;
560
			case "inputalias":
561
				if ($field['displayname']) {
562
					$etitle = $field['displayname'];
563

    
564
				} else if (!$field['dontdisplayname']) {
565
					$etitle =  fixup_string($field['name']);
566
				}
567

    
568
				$onchange = "";
569

    
570
				if ($field['validate']) {
571
					$onchange="FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")";
572
				}
573

    
574
				$section->addInput(new Form_Input(
575
					$name,
576
					$etitle,
577
					'text',
578
					$value
579
				))->setAttribute('autocomplete', 'off')
580
				  ->setOnchange($onchange)
581
				  ->setHelp($field['description']);
582

    
583
				break;
584
			case "interfaces_selection":
585
			case "interface_select":
586

    
587
				$name = strtolower($name);
588
				$options = array();
589
				$selected = array();
590

    
591
				$etitle = (fixup_string($field['displayname'])) ? $field['displayname'] : $field['name'];
592

    
593
				if (($field['multiple'] != "") && ($field['multiple'] != "0")) {
594
					$multiple = true;
595
				} else {
596
					$multiple = false;
597
				}
598

    
599
				if ($field['add_to_interfaces_selection'] != "") {
600
					if ($field['add_to_interfaces_selection'] == $value) {
601
						array_push($selected, $value);
602
					}
603

    
604
					$options[$field['add_to_interfaces_selection']] = $field['add_to_interfaces_selection'];
605
				}
606

    
607
				if ($field['type'] == "interface_select") {
608
					$interfaces = get_interface_list();
609
				} else {
610
					$interfaces = get_configured_interface_with_descr();
611
				}
612

    
613
				foreach ($interfaces as $ifname => $iface) {
614
					if ($field['type'] == "interface_select") {
615
						$iface = $ifname;
616
						if ($iface['mac']) {
617
							$iface .= " ({$iface['mac']})";
618
						}
619
					}
620

    
621
					if ($value == $ifname) {
622
						array_push($selected, $value);
623
					}
624

    
625
					$canecho = 0;
626
					if ($field['interface_filter'] != "") {
627
						if (stristr($ifname, $field['interface_filter']) == true) {
628
							$canecho = 1;
629
						}
630
					} else {
631
						$canecho = 1;
632
					}
633

    
634
					if ($canecho == 1) {
635
						$options[$ifname] = $iface;
636
					}
637
				}
638

    
639
				$section->addInput(new Form_Select(
640
					$name,
641
					$etitle,
642
					($multiple) ? $selected:$selected[0],
643
					$options,
644
					$multiple
645
				))->setHelp($field['description']);
646

    
647
				break;
648
			case "password":
649
				if ($field['displayname']) {
650
					$etitle = $field['displayname'];
651
				} else if (!$field['dontdisplayname']) {
652
					$etitle =  fixup_string($field['name']);
653
				}
654

    
655
				$section->addInput(new Form_Input(
656
					$name,
657
					$etitle,
658
					'password',
659
					$value
660
				))->setHelp($field['description'])
661
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] ."\")":"");
662

    
663
				break;
664
			case "certca_selection":
665
				$options = array();
666
				$selected = "";
667

    
668
				$name = strtolower($name);
669

    
670
				$etitle = (fixup_string($field['displayname']) ? $field['displayname'] : $field['name']);
671

    
672
				if ($field['add_to_certca_selection'] != "") {
673
					if ($field['add_to_certca_selection'] == $value) {
674
						$selected = $value;
675
					}
676

    
677
					$options[$field['add_to_certca_selection']] = $field['add_to_certca_selection'];
678
				}
679

    
680
				if (!is_array($config['ca'])) {
681
					$config['ca'] = array();
682
				}
683

    
684
				foreach ($config['ca'] as $ca) {
685
					$caname = htmlspecialchars($ca['descr']);
686

    
687
					if ($value == $caname) {
688
						$selected = $value;
689
					}
690

    
691
					$canecho = 0;
692
					if ($field['certca_filter'] != "") {
693
						if (stristr($caname, $field['certca_filter']) == true) {
694
							$canecho = 1;
695
						}
696
					} else {
697
						$canecho = 1;
698
					}
699
					if ($canecho == 1) {
700
						$options[$ca['refid']] = $caname;
701
					}
702
				}
703

    
704
				$section->addInput(new Form_Select(
705
					$name,
706
					$etitle,
707
					$selected,
708
					$options
709
				))->setHelp($field['description']);
710

    
711
				break;
712
			case "cert_selection":
713
				$options = array();
714
				$selected = array();
715

    
716
				$multiple = false;
717
				$name = strtolower($name);
718

    
719
				$etitle = (fixup_string($field['displayname']) ? $field['displayname'] : $field['name']);
720

    
721
				if ($field['add_to_cert_selection'] != "") {
722
					if ($field['add_to_cert_selection'] == $value) {
723
						array_push($selected, $value);
724
					}
725

    
726
					$options[$field['add_to_cert_selection']] = $field['add_to_cert_selection'];
727
				}
728

    
729
				if (!is_array($config['cert'])) {
730
					$config['cert'] = array();
731
				}
732

    
733
				foreach ($config['cert'] as $ca) {
734
					if (stristr($ca['descr'], "webconf")) {
735
						continue;
736
					}
737

    
738
					$caname = htmlspecialchars($ca['descr']);
739

    
740
					if ($value == $caname) {
741
						array_push($selected, $value);
742
					}
743

    
744

    
745
					$canecho = 0;
746
					if ($field['cert_filter'] != "") {
747
						if (stristr($caname, $field['cert_filter']) == true) {
748
							$canecho = 1;
749
						}
750
					} else {
751
						$canecho = 1;
752
					}
753

    
754
					if ($canecho == 1) {
755
						$options[$ca['refid']] = $caname;
756
					}
757
				}
758

    
759
				$section->addInput(new Form_Select(
760
					$name,
761
					$etitle,
762
					($multiple) ? $selected:$selected[0],
763
					$options,
764
					$multiple
765
				))->setHelp($field['description']);
766

    
767
				break;
768
			case "select":
769
				if ($field['displayname']) {
770
					$etitle = $field['displayname'];
771
				} else if (!$field['dontdisplayname']) {
772
					$etitle =  fixup_string($field['name']);
773
				}
774

    
775
				if ($field['size']) {
776
					$size = " size='" . $field['size'] . "' ";
777
				}
778

    
779
				$multiple = ($field['multiple'] == "yes");
780

    
781
				if ($multiple) {
782
					$values = explode(',', $value);
783
				} else {
784
					$values = array($value);
785
				}
786

    
787
				$onchange = "";
788
				foreach ($field['options']['option'] as $opt) {
789
					if ($opt['enablefields'] != "") {
790
						$onchange = "Javascript:enableitems(this.selectedIndex);";
791
					}
792
				}
793

    
794
				$options = array();
795
				$selected = array();
796

    
797
				foreach ($field['options']['option'] as $opt) {
798
					if (in_array($opt['value'], $values)) {
799
						array_push($selected, $opt['value']);
800
					}
801

    
802
					if ($opt['displayname']) {
803
						$options[$opt['value']] = $opt['displayname'];
804
					} else {
805
						$options[$opt['value']] = $opt['name'];
806
					}
807

    
808
				}
809

    
810
				$tmpselect = new Form_Select(
811
					$name,
812
					$etitle,
813
					($multiple) ? $selected:$selected[0],
814
					$options,
815
					$multiple
816
				);
817

    
818
				$tmpselect->setHelp($field['description'])->setOnchange($onchange);
819

    
820
				if (isset($field['size'])) {
821
					$tmpselect->setAttribute('size', $field['size']);
822
				}
823

    
824
				$section->addInput($tmpselect);
825

    
826
				break;
827
			case "select_source":
828
				if ($field['displayname']) {
829
					$etitle = $field['displayname'];
830
				} else if (!$field['dontdisplayname']) {
831
					$etitle =  fixup_string($name);
832
				}
833

    
834
				if ($field['size']) {
835
					$size = " size='" . $field['size'] . "' ";
836
				}
837

    
838
				if (isset($field['multiple'])) {
839
					$items = explode(',', $value);
840
					$name .= "[]";
841
				} else {
842
					$items = array($value);
843
				}
844

    
845
				$onchange = (isset($field['onchange']) ? "{$field['onchange']}" : '');
846

    
847
				$source = $field['source'];
848
				try{
849
					@eval("\$wizard_source_txt = &$source;");
850
				} catch (\Throwable | \Error | \Exception $e) {
851
					//do nothing
852
				}
853
				#check if show disable option is present on xml
854
				if (!is_array($wizard_source_txt)) {
855
					$wizard_source_txt = array();
856
				}
857
				if (isset($field['show_disable_value'])) {
858
					array_push($wizard_source_txt,
859
						array(
860
							($field['source_name'] ? $field['source_name'] : $name) => $field['show_disable_value'],
861
							($field['source_value'] ? $field['source_value'] : $value) => $field['show_disable_value']
862
						));
863
				}
864

    
865
				$srcoptions = array();
866
				$srcselected = array();
867

    
868
				foreach ($wizard_source_txt as $opt) {
869
					$source_name = ($field['source_name'] ? $opt[$field['source_name']] : $opt[$name]);
870
					$source_value = ($field['source_value'] ? $opt[$field['source_value']] : $opt[$value]);
871
					$srcoptions[$source_value] = $source_name;
872

    
873
					if (in_array($source_value, $items)) {
874
						array_push($srcselected, $source_value);
875
					}
876
				}
877

    
878
				$descr = (isset($field['description'])) ? $field['description'] : "";
879

    
880
				$section->addInput(new Form_Select(
881
					$name,
882
					$etitle,
883
					isset($field['multiple']) ? $srcselected : $srcselected[0],
884
					$srcoptions,
885
					isset($field['multiple'])
886
				))->setHelp($descr)->setOnchange($onchange);
887

    
888
				break;
889
			case "textarea":
890
				if ($field['displayname']) {
891
					$etitle = $field['displayname'];
892
				} else if (!$field['dontdisplayname']) {
893
					$etitle =  fixup_string($field['name']);
894
				}
895

    
896
				$section->addInput(new Form_Textarea(
897
					$name,
898
					$etitle,
899
					$value
900
				))->setHelp($field['description'])
901
				  ->setAttribute('rows', $field['rows'])
902
				  ->setOnchange(($field['validate']) ? "FieldValidate(this.value, \"" . $field['validate'] . "\", \"" . $field['message'] . "\")":"");
903

    
904
				break;
905
			case "submit":
906
				$form->addGlobal(new Form_Button(
907
					$name,
908
					$field['name'],
909
					null,
910
					'fa-angle-double-right'
911
				))->addClass('btn-primary');
912

    
913
				break;
914
			case "listtopic":
915
				$form->add($section);
916
				$section = new Form_Section($field['name']);
917

    
918
				break;
919
			case "subnet_select":
920
				if ($field['displayname']) {
921
					$etitle = $field['displayname'];
922
				} else /* if (!$field['dontdisplayname']) */ {
923
					$etitle =  fixup_string($field['name']);
924
				}
925

    
926
				$section->addInput(new Form_Select(
927
					$name,
928
					$etitle,
929
					$value,
930
					array_combine(range(32, 1, -1), range(32, 1, -1))
931
				))->setHelp($field['description']);
932

    
933
				break;
934
			case "timezone_select":
935
				$timezonelist = system_get_timezone_list();
936

    
937
				/* kill carriage returns */
938
				for ($x = 0; $x < count($timezonelist); $x++) {
939
					$timezonelist[$x] = str_replace("\n", "", $timezonelist[$x]);
940
				}
941

    
942
				if ($field['displayname']) {
943
					$etitle = $field['displayname'];
944
				} else if (!$field['dontdisplayname']) {
945
					$etitle =  fixup_string($field['name']);
946
				}
947

    
948
				if (!$field['dontcombinecells']) {
949
					//echo "<td class=\"vtable\">";
950
				}
951

    
952
				$section->addInput(new Form_Select(
953
					$name,
954
					$etitle,
955
					($value == "") ? $g['default_timezone'] : $value,
956
					array_combine($timezonelist, $timezonelist)
957
				))->setHelp($field['description']);
958

    
959
				break;
960
			case "checkbox":
961
				if ($field['displayname']) {
962
					$etitle = $field['displayname'];
963

    
964
				} else if (!$field['dontdisplayname']) {
965
					$etitle =  fixup_string($field['name']);
966
				}
967

    
968
				if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
969
					$onclick = "enablechange()";
970
				} else if (isset($field['disablefields']) or isset($field['checkdisablefields'])) {
971
					$onclick = "disablechange()";
972
				}
973

    
974
				$section->addInput(new Form_Checkbox(
975
					$name,
976
					$etitle,
977
					$field['typehint'],
978
					($value != ""),
979
					'on'
980
				))->setHelp($field['description'])
981
				  ->setOnclick($onclick);
982

    
983
				break;
984
		} // e-o-switch
985
	} // e-o-foreach (package)
986
} // e-o-if (we have fields)
987

    
988
$form->add($section);
989
print($form);
990
?>
991

    
992
<script type="text/javascript">
993
//<![CDATA[
994

    
995
		if (typeof ext_change != 'undefined') {
996
			ext_change();
997
		}
998
		if (typeof proto_change != 'undefined') {
999
			ext_change();
1000
		}
1001
		if (typeof proto_change != 'undefined') {
1002
			proto_change();
1003
		}
1004

    
1005
	<?php
1006
		$isfirst = 0;
1007
		$aliases = "";
1008
		$addrisfirst = 0;
1009
		$aliasesaddr = "";
1010
		foreach (config_get_path('aliases/alias', []) as $alias_name) {
1011
			if ($isfirst == 1) {
1012
				$aliases .= ",";
1013
			}
1014
			$aliases .= "'" . $alias_name['name'] . "'";
1015
			$isfirst = 1;
1016
		}
1017
	?>
1018

    
1019
		var customarray=new Array(<?=$aliases; ?>);
1020

    
1021
		window.onload = function () {
1022

    
1023
<?php
1024
		$counter = 0;
1025
		foreach ($inputaliases as $alias) {
1026
?>
1027
			$('#' + '<?=$alias;?>').autocomplete({
1028
				source: customarray
1029
			});
1030
<?php
1031
		}
1032
?>
1033
	}
1034

    
1035
//]]>
1036
</script>
1037

    
1038
<?php
1039

    
1040
$fieldnames_array = Array();
1041
if ($pkg['step'][$stepid]['disableallfieldsbydefault'] != "") {
1042
	// create a fieldname loop that can be used with javascript
1043
	// hide and enable features.
1044
	echo "\n<script type=\"text/javascript\">\n";
1045
	echo "//<![CDATA[\n";
1046
	echo "function disableall() {\n";
1047
	foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
1048
		if ($field['type'] != "submit" and $field['type'] != "listtopic") {
1049
			if (!$field['donotdisable'] != "") {
1050
				array_push($fieldnames_array, $field['name']);
1051
				$fieldname = preg_replace("/\s+/", "", $field['name']);
1052
				$fieldname = strtolower($fieldname);
1053
				echo "\tdocument.forms[0]." . $fieldname . ".disabled = 1;\n";
1054
			}
1055
		}
1056
	}
1057
	echo "}\ndisableall();\n";
1058
	echo "function enableitems(selectedindex) {\n";
1059
	echo "disableall();\n";
1060
	$idcounter = 0;
1061
	if ($pkg['step'][$stepid]['fields']['field'] != "") {
1062
		echo "\tswitch (selectedindex) {\n";
1063
		foreach ($pkg['step'][$stepid]['fields']['field'] as $field) {
1064
			if ($field['options']['option'] != "") {
1065
				foreach ($field['options']['option'] as $opt) {
1066
					if ($opt['enablefields'] != "") {
1067
						echo "\t\tcase " . $idcounter . ":\n";
1068
						$enablefields_split = explode(",", $opt['enablefields']);
1069
						foreach ($enablefields_split as $efs) {
1070
							$fieldname = preg_replace("/\s+/", "", $efs);
1071
							$fieldname = strtolower($fieldname);
1072
							if ($fieldname != "") {
1073
								$onchange = "\t\t\tdocument.forms[0]." . $fieldname . ".disabled = 0; \n";
1074
								echo $onchange;
1075
							}
1076
						}
1077
						echo "\t\t\tbreak;\n";
1078
					}
1079
					$idcounter = $idcounter + 1;
1080
				}
1081
			}
1082
		}
1083
		echo "\t}\n";
1084
	}
1085
	echo "}\n";
1086
	echo "//]]>\n";
1087
	echo "</script>\n\n";
1088
}
1089
?>
1090

    
1091
<script type="text/javascript">
1092
//<![CDATA[
1093
events.push(function() {
1094
	enablechange();
1095
	disablechange();
1096
	showchange();
1097
});
1098
//]]>
1099
</script>
1100

    
1101
<?php
1102
if ($pkg['step'][$stepid]['stepafterformdisplay'] != "") {
1103
	// handle after form display event.
1104
	eval($pkg['step'][$stepid]['stepafterformdisplay']);
1105
}
1106

    
1107
if ($pkg['step'][$stepid]['javascriptafterformdisplay'] != "") {
1108
	// handle after form display event.
1109
	echo "\n<script type=\"text/javascript\">\n";
1110
	echo "//<![CDATA[\n";
1111
	echo $pkg['step'][$stepid]['javascriptafterformdisplay'] . "\n";
1112
	echo "//]]>\n";
1113
	echo "</script>\n\n";
1114
}
1115

    
1116
include("foot.inc");
(227-227/228)