Project

General

Profile

Download (41.9 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
$id = $_GET['id'];
94
if (isset($_POST['id'])) {
95
	$id = htmlspecialchars($_POST['id']);
96
}
97

    
98
// Not posting?  Then user is editing a record. There must be a valid id
99
// when editing a record.
100
if (!$id && !$_POST) {
101
	$id = "0";
102
}
103

    
104
if (!is_numeric($id)) {
105
	header("Location: /");
106
	exit;
107
}
108

    
109
if ($pkg['custom_php_global_functions'] <> "") {
110
	eval($pkg['custom_php_global_functions']);
111
}
112

    
113
// grab the installedpackages->package_name section.
114
if ($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) {
115
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
116
}
117

    
118
// If the first entry in the array is an empty <config/> tag, kill it.
119
if ($config['installedpackages'] && (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0)
120
    && ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == "")) {
121
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
122
}
123

    
124
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
125

    
126
if ($_GET['savemsg'] <> "") {
127
	$savemsg = htmlspecialchars($_GET['savemsg']);
128
}
129

    
130
if ($pkg['custom_php_command_before_form'] <> "") {
131
	eval($pkg['custom_php_command_before_form']);
132
}
133

    
134
if ($_POST) {
135
	$rows = 0;
136

    
137
	$input_errors = array();
138
	$reqfields = array();
139
	$reqfieldsn = array();
140
	foreach ($pkg['fields']['field'] as $field) {
141
		if (($field['type'] == 'input') && isset($field['required'])) {
142
			if ($field['fieldname']) {
143
				$reqfields[] = $field['fieldname'];
144
			}
145
			if ($field['fielddescr']) {
146
				$reqfieldsn[] = $field['fielddescr'];
147
			}
148
		}
149
	}
150
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
151

    
152
	if ($pkg['custom_php_validation_command']) {
153
		eval($pkg['custom_php_validation_command']);
154
	}
155

    
156
	if ($_POST['act'] == "del") {
157
		if ($pkg['custom_delete_php_command']) {
158
			if ($pkg['custom_php_command_before_form'] <> "") {
159
				eval($pkg['custom_php_command_before_form']);
160
			}
161
			eval($pkg['custom_delete_php_command']);
162
		}
163
		write_config($pkg['delete_string']);
164
		// resync the configuration file code if defined.
165
		if ($pkg['custom_php_resync_config_command'] <> "") {
166
			if ($pkg['custom_php_command_before_form'] <> "") {
167
				eval($pkg['custom_php_command_before_form']);
168
			}
169
			eval($pkg['custom_php_resync_config_command']);
170
		}
171
	} else {
172
		if (!$input_errors && $pkg['custom_add_php_command']) {
173
			if ($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
174
			?>
175

    
176
<?php include("head.inc"); ?>
177
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
178
<?php include("fbegin.inc"); ?>
179
<?php
180
			}
181
			if ($pkg['preoutput']) {
182
				echo "<pre>";
183
			}
184
			eval($pkg['custom_add_php_command']);
185
			if ($pkg['preoutput']) {
186
				echo "</pre>";
187
			}
188
		}
189
	}
190

    
191
	// donotsave is enabled.  lets simply exit.
192
	if (empty($pkg['donotsave'])) {
193

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

    
230
			if (isset($id) && $a_pkg[$id]) {
231
				$a_pkg[$id] = $pkgarr;
232
			} else {
233
				$a_pkg[] = $pkgarr;
234
			}
235

    
236
			write_config($pkg['addedit_string']);
237
			// late running code
238
			if ($pkg['custom_add_php_command_late'] <> "") {
239
				eval($pkg['custom_add_php_command_late']);
240
			}
241

    
242
			if (isset($pkg['filter_rules_needed'])) {
243
				filter_configure();
244
			}
245

    
246
			// resync the configuration file code if defined.
247
			if ($pkg['custom_php_resync_config_command'] <> "") {
248
				eval($pkg['custom_php_resync_config_command']);
249
			}
250

    
251
			parse_package_templates();
252

    
253
			/* if start_command is defined, restart w/ this */
254
			if ($pkg['start_command'] <> "") {
255
				exec($pkg['start_command'] . ">/dev/null 2&>1");
256
			}
257

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

    
263
			if ($pkg['aftersaveredirect'] <> "") {
264
				pfSenseHeader($pkg['aftersaveredirect']);
265
			} elseif (!$pkg['adddeleteeditpagefields']) {
266
				pfSenseHeader("pkg_edit.php?xml={$xml}&amp;id=0");
267
			} elseif (!$pkg['preoutput']) {
268
				pfSenseHeader("pkg.php?xml=" . $xml);
269
			}
270
			exit;
271
		} else {
272
			$get_from_post = true;
273
		}
274
	} elseif (!$input_errors) {
275
		exit;
276
	}
277
}
278

    
279
if ($pkg['title'] <> "") {
280
	$edit = ($only_edit ? '' : ": " . gettext("Edit"));
281
	$pgtitle = $pkg['title'] . $edit;
282
} else {
283
	$pgtitle = gettext("Package Editor");
284
}
285

    
286
if ($pkg['custom_php_after_head_command']) {
287
	$closehead = false;
288
	include("head.inc");
289
	eval($pkg['custom_php_after_head_command']);
290
	echo "</head>\n";
291
} else {
292
	include("head.inc");
293
}
294

    
295
?>
296

    
297
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
298

    
299
<?php include("fbegin.inc"); ?>
300

    
301
<script type="text/javascript" src="/javascript/autosuggest.js?rev=1"></script>
302
<script type="text/javascript" src="/javascript/suggestions.js"></script>
303

    
304
<?php if ($pkg['fields']['field'] <> "") { ?>
305
<script type="text/javascript">
306
//<![CDATA[
307
	//Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded
308
	jQuery(document).ready(function() {
309

    
310
		//Sortable function
311
		jQuery('#mainarea table tbody').sortable({
312
			items: 'tr.sortable',
313
			cursor: 'move',
314
			distance: 10,
315
			opacity: 0.8,
316
			helper: function(e, ui) {
317
				ui.children().each(function() {
318
					jQuery(this).width(jQuery(this).width());
319
				});
320
			return ui;
321
			},
322
		});
323

    
324
		//delete current line jQuery function
325
		jQuery('#maintable td .delete').live('click', function() {
326
			//do not remove first line
327
			if (jQuery("#maintable tr").length > 2) {
328
				jQuery(this).parent().parent().remove();
329
				return false;
330
			}
331
		});
332

    
333
		//add new line jQuery function
334
		jQuery('#mainarea table .add').click(function() {
335
			//get table size and assign as new id
336
			var c_id=jQuery("#maintable tr").length;
337
			var new_row=jQuery("table#maintable tr:last").html().replace(/(name|id)="(\w+)(\d+)"/g,"$1='$2"+c_id+"'");
338
			//apply new id to created line rowhelperid
339
			jQuery("table#maintable tr:last").after("<tr>"+new_row+"<\/tr>");
340
			return false;
341
		});
342
		// Call enablechange function
343
		enablechange();
344
	});
345

    
346
	function enablechange() {
347
	<?php
348
	foreach ($pkg['fields']['field'] as $field) {
349
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
350
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
351

    
352
			if (isset($field['enablefields'])) {
353
				foreach (explode(',', $field['enablefields']) as $enablefield) {
354
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
355
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
356
					echo "\t\t}\n";
357
				}
358
			}
359

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

    
368
			echo "\t}\n\telse {\n";
369

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

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

    
386
			echo "\t}\n";
387
		}
388
	}
389
	?>
390
	}
391
//]]>
392
</script>
393
<?php } ?>
394
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
395
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
396
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
397
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
398
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
399

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

    
430
		$addresswithport = getenv("HTTP_HOST");
431
		$colonpos = strpos($addresswithport, ":");
432
		if ($colonpos !== False) {
433
			//my url is actually just the IP address of the pfsense box
434
			$myurl = substr($addresswithport, 0, $colonpos);
435
		} else {
436
			$myurl = $addresswithport;
437
		}
438
		// eval url so that above $myurl item can be processed if need be.
439
		$url = str_replace('$myurl', $myurl, $urltmp);
440

    
441
		$tab_array[$tab_level][] = array(
442
			$tab['text'],
443
			$active,
444
			$url
445
		);
446
	}
447

    
448
	ksort($tab_array);
449
	foreach ($tab_array as $tabid => $tab) {
450
		echo '<tr><td>';
451
		display_top_tabs($tab, $no_drop_down, $tabid);
452
		echo '</td></tr>';
453
	}
454
}
455

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

    
470
	if ($pkg['advanced_options'] == "enabled") {
471
		$adv_filed_count = 0;
472
		$advanced = "<td>&nbsp;</td>";
473
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br /></td></tr>\n";
474
	}
475
	$js_array = array();
476
	foreach ($pkg['fields']['field'] as $pkga) {
477
		if ($pkga['type'] == "sorting") {
478
			continue;
479
		}
480

    
481
		if ($pkga['type'] == "listtopic") {
482
			$input = "<tr id='td_{$pkga['fieldname']}'><td>&nbsp;</td></tr>";
483
			$input .= "<tr id='tr_{$pkga['fieldname']}'><td colspan=\"2\" class=\"listtopic\">{$pkga['name']}<br /></td></tr>\n";
484
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
485
				$advanced .= $input;
486
				$adv_filed_count++;
487
			} else {
488
				echo $input;
489
			}
490
			continue;
491
		}
492

    
493
		if ($pkga['combinefields'] == "begin") {
494
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
495
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
496
				$advanced .= $input;
497
			} else {
498
				echo $input;
499
			}
500
		}
501

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

    
558
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
559
		if (!isset($pkga['placeonbottom'])) {
560
			$input="<td valign='top' {$colspan} {$class}>";
561
			if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
562
				$advanced .= $input;
563
				$adv_filed_count++;
564
			} else {
565
				echo $input;
566
			}
567
		}
568

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

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

    
610
			case "info":
611
				$input = fixup_string($pkga['description']) . "\n";
612
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
613
					$js_array[] = $pkga['fieldname'];
614
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
615
				} else {
616
					echo $input;
617
				}
618
				break;
619

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

    
647
			case "select_source":
648
				$fieldname = $pkga['fieldname'];
649
				if (isset($pkga['multiple'])) {
650
					$multiple = 'multiple="multiple"';
651
					$items = explode(',', $value);
652
					$fieldname .= "[]";
653
				} else {
654
					$multiple = '';
655
					$items = array($value);
656
				}
657
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
658
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
659
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} name=\"{$fieldname}\">\n";
660

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

    
690
			case "vpn_selection" :
691
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
692
				foreach ($config['ipsec']['phase1'] as $vpn) {
693
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
694
				}
695
				$input .= "</select>\n";
696
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
697

    
698
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
699
					$js_array[] = $pkga['fieldname'];
700
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
701
					$advanced .= "</div>\n";
702
				} else {
703
					echo $input;
704
				}
705
				break;
706

    
707
			case "checkbox":
708
				$checkboxchecked =($value == "on" ? " checked=\"checked\"" : "");
709
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
710
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) {
711
					$onclick = ' onclick="javascript:enablechange();"';
712
				}
713
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick} {$onchange} />\n";
714
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
715

    
716
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
717
					$js_array[] = $pkga['fieldname'];
718
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
719
					$advanced .= "</div>\n";
720
				} else {
721
					echo $input;
722
				}
723
				break;
724

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

    
747
			case "aliases":
748
				// Use xml tag <typealiases> to filter type aliases
749
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
750
				$fieldname = $pkga['fieldname'];
751
				$a_aliases = &$config['aliases']['alias'];
752
				$addrisfirst = 0;
753
				$aliasesaddr = "";
754
				$value = "value='{$value}'";
755

    
756
				if (isset($a_aliases)) {
757
					if (!empty($pkga['typealiases'])) {
758
						foreach ($a_aliases as $alias) {
759
							if ($alias['type'] == $pkga['typealiases']) {
760
								if ($addrisfirst == 1) {
761
									$aliasesaddr .= ",";
762
								}
763
								$aliasesaddr .= "'" . $alias['name'] . "'";
764
								$addrisfirst = 1;
765
							}
766
						}
767
					} else {
768
						foreach ($a_aliases as $alias) {
769
							if ($addrisfirst == 1) {
770
								$aliasesaddr .= ",";
771
							}
772
							$aliasesaddr .= "'" . $alias['name'] . "'";
773
							$addrisfirst = 1;
774
						}
775
					}
776
				}
777

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

    
781
				$script = "<script type='text/javascript'>\n";
782
				$script .= "//<![CDATA[\n";
783
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
784
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
785
				$script .= "//]]>\n";
786
				$script .= "</script>";
787

    
788
				echo $input;
789
				echo $script;
790
				break;
791

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

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

    
866
			case "radio":
867
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
868
				if (isset($pkga['advancedfield']) && isset($adv_filed_count)) {
869
					$advanced .= $input;
870
				} else {
871
					echo $input;
872
				}
873
				break;
874

    
875
			case "button":
876
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}' />\n";
877
				if (isset($pkga['placeonbottom'])) {
878
					$pkg_buttons .= $input;
879
				} else {
880
					echo $input ."\n<br />" . fixup_string($pkga['description']) . "\n";
881
				}
882
				break;
883

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

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

    
938
					$rowcounter = 0;
939
					$trc = 0;
940

    
941
					//Use assigned $a_pkg or create an empty array to enter loop
942
					if (isset($a_pkg[$id][$rowhelpername])) {
943
						$saved_rows=$a_pkg[$id][$rowhelpername];
944
					} else {
945
						$saved_rows[] = array();
946
					}
947

    
948
					foreach ($saved_rows as $row) {
949
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
950
						foreach ($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
951
							unset($value);
952
							if ($rowhelper['value'] <> "") {
953
								$value = $rowhelper['value'];
954
							}
955
							$fieldname = $rowhelper['fieldname'];
956
							// if user is editing a record, load in the data.
957
							if (isset($id) && $a_pkg[$id]) {
958
								$value = $row[$fieldname];
959
							}
960
							$type = $rowhelper['type'];
961
							$fieldname = $rowhelper['fieldname'];
962
							if ($rowhelper['size']) {
963
								$size = $rowhelper['size'];
964
							} else if ($pkga['size']) {
965
								$size = $pkga['size'];
966
							} else {
967
								$size = "8";
968
							}
969
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
970

    
971
							$text = "";
972
							$trc++;
973
						}
974
						$rowcounter++;
975
						echo "<td>";
976
						#echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" alt=\"remove\" /></a>";
977
						echo "<a class='delete' href=\"#\"><img border='0' src='./themes/{$g['theme']}/images/icons/icon_x.gif' alt='delete' /></a>";
978
						echo "</td>\n";
979
					}
980
				?>
981
				</tr>
982
				<tbody></tbody>
983
				</table>
984

    
985
				<!-- <br /><a onclick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?#= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>-->
986
				<br /><a class="add" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>
987
				<br /><?php if ($pkga['description'] != "") echo $pkga['description']; ?>
988
				<script type="text/javascript">
989
				//<![CDATA[
990
				field_counter_js = <?= $fieldcounter ?>;
991
				rows = <?= $rowcounter ?>;
992
				totalrows = <?php echo $rowcounter; ?>;
993
				loaded = <?php echo $rowcounter; ?>;
994
				//typesel_change();
995
				//]]>
996
				</script>
997

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

    
1033
	#print advanced settings if any after reading all fields
1034
	if (isset($advanced) && $adv_filed_count > 0) {
1035
		echo $advanced;
1036
	}
1037

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

    
1061
</table>
1062
</div></td></tr>
1063
</table>
1064
</form>
1065

    
1066
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
1067

    
1068
<?php
1069
	/* JavaScript to handle the advanced fields. */
1070
	if ($pkg['advanced_options'] == "enabled") {
1071
		echo "<script type=\"text/javascript\">\n";
1072
		echo "//<![CDATA[\n";
1073
		foreach ($js_array as $advfieldname) {
1074
			echo "function show_" . $advfieldname . "() {\n";
1075
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
1076
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
1077
			echo "}\n\n";
1078
		}
1079
		echo "//]]>\n";
1080
		echo "</script>\n";
1081
	}
1082
?>
1083

    
1084
<?php include("fend.inc"); ?>
1085
</body>
1086
</html>
1087

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

    
1160
function fixup_string($string) {
1161
	global $config;
1162
	// fixup #1: $myurl -> http[s]://ip_address:port/
1163
	$https = "";
1164
	$port = $config['system']['webguiport'];
1165
	if ($port <> "443" and $port <> "80") {
1166
		$urlport = ":" . $port;
1167
	} else {
1168
		$urlport = "";
1169
	}
1170

    
1171
	if ($config['system']['webgui']['protocol'] == "https") {
1172
		$https = "s";
1173
	}
1174
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
1175
	$newstring = str_replace("\$myurl", $myurl, $string);
1176
	$string = $newstring;
1177
	// fixup #2: $wanip
1178
	$curwanip = get_interface_ip();
1179
	$newstring = str_replace("\$wanip", $curwanip, $string);
1180
	$string = $newstring;
1181
	// fixup #3: $lanip
1182
	$lancfg = $config['interfaces']['lan'];
1183
	$lanip = $lancfg['ipaddr'];
1184
	$newstring = str_replace("\$lanip", $lanip, $string);
1185
	$string = $newstring;
1186
	// fixup #4: fix'r'up here.
1187
	return $newstring;
1188
}
1189

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

    
1235
			/* replace cr's */
1236
			$template_text = str_replace("\\n", "\n", $template_text);
1237

    
1238
			/* write out new template file */
1239
			$fout = fopen($filename, "w");
1240
			fwrite($fout, $template_text);
1241
			fclose($fout);
1242
		}
1243
	}
1244
}
1245

    
1246
/* Return html div fields */
1247
function display_advanced_field($fieldname) {
1248
	$div = "<div id='showadv_{$fieldname}'>\n";
1249
	$div .= "<input type='button' onclick='show_{$fieldname}()' value='" . gettext("Advanced") . "' /> - " . gettext("Show advanced option") ."</div>\n";
1250
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1251
	return $div;
1252
}
1253

    
1254
?>
(114-114/235)