Project

General

Profile

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

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

    
63
ini_set('max_execution_time', '0');
64

    
65
require("guiconfig.inc");
66
require_once("functions.inc");
67
require_once("filter.inc");
68
require_once("shaper.inc");
69
require_once("pkg-utils.inc");
70

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

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

    
81
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
82

    
83
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
84
	include("head.inc");
85
	print_info_box_np(gettext("ERROR: No valid package defined."));
86
	include("foot.inc");
87
	die;
88
} else {
89
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
90
}
91

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
257
			parse_package_templates();
258

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

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

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

    
285

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
440
			break;
441
	}
442
}
443

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
599
	ksort($tab_array);
600
}
601

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

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

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

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

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

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

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

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

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

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

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

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

    
664
$js_array = array();
665

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

    
669
	if ($pkga['type'] == "sorting") {
670
		continue;
671
	}
672

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

    
681
			$advfield_count++;
682
		}  else {
683
			if (isset($section)) {
684
				$form->add($section);
685
			}
686

    
687
			$section = new Form_Section(strip_tags($pkga['name']));
688
		}
689

    
690
		continue;
691
	}
692

    
693
	// 'begin' starts a form group. ('end' ends it)
694
	if ($pkga['combinefields'] == "begin") {
695
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
696
		$grouping = true;
697
	}
698

    
699
	$size = "";
700
	$colspan="";
701

    
702
	// if user is editing a record, load in the data.
703
	$fieldname = $pkga['fieldname'];
704
	unset($value);
705
	if ($get_from_post) {
706
		$value = $_POST[$fieldname];
707
		if (is_array($value)) {
708
			$value = implode(',', $value);
709
		}
710
	} else {
711
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
712
			$value = $a_pkg[$id][$fieldname];
713
		} else {
714
			if (isset($pkga['default_value'])) {
715
				$value = $pkga['default_value'];
716
			}
717
		}
718
	}
719

    
720
	// If we get here but have no $section, the package config file probably had no listtopic field
721
	// We can create a section with a generic name to fix that
722
	if (!$section) {
723
		$section = new Form_Section(gettext('General options'));
724
	}
725

    
726
	switch ($pkga['type']) {
727
		// Create an input element. The format is slightly different depending on whether we are composing a group,
728
		// section, or advanced section. This is true for every element type
729
		case "input":
730
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
731
				$value = base64_decode($value);
732
			}
733

    
734
			$grp = new Form_Input(
735
					$pkga['fieldname'],
736
					$pkga['fielddescr'],
737
					'text',
738
					$value
739
				);
740

    
741
			$grp->setHelp($pkga['description']);
742

    
743
			if ($pkga['width']) {
744
				$grp->setWidth($pkga['width']);
745
			}
746

    
747
			if ($grouping) {
748
				$group->add($grp);
749
			} else {
750
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
751
					$advanced->addInput($grp);
752
				} else {
753
					$section->addInput($grp);
754
				}
755
			}
756

    
757
			break;
758

    
759
		case "password":
760
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
761
				$value = base64_decode($value);
762
			}
763

    
764
			// Create a password element
765
			if ($grouping) {
766
				$group->add(new Form_Input(
767
					$pkga['fieldname'],
768
					$pkga['fielddescr'],
769
					'password',
770
					$value
771
				))->setHelp($pkga['description']);
772
			} else {
773
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
774
					$advanced->addInput(new Form_Input(
775
						$pkga['fieldname'],
776
						$pkga['fielddescr'],
777
						'password',
778
						$value
779
					))->setHelp($pkga['description']);
780
				} else {
781
					$section->addInput(new Form_Input(
782
						$pkga['fieldname'],
783
						$pkga['fielddescr'],
784
						'password',
785
						$value
786
					))->setHelp($pkga['description']);
787
				}
788
			}
789

    
790
			break;
791

    
792
		case "info":
793
			// If the info contains a table we should detect and Bootstrap it
794

    
795
			if (strpos($pkga['description'], '<table') !== FALSE) {
796
				$info = bootstrapTable($pkga['description']);
797
			} else {
798
				$info = $pkga['description'];
799
			}
800

    
801
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
802
				$advanced->addInput(new Form_StaticText(
803
					strip_tags($pkga['fielddescr']),
804
					$info
805
				));
806
			} else {
807
				$section->addInput(new Form_StaticText(
808
					strip_tags($pkga['fielddescr']),
809
					$info
810
				));
811
			}
812

    
813
			break;
814

    
815
		case "select":
816
			// Create a select element
817
			$optionlist = array();
818
			$selectedlist = array();
819

    
820
			$fieldname = $pkga['fieldname'];
821

    
822
			if (isset($pkga['multiple'])) {
823
				$multiple = 'multiple';
824
				$items = explode(',', $value);
825
				$fieldname .= "[]";
826
			} else {
827
				$multiple = '';
828
				$items = array($value);
829
			}
830

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

    
833
			foreach ($pkga['options']['option'] as $opt) {
834
				$optionlist[$opt['value']] = $opt['name'];
835

    
836
				if (in_array($opt['value'], $items)) {
837
					array_push($selectedlist, $opt['value']);
838
				}
839
			}
840

    
841
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
842
				$function = $grouping ? $advanced->add:$advanced->addInput;
843
			} else {
844
				$function = ($grouping) ? $section->add:$section->addInput;
845
			}
846

    
847
			$grp = new Form_Select(
848
						$pkga['fieldname'],
849
						strip_tags($pkga['fielddescr']),
850
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
851
						$optionlist,
852
						isset($pkga['multiple'])
853
					);
854

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

    
857
			if ($pkga['width']) {
858
				$grp->setWidth($pkga['width']);
859
			}
860

    
861
			if ($grouping) {
862
				$group->add($grp);
863
			} else {
864
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
865
					$advanced->addInput($grp);
866
				} else {
867
					$section->addInput($grp);
868
				}
869
			}
870

    
871
			break;
872

    
873
		case "select_source":
874

    
875
			if (isset($pkga['multiple'])) {
876
				$items = explode(',', $value);
877
				$fieldname .= "[]";
878
			} else {
879
				$items = array($value);
880
			}
881

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

    
884
			$source_url = $pkga['source'];
885
			eval("\$pkg_source_txt = &$source_url;");
886

    
887
			#check if show disable option is present on xml
888
			if (isset($pkga['show_disable_value'])) {
889
				array_push($pkg_source_txt,
890
					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']));
891
			}
892

    
893
			$srcoptions = array();
894
			$srcselected = array();
895

    
896
			foreach ($pkg_source_txt as $opt) {
897
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
898
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
899
				$srcoptions[$source_value] = $source_name;
900

    
901
				if (in_array($source_value, $items)) {
902
					array_push($srcselected, $source_value);
903
				}
904
			}
905

    
906
			if ($grouping) {
907
				$group->add(new Form_Select(
908
					$pkga['fieldname'],
909
					strip_tags($pkga['fielddescr']),
910
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
911
					$srcoptions,
912
					isset($pkga['multiple'])
913
				))->setOnchange($onchange);
914
			} else {
915
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
916
					$advanced->addInput(new Form_Select(
917
						$pkga['fieldname'],
918
						strip_tags($pkga['fielddescr']),
919
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
920
						$srcoptions,
921
						isset($pkga['multiple'])
922
					))->setOnchange($onchange);
923
				} else {
924
					$section->addInput(new Form_Select(
925
						$pkga['fieldname'],
926
						strip_tags($pkga['fielddescr']),
927
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
928
						$srcoptions,
929
						isset($pkga['multiple'])
930
					))->setOnchange($onchange);
931
				}
932
			}
933

    
934
			break;
935

    
936
		case "vpn_selection" :
937
			$vpnlist = array();
938

    
939
			foreach ($config['ipsec']['phase1'] as $vpn) {
940
				$vpnlist[$vpn['descr']] = $vpn['descr'];
941

    
942
			}
943

    
944
			if ($grouping) {
945
				$group->add(new Form_Select(
946
					$pkga['fieldname'],
947
					null,
948
					false,
949
					$vpnlist
950
				))->setHelp(fixup_string($pkga['description']));
951
			} else {
952
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
953
					$advanced->addInput(new Form_Select(
954
						$pkga['fieldname'],
955
						null,
956
						false,
957
						$vpnlist
958
					))->setHelp(fixup_string($pkga['description']));
959
				} else {
960
					$section->addInput(new Form_Select(
961
						$pkga['fieldname'],
962
						null,
963
						false,
964
						$vpnlist
965
					))->setHelp(fixup_string($pkga['description']));
966
				}
967
			}
968

    
969
			break;
970

    
971
		// Create a checkbox element
972
		case "checkbox":
973
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
974
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
975
				$onclick = 'javascript:enablechange();';
976
			} else {
977
				$onclick = '';
978
			}
979

    
980
			if ($grouping) {
981
				$group->add(new Form_Checkbox(
982
					$pkga['fieldname'],
983
					$pkga['fielddescr'],
984
					fixup_string($pkga['description']),
985
					($value == "on"),
986
					'on'
987
				))->setOnclick($onclick)
988
				  ->setOnchange($onchange)
989
				  ->setHelp($pkga['sethelp']);
990
			} else {
991
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
992
					$advanced->addInput(new Form_Checkbox(
993
						$pkga['fieldname'],
994
						$pkga['fielddescr'],
995
						fixup_string($pkga['description']),
996
						($value == "on"),
997
						'on'
998
					))->setOnclick($onclick)
999
					  ->setOnchange($onchange)
1000
					  ->setHelp($pkga['sethelp']);
1001
				} else {
1002
					$section->addInput(new Form_Checkbox(
1003
						$pkga['fieldname'],
1004
						$pkga['fielddescr'],
1005
						fixup_string($pkga['description']),
1006
						($value == "on"),
1007
						'on'
1008
					))->setOnclick($onclick)
1009
					  ->setOnchange($onchange)
1010
					  ->setHelp($pkga['sethelp']);
1011
				}
1012
			}
1013

    
1014
			break;
1015

    
1016
		// Create a textarea element
1017
		case "textarea":
1018
			$rows = $cols = 0;
1019

    
1020
			if ($pkga['rows']) {
1021
				$rows = $pkga['rows'];
1022
			}
1023
			if ($pkga['cols']) {
1024
				$cols = $pkga['cols'];
1025
			}
1026

    
1027
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1028
				$value = base64_decode($value);
1029
			}
1030

    
1031
			$grp = new Form_Textarea(
1032
					$pkga['fieldname'],
1033
					$pkga['fielddescr'],
1034
					$value
1035
			);
1036

    
1037
			$grp->setHelp(fixup_string($pkga['description']));
1038

    
1039
			if ($rows > 0) {
1040
				$grp->setRows($rows);
1041
			}
1042

    
1043
			if ($cols > 0) {
1044
				$grp->setCols($cols);
1045
			}
1046

    
1047
			if ($pkga['wrap'] == "off") {
1048
				$grp->setAttribute("wrap", "off");
1049
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1050
			} else {
1051
				$grp->setAttribute("style", "width: auto;");
1052
			}
1053

    
1054
			if ($grouping) {
1055
				$group->add($grp);
1056
			} else {
1057
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1058
					$advanced->addInput($grp);
1059
				} else {
1060
					$section->addInput($grp);
1061
				}
1062
			}
1063

    
1064
			break;
1065

    
1066
		case "aliases":
1067

    
1068
			// Use xml tag <typealiases> to filter type aliases
1069
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1070
			$fieldname = $pkga['fieldname'];
1071
			$a_aliases = &$config['aliases']['alias'];
1072
			$addrisfirst = 0;
1073
			$aliasesaddr = "";
1074

    
1075
			if (isset($a_aliases)) {
1076
				if (!empty($pkga['typealiases'])) {
1077
					foreach ($a_aliases as $alias) {
1078
						if ($alias['type'] == $pkga['typealiases']) {
1079
							if ($addrisfirst == 1) {
1080
								$aliasesaddr .= ",";
1081
							}
1082
							$aliasesaddr .= "'" . $alias['name'] . "'";
1083
							$addrisfirst = 1;
1084
						}
1085
					}
1086
				} else {
1087
					foreach ($a_aliases as $alias) {
1088
						if ($addrisfirst == 1) {
1089
							$aliasesaddr .= ",";
1090
						}
1091
						$aliasesaddr .= "'" . $alias['name'] . "'";
1092
						$addrisfirst = 1;
1093
					}
1094
				}
1095
			}
1096

    
1097
			$grp = new Form_Input(
1098
					$pkga['fieldname'],
1099
					$pkga['fielddescr'],
1100
					'text',
1101
					$value
1102
				);
1103

    
1104
			$grp->setHelp($pkga['description']);
1105

    
1106
			if ($pkga['width']) {
1107
				$grp->setWidth($pkga['width']);
1108
			}
1109

    
1110
			if (grouping) {
1111
				$group->add($grp);
1112
			} else {
1113
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1114
					$advanced->addInput($grp);
1115
				} else {
1116
					$section->addInput($grp);
1117
				}
1118
			}
1119

    
1120
			$script = "<script type='text/javascript'>\n";
1121
			$script .= "//<![CDATA[\n";
1122
			$script .= "events.push(function(){\n";
1123
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1124
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1125
			$script .= "		source: aliasarray\n";
1126
			$script .= "	})\n";
1127
			$script .= "});\n";
1128
			$script .= "//]]>\n";
1129
			$script .= "</script>";
1130

    
1131
			echo $script;
1132

    
1133
			break;
1134

    
1135
		case "interfaces_selection":
1136
			$ips = array();
1137
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1138
			if (is_array($config['interfaces'])) {
1139
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1140
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1141
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1142
						if (isset($pkga['showips'])) {
1143
							$iface_description .= " address";
1144
						}
1145
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1146
					}
1147
				}
1148
			}
1149

    
1150
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1151
				foreach ($config['virtualip']['vip'] as $vip) {
1152
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1153
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1154
					}
1155
					switch ($vip['mode']) {
1156
						case "ipalias":
1157
						case "carp":
1158
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1159
							break;
1160
						case "proxyarp":
1161
							if ($vip['type'] == "network") {
1162
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1163
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1164
								$len = $end - $start;
1165
								for ($i = 0; $i <= $len; $i++) {
1166
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1167
								}
1168
							} else {
1169
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1170
							}
1171
							break;
1172
					}
1173
				}
1174
			}
1175

    
1176
			sort($ips);
1177
			if (isset($pkga['showlistenall'])) {
1178
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1179
			}
1180

    
1181
			if (!preg_match("/$interface_regex/", "loopback")) {
1182
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1183
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1184
			}
1185

    
1186
			#show interfaces array on gui
1187
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1188
			$multiple = '';
1189
			$fieldname = $pkga['fieldname'];
1190
			if (isset($pkga['multiple'])) {
1191
				$fieldname .= '[]';
1192
				$multiple = 'multiple';
1193
			}
1194

    
1195
			$selectedlist = array();
1196
			$optionlist = array();
1197

    
1198
			if (is_array($value)) {
1199
				$values = $value;
1200
			} else {
1201
				$values = explode(',', $value);
1202
			}
1203

    
1204
			foreach ($ips as $iface) {
1205
				if (in_array($iface['ip'], $values)) {
1206
					array_push($selectedlist, $iface['ip']);
1207
				}
1208

    
1209
				$optionlist[$iface['ip']] = $iface['description'];
1210
			}
1211

    
1212
			if ($grouping) {
1213
				$group->add(new Form_Select(
1214
					$pkga['fieldname'],
1215
					$pkga['fielddescr'],
1216
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1217
					$optionlist,
1218
					isset($pkga['multiple'])
1219
				))->setHelp($pkga['description']);
1220
			} else {
1221
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1222
					$advanced->addInput(new Form_Select(
1223
						$pkga['fieldname'],
1224
						$pkga['fielddescr'],
1225
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1226
						$optionlist,
1227
						isset($pkga['multiple'])
1228
					))->setHelp($pkga['description']);
1229
				} else {
1230
					$section->addInput(new Form_Select(
1231
						$pkga['fieldname'],
1232
						$pkga['fielddescr'],
1233
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1234
						$optionlist,
1235
						isset($pkga['multiple'])
1236
					))->setHelp($pkga['description']);
1237
				}
1238
			}
1239

    
1240
			break;
1241

    
1242
		// Create radio button
1243
		case "radio":
1244
			if ($grouping) {
1245
				$group->add(new Form_Checkbox(
1246
					$pkga['fieldname'],
1247
					$pkga['fielddescr'],
1248
					fixup_string($pkga['description']),
1249
					($value == "on"),
1250
					'on'
1251
				))->displayAsRadio();
1252
			} else {
1253
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1254
					$advanced->addInput(new Form_Checkbox(
1255
						$pkga['fieldname'],
1256
						$pkga['fielddescr'],
1257
						fixup_string($pkga['description']),
1258
						($value == "on"),
1259
						'on'
1260
					))->displayAsRadio();
1261
				} else {
1262
					$section->addInput(new Form_Checkbox(
1263
						$pkga['fieldname'],
1264
						$pkga['fielddescr'],
1265
						fixup_string($pkga['description']),
1266
						($value == "on"),
1267
						'on'
1268
					))->displayAsRadio();
1269
				}
1270
			}
1271

    
1272
			break;
1273

    
1274
		// Create form button
1275
		case "button":
1276
			$newbtn = new Form_Button(
1277
				$pkga['fieldname'],
1278
				$pkga['fieldname']
1279
			);
1280

    
1281
			if (grouping) {
1282
				$group->add(new Form_StaticText(
1283
					null,
1284
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1285
				));
1286
			} else {
1287
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1288
					$advanced->addInput(new Form_StaticText(
1289
						null,
1290
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1291
					));
1292
				} else {
1293
					$section->addInput(new Form_StaticText(
1294
						null,
1295
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1296
					));
1297
				}
1298
			}
1299

    
1300
			break;
1301

    
1302
		case "schedule_selection":
1303

    
1304
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1305
			$schedules = array();
1306
			$schedules[] = "none";
1307
			if (is_array($config['schedules']['schedule'])) {
1308
				foreach ($config['schedules']['schedule'] as $schedule) {
1309
					if ($schedule['name'] != "") {
1310
						$schedules[] = $schedule['name'];
1311
					}
1312
				}
1313
			}
1314

    
1315
			foreach ($schedules as $schedule) {
1316
				if ($schedule == "none") {
1317
					$schedlist[""] = $schedule;
1318
				} else {
1319
					$schedlist[$schedule] = $schedule;
1320
				}
1321
			}
1322

    
1323
			if ($grouping) {
1324
				$group->add(new Form_Select(
1325
					$pkga['fieldname'],
1326
					$pkga['fielddescr'],
1327
					$value,
1328
					$schedlist
1329
				))->setHelp(fixup_string($pkga['description']));
1330
			} else {
1331
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1332
					$advanced->addInput(new Form_Select(
1333
						$pkga['fieldname'],
1334
						$pkga['fielddescr'],
1335
						$value,
1336
						$schedlist
1337
					))->setHelp(fixup_string($pkga['description']));
1338
				} else {
1339
					$section->addInput(new Form_Select(
1340
						$pkga['fieldname'],
1341
						$pkga['fielddescr'],
1342
						$value,
1343
						$schedlist
1344
					))->setHelp(fixup_string($pkga['description']));
1345
				}
1346
			}
1347

    
1348
			break;
1349

    
1350
		case "rowhelper":
1351

    
1352
			$rowhelpername="row";
1353

    
1354
				$rowcounter = 0;
1355
				$trc = 0;
1356

    
1357
				//Use assigned $a_pkg or create an empty array to enter loop
1358
				if (isset($a_pkg[$id][$rowhelpername])) {
1359
					$saved_rows=$a_pkg[$id][$rowhelpername];
1360
				} else {
1361
					$saved_rows[] = array();
1362
				}
1363

    
1364
				$numrows = count($saved_rows) - 1;
1365

    
1366
				foreach ($saved_rows as $row) {
1367
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1368
					$group->addClass('repeatable');
1369

    
1370
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1371
						unset($value);
1372
						$width = null;
1373

    
1374
						if ($rowhelper['value'] != "") {
1375
							$value = $rowhelper['value'];
1376
						}
1377
						$fieldname = $rowhelper['fieldname'];
1378
						$fielddescr = $rowhelper['fielddescr'];
1379

    
1380
						// if user is editing a record, load in the data.
1381
						if (isset($id) && $a_pkg[$id]) {
1382
							$value = $row[$fieldname];
1383
						}
1384

    
1385
						$type = $rowhelper['type'];
1386
						if ($type == "input" || $type == "password" || $type == "textarea") {
1387
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1388
								$value = base64_decode($value);
1389
							}
1390
						}
1391
						$fieldname = $rowhelper['fieldname'];
1392

    
1393
						if ($rowhelper['size']) {
1394
							$size = $rowhelper['size'];
1395
						} else if ($pkga['size']) {
1396
							$size = $pkga['size'];
1397
						} else {
1398
							$size = "8";
1399
						}
1400

    
1401
						if ($rowhelper['width']) {
1402
							$width = $rowhelper['width'];
1403
						}
1404

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

    
1407
						$text = "";
1408
						$trc++;
1409
					}
1410

    
1411
					// Delete row button
1412
					$group->add(new Form_Button(
1413
						'deleterow' . $rowcounter,
1414
						'Delete',
1415
						null,
1416
						'fa-trash'
1417
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1418

    
1419
					$rowcounter++;
1420
					$section->add($group);
1421
				}
1422

    
1423
			// Add row button
1424
			$section->addInput(new Form_Button(
1425
				'addrow',
1426
				'Add'
1427
			))->removeClass('btn-primary')->addClass('btn-success');
1428

    
1429
			break;
1430

    
1431
	}
1432

    
1433
		if ($pkga['combinefields'] == "end") {
1434
			$group->add(new Form_StaticText(
1435
				null,
1436
				null
1437
			));
1438

    
1439
			if ($advanced) {
1440
				$advanced->add($group);
1441
			} else {
1442
				$section->add($group);
1443
			}
1444

    
1445
			$grouping = false;
1446
		}
1447

    
1448
	#increment counter
1449
	$i++;
1450
} // e-o-foreach field described in the XML
1451

    
1452
$form->add($section);
1453

    
1454
$form->addGlobal(new Form_Input(
1455
	'id',
1456
	null,
1457
	'hidden',
1458
	$id
1459
));
1460

    
1461
// If we created an advanced section, add it (and a button) to the form here
1462
if (!empty($advanced)) {
1463
	$form->addGlobal(new Form_Button(
1464
		'showadv',
1465
		'Show advanced options'
1466
	))->removeClass('btn-primary')->addClass('btn-default');
1467

    
1468
	$form->add($advanced);
1469
}
1470

    
1471
print($form);
1472

    
1473
if ($pkg['note'] != "") {
1474
	print_info_box($pkg['note']);
1475
}
1476

    
1477
if ($pkg['custom_php_after_form_command']) {
1478
	eval($pkg['custom_php_after_form_command']);
1479
}
1480

    
1481
if ($pkg['fields']['field'] != "") { ?>
1482
<script type="text/javascript">
1483
//<![CDATA[
1484
	events.push(function() {
1485

    
1486
	// Hide the advanced section
1487
	var advanced_visible = false;
1488

    
1489
	// Hide on page load
1490
	$('.advancedoptions').hide();
1491

    
1492
	// Suppress "Delete row" button if there are fewer than two rows
1493
	checkLastRow();
1494

    
1495
	// Show advanced section if you click the showadv button
1496
	$('#showadv').prop('type', 'button');
1497

    
1498
	$("#showadv").click(function() {
1499
		advanced_visible = !advanced_visible;
1500

    
1501
		if (advanced_visible) {
1502
			$('.advancedoptions').show();
1503
			$("#showadv").prop('value', 'Hide advanced Options');
1504
		} else {
1505
			$('.advancedoptions').hide();
1506
			$("#showadv").prop('value', 'Show advanced Options');
1507
		}
1508
	});
1509

    
1510
	// Call enablechange function
1511
	enablechange();
1512
});
1513

    
1514
	function enablechange() {
1515
<?php
1516
	foreach ($pkg['fields']['field'] as $field) {
1517
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1518
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1519

    
1520
			if (isset($field['enablefields'])) {
1521
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1522
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1523
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1524
					echo "\t\t}\n";
1525
				}
1526
			}
1527

    
1528
			if (isset($field['checkenablefields'])) {
1529
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1530
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1531
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1532
					echo "\t\t}\n";
1533
				}
1534
			}
1535

    
1536
			echo "\t}\n\telse {\n";
1537

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

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

    
1554
			echo "\t}\n";
1555
		}
1556
	}
1557
	?>
1558
	}
1559
//]]>
1560
</script>
1561

    
1562
<?php
1563
}
1564

    
1565
include("foot.inc");
(101-101/228)