Project

General

Profile

Download (36.3 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
if(!is_numeric($id)) {
88
	Header("Location: /");
89
	exit;
90
}
91
	
92
	
93
// Not posting?  Then user is editing a record. There must be a valid id
94
// when editing a record.
95
if(!$id && !$_POST)
96
	$id = "0";
97
	
98
if($pkg['custom_php_global_functions'] <> "")
99
        eval($pkg['custom_php_global_functions']);
100

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

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

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

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

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

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

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

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

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

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

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

    
170
	// store values in xml configration file.
171
	if (!$input_errors) {
172
		$pkgarr = array();
173
		foreach ($pkg['fields']['field'] as $fields) {
174
			if($fields['type'] == "sorting")
175
				continue;
176
			if($fields['type'] == "listtopic")
177
				continue;
178
			if($fields['type'] == "rowhelper") {
179
				// save rowhelper items.
180
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
181
				                         // XXX: this really is not helping embedded platforms.
182
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
183
						if($firstfield == "")  {
184
						  $firstfield = $rowhelperfield['fieldname'];
185
						} else {
186
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
187
						}
188
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
189
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
190
						eval($comd);
191
						if($value <> "") {
192
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
193
							//echo($comd . "<br>");
194
							eval($comd);
195
						}
196
					}
197
				}
198
			} else {
199
				$fieldname  = $fields['fieldname'];
200
				$fieldvalue = $_POST[$fieldname];
201
				if (is_array($fieldvalue))
202
					$fieldvalue = implode(',', $fieldvalue);
203
				else {
204
					$fieldvalue = trim($fieldvalue);
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
		$urltmp = "";
371
		if($tab['url'] <> "") $urltmp = $tab['url'];
372
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
373

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

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

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

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

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

    
438
		$size = "";
439

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

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

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

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

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

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

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

    
689
				echo "<tr>";
690
				  // XXX: traverse saved fields, add back needed rows.
691
				echo "</tr>";
692

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

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

    
769
					$rowcounter++;
770
					echo "<td>";
771
					echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
772
					echo "</td>\n";
773
					echo "</tr>\n";
774
				}
775
			?>
776

    
777
			  <tbody></tbody>
778
			</table>
779

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

    
795
		<?php
796
	      }
797
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
798
	     ?>
799

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

    
843
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
844

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

    
860
<?php include("fend.inc"); ?>
861
</body>
862
</html>
863

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

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

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

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

    
980
		/* replace $domain_total_rows with total rows */
981
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
982

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

    
1020
		/* replace cr's */
1021
		$template_text = str_replace("\\n", "\n", $template_text);
1022

    
1023
		/* write out new template file */
1024
		$fout = fopen($filename,"w");
1025
		fwrite($fout, $template_text);
1026
		fclose($fout);
1027
	    }
1028
}
1029

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

    
1038
?>
(116-116/220)