Project

General

Profile

Download (26.9 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")
529
								$options = &$rowhelper['options']['option'];
530
							if($rowhelper['size']) 
531
								$size = $rowhelper['size'];
532
							else if ($pkga['size'])
533
								$size = $pkga['size'];
534
							else
535
								$size = "8";
536
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
537
							// javascript helpers for row_helper_dynamic.js
538
							echo "</td>\n";
539
							echo "<script language=\"JavaScript\">\n";
540
							echo "<!--\n";
541
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
542
							echo "-->\n";
543
							echo "</script>\n";
544
							$text = "";
545
							$trc++;
546
						}
547

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

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

    
593
			  <tbody></tbody>
594
			</table>
595

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

    
611
		<?php
612
	      }
613
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
614
	     ?>
615

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

    
643
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
644

    
645
<?php include("fend.inc"); ?>
646
</body>
647
</html>
648

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

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

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

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

    
743
		/* replace $domain_total_rows with total rows */
744
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
745

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

    
783
		/* replace cr's */
784
		$template_text = str_replace("\\n", "\n", $template_text);
785

    
786
		/* write out new template file */
787
		$fout = fopen($filename,"w");
788
		fwrite($fout, $template_text);
789
		fclose($fout);
790
	    }
791
}
792

    
793
?>
(115-115/218)