Project

General

Profile

Download (41.7 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-2018 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

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

    
602
// Start of page display
603
if ($input_errors) {
604
	print_input_errors($input_errors);
605
}
606

    
607
if ($savemsg) {
608
	print_info_box($savemsg, 'success');
609
}
610

    
611
$cols = 0;
612
$savevalue = gettext("Save");
613
if ($pkg['savetext'] != "") {
614
	$savevalue = $pkg['savetext'];
615
}
616

    
617
$savehelp = "";
618
if ($pkg['savehelp'] != "") {
619
	$savehelp = $pkg['savehelp'];
620
}
621

    
622
$saveicon = "fa-save";
623
if ($pkg['saveicon'] != "") {
624
	$saveicon = $pkg['saveicon'];
625
}
626

    
627
$savebtnclass = "btn-primary";
628
if ($pkg['savebtnclass'] != "") {
629
	$savebtnclass = $pkg['savebtnclass'];
630
}
631

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

    
634
$savebutton = new Form_Button(
635
	'submit',
636
	$savevalue,
637
	null,
638
	$saveicon
639
);
640
$savebutton->addClass($savebtnclass);
641

    
642
if ($savehelp) {
643
	$savebutton->setHelp($savehelp);
644
}
645

    
646
$form = new Form($savebutton);
647

    
648
$form->addGlobal(new Form_Input(
649
	'xml',
650
	null,
651
	'hidden',
652
	$xml
653
));
654

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

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

    
667
$js_array = array();
668

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

    
675
	$action = "";
676
	$uid = "";
677

    
678
	if ($pkga['type'] == "sorting") {
679
		continue;
680
	}
681

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

    
690
			$advfield_count++;
691
		} else {
692
			if (isset($section)) {
693
				$form->add($section);
694
			}
695

    
696
			if (isset($pkga['collapse'])) {
697
				$uid = uniqid("section");
698

    
699
				$action = COLLAPSIBLE;
700

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

    
708
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
709
		}
710

    
711
		continue;
712
	}
713

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

    
720
	$size = "";
721
	$colspan="";
722

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

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

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

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

    
761
			$grp = new Form_Input(
762
					$pkga['fieldname'],
763
					$pkga['fielddescr'],
764
					'text',
765
					$value
766
				);
767

    
768
			$grp->setHelp($pkga['description']);
769

    
770
			if ($pkga['width']) {
771
				$grp->setWidth($pkga['width']);
772
			}
773

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

    
784
			break;
785

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

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

    
817
			break;
818

    
819
		case "info":
820
			// If the info contains a table we should detect and Bootstrap it
821

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

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

    
840
			break;
841

    
842
		case "select":
843
			// Create a select element
844
			$optionlist = array();
845
			$selectedlist = array();
846

    
847
			$fieldname = $pkga['fieldname'];
848

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

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

    
860
			foreach ($pkga['options']['option'] as $opt) {
861
				$optionlist[$opt['value']] = $opt['name'];
862

    
863
				if (in_array($opt['value'], $items)) {
864
					array_push($selectedlist, $opt['value']);
865
				}
866
			}
867

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

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

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

    
884
			if ($pkga['width']) {
885
				$grp->setWidth($pkga['width']);
886
			}
887

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

    
898
			break;
899

    
900
		case "select_source":
901

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

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

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

    
926
			$srcoptions = array();
927
			$srcselected = array();
928

    
929
			foreach ($pkg_source_txt as $opt) {
930
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
931
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
932
				$srcoptions[$source_value] = $source_name;
933

    
934
				if (in_array($source_value, $items)) {
935
					array_push($srcselected, $source_value);
936
				}
937
			}
938

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

    
968
			break;
969

    
970
		case "vpn_selection" :
971
			$vpnlist = array();
972

    
973
			foreach ($config['ipsec']['phase1'] as $vpn) {
974
				$vpnlist[$vpn['descr']] = $vpn['descr'];
975

    
976
			}
977

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

    
1003
			break;
1004

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

    
1014
			if ($grouping) {
1015
				$group->add(new Form_Checkbox(
1016
					$pkga['fieldname'],
1017
					$pkga['fielddescr'],
1018
					fixup_string($pkga['description']),
1019
					($value == "on"),
1020
					'on'
1021
				))->setOnclick($onclick)
1022
				  ->setOnchange($onchange)
1023
				  ->setHelp($pkga['sethelp']);
1024
			} else {
1025
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1026
					$advanced->addInput(new Form_Checkbox(
1027
						$pkga['fieldname'],
1028
						$pkga['fielddescr'],
1029
						fixup_string($pkga['description']),
1030
						($value == "on"),
1031
						'on'
1032
					))->setOnclick($onclick)
1033
					  ->setOnchange($onchange)
1034
					  ->setHelp($pkga['sethelp']);
1035
				} else {
1036
					$section->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
				}
1046
			}
1047

    
1048
			break;
1049

    
1050
		// Create a textarea element
1051
		case "textarea":
1052
			$rows = $cols = 0;
1053

    
1054
			if ($pkga['rows']) {
1055
				$rows = $pkga['rows'];
1056
			}
1057
			if ($pkga['cols']) {
1058
				$cols = $pkga['cols'];
1059
			}
1060

    
1061
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1062
				$value = base64_decode($value);
1063
			}
1064

    
1065
			$grp = new Form_Textarea(
1066
					$pkga['fieldname'],
1067
					$pkga['fielddescr'],
1068
					$value
1069
			);
1070

    
1071
			$grp->setHelp(fixup_string($pkga['description']));
1072

    
1073
			if ($rows > 0) {
1074
				$grp->setRows($rows);
1075
			}
1076

    
1077
			if ($cols > 0) {
1078
				$grp->setCols($cols);
1079
			}
1080

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

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

    
1098
			break;
1099

    
1100
		case "aliases":
1101

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

    
1106
			init_config_arr(array('aliases', 'alias'));
1107
			$a_aliases = &$config['aliases']['alias'];
1108
			$addrisfirst = 0;
1109
			$aliasesaddr = "";
1110

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

    
1133
			$grp = new Form_Input(
1134
					$pkga['fieldname'],
1135
					$pkga['fielddescr'],
1136
					'text',
1137
					$value
1138
				);
1139

    
1140
			$grp->setHelp($pkga['description']);
1141

    
1142
			if ($pkga['width']) {
1143
				$grp->setWidth($pkga['width']);
1144
			}
1145

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

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

    
1167
			echo $script;
1168

    
1169
			break;
1170

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

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

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

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

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

    
1232
			$selectedlist = array();
1233
			$optionlist = array();
1234

    
1235
			if (is_array($value)) {
1236
				$values = $value;
1237
			} else {
1238
				$values = explode(',', $value);
1239
			}
1240

    
1241
			foreach ($ips as $iface) {
1242
				if (in_array($iface['ip'], $values)) {
1243
					array_push($selectedlist, $iface['ip']);
1244
				}
1245

    
1246
				$optionlist[$iface['ip']] = $iface['description'];
1247
			}
1248

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

    
1277
			break;
1278

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

    
1309
			break;
1310

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

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

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

    
1349
			break;
1350

    
1351
		case "schedule_selection":
1352

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

    
1364
			foreach ($schedules as $schedule) {
1365
				if ($schedule == "none") {
1366
					$schedlist[""] = $schedule;
1367
				} else {
1368
					$schedlist[$schedule] = $schedule;
1369
				}
1370
			}
1371

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

    
1397
			break;
1398

    
1399
		case "rowhelper":
1400

    
1401
			$rowhelpername="row";
1402

    
1403
				$rowcounter = 0;
1404
				$trc = 0;
1405

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

    
1413
				$numrows = count($saved_rows) - 1;
1414

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

    
1419
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1420
						unset($value);
1421
						$width = null;
1422

    
1423
						$fieldname = $rowhelper['fieldname'];
1424
						$fielddescr = $rowhelper['fielddescr'];
1425

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

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

    
1442

    
1443
						if ($rowhelper['size']) {
1444
							$size = $rowhelper['size'];
1445
						} else if ($pkga['size']) {
1446
							$size = $pkga['size'];
1447
						} else {
1448
							$size = "8";
1449
						}
1450

    
1451
						if ($rowhelper['width']) {
1452
							$width = $rowhelper['width'];
1453
						}
1454

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

    
1457
						$text = "";
1458
						$trc++;
1459
					}
1460

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

    
1469
					$rowcounter++;
1470
					$section->add($group);
1471
				}
1472

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

    
1481
			break;
1482

    
1483
	}
1484

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

    
1491
			if ($advanced) {
1492
				$advanced->add($group);
1493
			} else {
1494
				$section->add($group);
1495
			}
1496

    
1497
			$grouping = false;
1498
		}
1499

    
1500
	#increment counter
1501
	$i++;
1502
} // e-o-foreach field described in the XML
1503

    
1504
if ($section) {
1505
	$form->add($section);
1506
}
1507

    
1508
$form->addGlobal(new Form_Input(
1509
	'id',
1510
	null,
1511
	'hidden',
1512
	$id
1513
));
1514

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

    
1524
	$form->add($advanced);
1525
}
1526

    
1527
print($form);
1528

    
1529
if ($pkg['note'] != "") {
1530
	print_info_box($pkg['note'], 'info');
1531
}
1532

    
1533
if ($pkg['custom_php_after_form_command']) {
1534
	eval($pkg['custom_php_after_form_command']);
1535
}
1536

    
1537

    
1538
$hidemsg = gettext("Show Advanced Options");
1539
$showmsg = gettext("Hide Advanced Options");
1540

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

    
1546
	// Hide the advanced section
1547
	var advanced_visible = false;
1548

    
1549
	// Hide on page load
1550
	$('.advancedoptions').hide();
1551

    
1552
	// Suppress "Delete row" button if there are fewer than two rows
1553
	checkLastRow();
1554

    
1555
	// Show advanced section if you click the showadv button
1556
	$("#showadv").click(function() {
1557
		advanced_visible = !advanced_visible;
1558

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

    
1568
	// Call enablechange function
1569
	enablechange();
1570
});
1571

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

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

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

    
1594
			echo "\t}\n\telse {\n";
1595

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

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

    
1612
			echo "\t}\n";
1613
		}
1614
	}
1615
	?>
1616
	}
1617
//]]>
1618
</script>
1619

    
1620
<?php
1621
}
1622

    
1623
include("foot.inc");
(102-102/234)