Project

General

Profile

Download (26.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2004 Scott Ullrich
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
$xml = htmlspecialchars($_GET['xml']);
58
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
59

    
60
if($xml == "") {
61
            print_info_box_np(gettext("ERROR: No package defined."));
62
            die;
63
} else {
64
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
65
}
66

    
67
if($pkg['include_file'] <> "") {
68
	require_once($pkg['include_file']);
69
}
70

    
71
if (!isset($pkg['adddeleteeditpagefields']))
72
	$only_edit = true;
73
else
74
	$only_edit = false;
75

    
76
$package_name = $pkg['menu'][0]['name'];
77
$section      = $pkg['menu'][0]['section'];
78
$config_path  = $pkg['configpath'];
79
$name         = $pkg['name'];
80
$title        = $pkg['title'];
81
$pgtitle      = $title;
82

    
83
$id = $_GET['id'];
84
if (isset($_POST['id']))
85
	$id = htmlspecialchars($_POST['id']);
86
	
87
// Not posting?  Then user is editing a record. There must be a valid id
88
// when editing a record.
89
if(!$id && !$_POST)
90
	$id = "0";
91
	
92
if($pkg['custom_php_global_functions'] <> "")
93
        eval($pkg['custom_php_global_functions']);
94

    
95
// grab the installedpackages->package_name section.
96
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
97
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
98

    
99
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
100

    
101
if($_GET['savemsg'] <> "")
102
	$savemsg = htmlspecialchars($_GET['savemsg']);
103

    
104
if($pkg['custom_php_command_before_form'] <> "")
105
	eval($pkg['custom_php_command_before_form']);
106

    
107
if ($_POST) {
108
	if($_POST['act'] == "del") {
109
		if($pkg['custom_delete_php_command']) {
110
		    if($pkg['custom_php_command_before_form'] <> "")
111
			    eval($pkg['custom_php_command_before_form']);
112
		    eval($pkg['custom_delete_php_command']);
113
		}
114
		write_config($pkg['delete_string']);
115
		// resync the configuration file code if defined.
116
		if($pkg['custom_php_resync_config_command'] <> "") {
117
			if($pkg['custom_php_command_before_form'] <> "")
118
				eval($pkg['custom_php_command_before_form']);
119
			eval($pkg['custom_php_resync_config_command']);
120
		}
121
	} else {
122
		if($pkg['custom_add_php_command']) {
123
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
124
			?>
125

    
126
<?php include("head.inc"); ?>
127
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
128
<?php include("fbegin.inc"); ?>
129
<?php
130
			}
131
			if($pkg['preoutput']) echo "<pre>";
132
			eval($pkg['custom_add_php_command']);
133
			if($pkg['preoutput']) echo "</pre>";
134
		}
135
	}
136

    
137
	// donotsave is enabled.  lets simply exit.
138
	if($pkg['donotsave'] <> "") exit;
139

    
140
	$firstfield = "";
141
	$rows = 0;
142

    
143
	$input_errors = array();
144
	$reqfields = array();
145
	$reqfieldsn = array();
146
	foreach ($pkg['fields']['field'] as $field) {
147
		if (($field['type'] == 'input') && isset($field['required'])) {
148
			if($field['fieldname'])
149
				$reqfields[] = $field['fieldname'];
150
			if($field['fielddescr'])	
151
				$reqfieldsn[] = $field['fielddescr'];
152
		}
153
	}
154
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
155

    
156
	if ($pkg['custom_php_validation_command'])
157
		eval($pkg['custom_php_validation_command']);
158

    
159
	// store values in xml configration file.
160
	if (!$input_errors) {
161
		$pkgarr = array();
162
		foreach ($pkg['fields']['field'] as $fields) {
163
			if($fields['type'] == "listtopic")
164
				continue;
165
			if($fields['type'] == "rowhelper") {
166
				// save rowhelper items.
167
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
168
				                         // XXX: this really is not helping embedded platforms.
169
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
170
						if($firstfield == "")  {
171
						  $firstfield = $rowhelperfield['fieldname'];
172
						} else {
173
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
174
						}
175
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
176
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
177
						eval($comd);
178
						if($value <> "") {
179
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
180
							//echo($comd . "<br>");
181
							eval($comd);
182
						}
183
					}
184
				}
185
			} else {
186
				$fieldname  = $fields['fieldname'];
187
				$fieldvalue = $_POST[$fieldname];
188
				if (is_array($fieldvalue))
189
					$fieldvalue = implode(',', $fieldvalue);
190
				else {
191
					$fieldvalue = trim($fieldvalue);
192
					if ($fields['encoding'] == 'base64')
193
						$fieldvalue = base64_encode($fieldvalue);
194
				}
195
				if($fieldname)
196
					$pkgarr[$fieldname] = $fieldvalue;
197
			}
198
		}
199

    
200
		if (isset($id) && $a_pkg[$id])
201
			$a_pkg[$id] = $pkgarr;
202
		else
203
			$a_pkg[] = $pkgarr;
204

    
205
		write_config($pkg['addedit_string']);
206
		// late running code
207
		if($pkg['custom_add_php_command_late'] <> "") {
208
		    eval($pkg['custom_add_php_command_late']);
209
		}
210

    
211
		if (isset($pkg['filter_rules_needed'])) 
212
			filter_configure();
213

    
214
		// resync the configuration file code if defined.
215
		if($pkg['custom_php_resync_config_command'] <> "") {
216
		    eval($pkg['custom_php_resync_config_command']);
217
		}
218

    
219
		parse_package_templates();
220

    
221
		/* if start_command is defined, restart w/ this */
222
		if($pkg['start_command'] <> "")
223
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
224

    
225
		/* if restart_command is defined, restart w/ this */
226
		if($pkg['restart_command'] <> "")
227
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
228

    
229
		if($pkg['aftersaveredirect'] <> "") {
230
		    pfSenseHeader($pkg['aftersaveredirect']);
231
		} elseif(!$pkg['adddeleteeditpagefields']) {
232
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
233
		} elseif(!$pkg['preoutput']) {
234
		    pfSenseHeader("pkg.php?xml=" . $xml);
235
		}
236
		exit;
237
	}
238
	else
239
		$get_from_post = true;
240
}
241

    
242
if($pkg['title'] <> "") {
243
	$edit = ($only_edit ? '' : ': Edit');
244
	$title = $pkg['title'] . $edit;
245
}
246
else
247
	$title = gettext("Package Editor");
248

    
249
$pgtitle = $title;
250
include("head.inc");
251

    
252
if ($pkg['custom_php_after_head_command'])
253
	eval($pkg['custom_php_after_head_command']);
254

    
255
?>
256

    
257
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
258
<?php if($pkg['fields']['field'] <> "") { ?>
259
<script language="JavaScript">
260
<!--
261
function enablechange() {
262
<?php
263
foreach ($pkg['fields']['field'] as $field) {
264
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
265
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
266

    
267
		if (isset($field['enablefields'])) {
268
			foreach (explode(',', $field['enablefields']) as $enablefield) {
269
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
270
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
271
				print("\t\t}else{\n");
272
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 1;\n");
273
				print("\t\t}\n");
274
			}
275
		}
276

    
277
		if (isset($field['checkenablefields'])) {
278
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
279
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
280
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
281
				print("\t\t}else{\n");
282
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n");
283
				print("\t\t}\n");
284
			}
285
		}
286

    
287
		print("\t}\n\telse {\n");
288

    
289
		if (isset($field['enablefields'])) {
290
			foreach (explode(',', $field['enablefields']) as $enablefield) {
291
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
292
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
293
				print("\t\t}else{\n");
294
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n");
295
				print("\t\t}\n");
296
			}
297
		}
298

    
299
		if (isset($field['checkenablefields'])) {
300
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
301
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
302
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
303
				print("\t\t}else{\n");
304
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n");
305
				print("\t\t}\n");
306
			}
307
		}
308

    
309
		print("\t}\n");
310
	}
311
}
312
?>
313
}
314
//-->
315
</script>
316
<?php } ?>
317
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
318
</script>
319

    
320
<?php include("fbegin.inc"); ?>
321
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
322
<form name="iform" action="pkg_edit.php" method="post">
323
<input type="hidden" name="xml" value="<?= $xml ?>">
324
<?php if ($savemsg) print_info_box($savemsg); ?>
325
<table width="100%" border="0" cellpadding="0" cellspacing="0">
326
<?php
327
if ($pkg['tabs'] <> "") {
328
    echo '<tr><td>';
329
    $tab_array = array();
330
    foreach($pkg['tabs']['tab'] as $tab) {
331
	if(isset($tab['active'])) {
332
		$active = true;
333
	} else {
334
		$active = false;
335
	}
336
	$urltmp = "";
337
	if($tab['url'] <> "") $urltmp = $tab['url'];
338
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
339

    
340
 	$addresswithport = getenv("HTTP_HOST");
341
	$colonpos = strpos($addresswithport, ":");
342
	if ($colonpos !== False){
343
		//my url is actually just the IP address of the pfsense box
344
		$myurl = substr($addresswithport, 0, $colonpos);
345
	}
346
	else
347
	{
348
		$myurl = $addresswithport;
349
	}
350
	// eval url so that above $myurl item can be processed if need be.
351
	$url = str_replace('$myurl', $myurl, $urltmp);
352

    
353
	$tab_array[] = array(
354
				$tab['text'],
355
				$active,
356
				$url
357
			);
358
    }
359
    display_top_tabs($tab_array);
360
    echo '</td></tr>';
361
}
362
?>
363
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
364
  <?php
365
  $cols = 0;
366
  $savevalue = gettext("Save");
367
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
368
  foreach ($pkg['fields']['field'] as $pkga) { 
369

    
370
		if ($pkga['type'] == "listtopic") {
371
			echo "<td>&nbsp;</td>";
372
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
373
			continue;
374
	    }	
375
	
376
?>
377

    
378
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
379

    
380
	  <?php
381

    
382
	  $size = "";
383

    
384
	  if(!$pkga['dontdisplayname']) {
385
		unset($req);
386
		if (isset($pkga['required']))
387
			$req = 'req';
388
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
389
		echo fixup_string($pkga['fielddescr']);
390
		echo "</td>";
391
	  }
392

    
393
	  if(!$pkga['dontcombinecells'])
394
		echo "<td class=\"vtable\">";
395
		// if user is editing a record, load in the data.
396
		$fieldname = $pkga['fieldname'];
397
		if ($get_from_post) {
398
			$value = $_POST[$fieldname];
399
			if (is_array($value)) $value = implode(',', $value);
400
		}
401
		else {
402
			if (isset($id) && $a_pkg[$id])
403
				$value = $a_pkg[$id][$fieldname];
404
			else
405
				$value = $pkga['default_value'];
406
		}
407

    
408
	      if($pkga['type'] == "input") {
409
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
410
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
411
			echo "<br>" . fixup_string($pkga['description']) . "\n";
412
	      } else if($pkga['type'] == "password") {
413
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
414
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
415
			echo "<br>" . fixup_string($pkga['description']) . "\n";
416
	      } else if($pkga['type'] == "select") {
417
                  $fieldname = $pkga['fieldname'];
418
                  if (isset($pkga['multiple'])) {
419
                    $multiple = 'multiple="multiple"';
420
                    $items = explode(',', $value);
421
                    $fieldname .= "[]";
422
                  }
423
                  else {
424
                    $multiple = '';
425
                    $items = array($value);
426
                  }
427
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
428
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
429

    
430
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
431
                  foreach ($pkga['options']['option'] as $opt) {
432
                      $selected = '';
433
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
434
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
435
                  }
436

    
437
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
438
	      } else if($pkga['type'] == "vpn_selection") {
439
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
440
		    foreach ($config['ipsec']['phase1'] as $vpn) {
441
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
442
		    }
443
		    echo "</select>\n";
444
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
445
	      } else if($pkga['type'] == "checkbox") {
446
			$checkboxchecked = "";
447
			if($value == "on") $checkboxchecked = " CHECKED";
448
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
449
				$onclick = ' onclick="javascript:enablechange();"';
450
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
451
			echo "<br>" . fixup_string($pkga['description']) . "\n";
452
	      } else if($pkga['type'] == "textarea") {
453
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
454
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
455
		  if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
456
		  if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
457
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
458
			echo "<br>" . fixup_string($pkga['description']) . "\n";
459
		  } else if($pkga['type'] == "interfaces_selection") {
460
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
461
			$multiple = '';
462
			$fieldname = $pkga['fieldname'];
463
			if (isset($pkga['multiple'])) {
464
				$fieldname .= '[]';
465
				$multiple = 'multiple';
466
			}
467
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
468
			$ifaces = get_configured_interface_with_descr();
469
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
470
			if (!empty($additional_ifaces))
471
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
472
			if(is_array($value))
473
				$values = $value;
474
			else
475
				$values  =  explode(',',  $value);
476
			foreach($ifaces as $ifname => $iface) {
477
				$selected = (in_array($ifname, $values) ? 'selected' : '');
478
				print("<option value=\"$ifname\" $selected>$iface</option>\n");
479
			}
480
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
481
	      } else if($pkga['type'] == "radio") {
482
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
483
	      } else if($pkga['type'] == "rowhelper") {
484
		?>
485
			<script type="text/javascript" language='javascript'>
486
			<!--
487
			<?php
488
				$rowcounter = 0;
489
				$fieldcounter = 0;
490
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
491
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
492
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
493
					$fieldcounter++;
494
				}
495
			?>
496
			-->
497
			</script>
498
			<table name="maintable" id="maintable">
499
			<tr>
500
			<?php
501
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
502
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
503
				}
504
				echo "</tr>";
505

    
506
				echo "<tr>";
507
				  // XXX: traverse saved fields, add back needed rows.
508
				echo "</tr>";
509

    
510
				echo "<tr>\n";
511
				$rowcounter = 0;
512
				$trc = 0;
513
				if(isset($a_pkg[$id]['row'])) {
514
					foreach($a_pkg[$id]['row'] as $row) {
515
					/*
516
					 * loop through saved data for record if it exists, populating rowhelper
517
					 */
518
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
519
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
520
							$fieldname = $rowhelper['fieldname'];
521
							// if user is editing a record, load in the data.
522
							if (isset($id) && $a_pkg[$id]) {
523
								$value = $row[$fieldname];
524
							}
525
							$options = "";
526
							$type = $rowhelper['type'];
527
							$fieldname = $rowhelper['fieldname'];
528
							if($type == "option") $options = &$rowhelper['options']['option'];
529
							if($rowhelper['size']) 
530
								$size = $rowhelper['size'];
531
							else
532
								$size = "8";
533
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
534
							// javascript helpers for row_helper_dynamic.js
535
							echo "</td>\n";
536
							echo "<script language=\"JavaScript\">\n";
537
							echo "<!--\n";
538
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
539
							echo "-->\n";
540
							echo "</script>\n";
541
							$text = "";
542
							$trc++;
543
						}
544

    
545
						$rowcounter++;
546
						echo "<td>";
547
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
548
						echo "</td>\n";
549
						echo "</tr>\n";
550
					}
551
				}
552
				if($trc == 0) {
553
					/*
554
					 *  no records loaded.
555
                                         *  just show a generic line non-populated with saved data
556
                                         */
557
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
558
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
559
						$fieldname = $rowhelper['fieldname'];
560
						$options = "";
561
						$type = $rowhelper['type'];
562
						$fieldname = $rowhelper['fieldname'];
563
						if($type == "option") $options = &$rowhelper['options']['option'];
564
						$size = "8";
565
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
566
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
567
						// javascript helpers for row_helper_dynamic.js
568
						echo "</td>\n";
569
						echo "<script language=\"JavaScript\">\n";
570
						echo "<!--\n";
571
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
572
						echo "-->\n";
573
						echo "</script>\n";
574
						$text = "";
575
						$trc++;
576
					}
577

    
578
					$rowcounter++;
579
				}
580
			?>
581

    
582
			  <tbody></tbody>
583
			</table>
584

    
585
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
586
		<script language="JavaScript">
587
		<!--
588
		field_counter_js = <?= $fieldcounter ?>;
589
		rows = <?= $rowcounter ?>;
590
		totalrows = <?php echo $rowcounter; ?>;
591
		loaded = <?php echo $rowcounter; ?>;
592
		//typesel_change();
593
		//-->
594
		</script>
595

    
596
		<?php
597
	      }
598
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
599
	     ?>
600

    
601
      <?php
602
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
603
      $i++;
604
  }
605
 ?>
606
  <tr>
607
	<td>&nbsp;</td>
608
  </tr>
609
  <tr>
610
    <td width="22%" valign="top">&nbsp;</td>
611
    <td width="78%">
612
<?php
613
if($pkg['note'] != "")
614
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
615
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
616
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
617
?>
618
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
619
<?php if (!$only_edit): ?>
620
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
621
<?php endif; ?>
622
    </td>
623
  </tr>
624
</table>
625
</div></tr></td></table>
626
</form>
627

    
628
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
629

    
630
<?php include("fend.inc"); ?>
631
</body>
632
</html>
633

    
634
<?php
635
/*
636
 * ROW Helpers function
637
 */
638
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
639
	global $text;
640
	echo "<td>\n";
641
	if($type == "input") {
642
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
643
	} else if($type == "checkbox") {
644
		if($value)
645
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
646
		else
647
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
648
	} else if($type == "password") {
649
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
650
	} else if($type == "textarea") {
651
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
652
	} else if($type == "select") {
653
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
654
		foreach($rowhelper['options']['option'] as $rowopt) {
655
			$selected = "";
656
			if($rowopt['value'] == $value) $selected = " SELECTED";
657
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
658
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
659
		}
660
		echo "</select>\n";
661
	}
662
}
663

    
664
function fixup_string($string) {
665
	global $config;
666
	// fixup #1: $myurl -> http[s]://ip_address:port/
667
	$https = "";
668
	$port = $config['system']['webguiport'];
669
	if($port <> "443" and $port <> "80")
670
		$urlport = ":" . $port;
671
	else
672
		$urlport = "";
673

    
674
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
675
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
676
	$newstring = str_replace("\$myurl", $myurl, $string);
677
	$string = $newstring;
678
	// fixup #2: $wanip
679
	$curwanip = get_interface_ip();
680
	$newstring = str_replace("\$wanip", $curwanip, $string);
681
	$string = $newstring;
682
	// fixup #3: $lanip
683
	$lancfg = $config['interfaces']['lan'];
684
	$lanip = $lancfg['ipaddr'];
685
	$newstring = str_replace("\$lanip", $lanip, $string);
686
	$string = $newstring;
687
	// fixup #4: fix'r'up here.
688
	return $newstring;
689
}
690

    
691
/*
692
 *  Parse templates if they are defined
693
 */
694
function parse_package_templates() {
695
	global $pkg, $config;
696
	$rows = 0;
697
	if($pkg['templates']['template'] <> "")
698
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
699
		$filename = $pkg_template_row['filename'];
700
		$template_text = $pkg_template_row['templatecontents'];
701
		$firstfield = "";
702
		/* calculate total row helpers count */
703
		foreach ($pkg['fields']['field'] as $fields) {
704
			if($fields['type'] == "rowhelper") {
705
				// save rowhelper items.
706
                                $row_helper_total_rows = 0;
707
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
708
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
709
						if($firstfield == "")  {
710
						  $firstfield = $rowhelperfield['fieldname'];
711
						} else {
712
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
713
						}
714
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
715
						$value = "";
716
						eval($comd);
717
						if($value <> "") {
718
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
719
						} else {
720
						    $row_helper_total_rows = $rows;
721
						    break;
722
						}
723
					}
724
				}
725
			}
726
		}
727

    
728
		/* replace $domain_total_rows with total rows */
729
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
730

    
731
		/* change fields defined as fieldname_fieldvalue to their value */
732
		foreach ($pkg['fields']['field'] as $fields) {
733
			if($fields['type'] == "rowhelper") {
734
				// save rowhelper items.
735
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
736
					$row_helper_data = "";
737
					$isfirst = 0;
738
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
739
						if($firstfield == "")  {
740
						  $firstfield = $rowhelperfield['fieldname'];
741
						} else {
742
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
743
						}
744
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
745
						eval($comd);
746
						if($value <> "") {
747
						    if($isfirst == 1) $row_helper_data .= "  " ;
748
						    $row_helper_data .= $value;
749
						    $isfirst = 1;
750
						}
751
						$sep = "";
752
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
753
						foreach ($sep as $se) $seperator = $se;
754
						if($seperator <> "") {
755
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
756
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
757
						}
758
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
759
					}
760
				}
761
			} else {
762
				$fieldname  = $fields['fieldname'];
763
				$fieldvalue = $_POST[$fieldname];
764
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
765
			}
766
		}
767

    
768
		/* replace cr's */
769
		$template_text = str_replace("\\n", "\n", $template_text);
770

    
771
		/* write out new template file */
772
		$fout = fopen($filename,"w");
773
		fwrite($fout, $template_text);
774
		fclose($fout);
775
	    }
776
}
777

    
778
?>
(112-112/215)