Project

General

Profile

Download (26.4 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
##|+PRIV
31
##|*IDENT=page-package-edit
32
##|*NAME=Package: Edit page
33
##|*DESCR=Allow access to the 'Package: Edit' page.
34
##|*MATCH=pkg_edit.php*
35
##|-PRIV
36

    
37

    
38
require_once("guiconfig.inc");
39
require_once("pkg-utils.inc");
40

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

    
44
function gentitle_pkg($pgname) {
45
	global $pfSense_config;
46
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
47
}
48

    
49
$xml = htmlspecialchars($_GET['xml']);
50
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
51

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

    
59
if($pkg['include_file'] <> "") {
60
	require_once($pkg['include_file']);
61
}
62

    
63
if (!isset($pkg['adddeleteeditpagefields']))
64
	$only_edit = true;
65
else
66
	$only_edit = false;
67

    
68
$package_name = $pkg['menu'][0]['name'];
69
$section      = $pkg['menu'][0]['section'];
70
$config_path  = $pkg['configpath'];
71
$name         = $pkg['name'];
72
$title        = $pkg['title'];
73
$pgtitle      = $title;
74

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

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

    
91
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
92

    
93
if($_GET['savemsg'] <> "")
94
	$savemsg = htmlspecialchars($_GET['savemsg']);
95

    
96
if($pkg['custom_php_command_before_form'] <> "")
97
	eval($pkg['custom_php_command_before_form']);
98

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

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

    
129
	// donotsave is enabled.  lets simply exit.
130
	if($pkg['donotsave'] <> "") exit;
131

    
132
	$firstfield = "";
133
	$rows = 0;
134

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

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

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

    
192
		if (isset($id) && $a_pkg[$id])
193
			$a_pkg[$id] = $pkgarr;
194
		else
195
			$a_pkg[] = $pkgarr;
196

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

    
203
		if (isset($pkg['filter_rules_needed'])) {
204
			pkg_build_filter_rules();
205
			filter_configure();
206
		}
207

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

    
213
		parse_package_templates();
214

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

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

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

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

    
243
$pgtitle = $title;
244
include("head.inc");
245

    
246
if ($pkg['custom_php_after_head_command'])
247
	eval($pkg['custom_php_after_head_command']);
248

    
249
?>
250

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

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

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

    
281
		print("\t}\n\telse {\n");
282

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

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

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

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

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

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

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

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

    
374
	  <?php
375

    
376
	  $size = "";
377

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

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

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

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

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

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

    
513
				echo "<tr>\n";
514
				$rowcounter = 0;
515
				$trc = 0;
516
				if(isset($a_pkg[$id]['row'])) {
517
					foreach($a_pkg[$id]['row'] as $row) {
518
					/*
519
					 * loop through saved data for record if it exists, populating rowhelper
520
					 */
521
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
522
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
523
							$fieldname = $rowhelper['fieldname'];
524
							// if user is editing a record, load in the data.
525
							if (isset($id) && $a_pkg[$id]) {
526
								$value = $row[$fieldname];
527
							}
528
							$options = "";
529
							$type = $rowhelper['type'];
530
							$fieldname = $rowhelper['fieldname'];
531
							if($type == "option") $options = &$rowhelper['options']['option'];
532
							if($rowhelper['size']) 
533
								$size = $rowhelper['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
						$size = "8";
568
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
569
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
570
						// javascript helpers for row_helper_dynamic.js
571
						echo "</td>\n";
572
						echo "<script language=\"JavaScript\">\n";
573
						echo "<!--\n";
574
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
575
						echo "-->\n";
576
						echo "</script>\n";
577
						$text = "";
578
						$trc++;
579
					}
580

    
581
					$rowcounter++;
582
				}
583
			?>
584

    
585
			  <tbody></tbody>
586
			</table>
587

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

    
599
		<?php
600
	      }
601
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
602
	     ?>
603

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

    
631
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
632

    
633
<?php include("fend.inc"); ?>
634
</body>
635
</html>
636

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

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

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

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

    
731
		/* replace $domain_total_rows with total rows */
732
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
733

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

    
771
		/* replace cr's */
772
		$template_text = str_replace("\\n", "\n", $template_text);
773

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

    
781
?>
(111-111/216)