Project

General

Profile

Download (15.6 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
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
require("guiconfig.inc");
31
require("xmlparse_pkg.inc");
32

    
33
$pfSense_config = $config; // copy this since we will be parsing
34
                           // another xml file which will be clobbered.
35

    
36
function gentitle_pkg($pgname) {
37
	global $pfSense_config;
38
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
39
}
40

    
41
// XXX: Make this input safe.
42
$xml = $_GET['xml'];
43
if($_POST['xml']) $xml = $_POST['xml'];
44

    
45
if($xml == "") {
46
            $xml = "not_defined";
47
            print_info_box_np("ERROR:  Could not open " . $xml . ".");
48
            die;
49
} else {
50
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
51
}
52

    
53
$package_name = $pkg['menu'][0]['name'];
54
$section      = $pkg['menu'][0]['section'];
55
$config_path  = $pkg['configpath'];
56
$name         = $pkg['name'];
57
$title        = $section . ": " . $package_name;
58

    
59
$id = $_GET['id'];
60
if (isset($_POST['id']))
61
	$id = $_POST['id'];
62

    
63
// grab the installedpackages->package_name section.
64
$toeval = "\$a_pkg = &\$config['installedpackages']['" . $name . "']['config'];";
65
eval($toeval);
66

    
67
$toeval = "if (!is_array(\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'])) \$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'] = array();";
68
eval($toeval);
69

    
70
$toeval = "\$a_pkg = &\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'];";
71
eval($toeval);
72

    
73
if($pkg['custom_php_command_before_form'] <> "")
74
  eval($pkg['custom_php_command_before_form']);
75

    
76

    
77
if ($_POST) {
78
	if($_POST['act'] == "del") {
79
		if($pkg['custom_delete_php_command']) {
80
		    eval($pkg['custom_delete_php_command']);
81
		}
82
		write_config();
83
		// resync the configuration file code if defined.
84
		if($pkg['custom_php_resync_config_command'] <> "") {
85
		    eval($pkg['custom_php_resync_config_command']);
86
		}
87
	} else {
88
		if($pkg['custom_add_php_command']) {
89
			if($pkg['donotsave'] <> "") {
90
				?>
91
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
92
<html>
93
<head>
94
<title><?=gentitle_pkg($title);?></title>
95
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
96
<link href="gui.css" rel="stylesheet" type="text/css">
97
</head>
98

    
99
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
100
<?php
101
include("fbegin.inc");
102
?>
103
<p class="pgtitle"><?=$title?></p>
104
				<?php
105
			}
106
			if($pkg['preoutput']) echo "<pre>";
107
			eval($pkg['custom_add_php_command']);
108
			if($pkg['preoutput']) echo "</pre>";
109
		}
110
	}
111

    
112
	// donotsave is enabled.  lets simply exit.
113
	if($pkg['donotsave'] <> "") exit;
114

    
115
	$firstfield = "";
116
	$rows = 0;
117

    
118
	// store values in xml configration file.
119
	if (!$input_errors) {
120
		$pkgarr = array();
121
		foreach ($pkg['fields']['field'] as $fields) {
122
			if($fields['type'] == "rowhelper") {
123
				// save rowhelper items.
124
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
125
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
126
						if($firstfield == "")  {
127
						  $firstfield = $rowhelperfield['fieldname'];
128
						} else {
129
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
130
						}
131
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
132
						//echo($comd . "<br>");
133
						eval($comd);
134
						if($value <> "") {
135
							$comd = "\$pkgarr['row'][" . $x . "]['" . $rowhelperfield['fieldname'] . "'] = \"" . $value . "\";";
136
							//echo($comd . "<br>");
137
							eval($comd);
138
						}
139
					}
140
				}
141
			} else {
142
				// simply loop through all field names looking for posted
143
				// values matching the fieldnames.  if found, save to package
144
				// configuration area.
145
				$fieldname  = $fields['fieldname'];
146
				$fieldvalue = $_POST[$fieldname];
147
				$toeval = "\$pkgarr['" . $fieldname . "'] 	= \"" . $fieldvalue . "\";";
148
				eval($toeval);
149
			}
150
		}
151

    
152
		if (isset($id) && $a_pkg[$id])
153
			$a_pkg[$id] = $pkgarr;
154
		else
155
			$a_pkg[] = $pkgarr;
156

    
157
		write_config();
158

    
159
		// late running code
160
		if($pkg['custom_add_php_command_late'] <> "") {
161
		    eval($pkg['custom_add_php_command_late']);
162
		}
163

    
164
		// resync the configuration file code if defined.
165
		if($pkg['custom_php_resync_config_command'] <> "") {
166
		    eval($pkg['custom_php_resync_config_command']);
167
		}
168

    
169
		header("Location:  pkg.php?xml=" . $xml);
170
		exit;
171
	}
172
}
173

    
174
?>
175
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
176
<html>
177
<head>
178
<title><?=gentitle_pkg($title);?></title>
179
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
180
<link href="gui.css" rel="stylesheet" type="text/css">
181
</head>
182

    
183
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
184

    
185
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
186
</script>
187

    
188
<?php
189
$config_tmp = $config;
190
$config = $pfSense_config;
191
include("fbegin.inc");
192
$config = $config_tmp;
193
?>
194
<p class="pgtitle"><?=$title?></p>
195
<form action="pkg_edit.php" method="post">
196
<input type="hidden" name="xml" value="<?= $xml ?>">
197
<?php if ($savemsg) print_info_box($savemsg); ?>
198

    
199
&nbsp;<br>
200

    
201
<table width="100%" border="0" cellpadding="6" cellspacing="0">
202
  <?php
203
  $cols = 0;
204
  $savevalue = "Save";
205
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
206
  foreach ($pkg['fields']['field'] as $pkga) { ?>
207

    
208
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
209

    
210
	  <?php
211
	  if(!$pkga['dontdisplayname']) {
212
		echo "<td width=\"22%\" class=\"vncellreq\">";
213
		echo fixup_string($pkga['fielddescr']);
214
		echo "</td>";
215
	  }
216

    
217
	  if(!$pkga['dontcombinecells'])
218
		echo "<td class=\"vtable\">";
219

    
220
		// if user is editing a record, load in the data.
221
		if (isset($id) && $a_pkg[$id]) {
222
			$fieldname = $pkga['fieldname'];
223
			$toeval = "\$value = \$a_pkg[" . $id . "]['" . $fieldname . "'];";
224
			echo "<!-- eval: " . $toeval . "-->\n";
225
			eval($toeval);
226
		}
227

    
228
	      if($pkga['type'] == "input") {
229
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
230
			echo "<input " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
231
			echo "<br>" . fixup_string($pkga['description']) . "\n";
232
	      } else if($pkga['type'] == "password") {
233
			echo "<input type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
234
			echo "<br>" . fixup_string($pkga['description']) . "\n";
235
	      } else if($pkga['type'] == "select") {
236
		  // XXX: TODO: set $selected
237
                  if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
238
		  if($pkga['multiple'] == "yes") $multiple = "MULTIPLE ";
239
		    echo "<select " . $multiple . $size . "id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "'>\n";
240
		    foreach ($pkga['options']['option'] as $opt) {
241
			  $selected = "";
242
			  if($opt['value'] == $value) $selected = " SELECTED";
243
			  echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
244
		    }
245
		    echo "</select>\n";
246
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
247
	      } else if($pkga['type'] == "vpn_selection") {
248
		    echo "<select name='" . $vpn['name'] . "'>\n";
249
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
250
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
251
		    }
252
		    echo "</select>\n";
253
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
254
	      } else if($pkga['type'] == "checkbox") {
255
			echo "<input type='checkbox' name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
256
			echo "<br>" . fixup_string($pkga['description']) . "\n";
257
	      } else if($pkga['type'] == "textarea") {
258
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
259
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
260
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
261
			echo "<br>" . fixup_string($pkga['description']) . "\n";
262
		  } else if($pkga['type'] == "interfaces_selection") {
263
			$size = "";
264
			$multiple = "";
265
			$fieldname = $pkga['fieldname'];
266
			if($pkga['size'] <> "") $size = " size=\"" . $pkga['size'] . "\"";
267
			if($pkga['multiple'] <> "" and $pkga['multiple'] <> "0") {
268
			  $multiple = " multiple=\"multiple\"";
269
			  $fieldname .= "[]";
270
			}
271
			echo "<select name='" . $fieldname . "'" . $size . $multiple . ">\n";
272
			foreach ($config['interfaces'] as $ifname => $iface) {
273
			  if ($iface['descr'])
274
				  $ifdescr = $iface['descr'];
275
			  else
276
				  $ifdescr = strtoupper($ifname);
277
				$ifname = $iface['if'];
278
				echo "<option value='" . $ifname . "'>" . $ifdescr . "</option>\n";
279
			}
280
			echo "</select>\n";
281
			echo "<br>" . fixup_string($pkga['description']) . "\n";
282
	      } else if($pkga['type'] == "radio") {
283
			echo "<input type='radio' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
284
	      } else if($pkga['type'] == "rowhelper") {
285
		?>
286
			<script type="text/javascript" language='javascript'>
287
			<!--
288

    
289
			<?php
290
				$rowcounter = 0;
291
				$fieldcounter = 0;
292
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
293
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
294
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
295
					$fieldcounter++;
296
				}
297
			?>
298

    
299
			-->
300
			</script>
301

    
302
			<table name="maintable" id="maintable">
303
			<tr>
304
			<?php
305
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
306
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
307
				}
308
				echo "</tr>";
309
				echo "<tbody>";
310

    
311
				echo "<tr>";
312
				  // XXX: traverse saved fields, add back needed rows.
313
				echo "</tr>";
314

    
315
				echo "<tr>\n";
316
				$rowcounter = 0;
317
				$trc = 0;
318
				if(isset($a_pkg[$id]['row'])) {
319
					foreach($a_pkg[$id]['row'] as $row) {
320
					/*
321
					 * loop through saved data for record if it exists, populating rowhelper
322
					 */
323
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
324
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
325
							$fieldname = $rowhelper['fieldname'];
326
							// if user is editing a record, load in the data.
327
							if (isset($id) && $a_pkg[$id]) {
328
								$toeval = "\$value = \$row['" . $fieldname . "'];";
329
								echo "<!-- eval: " . $toeval . "-->\n";
330
								eval($toeval);
331
								echo "<!-- value: " . $value . "-->\n";
332
							}
333
							$options = "";
334
							$type = $rowhelper['type'];
335
							$fieldname = $rowhelper['fieldname'];
336
							if($type == "option") $options = &$rowhelper['options']['option'];
337
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper);
338
							// javascript helpers for row_helper_dynamic.js
339
							echo "</td>\n";
340
							echo "<script language=\"JavaScript\">\n";
341
							echo "<!--\n";
342
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
343
							echo "-->\n";
344
							echo "</script>\n";
345
							$text = "";
346
							$trc++;
347
						}
348

    
349
						$rowcounter++;
350
						echo "<td>";
351
						echo "<input type=\"image\" src=\"/x.gif\" onclick=\"removeRow(this); return false;\" value=\"Delete\">";
352
						echo "</td>\n";
353
						echo "</tr>\n";
354
					}
355
				}
356
				if($trc == 0) {
357
					/*
358
					 *  no records loaded.
359
                                         *  just show a generic line non-populated with saved data
360
                                         */
361
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
362
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
363
						$fieldname = $rowhelper['fieldname'];
364
						$options = "";
365
						$type = $rowhelper['type'];
366
						$fieldname = $rowhelper['fieldname'];
367
						if($type == "option") $options = &$rowhelper['options']['option'];
368
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper);
369
						// javascript helpers for row_helper_dynamic.js
370
						echo "</td>\n";
371
						echo "<script language=\"JavaScript\">\n";
372
						echo "<!--\n";
373
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
374
						echo "-->\n";
375
						echo "</script>\n";
376
						$text = "";
377
						$trc++;
378
					}
379

    
380
					$rowcounter++;
381
				}
382
			?>
383

    
384
			  </tbody>
385
			</table>
386

    
387
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="/plus.gif"></a>
388
		<script language="JavaScript">
389
		<!--
390
		field_counter_js = <?= $fieldcounter ?>;
391
		rows = <?= $rowcounter ?>;
392
		totalrows = <?php echo $rowcounter; ?>;
393
		loaded = <?php echo $rowcounter; ?>;
394
		//typesel_change();
395
		//-->
396
		</script>
397

    
398
		<?php
399
	      }
400
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
401
	     ?>
402

    
403
      <?php
404
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
405
      $i++;
406
  }
407
 ?>
408
  <tr>
409
	<td>&nbsp;</td>
410
  </tr>
411
  <tr>
412
    <td width="22%" valign="top">&nbsp;</td>
413
    <td width="78%">
414
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
415
      <?php if (isset($id) && $a_pkg[$id]): ?>
416
      <input name="id" type="hidden" value="<?=$id;?>">
417
      <?php endif; ?>
418
    </td>
419
  </tr>
420
</table>
421

    
422
</form>
423
<?php include("fend.inc"); ?>
424
</body>
425
</html>
426

    
427

    
428
<?php
429

    
430
/*
431
 * ROW Helpers function
432
 */
433
function display_row($trc, $value, $fieldname, $type, $rowhelper) {
434
	global $text;
435
	echo "<td>\n";
436
	if($type == "input") {
437
		echo "<input size='8' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
438
	} else if($type == "password") {
439
		echo "<input type='password' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
440
	} else if($type == "textarea") {
441
		echo "<textarea name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
442
	} else if($type == "select") {
443
		echo "<select name='" . $fieldname . $trc . "'>\n";
444
		foreach($rowhelper['options']['option'] as $rowopt) {
445
			$selected = "";
446
			if($rowopt['value'] == $value) $selected = " SELECTED";
447
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
448
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
449
		}
450
		echo "</select>\n";
451
	}
452
}
453

    
454
function fixup_string($string) {
455
	global $config;
456
	// fixup #1: $myurl -> http[s]://ip_address:port/
457
	$https = "";
458
	$port = "";
459
	$urlport = "";
460
	$port = $config['system']['webguiport'];
461
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
462
	if($config['system']['webguiproto'] == "https") $https = "s";
463
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
464
	$newstring = str_replace("\$myurl", $myurl, $string);
465
	$string = $newstring;
466
	// fixup #2: $wanip
467
	$curwanip = get_current_wan_address();
468
	$newstring = str_replace("\$wanip", $curwanip, $string);
469
	$string = $newstring;
470
	// fixup #3: $lanip
471
	$lancfg = $config['interfaces']['lan'];
472
	$lanip = $lancfg['ipaddr'];
473
	$newstring = str_replace("\$lanip", $lanip, $string);
474
	$string = $newstring;
475
	// fixup #4: fix'r'up here.
476
	return $newstring;
477
}
478

    
479
?>
(53-53/99)