Project

General

Profile

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

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

    
61
ini_set('max_execution_time', '0');
62

    
63
require_once("guiconfig.inc");
64
require_once("functions.inc");
65
require_once("filter.inc");
66
require_once("shaper.inc");
67
require_once("pkg-utils.inc");
68

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

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

    
79
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
80

    
81
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
82
	$pgtitle = array(gettext("Package"), gettext("Editor"));
83
	$pglinks = array("", "@self");
84
	include("head.inc");
85
	print_info_box(gettext("No valid package defined."), 'danger', false);
86
	include("foot.inc");
87
	die;
88
} else {
89
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
90
}
91

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
257
			parse_package_templates();
258

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

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

    
269
			if ($pkg['aftersaveredirect'] != "") {
270
				pfSenseHeader($pkg['aftersaveredirect']);
271
			} elseif (!$pkg['adddeleteeditpagefields']) {
272
				pfSenseHeader("pkg_edit.php?xml={$xml}&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, $config;
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
			$pglinks[] = ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']);
542
		}
543
	} else {
544
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
545
		$pglinks = array("", ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']));
546
	}
547
} else {
548
	$pgtitle = array(gettext("Package"), gettext("Editor"));
549
	$pglinks = array("", "@self");
550
}
551

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

    
562
		if (isset($tab['active'])) {
563
			$active = true;
564
			$pgtitle[] = $tab['text'] ;
565
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
566
		} else {
567
			$active = false;
568
		}
569

    
570
		if (isset($tab['no_drop_down'])) {
571
			$no_drop_down = true;
572
		}
573

    
574
		$urltmp = "";
575
		if ($tab['url'] != "") {
576
			$urltmp = $tab['url'];
577
		}
578

    
579
		if ($tab['xml'] != "") {
580
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
581
		}
582

    
583
		$addresswithport = getenv("HTTP_HOST");
584
		$colonpos = strpos($addresswithport, ":");
585

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

    
593
		// eval url so that above $myurl item can be processed if need be.
594
		$url = str_replace('$myurl', $myurl, $urltmp);
595

    
596
		$tab_array[$tab_level][] = array(
597
			$tab['text'],
598
			$active,
599
			$url
600
		);
601
	}
602

    
603
	ksort($tab_array);
604
}
605

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

    
616
// Start of page display
617
if ($input_errors) {
618
	print_input_errors($input_errors);
619
}
620

    
621
if ($savemsg) {
622
	print_info_box($savemsg, 'success');
623
}
624

    
625
$cols = 0;
626
$savevalue = gettext("Save");
627
if ($pkg['savetext'] != "") {
628
	$savevalue = $pkg['savetext'];
629
}
630

    
631
$savehelp = "";
632
if ($pkg['savehelp'] != "") {
633
	$savehelp = $pkg['savehelp'];
634
}
635

    
636
$saveicon = "fa-save";
637
if ($pkg['saveicon'] != "") {
638
	$saveicon = $pkg['saveicon'];
639
}
640

    
641
$savebtnclass = "btn-primary";
642
if ($pkg['savebtnclass'] != "") {
643
	$savebtnclass = $pkg['savebtnclass'];
644
}
645

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

    
648
$savebutton = new Form_Button(
649
	'submit',
650
	$savevalue,
651
	null,
652
	$saveicon
653
);
654
$savebutton->addClass($savebtnclass);
655

    
656
if ($savehelp) {
657
	$savebutton->setHelp($savehelp);
658
}
659

    
660
$form = new Form($savebutton);
661

    
662
$form->addGlobal(new Form_Input(
663
	'xml',
664
	null,
665
	'hidden',
666
	$xml
667
));
668

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

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

    
681
$js_array = array();
682

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

    
689
	$action = "";
690
	$uid = "";
691

    
692
	if ($pkga['type'] == "sorting") {
693
		continue;
694
	}
695

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

    
704
			$advfield_count++;
705
		}  else {
706
			if (isset($section)) {
707
				$form->add($section);
708
			}
709

    
710
			if (isset($pkga['collapse'])) {
711
				$uid = uniqid("section");
712

    
713
				$action = COLLAPSIBLE;
714

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

    
722
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
723
		}
724

    
725
		continue;
726
	}
727

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

    
734
	$size = "";
735
	$colspan="";
736

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

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

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

    
769
			$grp = new Form_Input(
770
					$pkga['fieldname'],
771
					$pkga['fielddescr'],
772
					'text',
773
					$value
774
				);
775

    
776
			$grp->setHelp($pkga['description']);
777

    
778
			if ($pkga['width']) {
779
				$grp->setWidth($pkga['width']);
780
			}
781

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

    
792
			break;
793

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

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

    
825
			break;
826

    
827
		case "info":
828
			// If the info contains a table we should detect and Bootstrap it
829

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

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

    
848
			break;
849

    
850
		case "select":
851
			// Create a select element
852
			$optionlist = array();
853
			$selectedlist = array();
854

    
855
			$fieldname = $pkga['fieldname'];
856

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

    
866
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
867

    
868
			foreach ($pkga['options']['option'] as $opt) {
869
				$optionlist[$opt['value']] = $opt['name'];
870

    
871
				if (in_array($opt['value'], $items)) {
872
					array_push($selectedlist, $opt['value']);
873
				}
874
			}
875

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

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

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

    
892
			if ($pkga['width']) {
893
				$grp->setWidth($pkga['width']);
894
			}
895

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

    
906
			break;
907

    
908
		case "select_source":
909

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

    
917
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
918

    
919
			$source_url = $pkga['source'];
920
			eval("\$pkg_source_txt = &$source_url;");
921

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

    
931
			$srcoptions = array();
932
			$srcselected = array();
933

    
934
			foreach ($pkg_source_txt as $opt) {
935
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
936
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
937
				$srcoptions[$source_value] = $source_name;
938

    
939
				if (in_array($source_value, $items)) {
940
					array_push($srcselected, $source_value);
941
				}
942
			}
943

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

    
973
			break;
974

    
975
		case "vpn_selection" :
976
			$vpnlist = array();
977

    
978
			foreach ($config['ipsec']['phase1'] as $vpn) {
979
				$vpnlist[$vpn['descr']] = $vpn['descr'];
980

    
981
			}
982

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

    
1008
			break;
1009

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

    
1019
			if ($grouping) {
1020
				$group->add(new Form_Checkbox(
1021
					$pkga['fieldname'],
1022
					$pkga['fielddescr'],
1023
					fixup_string($pkga['description']),
1024
					($value == "on"),
1025
					'on'
1026
				))->setOnclick($onclick)
1027
				  ->setOnchange($onchange)
1028
				  ->setHelp($pkga['sethelp']);
1029
			} else {
1030
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1031
					$advanced->addInput(new Form_Checkbox(
1032
						$pkga['fieldname'],
1033
						$pkga['fielddescr'],
1034
						fixup_string($pkga['description']),
1035
						($value == "on"),
1036
						'on'
1037
					))->setOnclick($onclick)
1038
					  ->setOnchange($onchange)
1039
					  ->setHelp($pkga['sethelp']);
1040
				} else {
1041
					$section->addInput(new Form_Checkbox(
1042
						$pkga['fieldname'],
1043
						$pkga['fielddescr'],
1044
						fixup_string($pkga['description']),
1045
						($value == "on"),
1046
						'on'
1047
					))->setOnclick($onclick)
1048
					  ->setOnchange($onchange)
1049
					  ->setHelp($pkga['sethelp']);
1050
				}
1051
			}
1052

    
1053
			break;
1054

    
1055
		// Create a textarea element
1056
		case "textarea":
1057
			$rows = $cols = 0;
1058

    
1059
			if ($pkga['rows']) {
1060
				$rows = $pkga['rows'];
1061
			}
1062
			if ($pkga['cols']) {
1063
				$cols = $pkga['cols'];
1064
			}
1065

    
1066
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1067
				$value = base64_decode($value);
1068
			}
1069

    
1070
			$grp = new Form_Textarea(
1071
					$pkga['fieldname'],
1072
					$pkga['fielddescr'],
1073
					$value
1074
			);
1075

    
1076
			$grp->setHelp(fixup_string($pkga['description']));
1077

    
1078
			if ($rows > 0) {
1079
				$grp->setRows($rows);
1080
			}
1081

    
1082
			if ($cols > 0) {
1083
				$grp->setCols($cols);
1084
			}
1085

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

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

    
1103
			break;
1104

    
1105
		case "aliases":
1106

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

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

    
1136
			$grp = new Form_Input(
1137
					$pkga['fieldname'],
1138
					$pkga['fielddescr'],
1139
					'text',
1140
					$value
1141
				);
1142

    
1143
			$grp->setHelp($pkga['description']);
1144

    
1145
			if ($pkga['width']) {
1146
				$grp->setWidth($pkga['width']);
1147
			}
1148

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

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

    
1170
			echo $script;
1171

    
1172
			break;
1173

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

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

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

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

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

    
1235
			$selectedlist = array();
1236
			$optionlist = array();
1237

    
1238
			if (is_array($value)) {
1239
				$values = $value;
1240
			} else {
1241
				$values = explode(',', $value);
1242
			}
1243

    
1244
			foreach ($ips as $iface) {
1245
				if (in_array($iface['ip'], $values)) {
1246
					array_push($selectedlist, $iface['ip']);
1247
				}
1248

    
1249
				$optionlist[$iface['ip']] = $iface['description'];
1250
			}
1251

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

    
1280
			break;
1281

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

    
1312
			break;
1313

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

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

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

    
1352
			break;
1353

    
1354
		case "schedule_selection":
1355

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

    
1367
			foreach ($schedules as $schedule) {
1368
				if ($schedule == "none") {
1369
					$schedlist[""] = $schedule;
1370
				} else {
1371
					$schedlist[$schedule] = $schedule;
1372
				}
1373
			}
1374

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

    
1400
			break;
1401

    
1402
		case "rowhelper":
1403

    
1404
			$rowhelpername="row";
1405

    
1406
				$rowcounter = 0;
1407
				$trc = 0;
1408

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

    
1416
				$numrows = count($saved_rows) - 1;
1417

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

    
1422
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1423
						unset($value);
1424
						$width = null;
1425

    
1426
						$fieldname = $rowhelper['fieldname'];
1427
						$fielddescr = $rowhelper['fielddescr'];
1428

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

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

    
1445

    
1446
						if ($rowhelper['size']) {
1447
							$size = $rowhelper['size'];
1448
						} else if ($pkga['size']) {
1449
							$size = $pkga['size'];
1450
						} else {
1451
							$size = "8";
1452
						}
1453

    
1454
						if ($rowhelper['width']) {
1455
							$width = $rowhelper['width'];
1456
						}
1457

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

    
1460
						$text = "";
1461
						$trc++;
1462
					}
1463

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

    
1472
					$rowcounter++;
1473
					$section->add($group);
1474
				}
1475

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

    
1484
			break;
1485

    
1486
	}
1487

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

    
1494
			if ($advanced) {
1495
				$advanced->add($group);
1496
			} else {
1497
				$section->add($group);
1498
			}
1499

    
1500
			$grouping = false;
1501
		}
1502

    
1503
	#increment counter
1504
	$i++;
1505
} // e-o-foreach field described in the XML
1506

    
1507
if ($section) {
1508
	$form->add($section);
1509
}
1510

    
1511
$form->addGlobal(new Form_Input(
1512
	'id',
1513
	null,
1514
	'hidden',
1515
	$id
1516
));
1517

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

    
1527
	$form->add($advanced);
1528
}
1529

    
1530
print($form);
1531

    
1532
if ($pkg['note'] != "") {
1533
	print_info_box($pkg['note'], 'info');
1534
}
1535

    
1536
if ($pkg['custom_php_after_form_command']) {
1537
	eval($pkg['custom_php_after_form_command']);
1538
}
1539

    
1540

    
1541
$hidemsg = gettext("Show Advanced Options");
1542
$showmsg = gettext("Hide Advanced Options");
1543

    
1544
if ($pkg['fields']['field'] != "") { ?>
1545
<script type="text/javascript">
1546
//<![CDATA[
1547
	events.push(function() {
1548

    
1549
	// Hide the advanced section
1550
	var advanced_visible = false;
1551

    
1552
	// Hide on page load
1553
	$('.advancedoptions').hide();
1554

    
1555
	// Suppress "Delete row" button if there are fewer than two rows
1556
	checkLastRow();
1557

    
1558
	// Show advanced section if you click the showadv button
1559
	$("#showadv").click(function() {
1560
		advanced_visible = !advanced_visible;
1561

    
1562
		if (advanced_visible) {
1563
			$('.advancedoptions').show();
1564
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$showmsg?>");
1565
		} else {
1566
			$('.advancedoptions').hide();
1567
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$hidemsg?>");
1568
		}
1569
	});
1570

    
1571
	// Call enablechange function
1572
	enablechange();
1573
});
1574

    
1575
	function enablechange() {
1576
<?php
1577
	foreach ($pkg['fields']['field'] as $field) {
1578
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1579
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1580

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

    
1589
			if (isset($field['checkenablefields'])) {
1590
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1591
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1592
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1593
					echo "\t\t}\n";
1594
				}
1595
			}
1596

    
1597
			echo "\t}\n\telse {\n";
1598

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

    
1607
			if (isset($field['checkenablefields'])) {
1608
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1609
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1610
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1611
					echo "\t\t}\n";
1612
				}
1613
			}
1614

    
1615
			echo "\t}\n";
1616
		}
1617
	}
1618
	?>
1619
	}
1620
//]]>
1621
</script>
1622

    
1623
<?php
1624
}
1625

    
1626
include("foot.inc");
(98-98/225)