Project

General

Profile

Download (39.3 KB) Statistics
| Branch: | Tag: | Revision:
1 d47013e1 Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 d47013e1 Scott Ullrich
/*
4
    pkg_edit.php
5 55c846c4 Marcello Coutinho
    Copyright (C) 2004-2012 Scott Ullrich <sullrich@gmail.com>
6 d47013e1 Scott Ullrich
    All rights reserved.
7
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29 1d333258 Scott Ullrich
/*
30
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
31
	pfSense_MODULE:	pkgs
32
*/
33 d47013e1 Scott Ullrich
34 6b07c15a Matthew Grooms
##|+PRIV
35
##|*IDENT=page-package-edit
36
##|*NAME=Package: Edit page
37
##|*DESCR=Allow access to the 'Package: Edit' page.
38
##|*MATCH=pkg_edit.php*
39
##|-PRIV
40
41 0089af7c Scott Ullrich
ini_set('max_execution_time', '0');
42
43 7a927e67 Scott Ullrich
require("guiconfig.inc");
44
require_once("functions.inc");
45
require_once("filter.inc");
46
require_once("shaper.inc");
47 f8e335a3 Scott Ullrich
require_once("pkg-utils.inc");
48 d47013e1 Scott Ullrich
49 14db714e Scott Ullrich
/* dummy stubs needed by some code that was MFC'd */
50 916b74c6 Scott Ullrich
function pfSenseHeader($location) { header("Location: " . $location); }
51 14db714e Scott Ullrich
52 d47013e1 Scott Ullrich
function gentitle_pkg($pgname) {
53
	global $pfSense_config;
54
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
55
}
56
57 55c846c4 Marcello Coutinho
function domTT_title($title_msg){
58
	if (!empty($title_msg)){
59
		$title_msg=preg_replace("/\s+/"," ",$title_msg);
60
        $title_msg=preg_replace("/'/","\'",$title_msg);
61 504a57b2 Charlie Root
		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');\"";
62 55c846c4 Marcello Coutinho
	}
63
}
64
65 d8c1a6c5 Scott Ullrich
$xml = htmlspecialchars($_GET['xml']);
66
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
67 d47013e1 Scott Ullrich
68 bef9f697 Renato Botelho
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
69 69eb2e29 Renato Botelho
70 bef9f697 Renato Botelho
if ($xml == "" || $xml_fullpath === false ||
71
    substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
72
            print_info_box_np(gettext("ERROR: No valid package defined."));
73 69eb2e29 Renato Botelho
            die;
74 d47013e1 Scott Ullrich
} else {
75 bef9f697 Renato Botelho
            $pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
76 d47013e1 Scott Ullrich
}
77 7c172009 Scott Ullrich
78
if($pkg['include_file'] <> "") {
79
	require_once($pkg['include_file']);
80
}
81
82 b91540da Scott Ullrich
if (!isset($pkg['adddeleteeditpagefields']))
83
	$only_edit = true;
84
else
85
	$only_edit = false;
86
87 a28c8b59 Scott Ullrich
$package_name = $pkg['menu'][0]['name'];
88
$section      = $pkg['menu'][0]['section'];
89 d47013e1 Scott Ullrich
$config_path  = $pkg['configpath'];
90 bb940538 Scott Ullrich
$name         = $pkg['name'];
91 b91540da Scott Ullrich
$title        = $pkg['title'];
92 96d9f5c9 Bill Marquette
$pgtitle      = $title;
93 d47013e1 Scott Ullrich
94 da7bf505 Scott Ullrich
$id = $_GET['id'];
95
if (isset($_POST['id']))
96 d8c1a6c5 Scott Ullrich
	$id = htmlspecialchars($_POST['id']);
97 98bcf1f8 Scott Ullrich
98 916b74c6 Scott Ullrich
// 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 5a61331a jim-p
103
if(!is_numeric($id)) {
104 6f3d2063 Renato Botelho
	header("Location: /");
105 5a61331a jim-p
	exit;
106
}
107
108 529ffadb Bill Marquette
if($pkg['custom_php_global_functions'] <> "")
109
        eval($pkg['custom_php_global_functions']);
110
111 31d27c6c Scott Ullrich
// grab the installedpackages->package_name section.
112 80d5bd41 Phil Lavin
if($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
113 7db8ff99 Colin Smith
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
114 da7bf505 Scott Ullrich
115 a2a7f74d jim-p
// If the first entry in the array is an empty <config/> tag, kill it.
116 80d5bd41 Phil Lavin
if ($config['installedpackages'] && (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) 
117 a2a7f74d jim-p
	&& ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == ""))
118
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
119
120 7db8ff99 Colin Smith
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
121 da7bf505 Scott Ullrich
122 19d360bf Scott Ullrich
if($_GET['savemsg'] <> "")
123 d8c1a6c5 Scott Ullrich
	$savemsg = htmlspecialchars($_GET['savemsg']);
124 f9a91638 Scott Ullrich
125 194b4e0a Colin Smith
if($pkg['custom_php_command_before_form'] <> "")
126
	eval($pkg['custom_php_command_before_form']);
127 f9a91638 Scott Ullrich
128 6483da5d Scott Ullrich
if ($_POST) {
129 2c51f293 jim-p
	$firstfield = "";
130
	$rows = 0;
131
132
	$input_errors = array();
133
	$reqfields = array();
134
	$reqfieldsn = array();
135
	foreach ($pkg['fields']['field'] as $field) {
136
		if (($field['type'] == 'input') && isset($field['required'])) {
137
			if($field['fieldname'])
138
				$reqfields[] = $field['fieldname'];
139
			if($field['fielddescr'])
140
				$reqfieldsn[] = $field['fielddescr'];
141
		}
142
	}
143 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors);
144 2c51f293 jim-p
145
	if ($pkg['custom_php_validation_command'])
146
		eval($pkg['custom_php_validation_command']);
147
148 b3235baa Scott Ullrich
	if($_POST['act'] == "del") {
149
		if($pkg['custom_delete_php_command']) {
150 5cd30ac3 Scott Ullrich
		    if($pkg['custom_php_command_before_form'] <> "")
151
			    eval($pkg['custom_php_command_before_form']);
152 b3235baa Scott Ullrich
		    eval($pkg['custom_delete_php_command']);
153
		}
154 7c172009 Scott Ullrich
		write_config($pkg['delete_string']);
155 facd08f9 Scott Ullrich
		// resync the configuration file code if defined.
156
		if($pkg['custom_php_resync_config_command'] <> "") {
157 86f3fc00 Scott Ullrich
			if($pkg['custom_php_command_before_form'] <> "")
158
				eval($pkg['custom_php_command_before_form']);
159
			eval($pkg['custom_php_resync_config_command']);
160 facd08f9 Scott Ullrich
		}
161 b3235baa Scott Ullrich
	} else {
162 2c51f293 jim-p
		if(!$input_errors && $pkg['custom_add_php_command']) {
163 90779bf7 Scott Ullrich
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
164 12123e25 Colin Smith
			?>
165 afe4a7d3 Erik Kristensen
166 539b4c44 Colin Smith
<?php include("head.inc"); ?>
167 3eaeb703 Scott Ullrich
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
168 2fe6c52b Colin Smith
<?php include("fbegin.inc"); ?>
169 fbd14b13 Scott Ullrich
<?php
170 3eaeb703 Scott Ullrich
			}
171
			if($pkg['preoutput']) echo "<pre>";
172
			eval($pkg['custom_add_php_command']);
173
			if($pkg['preoutput']) echo "</pre>";
174 b3235baa Scott Ullrich
		}
175 6483da5d Scott Ullrich
	}
176 e3c4b6b7 Scott Ullrich
177 eec70f21 Scott Ullrich
	// donotsave is enabled.  lets simply exit.
178 2c51f293 jim-p
	if(empty($pkg['donotsave'])) {
179
180
		// store values in xml configration file.
181
		if (!$input_errors) {
182
			$pkgarr = array();
183
			foreach ($pkg['fields']['field'] as $fields) {
184
				switch($fields['type']){
185
					case "rowhelper":
186
						// save rowhelper items.
187
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
188
						$rowhelpername="row";
189
						foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
190
							foreach($_POST as $key => $value){
191
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches))
192
									$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']]=$value;
193
							}
194
						break;
195
					default:
196
						$fieldname  = $fields['fieldname'];
197
						if ($fieldname == "interface_array") {
198
							$fieldvalue = $_POST[$fieldname];
199
						} elseif (is_array($_POST[$fieldname])) {
200
							$fieldvalue = implode(',', $_POST[$fieldname]);
201
						} else {
202
							$fieldvalue = trim($_POST[$fieldname]);
203
							if ($fields['encoding'] == 'base64')
204
								$fieldvalue = base64_encode($fieldvalue);
205 eec70f21 Scott Ullrich
						}
206 2c51f293 jim-p
						if($fieldname)
207
							$pkgarr[$fieldname] = $fieldvalue;
208 eec70f21 Scott Ullrich
					}
209 2c51f293 jim-p
			}
210 0e730fee Scott Ullrich
211 2c51f293 jim-p
			if (isset($id) && $a_pkg[$id])
212
				$a_pkg[$id] = $pkgarr;
213
			else
214
				$a_pkg[] = $pkgarr;
215 0e730fee Scott Ullrich
216 2c51f293 jim-p
			write_config($pkg['addedit_string']);
217
			// late running code
218
			if($pkg['custom_add_php_command_late'] <> "") {
219
			    eval($pkg['custom_add_php_command_late']);
220
			}
221 0e730fee Scott Ullrich
222 2c51f293 jim-p
			if (isset($pkg['filter_rules_needed']))
223
				filter_configure();
224 a9b2e638 Ermal Lu?i
225 2c51f293 jim-p
			// resync the configuration file code if defined.
226
			if($pkg['custom_php_resync_config_command'] <> "") {
227
			    eval($pkg['custom_php_resync_config_command']);
228
			}
229 facd08f9 Scott Ullrich
230 2c51f293 jim-p
			parse_package_templates();
231 dcbe6f52 Scott Ullrich
232 2c51f293 jim-p
			/* if start_command is defined, restart w/ this */
233
			if($pkg['start_command'] <> "")
234
			    exec($pkg['start_command'] . ">/dev/null 2&>1");
235 a485a6f5 Scott Ullrich
236 2c51f293 jim-p
			/* if restart_command is defined, restart w/ this */
237
			if($pkg['restart_command'] <> "")
238
			    exec($pkg['restart_command'] . ">/dev/null 2&>1");
239 a485a6f5 Scott Ullrich
240 2c51f293 jim-p
			if($pkg['aftersaveredirect'] <> "") {
241
			    pfSenseHeader($pkg['aftersaveredirect']);
242
			} elseif(!$pkg['adddeleteeditpagefields']) {
243 b3733e10 Colin Fleming
			    pfSenseHeader("pkg_edit.php?xml={$xml}&amp;id=0");
244 2c51f293 jim-p
			} elseif(!$pkg['preoutput']) {
245
			    pfSenseHeader("pkg.php?xml=" . $xml);
246
			}
247
			exit;
248
		} else {
249
			$get_from_post = true;
250 dcbe6f52 Scott Ullrich
		}
251 2c51f293 jim-p
	} elseif (!$input_errors) {
252 0e730fee Scott Ullrich
		exit;
253 e11aa161 Warren Baker
	}
254 da7bf505 Scott Ullrich
}
255
256 b91540da Scott Ullrich
if($pkg['title'] <> "") {
257 f0d1af93 Carlos Eduardo Ramos
	$edit = ($only_edit ? '' : ": " .  gettext("Edit"));
258 b91540da Scott Ullrich
	$title = $pkg['title'] . $edit;
259
}
260 83ddedcd Scott Ullrich
else
261 b91540da Scott Ullrich
	$title = gettext("Package Editor");
262 83ddedcd Scott Ullrich
263
$pgtitle = $title;
264
265 2690afba Colin Fleming
if ($pkg['custom_php_after_head_command']) {
266
	$closehead = false;
267
	include("head.inc");
268 7c172009 Scott Ullrich
	eval($pkg['custom_php_after_head_command']);
269 2690afba Colin Fleming
	echo "</head>\n";
270
}
271
else
272
	include("head.inc");
273 7c172009 Scott Ullrich
274 d47013e1 Scott Ullrich
?>
275
276 cf6a1f80 Warren Baker
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
277 b89a8cbc bruno
278 befa8be0 Colin Fleming
<?php include("fbegin.inc"); ?>
279
280 b89a8cbc bruno
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
281
<script type="text/javascript" src="/javascript/suggestions.js"></script>
282
283 2fe6c52b Colin Smith
<?php if($pkg['fields']['field'] <> "") { ?>
284 befa8be0 Colin Fleming
<script type="text/javascript">
285
//<![CDATA[
286 55c846c4 Marcello Coutinho
	//Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded
287
	jQuery(document).ready(function() {
288
		
289
		//Sortable function
290
		jQuery('#mainarea table tbody').sortable({
291
			items: 'tr.sortable',
292
			cursor: 'move',
293
			distance: 10,
294
			opacity: 0.8,
295
			helper: function(e,ui){  
296
				ui.children().each(function(){  
297
					jQuery(this).width(jQuery(this).width());  
298
				});
299
			return ui;  
300
			},
301
		});
302
		
303
		//delete current line jQuery function
304
		jQuery('#maintable td .delete').live('click', function() {
305
			//do not remove first line
306 2c0dd263 Marcello Coutinho
			if (jQuery("#maintable tr").length > 2){
307 55c846c4 Marcello Coutinho
				jQuery(this).parent().parent().remove();
308 2c0dd263 Marcello Coutinho
				return false;
309
			}
310 55c846c4 Marcello Coutinho
	    });
311
	    
312
		//add new line jQuery function
313
		jQuery('#mainarea table .add').click(function() {
314
			//get table size and assign as new id
315
			var c_id=jQuery("#maintable tr").length;
316
			var new_row=jQuery("table#maintable tr:last").html().replace(/(name|id)="(\w+)(\d+)"/g,"$1='$2"+c_id+"'");
317
			//apply new id to created line rowhelperid
318 befa8be0 Colin Fleming
			jQuery("table#maintable tr:last").after("<tr>"+new_row+"<\/tr>");
319 2c0dd263 Marcello Coutinho
			return false;
320 55c846c4 Marcello Coutinho
	    });
321
		// Call enablechange function
322
		enablechange();
323
	});
324
325
	function enablechange() {
326
	<?php
327
	foreach ($pkg['fields']['field'] as $field) {
328
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
329
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
330
331
			if (isset($field['enablefields'])) {
332
				foreach (explode(',', $field['enablefields']) as $enablefield) {
333
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
334
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
335
					echo "\t\t}\n";
336
				}
337 dcf6d563 Manoel Carvalho
			}
338 fbd14b13 Scott Ullrich
339 55c846c4 Marcello Coutinho
			if (isset($field['checkenablefields'])) {
340
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
341
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
342
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
343
					echo "\t\t}\n";
344
				}
345 dcf6d563 Manoel Carvalho
			}
346 fbd14b13 Scott Ullrich
347 55c846c4 Marcello Coutinho
			echo "\t}\n\telse {\n";
348 fbd14b13 Scott Ullrich
349 55c846c4 Marcello Coutinho
			if (isset($field['enablefields'])) {
350
				foreach (explode(',', $field['enablefields']) as $enablefield) {
351
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
352
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
353
					echo "\t\t}\n";
354
				}
355 dcf6d563 Manoel Carvalho
			}
356 fbd14b13 Scott Ullrich
357 55c846c4 Marcello Coutinho
			if (isset($field['checkenablefields'])) {
358
				foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
359
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
360
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
361
					echo "\t\t}\n";
362
				}
363 dcf6d563 Manoel Carvalho
			}
364 fbd14b13 Scott Ullrich
365 55c846c4 Marcello Coutinho
			echo "\t}\n";
366
		}
367 fbd14b13 Scott Ullrich
	}
368 55c846c4 Marcello Coutinho
	?>
369 2fe6c52b Colin Smith
}
370 7c8bce79 Colin Fleming
//]]>
371 2fe6c52b Colin Smith
</script>
372
<?php } ?>
373 55c846c4 Marcello Coutinho
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
374
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
375
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
376
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
377 befa8be0 Colin Fleming
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
378 eec70f21 Scott Ullrich
379 7c172009 Scott Ullrich
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
380
<form name="iform" action="pkg_edit.php" method="post">
381 2e606955 Colin Fleming
<input type="hidden" name="xml" value="<?= htmlspecialchars($xml) ?>" />
382 d47013e1 Scott Ullrich
<?php if ($savemsg) print_info_box($savemsg); ?>
383 2a83d2bd Colin Fleming
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="package edit">
384 7c061036 Scott Ullrich
<?php
385
if ($pkg['tabs'] <> "") {
386 e11aa161 Warren Baker
	$tab_array = array();
387
	foreach($pkg['tabs']['tab'] as $tab) {
388 90551807 Warren Baker
		if($tab['tab_level'])
389
			$tab_level = $tab['tab_level'];
390
		else
391
			$tab_level = 1;
392
		if(isset($tab['active'])) {
393
			$active = true;
394
		} else {
395
			$active = false;
396
		}
397 f8c462dd Warren Baker
		if(isset($tab['no_drop_down']))
398
			$no_drop_down = true;
399 90551807 Warren Baker
		$urltmp = "";
400
		if($tab['url'] <> "") $urltmp = $tab['url'];
401
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
402
403
 		$addresswithport = getenv("HTTP_HOST");
404
		$colonpos = strpos($addresswithport, ":");
405
		if ($colonpos !== False) {
406
			//my url is actually just the IP address of the pfsense box
407
			$myurl = substr($addresswithport, 0, $colonpos);
408
		} else {
409
			$myurl = $addresswithport;
410
		}
411
		// eval url so that above $myurl item can be processed if need be.
412
		$url = str_replace('$myurl', $myurl, $urltmp);
413
414
		$tab_array[$tab_level][] = array(
415
						$tab['text'],
416
						$active,
417
						$url
418
					);
419 e11aa161 Warren Baker
    	}
420 90551807 Warren Baker
421
	ksort($tab_array);
422 e14fbca4 Marcello Coutinho
	foreach($tab_array as $tabid => $tab) {
423 f8c462dd Warren Baker
		echo '<tr><td>';
424 e14fbca4 Marcello Coutinho
		display_top_tabs($tab, $no_drop_down, $tabid);
425 f8c462dd Warren Baker
		echo '</td></tr>';
426 90551807 Warren Baker
	}
427 7c061036 Scott Ullrich
}
428 55c846c4 Marcello Coutinho
429 7c061036 Scott Ullrich
?>
430 2a83d2bd Colin Fleming
<tr><td><div id="mainarea"><table id="t" class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
431 e11aa161 Warren Baker
<?php
432
	$cols = 0;
433
	$savevalue = gettext("Save");
434
	if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
435
	/* If a package's XML has <advanced_options/> configured, then setup 
436
	 * the table rows for the fields that have <advancedfield/> set.
437
	 * These fields will be placed below other fields in a seprate area titled 'Advanced Features'.
438
	 * These advanced fields are not normally configured and generally left to default to 'default settings'.
439
	 */
440 55c846c4 Marcello Coutinho
441 e11aa161 Warren Baker
	if ($pkg['advanced_options'] == "enabled") {
442 55c846c4 Marcello Coutinho
		$adv_filed_count = 0;
443
		$advanced = "<td>&nbsp;</td>";
444 8cd558b6 ayvis
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br /></td></tr>\n";
445 55c846c4 Marcello Coutinho
		}		
446 e11aa161 Warren Baker
	foreach ($pkg['fields']['field'] as $pkga) {
447 969a36ce Scott Ullrich
		if ($pkga['type'] == "sorting") 
448
			continue;
449 32487e42 Scott Ullrich
450
		if ($pkga['type'] == "listtopic") {
451 55c846c4 Marcello Coutinho
			$input = "<tr id='td_{$pkga['fieldname']}'><td>&nbsp;</td></tr>";
452 8cd558b6 ayvis
			$input .= "<tr id='tr_{$pkga['fieldname']}'><td colspan=\"2\" class=\"listtopic\">{$pkga['name']}<br /></td></tr>\n";
453 55c846c4 Marcello Coutinho
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
454
				$advanced .= $input;
455
				$adv_filed_count++;
456
				}
457 e11aa161 Warren Baker
			else
458 55c846c4 Marcello Coutinho
				echo $input;
459
			continue;
460 e11aa161 Warren Baker
		}
461 1624b5f1 Marcello Coutinho
462 55c846c4 Marcello Coutinho
		if($pkga['combinefields']=="begin"){
463
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
464
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
465
				$advanced .= $input;
466
			else
467
			  	echo $input;
468
			}
469 1624b5f1 Marcello Coutinho
470 e11aa161 Warren Baker
		$size = "";
471 55c846c4 Marcello Coutinho
		if (isset($pkga['dontdisplayname'])){
472
			$input="";
473
			if(!isset($pkga['combinefields']))
474
				$input .= "<tr valign='top' id='tr_{$pkga['fieldname']}'>";
475
			if(isset($pkga['usecolspan2']))
476
				$colspan="colspan='2'";
477
			else
478
				$input .= "<td width='22%' class='vncell{$req}'>&nbsp;</td>";
479
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
480
				$advanced .= $input;
481
				$adv_filed_count++;
482
				}
483
			else
484
				echo $input;
485
			}
486
		else if (!isset($pkga['placeonbottom'])){
487 e11aa161 Warren Baker
			unset($req);
488
			if (isset($pkga['required']))
489
				$req = 'req';
490 2e606955 Colin Fleming
			$input= "<tr><td valign='top' width=\"22%\" class=\"vncell{$req}\">";
491 55c846c4 Marcello Coutinho
			$input .= fixup_string($pkga['fielddescr']);
492
			$input .= "</td>";
493
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
494
				$advanced .= $input;
495
				$adv_filed_count++;
496
				}
497
			else 
498
				echo $input;
499 e11aa161 Warren Baker
		}
500 55c846c4 Marcello Coutinho
		if($pkga['combinefields']=="begin"){
501 2a83d2bd Colin Fleming
			$input="<td class=\"vncell\"><table summary=\"advanced\">";
502 55c846c4 Marcello Coutinho
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
503
				$advanced .= $input;
504
			else
505
			  	echo $input;
506
			}
507 1624b5f1 Marcello Coutinho
508 55c846c4 Marcello Coutinho
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
509
		if (!isset($pkga['placeonbottom'])){
510
			$input="<td valign='top' {$colspan} {$class}>";
511
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)){
512
				$advanced .= $input;
513
				$adv_filed_count++;
514
				}
515 e11aa161 Warren Baker
			else
516 55c846c4 Marcello Coutinho
				echo $input;
517
		}
518 1624b5f1 Marcello Coutinho
519 31d27c6c Scott Ullrich
		// if user is editing a record, load in the data.
520 7c172009 Scott Ullrich
		$fieldname = $pkga['fieldname'];
521 fbd14b13 Scott Ullrich
		if ($get_from_post) {
522 7c172009 Scott Ullrich
			$value = $_POST[$fieldname];
523 fbd14b13 Scott Ullrich
			if (is_array($value)) $value = implode(',', $value);
524 e11aa161 Warren Baker
		} else {
525 91c31339 Warren Baker
			if (isset($id) && $a_pkg[$id])
526 7c172009 Scott Ullrich
				$value = $a_pkg[$id][$fieldname];
527 b91540da Scott Ullrich
			else
528
				$value = $pkga['default_value'];
529 31d27c6c Scott Ullrich
		}
530 55c846c4 Marcello Coutinho
		switch($pkga['type']){
531
			case "input":
532
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
533 605ae553 Renato Botelho
				$input = "<input {$size} id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formfld unknown' value=\"" . htmlspecialchars($value) ."\" />\n";
534 8cd558b6 ayvis
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
535 55c846c4 Marcello Coutinho
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
536
					$js_array[] = $pkga['fieldname'];
537
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
538
					}
539
				else
540
					echo $input;
541
				break;
542 1624b5f1 Marcello Coutinho
543 55c846c4 Marcello Coutinho
			case "password":
544
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
545 605ae553 Renato Botelho
				$input = "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' name='" . $pkga['fieldname'] . "' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
546 8cd558b6 ayvis
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
547 55c846c4 Marcello Coutinho
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
548
					$js_array[] = $pkga['fieldname'];
549
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
550
					}
551
				else
552
					echo $input;
553
				break;
554 1624b5f1 Marcello Coutinho
555 55c846c4 Marcello Coutinho
			case "info":
556
				$input = fixup_string($pkga['description']) . "\n";
557
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
558
					$js_array[] = $pkga['fieldname'];
559
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
560
					}
561
				else
562
					echo $input;
563
				break;
564 1624b5f1 Marcello Coutinho
565 55c846c4 Marcello Coutinho
			case "select":
566
				$fieldname = $pkga['fieldname'];
567
				if (isset($pkga['multiple'])) {
568
					$multiple = 'multiple="multiple"';
569
					$items = explode(',', $value);
570
					$fieldname .= "[]";
571
				} else {
572
					$multiple = '';
573
					$items = array($value);
574 e11aa161 Warren Baker
				}
575 55c846c4 Marcello Coutinho
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
576
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
577 2e606955 Colin Fleming
				$input = "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange name=\"$fieldname\">\n";
578 e11aa161 Warren Baker
				foreach ($pkga['options']['option'] as $opt) {
579 55c846c4 Marcello Coutinho
					$selected = (in_array($opt['value'], $items) ? 'selected="selected"' : '');
580 2e606955 Colin Fleming
					$input .= "\t<option value=\"{$opt['value']}\" {$selected}>{$opt['name']}</option>\n";
581 55c846c4 Marcello Coutinho
					}
582
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
583
                if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
584
					$js_array[] = $pkga['fieldname'];
585
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
586
					$advanced .= "</div>\n";
587
					}
588
				else
589
					echo $input;
590
				break;
591 1624b5f1 Marcello Coutinho
592 55c846c4 Marcello Coutinho
			case "select_source":
593
				$fieldname = $pkga['fieldname'];
594
				if (isset($pkga['multiple'])) {
595
					$multiple = 'multiple="multiple"';
596
					$items = explode(',', $value);
597
					$fieldname .= "[]";
598 e11aa161 Warren Baker
				} else {
599 55c846c4 Marcello Coutinho
					$multiple = '';
600
					$items = array($value);
601 e11aa161 Warren Baker
				}
602 55c846c4 Marcello Coutinho
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
603
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
604 2e606955 Colin Fleming
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} name=\"{$fieldname}\">\n";
605 55c846c4 Marcello Coutinho
606
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
607
					$js_array[] = $pkga['fieldname'];
608
					$advanced .= display_advanced_field($pkga['fieldname']) .$input;
609
					$advanced .= "</div>\n";
610 e11aa161 Warren Baker
				} else {
611 55c846c4 Marcello Coutinho
					echo $input;
612 e11aa161 Warren Baker
				}
613 55c846c4 Marcello Coutinho
				$source_url = $pkga['source'];
614
				eval("\$pkg_source_txt = &$source_url;");
615 1624b5f1 Marcello Coutinho
				$input="";
616 504a57b2 Charlie Root
				#check if show disable option is present on xml
617
				if(isset($pkga['show_disable_value'])){
618
					array_push($pkg_source_txt, array(($pkga['source_name']? $pkga['source_name'] : $pkga['name'])=> $pkga['show_disable_value'],
619
													  ($pkga['source_value']? $pkga['source_value'] : $pkga['value'])=> $pkga['show_disable_value']));
620
					}
621 55c846c4 Marcello Coutinho
				foreach ($pkg_source_txt as $opt) {
622
					$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
623
					$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
624
					$selected = (in_array($source_value, $items)? 'selected="selected"' : '' );
625 2e606955 Colin Fleming
					$input  .= "\t<option value=\"{$source_value}\" $selected>{$source_name}</option>\n";
626 55c846c4 Marcello Coutinho
					}
627 1624b5f1 Marcello Coutinho
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
628
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
629
					$advanced .= $input;
630
				else
631
					echo $input;
632 55c846c4 Marcello Coutinho
				break;
633 1624b5f1 Marcello Coutinho
634 55c846c4 Marcello Coutinho
			case "vpn_selection" :
635
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
636 e11aa161 Warren Baker
				foreach ($config['ipsec']['phase1'] as $vpn) {
637 55c846c4 Marcello Coutinho
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
638
					}
639
				$input .= "</select>\n";
640 8cd558b6 ayvis
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
641 55c846c4 Marcello Coutinho
642
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
643
					$js_array[] = $pkga['fieldname'];
644
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
645
					$advanced .= "</div>\n";
646
					}
647
				else
648
					echo $input;
649
				break;
650 1624b5f1 Marcello Coutinho
651 55c846c4 Marcello Coutinho
			case "checkbox":
652 2e606955 Colin Fleming
				$checkboxchecked =($value == "on" ? " checked=\"checked\"" : "");
653 cfa845a9 Luiz Gustavo Costa (gugabsd)
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
654 55c846c4 Marcello Coutinho
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
655
					$onclick = ' onclick="javascript:enablechange();"';
656 2e606955 Colin Fleming
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick} {$onchange} />\n";
657 8cd558b6 ayvis
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
658 55c846c4 Marcello Coutinho
659
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
660
					$js_array[] = $pkga['fieldname'];
661
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
662
					$advanced .= "</div>\n";
663
					}
664
				else
665
					echo $input;
666
				break;
667 1624b5f1 Marcello Coutinho
668 55c846c4 Marcello Coutinho
			case "textarea":
669
				if($pkga['rows'])
670
					$rows = " rows='{$pkga['rows']}' ";
671
				if($pkga['cols'])
672
					$cols = " cols='{$pkga['cols']}' ";
673
				if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value))
674
					$value = base64_decode($value);
675
				$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');		  
676
				$input = "<textarea {$rows} {$cols} name='{$pkga['fieldname']}'{$wrap}>{$value}</textarea>\n";
677 8cd558b6 ayvis
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
678 55c846c4 Marcello Coutinho
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
679
					$js_array[] = $pkga['fieldname'];
680
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
681
					$advanced .= "</div>\n";
682
					}
683
				else
684
					echo $input;
685
				break;
686 b89a8cbc bruno
687
			case "aliases":
688
				// Use xml tag <typealiases> to filter type aliases
689
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
690
				$fieldname = $pkga['fieldname'];
691
				$a_aliases = &$config['aliases']['alias'];
692
				$addrisfirst = 0;
693
				$aliasesaddr = "";
694
				$value = "value='{$value}'";
695
696
				if(isset($a_aliases)) {
697
					if(!empty($pkga['typealiases'])) {
698
						foreach($a_aliases as $alias)
699
							if($alias['type'] == $pkga['typealiases']) {
700
								if($addrisfirst == 1) $aliasesaddr .= ",";
701
								$aliasesaddr .= "'" . $alias['name'] . "'";
702
								$addrisfirst = 1;
703
							}
704
					} else {
705
						foreach($a_aliases as $alias) {
706
							if($addrisfirst == 1) $aliasesaddr .= ",";
707
							$aliasesaddr .= "'" . $alias['name'] . "'";
708
							$addrisfirst = 1;
709
						}
710
					}
711
				}
712
713 2e606955 Colin Fleming
				$input = "<input name='{$fieldname}' type='text' class='formfldalias' id='{$fieldname}' {$size} {$value} />\n<br />";
714 b89a8cbc bruno
				$input .= fixup_string($pkga['description']) . "\n";
715
716
				$script = "<script type='text/javascript'>\n";
717 8df076fe Colin Fleming
				$script .= "//<![CDATA[\n";
718 b89a8cbc bruno
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
719
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
720 befa8be0 Colin Fleming
				$script .= "//]]>\n";
721 b89a8cbc bruno
				$script .= "</script>";
722
723
				echo $input;
724
				echo $script;
725
                                break;
726
727 55c846c4 Marcello Coutinho
			case "interfaces_selection":
728 e14fbca4 Marcello Coutinho
				$ips=array();
729
				$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
730
				if (is_array($config['interfaces']))
731
					foreach ($config['interfaces'] as $iface_key=>$iface_value){
732
						if (isset($iface_value['enable']) && ! preg_match("/$interface_regex/",$iface_key)){
733 2d9063af ccesario
							$iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key));
734
							if (isset($pkga['showips']))
735
								$iface_description .= " address";
736 e14fbca4 Marcello Coutinho
							$ips[]=array('ip'=> $iface_key, 'description'=> $iface_description);
737
							}
738
					}
739
				if (is_array($config['virtualip']) && isset($pkga['showvirtualips']))
740
					foreach ($config['virtualip']['vip'] as $vip){
741
						if (! preg_match("/$interface_regex/",$vip['interface']))
742
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
743
						  switch ($vip['mode']){
744
							case "ipalias":
745
							case "carp":
746
									$ips[]=array(   'ip'=> $vip['subnet'],'description' => "{$vip['subnet']} $vip_description");
747
								break;
748
							case "proxyarp":
749
								if ($vip['type']=="network"){
750
									$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
751
									$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
752
									$len = $end - $start;
753
									for ($i = 0; $i <= $len; $i++)
754
										$ips[]= array('ip'=>long2ip32($start+$i),'description'=> long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
755
									}
756
								else{
757
									$ips[]= array('ip'=>$vip['subnet'],'description'=> "{$vip['subnet']} $vip_description");
758
									}
759
								break;
760
							}
761
					}
762
				sort($ips);
763
				if (isset($pkga['showlistenall']))
764
					array_unshift($ips,array('ip'=> 'All', 'description'=> 'Listen on All interfaces/ip addresses '));
765
				if (! preg_match("/$interface_regex/","loopback")){
766
					$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
767
					array_push($ips,array('ip'=> 'lo0', 'description'=> $iface_description));
768
					}
769
770
				#show interfaces array on gui
771 55c846c4 Marcello Coutinho
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
772
				$multiple = '';
773
				$fieldname = $pkga['fieldname'];
774
				if (isset($pkga['multiple'])) {
775
					$fieldname .= '[]';
776 2e606955 Colin Fleming
					$multiple = 'multiple="multiple"';
777 e14fbca4 Marcello Coutinho
					}
778 55c846c4 Marcello Coutinho
				$input = "<select id='{$pkga['fieldname']}' name=\"{$fieldname}\" {$size} {$multiple}>\n";
779
				if(is_array($value))
780
					$values = $value;
781 e11aa161 Warren Baker
				else
782 55c846c4 Marcello Coutinho
					$values  =  explode(',',  $value);
783 e14fbca4 Marcello Coutinho
				foreach($ips as $iface){
784 2e606955 Colin Fleming
					$selected = (in_array($iface['ip'], $values) ? 'selected="selected"' : '');
785 e14fbca4 Marcello Coutinho
					$input .= "<option value=\"{$iface['ip']}\" {$selected}>{$iface['description']}</option>\n";
786
					}
787
				$input .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
788 55c846c4 Marcello Coutinho
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
789 e14fbca4 Marcello Coutinho
					$advanced .= $input;
790 55c846c4 Marcello Coutinho
				else
791
					echo $input;
792
				break;
793 1624b5f1 Marcello Coutinho
794 55c846c4 Marcello Coutinho
			case "radio":
795 2e606955 Colin Fleming
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
796 55c846c4 Marcello Coutinho
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
797
					$advanced .= $input;
798
				else
799
					echo $input;
800
					break;
801 1624b5f1 Marcello Coutinho
802 55c846c4 Marcello Coutinho
			case "button":
803 2e606955 Colin Fleming
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}' />\n";
804 55c846c4 Marcello Coutinho
				if(isset($pkga['placeonbottom']))
805
					$pkg_buttons .= $input;
806
				else
807 91f026b0 ayvis
					echo $input ."\n<br />" . fixup_string($pkga['description']) . "\n";
808 55c846c4 Marcello Coutinho
				break;
809 1624b5f1 Marcello Coutinho
810 55c846c4 Marcello Coutinho
			case "rowhelper":
811
				#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
812
				$rowhelpername="row";
813
				?>
814 befa8be0 Colin Fleming
				<script type="text/javascript">
815
				//<![CDATA[
816 55c846c4 Marcello Coutinho
				<?php
817
					$rowcounter = 0;
818
					$fieldcounter = 0;
819
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
820
						echo "rowname[{$fieldcounter}] = \"{$rowhelper['fieldname']}\";\n";
821
						echo "rowtype[{$fieldcounter}] = \"{$rowhelper['type']}\";\n";
822
						echo "rowsize[{$fieldcounter}] = \"{$rowhelper['size']}\";\n";
823
						$fieldcounter++;
824
					}
825
				?>
826 befa8be0 Colin Fleming
				//]]>
827 55c846c4 Marcello Coutinho
				</script>
828 2a83d2bd Colin Fleming
				<table id="maintable" summary="main table">
829 55c846c4 Marcello Coutinho
				<tr id='<?="tr_{$pkga['fieldname']}";?>'>
830
				<?php
831
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
832 8612c539 Colin Fleming
					  echo "<td ".domTT_title($rowhelper['description'])."><b>" . fixup_string($rowhelper['fielddescr']) . "</b></td>\n";
833 55c846c4 Marcello Coutinho
					}
834 1624b5f1 Marcello Coutinho
835 55c846c4 Marcello Coutinho
					$rowcounter = 0;
836
					$trc = 0;
837 1624b5f1 Marcello Coutinho
838 55c846c4 Marcello Coutinho
					//Use assigned $a_pkg or create an empty array to enter loop
839
					if(isset($a_pkg[$id][$rowhelpername]))
840
						$saved_rows=$a_pkg[$id][$rowhelpername];
841
					else
842
						$saved_rows[]=array();
843 1624b5f1 Marcello Coutinho
844 55c846c4 Marcello Coutinho
					foreach($saved_rows as $row) {
845
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
846 31d27c6c Scott Ullrich
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
847 0c8cdb25 Warren Baker
							unset($value);
848 31d27c6c Scott Ullrich
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
849
							$fieldname = $rowhelper['fieldname'];
850
							// if user is editing a record, load in the data.
851 bb940538 Scott Ullrich
							if (isset($id) && $a_pkg[$id]) {
852 7db8ff99 Colin Smith
								$value = $row[$fieldname];
853 bb940538 Scott Ullrich
							}
854 31d27c6c Scott Ullrich
							$options = "";
855
							$type = $rowhelper['type'];
856 55c846c4 Marcello Coutinho
							$description = $rowhelper['description'];
857 31d27c6c Scott Ullrich
							$fieldname = $rowhelper['fieldname'];
858 e54626e2 Ermal Lu?i
							if($type == "option")
859
								$options = &$rowhelper['options']['option'];
860 7e542bd1 Scott Ullrich
							if($rowhelper['size']) 
861 4c09b655 Scott Ullrich
								$size = $rowhelper['size'];
862 e54626e2 Ermal Lu?i
							else if ($pkga['size'])
863
								$size = $pkga['size'];
864 4c09b655 Scott Ullrich
							else
865
								$size = "8";
866 e70e0b76 Scott Ullrich
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
867 1624b5f1 Marcello Coutinho
868 bb940538 Scott Ullrich
							$text = "";
869
							$trc++;
870 55c846c4 Marcello Coutinho
							}
871 31d27c6c Scott Ullrich
						$rowcounter++;
872
						echo "<td>";
873 befa8be0 Colin Fleming
						#echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" alt=\"remove\" /></a>";
874
						echo "<a class='delete' href=\"#\"><img border='0' src='./themes/{$g['theme']}/images/icons/icon_x.gif' alt='delete' /></a>";
875 eec70f21 Scott Ullrich
						echo "</td>\n";
876 55c846c4 Marcello Coutinho
						}
877
				?>
878 81ecdc6c Colin Fleming
				</tr>
879 55c846c4 Marcello Coutinho
				<tbody></tbody>
880
				</table>
881
	
882 8cd558b6 ayvis
				<!-- <br /><a onclick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?#= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>-->
883
				<br /><a class="add" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>
884
				<br /><?php if($pkga['description'] != "") echo $pkga['description']; ?>
885 befa8be0 Colin Fleming
				<script type="text/javascript">
886
				//<![CDATA[
887 55c846c4 Marcello Coutinho
				field_counter_js = <?= $fieldcounter ?>;
888
				rows = <?= $rowcounter ?>;
889
				totalrows = <?php echo $rowcounter; ?>;
890
				loaded = <?php echo $rowcounter; ?>;
891
				//typesel_change();
892 befa8be0 Colin Fleming
				//]]>
893 55c846c4 Marcello Coutinho
				</script>
894
		
895
				<?php
896
				break;
897
		    }
898
		#check typehint value
899
	   	if($pkga['typehint'])
900
	   		echo " " . $pkga['typehint'];
901
	   	#check combinefields options
902
     	if (isset($pkga['combinefields'])){
903
     		$input="</td>";
904
			if ($pkga['combinefields']=="end")
905
           		$input.="</table></td></tr>";
906
      		}
907
     	else{
908
			$input= "</td></tr>";
909
			if($pkga['usecolspan2'])
910 8cd558b6 ayvis
				$input.= "</tr><br />";
911 55c846c4 Marcello Coutinho
	     	}
912
   	 	if(isset($pkga['advancedfield']) && isset($adv_filed_count))
913
			$advanced .= "{$input}\n";
914
		else
915
			echo "{$input}\n";
916
		#increment counter
917
		$i++;
918
  		}
919
920
  	#print advanced settings if any after reading all fields
921
	if (isset($advanced) && $adv_filed_count > 0)
922
		echo $advanced;
923
  
924
	?>
925 b6e87566 Scott Ullrich
  <tr>
926
	<td>&nbsp;</td>
927
  </tr>
928 d47013e1 Scott Ullrich
  <tr>
929
    <td width="22%" valign="top">&nbsp;</td>
930
    <td width="78%">
931 2e606955 Colin Fleming
    <div id="buttons">
932 55c846c4 Marcello Coutinho
		<?php
933
		if($pkg['note'] != ""){
934
			echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
935
			}
936
		//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
937 605ae553 Renato Botelho
		echo "<input name='id' type='hidden' value=\"" . htmlspecialchars($id) . "\" />";
938
		echo "<input name='Submit' type='submit' class='formbtn' value=\"" . htmlspecialchars($savevalue) . "\" />\n{$pkg_buttons}\n";
939 55c846c4 Marcello Coutinho
		if (!$only_edit){
940 62424bdb Renato Botelho
			echo "<input class=\"formbtn\" type=\"button\" value=\"".gettext("Cancel")."\" onclick=\"window.location.href='" . $_SERVER['HTTP_REFERER'] . "'\" />";
941 55c846c4 Marcello Coutinho
			}
942
		?>
943
	</div>
944 d47013e1 Scott Ullrich
    </td>
945
  </tr>
946 55c846c4 Marcello Coutinho
947 e11aa161 Warren Baker
</table>
948 098686af bcyrill
</div></td></tr>
949 d47013e1 Scott Ullrich
</table>
950 7c172009 Scott Ullrich
</form>
951 7c061036 Scott Ullrich
952 7c172009 Scott Ullrich
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
953 95736b54 Scott Ullrich
954 e11aa161 Warren Baker
<?php
955
	/* JavaScript to handle the advanced fields. */
956 d92b3311 Warren Baker
	if ($pkg['advanced_options'] == "enabled") {
957 e11aa161 Warren Baker
		echo "<script type=\"text/javascript\">\n";
958 befa8be0 Colin Fleming
		echo "//<![CDATA[\n";
959 e11aa161 Warren Baker
		foreach($js_array as $advfieldname) {
960
			echo "function show_" . $advfieldname . "() {\n";
961 55c846c4 Marcello Coutinho
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
962
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
963 e11aa161 Warren Baker
			echo "}\n\n";
964
		}
965 befa8be0 Colin Fleming
		echo "//]]>\n";
966 e11aa161 Warren Baker
		echo "</script>\n";
967
	}
968
?>
969
970 d47013e1 Scott Ullrich
<?php include("fend.inc"); ?>
971
</body>
972
</html>
973
974 31d27c6c Scott Ullrich
<?php
975
/*
976
 * ROW Helpers function
977
 */
978 e70e0b76 Scott Ullrich
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
979 db3829e1 Scott Ullrich
	global $text, $config;
980 31d27c6c Scott Ullrich
	echo "<td>\n";
981 55c846c4 Marcello Coutinho
	switch($type){
982
		case "input":
983 605ae553 Renato Botelho
			echo "<input size='{$size}' name='{$fieldname}{$trc}' id='{$fieldname}{$trc}' class='formfld unknown' value=\"" . htmlspecialchars($value) . "\" />\n";
984 55c846c4 Marcello Coutinho
			break;
985
		case "checkbox":
986 2e606955 Colin Fleming
			echo "<input size='{$size}' type='checkbox' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' value='ON' ".($value?"CHECKED":"")." />\n";
987 55c846c4 Marcello Coutinho
			break;
988
		case "password":
989 605ae553 Renato Botelho
			echo "<input size='{$size}' type='password' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
990 55c846c4 Marcello Coutinho
			break;
991
		case "textarea":
992
			echo "<textarea rows='2' cols='12' id='{$fieldname}{$trc}' class='formfld unknown' name='{$fieldname}{$trc}'>{$value}</textarea>\n";
993
		case "select":
994
			echo "<select style='height:22px;'  id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$title}>\n";
995
			foreach($rowhelper['options']['option'] as $rowopt) {
996
				$text .= "<option value='{$rowopt['value']}'>{$rowopt['name']}</option>";
997 2e606955 Colin Fleming
				echo "<option value='{$rowopt['value']}'".($rowopt['value'] == $value?" selected=\"selected\"":"").">{$rowopt['name']}</option>\n";
998 55c846c4 Marcello Coutinho
				}
999
			echo "</select>\n";
1000
			break;
1001
		case "interfaces_selection":
1002 85a46fbd whizkidzz
			$size = ($size ? "size=\"{$size}\"" : '');
1003
			$multiple = '';
1004
			if (isset($rowhelper['multiple'])) {
1005
				$fieldname .= '[]';
1006 5655d48b Colin Fleming
				$multiple = "multiple=\"multiple\"";
1007 85a46fbd whizkidzz
			}
1008 504a57b2 Charlie Root
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
1009 85a46fbd whizkidzz
			$ifaces = get_configured_interface_with_descr();
1010
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
1011
			if (!empty($additional_ifaces))
1012
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
1013
			if(is_array($value))
1014
				$values = $value;
1015
			else
1016
				$values  =  explode(',',  $value);
1017
			$ifaces["lo0"] = "loopback";
1018
			echo "<option><name></name><value></value></option>/n";
1019
			foreach($ifaces as $ifname => $iface) {
1020 5655d48b Colin Fleming
				$text .="<option value=\"{$ifname}\">$iface</option>";
1021 2e606955 Colin Fleming
				echo "<option value=\"{$ifname}\" ".(in_array($ifname, $values) ? 'selected="selected"' : '').">{$iface}</option>\n";
1022 55c846c4 Marcello Coutinho
				}
1023 85a46fbd whizkidzz
			echo "</select>\n";
1024 55c846c4 Marcello Coutinho
			break;
1025
		case "select_source":
1026 504a57b2 Charlie Root
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}'>\n";
1027
			if(isset($rowhelper['show_disable_value']))
1028
				echo "<option value='{$rowhelper['show_disable_value']}'>{$rowhelper['show_disable_value']}</option>\n";
1029 55c846c4 Marcello Coutinho
			$source_url = $rowhelper['source'];
1030
			eval("\$pkg_source_txt = &$source_url;");
1031
			foreach($pkg_source_txt as $opt) {
1032
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
1033
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
1034
				$text .= "<option value='{$source_value}'>{$source_name}</option>";
1035 2e606955 Colin Fleming
				echo "<option value='{$source_value}'".($source_value == $value?" selected=\"selected\"":"").">{$source_name}</option>\n";
1036 55c846c4 Marcello Coutinho
				}
1037
			echo "</select>\n";
1038
			break;		
1039 db3829e1 Scott Ullrich
		}
1040 8612c539 Colin Fleming
	echo "</td>\n";
1041 31d27c6c Scott Ullrich
}
1042
1043 d51f86e0 Scott Ullrich
function fixup_string($string) {
1044 d2133701 Scott Ullrich
	global $config;
1045 d51f86e0 Scott Ullrich
	// fixup #1: $myurl -> http[s]://ip_address:port/
1046
	$https = "";
1047
	$port = $config['system']['webguiport'];
1048 63637de9 Bill Marquette
	if($port <> "443" and $port <> "80")
1049
		$urlport = ":" . $port;
1050
	else
1051
		$urlport = "";
1052 3c616886 Scott Ullrich
1053 58362f9d jim-p
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
1054 63637de9 Bill Marquette
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
1055 d51f86e0 Scott Ullrich
	$newstring = str_replace("\$myurl", $myurl, $string);
1056 d2133701 Scott Ullrich
	$string = $newstring;
1057
	// fixup #2: $wanip
1058 85a5da13 Ermal Luçi
	$curwanip = get_interface_ip();
1059 d2133701 Scott Ullrich
	$newstring = str_replace("\$wanip", $curwanip, $string);
1060
	$string = $newstring;
1061
	// fixup #3: $lanip
1062
	$lancfg = $config['interfaces']['lan'];
1063
	$lanip = $lancfg['ipaddr'];
1064
	$newstring = str_replace("\$lanip", $lanip, $string);
1065
	$string = $newstring;
1066
	// fixup #4: fix'r'up here.
1067 d51f86e0 Scott Ullrich
	return $newstring;
1068
}
1069
1070 dcbe6f52 Scott Ullrich
/*
1071
 *  Parse templates if they are defined
1072
 */
1073
function parse_package_templates() {
1074
	global $pkg, $config;
1075 767a716e Scott Ullrich
	$rows = 0;
1076 dcbe6f52 Scott Ullrich
	if($pkg['templates']['template'] <> "")
1077
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
1078 55c846c4 Marcello Coutinho
			$filename = $pkg_template_row['filename'];
1079
			$template_text = $pkg_template_row['templatecontents'];
1080
			$firstfield = "";
1081
			/* calculate total row helpers count and */
1082
			/* change fields defined as fieldname_fieldvalue to their value */
1083
			foreach ($pkg['fields']['field'] as $fields) {
1084
				switch($fields['type']){
1085
					case "rowhelper":
1086
					// save rowhelper items.
1087
					$row_helper_total_rows = 0;
1088
					$row_helper_data = "";
1089
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
1090
						foreach($_POST as $key => $value){
1091
							if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches)){
1092
								$row_helper_total_rows++;
1093
								$row_helper_data .= $value;
1094
								$sep = "";
1095
								ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
1096 5aa68a55 Renato Botelho
								foreach ($sep as $se) $separator = $se;
1097
								if($separator <> "") {
1098
							    	$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
1099
							    	$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
1100 55c846c4 Marcello Coutinho
									}
1101
								$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
1102
								}
1103
							}
1104
					break;
1105
				default:
1106
					$fieldname  = $fields['fieldname'];
1107
					$fieldvalue = $_POST[$fieldname];
1108
					$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
1109 dcbe6f52 Scott Ullrich
				}
1110
			}
1111
		/* replace $domain_total_rows with total rows */
1112
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
1113 1624b5f1 Marcello Coutinho
1114 dcbe6f52 Scott Ullrich
		/* replace cr's */
1115
		$template_text = str_replace("\\n", "\n", $template_text);
1116
1117
		/* write out new template file */
1118
		$fout = fopen($filename,"w");
1119
		fwrite($fout, $template_text);
1120
		fclose($fout);
1121
	    }
1122
}
1123
1124 e11aa161 Warren Baker
/* Return html div fields */
1125
function display_advanced_field($fieldname) {
1126 55c846c4 Marcello Coutinho
	$div = "<div id='showadv_{$fieldname}'>\n";
1127 2e606955 Colin Fleming
	$div .= "<input type='button' onclick='show_{$fieldname}()' value='" . gettext("Advanced") . "' /> - " . gettext("Show advanced option") ."</div>\n";
1128 55c846c4 Marcello Coutinho
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1129 e11aa161 Warren Baker
	return $div;
1130
}
1131
1132 b89a8cbc bruno
?>