Project

General

Profile

Download (41.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_once("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
	$pgtitle = array(gettext("Package"), gettext("Editor"));
85
	include("head.inc");
86
	print_info_box(gettext("No valid package defined."), 'danger', false);
87
	include("foot.inc");
88
	die;
89
} else {
90
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
91
}
92

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
258
			parse_package_templates();
259

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

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

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

    
286

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
441
			break;
442
	}
443
}
444

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
600
	ksort($tab_array);
601
}
602

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

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

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

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

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

    
633
$saveicon = "fa-save";
634
if ($pkg['saveicon'] != "") {
635
	$saveicon = $pkg['saveicon'];
636
}
637

    
638
$savebtnclass = "btn-primary";
639
if ($pkg['savebtnclass'] != "") {
640
	$savebtnclass = $pkg['savebtnclass'];
641
}
642

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

    
645
$savebutton = new Form_Button(
646
	'submit',
647
	$savevalue,
648
	null,
649
	$saveicon
650
);
651
$savebutton->addClass($savebtnclass);
652

    
653
if ($savehelp) {
654
	$savebutton->setHelp($savehelp);
655
}
656

    
657
$form = new Form($savebutton);
658

    
659
$form->addGlobal(new Form_Input(
660
	'xml',
661
	null,
662
	'hidden',
663
	$xml
664
));
665

    
666
/* If a package's XML has <advanced_options/> configured, then setup
667
 * the section for the fields that have <advancedfield/> set.
668
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
669
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
670
 */
671

    
672
if ($pkg['advanced_options'] == "enabled") {
673
	$advfield_count = 0;
674
	$advanced = new Form_Section("Advanced Features");
675
	$advanced->addClass('advancedoptions');
676
}
677

    
678
$js_array = array();
679

    
680
// Now loop through all of the fields defined in the XML
681
if (!is_array($pkg['fields']['field'])) {
682
	$pkg['fields']['field'] = array();
683
}
684
foreach ($pkg['fields']['field'] as $pkga) {
685

    
686
	$action = "";
687
	$uid = "";
688

    
689
	if ($pkga['type'] == "sorting") {
690
		continue;
691
	}
692

    
693
	// Generate a new section
694
	if ($pkga['type'] == "listtopic") {
695
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
696
			$advanced->addInput(new Form_StaticText(
697
				strip_tags($pkga['name']),
698
				null
699
			));
700

    
701
			$advfield_count++;
702
		}  else {
703
			if (isset($section)) {
704
				$form->add($section);
705
			}
706

    
707
			if (isset($pkga['collapse'])) {
708
				$uid = uniqid("section");
709

    
710
				$action = COLLAPSIBLE;
711

    
712
				if ($pkga['collapse'] == "open") {
713
					$action |= SEC_OPEN;
714
				} else {
715
					$action |= SEC_CLOSED;
716
				}
717
			}
718

    
719
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
720
		}
721

    
722
		continue;
723
	}
724

    
725
	// 'begin' starts a form group. ('end' ends it)
726
	if ($pkga['combinefields'] == "begin") {
727
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
728
		$grouping = true;
729
	}
730

    
731
	$size = "";
732
	$colspan="";
733

    
734
	// if user is editing a record, load in the data.
735
	$fieldname = $pkga['fieldname'];
736
	unset($value);
737
	if ($get_from_post) {
738
		$value = $_POST[$fieldname];
739
		if (is_array($value)) {
740
			$value = implode(',', $value);
741
		}
742
	} else {
743
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
744
			$value = $a_pkg[$id][$fieldname];
745
		} else {
746
			if (isset($pkga['default_value'])) {
747
				$value = $pkga['default_value'];
748
			}
749
		}
750
	}
751

    
752
	// If we get here but have no $section, the package config file probably had no listtopic field
753
	// We can create a section with a generic name to fix that
754
	if (!$section) {
755
		$section = new Form_Section('General Options');
756
	}
757

    
758
	switch ($pkga['type']) {
759
		// Create an input element. The format is slightly different depending on whether we are composing a group,
760
		// section, or advanced section. This is true for every element type
761
		case "input":
762
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
763
				$value = base64_decode($value);
764
			}
765

    
766
			$grp = new Form_Input(
767
					$pkga['fieldname'],
768
					$pkga['fielddescr'],
769
					'text',
770
					$value
771
				);
772

    
773
			$grp->setHelp($pkga['description']);
774

    
775
			if ($pkga['width']) {
776
				$grp->setWidth($pkga['width']);
777
			}
778

    
779
			if ($grouping) {
780
				$group->add($grp);
781
			} else {
782
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
783
					$advanced->addInput($grp);
784
				} else {
785
					$section->addInput($grp);
786
				}
787
			}
788

    
789
			break;
790

    
791
		case "password":
792
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
793
				$value = base64_decode($value);
794
			}
795

    
796
			// Create a password element
797
			if ($grouping) {
798
				$group->add(new Form_Input(
799
					$pkga['fieldname'],
800
					$pkga['fielddescr'],
801
					'password',
802
					$value
803
				))->setHelp($pkga['description']);
804
			} else {
805
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
806
					$advanced->addInput(new Form_Input(
807
						$pkga['fieldname'],
808
						$pkga['fielddescr'],
809
						'password',
810
						$value
811
					))->setHelp($pkga['description']);
812
				} else {
813
					$section->addInput(new Form_Input(
814
						$pkga['fieldname'],
815
						$pkga['fielddescr'],
816
						'password',
817
						$value
818
					))->setHelp($pkga['description']);
819
				}
820
			}
821

    
822
			break;
823

    
824
		case "info":
825
			// If the info contains a table we should detect and Bootstrap it
826

    
827
			if (strpos($pkga['description'], '<table') !== FALSE) {
828
				$info = bootstrapTable($pkga['description']);
829
			} else {
830
				$info = $pkga['description'];
831
			}
832

    
833
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
834
				$advanced->addInput(new Form_StaticText(
835
					strip_tags($pkga['fielddescr']),
836
					$info
837
				));
838
			} else {
839
				$section->addInput(new Form_StaticText(
840
					strip_tags($pkga['fielddescr']),
841
					$info
842
				));
843
			}
844

    
845
			break;
846

    
847
		case "select":
848
			// Create a select element
849
			$optionlist = array();
850
			$selectedlist = array();
851

    
852
			$fieldname = $pkga['fieldname'];
853

    
854
			if (isset($pkga['multiple'])) {
855
				$multiple = 'multiple';
856
				$items = explode(',', $value);
857
				$fieldname .= "[]";
858
			} else {
859
				$multiple = '';
860
				$items = array($value);
861
			}
862

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

    
865
			foreach ($pkga['options']['option'] as $opt) {
866
				$optionlist[$opt['value']] = $opt['name'];
867

    
868
				if (in_array($opt['value'], $items)) {
869
					array_push($selectedlist, $opt['value']);
870
				}
871
			}
872

    
873
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
874
				$function = $grouping ? $advanced->add:$advanced->addInput;
875
			} else {
876
				$function = ($grouping) ? $section->add:$section->addInput;
877
			}
878

    
879
			$grp = new Form_Select(
880
						$pkga['fieldname'],
881
						strip_tags($pkga['fielddescr']),
882
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
883
						$optionlist,
884
						isset($pkga['multiple'])
885
					);
886

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

    
889
			if ($pkga['width']) {
890
				$grp->setWidth($pkga['width']);
891
			}
892

    
893
			if ($grouping) {
894
				$group->add($grp);
895
			} else {
896
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
897
					$advanced->addInput($grp);
898
				} else {
899
					$section->addInput($grp);
900
				}
901
			}
902

    
903
			break;
904

    
905
		case "select_source":
906

    
907
			if (isset($pkga['multiple'])) {
908
				$items = explode(',', $value);
909
				$fieldname .= "[]";
910
			} else {
911
				$items = array($value);
912
			}
913

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

    
916
			$source_url = $pkga['source'];
917
			eval("\$pkg_source_txt = &$source_url;");
918

    
919
			#check if show disable option is present on xml
920
			if (!is_array($pkg_source_txt)) {
921
				$pkg_source_txt = array();
922
			}
923
			if (isset($pkga['show_disable_value'])) {
924
				array_push($pkg_source_txt,
925
					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']));
926
			}
927

    
928
			$srcoptions = array();
929
			$srcselected = array();
930

    
931
			foreach ($pkg_source_txt as $opt) {
932
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
933
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
934
				$srcoptions[$source_value] = $source_name;
935

    
936
				if (in_array($source_value, $items)) {
937
					array_push($srcselected, $source_value);
938
				}
939
			}
940

    
941
			$descr = (isset($pkga['description'])) ? $pkga['description'] : "";
942
			if ($grouping) {
943
				$group->add(new Form_Select(
944
					$pkga['fieldname'],
945
					strip_tags($pkga['fielddescr']),
946
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
947
					$srcoptions,
948
					isset($pkga['multiple'])
949
				))->setHelp($descr)->setOnchange($onchange);
950
			} else {
951
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
952
					$advanced->addInput(new Form_Select(
953
						$pkga['fieldname'],
954
						strip_tags($pkga['fielddescr']),
955
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
956
						$srcoptions,
957
						isset($pkga['multiple'])
958
					))->setHelp($descr)->setOnchange($onchange);
959
				} else {
960
					$section->addInput(new Form_Select(
961
						$pkga['fieldname'],
962
						strip_tags($pkga['fielddescr']),
963
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
964
						$srcoptions,
965
						isset($pkga['multiple'])
966
					))->setHelp($descr)->setOnchange($onchange);
967
				}
968
			}
969

    
970
			break;
971

    
972
		case "vpn_selection" :
973
			$vpnlist = array();
974

    
975
			foreach ($config['ipsec']['phase1'] as $vpn) {
976
				$vpnlist[$vpn['descr']] = $vpn['descr'];
977

    
978
			}
979

    
980
			if ($grouping) {
981
				$group->add(new Form_Select(
982
					$pkga['fieldname'],
983
					null,
984
					false,
985
					$vpnlist
986
				))->setHelp(fixup_string($pkga['description']));
987
			} else {
988
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
989
					$advanced->addInput(new Form_Select(
990
						$pkga['fieldname'],
991
						null,
992
						false,
993
						$vpnlist
994
					))->setHelp(fixup_string($pkga['description']));
995
				} else {
996
					$section->addInput(new Form_Select(
997
						$pkga['fieldname'],
998
						null,
999
						false,
1000
						$vpnlist
1001
					))->setHelp(fixup_string($pkga['description']));
1002
				}
1003
			}
1004

    
1005
			break;
1006

    
1007
		// Create a checkbox element
1008
		case "checkbox":
1009
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
1010
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
1011
				$onclick = 'javascript:enablechange();';
1012
			} else {
1013
				$onclick = '';
1014
			}
1015

    
1016
			if ($grouping) {
1017
				$group->add(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
			} else {
1027
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1028
					$advanced->addInput(new Form_Checkbox(
1029
						$pkga['fieldname'],
1030
						$pkga['fielddescr'],
1031
						fixup_string($pkga['description']),
1032
						($value == "on"),
1033
						'on'
1034
					))->setOnclick($onclick)
1035
					  ->setOnchange($onchange)
1036
					  ->setHelp($pkga['sethelp']);
1037
				} else {
1038
					$section->addInput(new Form_Checkbox(
1039
						$pkga['fieldname'],
1040
						$pkga['fielddescr'],
1041
						fixup_string($pkga['description']),
1042
						($value == "on"),
1043
						'on'
1044
					))->setOnclick($onclick)
1045
					  ->setOnchange($onchange)
1046
					  ->setHelp($pkga['sethelp']);
1047
				}
1048
			}
1049

    
1050
			break;
1051

    
1052
		// Create a textarea element
1053
		case "textarea":
1054
			$rows = $cols = 0;
1055

    
1056
			if ($pkga['rows']) {
1057
				$rows = $pkga['rows'];
1058
			}
1059
			if ($pkga['cols']) {
1060
				$cols = $pkga['cols'];
1061
			}
1062

    
1063
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1064
				$value = base64_decode($value);
1065
			}
1066

    
1067
			$grp = new Form_Textarea(
1068
					$pkga['fieldname'],
1069
					$pkga['fielddescr'],
1070
					$value
1071
			);
1072

    
1073
			$grp->setHelp(fixup_string($pkga['description']));
1074

    
1075
			if ($rows > 0) {
1076
				$grp->setRows($rows);
1077
			}
1078

    
1079
			if ($cols > 0) {
1080
				$grp->setCols($cols);
1081
			}
1082

    
1083
			if ($pkga['wrap'] == "off") {
1084
				$grp->setAttribute("wrap", "off");
1085
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1086
			} else {
1087
				$grp->setAttribute("style", "width: auto;");
1088
			}
1089

    
1090
			if ($grouping) {
1091
				$group->add($grp);
1092
			} else {
1093
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1094
					$advanced->addInput($grp);
1095
				} else {
1096
					$section->addInput($grp);
1097
				}
1098
			}
1099

    
1100
			break;
1101

    
1102
		case "aliases":
1103

    
1104
			// Use xml tag <typealiases> to filter type aliases
1105
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1106
			$fieldname = $pkga['fieldname'];
1107
			$a_aliases = &$config['aliases']['alias'];
1108
			$addrisfirst = 0;
1109
			$aliasesaddr = "";
1110

    
1111
			if (isset($a_aliases)) {
1112
				if (!empty($pkga['typealiases'])) {
1113
					foreach ($a_aliases as $alias) {
1114
						if ($alias['type'] == $pkga['typealiases']) {
1115
							if ($addrisfirst == 1) {
1116
								$aliasesaddr .= ",";
1117
							}
1118
							$aliasesaddr .= "'" . $alias['name'] . "'";
1119
							$addrisfirst = 1;
1120
						}
1121
					}
1122
				} else {
1123
					foreach ($a_aliases as $alias) {
1124
						if ($addrisfirst == 1) {
1125
							$aliasesaddr .= ",";
1126
						}
1127
						$aliasesaddr .= "'" . $alias['name'] . "'";
1128
						$addrisfirst = 1;
1129
					}
1130
				}
1131
			}
1132

    
1133
			$grp = new Form_Input(
1134
					$pkga['fieldname'],
1135
					$pkga['fielddescr'],
1136
					'text',
1137
					$value
1138
				);
1139

    
1140
			$grp->setHelp($pkga['description']);
1141

    
1142
			if ($pkga['width']) {
1143
				$grp->setWidth($pkga['width']);
1144
			}
1145

    
1146
			if (grouping) {
1147
				$group->add($grp);
1148
			} else {
1149
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1150
					$advanced->addInput($grp);
1151
				} else {
1152
					$section->addInput($grp);
1153
				}
1154
			}
1155

    
1156
			$script = "<script type='text/javascript'>\n";
1157
			$script .= "//<![CDATA[\n";
1158
			$script .= "events.push(function(){\n";
1159
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1160
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1161
			$script .= "		source: aliasarray\n";
1162
			$script .= "	})\n";
1163
			$script .= "});\n";
1164
			$script .= "//]]>\n";
1165
			$script .= "</script>";
1166

    
1167
			echo $script;
1168

    
1169
			break;
1170

    
1171
		case "interfaces_selection":
1172
			$ips = array();
1173
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1174
			if (is_array($config['interfaces'])) {
1175
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1176
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1177
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1178
						if (isset($pkga['showips'])) {
1179
							$iface_description .= " address";
1180
						}
1181
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1182
					}
1183
				}
1184
			}
1185

    
1186
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1187
				foreach ($config['virtualip']['vip'] as $vip) {
1188
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1189
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1190
					}
1191
					switch ($vip['mode']) {
1192
						case "ipalias":
1193
						case "carp":
1194
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1195
							break;
1196
						case "proxyarp":
1197
							if ($vip['type'] == "network") {
1198
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1199
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1200
								$len = $end - $start;
1201
								for ($i = 0; $i <= $len; $i++) {
1202
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1203
								}
1204
							} else {
1205
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1206
							}
1207
							break;
1208
					}
1209
				}
1210
			}
1211

    
1212
			sort($ips);
1213
			if (isset($pkga['showlistenall'])) {
1214
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1215
			}
1216

    
1217
			if (!preg_match("/$interface_regex/", "loopback")) {
1218
				$loopback_text = gettext("loopback");
1219
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1220
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1221
			}
1222

    
1223
			#show interfaces array on gui
1224
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1225
			$multiple = '';
1226
			$fieldname = $pkga['fieldname'];
1227
			if (isset($pkga['multiple'])) {
1228
				$fieldname .= '[]';
1229
				$multiple = 'multiple';
1230
			}
1231

    
1232
			$selectedlist = array();
1233
			$optionlist = array();
1234

    
1235
			if (is_array($value)) {
1236
				$values = $value;
1237
			} else {
1238
				$values = explode(',', $value);
1239
			}
1240

    
1241
			foreach ($ips as $iface) {
1242
				if (in_array($iface['ip'], $values)) {
1243
					array_push($selectedlist, $iface['ip']);
1244
				}
1245

    
1246
				$optionlist[$iface['ip']] = $iface['description'];
1247
			}
1248

    
1249
			if ($grouping) {
1250
				$group->add(new Form_Select(
1251
					$pkga['fieldname'],
1252
					$pkga['fielddescr'],
1253
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1254
					$optionlist,
1255
					isset($pkga['multiple'])
1256
				))->setHelp($pkga['description']);
1257
			} else {
1258
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1259
					$advanced->addInput(new Form_Select(
1260
						$pkga['fieldname'],
1261
						$pkga['fielddescr'],
1262
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1263
						$optionlist,
1264
						isset($pkga['multiple'])
1265
					))->setHelp($pkga['description']);
1266
				} else {
1267
					$section->addInput(new Form_Select(
1268
						$pkga['fieldname'],
1269
						$pkga['fielddescr'],
1270
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1271
						$optionlist,
1272
						isset($pkga['multiple'])
1273
					))->setHelp($pkga['description']);
1274
				}
1275
			}
1276

    
1277
			break;
1278

    
1279
		// Create radio button
1280
		case "radio":
1281
			if ($grouping) {
1282
				$group->add(new Form_Checkbox(
1283
					$pkga['fieldname'],
1284
					$pkga['fielddescr'],
1285
					fixup_string($pkga['description']),
1286
					($value == "on"),
1287
					'on'
1288
				))->displayAsRadio();
1289
			} else {
1290
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1291
					$advanced->addInput(new Form_Checkbox(
1292
						$pkga['fieldname'],
1293
						$pkga['fielddescr'],
1294
						fixup_string($pkga['description']),
1295
						($value == "on"),
1296
						'on'
1297
					))->displayAsRadio();
1298
				} else {
1299
					$section->addInput(new Form_Checkbox(
1300
						$pkga['fieldname'],
1301
						$pkga['fielddescr'],
1302
						fixup_string($pkga['description']),
1303
						($value == "on"),
1304
						'on'
1305
					))->displayAsRadio();
1306
				}
1307
			}
1308

    
1309
			break;
1310

    
1311
		// Create form button
1312
		case "button":
1313
			$newbtnicon = "fa-save";
1314
			if ($pkga['buttonicon'] != "") {
1315
				$newbtnicon = $pkga['buttonicon'];
1316
			}
1317
			$newbtnclass = "btn-primary";
1318
			if ($pkga['buttonclass'] != "") {
1319
				$newbtnclass = $pkga['buttonclass'];
1320
			}
1321

    
1322
			$newbtn = new Form_Button(
1323
				$pkga['fieldname'],
1324
				$pkga['fieldname'],
1325
				null,
1326
				$newbtnicon
1327
			);
1328
			$newbtn->addClass($newbtnclass);
1329

    
1330
			if (grouping) {
1331
				$group->add(new Form_StaticText(
1332
					null,
1333
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1334
				));
1335
			} else {
1336
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1337
					$advanced->addInput(new Form_StaticText(
1338
						null,
1339
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1340
					));
1341
				} else {
1342
					$section->addInput(new Form_StaticText(
1343
						null,
1344
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1345
					));
1346
				}
1347
			}
1348

    
1349
			break;
1350

    
1351
		case "schedule_selection":
1352

    
1353
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1354
			$schedules = array();
1355
			$schedules[] = "none";
1356
			if (is_array($config['schedules']['schedule'])) {
1357
				foreach ($config['schedules']['schedule'] as $schedule) {
1358
					if ($schedule['name'] != "") {
1359
						$schedules[] = $schedule['name'];
1360
					}
1361
				}
1362
			}
1363

    
1364
			foreach ($schedules as $schedule) {
1365
				if ($schedule == "none") {
1366
					$schedlist[""] = $schedule;
1367
				} else {
1368
					$schedlist[$schedule] = $schedule;
1369
				}
1370
			}
1371

    
1372
			if ($grouping) {
1373
				$group->add(new Form_Select(
1374
					$pkga['fieldname'],
1375
					$pkga['fielddescr'],
1376
					$value,
1377
					$schedlist
1378
				))->setHelp(fixup_string($pkga['description']));
1379
			} else {
1380
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1381
					$advanced->addInput(new Form_Select(
1382
						$pkga['fieldname'],
1383
						$pkga['fielddescr'],
1384
						$value,
1385
						$schedlist
1386
					))->setHelp(fixup_string($pkga['description']));
1387
				} else {
1388
					$section->addInput(new Form_Select(
1389
						$pkga['fieldname'],
1390
						$pkga['fielddescr'],
1391
						$value,
1392
						$schedlist
1393
					))->setHelp(fixup_string($pkga['description']));
1394
				}
1395
			}
1396

    
1397
			break;
1398

    
1399
		case "rowhelper":
1400

    
1401
			$rowhelpername="row";
1402

    
1403
				$rowcounter = 0;
1404
				$trc = 0;
1405

    
1406
				//Use assigned $a_pkg or create an empty array to enter loop
1407
				if (isset($a_pkg[$id][$rowhelpername])) {
1408
					$saved_rows=$a_pkg[$id][$rowhelpername];
1409
				} else {
1410
					$saved_rows[] = array();
1411
				}
1412

    
1413
				$numrows = count($saved_rows) - 1;
1414

    
1415
				foreach ($saved_rows as $row) {
1416
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1417
					$group->addClass('repeatable');
1418

    
1419
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1420
						unset($value);
1421
						$width = null;
1422

    
1423
						$fieldname = $rowhelper['fieldname'];
1424
						$fielddescr = $rowhelper['fielddescr'];
1425

    
1426
						// If input validation failed, read the value from the POST data so that hte user's input is not lost
1427
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1428
							$value = $_POST[$fieldname.$rowcounter];
1429
						} elseif (isset($id) && $a_pkg[$id]) {
1430
							$value = $row[$fieldname];
1431
						} elseif ($rowhelper['value'] != "") {
1432
							$value = $rowhelper['value'];
1433
						}
1434

    
1435
						$type = $rowhelper['type'];
1436
						if ($type == "input" || $type == "password" || $type == "textarea") {
1437
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1438
								$value = base64_decode($value);
1439
							}
1440
						}
1441

    
1442

    
1443
						if ($rowhelper['size']) {
1444
							$size = $rowhelper['size'];
1445
						} else if ($pkga['size']) {
1446
							$size = $pkga['size'];
1447
						} else {
1448
							$size = "8";
1449
						}
1450

    
1451
						if ($rowhelper['width']) {
1452
							$width = $rowhelper['width'];
1453
						}
1454

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

    
1457
						$text = "";
1458
						$trc++;
1459
					}
1460

    
1461
					// Delete row button
1462
					$group->add(new Form_Button(
1463
						'deleterow' . $rowcounter,
1464
						'Delete',
1465
						null,
1466
						'fa-trash'
1467
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1468

    
1469
					$rowcounter++;
1470
					$section->add($group);
1471
				}
1472

    
1473
			// Add row button
1474
			$section->addInput(new Form_Button(
1475
				'addrow',
1476
				'Add',
1477
				null,
1478
				'fa-plus'
1479
			))->addClass('btn-success');
1480

    
1481
			break;
1482

    
1483
	}
1484

    
1485
		if ($pkga['combinefields'] == "end") {
1486
			$group->add(new Form_StaticText(
1487
				null,
1488
				null
1489
			));
1490

    
1491
			if ($advanced) {
1492
				$advanced->add($group);
1493
			} else {
1494
				$section->add($group);
1495
			}
1496

    
1497
			$grouping = false;
1498
		}
1499

    
1500
	#increment counter
1501
	$i++;
1502
} // e-o-foreach field described in the XML
1503

    
1504
if ($section) {
1505
	$form->add($section);
1506
}
1507

    
1508
$form->addGlobal(new Form_Input(
1509
	'id',
1510
	null,
1511
	'hidden',
1512
	$id
1513
));
1514

    
1515
// If we created an advanced section, add it (and a button) to the form here
1516
if (!empty($advanced)) {
1517
	$form->addGlobal(new Form_Button(
1518
		'showadv',
1519
		'Show Advanced Options',
1520
		null,
1521
		'fa-cog'
1522
	))->setAttribute('type','button')->addClass('btn-info');
1523

    
1524
	$form->add($advanced);
1525
}
1526

    
1527
print($form);
1528

    
1529
if ($pkg['note'] != "") {
1530
	print_info_box($pkg['note'], 'info');
1531
}
1532

    
1533
if ($pkg['custom_php_after_form_command']) {
1534
	eval($pkg['custom_php_after_form_command']);
1535
}
1536

    
1537
if ($pkg['fields']['field'] != "") { ?>
1538
<script type="text/javascript">
1539
//<![CDATA[
1540
	events.push(function() {
1541

    
1542
	// Hide the advanced section
1543
	var advanced_visible = false;
1544

    
1545
	// Hide on page load
1546
	$('.advancedoptions').hide();
1547

    
1548
	// Suppress "Delete row" button if there are fewer than two rows
1549
	checkLastRow();
1550

    
1551
	// Show advanced section if you click the showadv button
1552
	$("#showadv").click(function() {
1553
		advanced_visible = !advanced_visible;
1554

    
1555
		if (advanced_visible) {
1556
			$('.advancedoptions').show();
1557
			$("#showadv").prop('value', 'Hide advanced Options');
1558
		} else {
1559
			$('.advancedoptions').hide();
1560
			$("#showadv").prop('value', 'Show advanced Options');
1561
		}
1562
	});
1563

    
1564
	// Call enablechange function
1565
	enablechange();
1566
});
1567

    
1568
	function enablechange() {
1569
<?php
1570
	foreach ($pkg['fields']['field'] as $field) {
1571
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1572
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1573

    
1574
			if (isset($field['enablefields'])) {
1575
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1576
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1577
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1578
					echo "\t\t}\n";
1579
				}
1580
			}
1581

    
1582
			if (isset($field['checkenablefields'])) {
1583
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1584
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1585
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1586
					echo "\t\t}\n";
1587
				}
1588
			}
1589

    
1590
			echo "\t}\n\telse {\n";
1591

    
1592
			if (isset($field['enablefields'])) {
1593
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1594
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1595
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1596
					echo "\t\t}\n";
1597
				}
1598
			}
1599

    
1600
			if (isset($field['checkenablefields'])) {
1601
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1602
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1603
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1604
					echo "\t\t}\n";
1605
				}
1606
			}
1607

    
1608
			echo "\t}\n";
1609
		}
1610
	}
1611
	?>
1612
	}
1613
//]]>
1614
</script>
1615

    
1616
<?php
1617
}
1618

    
1619
include("foot.inc");
(101-101/226)