Project

General

Profile

Download (36.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2004-2010 Scott Ullrich <sullrich@gmail.com>
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
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
31
	pfSense_MODULE:	pkgs
32
*/
33

    
34
##|+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
ini_set('max_execution_time', '0');
42

    
43
require("guiconfig.inc");
44
require_once("functions.inc");
45
require_once("filter.inc");
46
require_once("shaper.inc");
47
require_once("pkg-utils.inc");
48

    
49
/* dummy stubs needed by some code that was MFC'd */
50
function pfSenseHeader($location) { header("Location: " . $location); }
51

    
52
function gentitle_pkg($pgname) {
53
	global $pfSense_config;
54
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
55
}
56

    
57
$xml = htmlspecialchars($_GET['xml']);
58
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
59

    
60
if($xml == "") {
61
            print_info_box_np(gettext("ERROR: No package defined."));
62
            die;
63
} else {
64
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
65
}
66

    
67
if($pkg['include_file'] <> "") {
68
	require_once($pkg['include_file']);
69
}
70

    
71
if (!isset($pkg['adddeleteeditpagefields']))
72
	$only_edit = true;
73
else
74
	$only_edit = false;
75

    
76
$package_name = $pkg['menu'][0]['name'];
77
$section      = $pkg['menu'][0]['section'];
78
$config_path  = $pkg['configpath'];
79
$name         = $pkg['name'];
80
$title        = $pkg['title'];
81
$pgtitle      = $title;
82

    
83
$id = $_GET['id'];
84
if (isset($_POST['id']))
85
	$id = htmlspecialchars($_POST['id']);
86

    
87
// Not posting?  Then user is editing a record. There must be a valid id
88
// when editing a record.
89
if(!$id && !$_POST)
90
	$id = "0";
91

    
92
if(!is_numeric($id)) {
93
	Header("Location: /");
94
	exit;
95
}
96

    
97
if($pkg['custom_php_global_functions'] <> "")
98
        eval($pkg['custom_php_global_functions']);
99

    
100
// grab the installedpackages->package_name section.
101
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
102
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
103

    
104
// If the first entry in the array is an empty <config/> tag, kill it.
105
if ((count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) 
106
	&& ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == ""))
107
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
108

    
109
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
110

    
111
if($_GET['savemsg'] <> "")
112
	$savemsg = htmlspecialchars($_GET['savemsg']);
113

    
114
if($pkg['custom_php_command_before_form'] <> "")
115
	eval($pkg['custom_php_command_before_form']);
116

    
117
if ($_POST) {
118
	if($_POST['act'] == "del") {
119
		if($pkg['custom_delete_php_command']) {
120
		    if($pkg['custom_php_command_before_form'] <> "")
121
			    eval($pkg['custom_php_command_before_form']);
122
		    eval($pkg['custom_delete_php_command']);
123
		}
124
		write_config($pkg['delete_string']);
125
		// resync the configuration file code if defined.
126
		if($pkg['custom_php_resync_config_command'] <> "") {
127
			if($pkg['custom_php_command_before_form'] <> "")
128
				eval($pkg['custom_php_command_before_form']);
129
			eval($pkg['custom_php_resync_config_command']);
130
		}
131
	} else {
132
		if($pkg['custom_add_php_command']) {
133
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
134
			?>
135

    
136
<?php include("head.inc"); ?>
137
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
138
<?php include("fbegin.inc"); ?>
139
<?php
140
			}
141
			if($pkg['preoutput']) echo "<pre>";
142
			eval($pkg['custom_add_php_command']);
143
			if($pkg['preoutput']) echo "</pre>";
144
		}
145
	}
146

    
147
	// donotsave is enabled.  lets simply exit.
148
	if($pkg['donotsave'] <> "") exit;
149

    
150
	$firstfield = "";
151
	$rows = 0;
152

    
153
	$input_errors = array();
154
	$reqfields = array();
155
	$reqfieldsn = array();
156
	foreach ($pkg['fields']['field'] as $field) {
157
		if (($field['type'] == 'input') && isset($field['required'])) {
158
			if($field['fieldname'])
159
				$reqfields[] = $field['fieldname'];
160
			if($field['fielddescr'])	
161
				$reqfieldsn[] = $field['fielddescr'];
162
		}
163
	}
164
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
165

    
166
	if ($pkg['custom_php_validation_command'])
167
		eval($pkg['custom_php_validation_command']);
168

    
169
	// store values in xml configration file.
170
	if (!$input_errors) {
171
		$pkgarr = array();
172
		foreach ($pkg['fields']['field'] as $fields) {
173
			if($fields['type'] == "sorting")
174
				continue;
175
			if($fields['type'] == "listtopic")
176
				continue;
177
			if($fields['type'] == "rowhelper") {
178
				// save rowhelper items.
179
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
180
				                         // XXX: this really is not helping embedded platforms.
181
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
182
						if($firstfield == "")  {
183
						  $firstfield = $rowhelperfield['fieldname'];
184
						} else {
185
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
186
						}
187
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
188
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
189
						eval($comd);
190
						if($value <> "") {
191
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
192
							//echo($comd . "<br>");
193
							eval($comd);
194
						}
195
					}
196
				}
197
			} else {
198
				$fieldname  = $fields['fieldname'];
199
				if ($fieldname == "interface_array") {
200
					$fieldvalue = $_POST[$fieldname];
201
				} elseif (is_array($_POST[$fieldname])) {
202
					$fieldvalue = implode(',', $_POST[$fieldname]);
203
				} else {
204
					$fieldvalue = trim($_POST[$fieldname]);
205
					if ($fields['encoding'] == 'base64')
206
						$fieldvalue = base64_encode($fieldvalue);
207
				}
208
				if($fieldname)
209
					$pkgarr[$fieldname] = $fieldvalue;
210
			}
211
		}
212

    
213
		if (isset($id) && $a_pkg[$id])
214
			$a_pkg[$id] = $pkgarr;
215
		else
216
			$a_pkg[] = $pkgarr;
217

    
218
		write_config($pkg['addedit_string']);
219
		// late running code
220
		if($pkg['custom_add_php_command_late'] <> "") {
221
		    eval($pkg['custom_add_php_command_late']);
222
		}
223

    
224
		if (isset($pkg['filter_rules_needed'])) 
225
			filter_configure();
226

    
227
		// resync the configuration file code if defined.
228
		if($pkg['custom_php_resync_config_command'] <> "") {
229
		    eval($pkg['custom_php_resync_config_command']);
230
		}
231

    
232
		parse_package_templates();
233

    
234
		/* if start_command is defined, restart w/ this */
235
		if($pkg['start_command'] <> "")
236
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
237

    
238
		/* if restart_command is defined, restart w/ this */
239
		if($pkg['restart_command'] <> "")
240
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
241

    
242
		if($pkg['aftersaveredirect'] <> "") {
243
		    pfSenseHeader($pkg['aftersaveredirect']);
244
		} elseif(!$pkg['adddeleteeditpagefields']) {
245
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
246
		} elseif(!$pkg['preoutput']) {
247
		    pfSenseHeader("pkg.php?xml=" . $xml);
248
		}
249
		exit;
250
	} else {
251
		$get_from_post = true;
252
	}
253
}
254

    
255
if($pkg['title'] <> "") {
256
	$edit = ($only_edit ? '' : ": " .  gettext("Edit"));
257
	$title = $pkg['title'] . $edit;
258
}
259
else
260
	$title = gettext("Package Editor");
261

    
262
$pgtitle = $title;
263
include("head.inc");
264

    
265
if ($pkg['custom_php_after_head_command'])
266
	eval($pkg['custom_php_after_head_command']);
267

    
268
?>
269

    
270
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
271
<?php if($pkg['fields']['field'] <> "") { ?>
272
<script language="JavaScript">
273
<!--
274

    
275
	window.onDomReady = DomReady;
276
	function DomReady(fn) {
277
		if(document.addEventListener) { //W3C
278
			document.addEventListener("DOMContentLoaded", fn, false);
279
		} else { // IE
280
			document.onreadystatechange = function(){readyState(fn)}
281
		}
282
	}
283
	function readyState(fn) {
284
		//dom is ready for interaction
285
		if(document.readyState == "complete") {
286
			fn();
287
		}
288
	}
289
								        
290
	window.onDomReady(enablechange);
291

    
292
function enablechange() {
293
<?php
294
foreach ($pkg['fields']['field'] as $field) {
295
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
296
		echo "\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n";
297

    
298
		if (isset($field['enablefields'])) {
299
			foreach (explode(',', $field['enablefields']) as $enablefield) {
300
				echo "\t\tif(document.iform.elements[\"$enablefield\"]){\n";
301
				echo "\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n";
302
				echo "\t\t}else{\n";
303
				echo "\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 1;\n";
304
				echo "\t\t}\n";
305
			}
306
		}
307

    
308
		if (isset($field['checkenablefields'])) {
309
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
310
				echo "\t\tif(document.iform.elements[\"$checkenablefield\"]){\n";
311
				echo "\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n";
312
				echo "\t\t}else{\n";
313
				echo "\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n";
314
				echo "\t\t}\n";
315
			}
316
		}
317

    
318
		echo "\t}\n\telse {\n";
319

    
320
		if (isset($field['enablefields'])) {
321
			foreach (explode(',', $field['enablefields']) as $enablefield) {
322
				echo "\t\tif(document.iform.elements[\"$enablefield\"]){\n";
323
				echo "\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n";
324
				echo "\t\t}else{\n";
325
				echo "\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n";
326
				echo "\t\t}\n";
327
			}
328
		}
329

    
330
		if (isset($field['checkenablefields'])) {
331
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
332
				echo "\t\tif(document.iform.elements[\"$checkenablefield\"]){\n";
333
				echo "\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n";
334
				echo "\t\t}else{\n";
335
				echo "\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n";
336
				echo "\t\t}\n";
337
			}
338
		}
339

    
340
		echo "\t}\n";
341
	}
342
}
343
?>
344
}
345
//-->
346
</script>
347
<?php } ?>
348
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
349
</script>
350

    
351
<?php include("fbegin.inc"); ?>
352
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
353
<form name="iform" action="pkg_edit.php" method="post">
354
<input type="hidden" name="xml" value="<?= $xml ?>">
355
<?php if ($savemsg) print_info_box($savemsg); ?>
356
<table width="100%" border="0" cellpadding="0" cellspacing="0">
357
<?php
358
if ($pkg['tabs'] <> "") {
359
	$tab_array = array();
360
	foreach($pkg['tabs']['tab'] as $tab) {
361
		if($tab['tab_level'])
362
			$tab_level = $tab['tab_level'];
363
		else
364
			$tab_level = 1;
365
		if(isset($tab['active'])) {
366
			$active = true;
367
		} else {
368
			$active = false;
369
		}
370
		if(isset($tab['no_drop_down']))
371
			$no_drop_down = true;
372
		$urltmp = "";
373
		if($tab['url'] <> "") $urltmp = $tab['url'];
374
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
375

    
376
 		$addresswithport = getenv("HTTP_HOST");
377
		$colonpos = strpos($addresswithport, ":");
378
		if ($colonpos !== False) {
379
			//my url is actually just the IP address of the pfsense box
380
			$myurl = substr($addresswithport, 0, $colonpos);
381
		} else {
382
			$myurl = $addresswithport;
383
		}
384
		// eval url so that above $myurl item can be processed if need be.
385
		$url = str_replace('$myurl', $myurl, $urltmp);
386

    
387
		$tab_array[$tab_level][] = array(
388
						$tab['text'],
389
						$active,
390
						$url
391
					);
392
    	}
393

    
394
	ksort($tab_array);
395
	foreach($tab_array as $tab) {
396
		echo '<tr><td>';
397
		display_top_tabs($tab, $no_drop_down);
398
		echo '</td></tr>';
399
	}
400
}
401
?>
402
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
403
<?php
404
	$cols = 0;
405
	$savevalue = gettext("Save");
406
	if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
407
	/* If a package's XML has <advanced_options/> configured, then setup 
408
	 * the table rows for the fields that have <advancedfield/> set.
409
	 * These fields will be placed below other fields in a seprate area titled 'Advanced Features'.
410
	 * These advanced fields are not normally configured and generally left to default to 'default settings'.
411
	 */
412
	if ($pkg['advanced_options'] == "enabled") {
413
		$adv_enabled = true;
414
		$advanced .= "<td>&nbsp;</td>";
415
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br/></td></tr>\n";
416
	}
417

    
418
	foreach ($pkg['fields']['field'] as $pkga) {
419
		if ($pkga['type'] == "sorting") 
420
			continue;
421

    
422
		if ($pkga['type'] == "listtopic") {
423
			if(isset($pkga['advancedfield']) && $adv_enabled) {
424
				$advanced .= "<td>&nbsp;</td>";
425
				$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
426
			} else {
427
				echo "<td>&nbsp;</td>";
428
				echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
429
			}
430
			continue;
431
		}	
432
	
433
		if(!$pkga['combinefieldsend']) {
434
			if(isset($pkga['advancedfield']) && $adv_enabled)
435
				$advanced .= "<tr valign=\"top\">";
436
			else
437
		  		echo "<tr valign=\"top\">";
438
		}
439

    
440
		$size = "";
441

    
442
		if(!$pkga['dontdisplayname']) {
443
			unset($req);
444
			if (isset($pkga['required']))
445
				$req = 'req';
446
			if(isset($pkga['advancedfield']) && $adv_enabled) {
447
				$advanced .= "<td width=\"22%\" class=\"vncell{$req}\">";
448
				$advanced .= fixup_string($pkga['fielddescr']);
449
				$advanced .= "</td>";
450
			} else {
451
				echo "<td width=\"22%\" class=\"vncell{$req}\">";
452
				echo fixup_string($pkga['fielddescr']);
453
				echo "</td>";
454
			}
455
		}
456

    
457
		if(!$pkga['dontcombinecells'])
458
			if(isset($pkga['advancedfield']) && $adv_enabled)
459
				$advanced .= "<td class=\"vtable\">";
460
			else
461
				echo "<td class=\"vtable\">";
462
		// if user is editing a record, load in the data.
463
		$fieldname = $pkga['fieldname'];
464
		if ($get_from_post) {
465
			$value = $_POST[$fieldname];
466
			if (is_array($value)) $value = implode(',', $value);
467
		} else {
468
			if (isset($id) && $a_pkg[$id])
469
				$value = $a_pkg[$id][$fieldname];
470
			else
471
				$value = $pkga['default_value'];
472
		}
473

    
474
		if($pkga['type'] == "input") {
475
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
476
			if(isset($pkga['advancedfield']) && $adv_enabled) {
477
				$js_array[] = $pkga['fieldname'];
478
				$advanced .= display_advanced_field($pkga['fieldname']);
479
				$advanced .= "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
480
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
481
				$advanced .= "</div>\n";
482
			} else {
483
				echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
484
				echo "<br>" . fixup_string($pkga['description']) . "\n";
485
			}
486
		} else if($pkga['type'] == "password") {
487
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
488
			if(isset($pkga['advancedfield']) && $adv_enabled) {
489
				$js_array[] = $pkga['fieldname'];
490
				$advanced .= display_advanced_field($pkga['fieldname']);
491
				$advanced .= "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
492
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
493
				$advanced .= "</div>\n";
494
			} else {
495
				echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
496
				echo "<br>" . fixup_string($pkga['description']) . "\n";
497
			}
498
		} else if($pkga['type'] == "select") {
499
			$fieldname = $pkga['fieldname'];
500
			if (isset($pkga['multiple'])) {
501
      	$multiple = 'multiple="multiple"';
502
				$items = explode(',', $value);
503
				$fieldname .= "[]";
504
			} else {
505
				$multiple = '';
506
				$items = array($value);
507
			}
508
			$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
509
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
510

    
511
			if(isset($pkga['advancedfield']) && $adv_enabled) {
512
				$js_array[] = $pkga['fieldname'];
513
				$advanced .= display_advanced_field($pkga['fieldname']);
514
				$advanced .= "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n";
515
				foreach ($pkga['options']['option'] as $opt) {
516
					$selected = '';
517
					if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
518
						$advanced .= "\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n";
519
				}
520
				$advanced .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
521
				$advanced .= "</div>\n";
522
			} else {
523
				echo "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n";
524
				foreach ($pkga['options']['option'] as $opt) {
525
					$selected = '';
526
					if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
527
					echo "\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n";
528
				}
529
				echo "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
530
			}
531

    
532
		} else if($pkga['type'] == "select_source") {
533
			$fieldname = $pkga['fieldname'];
534
			if (isset($pkga['multiple'])) {
535
				$multiple = 'multiple="multiple"';
536
				$items = explode(',', $value);
537
				$fieldname .= "[]";
538
			} else {
539
				$multiple = '';
540
				$items = array($value);
541
			}
542
			$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
543
			$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
544

    
545
			if(isset($pkga['advancedfield']) && $adv_enabled) {
546
				$js_array[] = $pkga['fieldname'];
547
				$advanced .= display_advanced_field($pkga['fieldname']);
548
				$advanced .= "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"{$fieldname}\" name=\"{$fieldname}\">\n";
549
				$advanced .= "</div>\n";
550
			} else {
551
				echo "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n";
552
			}
553
			$source_url = $pkga['source'];
554
			eval("\$pkg_source_txt = &$source_url;");
555
			foreach ($pkg_source_txt as $opt) {
556
				$selected = '';
557
				if($pkga['source_name']) {
558
					$source_name = $opt[$pkga['source_name']];
559
				} else {
560
					$source_name = $opt[$pkga['name']];
561
				}
562
				if($pkga['source_value']) {
563
					$source_value = $opt[$pkga['source_value']];
564
				} else {
565
					$source_value = $opt[$pkga['value']];
566
				}
567
				if (in_array($source_value, $items)) $selected = 'selected="selected"';
568
					if(isset($pkga['advancedfield']) && $adv_enabled)
569
						$advanced .= "\t<option name=\"{$source_name}\" value=\"{$source_value}\" $selected>{$source_name}</option>\n";
570
					else
571
						echo "\t<option name=\"{$source_name}\" value=\"{$source_value}\" $selected>{$source_name}</option>\n";
572
			}
573

    
574
			if(isset($pkga['advancedfield']) && $adv_enabled)
575
				$advanced .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
576
			else
577
				echo "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";		
578
		} else if($pkga['type'] == "vpn_selection") {
579
			if(isset($pkga['advancedfield']) && $adv_enabled) {
580
				$js_array[] = $pkga['fieldname'];
581
				$advanced .= display_advanced_field($pkga['fieldname']);
582
				$advanced .= "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
583
				foreach ($config['ipsec']['phase1'] as $vpn) {
584
					$advanced .= "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
585
				}
586
				$advanced .= "</select>\n";
587
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
588
				$advanced .= "</div>\n";
589
			} else {
590
				echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
591
				foreach ($config['ipsec']['phase1'] as $vpn) {
592
					echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
593
				}
594
				echo "</select>\n";
595
				echo "<br>" . fixup_string($pkga['description']) . "\n";
596
			}
597
		} else if($pkga['type'] == "checkbox") {
598
			$checkboxchecked = "";
599
			if($value == "on") $checkboxchecked = " CHECKED";
600
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
601
				$onclick = ' onclick="javascript:enablechange();"';
602
			if(isset($pkga['advancedfield']) && $adv_enabled) {
603
				$js_array[] = $pkga['fieldname'];
604
				$advanced .= display_advanced_field($pkga['fieldname']);
605
				$advanced .= "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
606
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
607
				$advanced .= "</div>\n";
608
			} else {
609
				echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
610
				echo "<br>" . fixup_string($pkga['description']) . "\n";
611
			}
612
		} else if($pkga['type'] == "textarea") {
613
			if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
614
			if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
615
			if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
616
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
617
			if(isset($pkga['advancedfield']) && $adv_enabled) {
618
				$js_array[] = $pkga['fieldname'];
619
				$advanced .= display_advanced_field($pkga['fieldname']);
620
				$advanced .= "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
621
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
622
				$advanced .= "</div>\n";
623
			} else {
624
				echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
625
				echo "<br>" . fixup_string($pkga['description']) . "\n";
626
			}
627
		} else if($pkga['type'] == "interfaces_selection") {
628
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
629
			$multiple = '';
630
			$fieldname = $pkga['fieldname'];
631
			if (isset($pkga['multiple'])) {
632
				$fieldname .= '[]';
633
				$multiple = 'multiple';
634
			}
635
			if(isset($pkga['advancedfield']) && $adv_enabled) {
636
				$js_array[] = $pkga['fieldname'];
637
				$advanced .= display_advanced_field($pkga['fieldname']);
638
				$advanced .= "<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n";
639
			} else {
640
				echo "<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n";
641
			}
642
			$ifaces = get_configured_interface_with_descr();
643
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
644
			if (!empty($additional_ifaces))
645
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
646
			if(is_array($value))
647
				$values = $value;
648
			else
649
				$values  =  explode(',',  $value);
650
			$ifaces["lo0"] = "loopback";
651
			foreach($ifaces as $ifname => $iface) {
652
				$selected = (in_array($ifname, $values) ? 'selected' : '');
653
				if(isset($pkga['advancedfield']) && $adv_enabled)
654
					$advanced .= "<option value=\"$ifname\" $selected>$iface</option>\n";
655
				else
656
					echo "<option value=\"$ifname\" $selected>$iface</option>\n";
657
			}
658
			if(isset($pkga['advancedfield']) && $adv_enabled) {
659
				$advanced .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
660
				$advanced .= "</div>\n";
661
			} else {
662
				echo "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
663
			}
664
		} else if($pkga['type'] == "radio") {
665
			if(isset($pkga['advancedfield']) && $adv_enabled)
666
				$advanced .= "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
667
			else
668
				echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
669
		} else if($pkga['type'] == "rowhelper") {
670
		?>
671
			<script type="text/javascript" language='javascript'>
672
			<!--
673
			<?php
674
				$rowcounter = 0;
675
				$fieldcounter = 0;
676
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
677
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
678
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
679
					$fieldcounter++;
680
				}
681
			?>
682
			-->
683
			</script>
684
			<table name="maintable" id="maintable">
685
			<tr>
686
			<?php
687
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
688
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
689
				}
690
				echo "</tr>";
691

    
692
				echo "<tr>";
693
				  // XXX: traverse saved fields, add back needed rows.
694
				echo "</tr>";
695

    
696
				echo "<tr>\n";
697
				$rowcounter = 0;
698
				$trc = 0;
699
				if(isset($a_pkg[$id]['row'])) {
700
					foreach($a_pkg[$id]['row'] as $row) {
701
					/*
702
					 * loop through saved data for record if it exists, populating rowhelper
703
					 */
704
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
705
							unset($value);
706
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
707
							$fieldname = $rowhelper['fieldname'];
708
							// if user is editing a record, load in the data.
709
							if (isset($id) && $a_pkg[$id]) {
710
								$value = $row[$fieldname];
711
							}
712
							$options = "";
713
							$type = $rowhelper['type'];
714
							$fieldname = $rowhelper['fieldname'];
715
							if($type == "option")
716
								$options = &$rowhelper['options']['option'];
717
							if($rowhelper['size']) 
718
								$size = $rowhelper['size'];
719
							else if ($pkga['size'])
720
								$size = $pkga['size'];
721
							else
722
								$size = "8";
723
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
724
							// javascript helpers for row_helper_dynamic.js
725
							echo "</td>\n";
726
							echo "<script language=\"JavaScript\">\n";
727
							echo "<!--\n";
728
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
729
							echo "-->\n";
730
							echo "</script>\n";
731
							$text = "";
732
							$trc++;
733
						}
734

    
735
						$rowcounter++;
736
						echo "<td>";
737
						echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" /></a>";
738
						echo "</td>\n";
739
						echo "</tr>\n";
740
					}
741
				}
742
				if($trc == 0) {
743
					/*
744
					 *  no records loaded.
745
                                         *  just show a generic line non-populated with saved data
746
                                         */
747
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
748
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
749
						$fieldname = $rowhelper['fieldname'];
750
						$options = "";
751
						$type = $rowhelper['type'];
752
						$fieldname = $rowhelper['fieldname'];
753
						if($type == "option") $options = &$rowhelper['options']['option'];
754
						if($rowhelper['size'] <> "")
755
							$size = $rowhelper['size'];
756
						else if ($pkga['size'])
757
							$size = $pkga['size'];
758
						else
759
							$size = 8;
760
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
761
						// javascript helpers for row_helper_dynamic.js
762
						echo "</td>\n";
763
						echo "<script language=\"JavaScript\">\n";
764
						echo "<!--\n";
765
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
766
						echo "-->\n";
767
						echo "</script>\n";
768
						$text = "";
769
						$trc++;
770
					}
771

    
772
					$rowcounter++;
773
					echo "<td>";
774
					echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" /></a>";
775
					echo "</td>\n";
776
					echo "</tr>\n";
777
				}
778
			?>
779

    
780
			  <tbody></tbody>
781
			</table>
782

    
783
		<?php if ($pkga['size']): ?>
784
			<br><a onClick="javascript:addRowTo('maintable', <?=$pkga['size'];?>); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
785
		<?php else: ?>
786
			<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
787
		<?php endif; ?>
788
		<br><?php if($pkga['description'] != "") echo $pkga['description']; ?>
789
		<script language="JavaScript">
790
		<!--
791
		field_counter_js = <?= $fieldcounter ?>;
792
		rows = <?= $rowcounter ?>;
793
		totalrows = <?php echo $rowcounter; ?>;
794
		loaded = <?php echo $rowcounter; ?>;
795
		//typesel_change();
796
		//-->
797
		</script>
798

    
799
		<?php
800
	      }
801
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
802
	     ?>
803

    
804
      <?php
805
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
806
      $i++;
807
  }
808
 ?>
809
  <tr>
810
	<td>&nbsp;</td>
811
  </tr>
812
  <tr>
813
    <td width="22%" valign="top">&nbsp;</td>
814
    <td width="78%">
815
<?php
816
if($pkg['note'] != "")
817
	echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
818
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
819
      echo "<input name=\"id\" type=\"hidden\" value=\"$id\">";
820
?>
821
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
822
<?php if (!$only_edit): ?>
823
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
824
<?php endif; ?>
825
    </td>
826
  </tr>
827
<?php if (isset($advanced)) { 
828
				echo $advanced;
829
?>
830
	<tr>
831
		<td width="22%" valign="top">&nbsp;</td>
832
		<td width="78%">
833
			<input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
834
			<?php if (!$only_edit): ?>
835
				<input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
836
			<?php endif; ?>
837
		</td>
838
	</tr>
839
<?php
840
		}
841
?>
842
</table>
843
</div></tr></td>
844
</table>
845
</form>
846

    
847
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
848

    
849
<?php
850
	/* JavaScript to handle the advanced fields. */
851
	if ($pkg['advanced_options'] == "enabled") {
852
		echo "<script type=\"text/javascript\">\n";
853
		foreach($js_array as $advfieldname) {
854
			echo "function show_" . $advfieldname . "() {\n";
855
			echo "document.getElementById('showadv_{$advfieldname}').innerHTML='';\n";
856
			echo "aodiv = document.getElementById('show_{$advfieldname}');\n";
857
			echo "aodiv.style.display = 'block';\n";
858
			echo "}\n\n";
859
		}
860
		echo "</script>\n";
861
	}
862
?>
863

    
864
<?php include("fend.inc"); ?>
865
</body>
866
</html>
867

    
868
<?php
869
/*
870
 * ROW Helpers function
871
 */
872
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
873
	global $text, $config;
874
	echo "<td>\n";
875
	if($type == "input") {
876
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' class='formfld unknown' value='" . $value . "'>\n";
877
	} else if($type == "checkbox") {
878
		if($value)
879
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
880
		else
881
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
882
	} else if($type == "password") {
883
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
884
	} else if($type == "textarea") {
885
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
886
	} else if($type == "select") {
887
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
888
		foreach($rowhelper['options']['option'] as $rowopt) {
889
			$selected = "";
890
			if($rowopt['value'] == $value) $selected = " SELECTED";
891
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
892
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
893
		}
894
		echo "</select>\n";
895
	} else if($type == "select_source") {
896
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
897
		$source_url = $rowhelper['source'];
898
		eval("\$pkg_source_txt = &$source_url;");
899
		foreach($pkg_source_txt as $opt) {
900
			$selected = "";
901
			if($rowhelper['source_name']) {
902
				$source_name = $opt[$rowhelper['source_name']];
903
		    	} else {
904
				$source_name = $opt[$rowhelper['name']];
905
		    	}
906
		  	if($rowhelper['source_value']) {
907
				$source_value = $opt[$rowhelper['source_value']];
908
		  	} else {
909
				$source_value = $opt[$rowhelper['value']];
910
		  	}
911
			if($source_value == $value) 
912
				$selected = " SELECTED";
913
			$text .= "<option value='" . $source_value . "'" . $selected . ">" . $source_name . "</option>";
914
			echo "<option value='" . $source_value . "'" . $selected . ">" . $source_name . "</option>\n";
915
		}
916
		echo "</select>\n";		
917
	}
918
}
919

    
920
function fixup_string($string) {
921
	global $config;
922
	// fixup #1: $myurl -> http[s]://ip_address:port/
923
	$https = "";
924
	$port = $config['system']['webguiport'];
925
	if($port <> "443" and $port <> "80")
926
		$urlport = ":" . $port;
927
	else
928
		$urlport = "";
929

    
930
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
931
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
932
	$newstring = str_replace("\$myurl", $myurl, $string);
933
	$string = $newstring;
934
	// fixup #2: $wanip
935
	$curwanip = get_interface_ip();
936
	$newstring = str_replace("\$wanip", $curwanip, $string);
937
	$string = $newstring;
938
	// fixup #3: $lanip
939
	$lancfg = $config['interfaces']['lan'];
940
	$lanip = $lancfg['ipaddr'];
941
	$newstring = str_replace("\$lanip", $lanip, $string);
942
	$string = $newstring;
943
	// fixup #4: fix'r'up here.
944
	return $newstring;
945
}
946

    
947
/*
948
 *  Parse templates if they are defined
949
 */
950
function parse_package_templates() {
951
	global $pkg, $config;
952
	$rows = 0;
953
	if($pkg['templates']['template'] <> "")
954
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
955
		$filename = $pkg_template_row['filename'];
956
		$template_text = $pkg_template_row['templatecontents'];
957
		$firstfield = "";
958
		/* calculate total row helpers count */
959
		foreach ($pkg['fields']['field'] as $fields) {
960
			if($fields['type'] == "rowhelper") {
961
				// save rowhelper items.
962
                                $row_helper_total_rows = 0;
963
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
964
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
965
						if($firstfield == "")  {
966
						  $firstfield = $rowhelperfield['fieldname'];
967
						} else {
968
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
969
						}
970
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
971
						$value = "";
972
						eval($comd);
973
						if($value <> "") {
974
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
975
						} else {
976
						    $row_helper_total_rows = $rows;
977
						    break;
978
						}
979
					}
980
				}
981
			}
982
		}
983

    
984
		/* replace $domain_total_rows with total rows */
985
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
986

    
987
		/* change fields defined as fieldname_fieldvalue to their value */
988
		foreach ($pkg['fields']['field'] as $fields) {
989
			if($fields['type'] == "rowhelper") {
990
				// save rowhelper items.
991
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
992
					$row_helper_data = "";
993
					$isfirst = 0;
994
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
995
						if($firstfield == "")  {
996
						  $firstfield = $rowhelperfield['fieldname'];
997
						} else {
998
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
999
						}
1000
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
1001
						eval($comd);
1002
						if($value <> "") {
1003
						    if($isfirst == 1) $row_helper_data .= "  " ;
1004
						    $row_helper_data .= $value;
1005
						    $isfirst = 1;
1006
						}
1007
						$sep = "";
1008
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
1009
						foreach ($sep as $se) $seperator = $se;
1010
						if($seperator <> "") {
1011
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
1012
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
1013
						}
1014
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
1015
					}
1016
				}
1017
			} else {
1018
				$fieldname  = $fields['fieldname'];
1019
				$fieldvalue = $_POST[$fieldname];
1020
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
1021
			}
1022
		}
1023

    
1024
		/* replace cr's */
1025
		$template_text = str_replace("\\n", "\n", $template_text);
1026

    
1027
		/* write out new template file */
1028
		$fout = fopen($filename,"w");
1029
		fwrite($fout, $template_text);
1030
		fclose($fout);
1031
	    }
1032
}
1033

    
1034
/* Return html div fields */
1035
function display_advanced_field($fieldname) {
1036
	$div = "<div id='showadv_" . $fieldname . "'>\n";
1037
	$div .= "<input type='button' onClick='show_" . $fieldname . "()' value='" . gettext("Advanced") . "'></input> - " . gettext("Show advanced option") ."</div>\n";
1038
	$div .= "<div id='show_" . $fieldname . "' style='display:none'>\n";
1039
	return $div;
1040
}
1041

    
1042
?>
(125-125/239)