Project

General

Profile

Download (39.9 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-2016 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("guiconfig.inc");
32
require_once("functions.inc");
33
require_once("filter.inc");
34
require_once("shaper.inc");
35
require_once("pkg-utils.inc");
36

    
37
/* dummy stubs needed by some code that was MFC'd */
38
function pfSenseHeader($location) {
39
	header("Location: " . $location);
40
}
41

    
42
$xml = htmlspecialchars($_GET['xml']);
43
if ($_POST['xml']) {
44
	$xml = htmlspecialchars($_POST['xml']);
45
}
46

    
47
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
48

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

    
59
if ($pkg['include_file'] != "") {
60
	require_once($pkg['include_file']);
61
}
62

    
63
if (!isset($pkg['adddeleteeditpagefields'])) {
64
	$only_edit = true;
65
} else {
66
	$only_edit = false;
67
}
68

    
69
$id = $_GET['id'];
70
if (isset($_POST['id'])) {
71
	$id = htmlspecialchars($_POST['id']);
72
}
73

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

    
80
if (!is_numeric($id)) {
81
	header("Location: /");
82
	exit;
83
}
84

    
85
if ($pkg['custom_php_global_functions'] != "") {
86
	eval($pkg['custom_php_global_functions']);
87
}
88

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

    
94
// If the first entry in the array is an empty <config/> tag, kill it.
95
if ($config['installedpackages'] &&
96
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
97
    ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == "")) {
98
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
99
}
100

    
101
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
102

    
103
if ($_GET['savemsg'] != "") {
104
	$savemsg = htmlspecialchars($_GET['savemsg']);
105
}
106

    
107
if ($pkg['custom_php_command_before_form'] != "") {
108
	eval($pkg['custom_php_command_before_form']);
109
}
110

    
111
if ($_POST) {
112
	$rows = 0;
113

    
114
	$input_errors = array();
115
	$reqfields = array();
116
	$reqfieldsn = array();
117
	foreach ($pkg['fields']['field'] as $field) {
118
		if (isset($field['required'])) {
119
			if ($field['fieldname']) {
120
				$reqfields[] = $field['fieldname'];
121
			}
122
			if ($field['fielddescr']) {
123
				$reqfieldsn[] = $field['fielddescr'];
124
			}
125
		}
126
	}
127
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
128

    
129
	if ($pkg['custom_php_validation_command']) {
130
		eval($pkg['custom_php_validation_command']);
131
	}
132

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

    
154
			if ($pkg['preoutput']) {
155
				echo "<pre>";
156
			}
157
			eval($pkg['custom_add_php_command']);
158
			if ($pkg['preoutput']) {
159
				echo "</pre>";
160
			}
161
		}
162
	}
163

    
164
	// donotsave is enabled.  lets simply exit.
165
	if (empty($pkg['donotsave'])) {
166

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

    
203
			if (isset($id) && $a_pkg[$id]) {
204
				$a_pkg[$id] = $pkgarr;
205
			} else {
206
				$a_pkg[] = $pkgarr;
207
			}
208

    
209
			write_config($pkg['addedit_string']);
210
			// late running code
211
			if ($pkg['custom_add_php_command_late'] != "") {
212
				eval($pkg['custom_add_php_command_late']);
213
			}
214

    
215
			if (isset($pkg['filter_rules_needed'])) {
216
				filter_configure();
217
			}
218

    
219
			// resync the configuration file code if defined.
220
			if ($pkg['custom_php_resync_config_command'] != "") {
221
				eval($pkg['custom_php_resync_config_command']);
222
			}
223

    
224
			parse_package_templates();
225

    
226
			/* if start_command is defined, restart w/ this */
227
			if ($pkg['start_command'] != "") {
228
				exec($pkg['start_command'] . ">/dev/null 2&>1");
229
			}
230

    
231
			/* if restart_command is defined, restart w/ this */
232
			if ($pkg['restart_command'] != "") {
233
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
234
			}
235

    
236
			if ($pkg['aftersaveredirect'] != "") {
237
				pfSenseHeader($pkg['aftersaveredirect']);
238
			} elseif (!$pkg['adddeleteeditpagefields']) {
239
				pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
240
			} elseif (!$pkg['preoutput']) {
241
				pfSenseHeader("pkg.php?xml=" . $xml);
242
			}
243
			exit;
244
		} else {
245
			$get_from_post = true;
246
		}
247
	} elseif (!$input_errors) {
248
		exit;
249
	}
250
}
251

    
252

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

    
259
	// Substitute everything inbetween with our new classes
260
	if ($t && $c && (($c - $t) < 200)) {
261
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
262
	}
263
}
264

    
265
/*
266
 * ROW helper function. Creates one element in the row from a PHP table by adding
267
 * the specified element to $group
268
 */
269
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description, $ewidth = null) {
270
	global $text, $group, $config;
271

    
272
	switch ($type) {
273
		case "input":
274
			$inpt = new Form_Input(
275
				$fieldname . $trc,
276
				null,
277
				'text',
278
				$value
279
			);
280

    
281
			$inpt->setHelp($description);
282

    
283
			if ($ewidth) {
284
				$inpt->setWidth($ewidth);
285
			}
286

    
287
			$group->add($inpt);
288
			break;
289
		case "checkbox":
290
			$group->add(new Form_Checkbox(
291
				$fieldname . $trc,
292
				null,
293
				null,
294
				$value,
295
				'ON'
296
			))->setHelp($description);
297

    
298
			break;
299
		case "password":
300
			$group->add(new Form_Input(
301
				$fieldname . $trc,
302
				null,
303
				'password',
304
				$value
305
			))->setHelp($description);
306
			break;
307
		case "textarea":
308
			$group->add(new Form_Textarea(
309
				$fieldname . $trc,
310
				null,
311
				$value
312
			))->setHelp($description);
313

    
314
			break;
315
		case "select":
316
			$options = array();
317
			foreach ($rowhelper['options']['option'] as $rowopt) {
318
				$options[$rowopt['value']] = $rowopt['name'];
319
			}
320

    
321
			$grp = new Form_Select(
322
				$fieldname . $trc,
323
				null,
324
				$value,
325
				$options
326
			);
327

    
328
			$grp->setHelp($description);
329

    
330
			if ($ewidth) {
331
				$grp->setWidth($ewidth);
332
			}
333

    
334
			$group->add($grp);
335

    
336
			break;
337
		case "interfaces_selection":
338
			$size = ($size ? "size=\"{$size}\"" : '');
339
			$multiple = '';
340
			if (isset($rowhelper['multiple'])) {
341
				$multiple = "multiple";
342
			}
343
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
344
			$ifaces = get_configured_interface_with_descr();
345
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
346
			if (!empty($additional_ifaces)) {
347
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
348
			}
349

    
350
			if (is_array($value)) {
351
				$values = $value;
352
			} else {
353
				$values = explode(',', $value);
354
			}
355

    
356
			$ifaces["lo0"] = "loopback";
357
			$options = array();
358
			$selected = array();
359

    
360
			foreach ($ifaces as $ifname => $iface) {
361
				$options[$ifname] = $iface;
362

    
363
				if (in_array($ifname, $values)) {
364
					array_push($selected, $ifname);
365
				}
366
			}
367

    
368
			$group->add(new Form_Select(
369
				$fieldname . $trc,
370
				null,
371
				($multiple) ? $selected:$selected[0],
372
				$options,
373
				$multiple
374
			))->setHelp($description);
375

    
376
			//echo "</select>\n";
377
			break;
378
		case "select_source":
379
			$options = array();
380
			$selected = array();
381

    
382
			if (isset($rowhelper['show_disable_value'])) {
383
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
384
			}
385

    
386
			$source_url = $rowhelper['source'];
387
			eval("\$pkg_source_txt = &$source_url;");
388

    
389
			foreach ($pkg_source_txt as $opt) {
390
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
391
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
392
				$options[$source_value] = $source_name;
393

    
394
				if ($source_value == $value) {
395
					array_push($selected, $value);
396
				}
397
			}
398

    
399
			$group->add(new Form_Select(
400
				$fieldname . $trc,
401
				null,
402
				($multiple) ? $selected:$selected[0],
403
				$options,
404
				$multiple
405
			))->setHelp($description);
406

    
407
			break;
408
	}
409
}
410

    
411
function fixup_string($string) {
412
	global $config;
413
	// fixup #1: $myurl -> http[s]://ip_address:port/
414
	$https = "";
415
	$port = $config['system']['webguiport'];
416
	if ($port != "443" and $port != "80") {
417
		$urlport = ":" . $port;
418
	} else {
419
		$urlport = "";
420
	}
421

    
422
	if ($config['system']['webgui']['protocol'] == "https") {
423
		$https = "s";
424
	}
425
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
426
	$newstring = str_replace("\$myurl", $myurl, $string);
427
	$string = $newstring;
428
	// fixup #2: $wanip
429
	$curwanip = get_interface_ip();
430
	$newstring = str_replace("\$wanip", $curwanip, $string);
431
	$string = $newstring;
432
	// fixup #3: $lanip
433
	$lancfg = $config['interfaces']['lan'];
434
	$lanip = $lancfg['ipaddr'];
435
	$newstring = str_replace("\$lanip", $lanip, $string);
436
	$string = $newstring;
437
	// fixup #4: fix'r'up here.
438
	return $newstring;
439
}
440

    
441
/*
442
 *	Parse templates if they are defined
443
 */
444
function parse_package_templates() {
445
	global $pkg;
446
	if ($pkg['templates']['template'] != "") {
447
		foreach ($pkg['templates']['template'] as $pkg_template_row) {
448
			$filename = $pkg_template_row['filename'];
449
			$template_text = $pkg_template_row['templatecontents'];
450
			/* calculate total row helpers count and */
451
			/* change fields defined as fieldname_fieldvalue to their value */
452
			foreach ($pkg['fields']['field'] as $fields) {
453
				switch ($fields['type']) {
454
					case "rowhelper":
455
						// save rowhelper items.
456
						$row_helper_total_rows = 0;
457
						$row_helper_data = "";
458
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
459
							foreach ($_POST as $key => $value) {
460
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
461
									$row_helper_total_rows++;
462
									$row_helper_data .= $value;
463
									$sep = "";
464
									ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
465
									foreach ($sep as $se) {
466
										$separator = $se;
467
									}
468
									if ($separator != "") {
469
										$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
470
										$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
471
									}
472
									$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
473
								}
474
							}
475
						}
476
						break;
477
					default:
478
						$fieldname = $fields['fieldname'];
479
						$fieldvalue = $_POST[$fieldname];
480
						$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
481
				}
482
			}
483
			/* replace $domain_total_rows with total rows */
484
			$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
485

    
486
			/* replace cr's */
487
			$template_text = str_replace("\\n", "\n", $template_text);
488

    
489
			/* write out new template file */
490
			$fout = fopen($filename, "w");
491
			fwrite($fout, $template_text);
492
			fclose($fout);
493
		}
494
	}
495
}
496

    
497
//breadcrumb
498
if ($pkg['title'] != "") {
499
	if (!$only_edit) {
500
		$pkg['title'] = $pkg['title'] . '/Edit';
501
	}
502

    
503
	if (strpos($pkg['title'], '/')) {
504
		$title = explode('/', $pkg['title']);
505

    
506
		foreach ($title as $subtitle) {
507
			$pgtitle[] = gettext($subtitle);
508
		}
509
	} else {
510
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
511
	}
512
} else {
513
	$pgtitle = array(gettext("Package"), gettext("Editor"));
514
}
515

    
516
// Create any required tabs
517
if ($pkg['tabs'] != "") {
518
	$tab_array = array();
519
	foreach ($pkg['tabs']['tab'] as $tab) {
520
		if ($tab['tab_level']) {
521
			$tab_level = $tab['tab_level'];
522
		} else {
523
			$tab_level = 1;
524
		}
525

    
526
		if (isset($tab['active'])) {
527
			$active = true;
528
			$pgtitle[] = $tab['text'] ;
529
		} else {
530
			$active = false;
531
		}
532

    
533
		if (isset($tab['no_drop_down'])) {
534
			$no_drop_down = true;
535
		}
536

    
537
		$urltmp = "";
538
		if ($tab['url'] != "") {
539
			$urltmp = $tab['url'];
540
		}
541

    
542
		if ($tab['xml'] != "") {
543
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
544
		}
545

    
546
		$addresswithport = getenv("HTTP_HOST");
547
		$colonpos = strpos($addresswithport, ":");
548

    
549
		if ($colonpos !== False) {
550
			//my url is actually just the IP address of the pfsense box
551
			$myurl = substr($addresswithport, 0, $colonpos);
552
		} else {
553
			$myurl = $addresswithport;
554
		}
555

    
556
		// eval url so that above $myurl item can be processed if need be.
557
		$url = str_replace('$myurl', $myurl, $urltmp);
558

    
559
		$tab_array[$tab_level][] = array(
560
			$tab['text'],
561
			$active,
562
			$url
563
		);
564
	}
565

    
566
	ksort($tab_array);
567
}
568

    
569
include("head.inc");
570
if ($pkg['custom_php_after_head_command']) {
571
	eval($pkg['custom_php_after_head_command']);
572
}
573
if (isset($tab_array)) {
574
	foreach ($tab_array as $tabid => $tab) {
575
		display_top_tabs($tab); //, $no_drop_down, $tabid);
576
	}
577
}
578

    
579
// Start of page display
580
if ($input_errors) {
581
	print_input_errors($input_errors);
582
}
583

    
584
if ($savemsg) {
585
	print_info_box($savemsg, 'success');
586
}
587

    
588
$cols = 0;
589
$savevalue = gettext("Save");
590
if ($pkg['savetext'] != "") {
591
	$savevalue = $pkg['savetext'];
592
}
593

    
594
$savehelp = "";
595
if ($pkg['savehelp'] != "") {
596
	$savehelp = $pkg['savehelp'];
597
}
598

    
599
$saveicon = "fa-save";
600
if ($pkg['saveicon'] != "") {
601
	$saveicon = $pkg['saveicon'];
602
}
603

    
604
$savebtnclass = "btn-primary";
605
if ($pkg['savebtnclass'] != "") {
606
	$savebtnclass = $pkg['savebtnclass'];
607
}
608

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

    
611
$savebutton = new Form_Button(
612
	'submit',
613
	$savevalue,
614
	null,
615
	$saveicon
616
);
617
$savebutton->addClass($savebtnclass);
618

    
619
if ($savehelp) {
620
	$savebutton->setHelp($savehelp);
621
}
622

    
623
$form = new Form($savebutton);
624

    
625
$form->addGlobal(new Form_Input(
626
	'xml',
627
	null,
628
	'hidden',
629
	$xml
630
));
631

    
632
/* If a package's XML has <advanced_options/> configured, then setup
633
 * the section for the fields that have <advancedfield/> set.
634
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
635
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
636
 */
637

    
638
if ($pkg['advanced_options'] == "enabled") {
639
	$advfield_count = 0;
640
	$advanced = new Form_Section("Advanced Features");
641
	$advanced->addClass('advancedoptions');
642
}
643

    
644
$js_array = array();
645

    
646
// Now loop through all of the fields defined in the XML
647
if (!is_array($pkg['fields']['field'])) {
648
	$pkg['fields']['field'] = array();
649
}
650
foreach ($pkg['fields']['field'] as $pkga) {
651

    
652
	$action = "";
653
	$uid = "";
654

    
655
	if ($pkga['type'] == "sorting") {
656
		continue;
657
	}
658

    
659
	// Generate a new section
660
	if ($pkga['type'] == "listtopic") {
661
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
662
			$advanced->addInput(new Form_StaticText(
663
				strip_tags($pkga['name']),
664
				null
665
			));
666

    
667
			$advfield_count++;
668
		}  else {
669
			if (isset($section)) {
670
				$form->add($section);
671
			}
672

    
673
			if (isset($pkga['collapse'])) {
674
				$uid = uniqid("section");
675

    
676
				$action = COLLAPSIBLE;
677

    
678
				if ($pkga['collapse'] == "open") {
679
					$action |= SEC_OPEN;
680
				} else {
681
					$action |= SEC_CLOSED;
682
				}
683
			}
684

    
685
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
686
		}
687

    
688
		continue;
689
	}
690

    
691
	// 'begin' starts a form group. ('end' ends it)
692
	if ($pkga['combinefields'] == "begin") {
693
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
694
		$grouping = true;
695
	}
696

    
697
	$size = "";
698
	$colspan="";
699

    
700
	// if user is editing a record, load in the data.
701
	$fieldname = $pkga['fieldname'];
702
	unset($value);
703
	if ($get_from_post) {
704
		$value = $_POST[$fieldname];
705
		if (is_array($value)) {
706
			$value = implode(',', $value);
707
		}
708
	} else {
709
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
710
			$value = $a_pkg[$id][$fieldname];
711
		} else {
712
			if (isset($pkga['default_value'])) {
713
				$value = $pkga['default_value'];
714
			}
715
		}
716
	}
717

    
718
	// If we get here but have no $section, the package config file probably had no listtopic field
719
	// We can create a section with a generic name to fix that
720
	if (!$section) {
721
		$section = new Form_Section('General Options');
722
	}
723

    
724
	switch ($pkga['type']) {
725
		// Create an input element. The format is slightly different depending on whether we are composing a group,
726
		// section, or advanced section. This is true for every element type
727
		case "input":
728
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
729
				$value = base64_decode($value);
730
			}
731

    
732
			$grp = new Form_Input(
733
					$pkga['fieldname'],
734
					$pkga['fielddescr'],
735
					'text',
736
					$value
737
				);
738

    
739
			$grp->setHelp($pkga['description']);
740

    
741
			if ($pkga['width']) {
742
				$grp->setWidth($pkga['width']);
743
			}
744

    
745
			if ($grouping) {
746
				$group->add($grp);
747
			} else {
748
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
749
					$advanced->addInput($grp);
750
				} else {
751
					$section->addInput($grp);
752
				}
753
			}
754

    
755
			break;
756

    
757
		case "password":
758
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
759
				$value = base64_decode($value);
760
			}
761

    
762
			// Create a password element
763
			if ($grouping) {
764
				$group->add(new Form_Input(
765
					$pkga['fieldname'],
766
					$pkga['fielddescr'],
767
					'password',
768
					$value
769
				))->setHelp($pkga['description']);
770
			} else {
771
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
772
					$advanced->addInput(new Form_Input(
773
						$pkga['fieldname'],
774
						$pkga['fielddescr'],
775
						'password',
776
						$value
777
					))->setHelp($pkga['description']);
778
				} else {
779
					$section->addInput(new Form_Input(
780
						$pkga['fieldname'],
781
						$pkga['fielddescr'],
782
						'password',
783
						$value
784
					))->setHelp($pkga['description']);
785
				}
786
			}
787

    
788
			break;
789

    
790
		case "info":
791
			// If the info contains a table we should detect and Bootstrap it
792

    
793
			if (strpos($pkga['description'], '<table') !== FALSE) {
794
				$info = bootstrapTable($pkga['description']);
795
			} else {
796
				$info = $pkga['description'];
797
			}
798

    
799
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
800
				$advanced->addInput(new Form_StaticText(
801
					strip_tags($pkga['fielddescr']),
802
					$info
803
				));
804
			} else {
805
				$section->addInput(new Form_StaticText(
806
					strip_tags($pkga['fielddescr']),
807
					$info
808
				));
809
			}
810

    
811
			break;
812

    
813
		case "select":
814
			// Create a select element
815
			$optionlist = array();
816
			$selectedlist = array();
817

    
818
			$fieldname = $pkga['fieldname'];
819

    
820
			if (isset($pkga['multiple'])) {
821
				$multiple = 'multiple';
822
				$items = explode(',', $value);
823
				$fieldname .= "[]";
824
			} else {
825
				$multiple = '';
826
				$items = array($value);
827
			}
828

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

    
831
			foreach ($pkga['options']['option'] as $opt) {
832
				$optionlist[$opt['value']] = $opt['name'];
833

    
834
				if (in_array($opt['value'], $items)) {
835
					array_push($selectedlist, $opt['value']);
836
				}
837
			}
838

    
839
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
840
				$function = $grouping ? $advanced->add:$advanced->addInput;
841
			} else {
842
				$function = ($grouping) ? $section->add:$section->addInput;
843
			}
844

    
845
			$grp = new Form_Select(
846
						$pkga['fieldname'],
847
						strip_tags($pkga['fielddescr']),
848
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
849
						$optionlist,
850
						isset($pkga['multiple'])
851
					);
852

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

    
855
			if ($pkga['width']) {
856
				$grp->setWidth($pkga['width']);
857
			}
858

    
859
			if ($grouping) {
860
				$group->add($grp);
861
			} else {
862
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
863
					$advanced->addInput($grp);
864
				} else {
865
					$section->addInput($grp);
866
				}
867
			}
868

    
869
			break;
870

    
871
		case "select_source":
872

    
873
			if (isset($pkga['multiple'])) {
874
				$items = explode(',', $value);
875
				$fieldname .= "[]";
876
			} else {
877
				$items = array($value);
878
			}
879

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

    
882
			$source_url = $pkga['source'];
883
			eval("\$pkg_source_txt = &$source_url;");
884

    
885
			#check if show disable option is present on xml
886
			if (!is_array($pkg_source_txt)) {
887
				$pkg_source_txt = array();
888
			}
889
			if (isset($pkga['show_disable_value'])) {
890
				array_push($pkg_source_txt,
891
					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']));
892
			}
893

    
894
			$srcoptions = array();
895
			$srcselected = array();
896

    
897
			foreach ($pkg_source_txt as $opt) {
898
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
899
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
900
				$srcoptions[$source_value] = $source_name;
901

    
902
				if (in_array($source_value, $items)) {
903
					array_push($srcselected, $source_value);
904
				}
905
			}
906

    
907
			$descr = (isset($pkga['description'])) ? $pkga['description'] : "";
908
			if ($grouping) {
909
				$group->add(new Form_Select(
910
					$pkga['fieldname'],
911
					strip_tags($pkga['fielddescr']),
912
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
913
					$srcoptions,
914
					isset($pkga['multiple'])
915
				))->setHelp($descr)->setOnchange($onchange);
916
			} else {
917
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
918
					$advanced->addInput(new Form_Select(
919
						$pkga['fieldname'],
920
						strip_tags($pkga['fielddescr']),
921
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
922
						$srcoptions,
923
						isset($pkga['multiple'])
924
					))->setHelp($descr)->setOnchange($onchange);
925
				} else {
926
					$section->addInput(new Form_Select(
927
						$pkga['fieldname'],
928
						strip_tags($pkga['fielddescr']),
929
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
930
						$srcoptions,
931
						isset($pkga['multiple'])
932
					))->setHelp($descr)->setOnchange($onchange);
933
				}
934
			}
935

    
936
			break;
937

    
938
		case "vpn_selection" :
939
			$vpnlist = array();
940

    
941
			foreach ($config['ipsec']['phase1'] as $vpn) {
942
				$vpnlist[$vpn['descr']] = $vpn['descr'];
943

    
944
			}
945

    
946
			if ($grouping) {
947
				$group->add(new Form_Select(
948
					$pkga['fieldname'],
949
					null,
950
					false,
951
					$vpnlist
952
				))->setHelp(fixup_string($pkga['description']));
953
			} else {
954
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
955
					$advanced->addInput(new Form_Select(
956
						$pkga['fieldname'],
957
						null,
958
						false,
959
						$vpnlist
960
					))->setHelp(fixup_string($pkga['description']));
961
				} else {
962
					$section->addInput(new Form_Select(
963
						$pkga['fieldname'],
964
						null,
965
						false,
966
						$vpnlist
967
					))->setHelp(fixup_string($pkga['description']));
968
				}
969
			}
970

    
971
			break;
972

    
973
		// Create a checkbox element
974
		case "checkbox":
975
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
976
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
977
				$onclick = 'javascript:enablechange();';
978
			} else {
979
				$onclick = '';
980
			}
981

    
982
			if ($grouping) {
983
				$group->add(new Form_Checkbox(
984
					$pkga['fieldname'],
985
					$pkga['fielddescr'],
986
					fixup_string($pkga['description']),
987
					($value == "on"),
988
					'on'
989
				))->setOnclick($onclick)
990
				  ->setOnchange($onchange)
991
				  ->setHelp($pkga['sethelp']);
992
			} else {
993
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
994
					$advanced->addInput(new Form_Checkbox(
995
						$pkga['fieldname'],
996
						$pkga['fielddescr'],
997
						fixup_string($pkga['description']),
998
						($value == "on"),
999
						'on'
1000
					))->setOnclick($onclick)
1001
					  ->setOnchange($onchange)
1002
					  ->setHelp($pkga['sethelp']);
1003
				} else {
1004
					$section->addInput(new Form_Checkbox(
1005
						$pkga['fieldname'],
1006
						$pkga['fielddescr'],
1007
						fixup_string($pkga['description']),
1008
						($value == "on"),
1009
						'on'
1010
					))->setOnclick($onclick)
1011
					  ->setOnchange($onchange)
1012
					  ->setHelp($pkga['sethelp']);
1013
				}
1014
			}
1015

    
1016
			break;
1017

    
1018
		// Create a textarea element
1019
		case "textarea":
1020
			$rows = $cols = 0;
1021

    
1022
			if ($pkga['rows']) {
1023
				$rows = $pkga['rows'];
1024
			}
1025
			if ($pkga['cols']) {
1026
				$cols = $pkga['cols'];
1027
			}
1028

    
1029
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1030
				$value = base64_decode($value);
1031
			}
1032

    
1033
			$grp = new Form_Textarea(
1034
					$pkga['fieldname'],
1035
					$pkga['fielddescr'],
1036
					$value
1037
			);
1038

    
1039
			$grp->setHelp(fixup_string($pkga['description']));
1040

    
1041
			if ($rows > 0) {
1042
				$grp->setRows($rows);
1043
			}
1044

    
1045
			if ($cols > 0) {
1046
				$grp->setCols($cols);
1047
			}
1048

    
1049
			if ($pkga['wrap'] == "off") {
1050
				$grp->setAttribute("wrap", "off");
1051
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1052
			} else {
1053
				$grp->setAttribute("style", "width: auto;");
1054
			}
1055

    
1056
			if ($grouping) {
1057
				$group->add($grp);
1058
			} else {
1059
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1060
					$advanced->addInput($grp);
1061
				} else {
1062
					$section->addInput($grp);
1063
				}
1064
			}
1065

    
1066
			break;
1067

    
1068
		case "aliases":
1069

    
1070
			// Use xml tag <typealiases> to filter type aliases
1071
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1072
			$fieldname = $pkga['fieldname'];
1073
			$a_aliases = &$config['aliases']['alias'];
1074
			$addrisfirst = 0;
1075
			$aliasesaddr = "";
1076

    
1077
			if (isset($a_aliases)) {
1078
				if (!empty($pkga['typealiases'])) {
1079
					foreach ($a_aliases as $alias) {
1080
						if ($alias['type'] == $pkga['typealiases']) {
1081
							if ($addrisfirst == 1) {
1082
								$aliasesaddr .= ",";
1083
							}
1084
							$aliasesaddr .= "'" . $alias['name'] . "'";
1085
							$addrisfirst = 1;
1086
						}
1087
					}
1088
				} else {
1089
					foreach ($a_aliases as $alias) {
1090
						if ($addrisfirst == 1) {
1091
							$aliasesaddr .= ",";
1092
						}
1093
						$aliasesaddr .= "'" . $alias['name'] . "'";
1094
						$addrisfirst = 1;
1095
					}
1096
				}
1097
			}
1098

    
1099
			$grp = new Form_Input(
1100
					$pkga['fieldname'],
1101
					$pkga['fielddescr'],
1102
					'text',
1103
					$value
1104
				);
1105

    
1106
			$grp->setHelp($pkga['description']);
1107

    
1108
			if ($pkga['width']) {
1109
				$grp->setWidth($pkga['width']);
1110
			}
1111

    
1112
			if ($grouping) {
1113
				$group->add($grp);
1114
			} else {
1115
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1116
					$advanced->addInput($grp);
1117
				} else {
1118
					$section->addInput($grp);
1119
				}
1120
			}
1121

    
1122
			$script = "<script type='text/javascript'>\n";
1123
			$script .= "//<![CDATA[\n";
1124
			$script .= "events.push(function(){\n";
1125
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1126
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1127
			$script .= "		source: aliasarray\n";
1128
			$script .= "	})\n";
1129
			$script .= "});\n";
1130
			$script .= "//]]>\n";
1131
			$script .= "</script>";
1132

    
1133
			echo $script;
1134

    
1135
			break;
1136

    
1137
		case "interfaces_selection":
1138
			$ips = array();
1139
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1140
			if (is_array($config['interfaces'])) {
1141
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1142
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1143
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1144
						if (isset($pkga['showips'])) {
1145
							$iface_description .= " address";
1146
						}
1147
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1148
					}
1149
				}
1150
			}
1151

    
1152
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1153
				foreach ($config['virtualip']['vip'] as $vip) {
1154
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1155
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1156
					}
1157
					switch ($vip['mode']) {
1158
						case "ipalias":
1159
						case "carp":
1160
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1161
							break;
1162
						case "proxyarp":
1163
							if ($vip['type'] == "network") {
1164
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1165
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1166
								$len = $end - $start;
1167
								for ($i = 0; $i <= $len; $i++) {
1168
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1169
								}
1170
							} else {
1171
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1172
							}
1173
							break;
1174
					}
1175
				}
1176
			}
1177

    
1178
			sort($ips);
1179
			if (isset($pkga['showlistenall'])) {
1180
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1181
			}
1182

    
1183
			if (!preg_match("/$interface_regex/", "loopback")) {
1184
				$loopback_text = gettext("loopback");
1185
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1186
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1187
			}
1188

    
1189
			#show interfaces array on gui
1190
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1191
			$multiple = '';
1192
			$fieldname = $pkga['fieldname'];
1193
			if (isset($pkga['multiple'])) {
1194
				$fieldname .= '[]';
1195
				$multiple = 'multiple';
1196
			}
1197

    
1198
			$selectedlist = array();
1199
			$optionlist = array();
1200

    
1201
			if (is_array($value)) {
1202
				$values = $value;
1203
			} else {
1204
				$values = explode(',', $value);
1205
			}
1206

    
1207
			foreach ($ips as $iface) {
1208
				if (in_array($iface['ip'], $values)) {
1209
					array_push($selectedlist, $iface['ip']);
1210
				}
1211

    
1212
				$optionlist[$iface['ip']] = $iface['description'];
1213
			}
1214

    
1215
			if ($grouping) {
1216
				$group->add(new Form_Select(
1217
					$pkga['fieldname'],
1218
					$pkga['fielddescr'],
1219
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1220
					$optionlist,
1221
					isset($pkga['multiple'])
1222
				))->setHelp($pkga['description']);
1223
			} else {
1224
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1225
					$advanced->addInput(new Form_Select(
1226
						$pkga['fieldname'],
1227
						$pkga['fielddescr'],
1228
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1229
						$optionlist,
1230
						isset($pkga['multiple'])
1231
					))->setHelp($pkga['description']);
1232
				} else {
1233
					$section->addInput(new Form_Select(
1234
						$pkga['fieldname'],
1235
						$pkga['fielddescr'],
1236
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1237
						$optionlist,
1238
						isset($pkga['multiple'])
1239
					))->setHelp($pkga['description']);
1240
				}
1241
			}
1242

    
1243
			break;
1244

    
1245
		// Create radio button
1246
		case "radio":
1247
			if ($grouping) {
1248
				$group->add(new Form_Checkbox(
1249
					$pkga['fieldname'],
1250
					$pkga['fielddescr'],
1251
					fixup_string($pkga['description']),
1252
					($value == "on"),
1253
					'on'
1254
				))->displayAsRadio();
1255
			} else {
1256
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1257
					$advanced->addInput(new Form_Checkbox(
1258
						$pkga['fieldname'],
1259
						$pkga['fielddescr'],
1260
						fixup_string($pkga['description']),
1261
						($value == "on"),
1262
						'on'
1263
					))->displayAsRadio();
1264
				} else {
1265
					$section->addInput(new Form_Checkbox(
1266
						$pkga['fieldname'],
1267
						$pkga['fielddescr'],
1268
						fixup_string($pkga['description']),
1269
						($value == "on"),
1270
						'on'
1271
					))->displayAsRadio();
1272
				}
1273
			}
1274

    
1275
			break;
1276

    
1277
		// Create form button
1278
		case "button":
1279
			$newbtnicon = "fa-save";
1280
			if ($pkga['buttonicon'] != "") {
1281
				$newbtnicon = $pkga['buttonicon'];
1282
			}
1283
			$newbtnclass = "btn-primary";
1284
			if ($pkga['buttonclass'] != "") {
1285
				$newbtnclass = $pkga['buttonclass'];
1286
			}
1287

    
1288
			$newbtn = new Form_Button(
1289
				$pkga['fieldname'],
1290
				$pkga['fieldname'],
1291
				null,
1292
				$newbtnicon
1293
			);
1294
			$newbtn->addClass($newbtnclass);
1295

    
1296
			if ($grouping) {
1297
				$group->add(new Form_StaticText(
1298
					null,
1299
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1300
				));
1301
			} else {
1302
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1303
					$advanced->addInput(new Form_StaticText(
1304
						null,
1305
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1306
					));
1307
				} else {
1308
					$section->addInput(new Form_StaticText(
1309
						null,
1310
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1311
					));
1312
				}
1313
			}
1314

    
1315
			break;
1316

    
1317
		case "schedule_selection":
1318

    
1319
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1320
			$schedules = array();
1321
			$schedules[] = "none";
1322
			if (is_array($config['schedules']['schedule'])) {
1323
				foreach ($config['schedules']['schedule'] as $schedule) {
1324
					if ($schedule['name'] != "") {
1325
						$schedules[] = $schedule['name'];
1326
					}
1327
				}
1328
			}
1329

    
1330
			foreach ($schedules as $schedule) {
1331
				if ($schedule == "none") {
1332
					$schedlist[""] = $schedule;
1333
				} else {
1334
					$schedlist[$schedule] = $schedule;
1335
				}
1336
			}
1337

    
1338
			if ($grouping) {
1339
				$group->add(new Form_Select(
1340
					$pkga['fieldname'],
1341
					$pkga['fielddescr'],
1342
					$value,
1343
					$schedlist
1344
				))->setHelp(fixup_string($pkga['description']));
1345
			} else {
1346
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1347
					$advanced->addInput(new Form_Select(
1348
						$pkga['fieldname'],
1349
						$pkga['fielddescr'],
1350
						$value,
1351
						$schedlist
1352
					))->setHelp(fixup_string($pkga['description']));
1353
				} else {
1354
					$section->addInput(new Form_Select(
1355
						$pkga['fieldname'],
1356
						$pkga['fielddescr'],
1357
						$value,
1358
						$schedlist
1359
					))->setHelp(fixup_string($pkga['description']));
1360
				}
1361
			}
1362

    
1363
			break;
1364

    
1365
		case "rowhelper":
1366

    
1367
			$rowhelpername="row";
1368

    
1369
				$rowcounter = 0;
1370
				$trc = 0;
1371

    
1372
				//Use assigned $a_pkg or create an empty array to enter loop
1373
				if (isset($a_pkg[$id][$rowhelpername])) {
1374
					$saved_rows=$a_pkg[$id][$rowhelpername];
1375
				} else {
1376
					$saved_rows[] = array();
1377
				}
1378

    
1379
				$numrows = count($saved_rows) - 1;
1380

    
1381
				foreach ($saved_rows as $row) {
1382
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1383
					$group->addClass('repeatable');
1384

    
1385
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1386
						unset($value);
1387
						$width = null;
1388

    
1389
						$fieldname = $rowhelper['fieldname'];
1390
						$fielddescr = $rowhelper['fielddescr'];
1391

    
1392
						// If input validation failed, read the value from the POST data so that the user's input is not lost
1393
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1394
							$value = $_POST[$fieldname.$rowcounter];
1395
						} elseif (isset($id) && $a_pkg[$id]) {
1396
							$value = $row[$fieldname];
1397
						} elseif ($rowhelper['value'] != "") {
1398
							$value = $rowhelper['value'];
1399
						}
1400

    
1401
						$type = $rowhelper['type'];
1402
						if ($type == "input" || $type == "password" || $type == "textarea") {
1403
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1404
								$value = base64_decode($value);
1405
							}
1406
						}
1407

    
1408

    
1409
						if ($rowhelper['size']) {
1410
							$size = $rowhelper['size'];
1411
						} else if ($pkga['size']) {
1412
							$size = $pkga['size'];
1413
						} else {
1414
							$size = "8";
1415
						}
1416

    
1417
						if ($rowhelper['width']) {
1418
							$width = $rowhelper['width'];
1419
						}
1420

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

    
1423
						$text = "";
1424
						$trc++;
1425
					}
1426

    
1427
					// Delete row button
1428
					$group->add(new Form_Button(
1429
						'deleterow' . $rowcounter,
1430
						'Delete',
1431
						null,
1432
						'fa-trash'
1433
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1434

    
1435
					$rowcounter++;
1436
					$section->add($group);
1437
				}
1438

    
1439
			// Add row button
1440
			$section->addInput(new Form_Button(
1441
				'addrow',
1442
				'Add',
1443
				null,
1444
				'fa-plus'
1445
			))->addClass('btn-success');
1446

    
1447
			break;
1448

    
1449
	}
1450

    
1451
		if ($pkga['combinefields'] == "end") {
1452
			$group->add(new Form_StaticText(
1453
				null,
1454
				null
1455
			));
1456

    
1457
			if ($advanced) {
1458
				$advanced->add($group);
1459
			} else {
1460
				$section->add($group);
1461
			}
1462

    
1463
			$grouping = false;
1464
		}
1465

    
1466
	#increment counter
1467
	$i++;
1468
} // e-o-foreach field described in the XML
1469

    
1470
if ($section) {
1471
	$form->add($section);
1472
}
1473

    
1474
$form->addGlobal(new Form_Input(
1475
	'id',
1476
	null,
1477
	'hidden',
1478
	$id
1479
));
1480

    
1481
// If we created an advanced section, add it (and a button) to the form here
1482
if (!empty($advanced)) {
1483
	$form->addGlobal(new Form_Button(
1484
		'showadv',
1485
		'Show Advanced Options',
1486
		null,
1487
		'fa-cog'
1488
	))->setAttribute('type','button')->addClass('btn-info');
1489

    
1490
	$form->add($advanced);
1491
}
1492

    
1493
print($form);
1494

    
1495
if ($pkg['note'] != "") {
1496
	print_info_box($pkg['note'], 'info');
1497
}
1498

    
1499
if ($pkg['custom_php_after_form_command']) {
1500
	eval($pkg['custom_php_after_form_command']);
1501
}
1502

    
1503
if ($pkg['fields']['field'] != "") { ?>
1504
<script type="text/javascript">
1505
//<![CDATA[
1506
	events.push(function() {
1507

    
1508
	// Hide the advanced section
1509
	var advanced_visible = false;
1510

    
1511
	// Hide on page load
1512
	$('.advancedoptions').hide();
1513

    
1514
	// Suppress "Delete row" button if there are fewer than two rows
1515
	checkLastRow();
1516

    
1517
	// Show advanced section if you click the showadv button
1518
	$("#showadv").click(function() {
1519
		advanced_visible = !advanced_visible;
1520

    
1521
		if (advanced_visible) {
1522
			$('.advancedoptions').show();
1523
			$("#showadv").prop('value', 'Hide advanced Options');
1524
		} else {
1525
			$('.advancedoptions').hide();
1526
			$("#showadv").prop('value', 'Show advanced Options');
1527
		}
1528
	});
1529

    
1530
	// Call enablechange function
1531
	enablechange();
1532
});
1533

    
1534
	function enablechange() {
1535
<?php
1536
	foreach ($pkg['fields']['field'] as $field) {
1537
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1538
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1539

    
1540
			if (isset($field['enablefields'])) {
1541
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1542
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1543
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1544
					echo "\t\t}\n";
1545
				}
1546
			}
1547

    
1548
			if (isset($field['checkenablefields'])) {
1549
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1550
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1551
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1552
					echo "\t\t}\n";
1553
				}
1554
			}
1555

    
1556
			echo "\t}\n\telse {\n";
1557

    
1558
			if (isset($field['enablefields'])) {
1559
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1560
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1561
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1562
					echo "\t\t}\n";
1563
				}
1564
			}
1565

    
1566
			if (isset($field['checkenablefields'])) {
1567
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1568
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1569
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1570
					echo "\t\t}\n";
1571
				}
1572
			}
1573

    
1574
			echo "\t}\n";
1575
		}
1576
	}
1577
	?>
1578
	}
1579
//]]>
1580
</script>
1581

    
1582
<?php
1583
}
1584

    
1585
include("foot.inc");
(99-99/225)