Projet

Général

Profil

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

univnautes / usr / local / www / pkg_edit.php @ 69eb2e29

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 = basename($xml);
69

    
70
if ($xml == "") {
71
            print_info_box_np(gettext("ERROR: No package defined."));
72
            die;
73
} else if (!file_exists('/usr/local/pkg/' . $xml)) {
74
            print_info_box_np(gettext("ERROR: XML file not found"));
75
            die;
76
} else {
77
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
78
}
79

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
224
			if (isset($pkg['filter_rules_needed']))
225
				filter_configure();
226

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

    
232
			parse_package_templates();
233

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

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

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

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

    
265
$pgtitle = $title;
266

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

    
276
?>
277

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

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

    
282
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
283
<script type="text/javascript" src="/javascript/suggestions.js"></script>
284

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1134
?>
(130-130/255)