Project

General

Profile

Download (24.7 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
require_once("guiconfig.inc");
31
require_once("pkg-utils.inc");
32

    
33
/* dummy stubs needed by some code that was MFC'd */
34
function gettext($text) { return $text; }
35
function pfSenseHeader($location) { header("Location: $location"); }
36

    
37
function gentitle_pkg($pgname) {
38
	global $pfSense_config;
39
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
40
}
41

    
42
// XXX: Make this input safe.
43
$xml = $_GET['xml'];
44
if($_POST['xml']) $xml = $_POST['xml'];
45

    
46
if($xml == "") {
47
            print_info_box_np(gettext("ERROR: No package defined."));
48
            die;
49
} else {
50
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
51
}
52

    
53
if($pkg['include_file'] <> "") {
54
	require_once($pkg['include_file']);
55
}
56

    
57
if (!isset($pkg['adddeleteeditpagefields']))
58
	$only_edit = true;
59
else
60
	$only_edit = false;
61

    
62
$package_name = $pkg['menu'][0]['name'];
63
$section      = $pkg['menu'][0]['section'];
64
$config_path  = $pkg['configpath'];
65
$name         = $pkg['name'];
66
$title        = $pkg['title'];
67
$pgtitle      = $title;
68

    
69
$id = $_GET['id'];
70
if (isset($_POST['id']))
71
	$id = $_POST['id'];
72

    
73
if($pkg['custom_php_global_functions'] <> "")
74
        eval($pkg['custom_php_global_functions']);
75

    
76
// grab the installedpackages->package_name section.
77
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
78
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
79

    
80
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
81

    
82
if($_GET['savemsg'] <> "")
83
	$savemsg = $_GET['savemsg'];
84

    
85
if($pkg['custom_php_command_before_form'] <> "")
86
	eval($pkg['custom_php_command_before_form']);
87

    
88
if ($_POST) {
89
	if($_POST['act'] == "del") {
90
		if($pkg['custom_delete_php_command']) {
91
		    if($pkg['custom_php_command_before_form'] <> "")
92
			    eval($pkg['custom_php_command_before_form']);
93
		    eval($pkg['custom_delete_php_command']);
94
		}
95
		write_config($pkg['delete_string']);
96
		// resync the configuration file code if defined.
97
		if($pkg['custom_php_resync_config_command'] <> "") {
98
			if($pkg['custom_php_command_before_form'] <> "")
99
				eval($pkg['custom_php_command_before_form']);
100
			eval($pkg['custom_php_resync_config_command']);
101
		}
102
	} else {
103
		if($pkg['custom_add_php_command']) {
104
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
105
			?>
106

    
107
<?php include("head.inc"); ?>
108
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
109
<?php include("fbegin.inc"); ?>
110
<?php
111
			}
112
			if($pkg['preoutput']) echo "<pre>";
113
			eval($pkg['custom_add_php_command']);
114
			if($pkg['preoutput']) echo "</pre>";
115
		}
116
	}
117

    
118
	// donotsave is enabled.  lets simply exit.
119
	if($pkg['donotsave'] <> "") exit;
120

    
121
	$firstfield = "";
122
	$rows = 0;
123

    
124
	$input_errors = array();
125
	$reqfields = array();
126
	$reqfieldsn = array();
127
	foreach ($pkg['fields']['field'] as $field) {
128
		if (($field['type'] == 'input') && isset($field['required'])) {
129
			$reqfields[] = $field['fieldname'];
130
			$reqfieldsn[] = $field['fielddescr'];
131
		}
132
	}
133
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
134

    
135
	if ($pkg['custom_php_validation_command'])
136
		eval($pkg['custom_php_validation_command']);
137

    
138
	// store values in xml configration file.
139
	if (!$input_errors) {
140
		$pkgarr = array();
141
		foreach ($pkg['fields']['field'] as $fields) {
142
			if($fields['type'] == "rowhelper") {
143
				// save rowhelper items.
144
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
145
				                         // XXX: this really is not helping embedded platforms.
146
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
147
						if($firstfield == "")  {
148
						  $firstfield = $rowhelperfield['fieldname'];
149
						} else {
150
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
151
						}
152
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
153
						eval($comd);
154
						if($value <> "") {
155
							$comd = "\$pkgarr['row'][" . $x . "]['" . $rowhelperfield['fieldname'] . "'] = \"" . $value . "\";";
156
							//echo($comd . "<br>");
157
							eval($comd);
158
						}
159
					}
160
				}
161
			} else {
162
				$fieldname  = $fields['fieldname'];
163
				$fieldvalue = $_POST[$fieldname];
164
				if (is_array($fieldvalue))
165
					$fieldvalue = implode(',', $fieldvalue);
166
				else {
167
					$fieldvalue = trim($fieldvalue);
168
					if ($fields['encoding'] == 'base64')
169
						$fieldvalue = base64_encode($fieldvalue);
170
				}
171
				$pkgarr[$fieldname] = $fieldvalue;
172
			}
173
		}
174

    
175
		if (isset($id) && $a_pkg[$id])
176
			$a_pkg[$id] = $pkgarr;
177
		else
178
			$a_pkg[] = $pkgarr;
179

    
180
		write_config($pkg['addedit_string']);
181

    
182
		// late running code
183
		if($pkg['custom_add_php_command_late'] <> "") {
184
		    eval($pkg['custom_add_php_command_late']);
185
		}
186

    
187
		// resync the configuration file code if defined.
188
		if($pkg['custom_php_resync_config_command'] <> "") {
189
		    eval($pkg['custom_php_resync_config_command']);
190
		}
191

    
192
		parse_package_templates();
193

    
194
		/* if start_command is defined, restart w/ this */
195
		if($pkg['start_command'] <> "")
196
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
197

    
198
		/* if restart_command is defined, restart w/ this */
199
		if($pkg['restart_command'] <> "")
200
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
201

    
202
		if($pkg['aftersaveredirect'] <> "") {
203
		    pfSenseHeader($pkg['aftersaveredirect']);
204
		} elseif(!$pkg['adddeleteeditpagefields']) {
205
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
206
		} elseif(!$pkg['preoutput']) {
207
		    pfSenseHeader("pkg.php?xml=" . $xml);
208
		}
209
		exit;
210
	}
211
	else
212
		$get_from_post = true;
213
}
214

    
215
if($pkg['title'] <> "") {
216
	$edit = ($only_edit ? '' : ': Edit');
217
	$title = $pkg['title'] . $edit;
218
}
219
else
220
	$title = gettext("Package Editor");
221

    
222
$pgtitle = $title;
223
include("head.inc");
224

    
225
if ($pkg['custom_php_after_head_command'])
226
	eval($pkg['custom_php_after_head_command']);
227

    
228
?>
229

    
230
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
231
<?php if($pkg['fields']['field'] <> "") { ?>
232
<script language="JavaScript">
233
<!--
234
function enablechange() {
235
<?php
236
foreach ($pkg['fields']['field'] as $field) {
237
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
238
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
239

    
240
		if (isset($field['enablefields'])) {
241
			foreach (explode(',', $field['enablefields']) as $enablefield)
242
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
243
		}
244

    
245
		if (isset($field['checkenablefields'])) {
246
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield)
247
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
248
		}
249

    
250
		print("\t}\n\telse {\n");
251

    
252
		if (isset($field['enablefields'])) {
253
			foreach (explode(',', $field['enablefields']) as $enablefield)
254
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
255
		}
256

    
257
		if (isset($field['checkenablefields'])) {
258
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield)
259
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
260
		}
261

    
262
		print("\t}\n");
263
	}
264
}
265
?>
266
}
267
//-->
268
</script>
269
<?php } ?>
270
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
271
</script>
272

    
273
<?php include("fbegin.inc"); ?>
274
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
275
<form name="iform" action="pkg_edit.php" method="post">
276
<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
277
<script src="/javascript/scriptaculous/scriptaculous.js" type="text/javascript"></script>
278
<input type="hidden" name="xml" value="<?= $xml ?>">
279
<?php if ($savemsg) print_info_box($savemsg); ?>
280
<table width="100%" border="0" cellpadding="0" cellspacing="0">
281
<?php
282
if ($pkg['tabs'] <> "") {
283
    echo '<tr><td>';
284
    $tab_array = array();
285
    foreach($pkg['tabs']['tab'] as $tab) {
286
	if(isset($tab['active'])) {
287
		$active = true;
288
	} else {
289
		$active = false;
290
	}
291
	$urltmp = "";
292
	if($tab['url'] <> "") $urltmp = $tab['url'];
293
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
294

    
295
 	$addresswithport = getenv("HTTP_HOST");
296
	$colonpos = strpos($addresswithport, ":");
297
	if ($colonpos !== False){
298
		//my url is actually just the IP address of the pfsense box
299
		$myurl = substr($addresswithport, 0, $colonpos);
300
	}
301
	else
302
	{
303
		$myurl = $addresswithport;
304
	}
305
	// eval url so that above $myurl item can be processed if need be.
306
	$url = str_replace('$myurl', $myurl, $urltmp);
307

    
308
	$tab_array[] = array(
309
				$tab['text'],
310
				$active,
311
				$url
312
			);
313
    }
314
    display_top_tabs($tab_array);
315
    echo '</td></tr>';
316
}
317
?>
318
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
319
  <?php
320
  $cols = 0;
321
  $savevalue = gettext("Save");
322
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
323
  foreach ($pkg['fields']['field'] as $pkga) { ?>
324

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

    
327
	  <?php
328

    
329
	  $size = "";
330

    
331
	  if(!$pkga['dontdisplayname']) {
332
		unset($req);
333
		if (isset($pkga['required']))
334
			$req = 'req';
335
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
336
		echo fixup_string($pkga['fielddescr']);
337
		echo "</td>";
338
	  }
339

    
340
	  if(!$pkga['dontcombinecells'])
341
		echo "<td class=\"vtable\">";
342
		// if user is editing a record, load in the data.
343
		$fieldname = $pkga['fieldname'];
344
		if ($get_from_post) {
345
			$value = $_POST[$fieldname];
346
			if (is_array($value)) $value = implode(',', $value);
347
		}
348
		else {
349
			if (isset($id) && $a_pkg[$id])
350
				$value = $a_pkg[$id][$fieldname];
351
			else
352
				$value = $pkga['default_value'];
353
		}
354

    
355
	      if($pkga['type'] == "input") {
356
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
357
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
358
			echo "<br>" . fixup_string($pkga['description']) . "\n";
359
	      } else if($pkga['type'] == "password") {
360
			echo "<input id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
361
			echo "<br>" . fixup_string($pkga['description']) . "\n";
362
	      } else if($pkga['type'] == "select") {
363
                  $fieldname = $pkga['fieldname'];
364
                  if (isset($pkga['multiple'])) {
365
                    $multiple = 'multiple="multiple"';
366
                    $items = explode(',', $value);
367
                    $fieldname .= "[]";
368
                  }
369
                  else {
370
                    $multiple = '';
371
                    $items = array($value);
372
                  }
373
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
374
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
375

    
376
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
377
                  foreach ($pkga['options']['option'] as $opt) {
378
                      $selected = '';
379
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
380
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
381
                  }
382

    
383
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
384
	      } else if($pkga['type'] == "vpn_selection") {
385
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
386
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
387
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
388
		    }
389
		    echo "</select>\n";
390
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
391
	      } else if($pkga['type'] == "checkbox") {
392
			$checkboxchecked = "";
393
			if($value == "on") $checkboxchecked = " CHECKED";
394
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
395
				$onclick = ' onclick="javascript:enablechange();"';
396
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
397
			echo "<br>" . fixup_string($pkga['description']) . "\n";
398
	      } else if($pkga['type'] == "textarea") {
399
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
400
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
401
                  if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
402
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
403
			echo "<br>" . fixup_string($pkga['description']) . "\n";
404
		  } else if($pkga['type'] == "interfaces_selection") {
405
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
406
			$multiple = '';
407
			$fieldname = $pkga['fieldname'];
408
			if (isset($pkga['multiple'])) {
409
				$fieldname .= '[]';
410
				$multiple = 'multiple';
411
			}
412
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
413

    
414
			if (isset($pkga['all_interfaces']))
415
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
416
			else
417
				$ifaces = $config['interfaces'];
418

    
419
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
420
			if (!empty($additional_ifaces))
421
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
422
			if(is_array($value))
423
				$values = $value;
424
			else
425
				$values  =  explode(',',  $value);
426

    
427
			foreach($ifaces as $ifname => $iface) {
428
				if (isset($iface['descr']))
429
					$ifdescr = $iface['descr'];
430
				else
431
					$ifdescr = strtoupper($ifname);
432
				if ($ip = find_interface_ip($iface))
433
					$ip = " ($ip)";
434
				$selected = (in_array($ifname, $values) ? 'selected' : '');
435
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
436
			}
437

    
438
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
439
	      } else if($pkga['type'] == "radio") {
440
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
441
	      } else if($pkga['type'] == "rowhelper") {
442
		?>
443
			<script type="text/javascript" language='javascript'>
444
			<!--
445

    
446
			<?php
447
				$rowcounter = 0;
448
				$fieldcounter = 0;
449
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
450
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
451
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
452
					$fieldcounter++;
453
				}
454
			?>
455

    
456
			-->
457
			</script>
458

    
459
			<table name="maintable" id="maintable">
460
			<tr>
461
			<?php
462
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
463
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
464
				}
465
				echo "</tr>";
466

    
467
				echo "<tr>";
468
				  // XXX: traverse saved fields, add back needed rows.
469
				echo "</tr>";
470

    
471
				echo "<tr>\n";
472
				$rowcounter = 0;
473
				$trc = 0;
474
				if(isset($a_pkg[$id]['row'])) {
475
					foreach($a_pkg[$id]['row'] as $row) {
476
					/*
477
					 * loop through saved data for record if it exists, populating rowhelper
478
					 */
479
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
480
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
481
							$fieldname = $rowhelper['fieldname'];
482
							// if user is editing a record, load in the data.
483
							if (isset($id) && $a_pkg[$id]) {
484
								$value = $row[$fieldname];
485
							}
486
							$options = "";
487
							$type = $rowhelper['type'];
488
							$fieldname = $rowhelper['fieldname'];
489
							if($type == "option") $options = &$rowhelper['options']['option'];
490
							$size = "8";
491
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
492
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
493
							// javascript helpers for row_helper_dynamic.js
494
							echo "</td>\n";
495
							echo "<script language=\"JavaScript\">\n";
496
							echo "<!--\n";
497
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
498
							echo "-->\n";
499
							echo "</script>\n";
500
							$text = "";
501
							$trc++;
502
						}
503

    
504
						$rowcounter++;
505
						echo "<td>";
506
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
507
						echo "</td>\n";
508
						echo "</tr>\n";
509
					}
510
				}
511
				if($trc == 0) {
512
					/*
513
					 *  no records loaded.
514
                                         *  just show a generic line non-populated with saved data
515
                                         */
516
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
517
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
518
						$fieldname = $rowhelper['fieldname'];
519
						$options = "";
520
						$type = $rowhelper['type'];
521
						$fieldname = $rowhelper['fieldname'];
522
						if($type == "option") $options = &$rowhelper['options']['option'];
523
						$size = "8";
524
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
525
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
526
						// javascript helpers for row_helper_dynamic.js
527
						echo "</td>\n";
528
						echo "<script language=\"JavaScript\">\n";
529
						echo "<!--\n";
530
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
531
						echo "-->\n";
532
						echo "</script>\n";
533
						$text = "";
534
						$trc++;
535
					}
536

    
537
					$rowcounter++;
538
				}
539
			?>
540

    
541
			  <tbody></tbody>
542
			</table>
543

    
544
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
545
		<script language="JavaScript">
546
		<!--
547
		field_counter_js = <?= $fieldcounter ?>;
548
		rows = <?= $rowcounter ?>;
549
		totalrows = <?php echo $rowcounter; ?>;
550
		loaded = <?php echo $rowcounter; ?>;
551
		//typesel_change();
552
		//-->
553
		</script>
554

    
555
		<?php
556
	      }
557
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
558
	     ?>
559

    
560
      <?php
561
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
562
      $i++;
563
  }
564
 ?>
565
  <tr>
566
	<td>&nbsp;</td>
567
  </tr>
568
  <tr>
569
    <td width="22%" valign="top">&nbsp;</td>
570
    <td width="78%">
571
<?php
572
if($pkg['note'] != "")
573
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
574
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
575
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
576
?>
577
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
578
<?php if (!$only_edit): ?>
579
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
580
<?php endif; ?>
581
    </td>
582
  </tr>
583
</table>
584
</div></tr></td></table>
585
</form>
586

    
587
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
588

    
589
<?php include("fend.inc"); ?>
590
</body>
591
</html>
592

    
593
<?php
594
/*
595
 * ROW Helpers function
596
 */
597
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
598
	global $text;
599
	echo "<td>\n";
600
	if($type == "input") {
601
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
602
	} else if($type == "checkbox") {
603
		if($value)
604
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
605
		else
606
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
607
	} else if($type == "password") {
608
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
609
	} else if($type == "textarea") {
610
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
611
	} else if($type == "select") {
612
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
613
		foreach($rowhelper['options']['option'] as $rowopt) {
614
			$selected = "";
615
			if($rowopt['value'] == $value) $selected = " SELECTED";
616
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
617
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
618
		}
619
		echo "</select>\n";
620
	}
621
}
622

    
623
function fixup_string($string) {
624
	global $config;
625
	// fixup #1: $myurl -> http[s]://ip_address:port/
626
	$https = "";
627
	$port = $config['system']['webguiport'];
628
	if($port <> "443" and $port <> "80")
629
		$urlport = ":" . $port;
630
	else
631
		$urlport = "";
632

    
633
	if($config['system']['webguiproto'] == "https") $https = "s";
634
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
635
	$newstring = str_replace("\$myurl", $myurl, $string);
636
	$string = $newstring;
637
	// fixup #2: $wanip
638
	$curwanip = get_current_wan_address();
639
	$newstring = str_replace("\$wanip", $curwanip, $string);
640
	$string = $newstring;
641
	// fixup #3: $lanip
642
	$lancfg = $config['interfaces']['lan'];
643
	$lanip = $lancfg['ipaddr'];
644
	$newstring = str_replace("\$lanip", $lanip, $string);
645
	$string = $newstring;
646
	// fixup #4: fix'r'up here.
647
	return $newstring;
648
}
649

    
650
/*
651
 *  Parse templates if they are defined
652
 */
653
function parse_package_templates() {
654
	global $pkg, $config;
655
	$rows = 0;
656
	if($pkg['templates']['template'] <> "")
657
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
658
		$filename = $pkg_template_row['filename'];
659
		$template_text = $pkg_template_row['templatecontents'];
660
		$firstfield = "";
661
		/* calculate total row helpers count */
662
		foreach ($pkg['fields']['field'] as $fields) {
663
			if($fields['type'] == "rowhelper") {
664
				// save rowhelper items.
665
                                $row_helper_total_rows = 0;
666
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
667
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
668
						if($firstfield == "")  {
669
						  $firstfield = $rowhelperfield['fieldname'];
670
						} else {
671
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
672
						}
673
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
674
						$value = "";
675
						eval($comd);
676
						if($value <> "") {
677
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
678
						} else {
679
						    $row_helper_total_rows = $rows;
680
						    break;
681
						}
682
					}
683
				}
684
			}
685
		}
686

    
687
		/* replace $domain_total_rows with total rows */
688
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
689

    
690
		/* change fields defined as fieldname_fieldvalue to their value */
691
		foreach ($pkg['fields']['field'] as $fields) {
692
			if($fields['type'] == "rowhelper") {
693
				// save rowhelper items.
694
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
695
					$row_helper_data = "";
696
					$isfirst = 0;
697
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
698
						if($firstfield == "")  {
699
						  $firstfield = $rowhelperfield['fieldname'];
700
						} else {
701
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
702
						}
703
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
704
						eval($comd);
705
						if($value <> "") {
706
						    if($isfirst == 1) $row_helper_data .= "  " ;
707
						    $row_helper_data .= $value;
708
						    $isfirst = 1;
709
						}
710
						$sep = "";
711
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
712
						foreach ($sep as $se) $seperator = $se;
713
						if($seperator <> "") {
714
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
715
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
716
						}
717
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
718
					}
719
				}
720
			} else {
721
				$fieldname  = $fields['fieldname'];
722
				$fieldvalue = $_POST[$fieldname];
723
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
724
			}
725
		}
726

    
727
		/* replace cr's */
728
		$template_text = str_replace("\\n", "\n", $template_text);
729

    
730
		/* write out new template file */
731
		$fout = fopen($filename,"w");
732
		fwrite($fout, $template_text);
733
		fclose($fout);
734
	    }
735
}
736

    
737
?>
(94-94/186)