Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
257
			parse_package_templates();
258

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

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

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

    
285

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

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

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

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

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

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

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

    
347
			$group->add(new Form_Select(
348
				$fieldname . $trc,
349
				null,
350
				$value,
351
				$options
352
			))->setHelp($description);
353

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

    
368
			if (is_array($value)) {
369
				$values = $value;
370
			} else {
371
				$values = explode(',', $value);
372
			}
373

    
374
			$ifaces["lo0"] = "loopback";
375
			$options = array();
376
			$selected = array();
377

    
378
			foreach ($ifaces as $ifname => $iface) {
379
				$options[$ifname] = $iface;
380

    
381
				if (in_array($ifname, $values)) {
382
					array_push($selected, $ifname);
383
				}
384
			}
385

    
386
			$group->add(new Form_Select(
387
				$fieldname . $trc,
388
				null,
389
				($multiple) ? $selected:$selected[0],
390
				$options,
391
				$multiple
392
			))->setHelp($description);
393

    
394
			//echo "</select>\n";
395
			break;
396
		case "select_source":
397
			$options = array();
398
			$selected = array();
399

    
400
			if (isset($rowhelper['show_disable_value'])) {
401
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
402
			}
403

    
404
			$source_url = $rowhelper['source'];
405
			eval("\$pkg_source_txt = &$source_url;");
406

    
407
			foreach ($pkg_source_txt as $opt) {
408
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
409
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
410
				$options[$source_value] = $source_name;
411

    
412
				if ($source_value == $value) {
413
					array_push($selected, $value);
414
				}
415
			}
416

    
417
			$group->add(new Form_Select(
418
				$fieldname . $trc,
419
				null,
420
				($multiple) ? $selected:$selected[0],
421
				$options,
422
				$multiple
423
			))->setHelp($description);
424

    
425
			break;
426
	}
427
}
428

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

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

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

    
504
			/* replace cr's */
505
			$template_text = str_replace("\\n", "\n", $template_text);
506

    
507
			/* write out new template file */
508
			$fout = fopen($filename, "w");
509
			fwrite($fout, $template_text);
510
			fclose($fout);
511
		}
512
	}
513
}
514

    
515
//breadcrumb
516
if ($pkg['title'] != "") {
517
	if (!$only_edit) {
518
		$pkg['title'] = $pkg['title'] . '/Edit';
519
	}
520

    
521
	if (strpos($pkg['title'], '/')) {
522
		$title = explode('/', $pkg['title']);
523

    
524
		foreach ($title as $subtitle) {
525
			$pgtitle[] = gettext($subtitle);
526
		}
527
	} else {
528
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
529
	}
530
} else {
531
	$pgtitle = array(gettext("Package"), gettext("Editor"));
532
}
533

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

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

    
551
		if (isset($tab['no_drop_down'])) {
552
			$no_drop_down = true;
553
		}
554

    
555
		$urltmp = "";
556
		if ($tab['url'] != "") {
557
			$urltmp = $tab['url'];
558
		}
559

    
560
		if ($tab['xml'] != "") {
561
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
562
		}
563

    
564
		$addresswithport = getenv("HTTP_HOST");
565
		$colonpos = strpos($addresswithport, ":");
566

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

    
574
		// eval url so that above $myurl item can be processed if need be.
575
		$url = str_replace('$myurl', $myurl, $urltmp);
576

    
577
		$tab_array[$tab_level][] = array(
578
			$tab['text'],
579
			$active,
580
			$url
581
		);
582
	}
583

    
584
	ksort($tab_array);
585
}
586

    
587
if ($pkg['custom_php_after_head_command']) {
588
	$closehead = false;
589
	include("head.inc");
590
	eval($pkg['custom_php_after_head_command']);
591
} else {
592
	include("head.inc");
593
}
594
if (isset($tab_array)) {
595
	foreach ($tab_array as $tabid => $tab) {
596
		display_top_tabs($tab); //, $no_drop_down, $tabid);
597
	}
598
}
599

    
600
// Start of page display
601
if ($input_errors) {
602
	print_input_errors($input_errors);
603
}
604

    
605
if ($savemsg) {
606
	print_info_box($savemsg, 'success');
607
}
608

    
609
$cols = 0;
610
$savevalue = gettext("Save");
611
if ($pkg['savetext'] != "") {
612
	$savevalue = $pkg['savetext'];
613
}
614

    
615
$savehelp = gettext("");
616
if ($pkg['savehelp'] != "") {
617
	$savehelp = $pkg['savehelp'];
618
}
619

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

    
622
$savebutton = new Form_Button(
623
	'submit',
624
	$savevalue
625
);
626

    
627
if ($savehelp) {
628
	$savebutton->setHelp($savehelp);
629
}
630

    
631
$form = new Form($savebutton);
632

    
633
$form->addGlobal(new Form_Input(
634
	'xml',
635
	null,
636
	'hidden',
637
	$xml
638
));
639

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

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

    
652
$js_array = array();
653

    
654
// Now loop through all of the fields defined in the XML
655
foreach ($pkg['fields']['field'] as $pkga) {
656

    
657
	if ($pkga['type'] == "sorting") {
658
		continue;
659
	}
660

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

    
669
			$advfield_count++;
670
		}  else {
671
			if (isset($section)) {
672
				$form->add($section);
673
			}
674

    
675
			$section = new Form_Section(strip_tags($pkga['name']));
676
		}
677

    
678
		continue;
679
	}
680

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

    
687
	$size = "";
688
	$colspan="";
689

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

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

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

    
722
			if ($grouping) {
723
				$group->add(new Form_Input(
724
					$pkga['fieldname'],
725
					$pkga['fielddescr'],
726
					'text',
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
						'text',
735
						$value
736
					))->setHelp($pkga['description']);
737
				} else {
738
					$section->addInput(new Form_Input(
739
						$pkga['fieldname'],
740
						$pkga['fielddescr'],
741
						'text',
742
						$value
743
					))->setHelp($pkga['description']);
744
				}
745
			}
746

    
747
			break;
748

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

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

    
780
			break;
781

    
782
		case "info":
783
			// If the info contains a table we should detect and Bootstrap it
784

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

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

    
803
			break;
804

    
805
		case "select":
806
			// Create a select element
807
			$optionlist = array();
808
			$selectedlist = array();
809

    
810
			$fieldname = $pkga['fieldname'];
811

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

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

    
823
			foreach ($pkga['options']['option'] as $opt) {
824
				$optionlist[$opt['value']] = $opt['name'];
825

    
826
				if (in_array($opt['value'], $items)) {
827
					array_push($selectedlist, $opt['value']);
828
				}
829
			}
830

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

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

    
865
			break;
866

    
867
		case "select_source":
868

    
869
			if (isset($pkga['multiple'])) {
870
				$items = explode(',', $value);
871
				$fieldname .= "[]";
872
			} else {
873
				$items = array($value);
874
			}
875

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

    
878
			$source_url = $pkga['source'];
879
			eval("\$pkg_source_txt = &$source_url;");
880

    
881
			#check if show disable option is present on xml
882
			if (isset($pkga['show_disable_value'])) {
883
				array_push($pkg_source_txt,
884
					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']));
885
			}
886

    
887
			$srcoptions = array();
888
			$srcselected = array();
889

    
890
			foreach ($pkg_source_txt as $opt) {
891
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
892
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
893
				$srcoptions[$source_value] = $source_name;
894

    
895
				if (in_array($source_value, $items)) {
896
					array_push($srcselected, $source_value);
897
				}
898
			}
899

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

    
928
			break;
929

    
930
		case "vpn_selection" :
931
			$vpnlist = array();
932

    
933
			foreach ($config['ipsec']['phase1'] as $vpn) {
934
				$vpnlist[$vpn['descr']] = $vpn['descr'];
935

    
936
			}
937

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

    
963
			break;
964

    
965
		// Create a checkbox element
966
		case "checkbox":
967
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
968
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
969
				$onclick = 'javascript:enablechange();';
970
			} else {
971
				$onclick = '';
972
			}
973

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

    
1008
			break;
1009

    
1010
		// Create a textarea element
1011
		case "textarea":
1012
			if ($pkga['rows']) {
1013
				$rows = " rows='{$pkga['rows']}' ";
1014
			}
1015
			if ($pkga['cols']) {
1016
				$cols = " cols='{$pkga['cols']}' ";
1017
			}
1018
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1019
				$value = base64_decode($value);
1020
			}
1021

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

    
1024
			if ($grouping) {
1025
				$group->add(new Form_Textarea(
1026
					$pkga['fieldname'],
1027
					$pkga['fielddescr'],
1028
					$value
1029
				))->setHelp(fixup_string($pkga['description']));
1030
			} else {
1031
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1032
					$advanced->addInput(new Form_Textarea(
1033
						$pkga['fieldname'],
1034
						$pkga['fielddescr'],
1035
						$value
1036
					))->setHelp(fixup_string($pkga['description']));
1037
				} else {
1038
					$section->addInput(new Form_Textarea(
1039
						$pkga['fieldname'],
1040
						$pkga['fielddescr'],
1041
						$value
1042
					))->setHelp(fixup_string($pkga['description']));
1043
				}
1044
			}
1045

    
1046
			break;
1047

    
1048
		case "aliases":
1049

    
1050
			// Use xml tag <typealiases> to filter type aliases
1051
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1052
			$fieldname = $pkga['fieldname'];
1053
			$a_aliases = &$config['aliases']['alias'];
1054
			$addrisfirst = 0;
1055
			$aliasesaddr = "";
1056
			$value = "value='{$value}'";
1057

    
1058
			if (isset($a_aliases)) {
1059
				if (!empty($pkga['typealiases'])) {
1060
					foreach ($a_aliases as $alias) {
1061
						if ($alias['type'] == $pkga['typealiases']) {
1062
							if ($addrisfirst == 1) {
1063
								$aliasesaddr .= ",";
1064
							}
1065
							$aliasesaddr .= "'" . $alias['name'] . "'";
1066
							$addrisfirst = 1;
1067
						}
1068
					}
1069
				} else {
1070
					foreach ($a_aliases as $alias) {
1071
						if ($addrisfirst == 1) {
1072
							$aliasesaddr .= ",";
1073
						}
1074
						$aliasesaddr .= "'" . $alias['name'] . "'";
1075
						$addrisfirst = 1;
1076
					}
1077
				}
1078
			}
1079

    
1080
			if (grouping) {
1081
				$group->add(new Form_Input(
1082
					$pkga['fieldname'],
1083
					$pkga['fielddescr'],
1084
					'text',
1085
					$value
1086
				))->setHelp($pkga['description']);
1087
			} else {
1088
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1089
					$advanced->addInput(new Form_Input(
1090
						$pkga['fieldname'],
1091
						$pkga['fielddescr'],
1092
						'text',
1093
						$value
1094
					))->setHelp($pkga['description']);
1095
				} else {
1096
					$section->addInput(new Form_Input(
1097
						$pkga['fieldname'],
1098
						$pkga['fielddescr'],
1099
						'text',
1100
						$value
1101
					))->setHelp($pkga['description']);
1102
				}
1103
			}
1104

    
1105
			$script = "<script type='text/javascript'>\n";
1106
			$script .= "//<![CDATA[\n";
1107
			$script .= "events.push(function(){\n";
1108
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1109
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1110
			$script .= "		source: aliasarray\n";
1111
			$script .= "	})\n";
1112
			$script .= "});\n";
1113
			$script .= "//]]>\n";
1114
			$script .= "</script>";
1115

    
1116
			echo $script;
1117

    
1118
			break;
1119

    
1120
		case "interfaces_selection":
1121
			$ips = array();
1122
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1123
			if (is_array($config['interfaces'])) {
1124
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1125
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1126
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1127
						if (isset($pkga['showips'])) {
1128
							$iface_description .= " address";
1129
						}
1130
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1131
					}
1132
				}
1133
			}
1134

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

    
1161
			sort($ips);
1162
			if (isset($pkga['showlistenall'])) {
1163
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1164
			}
1165

    
1166
			if (!preg_match("/$interface_regex/", "loopback")) {
1167
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1168
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1169
			}
1170

    
1171
			#show interfaces array on gui
1172
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1173
			$multiple = '';
1174
			$fieldname = $pkga['fieldname'];
1175
			if (isset($pkga['multiple'])) {
1176
				$fieldname .= '[]';
1177
				$multiple = 'multiple';
1178
			}
1179

    
1180
			$selectedlist = array();
1181
			$optionlist = array();
1182

    
1183
			if (is_array($value)) {
1184
				$values = $value;
1185
			} else {
1186
				$values = explode(',', $value);
1187
			}
1188

    
1189
			foreach ($ips as $iface) {
1190
				if (in_array($iface['ip'], $values)) {
1191
					array_push($selectedlist, $iface['ip']);
1192
				}
1193

    
1194
				$optionlist[$iface['ip']] = $iface['description'];
1195
			}
1196

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

    
1225
			break;
1226

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

    
1257
			break;
1258

    
1259
		// Create form button
1260
		case "button":
1261
			$newbtn = new Form_Button(
1262
				$pkga['fieldname'],
1263
				$pkga['fieldname']
1264
			);
1265

    
1266
			if (grouping) {
1267
				$group->add(new Form_StaticText(
1268
					null,
1269
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1270
				));
1271
			} else {
1272
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1273
					$advanced->addInput(new Form_StaticText(
1274
						null,
1275
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1276
					));
1277
				} else {
1278
					$section->addInput(new Form_StaticText(
1279
						null,
1280
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1281
					));
1282
				}
1283
			}
1284

    
1285
			break;
1286

    
1287
		case "schedule_selection":
1288

    
1289
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1290
			$schedules = array();
1291
			$schedules[] = "none";
1292
			if (is_array($config['schedules']['schedule'])) {
1293
				foreach ($config['schedules']['schedule'] as $schedule) {
1294
					if ($schedule['name'] != "") {
1295
						$schedules[] = $schedule['name'];
1296
					}
1297
				}
1298
			}
1299

    
1300
			foreach ($schedules as $schedule) {
1301
				if ($schedule == "none") {
1302
					$schedlist[""] = $schedule;
1303
				} else {
1304
					$schedlist[$schedule] = $schedule;
1305
				}
1306
			}
1307

    
1308
			if ($grouping) {
1309
				$group->add(new Form_Select(
1310
					$pkga['fieldname'],
1311
					$pkga['fielddescr'],
1312
					$value,
1313
					$schedlist
1314
				))->setHelp(fixup_string($pkga['description']));
1315
			} else {
1316
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1317
					$advanced->addInput(new Form_Select(
1318
						$pkga['fieldname'],
1319
						$pkga['fielddescr'],
1320
						$value,
1321
						$schedlist
1322
					))->setHelp(fixup_string($pkga['description']));
1323
				} else {
1324
					$section->addInput(new Form_Select(
1325
						$pkga['fieldname'],
1326
						$pkga['fielddescr'],
1327
						$value,
1328
						$schedlist
1329
					))->setHelp(fixup_string($pkga['description']));
1330
				}
1331
			}
1332

    
1333
			break;
1334

    
1335
		case "rowhelper":
1336

    
1337
			$rowhelpername="row";
1338

    
1339
				$rowcounter = 0;
1340
				$trc = 0;
1341

    
1342
				//Use assigned $a_pkg or create an empty array to enter loop
1343
				if (isset($a_pkg[$id][$rowhelpername])) {
1344
					$saved_rows=$a_pkg[$id][$rowhelpername];
1345
				} else {
1346
					$saved_rows[] = array();
1347
				}
1348

    
1349
				$numrows = count($saved_rows) - 1;
1350

    
1351
				foreach ($saved_rows as $row) {
1352
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1353
					$group->addClass('repeatable');
1354

    
1355
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1356
						unset($value);
1357
						if ($rowhelper['value'] != "") {
1358
							$value = $rowhelper['value'];
1359
						}
1360
						$fieldname = $rowhelper['fieldname'];
1361
						$fielddescr = $rowhelper['fielddescr'];
1362

    
1363
						// if user is editing a record, load in the data.
1364
						if (isset($id) && $a_pkg[$id]) {
1365
							$value = $row[$fieldname];
1366
						}
1367

    
1368
						$type = $rowhelper['type'];
1369
						if ($type == "input" || $type == "password" || $type == "textarea") {
1370
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1371
								$value = base64_decode($value);
1372
							}
1373
						}
1374
						$fieldname = $rowhelper['fieldname'];
1375

    
1376
						if ($rowhelper['size']) {
1377
							$size = $rowhelper['size'];
1378
						} else if ($pkga['size']) {
1379
							$size = $pkga['size'];
1380
						} else {
1381
							$size = "8";
1382
						}
1383

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

    
1386
						$text = "";
1387
						$trc++;
1388
					}
1389

    
1390
					// Delete row button
1391
					$group->add(new Form_Button(
1392
						'deleterow' . $rowcounter,
1393
						'Delete'
1394
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1395

    
1396
					$rowcounter++;
1397
					$section->add($group);
1398
				}
1399

    
1400
			// Add row button
1401
			$section->addInput(new Form_Button(
1402
				'addrow',
1403
				'Add'
1404
			))->removeClass('btn-primary')->addClass('btn-success');
1405

    
1406
			break;
1407

    
1408
	}
1409

    
1410
		if ($pkga['combinefields'] == "end") {
1411
			$group->add(new Form_StaticText(
1412
				null,
1413
				null
1414
			));
1415

    
1416
			if ($advanced) {
1417
				$advanced->add($group);
1418
			} else {
1419
				$section->add($group);
1420
			}
1421

    
1422
			$grouping = false;
1423
		}
1424

    
1425
	#increment counter
1426
	$i++;
1427
} // e-o-foreach field described in the XML
1428

    
1429
$form->add($section);
1430

    
1431
$form->addGlobal(new Form_Input(
1432
	'id',
1433
	null,
1434
	'hidden',
1435
	$id
1436
));
1437

    
1438
// If we created an advanced section, add it (and a button) to the form here
1439
if (!empty($advanced)) {
1440
	$form->addGlobal(new Form_Button(
1441
		'showadv',
1442
		'Show advanced options'
1443
	))->removeClass('btn-primary')->addClass('btn-default');
1444

    
1445
	$form->add($advanced);
1446
}
1447

    
1448
print($form);
1449

    
1450
if ($pkg['note'] != "") {
1451
	print_info_box($pkg['note']);
1452
}
1453

    
1454
if ($pkg['custom_php_after_form_command']) {
1455
	eval($pkg['custom_php_after_form_command']);
1456
}
1457

    
1458
if ($pkg['fields']['field'] != "") { ?>
1459
<script type="text/javascript">
1460
//<![CDATA[
1461
	events.push(function() {
1462

    
1463
	// Hide the advanced section
1464
	var advanced_visible = false;
1465

    
1466
	// Hide on page load
1467
	$('.advancedoptions').hide();
1468

    
1469
	// Suppress "Delete row" button if there are fewer than two rows
1470
	checkLastRow();
1471

    
1472
	// Show advanced section if you click the showadv button
1473
	$('#showadv').prop('type', 'button');
1474

    
1475
	$("#showadv").click(function() {
1476
		advanced_visible = !advanced_visible;
1477

    
1478
		if (advanced_visible) {
1479
			$('.advancedoptions').show();
1480
			$("#showadv").prop('value', 'Hide advanced Options');
1481
		} else {
1482
			$('.advancedoptions').hide();
1483
			$("#showadv").prop('value', 'Show advanced Options');
1484
		}
1485
	});
1486

    
1487
	// Call enablechange function
1488
	enablechange();
1489
});
1490

    
1491
	function enablechange() {
1492
<?php
1493
	foreach ($pkg['fields']['field'] as $field) {
1494
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1495
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1496

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

    
1505
			if (isset($field['checkenablefields'])) {
1506
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1507
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1508
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1509
					echo "\t\t}\n";
1510
				}
1511
			}
1512

    
1513
			echo "\t}\n\telse {\n";
1514

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

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

    
1531
			echo "\t}\n";
1532
		}
1533
	}
1534
	?>
1535
	}
1536
//]]>
1537
</script>
1538

    
1539
<?php
1540
}
1541

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