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
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 (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";
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
if ($input_errors)
534
	print_input_errors($input_errors);
535

    
536
if ($savemsg)
537
	print_info_box($savemsg, 'success');
538

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

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

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

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

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

    
568
		$addresswithport = getenv("HTTP_HOST");
569
		$colonpos = strpos($addresswithport, ":");
570

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

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

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

    
588
	ksort($tab_array);
589

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

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

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

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

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

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

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

    
627
$js_array = array();
628

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

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

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

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

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

    
652
		continue;
653
	}
654

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

    
661
	$size = "";
662
	$colspan="";
663

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

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

    
687
	switch ($pkga['type']) {
688
		// Create an input element. The format is slightly different depending on whether we are composing a group,
689
		// section, or advanced section. This is true for every element type
690
		case "input":
691
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
692
				$value = base64_decode($value);
693
			}
694

    
695
			if ($grouping) {
696
				$group->add(new Form_Input(
697
					$pkga['fieldname'],
698
					$pkga['fielddescr'],
699
					'text',
700
					$value
701
				))->setHelp($pkga['description']);
702
			} else {
703
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
704
					$advanced->addInput(new Form_Input(
705
						$pkga['fieldname'],
706
						$pkga['fielddescr'],
707
						'text',
708
						$value
709
					))->setHelp($pkga['description']);
710
				} else {
711
					$section->addInput(new Form_Input(
712
						$pkga['fieldname'],
713
						$pkga['fielddescr'],
714
						'text',
715
						$value
716
					))->setHelp($pkga['description']);
717
				}
718
			}
719

    
720
			break;
721

    
722
		case "password":
723
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
724
				$value = base64_decode($value);
725
			}
726

    
727
			// Create a password element
728
			if ($grouping) {
729
				$group->add(new Form_Input(
730
					$pkga['fieldname'],
731
					$pkga['fielddescr'],
732
					'password',
733
					$value
734
				))->setHelp($pkga['description']);
735
			} else {
736
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
737
					$advanced->addInput(new Form_Input(
738
						$pkga['fieldname'],
739
						$pkga['fielddescr'],
740
						'password',
741
						$value
742
					))->setHelp($pkga['description']);
743
				} else {
744
					$section->addInput(new Form_Input(
745
						$pkga['fieldname'],
746
						$pkga['fielddescr'],
747
						'password',
748
						$value
749
					))->setHelp($pkga['description']);
750
				}
751
			}
752

    
753
			break;
754

    
755
		case "info":
756
			// If the info contains a table we should detect and Bootstrap it
757

    
758
			if (strpos($pkga['description'], '<table') !== FALSE)
759
				$info = bootstrapTable($pkga['description']);
760
			else
761
				$info = $pkga['description'];
762

    
763
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
764
				$advanced->addInput(new Form_StaticText(
765
					strip_tags($pkga['fielddescr']),
766
					$info
767
				));
768
			} else {
769
				$section->addInput(new Form_StaticText(
770
					strip_tags($pkga['fielddescr']),
771
					$info
772
				));
773
			}
774

    
775
			break;
776

    
777
		case "select":
778
			// Create a select element
779
			$optionlist = array();
780
			$selectedlist = array();
781

    
782
			$fieldname = $pkga['fieldname'];
783

    
784
			if (isset($pkga['multiple'])) {
785
				$multiple = 'multiple';
786
				$items = explode(',', $value);
787
				$fieldname .= "[]";
788
			} else {
789
				$multiple = '';
790
				$items = array($value);
791
			}
792

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

    
795
			foreach ($pkga['options']['option'] as $opt) {
796
				$optionlist[$opt['value']] = $opt['name'];
797

    
798
				if (in_array($opt['value'], $items)) {
799
					array_push($selectedlist, $opt['value']);
800
				}
801
			}
802

    
803
			if (isset($pkga['advancedfield']) && isset($advfield_count))
804
				$function = $grouping ? $advanced->add:$advanced->addInput;
805
			else
806
				$function = ($grouping) ? $section->add:$section->addInput;
807

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

    
836
			break;
837

    
838
		case "select_source":
839

    
840
			if (isset($pkga['multiple'])) {
841
				$items = explode(',', $value);
842
				$fieldname .= "[]";
843
			} else {
844
				$items = array($value);
845
			}
846

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

    
849
			$source_url = $pkga['source'];
850
			eval("\$pkg_source_txt = &$source_url;");
851

    
852
			#check if show disable option is present on xml
853
			if (isset($pkga['show_disable_value'])) {
854
				array_push($pkg_source_txt,
855
					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']));
856
			}
857

    
858
			$srcoptions = array();
859
			$srcselected = array();
860

    
861
			foreach ($pkg_source_txt as $opt) {
862
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
863
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
864
				$srcoptions[$source_value] = $source_name;
865

    
866
				if(in_array($source_value, $items))
867
					array_push($srcselected, $source_value);
868
			}
869

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

    
898
			break;
899

    
900
		case "vpn_selection" :
901
			$vpnlist = array();
902

    
903
			foreach ($config['ipsec']['phase1'] as $vpn) {
904
				$vpnlist[$vpn['descr']] = $vpn['descr'];
905

    
906
			}
907

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

    
933
			break;
934

    
935
		// Create a checkbox element
936
		case "checkbox":
937
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
938
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
939
				$onclick = 'javascript:enablechange();';
940
			else
941
				$onclick = '';
942

    
943
			if($grouping) {
944
				$group->add(new Form_Checkbox(
945
					$pkga['fieldname'],
946
					$pkga['fielddescr'],
947
					fixup_string($pkga['description']),
948
					($value == "on"),
949
					'on'
950
				))->setOnclick($onclick)
951
				  ->setOnchange($onchange)
952
				  ->setHelp($pkga['sethelp']);
953
			} else {
954
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
955
					$advanced->addInput(new Form_Checkbox(
956
						$pkga['fieldname'],
957
						$pkga['fielddescr'],
958
						fixup_string($pkga['description']),
959
						($value == "on"),
960
						'on'
961
					))->setOnclick($onclick)
962
					  ->setOnchange($onchange)
963
					  ->setHelp($pkga['sethelp']);
964
				} else {
965
					$section->addInput(new Form_Checkbox(
966
						$pkga['fieldname'],
967
						$pkga['fielddescr'],
968
						fixup_string($pkga['description']),
969
						($value == "on"),
970
						'on'
971
					))->setOnclick($onclick)
972
					  ->setOnchange($onchange)
973
					  ->setHelp($pkga['sethelp']);
974
				}
975
			}
976

    
977
			break;
978

    
979
		// Create a textarea element
980
		case "textarea":
981
			if ($pkga['rows']) {
982
				$rows = " rows='{$pkga['rows']}' ";
983
			}
984
			if ($pkga['cols']) {
985
				$cols = " cols='{$pkga['cols']}' ";
986
			}
987
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
988
				$value = base64_decode($value);
989
			}
990

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

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

    
1015
			break;
1016

    
1017
		case "aliases":
1018

    
1019
			// Use xml tag <typealiases> to filter type aliases
1020
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1021
			$fieldname = $pkga['fieldname'];
1022
			$a_aliases = &$config['aliases']['alias'];
1023
			$addrisfirst = 0;
1024
			$aliasesaddr = "";
1025
			$value = "value='{$value}'";
1026

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

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

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

    
1085
			echo $script;
1086

    
1087
			break;
1088

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

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

    
1130
			sort($ips);
1131
			if (isset($pkga['showlistenall'])) {
1132
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1133
			}
1134

    
1135
			if (!preg_match("/$interface_regex/", "loopback")) {
1136
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1137
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1138
			}
1139

    
1140
			#show interfaces array on gui
1141
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1142
			$multiple = '';
1143
			$fieldname = $pkga['fieldname'];
1144
			if (isset($pkga['multiple'])) {
1145
				$fieldname .= '[]';
1146
				$multiple = 'multiple';
1147
			}
1148

    
1149
			$selectedlist = array();
1150
			$optionlist = array();
1151

    
1152
			if (is_array($value)) {
1153
				$values = $value;
1154
			} else {
1155
				$values = explode(',', $value);
1156
			}
1157

    
1158
			foreach ($ips as $iface) {
1159
				if (in_array($iface['ip'], $values)) {
1160
					array_push($selectedlist, $iface['ip']);
1161
				}
1162

    
1163
				$optionlist[$iface['ip']] = $iface['description'];
1164
			}
1165

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

    
1194
			break;
1195

    
1196
		// Create radio button
1197
		case "radio":
1198
			if($grouping) {
1199
				$group->add(new Form_Checkbox(
1200
					$pkga['fieldname'],
1201
					$pkga['fielddescr'],
1202
					fixup_string($pkga['description']),
1203
					($value == "on"),
1204
					'on'
1205
				))->displayAsRadio();
1206
			} else {
1207
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1208
					$advanced->addInput(new Form_Checkbox(
1209
						$pkga['fieldname'],
1210
						$pkga['fielddescr'],
1211
						fixup_string($pkga['description']),
1212
						($value == "on"),
1213
						'on'
1214
					))->displayAsRadio();
1215
				} else {
1216
					$section->addInput(new Form_Checkbox(
1217
						$pkga['fieldname'],
1218
						$pkga['fielddescr'],
1219
						fixup_string($pkga['description']),
1220
						($value == "on"),
1221
						'on'
1222
					))->displayAsRadio();
1223
				}
1224
			}
1225

    
1226
			break;
1227

    
1228
		// Create form button
1229
		case "button":
1230
			$newbtn = new Form_Button(
1231
				$pkga['fieldname'],
1232
				$pkga['fieldname']
1233
			);
1234

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

    
1254
			break;
1255

    
1256
		case "schedule_selection":
1257

    
1258
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1259
			$schedules = array();
1260
			$schedules[] = "none";
1261
			if (is_array($config['schedules']['schedule'])) {
1262
				foreach ($config['schedules']['schedule'] as $schedule) {
1263
					if ($schedule['name'] != "") {
1264
						$schedules[] = $schedule['name'];
1265
					}
1266
				}
1267
			}
1268

    
1269
			foreach ($schedules as $schedule) {
1270
				if ($schedule == "none") {
1271
					$schedlist[""] = $schedule;
1272
				} else {
1273
					$schedlist[$schedule] = $schedule;
1274
				}
1275
			}
1276

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

    
1302
			break;
1303

    
1304
		case "rowhelper":
1305

    
1306
			$rowhelpername="row";
1307

    
1308
				$rowcounter = 0;
1309
				$trc = 0;
1310

    
1311
				//Use assigned $a_pkg or create an empty array to enter loop
1312
				if (isset($a_pkg[$id][$rowhelpername])) {
1313
					$saved_rows=$a_pkg[$id][$rowhelpername];
1314
				} else {
1315
					$saved_rows[] = array();
1316
				}
1317

    
1318
				$numrows = count($saved_rows) - 1;
1319

    
1320
				foreach ($saved_rows as $row) {
1321
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1322
					$group->addClass('repeatable');
1323

    
1324
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1325
						unset($value);
1326
						if ($rowhelper['value'] != "") {
1327
							$value = $rowhelper['value'];
1328
						}
1329
						$fieldname = $rowhelper['fieldname'];
1330
						$fielddescr = $rowhelper['fielddescr'];
1331

    
1332
						// if user is editing a record, load in the data.
1333
						if (isset($id) && $a_pkg[$id]) {
1334
							$value = $row[$fieldname];
1335
						}
1336

    
1337
						$type = $rowhelper['type'];
1338
						if ($type == "input" || $type == "password" || $type == "textarea" ) {
1339
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1340
								$value = base64_decode($value);
1341
							}
1342
						}
1343
						$fieldname = $rowhelper['fieldname'];
1344

    
1345
						if ($rowhelper['size']) {
1346
							$size = $rowhelper['size'];
1347
						} else if ($pkga['size']) {
1348
							$size = $pkga['size'];
1349
						} else {
1350
							$size = "8";
1351
						}
1352

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

    
1355
						$text = "";
1356
						$trc++;
1357
					}
1358

    
1359
					// Delete row button
1360
					$group->add(new Form_Button(
1361
						'deleterow' . $rowcounter,
1362
						'Delete'
1363
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1364

    
1365
					$rowcounter++;
1366
					$section->add($group);
1367
				}
1368

    
1369
			// Add row button
1370
			$section->addInput(new Form_Button(
1371
				'addrow',
1372
				'Add'
1373
			))->removeClass('btn-primary')->addClass('btn-success');
1374

    
1375
			break;
1376

    
1377
	}
1378

    
1379
		if ($pkga['combinefields'] == "end") {
1380
			$group->add(new Form_StaticText(
1381
				null,
1382
				null
1383
			));
1384

    
1385
			if($advanced)
1386
				$advanced->add($group);
1387
			else
1388
				$section->add($group);
1389

    
1390
			$grouping = false;
1391
		}
1392

    
1393
	#increment counter
1394
	$i++;
1395
} // e-o-foreach field described in the XML
1396

    
1397
$form->add($section);
1398

    
1399
$form->addGlobal(new Form_Input(
1400
	'id',
1401
	null,
1402
	'hidden',
1403
	$id
1404
));
1405

    
1406
// If we created an advanced section, add it (and a button) to the form here
1407
if(!empty($advanced)) {
1408
	$form->addGlobal(new Form_Button(
1409
		'showadv',
1410
		'Show advanced options'
1411
	))->removeClass('btn-primary')->addClass('btn-default');
1412

    
1413
	$form->add($advanced);
1414
}
1415

    
1416
print($form);
1417

    
1418
if ($pkg['note'] != "") {
1419
	print_info_box($pkg['note']);
1420

    
1421
if ($pkg['custom_php_after_form_command'])
1422
	eval($pkg['custom_php_after_form_command']);
1423
}
1424

    
1425
if ($pkg['fields']['field'] != "") { ?>
1426
<script type="text/javascript">
1427
//<![CDATA[
1428
	events.push(function(){
1429

    
1430
	// Hide the advanced section
1431
	var advanced_visible = false;
1432

    
1433
	// Hide on page load
1434
	$('.advancedoptions').hide();
1435

    
1436
	// Suppress "Delete row" button if there are fewer than two rows
1437
	checkLastRow();
1438

    
1439
	// Show advanced section if you click the showadv button
1440
	$('#showadv').prop('type', 'button');
1441

    
1442
	$("#showadv").click(function() {
1443
		advanced_visible = !advanced_visible;
1444

    
1445
		if(advanced_visible) {
1446
			$('.advancedoptions').show();
1447
			$("#showadv").prop('value', 'Hide advanced Options');
1448
		}
1449
		else {
1450
			$('.advancedoptions').hide();
1451
			$("#showadv").prop('value', 'Show advanced Options');
1452
		}
1453
	});
1454

    
1455
	// Call enablechange function
1456
	enablechange();
1457
});
1458

    
1459
	function enablechange() {
1460
<?php
1461
	foreach ($pkg['fields']['field'] as $field) {
1462
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1463
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1464

    
1465
			if (isset($field['enablefields'])) {
1466
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1467
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1468
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1469
					echo "\t\t}\n";
1470
				}
1471
			}
1472

    
1473
			if (isset($field['checkenablefields'])) {
1474
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1475
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1476
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1477
					echo "\t\t}\n";
1478
				}
1479
			}
1480

    
1481
			echo "\t}\n\telse {\n";
1482

    
1483
			if (isset($field['enablefields'])) {
1484
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1485
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1486
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1487
					echo "\t\t}\n";
1488
				}
1489
			}
1490

    
1491
			if (isset($field['checkenablefields'])) {
1492
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1493
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1494
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1495
					echo "\t\t}\n";
1496
				}
1497
			}
1498

    
1499
			echo "\t}\n";
1500
		}
1501
	}
1502
	?>
1503
	}
1504
//]]>
1505
</script>
1506

    
1507
<?php
1508
}
1509

    
1510
include("foot.inc");
(112-112/228)