Project

General

Profile

Download (26.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
require_once("guiconfig.inc");
42
require_once("pkg-utils.inc");
43

    
44
/* dummy stubs needed by some code that was MFC'd */
45
function pfSenseHeader($location) { header("Location: " . $location); }
46

    
47
function gentitle_pkg($pgname) {
48
	global $pfSense_config;
49
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
50
}
51

    
52
$xml = htmlspecialchars($_GET['xml']);
53
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
54

    
55
if($xml == "") {
56
            print_info_box_np(gettext("ERROR: No package defined."));
57
            die;
58
} else {
59
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
60
}
61

    
62
if($pkg['include_file'] <> "") {
63
	require_once($pkg['include_file']);
64
}
65

    
66
if (!isset($pkg['adddeleteeditpagefields']))
67
	$only_edit = true;
68
else
69
	$only_edit = false;
70

    
71
$package_name = $pkg['menu'][0]['name'];
72
$section      = $pkg['menu'][0]['section'];
73
$config_path  = $pkg['configpath'];
74
$name         = $pkg['name'];
75
$title        = $pkg['title'];
76
$pgtitle      = $title;
77

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

    
90
// grab the installedpackages->package_name section.
91
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
92
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
93

    
94
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
95

    
96
if($_GET['savemsg'] <> "")
97
	$savemsg = htmlspecialchars($_GET['savemsg']);
98

    
99
if($pkg['custom_php_command_before_form'] <> "")
100
	eval($pkg['custom_php_command_before_form']);
101

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

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

    
132
	// donotsave is enabled.  lets simply exit.
133
	if($pkg['donotsave'] <> "") exit;
134

    
135
	$firstfield = "";
136
	$rows = 0;
137

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

    
151
	if ($pkg['custom_php_validation_command'])
152
		eval($pkg['custom_php_validation_command']);
153

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

    
195
		if (isset($id) && $a_pkg[$id])
196
			$a_pkg[$id] = $pkgarr;
197
		else
198
			$a_pkg[] = $pkgarr;
199

    
200
		write_config($pkg['addedit_string']);
201
		// late running code
202
		if($pkg['custom_add_php_command_late'] <> "") {
203
		    eval($pkg['custom_add_php_command_late']);
204
		}
205

    
206
		if (isset($pkg['filter_rules_needed'])) {
207
			pkg_build_filter_rules();
208
			filter_configure();
209
		}
210

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

    
216
		parse_package_templates();
217

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

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

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

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

    
246
$pgtitle = $title;
247
include("head.inc");
248

    
249
if ($pkg['custom_php_after_head_command'])
250
	eval($pkg['custom_php_after_head_command']);
251

    
252
?>
253

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

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

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

    
284
		print("\t}\n\telse {\n");
285

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

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

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

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

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

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

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

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

    
377
	  <?php
378

    
379
	  $size = "";
380

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

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

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

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

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

    
503
				echo "<tr>";
504
				  // XXX: traverse saved fields, add back needed rows.
505
				echo "</tr>";
506

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

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

    
575
					$rowcounter++;
576
				}
577
			?>
578

    
579
			  <tbody></tbody>
580
			</table>
581

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

    
593
		<?php
594
	      }
595
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
596
	     ?>
597

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

    
625
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
626

    
627
<?php include("fend.inc"); ?>
628
</body>
629
</html>
630

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

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

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

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

    
725
		/* replace $domain_total_rows with total rows */
726
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
727

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

    
765
		/* replace cr's */
766
		$template_text = str_replace("\\n", "\n", $template_text);
767

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

    
775
?>
(114-114/218)