Project

General

Profile

Download (26.4 KB) Statistics
| Branch: | Tag: | Revision:
1 d47013e1 Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 d47013e1 Scott Ullrich
/*
4
    pkg_edit.php
5
    Copyright (C) 2004 Scott Ullrich
6
    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
30 6b07c15a Matthew Grooms
##|+PRIV
31
##|*IDENT=page-package-edit
32
##|*NAME=Package: Edit page
33
##|*DESCR=Allow access to the 'Package: Edit' page.
34
##|*MATCH=pkg_edit.php*
35
##|-PRIV
36
37
38 9f9dcd98 Scott Ullrich
require_once("guiconfig.inc");
39 f8e335a3 Scott Ullrich
require_once("pkg-utils.inc");
40 d47013e1 Scott Ullrich
41 14db714e Scott Ullrich
/* dummy stubs needed by some code that was MFC'd */
42 916b74c6 Scott Ullrich
function pfSenseHeader($location) { header("Location: " . $location); }
43 14db714e Scott Ullrich
44 d47013e1 Scott Ullrich
function gentitle_pkg($pgname) {
45
	global $pfSense_config;
46
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
47
}
48
49 d8c1a6c5 Scott Ullrich
$xml = htmlspecialchars($_GET['xml']);
50
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
51 d47013e1 Scott Ullrich
52
if($xml == "") {
53 b91540da Scott Ullrich
            print_info_box_np(gettext("ERROR: No package defined."));
54 d47013e1 Scott Ullrich
            die;
55
} else {
56 19a11678 Colin Smith
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
57 d47013e1 Scott Ullrich
}
58 7c172009 Scott Ullrich
59
if($pkg['include_file'] <> "") {
60
	require_once($pkg['include_file']);
61
}
62
63 b91540da Scott Ullrich
if (!isset($pkg['adddeleteeditpagefields']))
64
	$only_edit = true;
65
else
66
	$only_edit = false;
67
68 a28c8b59 Scott Ullrich
$package_name = $pkg['menu'][0]['name'];
69
$section      = $pkg['menu'][0]['section'];
70 d47013e1 Scott Ullrich
$config_path  = $pkg['configpath'];
71 bb940538 Scott Ullrich
$name         = $pkg['name'];
72 b91540da Scott Ullrich
$title        = $pkg['title'];
73 96d9f5c9 Bill Marquette
$pgtitle      = $title;
74 d47013e1 Scott Ullrich
75 da7bf505 Scott Ullrich
$id = $_GET['id'];
76
if (isset($_POST['id']))
77 d8c1a6c5 Scott Ullrich
	$id = htmlspecialchars($_POST['id']);
78 916b74c6 Scott Ullrich
	
79
// Not posting?  Then user is editing a record. There must be a valid id
80
// when editing a record.
81
if(!$id && !$_POST)
82
	$id = "0";
83
	
84 529ffadb Bill Marquette
if($pkg['custom_php_global_functions'] <> "")
85
        eval($pkg['custom_php_global_functions']);
86
87 31d27c6c Scott Ullrich
// grab the installedpackages->package_name section.
88 e315cfa1 Colin Smith
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
89 7db8ff99 Colin Smith
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
90 da7bf505 Scott Ullrich
91 7db8ff99 Colin Smith
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
92 da7bf505 Scott Ullrich
93 19d360bf Scott Ullrich
if($_GET['savemsg'] <> "")
94 d8c1a6c5 Scott Ullrich
	$savemsg = htmlspecialchars($_GET['savemsg']);
95 f9a91638 Scott Ullrich
96 194b4e0a Colin Smith
if($pkg['custom_php_command_before_form'] <> "")
97
	eval($pkg['custom_php_command_before_form']);
98 f9a91638 Scott Ullrich
99 6483da5d Scott Ullrich
if ($_POST) {
100 b3235baa Scott Ullrich
	if($_POST['act'] == "del") {
101
		if($pkg['custom_delete_php_command']) {
102 5cd30ac3 Scott Ullrich
		    if($pkg['custom_php_command_before_form'] <> "")
103
			    eval($pkg['custom_php_command_before_form']);
104 b3235baa Scott Ullrich
		    eval($pkg['custom_delete_php_command']);
105
		}
106 7c172009 Scott Ullrich
		write_config($pkg['delete_string']);
107 facd08f9 Scott Ullrich
		// resync the configuration file code if defined.
108
		if($pkg['custom_php_resync_config_command'] <> "") {
109 86f3fc00 Scott Ullrich
			if($pkg['custom_php_command_before_form'] <> "")
110
				eval($pkg['custom_php_command_before_form']);
111
			eval($pkg['custom_php_resync_config_command']);
112 facd08f9 Scott Ullrich
		}
113 b3235baa Scott Ullrich
	} else {
114
		if($pkg['custom_add_php_command']) {
115 90779bf7 Scott Ullrich
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
116 12123e25 Colin Smith
			?>
117 afe4a7d3 Erik Kristensen
118 539b4c44 Colin Smith
<?php include("head.inc"); ?>
119 3eaeb703 Scott Ullrich
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
120 2fe6c52b Colin Smith
<?php include("fbegin.inc"); ?>
121 fbd14b13 Scott Ullrich
<?php
122 3eaeb703 Scott Ullrich
			}
123
			if($pkg['preoutput']) echo "<pre>";
124
			eval($pkg['custom_add_php_command']);
125
			if($pkg['preoutput']) echo "</pre>";
126 b3235baa Scott Ullrich
		}
127 6483da5d Scott Ullrich
	}
128 e3c4b6b7 Scott Ullrich
129 eec70f21 Scott Ullrich
	// donotsave is enabled.  lets simply exit.
130 3eaeb703 Scott Ullrich
	if($pkg['donotsave'] <> "") exit;
131
132 b6e87566 Scott Ullrich
	$firstfield = "";
133
	$rows = 0;
134
135 7c172009 Scott Ullrich
	$input_errors = array();
136
	$reqfields = array();
137
	$reqfieldsn = array();
138
	foreach ($pkg['fields']['field'] as $field) {
139 b91540da Scott Ullrich
		if (($field['type'] == 'input') && isset($field['required'])) {
140 349ef5c9 Scott Ullrich
			if($field['fieldname'])
141
				$reqfields[] = $field['fieldname'];
142
			if($field['fielddescr'])	
143
				$reqfieldsn[] = $field['fielddescr'];
144 7c172009 Scott Ullrich
		}
145
	}
146
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
147
148
	if ($pkg['custom_php_validation_command'])
149
		eval($pkg['custom_php_validation_command']);
150
151 b3235baa Scott Ullrich
	// store values in xml configration file.
152
	if (!$input_errors) {
153
		$pkgarr = array();
154
		foreach ($pkg['fields']['field'] as $fields) {
155 41fd0445 Scott Ullrich
			if($fields['type'] == "listtopic")
156
				continue;
157 eec70f21 Scott Ullrich
			if($fields['type'] == "rowhelper") {
158
				// save rowhelper items.
159
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
160 628b33c1 Scott Ullrich
				                         // XXX: this really is not helping embedded platforms.
161 0e730fee Scott Ullrich
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
162 b6e87566 Scott Ullrich
						if($firstfield == "")  {
163
						  $firstfield = $rowhelperfield['fieldname'];
164
						} else {
165
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
166
						}
167 6e32d276 Scott Ullrich
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
168 13e92c36 Scott Ullrich
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
169 eec70f21 Scott Ullrich
						eval($comd);
170
						if($value <> "") {
171 6e32d276 Scott Ullrich
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
172 b6e87566 Scott Ullrich
							//echo($comd . "<br>");
173 eec70f21 Scott Ullrich
							eval($comd);
174
						}
175
					}
176
				}
177
			} else {
178
				$fieldname  = $fields['fieldname'];
179 fbd14b13 Scott Ullrich
				$fieldvalue = $_POST[$fieldname];
180
				if (is_array($fieldvalue))
181
					$fieldvalue = implode(',', $fieldvalue);
182
				else {
183
					$fieldvalue = trim($fieldvalue);
184
					if ($fields['encoding'] == 'base64')
185
						$fieldvalue = base64_encode($fieldvalue);
186
				}
187 349ef5c9 Scott Ullrich
				if($fieldname)
188
					$pkgarr[$fieldname] = $fieldvalue;
189 eec70f21 Scott Ullrich
			}
190 b3235baa Scott Ullrich
		}
191 0e730fee Scott Ullrich
192 e3c4b6b7 Scott Ullrich
		if (isset($id) && $a_pkg[$id])
193
			$a_pkg[$id] = $pkgarr;
194
		else
195
			$a_pkg[] = $pkgarr;
196 0e730fee Scott Ullrich
197 7c172009 Scott Ullrich
		write_config($pkg['addedit_string']);
198 facd08f9 Scott Ullrich
		// late running code
199 2a520a0a Scott Ullrich
		if($pkg['custom_add_php_command_late'] <> "") {
200
		    eval($pkg['custom_add_php_command_late']);
201
		}
202 0e730fee Scott Ullrich
203 07bcca25 Ermal Lu?i
		if (isset($pkg['filter_rules_needed'])) {
204 a9b2e638 Ermal Lu?i
			pkg_build_filter_rules();
205 07bcca25 Ermal Lu?i
			filter_configure();
206
		}
207 a9b2e638 Ermal Lu?i
208 facd08f9 Scott Ullrich
		// resync the configuration file code if defined.
209
		if($pkg['custom_php_resync_config_command'] <> "") {
210
		    eval($pkg['custom_php_resync_config_command']);
211
		}
212
213 dcbe6f52 Scott Ullrich
		parse_package_templates();
214
215 a485a6f5 Scott Ullrich
		/* if start_command is defined, restart w/ this */
216
		if($pkg['start_command'] <> "")
217
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
218
219
		/* if restart_command is defined, restart w/ this */
220
		if($pkg['restart_command'] <> "")
221
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
222
223 dcbe6f52 Scott Ullrich
		if($pkg['aftersaveredirect'] <> "") {
224 fbd14b13 Scott Ullrich
		    pfSenseHeader($pkg['aftersaveredirect']);
225 3089059e Colin Smith
		} elseif(!$pkg['adddeleteeditpagefields']) {
226 fbd14b13 Scott Ullrich
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
227 3089059e Colin Smith
		} elseif(!$pkg['preoutput']) {
228 fbd14b13 Scott Ullrich
		    pfSenseHeader("pkg.php?xml=" . $xml);
229 dcbe6f52 Scott Ullrich
		}
230 0e730fee Scott Ullrich
		exit;
231 e3c4b6b7 Scott Ullrich
	}
232 7c172009 Scott Ullrich
	else
233
		$get_from_post = true;
234 da7bf505 Scott Ullrich
}
235
236 b91540da Scott Ullrich
if($pkg['title'] <> "") {
237
	$edit = ($only_edit ? '' : ': Edit');
238
	$title = $pkg['title'] . $edit;
239
}
240 83ddedcd Scott Ullrich
else
241 b91540da Scott Ullrich
	$title = gettext("Package Editor");
242 83ddedcd Scott Ullrich
243
$pgtitle = $title;
244
include("head.inc");
245
246 7c172009 Scott Ullrich
if ($pkg['custom_php_after_head_command'])
247
	eval($pkg['custom_php_after_head_command']);
248
249 d47013e1 Scott Ullrich
?>
250
251 2fe6c52b Colin Smith
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
252
<?php if($pkg['fields']['field'] <> "") { ?>
253
<script language="JavaScript">
254
<!--
255
function enablechange() {
256 3c616886 Scott Ullrich
<?php
257 fbd14b13 Scott Ullrich
foreach ($pkg['fields']['field'] as $field) {
258
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
259
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
260
261
		if (isset($field['enablefields'])) {
262 dcf6d563 Manoel Carvalho
			foreach (explode(',', $field['enablefields']) as $enablefield) {
263
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
264
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
265
				print("\t\t}else{\n");
266
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 1;\n");
267
				print("\t\t}\n");
268
			}
269 fbd14b13 Scott Ullrich
		}
270
271
		if (isset($field['checkenablefields'])) {
272 dcf6d563 Manoel Carvalho
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
273
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
274
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
275
				print("\t\t}else{\n");
276
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n");
277
				print("\t\t}\n");
278
			}
279 fbd14b13 Scott Ullrich
		}
280
281
		print("\t}\n\telse {\n");
282
283
		if (isset($field['enablefields'])) {
284 dcf6d563 Manoel Carvalho
			foreach (explode(',', $field['enablefields']) as $enablefield) {
285
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
286
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
287
				print("\t\t}else{\n");
288
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n");
289
				print("\t\t}\n");
290
			}
291 fbd14b13 Scott Ullrich
		}
292
293
		if (isset($field['checkenablefields'])) {
294 dcf6d563 Manoel Carvalho
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
295
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
296
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
297
				print("\t\t}else{\n");
298
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n");
299
				print("\t\t}\n");
300
			}
301 fbd14b13 Scott Ullrich
		}
302
303
		print("\t}\n");
304
	}
305
}
306 2fe6c52b Colin Smith
?>
307
}
308
//-->
309
</script>
310
<?php } ?>
311 625dcc40 Bill Marquette
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
312 eec70f21 Scott Ullrich
</script>
313
314 96d9f5c9 Bill Marquette
<?php include("fbegin.inc"); ?>
315 7c172009 Scott Ullrich
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
316
<form name="iform" action="pkg_edit.php" method="post">
317 19fd2947 Scott Ullrich
<input type="hidden" name="xml" value="<?= $xml ?>">
318 d47013e1 Scott Ullrich
<?php if ($savemsg) print_info_box($savemsg); ?>
319 f4cb26b7 Colin Smith
<table width="100%" border="0" cellpadding="0" cellspacing="0">
320 7c061036 Scott Ullrich
<?php
321
if ($pkg['tabs'] <> "") {
322 f4cb26b7 Colin Smith
    echo '<tr><td>';
323
    $tab_array = array();
324 7c061036 Scott Ullrich
    foreach($pkg['tabs']['tab'] as $tab) {
325 f4cb26b7 Colin Smith
	if(isset($tab['active'])) {
326
		$active = true;
327
	} else {
328
		$active = false;
329
	}
330 e17ce934 Scott Ullrich
	$urltmp = "";
331
	if($tab['url'] <> "") $urltmp = $tab['url'];
332
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
333
334 656a2fea Seth Mos
 	$addresswithport = getenv("HTTP_HOST");
335
	$colonpos = strpos($addresswithport, ":");
336
	if ($colonpos !== False){
337
		//my url is actually just the IP address of the pfsense box
338
		$myurl = substr($addresswithport, 0, $colonpos);
339
	}
340
	else
341
	{
342
		$myurl = $addresswithport;
343
	}
344 e17ce934 Scott Ullrich
	// eval url so that above $myurl item can be processed if need be.
345 f4cb26b7 Colin Smith
	$url = str_replace('$myurl', $myurl, $urltmp);
346 656a2fea Seth Mos
347 f4cb26b7 Colin Smith
	$tab_array[] = array(
348
				$tab['text'],
349
				$active,
350
				$url
351
			);
352 7c061036 Scott Ullrich
    }
353 f4cb26b7 Colin Smith
    display_top_tabs($tab_array);
354
    echo '</td></tr>';
355 7c061036 Scott Ullrich
}
356
?>
357 f4cb26b7 Colin Smith
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
358 d47013e1 Scott Ullrich
  <?php
359
  $cols = 0;
360 457106da Renato Botelho
  $savevalue = gettext("Save");
361 3eaeb703 Scott Ullrich
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
362 32487e42 Scott Ullrich
  foreach ($pkg['fields']['field'] as $pkga) { 
363
364
		if ($pkga['type'] == "listtopic") {
365
			echo "<td>&nbsp;</td>";
366
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
367
			continue;
368
	    }	
369
	
370
?>
371 3c616886 Scott Ullrich
372 facd08f9 Scott Ullrich
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
373
374 d47013e1 Scott Ullrich
	  <?php
375 3c616886 Scott Ullrich
376 5c74f339 Scott Ullrich
	  $size = "";
377 3c616886 Scott Ullrich
378 facd08f9 Scott Ullrich
	  if(!$pkga['dontdisplayname']) {
379 7c172009 Scott Ullrich
		unset($req);
380 b91540da Scott Ullrich
		if (isset($pkga['required']))
381 7c172009 Scott Ullrich
			$req = 'req';
382
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
383 d51f86e0 Scott Ullrich
		echo fixup_string($pkga['fielddescr']);
384 31d27c6c Scott Ullrich
		echo "</td>";
385 facd08f9 Scott Ullrich
	  }
386
387
	  if(!$pkga['dontcombinecells'])
388
		echo "<td class=\"vtable\">";
389 31d27c6c Scott Ullrich
		// if user is editing a record, load in the data.
390 7c172009 Scott Ullrich
		$fieldname = $pkga['fieldname'];
391 fbd14b13 Scott Ullrich
		if ($get_from_post) {
392 7c172009 Scott Ullrich
			$value = $_POST[$fieldname];
393 fbd14b13 Scott Ullrich
			if (is_array($value)) $value = implode(',', $value);
394
		}
395 7c172009 Scott Ullrich
		else {
396
			if (isset($id) && $a_pkg[$id])
397
				$value = $a_pkg[$id][$fieldname];
398 b91540da Scott Ullrich
			else
399
				$value = $pkga['default_value'];
400 31d27c6c Scott Ullrich
		}
401
402 3eaeb703 Scott Ullrich
	      if($pkga['type'] == "input") {
403 b6e87566 Scott Ullrich
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
404 e4f8f7d2 Bill Marquette
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
405 d51f86e0 Scott Ullrich
			echo "<br>" . fixup_string($pkga['description']) . "\n";
406 3eaeb703 Scott Ullrich
	      } else if($pkga['type'] == "password") {
407 f1412ed6 Scott Ullrich
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
408 e4f8f7d2 Bill Marquette
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
409 d51f86e0 Scott Ullrich
			echo "<br>" . fixup_string($pkga['description']) . "\n";
410 3eaeb703 Scott Ullrich
	      } else if($pkga['type'] == "select") {
411 fbd14b13 Scott Ullrich
                  $fieldname = $pkga['fieldname'];
412
                  if (isset($pkga['multiple'])) {
413 67fa245a Scott Ullrich
                    $multiple = 'multiple="multiple"';
414 fbd14b13 Scott Ullrich
                    $items = explode(',', $value);
415 67fa245a Scott Ullrich
                    $fieldname .= "[]";
416 fbd14b13 Scott Ullrich
                  }
417
                  else {
418
                    $multiple = '';
419
                    $items = array($value);
420
                  }
421
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
422
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
423
424 ae2ea68c Scott Ullrich
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
425 fbd14b13 Scott Ullrich
                  foreach ($pkga['options']['option'] as $opt) {
426
                      $selected = '';
427 67fa245a Scott Ullrich
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
428 fbd14b13 Scott Ullrich
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
429
                  }
430
431
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
432 2c634bdd Scott Ullrich
	      } else if($pkga['type'] == "vpn_selection") {
433 ae2ea68c Scott Ullrich
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
434 a93e56c5 Matthew Grooms
		    foreach ($config['ipsec']['phase1'] as $vpn) {
435 2c634bdd Scott Ullrich
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
436
		    }
437
		    echo "</select>\n";
438 03f0bfe7 Scott Ullrich
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
439 3eaeb703 Scott Ullrich
	      } else if($pkga['type'] == "checkbox") {
440 64c1ebbb Scott Ullrich
			$checkboxchecked = "";
441 34865da0 Scott Ullrich
			if($value == "on") $checkboxchecked = " CHECKED";
442 b91540da Scott Ullrich
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
443
				$onclick = ' onclick="javascript:enablechange();"';
444 ae2ea68c Scott Ullrich
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
445 d51f86e0 Scott Ullrich
			echo "<br>" . fixup_string($pkga['description']) . "\n";
446 3eaeb703 Scott Ullrich
	      } else if($pkga['type'] == "textarea") {
447
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
448
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
449 82458abb Mark Crane
		  if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
450
          if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
451
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
452 d51f86e0 Scott Ullrich
			echo "<br>" . fixup_string($pkga['description']) . "\n";
453 ab73789e Scott Ullrich
		  } else if($pkga['type'] == "interfaces_selection") {
454 fbd14b13 Scott Ullrich
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
455
			$multiple = '';
456 7502342a Scott Ullrich
			$fieldname = $pkga['fieldname'];
457 fbd14b13 Scott Ullrich
			if (isset($pkga['multiple'])) {
458
				$fieldname .= '[]';
459
				$multiple = 'multiple';
460 e4a72146 Scott Ullrich
			}
461 ae2ea68c Scott Ullrich
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
462 fbd14b13 Scott Ullrich
			if (isset($pkga['all_interfaces']))
463
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
464
			else
465
				$ifaces = $config['interfaces'];
466
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
467
			if (!empty($additional_ifaces))
468
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
469 9996eff6 Scott Ullrich
			if(is_array($value))
470
				$values = $value;
471
			else
472
				$values  =  explode(',',  $value);
473 fbd14b13 Scott Ullrich
			foreach($ifaces as $ifname => $iface) {
474 0f68e352 Scott Ullrich
				if($iface['descr'] <> "")
475 fbd14b13 Scott Ullrich
					$ifdescr = $iface['descr'];
476
				else
477
					$ifdescr = strtoupper($ifname);
478
				if ($ip = find_interface_ip($iface))
479
					$ip = " ($ip)";
480
				$selected = (in_array($ifname, $values) ? 'selected' : '');
481
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
482 ab73789e Scott Ullrich
			}
483 fbd14b13 Scott Ullrich
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
484 3eaeb703 Scott Ullrich
	      } else if($pkga['type'] == "radio") {
485 ae2ea68c Scott Ullrich
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
486 eec70f21 Scott Ullrich
	      } else if($pkga['type'] == "rowhelper") {
487
		?>
488
			<script type="text/javascript" language='javascript'>
489
			<!--
490
			<?php
491
				$rowcounter = 0;
492
				$fieldcounter = 0;
493
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
494
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
495
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
496
					$fieldcounter++;
497
				}
498
			?>
499
			-->
500
			</script>
501
			<table name="maintable" id="maintable">
502 b6e87566 Scott Ullrich
			<tr>
503 eec70f21 Scott Ullrich
			<?php
504 31d27c6c Scott Ullrich
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
505 d51f86e0 Scott Ullrich
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
506 31d27c6c Scott Ullrich
				}
507 b6e87566 Scott Ullrich
				echo "</tr>";
508
509 eec70f21 Scott Ullrich
				echo "<tr>";
510
				  // XXX: traverse saved fields, add back needed rows.
511
				echo "</tr>";
512 0e730fee Scott Ullrich
513 31d27c6c Scott Ullrich
				echo "<tr>\n";
514
				$rowcounter = 0;
515
				$trc = 0;
516
				if(isset($a_pkg[$id]['row'])) {
517
					foreach($a_pkg[$id]['row'] as $row) {
518
					/*
519
					 * loop through saved data for record if it exists, populating rowhelper
520 bb940538 Scott Ullrich
					 */
521 31d27c6c Scott Ullrich
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
522
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
523
							$fieldname = $rowhelper['fieldname'];
524
							// if user is editing a record, load in the data.
525 bb940538 Scott Ullrich
							if (isset($id) && $a_pkg[$id]) {
526 7db8ff99 Colin Smith
								$value = $row[$fieldname];
527 bb940538 Scott Ullrich
							}
528 31d27c6c Scott Ullrich
							$options = "";
529
							$type = $rowhelper['type'];
530
							$fieldname = $rowhelper['fieldname'];
531
							if($type == "option") $options = &$rowhelper['options']['option'];
532 7e542bd1 Scott Ullrich
							if($rowhelper['size']) 
533 4c09b655 Scott Ullrich
								$size = $rowhelper['size'];
534
							else
535
								$size = "8";
536 e70e0b76 Scott Ullrich
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
537 bb940538 Scott Ullrich
							// javascript helpers for row_helper_dynamic.js
538
							echo "</td>\n";
539
							echo "<script language=\"JavaScript\">\n";
540
							echo "<!--\n";
541
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
542
							echo "-->\n";
543
							echo "</script>\n";
544
							$text = "";
545
							$trc++;
546 eec70f21 Scott Ullrich
						}
547 bb940538 Scott Ullrich
548 31d27c6c Scott Ullrich
						$rowcounter++;
549
						echo "<td>";
550 b91540da Scott Ullrich
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
551 eec70f21 Scott Ullrich
						echo "</td>\n";
552 31d27c6c Scott Ullrich
						echo "</tr>\n";
553
					}
554
				}
555
				if($trc == 0) {
556
					/*
557
					 *  no records loaded.
558
                                         *  just show a generic line non-populated with saved data
559
                                         */
560
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
561
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
562 bb940538 Scott Ullrich
						$fieldname = $rowhelper['fieldname'];
563 31d27c6c Scott Ullrich
						$options = "";
564
						$type = $rowhelper['type'];
565
						$fieldname = $rowhelper['fieldname'];
566
						if($type == "option") $options = &$rowhelper['options']['option'];
567 e70e0b76 Scott Ullrich
						$size = "8";
568
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
569
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
570 bb940538 Scott Ullrich
						// javascript helpers for row_helper_dynamic.js
571
						echo "</td>\n";
572
						echo "<script language=\"JavaScript\">\n";
573
						echo "<!--\n";
574
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
575
						echo "-->\n";
576
						echo "</script>\n";
577
						$text = "";
578 eec70f21 Scott Ullrich
						$trc++;
579
					}
580 bb940538 Scott Ullrich
581
					$rowcounter++;
582 eec70f21 Scott Ullrich
				}
583
			?>
584 0e730fee Scott Ullrich
585 03664f64 Scott Ullrich
			  <tbody></tbody>
586 eec70f21 Scott Ullrich
			</table>
587 0e730fee Scott Ullrich
588 677c0869 Erik Kristensen
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
589 eec70f21 Scott Ullrich
		<script language="JavaScript">
590
		<!--
591
		field_counter_js = <?= $fieldcounter ?>;
592
		rows = <?= $rowcounter ?>;
593
		totalrows = <?php echo $rowcounter; ?>;
594
		loaded = <?php echo $rowcounter; ?>;
595 e02a6839 Scott Ullrich
		//typesel_change();
596 eec70f21 Scott Ullrich
		//-->
597
		</script>
598
599
		<?php
600 d47013e1 Scott Ullrich
	      }
601 3eaeb703 Scott Ullrich
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
602 facd08f9 Scott Ullrich
	     ?>
603
604 d47013e1 Scott Ullrich
      <?php
605 facd08f9 Scott Ullrich
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
606 d47013e1 Scott Ullrich
      $i++;
607
  }
608
 ?>
609 b6e87566 Scott Ullrich
  <tr>
610
	<td>&nbsp;</td>
611
  </tr>
612 d47013e1 Scott Ullrich
  <tr>
613
    <td width="22%" valign="top">&nbsp;</td>
614
    <td width="78%">
615 7c172009 Scott Ullrich
<?php
616
if($pkg['note'] != "")
617 b91540da Scott Ullrich
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
618
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
619 7c172009 Scott Ullrich
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
620
?>
621
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
622 b91540da Scott Ullrich
<?php if (!$only_edit): ?>
623 457106da Renato Botelho
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
624 b91540da Scott Ullrich
<?php endif; ?>
625 d47013e1 Scott Ullrich
    </td>
626
  </tr>
627
</table>
628 f4cb26b7 Colin Smith
</div></tr></td></table>
629 7c172009 Scott Ullrich
</form>
630 7c061036 Scott Ullrich
631 7c172009 Scott Ullrich
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
632 95736b54 Scott Ullrich
633 d47013e1 Scott Ullrich
<?php include("fend.inc"); ?>
634
</body>
635
</html>
636
637 31d27c6c Scott Ullrich
<?php
638
/*
639
 * ROW Helpers function
640
 */
641 e70e0b76 Scott Ullrich
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
642 bb940538 Scott Ullrich
	global $text;
643 31d27c6c Scott Ullrich
	echo "<td>\n";
644
	if($type == "input") {
645 ae2ea68c Scott Ullrich
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
646 855cab21 Scott Ullrich
	} else if($type == "checkbox") {
647
		if($value)
648 ae2ea68c Scott Ullrich
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
649 855cab21 Scott Ullrich
		else
650 ae2ea68c Scott Ullrich
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
651 31d27c6c Scott Ullrich
	} else if($type == "password") {
652 e4f8f7d2 Bill Marquette
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
653 31d27c6c Scott Ullrich
	} else if($type == "textarea") {
654 e4f8f7d2 Bill Marquette
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
655 31d27c6c Scott Ullrich
	} else if($type == "select") {
656 ae2ea68c Scott Ullrich
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
657 31d27c6c Scott Ullrich
		foreach($rowhelper['options']['option'] as $rowopt) {
658
			$selected = "";
659
			if($rowopt['value'] == $value) $selected = " SELECTED";
660
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
661
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
662
		}
663
		echo "</select>\n";
664
	}
665
}
666
667 d51f86e0 Scott Ullrich
function fixup_string($string) {
668 d2133701 Scott Ullrich
	global $config;
669 d51f86e0 Scott Ullrich
	// fixup #1: $myurl -> http[s]://ip_address:port/
670
	$https = "";
671
	$port = $config['system']['webguiport'];
672 63637de9 Bill Marquette
	if($port <> "443" and $port <> "80")
673
		$urlport = ":" . $port;
674
	else
675
		$urlport = "";
676 3c616886 Scott Ullrich
677 d51f86e0 Scott Ullrich
	if($config['system']['webguiproto'] == "https") $https = "s";
678 63637de9 Bill Marquette
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
679 d51f86e0 Scott Ullrich
	$newstring = str_replace("\$myurl", $myurl, $string);
680 d2133701 Scott Ullrich
	$string = $newstring;
681
	// fixup #2: $wanip
682 85a5da13 Ermal Luçi
	$curwanip = get_interface_ip();
683 d2133701 Scott Ullrich
	$newstring = str_replace("\$wanip", $curwanip, $string);
684
	$string = $newstring;
685
	// fixup #3: $lanip
686
	$lancfg = $config['interfaces']['lan'];
687
	$lanip = $lancfg['ipaddr'];
688
	$newstring = str_replace("\$lanip", $lanip, $string);
689
	$string = $newstring;
690
	// fixup #4: fix'r'up here.
691 d51f86e0 Scott Ullrich
	return $newstring;
692
}
693
694 dcbe6f52 Scott Ullrich
/*
695
 *  Parse templates if they are defined
696
 */
697
function parse_package_templates() {
698
	global $pkg, $config;
699 767a716e Scott Ullrich
	$rows = 0;
700 dcbe6f52 Scott Ullrich
	if($pkg['templates']['template'] <> "")
701
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
702
		$filename = $pkg_template_row['filename'];
703
		$template_text = $pkg_template_row['templatecontents'];
704 767a716e Scott Ullrich
		$firstfield = "";
705 dcbe6f52 Scott Ullrich
		/* calculate total row helpers count */
706
		foreach ($pkg['fields']['field'] as $fields) {
707
			if($fields['type'] == "rowhelper") {
708
				// save rowhelper items.
709
                                $row_helper_total_rows = 0;
710
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
711
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
712
						if($firstfield == "")  {
713
						  $firstfield = $rowhelperfield['fieldname'];
714
						} else {
715
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
716
						}
717
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
718 767a716e Scott Ullrich
						$value = "";
719 dcbe6f52 Scott Ullrich
						eval($comd);
720
						if($value <> "") {
721
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
722
						} else {
723
						    $row_helper_total_rows = $rows;
724
						    break;
725
						}
726
					}
727
				}
728
			}
729
		}
730
731
		/* replace $domain_total_rows with total rows */
732
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
733
734
		/* change fields defined as fieldname_fieldvalue to their value */
735
		foreach ($pkg['fields']['field'] as $fields) {
736
			if($fields['type'] == "rowhelper") {
737
				// save rowhelper items.
738
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
739
					$row_helper_data = "";
740 1be00369 Scott Ullrich
					$isfirst = 0;
741 dcbe6f52 Scott Ullrich
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
742
						if($firstfield == "")  {
743
						  $firstfield = $rowhelperfield['fieldname'];
744
						} else {
745
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
746
						}
747
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
748
						eval($comd);
749
						if($value <> "") {
750 1be00369 Scott Ullrich
						    if($isfirst == 1) $row_helper_data .= "  " ;
751
						    $row_helper_data .= $value;
752
						    $isfirst = 1;
753 dcbe6f52 Scott Ullrich
						}
754 767a716e Scott Ullrich
						$sep = "";
755 dcbe6f52 Scott Ullrich
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
756
						foreach ($sep as $se) $seperator = $se;
757
						if($seperator <> "") {
758
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
759
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
760
						}
761
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
762
					}
763
				}
764
			} else {
765
				$fieldname  = $fields['fieldname'];
766
				$fieldvalue = $_POST[$fieldname];
767
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
768
			}
769
		}
770
771
		/* replace cr's */
772
		$template_text = str_replace("\\n", "\n", $template_text);
773
774
		/* write out new template file */
775
		$fout = fopen($filename,"w");
776
		fwrite($fout, $template_text);
777
		fclose($fout);
778
	    }
779
}
780
781 1ebe9f62 Scott Ullrich
?>