Project

General

Profile

Download (42.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * pkg_edit.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-2024 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-package-edit
26
##|*NAME=Package: Edit
27
##|*DESCR=Allow access to the 'Package: Edit' page.
28
##|*MATCH=pkg_edit.php*
29
##|-PRIV
30

    
31
ini_set('max_execution_time', '0');
32

    
33
require_once("filter.inc");
34
require_once("functions.inc");
35
require_once("guiconfig.inc");
36
require_once("shaper.inc");
37
require_once("pkg-utils.inc");
38
require_once("pfsense-utils.inc");
39
require_once("util.inc");
40

    
41
$xml = htmlspecialchars($_REQUEST['xml']);
42

    
43
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
44

    
45
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
46
	$pgtitle = array(gettext("Package"), gettext("Editor"));
47
	$pglinks = array("", "@self");
48
	include("head.inc");
49
	print_info_box(gettext("No valid package defined."), 'danger', false);
50
	include("foot.inc");
51
	die;
52
} else {
53
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
54
}
55

    
56
if ($pkg['include_file'] != "") {
57
	require_once($pkg['include_file']);
58
}
59

    
60
if (!isset($pkg['adddeleteeditpagefields'])) {
61
	$only_edit = true;
62
} else {
63
	$only_edit = false;
64
}
65

    
66
$id = $_REQUEST['id'];
67
if (isset($_POST['id'])) {
68
	$id = htmlspecialchars($_POST['id']);
69
}
70

    
71
// Not posting?	 Then user is editing a record. There must be a valid id
72
// when editing a record.
73
if (!$id && !$_POST) {
74
	$id = "0";
75
}
76

    
77
if (!is_numeric($id)) {
78
	header("Location: /");
79
	exit;
80
}
81

    
82
if ($pkg['custom_php_global_functions'] != "") {
83
	eval($pkg['custom_php_global_functions']);
84
}
85

    
86
// grab the installedpackages->package_name section.
87
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])])) {
88
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])] = array();
89
}
90

    
91
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
92
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
93
}
94

    
95
/* If the first entry in the array is an empty <config/> tag, kill it.
96
 * See the following tickets for more:
97
 *  https://redmine.pfsense.org/issues/7624
98
 *  https://redmine.pfsense.org/issues/476
99
 */
100

    
101
init_config_arr(array('installedpackages', xml_safe_fieldname($pkg['name']), 'config'));
102
if ((count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
103
    (empty($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0])) &&
104
    is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
105
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
106
}
107

    
108
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
109

    
110
if ($_REQUEST['savemsg'] != "") {
111
	$savemsg = htmlspecialchars($_REQUEST['savemsg']);
112
}
113

    
114
if ($pkg['custom_php_command_before_form'] != "") {
115
	eval($pkg['custom_php_command_before_form']);
116
}
117

    
118
if ($_POST) {
119
	$rows = 0;
120

    
121
	$input_errors = array();
122
	$reqfields = array();
123
	$reqfieldsn = array();
124
	foreach ($pkg['fields']['field'] as $field) {
125
		if (isset($field['required'])) {
126
			if ($field['fieldname']) {
127
				$reqfields[] = $field['fieldname'];
128
			}
129
			if ($field['fielddescr']) {
130
				$reqfieldsn[] = $field['fielddescr'];
131
			}
132
		}
133
	}
134
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
135

    
136
	if ($pkg['custom_php_validation_command']) {
137
		eval($pkg['custom_php_validation_command']);
138
	}
139

    
140
	if ($_POST['act'] == "del") {
141
		if ($pkg['custom_delete_php_command']) {
142
			if ($pkg['custom_php_command_before_form'] != "") {
143
				eval($pkg['custom_php_command_before_form']);
144
			}
145
			eval($pkg['custom_delete_php_command']);
146
		}
147
		write_config($pkg['delete_string']);
148
		// resync the configuration file code if defined.
149
		if ($pkg['custom_php_resync_config_command'] != "") {
150
			if ($pkg['custom_php_command_before_form'] != "") {
151
				eval($pkg['custom_php_command_before_form']);
152
			}
153
			eval($pkg['custom_php_resync_config_command']);
154
		}
155
	} else {
156
		if (!$input_errors && $pkg['custom_add_php_command']) {
157
			if ($pkg['donotsave'] != "" or $pkg['preoutput'] != "") {
158
				include("head.inc");
159
			}
160

    
161
			if ($pkg['preoutput']) {
162
				echo "<pre>";
163
			}
164
			eval($pkg['custom_add_php_command']);
165
			if ($pkg['preoutput']) {
166
				echo "</pre>";
167
			}
168
		}
169
	}
170

    
171
	if (empty($pkg['donotsave'])) {
172

    
173
		// store values in xml configuration file.
174
		if (!$input_errors) {
175
			$pkgarr = array();
176
			foreach ($pkg['fields']['field'] as $fields) {
177
				switch ($fields['type']) {
178
					case "rowhelper":
179
						// save rowhelper items.
180
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
181
						$rowhelpername="row";
182
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
183
							foreach ($_POST as $key => $value) {
184
								$matches = array();
185
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
186
									if ($rowhelperfield['type'] == "textarea") {
187
										$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = unixnewlines($value);
188
									} else {
189
										$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = $value;
190
									}
191
								}
192
							}
193
						}
194
						break;
195
					case "textarea":
196
						$fieldname = $fields['fieldname'];
197
						$fieldvalue = unixnewlines(trim($_POST[$fieldname]));
198
						if ($fields['encoding'] == 'base64') {
199
							$fieldvalue = base64_encode($fieldvalue);
200
						}
201
						if ($fieldname) {
202
							$pkgarr[$fieldname] = $fieldvalue;
203
						}
204
					default:
205
						$fieldname = $fields['fieldname'];
206
						if ($fieldname == "interface_array") {
207
							$fieldvalue = $_POST[$fieldname];
208
						} elseif (is_array($_POST[$fieldname])) {
209
							$fieldvalue = implode(',', $_POST[$fieldname]);
210
						} else {
211
							$fieldvalue = trim($_POST[$fieldname]);
212
							if ($fields['encoding'] == 'base64') {
213
								$fieldvalue = base64_encode($fieldvalue);
214
							}
215
						}
216
						if ($fieldname) {
217
							$pkgarr[$fieldname] = $fieldvalue;
218
						}
219
					}
220
			}
221

    
222
			/* If the user supplied an ID and it exists, or if id=0
223
			 * and the settings are invalid, overwrite.
224
			 * See https://redmine.pfsense.org/issues/7624
225
			 */
226
			if (isset($id) && ($a_pkg[$id] ||
227
			   (($id == 0) && !is_array($a_pkg[$id])) )) {
228
				$a_pkg[$id] = $pkgarr;
229
			} else {
230
				$a_pkg[] = $pkgarr;
231
			}
232

    
233
			write_config($pkg['addedit_string']);
234
			// late running code
235
			if ($pkg['custom_add_php_command_late'] != "") {
236
				eval($pkg['custom_add_php_command_late']);
237
			}
238

    
239
			if (isset($pkg['filter_rules_needed'])) {
240
				filter_configure();
241
			}
242

    
243
			// resync the configuration file code if defined.
244
			if ($pkg['custom_php_resync_config_command'] != "") {
245
				eval($pkg['custom_php_resync_config_command']);
246
			}
247

    
248
			parse_package_templates();
249

    
250
			/* if start_command is defined, restart w/ this */
251
			if ($pkg['start_command'] != "") {
252
				exec($pkg['start_command'] . ">/dev/null 2&>1");
253
			}
254

    
255
			/* if restart_command is defined, restart w/ this */
256
			if ($pkg['restart_command'] != "") {
257
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
258
			}
259

    
260
			if ($pkg['aftersaveredirect'] != "") {
261
				pfSenseHeader($pkg['aftersaveredirect']);
262
			} elseif (!$pkg['adddeleteeditpagefields']) {
263
				pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
264
			} elseif (!$pkg['preoutput']) {
265
				pfSenseHeader("pkg.php?xml=" . $xml);
266
			}
267
			exit;
268
		} else {
269
			$get_from_post = true;
270
		}
271
	} elseif (!$input_errors) {
272
		// donotsave is enabled.  lets simply exit.
273
		exit;
274
	}
275
}
276

    
277

    
278
// Turn an embedded table into a bootstrap class table. This is for backward compatibility.
279
// We remove any table attributes in the XML and replace them with Bootstrap table classes
280
function bootstrapTable($text) {
281
	$t = strpos($text, '<table') + strlen('<table');	// Find the <table tag
282
	$c = strpos($text, '>', $t);						// And its closing bracket
283

    
284
	// Substitute everything inbetween with our new classes
285
	if ($t && $c && (($c - $t) < 200)) {
286
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
287
	}
288
}
289

    
290
/*
291
 * ROW helper function. Creates one element in the row from a PHP table by adding
292
 * the specified element to $group
293
 */
294
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description, $ewidth = null) {
295
	global $text, $group, $config;
296

    
297
	switch ($type) {
298
		case "input":
299
			$inpt = new Form_Input(
300
				$fieldname . $trc,
301
				null,
302
				'text',
303
				$value
304
			);
305

    
306
			$inpt->setHelp($description);
307

    
308
			if ($ewidth) {
309
				$inpt->setWidth($ewidth);
310
			}
311

    
312
			$group->add($inpt);
313
			break;
314
		case "checkbox":
315
			$group->add(new Form_Checkbox(
316
				$fieldname . $trc,
317
				null,
318
				null,
319
				$value,
320
				'ON'
321
			))->setHelp($description);
322

    
323
			break;
324
		case "password":
325
			$group->add(new Form_Input(
326
				$fieldname . $trc,
327
				null,
328
				'password',
329
				$value
330
			))->setHelp($description)->setAttribute('autocomplete', 'new-password');
331
			break;
332
		case "textarea":
333
			$group->add(new Form_Textarea(
334
				$fieldname . $trc,
335
				null,
336
				$value
337
			))->setHelp($description);
338

    
339
			break;
340
		case "select":
341
			$options = array();
342
			foreach ($rowhelper['options']['option'] as $rowopt) {
343
				$options[$rowopt['value']] = $rowopt['name'];
344
			}
345

    
346
			$grp = new Form_Select(
347
				$fieldname . $trc,
348
				null,
349
				$value,
350
				$options
351
			);
352

    
353
			$grp->setHelp($description);
354

    
355
			if ($ewidth) {
356
				$grp->setWidth($ewidth);
357
			}
358

    
359
			$group->add($grp);
360

    
361
			break;
362
		case "interfaces_selection":
363
			$size = ($rowhelper['size'] ? "size=\"{$rowhelper['size']}\"" : '');
364
			$multiple = '';
365
			if (isset($rowhelper['multiple'])) {
366
				$multiple = "multiple";
367
			}
368
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
369
			$ifaces = get_configured_interface_with_descr();
370
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
371
			if (!empty($additional_ifaces)) {
372
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
373
			}
374

    
375
			if (is_array($value)) {
376
				$values = $value;
377
			} else {
378
				$values = explode(',', $value);
379
			}
380

    
381
			$ifaces["lo0"] = "loopback";
382
			$options = array();
383
			$selected = array();
384

    
385
			foreach ($ifaces as $ifname => $iface) {
386
				$options[$ifname] = $iface;
387

    
388
				if (in_array($ifname, $values)) {
389
					array_push($selected, $ifname);
390
				}
391
			}
392

    
393
			$group->add(new Form_Select(
394
				$fieldname . $trc,
395
				null,
396
				($multiple) ? $selected:$selected[0],
397
				$options,
398
				$multiple
399
			))->setHelp($description);
400

    
401
			//echo "</select>\n";
402
			break;
403
		case "select_source":
404
			$options = array();
405
			$selected = array();
406

    
407
			$multiple = '';
408
			if (isset($rowhelper['multiple'])) {
409
				$multiple = "multiple";
410
			}
411

    
412
			if (isset($rowhelper['show_disable_value'])) {
413
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
414
			}
415

    
416
			$source_url = $rowhelper['source'];
417
			eval("\$pkg_source_txt = &$source_url;");
418

    
419
			foreach ($pkg_source_txt as $opt) {
420
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
421
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
422
				$options[$source_value] = $source_name;
423

    
424
				if ($source_value == $value) {
425
					array_push($selected, $value);
426
				}
427
			}
428

    
429
			$group->add(new Form_Select(
430
				$fieldname . $trc,
431
				null,
432
				($multiple) ? $selected:$selected[0],
433
				$options,
434
				$multiple
435
			))->setHelp($description);
436

    
437
			break;
438
	}
439
}
440

    
441
function fixup_string($string) {
442
	global $config;
443
	// fixup #1: $myurl -> http[s]://ip_address:port/
444
	$https = "";
445
	$port = config_get_path('system/webguiport');
446
	if ($port != "443" and $port != "80") {
447
		$urlport = ":" . $port;
448
	} else {
449
		$urlport = "";
450
	}
451

    
452
	if ($config['system']['webgui']['protocol'] == "https") {
453
		$https = "s";
454
	}
455
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
456
	$newstring = str_replace("\$myurl", $myurl, $string);
457
	$string = $newstring;
458
	// fixup #2: $wanip
459
	$curwanip = get_interface_ip();
460
	$newstring = str_replace("\$wanip", $curwanip, $string);
461
	$string = $newstring;
462
	// fixup #3: $lanip
463
	$lancfg = config_get_path('interfaces/lan');
464
	$lanip = $lancfg['ipaddr'];
465
	$newstring = str_replace("\$lanip", $lanip, $string);
466
	$string = $newstring;
467
	// fixup #4: fix'r'up here.
468
	return $newstring;
469
}
470

    
471
/*
472
 *	Parse templates if they are defined
473
 */
474
function parse_package_templates() {
475
	global $pkg;
476
	if ($pkg['templates']['template'] != "") {
477
		foreach ($pkg['templates']['template'] as $pkg_template_row) {
478
			$filename = $pkg_template_row['filename'];
479
			$template_text = $pkg_template_row['templatecontents'];
480
			/* calculate total row helpers count and */
481
			/* change fields defined as fieldname_fieldvalue to their value */
482
			foreach ($pkg['fields']['field'] as $fields) {
483
				switch ($fields['type']) {
484
					case "rowhelper":
485
						// save rowhelper items.
486
						$row_helper_total_rows = 0;
487
						$row_helper_data = "";
488
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
489
							foreach ($_POST as $key => $value) {
490
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
491
									$row_helper_total_rows++;
492
									$row_helper_data .= $value;
493
									$sep = "";
494
									preg_match($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
495
									foreach ($sep as $se) {
496
										$separator = $se;
497
									}
498
									if ($separator != "") {
499
										$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
500
										$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
501
									}
502
									$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
503
								}
504
							}
505
						}
506
						break;
507
					default:
508
						$fieldname = $fields['fieldname'];
509
						$fieldvalue = $_POST[$fieldname];
510
						$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
511
				}
512
			}
513
			/* replace $domain_total_rows with total rows */
514
			$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
515

    
516
			/* replace cr's */
517
			$template_text = str_replace("\\n", "\n", $template_text);
518

    
519
			/* write out new template file */
520
			$fout = fopen($filename, "w");
521
			fwrite($fout, $template_text);
522
			fclose($fout);
523
		}
524
	}
525
}
526

    
527
//breadcrumb
528
if ($pkg['title'] != "") {
529
	if (!$only_edit) {
530
		$pkg['title'] = $pkg['title'] . '/Edit';
531
	}
532

    
533
	if (strpos($pkg['title'], '/')) {
534
		$title = explode('/', $pkg['title']);
535

    
536
		foreach ($title as $subtitle) {
537
			$pgtitle[] = gettext($subtitle);
538
			$pglinks[] = ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']);
539
		}
540
	} else {
541
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
542
		$pglinks = array("", ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']));
543
	}
544
} else {
545
	$pgtitle = array(gettext("Package"), gettext("Editor"));
546
	$pglinks = array("", "@self");
547
}
548

    
549
// Create any required tabs
550
if ($pkg['tabs'] != "") {
551
	$tab_array = array();
552
	foreach ($pkg['tabs']['tab'] as $tab) {
553
		if ($tab['tab_level']) {
554
			$tab_level = $tab['tab_level'];
555
		} else {
556
			$tab_level = 1;
557
		}
558

    
559
		if (isset($tab['active'])) {
560
			$active = true;
561
			$pgtitle[] = $tab['text'] ;
562
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
563
		} else {
564
			$active = false;
565
		}
566

    
567
		$urltmp = "";
568
		if ($tab['url'] != "") {
569
			$urltmp = $tab['url'];
570
		}
571

    
572
		if ($tab['xml'] != "") {
573
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
574
		}
575

    
576
		$addresswithport = getenv("HTTP_HOST");
577
		$colonpos = strpos($addresswithport, ":");
578

    
579
		if ($colonpos !== False) {
580
			//my url is actually just the IP address of the pfsense box
581
			$myurl = substr($addresswithport, 0, $colonpos);
582
		} else {
583
			$myurl = $addresswithport;
584
		}
585

    
586
		// eval url so that above $myurl item can be processed if need be.
587
		$url = str_replace('$myurl', $myurl, $urltmp);
588

    
589
		$tab_array[$tab_level][] = array(
590
			$tab['text'],
591
			$active,
592
			$url
593
		);
594
	}
595

    
596
	ksort($tab_array);
597
}
598
if (!empty($pkg['tabs'])) {
599
	$shortcut_section = $pkg['shortcut_section'];
600
}
601

    
602
include("head.inc");
603
if ($pkg['custom_php_after_head_command']) {
604
	eval($pkg['custom_php_after_head_command']);
605
}
606
if (isset($tab_array)) {
607
	foreach ($tab_array as $tab) {
608
		display_top_tabs($tab);
609
	}
610
}
611

    
612
// Start of page display
613
if ($input_errors) {
614
	print_input_errors($input_errors);
615
}
616

    
617
if ($savemsg) {
618
	print_info_box($savemsg, 'success');
619
}
620

    
621
$cols = 0;
622
$savevalue = gettext("Save");
623
if ($pkg['savetext'] != "") {
624
	$savevalue = $pkg['savetext'];
625
}
626

    
627
$savehelp = "";
628
if ($pkg['savehelp'] != "") {
629
	$savehelp = $pkg['savehelp'];
630
}
631

    
632
$saveicon = "fa-solid fa-save";
633
if ($pkg['saveicon'] != "") {
634
	$saveicon = $pkg['saveicon'];
635
}
636

    
637
$savebtnclass = "btn-primary";
638
if ($pkg['savebtnclass'] != "") {
639
	$savebtnclass = $pkg['savebtnclass'];
640
}
641

    
642
$grouping = false; // Indicates the elements we are composing are part of a combined group
643

    
644
$savebutton = new Form_Button(
645
	'submit',
646
	$savevalue,
647
	null,
648
	$saveicon
649
);
650
$savebutton->addClass($savebtnclass);
651

    
652
if ($savehelp) {
653
	$savebutton->setHelp($savehelp);
654
}
655

    
656
$form = new Form($savebutton);
657

    
658
$form->addGlobal(new Form_Input(
659
	'xml',
660
	null,
661
	'hidden',
662
	$xml
663
));
664

    
665
/* If a package's XML has <advanced_options/> configured, then setup
666
 * the section for the fields that have <advancedfield/> set.
667
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
668
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
669
 */
670

    
671
if ($pkg['advanced_options'] == "enabled") {
672
	$advfield_count = 0;
673
	$advanced = new Form_Section("Advanced Features");
674
	$advanced->addClass('advancedoptions');
675
}
676

    
677
$js_array = array();
678

    
679
// Now loop through all of the fields defined in the XML
680
if (!is_array($pkg['fields']['field'])) {
681
	$pkg['fields']['field'] = array();
682
}
683
foreach ($pkg['fields']['field'] as $pkga) {
684

    
685
	$action = "";
686
	$uid = "";
687

    
688
	if ($pkga['type'] == "sorting") {
689
		continue;
690
	}
691

    
692
	// Generate a new section
693
	if ($pkga['type'] == "listtopic") {
694
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
695
			$advanced->addInput(new Form_StaticText(
696
				strip_tags($pkga['name']),
697
				null
698
			));
699

    
700
			$advfield_count++;
701
		} else {
702
			if (isset($section)) {
703
				$form->add($section);
704
			}
705

    
706
			if (isset($pkga['collapse'])) {
707
				$uid = uniqid("section");
708

    
709
				$action = COLLAPSIBLE;
710

    
711
				if ($pkga['collapse'] == "open") {
712
					$action |= SEC_OPEN;
713
				} else {
714
					$action |= SEC_CLOSED;
715
				}
716
			}
717

    
718
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
719
		}
720

    
721
		continue;
722
	}
723

    
724
	// 'begin' starts a form group. ('end' ends it)
725
	if ($pkga['combinefields'] == "begin") {
726
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
727
		$grouping = true;
728
	}
729

    
730
	$size = "";
731
	$colspan="";
732

    
733
	// if user is editing a record, load in the data.
734
	$fieldname = $pkga['fieldname'];
735
	unset($value);
736
	if ($get_from_post) {
737
		$value = $_POST[$fieldname];
738
		if (is_array($value)) {
739
			$value = implode(',', $value);
740
		}
741
	} else {
742
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
743
			$value = $a_pkg[$id][$fieldname];
744
		} else {
745
			if (isset($pkga['default_value'])) {
746
				$value = $pkga['default_value'];
747
			}
748
		}
749
	}
750

    
751
	// If we get here but have no $section, the package config file probably had no listtopic field
752
	// We can create a section with a generic name to fix that
753
	if (!$section) {
754
		$section = new Form_Section('General Options');
755
	}
756

    
757
	// If this is a required field, pre-pend a "*" to the field description
758
	// This tells the system to add "element-required" class text decoration to the field label
759
	if (isset($pkga['required'])) {
760
		$pkga['fielddescr'] = "*" . $pkga['fielddescr'];
761
	}
762

    
763
	switch ($pkga['type']) {
764
		// Create an input element. The format is slightly different depending on whether we are composing a group,
765
		// section, or advanced section. This is true for every element type
766
		case "input":
767
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
768
				$value = base64_decode($value);
769
			}
770

    
771
			$grp = new Form_Input(
772
					$pkga['fieldname'],
773
					$pkga['fielddescr'],
774
					'text',
775
					$value
776
				);
777

    
778
			$grp->setHelp($pkga['description']);
779

    
780
			if ($pkga['width']) {
781
				$grp->setWidth($pkga['width']);
782
			}
783

    
784
			if ($grouping) {
785
				$group->add($grp);
786
			} else {
787
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
788
					$advanced->addInput($grp);
789
				} else {
790
					$section->addInput($grp);
791
				}
792
			}
793

    
794
			break;
795

    
796
		case "password":
797
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
798
				$value = base64_decode($value);
799
			}
800

    
801
			// Create a password element
802
			if ($grouping) {
803
				$group->add(new Form_Input(
804
					$pkga['fieldname'],
805
					$pkga['fielddescr'],
806
					'password',
807
					$value
808
				))->setHelp($pkga['description'])->setAttribute('autocomplete', 'new-password');
809
			} else {
810
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
811
					$advanced->addInput(new Form_Input(
812
						$pkga['fieldname'],
813
						$pkga['fielddescr'],
814
						'password',
815
						$value
816
					))->setHelp($pkga['description'])->setAttribute('autocomplete', 'new-password');
817
				} else {
818
					$section->addInput(new Form_Input(
819
						$pkga['fieldname'],
820
						$pkga['fielddescr'],
821
						'password',
822
						$value
823
					))->setHelp($pkga['description'])->setAttribute('autocomplete', 'new-password');
824
				}
825
			}
826

    
827
			break;
828

    
829
		case "info":
830
			// If the info contains a table we should detect and Bootstrap it
831

    
832
			if (strpos($pkga['description'], '<table') !== FALSE) {
833
				$info = bootstrapTable($pkga['description']);
834
			} else {
835
				$info = $pkga['description'];
836
			}
837

    
838
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
839
				$advanced->addInput(new Form_StaticText(
840
					strip_tags($pkga['fielddescr']),
841
					$info
842
				));
843
			} else {
844
				$section->addInput(new Form_StaticText(
845
					strip_tags($pkga['fielddescr']),
846
					$info
847
				));
848
			}
849

    
850
			break;
851

    
852
		case "select":
853
			// Create a select element
854
			$optionlist = array();
855
			$selectedlist = array();
856

    
857
			$fieldname = $pkga['fieldname'];
858

    
859
			if (isset($pkga['multiple'])) {
860
				$multiple = 'multiple';
861
				$items = explode(',', $value);
862
				$fieldname .= "[]";
863
			} else {
864
				$multiple = '';
865
				$items = array($value);
866
			}
867

    
868
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
869

    
870
			foreach ($pkga['options']['option'] as $opt) {
871
				$optionlist[$opt['value']] = $opt['name'];
872

    
873
				if (in_array($opt['value'], $items)) {
874
					array_push($selectedlist, $opt['value']);
875
				}
876
			}
877

    
878
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
879
				$function = $grouping ? $advanced->add:$advanced->addInput;
880
			} else {
881
				$function = ($grouping) ? $section->add:$section->addInput;
882
			}
883

    
884
			$grp = new Form_Select(
885
						$pkga['fieldname'],
886
						strip_tags($pkga['fielddescr']),
887
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
888
						$optionlist,
889
						isset($pkga['multiple'])
890
					);
891

    
892
			$grp ->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
893

    
894
			if ($pkga['width']) {
895
				$grp->setWidth($pkga['width']);
896
			}
897

    
898
			if ($grouping) {
899
				$group->add($grp);
900
			} else {
901
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
902
					$advanced->addInput($grp);
903
				} else {
904
					$section->addInput($grp);
905
				}
906
			}
907

    
908
			break;
909

    
910
		case "select_source":
911

    
912
			if (isset($pkga['multiple'])) {
913
				$items = explode(',', $value);
914
				$fieldname .= "[]";
915
			} else {
916
				$items = array($value);
917
			}
918

    
919
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
920

    
921
			$source_url = $pkga['source'];
922
			try{
923
				@eval("\$pkg_source_txt = &$source_url;");
924
			} catch (\Throwable | \Error | \Exception $e) {
925
				//do nothing
926
			}
927
			#check if show disable option is present on xml
928
			if (!is_array($pkg_source_txt)) {
929
				$pkg_source_txt = array();
930
			}
931
			if (isset($pkga['show_disable_value'])) {
932
				array_push($pkg_source_txt,
933
					array(($pkga['source_name']? $pkga['source_name'] : $pkga['name'])=> $pkga['show_disable_value'], ($pkga['source_value']? $pkga['source_value'] : $pkga['value'])=> $pkga['show_disable_value']));
934
			}
935

    
936
			$srcoptions = array();
937
			$srcselected = array();
938

    
939
			foreach ($pkg_source_txt as $opt) {
940
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
941
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
942
				$srcoptions[$source_value] = $source_name;
943

    
944
				if (in_array($source_value, $items)) {
945
					array_push($srcselected, $source_value);
946
				}
947
			}
948

    
949
			$descr = (isset($pkga['description'])) ? $pkga['description'] : "";
950
			if ($grouping) {
951
				$group->add(new Form_Select(
952
					$pkga['fieldname'],
953
					strip_tags($pkga['fielddescr']),
954
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
955
					$srcoptions,
956
					isset($pkga['multiple'])
957
				))->setHelp($descr)->setOnchange($onchange);
958
			} else {
959
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
960
					$advanced->addInput(new Form_Select(
961
						$pkga['fieldname'],
962
						strip_tags($pkga['fielddescr']),
963
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
964
						$srcoptions,
965
						isset($pkga['multiple'])
966
					))->setHelp($descr)->setOnchange($onchange);
967
				} else {
968
					$section->addInput(new Form_Select(
969
						$pkga['fieldname'],
970
						strip_tags($pkga['fielddescr']),
971
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
972
						$srcoptions,
973
						isset($pkga['multiple'])
974
					))->setHelp($descr)->setOnchange($onchange);
975
				}
976
			}
977

    
978
			break;
979

    
980
		case "vpn_selection" :
981
			$vpnlist = array();
982

    
983
			foreach (config_get_path('ipsec/phase1', []) as $vpn) {
984
				$vpnlist[$vpn['descr']] = $vpn['descr'];
985

    
986
			}
987

    
988
			if ($grouping) {
989
				$group->add(new Form_Select(
990
					$pkga['fieldname'],
991
					null,
992
					false,
993
					$vpnlist
994
				))->setHelp(fixup_string($pkga['description']));
995
			} else {
996
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
997
					$advanced->addInput(new Form_Select(
998
						$pkga['fieldname'],
999
						null,
1000
						false,
1001
						$vpnlist
1002
					))->setHelp(fixup_string($pkga['description']));
1003
				} else {
1004
					$section->addInput(new Form_Select(
1005
						$pkga['fieldname'],
1006
						null,
1007
						false,
1008
						$vpnlist
1009
					))->setHelp(fixup_string($pkga['description']));
1010
				}
1011
			}
1012

    
1013
			break;
1014

    
1015
		// Create a checkbox element
1016
		case "checkbox":
1017
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
1018
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
1019
				$onclick = 'javascript:enablechange();';
1020
			} else {
1021
				$onclick = '';
1022
			}
1023

    
1024
			if ($grouping) {
1025
				$group->add(new Form_Checkbox(
1026
					$pkga['fieldname'],
1027
					$pkga['fielddescr'],
1028
					fixup_string($pkga['description']),
1029
					($value == "on"),
1030
					'on'
1031
				))->setOnclick($onclick)
1032
				  ->setOnchange($onchange)
1033
				  ->setHelp($pkga['sethelp']);
1034
			} else {
1035
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1036
					$advanced->addInput(new Form_Checkbox(
1037
						$pkga['fieldname'],
1038
						$pkga['fielddescr'],
1039
						fixup_string($pkga['description']),
1040
						($value == "on"),
1041
						'on'
1042
					))->setOnclick($onclick)
1043
					  ->setOnchange($onchange)
1044
					  ->setHelp($pkga['sethelp']);
1045
				} else {
1046
					$section->addInput(new Form_Checkbox(
1047
						$pkga['fieldname'],
1048
						$pkga['fielddescr'],
1049
						fixup_string($pkga['description']),
1050
						($value == "on"),
1051
						'on'
1052
					))->setOnclick($onclick)
1053
					  ->setOnchange($onchange)
1054
					  ->setHelp($pkga['sethelp']);
1055
				}
1056
			}
1057

    
1058
			break;
1059

    
1060
		// Create a textarea element
1061
		case "textarea":
1062
			$rows = $cols = 0;
1063

    
1064
			if ($pkga['rows']) {
1065
				$rows = $pkga['rows'];
1066
			}
1067
			if ($pkga['cols']) {
1068
				$cols = $pkga['cols'];
1069
			}
1070

    
1071
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1072
				$value = base64_decode($value);
1073
			}
1074

    
1075
			$grp = new Form_Textarea(
1076
					$pkga['fieldname'],
1077
					$pkga['fielddescr'],
1078
					$value
1079
			);
1080

    
1081
			$grp->setHelp(fixup_string($pkga['description']));
1082

    
1083
			if ($rows > 0) {
1084
				$grp->setRows($rows);
1085
			}
1086

    
1087
			if ($cols > 0) {
1088
				$grp->setCols($cols);
1089
			}
1090

    
1091
			if ($pkga['wrap'] == "off") {
1092
				$grp->setAttribute("wrap", "off");
1093
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1094
			} else {
1095
				$grp->setAttribute("style", "width: auto;");
1096
			}
1097

    
1098
			if ($grouping) {
1099
				$group->add($grp);
1100
			} else {
1101
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1102
					$advanced->addInput($grp);
1103
				} else {
1104
					$section->addInput($grp);
1105
				}
1106
			}
1107

    
1108
			break;
1109

    
1110
		case "aliases":
1111

    
1112
			// Use xml tag <typealiases> to filter type aliases
1113
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1114
			$fieldname = $pkga['fieldname'];
1115

    
1116
			init_config_arr(array('aliases', 'alias'));
1117
			$a_aliases = &$config['aliases']['alias'];
1118
			$addrisfirst = 0;
1119
			$aliasesaddr = "";
1120

    
1121
			if (isset($a_aliases)) {
1122
				if (!empty($pkga['typealiases'])) {
1123
					foreach ($a_aliases as $alias) {
1124
						if ($alias['type'] == $pkga['typealiases']) {
1125
							if ($addrisfirst == 1) {
1126
								$aliasesaddr .= ",";
1127
							}
1128
							$aliasesaddr .= "'" . $alias['name'] . "'";
1129
							$addrisfirst = 1;
1130
						}
1131
					}
1132
				} else {
1133
					foreach ($a_aliases as $alias) {
1134
						if ($addrisfirst == 1) {
1135
							$aliasesaddr .= ",";
1136
						}
1137
						$aliasesaddr .= "'" . $alias['name'] . "'";
1138
						$addrisfirst = 1;
1139
					}
1140
				}
1141
			}
1142

    
1143
			$grp = new Form_Input(
1144
					$pkga['fieldname'],
1145
					$pkga['fielddescr'],
1146
					'text',
1147
					$value
1148
				);
1149

    
1150
			$grp->setHelp($pkga['description']);
1151

    
1152
			if ($pkga['width']) {
1153
				$grp->setWidth($pkga['width']);
1154
			}
1155

    
1156
			if ($grouping) {
1157
				$group->add($grp);
1158
			} else {
1159
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1160
					$advanced->addInput($grp);
1161
				} else {
1162
					$section->addInput($grp);
1163
				}
1164
			}
1165

    
1166
			$script = "<script type='text/javascript'>\n";
1167
			$script .= "//<![CDATA[\n";
1168
			$script .= "events.push(function(){\n";
1169
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1170
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1171
			$script .= "		source: aliasarray\n";
1172
			$script .= "	})\n";
1173
			$script .= "});\n";
1174
			$script .= "//]]>\n";
1175
			$script .= "</script>";
1176

    
1177
			echo $script;
1178

    
1179
			break;
1180

    
1181
		case "interfaces_selection":
1182
			$ips = array();
1183
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1184
			if (is_array($config['interfaces'])) {
1185
				foreach (config_get_path('interfaces', []) as $iface_key=>$iface_value) {
1186
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1187
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1188
						if (isset($pkga['showips'])) {
1189
							$iface_description .= " address";
1190
						}
1191
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1192
					}
1193
				}
1194
			}
1195

    
1196
			if (isset($pkga['showvirtualips'])) {
1197
				foreach (config_get_path('virtualip/vip', []) as $vip) {
1198
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1199
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1200
					}
1201
					switch ($vip['mode']) {
1202
						case "ipalias":
1203
						case "carp":
1204
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1205
							break;
1206
						case "proxyarp":
1207
							if ($vip['type'] == "network") {
1208
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1209
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1210
								$len = $end - $start;
1211
								for ($i = 0; $i <= $len; $i++) {
1212
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1213
								}
1214
							} else {
1215
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1216
							}
1217
							break;
1218
					}
1219
				}
1220
			}
1221

    
1222
			sort($ips);
1223
			if (isset($pkga['showlistenall'])) {
1224
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1225
			}
1226

    
1227
			if (!preg_match("/$interface_regex/", "loopback")) {
1228
				$loopback_text = gettext("loopback");
1229
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1230
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1231
			}
1232

    
1233
			#show interfaces array on gui
1234
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1235
			$multiple = '';
1236
			$fieldname = $pkga['fieldname'];
1237
			if (isset($pkga['multiple'])) {
1238
				$fieldname .= '[]';
1239
				$multiple = 'multiple';
1240
			}
1241

    
1242
			$selectedlist = array();
1243
			$optionlist = array();
1244

    
1245
			if (is_array($value)) {
1246
				$values = $value;
1247
			} else {
1248
				$values = explode(',', $value);
1249
			}
1250

    
1251
			foreach ($ips as $iface) {
1252
				if (in_array($iface['ip'], $values)) {
1253
					array_push($selectedlist, $iface['ip']);
1254
				}
1255

    
1256
				$optionlist[$iface['ip']] = $iface['description'];
1257
			}
1258

    
1259
			if ($grouping) {
1260
				$group->add(new Form_Select(
1261
					$pkga['fieldname'],
1262
					$pkga['fielddescr'],
1263
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1264
					$optionlist,
1265
					isset($pkga['multiple'])
1266
				))->setHelp($pkga['description']);
1267
			} else {
1268
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1269
					$advanced->addInput(new Form_Select(
1270
						$pkga['fieldname'],
1271
						$pkga['fielddescr'],
1272
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1273
						$optionlist,
1274
						isset($pkga['multiple'])
1275
					))->setHelp($pkga['description']);
1276
				} else {
1277
					$section->addInput(new Form_Select(
1278
						$pkga['fieldname'],
1279
						$pkga['fielddescr'],
1280
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1281
						$optionlist,
1282
						isset($pkga['multiple'])
1283
					))->setHelp($pkga['description']);
1284
				}
1285
			}
1286

    
1287
			break;
1288

    
1289
		// Create radio button
1290
		case "radio":
1291
			if ($grouping) {
1292
				$group->add(new Form_Checkbox(
1293
					$pkga['fieldname'],
1294
					$pkga['fielddescr'],
1295
					fixup_string($pkga['description']),
1296
					($value == "on"),
1297
					'on'
1298
				))->displayAsRadio();
1299
			} else {
1300
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1301
					$advanced->addInput(new Form_Checkbox(
1302
						$pkga['fieldname'],
1303
						$pkga['fielddescr'],
1304
						fixup_string($pkga['description']),
1305
						($value == "on"),
1306
						'on'
1307
					))->displayAsRadio();
1308
				} else {
1309
					$section->addInput(new Form_Checkbox(
1310
						$pkga['fieldname'],
1311
						$pkga['fielddescr'],
1312
						fixup_string($pkga['description']),
1313
						($value == "on"),
1314
						'on'
1315
					))->displayAsRadio();
1316
				}
1317
			}
1318

    
1319
			break;
1320

    
1321
		// Create form button
1322
		case "button":
1323
			$newbtnicon = "fa-solid fa-save";
1324
			if ($pkga['buttonicon'] != "") {
1325
				$newbtnicon = $pkga['buttonicon'];
1326
			}
1327
			$newbtnclass = "btn-primary";
1328
			if ($pkga['buttonclass'] != "") {
1329
				$newbtnclass = $pkga['buttonclass'];
1330
			}
1331

    
1332
			$newbtn = new Form_Button(
1333
				$pkga['fieldname'],
1334
				$pkga['fielddescr'],
1335
				null,
1336
				$newbtnicon
1337
			);
1338
			$newbtn->addClass($newbtnclass);
1339

    
1340
			if ($grouping) {
1341
				$group->add(new Form_StaticText(
1342
					null,
1343
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1344
				));
1345
			} else {
1346
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1347
					$advanced->addInput(new Form_StaticText(
1348
						null,
1349
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1350
					));
1351
				} else {
1352
					$section->addInput(new Form_StaticText(
1353
						null,
1354
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1355
					));
1356
				}
1357
			}
1358

    
1359
			break;
1360

    
1361
		case "schedule_selection":
1362

    
1363
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1364
			$schedules = array();
1365
			$schedules[] = "none";
1366
			if (is_array($config['schedules']['schedule'])) {
1367
				foreach (config_get_path('schedules/schedule', []) as $schedule) {
1368
					if ($schedule['name'] != "") {
1369
						$schedules[] = $schedule['name'];
1370
					}
1371
				}
1372
			}
1373

    
1374
			foreach ($schedules as $schedule) {
1375
				if ($schedule == "none") {
1376
					$schedlist[""] = $schedule;
1377
				} else {
1378
					$schedlist[$schedule] = $schedule;
1379
				}
1380
			}
1381

    
1382
			if ($grouping) {
1383
				$group->add(new Form_Select(
1384
					$pkga['fieldname'],
1385
					$pkga['fielddescr'],
1386
					$value,
1387
					$schedlist
1388
				))->setHelp(fixup_string($pkga['description']));
1389
			} else {
1390
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1391
					$advanced->addInput(new Form_Select(
1392
						$pkga['fieldname'],
1393
						$pkga['fielddescr'],
1394
						$value,
1395
						$schedlist
1396
					))->setHelp(fixup_string($pkga['description']));
1397
				} else {
1398
					$section->addInput(new Form_Select(
1399
						$pkga['fieldname'],
1400
						$pkga['fielddescr'],
1401
						$value,
1402
						$schedlist
1403
					))->setHelp(fixup_string($pkga['description']));
1404
				}
1405
			}
1406

    
1407
			break;
1408

    
1409
		case "rowhelper":
1410

    
1411
			$rowhelpername="row";
1412

    
1413
				$rowcounter = 0;
1414
				$trc = 0;
1415

    
1416
				//Use assigned $a_pkg or create an empty array to enter loop
1417
				if (isset($a_pkg[$id][$rowhelpername])) {
1418
					$saved_rows=$a_pkg[$id][$rowhelpername];
1419
				} else {
1420
					$saved_rows[] = array();
1421
				}
1422

    
1423
				$numrows = count($saved_rows) - 1;
1424

    
1425
				foreach ($saved_rows as $row) {
1426
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1427
					$group->addClass('repeatable');
1428

    
1429
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1430
						unset($value);
1431
						$width = null;
1432

    
1433
						$fieldname = $rowhelper['fieldname'];
1434
						$fielddescr = $rowhelper['fielddescr'];
1435

    
1436
						// If input validation failed, read the value from the POST data so that the user's input is not lost
1437
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1438
							$value = $_POST[$fieldname.$rowcounter];
1439
						} elseif (isset($id) && $a_pkg[$id]) {
1440
							$value = $row[$fieldname];
1441
						} elseif ($rowhelper['value'] != "") {
1442
							$value = $rowhelper['value'];
1443
						}
1444

    
1445
						$type = $rowhelper['type'];
1446
						if ($type == "input" || $type == "password" || $type == "textarea") {
1447
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1448
								$value = base64_decode($value);
1449
							}
1450
						}
1451

    
1452

    
1453
						if ($rowhelper['size']) {
1454
							$size = $rowhelper['size'];
1455
						} else if ($pkga['size']) {
1456
							$size = $pkga['size'];
1457
						} else {
1458
							$size = "8";
1459
						}
1460

    
1461
						if ($rowhelper['width']) {
1462
							$width = $rowhelper['width'];
1463
						}
1464

    
1465
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, ($numrows == $rowcounter) ? $fielddescr:null, $width);
1466

    
1467
						$text = "";
1468
						$trc++;
1469
					}
1470

    
1471
					// Delete row button
1472
					$group->add(new Form_Button(
1473
						'deleterow' . $rowcounter,
1474
						'Delete',
1475
						null,
1476
						'fa-solid fa-trash-can'
1477
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1478

    
1479
					$rowcounter++;
1480
					$section->add($group);
1481
				}
1482

    
1483
			// Add row button
1484
			$section->addInput(new Form_Button(
1485
				'addrow',
1486
				'Add',
1487
				null,
1488
				'fa-solid fa-plus'
1489
			))->addClass('btn-success');
1490

    
1491
			break;
1492

    
1493
	}
1494

    
1495
		if ($pkga['combinefields'] == "end") {
1496
			$group->add(new Form_StaticText(
1497
				null,
1498
				null
1499
			));
1500

    
1501
			if ($advanced) {
1502
				$advanced->add($group);
1503
			} else {
1504
				$section->add($group);
1505
			}
1506

    
1507
			$grouping = false;
1508
		}
1509

    
1510
	#increment counter
1511
	$i++;
1512
} // e-o-foreach field described in the XML
1513

    
1514
if ($section) {
1515
	$form->add($section);
1516
}
1517

    
1518
$form->addGlobal(new Form_Input(
1519
	'id',
1520
	null,
1521
	'hidden',
1522
	$id
1523
));
1524

    
1525
// If we created an advanced section, add it (and a button) to the form here
1526
if (!empty($advanced)) {
1527
	$form->addGlobal(new Form_Button(
1528
		'showadv',
1529
		'Show Advanced Options',
1530
		null,
1531
		'fa-solid fa-cog'
1532
	))->setAttribute('type','button')->addClass('btn-info');
1533

    
1534
	$form->add($advanced);
1535
}
1536

    
1537
print($form);
1538

    
1539
if ($pkg['note'] != "") {
1540
	print_info_box($pkg['note'], 'info');
1541
}
1542

    
1543
if ($pkg['custom_php_after_form_command']) {
1544
	eval($pkg['custom_php_after_form_command']);
1545
}
1546

    
1547

    
1548
$hidemsg = gettext("Show Advanced Options");
1549
$showmsg = gettext("Hide Advanced Options");
1550

    
1551
if ($pkg['fields']['field'] != "") { ?>
1552
<script type="text/javascript">
1553
//<![CDATA[
1554
	events.push(function() {
1555

    
1556
	// Hide the advanced section
1557
	var advanced_visible = false;
1558

    
1559
	// Hide on page load
1560
	$('.advancedoptions').hide();
1561

    
1562
	// Suppress "Delete row" button if there are fewer than two rows
1563
	checkLastRow();
1564

    
1565
	// Show advanced section if you click the showadv button
1566
	$("#showadv").click(function() {
1567
		advanced_visible = !advanced_visible;
1568

    
1569
		if (advanced_visible) {
1570
			$('.advancedoptions').show();
1571
			$("#showadv").html('<i class="fa-solid fa-cog icon-embed-btn"></i>' + "<?=$showmsg?>");
1572
		} else {
1573
			$('.advancedoptions').hide();
1574
			$("#showadv").html('<i class="fa-solid fa-cog icon-embed-btn"></i>' + "<?=$hidemsg?>");
1575
		}
1576
	});
1577

    
1578
	// Call enablechange function
1579
	enablechange();
1580
});
1581

    
1582
	function enablechange() {
1583
<?php
1584
	foreach ($pkg['fields']['field'] as $field) {
1585
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1586
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1587

    
1588
			if (isset($field['enablefields'])) {
1589
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1590
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1591
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1592
					echo "\t\t}\n";
1593
				}
1594
			}
1595

    
1596
			if (isset($field['checkenablefields'])) {
1597
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1598
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1599
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1600
					echo "\t\t}\n";
1601
				}
1602
			}
1603

    
1604
			echo "\t}\n\telse {\n";
1605

    
1606
			if (isset($field['enablefields'])) {
1607
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1608
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1609
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1610
					echo "\t\t}\n";
1611
				}
1612
			}
1613

    
1614
			if (isset($field['checkenablefields'])) {
1615
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1616
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1617
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1618
					echo "\t\t}\n";
1619
				}
1620
			}
1621

    
1622
			echo "\t}\n";
1623
		}
1624
	}
1625
	?>
1626
	}
1627
//]]>
1628
</script>
1629

    
1630
<?php
1631
}
1632

    
1633
include("foot.inc");
(97-97/230)