Project

General

Profile

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