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
			foreach($ifaces as $ifname => $iface) {
648
				$selected = (in_array($ifname, $values) ? 'selected' : '');
649
				if(isset($pkga['advancedfield']) && $adv_enabled)
650
					$advanced .= "<option value=\"$ifname\" $selected>$iface</option>\n";
651
				else
652
					echo "<option value=\"$ifname\" $selected>$iface</option>\n";
653
			}
654
			if(isset($pkga['advancedfield']) && $adv_enabled) {
655
				$advanced .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
656
				$advanced .= "</div>\n";
657
			} else {
658
				echo "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
659
			}
660
		} else if($pkga['type'] == "radio") {
661
			if(isset($pkga['advancedfield']) && $adv_enabled)
662
				$advanced .= "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
663
			else
664
				echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
665
		} else if($pkga['type'] == "rowhelper") {
666
		?>
667
			<script type="text/javascript" language='javascript'>
668
			<!--
669
			<?php
670
				$rowcounter = 0;
671
				$fieldcounter = 0;
672
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
673
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
674
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
675
					$fieldcounter++;
676
				}
677
			?>
678
			-->
679
			</script>
680
			<table name="maintable" id="maintable">
681
			<tr>
682
			<?php
683
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
684
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
685
				}
686
				echo "</tr>";
687

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

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

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

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

    
776
			  <tbody></tbody>
777
			</table>
778

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1037
?>
(117-117/221)