Project

General

Profile

Download (25 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
if($pkg['custom_php_global_functions'] <> "")
80
        eval($pkg['custom_php_global_functions']);
81

    
82
// grab the installedpackages->package_name section.
83
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
84
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
85

    
86
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
87

    
88
if($_GET['savemsg'] <> "")
89
	$savemsg = htmlspecialchars($_GET['savemsg']);
90

    
91
if($pkg['custom_php_command_before_form'] <> "")
92
	eval($pkg['custom_php_command_before_form']);
93

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

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

    
124
	// donotsave is enabled.  lets simply exit.
125
	if($pkg['donotsave'] <> "") exit;
126

    
127
	$firstfield = "";
128
	$rows = 0;
129

    
130
	$input_errors = array();
131
	$reqfields = array();
132
	$reqfieldsn = array();
133
	foreach ($pkg['fields']['field'] as $field) {
134
		if (($field['type'] == 'input') && isset($field['required'])) {
135
			$reqfields[] = $field['fieldname'];
136
			$reqfieldsn[] = $field['fielddescr'];
137
		}
138
	}
139
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
140

    
141
	if ($pkg['custom_php_validation_command'])
142
		eval($pkg['custom_php_validation_command']);
143

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

    
181
		if (isset($id) && $a_pkg[$id])
182
			$a_pkg[$id] = $pkgarr;
183
		else
184
			$a_pkg[] = $pkgarr;
185

    
186
		write_config($pkg['addedit_string']);
187

    
188
		// late running code
189
		if($pkg['custom_add_php_command_late'] <> "") {
190
		    eval($pkg['custom_add_php_command_late']);
191
		}
192

    
193
		// resync the configuration file code if defined.
194
		if($pkg['custom_php_resync_config_command'] <> "") {
195
		    eval($pkg['custom_php_resync_config_command']);
196
		}
197

    
198
		parse_package_templates();
199

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

    
204
		/* if restart_command is defined, restart w/ this */
205
		if($pkg['restart_command'] <> "")
206
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
207

    
208
		if($pkg['aftersaveredirect'] <> "") {
209
		    pfSenseHeader($pkg['aftersaveredirect']);
210
		} elseif(!$pkg['adddeleteeditpagefields']) {
211
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
212
		} elseif(!$pkg['preoutput']) {
213
		    pfSenseHeader("pkg.php?xml=" . $xml);
214
		}
215
		exit;
216
	}
217
	else
218
		$get_from_post = true;
219
}
220

    
221
if($pkg['title'] <> "") {
222
	$edit = ($only_edit ? '' : ': Edit');
223
	$title = $pkg['title'] . $edit;
224
}
225
else
226
	$title = gettext("Package Editor");
227

    
228
$pgtitle = $title;
229
include("head.inc");
230

    
231
if ($pkg['custom_php_after_head_command'])
232
	eval($pkg['custom_php_after_head_command']);
233

    
234
?>
235

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

    
246
		if (isset($field['enablefields'])) {
247
			foreach (explode(',', $field['enablefields']) as $enablefield)
248
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
249
		}
250

    
251
		if (isset($field['checkenablefields'])) {
252
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield)
253
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
254
		}
255

    
256
		print("\t}\n\telse {\n");
257

    
258
		if (isset($field['enablefields'])) {
259
			foreach (explode(',', $field['enablefields']) as $enablefield)
260
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
261
		}
262

    
263
		if (isset($field['checkenablefields'])) {
264
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield)
265
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
266
		}
267

    
268
		print("\t}\n");
269
	}
270
}
271
?>
272
}
273
//-->
274
</script>
275
<?php } ?>
276
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
277
</script>
278

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

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

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

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

    
333
	  <?php
334

    
335
	  $size = "";
336

    
337
	  if(!$pkga['dontdisplayname']) {
338
		unset($req);
339
		if (isset($pkga['required']))
340
			$req = 'req';
341
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
342
		echo fixup_string($pkga['fielddescr']);
343
		echo "</td>";
344
	  }
345

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

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

    
383
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
384
                  foreach ($pkga['options']['option'] as $opt) {
385
                      $selected = '';
386
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
387
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
388
                  }
389

    
390
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
391
	      } else if($pkga['type'] == "vpn_selection") {
392
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
393
		    foreach ($config['ipsec']['phase1'] as $vpn) {
394
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
395
		    }
396
		    echo "</select>\n";
397
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
398
	      } else if($pkga['type'] == "checkbox") {
399
			$checkboxchecked = "";
400
			if($value == "on") $checkboxchecked = " CHECKED";
401
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
402
				$onclick = ' onclick="javascript:enablechange();"';
403
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
404
			echo "<br>" . fixup_string($pkga['description']) . "\n";
405
	      } else if($pkga['type'] == "textarea") {
406
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
407
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
408
		  if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
409
          if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
410
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
411
			echo "<br>" . fixup_string($pkga['description']) . "\n";
412
		  } else if($pkga['type'] == "interfaces_selection") {
413
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
414
			$multiple = '';
415
			$fieldname = $pkga['fieldname'];
416
			if (isset($pkga['multiple'])) {
417
				$fieldname .= '[]';
418
				$multiple = 'multiple';
419
			}
420
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
421

    
422
			if (isset($pkga['all_interfaces']))
423
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
424
			else
425
				$ifaces = $config['interfaces'];
426

    
427
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
428
			if (!empty($additional_ifaces))
429
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
430
			if(is_array($value))
431
				$values = $value;
432
			else
433
				$values  =  explode(',',  $value);
434

    
435
			foreach($ifaces as $ifname => $iface) {
436
				if (isset($iface['descr']))
437
					$ifdescr = $iface['descr'];
438
				else
439
					$ifdescr = strtoupper($ifname);
440
				if ($ip = find_interface_ip($iface))
441
					$ip = " ($ip)";
442
				$selected = (in_array($ifname, $values) ? 'selected' : '');
443
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
444
			}
445

    
446
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
447
	      } else if($pkga['type'] == "radio") {
448
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
449
	      } else if($pkga['type'] == "rowhelper") {
450
		?>
451
			<script type="text/javascript" language='javascript'>
452
			<!--
453

    
454
			<?php
455
				$rowcounter = 0;
456
				$fieldcounter = 0;
457
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
458
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
459
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
460
					$fieldcounter++;
461
				}
462
			?>
463

    
464
			-->
465
			</script>
466

    
467
			<table name="maintable" id="maintable">
468
			<tr>
469
			<?php
470
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
471
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
472
				}
473
				echo "</tr>";
474

    
475
				echo "<tr>";
476
				  // XXX: traverse saved fields, add back needed rows.
477
				echo "</tr>";
478

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

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

    
545
					$rowcounter++;
546
				}
547
			?>
548

    
549
			  <tbody></tbody>
550
			</table>
551

    
552
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
553
		<script language="JavaScript">
554
		<!--
555
		field_counter_js = <?= $fieldcounter ?>;
556
		rows = <?= $rowcounter ?>;
557
		totalrows = <?php echo $rowcounter; ?>;
558
		loaded = <?php echo $rowcounter; ?>;
559
		//typesel_change();
560
		//-->
561
		</script>
562

    
563
		<?php
564
	      }
565
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
566
	     ?>
567

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

    
595
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
596

    
597
<?php include("fend.inc"); ?>
598
</body>
599
</html>
600

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

    
631
function fixup_string($string) {
632
	global $config;
633
	// fixup #1: $myurl -> http[s]://ip_address:port/
634
	$https = "";
635
	$port = $config['system']['webguiport'];
636
	if($port <> "443" and $port <> "80")
637
		$urlport = ":" . $port;
638
	else
639
		$urlport = "";
640

    
641
	if($config['system']['webguiproto'] == "https") $https = "s";
642
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
643
	$newstring = str_replace("\$myurl", $myurl, $string);
644
	$string = $newstring;
645
	// fixup #2: $wanip
646
	$curwanip = get_interface_ip();
647
	$newstring = str_replace("\$wanip", $curwanip, $string);
648
	$string = $newstring;
649
	// fixup #3: $lanip
650
	$lancfg = $config['interfaces']['lan'];
651
	$lanip = $lancfg['ipaddr'];
652
	$newstring = str_replace("\$lanip", $lanip, $string);
653
	$string = $newstring;
654
	// fixup #4: fix'r'up here.
655
	return $newstring;
656
}
657

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

    
695
		/* replace $domain_total_rows with total rows */
696
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
697

    
698
		/* change fields defined as fieldname_fieldvalue to their value */
699
		foreach ($pkg['fields']['field'] as $fields) {
700
			if($fields['type'] == "rowhelper") {
701
				// save rowhelper items.
702
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
703
					$row_helper_data = "";
704
					$isfirst = 0;
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
						eval($comd);
713
						if($value <> "") {
714
						    if($isfirst == 1) $row_helper_data .= "  " ;
715
						    $row_helper_data .= $value;
716
						    $isfirst = 1;
717
						}
718
						$sep = "";
719
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
720
						foreach ($sep as $se) $seperator = $se;
721
						if($seperator <> "") {
722
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
723
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
724
						}
725
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
726
					}
727
				}
728
			} else {
729
				$fieldname  = $fields['fieldname'];
730
				$fieldvalue = $_POST[$fieldname];
731
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
732
			}
733
		}
734

    
735
		/* replace cr's */
736
		$template_text = str_replace("\\n", "\n", $template_text);
737

    
738
		/* write out new template file */
739
		$fout = fopen($filename,"w");
740
		fwrite($fout, $template_text);
741
		fclose($fout);
742
	    }
743
}
744

    
745
?>
(107-107/209)