Project

General

Profile

Download (27.2 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
// If the first entry in the array is an empty <config/> tag, kill it.
100
if ((count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) 
101
	&& ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == ""))
102
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
103

    
104
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
105

    
106
if($_GET['savemsg'] <> "")
107
	$savemsg = htmlspecialchars($_GET['savemsg']);
108

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

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

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

    
142
	// donotsave is enabled.  lets simply exit.
143
	if($pkg['donotsave'] <> "") exit;
144

    
145
	$firstfield = "";
146
	$rows = 0;
147

    
148
	$input_errors = array();
149
	$reqfields = array();
150
	$reqfieldsn = array();
151
	foreach ($pkg['fields']['field'] as $field) {
152
		if (($field['type'] == 'input') && isset($field['required'])) {
153
			if($field['fieldname'])
154
				$reqfields[] = $field['fieldname'];
155
			if($field['fielddescr'])	
156
				$reqfieldsn[] = $field['fielddescr'];
157
		}
158
	}
159
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
160

    
161
	if ($pkg['custom_php_validation_command'])
162
		eval($pkg['custom_php_validation_command']);
163

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

    
205
		if (isset($id) && $a_pkg[$id])
206
			$a_pkg[$id] = $pkgarr;
207
		else
208
			$a_pkg[] = $pkgarr;
209

    
210
		write_config($pkg['addedit_string']);
211
		// late running code
212
		if($pkg['custom_add_php_command_late'] <> "") {
213
		    eval($pkg['custom_add_php_command_late']);
214
		}
215

    
216
		if (isset($pkg['filter_rules_needed'])) 
217
			filter_configure();
218

    
219
		// resync the configuration file code if defined.
220
		if($pkg['custom_php_resync_config_command'] <> "") {
221
		    eval($pkg['custom_php_resync_config_command']);
222
		}
223

    
224
		parse_package_templates();
225

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

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

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

    
247
if($pkg['title'] <> "") {
248
	$edit = ($only_edit ? '' : ': Edit');
249
	$title = $pkg['title'] . $edit;
250
}
251
else
252
	$title = gettext("Package Editor");
253

    
254
$pgtitle = $title;
255
include("head.inc");
256

    
257
if ($pkg['custom_php_after_head_command'])
258
	eval($pkg['custom_php_after_head_command']);
259

    
260
?>
261

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

    
272
		if (isset($field['enablefields'])) {
273
			foreach (explode(',', $field['enablefields']) as $enablefield) {
274
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
275
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
276
				print("\t\t}else{\n");
277
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 1;\n");
278
				print("\t\t}\n");
279
			}
280
		}
281

    
282
		if (isset($field['checkenablefields'])) {
283
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
284
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
285
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
286
				print("\t\t}else{\n");
287
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n");
288
				print("\t\t}\n");
289
			}
290
		}
291

    
292
		print("\t}\n\telse {\n");
293

    
294
		if (isset($field['enablefields'])) {
295
			foreach (explode(',', $field['enablefields']) as $enablefield) {
296
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
297
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
298
				print("\t\t}else{\n");
299
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n");
300
				print("\t\t}\n");
301
			}
302
		}
303

    
304
		if (isset($field['checkenablefields'])) {
305
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
306
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
307
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
308
				print("\t\t}else{\n");
309
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n");
310
				print("\t\t}\n");
311
			}
312
		}
313

    
314
		print("\t}\n");
315
	}
316
}
317
?>
318
}
319
//-->
320
</script>
321
<?php } ?>
322
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
323
</script>
324

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

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

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

    
375
		if ($pkga['type'] == "listtopic") {
376
			echo "<td>&nbsp;</td>";
377
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
378
			continue;
379
	    }	
380
	
381
?>
382

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

    
385
	  <?php
386

    
387
	  $size = "";
388

    
389
	  if(!$pkga['dontdisplayname']) {
390
		unset($req);
391
		if (isset($pkga['required']))
392
			$req = 'req';
393
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
394
		echo fixup_string($pkga['fielddescr']);
395
		echo "</td>";
396
	  }
397

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

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

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

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

    
511
				echo "<tr>";
512
				  // XXX: traverse saved fields, add back needed rows.
513
				echo "</tr>";
514

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

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

    
590
					$rowcounter++;
591
					echo "<td>";
592
					echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
593
					echo "</td>\n";
594
					echo "</tr>\n";
595
				}
596
			?>
597

    
598
			  <tbody></tbody>
599
			</table>
600

    
601
		<?php if ($pkga['size']): ?>
602
			<br><a onClick="javascript:addRowTo('maintable', <?=$pkga['size'];?>); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
603
		<?php else: ?>
604
			<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
605
		<?php endif; ?>
606
		<script language="JavaScript">
607
		<!--
608
		field_counter_js = <?= $fieldcounter ?>;
609
		rows = <?= $rowcounter ?>;
610
		totalrows = <?php echo $rowcounter; ?>;
611
		loaded = <?php echo $rowcounter; ?>;
612
		//typesel_change();
613
		//-->
614
		</script>
615

    
616
		<?php
617
	      }
618
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
619
	     ?>
620

    
621
      <?php
622
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
623
      $i++;
624
  }
625
 ?>
626
  <tr>
627
	<td>&nbsp;</td>
628
  </tr>
629
  <tr>
630
    <td width="22%" valign="top">&nbsp;</td>
631
    <td width="78%">
632
<?php
633
if($pkg['note'] != "")
634
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
635
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
636
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
637
?>
638
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
639
<?php if (!$only_edit): ?>
640
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
641
<?php endif; ?>
642
    </td>
643
  </tr>
644
</table>
645
</div></tr></td></table>
646
</form>
647

    
648
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
649

    
650
<?php include("fend.inc"); ?>
651
</body>
652
</html>
653

    
654
<?php
655
/*
656
 * ROW Helpers function
657
 */
658
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
659
	global $text;
660
	echo "<td>\n";
661
	if($type == "input") {
662
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
663
	} else if($type == "checkbox") {
664
		if($value)
665
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
666
		else
667
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
668
	} else if($type == "password") {
669
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
670
	} else if($type == "textarea") {
671
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
672
	} else if($type == "select") {
673
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
674
		foreach($rowhelper['options']['option'] as $rowopt) {
675
			$selected = "";
676
			if($rowopt['value'] == $value) $selected = " SELECTED";
677
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
678
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
679
		}
680
		echo "</select>\n";
681
	}
682
}
683

    
684
function fixup_string($string) {
685
	global $config;
686
	// fixup #1: $myurl -> http[s]://ip_address:port/
687
	$https = "";
688
	$port = $config['system']['webguiport'];
689
	if($port <> "443" and $port <> "80")
690
		$urlport = ":" . $port;
691
	else
692
		$urlport = "";
693

    
694
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
695
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
696
	$newstring = str_replace("\$myurl", $myurl, $string);
697
	$string = $newstring;
698
	// fixup #2: $wanip
699
	$curwanip = get_interface_ip();
700
	$newstring = str_replace("\$wanip", $curwanip, $string);
701
	$string = $newstring;
702
	// fixup #3: $lanip
703
	$lancfg = $config['interfaces']['lan'];
704
	$lanip = $lancfg['ipaddr'];
705
	$newstring = str_replace("\$lanip", $lanip, $string);
706
	$string = $newstring;
707
	// fixup #4: fix'r'up here.
708
	return $newstring;
709
}
710

    
711
/*
712
 *  Parse templates if they are defined
713
 */
714
function parse_package_templates() {
715
	global $pkg, $config;
716
	$rows = 0;
717
	if($pkg['templates']['template'] <> "")
718
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
719
		$filename = $pkg_template_row['filename'];
720
		$template_text = $pkg_template_row['templatecontents'];
721
		$firstfield = "";
722
		/* calculate total row helpers count */
723
		foreach ($pkg['fields']['field'] as $fields) {
724
			if($fields['type'] == "rowhelper") {
725
				// save rowhelper items.
726
                                $row_helper_total_rows = 0;
727
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
728
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
729
						if($firstfield == "")  {
730
						  $firstfield = $rowhelperfield['fieldname'];
731
						} else {
732
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
733
						}
734
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
735
						$value = "";
736
						eval($comd);
737
						if($value <> "") {
738
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
739
						} else {
740
						    $row_helper_total_rows = $rows;
741
						    break;
742
						}
743
					}
744
				}
745
			}
746
		}
747

    
748
		/* replace $domain_total_rows with total rows */
749
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
750

    
751
		/* change fields defined as fieldname_fieldvalue to their value */
752
		foreach ($pkg['fields']['field'] as $fields) {
753
			if($fields['type'] == "rowhelper") {
754
				// save rowhelper items.
755
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
756
					$row_helper_data = "";
757
					$isfirst = 0;
758
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
759
						if($firstfield == "")  {
760
						  $firstfield = $rowhelperfield['fieldname'];
761
						} else {
762
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
763
						}
764
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
765
						eval($comd);
766
						if($value <> "") {
767
						    if($isfirst == 1) $row_helper_data .= "  " ;
768
						    $row_helper_data .= $value;
769
						    $isfirst = 1;
770
						}
771
						$sep = "";
772
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
773
						foreach ($sep as $se) $seperator = $se;
774
						if($seperator <> "") {
775
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
776
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
777
						}
778
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
779
					}
780
				}
781
			} else {
782
				$fieldname  = $fields['fieldname'];
783
				$fieldvalue = $_POST[$fieldname];
784
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
785
			}
786
		}
787

    
788
		/* replace cr's */
789
		$template_text = str_replace("\\n", "\n", $template_text);
790

    
791
		/* write out new template file */
792
		$fout = fopen($filename,"w");
793
		fwrite($fout, $template_text);
794
		fclose($fout);
795
	    }
796
}
797

    
798
?>
(113-113/216)