Project

General

Profile

Download (38.9 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($config['installedpackages'] && !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 ($config['installedpackages'] && (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

    
270
<?php include("fbegin.inc"); ?>
271

    
272
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
273
<script type="text/javascript" src="/javascript/suggestions.js"></script>
274

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

    
314
	function enablechange() {
315
	<?php
316
	foreach ($pkg['fields']['field'] as $field) {
317
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
318
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
319

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

    
328
			if (isset($field['checkenablefields'])) {
329
				foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
330
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
331
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',true);\n";
332
					echo "\t\t}\n";
333
				}
334
			}
335

    
336
			echo "\t}\n\telse {\n";
337

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

    
346
			if (isset($field['checkenablefields'])) {
347
				foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
348
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
349
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
350
					echo "\t\t}\n";
351
				}
352
			}
353

    
354
			echo "\t}\n";
355
		}
356
	}
357
	?>
358
}
359
//]]>
360
</script>
361
<?php } ?>
362
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
363
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
364
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
365
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
366
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
367

    
368
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
369
<form name="iform" action="pkg_edit.php" method="post">
370
<input type="hidden" name="xml" value="<?= htmlspecialchars($xml) ?>" />
371
<?php if ($savemsg) print_info_box($savemsg); ?>
372
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="package edit">
373
<?php
374
if ($pkg['tabs'] <> "") {
375
	$tab_array = array();
376
	foreach($pkg['tabs']['tab'] as $tab) {
377
		if($tab['tab_level'])
378
			$tab_level = $tab['tab_level'];
379
		else
380
			$tab_level = 1;
381
		if(isset($tab['active'])) {
382
			$active = true;
383
		} else {
384
			$active = false;
385
		}
386
		if(isset($tab['no_drop_down']))
387
			$no_drop_down = true;
388
		$urltmp = "";
389
		if($tab['url'] <> "") $urltmp = $tab['url'];
390
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
391

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

    
403
		$tab_array[$tab_level][] = array(
404
						$tab['text'],
405
						$active,
406
						$url
407
					);
408
    	}
409

    
410
	ksort($tab_array);
411
	foreach($tab_array as $tabid => $tab) {
412
		echo '<tr><td>';
413
		display_top_tabs($tab, $no_drop_down, $tabid);
414
		echo '</td></tr>';
415
	}
416
}
417

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

    
430
	if ($pkg['advanced_options'] == "enabled") {
431
		$adv_filed_count = 0;
432
		$advanced = "<td>&nbsp;</td>";
433
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br/></td></tr>\n";
434
		}		
435
	foreach ($pkg['fields']['field'] as $pkga) {
436
		if ($pkga['type'] == "sorting") 
437
			continue;
438

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

    
451
		if($pkga['combinefields']=="begin"){
452
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
453
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
454
				$advanced .= $input;
455
			else
456
			  	echo $input;
457
			}
458

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

    
497
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
498
		if (!isset($pkga['placeonbottom'])){
499
			$input="<td valign='top' {$colspan} {$class}>";
500
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)){
501
				$advanced .= $input;
502
				$adv_filed_count++;
503
				}
504
			else
505
				echo $input;
506
		}
507

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

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

    
544
			case "info":
545
				$input = fixup_string($pkga['description']) . "\n";
546
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
547
					$js_array[] = $pkga['fieldname'];
548
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
549
					}
550
				else
551
					echo $input;
552
				break;
553

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

    
581
			case "select_source":
582
				$fieldname = $pkga['fieldname'];
583
				if (isset($pkga['multiple'])) {
584
					$multiple = 'multiple="multiple"';
585
					$items = explode(',', $value);
586
					$fieldname .= "[]";
587
				} else {
588
					$multiple = '';
589
					$items = array($value);
590
				}
591
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
592
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
593
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} name=\"{$fieldname}\">\n";
594

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

    
623
			case "vpn_selection" :
624
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
625
				foreach ($config['ipsec']['phase1'] as $vpn) {
626
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
627
					}
628
				$input .= "</select>\n";
629
				$input .= "<br/>" . fixup_string($pkga['description']) . "\n";
630

    
631
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
632
					$js_array[] = $pkga['fieldname'];
633
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
634
					$advanced .= "</div>\n";
635
					}
636
				else
637
					echo $input;
638
				break;
639

    
640
			case "checkbox":
641
				$checkboxchecked =($value == "on" ? " checked=\"checked\"" : "");
642
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
643
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
644
					$onclick = ' onclick="javascript:enablechange();"';
645
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick} {$onchange} />\n";
646
				$input .= "<br/>" . fixup_string($pkga['description']) . "\n";
647

    
648
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
649
					$js_array[] = $pkga['fieldname'];
650
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
651
					$advanced .= "</div>\n";
652
					}
653
				else
654
					echo $input;
655
				break;
656

    
657
			case "textarea":
658
				if($pkga['rows'])
659
					$rows = " rows='{$pkga['rows']}' ";
660
				if($pkga['cols'])
661
					$cols = " cols='{$pkga['cols']}' ";
662
				if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value))
663
					$value = base64_decode($value);
664
				$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');		  
665
				$input = "<textarea {$rows} {$cols} name='{$pkga['fieldname']}'{$wrap}>{$value}</textarea>\n";
666
				$input .= "<br/>" . fixup_string($pkga['description']) . "\n";
667
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
668
					$js_array[] = $pkga['fieldname'];
669
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
670
					$advanced .= "</div>\n";
671
					}
672
				else
673
					echo $input;
674
				break;
675

    
676
			case "aliases":
677
				// Use xml tag <typealiases> to filter type aliases
678
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
679
				$fieldname = $pkga['fieldname'];
680
				$a_aliases = &$config['aliases']['alias'];
681
				$addrisfirst = 0;
682
				$aliasesaddr = "";
683
				$value = "value='{$value}'";
684

    
685
				if(isset($a_aliases)) {
686
					if(!empty($pkga['typealiases'])) {
687
						foreach($a_aliases as $alias)
688
							if($alias['type'] == $pkga['typealiases']) {
689
								if($addrisfirst == 1) $aliasesaddr .= ",";
690
								$aliasesaddr .= "'" . $alias['name'] . "'";
691
								$addrisfirst = 1;
692
							}
693
					} else {
694
						foreach($a_aliases as $alias) {
695
							if($addrisfirst == 1) $aliasesaddr .= ",";
696
							$aliasesaddr .= "'" . $alias['name'] . "'";
697
							$addrisfirst = 1;
698
						}
699
					}
700
				}
701

    
702
				$input = "<input name='{$fieldname}' type='text' class='formfldalias' id='{$fieldname}' {$size} {$value} />\n<br />";
703
				$input .= fixup_string($pkga['description']) . "\n";
704

    
705
				$script = "<script type='text/javascript'>\n";
706
				$script .= "//<![CDATA[\n";
707
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
708
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
709
				$script .= "//]]>\n";
710
				$script .= "</script>";
711

    
712
				echo $input;
713
				echo $script;
714
                                break;
715

    
716
			case "interfaces_selection":
717
				$ips=array();
718
				$interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide");
719
				if (is_array($config['interfaces']))
720
					foreach ($config['interfaces'] as $iface_key=>$iface_value){
721
						if (isset($iface_value['enable']) && ! preg_match("/$interface_regex/",$iface_key)){
722
							$iface_description=(isset($pkga['showips']) ? strtoupper($iface_key)." address" : strtoupper($iface_key));
723
							$ips[]=array('ip'=> $iface_key, 'description'=> $iface_description);
724
							}
725
					}
726
				if (is_array($config['virtualip']) && isset($pkga['showvirtualips']))
727
					foreach ($config['virtualip']['vip'] as $vip){
728
						if (! preg_match("/$interface_regex/",$vip['interface']))
729
						$vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " ");
730
						  switch ($vip['mode']){
731
							case "ipalias":
732
							case "carp":
733
									$ips[]=array(   'ip'=> $vip['subnet'],'description' => "{$vip['subnet']} $vip_description");
734
								break;
735
							case "proxyarp":
736
								if ($vip['type']=="network"){
737
									$start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits']));
738
									$end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits']));
739
									$len = $end - $start;
740
									for ($i = 0; $i <= $len; $i++)
741
										$ips[]= array('ip'=>long2ip32($start+$i),'description'=> long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}");
742
									}
743
								else{
744
									$ips[]= array('ip'=>$vip['subnet'],'description'=> "{$vip['subnet']} $vip_description");
745
									}
746
								break;
747
							}
748
					}
749
				sort($ips);
750
				if (isset($pkga['showlistenall']))
751
					array_unshift($ips,array('ip'=> 'All', 'description'=> 'Listen on All interfaces/ip addresses '));
752
				if (! preg_match("/$interface_regex/","loopback")){
753
					$iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback");
754
					array_push($ips,array('ip'=> 'lo0', 'description'=> $iface_description));
755
					}
756

    
757
				#show interfaces array on gui
758
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
759
				$multiple = '';
760
				$fieldname = $pkga['fieldname'];
761
				if (isset($pkga['multiple'])) {
762
					$fieldname .= '[]';
763
					$multiple = 'multiple="multiple"';
764
					}
765
				$input = "<select id='{$pkga['fieldname']}' name=\"{$fieldname}\" {$size} {$multiple}>\n";
766
				if(is_array($value))
767
					$values = $value;
768
				else
769
					$values  =  explode(',',  $value);
770
				foreach($ips as $iface){
771
					$selected = (in_array($iface['ip'], $values) ? 'selected="selected"' : '');
772
					$input .= "<option value=\"{$iface['ip']}\" {$selected}>{$iface['description']}</option>\n";
773
					}
774
				$input .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
775
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
776
					$advanced .= $input;
777
				else
778
					echo $input;
779
				break;
780

    
781
			case "radio":
782
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
783
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
784
					$advanced .= $input;
785
				else
786
					echo $input;
787
					break;
788

    
789
			case "button":
790
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}' />\n";
791
				if(isset($pkga['placeonbottom']))
792
					$pkg_buttons .= $input;
793
				else
794
					echo $input ."\n<br/>" . fixup_string($pkga['description']) . "\n";;
795
				break;
796

    
797
			case "rowhelper":
798
				#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
799
				$rowhelpername="row";
800
				?>
801
				<script type="text/javascript">
802
				//<![CDATA[
803
				<?php
804
					$rowcounter = 0;
805
					$fieldcounter = 0;
806
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
807
						echo "rowname[{$fieldcounter}] = \"{$rowhelper['fieldname']}\";\n";
808
						echo "rowtype[{$fieldcounter}] = \"{$rowhelper['type']}\";\n";
809
						echo "rowsize[{$fieldcounter}] = \"{$rowhelper['size']}\";\n";
810
						$fieldcounter++;
811
					}
812
				?>
813
				//]]>
814
				</script>
815
				<table id="maintable" summary="main table">
816
				<tr id='<?="tr_{$pkga['fieldname']}";?>'>
817
				<?php
818
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
819
					  echo "<td ".domTT_title($rowhelper['description'])."><b>" . fixup_string($rowhelper['fielddescr']) . "</b></td>\n";
820
					}
821

    
822
					$rowcounter = 0;
823
					$trc = 0;
824

    
825
					//Use assigned $a_pkg or create an empty array to enter loop
826
					if(isset($a_pkg[$id][$rowhelpername]))
827
						$saved_rows=$a_pkg[$id][$rowhelpername];
828
					else
829
						$saved_rows[]=array();
830

    
831
					foreach($saved_rows as $row) {
832
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
833
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
834
							unset($value);
835
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
836
							$fieldname = $rowhelper['fieldname'];
837
							// if user is editing a record, load in the data.
838
							if (isset($id) && $a_pkg[$id]) {
839
								$value = $row[$fieldname];
840
							}
841
							$options = "";
842
							$type = $rowhelper['type'];
843
							$description = $rowhelper['description'];
844
							$fieldname = $rowhelper['fieldname'];
845
							if($type == "option")
846
								$options = &$rowhelper['options']['option'];
847
							if($rowhelper['size']) 
848
								$size = $rowhelper['size'];
849
							else if ($pkga['size'])
850
								$size = $pkga['size'];
851
							else
852
								$size = "8";
853
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
854

    
855
							$text = "";
856
							$trc++;
857
							}
858
						$rowcounter++;
859
						echo "<td>";
860
						#echo "<a onclick=\"removeRow(this); return false;\" href=\"#\"><img border=\"0\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" alt=\"remove\" /></a>";
861
						echo "<a class='delete' href=\"#\"><img border='0' src='./themes/{$g['theme']}/images/icons/icon_x.gif' alt='delete' /></a>";
862
						echo "</td>\n";
863
						}
864
				?>
865
				</tr>
866
				<tbody></tbody>
867
				</table>
868
	
869
				<!-- <br/><a onclick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?#= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>-->
870
				<br/><a class="add" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" alt="add" /></a>
871
				<br/><?php if($pkga['description'] != "") echo $pkga['description']; ?>
872
				<script type="text/javascript">
873
				//<![CDATA[
874
				field_counter_js = <?= $fieldcounter ?>;
875
				rows = <?= $rowcounter ?>;
876
				totalrows = <?php echo $rowcounter; ?>;
877
				loaded = <?php echo $rowcounter; ?>;
878
				//typesel_change();
879
				//]]>
880
				</script>
881
		
882
				<?php
883
				break;
884
		    }
885
		#check typehint value
886
	   	if($pkga['typehint'])
887
	   		echo " " . $pkga['typehint'];
888
	   	#check combinefields options
889
     	if (isset($pkga['combinefields'])){
890
     		$input="</td>";
891
			if ($pkga['combinefields']=="end")
892
           		$input.="</table></td></tr>";
893
      		}
894
     	else{
895
			$input= "</td></tr>";
896
			if($pkga['usecolspan2'])
897
				$input.= "</tr><br/>";
898
	     	}
899
   	 	if(isset($pkga['advancedfield']) && isset($adv_filed_count))
900
			$advanced .= "{$input}\n";
901
		else
902
			echo "{$input}\n";
903
		#increment counter
904
		$i++;
905
  		}
906

    
907
  	#print advanced settings if any after reading all fields
908
	if (isset($advanced) && $adv_filed_count > 0)
909
		echo $advanced;
910
  
911
	?>
912
  <tr>
913
	<td>&nbsp;</td>
914
  </tr>
915
  <tr>
916
    <td width="22%" valign="top">&nbsp;</td>
917
    <td width="78%">
918
    <div id="buttons">
919
		<?php
920
		if($pkg['note'] != ""){
921
			echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
922
			}
923
		//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
924
		echo "<input name='id' type='hidden' value='" . htmlspecialchars($id) . "' />";
925
		echo "<input name='Submit' type='submit' class='formbtn' value='" . htmlspecialchars($savevalue) . "' />\n{$pkg_buttons}\n";
926
		if (!$only_edit){
927
			echo "<input class='formbtn' type='button' value='".gettext("Cancel")."' onclick='history.back()' />";
928
			}
929
		?>
930
	</div>
931
    </td>
932
  </tr>
933

    
934
</table>
935
</div></td></tr>
936
</table>
937
</form>
938

    
939
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
940

    
941
<?php
942
	/* JavaScript to handle the advanced fields. */
943
	if ($pkg['advanced_options'] == "enabled") {
944
		echo "<script type=\"text/javascript\">\n";
945
		echo "//<![CDATA[\n";
946
		foreach($js_array as $advfieldname) {
947
			echo "function show_" . $advfieldname . "() {\n";
948
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
949
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
950
			echo "}\n\n";
951
		}
952
		echo "//]]>\n";
953
		echo "</script>\n";
954
	}
955
?>
956

    
957
<?php include("fend.inc"); ?>
958
</body>
959
</html>
960

    
961
<?php
962
/*
963
 * ROW Helpers function
964
 */
965
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
966
	global $text, $config;
967
	echo "<td>\n";
968
	switch($type){
969
		case "input":
970
			echo "<input size='{$size}' name='{$fieldname}{$trc}' id='{$fieldname}{$trc}' class='formfld unknown' value='" . htmlspecialchars($value) . "' />\n";
971
			break;
972
		case "checkbox":
973
			echo "<input size='{$size}' type='checkbox' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' value='ON' ".($value?"CHECKED":"")." />\n";
974
			break;
975
		case "password":
976
			echo "<input size='{$size}' type='password' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' class='formfld pwd' value='" . htmlspecialchars($value) . "' />\n";
977
			break;
978
		case "textarea":
979
			echo "<textarea rows='2' cols='12' id='{$fieldname}{$trc}' class='formfld unknown' name='{$fieldname}{$trc}'>{$value}</textarea>\n";
980
		case "select":
981
			echo "<select style='height:22px;'  id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$title}>\n";
982
			foreach($rowhelper['options']['option'] as $rowopt) {
983
				$text .= "<option value='{$rowopt['value']}'>{$rowopt['name']}</option>";
984
				echo "<option value='{$rowopt['value']}'".($rowopt['value'] == $value?" selected=\"selected\"":"").">{$rowopt['name']}</option>\n";
985
				}
986
			echo "</select>\n";
987
			break;
988
		case "interfaces_selection":
989
			$size = ($size ? "size=\"{$size}\"" : '');
990
			$multiple = '';
991
			if (isset($rowhelper['multiple'])) {
992
				$fieldname .= '[]';
993
				$multiple = 'multiple="multiple';
994
			}
995
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}' {$size} {$multiple}>\n";
996
			$ifaces = get_configured_interface_with_descr();
997
			$additional_ifaces = $rowhelper['add_to_interfaces_selection'];
998
			if (!empty($additional_ifaces))
999
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
1000
			if(is_array($value))
1001
				$values = $value;
1002
			else
1003
				$values  =  explode(',',  $value);
1004
			$ifaces["lo0"] = "loopback";
1005
			echo "<option><name></name><value></value></option>/n";
1006
			foreach($ifaces as $ifname => $iface) {
1007
				$text .="<option value=\\\"$ifname\\\">$iface</option>";
1008
				echo "<option value=\"{$ifname}\" ".(in_array($ifname, $values) ? 'selected="selected"' : '').">{$iface}</option>\n";
1009
				}
1010
			echo "</select>\n";
1011
			break;
1012
		case "select_source":
1013
			echo "<select style='height:22px;' id='{$fieldname}{$trc}' name='{$fieldname}{$trc}'>\n";
1014
			if(isset($rowhelper['show_disable_value']))
1015
				echo "<option value='{$rowhelper['show_disable_value']}'>{$rowhelper['show_disable_value']}</option>\n";
1016
			$source_url = $rowhelper['source'];
1017
			eval("\$pkg_source_txt = &$source_url;");
1018
			foreach($pkg_source_txt as $opt) {
1019
				$source_name = ($rowhelper['source_name'] ? $opt[$rowhelper['source_name']] : $opt[$rowhelper['name']]);
1020
				$source_value = ($rowhelper['source_value'] ? $opt[$rowhelper['source_value']] : $opt[$rowhelper['value']]);
1021
				$text .= "<option value='{$source_value}'>{$source_name}</option>";
1022
				echo "<option value='{$source_value}'".($source_value == $value?" selected=\"selected\"":"").">{$source_name}</option>\n";
1023
				}
1024
			echo "</select>\n";
1025
			break;		
1026
		}
1027
	echo "</td>\n";
1028
}
1029

    
1030
function fixup_string($string) {
1031
	global $config;
1032
	// fixup #1: $myurl -> http[s]://ip_address:port/
1033
	$https = "";
1034
	$port = $config['system']['webguiport'];
1035
	if($port <> "443" and $port <> "80")
1036
		$urlport = ":" . $port;
1037
	else
1038
		$urlport = "";
1039

    
1040
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
1041
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
1042
	$newstring = str_replace("\$myurl", $myurl, $string);
1043
	$string = $newstring;
1044
	// fixup #2: $wanip
1045
	$curwanip = get_interface_ip();
1046
	$newstring = str_replace("\$wanip", $curwanip, $string);
1047
	$string = $newstring;
1048
	// fixup #3: $lanip
1049
	$lancfg = $config['interfaces']['lan'];
1050
	$lanip = $lancfg['ipaddr'];
1051
	$newstring = str_replace("\$lanip", $lanip, $string);
1052
	$string = $newstring;
1053
	// fixup #4: fix'r'up here.
1054
	return $newstring;
1055
}
1056

    
1057
/*
1058
 *  Parse templates if they are defined
1059
 */
1060
function parse_package_templates() {
1061
	global $pkg, $config;
1062
	$rows = 0;
1063
	if($pkg['templates']['template'] <> "")
1064
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
1065
			$filename = $pkg_template_row['filename'];
1066
			$template_text = $pkg_template_row['templatecontents'];
1067
			$firstfield = "";
1068
			/* calculate total row helpers count and */
1069
			/* change fields defined as fieldname_fieldvalue to their value */
1070
			foreach ($pkg['fields']['field'] as $fields) {
1071
				switch($fields['type']){
1072
					case "rowhelper":
1073
					// save rowhelper items.
1074
					$row_helper_total_rows = 0;
1075
					$row_helper_data = "";
1076
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
1077
						foreach($_POST as $key => $value){
1078
							if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches)){
1079
								$row_helper_total_rows++;
1080
								$row_helper_data .= $value;
1081
								$sep = "";
1082
								ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
1083
								foreach ($sep as $se) $separator = $se;
1084
								if($separator <> "") {
1085
							    	$row_helper_data = ereg_replace("  ", $separator, $row_helper_data);
1086
							    	$template_text = ereg_replace("\[{$separator}\]", "", $template_text);
1087
									}
1088
								$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
1089
								}
1090
							}
1091
					break;
1092
				default:
1093
					$fieldname  = $fields['fieldname'];
1094
					$fieldvalue = $_POST[$fieldname];
1095
					$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
1096
				}
1097
			}
1098
		/* replace $domain_total_rows with total rows */
1099
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
1100

    
1101
		/* replace cr's */
1102
		$template_text = str_replace("\\n", "\n", $template_text);
1103

    
1104
		/* write out new template file */
1105
		$fout = fopen($filename,"w");
1106
		fwrite($fout, $template_text);
1107
		fclose($fout);
1108
	    }
1109
}
1110

    
1111
/* Return html div fields */
1112
function display_advanced_field($fieldname) {
1113
	$div = "<div id='showadv_{$fieldname}'>\n";
1114
	$div .= "<input type='button' onclick='show_{$fieldname}()' value='" . gettext("Advanced") . "' /> - " . gettext("Show advanced option") ."</div>\n";
1115
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1116
	return $div;
1117
}
1118

    
1119
?>
(129-129/246)