Project

General

Profile

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