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
##|+PRIV
57
##|*IDENT=page-package-edit
58
##|*NAME=Package: Edit
59
##|*DESCR=Allow access to the 'Package: Edit' page.
60
##|*MATCH=pkg_edit.php*
61
##|-PRIV
62

    
63
ini_set('max_execution_time', '0');
64

    
65
require("guiconfig.inc");
66
require_once("functions.inc");
67
require_once("filter.inc");
68
require_once("shaper.inc");
69
require_once("pkg-utils.inc");
70

    
71
/* dummy stubs needed by some code that was MFC'd */
72
function pfSenseHeader($location) {
73
	header("Location: " . $location);
74
}
75

    
76
$xml = htmlspecialchars($_GET['xml']);
77
if ($_POST['xml']) {
78
	$xml = htmlspecialchars($_POST['xml']);
79
}
80

    
81
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
82

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

    
92
if ($pkg['include_file'] != "") {
93
	require_once($pkg['include_file']);
94
}
95

    
96
if (!isset($pkg['adddeleteeditpagefields'])) {
97
	$only_edit = true;
98
} else {
99
	$only_edit = false;
100
}
101

    
102
$id = $_GET['id'];
103
if (isset($_POST['id'])) {
104
	$id = htmlspecialchars($_POST['id']);
105
}
106

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

    
113
if (!is_numeric($id)) {
114
	header("Location: /");
115
	exit;
116
}
117

    
118
if ($pkg['custom_php_global_functions'] != "") {
119
	eval($pkg['custom_php_global_functions']);
120
}
121

    
122
// grab the installedpackages->package_name section.
123
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
124
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
125
}
126

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

    
134
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
135

    
136
if ($_GET['savemsg'] != "") {
137
	$savemsg = htmlspecialchars($_GET['savemsg']);
138
}
139

    
140
if ($pkg['custom_php_command_before_form'] != "") {
141
	eval($pkg['custom_php_command_before_form']);
142
}
143

    
144
if ($_POST) {
145
	$rows = 0;
146

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

    
162
	if ($pkg['custom_php_validation_command']) {
163
		eval($pkg['custom_php_validation_command']);
164
	}
165

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

    
187
			if ($pkg['preoutput']) {
188
				echo "<pre>";
189
			}
190
			eval($pkg['custom_add_php_command']);
191
			if ($pkg['preoutput']) {
192
				echo "</pre>";
193
			}
194
		}
195
	}
196

    
197
	// donotsave is enabled.  lets simply exit.
198
	if (empty($pkg['donotsave'])) {
199

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

    
236
			if (isset($id) && $a_pkg[$id]) {
237
				$a_pkg[$id] = $pkgarr;
238
			} else {
239
				$a_pkg[] = $pkgarr;
240
			}
241

    
242
			write_config($pkg['addedit_string']);
243
			// late running code
244
			if ($pkg['custom_add_php_command_late'] != "") {
245
				eval($pkg['custom_add_php_command_late']);
246
			}
247

    
248
			if (isset($pkg['filter_rules_needed'])) {
249
				filter_configure();
250
			}
251

    
252
			// resync the configuration file code if defined.
253
			if ($pkg['custom_php_resync_config_command'] != "") {
254
				eval($pkg['custom_php_resync_config_command']);
255
			}
256

    
257
			parse_package_templates();
258

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

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

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

    
285

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

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

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

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

    
314
			break;
315
		case "checkbox":
316
			$group->add(new Form_Checkbox(
317
				$fieldname . $trc,
318
				null,
319
				null,
320
				$value,
321
				'ON'
322
			))->setHelp($description);
323

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

    
340
			break;
341
		case "select":
342
			$options = array();
343
			foreach ($rowhelper['options']['option'] as $rowopt) {
344
				$options[$rowopt['value']] = $rowopt['name'];
345
			}
346

    
347
			$group->add(new Form_Select(
348
				$fieldname . $trc,
349
				null,
350
				$value,
351
				$options
352
			))->setHelp($description);
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
		}
527
	} else {
528
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
529
	}
530
} else {
531
	$pgtitle = array(gettext("Package"), gettext("Editor"));
532
}
533

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

    
544
		if (isset($tab['active'])) {
545
			$active = true;
546
			$pgtitle[] = $tab['text'] ;
547
		} else {
548
			$active = false;
549
		}
550

    
551
		if (isset($tab['no_drop_down'])) {
552
			$no_drop_down = true;
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); //, $no_drop_down, $tabid);
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 = gettext("");
613
if ($pkg['savehelp'] != "") {
614
	$savehelp = $pkg['savehelp'];
615
}
616

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

    
619
$savebutton = new Form_Button(
620
	'submit',
621
	$savevalue
622
);
623

    
624
if ($savehelp) {
625
	$savebutton->setHelp($savehelp);
626
}
627

    
628
$form = new Form($savebutton);
629

    
630
$form->addGlobal(new Form_Input(
631
	'xml',
632
	null,
633
	'hidden',
634
	$xml
635
));
636

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

    
643
if ($pkg['advanced_options'] == "enabled") {
644
	$advfield_count = 0;
645
	$advanced = new Form_Section(gettext("Advanced features"));
646
	$advanced->addClass('advancedoptions');
647
}
648

    
649
$js_array = array();
650

    
651
// Now loop through all of the fields defined in the XML
652
foreach ($pkg['fields']['field'] as $pkga) {
653

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

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

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

    
672
			$section = new Form_Section(strip_tags($pkga['name']));
673
		}
674

    
675
		continue;
676
	}
677

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

    
684
	$size = "";
685
	$colspan="";
686

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

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

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

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

    
744
			break;
745

    
746
		case "password":
747
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
748
				$value = base64_decode($value);
749
			}
750

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

    
777
			break;
778

    
779
		case "info":
780
			// If the info contains a table we should detect and Bootstrap it
781

    
782
			if (strpos($pkga['description'], '<table') !== FALSE) {
783
				$info = bootstrapTable($pkga['description']);
784
			} else {
785
				$info = $pkga['description'];
786
			}
787

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

    
800
			break;
801

    
802
		case "select":
803
			// Create a select element
804
			$optionlist = array();
805
			$selectedlist = array();
806

    
807
			$fieldname = $pkga['fieldname'];
808

    
809
			if (isset($pkga['multiple'])) {
810
				$multiple = 'multiple';
811
				$items = explode(',', $value);
812
				$fieldname .= "[]";
813
			} else {
814
				$multiple = '';
815
				$items = array($value);
816
			}
817

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

    
820
			foreach ($pkga['options']['option'] as $opt) {
821
				$optionlist[$opt['value']] = $opt['name'];
822

    
823
				if (in_array($opt['value'], $items)) {
824
					array_push($selectedlist, $opt['value']);
825
				}
826
			}
827

    
828
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
829
				$function = $grouping ? $advanced->add:$advanced->addInput;
830
			} else {
831
				$function = ($grouping) ? $section->add:$section->addInput;
832
			}
833

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

    
853
					$selector = new Form_Select(
854
						$pkga['fieldname'],
855
						strip_tags($pkga['fielddescr']),
856
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
857
						$optionlist,
858
						isset($pkga['multiple'])
859
					);
860

    
861
					$selector->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
862

    
863
					if ($pkga['width']) {
864
						$selector->setWidth($pkga['width']);
865
					}
866

    
867
					$section->addInput($selector);
868
				}
869
			}
870

    
871
			break;
872

    
873
		case "select_source":
874

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

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

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

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

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

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

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

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

    
934
			break;
935

    
936
		case "vpn_selection" :
937
			$vpnlist = array();
938

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

    
942
			}
943

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

    
969
			break;
970

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

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

    
1014
			break;
1015

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

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

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

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

    
1037
			$grp->setHelp(fixup_string($pkga['description']));
1038

    
1039
			if ($rows > 0) {
1040
				$grp->setRows($rows);
1041
			}
1042

    
1043
			if ($cols > 0) {
1044
				$grp->setCols($cols);
1045
			}
1046

    
1047
			if ($pkga['wrap'] == "off") {
1048
				$grp->setAttribute("wrap", "off");
1049
				$grp->setAttribute("style", "white-space:nowrap;");
1050
			}
1051

    
1052
			if ($grouping) {
1053
				$group->add($grp);
1054
			} else {
1055
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1056
					$advanced->addInput($grp);
1057
				} else {
1058
					$section->addInput($grp);
1059
				}
1060
			}
1061

    
1062
			break;
1063

    
1064
		case "aliases":
1065

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

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

    
1096
			if (grouping) {
1097
				$group->add(new Form_Input(
1098
					$pkga['fieldname'],
1099
					$pkga['fielddescr'],
1100
					'text',
1101
					$value
1102
				))->setHelp($pkga['description']);
1103
			} else {
1104
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1105
					$advanced->addInput(new Form_Input(
1106
						$pkga['fieldname'],
1107
						$pkga['fielddescr'],
1108
						'text',
1109
						$value
1110
					))->setHelp($pkga['description']);
1111
				} else {
1112
					$section->addInput(new Form_Input(
1113
						$pkga['fieldname'],
1114
						$pkga['fielddescr'],
1115
						'text',
1116
						$value
1117
					))->setHelp($pkga['description']);
1118
				}
1119
			}
1120

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

    
1132
			echo $script;
1133

    
1134
			break;
1135

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

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

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

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

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

    
1196
			$selectedlist = array();
1197
			$optionlist = array();
1198

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

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

    
1210
				$optionlist[$iface['ip']] = $iface['description'];
1211
			}
1212

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

    
1241
			break;
1242

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

    
1273
			break;
1274

    
1275
		// Create form button
1276
		case "button":
1277
			$newbtn = new Form_Button(
1278
				$pkga['fieldname'],
1279
				$pkga['fieldname']
1280
			);
1281

    
1282
			if (grouping) {
1283
				$group->add(new Form_StaticText(
1284
					null,
1285
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1286
				));
1287
			} else {
1288
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1289
					$advanced->addInput(new Form_StaticText(
1290
						null,
1291
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1292
					));
1293
				} else {
1294
					$section->addInput(new Form_StaticText(
1295
						null,
1296
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1297
					));
1298
				}
1299
			}
1300

    
1301
			break;
1302

    
1303
		case "schedule_selection":
1304

    
1305
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1306
			$schedules = array();
1307
			$schedules[] = "none";
1308
			if (is_array($config['schedules']['schedule'])) {
1309
				foreach ($config['schedules']['schedule'] as $schedule) {
1310
					if ($schedule['name'] != "") {
1311
						$schedules[] = $schedule['name'];
1312
					}
1313
				}
1314
			}
1315

    
1316
			foreach ($schedules as $schedule) {
1317
				if ($schedule == "none") {
1318
					$schedlist[""] = $schedule;
1319
				} else {
1320
					$schedlist[$schedule] = $schedule;
1321
				}
1322
			}
1323

    
1324
			if ($grouping) {
1325
				$group->add(new Form_Select(
1326
					$pkga['fieldname'],
1327
					$pkga['fielddescr'],
1328
					$value,
1329
					$schedlist
1330
				))->setHelp(fixup_string($pkga['description']));
1331
			} else {
1332
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1333
					$advanced->addInput(new Form_Select(
1334
						$pkga['fieldname'],
1335
						$pkga['fielddescr'],
1336
						$value,
1337
						$schedlist
1338
					))->setHelp(fixup_string($pkga['description']));
1339
				} else {
1340
					$section->addInput(new Form_Select(
1341
						$pkga['fieldname'],
1342
						$pkga['fielddescr'],
1343
						$value,
1344
						$schedlist
1345
					))->setHelp(fixup_string($pkga['description']));
1346
				}
1347
			}
1348

    
1349
			break;
1350

    
1351
		case "rowhelper":
1352

    
1353
			$rowhelpername="row";
1354

    
1355
				$rowcounter = 0;
1356
				$trc = 0;
1357

    
1358
				//Use assigned $a_pkg or create an empty array to enter loop
1359
				if (isset($a_pkg[$id][$rowhelpername])) {
1360
					$saved_rows=$a_pkg[$id][$rowhelpername];
1361
				} else {
1362
					$saved_rows[] = array();
1363
				}
1364

    
1365
				$numrows = count($saved_rows) - 1;
1366

    
1367
				foreach ($saved_rows as $row) {
1368
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1369
					$group->addClass('repeatable');
1370

    
1371
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1372
						unset($value);
1373
						if ($rowhelper['value'] != "") {
1374
							$value = $rowhelper['value'];
1375
						}
1376
						$fieldname = $rowhelper['fieldname'];
1377
						$fielddescr = $rowhelper['fielddescr'];
1378

    
1379
						// if user is editing a record, load in the data.
1380
						if (isset($id) && $a_pkg[$id]) {
1381
							$value = $row[$fieldname];
1382
						}
1383

    
1384
						$type = $rowhelper['type'];
1385
						if ($type == "input" || $type == "password" || $type == "textarea") {
1386
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1387
								$value = base64_decode($value);
1388
							}
1389
						}
1390
						$fieldname = $rowhelper['fieldname'];
1391

    
1392
						if ($rowhelper['size']) {
1393
							$size = $rowhelper['size'];
1394
						} else if ($pkga['size']) {
1395
							$size = $pkga['size'];
1396
						} else {
1397
							$size = "8";
1398
						}
1399

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

    
1402
						$text = "";
1403
						$trc++;
1404
					}
1405

    
1406
					// Delete row button
1407
					$group->add(new Form_Button(
1408
						'deleterow' . $rowcounter,
1409
						'Delete'
1410
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1411

    
1412
					$rowcounter++;
1413
					$section->add($group);
1414
				}
1415

    
1416
			// Add row button
1417
			$section->addInput(new Form_Button(
1418
				'addrow',
1419
				'Add'
1420
			))->removeClass('btn-primary')->addClass('btn-success');
1421

    
1422
			break;
1423

    
1424
	}
1425

    
1426
		if ($pkga['combinefields'] == "end") {
1427
			$group->add(new Form_StaticText(
1428
				null,
1429
				null
1430
			));
1431

    
1432
			if ($advanced) {
1433
				$advanced->add($group);
1434
			} else {
1435
				$section->add($group);
1436
			}
1437

    
1438
			$grouping = false;
1439
		}
1440

    
1441
	#increment counter
1442
	$i++;
1443
} // e-o-foreach field described in the XML
1444

    
1445
$form->add($section);
1446

    
1447
$form->addGlobal(new Form_Input(
1448
	'id',
1449
	null,
1450
	'hidden',
1451
	$id
1452
));
1453

    
1454
// If we created an advanced section, add it (and a button) to the form here
1455
if (!empty($advanced)) {
1456
	$form->addGlobal(new Form_Button(
1457
		'showadv',
1458
		'Show advanced options'
1459
	))->removeClass('btn-primary')->addClass('btn-default');
1460

    
1461
	$form->add($advanced);
1462
}
1463

    
1464
print($form);
1465

    
1466
if ($pkg['note'] != "") {
1467
	print_info_box($pkg['note']);
1468
}
1469

    
1470
if ($pkg['custom_php_after_form_command']) {
1471
	eval($pkg['custom_php_after_form_command']);
1472
}
1473

    
1474
if ($pkg['fields']['field'] != "") { ?>
1475
<script type="text/javascript">
1476
//<![CDATA[
1477
	events.push(function() {
1478

    
1479
	// Hide the advanced section
1480
	var advanced_visible = false;
1481

    
1482
	// Hide on page load
1483
	$('.advancedoptions').hide();
1484

    
1485
	// Suppress "Delete row" button if there are fewer than two rows
1486
	checkLastRow();
1487

    
1488
	// Show advanced section if you click the showadv button
1489
	$('#showadv').prop('type', 'button');
1490

    
1491
	$("#showadv").click(function() {
1492
		advanced_visible = !advanced_visible;
1493

    
1494
		if (advanced_visible) {
1495
			$('.advancedoptions').show();
1496
			$("#showadv").prop('value', 'Hide advanced Options');
1497
		} else {
1498
			$('.advancedoptions').hide();
1499
			$("#showadv").prop('value', 'Show advanced Options');
1500
		}
1501
	});
1502

    
1503
	// Call enablechange function
1504
	enablechange();
1505
});
1506

    
1507
	function enablechange() {
1508
<?php
1509
	foreach ($pkg['fields']['field'] as $field) {
1510
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1511
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1512

    
1513
			if (isset($field['enablefields'])) {
1514
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1515
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1516
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1517
					echo "\t\t}\n";
1518
				}
1519
			}
1520

    
1521
			if (isset($field['checkenablefields'])) {
1522
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1523
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1524
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1525
					echo "\t\t}\n";
1526
				}
1527
			}
1528

    
1529
			echo "\t}\n\telse {\n";
1530

    
1531
			if (isset($field['enablefields'])) {
1532
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1533
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1534
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1535
					echo "\t\t}\n";
1536
				}
1537
			}
1538

    
1539
			if (isset($field['checkenablefields'])) {
1540
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1541
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1542
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1543
					echo "\t\t}\n";
1544
				}
1545
			}
1546

    
1547
			echo "\t}\n";
1548
		}
1549
	}
1550
	?>
1551
	}
1552
//]]>
1553
</script>
1554

    
1555
<?php
1556
}
1557

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