Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
260
			parse_package_templates();
261

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

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

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

    
288
if ($pkg['title'] != "") {
289
	$edit = ($only_edit ? '' : ": " . gettext("Edit"));
290
	$pgtitle = $pkg['title'] . $edit;
291
} else {
292
	$pgtitle = gettext("Package Editor");
293
}
294

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
442
			break;
443
	}
444
}
445

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
590
	ksort($tab_array);
591

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

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

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

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

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

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

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

    
629
$js_array = array();
630

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

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

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

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

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

    
654
		continue;
655
	}
656

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

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

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

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

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

    
718
			break;
719

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

    
747
			break;
748

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

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

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

    
769
			break;
770

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

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

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

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

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

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

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

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

    
830
			break;
831

    
832
		case "select_source":
833

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

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

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

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

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

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

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

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

    
892
			break;
893

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

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

    
900
			}
901

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

    
927
			break;
928

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

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

    
971
			break;
972

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

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

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

    
1009
			break;
1010

    
1011
		case "aliases":
1012

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

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

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

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

    
1079
			echo $script;
1080

    
1081
			break;
1082

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

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

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

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

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

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

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

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

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

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

    
1188
			break;
1189

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

    
1220
			break;
1221

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

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

    
1248
			break;
1249

    
1250
		case "schedule_selection":
1251

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

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

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

    
1296
			break;
1297

    
1298
		case "rowhelper":
1299

    
1300
			$rowhelpername="row";
1301

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1364
			break;
1365

    
1366
	}
1367

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

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

    
1379
			$grouping = false;
1380
		}
1381

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

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

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

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

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

    
1405
print($form);
1406

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

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

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

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

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

    
1425
	// Suppress "Delete row" button if there are fewer than two rows
1426
	checkLastRow();
1427

    
1428
	// Show advanced section if you click the showadv button
1429
	$('#showadv').prop('type', 'button');
1430

    
1431
	$("#showadv").click(function() {
1432
		advanced_visible = !advanced_visible;
1433

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

    
1444
	// Call enablechange function
1445
	enablechange();
1446
});
1447

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

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

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

    
1470
			echo "\t}\n\telse {\n";
1471

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

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

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

    
1496
<?php
1497
}
1498

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