Project

General

Profile

Download (36.4 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
		if(isset($tab['no_drop_down']))
370
			$no_drop_down = true;
371
		$urltmp = "";
372
		if($tab['url'] <> "") $urltmp = $tab['url'];
373
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
374

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

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

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

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

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

    
439
		$size = "";
440

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

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

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

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

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

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

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

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

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

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

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

    
779
			  <tbody></tbody>
780
			</table>
781

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

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

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

    
845
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
846

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

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

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

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

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

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

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

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

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

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

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

    
1040
?>
(119-119/226)