Project

General

Profile

Download (41.4 KB) Statistics
| Branch: | Tag: | Revision:
1 d47013e1 Scott Ullrich
<?php
2
/*
3 f8ac4407 Phil Davis
	pkg_edit.php
4 919d91f9 Phil Davis
*/
5
/* ====================================================================
6 23f6cdd7 Stephen Beaver
 *	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 d47013e1 Scott Ullrich
56 6b07c15a Matthew Grooms
##|+PRIV
57
##|*IDENT=page-package-edit
58 5230f468 jim-p
##|*NAME=Package: Edit
59 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'Package: Edit' page.
60
##|*MATCH=pkg_edit.php*
61
##|-PRIV
62
63 0089af7c Scott Ullrich
ini_set('max_execution_time', '0');
64
65 7a927e67 Scott Ullrich
require("guiconfig.inc");
66
require_once("functions.inc");
67
require_once("filter.inc");
68
require_once("shaper.inc");
69 f8e335a3 Scott Ullrich
require_once("pkg-utils.inc");
70 d47013e1 Scott Ullrich
71 14db714e Scott Ullrich
/* dummy stubs needed by some code that was MFC'd */
72 f8ac4407 Phil Davis
function pfSenseHeader($location) {
73
	header("Location: " . $location);
74
}
75 14db714e Scott Ullrich
76 d8c1a6c5 Scott Ullrich
$xml = htmlspecialchars($_GET['xml']);
77 f8ac4407 Phil Davis
if ($_POST['xml']) {
78
	$xml = htmlspecialchars($_POST['xml']);
79
}
80 d47013e1 Scott Ullrich
81 bef9f697 Renato Botelho
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
82 69eb2e29 Renato Botelho
83 f5dbca0d Stephen Beaver
if ($xml == "" || $xml_fullpath === false || substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
84 84320769 k-paulius
	$pgtitle = array(gettext("Package"), gettext("Editor"));
85 f5dbca0d Stephen Beaver
	include("head.inc");
86 8545adde k-paulius
	print_info_box(gettext("No valid package defined."), 'danger', false);
87 f5dbca0d Stephen Beaver
	include("foot.inc");
88 f8ac4407 Phil Davis
	die;
89 d47013e1 Scott Ullrich
} else {
90 f8ac4407 Phil Davis
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
91 d47013e1 Scott Ullrich
}
92 7c172009 Scott Ullrich
93 23f6cdd7 Stephen Beaver
if ($pkg['include_file'] != "") {
94 7c172009 Scott Ullrich
	require_once($pkg['include_file']);
95
}
96
97 f8ac4407 Phil Davis
if (!isset($pkg['adddeleteeditpagefields'])) {
98 b91540da Scott Ullrich
	$only_edit = true;
99 f8ac4407 Phil Davis
} else {
100 b91540da Scott Ullrich
	$only_edit = false;
101 f8ac4407 Phil Davis
}
102 b91540da Scott Ullrich
103 da7bf505 Scott Ullrich
$id = $_GET['id'];
104 f8ac4407 Phil Davis
if (isset($_POST['id'])) {
105 d8c1a6c5 Scott Ullrich
	$id = htmlspecialchars($_POST['id']);
106 f8ac4407 Phil Davis
}
107 98bcf1f8 Scott Ullrich
108 23f6cdd7 Stephen Beaver
// Not posting?	 Then user is editing a record. There must be a valid id
109 916b74c6 Scott Ullrich
// when editing a record.
110 f8ac4407 Phil Davis
if (!$id && !$_POST) {
111 916b74c6 Scott Ullrich
	$id = "0";
112 f8ac4407 Phil Davis
}
113 5a61331a jim-p
114 f8ac4407 Phil Davis
if (!is_numeric($id)) {
115 6f3d2063 Renato Botelho
	header("Location: /");
116 5a61331a jim-p
	exit;
117
}
118
119 23f6cdd7 Stephen Beaver
if ($pkg['custom_php_global_functions'] != "") {
120 f8ac4407 Phil Davis
	eval($pkg['custom_php_global_functions']);
121
}
122 529ffadb Bill Marquette
123 31d27c6c Scott Ullrich
// grab the installedpackages->package_name section.
124 f8ac4407 Phil Davis
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
125 7db8ff99 Colin Smith
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
126 f8ac4407 Phil Davis
}
127 da7bf505 Scott Ullrich
128 a2a7f74d jim-p
// If the first entry in the array is an empty <config/> tag, kill it.
129 0f649c97 Phil Davis
if ($config['installedpackages'] &&
130
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
131
    ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == "")) {
132 a2a7f74d jim-p
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
133 f8ac4407 Phil Davis
}
134 a2a7f74d jim-p
135 7db8ff99 Colin Smith
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
136 da7bf505 Scott Ullrich
137 23f6cdd7 Stephen Beaver
if ($_GET['savemsg'] != "") {
138 d8c1a6c5 Scott Ullrich
	$savemsg = htmlspecialchars($_GET['savemsg']);
139 f8ac4407 Phil Davis
}
140 f9a91638 Scott Ullrich
141 23f6cdd7 Stephen Beaver
if ($pkg['custom_php_command_before_form'] != "") {
142 194b4e0a Colin Smith
	eval($pkg['custom_php_command_before_form']);
143 f8ac4407 Phil Davis
}
144 f9a91638 Scott Ullrich
145 6483da5d Scott Ullrich
if ($_POST) {
146 2c51f293 jim-p
	$rows = 0;
147
148
	$input_errors = array();
149
	$reqfields = array();
150
	$reqfieldsn = array();
151
	foreach ($pkg['fields']['field'] as $field) {
152 86f28390 doktornotor
		if (isset($field['required'])) {
153 f8ac4407 Phil Davis
			if ($field['fieldname']) {
154 2c51f293 jim-p
				$reqfields[] = $field['fieldname'];
155 f8ac4407 Phil Davis
			}
156
			if ($field['fielddescr']) {
157 2c51f293 jim-p
				$reqfieldsn[] = $field['fielddescr'];
158 f8ac4407 Phil Davis
			}
159 2c51f293 jim-p
		}
160
	}
161 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
162 2c51f293 jim-p
163 f8ac4407 Phil Davis
	if ($pkg['custom_php_validation_command']) {
164 2c51f293 jim-p
		eval($pkg['custom_php_validation_command']);
165 f8ac4407 Phil Davis
	}
166 2c51f293 jim-p
167 f8ac4407 Phil Davis
	if ($_POST['act'] == "del") {
168
		if ($pkg['custom_delete_php_command']) {
169 23f6cdd7 Stephen Beaver
			if ($pkg['custom_php_command_before_form'] != "") {
170 f8ac4407 Phil Davis
				eval($pkg['custom_php_command_before_form']);
171
			}
172
			eval($pkg['custom_delete_php_command']);
173 b3235baa Scott Ullrich
		}
174 7c172009 Scott Ullrich
		write_config($pkg['delete_string']);
175 facd08f9 Scott Ullrich
		// resync the configuration file code if defined.
176 23f6cdd7 Stephen Beaver
		if ($pkg['custom_php_resync_config_command'] != "") {
177
			if ($pkg['custom_php_command_before_form'] != "") {
178 86f3fc00 Scott Ullrich
				eval($pkg['custom_php_command_before_form']);
179 f8ac4407 Phil Davis
			}
180 86f3fc00 Scott Ullrich
			eval($pkg['custom_php_resync_config_command']);
181 facd08f9 Scott Ullrich
		}
182 b3235baa Scott Ullrich
	} else {
183 f8ac4407 Phil Davis
		if (!$input_errors && $pkg['custom_add_php_command']) {
184 23f6cdd7 Stephen Beaver
			if ($pkg['donotsave'] != "" or $pkg['preoutput'] != "") {
185
				include("head.inc");
186 3eaeb703 Scott Ullrich
			}
187 23f6cdd7 Stephen Beaver
188 f8ac4407 Phil Davis
			if ($pkg['preoutput']) {
189
				echo "<pre>";
190
			}
191 3eaeb703 Scott Ullrich
			eval($pkg['custom_add_php_command']);
192 f8ac4407 Phil Davis
			if ($pkg['preoutput']) {
193
				echo "</pre>";
194
			}
195 b3235baa Scott Ullrich
		}
196 6483da5d Scott Ullrich
	}
197 e3c4b6b7 Scott Ullrich
198 eec70f21 Scott Ullrich
	// donotsave is enabled.  lets simply exit.
199 f8ac4407 Phil Davis
	if (empty($pkg['donotsave'])) {
200 2c51f293 jim-p
201 f8ac4407 Phil Davis
		// store values in xml configuration file.
202 2c51f293 jim-p
		if (!$input_errors) {
203
			$pkgarr = array();
204
			foreach ($pkg['fields']['field'] as $fields) {
205 f8ac4407 Phil Davis
				switch ($fields['type']) {
206 2c51f293 jim-p
					case "rowhelper":
207
						// save rowhelper items.
208
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
209
						$rowhelpername="row";
210 f8ac4407 Phil Davis
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
211
							foreach ($_POST as $key => $value) {
212 29fc0334 k-paulius
								$matches = array();
213 6c07db48 Phil Davis
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
214
									$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']] = $value;
215
								}
216 2c51f293 jim-p
							}
217 f8ac4407 Phil Davis
						}
218 2c51f293 jim-p
						break;
219
					default:
220 6c07db48 Phil Davis
						$fieldname = $fields['fieldname'];
221 2c51f293 jim-p
						if ($fieldname == "interface_array") {
222
							$fieldvalue = $_POST[$fieldname];
223
						} elseif (is_array($_POST[$fieldname])) {
224
							$fieldvalue = implode(',', $_POST[$fieldname]);
225
						} else {
226
							$fieldvalue = trim($_POST[$fieldname]);
227 f8ac4407 Phil Davis
							if ($fields['encoding'] == 'base64') {
228 2c51f293 jim-p
								$fieldvalue = base64_encode($fieldvalue);
229 f8ac4407 Phil Davis
							}
230 eec70f21 Scott Ullrich
						}
231 f8ac4407 Phil Davis
						if ($fieldname) {
232 2c51f293 jim-p
							$pkgarr[$fieldname] = $fieldvalue;
233 f8ac4407 Phil Davis
						}
234 eec70f21 Scott Ullrich
					}
235 2c51f293 jim-p
			}
236 0e730fee Scott Ullrich
237 f8ac4407 Phil Davis
			if (isset($id) && $a_pkg[$id]) {
238 2c51f293 jim-p
				$a_pkg[$id] = $pkgarr;
239 f8ac4407 Phil Davis
			} else {
240 2c51f293 jim-p
				$a_pkg[] = $pkgarr;
241 f8ac4407 Phil Davis
			}
242 0e730fee Scott Ullrich
243 2c51f293 jim-p
			write_config($pkg['addedit_string']);
244
			// late running code
245 23f6cdd7 Stephen Beaver
			if ($pkg['custom_add_php_command_late'] != "") {
246 f8ac4407 Phil Davis
				eval($pkg['custom_add_php_command_late']);
247 2c51f293 jim-p
			}
248 0e730fee Scott Ullrich
249 f8ac4407 Phil Davis
			if (isset($pkg['filter_rules_needed'])) {
250 2c51f293 jim-p
				filter_configure();
251 f8ac4407 Phil Davis
			}
252 a9b2e638 Ermal Lu?i
253 2c51f293 jim-p
			// resync the configuration file code if defined.
254 23f6cdd7 Stephen Beaver
			if ($pkg['custom_php_resync_config_command'] != "") {
255 f8ac4407 Phil Davis
				eval($pkg['custom_php_resync_config_command']);
256 2c51f293 jim-p
			}
257 facd08f9 Scott Ullrich
258 2c51f293 jim-p
			parse_package_templates();
259 dcbe6f52 Scott Ullrich
260 2c51f293 jim-p
			/* if start_command is defined, restart w/ this */
261 23f6cdd7 Stephen Beaver
			if ($pkg['start_command'] != "") {
262 f8ac4407 Phil Davis
				exec($pkg['start_command'] . ">/dev/null 2&>1");
263
			}
264 a485a6f5 Scott Ullrich
265 2c51f293 jim-p
			/* if restart_command is defined, restart w/ this */
266 23f6cdd7 Stephen Beaver
			if ($pkg['restart_command'] != "") {
267 f8ac4407 Phil Davis
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
268
			}
269
270 23f6cdd7 Stephen Beaver
			if ($pkg['aftersaveredirect'] != "") {
271 f8ac4407 Phil Davis
				pfSenseHeader($pkg['aftersaveredirect']);
272
			} elseif (!$pkg['adddeleteeditpagefields']) {
273 71e8cdd6 NewEraCracker
				pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
274 f8ac4407 Phil Davis
			} elseif (!$pkg['preoutput']) {
275
				pfSenseHeader("pkg.php?xml=" . $xml);
276 2c51f293 jim-p
			}
277
			exit;
278
		} else {
279
			$get_from_post = true;
280 dcbe6f52 Scott Ullrich
		}
281 2c51f293 jim-p
	} elseif (!$input_errors) {
282 0e730fee Scott Ullrich
		exit;
283 e11aa161 Warren Baker
	}
284 da7bf505 Scott Ullrich
}
285
286 7c172009 Scott Ullrich
287 eb3743d8 Stephen Beaver
// Turn an embedded table into a bootstrap class table. This is for backward compatibility.
288
// We remove any table attributes in the XML and replace them with Bootstrap table classes
289
function bootstrapTable($text) {
290
	$t = strpos($text, '<table') + strlen('<table');	// Find the <table tag
291
	$c = strpos($text, '>', $t);						// And its closing bracket
292
293
	// Substitute everything inbetween with our new classes
294 0f649c97 Phil Davis
	if ($t && $c && (($c - $t) < 200)) {
295 eb3743d8 Stephen Beaver
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
296
	}
297
}
298
299
/*
300
 * ROW helper function. Creates one element in the row from a PHP table by adding
301
 * the specified element to $group
302
 */
303 36cefd8f Stephen Beaver
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description, $ewidth = null) {
304 eb3743d8 Stephen Beaver
	global $text, $group;
305
306
	switch ($type) {
307
		case "input":
308 bd0d36bd Stephen Beaver
			$inpt = new Form_Input(
309 eb3743d8 Stephen Beaver
				$fieldname . $trc,
310
				null,
311
				'text',
312
				$value
313 bd0d36bd Stephen Beaver
			);
314
315
			$inpt->setHelp($description);
316
317 36cefd8f Stephen Beaver
			if ($ewidth) {
318
				$inpt->setWidth($ewidth);
319 bd0d36bd Stephen Beaver
			}
320 eb3743d8 Stephen Beaver
321 bd0d36bd Stephen Beaver
			$group->add($inpt);
322 eb3743d8 Stephen Beaver
			break;
323
		case "checkbox":
324
			$group->add(new Form_Checkbox(
325
				$fieldname . $trc,
326
				null,
327
				null,
328
				$value,
329
				'ON'
330
			))->setHelp($description);
331
332
			break;
333
		case "password":
334
			$group->add(new Form_Input(
335
				$fieldname . $trc,
336
				null,
337
				'password',
338
				$value
339
			))->setHelp($description);
340
			break;
341
		case "textarea":
342 3478ac16 Stephen Beaver
			$group->add(new Form_Textarea(
343 eb3743d8 Stephen Beaver
				$fieldname . $trc,
344
				null,
345
				$value
346
			))->setHelp($description);
347
348
			break;
349
		case "select":
350
			$options = array();
351
			foreach ($rowhelper['options']['option'] as $rowopt) {
352
				$options[$rowopt['value']] = $rowopt['name'];
353
			}
354
355 57fc02b1 Stephen Beaver
			$grp = new Form_Select(
356 eb3743d8 Stephen Beaver
				$fieldname . $trc,
357
				null,
358
				$value,
359
				$options
360 57fc02b1 Stephen Beaver
			);
361
362
			$grp->setHelp($description);
363
364 bf7ee0e0 BBcan177
			if ($ewidth) {
365
				$grp->setWidth($ewidth);
366 57fc02b1 Stephen Beaver
			}
367
368
			$group->add($grp);
369 eb3743d8 Stephen Beaver
370
			break;
371
		case "interfaces_selection":
372
			$size = ($size ? "size=\"{$size}\"" : '');
373
			$multiple = '';
374
			if (isset($rowhelper['multiple'])) {
375 c4b60a9a Colin Fleming
				$multiple = "multiple";
376 eb3743d8 Stephen Beaver
			}
377
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
378
			$ifaces = get_configured_interface_with_descr();
379
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
380
			if (!empty($additional_ifaces)) {
381
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
382
			}
383
384
			if (is_array($value)) {
385
				$values = $value;
386
			} else {
387
				$values = explode(',', $value);
388
			}
389
390
			$ifaces["lo0"] = "loopback";
391
			$options = array();
392
			$selected = array();
393
394
			foreach ($ifaces as $ifname => $iface) {
395
				$options[$ifname] = $iface;
396
397 0f649c97 Phil Davis
				if (in_array($ifname, $values)) {
398 eb3743d8 Stephen Beaver
					array_push($selected, $ifname);
399
				}
400
			}
401
402
			$group->add(new Form_Select(
403
				$fieldname . $trc,
404
				null,
405
				($multiple) ? $selected:$selected[0],
406
				$options,
407
				$multiple
408
			))->setHelp($description);
409
410
			//echo "</select>\n";
411
			break;
412
		case "select_source":
413
			$options = array();
414
			$selected = array();
415
416
			if (isset($rowhelper['show_disable_value'])) {
417
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
418
			}
419
420
			$source_url = $rowhelper['source'];
421
			eval("\$pkg_source_txt = &$source_url;");
422
423
			foreach ($pkg_source_txt as $opt) {
424
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
425
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
426
				$options[$source_value] = $source_name;
427
428 0f649c97 Phil Davis
				if ($source_value == $value) {
429 eb3743d8 Stephen Beaver
					array_push($selected, $value);
430
				}
431
			}
432
433
			$group->add(new Form_Select(
434
				$fieldname . $trc,
435
				null,
436
				($multiple) ? $selected:$selected[0],
437
				$options,
438
				$multiple
439
			))->setHelp($description);
440
441
			break;
442
	}
443
}
444
445
function fixup_string($string) {
446
	global $config;
447
	// fixup #1: $myurl -> http[s]://ip_address:port/
448
	$https = "";
449
	$port = $config['system']['webguiport'];
450
	if ($port != "443" and $port != "80") {
451
		$urlport = ":" . $port;
452
	} else {
453
		$urlport = "";
454
	}
455
456
	if ($config['system']['webgui']['protocol'] == "https") {
457
		$https = "s";
458
	}
459
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
460
	$newstring = str_replace("\$myurl", $myurl, $string);
461
	$string = $newstring;
462
	// fixup #2: $wanip
463
	$curwanip = get_interface_ip();
464
	$newstring = str_replace("\$wanip", $curwanip, $string);
465
	$string = $newstring;
466
	// fixup #3: $lanip
467
	$lancfg = $config['interfaces']['lan'];
468
	$lanip = $lancfg['ipaddr'];
469
	$newstring = str_replace("\$lanip", $lanip, $string);
470
	$string = $newstring;
471
	// fixup #4: fix'r'up here.
472
	return $newstring;
473
}
474
475
/*
476
 *	Parse templates if they are defined
477
 */
478
function parse_package_templates() {
479
	global $pkg;
480
	if ($pkg['templates']['template'] != "") {
481
		foreach ($pkg['templates']['template'] as $pkg_template_row) {
482
			$filename = $pkg_template_row['filename'];
483
			$template_text = $pkg_template_row['templatecontents'];
484
			/* calculate total row helpers count and */
485
			/* change fields defined as fieldname_fieldvalue to their value */
486
			foreach ($pkg['fields']['field'] as $fields) {
487
				switch ($fields['type']) {
488
					case "rowhelper":
489
						// save rowhelper items.
490
						$row_helper_total_rows = 0;
491
						$row_helper_data = "";
492
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
493
							foreach ($_POST as $key => $value) {
494
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
495
									$row_helper_total_rows++;
496
									$row_helper_data .= $value;
497
									$sep = "";
498
									ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
499
									foreach ($sep as $se) {
500
										$separator = $se;
501
									}
502
									if ($separator != "") {
503
										$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
504
										$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
505
									}
506
									$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
507
								}
508
							}
509
						}
510
						break;
511
					default:
512
						$fieldname = $fields['fieldname'];
513
						$fieldvalue = $_POST[$fieldname];
514
						$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
515
				}
516
			}
517
			/* replace $domain_total_rows with total rows */
518
			$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
519
520
			/* replace cr's */
521
			$template_text = str_replace("\\n", "\n", $template_text);
522
523
			/* write out new template file */
524
			$fout = fopen($filename, "w");
525
			fwrite($fout, $template_text);
526
			fclose($fout);
527
		}
528
	}
529
}
530
531 0a0447de heper
//breadcrumb
532
if ($pkg['title'] != "") {
533 0f649c97 Phil Davis
	if (!$only_edit) {
534 b238793b Stephen Beaver
		$pkg['title'] = $pkg['title'] . '/Edit';
535
	}
536
537
	if (strpos($pkg['title'], '/')) {
538
		$title = explode('/', $pkg['title']);
539
540
		foreach ($title as $subtitle) {
541
			$pgtitle[] = gettext($subtitle);
542 9ec7699b Stephen Beaver
		}
543 b238793b Stephen Beaver
	} else {
544
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
545 62cdbf62 heper
	}
546 0a0447de heper
} else {
547 4a457d8d Stephen Beaver
	$pgtitle = array(gettext("Package"), gettext("Editor"));
548 0a0447de heper
}
549 b89a8cbc bruno
550 eb3743d8 Stephen Beaver
// Create any required tabs
551 23f6cdd7 Stephen Beaver
if ($pkg['tabs'] != "") {
552 e11aa161 Warren Baker
	$tab_array = array();
553 f8ac4407 Phil Davis
	foreach ($pkg['tabs']['tab'] as $tab) {
554
		if ($tab['tab_level']) {
555 90551807 Warren Baker
			$tab_level = $tab['tab_level'];
556 f8ac4407 Phil Davis
		} else {
557 90551807 Warren Baker
			$tab_level = 1;
558 f8ac4407 Phil Davis
		}
559 eb3743d8 Stephen Beaver
560 f8ac4407 Phil Davis
		if (isset($tab['active'])) {
561 90551807 Warren Baker
			$active = true;
562 b238793b Stephen Beaver
			$pgtitle[] = $tab['text'] ;
563 90551807 Warren Baker
		} else {
564
			$active = false;
565
		}
566 eb3743d8 Stephen Beaver
567 f8ac4407 Phil Davis
		if (isset($tab['no_drop_down'])) {
568 f8c462dd Warren Baker
			$no_drop_down = true;
569 f8ac4407 Phil Davis
		}
570 eb3743d8 Stephen Beaver
571 90551807 Warren Baker
		$urltmp = "";
572 23f6cdd7 Stephen Beaver
		if ($tab['url'] != "") {
573 f8ac4407 Phil Davis
			$urltmp = $tab['url'];
574
		}
575 eb3743d8 Stephen Beaver
576 23f6cdd7 Stephen Beaver
		if ($tab['xml'] != "") {
577 f8ac4407 Phil Davis
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
578
		}
579 90551807 Warren Baker
580 f8ac4407 Phil Davis
		$addresswithport = getenv("HTTP_HOST");
581 90551807 Warren Baker
		$colonpos = strpos($addresswithport, ":");
582 eb3743d8 Stephen Beaver
583 90551807 Warren Baker
		if ($colonpos !== False) {
584
			//my url is actually just the IP address of the pfsense box
585
			$myurl = substr($addresswithport, 0, $colonpos);
586
		} else {
587
			$myurl = $addresswithport;
588
		}
589 eb3743d8 Stephen Beaver
590 90551807 Warren Baker
		// eval url so that above $myurl item can be processed if need be.
591
		$url = str_replace('$myurl', $myurl, $urltmp);
592
593
		$tab_array[$tab_level][] = array(
594 f8ac4407 Phil Davis
			$tab['text'],
595
			$active,
596
			$url
597
		);
598
	}
599 90551807 Warren Baker
600
	ksort($tab_array);
601 0a0447de heper
}
602 23f6cdd7 Stephen Beaver
603 4b1d882f Colin Fleming
include("head.inc");
604 0a0447de heper
if ($pkg['custom_php_after_head_command']) {
605
	eval($pkg['custom_php_after_head_command']);
606
}
607 0f649c97 Phil Davis
if (isset($tab_array)) {
608 f8ac4407 Phil Davis
	foreach ($tab_array as $tabid => $tab) {
609 23f6cdd7 Stephen Beaver
		display_top_tabs($tab); //, $no_drop_down, $tabid);
610 90551807 Warren Baker
	}
611 7c061036 Scott Ullrich
}
612 55c846c4 Marcello Coutinho
613 0a0447de heper
// Start of page display
614 0f649c97 Phil Davis
if ($input_errors) {
615 0a0447de heper
	print_input_errors($input_errors);
616 0f649c97 Phil Davis
}
617 0a0447de heper
618 0f649c97 Phil Davis
if ($savemsg) {
619 0a0447de heper
	print_info_box($savemsg, 'success');
620 0f649c97 Phil Davis
}
621 55c846c4 Marcello Coutinho
622 eb3743d8 Stephen Beaver
$cols = 0;
623
$savevalue = gettext("Save");
624
if ($pkg['savetext'] != "") {
625
	$savevalue = $pkg['savetext'];
626
}
627
628 e0d1a2f8 Renato Botelho
$savehelp = "";
629 608bb662 Stephen Beaver
if ($pkg['savehelp'] != "") {
630
	$savehelp = $pkg['savehelp'];
631
}
632
633 f84e2105 jim-p
$saveicon = "fa-save";
634
if ($pkg['saveicon'] != "") {
635
	$saveicon = $pkg['saveicon'];
636
}
637
638
$savebtnclass = "btn-primary";
639
if ($pkg['savebtnclass'] != "") {
640
	$savebtnclass = $pkg['savebtnclass'];
641
}
642
643 eb3743d8 Stephen Beaver
$grouping = false; // Indicates the elements we are composing are part of a combined group
644 23f6cdd7 Stephen Beaver
645 608bb662 Stephen Beaver
$savebutton = new Form_Button(
646 eb3743d8 Stephen Beaver
	'submit',
647 f84e2105 jim-p
	$savevalue,
648
	null,
649
	$saveicon
650 608bb662 Stephen Beaver
);
651 f84e2105 jim-p
$savebutton->addClass($savebtnclass);
652 608bb662 Stephen Beaver
653
if ($savehelp) {
654
	$savebutton->setHelp($savehelp);
655
}
656
657
$form = new Form($savebutton);
658 23f6cdd7 Stephen Beaver
659 eb3743d8 Stephen Beaver
$form->addGlobal(new Form_Input(
660
	'xml',
661
	null,
662
	'hidden',
663
	$xml
664
));
665 23f6cdd7 Stephen Beaver
666 eb3743d8 Stephen Beaver
/* If a package's XML has <advanced_options/> configured, then setup
667 2d911f2c Phil Davis
 * the section for the fields that have <advancedfield/> set.
668 eb3743d8 Stephen Beaver
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
669
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
670
 */
671 32487e42 Scott Ullrich
672 eb3743d8 Stephen Beaver
if ($pkg['advanced_options'] == "enabled") {
673
	$advfield_count = 0;
674 20414655 k-paulius
	$advanced = new Form_Section("Advanced Features");
675 eb3743d8 Stephen Beaver
	$advanced->addClass('advancedoptions');
676
}
677 23f6cdd7 Stephen Beaver
678 eb3743d8 Stephen Beaver
$js_array = array();
679 23f6cdd7 Stephen Beaver
680 2d911f2c Phil Davis
// Now loop through all of the fields defined in the XML
681 eb3743d8 Stephen Beaver
foreach ($pkg['fields']['field'] as $pkga) {
682 23f6cdd7 Stephen Beaver
683 4a6272be Stephen Beaver
	$action = "";
684
	$uid = "";
685
686 eb3743d8 Stephen Beaver
	if ($pkga['type'] == "sorting") {
687
		continue;
688
	}
689 23f6cdd7 Stephen Beaver
690 eb3743d8 Stephen Beaver
	// Generate a new section
691
	if ($pkga['type'] == "listtopic") {
692
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
693
			$advanced->addInput(new Form_StaticText(
694
				strip_tags($pkga['name']),
695
				null
696
			));
697 1624b5f1 Marcello Coutinho
698 eb3743d8 Stephen Beaver
			$advfield_count++;
699
		}  else {
700 0f649c97 Phil Davis
			if (isset($section)) {
701 eb3743d8 Stephen Beaver
				$form->add($section);
702 0f649c97 Phil Davis
			}
703 1624b5f1 Marcello Coutinho
704 4a6272be Stephen Beaver
			if (isset($pkga['collapse'])) {
705
				$uid = uniqid("section");
706
707
				$action = COLLAPSIBLE;
708
709
				if ($pkga['collapse'] == "open") {
710
					$action |= SEC_OPEN;
711
				} else {
712
					$action |= SEC_CLOSED;
713
				}
714
			}
715
716
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
717 e11aa161 Warren Baker
		}
718 23f6cdd7 Stephen Beaver
719 eb3743d8 Stephen Beaver
		continue;
720
	}
721 1624b5f1 Marcello Coutinho
722 eb3743d8 Stephen Beaver
	// 'begin' starts a form group. ('end' ends it)
723
	if ($pkga['combinefields'] == "begin") {
724
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
725
		$grouping = true;
726
	}
727
728
	$size = "";
729
	$colspan="";
730
731
	// if user is editing a record, load in the data.
732
	$fieldname = $pkga['fieldname'];
733
	unset($value);
734
	if ($get_from_post) {
735
		$value = $_POST[$fieldname];
736
		if (is_array($value)) {
737
			$value = implode(',', $value);
738 55c846c4 Marcello Coutinho
		}
739 eb3743d8 Stephen Beaver
	} else {
740
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
741
			$value = $a_pkg[$id][$fieldname];
742 e11aa161 Warren Baker
		} else {
743 eb3743d8 Stephen Beaver
			if (isset($pkga['default_value'])) {
744
				$value = $pkga['default_value'];
745 f8ac4407 Phil Davis
			}
746 31d27c6c Scott Ullrich
		}
747 eb3743d8 Stephen Beaver
	}
748 23f6cdd7 Stephen Beaver
749 86a20575 Stephen Beaver
	// If we get here but have no $section, the package config file probably had no listtopic field
750
	// We can create a section with a generic name to fix that
751 0f649c97 Phil Davis
	if (!$section) {
752 20414655 k-paulius
		$section = new Form_Section('General Options');
753 0f649c97 Phil Davis
	}
754 86a20575 Stephen Beaver
755 eb3743d8 Stephen Beaver
	switch ($pkga['type']) {
756 5697a26a Stephen Beaver
		// Create an input element. The format is slightly different depending on whether we are composing a group,
757 eb3743d8 Stephen Beaver
		// section, or advanced section. This is true for every element type
758
		case "input":
759 6e5cec8e doktornotor
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
760
				$value = base64_decode($value);
761
			}
762
763 962ec941 Stephen Beaver
			$grp = new Form_Input(
764 eb3743d8 Stephen Beaver
					$pkga['fieldname'],
765
					$pkga['fielddescr'],
766
					'text',
767
					$value
768 962ec941 Stephen Beaver
				);
769
770
			$grp->setHelp($pkga['description']);
771
772
			if ($pkga['width']) {
773
				$grp->setWidth($pkga['width']);
774
			}
775
776
			if ($grouping) {
777
				$group->add($grp);
778 eb3743d8 Stephen Beaver
			} else {
779 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
780 962ec941 Stephen Beaver
					$advanced->addInput($grp);
781 f8ac4407 Phil Davis
				} else {
782 962ec941 Stephen Beaver
					$section->addInput($grp);
783 f8ac4407 Phil Davis
				}
784 eb3743d8 Stephen Beaver
			}
785 23f6cdd7 Stephen Beaver
786 eb3743d8 Stephen Beaver
			break;
787 1624b5f1 Marcello Coutinho
788 eb3743d8 Stephen Beaver
		case "password":
789 6e5cec8e doktornotor
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
790
				$value = base64_decode($value);
791
			}
792
793 2d911f2c Phil Davis
			// Create a password element
794 6e5cec8e doktornotor
			if ($grouping) {
795 eb3743d8 Stephen Beaver
				$group->add(new Form_Input(
796
					$pkga['fieldname'],
797
					$pkga['fielddescr'],
798
					'password',
799
					$value
800
				))->setHelp($pkga['description']);
801
			} else {
802 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
803
					$advanced->addInput(new Form_Input(
804
						$pkga['fieldname'],
805
						$pkga['fielddescr'],
806
						'password',
807
						$value
808
					))->setHelp($pkga['description']);
809 f8ac4407 Phil Davis
				} else {
810 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Input(
811
						$pkga['fieldname'],
812
						$pkga['fielddescr'],
813
						'password',
814
						$value
815
					))->setHelp($pkga['description']);
816 f8ac4407 Phil Davis
				}
817 eb3743d8 Stephen Beaver
			}
818 23f6cdd7 Stephen Beaver
819 eb3743d8 Stephen Beaver
			break;
820 23f6cdd7 Stephen Beaver
821 eb3743d8 Stephen Beaver
		case "info":
822 2d911f2c Phil Davis
			// If the info contains a table we should detect and Bootstrap it
823 23f6cdd7 Stephen Beaver
824 0f649c97 Phil Davis
			if (strpos($pkga['description'], '<table') !== FALSE) {
825 eb3743d8 Stephen Beaver
				$info = bootstrapTable($pkga['description']);
826 0f649c97 Phil Davis
			} else {
827 eb3743d8 Stephen Beaver
				$info = $pkga['description'];
828 0f649c97 Phil Davis
			}
829 23f6cdd7 Stephen Beaver
830 eb3743d8 Stephen Beaver
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
831
				$advanced->addInput(new Form_StaticText(
832
					strip_tags($pkga['fielddescr']),
833
					$info
834
				));
835
			} else {
836
				$section->addInput(new Form_StaticText(
837
					strip_tags($pkga['fielddescr']),
838
					$info
839
				));
840
			}
841 23f6cdd7 Stephen Beaver
842 eb3743d8 Stephen Beaver
			break;
843 1624b5f1 Marcello Coutinho
844 eb3743d8 Stephen Beaver
		case "select":
845
			// Create a select element
846
			$optionlist = array();
847
			$selectedlist = array();
848 23f6cdd7 Stephen Beaver
849 eb3743d8 Stephen Beaver
			$fieldname = $pkga['fieldname'];
850 23f6cdd7 Stephen Beaver
851 eb3743d8 Stephen Beaver
			if (isset($pkga['multiple'])) {
852 c4b60a9a Colin Fleming
				$multiple = 'multiple';
853 eb3743d8 Stephen Beaver
				$items = explode(',', $value);
854
				$fieldname .= "[]";
855
			} else {
856
				$multiple = '';
857
				$items = array($value);
858
			}
859 23f6cdd7 Stephen Beaver
860 eb3743d8 Stephen Beaver
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
861 23f6cdd7 Stephen Beaver
862 eb3743d8 Stephen Beaver
			foreach ($pkga['options']['option'] as $opt) {
863
				$optionlist[$opt['value']] = $opt['name'];
864 23f6cdd7 Stephen Beaver
865 eb3743d8 Stephen Beaver
				if (in_array($opt['value'], $items)) {
866
					array_push($selectedlist, $opt['value']);
867 f8ac4407 Phil Davis
				}
868 eb3743d8 Stephen Beaver
			}
869 23f6cdd7 Stephen Beaver
870 0f649c97 Phil Davis
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
871 eb3743d8 Stephen Beaver
				$function = $grouping ? $advanced->add:$advanced->addInput;
872 0f649c97 Phil Davis
			} else {
873 eb3743d8 Stephen Beaver
				$function = ($grouping) ? $section->add:$section->addInput;
874 0f649c97 Phil Davis
			}
875 23f6cdd7 Stephen Beaver
876 57fc02b1 Stephen Beaver
			$grp = new Form_Select(
877 23f6cdd7 Stephen Beaver
						$pkga['fieldname'],
878 eb3743d8 Stephen Beaver
						strip_tags($pkga['fielddescr']),
879 23f6cdd7 Stephen Beaver
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
880
						$optionlist,
881
						isset($pkga['multiple'])
882 a1cd3857 Stephen Beaver
					);
883
884 57fc02b1 Stephen Beaver
			$grp ->setHelp($pkga['description'])->setOnchange($onchange)->setAttribute('size', $pkga['size']);
885 a1cd3857 Stephen Beaver
886 57fc02b1 Stephen Beaver
			if ($pkga['width']) {
887
				$grp->setWidth($pkga['width']);
888
			}
889 a1cd3857 Stephen Beaver
890 57fc02b1 Stephen Beaver
			if ($grouping) {
891
				$group->add($grp);
892
			} else {
893
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
894
					$advanced->addInput($grp);
895
				} else {
896
					$section->addInput($grp);
897 f8ac4407 Phil Davis
				}
898 eb3743d8 Stephen Beaver
			}
899 23f6cdd7 Stephen Beaver
900 eb3743d8 Stephen Beaver
			break;
901 1624b5f1 Marcello Coutinho
902 eb3743d8 Stephen Beaver
		case "select_source":
903 23f6cdd7 Stephen Beaver
904 eb3743d8 Stephen Beaver
			if (isset($pkga['multiple'])) {
905
				$items = explode(',', $value);
906
				$fieldname .= "[]";
907
			} else {
908
				$items = array($value);
909
			}
910 23f6cdd7 Stephen Beaver
911 eb3743d8 Stephen Beaver
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
912 55c846c4 Marcello Coutinho
913 eb3743d8 Stephen Beaver
			$source_url = $pkga['source'];
914
			eval("\$pkg_source_txt = &$source_url;");
915 23f6cdd7 Stephen Beaver
916 eb3743d8 Stephen Beaver
			#check if show disable option is present on xml
917 eeae21e8 jim-p
			if (!is_array($pkg_source_txt)) {
918
				$pkg_source_txt = array();
919
			}
920 eb3743d8 Stephen Beaver
			if (isset($pkga['show_disable_value'])) {
921
				array_push($pkg_source_txt,
922
					array(($pkga['source_name']? $pkga['source_name'] : $pkga['name'])=> $pkga['show_disable_value'], ($pkga['source_value']? $pkga['source_value'] : $pkga['value'])=> $pkga['show_disable_value']));
923
			}
924 23f6cdd7 Stephen Beaver
925 eb3743d8 Stephen Beaver
			$srcoptions = array();
926
			$srcselected = array();
927 23f6cdd7 Stephen Beaver
928 eb3743d8 Stephen Beaver
			foreach ($pkg_source_txt as $opt) {
929
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
930
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
931
				$srcoptions[$source_value] = $source_name;
932 23f6cdd7 Stephen Beaver
933 0f649c97 Phil Davis
				if (in_array($source_value, $items)) {
934 eb3743d8 Stephen Beaver
					array_push($srcselected, $source_value);
935 0f649c97 Phil Davis
				}
936 eb3743d8 Stephen Beaver
			}
937 23f6cdd7 Stephen Beaver
938 83d8e9ab Luiz Otavio O Souza
			$descr = (isset($pkga['description'])) ? $pkga['description'] : "";
939 0f649c97 Phil Davis
			if ($grouping) {
940 eb3743d8 Stephen Beaver
				$group->add(new Form_Select(
941
					$pkga['fieldname'],
942 2512e643 jim-p
					strip_tags($pkga['fielddescr']),
943 eb3743d8 Stephen Beaver
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
944
					$srcoptions,
945
					isset($pkga['multiple'])
946 83d8e9ab Luiz Otavio O Souza
				))->setHelp($descr)->setOnchange($onchange);
947 eb3743d8 Stephen Beaver
			} else {
948 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
949
					$advanced->addInput(new Form_Select(
950
						$pkga['fieldname'],
951 2512e643 jim-p
						strip_tags($pkga['fielddescr']),
952 23f6cdd7 Stephen Beaver
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
953 eb3743d8 Stephen Beaver
						$srcoptions,
954 23f6cdd7 Stephen Beaver
						isset($pkga['multiple'])
955 83d8e9ab Luiz Otavio O Souza
					))->setHelp($descr)->setOnchange($onchange);
956 f8ac4407 Phil Davis
				} else {
957 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Select(
958
						$pkga['fieldname'],
959 2512e643 jim-p
						strip_tags($pkga['fielddescr']),
960 23f6cdd7 Stephen Beaver
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
961 eb3743d8 Stephen Beaver
						$srcoptions,
962 23f6cdd7 Stephen Beaver
						isset($pkga['multiple'])
963 83d8e9ab Luiz Otavio O Souza
					))->setHelp($descr)->setOnchange($onchange);
964 f8ac4407 Phil Davis
				}
965 eb3743d8 Stephen Beaver
			}
966 23f6cdd7 Stephen Beaver
967 eb3743d8 Stephen Beaver
			break;
968 1624b5f1 Marcello Coutinho
969 eb3743d8 Stephen Beaver
		case "vpn_selection" :
970
			$vpnlist = array();
971 23f6cdd7 Stephen Beaver
972 eb3743d8 Stephen Beaver
			foreach ($config['ipsec']['phase1'] as $vpn) {
973
				$vpnlist[$vpn['descr']] = $vpn['descr'];
974 23f6cdd7 Stephen Beaver
975 eb3743d8 Stephen Beaver
			}
976 55c846c4 Marcello Coutinho
977 0f649c97 Phil Davis
			if ($grouping) {
978 eb3743d8 Stephen Beaver
				$group->add(new Form_Select(
979
					$pkga['fieldname'],
980
					null,
981
					false,
982
					$vpnlist
983
				))->setHelp(fixup_string($pkga['description']));
984
			} else {
985 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
986
					$advanced->addInput(new Form_Select(
987
						$pkga['fieldname'],
988
						null,
989
						false,
990
						$vpnlist
991
					))->setHelp(fixup_string($pkga['description']));
992 f8ac4407 Phil Davis
				} else {
993 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Select(
994
						$pkga['fieldname'],
995
						null,
996
						false,
997
						$vpnlist
998
					))->setHelp(fixup_string($pkga['description']));
999 f8ac4407 Phil Davis
				}
1000 eb3743d8 Stephen Beaver
			}
1001 23f6cdd7 Stephen Beaver
1002 eb3743d8 Stephen Beaver
			break;
1003 23f6cdd7 Stephen Beaver
1004 eb3743d8 Stephen Beaver
		// Create a checkbox element
1005
		case "checkbox":
1006
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
1007 0f649c97 Phil Davis
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
1008 eb3743d8 Stephen Beaver
				$onclick = 'javascript:enablechange();';
1009 0f649c97 Phil Davis
			} else {
1010 eb3743d8 Stephen Beaver
				$onclick = '';
1011 0f649c97 Phil Davis
			}
1012 eb3743d8 Stephen Beaver
1013 0f649c97 Phil Davis
			if ($grouping) {
1014 eb3743d8 Stephen Beaver
				$group->add(new Form_Checkbox(
1015
					$pkga['fieldname'],
1016
					$pkga['fielddescr'],
1017 86c77223 Stephen Beaver
					fixup_string($pkga['description']),
1018 eb3743d8 Stephen Beaver
					($value == "on"),
1019
					'on'
1020 86c77223 Stephen Beaver
				))->setOnclick($onclick)
1021 bb69870b Stephen Beaver
				  ->setOnchange($onchange)
1022 82c02c9e heper
				  ->setHelp($pkga['sethelp']);
1023 eb3743d8 Stephen Beaver
			} else {
1024 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1025
					$advanced->addInput(new Form_Checkbox(
1026
						$pkga['fieldname'],
1027 1ae171a3 Stephen Beaver
						$pkga['fielddescr'],
1028 86c77223 Stephen Beaver
						fixup_string($pkga['description']),
1029 23f6cdd7 Stephen Beaver
						($value == "on"),
1030
						'on'
1031 86c77223 Stephen Beaver
					))->setOnclick($onclick)
1032 bb69870b Stephen Beaver
					  ->setOnchange($onchange)
1033 82c02c9e heper
					  ->setHelp($pkga['sethelp']);
1034 f8ac4407 Phil Davis
				} else {
1035 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Checkbox(
1036
						$pkga['fieldname'],
1037
						$pkga['fielddescr'],
1038 86c77223 Stephen Beaver
						fixup_string($pkga['description']),
1039 23f6cdd7 Stephen Beaver
						($value == "on"),
1040
						'on'
1041 86c77223 Stephen Beaver
					))->setOnclick($onclick)
1042 1ae171a3 Stephen Beaver
					  ->setOnchange($onchange)
1043 bb69870b Stephen Beaver
					  ->setHelp($pkga['sethelp']);
1044 f8ac4407 Phil Davis
				}
1045 eb3743d8 Stephen Beaver
			}
1046 23f6cdd7 Stephen Beaver
1047 eb3743d8 Stephen Beaver
			break;
1048 1624b5f1 Marcello Coutinho
1049 2d911f2c Phil Davis
		// Create a textarea element
1050 eb3743d8 Stephen Beaver
		case "textarea":
1051 c70b6835 Stephen Beaver
			$rows = $cols = 0;
1052
1053 eb3743d8 Stephen Beaver
			if ($pkga['rows']) {
1054 c70b6835 Stephen Beaver
				$rows = $pkga['rows'];
1055 eb3743d8 Stephen Beaver
			}
1056
			if ($pkga['cols']) {
1057 c70b6835 Stephen Beaver
				$cols = $pkga['cols'];
1058 eb3743d8 Stephen Beaver
			}
1059 c70b6835 Stephen Beaver
1060 eb3743d8 Stephen Beaver
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1061
				$value = base64_decode($value);
1062
			}
1063 23f6cdd7 Stephen Beaver
1064 c70b6835 Stephen Beaver
			$grp = new Form_Textarea(
1065 eb3743d8 Stephen Beaver
					$pkga['fieldname'],
1066
					$pkga['fielddescr'],
1067
					$value
1068 c70b6835 Stephen Beaver
			);
1069
1070
			$grp->setHelp(fixup_string($pkga['description']));
1071
1072
			if ($rows > 0) {
1073
				$grp->setRows($rows);
1074
			}
1075
1076
			if ($cols > 0) {
1077
				$grp->setCols($cols);
1078
			}
1079
1080
			if ($pkga['wrap'] == "off") {
1081
				$grp->setAttribute("wrap", "off");
1082 e5274f51 Stephen Beaver
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1083
			} else {
1084
				$grp->setAttribute("style", "width: auto;");
1085 c70b6835 Stephen Beaver
			}
1086
1087
			if ($grouping) {
1088
				$group->add($grp);
1089 eb3743d8 Stephen Beaver
			} else {
1090 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1091 c70b6835 Stephen Beaver
					$advanced->addInput($grp);
1092 f8ac4407 Phil Davis
				} else {
1093 c70b6835 Stephen Beaver
					$section->addInput($grp);
1094 f8ac4407 Phil Davis
				}
1095 eb3743d8 Stephen Beaver
			}
1096 23f6cdd7 Stephen Beaver
1097 eb3743d8 Stephen Beaver
			break;
1098 b89a8cbc bruno
1099 eb3743d8 Stephen Beaver
		case "aliases":
1100
1101
			// Use xml tag <typealiases> to filter type aliases
1102
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1103
			$fieldname = $pkga['fieldname'];
1104
			$a_aliases = &$config['aliases']['alias'];
1105
			$addrisfirst = 0;
1106
			$aliasesaddr = "";
1107
1108
			if (isset($a_aliases)) {
1109
				if (!empty($pkga['typealiases'])) {
1110
					foreach ($a_aliases as $alias) {
1111
						if ($alias['type'] == $pkga['typealiases']) {
1112 f8ac4407 Phil Davis
							if ($addrisfirst == 1) {
1113
								$aliasesaddr .= ",";
1114
							}
1115 b89a8cbc bruno
							$aliasesaddr .= "'" . $alias['name'] . "'";
1116
							$addrisfirst = 1;
1117
						}
1118
					}
1119 eb3743d8 Stephen Beaver
				} else {
1120
					foreach ($a_aliases as $alias) {
1121
						if ($addrisfirst == 1) {
1122
							$aliasesaddr .= ",";
1123
						}
1124
						$aliasesaddr .= "'" . $alias['name'] . "'";
1125
						$addrisfirst = 1;
1126
					}
1127 b89a8cbc bruno
				}
1128 eb3743d8 Stephen Beaver
			}
1129 b89a8cbc bruno
1130 962ec941 Stephen Beaver
			$grp = new Form_Input(
1131 eb3743d8 Stephen Beaver
					$pkga['fieldname'],
1132
					$pkga['fielddescr'],
1133
					'text',
1134
					$value
1135 962ec941 Stephen Beaver
				);
1136
1137
			$grp->setHelp($pkga['description']);
1138
1139
			if ($pkga['width']) {
1140
				$grp->setWidth($pkga['width']);
1141
			}
1142
1143
			if (grouping) {
1144
				$group->add($grp);
1145 eb3743d8 Stephen Beaver
			} else {
1146
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1147 962ec941 Stephen Beaver
					$advanced->addInput($grp);
1148 eb3743d8 Stephen Beaver
				} else {
1149 962ec941 Stephen Beaver
					$section->addInput($grp);
1150 eb3743d8 Stephen Beaver
				}
1151
			}
1152 b89a8cbc bruno
1153 eb3743d8 Stephen Beaver
			$script = "<script type='text/javascript'>\n";
1154
			$script .= "//<![CDATA[\n";
1155
			$script .= "events.push(function(){\n";
1156
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1157
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1158
			$script .= "		source: aliasarray\n";
1159
			$script .= "	})\n";
1160
			$script .= "});\n";
1161
			$script .= "//]]>\n";
1162
			$script .= "</script>";
1163 b89a8cbc bruno
1164 eb3743d8 Stephen Beaver
			echo $script;
1165
1166
			break;
1167
1168
		case "interfaces_selection":
1169
			$ips = array();
1170
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1171
			if (is_array($config['interfaces'])) {
1172
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1173
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1174
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1175
						if (isset($pkga['showips'])) {
1176
							$iface_description .= " address";
1177 f8ac4407 Phil Davis
						}
1178 eb3743d8 Stephen Beaver
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1179 e14fbca4 Marcello Coutinho
					}
1180 f8ac4407 Phil Davis
				}
1181 eb3743d8 Stephen Beaver
			}
1182 23f6cdd7 Stephen Beaver
1183 eb3743d8 Stephen Beaver
			if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
1184
				foreach ($config['virtualip']['vip'] as $vip) {
1185
					if (!preg_match("/$interface_regex/", $vip['interface'])) {
1186
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
1187
					}
1188
					switch ($vip['mode']) {
1189
						case "ipalias":
1190
						case "carp":
1191
							$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1192
							break;
1193
						case "proxyarp":
1194
							if ($vip['type'] == "network") {
1195
								$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
1196
								$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
1197
								$len = $end - $start;
1198
								for ($i = 0; $i <= $len; $i++) {
1199
									$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
1200 f8ac4407 Phil Davis
								}
1201 eb3743d8 Stephen Beaver
							} else {
1202
								$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
1203
							}
1204
							break;
1205 e14fbca4 Marcello Coutinho
					}
1206 f8ac4407 Phil Davis
				}
1207 eb3743d8 Stephen Beaver
			}
1208 23f6cdd7 Stephen Beaver
1209 eb3743d8 Stephen Beaver
			sort($ips);
1210
			if (isset($pkga['showlistenall'])) {
1211 cadeb9fd Phil Davis
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1212 eb3743d8 Stephen Beaver
			}
1213 e14fbca4 Marcello Coutinho
1214 eb3743d8 Stephen Beaver
			if (!preg_match("/$interface_regex/", "loopback")) {
1215 cadeb9fd Phil Davis
				$loopback_text = gettext("loopback");
1216
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1217 eb3743d8 Stephen Beaver
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1218
			}
1219 23f6cdd7 Stephen Beaver
1220 eb3743d8 Stephen Beaver
			#show interfaces array on gui
1221
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1222
			$multiple = '';
1223
			$fieldname = $pkga['fieldname'];
1224
			if (isset($pkga['multiple'])) {
1225
				$fieldname .= '[]';
1226 c4b60a9a Colin Fleming
				$multiple = 'multiple';
1227 eb3743d8 Stephen Beaver
			}
1228 23f6cdd7 Stephen Beaver
1229 eb3743d8 Stephen Beaver
			$selectedlist = array();
1230
			$optionlist = array();
1231 23f6cdd7 Stephen Beaver
1232 eb3743d8 Stephen Beaver
			if (is_array($value)) {
1233
				$values = $value;
1234
			} else {
1235
				$values = explode(',', $value);
1236
			}
1237 23f6cdd7 Stephen Beaver
1238 eb3743d8 Stephen Beaver
			foreach ($ips as $iface) {
1239
				if (in_array($iface['ip'], $values)) {
1240
					array_push($selectedlist, $iface['ip']);
1241 f8ac4407 Phil Davis
				}
1242 23f6cdd7 Stephen Beaver
1243 eb3743d8 Stephen Beaver
				$optionlist[$iface['ip']] = $iface['description'];
1244
			}
1245
1246 0f649c97 Phil Davis
			if ($grouping) {
1247 eb3743d8 Stephen Beaver
				$group->add(new Form_Select(
1248
					$pkga['fieldname'],
1249
					$pkga['fielddescr'],
1250
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1251
					$optionlist,
1252
					isset($pkga['multiple'])
1253
				))->setHelp($pkga['description']);
1254
			} else {
1255 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1256
					$advanced->addInput(new Form_Select(
1257
						$pkga['fieldname'],
1258
						$pkga['fielddescr'],
1259
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1260
						$optionlist,
1261
						isset($pkga['multiple'])
1262
					))->setHelp($pkga['description']);
1263 f8ac4407 Phil Davis
				} else {
1264 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Select(
1265
						$pkga['fieldname'],
1266
						$pkga['fielddescr'],
1267
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1268
						$optionlist,
1269
						isset($pkga['multiple'])
1270
					))->setHelp($pkga['description']);
1271 f8ac4407 Phil Davis
				}
1272 eb3743d8 Stephen Beaver
			}
1273 23f6cdd7 Stephen Beaver
1274 eb3743d8 Stephen Beaver
			break;
1275 1624b5f1 Marcello Coutinho
1276 eb3743d8 Stephen Beaver
		// Create radio button
1277
		case "radio":
1278 0f649c97 Phil Davis
			if ($grouping) {
1279 eb3743d8 Stephen Beaver
				$group->add(new Form_Checkbox(
1280
					$pkga['fieldname'],
1281
					$pkga['fielddescr'],
1282 86c77223 Stephen Beaver
					fixup_string($pkga['description']),
1283 eb3743d8 Stephen Beaver
					($value == "on"),
1284
					'on'
1285 86c77223 Stephen Beaver
				))->displayAsRadio();
1286 eb3743d8 Stephen Beaver
			} else {
1287 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1288
					$advanced->addInput(new Form_Checkbox(
1289
						$pkga['fieldname'],
1290
						$pkga['fielddescr'],
1291 86c77223 Stephen Beaver
						fixup_string($pkga['description']),
1292 23f6cdd7 Stephen Beaver
						($value == "on"),
1293
						'on'
1294 86c77223 Stephen Beaver
					))->displayAsRadio();
1295 f8ac4407 Phil Davis
				} else {
1296 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Checkbox(
1297
						$pkga['fieldname'],
1298
						$pkga['fielddescr'],
1299 86c77223 Stephen Beaver
						fixup_string($pkga['description']),
1300 23f6cdd7 Stephen Beaver
						($value == "on"),
1301
						'on'
1302 86c77223 Stephen Beaver
					))->displayAsRadio();
1303 f8ac4407 Phil Davis
				}
1304 eb3743d8 Stephen Beaver
			}
1305 23f6cdd7 Stephen Beaver
1306 eb3743d8 Stephen Beaver
			break;
1307 1624b5f1 Marcello Coutinho
1308 eb3743d8 Stephen Beaver
		// Create form button
1309
		case "button":
1310 827a3812 jim-p
			$newbtnicon = "fa-save";
1311
			if ($pkga['buttonicon'] != "") {
1312
				$newbtnicon = $pkga['buttonicon'];
1313
			}
1314
			$newbtnclass = "btn-primary";
1315
			if ($pkga['buttonclass'] != "") {
1316
				$newbtnclass = $pkga['buttonclass'];
1317
			}
1318
1319 eb3743d8 Stephen Beaver
			$newbtn = new Form_Button(
1320
				$pkga['fieldname'],
1321 827a3812 jim-p
				$pkga['fieldname'],
1322
				null,
1323
				$newbtnicon
1324 eb3743d8 Stephen Beaver
			);
1325 827a3812 jim-p
			$newbtn->addClass($newbtnclass);
1326 23f6cdd7 Stephen Beaver
1327 0f649c97 Phil Davis
			if (grouping) {
1328 eb3743d8 Stephen Beaver
				$group->add(new Form_StaticText(
1329
					null,
1330
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1331
				));
1332
			} else {
1333
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1334 0f649c97 Phil Davis
					$advanced->addInput(new Form_StaticText(
1335
						null,
1336
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1337
					));
1338 eb3743d8 Stephen Beaver
				} else {
1339 0f649c97 Phil Davis
					$section->addInput(new Form_StaticText(
1340
						null,
1341
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1342
					));
1343 eb3743d8 Stephen Beaver
				}
1344
			}
1345 23f6cdd7 Stephen Beaver
1346 eb3743d8 Stephen Beaver
			break;
1347 1624b5f1 Marcello Coutinho
1348 eb3743d8 Stephen Beaver
		case "schedule_selection":
1349 23f6cdd7 Stephen Beaver
1350 eb3743d8 Stephen Beaver
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1351
			$schedules = array();
1352
			$schedules[] = "none";
1353
			if (is_array($config['schedules']['schedule'])) {
1354
				foreach ($config['schedules']['schedule'] as $schedule) {
1355
					if ($schedule['name'] != "") {
1356
						$schedules[] = $schedule['name'];
1357 c67b75d1 xbipin
					}
1358 c6c71b36 xbipin
				}
1359 eb3743d8 Stephen Beaver
			}
1360 23f6cdd7 Stephen Beaver
1361 eb3743d8 Stephen Beaver
			foreach ($schedules as $schedule) {
1362
				if ($schedule == "none") {
1363
					$schedlist[""] = $schedule;
1364
				} else {
1365
					$schedlist[$schedule] = $schedule;
1366 c6c71b36 xbipin
				}
1367 eb3743d8 Stephen Beaver
			}
1368 23f6cdd7 Stephen Beaver
1369 0f649c97 Phil Davis
			if ($grouping) {
1370 eb3743d8 Stephen Beaver
				$group->add(new Form_Select(
1371
					$pkga['fieldname'],
1372
					$pkga['fielddescr'],
1373
					$value,
1374
					$schedlist
1375
				))->setHelp(fixup_string($pkga['description']));
1376
			} else {
1377 23f6cdd7 Stephen Beaver
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1378
					$advanced->addInput(new Form_Select(
1379
						$pkga['fieldname'],
1380
						$pkga['fielddescr'],
1381
						$value,
1382
						$schedlist
1383
					))->setHelp(fixup_string($pkga['description']));
1384 f8ac4407 Phil Davis
				} else {
1385 23f6cdd7 Stephen Beaver
					$section->addInput(new Form_Select(
1386
						$pkga['fieldname'],
1387
						$pkga['fielddescr'],
1388
						$value,
1389
						$schedlist
1390
					))->setHelp(fixup_string($pkga['description']));
1391 f8ac4407 Phil Davis
				}
1392 eb3743d8 Stephen Beaver
			}
1393 f8ac4407 Phil Davis
1394 eb3743d8 Stephen Beaver
			break;
1395 1624b5f1 Marcello Coutinho
1396 eb3743d8 Stephen Beaver
		case "rowhelper":
1397 1624b5f1 Marcello Coutinho
1398 eb3743d8 Stephen Beaver
			$rowhelpername="row";
1399 1624b5f1 Marcello Coutinho
1400 eb3743d8 Stephen Beaver
				$rowcounter = 0;
1401
				$trc = 0;
1402
1403
				//Use assigned $a_pkg or create an empty array to enter loop
1404
				if (isset($a_pkg[$id][$rowhelpername])) {
1405
					$saved_rows=$a_pkg[$id][$rowhelpername];
1406
				} else {
1407
					$saved_rows[] = array();
1408
				}
1409
1410
				$numrows = count($saved_rows) - 1;
1411
1412
				foreach ($saved_rows as $row) {
1413
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1414
					$group->addClass('repeatable');
1415
1416
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1417
						unset($value);
1418 57fc02b1 Stephen Beaver
						$width = null;
1419
1420 eb3743d8 Stephen Beaver
						if ($rowhelper['value'] != "") {
1421
							$value = $rowhelper['value'];
1422
						}
1423
						$fieldname = $rowhelper['fieldname'];
1424
						$fielddescr = $rowhelper['fielddescr'];
1425 1624b5f1 Marcello Coutinho
1426 eb3743d8 Stephen Beaver
						// if user is editing a record, load in the data.
1427
						if (isset($id) && $a_pkg[$id]) {
1428
							$value = $row[$fieldname];
1429 f8ac4407 Phil Davis
						}
1430 eb3743d8 Stephen Beaver
1431
						$type = $rowhelper['type'];
1432 0f649c97 Phil Davis
						if ($type == "input" || $type == "password" || $type == "textarea") {
1433 6e5cec8e doktornotor
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1434
								$value = base64_decode($value);
1435
							}
1436
						}
1437 eb3743d8 Stephen Beaver
						$fieldname = $rowhelper['fieldname'];
1438
1439
						if ($rowhelper['size']) {
1440
							$size = $rowhelper['size'];
1441
						} else if ($pkga['size']) {
1442
							$size = $pkga['size'];
1443
						} else {
1444
							$size = "8";
1445
						}
1446
1447 57fc02b1 Stephen Beaver
						if ($rowhelper['width']) {
1448
							$width = $rowhelper['width'];
1449
						}
1450
1451
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, ($numrows == $rowcounter) ? $fielddescr:null, $width);
1452 eb3743d8 Stephen Beaver
1453
						$text = "";
1454
						$trc++;
1455 f8ac4407 Phil Davis
					}
1456 23f6cdd7 Stephen Beaver
1457 eb3743d8 Stephen Beaver
					// Delete row button
1458
					$group->add(new Form_Button(
1459
						'deleterow' . $rowcounter,
1460 5a2512b3 Stephen Beaver
						'Delete',
1461
						null,
1462
						'fa-trash'
1463 eb3743d8 Stephen Beaver
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1464
1465
					$rowcounter++;
1466
					$section->add($group);
1467
				}
1468
1469
			// Add row button
1470
			$section->addInput(new Form_Button(
1471
				'addrow',
1472 05e539af jim-p
				'Add',
1473
				null,
1474
				'fa-plus'
1475
			))->addClass('btn-success');
1476 eb3743d8 Stephen Beaver
1477
			break;
1478 23f6cdd7 Stephen Beaver
1479 f8ac4407 Phil Davis
	}
1480 55c846c4 Marcello Coutinho
1481 eb3743d8 Stephen Beaver
		if ($pkga['combinefields'] == "end") {
1482
			$group->add(new Form_StaticText(
1483
				null,
1484
				null
1485
			));
1486 23f6cdd7 Stephen Beaver
1487 0f649c97 Phil Davis
			if ($advanced) {
1488 eb3743d8 Stephen Beaver
				$advanced->add($group);
1489 0f649c97 Phil Davis
			} else {
1490 eb3743d8 Stephen Beaver
				$section->add($group);
1491 0f649c97 Phil Davis
			}
1492 23f6cdd7 Stephen Beaver
1493 eb3743d8 Stephen Beaver
			$grouping = false;
1494
		}
1495 23f6cdd7 Stephen Beaver
1496 eb3743d8 Stephen Beaver
	#increment counter
1497
	$i++;
1498
} // e-o-foreach field described in the XML
1499 f8ac4407 Phil Davis
1500 eb3743d8 Stephen Beaver
$form->add($section);
1501 23f6cdd7 Stephen Beaver
1502 eb3743d8 Stephen Beaver
$form->addGlobal(new Form_Input(
1503
	'id',
1504
	null,
1505
	'hidden',
1506
	$id
1507
));
1508 23f6cdd7 Stephen Beaver
1509 eb3743d8 Stephen Beaver
// If we created an advanced section, add it (and a button) to the form here
1510 0f649c97 Phil Davis
if (!empty($advanced)) {
1511 eb3743d8 Stephen Beaver
	$form->addGlobal(new Form_Button(
1512
		'showadv',
1513 827a3812 jim-p
		'Show Advanced Options',
1514
		null,
1515
		'fa-cog'
1516
	))->addClass('btn-info');
1517 23f6cdd7 Stephen Beaver
1518 eb3743d8 Stephen Beaver
	$form->add($advanced);
1519
}
1520
1521
print($form);
1522
1523
if ($pkg['note'] != "") {
1524 f78bbe16 Phil Davis
	print_info_box($pkg['note'], 'info');
1525 4cb4d820 Phil Davis
}
1526 eb3743d8 Stephen Beaver
1527 4cb4d820 Phil Davis
if ($pkg['custom_php_after_form_command']) {
1528 eb3743d8 Stephen Beaver
	eval($pkg['custom_php_after_form_command']);
1529 23f6cdd7 Stephen Beaver
}
1530
1531
if ($pkg['fields']['field'] != "") { ?>
1532
<script type="text/javascript">
1533
//<![CDATA[
1534 0f649c97 Phil Davis
	events.push(function() {
1535 23f6cdd7 Stephen Beaver
1536 eb3743d8 Stephen Beaver
	// Hide the advanced section
1537
	var advanced_visible = false;
1538
1539
	// Hide on page load
1540
	$('.advancedoptions').hide();
1541
1542 0bc61baa Stephen Beaver
	// Suppress "Delete row" button if there are fewer than two rows
1543
	checkLastRow();
1544
1545
	// Show advanced section if you click the showadv button
1546 eb3743d8 Stephen Beaver
	$('#showadv').prop('type', 'button');
1547
1548
	$("#showadv").click(function() {
1549
		advanced_visible = !advanced_visible;
1550
1551 0f649c97 Phil Davis
		if (advanced_visible) {
1552 eb3743d8 Stephen Beaver
			$('.advancedoptions').show();
1553
			$("#showadv").prop('value', 'Hide advanced Options');
1554 0f649c97 Phil Davis
		} else {
1555 eb3743d8 Stephen Beaver
			$('.advancedoptions').hide();
1556
			$("#showadv").prop('value', 'Show advanced Options');
1557
		}
1558 23f6cdd7 Stephen Beaver
	});
1559
1560 eb3743d8 Stephen Beaver
	// Call enablechange function
1561
	enablechange();
1562
});
1563
1564 23f6cdd7 Stephen Beaver
	function enablechange() {
1565
<?php
1566
	foreach ($pkg['fields']['field'] as $field) {
1567
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1568 3f98044a Francisco Cavalcante
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1569 23f6cdd7 Stephen Beaver
1570
			if (isset($field['enablefields'])) {
1571
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1572 3f98044a Francisco Cavalcante
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1573
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1574 23f6cdd7 Stephen Beaver
					echo "\t\t}\n";
1575 f8ac4407 Phil Davis
				}
1576 23f6cdd7 Stephen Beaver
			}
1577
1578
			if (isset($field['checkenablefields'])) {
1579
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1580 3f98044a Francisco Cavalcante
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1581
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
1582 23f6cdd7 Stephen Beaver
					echo "\t\t}\n";
1583 f8ac4407 Phil Davis
				}
1584 23f6cdd7 Stephen Beaver
			}
1585 55c846c4 Marcello Coutinho
1586 23f6cdd7 Stephen Beaver
			echo "\t}\n\telse {\n";
1587 7c061036 Scott Ullrich
1588 23f6cdd7 Stephen Beaver
			if (isset($field['enablefields'])) {
1589
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1590 3f98044a Francisco Cavalcante
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1591
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1592 23f6cdd7 Stephen Beaver
					echo "\t\t}\n";
1593
				}
1594
			}
1595 95736b54 Scott Ullrich
1596 23f6cdd7 Stephen Beaver
			if (isset($field['checkenablefields'])) {
1597
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1598 3f98044a Francisco Cavalcante
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1599
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1600 23f6cdd7 Stephen Beaver
					echo "\t\t}\n";
1601
				}
1602
			}
1603
1604
			echo "\t}\n";
1605 e11aa161 Warren Baker
		}
1606
	}
1607 23f6cdd7 Stephen Beaver
	?>
1608
	}
1609
//]]>
1610
</script>
1611 d47013e1 Scott Ullrich
1612 31d27c6c Scott Ullrich
<?php
1613 23f6cdd7 Stephen Beaver
}
1614
1615
include("foot.inc");