Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
132
// If the first entry in the array is an empty <config/> tag, kill it.
133
if ($config['installedpackages'] && (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 (($field['type'] == 'input') && 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
if ($pkg['title'] != "") {
290
	$edit = ($only_edit ? '' : ": " . gettext("Edit"));
291
	$pgtitle = $pkg['title'] . $edit;
292
} else {
293
	$pgtitle = gettext("Package Editor");
294
}
295

    
296
if ($pkg['custom_php_after_head_command']) {
297
	$closehead = false;
298
	include("head.inc");
299
	eval($pkg['custom_php_after_head_command']);
300
} else {
301
	include("head.inc");
302
}
303

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

    
310
	// Substitute everything inbetween with our new classes
311
	if($t && $c && (($c - $t) < 200) ) {
312
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
313
	}
314
}
315

    
316
/*
317
 * ROW helper function. Creates one element in the row from a PHP table by adding
318
 * the specified element to $group
319
 */
320
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description) {
321
	global $text, $group;
322

    
323
	switch ($type) {
324
		case "input":
325
			$group->add(new Form_Input(
326
				$fieldname . $trc,
327
				null,
328
				'text',
329
				$value
330
			))->setHelp($description);
331

    
332
			break;
333
		case "checkbox":
334
			$group->add(new Form_Checkbox(
335
				$fieldname . $trc,
336
				null,
337
				null,
338
				$value,
339
				'ON'
340
			))->setHelp($description);
341

    
342
			break;
343
		case "password":
344
			$group->add(new Form_Input(
345
				$fieldname . $trc,
346
				null,
347
				'password',
348
				$value
349
			))->setHelp($description);
350
			break;
351
		case "textarea":
352
			$group->add(new Form_TextArea(
353
				$fieldname . $trc,
354
				null,
355
				$value
356
			))->setHelp($description);
357

    
358
			break;
359
		case "select":
360
			$options = array();
361
			foreach ($rowhelper['options']['option'] as $rowopt) {
362
				$options[$rowopt['value']] = $rowopt['name'];
363
			}
364

    
365
			$group->add(new Form_Select(
366
				$fieldname . $trc,
367
				null,
368
				$value,
369
				$options
370
			))->setHelp($description);
371

    
372
			break;
373
		case "interfaces_selection":
374
			$size = ($size ? "size=\"{$size}\"" : '');
375
			$multiple = '';
376
			if (isset($rowhelper['multiple'])) {
377
				$multiple = "multiple=\"multiple\"";
378
			}
379
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
380
			$ifaces = get_configured_interface_with_descr();
381
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
382
			if (!empty($additional_ifaces)) {
383
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
384
			}
385

    
386
			if (is_array($value)) {
387
				$values = $value;
388
			} else {
389
				$values = explode(',', $value);
390
			}
391

    
392
			$ifaces["lo0"] = "loopback";
393
			$options = array();
394
			$selected = array();
395

    
396
			foreach ($ifaces as $ifname => $iface) {
397
				$options[$ifname] = $iface;
398

    
399
				if(in_array($ifname, $values)) {
400
					array_push($selected, $ifname);
401
				}
402
			}
403

    
404
			$group->add(new Form_Select(
405
				$fieldname . $trc,
406
				null,
407
				($multiple) ? $selected:$selected[0],
408
				$options,
409
				$multiple
410
			))->setHelp($description);
411

    
412
			//echo "</select>\n";
413
			break;
414
		case "select_source":
415
			$options = array();
416
			$selected = array();
417

    
418
			if (isset($rowhelper['show_disable_value'])) {
419
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
420
			}
421

    
422
			$source_url = $rowhelper['source'];
423
			eval("\$pkg_source_txt = &$source_url;");
424

    
425
			foreach ($pkg_source_txt as $opt) {
426
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
427
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
428
				$options[$source_value] = $source_name;
429

    
430
				if($source_value == $value) {
431
					array_push($selected, $value);
432
				}
433
			}
434

    
435
			$group->add(new Form_Select(
436
				$fieldname . $trc,
437
				null,
438
				($multiple) ? $selected:$selected[0],
439
				$options,
440
				$multiple
441
			))->setHelp($description);
442

    
443
			break;
444
	}
445
}
446

    
447
function fixup_string($string) {
448
	global $config;
449
	// fixup #1: $myurl -> http[s]://ip_address:port/
450
	$https = "";
451
	$port = $config['system']['webguiport'];
452
	if ($port != "443" and $port != "80") {
453
		$urlport = ":" . $port;
454
	} else {
455
		$urlport = "";
456
	}
457

    
458
	if ($config['system']['webgui']['protocol'] == "https") {
459
		$https = "s";
460
	}
461
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
462
	$newstring = str_replace("\$myurl", $myurl, $string);
463
	$string = $newstring;
464
	// fixup #2: $wanip
465
	$curwanip = get_interface_ip();
466
	$newstring = str_replace("\$wanip", $curwanip, $string);
467
	$string = $newstring;
468
	// fixup #3: $lanip
469
	$lancfg = $config['interfaces']['lan'];
470
	$lanip = $lancfg['ipaddr'];
471
	$newstring = str_replace("\$lanip", $lanip, $string);
472
	$string = $newstring;
473
	// fixup #4: fix'r'up here.
474
	return $newstring;
475
}
476

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

    
522
			/* replace cr's */
523
			$template_text = str_replace("\\n", "\n", $template_text);
524

    
525
			/* write out new template file */
526
			$fout = fopen($filename, "w");
527
			fwrite($fout, $template_text);
528
			fclose($fout);
529
		}
530
	}
531
}
532

    
533
// Start of page display
534
require_once('classes/Form.class.php');
535

    
536
if ($input_errors)
537
	print_input_errors($input_errors);
538

    
539
if ($savemsg)
540
	print_info_box($savemsg, 'success');
541

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

    
552
		if (isset($tab['active'])) {
553
			$active = true;
554
		} else {
555
			$active = false;
556
		}
557

    
558
		if (isset($tab['no_drop_down'])) {
559
			$no_drop_down = true;
560
		}
561

    
562
		$urltmp = "";
563
		if ($tab['url'] != "") {
564
			$urltmp = $tab['url'];
565
		}
566

    
567
		if ($tab['xml'] != "") {
568
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
569
		}
570

    
571
		$addresswithport = getenv("HTTP_HOST");
572
		$colonpos = strpos($addresswithport, ":");
573

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

    
581
		// eval url so that above $myurl item can be processed if need be.
582
		$url = str_replace('$myurl', $myurl, $urltmp);
583

    
584
		$tab_array[$tab_level][] = array(
585
			$tab['text'],
586
			$active,
587
			$url
588
		);
589
	}
590

    
591
	ksort($tab_array);
592

    
593
	foreach ($tab_array as $tabid => $tab) {
594
		display_top_tabs($tab); //, $no_drop_down, $tabid);
595
	}
596
}
597

    
598
$cols = 0;
599
$savevalue = gettext("Save");
600
if ($pkg['savetext'] != "") {
601
	$savevalue = $pkg['savetext'];
602
}
603

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

    
606
$form = new Form(new Form_Button(
607
	'submit',
608
	$savevalue
609
));
610

    
611
$form->addGlobal(new Form_Input(
612
	'xml',
613
	null,
614
	'hidden',
615
	$xml
616
));
617

    
618
/* If a package's XML has <advanced_options/> configured, then setup
619
 * ta section for the fields that have <advancedfield/> set.
620
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
621
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
622
 */
623

    
624
if ($pkg['advanced_options'] == "enabled") {
625
	$advfield_count = 0;
626
	$advanced = new Form_Section(gettext("Advanced features"));
627
	$advanced->addClass('advancedoptions');
628
}
629

    
630
$js_array = array();
631

    
632
// Now loop through all of hte fields defined in the XML
633
foreach ($pkg['fields']['field'] as $pkga) {
634

    
635
	if ($pkga['type'] == "sorting") {
636
		continue;
637
	}
638

    
639
	// Generate a new section
640
	if ($pkga['type'] == "listtopic") {
641
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
642
			$advanced->addInput(new Form_StaticText(
643
				strip_tags($pkga['name']),
644
				null
645
			));
646

    
647
			$advfield_count++;
648
		}  else {
649
			if(isset($section))
650
				$form->add($section);
651

    
652
			$section = new Form_Section(strip_tags($pkga['name']));
653
		}
654

    
655
		continue;
656
	}
657

    
658
	// 'begin' starts a form group. ('end' ends it)
659
	if ($pkga['combinefields'] == "begin") {
660
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
661
		$grouping = true;
662
	}
663

    
664
	$size = "";
665
	$colspan="";
666

    
667
	// if user is editing a record, load in the data.
668
	$fieldname = $pkga['fieldname'];
669
	unset($value);
670
	if ($get_from_post) {
671
		$value = $_POST[$fieldname];
672
		if (is_array($value)) {
673
			$value = implode(',', $value);
674
		}
675
	} else {
676
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
677
			$value = $a_pkg[$id][$fieldname];
678
		} else {
679
			if (isset($pkga['default_value'])) {
680
				$value = $pkga['default_value'];
681
			}
682
		}
683
	}
684

    
685
	// If we get here but have no $section, the package config file probably had no listtopic field
686
	// We can create a section with a generic name to fix that
687
	if(!$section)
688
		$section = new Form_Section(gettext('General options'));
689

    
690
	switch ($pkga['type']) {
691
		// Create an input element. The format is slightly different depending on whether we are composing a group,
692
		// section, or advanced section. This is true for every element type
693
		case "input":
694
			if($grouping) {
695
				$group->add(new Form_Input(
696
					$pkga['fieldname'],
697
					$pkga['fielddescr'],
698
					'text',
699
					$value
700
				))->setHelp($pkga['description']);
701
			} else {
702
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
703
					$advanced->addInput(new Form_Input(
704
						$pkga['fieldname'],
705
						$pkga['fielddescr'],
706
						'text',
707
						$value
708
					))->setHelp($pkga['description']);
709
				} else {
710
					$section->addInput(new Form_Input(
711
						$pkga['fieldname'],
712
						$pkga['fielddescr'],
713
						'text',
714
						$value
715
					))->setHelp($pkga['description']);
716
				}
717
			}
718

    
719
			break;
720

    
721
		case "password":
722
		// Creat a password element
723
			if($grouping) {
724
				$group->add(new Form_Input(
725
					$pkga['fieldname'],
726
					$pkga['fielddescr'],
727
					'password',
728
					$value
729
				))->setHelp($pkga['description']);
730
			} else {
731
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
732
					$advanced->addInput(new Form_Input(
733
						$pkga['fieldname'],
734
						$pkga['fielddescr'],
735
						'password',
736
						$value
737
					))->setHelp($pkga['description']);
738
				} else {
739
					$section->addInput(new Form_Input(
740
						$pkga['fieldname'],
741
						$pkga['fielddescr'],
742
						'password',
743
						$value
744
					))->setHelp($pkga['description']);
745
				}
746
			}
747

    
748
			break;
749

    
750
		case "info":
751
			// If the info containe a table we should detect and Bootstrap it
752

    
753
			if (strpos($pkga['description'], '<table') !== FALSE)
754
				$info = bootstrapTable($pkga['description']);
755
			else
756
				$info = $pkga['description'];
757

    
758
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
759
				$advanced->addInput(new Form_StaticText(
760
					strip_tags($pkga['fielddescr']),
761
					$info
762
				));
763
			} else {
764
				$section->addInput(new Form_StaticText(
765
					strip_tags($pkga['fielddescr']),
766
					$info
767
				));
768
			}
769

    
770
			break;
771

    
772
		case "select":
773
			// Create a select element
774
			$optionlist = array();
775
			$selectedlist = array();
776

    
777
			$fieldname = $pkga['fieldname'];
778

    
779
			if (isset($pkga['multiple'])) {
780
				$multiple = 'multiple="multiple"';
781
				$items = explode(',', $value);
782
				$fieldname .= "[]";
783
			} else {
784
				$multiple = '';
785
				$items = array($value);
786
			}
787

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

    
790
			foreach ($pkga['options']['option'] as $opt) {
791
				$optionlist[$opt['value']] = $opt['name'];
792

    
793
				if (in_array($opt['value'], $items)) {
794
					array_push($selectedlist, $opt['value']);
795
				}
796
			}
797

    
798
			if (isset($pkga['advancedfield']) && isset($advfield_count))
799
				$function = $grouping ? $advanced->add:$advanced->addInput;
800
			else
801
				$function = ($grouping) ? $section->add:$section->addInput;
802

    
803
			if($grouping) {
804
					$group->add(new Form_Select(
805
						$pkga['fieldname'],
806
						strip_tags($pkga['fielddescr']),
807
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
808
						$optionlist,
809
						isset($pkga['multiple'])
810
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
811
			} else {
812
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
813
					$advanced->addInput(new Form_Select(
814
						$pkga['fieldname'],
815
						$pkga['fielddescr'],
816
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
817
						$optionlist,
818
						isset($pkga['multiple'])
819
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
820
				} else {
821
					$section->addInput(new Form_Select(
822
						$pkga['fieldname'],
823
						strip_tags($pkga['fielddescr']),
824
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
825
						$optionlist,
826
						isset($pkga['multiple'])
827
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
828
				}
829
			}
830

    
831
			break;
832

    
833
		case "select_source":
834

    
835
			if (isset($pkga['multiple'])) {
836
				$items = explode(',', $value);
837
				$fieldname .= "[]";
838
			} else {
839
				$items = array($value);
840
			}
841

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

    
844
			$source_url = $pkga['source'];
845
			eval("\$pkg_source_txt = &$source_url;");
846

    
847
			#check if show disable option is present on xml
848
			if (isset($pkga['show_disable_value'])) {
849
				array_push($pkg_source_txt,
850
					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']));
851
			}
852

    
853
			$srcoptions = array();
854
			$srcselected = array();
855

    
856
			foreach ($pkg_source_txt as $opt) {
857
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
858
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
859
				$srcoptions[$source_value] = $source_name;
860

    
861
				if(in_array($source_value, $items))
862
					array_push($srcselected, $source_value);
863
			}
864

    
865
			if($grouping) {
866
				$group->add(new Form_Select(
867
					$pkga['fieldname'],
868
					$pkga['name'],
869
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
870
					$srcoptions,
871
					isset($pkga['multiple'])
872
				))->setOnchange($onchange);
873
			} else {
874
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
875
					$advanced->addInput(new Form_Select(
876
						$pkga['fieldname'],
877
						$pkga['name'],
878
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
879
						$srcoptions,
880
						isset($pkga['multiple'])
881
					))->setOnchange($onchange);
882
				} else {
883
					$section->addInput(new Form_Select(
884
						$pkga['fieldname'],
885
						$pkga['name'],
886
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
887
						$srcoptions,
888
						isset($pkga['multiple'])
889
					))->setOnchange($onchange);
890
				}
891
			}
892

    
893
			break;
894

    
895
		case "vpn_selection" :
896
			$vpnlist = array();
897

    
898
			foreach ($config['ipsec']['phase1'] as $vpn) {
899
				$vpnlist[$vpn['descr']] = $vpn['descr'];
900

    
901
			}
902

    
903
			if($grouping) {
904
				$group->add(new Form_Select(
905
					$pkga['fieldname'],
906
					null,
907
					false,
908
					$vpnlist
909
				))->setHelp(fixup_string($pkga['description']));
910
			} else {
911
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
912
					$advanced->addInput(new Form_Select(
913
						$pkga['fieldname'],
914
						null,
915
						false,
916
						$vpnlist
917
					))->setHelp(fixup_string($pkga['description']));
918
				} else {
919
					$section->addInput(new Form_Select(
920
						$pkga['fieldname'],
921
						null,
922
						false,
923
						$vpnlist
924
					))->setHelp(fixup_string($pkga['description']));
925
				}
926
			}
927

    
928
			break;
929

    
930
		// Create a checkbox element
931
		case "checkbox":
932
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
933
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
934
				$onclick = 'javascript:enablechange();';
935
			else
936
				$onclick = '';
937

    
938
			if($grouping) {
939
				$group->add(new Form_Checkbox(
940
					$pkga['fieldname'],
941
					$pkga['fielddescr'],
942
					'Show log entries in reverse order (newest entries on top)',
943
					($value == "on"),
944
					'on'
945
				))->setHelp(fixup_string($pkga['description']))
946
				  ->setOnclick($onclick)
947
				  ->setOnchange($onchange);
948
			} else {
949
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
950
					$advanced->addInput(new Form_Checkbox(
951
						$pkga['fieldname'],
952
						$pkga['fielddescr'],
953
						'Show log entries in reverse order (newest entries on top)',
954
						($value == "on"),
955
						'on'
956
					))->setHelp(fixup_string($pkga['description']))
957
					  ->setOnclick($onclick)
958
					  ->setOnchange($onchange);
959
				} else {
960
					$section->addInput(new Form_Checkbox(
961
						$pkga['fieldname'],
962
						$pkga['fielddescr'],
963
						'Show log entries in reverse order (newest entries on top)',
964
						($value == "on"),
965
						'on'
966
					))->setHelp(fixup_string($pkga['description']))
967
					  ->setOnclick($onclick)
968
					  ->setOnchange($onchange);
969
				}
970
			}
971

    
972
			break;
973

    
974
		// Creat textarea element
975
		case "textarea":
976
			if ($pkga['rows']) {
977
				$rows = " rows='{$pkga['rows']}' ";
978
			}
979
			if ($pkga['cols']) {
980
				$cols = " cols='{$pkga['cols']}' ";
981
			}
982
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
983
				$value = base64_decode($value);
984
			}
985

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

    
988
			if ($grouping) {
989
				$group->add(new Form_TextArea(
990
					$pkga['fieldname'],
991
					$pkga['fielddescr'],
992
					$value
993
				))->setHelp(fixup_string($pkga['description']));
994
			} else {
995
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
996
					$advanced->addInput(new Form_TextArea(
997
						$pkga['fieldname'],
998
						$pkga['fielddescr'],
999
						$value
1000
					))->setHelp(fixup_string($pkga['description']));
1001
				} else {
1002
					$section->addInput(new Form_TextArea(
1003
						$pkga['fieldname'],
1004
						$pkga['fielddescr'],
1005
						$value
1006
					))->setHelp(fixup_string($pkga['description']));
1007
				}
1008
			}
1009

    
1010
			break;
1011

    
1012
		case "aliases":
1013

    
1014
			// Use xml tag <typealiases> to filter type aliases
1015
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1016
			$fieldname = $pkga['fieldname'];
1017
			$a_aliases = &$config['aliases']['alias'];
1018
			$addrisfirst = 0;
1019
			$aliasesaddr = "";
1020
			$value = "value='{$value}'";
1021

    
1022
			if (isset($a_aliases)) {
1023
				if (!empty($pkga['typealiases'])) {
1024
					foreach ($a_aliases as $alias) {
1025
						if ($alias['type'] == $pkga['typealiases']) {
1026
							if ($addrisfirst == 1) {
1027
								$aliasesaddr .= ",";
1028
							}
1029
							$aliasesaddr .= "'" . $alias['name'] . "'";
1030
							$addrisfirst = 1;
1031
						}
1032
					}
1033
				} else {
1034
					foreach ($a_aliases as $alias) {
1035
						if ($addrisfirst == 1) {
1036
							$aliasesaddr .= ",";
1037
						}
1038
						$aliasesaddr .= "'" . $alias['name'] . "'";
1039
						$addrisfirst = 1;
1040
					}
1041
				}
1042
			}
1043

    
1044
			if(grouping) {
1045
				$group->add(new Form_Input(
1046
					$pkga['fieldname'],
1047
					$pkga['fielddescr'],
1048
					'text',
1049
					$value
1050
				))->setHelp($pkga['description']);
1051
			} else {
1052
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1053
					$advanced->addInput(new Form_Input(
1054
						$pkga['fieldname'],
1055
						$pkga['fielddescr'],
1056
						'text',
1057
						$value
1058
					))->setHelp($pkga['description']);
1059
				} else {
1060
					$section->addInput(new Form_Input(
1061
						$pkga['fieldname'],
1062
						$pkga['fielddescr'],
1063
						'text',
1064
						$value
1065
					))->setHelp($pkga['description']);
1066
				}
1067
			}
1068

    
1069
			$script = "<script type='text/javascript'>\n";
1070
			$script .= "//<![CDATA[\n";
1071
			$script .= "events.push(function(){\n";
1072
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1073
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1074
			$script .= "		source: aliasarray\n";
1075
			$script .= "	})\n";
1076
			$script .= "});\n";
1077
			$script .= "//]]>\n";
1078
			$script .= "</script>";
1079

    
1080
			echo $script;
1081

    
1082
			break;
1083

    
1084
		case "interfaces_selection":
1085
			$ips = array();
1086
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1087
			if (is_array($config['interfaces'])) {
1088
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1089
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1090
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1091
						if (isset($pkga['showips'])) {
1092
							$iface_description .= " address";
1093
						}
1094
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1095
					}
1096
				}
1097
			}
1098

    
1099
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1100
				foreach ($config['virtualip']['vip'] as $vip) {
1101
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1102
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1103
					}
1104
					switch ($vip['mode']) {
1105
						case "ipalias":
1106
						case "carp":
1107
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1108
							break;
1109
						case "proxyarp":
1110
							if ($vip['type'] == "network") {
1111
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1112
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1113
								$len = $end - $start;
1114
								for ($i = 0; $i <= $len; $i++) {
1115
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1116
								}
1117
							} else {
1118
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1119
							}
1120
							break;
1121
					}
1122
				}
1123
			}
1124

    
1125
			sort($ips);
1126
			if (isset($pkga['showlistenall'])) {
1127
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1128
			}
1129

    
1130
			if (!preg_match("/$interface_regex/", "loopback")) {
1131
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1132
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1133
			}
1134

    
1135
			#show interfaces array on gui
1136
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1137
			$multiple = '';
1138
			$fieldname = $pkga['fieldname'];
1139
			if (isset($pkga['multiple'])) {
1140
				$fieldname .= '[]';
1141
				$multiple = 'multiple="multiple"';
1142
			}
1143

    
1144
			$selectedlist = array();
1145
			$optionlist = array();
1146

    
1147
			if (is_array($value)) {
1148
				$values = $value;
1149
			} else {
1150
				$values = explode(',', $value);
1151
			}
1152

    
1153
			foreach ($ips as $iface) {
1154
				if (in_array($iface['ip'], $values)) {
1155
					array_push($selectedlist, $iface['ip']);
1156
				}
1157

    
1158
				$optionlist[$iface['ip']] = $iface['description'];
1159
			}
1160

    
1161
			if($grouping) {
1162
				$group->add(new Form_Select(
1163
					$pkga['fieldname'],
1164
					$pkga['fielddescr'],
1165
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1166
					$optionlist,
1167
					isset($pkga['multiple'])
1168
				))->setHelp($pkga['description']);
1169
			} else {
1170
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1171
					$advanced->addInput(new Form_Select(
1172
						$pkga['fieldname'],
1173
						$pkga['fielddescr'],
1174
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1175
						$optionlist,
1176
						isset($pkga['multiple'])
1177
					))->setHelp($pkga['description']);
1178
				} else {
1179
					$section->addInput(new Form_Select(
1180
						$pkga['fieldname'],
1181
						$pkga['fielddescr'],
1182
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1183
						$optionlist,
1184
						isset($pkga['multiple'])
1185
					))->setHelp($pkga['description']);
1186
				}
1187
			}
1188

    
1189
			break;
1190

    
1191
		// Create radio button
1192
		case "radio":
1193
			if($grouping) {
1194
				$group->add(new Form_Checkbox(
1195
					$pkga['fieldname'],
1196
					$pkga['fielddescr'],
1197
					'Show log entries in reverse order (newest entries on top)',
1198
					($value == "on"),
1199
					'on'
1200
				))->setHelp(fixup_string($pkga['description']))->displayAsRadio();
1201
			} else {
1202
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1203
					$advanced->addInput(new Form_Checkbox(
1204
						$pkga['fieldname'],
1205
						$pkga['fielddescr'],
1206
						'Show log entries in reverse order (newest entries on top)',
1207
						($value == "on"),
1208
						'on'
1209
					))->setHelp(fixup_string($pkga['description']))->displayAsRadio();
1210
				} else {
1211
					$section->addInput(new Form_Checkbox(
1212
						$pkga['fieldname'],
1213
						$pkga['fielddescr'],
1214
						'Show log entries in reverse order (newest entries on top)',
1215
						($value == "on"),
1216
						'on'
1217
					))->setHelp(fixup_string($pkga['description']))->displayAsRadio();
1218
				}
1219
			}
1220

    
1221
			break;
1222

    
1223
		// Create form button
1224
		case "button":
1225
			$newbtn = new Form_Button(
1226
				$pkga['fieldname'],
1227
				$pkga['fieldname']
1228
			);
1229

    
1230
			if(grouping) {
1231
				$group->add(new Form_StaticText(
1232
					null,
1233
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1234
				));
1235
			} else {
1236
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1237
				$advanced->addInput(new Form_StaticText(
1238
					null,
1239
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1240
				));
1241
				} else {
1242
				$section->addInput(new Form_StaticText(
1243
					null,
1244
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1245
				));
1246
				}
1247
			}
1248

    
1249
			break;
1250

    
1251
		case "schedule_selection":
1252

    
1253
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1254
			$schedules = array();
1255
			$schedules[] = "none";
1256
			if (is_array($config['schedules']['schedule'])) {
1257
				foreach ($config['schedules']['schedule'] as $schedule) {
1258
					if ($schedule['name'] != "") {
1259
						$schedules[] = $schedule['name'];
1260
					}
1261
				}
1262
			}
1263

    
1264
			foreach ($schedules as $schedule) {
1265
				if ($schedule == "none") {
1266
					$schedlist[""] = $schedule;
1267
				} else {
1268
					$schedlist[$schedule] = $schedule;
1269
				}
1270
			}
1271

    
1272
			if($grouping) {
1273
				$group->add(new Form_Select(
1274
					$pkga['fieldname'],
1275
					$pkga['fielddescr'],
1276
					$value,
1277
					$schedlist
1278
				))->setHelp(fixup_string($pkga['description']));
1279
			} else {
1280
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1281
					$advanced->addInput(new Form_Select(
1282
						$pkga['fieldname'],
1283
						$pkga['fielddescr'],
1284
						$value,
1285
						$schedlist
1286
					))->setHelp(fixup_string($pkga['description']));
1287
				} else {
1288
					$section->addInput(new Form_Select(
1289
						$pkga['fieldname'],
1290
						$pkga['fielddescr'],
1291
						$value,
1292
						$schedlist
1293
					))->setHelp(fixup_string($pkga['description']));
1294
				}
1295
			}
1296

    
1297
			break;
1298

    
1299
		case "rowhelper":
1300

    
1301
			$rowhelpername="row";
1302

    
1303
				$rowcounter = 0;
1304
				$trc = 0;
1305

    
1306
				//Use assigned $a_pkg or create an empty array to enter loop
1307
				if (isset($a_pkg[$id][$rowhelpername])) {
1308
					$saved_rows=$a_pkg[$id][$rowhelpername];
1309
				} else {
1310
					$saved_rows[] = array();
1311
				}
1312

    
1313
				$numrows = count($saved_rows) - 1;
1314

    
1315
				foreach ($saved_rows as $row) {
1316
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1317
					$group->addClass('repeatable');
1318

    
1319
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1320
						unset($value);
1321
						if ($rowhelper['value'] != "") {
1322
							$value = $rowhelper['value'];
1323
						}
1324
						$fieldname = $rowhelper['fieldname'];
1325
						$fielddescr = $rowhelper['fielddescr'];
1326

    
1327
						// if user is editing a record, load in the data.
1328
						if (isset($id) && $a_pkg[$id]) {
1329
							$value = $row[$fieldname];
1330
						}
1331

    
1332
						$type = $rowhelper['type'];
1333
						$fieldname = $rowhelper['fieldname'];
1334

    
1335
						if ($rowhelper['size']) {
1336
							$size = $rowhelper['size'];
1337
						} else if ($pkga['size']) {
1338
							$size = $pkga['size'];
1339
						} else {
1340
							$size = "8";
1341
						}
1342

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

    
1345
						$text = "";
1346
						$trc++;
1347
					}
1348

    
1349
					// Delete row button
1350
					$group->add(new Form_Button(
1351
						'deleterow' . $rowcounter,
1352
						'Delete'
1353
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1354

    
1355
					$rowcounter++;
1356
					$section->add($group);
1357
				}
1358

    
1359
			// Add row button
1360
			$section->addInput(new Form_Button(
1361
				'addrow',
1362
				'Add'
1363
			))->removeClass('btn-primary')->addClass('btn-success');
1364

    
1365
			break;
1366

    
1367
	}
1368

    
1369
		if ($pkga['combinefields'] == "end") {
1370
			$group->add(new Form_StaticText(
1371
				null,
1372
				null
1373
			));
1374

    
1375
			if($advanced)
1376
				$advanced->add($group);
1377
			else
1378
				$section->add($group);
1379

    
1380
			$grouping = false;
1381
		}
1382

    
1383
	#increment counter
1384
	$i++;
1385
} // e-o-foreach field described in the XML
1386

    
1387
$form->add($section);
1388

    
1389
$form->addGlobal(new Form_Input(
1390
	'id',
1391
	null,
1392
	'hidden',
1393
	$id
1394
));
1395

    
1396
// If we created an advanced section, add it (and a button) to the form here
1397
if(!empty($advanced)) {
1398
	$form->addGlobal(new Form_Button(
1399
		'showadv',
1400
		'Show advanced options'
1401
	))->removeClass('btn-primary')->addClass('btn-default');
1402

    
1403
	$form->add($advanced);
1404
}
1405

    
1406
print($form);
1407

    
1408
if ($pkg['note'] != "") {
1409
	print_info_box($pkg['note']);
1410

    
1411
if ($pkg['custom_php_after_form_command'])
1412
	eval($pkg['custom_php_after_form_command']);
1413
}
1414

    
1415
if ($pkg['fields']['field'] != "") { ?>
1416
<script type="text/javascript">
1417
//<![CDATA[
1418
	events.push(function(){
1419

    
1420
	// Hide the advanced section
1421
	var advanced_visible = false;
1422

    
1423
	// Hide on page load
1424
	$('.advancedoptions').hide();
1425

    
1426
	// But show it if you click the showadv button
1427
	$('#showadv').prop('type', 'button');
1428

    
1429
	$("#showadv").click(function() {
1430
		advanced_visible = !advanced_visible;
1431

    
1432
		if(advanced_visible) {
1433
			$('.advancedoptions').show();
1434
			$("#showadv").prop('value', 'Hide advanced Options');
1435
		}
1436
		else {
1437
			$('.advancedoptions').hide();
1438
			$("#showadv").prop('value', 'Show advanced Options');
1439
		}
1440
	});
1441

    
1442
	// Call enablechange function
1443
	enablechange();
1444
});
1445

    
1446
	function enablechange() {
1447
<?php
1448
	foreach ($pkg['fields']['field'] as $field) {
1449
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1450
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1451

    
1452
			if (isset($field['enablefields'])) {
1453
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1454
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1455
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1456
					echo "\t\t}\n";
1457
				}
1458
			}
1459

    
1460
			if (isset($field['checkenablefields'])) {
1461
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1462
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1463
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1464
					echo "\t\t}\n";
1465
				}
1466
			}
1467

    
1468
			echo "\t}\n\telse {\n";
1469

    
1470
			if (isset($field['enablefields'])) {
1471
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1472
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1473
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1474
					echo "\t\t}\n";
1475
				}
1476
			}
1477

    
1478
			if (isset($field['checkenablefields'])) {
1479
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1480
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1481
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1482
					echo "\t\t}\n";
1483
				}
1484
			}
1485

    
1486
			echo "\t}\n";
1487
		}
1488
	}
1489
	?>
1490
	}
1491
//]]>
1492
</script>
1493

    
1494
<?php
1495
}
1496

    
1497
include("foot.inc");
(114-114/234)