Project

General

Profile

Download (40.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	pkg_edit.php
4
*/
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *
8
 *	Redistribution and use in source and binary forms, with or without modification,
9
 *	are permitted provided that the following conditions are met:
10
 *
11
 *	1. Redistributions of source code must retain the above copyright notice,
12
 *		this list of conditions and the following disclaimer.
13
 *
14
 *	2. Redistributions in binary form must reproduce the above copyright
15
 *		notice, this list of conditions and the following disclaimer in
16
 *		the documentation and/or other materials provided with the
17
 *		distribution.
18
 *
19
 *	3. All advertising materials mentioning features or use of this software
20
 *		must display the following acknowledgment:
21
 *		"This product includes software developed by the pfSense Project
22
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
23
 *
24
 *	4. The names "pfSense" and "pfSense Project" must not be used to
25
 *		 endorse or promote products derived from this software without
26
 *		 prior written permission. For written permission, please contact
27
 *		 coreteam@pfsense.org.
28
 *
29
 *	5. Products derived from this software may not be called "pfSense"
30
 *		nor may "pfSense" appear in their names without prior written
31
 *		permission of the Electric Sheep Fencing, LLC.
32
 *
33
 *	6. Redistributions of any form whatsoever must retain the following
34
 *		acknowledgment:
35
 *
36
 *	"This product includes software developed by the pfSense Project
37
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
38
 *
39
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
40
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
43
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
51
 *
52
 *	====================================================================
53
 *
54
 */
55
/*
56
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
57
	pfSense_MODULE: pkgs
58
*/
59

    
60
##|+PRIV
61
##|*IDENT=page-package-edit
62
##|*NAME=Package: Edit
63
##|*DESCR=Allow access to the 'Package: Edit' page.
64
##|*MATCH=pkg_edit.php*
65
##|-PRIV
66

    
67
ini_set('max_execution_time', '0');
68

    
69
require("guiconfig.inc");
70
require_once("functions.inc");
71
require_once("filter.inc");
72
require_once("shaper.inc");
73
require_once("pkg-utils.inc");
74

    
75
/* dummy stubs needed by some code that was MFC'd */
76
function pfSenseHeader($location) {
77
	header("Location: " . $location);
78
}
79

    
80
$xml = htmlspecialchars($_GET['xml']);
81
if ($_POST['xml']) {
82
	$xml = htmlspecialchars($_POST['xml']);
83
}
84

    
85
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
86

    
87
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
88
	include("head.inc");
89
	print_info_box_np(gettext("ERROR: No valid package defined."));
90
	include("foot.inc");
91
	die;
92
} else {
93
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
94
}
95

    
96
if ($pkg['include_file'] != "") {
97
	require_once($pkg['include_file']);
98
}
99

    
100
if (!isset($pkg['adddeleteeditpagefields'])) {
101
	$only_edit = true;
102
} else {
103
	$only_edit = false;
104
}
105

    
106
$id = $_GET['id'];
107
if (isset($_POST['id'])) {
108
	$id = htmlspecialchars($_POST['id']);
109
}
110

    
111
// Not posting?	 Then user is editing a record. There must be a valid id
112
// when editing a record.
113
if (!$id && !$_POST) {
114
	$id = "0";
115
}
116

    
117
if (!is_numeric($id)) {
118
	header("Location: /");
119
	exit;
120
}
121

    
122
if ($pkg['custom_php_global_functions'] != "") {
123
	eval($pkg['custom_php_global_functions']);
124
}
125

    
126
// grab the installedpackages->package_name section.
127
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
128
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
129
}
130

    
131
// If the first entry in the array is an empty <config/> tag, kill it.
132
if ($config['installedpackages'] &&
133
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
134
    ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == "")) {
135
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
136
}
137

    
138
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
139

    
140
if ($_GET['savemsg'] != "") {
141
	$savemsg = htmlspecialchars($_GET['savemsg']);
142
}
143

    
144
if ($pkg['custom_php_command_before_form'] != "") {
145
	eval($pkg['custom_php_command_before_form']);
146
}
147

    
148
if ($_POST) {
149
	$rows = 0;
150

    
151
	$input_errors = array();
152
	$reqfields = array();
153
	$reqfieldsn = array();
154
	foreach ($pkg['fields']['field'] as $field) {
155
		if (isset($field['required'])) {
156
			if ($field['fieldname']) {
157
				$reqfields[] = $field['fieldname'];
158
			}
159
			if ($field['fielddescr']) {
160
				$reqfieldsn[] = $field['fielddescr'];
161
			}
162
		}
163
	}
164
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
165

    
166
	if ($pkg['custom_php_validation_command']) {
167
		eval($pkg['custom_php_validation_command']);
168
	}
169

    
170
	if ($_POST['act'] == "del") {
171
		if ($pkg['custom_delete_php_command']) {
172
			if ($pkg['custom_php_command_before_form'] != "") {
173
				eval($pkg['custom_php_command_before_form']);
174
			}
175
			eval($pkg['custom_delete_php_command']);
176
		}
177
		write_config($pkg['delete_string']);
178
		// resync the configuration file code if defined.
179
		if ($pkg['custom_php_resync_config_command'] != "") {
180
			if ($pkg['custom_php_command_before_form'] != "") {
181
				eval($pkg['custom_php_command_before_form']);
182
			}
183
			eval($pkg['custom_php_resync_config_command']);
184
		}
185
	} else {
186
		if (!$input_errors && $pkg['custom_add_php_command']) {
187
			if ($pkg['donotsave'] != "" or $pkg['preoutput'] != "") {
188
				include("head.inc");
189
			}
190

    
191
			if ($pkg['preoutput']) {
192
				echo "<pre>";
193
			}
194
			eval($pkg['custom_add_php_command']);
195
			if ($pkg['preoutput']) {
196
				echo "</pre>";
197
			}
198
		}
199
	}
200

    
201
	// donotsave is enabled.  lets simply exit.
202
	if (empty($pkg['donotsave'])) {
203

    
204
		// store values in xml configuration file.
205
		if (!$input_errors) {
206
			$pkgarr = array();
207
			foreach ($pkg['fields']['field'] as $fields) {
208
				switch ($fields['type']) {
209
					case "rowhelper":
210
						// save rowhelper items.
211
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
212
						$rowhelpername="row";
213
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
214
							foreach ($_POST as $key => $value) {
215
								$matches = array();
216
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
217
									$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = $value;
218
								}
219
							}
220
						}
221
						break;
222
					default:
223
						$fieldname = $fields['fieldname'];
224
						if ($fieldname == "interface_array") {
225
							$fieldvalue = $_POST[$fieldname];
226
						} elseif (is_array($_POST[$fieldname])) {
227
							$fieldvalue = implode(',', $_POST[$fieldname]);
228
						} else {
229
							$fieldvalue = trim($_POST[$fieldname]);
230
							if ($fields['encoding'] == 'base64') {
231
								$fieldvalue = base64_encode($fieldvalue);
232
							}
233
						}
234
						if ($fieldname) {
235
							$pkgarr[$fieldname] = $fieldvalue;
236
						}
237
					}
238
			}
239

    
240
			if (isset($id) && $a_pkg[$id]) {
241
				$a_pkg[$id] = $pkgarr;
242
			} else {
243
				$a_pkg[] = $pkgarr;
244
			}
245

    
246
			write_config($pkg['addedit_string']);
247
			// late running code
248
			if ($pkg['custom_add_php_command_late'] != "") {
249
				eval($pkg['custom_add_php_command_late']);
250
			}
251

    
252
			if (isset($pkg['filter_rules_needed'])) {
253
				filter_configure();
254
			}
255

    
256
			// resync the configuration file code if defined.
257
			if ($pkg['custom_php_resync_config_command'] != "") {
258
				eval($pkg['custom_php_resync_config_command']);
259
			}
260

    
261
			parse_package_templates();
262

    
263
			/* if start_command is defined, restart w/ this */
264
			if ($pkg['start_command'] != "") {
265
				exec($pkg['start_command'] . ">/dev/null 2&>1");
266
			}
267

    
268
			/* if restart_command is defined, restart w/ this */
269
			if ($pkg['restart_command'] != "") {
270
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
271
			}
272

    
273
			if ($pkg['aftersaveredirect'] != "") {
274
				pfSenseHeader($pkg['aftersaveredirect']);
275
			} elseif (!$pkg['adddeleteeditpagefields']) {
276
				pfSenseHeader("pkg_edit.php?xml={$xml}&amp;id=0");
277
			} elseif (!$pkg['preoutput']) {
278
				pfSenseHeader("pkg.php?xml=" . $xml);
279
			}
280
			exit;
281
		} else {
282
			$get_from_post = true;
283
		}
284
	} elseif (!$input_errors) {
285
		exit;
286
	}
287
}
288

    
289

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

    
296
	// Substitute everything inbetween with our new classes
297
	if ($t && $c && (($c - $t) < 200)) {
298
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
299
	}
300
}
301

    
302
/*
303
 * ROW helper function. Creates one element in the row from a PHP table by adding
304
 * the specified element to $group
305
 */
306
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description) {
307
	global $text, $group;
308

    
309
	switch ($type) {
310
		case "input":
311
			$group->add(new Form_Input(
312
				$fieldname . $trc,
313
				null,
314
				'text',
315
				$value
316
			))->setHelp($description);
317

    
318
			break;
319
		case "checkbox":
320
			$group->add(new Form_Checkbox(
321
				$fieldname . $trc,
322
				null,
323
				null,
324
				$value,
325
				'ON'
326
			))->setHelp($description);
327

    
328
			break;
329
		case "password":
330
			$group->add(new Form_Input(
331
				$fieldname . $trc,
332
				null,
333
				'password',
334
				$value
335
			))->setHelp($description);
336
			break;
337
		case "textarea":
338
			$group->add(new Form_Textarea(
339
				$fieldname . $trc,
340
				null,
341
				$value
342
			))->setHelp($description);
343

    
344
			break;
345
		case "select":
346
			$options = array();
347
			foreach ($rowhelper['options']['option'] as $rowopt) {
348
				$options[$rowopt['value']] = $rowopt['name'];
349
			}
350

    
351
			$group->add(new Form_Select(
352
				$fieldname . $trc,
353
				null,
354
				$value,
355
				$options
356
			))->setHelp($description);
357

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

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

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

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

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

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

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

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

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

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

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

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

    
429
			break;
430
	}
431
}
432

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

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

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

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

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

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

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

    
528
		foreach ($title as $subtitle) {
529
			$pgtitle[] = gettext($subtitle);
530
		}
531
	} else {
532
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
533
	}
534
} else {
535
	$pgtitle = array(gettext("Package"), gettext("Editor"));
536
}
537

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

    
548
		if (isset($tab['active'])) {
549
			$active = true;
550
			$pgtitle[] = $tab['text'] ;
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
if ($pkg['custom_php_after_head_command']) {
592
	$closehead = false;
593
	include("head.inc");
594
	eval($pkg['custom_php_after_head_command']);
595
} else {
596
	include("head.inc");
597
}
598
if (isset($tab_array)) {
599
	foreach ($tab_array as $tabid => $tab) {
600
		display_top_tabs($tab); //, $no_drop_down, $tabid);
601
	}
602
}
603

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

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

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

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

    
621
$form = new Form(new Form_Button(
622
	'submit',
623
	$savevalue
624
));
625

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

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

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

    
645
$js_array = array();
646

    
647
// Now loop through all of the fields defined in the XML
648
foreach ($pkg['fields']['field'] as $pkga) {
649

    
650
	if ($pkga['type'] == "sorting") {
651
		continue;
652
	}
653

    
654
	// Generate a new section
655
	if ($pkga['type'] == "listtopic") {
656
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
657
			$advanced->addInput(new Form_StaticText(
658
				strip_tags($pkga['name']),
659
				null
660
			));
661

    
662
			$advfield_count++;
663
		}  else {
664
			if (isset($section)) {
665
				$form->add($section);
666
			}
667

    
668
			$section = new Form_Section(strip_tags($pkga['name']));
669
		}
670

    
671
		continue;
672
	}
673

    
674
	// 'begin' starts a form group. ('end' ends it)
675
	if ($pkga['combinefields'] == "begin") {
676
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
677
		$grouping = true;
678
	}
679

    
680
	$size = "";
681
	$colspan="";
682

    
683
	// if user is editing a record, load in the data.
684
	$fieldname = $pkga['fieldname'];
685
	unset($value);
686
	if ($get_from_post) {
687
		$value = $_POST[$fieldname];
688
		if (is_array($value)) {
689
			$value = implode(',', $value);
690
		}
691
	} else {
692
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
693
			$value = $a_pkg[$id][$fieldname];
694
		} else {
695
			if (isset($pkga['default_value'])) {
696
				$value = $pkga['default_value'];
697
			}
698
		}
699
	}
700

    
701
	// If we get here but have no $section, the package config file probably had no listtopic field
702
	// We can create a section with a generic name to fix that
703
	if (!$section) {
704
		$section = new Form_Section(gettext('General options'));
705
	}
706

    
707
	switch ($pkga['type']) {
708
		// Create an input element. The format is slightly different depending on whether we are composing a group,
709
		// section, or advanced section. This is true for every element type
710
		case "input":
711
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
712
				$value = base64_decode($value);
713
			}
714

    
715
			if ($grouping) {
716
				$group->add(new Form_Input(
717
					$pkga['fieldname'],
718
					$pkga['fielddescr'],
719
					'text',
720
					$value
721
				))->setHelp($pkga['description']);
722
			} else {
723
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
724
					$advanced->addInput(new Form_Input(
725
						$pkga['fieldname'],
726
						$pkga['fielddescr'],
727
						'text',
728
						$value
729
					))->setHelp($pkga['description']);
730
				} else {
731
					$section->addInput(new Form_Input(
732
						$pkga['fieldname'],
733
						$pkga['fielddescr'],
734
						'text',
735
						$value
736
					))->setHelp($pkga['description']);
737
				}
738
			}
739

    
740
			break;
741

    
742
		case "password":
743
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
744
				$value = base64_decode($value);
745
			}
746

    
747
			// Create a password element
748
			if ($grouping) {
749
				$group->add(new Form_Input(
750
					$pkga['fieldname'],
751
					$pkga['fielddescr'],
752
					'password',
753
					$value
754
				))->setHelp($pkga['description']);
755
			} else {
756
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
757
					$advanced->addInput(new Form_Input(
758
						$pkga['fieldname'],
759
						$pkga['fielddescr'],
760
						'password',
761
						$value
762
					))->setHelp($pkga['description']);
763
				} else {
764
					$section->addInput(new Form_Input(
765
						$pkga['fieldname'],
766
						$pkga['fielddescr'],
767
						'password',
768
						$value
769
					))->setHelp($pkga['description']);
770
				}
771
			}
772

    
773
			break;
774

    
775
		case "info":
776
			// If the info contains a table we should detect and Bootstrap it
777

    
778
			if (strpos($pkga['description'], '<table') !== FALSE) {
779
				$info = bootstrapTable($pkga['description']);
780
			} else {
781
				$info = $pkga['description'];
782
			}
783

    
784
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
785
				$advanced->addInput(new Form_StaticText(
786
					strip_tags($pkga['fielddescr']),
787
					$info
788
				));
789
			} else {
790
				$section->addInput(new Form_StaticText(
791
					strip_tags($pkga['fielddescr']),
792
					$info
793
				));
794
			}
795

    
796
			break;
797

    
798
		case "select":
799
			// Create a select element
800
			$optionlist = array();
801
			$selectedlist = array();
802

    
803
			$fieldname = $pkga['fieldname'];
804

    
805
			if (isset($pkga['multiple'])) {
806
				$multiple = 'multiple';
807
				$items = explode(',', $value);
808
				$fieldname .= "[]";
809
			} else {
810
				$multiple = '';
811
				$items = array($value);
812
			}
813

    
814
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
815

    
816
			foreach ($pkga['options']['option'] as $opt) {
817
				$optionlist[$opt['value']] = $opt['name'];
818

    
819
				if (in_array($opt['value'], $items)) {
820
					array_push($selectedlist, $opt['value']);
821
				}
822
			}
823

    
824
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
825
				$function = $grouping ? $advanced->add:$advanced->addInput;
826
			} else {
827
				$function = ($grouping) ? $section->add:$section->addInput;
828
			}
829

    
830
			if ($grouping) {
831
					$group->add(new Form_Select(
832
						$pkga['fieldname'],
833
						strip_tags($pkga['fielddescr']),
834
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
835
						$optionlist,
836
						isset($pkga['multiple'])
837
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
838
			} else {
839
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
840
					$advanced->addInput(new Form_Select(
841
						$pkga['fieldname'],
842
						$pkga['fielddescr'],
843
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
844
						$optionlist,
845
						isset($pkga['multiple'])
846
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
847
				} else {
848
					$section->addInput(new Form_Select(
849
						$pkga['fieldname'],
850
						strip_tags($pkga['fielddescr']),
851
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
852
						$optionlist,
853
						isset($pkga['multiple'])
854
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
855
				}
856
			}
857

    
858
			break;
859

    
860
		case "select_source":
861

    
862
			if (isset($pkga['multiple'])) {
863
				$items = explode(',', $value);
864
				$fieldname .= "[]";
865
			} else {
866
				$items = array($value);
867
			}
868

    
869
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
870

    
871
			$source_url = $pkga['source'];
872
			eval("\$pkg_source_txt = &$source_url;");
873

    
874
			#check if show disable option is present on xml
875
			if (isset($pkga['show_disable_value'])) {
876
				array_push($pkg_source_txt,
877
					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']));
878
			}
879

    
880
			$srcoptions = array();
881
			$srcselected = array();
882

    
883
			foreach ($pkg_source_txt as $opt) {
884
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
885
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
886
				$srcoptions[$source_value] = $source_name;
887

    
888
				if (in_array($source_value, $items)) {
889
					array_push($srcselected, $source_value);
890
				}
891
			}
892

    
893
			if ($grouping) {
894
				$group->add(new Form_Select(
895
					$pkga['fieldname'],
896
					strip_tags($pkga['fielddescr']),
897
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
898
					$srcoptions,
899
					isset($pkga['multiple'])
900
				))->setOnchange($onchange);
901
			} else {
902
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
903
					$advanced->addInput(new Form_Select(
904
						$pkga['fieldname'],
905
						strip_tags($pkga['fielddescr']),
906
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
907
						$srcoptions,
908
						isset($pkga['multiple'])
909
					))->setOnchange($onchange);
910
				} else {
911
					$section->addInput(new Form_Select(
912
						$pkga['fieldname'],
913
						strip_tags($pkga['fielddescr']),
914
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
915
						$srcoptions,
916
						isset($pkga['multiple'])
917
					))->setOnchange($onchange);
918
				}
919
			}
920

    
921
			break;
922

    
923
		case "vpn_selection" :
924
			$vpnlist = array();
925

    
926
			foreach ($config['ipsec']['phase1'] as $vpn) {
927
				$vpnlist[$vpn['descr']] = $vpn['descr'];
928

    
929
			}
930

    
931
			if ($grouping) {
932
				$group->add(new Form_Select(
933
					$pkga['fieldname'],
934
					null,
935
					false,
936
					$vpnlist
937
				))->setHelp(fixup_string($pkga['description']));
938
			} else {
939
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
940
					$advanced->addInput(new Form_Select(
941
						$pkga['fieldname'],
942
						null,
943
						false,
944
						$vpnlist
945
					))->setHelp(fixup_string($pkga['description']));
946
				} else {
947
					$section->addInput(new Form_Select(
948
						$pkga['fieldname'],
949
						null,
950
						false,
951
						$vpnlist
952
					))->setHelp(fixup_string($pkga['description']));
953
				}
954
			}
955

    
956
			break;
957

    
958
		// Create a checkbox element
959
		case "checkbox":
960
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
961
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
962
				$onclick = 'javascript:enablechange();';
963
			} else {
964
				$onclick = '';
965
			}
966

    
967
			if ($grouping) {
968
				$group->add(new Form_Checkbox(
969
					$pkga['fieldname'],
970
					$pkga['fielddescr'],
971
					fixup_string($pkga['description']),
972
					($value == "on"),
973
					'on'
974
				))->setOnclick($onclick)
975
				  ->setOnchange($onchange)
976
				  ->setHelp($pkga['sethelp']);
977
			} else {
978
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
979
					$advanced->addInput(new Form_Checkbox(
980
						$pkga['fieldname'],
981
						$pkga['fielddescr'],
982
						fixup_string($pkga['description']),
983
						($value == "on"),
984
						'on'
985
					))->setOnclick($onclick)
986
					  ->setOnchange($onchange)
987
					  ->setHelp($pkga['sethelp']);
988
				} else {
989
					$section->addInput(new Form_Checkbox(
990
						$pkga['fieldname'],
991
						$pkga['fielddescr'],
992
						fixup_string($pkga['description']),
993
						($value == "on"),
994
						'on'
995
					))->setOnclick($onclick)
996
					  ->setOnchange($onchange)
997
					  ->setHelp($pkga['sethelp']);
998
				}
999
			}
1000

    
1001
			break;
1002

    
1003
		// Create a textarea element
1004
		case "textarea":
1005
			if ($pkga['rows']) {
1006
				$rows = " rows='{$pkga['rows']}' ";
1007
			}
1008
			if ($pkga['cols']) {
1009
				$cols = " cols='{$pkga['cols']}' ";
1010
			}
1011
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1012
				$value = base64_decode($value);
1013
			}
1014

    
1015
			$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');
1016

    
1017
			if ($grouping) {
1018
				$group->add(new Form_Textarea(
1019
					$pkga['fieldname'],
1020
					$pkga['fielddescr'],
1021
					$value
1022
				))->setHelp(fixup_string($pkga['description']));
1023
			} else {
1024
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1025
					$advanced->addInput(new Form_Textarea(
1026
						$pkga['fieldname'],
1027
						$pkga['fielddescr'],
1028
						$value
1029
					))->setHelp(fixup_string($pkga['description']));
1030
				} else {
1031
					$section->addInput(new Form_Textarea(
1032
						$pkga['fieldname'],
1033
						$pkga['fielddescr'],
1034
						$value
1035
					))->setHelp(fixup_string($pkga['description']));
1036
				}
1037
			}
1038

    
1039
			break;
1040

    
1041
		case "aliases":
1042

    
1043
			// Use xml tag <typealiases> to filter type aliases
1044
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1045
			$fieldname = $pkga['fieldname'];
1046
			$a_aliases = &$config['aliases']['alias'];
1047
			$addrisfirst = 0;
1048
			$aliasesaddr = "";
1049
			$value = "value='{$value}'";
1050

    
1051
			if (isset($a_aliases)) {
1052
				if (!empty($pkga['typealiases'])) {
1053
					foreach ($a_aliases as $alias) {
1054
						if ($alias['type'] == $pkga['typealiases']) {
1055
							if ($addrisfirst == 1) {
1056
								$aliasesaddr .= ",";
1057
							}
1058
							$aliasesaddr .= "'" . $alias['name'] . "'";
1059
							$addrisfirst = 1;
1060
						}
1061
					}
1062
				} else {
1063
					foreach ($a_aliases as $alias) {
1064
						if ($addrisfirst == 1) {
1065
							$aliasesaddr .= ",";
1066
						}
1067
						$aliasesaddr .= "'" . $alias['name'] . "'";
1068
						$addrisfirst = 1;
1069
					}
1070
				}
1071
			}
1072

    
1073
			if (grouping) {
1074
				$group->add(new Form_Input(
1075
					$pkga['fieldname'],
1076
					$pkga['fielddescr'],
1077
					'text',
1078
					$value
1079
				))->setHelp($pkga['description']);
1080
			} else {
1081
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1082
					$advanced->addInput(new Form_Input(
1083
						$pkga['fieldname'],
1084
						$pkga['fielddescr'],
1085
						'text',
1086
						$value
1087
					))->setHelp($pkga['description']);
1088
				} else {
1089
					$section->addInput(new Form_Input(
1090
						$pkga['fieldname'],
1091
						$pkga['fielddescr'],
1092
						'text',
1093
						$value
1094
					))->setHelp($pkga['description']);
1095
				}
1096
			}
1097

    
1098
			$script = "<script type='text/javascript'>\n";
1099
			$script .= "//<![CDATA[\n";
1100
			$script .= "events.push(function(){\n";
1101
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1102
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1103
			$script .= "		source: aliasarray\n";
1104
			$script .= "	})\n";
1105
			$script .= "});\n";
1106
			$script .= "//]]>\n";
1107
			$script .= "</script>";
1108

    
1109
			echo $script;
1110

    
1111
			break;
1112

    
1113
		case "interfaces_selection":
1114
			$ips = array();
1115
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1116
			if (is_array($config['interfaces'])) {
1117
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1118
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1119
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1120
						if (isset($pkga['showips'])) {
1121
							$iface_description .= " address";
1122
						}
1123
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1124
					}
1125
				}
1126
			}
1127

    
1128
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1129
				foreach ($config['virtualip']['vip'] as $vip) {
1130
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1131
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1132
					}
1133
					switch ($vip['mode']) {
1134
						case "ipalias":
1135
						case "carp":
1136
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1137
							break;
1138
						case "proxyarp":
1139
							if ($vip['type'] == "network") {
1140
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1141
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1142
								$len = $end - $start;
1143
								for ($i = 0; $i <= $len; $i++) {
1144
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1145
								}
1146
							} else {
1147
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1148
							}
1149
							break;
1150
					}
1151
				}
1152
			}
1153

    
1154
			sort($ips);
1155
			if (isset($pkga['showlistenall'])) {
1156
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1157
			}
1158

    
1159
			if (!preg_match("/$interface_regex/", "loopback")) {
1160
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1161
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1162
			}
1163

    
1164
			#show interfaces array on gui
1165
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1166
			$multiple = '';
1167
			$fieldname = $pkga['fieldname'];
1168
			if (isset($pkga['multiple'])) {
1169
				$fieldname .= '[]';
1170
				$multiple = 'multiple';
1171
			}
1172

    
1173
			$selectedlist = array();
1174
			$optionlist = array();
1175

    
1176
			if (is_array($value)) {
1177
				$values = $value;
1178
			} else {
1179
				$values = explode(',', $value);
1180
			}
1181

    
1182
			foreach ($ips as $iface) {
1183
				if (in_array($iface['ip'], $values)) {
1184
					array_push($selectedlist, $iface['ip']);
1185
				}
1186

    
1187
				$optionlist[$iface['ip']] = $iface['description'];
1188
			}
1189

    
1190
			if ($grouping) {
1191
				$group->add(new Form_Select(
1192
					$pkga['fieldname'],
1193
					$pkga['fielddescr'],
1194
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1195
					$optionlist,
1196
					isset($pkga['multiple'])
1197
				))->setHelp($pkga['description']);
1198
			} else {
1199
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1200
					$advanced->addInput(new Form_Select(
1201
						$pkga['fieldname'],
1202
						$pkga['fielddescr'],
1203
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1204
						$optionlist,
1205
						isset($pkga['multiple'])
1206
					))->setHelp($pkga['description']);
1207
				} else {
1208
					$section->addInput(new Form_Select(
1209
						$pkga['fieldname'],
1210
						$pkga['fielddescr'],
1211
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1212
						$optionlist,
1213
						isset($pkga['multiple'])
1214
					))->setHelp($pkga['description']);
1215
				}
1216
			}
1217

    
1218
			break;
1219

    
1220
		// Create radio button
1221
		case "radio":
1222
			if ($grouping) {
1223
				$group->add(new Form_Checkbox(
1224
					$pkga['fieldname'],
1225
					$pkga['fielddescr'],
1226
					fixup_string($pkga['description']),
1227
					($value == "on"),
1228
					'on'
1229
				))->displayAsRadio();
1230
			} else {
1231
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1232
					$advanced->addInput(new Form_Checkbox(
1233
						$pkga['fieldname'],
1234
						$pkga['fielddescr'],
1235
						fixup_string($pkga['description']),
1236
						($value == "on"),
1237
						'on'
1238
					))->displayAsRadio();
1239
				} else {
1240
					$section->addInput(new Form_Checkbox(
1241
						$pkga['fieldname'],
1242
						$pkga['fielddescr'],
1243
						fixup_string($pkga['description']),
1244
						($value == "on"),
1245
						'on'
1246
					))->displayAsRadio();
1247
				}
1248
			}
1249

    
1250
			break;
1251

    
1252
		// Create form button
1253
		case "button":
1254
			$newbtn = new Form_Button(
1255
				$pkga['fieldname'],
1256
				$pkga['fieldname']
1257
			);
1258

    
1259
			if (grouping) {
1260
				$group->add(new Form_StaticText(
1261
					null,
1262
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1263
				));
1264
			} else {
1265
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1266
					$advanced->addInput(new Form_StaticText(
1267
						null,
1268
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1269
					));
1270
				} else {
1271
					$section->addInput(new Form_StaticText(
1272
						null,
1273
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1274
					));
1275
				}
1276
			}
1277

    
1278
			break;
1279

    
1280
		case "schedule_selection":
1281

    
1282
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1283
			$schedules = array();
1284
			$schedules[] = "none";
1285
			if (is_array($config['schedules']['schedule'])) {
1286
				foreach ($config['schedules']['schedule'] as $schedule) {
1287
					if ($schedule['name'] != "") {
1288
						$schedules[] = $schedule['name'];
1289
					}
1290
				}
1291
			}
1292

    
1293
			foreach ($schedules as $schedule) {
1294
				if ($schedule == "none") {
1295
					$schedlist[""] = $schedule;
1296
				} else {
1297
					$schedlist[$schedule] = $schedule;
1298
				}
1299
			}
1300

    
1301
			if ($grouping) {
1302
				$group->add(new Form_Select(
1303
					$pkga['fieldname'],
1304
					$pkga['fielddescr'],
1305
					$value,
1306
					$schedlist
1307
				))->setHelp(fixup_string($pkga['description']));
1308
			} else {
1309
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1310
					$advanced->addInput(new Form_Select(
1311
						$pkga['fieldname'],
1312
						$pkga['fielddescr'],
1313
						$value,
1314
						$schedlist
1315
					))->setHelp(fixup_string($pkga['description']));
1316
				} else {
1317
					$section->addInput(new Form_Select(
1318
						$pkga['fieldname'],
1319
						$pkga['fielddescr'],
1320
						$value,
1321
						$schedlist
1322
					))->setHelp(fixup_string($pkga['description']));
1323
				}
1324
			}
1325

    
1326
			break;
1327

    
1328
		case "rowhelper":
1329

    
1330
			$rowhelpername="row";
1331

    
1332
				$rowcounter = 0;
1333
				$trc = 0;
1334

    
1335
				//Use assigned $a_pkg or create an empty array to enter loop
1336
				if (isset($a_pkg[$id][$rowhelpername])) {
1337
					$saved_rows=$a_pkg[$id][$rowhelpername];
1338
				} else {
1339
					$saved_rows[] = array();
1340
				}
1341

    
1342
				$numrows = count($saved_rows) - 1;
1343

    
1344
				foreach ($saved_rows as $row) {
1345
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1346
					$group->addClass('repeatable');
1347

    
1348
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1349
						unset($value);
1350
						if ($rowhelper['value'] != "") {
1351
							$value = $rowhelper['value'];
1352
						}
1353
						$fieldname = $rowhelper['fieldname'];
1354
						$fielddescr = $rowhelper['fielddescr'];
1355

    
1356
						// if user is editing a record, load in the data.
1357
						if (isset($id) && $a_pkg[$id]) {
1358
							$value = $row[$fieldname];
1359
						}
1360

    
1361
						$type = $rowhelper['type'];
1362
						if ($type == "input" || $type == "password" || $type == "textarea") {
1363
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1364
								$value = base64_decode($value);
1365
							}
1366
						}
1367
						$fieldname = $rowhelper['fieldname'];
1368

    
1369
						if ($rowhelper['size']) {
1370
							$size = $rowhelper['size'];
1371
						} else if ($pkga['size']) {
1372
							$size = $pkga['size'];
1373
						} else {
1374
							$size = "8";
1375
						}
1376

    
1377
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, ($numrows == $rowcounter) ? $fielddescr:null);
1378

    
1379
						$text = "";
1380
						$trc++;
1381
					}
1382

    
1383
					// Delete row button
1384
					$group->add(new Form_Button(
1385
						'deleterow' . $rowcounter,
1386
						'Delete'
1387
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1388

    
1389
					$rowcounter++;
1390
					$section->add($group);
1391
				}
1392

    
1393
			// Add row button
1394
			$section->addInput(new Form_Button(
1395
				'addrow',
1396
				'Add'
1397
			))->removeClass('btn-primary')->addClass('btn-success');
1398

    
1399
			break;
1400

    
1401
	}
1402

    
1403
		if ($pkga['combinefields'] == "end") {
1404
			$group->add(new Form_StaticText(
1405
				null,
1406
				null
1407
			));
1408

    
1409
			if ($advanced) {
1410
				$advanced->add($group);
1411
			} else {
1412
				$section->add($group);
1413
			}
1414

    
1415
			$grouping = false;
1416
		}
1417

    
1418
	#increment counter
1419
	$i++;
1420
} // e-o-foreach field described in the XML
1421

    
1422
$form->add($section);
1423

    
1424
$form->addGlobal(new Form_Input(
1425
	'id',
1426
	null,
1427
	'hidden',
1428
	$id
1429
));
1430

    
1431
// If we created an advanced section, add it (and a button) to the form here
1432
if (!empty($advanced)) {
1433
	$form->addGlobal(new Form_Button(
1434
		'showadv',
1435
		'Show advanced options'
1436
	))->removeClass('btn-primary')->addClass('btn-default');
1437

    
1438
	$form->add($advanced);
1439
}
1440

    
1441
print($form);
1442

    
1443
if ($pkg['note'] != "") {
1444
	print_info_box($pkg['note']);
1445

    
1446
if ($pkg['custom_php_after_form_command'])
1447
	eval($pkg['custom_php_after_form_command']);
1448
}
1449

    
1450
if ($pkg['fields']['field'] != "") { ?>
1451
<script type="text/javascript">
1452
//<![CDATA[
1453
	events.push(function() {
1454

    
1455
	// Hide the advanced section
1456
	var advanced_visible = false;
1457

    
1458
	// Hide on page load
1459
	$('.advancedoptions').hide();
1460

    
1461
	// Suppress "Delete row" button if there are fewer than two rows
1462
	checkLastRow();
1463

    
1464
	// Show advanced section if you click the showadv button
1465
	$('#showadv').prop('type', 'button');
1466

    
1467
	$("#showadv").click(function() {
1468
		advanced_visible = !advanced_visible;
1469

    
1470
		if (advanced_visible) {
1471
			$('.advancedoptions').show();
1472
			$("#showadv").prop('value', 'Hide advanced Options');
1473
		} else {
1474
			$('.advancedoptions').hide();
1475
			$("#showadv").prop('value', 'Show advanced Options');
1476
		}
1477
	});
1478

    
1479
	// Call enablechange function
1480
	enablechange();
1481
});
1482

    
1483
	function enablechange() {
1484
<?php
1485
	foreach ($pkg['fields']['field'] as $field) {
1486
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1487
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1488

    
1489
			if (isset($field['enablefields'])) {
1490
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1491
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1492
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1493
					echo "\t\t}\n";
1494
				}
1495
			}
1496

    
1497
			if (isset($field['checkenablefields'])) {
1498
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1499
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1500
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1501
					echo "\t\t}\n";
1502
				}
1503
			}
1504

    
1505
			echo "\t}\n\telse {\n";
1506

    
1507
			if (isset($field['enablefields'])) {
1508
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1509
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1510
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1511
					echo "\t\t}\n";
1512
				}
1513
			}
1514

    
1515
			if (isset($field['checkenablefields'])) {
1516
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1517
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1518
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1519
					echo "\t\t}\n";
1520
				}
1521
			}
1522

    
1523
			echo "\t}\n";
1524
		}
1525
	}
1526
	?>
1527
	}
1528
//]]>
1529
</script>
1530

    
1531
<?php
1532
}
1533

    
1534
include("foot.inc");
(101-101/228)