Project

General

Profile

Download (40.6 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, $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 = gettext("");
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(gettext("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(gettext('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 (isset($pkga['show_disable_value'])) {
904
				array_push($pkg_source_txt,
905
					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']));
906
			}
907

    
908
			$srcoptions = array();
909
			$srcselected = array();
910

    
911
			foreach ($pkg_source_txt as $opt) {
912
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
913
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
914
				$srcoptions[$source_value] = $source_name;
915

    
916
				if (in_array($source_value, $items)) {
917
					array_push($srcselected, $source_value);
918
				}
919
			}
920

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

    
949
			break;
950

    
951
		case "vpn_selection" :
952
			$vpnlist = array();
953

    
954
			foreach ($config['ipsec']['phase1'] as $vpn) {
955
				$vpnlist[$vpn['descr']] = $vpn['descr'];
956

    
957
			}
958

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

    
984
			break;
985

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

    
995
			if ($grouping) {
996
				$group->add(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
			} else {
1006
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1007
					$advanced->addInput(new Form_Checkbox(
1008
						$pkga['fieldname'],
1009
						$pkga['fielddescr'],
1010
						fixup_string($pkga['description']),
1011
						($value == "on"),
1012
						'on'
1013
					))->setOnclick($onclick)
1014
					  ->setOnchange($onchange)
1015
					  ->setHelp($pkga['sethelp']);
1016
				} else {
1017
					$section->addInput(new Form_Checkbox(
1018
						$pkga['fieldname'],
1019
						$pkga['fielddescr'],
1020
						fixup_string($pkga['description']),
1021
						($value == "on"),
1022
						'on'
1023
					))->setOnclick($onclick)
1024
					  ->setOnchange($onchange)
1025
					  ->setHelp($pkga['sethelp']);
1026
				}
1027
			}
1028

    
1029
			break;
1030

    
1031
		// Create a textarea element
1032
		case "textarea":
1033
			$rows = $cols = 0;
1034

    
1035
			if ($pkga['rows']) {
1036
				$rows = $pkga['rows'];
1037
			}
1038
			if ($pkga['cols']) {
1039
				$cols = $pkga['cols'];
1040
			}
1041

    
1042
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1043
				$value = base64_decode($value);
1044
			}
1045

    
1046
			$grp = new Form_Textarea(
1047
					$pkga['fieldname'],
1048
					$pkga['fielddescr'],
1049
					$value
1050
			);
1051

    
1052
			$grp->setHelp(fixup_string($pkga['description']));
1053

    
1054
			if ($rows > 0) {
1055
				$grp->setRows($rows);
1056
			}
1057

    
1058
			if ($cols > 0) {
1059
				$grp->setCols($cols);
1060
			}
1061

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

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

    
1079
			break;
1080

    
1081
		case "aliases":
1082

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

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

    
1112
			$grp = new Form_Input(
1113
					$pkga['fieldname'],
1114
					$pkga['fielddescr'],
1115
					'text',
1116
					$value
1117
				);
1118

    
1119
			$grp->setHelp($pkga['description']);
1120

    
1121
			if ($pkga['width']) {
1122
				$grp->setWidth($pkga['width']);
1123
			}
1124

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

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

    
1146
			echo $script;
1147

    
1148
			break;
1149

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

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

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

    
1196
			if (!preg_match("/$interface_regex/", "loopback")) {
1197
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1198
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1199
			}
1200

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

    
1210
			$selectedlist = array();
1211
			$optionlist = array();
1212

    
1213
			if (is_array($value)) {
1214
				$values = $value;
1215
			} else {
1216
				$values = explode(',', $value);
1217
			}
1218

    
1219
			foreach ($ips as $iface) {
1220
				if (in_array($iface['ip'], $values)) {
1221
					array_push($selectedlist, $iface['ip']);
1222
				}
1223

    
1224
				$optionlist[$iface['ip']] = $iface['description'];
1225
			}
1226

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

    
1255
			break;
1256

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

    
1287
			break;
1288

    
1289
		// Create form button
1290
		case "button":
1291
			$newbtn = new Form_Button(
1292
				$pkga['fieldname'],
1293
				$pkga['fieldname']
1294
			);
1295

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

    
1315
			break;
1316

    
1317
		case "schedule_selection":
1318

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

    
1330
			foreach ($schedules as $schedule) {
1331
				if ($schedule == "none") {
1332
					$schedlist[""] = $schedule;
1333
				} else {
1334
					$schedlist[$schedule] = $schedule;
1335
				}
1336
			}
1337

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

    
1363
			break;
1364

    
1365
		case "rowhelper":
1366

    
1367
			$rowhelpername="row";
1368

    
1369
				$rowcounter = 0;
1370
				$trc = 0;
1371

    
1372
				//Use assigned $a_pkg or create an empty array to enter loop
1373
				if (isset($a_pkg[$id][$rowhelpername])) {
1374
					$saved_rows=$a_pkg[$id][$rowhelpername];
1375
				} else {
1376
					$saved_rows[] = array();
1377
				}
1378

    
1379
				$numrows = count($saved_rows) - 1;
1380

    
1381
				foreach ($saved_rows as $row) {
1382
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1383
					$group->addClass('repeatable');
1384

    
1385
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1386
						unset($value);
1387
						$width = null;
1388

    
1389
						if ($rowhelper['value'] != "") {
1390
							$value = $rowhelper['value'];
1391
						}
1392
						$fieldname = $rowhelper['fieldname'];
1393
						$fielddescr = $rowhelper['fielddescr'];
1394

    
1395
						// if user is editing a record, load in the data.
1396
						if (isset($id) && $a_pkg[$id]) {
1397
							$value = $row[$fieldname];
1398
						}
1399

    
1400
						$type = $rowhelper['type'];
1401
						if ($type == "input" || $type == "password" || $type == "textarea") {
1402
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1403
								$value = base64_decode($value);
1404
							}
1405
						}
1406
						$fieldname = $rowhelper['fieldname'];
1407

    
1408
						if ($rowhelper['size']) {
1409
							$size = $rowhelper['size'];
1410
						} else if ($pkga['size']) {
1411
							$size = $pkga['size'];
1412
						} else {
1413
							$size = "8";
1414
						}
1415

    
1416
						if ($rowhelper['width']) {
1417
							$width = $rowhelper['width'];
1418
						}
1419

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

    
1422
						$text = "";
1423
						$trc++;
1424
					}
1425

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

    
1434
					$rowcounter++;
1435
					$section->add($group);
1436
				}
1437

    
1438
			// Add row button
1439
			$section->addInput(new Form_Button(
1440
				'addrow',
1441
				'Add'
1442
			))->removeClass('btn-primary')->addClass('btn-success');
1443

    
1444
			break;
1445

    
1446
	}
1447

    
1448
		if ($pkga['combinefields'] == "end") {
1449
			$group->add(new Form_StaticText(
1450
				null,
1451
				null
1452
			));
1453

    
1454
			if ($advanced) {
1455
				$advanced->add($group);
1456
			} else {
1457
				$section->add($group);
1458
			}
1459

    
1460
			$grouping = false;
1461
		}
1462

    
1463
	#increment counter
1464
	$i++;
1465
} // e-o-foreach field described in the XML
1466

    
1467
$form->add($section);
1468

    
1469
$form->addGlobal(new Form_Input(
1470
	'id',
1471
	null,
1472
	'hidden',
1473
	$id
1474
));
1475

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

    
1483
	$form->add($advanced);
1484
}
1485

    
1486
print($form);
1487

    
1488
if ($pkg['note'] != "") {
1489
	print_info_box($pkg['note']);
1490
}
1491

    
1492
if ($pkg['custom_php_after_form_command']) {
1493
	eval($pkg['custom_php_after_form_command']);
1494
}
1495

    
1496
if ($pkg['fields']['field'] != "") { ?>
1497
<script type="text/javascript">
1498
//<![CDATA[
1499
	events.push(function() {
1500

    
1501
	// Hide the advanced section
1502
	var advanced_visible = false;
1503

    
1504
	// Hide on page load
1505
	$('.advancedoptions').hide();
1506

    
1507
	// Suppress "Delete row" button if there are fewer than two rows
1508
	checkLastRow();
1509

    
1510
	// Show advanced section if you click the showadv button
1511
	$('#showadv').prop('type', 'button');
1512

    
1513
	$("#showadv").click(function() {
1514
		advanced_visible = !advanced_visible;
1515

    
1516
		if (advanced_visible) {
1517
			$('.advancedoptions').show();
1518
			$("#showadv").prop('value', 'Hide advanced Options');
1519
		} else {
1520
			$('.advancedoptions').hide();
1521
			$("#showadv").prop('value', 'Show advanced Options');
1522
		}
1523
	});
1524

    
1525
	// Call enablechange function
1526
	enablechange();
1527
});
1528

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

    
1535
			if (isset($field['enablefields'])) {
1536
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1537
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1538
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1539
					echo "\t\t}\n";
1540
				}
1541
			}
1542

    
1543
			if (isset($field['checkenablefields'])) {
1544
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1545
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1546
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1547
					echo "\t\t}\n";
1548
				}
1549
			}
1550

    
1551
			echo "\t}\n\telse {\n";
1552

    
1553
			if (isset($field['enablefields'])) {
1554
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1555
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1556
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1557
					echo "\t\t}\n";
1558
				}
1559
			}
1560

    
1561
			if (isset($field['checkenablefields'])) {
1562
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1563
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1564
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1565
					echo "\t\t}\n";
1566
				}
1567
			}
1568

    
1569
			echo "\t}\n";
1570
		}
1571
	}
1572
	?>
1573
	}
1574
//]]>
1575
</script>
1576

    
1577
<?php
1578
}
1579

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