Project

General

Profile

Download (37.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2004-2012 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
function domTT_title($title_msg){
58
	if (!empty($title_msg)){
59
		$title_msg=preg_replace("/\s+/"," ",$title_msg);
60
        $title_msg=preg_replace("/'/","\'",$title_msg);
61
		return "onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\" onmouseover=\"domTT_activate(this, event, 'content', '{$title_msg}', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'delay',300,'styleClass', 'niceTitle');\"";
62
	}
63
}
64

    
65
$xml = htmlspecialchars($_GET['xml']);
66
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
67

    
68
if($xml == "") {
69
            print_info_box_np(gettext("ERROR: No package defined."));
70
            die;
71
} else {
72
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
73
}
74

    
75
if($pkg['include_file'] <> "") {
76
	require_once($pkg['include_file']);
77
}
78

    
79
if (!isset($pkg['adddeleteeditpagefields']))
80
	$only_edit = true;
81
else
82
	$only_edit = false;
83

    
84
$package_name = $pkg['menu'][0]['name'];
85
$section      = $pkg['menu'][0]['section'];
86
$config_path  = $pkg['configpath'];
87
$name         = $pkg['name'];
88
$title        = $pkg['title'];
89
$pgtitle      = $title;
90

    
91
$id = $_GET['id'];
92
if (isset($_POST['id']))
93
	$id = htmlspecialchars($_POST['id']);
94

    
95
// Not posting?  Then user is editing a record. There must be a valid id
96
// when editing a record.
97
if(!$id && !$_POST)
98
	$id = "0";
99

    
100
if(!is_numeric($id)) {
101
	Header("Location: /");
102
	exit;
103
}
104

    
105
if($pkg['custom_php_global_functions'] <> "")
106
        eval($pkg['custom_php_global_functions']);
107

    
108
// grab the installedpackages->package_name section.
109
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
110
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
111

    
112
// If the first entry in the array is an empty <config/> tag, kill it.
113
if ((count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) 
114
	&& ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == ""))
115
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
116

    
117
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
118

    
119
if($_GET['savemsg'] <> "")
120
	$savemsg = htmlspecialchars($_GET['savemsg']);
121

    
122
if($pkg['custom_php_command_before_form'] <> "")
123
	eval($pkg['custom_php_command_before_form']);
124

    
125
if ($_POST) {
126
	$firstfield = "";
127
	$rows = 0;
128

    
129
	$input_errors = array();
130
	$reqfields = array();
131
	$reqfieldsn = array();
132
	foreach ($pkg['fields']['field'] as $field) {
133
		if (($field['type'] == 'input') && isset($field['required'])) {
134
			if($field['fieldname'])
135
				$reqfields[] = $field['fieldname'];
136
			if($field['fielddescr'])
137
				$reqfieldsn[] = $field['fielddescr'];
138
		}
139
	}
140
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
141

    
142
	if ($pkg['custom_php_validation_command'])
143
		eval($pkg['custom_php_validation_command']);
144

    
145
	if($_POST['act'] == "del") {
146
		if($pkg['custom_delete_php_command']) {
147
		    if($pkg['custom_php_command_before_form'] <> "")
148
			    eval($pkg['custom_php_command_before_form']);
149
		    eval($pkg['custom_delete_php_command']);
150
		}
151
		write_config($pkg['delete_string']);
152
		// resync the configuration file code if defined.
153
		if($pkg['custom_php_resync_config_command'] <> "") {
154
			if($pkg['custom_php_command_before_form'] <> "")
155
				eval($pkg['custom_php_command_before_form']);
156
			eval($pkg['custom_php_resync_config_command']);
157
		}
158
	} else {
159
		if(!$input_errors && $pkg['custom_add_php_command']) {
160
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
161
			?>
162

    
163
<?php include("head.inc"); ?>
164
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
165
<?php include("fbegin.inc"); ?>
166
<?php
167
			}
168
			if($pkg['preoutput']) echo "<pre>";
169
			eval($pkg['custom_add_php_command']);
170
			if($pkg['preoutput']) echo "</pre>";
171
		}
172
	}
173

    
174
	// donotsave is enabled.  lets simply exit.
175
	if(empty($pkg['donotsave'])) {
176

    
177
		// store values in xml configration file.
178
		if (!$input_errors) {
179
			$pkgarr = array();
180
			foreach ($pkg['fields']['field'] as $fields) {
181
				switch($fields['type']){
182
					case "rowhelper":
183
						// save rowhelper items.
184
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
185
						$rowhelpername="row";
186
						foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
187
							foreach($_POST as $key => $value){
188
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches))
189
									$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']]=$value;
190
							}
191
						break;
192
					default:
193
						$fieldname  = $fields['fieldname'];
194
						if ($fieldname == "interface_array") {
195
							$fieldvalue = $_POST[$fieldname];
196
						} elseif (is_array($_POST[$fieldname])) {
197
							$fieldvalue = implode(',', $_POST[$fieldname]);
198
						} else {
199
							$fieldvalue = trim($_POST[$fieldname]);
200
							if ($fields['encoding'] == 'base64')
201
								$fieldvalue = base64_encode($fieldvalue);
202
						}
203
						if($fieldname)
204
							$pkgarr[$fieldname] = $fieldvalue;
205
					}
206
			}
207

    
208
			if (isset($id) && $a_pkg[$id])
209
				$a_pkg[$id] = $pkgarr;
210
			else
211
				$a_pkg[] = $pkgarr;
212

    
213
			write_config($pkg['addedit_string']);
214
			// late running code
215
			if($pkg['custom_add_php_command_late'] <> "") {
216
			    eval($pkg['custom_add_php_command_late']);
217
			}
218

    
219
			if (isset($pkg['filter_rules_needed']))
220
				filter_configure();
221

    
222
			// resync the configuration file code if defined.
223
			if($pkg['custom_php_resync_config_command'] <> "") {
224
			    eval($pkg['custom_php_resync_config_command']);
225
			}
226

    
227
			parse_package_templates();
228

    
229
			/* if start_command is defined, restart w/ this */
230
			if($pkg['start_command'] <> "")
231
			    exec($pkg['start_command'] . ">/dev/null 2&>1");
232

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

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

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

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

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

    
266
?>
267

    
268
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
269
<?php if($pkg['fields']['field'] <> "") { ?>
270
<script type="text/javascript" language="javascript">
271
	//Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded
272
	jQuery(document).ready(function() {
273
		
274
		//Sortable function
275
		jQuery('#mainarea table tbody').sortable({
276
			items: 'tr.sortable',
277
			cursor: 'move',
278
			distance: 10,
279
			opacity: 0.8,
280
			helper: function(e,ui){  
281
				ui.children().each(function(){  
282
					jQuery(this).width(jQuery(this).width());  
283
				});
284
			return ui;  
285
			},
286
		});
287
		
288
		//delete current line jQuery function
289
		jQuery('#maintable td .delete').live('click', function() {
290
			//do not remove first line
291
			if (jQuery("#maintable tr").length > 2)
292
				jQuery(this).parent().parent().remove();
293
	    });
294
	    
295
		//add new line jQuery function
296
		jQuery('#mainarea table .add').click(function() {
297
			//get table size and assign as new id
298
			var c_id=jQuery("#maintable tr").length;
299
			var new_row=jQuery("table#maintable tr:last").html().replace(/(name|id)="(\w+)(\d+)"/g,"$1='$2"+c_id+"'");
300
			//apply new id to created line rowhelperid
301
			jQuery("table#maintable tr:last").after("<tr>"+new_row+"</tr>");
302
	    });
303
		// Call enablechange function
304
		enablechange();
305
	});
306

    
307
	function enablechange() {
308
	<?php
309
	foreach ($pkg['fields']['field'] as $field) {
310
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
311
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
312

    
313
			if (isset($field['enablefields'])) {
314
				foreach (explode(',', $field['enablefields']) as $enablefield) {
315
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
316
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',true);\n";
317
					echo "\t\t}\n";
318
				}
319
			}
320

    
321
			if (isset($field['checkenablefields'])) {
322
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
323
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
324
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
325
					echo "\t\t}\n";
326
				}
327
			}
328

    
329
			echo "\t}\n\telse {\n";
330

    
331
			if (isset($field['enablefields'])) {
332
				foreach (explode(',', $field['enablefields']) as $enablefield) {
333
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
334
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
335
					echo "\t\t}\n";
336
				}
337
			}
338

    
339
			if (isset($field['checkenablefields'])) {
340
				foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
341
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
342
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
343
					echo "\t\t}\n";
344
				}
345
			}
346

    
347
			echo "\t}\n";
348
		}
349
	}
350
	?>
351
}
352
//-->
353
</script>
354
<?php } ?>
355
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
356
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
357
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
358
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
359
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js"></script>
360

    
361
<?php include("fbegin.inc"); ?>
362
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
363
<form name="iform" action="pkg_edit.php" method="post">
364
<input type="hidden" name="xml" value="<?= htmlspecialchars($xml) ?>">
365
<?php if ($savemsg) print_info_box($savemsg); ?>
366
<table width="100%" border="0" cellpadding="0" cellspacing="0">
367
<?php
368
if ($pkg['tabs'] <> "") {
369
	$tab_array = array();
370
	foreach($pkg['tabs']['tab'] as $tab) {
371
		if($tab['tab_level'])
372
			$tab_level = $tab['tab_level'];
373
		else
374
			$tab_level = 1;
375
		if(isset($tab['active'])) {
376
			$active = true;
377
		} else {
378
			$active = false;
379
		}
380
		if(isset($tab['no_drop_down']))
381
			$no_drop_down = true;
382
		$urltmp = "";
383
		if($tab['url'] <> "") $urltmp = $tab['url'];
384
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
385

    
386
 		$addresswithport = getenv("HTTP_HOST");
387
		$colonpos = strpos($addresswithport, ":");
388
		if ($colonpos !== False) {
389
			//my url is actually just the IP address of the pfsense box
390
			$myurl = substr($addresswithport, 0, $colonpos);
391
		} else {
392
			$myurl = $addresswithport;
393
		}
394
		// eval url so that above $myurl item can be processed if need be.
395
		$url = str_replace('$myurl', $myurl, $urltmp);
396

    
397
		$tab_array[$tab_level][] = array(
398
						$tab['text'],
399
						$active,
400
						$url
401
					);
402
    	}
403

    
404
	ksort($tab_array);
405
	foreach($tab_array as $tabid => $tab) {
406
		echo '<tr><td>';
407
		display_top_tabs($tab, $no_drop_down, $tabid);
408
		echo '</td></tr>';
409
	}
410
}
411

    
412
?>
413
<tr><td><div id="mainarea"><table id="t" class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
414
<?php
415
	$cols = 0;
416
	$savevalue = gettext("Save");
417
	if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
418
	/* If a package's XML has <advanced_options/> configured, then setup 
419
	 * the table rows for the fields that have <advancedfield/> set.
420
	 * These fields will be placed below other fields in a seprate area titled 'Advanced Features'.
421
	 * These advanced fields are not normally configured and generally left to default to 'default settings'.
422
	 */
423

    
424
	if ($pkg['advanced_options'] == "enabled") {
425
		$adv_filed_count = 0;
426
		$advanced = "<td>&nbsp;</td>";
427
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br/></td></tr>\n";
428
		}		
429
	foreach ($pkg['fields']['field'] as $pkga) {
430
		if ($pkga['type'] == "sorting") 
431
			continue;
432

    
433
		if ($pkga['type'] == "listtopic") {
434
			$input = "<tr id='td_{$pkga['fieldname']}'><td>&nbsp;</td></tr>";
435
			$input .= "<tr id='tr_{$pkga['fieldname']}'><td colspan=\"2\" class=\"listtopic\">{$pkga['name']}<br></td></tr>\n";
436
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
437
				$advanced .= $input;
438
				$adv_filed_count++;
439
				}
440
			else
441
				echo $input;
442
			continue;
443
		}
444

    
445
		if($pkga['combinefields']=="begin"){
446
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
447
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
448
				$advanced .= $input;
449
			else
450
			  	echo $input;
451
			}
452

    
453
		$size = "";
454
		if (isset($pkga['dontdisplayname'])){
455
			$input="";
456
			if(!isset($pkga['combinefields']))
457
				$input .= "<tr valign='top' id='tr_{$pkga['fieldname']}'>";
458
			if(isset($pkga['usecolspan2']))
459
				$colspan="colspan='2'";
460
			else
461
				$input .= "<td width='22%' class='vncell{$req}'>&nbsp;</td>";
462
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
463
				$advanced .= $input;
464
				$adv_filed_count++;
465
				}
466
			else
467
				echo $input;
468
			}
469
		else if (!isset($pkga['placeonbottom'])){
470
			unset($req);
471
			if (isset($pkga['required']))
472
				$req = 'req';
473
			$input= "<td valign='top' width=\"22%\" class=\"vncell{$req}\">";
474
			$input .= fixup_string($pkga['fielddescr']);
475
			$input .= "</td>";
476
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
477
				$advanced .= $input;
478
				$adv_filed_count++;
479
				}
480
			else 
481
				echo $input;
482
		}
483
		if($pkga['combinefields']=="begin"){
484
			$input="<td class=\"vncell\"><table>";
485
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
486
				$advanced .= $input;
487
			else
488
			  	echo $input;
489
			}
490

    
491
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
492
		if (!isset($pkga['placeonbottom'])){
493
			$input="<td valign='top' {$colspan} {$class}>";
494
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)){
495
				$advanced .= $input;
496
				$adv_filed_count++;
497
				}
498
			else
499
				echo $input;
500
		}
501

    
502
		// if user is editing a record, load in the data.
503
		$fieldname = $pkga['fieldname'];
504
		if ($get_from_post) {
505
			$value = $_POST[$fieldname];
506
			if (is_array($value)) $value = implode(',', $value);
507
		} else {
508
			if (isset($id) && $a_pkg[$id])
509
				$value = $a_pkg[$id][$fieldname];
510
			else
511
				$value = $pkga['default_value'];
512
		}
513
		switch($pkga['type']){
514
			case "input":
515
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
516
				$input = "<input {$size} id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formfld unknown' value='" . htmlspecialchars($value) ."'>\n";
517
				$input .= "<br>" . fixup_string($pkga['description']) . "\n";
518
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
519
					$js_array[] = $pkga['fieldname'];
520
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
521
					}
522
				else
523
					echo $input;
524
				break;
525

    
526
			case "password":
527
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
528
				$input = "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . htmlspecialchars($value) . "'>\n";
529
				$input .= "<br>" . fixup_string($pkga['description']) . "\n";
530
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
531
					$js_array[] = $pkga['fieldname'];
532
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
533
					}
534
				else
535
					echo $input;
536
				break;
537

    
538
			case "info":
539
				$input = fixup_string($pkga['description']) . "\n";
540
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
541
					$js_array[] = $pkga['fieldname'];
542
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
543
					}
544
				else
545
					echo $input;
546
				break;
547

    
548
			case "select":
549
				$fieldname = $pkga['fieldname'];
550
				if (isset($pkga['multiple'])) {
551
					$multiple = 'multiple="multiple"';
552
					$items = explode(',', $value);
553
					$fieldname .= "[]";
554
				} else {
555
					$multiple = '';
556
					$items = array($value);
557
				}
558
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
559
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
560
				$input = "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n";
561
				foreach ($pkga['options']['option'] as $opt) {
562
					$selected = (in_array($opt['value'], $items) ? 'selected="selected"' : '');
563
					$input .= "\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" {$selected}>{$opt['name']}</option>\n";
564
					}
565
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
566
                if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
567
					$js_array[] = $pkga['fieldname'];
568
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
569
					$advanced .= "</div>\n";
570
					}
571
				else
572
					echo $input;
573
				break;
574

    
575
			case "select_source":
576
				$fieldname = $pkga['fieldname'];
577
				if (isset($pkga['multiple'])) {
578
					$multiple = 'multiple="multiple"';
579
					$items = explode(',', $value);
580
					$fieldname .= "[]";
581
				} else {
582
					$multiple = '';
583
					$items = array($value);
584
				}
585
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
586
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
587
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} id=\"{$fieldname}\" name=\"{$fieldname}\">\n";
588

    
589
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
590
					$js_array[] = $pkga['fieldname'];
591
					$advanced .= display_advanced_field($pkga['fieldname']) .$input;
592
					$advanced .= "</div>\n";
593
				} else {
594
					echo $input;
595
				}
596
				$source_url = $pkga['source'];
597
				eval("\$pkg_source_txt = &$source_url;");
598
				$input="";
599
				#check if show disable option is present on xml
600
				if(isset($pkga['show_disable_value'])){
601
					array_push($pkg_source_txt, array(($pkga['source_name']? $pkga['source_name'] : $pkga['name'])=> $pkga['show_disable_value'],
602
													  ($pkga['source_value']? $pkga['source_value'] : $pkga['value'])=> $pkga['show_disable_value']));
603
					}
604
				foreach ($pkg_source_txt as $opt) {
605
					$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
606
					$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
607
					$selected = (in_array($source_value, $items)? 'selected="selected"' : '' );
608
					$input  .= "\t<option name=\"{$source_name}\" value=\"{$source_value}\" $selected>{$source_name}</option>\n";
609
					}
610
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
611
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
612
					$advanced .= $input;
613
				else
614
					echo $input;
615
				break;
616

    
617
			case "vpn_selection" :
618
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
619
				foreach ($config['ipsec']['phase1'] as $vpn) {
620
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
621
					}
622
				$input .= "</select>\n";
623
				$input .= "<br>" . fixup_string($pkga['description']) . "\n";
624

    
625
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
626
					$js_array[] = $pkga['fieldname'];
627
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
628
					$advanced .= "</div>\n";
629
					}
630
				else
631
					echo $input;
632
				break;
633

    
634
			case "checkbox":
635
				$checkboxchecked =($value == "on" ? " CHECKED" : "");
636
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
637
					$onclick = ' onclick="javascript:enablechange();"';
638
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick}>\n";
639
				$input .= "<br>" . fixup_string($pkga['description']) . "\n";
640

    
641
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
642
					$js_array[] = $pkga['fieldname'];
643
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
644
					$advanced .= "</div>\n";
645
					}
646
				else
647
					echo $input;
648
				break;
649

    
650
			case "textarea":
651
				if($pkga['rows'])
652
					$rows = " rows='{$pkga['rows']}' ";
653
				if($pkga['cols'])
654
					$cols = " cols='{$pkga['cols']}' ";
655
				if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value))
656
					$value = base64_decode($value);
657
				$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');		  
658
				$input = "<textarea {$rows} {$cols} name='{$pkga['fieldname']}'{$wrap}>{$value}</textarea>\n";
659
				$input .= "<br>" . fixup_string($pkga['description']) . "\n";
660
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
661
					$js_array[] = $pkga['fieldname'];
662
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
663
					$advanced .= "</div>\n";
664
					}
665
				else
666
					echo $input;
667
				break;
668
			case "interfaces_selection":
669
				$ips=array();
670
				$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
671
				if (is_array($config['interfaces']))
672
					foreach ($config['interfaces'] as $iface_key=>$iface_value){
673
						if (isset($iface_value['enable']) && ! preg_match("/$interface_regex/",$iface_key)){
674
							$iface_description=(isset($pkga['showips']) ? strtoupper($iface_key)." address" : strtoupper($iface_key));
675
							$ips[]=array('ip'=> $iface_key, 'description'=> $iface_description);
676
							}
677
					}
678
				if (is_array($config['virtualip']) && isset($pkga['showvirtualips']))
679
					foreach ($config['virtualip']['vip'] as $vip){
680
						if (! preg_match("/$interface_regex/",$vip['interface']))
681
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
682
						  switch ($vip['mode']){
683
							case "ipalias":
684
							case "carp":
685
									$ips[]=array(   'ip'=> $vip['subnet'],'description' => "{$vip['subnet']} $vip_description");
686
								break;
687
							case "proxyarp":
688
								if ($vip['type']=="network"){
689
									$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
690
									$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
691
									$len = $end - $start;
692
									for ($i = 0; $i <= $len; $i++)
693
										$ips[]= array('ip'=>long2ip32($start+$i),'description'=> long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
694
									}
695
								else{
696
									$ips[]= array('ip'=>$vip['subnet'],'description'=> "{$vip['subnet']} $vip_description");
697
									}
698
								break;
699
							}
700
					}
701
				sort($ips);
702
				if (isset($pkga['showlistenall']))
703
					array_unshift($ips,array('ip'=> 'All', 'description'=> 'Listen on All interfaces/ip addresses '));
704
				if (! preg_match("/$interface_regex/","loopback")){
705
					$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
706
					array_push($ips,array('ip'=> 'lo0', 'description'=> $iface_description));
707
					}
708

    
709
				#show interfaces array on gui
710
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
711
				$multiple = '';
712
				$fieldname = $pkga['fieldname'];
713
				if (isset($pkga['multiple'])) {
714
					$fieldname .= '[]';
715
					$multiple = 'multiple';
716
					}
717
				$input = "<select id='{$pkga['fieldname']}' name=\"{$fieldname}\" {$size} {$multiple}>\n";
718
				if(is_array($value))
719
					$values = $value;
720
				else
721
					$values  =  explode(',',  $value);
722
				foreach($ips as $iface){
723
					$selected = (in_array($iface['ip'], $values) ? 'selected' : '');
724
					$input .= "<option value=\"{$iface['ip']}\" {$selected}>{$iface['description']}</option>\n";
725
					}
726
				$input .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
727
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
728
					$advanced .= $input;
729
				else
730
					echo $input;
731
				break;
732

    
733
			case "radio":
734
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}'>";
735
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
736
					$advanced .= $input;
737
				else
738
					echo $input;
739
					break;
740

    
741
			case "button":
742
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}'>\n";
743
				if(isset($pkga['placeonbottom']))
744
					$pkg_buttons .= $input;
745
				else
746
					echo $input ."\n<br>" . fixup_string($pkga['description']) . "\n";;
747
				break;
748

    
749
			case "rowhelper":
750
				#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
751
				$rowhelpername="row";
752
				?>
753
				<script type="text/javascript" language='javascript'>
754
				<!--
755
				<?php
756
					$rowcounter = 0;
757
					$fieldcounter = 0;
758
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
759
						echo "rowname[{$fieldcounter}] = \"{$rowhelper['fieldname']}\";\n";
760
						echo "rowtype[{$fieldcounter}] = \"{$rowhelper['type']}\";\n";
761
						echo "rowsize[{$fieldcounter}] = \"{$rowhelper['size']}\";\n";
762
						$fieldcounter++;
763
					}
764
				?>
765
				-->
766
				</script>
767
				<table name="maintable" id="maintable">
768
				<tr id='<?="tr_{$pkga['fieldname']}";?>'>
769
				<?php
770
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
771
					  echo "<td ".domTT_title($rowhelper['description'])."><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
772
					}
773

    
774
					$rowcounter = 0;
775
					$trc = 0;
776

    
777
					//Use assigned $a_pkg or create an empty array to enter loop
778
					if(isset($a_pkg[$id][$rowhelpername]))
779
						$saved_rows=$a_pkg[$id][$rowhelpername];
780
					else
781
						$saved_rows[]=array();
782

    
783
					foreach($saved_rows as $row) {
784
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
785
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
786
							unset($value);
787
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
788
							$fieldname = $rowhelper['fieldname'];
789
							// if user is editing a record, load in the data.
790
							if (isset($id) && $a_pkg[$id]) {
791
								$value = $row[$fieldname];
792
							}
793
							$options = "";
794
							$type = $rowhelper['type'];
795
							$description = $rowhelper['description'];
796
							$fieldname = $rowhelper['fieldname'];
797
							if($type == "option")
798
								$options = &$rowhelper['options']['option'];
799
							if($rowhelper['size']) 
800
								$size = $rowhelper['size'];
801
							else if ($pkga['size'])
802
								$size = $pkga['size'];
803
							else
804
								$size = "8";
805
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
806

    
807
							$text = "";
808
							$trc++;
809
							}
810
						$rowcounter++;
811
						echo "<td>";
812
						#echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" /></a>";
813
						echo "<a class='delete' href=\"#\"><img border='0' src='./themes/{$g['theme']}/images/icons/icon_x.gif' /></a>";
814
						echo "</td>\n";
815
						echo "</tr>\n";
816
						}
817
				?>
818
				<tbody></tbody>
819
				</table>
820
	
821
				<!-- <br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?#= $g['theme']; ?>/images/icons/icon_plus.gif"></a>-->
822
				<br><a class="add" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
823
				<br><?php if($pkga['description'] != "") echo $pkga['description']; ?>
824
				<script language="JavaScript">
825
				<!--
826
				field_counter_js = <?= $fieldcounter ?>;
827
				rows = <?= $rowcounter ?>;
828
				totalrows = <?php echo $rowcounter; ?>;
829
				loaded = <?php echo $rowcounter; ?>;
830
				//typesel_change();
831
				//-->
832
				</script>
833
		
834
				<?php
835
				break;
836
		    }
837
		#check typehint value
838
	   	if($pkga['typehint'])
839
	   		echo " " . $pkga['typehint'];
840
	   	#check combinefields options
841
     	if (isset($pkga['combinefields'])){
842
     		$input="</td>";
843
			if ($pkga['combinefields']=="end")
844
           		$input.="</table></td></tr>";
845
      		}
846
     	else{
847
			$input= "</td></tr>";
848
			if($pkga['usecolspan2'])
849
				$input.= "</tr><br>";
850
	     	}
851
   	 	if(isset($pkga['advancedfield']) && isset($adv_filed_count))
852
			$advanced .= "{$input}\n";
853
		else
854
			echo "{$input}\n";
855
		#increment counter
856
		$i++;
857
  		}
858

    
859
  	#print advanced settings if any after reading all fields
860
	if (isset($advanced) && $adv_filed_count > 0)
861
		echo $advanced;
862
  
863
	?>
864
  <tr>
865
	<td>&nbsp;</td>
866
  </tr>
867
  <tr>
868
    <td width="22%" valign="top">&nbsp;</td>
869
    <td width="78%">
870
    <div id=buttons>
871
		<?php
872
		if($pkg['note'] != ""){
873
			echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
874
			}
875
		//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
876
		echo "<input name='id' type='hidden' value='" . htmlspecialchars($id) . "'>";
877
		echo "<input name='Submit' type='submit' class='formbtn' value='" . htmlspecialchars($savevalue) . "'>\n{$pkg_buttons}\n";
878
		if (!$only_edit){
879
			echo "<input class='formbtn' type='button' value='".gettext("Cancel")."' onclick='history.back()'>";
880
			}
881
		?>
882
	</div>
883
    </td>
884
  </tr>
885

    
886
</table>
887
</div></td></tr>
888
</table>
889
</form>
890

    
891
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
892

    
893
<?php
894
	/* JavaScript to handle the advanced fields. */
895
	if ($pkg['advanced_options'] == "enabled") {
896
		echo "<script type=\"text/javascript\">\n";
897
		foreach($js_array as $advfieldname) {
898
			echo "function show_" . $advfieldname . "() {\n";
899
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
900
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
901
			echo "}\n\n";
902
		}
903
		echo "</script>\n";
904
	}
905
?>
906

    
907
<?php include("fend.inc"); ?>
908
</body>
909
</html>
910

    
911
<?php
912
/*
913
 * ROW Helpers function
914
 */
915
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
916
	global $text, $config;
917
	echo "<td>\n";
918
	switch($type){
919
		case "input":
920
			echo "<input size='{$size}' name='{$fieldname}{$trc}' id='{$fieldname}{$trc}' class='formfld unknown' value='" . htmlspecialchars($value) . "'>\n";
921
			break;
922
		case "checkbox":
923
			echo "<input size='{$size}' type='checkbox' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' value='ON' ".($value?"CHECKED":"").">\n";
924
			break;
925
		case "password":
926
			echo "<input size='{$size}' type='password' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' class='formfld pwd' value='" . htmlspecialchars($value) . "'>\n";
927
			break;
928
		case "textarea":
929
			echo "<textarea rows='2' cols='12' id='{$fieldname}{$trc}' class='formfld unknown' name='{$fieldname}{$trc}'>{$value}</textarea>\n";
930
		case "select":
931
			echo "<select style='height:22px;'  id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$title}>\n";
932
			foreach($rowhelper['options']['option'] as $rowopt) {
933
				$text .= "<option value='{$rowopt['value']}'>{$rowopt['name']}</option>";
934
				echo "<option value='{$rowopt['value']}'".($rowopt['value'] == $value?" SELECTED":"").">{$rowopt['name']}</option>\n";
935
				}
936
			echo "</select>\n";
937
			break;
938
		case "interfaces_selection":
939
			$size = ($size ? "size=\"{$size}\"" : '');
940
			$multiple = '';
941
			if (isset($rowhelper['multiple'])) {
942
				$fieldname .= '[]';
943
				$multiple = 'multiple';
944
			}
945
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
946
			$ifaces = get_configured_interface_with_descr();
947
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
948
			if (!empty($additional_ifaces))
949
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
950
			if(is_array($value))
951
				$values = $value;
952
			else
953
				$values  =  explode(',',  $value);
954
			$ifaces["lo0"] = "loopback";
955
			echo "<option><name></name><value></value></option>/n";
956
			foreach($ifaces as $ifname => $iface) {
957
				$text .="<option value=\\\"$ifname\\\">$iface</option>";
958
				echo "<option value=\"{$ifname}\" ".(in_array($ifname, $values) ? 'selected' : '').">{$iface}</option>\n";
959
				}
960
			echo "</select>\n";
961
			break;
962
		case "select_source":
963
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}'>\n";
964
			if(isset($rowhelper['show_disable_value']))
965
				echo "<option value='{$rowhelper['show_disable_value']}'>{$rowhelper['show_disable_value']}</option>\n";
966
			$source_url = $rowhelper['source'];
967
			eval("\$pkg_source_txt = &$source_url;");
968
			foreach($pkg_source_txt as $opt) {
969
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
970
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
971
				$text .= "<option value='{$source_value}'>{$source_name}</option>";
972
				echo "<option value='{$source_value}'".($source_value == $value?" SELECTED":"").">{$source_name}</option>\n";
973
				}
974
			echo "</select>\n";
975
			break;		
976
		}
977
}
978

    
979
function fixup_string($string) {
980
	global $config;
981
	// fixup #1: $myurl -> http[s]://ip_address:port/
982
	$https = "";
983
	$port = $config['system']['webguiport'];
984
	if($port <> "443" and $port <> "80")
985
		$urlport = ":" . $port;
986
	else
987
		$urlport = "";
988

    
989
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
990
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
991
	$newstring = str_replace("\$myurl", $myurl, $string);
992
	$string = $newstring;
993
	// fixup #2: $wanip
994
	$curwanip = get_interface_ip();
995
	$newstring = str_replace("\$wanip", $curwanip, $string);
996
	$string = $newstring;
997
	// fixup #3: $lanip
998
	$lancfg = $config['interfaces']['lan'];
999
	$lanip = $lancfg['ipaddr'];
1000
	$newstring = str_replace("\$lanip", $lanip, $string);
1001
	$string = $newstring;
1002
	// fixup #4: fix'r'up here.
1003
	return $newstring;
1004
}
1005

    
1006
/*
1007
 *  Parse templates if they are defined
1008
 */
1009
function parse_package_templates() {
1010
	global $pkg, $config;
1011
	$rows = 0;
1012
	if($pkg['templates']['template'] <> "")
1013
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
1014
			$filename = $pkg_template_row['filename'];
1015
			$template_text = $pkg_template_row['templatecontents'];
1016
			$firstfield = "";
1017
			/* calculate total row helpers count and */
1018
			/* change fields defined as fieldname_fieldvalue to their value */
1019
			foreach ($pkg['fields']['field'] as $fields) {
1020
				switch($fields['type']){
1021
					case "rowhelper":
1022
					// save rowhelper items.
1023
					$row_helper_total_rows = 0;
1024
					$row_helper_data = "";
1025
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
1026
						foreach($_POST as $key => $value){
1027
							if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches)){
1028
								$row_helper_total_rows++;
1029
								$row_helper_data .= $value;
1030
								$sep = "";
1031
								ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
1032
								foreach ($sep as $se) $seperator = $se;
1033
								if($seperator <> "") {
1034
							    	$row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
1035
							    	$template_text = ereg_replace("\[{$seperator}\]", "", $template_text);
1036
									}
1037
								$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
1038
								}
1039
							}
1040
					break;
1041
				default:
1042
					$fieldname  = $fields['fieldname'];
1043
					$fieldvalue = $_POST[$fieldname];
1044
					$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
1045
				}
1046
			}
1047
		/* replace $domain_total_rows with total rows */
1048
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
1049

    
1050
		/* replace cr's */
1051
		$template_text = str_replace("\\n", "\n", $template_text);
1052

    
1053
		/* write out new template file */
1054
		$fout = fopen($filename,"w");
1055
		fwrite($fout, $template_text);
1056
		fclose($fout);
1057
	    }
1058
}
1059

    
1060
/* Return html div fields */
1061
function display_advanced_field($fieldname) {
1062
	$div = "<div id='showadv_{$fieldname}'>\n";
1063
	$div .= "<input type='button' onClick='show_{$fieldname}()' value='" . gettext("Advanced") . "'></input> - " . gettext("Show advanced option") ."</div>\n";
1064
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1065
	return $div;
1066
}
1067

    
1068
?>
(129-129/251)