Project

General

Profile

Download (39.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
    Copyright (C) 2004-2012 Scott Ullrich <sullrich@gmail.com>
7
    All rights reserved.
8

    
9
    Redistribution and use in source and binary forms, with or without
10
    modification, are permitted provided that the following conditions are met:
11

    
12
    1. Redistributions of source code must retain the above copyright notice,
13
       this list of conditions and the following disclaimer.
14

    
15
    2. Redistributions in binary form must reproduce the above copyright
16
       notice, this list of conditions and the following disclaimer in the
17
       documentation and/or other materials provided with the distribution.
18

    
19
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
    POSSIBILITY OF SUCH DAMAGE.
29
*/
30
/*
31
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
32
	pfSense_MODULE:	pkgs
33
*/
34

    
35
##|+PRIV
36
##|*IDENT=page-package-edit
37
##|*NAME=Package: Edit page
38
##|*DESCR=Allow access to the 'Package: Edit' page.
39
##|*MATCH=pkg_edit.php*
40
##|-PRIV
41

    
42
ini_set('max_execution_time', '0');
43

    
44
require("guiconfig.inc");
45
require_once("functions.inc");
46
require_once("filter.inc");
47
require_once("shaper.inc");
48
require_once("pkg-utils.inc");
49

    
50
/* dummy stubs needed by some code that was MFC'd */
51
function pfSenseHeader($location) { header("Location: " . $location); }
52

    
53
function gentitle_pkg($pgname) {
54
	global $pfSense_config;
55
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
56
}
57

    
58
function domTT_title($title_msg){
59
	if (!empty($title_msg)){
60
		$title_msg=preg_replace("/\s+/"," ",$title_msg);
61
        $title_msg=preg_replace("/'/","\'",$title_msg);
62
		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');\"";
63
	}
64
}
65

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

    
69
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
70

    
71
if ($xml == "" || $xml_fullpath === false ||
72
    substr($xml_fullpath, 0, strlen('/usr/local/pkg/')) != '/usr/local/pkg/') {
73
            print_info_box_np(gettext("ERROR: No valid package defined."));
74
            die;
75
} else {
76
            $pkg = parse_xml_config_pkg($xml_fullpath, "packagegui");
77
}
78

    
79
if($pkg['include_file'] <> "") {
80
	require_once($pkg['include_file']);
81
}
82

    
83
if (!isset($pkg['adddeleteeditpagefields']))
84
	$only_edit = true;
85
else
86
	$only_edit = false;
87

    
88
$package_name = $pkg['menu'][0]['name'];
89
$section      = $pkg['menu'][0]['section'];
90
$config_path  = $pkg['configpath'];
91
$name         = $pkg['name'];
92
$title        = $pkg['title'];
93
$pgtitle      = $title;
94

    
95
$id = $_GET['id'];
96
if (isset($_POST['id']))
97
	$id = htmlspecialchars($_POST['id']);
98

    
99
// Not posting?  Then user is editing a record. There must be a valid id
100
// when editing a record.
101
if(!$id && !$_POST)
102
	$id = "0";
103

    
104
if(!is_numeric($id)) {
105
	header("Location: /");
106
	exit;
107
}
108

    
109
if($pkg['custom_php_global_functions'] <> "")
110
        eval($pkg['custom_php_global_functions']);
111

    
112
// grab the installedpackages->package_name section.
113
if($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
114
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
115

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

    
121
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
122

    
123
if($_GET['savemsg'] <> "")
124
	$savemsg = htmlspecialchars($_GET['savemsg']);
125

    
126
if($pkg['custom_php_command_before_form'] <> "")
127
	eval($pkg['custom_php_command_before_form']);
128

    
129
if ($_POST) {
130
	$firstfield = "";
131
	$rows = 0;
132

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

    
146
	if ($pkg['custom_php_validation_command'])
147
		eval($pkg['custom_php_validation_command']);
148

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

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

    
178
	// donotsave is enabled.  lets simply exit.
179
	if(empty($pkg['donotsave'])) {
180

    
181
		// store values in xml configration file.
182
		if (!$input_errors) {
183
			$pkgarr = array();
184
			foreach ($pkg['fields']['field'] as $fields) {
185
				switch($fields['type']){
186
					case "rowhelper":
187
						// save rowhelper items.
188
						#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
189
						$rowhelpername="row";
190
						foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield)
191
							foreach($_POST as $key => $value){
192
								if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches))
193
									$pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']]=$value;
194
							}
195
						break;
196
					default:
197
						$fieldname  = $fields['fieldname'];
198
						if ($fieldname == "interface_array") {
199
							$fieldvalue = $_POST[$fieldname];
200
						} elseif (is_array($_POST[$fieldname])) {
201
							$fieldvalue = implode(',', $_POST[$fieldname]);
202
						} else {
203
							$fieldvalue = trim($_POST[$fieldname]);
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}&amp;id=0");
245
			} elseif(!$pkg['preoutput']) {
246
			    pfSenseHeader("pkg.php?xml=" . $xml);
247
			}
248
			exit;
249
		} else {
250
			$get_from_post = true;
251
		}
252
	} elseif (!$input_errors) {
253
		exit;
254
	}
255
}
256

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

    
264
$pgtitle = $title;
265

    
266
if ($pkg['custom_php_after_head_command']) {
267
	$closehead = false;
268
	include("head.inc");
269
	eval($pkg['custom_php_after_head_command']);
270
	echo "</head>\n";
271
}
272
else
273
	include("head.inc");
274

    
275
?>
276

    
277
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
278

    
279
<?php include("fbegin.inc"); ?>
280

    
281
<script type="text/javascript" src="/javascript/autosuggest.js?rev=1"></script>
282
<script type="text/javascript" src="/javascript/suggestions.js"></script>
283

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

    
326
	function enablechange() {
327
	<?php
328
	foreach ($pkg['fields']['field'] as $field) {
329
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
330
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
331

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

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

    
348
			echo "\t}\n\telse {\n";
349

    
350
			if (isset($field['enablefields'])) {
351
				foreach (explode(',', $field['enablefields']) as $enablefield) {
352
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').length > 0) {\n";
353
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$enablefield}\"]').prop('disabled',false);\n";
354
					echo "\t\t}\n";
355
				}
356
			}
357

    
358
			if (isset($field['checkenablefields'])) {
359
				foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
360
					echo "\t\tif (jQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').length > 0) {\n";
361
					echo "\t\t\tjQuery('form[name=\"iform\"] input[name=\"{$checkenablefield}\"]').prop('checked',false);\n";
362
					echo "\t\t}\n";
363
				}
364
			}
365

    
366
			echo "\t}\n";
367
		}
368
	}
369
	?>
370
}
371
//]]>
372
</script>
373
<?php } ?>
374
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
375
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
376
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
377
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
378
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
379

    
380
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
381
<form name="iform" action="pkg_edit.php" method="post">
382
<input type="hidden" name="xml" value="<?= htmlspecialchars($xml) ?>" />
383
<?php if ($savemsg) print_info_box($savemsg); ?>
384
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="package edit">
385
<?php
386
if ($pkg['tabs'] <> "") {
387
	$tab_array = array();
388
	foreach($pkg['tabs']['tab'] as $tab) {
389
		if($tab['tab_level'])
390
			$tab_level = $tab['tab_level'];
391
		else
392
			$tab_level = 1;
393
		if(isset($tab['active'])) {
394
			$active = true;
395
		} else {
396
			$active = false;
397
		}
398
		if(isset($tab['no_drop_down']))
399
			$no_drop_down = true;
400
		$urltmp = "";
401
		if($tab['url'] <> "") $urltmp = $tab['url'];
402
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
403

    
404
 		$addresswithport = getenv("HTTP_HOST");
405
		$colonpos = strpos($addresswithport, ":");
406
		if ($colonpos !== False) {
407
			//my url is actually just the IP address of the pfsense box
408
			$myurl = substr($addresswithport, 0, $colonpos);
409
		} else {
410
			$myurl = $addresswithport;
411
		}
412
		// eval url so that above $myurl item can be processed if need be.
413
		$url = str_replace('$myurl', $myurl, $urltmp);
414

    
415
		$tab_array[$tab_level][] = array(
416
						$tab['text'],
417
						$active,
418
						$url
419
					);
420
    	}
421

    
422
	ksort($tab_array);
423
	foreach($tab_array as $tabid => $tab) {
424
		echo '<tr><td>';
425
		display_top_tabs($tab, $no_drop_down, $tabid);
426
		echo '</td></tr>';
427
	}
428
}
429

    
430
?>
431
<tr><td><div id="mainarea"><table id="t" class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
432
<?php
433
	$cols = 0;
434
	$savevalue = gettext("Save");
435
	if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
436
	/* If a package's XML has <advanced_options/> configured, then setup 
437
	 * the table rows for the fields that have <advancedfield/> set.
438
	 * These fields will be placed below other fields in a seprate area titled 'Advanced Features'.
439
	 * These advanced fields are not normally configured and generally left to default to 'default settings'.
440
	 */
441

    
442
	if ($pkg['advanced_options'] == "enabled") {
443
		$adv_filed_count = 0;
444
		$advanced = "<td>&nbsp;</td>";
445
		$advanced .= "<tr><td colspan=\"2\" class=\"listtopic\">". gettext("Advanced features") . "<br /></td></tr>\n";
446
		}		
447
	foreach ($pkg['fields']['field'] as $pkga) {
448
		if ($pkga['type'] == "sorting") 
449
			continue;
450

    
451
		if ($pkga['type'] == "listtopic") {
452
			$input = "<tr id='td_{$pkga['fieldname']}'><td>&nbsp;</td></tr>";
453
			$input .= "<tr id='tr_{$pkga['fieldname']}'><td colspan=\"2\" class=\"listtopic\">{$pkga['name']}<br /></td></tr>\n";
454
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
455
				$advanced .= $input;
456
				$adv_filed_count++;
457
				}
458
			else
459
				echo $input;
460
			continue;
461
		}
462

    
463
		if($pkga['combinefields']=="begin"){
464
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
465
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
466
				$advanced .= $input;
467
			else
468
			  	echo $input;
469
			}
470

    
471
		$size = "";
472
		if (isset($pkga['dontdisplayname'])){
473
			$input="";
474
			if(!isset($pkga['combinefields']))
475
				$input .= "<tr valign='top' id='tr_{$pkga['fieldname']}'>";
476
			if(isset($pkga['usecolspan2']))
477
				$colspan="colspan='2'";
478
			else
479
				$input .= "<td width='22%' class='vncell{$req}'>&nbsp;</td>";
480
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
481
				$advanced .= $input;
482
				$adv_filed_count++;
483
				}
484
			else
485
				echo $input;
486
			}
487
		else if (!isset($pkga['placeonbottom'])){
488
			unset($req);
489
			if (isset($pkga['required']))
490
				$req = 'req';
491
			$input= "<tr><td valign='top' width=\"22%\" class=\"vncell{$req}\">";
492
			$input .= fixup_string($pkga['fielddescr']);
493
			$input .= "</td>";
494
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
495
				$advanced .= $input;
496
				$adv_filed_count++;
497
				}
498
			else 
499
				echo $input;
500
		}
501
		if($pkga['combinefields']=="begin"){
502
			$input="<td class=\"vncell\"><table summary=\"advanced\">";
503
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
504
				$advanced .= $input;
505
			else
506
			  	echo $input;
507
			}
508

    
509
		$class=(isset($pkga['combinefields']) ? '' : 'class="vtable"');
510
		if (!isset($pkga['placeonbottom'])){
511
			$input="<td valign='top' {$colspan} {$class}>";
512
			if(isset($pkga['advancedfield']) && isset($adv_filed_count)){
513
				$advanced .= $input;
514
				$adv_filed_count++;
515
				}
516
			else
517
				echo $input;
518
		}
519

    
520
		// if user is editing a record, load in the data.
521
		$fieldname = $pkga['fieldname'];
522
		if ($get_from_post) {
523
			$value = $_POST[$fieldname];
524
			if (is_array($value)) $value = implode(',', $value);
525
		} else {
526
			if (isset($id) && $a_pkg[$id])
527
				$value = $a_pkg[$id][$fieldname];
528
			else
529
				$value = $pkga['default_value'];
530
		}
531
		switch($pkga['type']){
532
			case "input":
533
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
534
				$input = "<input {$size} id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formfld unknown' 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 "password":
545
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
546
				$input = "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' name='" . $pkga['fieldname'] . "' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
547
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
548
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
549
					$js_array[] = $pkga['fieldname'];
550
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
551
					}
552
				else
553
					echo $input;
554
				break;
555

    
556
			case "info":
557
				$input = fixup_string($pkga['description']) . "\n";
558
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
559
					$js_array[] = $pkga['fieldname'];
560
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
561
					}
562
				else
563
					echo $input;
564
				break;
565

    
566
			case "select":
567
				$fieldname = $pkga['fieldname'];
568
				if (isset($pkga['multiple'])) {
569
					$multiple = 'multiple="multiple"';
570
					$items = explode(',', $value);
571
					$fieldname .= "[]";
572
				} else {
573
					$multiple = '';
574
					$items = array($value);
575
				}
576
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
577
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
578
				$input = "<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange name=\"$fieldname\">\n";
579
				foreach ($pkga['options']['option'] as $opt) {
580
					$selected = (in_array($opt['value'], $items) ? 'selected="selected"' : '');
581
					$input .= "\t<option value=\"{$opt['value']}\" {$selected}>{$opt['name']}</option>\n";
582
					}
583
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
584
                if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
585
					$js_array[] = $pkga['fieldname'];
586
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
587
					$advanced .= "</div>\n";
588
					}
589
				else
590
					echo $input;
591
				break;
592

    
593
			case "select_source":
594
				$fieldname = $pkga['fieldname'];
595
				if (isset($pkga['multiple'])) {
596
					$multiple = 'multiple="multiple"';
597
					$items = explode(',', $value);
598
					$fieldname .= "[]";
599
				} else {
600
					$multiple = '';
601
					$items = array($value);
602
				}
603
				$size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
604
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
605
				$input = "<select id='{$pkga['fieldname']}' {$multiple} {$size} {$onchange} name=\"{$fieldname}\">\n";
606

    
607
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
608
					$js_array[] = $pkga['fieldname'];
609
					$advanced .= display_advanced_field($pkga['fieldname']) .$input;
610
					$advanced .= "</div>\n";
611
				} else {
612
					echo $input;
613
				}
614
				$source_url = $pkga['source'];
615
				eval("\$pkg_source_txt = &$source_url;");
616
				$input="";
617
				#check if show disable option is present on xml
618
				if(isset($pkga['show_disable_value'])){
619
					array_push($pkg_source_txt, array(($pkga['source_name']? $pkga['source_name'] : $pkga['name'])=> $pkga['show_disable_value'],
620
													  ($pkga['source_value']? $pkga['source_value'] : $pkga['value'])=> $pkga['show_disable_value']));
621
					}
622
				foreach ($pkg_source_txt as $opt) {
623
					$source_name =($pkga['source_name']? $opt[$pkga['source_name']] : $opt[$pkga['name']]);
624
					$source_value =($pkga['source_value'] ? $opt[$pkga['source_value']] : $opt[$pkga['value']]);
625
					$selected = (in_array($source_value, $items)? 'selected="selected"' : '' );
626
					$input  .= "\t<option value=\"{$source_value}\" $selected>{$source_name}</option>\n";
627
					}
628
				$input .= "</select>\n<br />\n" . fixup_string($pkga['description']) . "\n";
629
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
630
					$advanced .= $input;
631
				else
632
					echo $input;
633
				break;
634

    
635
			case "vpn_selection" :
636
				$input = "<select id='{$pkga['fieldname']}' name='{$vpn['name']}'>\n";
637
				foreach ($config['ipsec']['phase1'] as $vpn) {
638
					$input .= "\t<option value=\"{$vpn['descr']}\">{$vpn['descr']}</option>\n";
639
					}
640
				$input .= "</select>\n";
641
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
642

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

    
652
			case "checkbox":
653
				$checkboxchecked =($value == "on" ? " checked=\"checked\"" : "");
654
				$onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
655
				if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
656
					$onclick = ' onclick="javascript:enablechange();"';
657
				$input = "<input id='{$pkga['fieldname']}' type='checkbox' name='{$pkga['fieldname']}' {$checkboxchecked} {$onclick} {$onchange} />\n";
658
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
659

    
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

    
669
			case "textarea":
670
				if($pkga['rows'])
671
					$rows = " rows='{$pkga['rows']}' ";
672
				if($pkga['cols'])
673
					$cols = " cols='{$pkga['cols']}' ";
674
				if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value))
675
					$value = base64_decode($value);
676
				$wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : '');		  
677
				$input = "<textarea {$rows} {$cols} name='{$pkga['fieldname']}'{$wrap}>{$value}</textarea>\n";
678
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
679
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
680
					$js_array[] = $pkga['fieldname'];
681
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
682
					$advanced .= "</div>\n";
683
					}
684
				else
685
					echo $input;
686
				break;
687

    
688
			case "aliases":
689
				// Use xml tag <typealiases> to filter type aliases
690
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
691
				$fieldname = $pkga['fieldname'];
692
				$a_aliases = &$config['aliases']['alias'];
693
				$addrisfirst = 0;
694
				$aliasesaddr = "";
695
				$value = "value='{$value}'";
696

    
697
				if(isset($a_aliases)) {
698
					if(!empty($pkga['typealiases'])) {
699
						foreach($a_aliases as $alias)
700
							if($alias['type'] == $pkga['typealiases']) {
701
								if($addrisfirst == 1) $aliasesaddr .= ",";
702
								$aliasesaddr .= "'" . $alias['name'] . "'";
703
								$addrisfirst = 1;
704
							}
705
					} else {
706
						foreach($a_aliases as $alias) {
707
							if($addrisfirst == 1) $aliasesaddr .= ",";
708
							$aliasesaddr .= "'" . $alias['name'] . "'";
709
							$addrisfirst = 1;
710
						}
711
					}
712
				}
713

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

    
717
				$script = "<script type='text/javascript'>\n";
718
				$script .= "//<![CDATA[\n";
719
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
720
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
721
				$script .= "//]]>\n";
722
				$script .= "</script>";
723

    
724
				echo $input;
725
				echo $script;
726
                                break;
727

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

    
771
				#show interfaces array on gui
772
				$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
773
				$multiple = '';
774
				$fieldname = $pkga['fieldname'];
775
				if (isset($pkga['multiple'])) {
776
					$fieldname .= '[]';
777
					$multiple = 'multiple="multiple"';
778
					}
779
				$input = "<select id='{$pkga['fieldname']}' name=\"{$fieldname}\" {$size} {$multiple}>\n";
780
				if(is_array($value))
781
					$values = $value;
782
				else
783
					$values  =  explode(',',  $value);
784
				foreach($ips as $iface){
785
					$selected = (in_array($iface['ip'], $values) ? 'selected="selected"' : '');
786
					$input .= "<option value=\"{$iface['ip']}\" {$selected}>{$iface['description']}</option>\n";
787
					}
788
				$input .= "</select>\n<br />" . fixup_string($pkga['description']) . "\n";
789
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
790
					$advanced .= $input;
791
				else
792
					echo $input;
793
				break;
794

    
795
			case "radio":
796
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
797
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
798
					$advanced .= $input;
799
				else
800
					echo $input;
801
					break;
802

    
803
			case "button":
804
				$input = "<input type='submit' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formbtn' value='{$pkga['fieldname']}' />\n";
805
				if(isset($pkga['placeonbottom']))
806
					$pkg_buttons .= $input;
807
				else
808
					echo $input ."\n<br />" . fixup_string($pkga['description']) . "\n";
809
				break;
810

    
811
			case "rowhelper":
812
				#$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row");
813
				$rowhelpername="row";
814
				?>
815
				<script type="text/javascript">
816
				//<![CDATA[
817
				<?php
818
					$rowcounter = 0;
819
					$fieldcounter = 0;
820
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
821
						echo "rowname[{$fieldcounter}] = \"{$rowhelper['fieldname']}\";\n";
822
						echo "rowtype[{$fieldcounter}] = \"{$rowhelper['type']}\";\n";
823
						echo "rowsize[{$fieldcounter}] = \"{$rowhelper['size']}\";\n";
824
						$fieldcounter++;
825
					}
826
				?>
827
				//]]>
828
				</script>
829
				<table id="maintable" summary="main table">
830
				<tr id='<?="tr_{$pkga['fieldname']}";?>'>
831
				<?php
832
					foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
833
					  echo "<td ".domTT_title($rowhelper['description'])."><b>" . fixup_string($rowhelper['fielddescr']) . "</b></td>\n";
834
					}
835

    
836
					$rowcounter = 0;
837
					$trc = 0;
838

    
839
					//Use assigned $a_pkg or create an empty array to enter loop
840
					if(isset($a_pkg[$id][$rowhelpername]))
841
						$saved_rows=$a_pkg[$id][$rowhelpername];
842
					else
843
						$saved_rows[]=array();
844

    
845
					foreach($saved_rows as $row) {
846
						echo "</tr>\n<tr class=\"sortable\" id=\"id_{$rowcounter}\">\n";
847
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
848
							unset($value);
849
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
850
							$fieldname = $rowhelper['fieldname'];
851
							// if user is editing a record, load in the data.
852
							if (isset($id) && $a_pkg[$id]) {
853
								$value = $row[$fieldname];
854
							}
855
							$options = "";
856
							$type = $rowhelper['type'];
857
							$description = $rowhelper['description'];
858
							$fieldname = $rowhelper['fieldname'];
859
							if($type == "option")
860
								$options = &$rowhelper['options']['option'];
861
							if($rowhelper['size']) 
862
								$size = $rowhelper['size'];
863
							else if ($pkga['size'])
864
								$size = $pkga['size'];
865
							else
866
								$size = "8";
867
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
868

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

    
921
  	#print advanced settings if any after reading all fields
922
	if (isset($advanced) && $adv_filed_count > 0)
923
		echo $advanced;
924
  
925
	?>
926
  <tr>
927
	<td>&nbsp;</td>
928
  </tr>
929
  <tr>
930
    <td width="22%" valign="top">&nbsp;</td>
931
    <td width="78%">
932
    <div id="buttons">
933
		<?php
934
		if($pkg['note'] != ""){
935
			echo "<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>";
936
			}
937
		//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
938
		echo "<input name='id' type='hidden' value=\"" . htmlspecialchars($id) . "\" />";
939
		echo "<input name='Submit' type='submit' class='formbtn' value=\"" . htmlspecialchars($savevalue) . "\" />\n{$pkg_buttons}\n";
940
		if (!$only_edit){
941
			echo "<input class=\"formbtn\" type=\"button\" value=\"".gettext("Cancel")."\" onclick=\"window.location.href='" . $_SERVER['HTTP_REFERER'] . "'\" />";
942
			}
943
		?>
944
	</div>
945
    </td>
946
  </tr>
947

    
948
</table>
949
</div></td></tr>
950
</table>
951
</form>
952

    
953
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
954

    
955
<?php
956
	/* JavaScript to handle the advanced fields. */
957
	if ($pkg['advanced_options'] == "enabled") {
958
		echo "<script type=\"text/javascript\">\n";
959
		echo "//<![CDATA[\n";
960
		foreach($js_array as $advfieldname) {
961
			echo "function show_" . $advfieldname . "() {\n";
962
			echo "\tjQuery('#showadv_{$advfieldname}').empty();\n";
963
			echo "\tjQuery('#show_{$advfieldname}').css('display', 'block');\n";
964
			echo "}\n\n";
965
		}
966
		echo "//]]>\n";
967
		echo "</script>\n";
968
	}
969
?>
970

    
971
<?php include("fend.inc"); ?>
972
</body>
973
</html>
974

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

    
1044
function fixup_string($string) {
1045
	global $config;
1046
	// fixup #1: $myurl -> http[s]://ip_address:port/
1047
	$https = "";
1048
	$port = $config['system']['webguiport'];
1049
	if($port <> "443" and $port <> "80")
1050
		$urlport = ":" . $port;
1051
	else
1052
		$urlport = "";
1053

    
1054
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
1055
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
1056
	$newstring = str_replace("\$myurl", $myurl, $string);
1057
	$string = $newstring;
1058
	// fixup #2: $wanip
1059
	$curwanip = get_interface_ip();
1060
	$newstring = str_replace("\$wanip", $curwanip, $string);
1061
	$string = $newstring;
1062
	// fixup #3: $lanip
1063
	$lancfg = $config['interfaces']['lan'];
1064
	$lanip = $lancfg['ipaddr'];
1065
	$newstring = str_replace("\$lanip", $lanip, $string);
1066
	$string = $newstring;
1067
	// fixup #4: fix'r'up here.
1068
	return $newstring;
1069
}
1070

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

    
1115
		/* replace cr's */
1116
		$template_text = str_replace("\\n", "\n", $template_text);
1117

    
1118
		/* write out new template file */
1119
		$fout = fopen($filename,"w");
1120
		fwrite($fout, $template_text);
1121
		fclose($fout);
1122
	    }
1123
}
1124

    
1125
/* Return html div fields */
1126
function display_advanced_field($fieldname) {
1127
	$div = "<div id='showadv_{$fieldname}'>\n";
1128
	$div .= "<input type='button' onclick='show_{$fieldname}()' value='" . gettext("Advanced") . "' /> - " . gettext("Show advanced option") ."</div>\n";
1129
	$div .= "<div id='show_{$fieldname}' style='display:none'>\n";
1130
	return $div;
1131
}
1132

    
1133
?>
(131-131/256)