Project

General

Profile

Download (41.4 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("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'])]['config'])) {
86
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
87
}
88

    
89
/* If the first entry in the array is an empty <config/> tag, kill it.
90
 * See the following tickets for more:
91
 *  https://redmine.pfsense.org/issues/7624
92
 *  https://redmine.pfsense.org/issues/476
93
 */
94
if ($config['installedpackages'] &&
95
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
96
    (empty($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0])) &&
97
    is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
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 ($_REQUEST['savemsg'] != "") {
104
	$savemsg = htmlspecialchars($_REQUEST['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
	if (empty($pkg['donotsave'])) {
165

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

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

    
226
			write_config($pkg['addedit_string']);
227
			// late running code
228
			if ($pkg['custom_add_php_command_late'] != "") {
229
				eval($pkg['custom_add_php_command_late']);
230
			}
231

    
232
			if (isset($pkg['filter_rules_needed'])) {
233
				filter_configure();
234
			}
235

    
236
			// resync the configuration file code if defined.
237
			if ($pkg['custom_php_resync_config_command'] != "") {
238
				eval($pkg['custom_php_resync_config_command']);
239
			}
240

    
241
			parse_package_templates();
242

    
243
			/* if start_command is defined, restart w/ this */
244
			if ($pkg['start_command'] != "") {
245
				exec($pkg['start_command'] . ">/dev/null 2&>1");
246
			}
247

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

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

    
270

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

    
277
	// Substitute everything inbetween with our new classes
278
	if ($t && $c && (($c - $t) < 200)) {
279
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
280
	}
281
}
282

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

    
290
	switch ($type) {
291
		case "input":
292
			$inpt = new Form_Input(
293
				$fieldname . $trc,
294
				null,
295
				'text',
296
				$value
297
			);
298

    
299
			$inpt->setHelp($description);
300

    
301
			if ($ewidth) {
302
				$inpt->setWidth($ewidth);
303
			}
304

    
305
			$group->add($inpt);
306
			break;
307
		case "checkbox":
308
			$group->add(new Form_Checkbox(
309
				$fieldname . $trc,
310
				null,
311
				null,
312
				$value,
313
				'ON'
314
			))->setHelp($description);
315

    
316
			break;
317
		case "password":
318
			$group->add(new Form_Input(
319
				$fieldname . $trc,
320
				null,
321
				'password',
322
				$value
323
			))->setHelp($description);
324
			break;
325
		case "textarea":
326
			$group->add(new Form_Textarea(
327
				$fieldname . $trc,
328
				null,
329
				$value
330
			))->setHelp($description);
331

    
332
			break;
333
		case "select":
334
			$options = array();
335
			foreach ($rowhelper['options']['option'] as $rowopt) {
336
				$options[$rowopt['value']] = $rowopt['name'];
337
			}
338

    
339
			$grp = new Form_Select(
340
				$fieldname . $trc,
341
				null,
342
				$value,
343
				$options
344
			);
345

    
346
			$grp->setHelp($description);
347

    
348
			if ($ewidth) {
349
				$grp->setWidth($ewidth);
350
			}
351

    
352
			$group->add($grp);
353

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

    
368
			if (is_array($value)) {
369
				$values = $value;
370
			} else {
371
				$values = explode(',', $value);
372
			}
373

    
374
			$ifaces["lo0"] = "loopback";
375
			$options = array();
376
			$selected = array();
377

    
378
			foreach ($ifaces as $ifname => $iface) {
379
				$options[$ifname] = $iface;
380

    
381
				if (in_array($ifname, $values)) {
382
					array_push($selected, $ifname);
383
				}
384
			}
385

    
386
			$group->add(new Form_Select(
387
				$fieldname . $trc,
388
				null,
389
				($multiple) ? $selected:$selected[0],
390
				$options,
391
				$multiple
392
			))->setHelp($description);
393

    
394
			//echo "</select>\n";
395
			break;
396
		case "select_source":
397
			$options = array();
398
			$selected = array();
399

    
400
			if (isset($rowhelper['show_disable_value'])) {
401
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
402
			}
403

    
404
			$source_url = $rowhelper['source'];
405
			eval("\$pkg_source_txt = &$source_url;");
406

    
407
			foreach ($pkg_source_txt as $opt) {
408
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
409
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
410
				$options[$source_value] = $source_name;
411

    
412
				if ($source_value == $value) {
413
					array_push($selected, $value);
414
				}
415
			}
416

    
417
			$group->add(new Form_Select(
418
				$fieldname . $trc,
419
				null,
420
				($multiple) ? $selected:$selected[0],
421
				$options,
422
				$multiple
423
			))->setHelp($description);
424

    
425
			break;
426
	}
427
}
428

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

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

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

    
504
			/* replace cr's */
505
			$template_text = str_replace("\\n", "\n", $template_text);
506

    
507
			/* write out new template file */
508
			$fout = fopen($filename, "w");
509
			fwrite($fout, $template_text);
510
			fclose($fout);
511
		}
512
	}
513
}
514

    
515
//breadcrumb
516
if ($pkg['title'] != "") {
517
	if (!$only_edit) {
518
		$pkg['title'] = $pkg['title'] . '/Edit';
519
	}
520

    
521
	if (strpos($pkg['title'], '/')) {
522
		$title = explode('/', $pkg['title']);
523

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

    
537
// Create any required tabs
538
if ($pkg['tabs'] != "") {
539
	$tab_array = array();
540
	foreach ($pkg['tabs']['tab'] as $tab) {
541
		if ($tab['tab_level']) {
542
			$tab_level = $tab['tab_level'];
543
		} else {
544
			$tab_level = 1;
545
		}
546

    
547
		if (isset($tab['active'])) {
548
			$active = true;
549
			$pgtitle[] = $tab['text'] ;
550
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
551
		} else {
552
			$active = false;
553
		}
554

    
555
		if (isset($tab['no_drop_down'])) {
556
			$no_drop_down = true;
557
		}
558

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

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

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

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

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

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

    
588
	ksort($tab_array);
589
}
590

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
666
$js_array = array();
667

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

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

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

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

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

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

    
698
				$action = COLLAPSIBLE;
699

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

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

    
710
		continue;
711
	}
712

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

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

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

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

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

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

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

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

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

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

    
783
			break;
784

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

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

    
816
			break;
817

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

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

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

    
839
			break;
840

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

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

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

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

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

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

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

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

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

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

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

    
897
			break;
898

    
899
		case "select_source":
900

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

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

    
910
			$source_url = $pkga['source'];
911
			eval("\$pkg_source_txt = &$source_url;");
912

    
913
			#check if show disable option is present on xml
914
			if (!is_array($pkg_source_txt)) {
915
				$pkg_source_txt = array();
916
			}
917
			if (isset($pkga['show_disable_value'])) {
918
				array_push($pkg_source_txt,
919
					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']));
920
			}
921

    
922
			$srcoptions = array();
923
			$srcselected = array();
924

    
925
			foreach ($pkg_source_txt as $opt) {
926
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
927
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
928
				$srcoptions[$source_value] = $source_name;
929

    
930
				if (in_array($source_value, $items)) {
931
					array_push($srcselected, $source_value);
932
				}
933
			}
934

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

    
964
			break;
965

    
966
		case "vpn_selection" :
967
			$vpnlist = array();
968

    
969
			foreach ($config['ipsec']['phase1'] as $vpn) {
970
				$vpnlist[$vpn['descr']] = $vpn['descr'];
971

    
972
			}
973

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

    
999
			break;
1000

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

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

    
1044
			break;
1045

    
1046
		// Create a textarea element
1047
		case "textarea":
1048
			$rows = $cols = 0;
1049

    
1050
			if ($pkga['rows']) {
1051
				$rows = $pkga['rows'];
1052
			}
1053
			if ($pkga['cols']) {
1054
				$cols = $pkga['cols'];
1055
			}
1056

    
1057
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1058
				$value = base64_decode($value);
1059
			}
1060

    
1061
			$grp = new Form_Textarea(
1062
					$pkga['fieldname'],
1063
					$pkga['fielddescr'],
1064
					$value
1065
			);
1066

    
1067
			$grp->setHelp(fixup_string($pkga['description']));
1068

    
1069
			if ($rows > 0) {
1070
				$grp->setRows($rows);
1071
			}
1072

    
1073
			if ($cols > 0) {
1074
				$grp->setCols($cols);
1075
			}
1076

    
1077
			if ($pkga['wrap'] == "off") {
1078
				$grp->setAttribute("wrap", "off");
1079
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1080
			} else {
1081
				$grp->setAttribute("style", "width: auto;");
1082
			}
1083

    
1084
			if ($grouping) {
1085
				$group->add($grp);
1086
			} else {
1087
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1088
					$advanced->addInput($grp);
1089
				} else {
1090
					$section->addInput($grp);
1091
				}
1092
			}
1093

    
1094
			break;
1095

    
1096
		case "aliases":
1097

    
1098
			// Use xml tag <typealiases> to filter type aliases
1099
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1100
			$fieldname = $pkga['fieldname'];
1101
			$a_aliases = &$config['aliases']['alias'];
1102
			$addrisfirst = 0;
1103
			$aliasesaddr = "";
1104

    
1105
			if (isset($a_aliases)) {
1106
				if (!empty($pkga['typealiases'])) {
1107
					foreach ($a_aliases as $alias) {
1108
						if ($alias['type'] == $pkga['typealiases']) {
1109
							if ($addrisfirst == 1) {
1110
								$aliasesaddr .= ",";
1111
							}
1112
							$aliasesaddr .= "'" . $alias['name'] . "'";
1113
							$addrisfirst = 1;
1114
						}
1115
					}
1116
				} else {
1117
					foreach ($a_aliases as $alias) {
1118
						if ($addrisfirst == 1) {
1119
							$aliasesaddr .= ",";
1120
						}
1121
						$aliasesaddr .= "'" . $alias['name'] . "'";
1122
						$addrisfirst = 1;
1123
					}
1124
				}
1125
			}
1126

    
1127
			$grp = new Form_Input(
1128
					$pkga['fieldname'],
1129
					$pkga['fielddescr'],
1130
					'text',
1131
					$value
1132
				);
1133

    
1134
			$grp->setHelp($pkga['description']);
1135

    
1136
			if ($pkga['width']) {
1137
				$grp->setWidth($pkga['width']);
1138
			}
1139

    
1140
			if ($grouping) {
1141
				$group->add($grp);
1142
			} else {
1143
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1144
					$advanced->addInput($grp);
1145
				} else {
1146
					$section->addInput($grp);
1147
				}
1148
			}
1149

    
1150
			$script = "<script type='text/javascript'>\n";
1151
			$script .= "//<![CDATA[\n";
1152
			$script .= "events.push(function(){\n";
1153
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1154
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1155
			$script .= "		source: aliasarray\n";
1156
			$script .= "	})\n";
1157
			$script .= "});\n";
1158
			$script .= "//]]>\n";
1159
			$script .= "</script>";
1160

    
1161
			echo $script;
1162

    
1163
			break;
1164

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

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

    
1206
			sort($ips);
1207
			if (isset($pkga['showlistenall'])) {
1208
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1209
			}
1210

    
1211
			if (!preg_match("/$interface_regex/", "loopback")) {
1212
				$loopback_text = gettext("loopback");
1213
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1214
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1215
			}
1216

    
1217
			#show interfaces array on gui
1218
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1219
			$multiple = '';
1220
			$fieldname = $pkga['fieldname'];
1221
			if (isset($pkga['multiple'])) {
1222
				$fieldname .= '[]';
1223
				$multiple = 'multiple';
1224
			}
1225

    
1226
			$selectedlist = array();
1227
			$optionlist = array();
1228

    
1229
			if (is_array($value)) {
1230
				$values = $value;
1231
			} else {
1232
				$values = explode(',', $value);
1233
			}
1234

    
1235
			foreach ($ips as $iface) {
1236
				if (in_array($iface['ip'], $values)) {
1237
					array_push($selectedlist, $iface['ip']);
1238
				}
1239

    
1240
				$optionlist[$iface['ip']] = $iface['description'];
1241
			}
1242

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

    
1271
			break;
1272

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

    
1303
			break;
1304

    
1305
		// Create form button
1306
		case "button":
1307
			$newbtnicon = "fa-save";
1308
			if ($pkga['buttonicon'] != "") {
1309
				$newbtnicon = $pkga['buttonicon'];
1310
			}
1311
			$newbtnclass = "btn-primary";
1312
			if ($pkga['buttonclass'] != "") {
1313
				$newbtnclass = $pkga['buttonclass'];
1314
			}
1315

    
1316
			$newbtn = new Form_Button(
1317
				$pkga['fieldname'],
1318
				$pkga['fieldname'],
1319
				null,
1320
				$newbtnicon
1321
			);
1322
			$newbtn->addClass($newbtnclass);
1323

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

    
1343
			break;
1344

    
1345
		case "schedule_selection":
1346

    
1347
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1348
			$schedules = array();
1349
			$schedules[] = "none";
1350
			if (is_array($config['schedules']['schedule'])) {
1351
				foreach ($config['schedules']['schedule'] as $schedule) {
1352
					if ($schedule['name'] != "") {
1353
						$schedules[] = $schedule['name'];
1354
					}
1355
				}
1356
			}
1357

    
1358
			foreach ($schedules as $schedule) {
1359
				if ($schedule == "none") {
1360
					$schedlist[""] = $schedule;
1361
				} else {
1362
					$schedlist[$schedule] = $schedule;
1363
				}
1364
			}
1365

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

    
1391
			break;
1392

    
1393
		case "rowhelper":
1394

    
1395
			$rowhelpername="row";
1396

    
1397
				$rowcounter = 0;
1398
				$trc = 0;
1399

    
1400
				//Use assigned $a_pkg or create an empty array to enter loop
1401
				if (isset($a_pkg[$id][$rowhelpername])) {
1402
					$saved_rows=$a_pkg[$id][$rowhelpername];
1403
				} else {
1404
					$saved_rows[] = array();
1405
				}
1406

    
1407
				$numrows = count($saved_rows) - 1;
1408

    
1409
				foreach ($saved_rows as $row) {
1410
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1411
					$group->addClass('repeatable');
1412

    
1413
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1414
						unset($value);
1415
						$width = null;
1416

    
1417
						$fieldname = $rowhelper['fieldname'];
1418
						$fielddescr = $rowhelper['fielddescr'];
1419

    
1420
						// If input validation failed, read the value from the POST data so that the user's input is not lost
1421
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1422
							$value = $_POST[$fieldname.$rowcounter];
1423
						} elseif (isset($id) && $a_pkg[$id]) {
1424
							$value = $row[$fieldname];
1425
						} elseif ($rowhelper['value'] != "") {
1426
							$value = $rowhelper['value'];
1427
						}
1428

    
1429
						$type = $rowhelper['type'];
1430
						if ($type == "input" || $type == "password" || $type == "textarea") {
1431
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1432
								$value = base64_decode($value);
1433
							}
1434
						}
1435

    
1436

    
1437
						if ($rowhelper['size']) {
1438
							$size = $rowhelper['size'];
1439
						} else if ($pkga['size']) {
1440
							$size = $pkga['size'];
1441
						} else {
1442
							$size = "8";
1443
						}
1444

    
1445
						if ($rowhelper['width']) {
1446
							$width = $rowhelper['width'];
1447
						}
1448

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

    
1451
						$text = "";
1452
						$trc++;
1453
					}
1454

    
1455
					// Delete row button
1456
					$group->add(new Form_Button(
1457
						'deleterow' . $rowcounter,
1458
						'Delete',
1459
						null,
1460
						'fa-trash'
1461
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1462

    
1463
					$rowcounter++;
1464
					$section->add($group);
1465
				}
1466

    
1467
			// Add row button
1468
			$section->addInput(new Form_Button(
1469
				'addrow',
1470
				'Add',
1471
				null,
1472
				'fa-plus'
1473
			))->addClass('btn-success');
1474

    
1475
			break;
1476

    
1477
	}
1478

    
1479
		if ($pkga['combinefields'] == "end") {
1480
			$group->add(new Form_StaticText(
1481
				null,
1482
				null
1483
			));
1484

    
1485
			if ($advanced) {
1486
				$advanced->add($group);
1487
			} else {
1488
				$section->add($group);
1489
			}
1490

    
1491
			$grouping = false;
1492
		}
1493

    
1494
	#increment counter
1495
	$i++;
1496
} // e-o-foreach field described in the XML
1497

    
1498
if ($section) {
1499
	$form->add($section);
1500
}
1501

    
1502
$form->addGlobal(new Form_Input(
1503
	'id',
1504
	null,
1505
	'hidden',
1506
	$id
1507
));
1508

    
1509
// If we created an advanced section, add it (and a button) to the form here
1510
if (!empty($advanced)) {
1511
	$form->addGlobal(new Form_Button(
1512
		'showadv',
1513
		'Show Advanced Options',
1514
		null,
1515
		'fa-cog'
1516
	))->setAttribute('type','button')->addClass('btn-info');
1517

    
1518
	$form->add($advanced);
1519
}
1520

    
1521
print($form);
1522

    
1523
if ($pkg['note'] != "") {
1524
	print_info_box($pkg['note'], 'info');
1525
}
1526

    
1527
if ($pkg['custom_php_after_form_command']) {
1528
	eval($pkg['custom_php_after_form_command']);
1529
}
1530

    
1531

    
1532
$hidemsg = gettext("Show Advanced Options");
1533
$showmsg = gettext("Hide Advanced Options");
1534

    
1535
if ($pkg['fields']['field'] != "") { ?>
1536
<script type="text/javascript">
1537
//<![CDATA[
1538
	events.push(function() {
1539

    
1540
	// Hide the advanced section
1541
	var advanced_visible = false;
1542

    
1543
	// Hide on page load
1544
	$('.advancedoptions').hide();
1545

    
1546
	// Suppress "Delete row" button if there are fewer than two rows
1547
	checkLastRow();
1548

    
1549
	// Show advanced section if you click the showadv button
1550
	$("#showadv").click(function() {
1551
		advanced_visible = !advanced_visible;
1552

    
1553
		if (advanced_visible) {
1554
			$('.advancedoptions').show();
1555
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$showmsg?>");
1556
		} else {
1557
			$('.advancedoptions').hide();
1558
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$hidemsg?>");
1559
		}
1560
	});
1561

    
1562
	// Call enablechange function
1563
	enablechange();
1564
});
1565

    
1566
	function enablechange() {
1567
<?php
1568
	foreach ($pkg['fields']['field'] as $field) {
1569
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1570
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1571

    
1572
			if (isset($field['enablefields'])) {
1573
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1574
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1575
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1576
					echo "\t\t}\n";
1577
				}
1578
			}
1579

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

    
1588
			echo "\t}\n\telse {\n";
1589

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

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

    
1606
			echo "\t}\n";
1607
		}
1608
	}
1609
	?>
1610
	}
1611
//]]>
1612
</script>
1613

    
1614
<?php
1615
}
1616

    
1617
include("foot.inc");
(102-102/229)