Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
127
/* If the first entry in the array is an empty <config/> tag, kill it.
128
 * See the following tickets for more:
129
 *  https://redmine.pfsense.org/issues/7624
130
 *  https://redmine.pfsense.org/issues/476
131
 */
132
if ($config['installedpackages'] &&
133
    (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) &&
134
    (empty($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0])) &&
135
    is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
136
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
137
}
138

    
139
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
140

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

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

    
149
if ($_POST) {
150
	$rows = 0;
151

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

    
167
	if ($pkg['custom_php_validation_command']) {
168
		eval($pkg['custom_php_validation_command']);
169
	}
170

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

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

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

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

    
241
			/* If the user supplied an ID and it eixsts, or if id=0
242
			 * and the settings are invalid, overwrite.
243
			 * See https://redmine.pfsense.org/issues/7624
244
			 */
245
			if (isset($id) && ($a_pkg[$id] ||
246
			   (($id == 0) && !is_array($a_pkg[$id])) )) {
247
				$a_pkg[$id] = $pkgarr;
248
			} else {
249
				$a_pkg[] = $pkgarr;
250
			}
251

    
252
			write_config($pkg['addedit_string']);
253
			// late running code
254
			if ($pkg['custom_add_php_command_late'] != "") {
255
				eval($pkg['custom_add_php_command_late']);
256
			}
257

    
258
			if (isset($pkg['filter_rules_needed'])) {
259
				filter_configure();
260
			}
261

    
262
			// resync the configuration file code if defined.
263
			if ($pkg['custom_php_resync_config_command'] != "") {
264
				eval($pkg['custom_php_resync_config_command']);
265
			}
266

    
267
			parse_package_templates();
268

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

    
274
			/* if restart_command is defined, restart w/ this */
275
			if ($pkg['restart_command'] != "") {
276
				exec($pkg['restart_command'] . ">/dev/null 2&>1");
277
			}
278

    
279
			if ($pkg['aftersaveredirect'] != "") {
280
				pfSenseHeader($pkg['aftersaveredirect']);
281
			} elseif (!$pkg['adddeleteeditpagefields']) {
282
				pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
283
			} elseif (!$pkg['preoutput']) {
284
				pfSenseHeader("pkg.php?xml=" . $xml);
285
			}
286
			exit;
287
		} else {
288
			$get_from_post = true;
289
		}
290
	} elseif (!$input_errors) {
291
		exit;
292
	}
293
}
294

    
295

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

    
302
	// Substitute everything inbetween with our new classes
303
	if ($t && $c && (($c - $t) < 200)) {
304
		return(substr_replace($text, ' class="table table-striped table-hover table-condensed"', $t, ($c - $t)));
305
	}
306
}
307

    
308
/*
309
 * ROW helper function. Creates one element in the row from a PHP table by adding
310
 * the specified element to $group
311
 */
312
function display_row($trc, $value, $fieldname, $type, $rowhelper, $description, $ewidth = null) {
313
	global $text, $group, $config;
314

    
315
	switch ($type) {
316
		case "input":
317
			$inpt = new Form_Input(
318
				$fieldname . $trc,
319
				null,
320
				'text',
321
				$value
322
			);
323

    
324
			$inpt->setHelp($description);
325

    
326
			if ($ewidth) {
327
				$inpt->setWidth($ewidth);
328
			}
329

    
330
			$group->add($inpt);
331
			break;
332
		case "checkbox":
333
			$group->add(new Form_Checkbox(
334
				$fieldname . $trc,
335
				null,
336
				null,
337
				$value,
338
				'ON'
339
			))->setHelp($description);
340

    
341
			break;
342
		case "password":
343
			$group->add(new Form_Input(
344
				$fieldname . $trc,
345
				null,
346
				'password',
347
				$value
348
			))->setHelp($description);
349
			break;
350
		case "textarea":
351
			$group->add(new Form_Textarea(
352
				$fieldname . $trc,
353
				null,
354
				$value
355
			))->setHelp($description);
356

    
357
			break;
358
		case "select":
359
			$options = array();
360
			foreach ($rowhelper['options']['option'] as $rowopt) {
361
				$options[$rowopt['value']] = $rowopt['name'];
362
			}
363

    
364
			$grp = new Form_Select(
365
				$fieldname . $trc,
366
				null,
367
				$value,
368
				$options
369
			);
370

    
371
			$grp->setHelp($description);
372

    
373
			if ($ewidth) {
374
				$grp->setWidth($ewidth);
375
			}
376

    
377
			$group->add($grp);
378

    
379
			break;
380
		case "interfaces_selection":
381
			$size = ($size ? "size=\"{$size}\"" : '');
382
			$multiple = '';
383
			if (isset($rowhelper['multiple'])) {
384
				$multiple = "multiple";
385
			}
386
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
387
			$ifaces = get_configured_interface_with_descr();
388
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
389
			if (!empty($additional_ifaces)) {
390
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
391
			}
392

    
393
			if (is_array($value)) {
394
				$values = $value;
395
			} else {
396
				$values = explode(',', $value);
397
			}
398

    
399
			$ifaces["lo0"] = "loopback";
400
			$options = array();
401
			$selected = array();
402

    
403
			foreach ($ifaces as $ifname => $iface) {
404
				$options[$ifname] = $iface;
405

    
406
				if (in_array($ifname, $values)) {
407
					array_push($selected, $ifname);
408
				}
409
			}
410

    
411
			$group->add(new Form_Select(
412
				$fieldname . $trc,
413
				null,
414
				($multiple) ? $selected:$selected[0],
415
				$options,
416
				$multiple
417
			))->setHelp($description);
418

    
419
			//echo "</select>\n";
420
			break;
421
		case "select_source":
422
			$options = array();
423
			$selected = array();
424

    
425
			if (isset($rowhelper['show_disable_value'])) {
426
				$options[$rowhelper['show_disable_value']] = $rowhelper['show_disable_value'];
427
			}
428

    
429
			$source_url = $rowhelper['source'];
430
			eval("\$pkg_source_txt = &$source_url;");
431

    
432
			foreach ($pkg_source_txt as $opt) {
433
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
434
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
435
				$options[$source_value] = $source_name;
436

    
437
				if ($source_value == $value) {
438
					array_push($selected, $value);
439
				}
440
			}
441

    
442
			$group->add(new Form_Select(
443
				$fieldname . $trc,
444
				null,
445
				($multiple) ? $selected:$selected[0],
446
				$options,
447
				$multiple
448
			))->setHelp($description);
449

    
450
			break;
451
	}
452
}
453

    
454
function fixup_string($string) {
455
	global $config;
456
	// fixup #1: $myurl -> http[s]://ip_address:port/
457
	$https = "";
458
	$port = $config['system']['webguiport'];
459
	if ($port != "443" and $port != "80") {
460
		$urlport = ":" . $port;
461
	} else {
462
		$urlport = "";
463
	}
464

    
465
	if ($config['system']['webgui']['protocol'] == "https") {
466
		$https = "s";
467
	}
468
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
469
	$newstring = str_replace("\$myurl", $myurl, $string);
470
	$string = $newstring;
471
	// fixup #2: $wanip
472
	$curwanip = get_interface_ip();
473
	$newstring = str_replace("\$wanip", $curwanip, $string);
474
	$string = $newstring;
475
	// fixup #3: $lanip
476
	$lancfg = $config['interfaces']['lan'];
477
	$lanip = $lancfg['ipaddr'];
478
	$newstring = str_replace("\$lanip", $lanip, $string);
479
	$string = $newstring;
480
	// fixup #4: fix'r'up here.
481
	return $newstring;
482
}
483

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

    
529
			/* replace cr's */
530
			$template_text = str_replace("\\n", "\n", $template_text);
531

    
532
			/* write out new template file */
533
			$fout = fopen($filename, "w");
534
			fwrite($fout, $template_text);
535
			fclose($fout);
536
		}
537
	}
538
}
539

    
540
//breadcrumb
541
if ($pkg['title'] != "") {
542
	if (!$only_edit) {
543
		$pkg['title'] = $pkg['title'] . '/Edit';
544
	}
545

    
546
	if (strpos($pkg['title'], '/')) {
547
		$title = explode('/', $pkg['title']);
548

    
549
		foreach ($title as $subtitle) {
550
			$pgtitle[] = gettext($subtitle);
551
			$pglinks[] = ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']);
552
		}
553
	} else {
554
		$pgtitle = array(gettext("Package"), gettext($pkg['title']));
555
		$pglinks = array("", ((($subtitle == "Edit") || (strlen($pkg['menu'][0]['url']) == 0)) ? "@self" : $pkg['menu'][0]['url']));
556
	}
557
} else {
558
	$pgtitle = array(gettext("Package"), gettext("Editor"));
559
	$pglinks = array("", "@self");
560
}
561

    
562
// Create any required tabs
563
if ($pkg['tabs'] != "") {
564
	$tab_array = array();
565
	foreach ($pkg['tabs']['tab'] as $tab) {
566
		if ($tab['tab_level']) {
567
			$tab_level = $tab['tab_level'];
568
		} else {
569
			$tab_level = 1;
570
		}
571

    
572
		if (isset($tab['active'])) {
573
			$active = true;
574
			$pgtitle[] = $tab['text'] ;
575
			$pglinks[] = ((strlen($tab['url']) > 0) ? $tab['url'] : "@self");
576
		} else {
577
			$active = false;
578
		}
579

    
580
		if (isset($tab['no_drop_down'])) {
581
			$no_drop_down = true;
582
		}
583

    
584
		$urltmp = "";
585
		if ($tab['url'] != "") {
586
			$urltmp = $tab['url'];
587
		}
588

    
589
		if ($tab['xml'] != "") {
590
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
591
		}
592

    
593
		$addresswithport = getenv("HTTP_HOST");
594
		$colonpos = strpos($addresswithport, ":");
595

    
596
		if ($colonpos !== False) {
597
			//my url is actually just the IP address of the pfsense box
598
			$myurl = substr($addresswithport, 0, $colonpos);
599
		} else {
600
			$myurl = $addresswithport;
601
		}
602

    
603
		// eval url so that above $myurl item can be processed if need be.
604
		$url = str_replace('$myurl', $myurl, $urltmp);
605

    
606
		$tab_array[$tab_level][] = array(
607
			$tab['text'],
608
			$active,
609
			$url
610
		);
611
	}
612

    
613
	ksort($tab_array);
614
}
615

    
616
include("head.inc");
617
if ($pkg['custom_php_after_head_command']) {
618
	eval($pkg['custom_php_after_head_command']);
619
}
620
if (isset($tab_array)) {
621
	foreach ($tab_array as $tabid => $tab) {
622
		display_top_tabs($tab); //, $no_drop_down, $tabid);
623
	}
624
}
625

    
626
// Start of page display
627
if ($input_errors) {
628
	print_input_errors($input_errors);
629
}
630

    
631
if ($savemsg) {
632
	print_info_box($savemsg, 'success');
633
}
634

    
635
$cols = 0;
636
$savevalue = gettext("Save");
637
if ($pkg['savetext'] != "") {
638
	$savevalue = $pkg['savetext'];
639
}
640

    
641
$savehelp = "";
642
if ($pkg['savehelp'] != "") {
643
	$savehelp = $pkg['savehelp'];
644
}
645

    
646
$saveicon = "fa-save";
647
if ($pkg['saveicon'] != "") {
648
	$saveicon = $pkg['saveicon'];
649
}
650

    
651
$savebtnclass = "btn-primary";
652
if ($pkg['savebtnclass'] != "") {
653
	$savebtnclass = $pkg['savebtnclass'];
654
}
655

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

    
658
$savebutton = new Form_Button(
659
	'submit',
660
	$savevalue,
661
	null,
662
	$saveicon
663
);
664
$savebutton->addClass($savebtnclass);
665

    
666
if ($savehelp) {
667
	$savebutton->setHelp($savehelp);
668
}
669

    
670
$form = new Form($savebutton);
671

    
672
$form->addGlobal(new Form_Input(
673
	'xml',
674
	null,
675
	'hidden',
676
	$xml
677
));
678

    
679
/* If a package's XML has <advanced_options/> configured, then setup
680
 * the section for the fields that have <advancedfield/> set.
681
 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
682
 * These advanced fields are not normally configured and generally left to default to 'default settings'.
683
 */
684

    
685
if ($pkg['advanced_options'] == "enabled") {
686
	$advfield_count = 0;
687
	$advanced = new Form_Section("Advanced Features");
688
	$advanced->addClass('advancedoptions');
689
}
690

    
691
$js_array = array();
692

    
693
// Now loop through all of the fields defined in the XML
694
if (!is_array($pkg['fields']['field'])) {
695
	$pkg['fields']['field'] = array();
696
}
697
foreach ($pkg['fields']['field'] as $pkga) {
698

    
699
	$action = "";
700
	$uid = "";
701

    
702
	if ($pkga['type'] == "sorting") {
703
		continue;
704
	}
705

    
706
	// Generate a new section
707
	if ($pkga['type'] == "listtopic") {
708
		if (isset($pkga['advancedfield']) && isset($advfield_count)) {
709
			$advanced->addInput(new Form_StaticText(
710
				strip_tags($pkga['name']),
711
				null
712
			));
713

    
714
			$advfield_count++;
715
		}  else {
716
			if (isset($section)) {
717
				$form->add($section);
718
			}
719

    
720
			if (isset($pkga['collapse'])) {
721
				$uid = uniqid("section");
722

    
723
				$action = COLLAPSIBLE;
724

    
725
				if ($pkga['collapse'] == "open") {
726
					$action |= SEC_OPEN;
727
				} else {
728
					$action |= SEC_CLOSED;
729
				}
730
			}
731

    
732
			$section = new Form_Section(strip_tags($pkga['name']), $uid, $action);
733
		}
734

    
735
		continue;
736
	}
737

    
738
	// 'begin' starts a form group. ('end' ends it)
739
	if ($pkga['combinefields'] == "begin") {
740
		$group = new Form_Group(strip_tags($pkga['fielddescr']));
741
		$grouping = true;
742
	}
743

    
744
	$size = "";
745
	$colspan="";
746

    
747
	// if user is editing a record, load in the data.
748
	$fieldname = $pkga['fieldname'];
749
	unset($value);
750
	if ($get_from_post) {
751
		$value = $_POST[$fieldname];
752
		if (is_array($value)) {
753
			$value = implode(',', $value);
754
		}
755
	} else {
756
		if (isset($id) && isset($a_pkg[$id][$fieldname])) {
757
			$value = $a_pkg[$id][$fieldname];
758
		} else {
759
			if (isset($pkga['default_value'])) {
760
				$value = $pkga['default_value'];
761
			}
762
		}
763
	}
764

    
765
	// If we get here but have no $section, the package config file probably had no listtopic field
766
	// We can create a section with a generic name to fix that
767
	if (!$section) {
768
		$section = new Form_Section('General Options');
769
	}
770

    
771
	switch ($pkga['type']) {
772
		// Create an input element. The format is slightly different depending on whether we are composing a group,
773
		// section, or advanced section. This is true for every element type
774
		case "input":
775
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
776
				$value = base64_decode($value);
777
			}
778

    
779
			$grp = new Form_Input(
780
					$pkga['fieldname'],
781
					$pkga['fielddescr'],
782
					'text',
783
					$value
784
				);
785

    
786
			$grp->setHelp($pkga['description']);
787

    
788
			if ($pkga['width']) {
789
				$grp->setWidth($pkga['width']);
790
			}
791

    
792
			if ($grouping) {
793
				$group->add($grp);
794
			} else {
795
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
796
					$advanced->addInput($grp);
797
				} else {
798
					$section->addInput($grp);
799
				}
800
			}
801

    
802
			break;
803

    
804
		case "password":
805
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
806
				$value = base64_decode($value);
807
			}
808

    
809
			// Create a password element
810
			if ($grouping) {
811
				$group->add(new Form_Input(
812
					$pkga['fieldname'],
813
					$pkga['fielddescr'],
814
					'password',
815
					$value
816
				))->setHelp($pkga['description']);
817
			} else {
818
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
819
					$advanced->addInput(new Form_Input(
820
						$pkga['fieldname'],
821
						$pkga['fielddescr'],
822
						'password',
823
						$value
824
					))->setHelp($pkga['description']);
825
				} else {
826
					$section->addInput(new Form_Input(
827
						$pkga['fieldname'],
828
						$pkga['fielddescr'],
829
						'password',
830
						$value
831
					))->setHelp($pkga['description']);
832
				}
833
			}
834

    
835
			break;
836

    
837
		case "info":
838
			// If the info contains a table we should detect and Bootstrap it
839

    
840
			if (strpos($pkga['description'], '<table') !== FALSE) {
841
				$info = bootstrapTable($pkga['description']);
842
			} else {
843
				$info = $pkga['description'];
844
			}
845

    
846
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
847
				$advanced->addInput(new Form_StaticText(
848
					strip_tags($pkga['fielddescr']),
849
					$info
850
				));
851
			} else {
852
				$section->addInput(new Form_StaticText(
853
					strip_tags($pkga['fielddescr']),
854
					$info
855
				));
856
			}
857

    
858
			break;
859

    
860
		case "select":
861
			// Create a select element
862
			$optionlist = array();
863
			$selectedlist = array();
864

    
865
			$fieldname = $pkga['fieldname'];
866

    
867
			if (isset($pkga['multiple'])) {
868
				$multiple = 'multiple';
869
				$items = explode(',', $value);
870
				$fieldname .= "[]";
871
			} else {
872
				$multiple = '';
873
				$items = array($value);
874
			}
875

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

    
878
			foreach ($pkga['options']['option'] as $opt) {
879
				$optionlist[$opt['value']] = $opt['name'];
880

    
881
				if (in_array($opt['value'], $items)) {
882
					array_push($selectedlist, $opt['value']);
883
				}
884
			}
885

    
886
			if (isset($pkga['advancedfield']) && isset($advfield_count)) {
887
				$function = $grouping ? $advanced->add:$advanced->addInput;
888
			} else {
889
				$function = ($grouping) ? $section->add:$section->addInput;
890
			}
891

    
892
			$grp = new Form_Select(
893
						$pkga['fieldname'],
894
						strip_tags($pkga['fielddescr']),
895
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
896
						$optionlist,
897
						isset($pkga['multiple'])
898
					);
899

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

    
902
			if ($pkga['width']) {
903
				$grp->setWidth($pkga['width']);
904
			}
905

    
906
			if ($grouping) {
907
				$group->add($grp);
908
			} else {
909
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
910
					$advanced->addInput($grp);
911
				} else {
912
					$section->addInput($grp);
913
				}
914
			}
915

    
916
			break;
917

    
918
		case "select_source":
919

    
920
			if (isset($pkga['multiple'])) {
921
				$items = explode(',', $value);
922
				$fieldname .= "[]";
923
			} else {
924
				$items = array($value);
925
			}
926

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

    
929
			$source_url = $pkga['source'];
930
			eval("\$pkg_source_txt = &$source_url;");
931

    
932
			#check if show disable option is present on xml
933
			if (!is_array($pkg_source_txt)) {
934
				$pkg_source_txt = array();
935
			}
936
			if (isset($pkga['show_disable_value'])) {
937
				array_push($pkg_source_txt,
938
					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']));
939
			}
940

    
941
			$srcoptions = array();
942
			$srcselected = array();
943

    
944
			foreach ($pkg_source_txt as $opt) {
945
				$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
946
				$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
947
				$srcoptions[$source_value] = $source_name;
948

    
949
				if (in_array($source_value, $items)) {
950
					array_push($srcselected, $source_value);
951
				}
952
			}
953

    
954
			$descr = (isset($pkga['description'])) ? $pkga['description'] : "";
955
			if ($grouping) {
956
				$group->add(new Form_Select(
957
					$pkga['fieldname'],
958
					strip_tags($pkga['fielddescr']),
959
					isset($pkga['multiple']) ? $srcselected:$srcselected[0],
960
					$srcoptions,
961
					isset($pkga['multiple'])
962
				))->setHelp($descr)->setOnchange($onchange);
963
			} else {
964
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
965
					$advanced->addInput(new Form_Select(
966
						$pkga['fieldname'],
967
						strip_tags($pkga['fielddescr']),
968
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
969
						$srcoptions,
970
						isset($pkga['multiple'])
971
					))->setHelp($descr)->setOnchange($onchange);
972
				} else {
973
					$section->addInput(new Form_Select(
974
						$pkga['fieldname'],
975
						strip_tags($pkga['fielddescr']),
976
						isset($pkga['multiple']) ? $srcselected:$srcselected[0],
977
						$srcoptions,
978
						isset($pkga['multiple'])
979
					))->setHelp($descr)->setOnchange($onchange);
980
				}
981
			}
982

    
983
			break;
984

    
985
		case "vpn_selection" :
986
			$vpnlist = array();
987

    
988
			foreach ($config['ipsec']['phase1'] as $vpn) {
989
				$vpnlist[$vpn['descr']] = $vpn['descr'];
990

    
991
			}
992

    
993
			if ($grouping) {
994
				$group->add(new Form_Select(
995
					$pkga['fieldname'],
996
					null,
997
					false,
998
					$vpnlist
999
				))->setHelp(fixup_string($pkga['description']));
1000
			} else {
1001
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1002
					$advanced->addInput(new Form_Select(
1003
						$pkga['fieldname'],
1004
						null,
1005
						false,
1006
						$vpnlist
1007
					))->setHelp(fixup_string($pkga['description']));
1008
				} else {
1009
					$section->addInput(new Form_Select(
1010
						$pkga['fieldname'],
1011
						null,
1012
						false,
1013
						$vpnlist
1014
					))->setHelp(fixup_string($pkga['description']));
1015
				}
1016
			}
1017

    
1018
			break;
1019

    
1020
		// Create a checkbox element
1021
		case "checkbox":
1022
			$onchange = (isset($pkga['onchange']) ? "{$pkga['onchange']}" : '');
1023
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
1024
				$onclick = 'javascript:enablechange();';
1025
			} else {
1026
				$onclick = '';
1027
			}
1028

    
1029
			if ($grouping) {
1030
				$group->add(new Form_Checkbox(
1031
					$pkga['fieldname'],
1032
					$pkga['fielddescr'],
1033
					fixup_string($pkga['description']),
1034
					($value == "on"),
1035
					'on'
1036
				))->setOnclick($onclick)
1037
				  ->setOnchange($onchange)
1038
				  ->setHelp($pkga['sethelp']);
1039
			} else {
1040
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1041
					$advanced->addInput(new Form_Checkbox(
1042
						$pkga['fieldname'],
1043
						$pkga['fielddescr'],
1044
						fixup_string($pkga['description']),
1045
						($value == "on"),
1046
						'on'
1047
					))->setOnclick($onclick)
1048
					  ->setOnchange($onchange)
1049
					  ->setHelp($pkga['sethelp']);
1050
				} else {
1051
					$section->addInput(new Form_Checkbox(
1052
						$pkga['fieldname'],
1053
						$pkga['fielddescr'],
1054
						fixup_string($pkga['description']),
1055
						($value == "on"),
1056
						'on'
1057
					))->setOnclick($onclick)
1058
					  ->setOnchange($onchange)
1059
					  ->setHelp($pkga['sethelp']);
1060
				}
1061
			}
1062

    
1063
			break;
1064

    
1065
		// Create a textarea element
1066
		case "textarea":
1067
			$rows = $cols = 0;
1068

    
1069
			if ($pkga['rows']) {
1070
				$rows = $pkga['rows'];
1071
			}
1072
			if ($pkga['cols']) {
1073
				$cols = $pkga['cols'];
1074
			}
1075

    
1076
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1077
				$value = base64_decode($value);
1078
			}
1079

    
1080
			$grp = new Form_Textarea(
1081
					$pkga['fieldname'],
1082
					$pkga['fielddescr'],
1083
					$value
1084
			);
1085

    
1086
			$grp->setHelp(fixup_string($pkga['description']));
1087

    
1088
			if ($rows > 0) {
1089
				$grp->setRows($rows);
1090
			}
1091

    
1092
			if ($cols > 0) {
1093
				$grp->setCols($cols);
1094
			}
1095

    
1096
			if ($pkga['wrap'] == "off") {
1097
				$grp->setAttribute("wrap", "off");
1098
				$grp->setAttribute("style", "white-space:nowrap; width: auto;");
1099
			} else {
1100
				$grp->setAttribute("style", "width: auto;");
1101
			}
1102

    
1103
			if ($grouping) {
1104
				$group->add($grp);
1105
			} else {
1106
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1107
					$advanced->addInput($grp);
1108
				} else {
1109
					$section->addInput($grp);
1110
				}
1111
			}
1112

    
1113
			break;
1114

    
1115
		case "aliases":
1116

    
1117
			// Use xml tag <typealiases> to filter type aliases
1118
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1119
			$fieldname = $pkga['fieldname'];
1120
			$a_aliases = &$config['aliases']['alias'];
1121
			$addrisfirst = 0;
1122
			$aliasesaddr = "";
1123

    
1124
			if (isset($a_aliases)) {
1125
				if (!empty($pkga['typealiases'])) {
1126
					foreach ($a_aliases as $alias) {
1127
						if ($alias['type'] == $pkga['typealiases']) {
1128
							if ($addrisfirst == 1) {
1129
								$aliasesaddr .= ",";
1130
							}
1131
							$aliasesaddr .= "'" . $alias['name'] . "'";
1132
							$addrisfirst = 1;
1133
						}
1134
					}
1135
				} else {
1136
					foreach ($a_aliases as $alias) {
1137
						if ($addrisfirst == 1) {
1138
							$aliasesaddr .= ",";
1139
						}
1140
						$aliasesaddr .= "'" . $alias['name'] . "'";
1141
						$addrisfirst = 1;
1142
					}
1143
				}
1144
			}
1145

    
1146
			$grp = new Form_Input(
1147
					$pkga['fieldname'],
1148
					$pkga['fielddescr'],
1149
					'text',
1150
					$value
1151
				);
1152

    
1153
			$grp->setHelp($pkga['description']);
1154

    
1155
			if ($pkga['width']) {
1156
				$grp->setWidth($pkga['width']);
1157
			}
1158

    
1159
			if ($grouping) {
1160
				$group->add($grp);
1161
			} else {
1162
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1163
					$advanced->addInput($grp);
1164
				} else {
1165
					$section->addInput($grp);
1166
				}
1167
			}
1168

    
1169
			$script = "<script type='text/javascript'>\n";
1170
			$script .= "//<![CDATA[\n";
1171
			$script .= "events.push(function(){\n";
1172
			$script .= "	var aliasarray = new Array({$aliasesaddr})\n";
1173
			$script .= "	$('#' + '{$fieldname}').autocomplete({\n";
1174
			$script .= "		source: aliasarray\n";
1175
			$script .= "	})\n";
1176
			$script .= "});\n";
1177
			$script .= "//]]>\n";
1178
			$script .= "</script>";
1179

    
1180
			echo $script;
1181

    
1182
			break;
1183

    
1184
		case "interfaces_selection":
1185
			$ips = array();
1186
			$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
1187
			if (is_array($config['interfaces'])) {
1188
				foreach ($config['interfaces'] as $iface_key=>$iface_value) {
1189
					if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
1190
						$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
1191
						if (isset($pkga['showips'])) {
1192
							$iface_description .= " address";
1193
						}
1194
						$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
1195
					}
1196
				}
1197
			}
1198

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

    
1225
			sort($ips);
1226
			if (isset($pkga['showlistenall'])) {
1227
				array_unshift($ips, array('ip' => gettext('All'), 'description' => gettext('Listen on All interfaces/ip addresses ')));
1228
			}
1229

    
1230
			if (!preg_match("/$interface_regex/", "loopback")) {
1231
				$loopback_text = gettext("loopback");
1232
				$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (" . $loopback_text . ")" : $loopback_text);
1233
				array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
1234
			}
1235

    
1236
			#show interfaces array on gui
1237
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
1238
			$multiple = '';
1239
			$fieldname = $pkga['fieldname'];
1240
			if (isset($pkga['multiple'])) {
1241
				$fieldname .= '[]';
1242
				$multiple = 'multiple';
1243
			}
1244

    
1245
			$selectedlist = array();
1246
			$optionlist = array();
1247

    
1248
			if (is_array($value)) {
1249
				$values = $value;
1250
			} else {
1251
				$values = explode(',', $value);
1252
			}
1253

    
1254
			foreach ($ips as $iface) {
1255
				if (in_array($iface['ip'], $values)) {
1256
					array_push($selectedlist, $iface['ip']);
1257
				}
1258

    
1259
				$optionlist[$iface['ip']] = $iface['description'];
1260
			}
1261

    
1262
			if ($grouping) {
1263
				$group->add(new Form_Select(
1264
					$pkga['fieldname'],
1265
					$pkga['fielddescr'],
1266
					isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1267
					$optionlist,
1268
					isset($pkga['multiple'])
1269
				))->setHelp($pkga['description']);
1270
			} else {
1271
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1272
					$advanced->addInput(new Form_Select(
1273
						$pkga['fieldname'],
1274
						$pkga['fielddescr'],
1275
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1276
						$optionlist,
1277
						isset($pkga['multiple'])
1278
					))->setHelp($pkga['description']);
1279
				} else {
1280
					$section->addInput(new Form_Select(
1281
						$pkga['fieldname'],
1282
						$pkga['fielddescr'],
1283
						isset($pkga['multiple']) ? $selectedlist:$selectedlist[0],
1284
						$optionlist,
1285
						isset($pkga['multiple'])
1286
					))->setHelp($pkga['description']);
1287
				}
1288
			}
1289

    
1290
			break;
1291

    
1292
		// Create radio button
1293
		case "radio":
1294
			if ($grouping) {
1295
				$group->add(new Form_Checkbox(
1296
					$pkga['fieldname'],
1297
					$pkga['fielddescr'],
1298
					fixup_string($pkga['description']),
1299
					($value == "on"),
1300
					'on'
1301
				))->displayAsRadio();
1302
			} else {
1303
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1304
					$advanced->addInput(new Form_Checkbox(
1305
						$pkga['fieldname'],
1306
						$pkga['fielddescr'],
1307
						fixup_string($pkga['description']),
1308
						($value == "on"),
1309
						'on'
1310
					))->displayAsRadio();
1311
				} else {
1312
					$section->addInput(new Form_Checkbox(
1313
						$pkga['fieldname'],
1314
						$pkga['fielddescr'],
1315
						fixup_string($pkga['description']),
1316
						($value == "on"),
1317
						'on'
1318
					))->displayAsRadio();
1319
				}
1320
			}
1321

    
1322
			break;
1323

    
1324
		// Create form button
1325
		case "button":
1326
			$newbtnicon = "fa-save";
1327
			if ($pkga['buttonicon'] != "") {
1328
				$newbtnicon = $pkga['buttonicon'];
1329
			}
1330
			$newbtnclass = "btn-primary";
1331
			if ($pkga['buttonclass'] != "") {
1332
				$newbtnclass = $pkga['buttonclass'];
1333
			}
1334

    
1335
			$newbtn = new Form_Button(
1336
				$pkga['fieldname'],
1337
				$pkga['fieldname'],
1338
				null,
1339
				$newbtnicon
1340
			);
1341
			$newbtn->addClass($newbtnclass);
1342

    
1343
			if ($grouping) {
1344
				$group->add(new Form_StaticText(
1345
					null,
1346
					$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1347
				));
1348
			} else {
1349
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1350
					$advanced->addInput(new Form_StaticText(
1351
						null,
1352
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1353
					));
1354
				} else {
1355
					$section->addInput(new Form_StaticText(
1356
						null,
1357
						$newbtn . '<br />' . '<div class="help-block">' . fixup_string($pkga['description']) . '</div>'
1358
					));
1359
				}
1360
			}
1361

    
1362
			break;
1363

    
1364
		case "schedule_selection":
1365

    
1366
			$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
1367
			$schedules = array();
1368
			$schedules[] = "none";
1369
			if (is_array($config['schedules']['schedule'])) {
1370
				foreach ($config['schedules']['schedule'] as $schedule) {
1371
					if ($schedule['name'] != "") {
1372
						$schedules[] = $schedule['name'];
1373
					}
1374
				}
1375
			}
1376

    
1377
			foreach ($schedules as $schedule) {
1378
				if ($schedule == "none") {
1379
					$schedlist[""] = $schedule;
1380
				} else {
1381
					$schedlist[$schedule] = $schedule;
1382
				}
1383
			}
1384

    
1385
			if ($grouping) {
1386
				$group->add(new Form_Select(
1387
					$pkga['fieldname'],
1388
					$pkga['fielddescr'],
1389
					$value,
1390
					$schedlist
1391
				))->setHelp(fixup_string($pkga['description']));
1392
			} else {
1393
				if (isset($pkga['advancedfield']) && isset($advfield_count)) {
1394
					$advanced->addInput(new Form_Select(
1395
						$pkga['fieldname'],
1396
						$pkga['fielddescr'],
1397
						$value,
1398
						$schedlist
1399
					))->setHelp(fixup_string($pkga['description']));
1400
				} else {
1401
					$section->addInput(new Form_Select(
1402
						$pkga['fieldname'],
1403
						$pkga['fielddescr'],
1404
						$value,
1405
						$schedlist
1406
					))->setHelp(fixup_string($pkga['description']));
1407
				}
1408
			}
1409

    
1410
			break;
1411

    
1412
		case "rowhelper":
1413

    
1414
			$rowhelpername="row";
1415

    
1416
				$rowcounter = 0;
1417
				$trc = 0;
1418

    
1419
				//Use assigned $a_pkg or create an empty array to enter loop
1420
				if (isset($a_pkg[$id][$rowhelpername])) {
1421
					$saved_rows=$a_pkg[$id][$rowhelpername];
1422
				} else {
1423
					$saved_rows[] = array();
1424
				}
1425

    
1426
				$numrows = count($saved_rows) - 1;
1427

    
1428
				foreach ($saved_rows as $row) {
1429
					$group = new Form_Group(($rowcounter == 0) ? $pkga['fielddescr']:null);
1430
					$group->addClass('repeatable');
1431

    
1432
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
1433
						unset($value);
1434
						$width = null;
1435

    
1436
						$fieldname = $rowhelper['fieldname'];
1437
						$fielddescr = $rowhelper['fielddescr'];
1438

    
1439
						// If input validation failed, read the value from the POST data so that the user's input is not lost
1440
						if ($get_from_post && isset($_POST[$fieldname.$rowcounter])) {
1441
							$value = $_POST[$fieldname.$rowcounter];
1442
						} elseif (isset($id) && $a_pkg[$id]) {
1443
							$value = $row[$fieldname];
1444
						} elseif ($rowhelper['value'] != "") {
1445
							$value = $rowhelper['value'];
1446
						}
1447

    
1448
						$type = $rowhelper['type'];
1449
						if ($type == "input" || $type == "password" || $type == "textarea") {
1450
							if (($rowhelper['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
1451
								$value = base64_decode($value);
1452
							}
1453
						}
1454

    
1455

    
1456
						if ($rowhelper['size']) {
1457
							$size = $rowhelper['size'];
1458
						} else if ($pkga['size']) {
1459
							$size = $pkga['size'];
1460
						} else {
1461
							$size = "8";
1462
						}
1463

    
1464
						if ($rowhelper['width']) {
1465
							$width = $rowhelper['width'];
1466
						}
1467

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

    
1470
						$text = "";
1471
						$trc++;
1472
					}
1473

    
1474
					// Delete row button
1475
					$group->add(new Form_Button(
1476
						'deleterow' . $rowcounter,
1477
						'Delete',
1478
						null,
1479
						'fa-trash'
1480
					))->removeClass('btn-primary')->addClass('btn-warning btn-sm');
1481

    
1482
					$rowcounter++;
1483
					$section->add($group);
1484
				}
1485

    
1486
			// Add row button
1487
			$section->addInput(new Form_Button(
1488
				'addrow',
1489
				'Add',
1490
				null,
1491
				'fa-plus'
1492
			))->addClass('btn-success');
1493

    
1494
			break;
1495

    
1496
	}
1497

    
1498
		if ($pkga['combinefields'] == "end") {
1499
			$group->add(new Form_StaticText(
1500
				null,
1501
				null
1502
			));
1503

    
1504
			if ($advanced) {
1505
				$advanced->add($group);
1506
			} else {
1507
				$section->add($group);
1508
			}
1509

    
1510
			$grouping = false;
1511
		}
1512

    
1513
	#increment counter
1514
	$i++;
1515
} // e-o-foreach field described in the XML
1516

    
1517
if ($section) {
1518
	$form->add($section);
1519
}
1520

    
1521
$form->addGlobal(new Form_Input(
1522
	'id',
1523
	null,
1524
	'hidden',
1525
	$id
1526
));
1527

    
1528
// If we created an advanced section, add it (and a button) to the form here
1529
if (!empty($advanced)) {
1530
	$form->addGlobal(new Form_Button(
1531
		'showadv',
1532
		'Show Advanced Options',
1533
		null,
1534
		'fa-cog'
1535
	))->setAttribute('type','button')->addClass('btn-info');
1536

    
1537
	$form->add($advanced);
1538
}
1539

    
1540
print($form);
1541

    
1542
if ($pkg['note'] != "") {
1543
	print_info_box($pkg['note'], 'info');
1544
}
1545

    
1546
if ($pkg['custom_php_after_form_command']) {
1547
	eval($pkg['custom_php_after_form_command']);
1548
}
1549

    
1550

    
1551
$hidemsg = gettext("Show Advanced Options");
1552
$showmsg = gettext("Hide Advanced Options");
1553

    
1554
if ($pkg['fields']['field'] != "") { ?>
1555
<script type="text/javascript">
1556
//<![CDATA[
1557
	events.push(function() {
1558

    
1559
	// Hide the advanced section
1560
	var advanced_visible = false;
1561

    
1562
	// Hide on page load
1563
	$('.advancedoptions').hide();
1564

    
1565
	// Suppress "Delete row" button if there are fewer than two rows
1566
	checkLastRow();
1567

    
1568
	// Show advanced section if you click the showadv button
1569
	$("#showadv").click(function() {
1570
		advanced_visible = !advanced_visible;
1571

    
1572
		if (advanced_visible) {
1573
			$('.advancedoptions').show();
1574
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$showmsg?>");
1575
		} else {
1576
			$('.advancedoptions').hide();
1577
			$("#showadv").html('<i class="fa fa-cog icon-embed-btn"></i>' + "<?=$hidemsg?>");
1578
		}
1579
	});
1580

    
1581
	// Call enablechange function
1582
	enablechange();
1583
});
1584

    
1585
	function enablechange() {
1586
<?php
1587
	foreach ($pkg['fields']['field'] as $field) {
1588
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
1589
			echo "\tif ($('input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
1590

    
1591
			if (isset($field['enablefields'])) {
1592
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1593
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1594
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
1595
					echo "\t\t}\n";
1596
				}
1597
			}
1598

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

    
1607
			echo "\t}\n\telse {\n";
1608

    
1609
			if (isset($field['enablefields'])) {
1610
				foreach (explode(',', $field['enablefields']) as $enablefield) {
1611
					echo "\t\tif ($('input[name=\"{$enablefield}\"]').length > 0) {\n";
1612
					echo "\t\t\t$('input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
1613
					echo "\t\t}\n";
1614
				}
1615
			}
1616

    
1617
			if (isset($field['checkenablefields'])) {
1618
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
1619
					echo "\t\tif ($('input[name=\"{$checkenablefield}\"]').length > 0) {\n";
1620
					echo "\t\t\t$('input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
1621
					echo "\t\t}\n";
1622
				}
1623
			}
1624

    
1625
			echo "\t}\n";
1626
		}
1627
	}
1628
	?>
1629
	}
1630
//]]>
1631
</script>
1632

    
1633
<?php
1634
}
1635

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