Project

General

Profile

Download (41.7 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-2018 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-package-edit
24
##|*NAME=Package: Edit
25
##|*DESCR=Allow access to the 'Package: Edit' page.
26
##|*MATCH=pkg_edit.php*
27
##|-PRIV
28

    
29
ini_set('max_execution_time', '0');
30

    
31
require_once("filter.inc");
32
require_once("functions.inc");
33
require_once("guiconfig.inc");
34
require_once("shaper.inc");
35
require_once("pkg-utils.inc");
36
require_once("pfsense-utils.inc");
37
require_once("util.inc");
38

    
39
$xml = htmlspecialchars($_REQUEST['xml']);
40

    
41
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
42

    
43
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
44
	$pgtitle = array(gettext("Package"), gettext("Editor"));
45
	$pglinks = array("", "@self");
46
	include("head.inc");
47
	print_info_box(gettext("No valid package defined."), 'danger', false);
48
	include("foot.inc");
49
	die;
50
} else {
51
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
52
}
53

    
54
if ($pkg['include_file'] != "") {
55
	require_once($pkg['include_file']);
56
}
57

    
58
if (!isset($pkg['adddeleteeditpagefields'])) {
59
	$only_edit = true;
60
} else {
61
	$only_edit = false;
62
}
63

    
64
$id = $_REQUEST['id'];
65
if (isset($_POST['id'])) {
66
	$id = htmlspecialchars($_POST['id']);
67
}
68

    
69
// Not posting?	 Then user is editing a record. There must be a valid id
70
// when editing a record.
71
if (!$id && !$_POST) {
72
	$id = "0";
73
}
74

    
75
if (!is_numeric($id)) {
76
	header("Location: /");
77
	exit;
78
}
79

    
80
if ($pkg['custom_php_global_functions'] != "") {
81
	eval($pkg['custom_php_global_functions']);
82
}
83

    
84
// grab the installedpackages->package_name section.
85
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])])) {
86
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])] = array();
87
}
88

    
89
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
90
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
91
}
92

    
93
/* If the first entry in the array is an empty <config/> tag, kill it.
94
 * See the following tickets for more:
95
 *  https://redmine.pfsense.org/issues/7624
96
 *  https://redmine.pfsense.org/issues/476
97
 */
98
if ($config['installedpackages'] &&
99
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
100
    (empty($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0])) &&
101
    is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
102
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
103
}
104

    
105
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
106

    
107
if ($_REQUEST['savemsg'] != "") {
108
	$savemsg = htmlspecialchars($_REQUEST['savemsg']);
109
}
110

    
111
if ($pkg['custom_php_command_before_form'] != "") {
112
	eval($pkg['custom_php_command_before_form']);
113
}
114

    
115
if ($_POST) {
116
	$rows = 0;
117

    
118
	$input_errors = array();
119
	$reqfields = array();
120
	$reqfieldsn = array();
121
	foreach ($pkg['fields']['field'] as $field) {
122
		if (isset($field['required'])) {
123
			if ($field['fieldname']) {
124
				$reqfields[] = $field['fieldname'];
125
			}
126
			if ($field['fielddescr']) {
127
				$reqfieldsn[] = $field['fielddescr'];
128
			}
129
		}
130
	}
131
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
132

    
133
	if ($pkg['custom_php_validation_command']) {
134
		eval($pkg['custom_php_validation_command']);
135
	}
136

    
137
	if ($_POST['act'] == "del") {
138
		if ($pkg['custom_delete_php_command']) {
139
			if ($pkg['custom_php_command_before_form'] != "") {
140
				eval($pkg['custom_php_command_before_form']);
141
			}
142
			eval($pkg['custom_delete_php_command']);
143
		}
144
		write_config($pkg['delete_string']);
145
		// resync the configuration file code if defined.
146
		if ($pkg['custom_php_resync_config_command'] != "") {
147
			if ($pkg['custom_php_command_before_form'] != "") {
148
				eval($pkg['custom_php_command_before_form']);
149
			}
150
			eval($pkg['custom_php_resync_config_command']);
151
		}
152
	} else {
153
		if (!$input_errors && $pkg['custom_add_php_command']) {
154
			if ($pkg['donotsave'] != "" or $pkg['preoutput'] != "") {
155
				include("head.inc");
156
			}
157

    
158
			if ($pkg['preoutput']) {
159
				echo "<pre>";
160
			}
161
			eval($pkg['custom_add_php_command']);
162
			if ($pkg['preoutput']) {
163
				echo "</pre>";
164
			}
165
		}
166
	}
167

    
168
	if (empty($pkg['donotsave'])) {
169

    
170
		// store values in xml configuration file.
171
		if (!$input_errors) {
172
			$pkgarr = array();
173
			foreach ($pkg['fields']['field'] as $fields) {
174
				switch ($fields['type']) {
175
					case "rowhelper":
176
						// save rowhelper items.
177
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
178
						$rowhelpername="row";
179
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
180
							foreach ($_POST as $key => $value) {
181
								$matches = array();
182
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
183
									if ($rowhelperfield['type'] == "textarea") {
184
										$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = unixnewlines($value);
185
									} else {
186
										$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = $value;
187
									}
188
								}
189
							}
190
						}
191
						break;
192
					case "textarea":
193
						$fieldname = $fields['fieldname'];
194
						$fieldvalue = unixnewlines(trim($_POST[$fieldname]));
195
						if ($fields['encoding'] == 'base64') {
196
							$fieldvalue = base64_encode($fieldvalue);
197
						}
198
						if ($fieldname) {
199
							$pkgarr[$fieldname] = $fieldvalue;
200
						}
201
					default:
202
						$fieldname = $fields['fieldname'];
203
						if ($fieldname == "interface_array") {
204
							$fieldvalue = $_POST[$fieldname];
205
						} elseif (is_array($_POST[$fieldname])) {
206
							$fieldvalue = implode(',', $_POST[$fieldname]);
207
						} else {
208
							$fieldvalue = trim($_POST[$fieldname]);
209
							if ($fields['encoding'] == 'base64') {
210
								$fieldvalue = base64_encode($fieldvalue);
211
							}
212
						}
213
						if ($fieldname) {
214
							$pkgarr[$fieldname] = $fieldvalue;
215
						}
216
					}
217
			}
218

    
219
			/* If the user supplied an ID and it exists, or if id=0
220
			 * and the settings are invalid, overwrite.
221
			 * See https://redmine.pfsense.org/issues/7624
222
			 */
223
			if (isset($id) && ($a_pkg[$id] ||
224
			   (($id == 0) && !is_array($a_pkg[$id])) )) {
225
				$a_pkg[$id] = $pkgarr;
226
			} else {
227
				$a_pkg[] = $pkgarr;
228
			}
229

    
230
			write_config($pkg['addedit_string']);
231
			// late running code
232
			if ($pkg['custom_add_php_command_late'] != "") {
233
				eval($pkg['custom_add_php_command_late']);
234
			}
235

    
236
			if (isset($pkg['filter_rules_needed'])) {
237
				filter_configure();
238
			}
239

    
240
			// resync the configuration file code if defined.
241
			if ($pkg['custom_php_resync_config_command'] != "") {
242
				eval($pkg['custom_php_resync_config_command']);
243
			}
244

    
245
			parse_package_templates();
246

    
247
			/* if start_command is defined, restart w/ this */
248
			if ($pkg['start_command'] != "") {
249
				exec($pkg['start_command'] . ">/dev/null 2&>1");
250
			}
251

    
252
			/* if restart_command is defined, restart w/ this */
253
			if ($pkg['restart_command'] != "") {
254
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
255
			}
256

    
257
			if ($pkg['aftersaveredirect'] != "") {
258
				pfSenseHeader($pkg['aftersaveredirect']);
259
			} elseif (!$pkg['adddeleteeditpagefields']) {
260
				pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
261
			} elseif (!$pkg['preoutput']) {
262
				pfSenseHeader("pkg.php?xml=" . $xml);
263
			}
264
			exit;
265
		} else {
266
			$get_from_post = true;
267
		}
268
	} elseif (!$input_errors) {
269
		// donotsave is enabled.  lets simply exit.
270
		exit;
271
	}
272
}
273

    
274

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

    
281
	// Substitute everything inbetween with our new classes
282
	if ($t && $c && (($c - $t) < 200)) {
283
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
284
	}
285
}
286

    
287
/*
288
 * ROW helper function. Creates one element in the row from a PHP table by adding
289
 * the specified element to $group
290
 */
291
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description, $ewidth = null) {
292
	global $text, $group, $config;
293

    
294
	switch ($type) {
295
		case "input":
296
			$inpt = new Form_Input(
297
				$fieldname . $trc,
298
				null,
299
				'text',
300
				$value
301
			);
302

    
303
			$inpt->setHelp($description);
304

    
305
			if ($ewidth) {
306
				$inpt->setWidth($ewidth);
307
			}
308

    
309
			$group->add($inpt);
310
			break;
311
		case "checkbox":
312
			$group->add(new Form_Checkbox(
313
				$fieldname . $trc,
314
				null,
315
				null,
316
				$value,
317
				'ON'
318
			))->setHelp($description);
319

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

    
336
			break;
337
		case "select":
338
			$options = array();
339
			foreach ($rowhelper['options']['option'] as $rowopt) {
340
				$options[$rowopt['value']] = $rowopt['name'];
341
			}
342

    
343
			$grp = new Form_Select(
344
				$fieldname . $trc,
345
				null,
346
				$value,
347
				$options
348
			);
349

    
350
			$grp->setHelp($description);
351

    
352
			if ($ewidth) {
353
				$grp->setWidth($ewidth);
354
			}
355

    
356
			$group->add($grp);
357

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

    
372
			if (is_array($value)) {
373
				$values = $value;
374
			} else {
375
				$values = explode(',', $value);
376
			}
377

    
378
			$ifaces["lo0"] = "loopback";
379
			$options = array();
380
			$selected = array();
381

    
382
			foreach ($ifaces as $ifname => $iface) {
383
				$options[$ifname] = $iface;
384

    
385
				if (in_array($ifname, $values)) {
386
					array_push($selected, $ifname);
387
				}
388
			}
389

    
390
			$group->add(new Form_Select(
391
				$fieldname . $trc,
392
				null,
393
				($multiple) ? $selected:$selected[0],
394
				$options,
395
				$multiple
396
			))->setHelp($description);
397

    
398
			//echo "</select>\n";
399
			break;
400
		case "select_source":
401
			$options = array();
402
			$selected = array();
403

    
404
			if (isset($rowhelper['show_disable_value'])) {
405
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
406
			}
407

    
408
			$source_url = $rowhelper['source'];
409
			eval("\$pkg_source_txt = &$source_url;");
410

    
411
			foreach ($pkg_source_txt as $opt) {
412
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
413
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
414
				$options[$source_value] = $source_name;
415

    
416
				if ($source_value == $value) {
417
					array_push($selected, $value);
418
				}
419
			}
420

    
421
			$group->add(new Form_Select(
422
				$fieldname . $trc,
423
				null,
424
				($multiple) ? $selected:$selected[0],
425
				$options,
426
				$multiple
427
			))->setHelp($description);
428

    
429
			break;
430
	}
431
}
432

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

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

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

    
508
			/* replace cr's */
509
			$template_text = str_replace("\\n", "\n", $template_text);
510

    
511
			/* write out new template file */
512
			$fout = fopen($filename, "w");
513
			fwrite($fout, $template_text);
514
			fclose($fout);
515
		}
516
	}
517
}
518

    
519
//breadcrumb
520
if ($pkg['title'] != "") {
521
	if (!$only_edit) {
522
		$pkg['title'] = $pkg['title'] . '/Edit';
523
	}
524

    
525
	if (strpos($pkg['title'], '/')) {
526
		$title = explode('/', $pkg['title']);
527

    
528
		foreach ($title as $subtitle) {
529
			$pgtitle[] = gettext($subtitle);
530
			$pglinks[] = ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']);
531
		}
532
	} else {
533
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
534
		$pglinks = array("", ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']));
535
	}
536
} else {
537
	$pgtitle = array(gettext("Package"), gettext("Editor"));
538
	$pglinks = array("", "@self");
539
}
540

    
541
// Create any required tabs
542
if ($pkg['tabs'] != "") {
543
	$tab_array = array();
544
	foreach ($pkg['tabs']['tab'] as $tab) {
545
		if ($tab['tab_level']) {
546
			$tab_level = $tab['tab_level'];
547
		} else {
548
			$tab_level = 1;
549
		}
550

    
551
		if (isset($tab['active'])) {
552
			$active = true;
553
			$pgtitle[] = $tab['text'] ;
554
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
555
		} else {
556
			$active = false;
557
		}
558

    
559
		$urltmp = "";
560
		if ($tab['url'] != "") {
561
			$urltmp = $tab['url'];
562
		}
563

    
564
		if ($tab['xml'] != "") {
565
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
566
		}
567

    
568
		$addresswithport = getenv("HTTP_HOST");
569
		$colonpos = strpos($addresswithport, ":");
570

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

    
578
		// eval url so that above $myurl item can be processed if need be.
579
		$url = str_replace('$myurl', $myurl, $urltmp);
580

    
581
		$tab_array[$tab_level][] = array(
582
			$tab['text'],
583
			$active,
584
			$url
585
		);
586
	}
587

    
588
	ksort($tab_array);
589
}
590

    
591
include("head.inc");
592
if ($pkg['custom_php_after_head_command']) {
593
	eval($pkg['custom_php_after_head_command']);
594
}
595
if (isset($tab_array)) {
596
	foreach ($tab_array as $tabid => $tab) {
597
		display_top_tabs($tab);
598
	}
599
}
600

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

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

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

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

    
621
$saveicon = "fa-save";
622
if ($pkg['saveicon'] != "") {
623
	$saveicon = $pkg['saveicon'];
624
}
625

    
626
$savebtnclass = "btn-primary";
627
if ($pkg['savebtnclass'] != "") {
628
	$savebtnclass = $pkg['savebtnclass'];
629
}
630

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

    
633
$savebutton = new Form_Button(
634
	'submit',
635
	$savevalue,
636
	null,
637
	$saveicon
638
);
639
$savebutton->addClass($savebtnclass);
640

    
641
if ($savehelp) {
642
	$savebutton->setHelp($savehelp);
643
}
644

    
645
$form = new Form($savebutton);
646

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

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

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

    
666
$js_array = array();
667

    
668
// Now loop through all of the fields defined in the XML
669
if (!is_array($pkg['fields']['field'])) {
670
	$pkg['fields']['field'] = array();
671
}
672
foreach ($pkg['fields']['field'] as $pkga) {
673

    
674
	$action = "";
675
	$uid = "";
676

    
677
	if ($pkga['type'] == "sorting") {
678
		continue;
679
	}
680

    
681
	// Generate a new section
682
	if ($pkga['type'] == "listtopic") {
683
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
684
			$advanced->addInput(new Form_StaticText(
685
				strip_tags($pkga['name']),
686
				null
687
			));
688

    
689
			$advfield_count++;
690
		} else {
691
			if (isset($section)) {
692
				$form->add($section);
693
			}
694

    
695
			if (isset($pkga['collapse'])) {
696
				$uid = uniqid("section");
697

    
698
				$action = COLLAPSIBLE;
699

    
700
				if ($pkga['collapse'] == "open") {
701
					$action |= SEC_OPEN;
702
				} else {
703
					$action |= SEC_CLOSED;
704
				}
705
			}
706

    
707
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
708
		}
709

    
710
		continue;
711
	}
712

    
713
	// 'begin' starts a form group. ('end' ends it)
714
	if ($pkga['combinefields'] == "begin") {
715
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
716
		$grouping = true;
717
	}
718

    
719
	$size = "";
720
	$colspan="";
721

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

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

    
746
	// If this is a required field, pre-pend a "*" to the field description
747
	// This tells the system to add "element-required" class text decoration to the field label
748
	if (isset($pkga['required'])) {
749
		$pkga['fielddescr'] = "*" . $pkga['fielddescr'];
750
	}
751

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

    
760
			$grp = new Form_Input(
761
					$pkga['fieldname'],
762
					$pkga['fielddescr'],
763
					'text',
764
					$value
765
				);
766

    
767
			$grp->setHelp($pkga['description']);
768

    
769
			if ($pkga['width']) {
770
				$grp->setWidth($pkga['width']);
771
			}
772

    
773
			if ($grouping) {
774
				$group->add($grp);
775
			} else {
776
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
777
					$advanced->addInput($grp);
778
				} else {
779
					$section->addInput($grp);
780
				}
781
			}
782

    
783
			break;
784

    
785
		case "password":
786
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
787
				$value = base64_decode($value);
788
			}
789

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

    
816
			break;
817

    
818
		case "info":
819
			// If the info contains a table we should detect and Bootstrap it
820

    
821
			if (strpos($pkga['description'], '<table') !== FALSE) {
822
				$info = bootstrapTable($pkga['description']);
823
			} else {
824
				$info = $pkga['description'];
825
			}
826

    
827
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
828
				$advanced->addInput(new Form_StaticText(
829
					strip_tags($pkga['fielddescr']),
830
					$info
831
				));
832
			} else {
833
				$section->addInput(new Form_StaticText(
834
					strip_tags($pkga['fielddescr']),
835
					$info
836
				));
837
			}
838

    
839
			break;
840

    
841
		case "select":
842
			// Create a select element
843
			$optionlist = array();
844
			$selectedlist = array();
845

    
846
			$fieldname = $pkga['fieldname'];
847

    
848
			if (isset($pkga['multiple'])) {
849
				$multiple = 'multiple';
850
				$items = explode(',', $value);
851
				$fieldname .= "[]";
852
			} else {
853
				$multiple = '';
854
				$items = array($value);
855
			}
856

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

    
859
			foreach ($pkga['options']['option'] as $opt) {
860
				$optionlist[$opt['value']] = $opt['name'];
861

    
862
				if (in_array($opt['value'], $items)) {
863
					array_push($selectedlist, $opt['value']);
864
				}
865
			}
866

    
867
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
868
				$function = $grouping ? $advanced->add:$advanced->addInput;
869
			} else {
870
				$function = ($grouping) ? $section->add:$section->addInput;
871
			}
872

    
873
			$grp = new Form_Select(
874
						$pkga['fieldname'],
875
						strip_tags($pkga['fielddescr']),
876
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
877
						$optionlist,
878
						isset($pkga['multiple'])
879
					);
880

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

    
883
			if ($pkga['width']) {
884
				$grp->setWidth($pkga['width']);
885
			}
886

    
887
			if ($grouping) {
888
				$group->add($grp);
889
			} else {
890
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
891
					$advanced->addInput($grp);
892
				} else {
893
					$section->addInput($grp);
894
				}
895
			}
896

    
897
			break;
898

    
899
		case "select_source":
900

    
901
			if (isset($pkga['multiple'])) {
902
				$items = explode(',', $value);
903
				$fieldname .= "[]";
904
			} else {
905
				$items = array($value);
906
			}
907

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

    
910
			$source_url = $pkga['source'];
911
			try{
912
				@eval("\$pkg_source_txt = &$source_url;");
913
			} catch (\Throwable | \Error | \Exception $e) {
914
				//do nothing
915
			}
916
			#check if show disable option is present on xml
917
			if (!is_array($pkg_source_txt)) {
918
				$pkg_source_txt = array();
919
			}
920
			if (isset($pkga['show_disable_value'])) {
921
				array_push($pkg_source_txt,
922
					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']));
923
			}
924

    
925
			$srcoptions = array();
926
			$srcselected = array();
927

    
928
			foreach ($pkg_source_txt as $opt) {
929
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
930
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
931
				$srcoptions[$source_value] = $source_name;
932

    
933
				if (in_array($source_value, $items)) {
934
					array_push($srcselected, $source_value);
935
				}
936
			}
937

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

    
967
			break;
968

    
969
		case "vpn_selection" :
970
			$vpnlist = array();
971

    
972
			foreach ($config['ipsec']['phase1'] as $vpn) {
973
				$vpnlist[$vpn['descr']] = $vpn['descr'];
974

    
975
			}
976

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

    
1002
			break;
1003

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

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

    
1047
			break;
1048

    
1049
		// Create a textarea element
1050
		case "textarea":
1051
			$rows = $cols = 0;
1052

    
1053
			if ($pkga['rows']) {
1054
				$rows = $pkga['rows'];
1055
			}
1056
			if ($pkga['cols']) {
1057
				$cols = $pkga['cols'];
1058
			}
1059

    
1060
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1061
				$value = base64_decode($value);
1062
			}
1063

    
1064
			$grp = new Form_Textarea(
1065
					$pkga['fieldname'],
1066
					$pkga['fielddescr'],
1067
					$value
1068
			);
1069

    
1070
			$grp->setHelp(fixup_string($pkga['description']));
1071

    
1072
			if ($rows > 0) {
1073
				$grp->setRows($rows);
1074
			}
1075

    
1076
			if ($cols > 0) {
1077
				$grp->setCols($cols);
1078
			}
1079

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

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

    
1097
			break;
1098

    
1099
		case "aliases":
1100

    
1101
			// Use xml tag <typealiases> to filter type aliases
1102
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1103
			$fieldname = $pkga['fieldname'];
1104

    
1105
			if (!is_array($config['aliases'])) {
1106
				$config['aliases'] = array();
1107
			}
1108
			
1109
			$a_aliases = &$config['aliases']['alias'];
1110
			$addrisfirst = 0;
1111
			$aliasesaddr = "";
1112

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

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

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

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

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

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

    
1169
			echo $script;
1170

    
1171
			break;
1172

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

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

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

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

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

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

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

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

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

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

    
1279
			break;
1280

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

    
1311
			break;
1312

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

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

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

    
1351
			break;
1352

    
1353
		case "schedule_selection":
1354

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

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

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

    
1399
			break;
1400

    
1401
		case "rowhelper":
1402

    
1403
			$rowhelpername="row";
1404

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

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

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

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

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

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

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

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

    
1444

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

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

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

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

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

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

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

    
1483
			break;
1484

    
1485
	}
1486

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

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

    
1499
			$grouping = false;
1500
		}
1501

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

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

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

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

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

    
1529
print($form);
1530

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

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

    
1539

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1622
<?php
1623
}
1624

    
1625
include("foot.inc");
(102-102/234)