Project

General

Profile

Download (40.7 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(gettext("No valid package defined."), 'danger', false);
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, $ewidth = null) {
303
	global $text, $group;
304

    
305
	switch ($type) {
306
		case "input":
307
			$inpt = new Form_Input(
308
				$fieldname . $trc,
309
				null,
310
				'text',
311
				$value
312
			);
313

    
314
			$inpt->setHelp($description);
315

    
316
			if ($ewidth) {
317
				$inpt->setWidth($ewidth);
318
			}
319

    
320
			$group->add($inpt);
321
			break;
322
		case "checkbox":
323
			$group->add(new Form_Checkbox(
324
				$fieldname . $trc,
325
				null,
326
				null,
327
				$value,
328
				'ON'
329
			))->setHelp($description);
330

    
331
			break;
332
		case "password":
333
			$group->add(new Form_Input(
334
				$fieldname . $trc,
335
				null,
336
				'password',
337
				$value
338
			))->setHelp($description);
339
			break;
340
		case "textarea":
341
			$group->add(new Form_Textarea(
342
				$fieldname . $trc,
343
				null,
344
				$value
345
			))->setHelp($description);
346

    
347
			break;
348
		case "select":
349
			$options = array();
350
			foreach ($rowhelper['options']['option'] as $rowopt) {
351
				$options[$rowopt['value']] = $rowopt['name'];
352
			}
353

    
354
			$grp = new Form_Select(
355
				$fieldname . $trc,
356
				null,
357
				$value,
358
				$options
359
			);
360

    
361
			$grp->setHelp($description);
362

    
363
			if ($ewidth) {
364
				$grp->setWidth($ewidth);
365
			}
366

    
367
			$group->add($grp);
368

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

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

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

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

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

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

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

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

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

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

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

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

    
440
			break;
441
	}
442
}
443

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

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

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

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

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

    
530
//breadcrumb
531
if ($pkg['title'] != "") {
532
	if (!$only_edit) {
533
		$pkg['title'] = $pkg['title'] . '/Edit';
534
	}
535

    
536
	if (strpos($pkg['title'], '/')) {
537
		$title = explode('/', $pkg['title']);
538

    
539
		foreach ($title as $subtitle) {
540
			$pgtitle[] = gettext($subtitle);
541
		}
542
	} else {
543
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
544
	}
545
} else {
546
	$pgtitle = array(gettext("Package"), gettext("Editor"));
547
}
548

    
549
// Create any required tabs
550
if ($pkg['tabs'] != "") {
551
	$tab_array = array();
552
	foreach ($pkg['tabs']['tab'] as $tab) {
553
		if ($tab['tab_level']) {
554
			$tab_level = $tab['tab_level'];
555
		} else {
556
			$tab_level = 1;
557
		}
558

    
559
		if (isset($tab['active'])) {
560
			$active = true;
561
			$pgtitle[] = $tab['text'] ;
562
		} else {
563
			$active = false;
564
		}
565

    
566
		if (isset($tab['no_drop_down'])) {
567
			$no_drop_down = true;
568
		}
569

    
570
		$urltmp = "";
571
		if ($tab['url'] != "") {
572
			$urltmp = $tab['url'];
573
		}
574

    
575
		if ($tab['xml'] != "") {
576
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
577
		}
578

    
579
		$addresswithport = getenv("HTTP_HOST");
580
		$colonpos = strpos($addresswithport, ":");
581

    
582
		if ($colonpos !== False) {
583
			//my url is actually just the IP address of the pfsense box
584
			$myurl = substr($addresswithport, 0, $colonpos);
585
		} else {
586
			$myurl = $addresswithport;
587
		}
588

    
589
		// eval url so that above $myurl item can be processed if need be.
590
		$url = str_replace('$myurl', $myurl, $urltmp);
591

    
592
		$tab_array[$tab_level][] = array(
593
			$tab['text'],
594
			$active,
595
			$url
596
		);
597
	}
598

    
599
	ksort($tab_array);
600
}
601

    
602
include("head.inc");
603
if ($pkg['custom_php_after_head_command']) {
604
	eval($pkg['custom_php_after_head_command']);
605
}
606
if (isset($tab_array)) {
607
	foreach ($tab_array as $tabid => $tab) {
608
		display_top_tabs($tab); //, $no_drop_down, $tabid);
609
	}
610
}
611

    
612
// Start of page display
613
if ($input_errors) {
614
	print_input_errors($input_errors);
615
}
616

    
617
if ($savemsg) {
618
	print_info_box($savemsg, 'success');
619
}
620

    
621
$cols = 0;
622
$savevalue = gettext("Save");
623
if ($pkg['savetext'] != "") {
624
	$savevalue = $pkg['savetext'];
625
}
626

    
627
$savehelp = "";
628
if ($pkg['savehelp'] != "") {
629
	$savehelp = $pkg['savehelp'];
630
}
631

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

    
634
$savebutton = new Form_Button(
635
	'submit',
636
	$savevalue
637
);
638

    
639
if ($savehelp) {
640
	$savebutton->setHelp($savehelp);
641
}
642

    
643
$form = new Form($savebutton);
644

    
645
$form->addGlobal(new Form_Input(
646
	'xml',
647
	null,
648
	'hidden',
649
	$xml
650
));
651

    
652
/* If a package's XML has <advanced_options/> configured, then setup
653
 * the section for the fields that have <advancedfield/> set.
654
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
655
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
656
 */
657

    
658
if ($pkg['advanced_options'] == "enabled") {
659
	$advfield_count = 0;
660
	$advanced = new Form_Section("Advanced Features");
661
	$advanced->addClass('advancedoptions');
662
}
663

    
664
$js_array = array();
665

    
666
// Now loop through all of the fields defined in the XML
667
foreach ($pkg['fields']['field'] as $pkga) {
668

    
669
	$action = "";
670
	$uid = "";
671

    
672
	if ($pkga['type'] == "sorting") {
673
		continue;
674
	}
675

    
676
	// Generate a new section
677
	if ($pkga['type'] == "listtopic") {
678
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
679
			$advanced->addInput(new Form_StaticText(
680
				strip_tags($pkga['name']),
681
				null
682
			));
683

    
684
			$advfield_count++;
685
		}  else {
686
			if (isset($section)) {
687
				$form->add($section);
688
			}
689

    
690
			if (isset($pkga['collapse'])) {
691
				$uid = uniqid("section");
692

    
693
				$action = COLLAPSIBLE;
694

    
695
				if ($pkga['collapse'] == "open") {
696
					$action |= SEC_OPEN;
697
				} else {
698
					$action |= SEC_CLOSED;
699
				}
700
			}
701

    
702
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
703
		}
704

    
705
		continue;
706
	}
707

    
708
	// 'begin' starts a form group. ('end' ends it)
709
	if ($pkga['combinefields'] == "begin") {
710
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
711
		$grouping = true;
712
	}
713

    
714
	$size = "";
715
	$colspan="";
716

    
717
	// if user is editing a record, load in the data.
718
	$fieldname = $pkga['fieldname'];
719
	unset($value);
720
	if ($get_from_post) {
721
		$value = $_POST[$fieldname];
722
		if (is_array($value)) {
723
			$value = implode(',', $value);
724
		}
725
	} else {
726
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
727
			$value = $a_pkg[$id][$fieldname];
728
		} else {
729
			if (isset($pkga['default_value'])) {
730
				$value = $pkga['default_value'];
731
			}
732
		}
733
	}
734

    
735
	// If we get here but have no $section, the package config file probably had no listtopic field
736
	// We can create a section with a generic name to fix that
737
	if (!$section) {
738
		$section = new Form_Section('General Options');
739
	}
740

    
741
	switch ($pkga['type']) {
742
		// Create an input element. The format is slightly different depending on whether we are composing a group,
743
		// section, or advanced section. This is true for every element type
744
		case "input":
745
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
746
				$value = base64_decode($value);
747
			}
748

    
749
			$grp = new Form_Input(
750
					$pkga['fieldname'],
751
					$pkga['fielddescr'],
752
					'text',
753
					$value
754
				);
755

    
756
			$grp->setHelp($pkga['description']);
757

    
758
			if ($pkga['width']) {
759
				$grp->setWidth($pkga['width']);
760
			}
761

    
762
			if ($grouping) {
763
				$group->add($grp);
764
			} else {
765
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
766
					$advanced->addInput($grp);
767
				} else {
768
					$section->addInput($grp);
769
				}
770
			}
771

    
772
			break;
773

    
774
		case "password":
775
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
776
				$value = base64_decode($value);
777
			}
778

    
779
			// Create a password element
780
			if ($grouping) {
781
				$group->add(new Form_Input(
782
					$pkga['fieldname'],
783
					$pkga['fielddescr'],
784
					'password',
785
					$value
786
				))->setHelp($pkga['description']);
787
			} else {
788
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
789
					$advanced->addInput(new Form_Input(
790
						$pkga['fieldname'],
791
						$pkga['fielddescr'],
792
						'password',
793
						$value
794
					))->setHelp($pkga['description']);
795
				} else {
796
					$section->addInput(new Form_Input(
797
						$pkga['fieldname'],
798
						$pkga['fielddescr'],
799
						'password',
800
						$value
801
					))->setHelp($pkga['description']);
802
				}
803
			}
804

    
805
			break;
806

    
807
		case "info":
808
			// If the info contains a table we should detect and Bootstrap it
809

    
810
			if (strpos($pkga['description'], '<table') !== FALSE) {
811
				$info = bootstrapTable($pkga['description']);
812
			} else {
813
				$info = $pkga['description'];
814
			}
815

    
816
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
817
				$advanced->addInput(new Form_StaticText(
818
					strip_tags($pkga['fielddescr']),
819
					$info
820
				));
821
			} else {
822
				$section->addInput(new Form_StaticText(
823
					strip_tags($pkga['fielddescr']),
824
					$info
825
				));
826
			}
827

    
828
			break;
829

    
830
		case "select":
831
			// Create a select element
832
			$optionlist = array();
833
			$selectedlist = array();
834

    
835
			$fieldname = $pkga['fieldname'];
836

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

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

    
848
			foreach ($pkga['options']['option'] as $opt) {
849
				$optionlist[$opt['value']] = $opt['name'];
850

    
851
				if (in_array($opt['value'], $items)) {
852
					array_push($selectedlist, $opt['value']);
853
				}
854
			}
855

    
856
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
857
				$function = $grouping ? $advanced->add:$advanced->addInput;
858
			} else {
859
				$function = ($grouping) ? $section->add:$section->addInput;
860
			}
861

    
862
			$grp = new Form_Select(
863
						$pkga['fieldname'],
864
						strip_tags($pkga['fielddescr']),
865
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
866
						$optionlist,
867
						isset($pkga['multiple'])
868
					);
869

    
870
			$grp ->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
871

    
872
			if ($pkga['width']) {
873
				$grp->setWidth($pkga['width']);
874
			}
875

    
876
			if ($grouping) {
877
				$group->add($grp);
878
			} else {
879
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
880
					$advanced->addInput($grp);
881
				} else {
882
					$section->addInput($grp);
883
				}
884
			}
885

    
886
			break;
887

    
888
		case "select_source":
889

    
890
			if (isset($pkga['multiple'])) {
891
				$items = explode(',', $value);
892
				$fieldname .= "[]";
893
			} else {
894
				$items = array($value);
895
			}
896

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

    
899
			$source_url = $pkga['source'];
900
			eval("\$pkg_source_txt = &$source_url;");
901

    
902
			#check if show disable option is present on xml
903
			if (!is_array($pkg_source_txt)) {
904
				$pkg_source_txt = array();
905
			}
906
			if (isset($pkga['show_disable_value'])) {
907
				array_push($pkg_source_txt,
908
					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']));
909
			}
910

    
911
			$srcoptions = array();
912
			$srcselected = array();
913

    
914
			foreach ($pkg_source_txt as $opt) {
915
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
916
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
917
				$srcoptions[$source_value] = $source_name;
918

    
919
				if (in_array($source_value, $items)) {
920
					array_push($srcselected, $source_value);
921
				}
922
			}
923

    
924
			if ($grouping) {
925
				$group->add(new Form_Select(
926
					$pkga['fieldname'],
927
					strip_tags($pkga['fielddescr']),
928
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
929
					$srcoptions,
930
					isset($pkga['multiple'])
931
				))->setOnchange($onchange);
932
			} else {
933
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
934
					$advanced->addInput(new Form_Select(
935
						$pkga['fieldname'],
936
						strip_tags($pkga['fielddescr']),
937
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
938
						$srcoptions,
939
						isset($pkga['multiple'])
940
					))->setOnchange($onchange);
941
				} else {
942
					$section->addInput(new Form_Select(
943
						$pkga['fieldname'],
944
						strip_tags($pkga['fielddescr']),
945
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
946
						$srcoptions,
947
						isset($pkga['multiple'])
948
					))->setOnchange($onchange);
949
				}
950
			}
951

    
952
			break;
953

    
954
		case "vpn_selection" :
955
			$vpnlist = array();
956

    
957
			foreach ($config['ipsec']['phase1'] as $vpn) {
958
				$vpnlist[$vpn['descr']] = $vpn['descr'];
959

    
960
			}
961

    
962
			if ($grouping) {
963
				$group->add(new Form_Select(
964
					$pkga['fieldname'],
965
					null,
966
					false,
967
					$vpnlist
968
				))->setHelp(fixup_string($pkga['description']));
969
			} else {
970
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
971
					$advanced->addInput(new Form_Select(
972
						$pkga['fieldname'],
973
						null,
974
						false,
975
						$vpnlist
976
					))->setHelp(fixup_string($pkga['description']));
977
				} else {
978
					$section->addInput(new Form_Select(
979
						$pkga['fieldname'],
980
						null,
981
						false,
982
						$vpnlist
983
					))->setHelp(fixup_string($pkga['description']));
984
				}
985
			}
986

    
987
			break;
988

    
989
		// Create a checkbox element
990
		case "checkbox":
991
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
992
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
993
				$onclick = 'javascript:enablechange();';
994
			} else {
995
				$onclick = '';
996
			}
997

    
998
			if ($grouping) {
999
				$group->add(new Form_Checkbox(
1000
					$pkga['fieldname'],
1001
					$pkga['fielddescr'],
1002
					fixup_string($pkga['description']),
1003
					($value == "on"),
1004
					'on'
1005
				))->setOnclick($onclick)
1006
				  ->setOnchange($onchange)
1007
				  ->setHelp($pkga['sethelp']);
1008
			} else {
1009
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1010
					$advanced->addInput(new Form_Checkbox(
1011
						$pkga['fieldname'],
1012
						$pkga['fielddescr'],
1013
						fixup_string($pkga['description']),
1014
						($value == "on"),
1015
						'on'
1016
					))->setOnclick($onclick)
1017
					  ->setOnchange($onchange)
1018
					  ->setHelp($pkga['sethelp']);
1019
				} else {
1020
					$section->addInput(new Form_Checkbox(
1021
						$pkga['fieldname'],
1022
						$pkga['fielddescr'],
1023
						fixup_string($pkga['description']),
1024
						($value == "on"),
1025
						'on'
1026
					))->setOnclick($onclick)
1027
					  ->setOnchange($onchange)
1028
					  ->setHelp($pkga['sethelp']);
1029
				}
1030
			}
1031

    
1032
			break;
1033

    
1034
		// Create a textarea element
1035
		case "textarea":
1036
			$rows = $cols = 0;
1037

    
1038
			if ($pkga['rows']) {
1039
				$rows = $pkga['rows'];
1040
			}
1041
			if ($pkga['cols']) {
1042
				$cols = $pkga['cols'];
1043
			}
1044

    
1045
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1046
				$value = base64_decode($value);
1047
			}
1048

    
1049
			$grp = new Form_Textarea(
1050
					$pkga['fieldname'],
1051
					$pkga['fielddescr'],
1052
					$value
1053
			);
1054

    
1055
			$grp->setHelp(fixup_string($pkga['description']));
1056

    
1057
			if ($rows > 0) {
1058
				$grp->setRows($rows);
1059
			}
1060

    
1061
			if ($cols > 0) {
1062
				$grp->setCols($cols);
1063
			}
1064

    
1065
			if ($pkga['wrap'] == "off") {
1066
				$grp->setAttribute("wrap", "off");
1067
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1068
			} else {
1069
				$grp->setAttribute("style", "width: auto;");
1070
			}
1071

    
1072
			if ($grouping) {
1073
				$group->add($grp);
1074
			} else {
1075
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1076
					$advanced->addInput($grp);
1077
				} else {
1078
					$section->addInput($grp);
1079
				}
1080
			}
1081

    
1082
			break;
1083

    
1084
		case "aliases":
1085

    
1086
			// Use xml tag <typealiases> to filter type aliases
1087
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1088
			$fieldname = $pkga['fieldname'];
1089
			$a_aliases = &$config['aliases']['alias'];
1090
			$addrisfirst = 0;
1091
			$aliasesaddr = "";
1092

    
1093
			if (isset($a_aliases)) {
1094
				if (!empty($pkga['typealiases'])) {
1095
					foreach ($a_aliases as $alias) {
1096
						if ($alias['type'] == $pkga['typealiases']) {
1097
							if ($addrisfirst == 1) {
1098
								$aliasesaddr .= ",";
1099
							}
1100
							$aliasesaddr .= "'" . $alias['name'] . "'";
1101
							$addrisfirst = 1;
1102
						}
1103
					}
1104
				} else {
1105
					foreach ($a_aliases as $alias) {
1106
						if ($addrisfirst == 1) {
1107
							$aliasesaddr .= ",";
1108
						}
1109
						$aliasesaddr .= "'" . $alias['name'] . "'";
1110
						$addrisfirst = 1;
1111
					}
1112
				}
1113
			}
1114

    
1115
			$grp = new Form_Input(
1116
					$pkga['fieldname'],
1117
					$pkga['fielddescr'],
1118
					'text',
1119
					$value
1120
				);
1121

    
1122
			$grp->setHelp($pkga['description']);
1123

    
1124
			if ($pkga['width']) {
1125
				$grp->setWidth($pkga['width']);
1126
			}
1127

    
1128
			if (grouping) {
1129
				$group->add($grp);
1130
			} else {
1131
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1132
					$advanced->addInput($grp);
1133
				} else {
1134
					$section->addInput($grp);
1135
				}
1136
			}
1137

    
1138
			$script = "<script type='text/javascript'>\n";
1139
			$script .= "//<![CDATA[\n";
1140
			$script .= "events.push(function(){\n";
1141
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1142
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1143
			$script .= "		source: aliasarray\n";
1144
			$script .= "	})\n";
1145
			$script .= "});\n";
1146
			$script .= "//]]>\n";
1147
			$script .= "</script>";
1148

    
1149
			echo $script;
1150

    
1151
			break;
1152

    
1153
		case "interfaces_selection":
1154
			$ips = array();
1155
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1156
			if (is_array($config['interfaces'])) {
1157
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1158
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1159
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1160
						if (isset($pkga['showips'])) {
1161
							$iface_description .= " address";
1162
						}
1163
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1164
					}
1165
				}
1166
			}
1167

    
1168
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1169
				foreach ($config['virtualip']['vip'] as $vip) {
1170
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1171
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1172
					}
1173
					switch ($vip['mode']) {
1174
						case "ipalias":
1175
						case "carp":
1176
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1177
							break;
1178
						case "proxyarp":
1179
							if ($vip['type'] == "network") {
1180
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1181
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1182
								$len = $end - $start;
1183
								for ($i = 0; $i <= $len; $i++) {
1184
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1185
								}
1186
							} else {
1187
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1188
							}
1189
							break;
1190
					}
1191
				}
1192
			}
1193

    
1194
			sort($ips);
1195
			if (isset($pkga['showlistenall'])) {
1196
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1197
			}
1198

    
1199
			if (!preg_match("/$interface_regex/", "loopback")) {
1200
				$loopback_text = gettext("loopback");
1201
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1202
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1203
			}
1204

    
1205
			#show interfaces array on gui
1206
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1207
			$multiple = '';
1208
			$fieldname = $pkga['fieldname'];
1209
			if (isset($pkga['multiple'])) {
1210
				$fieldname .= '[]';
1211
				$multiple = 'multiple';
1212
			}
1213

    
1214
			$selectedlist = array();
1215
			$optionlist = array();
1216

    
1217
			if (is_array($value)) {
1218
				$values = $value;
1219
			} else {
1220
				$values = explode(',', $value);
1221
			}
1222

    
1223
			foreach ($ips as $iface) {
1224
				if (in_array($iface['ip'], $values)) {
1225
					array_push($selectedlist, $iface['ip']);
1226
				}
1227

    
1228
				$optionlist[$iface['ip']] = $iface['description'];
1229
			}
1230

    
1231
			if ($grouping) {
1232
				$group->add(new Form_Select(
1233
					$pkga['fieldname'],
1234
					$pkga['fielddescr'],
1235
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1236
					$optionlist,
1237
					isset($pkga['multiple'])
1238
				))->setHelp($pkga['description']);
1239
			} else {
1240
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1241
					$advanced->addInput(new Form_Select(
1242
						$pkga['fieldname'],
1243
						$pkga['fielddescr'],
1244
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1245
						$optionlist,
1246
						isset($pkga['multiple'])
1247
					))->setHelp($pkga['description']);
1248
				} else {
1249
					$section->addInput(new Form_Select(
1250
						$pkga['fieldname'],
1251
						$pkga['fielddescr'],
1252
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1253
						$optionlist,
1254
						isset($pkga['multiple'])
1255
					))->setHelp($pkga['description']);
1256
				}
1257
			}
1258

    
1259
			break;
1260

    
1261
		// Create radio button
1262
		case "radio":
1263
			if ($grouping) {
1264
				$group->add(new Form_Checkbox(
1265
					$pkga['fieldname'],
1266
					$pkga['fielddescr'],
1267
					fixup_string($pkga['description']),
1268
					($value == "on"),
1269
					'on'
1270
				))->displayAsRadio();
1271
			} else {
1272
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1273
					$advanced->addInput(new Form_Checkbox(
1274
						$pkga['fieldname'],
1275
						$pkga['fielddescr'],
1276
						fixup_string($pkga['description']),
1277
						($value == "on"),
1278
						'on'
1279
					))->displayAsRadio();
1280
				} else {
1281
					$section->addInput(new Form_Checkbox(
1282
						$pkga['fieldname'],
1283
						$pkga['fielddescr'],
1284
						fixup_string($pkga['description']),
1285
						($value == "on"),
1286
						'on'
1287
					))->displayAsRadio();
1288
				}
1289
			}
1290

    
1291
			break;
1292

    
1293
		// Create form button
1294
		case "button":
1295
			$newbtn = new Form_Button(
1296
				$pkga['fieldname'],
1297
				$pkga['fieldname']
1298
			);
1299

    
1300
			if (grouping) {
1301
				$group->add(new Form_StaticText(
1302
					null,
1303
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1304
				));
1305
			} else {
1306
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1307
					$advanced->addInput(new Form_StaticText(
1308
						null,
1309
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1310
					));
1311
				} else {
1312
					$section->addInput(new Form_StaticText(
1313
						null,
1314
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1315
					));
1316
				}
1317
			}
1318

    
1319
			break;
1320

    
1321
		case "schedule_selection":
1322

    
1323
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1324
			$schedules = array();
1325
			$schedules[] = "none";
1326
			if (is_array($config['schedules']['schedule'])) {
1327
				foreach ($config['schedules']['schedule'] as $schedule) {
1328
					if ($schedule['name'] != "") {
1329
						$schedules[] = $schedule['name'];
1330
					}
1331
				}
1332
			}
1333

    
1334
			foreach ($schedules as $schedule) {
1335
				if ($schedule == "none") {
1336
					$schedlist[""] = $schedule;
1337
				} else {
1338
					$schedlist[$schedule] = $schedule;
1339
				}
1340
			}
1341

    
1342
			if ($grouping) {
1343
				$group->add(new Form_Select(
1344
					$pkga['fieldname'],
1345
					$pkga['fielddescr'],
1346
					$value,
1347
					$schedlist
1348
				))->setHelp(fixup_string($pkga['description']));
1349
			} else {
1350
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1351
					$advanced->addInput(new Form_Select(
1352
						$pkga['fieldname'],
1353
						$pkga['fielddescr'],
1354
						$value,
1355
						$schedlist
1356
					))->setHelp(fixup_string($pkga['description']));
1357
				} else {
1358
					$section->addInput(new Form_Select(
1359
						$pkga['fieldname'],
1360
						$pkga['fielddescr'],
1361
						$value,
1362
						$schedlist
1363
					))->setHelp(fixup_string($pkga['description']));
1364
				}
1365
			}
1366

    
1367
			break;
1368

    
1369
		case "rowhelper":
1370

    
1371
			$rowhelpername="row";
1372

    
1373
				$rowcounter = 0;
1374
				$trc = 0;
1375

    
1376
				//Use assigned $a_pkg or create an empty array to enter loop
1377
				if (isset($a_pkg[$id][$rowhelpername])) {
1378
					$saved_rows=$a_pkg[$id][$rowhelpername];
1379
				} else {
1380
					$saved_rows[] = array();
1381
				}
1382

    
1383
				$numrows = count($saved_rows) - 1;
1384

    
1385
				foreach ($saved_rows as $row) {
1386
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1387
					$group->addClass('repeatable');
1388

    
1389
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1390
						unset($value);
1391
						$width = null;
1392

    
1393
						if ($rowhelper['value'] != "") {
1394
							$value = $rowhelper['value'];
1395
						}
1396
						$fieldname = $rowhelper['fieldname'];
1397
						$fielddescr = $rowhelper['fielddescr'];
1398

    
1399
						// if user is editing a record, load in the data.
1400
						if (isset($id) && $a_pkg[$id]) {
1401
							$value = $row[$fieldname];
1402
						}
1403

    
1404
						$type = $rowhelper['type'];
1405
						if ($type == "input" || $type == "password" || $type == "textarea") {
1406
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1407
								$value = base64_decode($value);
1408
							}
1409
						}
1410
						$fieldname = $rowhelper['fieldname'];
1411

    
1412
						if ($rowhelper['size']) {
1413
							$size = $rowhelper['size'];
1414
						} else if ($pkga['size']) {
1415
							$size = $pkga['size'];
1416
						} else {
1417
							$size = "8";
1418
						}
1419

    
1420
						if ($rowhelper['width']) {
1421
							$width = $rowhelper['width'];
1422
						}
1423

    
1424
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, ($numrows == $rowcounter) ? $fielddescr:null, $width);
1425

    
1426
						$text = "";
1427
						$trc++;
1428
					}
1429

    
1430
					// Delete row button
1431
					$group->add(new Form_Button(
1432
						'deleterow' . $rowcounter,
1433
						'Delete',
1434
						null,
1435
						'fa-trash'
1436
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1437

    
1438
					$rowcounter++;
1439
					$section->add($group);
1440
				}
1441

    
1442
			// Add row button
1443
			$section->addInput(new Form_Button(
1444
				'addrow',
1445
				'Add'
1446
			))->removeClass('btn-primary')->addClass('btn-success');
1447

    
1448
			break;
1449

    
1450
	}
1451

    
1452
		if ($pkga['combinefields'] == "end") {
1453
			$group->add(new Form_StaticText(
1454
				null,
1455
				null
1456
			));
1457

    
1458
			if ($advanced) {
1459
				$advanced->add($group);
1460
			} else {
1461
				$section->add($group);
1462
			}
1463

    
1464
			$grouping = false;
1465
		}
1466

    
1467
	#increment counter
1468
	$i++;
1469
} // e-o-foreach field described in the XML
1470

    
1471
$form->add($section);
1472

    
1473
$form->addGlobal(new Form_Input(
1474
	'id',
1475
	null,
1476
	'hidden',
1477
	$id
1478
));
1479

    
1480
// If we created an advanced section, add it (and a button) to the form here
1481
if (!empty($advanced)) {
1482
	$form->addGlobal(new Form_Button(
1483
		'showadv',
1484
		'Show advanced options'
1485
	))->removeClass('btn-primary')->addClass('btn-default');
1486

    
1487
	$form->add($advanced);
1488
}
1489

    
1490
print($form);
1491

    
1492
if ($pkg['note'] != "") {
1493
	print_info_box($pkg['note'], 'info');
1494
}
1495

    
1496
if ($pkg['custom_php_after_form_command']) {
1497
	eval($pkg['custom_php_after_form_command']);
1498
}
1499

    
1500
if ($pkg['fields']['field'] != "") { ?>
1501
<script type="text/javascript">
1502
//<![CDATA[
1503
	events.push(function() {
1504

    
1505
	// Hide the advanced section
1506
	var advanced_visible = false;
1507

    
1508
	// Hide on page load
1509
	$('.advancedoptions').hide();
1510

    
1511
	// Suppress "Delete row" button if there are fewer than two rows
1512
	checkLastRow();
1513

    
1514
	// Show advanced section if you click the showadv button
1515
	$('#showadv').prop('type', 'button');
1516

    
1517
	$("#showadv").click(function() {
1518
		advanced_visible = !advanced_visible;
1519

    
1520
		if (advanced_visible) {
1521
			$('.advancedoptions').show();
1522
			$("#showadv").prop('value', 'Hide advanced Options');
1523
		} else {
1524
			$('.advancedoptions').hide();
1525
			$("#showadv").prop('value', 'Show advanced Options');
1526
		}
1527
	});
1528

    
1529
	// Call enablechange function
1530
	enablechange();
1531
});
1532

    
1533
	function enablechange() {
1534
<?php
1535
	foreach ($pkg['fields']['field'] as $field) {
1536
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1537
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1538

    
1539
			if (isset($field['enablefields'])) {
1540
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1541
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1542
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1543
					echo "\t\t}\n";
1544
				}
1545
			}
1546

    
1547
			if (isset($field['checkenablefields'])) {
1548
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1549
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1550
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1551
					echo "\t\t}\n";
1552
				}
1553
			}
1554

    
1555
			echo "\t}\n\telse {\n";
1556

    
1557
			if (isset($field['enablefields'])) {
1558
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1559
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1560
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1561
					echo "\t\t}\n";
1562
				}
1563
			}
1564

    
1565
			if (isset($field['checkenablefields'])) {
1566
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1567
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1568
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1569
					echo "\t\t}\n";
1570
				}
1571
			}
1572

    
1573
			echo "\t}\n";
1574
		}
1575
	}
1576
	?>
1577
	}
1578
//]]>
1579
</script>
1580

    
1581
<?php
1582
}
1583

    
1584
include("foot.inc");
(101-101/229)