Project

General

Profile

Download (39.3 KB) Statistics
| Branch: | Tag: | Revision:

univnautes / usr / local / www / pkg_edit.php @ a1b66bec

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
$xml_fullpath = realpath('/usr/local/pkg/' . $xml);
69

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
211
			if (isset($id) && $a_pkg[$id])
212
				$a_pkg[$id] = $pkgarr;
213
			else
214
				$a_pkg[] = $pkgarr;
215

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

    
222
			if (isset($pkg['filter_rules_needed']))
223
				filter_configure();
224

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

    
230
			parse_package_templates();
231

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

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

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

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

    
263
$pgtitle = $title;
264

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

    
274
?>
275

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

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

    
280
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
281
<script type="text/javascript" src="/javascript/suggestions.js"></script>
282

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

    
325
	function enablechange() {
326
	<?php
327
	foreach ($pkg['fields']['field'] as $field) {
328
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
329
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\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',true);\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',true);\n";
343
					echo "\t\t}\n";
344
				}
345
			}
346

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
519
		// if user is editing a record, load in the data.
520
		$fieldname = $pkga['fieldname'];
521
		if ($get_from_post) {
522
			$value = $_POST[$fieldname];
523
			if (is_array($value)) $value = implode(',', $value);
524
		} else {
525
			if (isset($id) && $a_pkg[$id])
526
				$value = $a_pkg[$id][$fieldname];
527
			else
528
				$value = $pkga['default_value'];
529
		}
530
		switch($pkga['type']){
531
			case "input":
532
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
533
				$input = "<input {$size} id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' class='formfld unknown' value=\"" . htmlspecialchars($value) ."\" />\n";
534
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
535
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
536
					$js_array[] = $pkga['fieldname'];
537
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
538
					}
539
				else
540
					echo $input;
541
				break;
542

    
543
			case "password":
544
				$size = ($pkga['size'] ? " size='{$pkga['size']}' " : "");
545
				$input = "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' name='" . $pkga['fieldname'] . "' class='formfld pwd' value=\"" . htmlspecialchars($value) . "\" />\n";
546
				$input .= "<br />" . fixup_string($pkga['description']) . "\n";
547
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
548
					$js_array[] = $pkga['fieldname'];
549
					$advanced .= display_advanced_field($pkga['fieldname']).$input ."</div>\n";
550
					}
551
				else
552
					echo $input;
553
				break;
554

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

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

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

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

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

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

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

    
659
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
660
					$js_array[] = $pkga['fieldname'];
661
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
662
					$advanced .= "</div>\n";
663
					}
664
				else
665
					echo $input;
666
				break;
667

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1132
?>
(131-131/256)