Projet

Général

Profil

Télécharger (39,2 ko) Statistiques
| Branche: | Tag: | Révision:

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

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}&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
include("head.inc");
265

    
266
if ($pkg['custom_php_after_head_command'])
267
	eval($pkg['custom_php_after_head_command']);
268

    
269
?>
270

    
271
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
272

    
273
<?php include("fbegin.inc"); ?>
274

    
275
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
276
<script type="text/javascript" src="/javascript/suggestions.js"></script>
277

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

    
320
	function enablechange() {
321
	<?php
322
	foreach ($pkg['fields']['field'] as $field) {
323
		if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
324
			echo "\tif (jQuery('form[name=\"iform\"] input[name=\"{$field['fieldname']}\"]').prop('checked') == false) {\n";
325

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

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

    
342
			echo "\t}\n\telse {\n";
343

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

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

    
360
			echo "\t}\n";
361
		}
362
	}
363
	?>
364
}
365
//]]>
366
</script>
367
<?php } ?>
368
<script type="text/javascript" src="javascript/domTT/domLib.js"></script>
369
<script type="text/javascript" src="javascript/domTT/domTT.js"></script>
370
<script type="text/javascript" src="javascript/domTT/behaviour.js"></script>
371
<script type="text/javascript" src="javascript/domTT/fadomatic.js"></script>
372
<script type="text/javascript" src="/javascript/row_helper_dynamic.js"></script>
373

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

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

    
409
		$tab_array[$tab_level][] = array(
410
						$tab['text'],
411
						$active,
412
						$url
413
					);
414
    	}
415

    
416
	ksort($tab_array);
417
	foreach($tab_array as $tabid => $tab) {
418
		echo '<tr><td>';
419
		display_top_tabs($tab, $no_drop_down, $tabid);
420
		echo '</td></tr>';
421
	}
422
}
423

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

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

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

    
457
		if($pkga['combinefields']=="begin"){
458
			$input="<tr valign='top' id='tr_{$pkga['fieldname']}'>";
459
			if(isset($pkga['advancedfield']) && isset($adv_filed_count))
460
				$advanced .= $input;
461
			else
462
			  	echo $input;
463
			}
464

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

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

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

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

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

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

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

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

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

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

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

    
654
				if(isset($pkga['advancedfield']) && isset($adv_filed_count)) {
655
					$js_array[] = $pkga['fieldname'];
656
					$advanced .= display_advanced_field($pkga['fieldname']).$input;
657
					$advanced .= "</div>\n";
658
					}
659
				else
660
					echo $input;
661
				break;
662

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

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

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

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

    
711
				$script = "<script type='text/javascript'>\n";
712
				$script .= "//<![CDATA[\n";
713
				$script .= "var aliasarray = new Array({$aliasesaddr})\n";
714
				$script .= "var oTextbox1 = new AutoSuggestControl(document.getElementById('{$fieldname}'), new StateSuggestions(aliasarray))\n";
715
				$script .= "//]]>\n";
716
				$script .= "</script>";
717

    
718
				echo $input;
719
				echo $script;
720
                                break;
721

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

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

    
789
			case "radio":
790
				$input = "<input type='radio' id='{$pkga['fieldname']}' name='{$pkga['fieldname']}' value='{$value}' />";
791
				if(isset($pkga['advancedfield']) && isset($adv_filed_count))
792
					$advanced .= $input;
793
				else
794
					echo $input;
795
					break;
796

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

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

    
830
					$rowcounter = 0;
831
					$trc = 0;
832

    
833
					//Use assigned $a_pkg or create an empty array to enter loop
834
					if(isset($a_pkg[$id][$rowhelpername]))
835
						$saved_rows=$a_pkg[$id][$rowhelpername];
836
					else
837
						$saved_rows[]=array();
838

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

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

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

    
942
</table>
943
</div></td></tr>
944
</table>
945
</form>
946

    
947
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
948

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

    
965
<?php include("fend.inc"); ?>
966
</body>
967
</html>
968

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

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

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

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

    
1109
		/* replace cr's */
1110
		$template_text = str_replace("\\n", "\n", $template_text);
1111

    
1112
		/* write out new template file */
1113
		$fout = fopen($filename,"w");
1114
		fwrite($fout, $template_text);
1115
		fclose($fout);
1116
	    }
1117
}
1118

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

    
1127
?>
(129-129/246)