Project

General

Profile

Download (26.5 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
			if (isset($pkga['all_interfaces']))
466
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
467
			else
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
				if($iface['descr'] <> "")
478
					$ifdescr = $iface['descr'];
479
				else
480
					$ifdescr = strtoupper($ifname);
481
				if ($ip = find_interface_ip($iface))
482
					$ip = " ($ip)";
483
				$selected = (in_array($ifname, $values) ? 'selected' : '');
484
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
485
			}
486
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
487
	      } else if($pkga['type'] == "radio") {
488
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
489
	      } else if($pkga['type'] == "rowhelper") {
490
		?>
491
			<script type="text/javascript" language='javascript'>
492
			<!--
493
			<?php
494
				$rowcounter = 0;
495
				$fieldcounter = 0;
496
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
497
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
498
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
499
					$fieldcounter++;
500
				}
501
			?>
502
			-->
503
			</script>
504
			<table name="maintable" id="maintable">
505
			<tr>
506
			<?php
507
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
508
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
509
				}
510
				echo "</tr>";
511

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

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

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

    
584
					$rowcounter++;
585
				}
586
			?>
587

    
588
			  <tbody></tbody>
589
			</table>
590

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

    
602
		<?php
603
	      }
604
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
605
	     ?>
606

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

    
634
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
635

    
636
<?php include("fend.inc"); ?>
637
</body>
638
</html>
639

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

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

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

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

    
734
		/* replace $domain_total_rows with total rows */
735
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
736

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

    
774
		/* replace cr's */
775
		$template_text = str_replace("\\n", "\n", $template_text);
776

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

    
784
?>
(112-112/215)