Project

General

Profile

Download (41.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * pkg_edit.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

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

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

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

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

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

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

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

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

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

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

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

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

    
84
// grab the installedpackages->package_name section.
85
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['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
		$urltmp = "";
556
		if ($tab['url'] != "") {
557
			$urltmp = $tab['url'];
558
		}
559

    
560
		if ($tab['xml'] != "") {
561
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
562
		}
563

    
564
		$addresswithport = getenv("HTTP_HOST");
565
		$colonpos = strpos($addresswithport, ":");
566

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

    
574
		// eval url so that above $myurl item can be processed if need be.
575
		$url = str_replace('$myurl', $myurl, $urltmp);
576

    
577
		$tab_array[$tab_level][] = array(
578
			$tab['text'],
579
			$active,
580
			$url
581
		);
582
	}
583

    
584
	ksort($tab_array);
585
}
586

    
587
include("head.inc");
588
if ($pkg['custom_php_after_head_command']) {
589
	eval($pkg['custom_php_after_head_command']);
590
}
591
if (isset($tab_array)) {
592
	foreach ($tab_array as $tabid => $tab) {
593
		display_top_tabs($tab);
594
	}
595
}
596

    
597
// Start of page display
598
if ($input_errors) {
599
	print_input_errors($input_errors);
600
}
601

    
602
if ($savemsg) {
603
	print_info_box($savemsg, 'success');
604
}
605

    
606
$cols = 0;
607
$savevalue = gettext("Save");
608
if ($pkg['savetext'] != "") {
609
	$savevalue = $pkg['savetext'];
610
}
611

    
612
$savehelp = "";
613
if ($pkg['savehelp'] != "") {
614
	$savehelp = $pkg['savehelp'];
615
}
616

    
617
$saveicon = "fa-save";
618
if ($pkg['saveicon'] != "") {
619
	$saveicon = $pkg['saveicon'];
620
}
621

    
622
$savebtnclass = "btn-primary";
623
if ($pkg['savebtnclass'] != "") {
624
	$savebtnclass = $pkg['savebtnclass'];
625
}
626

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

    
629
$savebutton = new Form_Button(
630
	'submit',
631
	$savevalue,
632
	null,
633
	$saveicon
634
);
635
$savebutton->addClass($savebtnclass);
636

    
637
if ($savehelp) {
638
	$savebutton->setHelp($savehelp);
639
}
640

    
641
$form = new Form($savebutton);
642

    
643
$form->addGlobal(new Form_Input(
644
	'xml',
645
	null,
646
	'hidden',
647
	$xml
648
));
649

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

    
656
if ($pkg['advanced_options'] == "enabled") {
657
	$advfield_count = 0;
658
	$advanced = new Form_Section("Advanced Features");
659
	$advanced->addClass('advancedoptions');
660
}
661

    
662
$js_array = array();
663

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

    
670
	$action = "";
671
	$uid = "";
672

    
673
	if ($pkga['type'] == "sorting") {
674
		continue;
675
	}
676

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

    
685
			$advfield_count++;
686
		} else {
687
			if (isset($section)) {
688
				$form->add($section);
689
			}
690

    
691
			if (isset($pkga['collapse'])) {
692
				$uid = uniqid("section");
693

    
694
				$action = COLLAPSIBLE;
695

    
696
				if ($pkga['collapse'] == "open") {
697
					$action |= SEC_OPEN;
698
				} else {
699
					$action |= SEC_CLOSED;
700
				}
701
			}
702

    
703
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
704
		}
705

    
706
		continue;
707
	}
708

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

    
715
	$size = "";
716
	$colspan="";
717

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

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

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

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

    
756
			$grp = new Form_Input(
757
					$pkga['fieldname'],
758
					$pkga['fielddescr'],
759
					'text',
760
					$value
761
				);
762

    
763
			$grp->setHelp($pkga['description']);
764

    
765
			if ($pkga['width']) {
766
				$grp->setWidth($pkga['width']);
767
			}
768

    
769
			if ($grouping) {
770
				$group->add($grp);
771
			} else {
772
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
773
					$advanced->addInput($grp);
774
				} else {
775
					$section->addInput($grp);
776
				}
777
			}
778

    
779
			break;
780

    
781
		case "password":
782
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
783
				$value = base64_decode($value);
784
			}
785

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

    
812
			break;
813

    
814
		case "info":
815
			// If the info contains a table we should detect and Bootstrap it
816

    
817
			if (strpos($pkga['description'], '<table') !== FALSE) {
818
				$info = bootstrapTable($pkga['description']);
819
			} else {
820
				$info = $pkga['description'];
821
			}
822

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

    
835
			break;
836

    
837
		case "select":
838
			// Create a select element
839
			$optionlist = array();
840
			$selectedlist = array();
841

    
842
			$fieldname = $pkga['fieldname'];
843

    
844
			if (isset($pkga['multiple'])) {
845
				$multiple = 'multiple';
846
				$items = explode(',', $value);
847
				$fieldname .= "[]";
848
			} else {
849
				$multiple = '';
850
				$items = array($value);
851
			}
852

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

    
855
			foreach ($pkga['options']['option'] as $opt) {
856
				$optionlist[$opt['value']] = $opt['name'];
857

    
858
				if (in_array($opt['value'], $items)) {
859
					array_push($selectedlist, $opt['value']);
860
				}
861
			}
862

    
863
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
864
				$function = $grouping ? $advanced->add:$advanced->addInput;
865
			} else {
866
				$function = ($grouping) ? $section->add:$section->addInput;
867
			}
868

    
869
			$grp = new Form_Select(
870
						$pkga['fieldname'],
871
						strip_tags($pkga['fielddescr']),
872
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
873
						$optionlist,
874
						isset($pkga['multiple'])
875
					);
876

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

    
879
			if ($pkga['width']) {
880
				$grp->setWidth($pkga['width']);
881
			}
882

    
883
			if ($grouping) {
884
				$group->add($grp);
885
			} else {
886
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
887
					$advanced->addInput($grp);
888
				} else {
889
					$section->addInput($grp);
890
				}
891
			}
892

    
893
			break;
894

    
895
		case "select_source":
896

    
897
			if (isset($pkga['multiple'])) {
898
				$items = explode(',', $value);
899
				$fieldname .= "[]";
900
			} else {
901
				$items = array($value);
902
			}
903

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

    
906
			$source_url = $pkga['source'];
907
			eval("\$pkg_source_txt = &$source_url;");
908

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

    
918
			$srcoptions = array();
919
			$srcselected = array();
920

    
921
			foreach ($pkg_source_txt as $opt) {
922
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
923
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
924
				$srcoptions[$source_value] = $source_name;
925

    
926
				if (in_array($source_value, $items)) {
927
					array_push($srcselected, $source_value);
928
				}
929
			}
930

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

    
960
			break;
961

    
962
		case "vpn_selection" :
963
			$vpnlist = array();
964

    
965
			foreach ($config['ipsec']['phase1'] as $vpn) {
966
				$vpnlist[$vpn['descr']] = $vpn['descr'];
967

    
968
			}
969

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

    
995
			break;
996

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

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

    
1040
			break;
1041

    
1042
		// Create a textarea element
1043
		case "textarea":
1044
			$rows = $cols = 0;
1045

    
1046
			if ($pkga['rows']) {
1047
				$rows = $pkga['rows'];
1048
			}
1049
			if ($pkga['cols']) {
1050
				$cols = $pkga['cols'];
1051
			}
1052

    
1053
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1054
				$value = base64_decode($value);
1055
			}
1056

    
1057
			$grp = new Form_Textarea(
1058
					$pkga['fieldname'],
1059
					$pkga['fielddescr'],
1060
					$value
1061
			);
1062

    
1063
			$grp->setHelp(fixup_string($pkga['description']));
1064

    
1065
			if ($rows > 0) {
1066
				$grp->setRows($rows);
1067
			}
1068

    
1069
			if ($cols > 0) {
1070
				$grp->setCols($cols);
1071
			}
1072

    
1073
			if ($pkga['wrap'] == "off") {
1074
				$grp->setAttribute("wrap", "off");
1075
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1076
			} else {
1077
				$grp->setAttribute("style", "width: auto;");
1078
			}
1079

    
1080
			if ($grouping) {
1081
				$group->add($grp);
1082
			} else {
1083
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1084
					$advanced->addInput($grp);
1085
				} else {
1086
					$section->addInput($grp);
1087
				}
1088
			}
1089

    
1090
			break;
1091

    
1092
		case "aliases":
1093

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

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

    
1123
			$grp = new Form_Input(
1124
					$pkga['fieldname'],
1125
					$pkga['fielddescr'],
1126
					'text',
1127
					$value
1128
				);
1129

    
1130
			$grp->setHelp($pkga['description']);
1131

    
1132
			if ($pkga['width']) {
1133
				$grp->setWidth($pkga['width']);
1134
			}
1135

    
1136
			if ($grouping) {
1137
				$group->add($grp);
1138
			} else {
1139
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1140
					$advanced->addInput($grp);
1141
				} else {
1142
					$section->addInput($grp);
1143
				}
1144
			}
1145

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

    
1157
			echo $script;
1158

    
1159
			break;
1160

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

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

    
1202
			sort($ips);
1203
			if (isset($pkga['showlistenall'])) {
1204
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1205
			}
1206

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

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

    
1222
			$selectedlist = array();
1223
			$optionlist = array();
1224

    
1225
			if (is_array($value)) {
1226
				$values = $value;
1227
			} else {
1228
				$values = explode(',', $value);
1229
			}
1230

    
1231
			foreach ($ips as $iface) {
1232
				if (in_array($iface['ip'], $values)) {
1233
					array_push($selectedlist, $iface['ip']);
1234
				}
1235

    
1236
				$optionlist[$iface['ip']] = $iface['description'];
1237
			}
1238

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

    
1267
			break;
1268

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

    
1299
			break;
1300

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

    
1312
			$newbtn = new Form_Button(
1313
				$pkga['fieldname'],
1314
				$pkga['fieldname'],
1315
				null,
1316
				$newbtnicon
1317
			);
1318
			$newbtn->addClass($newbtnclass);
1319

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

    
1339
			break;
1340

    
1341
		case "schedule_selection":
1342

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

    
1354
			foreach ($schedules as $schedule) {
1355
				if ($schedule == "none") {
1356
					$schedlist[""] = $schedule;
1357
				} else {
1358
					$schedlist[$schedule] = $schedule;
1359
				}
1360
			}
1361

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

    
1387
			break;
1388

    
1389
		case "rowhelper":
1390

    
1391
			$rowhelpername="row";
1392

    
1393
				$rowcounter = 0;
1394
				$trc = 0;
1395

    
1396
				//Use assigned $a_pkg or create an empty array to enter loop
1397
				if (isset($a_pkg[$id][$rowhelpername])) {
1398
					$saved_rows=$a_pkg[$id][$rowhelpername];
1399
				} else {
1400
					$saved_rows[] = array();
1401
				}
1402

    
1403
				$numrows = count($saved_rows) - 1;
1404

    
1405
				foreach ($saved_rows as $row) {
1406
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1407
					$group->addClass('repeatable');
1408

    
1409
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1410
						unset($value);
1411
						$width = null;
1412

    
1413
						$fieldname = $rowhelper['fieldname'];
1414
						$fielddescr = $rowhelper['fielddescr'];
1415

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

    
1425
						$type = $rowhelper['type'];
1426
						if ($type == "input" || $type == "password" || $type == "textarea") {
1427
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1428
								$value = base64_decode($value);
1429
							}
1430
						}
1431

    
1432

    
1433
						if ($rowhelper['size']) {
1434
							$size = $rowhelper['size'];
1435
						} else if ($pkga['size']) {
1436
							$size = $pkga['size'];
1437
						} else {
1438
							$size = "8";
1439
						}
1440

    
1441
						if ($rowhelper['width']) {
1442
							$width = $rowhelper['width'];
1443
						}
1444

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

    
1447
						$text = "";
1448
						$trc++;
1449
					}
1450

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

    
1459
					$rowcounter++;
1460
					$section->add($group);
1461
				}
1462

    
1463
			// Add row button
1464
			$section->addInput(new Form_Button(
1465
				'addrow',
1466
				'Add',
1467
				null,
1468
				'fa-plus'
1469
			))->addClass('btn-success');
1470

    
1471
			break;
1472

    
1473
	}
1474

    
1475
		if ($pkga['combinefields'] == "end") {
1476
			$group->add(new Form_StaticText(
1477
				null,
1478
				null
1479
			));
1480

    
1481
			if ($advanced) {
1482
				$advanced->add($group);
1483
			} else {
1484
				$section->add($group);
1485
			}
1486

    
1487
			$grouping = false;
1488
		}
1489

    
1490
	#increment counter
1491
	$i++;
1492
} // e-o-foreach field described in the XML
1493

    
1494
if ($section) {
1495
	$form->add($section);
1496
}
1497

    
1498
$form->addGlobal(new Form_Input(
1499
	'id',
1500
	null,
1501
	'hidden',
1502
	$id
1503
));
1504

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

    
1514
	$form->add($advanced);
1515
}
1516

    
1517
print($form);
1518

    
1519
if ($pkg['note'] != "") {
1520
	print_info_box($pkg['note'], 'info');
1521
}
1522

    
1523
if ($pkg['custom_php_after_form_command']) {
1524
	eval($pkg['custom_php_after_form_command']);
1525
}
1526

    
1527

    
1528
$hidemsg = gettext("Show Advanced Options");
1529
$showmsg = gettext("Hide Advanced Options");
1530

    
1531
if ($pkg['fields']['field'] != "") { ?>
1532
<script type="text/javascript">
1533
//<![CDATA[
1534
	events.push(function() {
1535

    
1536
	// Hide the advanced section
1537
	var advanced_visible = false;
1538

    
1539
	// Hide on page load
1540
	$('.advancedoptions').hide();
1541

    
1542
	// Suppress "Delete row" button if there are fewer than two rows
1543
	checkLastRow();
1544

    
1545
	// Show advanced section if you click the showadv button
1546
	$("#showadv").click(function() {
1547
		advanced_visible = !advanced_visible;
1548

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

    
1558
	// Call enablechange function
1559
	enablechange();
1560
});
1561

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

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

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

    
1584
			echo "\t}\n\telse {\n";
1585

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

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

    
1602
			echo "\t}\n";
1603
		}
1604
	}
1605
	?>
1606
	}
1607
//]]>
1608
</script>
1609

    
1610
<?php
1611
}
1612

    
1613
include("foot.inc");
(104-104/232)