Project

General

Profile

Download (41.8 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-2020 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-package-edit
24
##|*NAME=Package: Edit
25
##|*DESCR=Allow access to the 'Package: Edit' page.
26
##|*MATCH=pkg_edit.php*
27
##|-PRIV
28

    
29
ini_set('max_execution_time', '0');
30

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

    
39
$xml = htmlspecialchars($_REQUEST['xml']);
40

    
41
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
42

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

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

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

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

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

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

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

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

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

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

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

    
106
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
107

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

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

    
116
if ($_POST) {
117
	$rows = 0;
118

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

    
134
	if ($pkg['custom_php_validation_command']) {
135
		eval($pkg['custom_php_validation_command']);
136
	}
137

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

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

    
169
	if (empty($pkg['donotsave'])) {
170

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

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

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

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

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

    
246
			parse_package_templates();
247

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

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

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

    
275

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

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

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

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

    
304
			$inpt->setHelp($description);
305

    
306
			if ($ewidth) {
307
				$inpt->setWidth($ewidth);
308
			}
309

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

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

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

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

    
351
			$grp->setHelp($description);
352

    
353
			if ($ewidth) {
354
				$grp->setWidth($ewidth);
355
			}
356

    
357
			$group->add($grp);
358

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

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

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

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

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

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

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

    
405
			if (isset($rowhelper['show_disable_value'])) {
406
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
407
			}
408

    
409
			$source_url = $rowhelper['source'];
410
			eval("\$pkg_source_txt = &$source_url;");
411

    
412
			foreach ($pkg_source_txt as $opt) {
413
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
414
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
415
				$options[$source_value] = $source_name;
416

    
417
				if ($source_value == $value) {
418
					array_push($selected, $value);
419
				}
420
			}
421

    
422
			$group->add(new Form_Select(
423
				$fieldname . $trc,
424
				null,
425
				($multiple) ? $selected:$selected[0],
426
				$options,
427
				$multiple
428
			))->setHelp($description);
429

    
430
			break;
431
	}
432
}
433

    
434
function fixup_string($string) {
435
	global $config;
436
	// fixup #1: $myurl -> http[s]://ip_address:port/
437
	$https = "";
438
	$port = $config['system']['webguiport'];
439
	if ($port != "443" and $port != "80") {
440
		$urlport = ":" . $port;
441
	} else {
442
		$urlport = "";
443
	}
444

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

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

    
509
			/* replace cr's */
510
			$template_text = str_replace("\\n", "\n", $template_text);
511

    
512
			/* write out new template file */
513
			$fout = fopen($filename, "w");
514
			fwrite($fout, $template_text);
515
			fclose($fout);
516
		}
517
	}
518
}
519

    
520
//breadcrumb
521
if ($pkg['title'] != "") {
522
	if (!$only_edit) {
523
		$pkg['title'] = $pkg['title'] . '/Edit';
524
	}
525

    
526
	if (strpos($pkg['title'], '/')) {
527
		$title = explode('/', $pkg['title']);
528

    
529
		foreach ($title as $subtitle) {
530
			$pgtitle[] = gettext($subtitle);
531
			$pglinks[] = ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']);
532
		}
533
	} else {
534
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
535
		$pglinks = array("", ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']));
536
	}
537
} else {
538
	$pgtitle = array(gettext("Package"), gettext("Editor"));
539
	$pglinks = array("", "@self");
540
}
541

    
542
// Create any required tabs
543
if ($pkg['tabs'] != "") {
544
	$tab_array = array();
545
	foreach ($pkg['tabs']['tab'] as $tab) {
546
		if ($tab['tab_level']) {
547
			$tab_level = $tab['tab_level'];
548
		} else {
549
			$tab_level = 1;
550
		}
551

    
552
		if (isset($tab['active'])) {
553
			$active = true;
554
			$pgtitle[] = $tab['text'] ;
555
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
556
		} else {
557
			$active = false;
558
		}
559

    
560
		$urltmp = "";
561
		if ($tab['url'] != "") {
562
			$urltmp = $tab['url'];
563
		}
564

    
565
		if ($tab['xml'] != "") {
566
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
567
		}
568

    
569
		$addresswithport = getenv("HTTP_HOST");
570
		$colonpos = strpos($addresswithport, ":");
571

    
572
		if ($colonpos !== False) {
573
			//my url is actually just the IP address of the pfsense box
574
			$myurl = substr($addresswithport, 0, $colonpos);
575
		} else {
576
			$myurl = $addresswithport;
577
		}
578

    
579
		// eval url so that above $myurl item can be processed if need be.
580
		$url = str_replace('$myurl', $myurl, $urltmp);
581

    
582
		$tab_array[$tab_level][] = array(
583
			$tab['text'],
584
			$active,
585
			$url
586
		);
587
	}
588

    
589
	ksort($tab_array);
590
}
591
if (!empty($pkg['tabs'])) {
592
	$shortcut_section = $pkg['shortcut_section'];
593
}
594

    
595
include("head.inc");
596
if ($pkg['custom_php_after_head_command']) {
597
	eval($pkg['custom_php_after_head_command']);
598
}
599
if (isset($tab_array)) {
600
	foreach ($tab_array as $tabid => $tab) {
601
		display_top_tabs($tab);
602
	}
603
}
604

    
605
// Start of page display
606
if ($input_errors) {
607
	print_input_errors($input_errors);
608
}
609

    
610
if ($savemsg) {
611
	print_info_box($savemsg, 'success');
612
}
613

    
614
$cols = 0;
615
$savevalue = gettext("Save");
616
if ($pkg['savetext'] != "") {
617
	$savevalue = $pkg['savetext'];
618
}
619

    
620
$savehelp = "";
621
if ($pkg['savehelp'] != "") {
622
	$savehelp = $pkg['savehelp'];
623
}
624

    
625
$saveicon = "fa-save";
626
if ($pkg['saveicon'] != "") {
627
	$saveicon = $pkg['saveicon'];
628
}
629

    
630
$savebtnclass = "btn-primary";
631
if ($pkg['savebtnclass'] != "") {
632
	$savebtnclass = $pkg['savebtnclass'];
633
}
634

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

    
637
$savebutton = new Form_Button(
638
	'submit',
639
	$savevalue,
640
	null,
641
	$saveicon
642
);
643
$savebutton->addClass($savebtnclass);
644

    
645
if ($savehelp) {
646
	$savebutton->setHelp($savehelp);
647
}
648

    
649
$form = new Form($savebutton);
650

    
651
$form->addGlobal(new Form_Input(
652
	'xml',
653
	null,
654
	'hidden',
655
	$xml
656
));
657

    
658
/* If a package's XML has <advanced_options/> configured, then setup
659
 * the section for the fields that have <advancedfield/> set.
660
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
661
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
662
 */
663

    
664
if ($pkg['advanced_options'] == "enabled") {
665
	$advfield_count = 0;
666
	$advanced = new Form_Section("Advanced Features");
667
	$advanced->addClass('advancedoptions');
668
}
669

    
670
$js_array = array();
671

    
672
// Now loop through all of the fields defined in the XML
673
if (!is_array($pkg['fields']['field'])) {
674
	$pkg['fields']['field'] = array();
675
}
676
foreach ($pkg['fields']['field'] as $pkga) {
677

    
678
	$action = "";
679
	$uid = "";
680

    
681
	if ($pkga['type'] == "sorting") {
682
		continue;
683
	}
684

    
685
	// Generate a new section
686
	if ($pkga['type'] == "listtopic") {
687
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
688
			$advanced->addInput(new Form_StaticText(
689
				strip_tags($pkga['name']),
690
				null
691
			));
692

    
693
			$advfield_count++;
694
		} else {
695
			if (isset($section)) {
696
				$form->add($section);
697
			}
698

    
699
			if (isset($pkga['collapse'])) {
700
				$uid = uniqid("section");
701

    
702
				$action = COLLAPSIBLE;
703

    
704
				if ($pkga['collapse'] == "open") {
705
					$action |= SEC_OPEN;
706
				} else {
707
					$action |= SEC_CLOSED;
708
				}
709
			}
710

    
711
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
712
		}
713

    
714
		continue;
715
	}
716

    
717
	// 'begin' starts a form group. ('end' ends it)
718
	if ($pkga['combinefields'] == "begin") {
719
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
720
		$grouping = true;
721
	}
722

    
723
	$size = "";
724
	$colspan="";
725

    
726
	// if user is editing a record, load in the data.
727
	$fieldname = $pkga['fieldname'];
728
	unset($value);
729
	if ($get_from_post) {
730
		$value = $_POST[$fieldname];
731
		if (is_array($value)) {
732
			$value = implode(',', $value);
733
		}
734
	} else {
735
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
736
			$value = $a_pkg[$id][$fieldname];
737
		} else {
738
			if (isset($pkga['default_value'])) {
739
				$value = $pkga['default_value'];
740
			}
741
		}
742
	}
743

    
744
	// If we get here but have no $section, the package config file probably had no listtopic field
745
	// We can create a section with a generic name to fix that
746
	if (!$section) {
747
		$section = new Form_Section('General Options');
748
	}
749

    
750
	// If this is a required field, pre-pend a "*" to the field description
751
	// This tells the system to add "element-required" class text decoration to the field label
752
	if (isset($pkga['required'])) {
753
		$pkga['fielddescr'] = "*" . $pkga['fielddescr'];
754
	}
755

    
756
	switch ($pkga['type']) {
757
		// Create an input element. The format is slightly different depending on whether we are composing a group,
758
		// section, or advanced section. This is true for every element type
759
		case "input":
760
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
761
				$value = base64_decode($value);
762
			}
763

    
764
			$grp = new Form_Input(
765
					$pkga['fieldname'],
766
					$pkga['fielddescr'],
767
					'text',
768
					$value
769
				);
770

    
771
			$grp->setHelp($pkga['description']);
772

    
773
			if ($pkga['width']) {
774
				$grp->setWidth($pkga['width']);
775
			}
776

    
777
			if ($grouping) {
778
				$group->add($grp);
779
			} else {
780
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
781
					$advanced->addInput($grp);
782
				} else {
783
					$section->addInput($grp);
784
				}
785
			}
786

    
787
			break;
788

    
789
		case "password":
790
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
791
				$value = base64_decode($value);
792
			}
793

    
794
			// Create a password element
795
			if ($grouping) {
796
				$group->add(new Form_Input(
797
					$pkga['fieldname'],
798
					$pkga['fielddescr'],
799
					'password',
800
					$value
801
				))->setHelp($pkga['description']);
802
			} else {
803
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
804
					$advanced->addInput(new Form_Input(
805
						$pkga['fieldname'],
806
						$pkga['fielddescr'],
807
						'password',
808
						$value
809
					))->setHelp($pkga['description']);
810
				} else {
811
					$section->addInput(new Form_Input(
812
						$pkga['fieldname'],
813
						$pkga['fielddescr'],
814
						'password',
815
						$value
816
					))->setHelp($pkga['description']);
817
				}
818
			}
819

    
820
			break;
821

    
822
		case "info":
823
			// If the info contains a table we should detect and Bootstrap it
824

    
825
			if (strpos($pkga['description'], '<table') !== FALSE) {
826
				$info = bootstrapTable($pkga['description']);
827
			} else {
828
				$info = $pkga['description'];
829
			}
830

    
831
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
832
				$advanced->addInput(new Form_StaticText(
833
					strip_tags($pkga['fielddescr']),
834
					$info
835
				));
836
			} else {
837
				$section->addInput(new Form_StaticText(
838
					strip_tags($pkga['fielddescr']),
839
					$info
840
				));
841
			}
842

    
843
			break;
844

    
845
		case "select":
846
			// Create a select element
847
			$optionlist = array();
848
			$selectedlist = array();
849

    
850
			$fieldname = $pkga['fieldname'];
851

    
852
			if (isset($pkga['multiple'])) {
853
				$multiple = 'multiple';
854
				$items = explode(',', $value);
855
				$fieldname .= "[]";
856
			} else {
857
				$multiple = '';
858
				$items = array($value);
859
			}
860

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

    
863
			foreach ($pkga['options']['option'] as $opt) {
864
				$optionlist[$opt['value']] = $opt['name'];
865

    
866
				if (in_array($opt['value'], $items)) {
867
					array_push($selectedlist, $opt['value']);
868
				}
869
			}
870

    
871
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
872
				$function = $grouping ? $advanced->add:$advanced->addInput;
873
			} else {
874
				$function = ($grouping) ? $section->add:$section->addInput;
875
			}
876

    
877
			$grp = new Form_Select(
878
						$pkga['fieldname'],
879
						strip_tags($pkga['fielddescr']),
880
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
881
						$optionlist,
882
						isset($pkga['multiple'])
883
					);
884

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

    
887
			if ($pkga['width']) {
888
				$grp->setWidth($pkga['width']);
889
			}
890

    
891
			if ($grouping) {
892
				$group->add($grp);
893
			} else {
894
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
895
					$advanced->addInput($grp);
896
				} else {
897
					$section->addInput($grp);
898
				}
899
			}
900

    
901
			break;
902

    
903
		case "select_source":
904

    
905
			if (isset($pkga['multiple'])) {
906
				$items = explode(',', $value);
907
				$fieldname .= "[]";
908
			} else {
909
				$items = array($value);
910
			}
911

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

    
914
			$source_url = $pkga['source'];
915
			try{
916
				@eval("\$pkg_source_txt = &$source_url;");
917
			} catch (\Throwable | \Error | \Exception $e) {
918
				//do nothing
919
			}
920
			#check if show disable option is present on xml
921
			if (!is_array($pkg_source_txt)) {
922
				$pkg_source_txt = array();
923
			}
924
			if (isset($pkga['show_disable_value'])) {
925
				array_push($pkg_source_txt,
926
					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']));
927
			}
928

    
929
			$srcoptions = array();
930
			$srcselected = array();
931

    
932
			foreach ($pkg_source_txt as $opt) {
933
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
934
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
935
				$srcoptions[$source_value] = $source_name;
936

    
937
				if (in_array($source_value, $items)) {
938
					array_push($srcselected, $source_value);
939
				}
940
			}
941

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

    
971
			break;
972

    
973
		case "vpn_selection" :
974
			$vpnlist = array();
975

    
976
			foreach ($config['ipsec']['phase1'] as $vpn) {
977
				$vpnlist[$vpn['descr']] = $vpn['descr'];
978

    
979
			}
980

    
981
			if ($grouping) {
982
				$group->add(new Form_Select(
983
					$pkga['fieldname'],
984
					null,
985
					false,
986
					$vpnlist
987
				))->setHelp(fixup_string($pkga['description']));
988
			} else {
989
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
990
					$advanced->addInput(new Form_Select(
991
						$pkga['fieldname'],
992
						null,
993
						false,
994
						$vpnlist
995
					))->setHelp(fixup_string($pkga['description']));
996
				} else {
997
					$section->addInput(new Form_Select(
998
						$pkga['fieldname'],
999
						null,
1000
						false,
1001
						$vpnlist
1002
					))->setHelp(fixup_string($pkga['description']));
1003
				}
1004
			}
1005

    
1006
			break;
1007

    
1008
		// Create a checkbox element
1009
		case "checkbox":
1010
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
1011
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
1012
				$onclick = 'javascript:enablechange();';
1013
			} else {
1014
				$onclick = '';
1015
			}
1016

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

    
1051
			break;
1052

    
1053
		// Create a textarea element
1054
		case "textarea":
1055
			$rows = $cols = 0;
1056

    
1057
			if ($pkga['rows']) {
1058
				$rows = $pkga['rows'];
1059
			}
1060
			if ($pkga['cols']) {
1061
				$cols = $pkga['cols'];
1062
			}
1063

    
1064
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1065
				$value = base64_decode($value);
1066
			}
1067

    
1068
			$grp = new Form_Textarea(
1069
					$pkga['fieldname'],
1070
					$pkga['fielddescr'],
1071
					$value
1072
			);
1073

    
1074
			$grp->setHelp(fixup_string($pkga['description']));
1075

    
1076
			if ($rows > 0) {
1077
				$grp->setRows($rows);
1078
			}
1079

    
1080
			if ($cols > 0) {
1081
				$grp->setCols($cols);
1082
			}
1083

    
1084
			if ($pkga['wrap'] == "off") {
1085
				$grp->setAttribute("wrap", "off");
1086
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1087
			} else {
1088
				$grp->setAttribute("style", "width: auto;");
1089
			}
1090

    
1091
			if ($grouping) {
1092
				$group->add($grp);
1093
			} else {
1094
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1095
					$advanced->addInput($grp);
1096
				} else {
1097
					$section->addInput($grp);
1098
				}
1099
			}
1100

    
1101
			break;
1102

    
1103
		case "aliases":
1104

    
1105
			// Use xml tag <typealiases> to filter type aliases
1106
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1107
			$fieldname = $pkga['fieldname'];
1108

    
1109
			init_config_arr(array('aliases', 'alias'));
1110
			$a_aliases = &$config['aliases']['alias'];
1111
			$addrisfirst = 0;
1112
			$aliasesaddr = "";
1113

    
1114
			if (isset($a_aliases)) {
1115
				if (!empty($pkga['typealiases'])) {
1116
					foreach ($a_aliases as $alias) {
1117
						if ($alias['type'] == $pkga['typealiases']) {
1118
							if ($addrisfirst == 1) {
1119
								$aliasesaddr .= ",";
1120
							}
1121
							$aliasesaddr .= "'" . $alias['name'] . "'";
1122
							$addrisfirst = 1;
1123
						}
1124
					}
1125
				} else {
1126
					foreach ($a_aliases as $alias) {
1127
						if ($addrisfirst == 1) {
1128
							$aliasesaddr .= ",";
1129
						}
1130
						$aliasesaddr .= "'" . $alias['name'] . "'";
1131
						$addrisfirst = 1;
1132
					}
1133
				}
1134
			}
1135

    
1136
			$grp = new Form_Input(
1137
					$pkga['fieldname'],
1138
					$pkga['fielddescr'],
1139
					'text',
1140
					$value
1141
				);
1142

    
1143
			$grp->setHelp($pkga['description']);
1144

    
1145
			if ($pkga['width']) {
1146
				$grp->setWidth($pkga['width']);
1147
			}
1148

    
1149
			if ($grouping) {
1150
				$group->add($grp);
1151
			} else {
1152
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1153
					$advanced->addInput($grp);
1154
				} else {
1155
					$section->addInput($grp);
1156
				}
1157
			}
1158

    
1159
			$script = "<script type='text/javascript'>\n";
1160
			$script .= "//<![CDATA[\n";
1161
			$script .= "events.push(function(){\n";
1162
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1163
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1164
			$script .= "		source: aliasarray\n";
1165
			$script .= "	})\n";
1166
			$script .= "});\n";
1167
			$script .= "//]]>\n";
1168
			$script .= "</script>";
1169

    
1170
			echo $script;
1171

    
1172
			break;
1173

    
1174
		case "interfaces_selection":
1175
			$ips = array();
1176
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1177
			if (is_array($config['interfaces'])) {
1178
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1179
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1180
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1181
						if (isset($pkga['showips'])) {
1182
							$iface_description .= " address";
1183
						}
1184
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1185
					}
1186
				}
1187
			}
1188

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

    
1215
			sort($ips);
1216
			if (isset($pkga['showlistenall'])) {
1217
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1218
			}
1219

    
1220
			if (!preg_match("/$interface_regex/", "loopback")) {
1221
				$loopback_text = gettext("loopback");
1222
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1223
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1224
			}
1225

    
1226
			#show interfaces array on gui
1227
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1228
			$multiple = '';
1229
			$fieldname = $pkga['fieldname'];
1230
			if (isset($pkga['multiple'])) {
1231
				$fieldname .= '[]';
1232
				$multiple = 'multiple';
1233
			}
1234

    
1235
			$selectedlist = array();
1236
			$optionlist = array();
1237

    
1238
			if (is_array($value)) {
1239
				$values = $value;
1240
			} else {
1241
				$values = explode(',', $value);
1242
			}
1243

    
1244
			foreach ($ips as $iface) {
1245
				if (in_array($iface['ip'], $values)) {
1246
					array_push($selectedlist, $iface['ip']);
1247
				}
1248

    
1249
				$optionlist[$iface['ip']] = $iface['description'];
1250
			}
1251

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

    
1280
			break;
1281

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

    
1312
			break;
1313

    
1314
		// Create form button
1315
		case "button":
1316
			$newbtnicon = "fa-save";
1317
			if ($pkga['buttonicon'] != "") {
1318
				$newbtnicon = $pkga['buttonicon'];
1319
			}
1320
			$newbtnclass = "btn-primary";
1321
			if ($pkga['buttonclass'] != "") {
1322
				$newbtnclass = $pkga['buttonclass'];
1323
			}
1324

    
1325
			$newbtn = new Form_Button(
1326
				$pkga['fieldname'],
1327
				$pkga['fieldname'],
1328
				null,
1329
				$newbtnicon
1330
			);
1331
			$newbtn->addClass($newbtnclass);
1332

    
1333
			if ($grouping) {
1334
				$group->add(new Form_StaticText(
1335
					null,
1336
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1337
				));
1338
			} else {
1339
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1340
					$advanced->addInput(new Form_StaticText(
1341
						null,
1342
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1343
					));
1344
				} else {
1345
					$section->addInput(new Form_StaticText(
1346
						null,
1347
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1348
					));
1349
				}
1350
			}
1351

    
1352
			break;
1353

    
1354
		case "schedule_selection":
1355

    
1356
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1357
			$schedules = array();
1358
			$schedules[] = "none";
1359
			if (is_array($config['schedules']['schedule'])) {
1360
				foreach ($config['schedules']['schedule'] as $schedule) {
1361
					if ($schedule['name'] != "") {
1362
						$schedules[] = $schedule['name'];
1363
					}
1364
				}
1365
			}
1366

    
1367
			foreach ($schedules as $schedule) {
1368
				if ($schedule == "none") {
1369
					$schedlist[""] = $schedule;
1370
				} else {
1371
					$schedlist[$schedule] = $schedule;
1372
				}
1373
			}
1374

    
1375
			if ($grouping) {
1376
				$group->add(new Form_Select(
1377
					$pkga['fieldname'],
1378
					$pkga['fielddescr'],
1379
					$value,
1380
					$schedlist
1381
				))->setHelp(fixup_string($pkga['description']));
1382
			} else {
1383
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1384
					$advanced->addInput(new Form_Select(
1385
						$pkga['fieldname'],
1386
						$pkga['fielddescr'],
1387
						$value,
1388
						$schedlist
1389
					))->setHelp(fixup_string($pkga['description']));
1390
				} else {
1391
					$section->addInput(new Form_Select(
1392
						$pkga['fieldname'],
1393
						$pkga['fielddescr'],
1394
						$value,
1395
						$schedlist
1396
					))->setHelp(fixup_string($pkga['description']));
1397
				}
1398
			}
1399

    
1400
			break;
1401

    
1402
		case "rowhelper":
1403

    
1404
			$rowhelpername="row";
1405

    
1406
				$rowcounter = 0;
1407
				$trc = 0;
1408

    
1409
				//Use assigned $a_pkg or create an empty array to enter loop
1410
				if (isset($a_pkg[$id][$rowhelpername])) {
1411
					$saved_rows=$a_pkg[$id][$rowhelpername];
1412
				} else {
1413
					$saved_rows[] = array();
1414
				}
1415

    
1416
				$numrows = count($saved_rows) - 1;
1417

    
1418
				foreach ($saved_rows as $row) {
1419
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1420
					$group->addClass('repeatable');
1421

    
1422
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1423
						unset($value);
1424
						$width = null;
1425

    
1426
						$fieldname = $rowhelper['fieldname'];
1427
						$fielddescr = $rowhelper['fielddescr'];
1428

    
1429
						// If input validation failed, read the value from the POST data so that the user's input is not lost
1430
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1431
							$value = $_POST[$fieldname.$rowcounter];
1432
						} elseif (isset($id) && $a_pkg[$id]) {
1433
							$value = $row[$fieldname];
1434
						} elseif ($rowhelper['value'] != "") {
1435
							$value = $rowhelper['value'];
1436
						}
1437

    
1438
						$type = $rowhelper['type'];
1439
						if ($type == "input" || $type == "password" || $type == "textarea") {
1440
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1441
								$value = base64_decode($value);
1442
							}
1443
						}
1444

    
1445

    
1446
						if ($rowhelper['size']) {
1447
							$size = $rowhelper['size'];
1448
						} else if ($pkga['size']) {
1449
							$size = $pkga['size'];
1450
						} else {
1451
							$size = "8";
1452
						}
1453

    
1454
						if ($rowhelper['width']) {
1455
							$width = $rowhelper['width'];
1456
						}
1457

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

    
1460
						$text = "";
1461
						$trc++;
1462
					}
1463

    
1464
					// Delete row button
1465
					$group->add(new Form_Button(
1466
						'deleterow' . $rowcounter,
1467
						'Delete',
1468
						null,
1469
						'fa-trash'
1470
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1471

    
1472
					$rowcounter++;
1473
					$section->add($group);
1474
				}
1475

    
1476
			// Add row button
1477
			$section->addInput(new Form_Button(
1478
				'addrow',
1479
				'Add',
1480
				null,
1481
				'fa-plus'
1482
			))->addClass('btn-success');
1483

    
1484
			break;
1485

    
1486
	}
1487

    
1488
		if ($pkga['combinefields'] == "end") {
1489
			$group->add(new Form_StaticText(
1490
				null,
1491
				null
1492
			));
1493

    
1494
			if ($advanced) {
1495
				$advanced->add($group);
1496
			} else {
1497
				$section->add($group);
1498
			}
1499

    
1500
			$grouping = false;
1501
		}
1502

    
1503
	#increment counter
1504
	$i++;
1505
} // e-o-foreach field described in the XML
1506

    
1507
if ($section) {
1508
	$form->add($section);
1509
}
1510

    
1511
$form->addGlobal(new Form_Input(
1512
	'id',
1513
	null,
1514
	'hidden',
1515
	$id
1516
));
1517

    
1518
// If we created an advanced section, add it (and a button) to the form here
1519
if (!empty($advanced)) {
1520
	$form->addGlobal(new Form_Button(
1521
		'showadv',
1522
		'Show Advanced Options',
1523
		null,
1524
		'fa-cog'
1525
	))->setAttribute('type','button')->addClass('btn-info');
1526

    
1527
	$form->add($advanced);
1528
}
1529

    
1530
print($form);
1531

    
1532
if ($pkg['note'] != "") {
1533
	print_info_box($pkg['note'], 'info');
1534
}
1535

    
1536
if ($pkg['custom_php_after_form_command']) {
1537
	eval($pkg['custom_php_after_form_command']);
1538
}
1539

    
1540

    
1541
$hidemsg = gettext("Show Advanced Options");
1542
$showmsg = gettext("Hide Advanced Options");
1543

    
1544
if ($pkg['fields']['field'] != "") { ?>
1545
<script type="text/javascript">
1546
//<![CDATA[
1547
	events.push(function() {
1548

    
1549
	// Hide the advanced section
1550
	var advanced_visible = false;
1551

    
1552
	// Hide on page load
1553
	$('.advancedoptions').hide();
1554

    
1555
	// Suppress "Delete row" button if there are fewer than two rows
1556
	checkLastRow();
1557

    
1558
	// Show advanced section if you click the showadv button
1559
	$("#showadv").click(function() {
1560
		advanced_visible = !advanced_visible;
1561

    
1562
		if (advanced_visible) {
1563
			$('.advancedoptions').show();
1564
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$showmsg?>");
1565
		} else {
1566
			$('.advancedoptions').hide();
1567
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$hidemsg?>");
1568
		}
1569
	});
1570

    
1571
	// Call enablechange function
1572
	enablechange();
1573
});
1574

    
1575
	function enablechange() {
1576
<?php
1577
	foreach ($pkg['fields']['field'] as $field) {
1578
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1579
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1580

    
1581
			if (isset($field['enablefields'])) {
1582
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1583
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1584
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1585
					echo "\t\t}\n";
1586
				}
1587
			}
1588

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

    
1597
			echo "\t}\n\telse {\n";
1598

    
1599
			if (isset($field['enablefields'])) {
1600
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1601
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1602
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1603
					echo "\t\t}\n";
1604
				}
1605
			}
1606

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

    
1615
			echo "\t}\n";
1616
		}
1617
	}
1618
	?>
1619
	}
1620
//]]>
1621
</script>
1622

    
1623
<?php
1624
}
1625

    
1626
include("foot.inc");
(103-103/235)