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
// 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
				$fieldvalue = $_POST[$fieldname];
200
				if (is_array($fieldvalue))
201
					$fieldvalue = implode(',', $fieldvalue);
202
				else {
203
					$fieldvalue = trim($fieldvalue);
204
					if ($fields['encoding'] == 'base64')
205
						$fieldvalue = base64_encode($fieldvalue);
206
				}
207
				if($fieldname)
208
					$pkgarr[$fieldname] = $fieldvalue;
209
			}
210
		}
211

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

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

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

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

    
231
		parse_package_templates();
232

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

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

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

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

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

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

    
267
?>
268

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
437
		$size = "";
438

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

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

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

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

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

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

    
571
			if(isset($pkga['advancedfield']) && $adv_enabled)
572
				$advanced .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
573
			else
574
				echo "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";		
575
		} else if($pkga['type'] == "vpn_selection") {
576
			if(isset($pkga['advancedfield']) && $adv_enabled) {
577
				$js_array[] = $pkga['fieldname'];
578
				$advanced .= display_advanced_field($pkga['fieldname']);
579
				$advanced .= "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
580
				foreach ($config['ipsec']['phase1'] as $vpn) {
581
					$advanced .= "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
582
				}
583
				$advanced .= "</select>\n";
584
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
585
				$advanced .= "</div>\n";
586
			} else {
587
				echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
588
				foreach ($config['ipsec']['phase1'] as $vpn) {
589
					echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
590
				}
591
				echo "</select>\n";
592
				echo "<br>" . fixup_string($pkga['description']) . "\n";
593
			}
594
		} else if($pkga['type'] == "checkbox") {
595
			$checkboxchecked = "";
596
			if($value == "on") $checkboxchecked = " CHECKED";
597
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
598
				$onclick = ' onclick="javascript:enablechange();"';
599
			if(isset($pkga['advancedfield']) && $adv_enabled) {
600
				$js_array[] = $pkga['fieldname'];
601
				$advanced .= display_advanced_field($pkga['fieldname']);
602
				$advanced .= "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
603
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
604
				$advanced .= "</div>\n";
605
			} else {
606
				echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
607
				echo "<br>" . fixup_string($pkga['description']) . "\n";
608
			}
609
		} else if($pkga['type'] == "textarea") {
610
			if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
611
			if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
612
			if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
613
			if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
614
			if(isset($pkga['advancedfield']) && $adv_enabled) {
615
				$js_array[] = $pkga['fieldname'];
616
				$advanced .= display_advanced_field($pkga['fieldname']);
617
				$advanced .= "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
618
				$advanced .= "<br>" . fixup_string($pkga['description']) . "\n";
619
				$advanced .= "</div>\n";
620
			} else {
621
				echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
622
				echo "<br>" . fixup_string($pkga['description']) . "\n";
623
			}
624
		} else if($pkga['type'] == "interfaces_selection") {
625
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
626
			$multiple = '';
627
			$fieldname = $pkga['fieldname'];
628
			if (isset($pkga['multiple'])) {
629
				$fieldname .= '[]';
630
				$multiple = 'multiple';
631
			}
632
			if(isset($pkga['advancedfield']) && $adv_enabled) {
633
				$js_array[] = $pkga['fieldname'];
634
				$advanced .= display_advanced_field($pkga['fieldname']);
635
				$advanced .= "<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n";
636
			} else {
637
				echo "<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n";
638
			}
639
			$ifaces = get_configured_interface_with_descr();
640
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
641
			if (!empty($additional_ifaces))
642
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
643
			if(is_array($value))
644
				$values = $value;
645
			else
646
				$values  =  explode(',',  $value);
647
			$ifaces["lo0"] = "loopback";
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 "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" /></a>";
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 "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" /></a>";
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 . "' class='formfld unknown' 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
?>
(119-119/225)