Project

General

Profile

Download (42.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	pkg_edit.php
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	Copyright (C) 2004-2012 Scott Ullrich <sullrich@gmail.com>
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 the
17
	   documentation and/or other materials provided with the distribution.
18

    
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30
/*
31
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
32
	pfSense_MODULE:	pkgs
33
*/
34

    
35
##|+PRIV
36
##|*IDENT=page-package-edit
37
##|*NAME=Package: Edit page
38
##|*DESCR=Allow access to the 'Package: Edit' page.
39
##|*MATCH=pkg_edit.php*
40
##|-PRIV
41

    
42
ini_set('max_execution_time', '0');
43

    
44
require("guiconfig.inc");
45
require_once("functions.inc");
46
require_once("filter.inc");
47
require_once("shaper.inc");
48
require_once("pkg-utils.inc");
49

    
50
/* dummy stubs needed by some code that was MFC'd */
51
function pfSenseHeader($location) {
52
	header("Location: " . $location);
53
}
54

    
55
function gentitle_pkg($pgname) {
56
	global $pfSense_config;
57
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
58
}
59

    
60
function domTT_title($title_msg) {
61
	if (!empty($title_msg)) {
62
		$title_msg = preg_replace("/\s+/", " ", $title_msg);
63
		$title_msg = preg_replace("/'/", "\'", $title_msg);
64
		return "onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\" onmouseover=\"domTT_activate(this, event, 'content', '{$title_msg}', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'delay', 300, 'styleClass', 'niceTitle');\"";
65
	}
66
}
67

    
68
$xml = htmlspecialchars($_GET['xml']);
69
if ($_POST['xml']) {
70
	$xml = htmlspecialchars($_POST['xml']);
71
}
72

    
73
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
74

    
75
if ($xml == "" || $xml_fullpath === false ||
76
    substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
77
	print_info_box_np(gettext("ERROR: No valid package defined."));
78
	die;
79
} else {
80
	$pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
81
}
82

    
83
if ($pkg['include_file'] <> "") {
84
	require_once($pkg['include_file']);
85
}
86

    
87
if (!isset($pkg['adddeleteeditpagefields'])) {
88
	$only_edit = true;
89
} else {
90
	$only_edit = false;
91
}
92

    
93
$package_name = $pkg['menu'][0]['name'];
94
$section      = $pkg['menu'][0]['section'];
95
$config_path  = $pkg['configpath'];
96
$name         = $pkg['name'];
97
$title        = $pkg['title'];
98
$pgtitle      = $title;
99

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

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

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

    
116
if ($pkg['custom_php_global_functions'] <> "") {
117
	eval($pkg['custom_php_global_functions']);
118
}
119

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

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

    
131
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
132

    
133
if ($_GET['savemsg'] <> "") {
134
	$savemsg = htmlspecialchars($_GET['savemsg']);
135
}
136

    
137
if ($pkg['custom_php_command_before_form'] <> "") {
138
	eval($pkg['custom_php_command_before_form']);
139
}
140

    
141
if ($_POST) {
142
	$firstfield = "";
143
	$rows = 0;
144

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

    
160
	if ($pkg['custom_php_validation_command']) {
161
		eval($pkg['custom_php_validation_command']);
162
	}
163

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

    
184
<?php include("head.inc"); ?>
185
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
186
<?php include("fbegin.inc"); ?>
187
<?php
188
			}
189
			if ($pkg['preoutput']) {
190
				echo "<pre>";
191
			}
192
			eval($pkg['custom_add_php_command']);
193
			if ($pkg['preoutput']) {
194
				echo "</pre>";
195
			}
196
		}
197
	}
198

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

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

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

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

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

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

    
258
			parse_package_templates();
259

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

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

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

    
286
if ($pkg['title'] <> "") {
287
	$edit = ($only_edit ? '' : ": " . gettext("Edit"));
288
	$title = $pkg['title'] . $edit;
289
} else {
290
	$title = gettext("Package Editor");
291
}
292

    
293
$pgtitle = $title;
294

    
295
if ($pkg['custom_php_after_head_command']) {
296
	$closehead = false;
297
	include("head.inc");
298
	eval($pkg['custom_php_after_head_command']);
299
	echo "</head>\n";
300
} else {
301
	include("head.inc");
302
}
303

    
304
?>
305

    
306
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
307

    
308
<?php include("fbegin.inc"); ?>
309

    
310
<script type="text/javascript" src="/javascript/autosuggest.js?rev=1"></script>
311
<script type="text/javascript" src="/javascript/suggestions.js"></script>
312

    
313
<?php if ($pkg['fields']['field'] <> "") { ?>
314
<script type="text/javascript">
315
//<![CDATA[
316
	//Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded
317
	jQuery(document).ready(function() {
318

    
319
		//Sortable function
320
		jQuery('#mainarea table tbody').sortable({
321
			items: 'tr.sortable',
322
			cursor: 'move',
323
			distance: 10,
324
			opacity: 0.8,
325
			helper: function(e, ui) {
326
				ui.children().each(function() {
327
					jQuery(this).width(jQuery(this).width());
328
				});
329
			return ui;
330
			},
331
		});
332

    
333
		//delete current line jQuery function
334
		jQuery('#maintable td .delete').live('click', function() {
335
			//do not remove first line
336
			if (jQuery("#maintable tr").length > 2) {
337
				jQuery(this).parent().parent().remove();
338
				return false;
339
			}
340
		});
341

    
342
		//add new line jQuery function
343
		jQuery('#mainarea table .add').click(function() {
344
			//get table size and assign as new id
345
			var c_id=jQuery("#maintable tr").length;
346
			var new_row=jQuery("table#maintable tr:last").html().replace(/(name|id)="(\w+)(\d+)"/g,"$1='$2"+c_id+"'");
347
			//apply new id to created line rowhelperid
348
			jQuery("table#maintable tr:last").after("<tr>"+new_row+"<\/tr>");
349
			return false;
350
		});
351
		// Call enablechange function
352
		enablechange();
353
	});
354

    
355
	function enablechange() {
356
	<?php
357
	foreach ($pkg['fields']['field'] as $field) {
358
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
359
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
360

    
361
			if (isset($field['enablefields'])) {
362
				foreach (explode(',', $field['enablefields']) as $enablefield) {
363
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
364
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
365
					echo "\t\t}\n";
366
				}
367
			}
368

    
369
			if (isset($field['checkenablefields'])) {
370
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
371
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
372
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
373
					echo "\t\t}\n";
374
				}
375
			}
376

    
377
			echo "\t}\n\telse {\n";
378

    
379
			if (isset($field['enablefields'])) {
380
				foreach (explode(',', $field['enablefields']) as $enablefield) {
381
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
382
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
383
					echo "\t\t}\n";
384
				}
385
			}
386

    
387
			if (isset($field['checkenablefields'])) {
388
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
389
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
390
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
391
					echo "\t\t}\n";
392
				}
393
			}
394

    
395
			echo "\t}\n";
396
		}
397
	}
398
	?>
399
	}
400
//]]>
401
</script>
402
<?php } ?>
403
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
404
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
405
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
406
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
407
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
408

    
409
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
410
<form name="iform" action="pkg_edit.php" method="post">
411
<input type="hidden" name="xml" value="<?= htmlspecialchars($xml) ?>" />
412
<?php if ($savemsg) print_info_box($savemsg); ?>
413
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="package edit">
414
<?php
415
if ($pkg['tabs'] <> "") {
416
	$tab_array = array();
417
	foreach ($pkg['tabs']['tab'] as $tab) {
418
		if ($tab['tab_level']) {
419
			$tab_level = $tab['tab_level'];
420
		} else {
421
			$tab_level = 1;
422
		}
423
		if (isset($tab['active'])) {
424
			$active = true;
425
		} else {
426
			$active = false;
427
		}
428
		if (isset($tab['no_drop_down'])) {
429
			$no_drop_down = true;
430
		}
431
		$urltmp = "";
432
		if ($tab['url'] <> "") {
433
			$urltmp = $tab['url'];
434
		}
435
		if ($tab['xml'] <> "") {
436
			$urltmp = "pkg_edit.php?xml=" . $tab['xml'];
437
		}
438

    
439
		$addresswithport = getenv("HTTP_HOST");
440
		$colonpos = strpos($addresswithport, ":");
441
		if ($colonpos !== False) {
442
			//my url is actually just the IP address of the pfsense box
443
			$myurl = substr($addresswithport, 0, $colonpos);
444
		} else {
445
			$myurl = $addresswithport;
446
		}
447
		// eval url so that above $myurl item can be processed if need be.
448
		$url = str_replace('$myurl', $myurl, $urltmp);
449

    
450
		$tab_array[$tab_level][] = array(
451
			$tab['text'],
452
			$active,
453
			$url
454
		);
455
	}
456

    
457
	ksort($tab_array);
458
	foreach ($tab_array as $tabid => $tab) {
459
		echo '<tr><td>';
460
		display_top_tabs($tab, $no_drop_down, $tabid);
461
		echo '</td></tr>';
462
	}
463
}
464

    
465
?>
466
<tr><td><div id="mainarea"><table id="t" class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
467
<?php
468
	$cols = 0;
469
	$savevalue = gettext("Save");
470
	if ($pkg['savetext'] <> "") {
471
		$savevalue = $pkg['savetext'];
472
	}
473
	/* If a package's XML has <advanced_options/> configured, then setup
474
	 * the table rows for the fields that have <advancedfield/> set.
475
	 * These fields will be placed below other fields in a separate area titled 'Advanced Features'.
476
	 * These advanced fields are not normally configured and generally left to default to 'default settings'.
477
	 */
478

    
479
	if ($pkg['advanced_options'] == "enabled") {
480
		$adv_filed_count = 0;
481
		$advanced = "<td>&nbsp;</td>";
482
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br /></td></tr>\n";
483
	}
484
	foreach ($pkg['fields']['field'] as $pkga) {
485
		if ($pkga['type'] == "sorting") {
486
			continue;
487
		}
488

    
489
		if ($pkga['type'] == "listtopic") {
490
			$input = "<tr id='td_{$pkga['fieldname']}'><td>&nbsp;</td></tr>";
491
			$input .= "<tr id='tr_{$pkga['fieldname']}'><td colspan=\"2\" class=\"listtopic\">{$pkga['name']}<br /></td></tr>\n";
492
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
493
				$advanced .= $input;
494
				$adv_filed_count++;
495
			} else {
496
				echo $input;
497
			}
498
			continue;
499
		}
500

    
501
		if ($pkga['combinefields'] == "begin") {
502
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
503
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
504
				$advanced .= $input;
505
			} else {
506
				echo $input;
507
			}
508
		}
509

    
510
		$size = "";
511
		$colspan="";
512
		if (isset($pkga['dontdisplayname'])) {
513
			$input="";
514
			// If this is in a set of combined fields and;
515
			// - it is a "begin" (case already handled above) or
516
			// - usecolspan2 is in effect (so we want to spread all the combined fields horizontally)
517
			// then we do not want this "tr" to be inserted.
518
			// Thus only insert the "tr" if the not (!) of the above condition.
519
			if (!((isset($pkga['combinefields'])) && (($pkga['combinefields'] == "begin") || (isset($pkga['usecolspan2']))))) {
520
				$input .= "<tr valign='top' id='tr_{$pkga['fieldname']}'>";
521
			}
522
			if (isset($pkga['usecolspan2'])) {
523
				$colspan="colspan='2'";
524
			} else {
525
				$input .= "<td width='22%' class='vncell{$req}'>&nbsp;</td>";
526
			}
527
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
528
				$advanced .= $input;
529
				$adv_filed_count++;
530
			} else {
531
				echo $input;
532
			}
533
		} else if (!isset($pkga['placeonbottom'])) {
534
			unset($req);
535
			if (isset($pkga['required'])) {
536
				$req = 'req';
537
			}
538
			$input="";
539
			// If this is in a set of combined fields and;
540
			// - it is a "begin" (case already handled above) or
541
			// - usecolspan2 is in effect (so we want to spread all the combined fields horizontally)
542
			// then we do not want this "tr" to be inserted.
543
			// Thus only insert the "tr" if the not (!) of the above condition.
544
			if (!((isset($pkga['combinefields'])) && (($pkga['combinefields'] == "begin") || (isset($pkga['usecolspan2']))))) {
545
				$input .= "<tr>";
546
			}
547
			$input .= "<td valign='top' width=\"22%\" class=\"vncell{$req}\">";
548
			$input .= fixup_string($pkga['fielddescr']);
549
			$input .= "</td>";
550
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
551
				$advanced .= $input;
552
				$adv_filed_count++;
553
			} else {
554
				echo $input;
555
			}
556
		}
557
		if ($pkga['combinefields'] == "begin") {
558
			$input="<td class=\"vncell\"><table summary=\"advanced\"><tr>";
559
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
560
				$advanced .= $input;
561
			} else {
562
				echo $input;
563
			}
564
		}
565

    
566
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
567
		if (!isset($pkga['placeonbottom'])) {
568
			$input="<td valign='top' {$colspan} {$class}>";
569
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
570
				$advanced .= $input;
571
				$adv_filed_count++;
572
			} else {
573
				echo $input;
574
			}
575
		}
576

    
577
		// if user is editing a record, load in the data.
578
		$fieldname = $pkga['fieldname'];
579
		if ($get_from_post) {
580
			$value = $_POST[$fieldname];
581
			if (is_array($value)) {
582
				$value = implode(',', $value);
583
			}
584
		} else {
585
			if (isset($id) && $a_pkg[$id]) {
586
				$value = $a_pkg[$id][$fieldname];
587
			} else {
588
				$value = $pkga['default_value'];
589
			}
590
		}
591
		switch ($pkga['type']) {
592
			case "input":
593
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
594
				$input = "<input {$size} id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formfld unknown' value=\"" . htmlspecialchars($value) ."\" />\n";
595
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
596
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
597
					$js_array[] = $pkga['fieldname'];
598
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
599
				} else {
600
					echo $input;
601
				}
602
				break;
603

    
604
			case "password":
605
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
606
				$input = "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' name='" . $pkga['fieldname'] . "' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
607
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
608
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
609
					$js_array[] = $pkga['fieldname'];
610
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
611
				} else {
612
					echo $input;
613
				}
614
				break;
615

    
616
			case "info":
617
				$input = fixup_string($pkga['description']) . "\n";
618
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
619
					$js_array[] = $pkga['fieldname'];
620
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
621
				} else {
622
					echo $input;
623
				}
624
				break;
625

    
626
			case "select":
627
				$fieldname = $pkga['fieldname'];
628
				if (isset($pkga['multiple'])) {
629
					$multiple = 'multiple="multiple"';
630
					$items = explode(',', $value);
631
					$fieldname .= "[]";
632
				} else {
633
					$multiple = '';
634
					$items = array($value);
635
				}
636
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
637
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
638
				$input = "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange name=\"$fieldname\">\n";
639
				foreach ($pkga['options']['option'] as $opt) {
640
					$selected = (in_array($opt['value'], $items) ? 'selected="selected"' : '');
641
					$input .= "\t<option value=\"{$opt['value']}\" {$selected}>{$opt['name']}</option>\n";
642
				}
643
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
644
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
645
					$js_array[] = $pkga['fieldname'];
646
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
647
					$advanced .= "</div>\n";
648
				} else {
649
					echo $input;
650
				}
651
				break;
652

    
653
			case "select_source":
654
				$fieldname = $pkga['fieldname'];
655
				if (isset($pkga['multiple'])) {
656
					$multiple = 'multiple="multiple"';
657
					$items = explode(',', $value);
658
					$fieldname .= "[]";
659
				} else {
660
					$multiple = '';
661
					$items = array($value);
662
				}
663
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
664
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
665
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} name=\"{$fieldname}\">\n";
666

    
667
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
668
					$js_array[] = $pkga['fieldname'];
669
					$advanced .= display_advanced_field($pkga['fieldname']) .$input;
670
					$advanced .= "</div>\n";
671
				} else {
672
					echo $input;
673
				}
674
				$source_url = $pkga['source'];
675
				eval("\$pkg_source_txt = &$source_url;");
676
				$input="";
677
				#check if show disable option is present on xml
678
				if (isset($pkga['show_disable_value'])) {
679
					array_push($pkg_source_txt,
680
						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']));
681
				}
682
				foreach ($pkg_source_txt as $opt) {
683
					$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
684
					$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
685
					$selected = (in_array($source_value, $items)? 'selected="selected"' : '');
686
					$input .= "\t<option value=\"{$source_value}\" $selected>{$source_name}</option>\n";
687
				}
688
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
689
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
690
					$advanced .= $input;
691
				} else {
692
					echo $input;
693
				}
694
				break;
695

    
696
			case "vpn_selection" :
697
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
698
				foreach ($config['ipsec']['phase1'] as $vpn) {
699
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
700
				}
701
				$input .= "</select>\n";
702
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
703

    
704
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
705
					$js_array[] = $pkga['fieldname'];
706
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
707
					$advanced .= "</div>\n";
708
				} else {
709
					echo $input;
710
				}
711
				break;
712

    
713
			case "checkbox":
714
				$checkboxchecked =($value == "on" ? " checked=\"checked\"" : "");
715
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
716
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
717
					$onclick = ' onclick="javascript:enablechange();"';
718
				}
719
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick} {$onchange} />\n";
720
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
721

    
722
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
723
					$js_array[] = $pkga['fieldname'];
724
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
725
					$advanced .= "</div>\n";
726
				} else {
727
					echo $input;
728
				}
729
				break;
730

    
731
			case "textarea":
732
				if ($pkga['rows']) {
733
					$rows = " rows='{$pkga['rows']}' ";
734
				}
735
				if ($pkga['cols']) {
736
					$cols = " cols='{$pkga['cols']}' ";
737
				}
738
				if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) {
739
					$value = base64_decode($value);
740
				}
741
				$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');
742
				$input = "<textarea {$rows} {$cols} name='{$pkga['fieldname']}'{$wrap}>{$value}</textarea>\n";
743
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
744
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
745
					$js_array[] = $pkga['fieldname'];
746
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
747
					$advanced .= "</div>\n";
748
				} else {
749
					echo $input;
750
				}
751
				break;
752

    
753
			case "aliases":
754
				// Use xml tag <typealiases> to filter type aliases
755
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
756
				$fieldname = $pkga['fieldname'];
757
				$a_aliases = &$config['aliases']['alias'];
758
				$addrisfirst = 0;
759
				$aliasesaddr = "";
760
				$value = "value='{$value}'";
761

    
762
				if (isset($a_aliases)) {
763
					if (!empty($pkga['typealiases'])) {
764
						foreach ($a_aliases as $alias) {
765
							if ($alias['type'] == $pkga['typealiases']) {
766
								if ($addrisfirst == 1) {
767
									$aliasesaddr .= ",";
768
								}
769
								$aliasesaddr .= "'" . $alias['name'] . "'";
770
								$addrisfirst = 1;
771
							}
772
						}
773
					} else {
774
						foreach ($a_aliases as $alias) {
775
							if ($addrisfirst == 1) {
776
								$aliasesaddr .= ",";
777
							}
778
							$aliasesaddr .= "'" . $alias['name'] . "'";
779
							$addrisfirst = 1;
780
						}
781
					}
782
				}
783

    
784
				$input = "<input name='{$fieldname}' type='text' class='formfldalias' id='{$fieldname}' {$size} {$value} />\n<br />";
785
				$input .= fixup_string($pkga['description']) . "\n";
786

    
787
				$script = "<script type='text/javascript'>\n";
788
				$script .= "//<![CDATA[\n";
789
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
790
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
791
				$script .= "//]]>\n";
792
				$script .= "</script>";
793

    
794
				echo $input;
795
				echo $script;
796
				break;
797

    
798
			case "interfaces_selection":
799
				$ips = array();
800
				$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
801
				if (is_array($config['interfaces'])) {
802
					foreach ($config['interfaces'] as $iface_key=>$iface_value) {
803
						if (isset($iface_value['enable']) && !preg_match("/$interface_regex/", $iface_key)) {
804
							$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
805
							if (isset($pkga['showips'])) {
806
								$iface_description .= " address";
807
							}
808
							$ips[] = array('ip'=> $iface_key, 'description'=> $iface_description);
809
						}
810
					}
811
				}
812
				if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) {
813
					foreach ($config['virtualip']['vip'] as $vip) {
814
						if (!preg_match("/$interface_regex/", $vip['interface'])) {
815
							$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
816
						}
817
						switch ($vip['mode']) {
818
							case "ipalias":
819
							case "carp":
820
								$ips[] = array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
821
								break;
822
							case "proxyarp":
823
								if ($vip['type'] == "network") {
824
									$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
825
									$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
826
									$len = $end - $start;
827
									for ($i = 0; $i <= $len; $i++) {
828
										$ips[]= array('ip' => long2ip32($start+$i), 'description' => long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
829
									}
830
								} else {
831
									$ips[]= array('ip' => $vip['subnet'], 'description' => "{$vip['subnet']} $vip_description");
832
								}
833
								break;
834
						}
835
					}
836
				}
837
				sort($ips);
838
				if (isset($pkga['showlistenall'])) {
839
					array_unshift($ips, array('ip' => 'All', 'description' => 'Listen on All interfaces/ip addresses '));
840
				}
841
				if (!preg_match("/$interface_regex/", "loopback")) {
842
					$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
843
					array_push($ips, array('ip' => 'lo0', 'description' => $iface_description));
844
				}
845

    
846
				#show interfaces array on gui
847
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
848
				$multiple = '';
849
				$fieldname = $pkga['fieldname'];
850
				if (isset($pkga['multiple'])) {
851
					$fieldname .= '[]';
852
					$multiple = 'multiple="multiple"';
853
				}
854
				$input = "<select id='{$pkga['fieldname']}' name=\"{$fieldname}\" {$size} {$multiple}>\n";
855
				if (is_array($value)) {
856
					$values = $value;
857
				} else {
858
					$values = explode(',', $value);
859
				}
860
				foreach ($ips as $iface) {
861
					$selected = (in_array($iface['ip'], $values) ? 'selected="selected"' : '');
862
					$input .= "<option value=\"{$iface['ip']}\" {$selected}>{$iface['description']}</option>\n";
863
				}
864
				$input .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
865
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
866
					$advanced .= $input;
867
				} else {
868
					echo $input;
869
				}
870
				break;
871

    
872
			case "radio":
873
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
874
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
875
					$advanced .= $input;
876
				} else {
877
					echo $input;
878
				}
879
				break;
880

    
881
			case "button":
882
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}' />\n";
883
				if (isset($pkga['placeonbottom'])) {
884
					$pkg_buttons .= $input;
885
				} else {
886
					echo $input ."\n<br />" . fixup_string($pkga['description']) . "\n";
887
				}
888
				break;
889

    
890
			case "schedule_selection":
891
				$input = "<select id='{$pkga['fieldname']}' name='{$pkga['fieldname']}'>\n";
892
				$schedules = array();
893
				$schedules[] = "none";
894
				if (is_array($config['schedules']['schedule'])) {
895
					foreach ($config['schedules']['schedule'] as $schedule) {
896
						if ($schedule['name'] <> "") {
897
							$schedules[] = $schedule['name'];
898
						}
899
					}
900
				}
901
				foreach ($schedules as $schedule) {
902
					$selected = ($value == $schedule ? "selected=\"selected\"" : "");
903
					if ($schedule == "none") {
904
						$input .= "<option value=\"\" {$selected}>{$schedule}</option>\n";
905
					} else {
906
						$input .= "<option value=\"{$schedule}\" {$selected}>{$schedule}</option>\n";
907
					}
908
				}
909
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
910
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
911
					$js_array[] = $pkga['fieldname'];
912
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
913
					$advanced .= "</div>\n";
914
				} else {
915
					echo $input;
916
				}
917
				break;
918

    
919
			case "rowhelper":
920
				#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
921
				$rowhelpername="row";
922
				?>
923
				<script type="text/javascript">
924
				//<![CDATA[
925
				<?php
926
					$rowcounter = 0;
927
					$fieldcounter = 0;
928
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
929
						echo "rowname[{$fieldcounter}] = \"{$rowhelper['fieldname']}\";\n";
930
						echo "rowtype[{$fieldcounter}] = \"{$rowhelper['type']}\";\n";
931
						echo "rowsize[{$fieldcounter}] = \"{$rowhelper['size']}\";\n";
932
						$fieldcounter++;
933
					}
934
				?>
935
				//]]>
936
				</script>
937
				<table id="maintable" summary="main table">
938
				<tr id='<?="tr_{$pkga['fieldname']}";?>'>
939
				<?php
940
					foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
941
						echo "<td ".domTT_title($rowhelper['description'])."><b>" . fixup_string($rowhelper['fielddescr']) . "</b></td>\n";
942
					}
943

    
944
					$rowcounter = 0;
945
					$trc = 0;
946

    
947
					//Use assigned $a_pkg or create an empty array to enter loop
948
					if (isset($a_pkg[$id][$rowhelpername])) {
949
						$saved_rows=$a_pkg[$id][$rowhelpername];
950
					} else {
951
						$saved_rows[] = array();
952
					}
953

    
954
					foreach ($saved_rows as $row) {
955
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
956
						foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
957
							unset($value);
958
							if ($rowhelper['value'] <> "") {
959
								$value = $rowhelper['value'];
960
							}
961
							$fieldname = $rowhelper['fieldname'];
962
							// if user is editing a record, load in the data.
963
							if (isset($id) && $a_pkg[$id]) {
964
								$value = $row[$fieldname];
965
							}
966
							$options = "";
967
							$type = $rowhelper['type'];
968
							$description = $rowhelper['description'];
969
							$fieldname = $rowhelper['fieldname'];
970
							if ($type == "option") {
971
								$options = &$rowhelper['options']['option'];
972
							}
973
							if ($rowhelper['size']) {
974
								$size = $rowhelper['size'];
975
							} else if ($pkga['size']) {
976
								$size = $pkga['size'];
977
							} else {
978
								$size = "8";
979
							}
980
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
981

    
982
							$text = "";
983
							$trc++;
984
						}
985
						$rowcounter++;
986
						echo "<td>";
987
						#echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" alt=\"remove\" /></a>";
988
						echo "<a class='delete' href=\"#\"><img border='0' src='./themes/{$g['theme']}/images/icons/icon_x.gif' alt='delete' /></a>";
989
						echo "</td>\n";
990
					}
991
				?>
992
				</tr>
993
				<tbody></tbody>
994
				</table>
995

    
996
				<!-- <br /><a onclick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?#= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>-->
997
				<br /><a class="add" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>
998
				<br /><?php if ($pkga['description'] != "") echo $pkga['description']; ?>
999
				<script type="text/javascript">
1000
				//<![CDATA[
1001
				field_counter_js = <?= $fieldcounter ?>;
1002
				rows = <?= $rowcounter ?>;
1003
				totalrows = <?php echo $rowcounter; ?>;
1004
				loaded = <?php echo $rowcounter; ?>;
1005
				//typesel_change();
1006
				//]]>
1007
				</script>
1008

    
1009
				<?php
1010
				break;
1011
		}
1012
		#check typehint value
1013
		if ($pkga['typehint']) {
1014
			echo " " . $pkga['typehint'];
1015
		}
1016
		#check combinefields options
1017
		if (isset($pkga['combinefields'])) {
1018
			// At the end of each combined-fields field we always want to end a td tag.
1019
			$input = "</td>";
1020
			// The tr tag end is used to end the whole set of combined fields,
1021
			// but also if usecolspan2 is not in effect then we also put each combined field in its own tr.
1022
			if (($pkga['combinefields'] == "end") || (!isset($pkga['usecolspan2']))) {
1023
				$input .= "</tr>";
1024
			}
1025
			// At the end of the combined fields we finish up the table that encloses the combined fields...
1026
			if ($pkga['combinefields'] == "end") {
1027
				$input .= "</table></td></tr>";
1028
			}
1029
		} else {
1030
			$input = "</td></tr>";
1031
			if ($pkga['usecolspan2']) {
1032
				$input .= "</tr><br />";
1033
			}
1034
		}
1035
		if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
1036
			$advanced .= "{$input}\n";
1037
		} else {
1038
			echo "{$input}\n";
1039
		}
1040
		#increment counter
1041
		$i++;
1042
	}
1043

    
1044
	#print advanced settings if any after reading all fields
1045
	if (isset($advanced) && $adv_filed_count > 0) {
1046
		echo $advanced;
1047
	}
1048

    
1049
	?>
1050
	<tr>
1051
		<td>&nbsp;</td>
1052
	</tr>
1053
	<tr>
1054
		<td width="22%" valign="top">&nbsp;</td>
1055
		<td width="78%">
1056
			<div id="buttons">
1057
			<?php
1058
				if ($pkg['note'] != "") {
1059
					echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
1060
				}
1061
				//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
1062
				echo "<input name='id' type='hidden' value=\"" . htmlspecialchars($id) . "\" />";
1063
				echo "<input name='Submit' type='submit' class='formbtn' value=\"" . htmlspecialchars($savevalue) . "\" />\n{$pkg_buttons}\n";
1064
				if (!$only_edit) {
1065
					echo "<input class=\"formbtn\" type=\"button\" value=\"".gettext("Cancel")."\" onclick=\"window.location.href='" . $_SERVER['HTTP_REFERER'] . "'\" />";
1066
				}
1067
			?>
1068
			</div>
1069
		</td>
1070
	</tr>
1071

    
1072
</table>
1073
</div></td></tr>
1074
</table>
1075
</form>
1076

    
1077
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
1078

    
1079
<?php
1080
	/* JavaScript to handle the advanced fields. */
1081
	if ($pkg['advanced_options'] == "enabled") {
1082
		echo "<script type=\"text/javascript\">\n";
1083
		echo "//<![CDATA[\n";
1084
		foreach ($js_array as $advfieldname) {
1085
			echo "function show_" . $advfieldname . "() {\n";
1086
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
1087
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
1088
			echo "}\n\n";
1089
		}
1090
		echo "//]]>\n";
1091
		echo "</script>\n";
1092
	}
1093
?>
1094

    
1095
<?php include("fend.inc"); ?>
1096
</body>
1097
</html>
1098

    
1099
<?php
1100
/*
1101
 * ROW Helpers function
1102
 */
1103
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
1104
	global $text, $config;
1105
	echo "<td>\n";
1106
	switch ($type) {
1107
		case "input":
1108
			echo "<input size='{$size}' name='{$fieldname}{$trc}' id='{$fieldname}{$trc}' class='formfld unknown' value=\"" . htmlspecialchars($value) . "\" />\n";
1109
			break;
1110
		case "checkbox":
1111
			echo "<input size='{$size}' type='checkbox' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' value='ON' ".($value?"CHECKED":"")." />\n";
1112
			break;
1113
		case "password":
1114
			echo "<input size='{$size}' type='password' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
1115
			break;
1116
		case "textarea":
1117
			echo "<textarea rows='2' cols='12' id='{$fieldname}{$trc}' class='formfld unknown' name='{$fieldname}{$trc}'>{$value}</textarea>\n";
1118
		case "select":
1119
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$title}>\n";
1120
			foreach ($rowhelper['options']['option'] as $rowopt) {
1121
				$text .= "<option value='{$rowopt['value']}'>{$rowopt['name']}</option>";
1122
				echo "<option value='{$rowopt['value']}'".($rowopt['value'] == $value?" selected=\"selected\"":"").">{$rowopt['name']}</option>\n";
1123
			}
1124
			echo "</select>\n";
1125
			break;
1126
		case "interfaces_selection":
1127
			$size = ($size ? "size=\"{$size}\"" : '');
1128
			$multiple = '';
1129
			if (isset($rowhelper['multiple'])) {
1130
				$fieldname .= '[]';
1131
				$multiple = "multiple=\"multiple\"";
1132
			}
1133
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
1134
			$ifaces = get_configured_interface_with_descr();
1135
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
1136
			if (!empty($additional_ifaces)) {
1137
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
1138
			}
1139
			if (is_array($value)) {
1140
				$values = $value;
1141
			} else {
1142
				$values = explode(',', $value);
1143
			}
1144
			$ifaces["lo0"] = "loopback";
1145
			echo "<option><name></name><value></value></option>/n";
1146
			foreach ($ifaces as $ifname => $iface) {
1147
				$text .="<option value=\"{$ifname}\">$iface</option>";
1148
				echo "<option value=\"{$ifname}\" ".(in_array($ifname, $values) ? 'selected="selected"' : '').">{$iface}</option>\n";
1149
			}
1150
			echo "</select>\n";
1151
			break;
1152
		case "select_source":
1153
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}'>\n";
1154
			if (isset($rowhelper['show_disable_value'])) {
1155
				echo "<option value='{$rowhelper['show_disable_value']}'>{$rowhelper['show_disable_value']}</option>\n";
1156
			}
1157
			$source_url = $rowhelper['source'];
1158
			eval("\$pkg_source_txt = &$source_url;");
1159
			foreach ($pkg_source_txt as $opt) {
1160
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
1161
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
1162
				$text .= "<option value='{$source_value}'>{$source_name}</option>";
1163
				echo "<option value='{$source_value}'".($source_value == $value?" selected=\"selected\"":"").">{$source_name}</option>\n";
1164
			}
1165
			echo "</select>\n";
1166
			break;
1167
	}
1168
	echo "</td>\n";
1169
}
1170

    
1171
function fixup_string($string) {
1172
	global $config;
1173
	// fixup #1: $myurl -> http[s]://ip_address:port/
1174
	$https = "";
1175
	$port = $config['system']['webguiport'];
1176
	if ($port <> "443" and $port <> "80") {
1177
		$urlport = ":" . $port;
1178
	} else {
1179
		$urlport = "";
1180
	}
1181

    
1182
	if ($config['system']['webgui']['protocol'] == "https") {
1183
		$https = "s";
1184
	}
1185
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
1186
	$newstring = str_replace("\$myurl", $myurl, $string);
1187
	$string = $newstring;
1188
	// fixup #2: $wanip
1189
	$curwanip = get_interface_ip();
1190
	$newstring = str_replace("\$wanip", $curwanip, $string);
1191
	$string = $newstring;
1192
	// fixup #3: $lanip
1193
	$lancfg = $config['interfaces']['lan'];
1194
	$lanip = $lancfg['ipaddr'];
1195
	$newstring = str_replace("\$lanip", $lanip, $string);
1196
	$string = $newstring;
1197
	// fixup #4: fix'r'up here.
1198
	return $newstring;
1199
}
1200

    
1201
/*
1202
 *  Parse templates if they are defined
1203
 */
1204
function parse_package_templates() {
1205
	global $pkg, $config;
1206
	$rows = 0;
1207
	if ($pkg['templates']['template'] <> "") {
1208
		foreach ($pkg['templates']['template'] as $pkg_template_row) {
1209
			$filename = $pkg_template_row['filename'];
1210
			$template_text = $pkg_template_row['templatecontents'];
1211
			$firstfield = "";
1212
			/* calculate total row helpers count and */
1213
			/* change fields defined as fieldname_fieldvalue to their value */
1214
			foreach ($pkg['fields']['field'] as $fields) {
1215
				switch ($fields['type']) {
1216
					case "rowhelper":
1217
						// save rowhelper items.
1218
						$row_helper_total_rows = 0;
1219
						$row_helper_data = "";
1220
						foreach ($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
1221
							foreach ($_POST as $key => $value) {
1222
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/", $key, $matches)) {
1223
									$row_helper_total_rows++;
1224
									$row_helper_data .= $value;
1225
									$sep = "";
1226
									ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
1227
									foreach ($sep as $se) {
1228
										$separator = $se;
1229
									}
1230
									if ($separator <> "") {
1231
										$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
1232
										$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
1233
									}
1234
									$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
1235
								}
1236
							}
1237
						}
1238
						break;
1239
					default:
1240
						$fieldname = $fields['fieldname'];
1241
						$fieldvalue = $_POST[$fieldname];
1242
						$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
1243
				}
1244
			}
1245
			/* replace $domain_total_rows with total rows */
1246
			$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
1247

    
1248
			/* replace cr's */
1249
			$template_text = str_replace("\\n", "\n", $template_text);
1250

    
1251
			/* write out new template file */
1252
			$fout = fopen($filename, "w");
1253
			fwrite($fout, $template_text);
1254
			fclose($fout);
1255
		}
1256
	}
1257
}
1258

    
1259
/* Return html div fields */
1260
function display_advanced_field($fieldname) {
1261
	$div = "<div id='showadv_{$fieldname}'>\n";
1262
	$div .= "<input type='button' onclick='show_{$fieldname}()' value='" . gettext("Advanced") . "' /> - " . gettext("Show advanced option") ."</div>\n";
1263
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1264
	return $div;
1265
}
1266

    
1267
?>
(114-114/238)