Project

General

Profile

Download (40.5 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
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
57
	pfSense_MODULE: pkgs
58
*/
59

    
60
##|+PRIV
61
##|*IDENT=page-package-edit
62
##|*NAME=Package: Edit
63
##|*DESCR=Allow access to the 'Package: Edit' page.
64
##|*MATCH=pkg_edit.php*
65
##|-PRIV
66

    
67
ini_set('max_execution_time', '0');
68

    
69
require("guiconfig.inc");
70
require_once("functions.inc");
71
require_once("filter.inc");
72
require_once("shaper.inc");
73
require_once("pkg-utils.inc");
74

    
75
/* dummy stubs needed by some code that was MFC'd */
76
function pfSenseHeader($location) {
77
	header("Location: " . $location);
78
}
79

    
80
$xml = htmlspecialchars($_GET['xml']);
81
if ($_POST['xml']) {
82
	$xml = htmlspecialchars($_POST['xml']);
83
}
84

    
85
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
86

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

    
96
if ($pkg['include_file'] != "") {
97
	require_once($pkg['include_file']);
98
}
99

    
100
if (!isset($pkg['adddeleteeditpagefields'])) {
101
	$only_edit = true;
102
} else {
103
	$only_edit = false;
104
}
105

    
106
$id = $_GET['id'];
107
if (isset($_POST['id'])) {
108
	$id = htmlspecialchars($_POST['id']);
109
}
110

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

    
117
if (!is_numeric($id)) {
118
	header("Location: /");
119
	exit;
120
}
121

    
122
if ($pkg['custom_php_global_functions'] != "") {
123
	eval($pkg['custom_php_global_functions']);
124
}
125

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

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

    
137
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
138

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

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

    
147
if ($_POST) {
148
	$rows = 0;
149

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

    
165
	if ($pkg['custom_php_validation_command']) {
166
		eval($pkg['custom_php_validation_command']);
167
	}
168

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

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

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

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

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

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

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

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

    
260
			parse_package_templates();
261

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

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

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

    
288

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

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

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

    
308
	switch ($type) {
309
		case "input":
310
			$group->add(new Form_Input(
311
				$fieldname . $trc,
312
				null,
313
				'text',
314
				$value
315
			))->setHelp($description);
316

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

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

    
343
			break;
344
		case "select":
345
			$options = array();
346
			foreach ($rowhelper['options']['option'] as $rowopt) {
347
				$options[$rowopt['value']] = $rowopt['name'];
348
			}
349

    
350
			$group->add(new Form_Select(
351
				$fieldname . $trc,
352
				null,
353
				$value,
354
				$options
355
			))->setHelp($description);
356

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

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

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

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

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

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

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

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

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

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

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

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

    
428
			break;
429
	}
430
}
431

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

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

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

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

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

    
518
//breadcrumb
519
if ($pkg['title'] != "") {
520
	if(!$only_edit) {
521
		$pgtitle = array($pkg['title'], gettext("Edit"));
522
	} else {
523
		$pgtitle = array($pkg['title']);
524
	}
525
} else {
526
	$pgtitle = array(gettext("Package Editor"));
527
}
528

    
529

    
530
// Create any required tabs
531
if ($pkg['tabs'] != "") {
532
	$tab_array = array();
533
	foreach ($pkg['tabs']['tab'] as $tab) {
534
		if ($tab['tab_level']) {
535
			$tab_level = $tab['tab_level'];
536
		} else {
537
			$tab_level = 1;
538
		}
539

    
540
		if (isset($tab['active'])) {
541
			$active = true;
542
			$pgtitle[] = $tab['text'] ; 
543
		} else {
544
			$active = false;
545
		}
546

    
547
		if (isset($tab['no_drop_down'])) {
548
			$no_drop_down = true;
549
		}
550

    
551
		$urltmp = "";
552
		if ($tab['url'] != "") {
553
			$urltmp = $tab['url'];
554
		}
555

    
556
		if ($tab['xml'] != "") {
557
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
558
		}
559

    
560
		$addresswithport = getenv("HTTP_HOST");
561
		$colonpos = strpos($addresswithport, ":");
562

    
563
		if ($colonpos !== False) {
564
			//my url is actually just the IP address of the pfsense box
565
			$myurl = substr($addresswithport, 0, $colonpos);
566
		} else {
567
			$myurl = $addresswithport;
568
		}
569

    
570
		// eval url so that above $myurl item can be processed if need be.
571
		$url = str_replace('$myurl', $myurl, $urltmp);
572

    
573
		$tab_array[$tab_level][] = array(
574
			$tab['text'],
575
			$active,
576
			$url
577
		);
578
	}
579

    
580
	ksort($tab_array);
581
}
582

    
583
if ($pkg['custom_php_after_head_command']) {
584
	$closehead = false;
585
	include("head.inc");
586
	eval($pkg['custom_php_after_head_command']);
587
} else {
588
	include("head.inc");
589
}
590
if(isset($tab_array)) {
591
	foreach ($tab_array as $tabid => $tab) {
592
		display_top_tabs($tab); //, $no_drop_down, $tabid);
593
	}
594
}
595

    
596
// Start of page display
597
if ($input_errors)
598
	print_input_errors($input_errors);
599

    
600
if ($savemsg)
601
	print_info_box($savemsg, 'success');
602

    
603
$cols = 0;
604
$savevalue = gettext("Save");
605
if ($pkg['savetext'] != "") {
606
	$savevalue = $pkg['savetext'];
607
}
608

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

    
611
$form = new Form(new Form_Button(
612
	'submit',
613
	$savevalue
614
));
615

    
616
$form->addGlobal(new Form_Input(
617
	'xml',
618
	null,
619
	'hidden',
620
	$xml
621
));
622

    
623
/* If a package's XML has <advanced_options/> configured, then setup
624
 * the section for the fields that have <advancedfield/> set.
625
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
626
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
627
 */
628

    
629
if ($pkg['advanced_options'] == "enabled") {
630
	$advfield_count = 0;
631
	$advanced = new Form_Section(gettext("Advanced features"));
632
	$advanced->addClass('advancedoptions');
633
}
634

    
635
$js_array = array();
636

    
637
// Now loop through all of the fields defined in the XML
638
foreach ($pkg['fields']['field'] as $pkga) {
639

    
640
	if ($pkga['type'] == "sorting") {
641
		continue;
642
	}
643

    
644
	// Generate a new section
645
	if ($pkga['type'] == "listtopic") {
646
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
647
			$advanced->addInput(new Form_StaticText(
648
				strip_tags($pkga['name']),
649
				null
650
			));
651

    
652
			$advfield_count++;
653
		}  else {
654
			if(isset($section))
655
				$form->add($section);
656

    
657
			$section = new Form_Section(strip_tags($pkga['name']));
658
		}
659

    
660
		continue;
661
	}
662

    
663
	// 'begin' starts a form group. ('end' ends it)
664
	if ($pkga['combinefields'] == "begin") {
665
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
666
		$grouping = true;
667
	}
668

    
669
	$size = "";
670
	$colspan="";
671

    
672
	// if user is editing a record, load in the data.
673
	$fieldname = $pkga['fieldname'];
674
	unset($value);
675
	if ($get_from_post) {
676
		$value = $_POST[$fieldname];
677
		if (is_array($value)) {
678
			$value = implode(',', $value);
679
		}
680
	} else {
681
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
682
			$value = $a_pkg[$id][$fieldname];
683
		} else {
684
			if (isset($pkga['default_value'])) {
685
				$value = $pkga['default_value'];
686
			}
687
		}
688
	}
689

    
690
	// If we get here but have no $section, the package config file probably had no listtopic field
691
	// We can create a section with a generic name to fix that
692
	if(!$section)
693
		$section = new Form_Section(gettext('General options'));
694

    
695
	switch ($pkga['type']) {
696
		// Create an input element. The format is slightly different depending on whether we are composing a group,
697
		// section, or advanced section. This is true for every element type
698
		case "input":
699
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
700
				$value = base64_decode($value);
701
			}
702

    
703
			if ($grouping) {
704
				$group->add(new Form_Input(
705
					$pkga['fieldname'],
706
					$pkga['fielddescr'],
707
					'text',
708
					$value
709
				))->setHelp($pkga['description']);
710
			} else {
711
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
712
					$advanced->addInput(new Form_Input(
713
						$pkga['fieldname'],
714
						$pkga['fielddescr'],
715
						'text',
716
						$value
717
					))->setHelp($pkga['description']);
718
				} else {
719
					$section->addInput(new Form_Input(
720
						$pkga['fieldname'],
721
						$pkga['fielddescr'],
722
						'text',
723
						$value
724
					))->setHelp($pkga['description']);
725
				}
726
			}
727

    
728
			break;
729

    
730
		case "password":
731
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
732
				$value = base64_decode($value);
733
			}
734

    
735
			// Create a password element
736
			if ($grouping) {
737
				$group->add(new Form_Input(
738
					$pkga['fieldname'],
739
					$pkga['fielddescr'],
740
					'password',
741
					$value
742
				))->setHelp($pkga['description']);
743
			} else {
744
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
745
					$advanced->addInput(new Form_Input(
746
						$pkga['fieldname'],
747
						$pkga['fielddescr'],
748
						'password',
749
						$value
750
					))->setHelp($pkga['description']);
751
				} else {
752
					$section->addInput(new Form_Input(
753
						$pkga['fieldname'],
754
						$pkga['fielddescr'],
755
						'password',
756
						$value
757
					))->setHelp($pkga['description']);
758
				}
759
			}
760

    
761
			break;
762

    
763
		case "info":
764
			// If the info contains a table we should detect and Bootstrap it
765

    
766
			if (strpos($pkga['description'], '<table') !== FALSE)
767
				$info = bootstrapTable($pkga['description']);
768
			else
769
				$info = $pkga['description'];
770

    
771
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
772
				$advanced->addInput(new Form_StaticText(
773
					strip_tags($pkga['fielddescr']),
774
					$info
775
				));
776
			} else {
777
				$section->addInput(new Form_StaticText(
778
					strip_tags($pkga['fielddescr']),
779
					$info
780
				));
781
			}
782

    
783
			break;
784

    
785
		case "select":
786
			// Create a select element
787
			$optionlist = array();
788
			$selectedlist = array();
789

    
790
			$fieldname = $pkga['fieldname'];
791

    
792
			if (isset($pkga['multiple'])) {
793
				$multiple = 'multiple';
794
				$items = explode(',', $value);
795
				$fieldname .= "[]";
796
			} else {
797
				$multiple = '';
798
				$items = array($value);
799
			}
800

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

    
803
			foreach ($pkga['options']['option'] as $opt) {
804
				$optionlist[$opt['value']] = $opt['name'];
805

    
806
				if (in_array($opt['value'], $items)) {
807
					array_push($selectedlist, $opt['value']);
808
				}
809
			}
810

    
811
			if (isset($pkga['advancedfield']) && isset($advfield_count))
812
				$function = $grouping ? $advanced->add:$advanced->addInput;
813
			else
814
				$function = ($grouping) ? $section->add:$section->addInput;
815

    
816
			if($grouping) {
817
					$group->add(new Form_Select(
818
						$pkga['fieldname'],
819
						strip_tags($pkga['fielddescr']),
820
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
821
						$optionlist,
822
						isset($pkga['multiple'])
823
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
824
			} else {
825
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
826
					$advanced->addInput(new Form_Select(
827
						$pkga['fieldname'],
828
						$pkga['fielddescr'],
829
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
830
						$optionlist,
831
						isset($pkga['multiple'])
832
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
833
				} else {
834
					$section->addInput(new Form_Select(
835
						$pkga['fieldname'],
836
						strip_tags($pkga['fielddescr']),
837
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
838
						$optionlist,
839
						isset($pkga['multiple'])
840
					))->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
841
				}
842
			}
843

    
844
			break;
845

    
846
		case "select_source":
847

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

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

    
857
			$source_url = $pkga['source'];
858
			eval("\$pkg_source_txt = &$source_url;");
859

    
860
			#check if show disable option is present on xml
861
			if (isset($pkga['show_disable_value'])) {
862
				array_push($pkg_source_txt,
863
					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']));
864
			}
865

    
866
			$srcoptions = array();
867
			$srcselected = array();
868

    
869
			foreach ($pkg_source_txt as $opt) {
870
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
871
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
872
				$srcoptions[$source_value] = $source_name;
873

    
874
				if(in_array($source_value, $items))
875
					array_push($srcselected, $source_value);
876
			}
877

    
878
			if($grouping) {
879
				$group->add(new Form_Select(
880
					$pkga['fieldname'],
881
					$pkga['name'],
882
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
883
					$srcoptions,
884
					isset($pkga['multiple'])
885
				))->setOnchange($onchange);
886
			} else {
887
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
888
					$advanced->addInput(new Form_Select(
889
						$pkga['fieldname'],
890
						$pkga['name'],
891
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
892
						$srcoptions,
893
						isset($pkga['multiple'])
894
					))->setOnchange($onchange);
895
				} else {
896
					$section->addInput(new Form_Select(
897
						$pkga['fieldname'],
898
						$pkga['name'],
899
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
900
						$srcoptions,
901
						isset($pkga['multiple'])
902
					))->setOnchange($onchange);
903
				}
904
			}
905

    
906
			break;
907

    
908
		case "vpn_selection" :
909
			$vpnlist = array();
910

    
911
			foreach ($config['ipsec']['phase1'] as $vpn) {
912
				$vpnlist[$vpn['descr']] = $vpn['descr'];
913

    
914
			}
915

    
916
			if($grouping) {
917
				$group->add(new Form_Select(
918
					$pkga['fieldname'],
919
					null,
920
					false,
921
					$vpnlist
922
				))->setHelp(fixup_string($pkga['description']));
923
			} else {
924
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
925
					$advanced->addInput(new Form_Select(
926
						$pkga['fieldname'],
927
						null,
928
						false,
929
						$vpnlist
930
					))->setHelp(fixup_string($pkga['description']));
931
				} else {
932
					$section->addInput(new Form_Select(
933
						$pkga['fieldname'],
934
						null,
935
						false,
936
						$vpnlist
937
					))->setHelp(fixup_string($pkga['description']));
938
				}
939
			}
940

    
941
			break;
942

    
943
		// Create a checkbox element
944
		case "checkbox":
945
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
946
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
947
				$onclick = 'javascript:enablechange();';
948
			else
949
				$onclick = '';
950

    
951
			if($grouping) {
952
				$group->add(new Form_Checkbox(
953
					$pkga['fieldname'],
954
					$pkga['fielddescr'],
955
					fixup_string($pkga['description']),
956
					($value == "on"),
957
					'on'
958
				))->setOnclick($onclick)
959
				  ->setOnchange($onchange)
960
				  ->setHelp($pkga['sethelp']);
961
			} else {
962
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
963
					$advanced->addInput(new Form_Checkbox(
964
						$pkga['fieldname'],
965
						$pkga['fielddescr'],
966
						fixup_string($pkga['description']),
967
						($value == "on"),
968
						'on'
969
					))->setOnclick($onclick)
970
					  ->setOnchange($onchange)
971
					  ->setHelp($pkga['sethelp']);
972
				} else {
973
					$section->addInput(new Form_Checkbox(
974
						$pkga['fieldname'],
975
						$pkga['fielddescr'],
976
						fixup_string($pkga['description']),
977
						($value == "on"),
978
						'on'
979
					))->setOnclick($onclick)
980
					  ->setOnchange($onchange)
981
					  ->setHelp($pkga['sethelp']);
982
				}
983
			}
984

    
985
			break;
986

    
987
		// Create a textarea element
988
		case "textarea":
989
			if ($pkga['rows']) {
990
				$rows = " rows='{$pkga['rows']}' ";
991
			}
992
			if ($pkga['cols']) {
993
				$cols = " cols='{$pkga['cols']}' ";
994
			}
995
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
996
				$value = base64_decode($value);
997
			}
998

    
999
			$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');
1000

    
1001
			if ($grouping) {
1002
				$group->add(new Form_Textarea(
1003
					$pkga['fieldname'],
1004
					$pkga['fielddescr'],
1005
					$value
1006
				))->setHelp(fixup_string($pkga['description']));
1007
			} else {
1008
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1009
					$advanced->addInput(new Form_Textarea(
1010
						$pkga['fieldname'],
1011
						$pkga['fielddescr'],
1012
						$value
1013
					))->setHelp(fixup_string($pkga['description']));
1014
				} else {
1015
					$section->addInput(new Form_Textarea(
1016
						$pkga['fieldname'],
1017
						$pkga['fielddescr'],
1018
						$value
1019
					))->setHelp(fixup_string($pkga['description']));
1020
				}
1021
			}
1022

    
1023
			break;
1024

    
1025
		case "aliases":
1026

    
1027
			// Use xml tag <typealiases> to filter type aliases
1028
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1029
			$fieldname = $pkga['fieldname'];
1030
			$a_aliases = &$config['aliases']['alias'];
1031
			$addrisfirst = 0;
1032
			$aliasesaddr = "";
1033
			$value = "value='{$value}'";
1034

    
1035
			if (isset($a_aliases)) {
1036
				if (!empty($pkga['typealiases'])) {
1037
					foreach ($a_aliases as $alias) {
1038
						if ($alias['type'] == $pkga['typealiases']) {
1039
							if ($addrisfirst == 1) {
1040
								$aliasesaddr .= ",";
1041
							}
1042
							$aliasesaddr .= "'" . $alias['name'] . "'";
1043
							$addrisfirst = 1;
1044
						}
1045
					}
1046
				} else {
1047
					foreach ($a_aliases as $alias) {
1048
						if ($addrisfirst == 1) {
1049
							$aliasesaddr .= ",";
1050
						}
1051
						$aliasesaddr .= "'" . $alias['name'] . "'";
1052
						$addrisfirst = 1;
1053
					}
1054
				}
1055
			}
1056

    
1057
			if(grouping) {
1058
				$group->add(new Form_Input(
1059
					$pkga['fieldname'],
1060
					$pkga['fielddescr'],
1061
					'text',
1062
					$value
1063
				))->setHelp($pkga['description']);
1064
			} else {
1065
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1066
					$advanced->addInput(new Form_Input(
1067
						$pkga['fieldname'],
1068
						$pkga['fielddescr'],
1069
						'text',
1070
						$value
1071
					))->setHelp($pkga['description']);
1072
				} else {
1073
					$section->addInput(new Form_Input(
1074
						$pkga['fieldname'],
1075
						$pkga['fielddescr'],
1076
						'text',
1077
						$value
1078
					))->setHelp($pkga['description']);
1079
				}
1080
			}
1081

    
1082
			$script = "<script type='text/javascript'>\n";
1083
			$script .= "//<![CDATA[\n";
1084
			$script .= "events.push(function(){\n";
1085
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1086
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1087
			$script .= "		source: aliasarray\n";
1088
			$script .= "	})\n";
1089
			$script .= "});\n";
1090
			$script .= "//]]>\n";
1091
			$script .= "</script>";
1092

    
1093
			echo $script;
1094

    
1095
			break;
1096

    
1097
		case "interfaces_selection":
1098
			$ips = array();
1099
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1100
			if (is_array($config['interfaces'])) {
1101
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1102
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1103
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1104
						if (isset($pkga['showips'])) {
1105
							$iface_description .= " address";
1106
						}
1107
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1108
					}
1109
				}
1110
			}
1111

    
1112
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1113
				foreach ($config['virtualip']['vip'] as $vip) {
1114
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1115
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1116
					}
1117
					switch ($vip['mode']) {
1118
						case "ipalias":
1119
						case "carp":
1120
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1121
							break;
1122
						case "proxyarp":
1123
							if ($vip['type'] == "network") {
1124
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1125
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1126
								$len = $end - $start;
1127
								for ($i = 0; $i <= $len; $i++) {
1128
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1129
								}
1130
							} else {
1131
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1132
							}
1133
							break;
1134
					}
1135
				}
1136
			}
1137

    
1138
			sort($ips);
1139
			if (isset($pkga['showlistenall'])) {
1140
				array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
1141
			}
1142

    
1143
			if (!preg_match("/$interface_regex/", "loopback")) {
1144
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
1145
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1146
			}
1147

    
1148
			#show interfaces array on gui
1149
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1150
			$multiple = '';
1151
			$fieldname = $pkga['fieldname'];
1152
			if (isset($pkga['multiple'])) {
1153
				$fieldname .= '[]';
1154
				$multiple = 'multiple';
1155
			}
1156

    
1157
			$selectedlist = array();
1158
			$optionlist = array();
1159

    
1160
			if (is_array($value)) {
1161
				$values = $value;
1162
			} else {
1163
				$values = explode(',', $value);
1164
			}
1165

    
1166
			foreach ($ips as $iface) {
1167
				if (in_array($iface['ip'], $values)) {
1168
					array_push($selectedlist, $iface['ip']);
1169
				}
1170

    
1171
				$optionlist[$iface['ip']] = $iface['description'];
1172
			}
1173

    
1174
			if($grouping) {
1175
				$group->add(new Form_Select(
1176
					$pkga['fieldname'],
1177
					$pkga['fielddescr'],
1178
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1179
					$optionlist,
1180
					isset($pkga['multiple'])
1181
				))->setHelp($pkga['description']);
1182
			} else {
1183
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1184
					$advanced->addInput(new Form_Select(
1185
						$pkga['fieldname'],
1186
						$pkga['fielddescr'],
1187
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1188
						$optionlist,
1189
						isset($pkga['multiple'])
1190
					))->setHelp($pkga['description']);
1191
				} else {
1192
					$section->addInput(new Form_Select(
1193
						$pkga['fieldname'],
1194
						$pkga['fielddescr'],
1195
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1196
						$optionlist,
1197
						isset($pkga['multiple'])
1198
					))->setHelp($pkga['description']);
1199
				}
1200
			}
1201

    
1202
			break;
1203

    
1204
		// Create radio button
1205
		case "radio":
1206
			if($grouping) {
1207
				$group->add(new Form_Checkbox(
1208
					$pkga['fieldname'],
1209
					$pkga['fielddescr'],
1210
					fixup_string($pkga['description']),
1211
					($value == "on"),
1212
					'on'
1213
				))->displayAsRadio();
1214
			} else {
1215
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1216
					$advanced->addInput(new Form_Checkbox(
1217
						$pkga['fieldname'],
1218
						$pkga['fielddescr'],
1219
						fixup_string($pkga['description']),
1220
						($value == "on"),
1221
						'on'
1222
					))->displayAsRadio();
1223
				} else {
1224
					$section->addInput(new Form_Checkbox(
1225
						$pkga['fieldname'],
1226
						$pkga['fielddescr'],
1227
						fixup_string($pkga['description']),
1228
						($value == "on"),
1229
						'on'
1230
					))->displayAsRadio();
1231
				}
1232
			}
1233

    
1234
			break;
1235

    
1236
		// Create form button
1237
		case "button":
1238
			$newbtn = new Form_Button(
1239
				$pkga['fieldname'],
1240
				$pkga['fieldname']
1241
			);
1242

    
1243
			if(grouping) {
1244
				$group->add(new Form_StaticText(
1245
					null,
1246
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1247
				));
1248
			} else {
1249
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1250
				$advanced->addInput(new Form_StaticText(
1251
					null,
1252
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1253
				));
1254
				} else {
1255
				$section->addInput(new Form_StaticText(
1256
					null,
1257
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1258
				));
1259
				}
1260
			}
1261

    
1262
			break;
1263

    
1264
		case "schedule_selection":
1265

    
1266
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1267
			$schedules = array();
1268
			$schedules[] = "none";
1269
			if (is_array($config['schedules']['schedule'])) {
1270
				foreach ($config['schedules']['schedule'] as $schedule) {
1271
					if ($schedule['name'] != "") {
1272
						$schedules[] = $schedule['name'];
1273
					}
1274
				}
1275
			}
1276

    
1277
			foreach ($schedules as $schedule) {
1278
				if ($schedule == "none") {
1279
					$schedlist[""] = $schedule;
1280
				} else {
1281
					$schedlist[$schedule] = $schedule;
1282
				}
1283
			}
1284

    
1285
			if($grouping) {
1286
				$group->add(new Form_Select(
1287
					$pkga['fieldname'],
1288
					$pkga['fielddescr'],
1289
					$value,
1290
					$schedlist
1291
				))->setHelp(fixup_string($pkga['description']));
1292
			} else {
1293
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1294
					$advanced->addInput(new Form_Select(
1295
						$pkga['fieldname'],
1296
						$pkga['fielddescr'],
1297
						$value,
1298
						$schedlist
1299
					))->setHelp(fixup_string($pkga['description']));
1300
				} else {
1301
					$section->addInput(new Form_Select(
1302
						$pkga['fieldname'],
1303
						$pkga['fielddescr'],
1304
						$value,
1305
						$schedlist
1306
					))->setHelp(fixup_string($pkga['description']));
1307
				}
1308
			}
1309

    
1310
			break;
1311

    
1312
		case "rowhelper":
1313

    
1314
			$rowhelpername="row";
1315

    
1316
				$rowcounter = 0;
1317
				$trc = 0;
1318

    
1319
				//Use assigned $a_pkg or create an empty array to enter loop
1320
				if (isset($a_pkg[$id][$rowhelpername])) {
1321
					$saved_rows=$a_pkg[$id][$rowhelpername];
1322
				} else {
1323
					$saved_rows[] = array();
1324
				}
1325

    
1326
				$numrows = count($saved_rows) - 1;
1327

    
1328
				foreach ($saved_rows as $row) {
1329
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1330
					$group->addClass('repeatable');
1331

    
1332
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1333
						unset($value);
1334
						if ($rowhelper['value'] != "") {
1335
							$value = $rowhelper['value'];
1336
						}
1337
						$fieldname = $rowhelper['fieldname'];
1338
						$fielddescr = $rowhelper['fielddescr'];
1339

    
1340
						// if user is editing a record, load in the data.
1341
						if (isset($id) && $a_pkg[$id]) {
1342
							$value = $row[$fieldname];
1343
						}
1344

    
1345
						$type = $rowhelper['type'];
1346
						if ($type == "input" || $type == "password" || $type == "textarea" ) {
1347
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1348
								$value = base64_decode($value);
1349
							}
1350
						}
1351
						$fieldname = $rowhelper['fieldname'];
1352

    
1353
						if ($rowhelper['size']) {
1354
							$size = $rowhelper['size'];
1355
						} else if ($pkga['size']) {
1356
							$size = $pkga['size'];
1357
						} else {
1358
							$size = "8";
1359
						}
1360

    
1361
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, ($numrows == $rowcounter) ? $fielddescr:null);
1362

    
1363
						$text = "";
1364
						$trc++;
1365
					}
1366

    
1367
					// Delete row button
1368
					$group->add(new Form_Button(
1369
						'deleterow' . $rowcounter,
1370
						'Delete'
1371
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1372

    
1373
					$rowcounter++;
1374
					$section->add($group);
1375
				}
1376

    
1377
			// Add row button
1378
			$section->addInput(new Form_Button(
1379
				'addrow',
1380
				'Add'
1381
			))->removeClass('btn-primary')->addClass('btn-success');
1382

    
1383
			break;
1384

    
1385
	}
1386

    
1387
		if ($pkga['combinefields'] == "end") {
1388
			$group->add(new Form_StaticText(
1389
				null,
1390
				null
1391
			));
1392

    
1393
			if($advanced)
1394
				$advanced->add($group);
1395
			else
1396
				$section->add($group);
1397

    
1398
			$grouping = false;
1399
		}
1400

    
1401
	#increment counter
1402
	$i++;
1403
} // e-o-foreach field described in the XML
1404

    
1405
$form->add($section);
1406

    
1407
$form->addGlobal(new Form_Input(
1408
	'id',
1409
	null,
1410
	'hidden',
1411
	$id
1412
));
1413

    
1414
// If we created an advanced section, add it (and a button) to the form here
1415
if(!empty($advanced)) {
1416
	$form->addGlobal(new Form_Button(
1417
		'showadv',
1418
		'Show advanced options'
1419
	))->removeClass('btn-primary')->addClass('btn-default');
1420

    
1421
	$form->add($advanced);
1422
}
1423

    
1424
print($form);
1425

    
1426
if ($pkg['note'] != "") {
1427
	print_info_box($pkg['note']);
1428

    
1429
if ($pkg['custom_php_after_form_command'])
1430
	eval($pkg['custom_php_after_form_command']);
1431
}
1432

    
1433
if ($pkg['fields']['field'] != "") { ?>
1434
<script type="text/javascript">
1435
//<![CDATA[
1436
	events.push(function(){
1437

    
1438
	// Hide the advanced section
1439
	var advanced_visible = false;
1440

    
1441
	// Hide on page load
1442
	$('.advancedoptions').hide();
1443

    
1444
	// Suppress "Delete row" button if there are fewer than two rows
1445
	checkLastRow();
1446

    
1447
	// Show advanced section if you click the showadv button
1448
	$('#showadv').prop('type', 'button');
1449

    
1450
	$("#showadv").click(function() {
1451
		advanced_visible = !advanced_visible;
1452

    
1453
		if(advanced_visible) {
1454
			$('.advancedoptions').show();
1455
			$("#showadv").prop('value', 'Hide advanced Options');
1456
		}
1457
		else {
1458
			$('.advancedoptions').hide();
1459
			$("#showadv").prop('value', 'Show advanced Options');
1460
		}
1461
	});
1462

    
1463
	// Call enablechange function
1464
	enablechange();
1465
});
1466

    
1467
	function enablechange() {
1468
<?php
1469
	foreach ($pkg['fields']['field'] as $field) {
1470
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1471
			echo "\tif (jQuery('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1472

    
1473
			if (isset($field['enablefields'])) {
1474
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1475
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1476
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1477
					echo "\t\t}\n";
1478
				}
1479
			}
1480

    
1481
			if (isset($field['checkenablefields'])) {
1482
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1483
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1484
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1485
					echo "\t\t}\n";
1486
				}
1487
			}
1488

    
1489
			echo "\t}\n\telse {\n";
1490

    
1491
			if (isset($field['enablefields'])) {
1492
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1493
					echo "\t\tif (jQuery('input[name=\"{$enablefield}\"]').length > 0) {\n";
1494
					echo "\t\t\tjQuery('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1495
					echo "\t\t}\n";
1496
				}
1497
			}
1498

    
1499
			if (isset($field['checkenablefields'])) {
1500
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1501
					echo "\t\tif (jQuery('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1502
					echo "\t\t\tjQuery('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1503
					echo "\t\t}\n";
1504
				}
1505
			}
1506

    
1507
			echo "\t}\n";
1508
		}
1509
	}
1510
	?>
1511
	}
1512
//]]>
1513
</script>
1514

    
1515
<?php
1516
}
1517

    
1518
include("foot.inc");
(112-112/228)