Project

General

Profile

Download (25.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
##|+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
						$fieldname = "\$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

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

    
204
		if (isset($pkg['filter_rules_needed']))
205
			pkg_build_filter_rules();
206

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

    
212
		parse_package_templates();
213

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

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

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

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

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

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

    
248
?>
249

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

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

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

    
270
		print("\t}\n\telse {\n");
271

    
272
		if (isset($field['enablefields'])) {
273
			foreach (explode(',', $field['enablefields']) as $enablefield)
274
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
275
		}
276

    
277
		if (isset($field['checkenablefields'])) {
278
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield)
279
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
280
		}
281

    
282
		print("\t}\n");
283
	}
284
}
285
?>
286
}
287
//-->
288
</script>
289
<?php } ?>
290
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
291
</script>
292

    
293
<?php include("fbegin.inc"); ?>
294
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
295
<form name="iform" action="pkg_edit.php" method="post">
296
<input type="hidden" name="xml" value="<?= $xml ?>">
297
<?php if ($savemsg) print_info_box($savemsg); ?>
298
<table width="100%" border="0" cellpadding="0" cellspacing="0">
299
<?php
300
if ($pkg['tabs'] <> "") {
301
    echo '<tr><td>';
302
    $tab_array = array();
303
    foreach($pkg['tabs']['tab'] as $tab) {
304
	if(isset($tab['active'])) {
305
		$active = true;
306
	} else {
307
		$active = false;
308
	}
309
	$urltmp = "";
310
	if($tab['url'] <> "") $urltmp = $tab['url'];
311
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
312

    
313
 	$addresswithport = getenv("HTTP_HOST");
314
	$colonpos = strpos($addresswithport, ":");
315
	if ($colonpos !== False){
316
		//my url is actually just the IP address of the pfsense box
317
		$myurl = substr($addresswithport, 0, $colonpos);
318
	}
319
	else
320
	{
321
		$myurl = $addresswithport;
322
	}
323
	// eval url so that above $myurl item can be processed if need be.
324
	$url = str_replace('$myurl', $myurl, $urltmp);
325

    
326
	$tab_array[] = array(
327
				$tab['text'],
328
				$active,
329
				$url
330
			);
331
    }
332
    display_top_tabs($tab_array);
333
    echo '</td></tr>';
334
}
335
?>
336
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
337
  <?php
338
  $cols = 0;
339
  $savevalue = gettext("Save");
340
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
341
  foreach ($pkg['fields']['field'] as $pkga) { 
342

    
343
		if ($pkga['type'] == "listtopic") {
344
			echo "<td>&nbsp;</td>";
345
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
346
			continue;
347
	    }	
348
	
349
?>
350

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

    
353
	  <?php
354

    
355
	  $size = "";
356

    
357
	  if(!$pkga['dontdisplayname']) {
358
		unset($req);
359
		if (isset($pkga['required']))
360
			$req = 'req';
361
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
362
		echo fixup_string($pkga['fielddescr']);
363
		echo "</td>";
364
	  }
365

    
366
	  if(!$pkga['dontcombinecells'])
367
		echo "<td class=\"vtable\">";
368
		// if user is editing a record, load in the data.
369
		$fieldname = $pkga['fieldname'];
370
		if ($get_from_post) {
371
			$value = $_POST[$fieldname];
372
			if (is_array($value)) $value = implode(',', $value);
373
		}
374
		else {
375
			if (isset($id) && $a_pkg[$id])
376
				$value = $a_pkg[$id][$fieldname];
377
			else
378
				$value = $pkga['default_value'];
379
		}
380

    
381
	      if($pkga['type'] == "input") {
382
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
383
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
384
			echo "<br>" . fixup_string($pkga['description']) . "\n";
385
	      } else if($pkga['type'] == "password") {
386
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
387
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
388
			echo "<br>" . fixup_string($pkga['description']) . "\n";
389
	      } else if($pkga['type'] == "select") {
390
                  $fieldname = $pkga['fieldname'];
391
                  if (isset($pkga['multiple'])) {
392
                    $multiple = 'multiple="multiple"';
393
                    $items = explode(',', $value);
394
                    $fieldname .= "[]";
395
                  }
396
                  else {
397
                    $multiple = '';
398
                    $items = array($value);
399
                  }
400
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
401
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
402

    
403
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
404
                  foreach ($pkga['options']['option'] as $opt) {
405
                      $selected = '';
406
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
407
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
408
                  }
409

    
410
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
411
	      } else if($pkga['type'] == "vpn_selection") {
412
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
413
		    foreach ($config['ipsec']['phase1'] as $vpn) {
414
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
415
		    }
416
		    echo "</select>\n";
417
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
418
	      } else if($pkga['type'] == "checkbox") {
419
			$checkboxchecked = "";
420
			if($value == "on") $checkboxchecked = " CHECKED";
421
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
422
				$onclick = ' onclick="javascript:enablechange();"';
423
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
424
			echo "<br>" . fixup_string($pkga['description']) . "\n";
425
	      } else if($pkga['type'] == "textarea") {
426
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
427
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
428
		  if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
429
          if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
430
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
431
			echo "<br>" . fixup_string($pkga['description']) . "\n";
432
		  } else if($pkga['type'] == "interfaces_selection") {
433
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
434
			$multiple = '';
435
			$fieldname = $pkga['fieldname'];
436
			if (isset($pkga['multiple'])) {
437
				$fieldname .= '[]';
438
				$multiple = 'multiple';
439
			}
440
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
441
			if (isset($pkga['all_interfaces']))
442
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
443
			else
444
				$ifaces = $config['interfaces'];
445
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
446
			if (!empty($additional_ifaces))
447
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
448
			if(is_array($value))
449
				$values = $value;
450
			else
451
				$values  =  explode(',',  $value);
452
			foreach($ifaces as $ifname => $iface) {
453
				if($iface['descr'] <> "")
454
					$ifdescr = $iface['descr'];
455
				else
456
					$ifdescr = strtoupper($ifname);
457
				if ($ip = find_interface_ip($iface))
458
					$ip = " ($ip)";
459
				$selected = (in_array($ifname, $values) ? 'selected' : '');
460
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
461
			}
462
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
463
	      } else if($pkga['type'] == "radio") {
464
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
465
	      } else if($pkga['type'] == "rowhelper") {
466
		?>
467
			<script type="text/javascript" language='javascript'>
468
			<!--
469
			<?php
470
				$rowcounter = 0;
471
				$fieldcounter = 0;
472
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
473
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
474
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
475
					$fieldcounter++;
476
				}
477
			?>
478
			-->
479
			</script>
480
			<table name="maintable" id="maintable">
481
			<tr>
482
			<?php
483
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
484
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
485
				}
486
				echo "</tr>";
487

    
488
				echo "<tr>";
489
				  // XXX: traverse saved fields, add back needed rows.
490
				echo "</tr>";
491

    
492
				echo "<tr>\n";
493
				$rowcounter = 0;
494
				$trc = 0;
495
				if(isset($a_pkg[$id]['row'])) {
496
					foreach($a_pkg[$id]['row'] as $row) {
497
					/*
498
					 * loop through saved data for record if it exists, populating rowhelper
499
					 */
500
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
501
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
502
							$fieldname = $rowhelper['fieldname'];
503
							// if user is editing a record, load in the data.
504
							if (isset($id) && $a_pkg[$id]) {
505
								$value = $row[$fieldname];
506
							}
507
							$options = "";
508
							$type = $rowhelper['type'];
509
							$fieldname = $rowhelper['fieldname'];
510
							if($type == "option") $options = &$rowhelper['options']['option'];
511
							$size = "8";
512
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
513
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
514
							// javascript helpers for row_helper_dynamic.js
515
							echo "</td>\n";
516
							echo "<script language=\"JavaScript\">\n";
517
							echo "<!--\n";
518
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
519
							echo "-->\n";
520
							echo "</script>\n";
521
							$text = "";
522
							$trc++;
523
						}
524

    
525
						$rowcounter++;
526
						echo "<td>";
527
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
528
						echo "</td>\n";
529
						echo "</tr>\n";
530
					}
531
				}
532
				if($trc == 0) {
533
					/*
534
					 *  no records loaded.
535
                                         *  just show a generic line non-populated with saved data
536
                                         */
537
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
538
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
539
						$fieldname = $rowhelper['fieldname'];
540
						$options = "";
541
						$type = $rowhelper['type'];
542
						$fieldname = $rowhelper['fieldname'];
543
						if($type == "option") $options = &$rowhelper['options']['option'];
544
						$size = "8";
545
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
546
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
547
						// javascript helpers for row_helper_dynamic.js
548
						echo "</td>\n";
549
						echo "<script language=\"JavaScript\">\n";
550
						echo "<!--\n";
551
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
552
						echo "-->\n";
553
						echo "</script>\n";
554
						$text = "";
555
						$trc++;
556
					}
557

    
558
					$rowcounter++;
559
				}
560
			?>
561

    
562
			  <tbody></tbody>
563
			</table>
564

    
565
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
566
		<script language="JavaScript">
567
		<!--
568
		field_counter_js = <?= $fieldcounter ?>;
569
		rows = <?= $rowcounter ?>;
570
		totalrows = <?php echo $rowcounter; ?>;
571
		loaded = <?php echo $rowcounter; ?>;
572
		//typesel_change();
573
		//-->
574
		</script>
575

    
576
		<?php
577
	      }
578
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
579
	     ?>
580

    
581
      <?php
582
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
583
      $i++;
584
  }
585
 ?>
586
  <tr>
587
	<td>&nbsp;</td>
588
  </tr>
589
  <tr>
590
    <td width="22%" valign="top">&nbsp;</td>
591
    <td width="78%">
592
<?php
593
if($pkg['note'] != "")
594
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
595
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
596
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
597
?>
598
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
599
<?php if (!$only_edit): ?>
600
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
601
<?php endif; ?>
602
    </td>
603
  </tr>
604
</table>
605
</div></tr></td></table>
606
</form>
607

    
608
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
609

    
610
<?php include("fend.inc"); ?>
611
</body>
612
</html>
613

    
614
<?php
615
/*
616
 * ROW Helpers function
617
 */
618
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
619
	global $text;
620
	echo "<td>\n";
621
	if($type == "input") {
622
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
623
	} else if($type == "checkbox") {
624
		if($value)
625
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
626
		else
627
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
628
	} else if($type == "password") {
629
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
630
	} else if($type == "textarea") {
631
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
632
	} else if($type == "select") {
633
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
634
		foreach($rowhelper['options']['option'] as $rowopt) {
635
			$selected = "";
636
			if($rowopt['value'] == $value) $selected = " SELECTED";
637
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
638
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
639
		}
640
		echo "</select>\n";
641
	}
642
}
643

    
644
function fixup_string($string) {
645
	global $config;
646
	// fixup #1: $myurl -> http[s]://ip_address:port/
647
	$https = "";
648
	$port = $config['system']['webguiport'];
649
	if($port <> "443" and $port <> "80")
650
		$urlport = ":" . $port;
651
	else
652
		$urlport = "";
653

    
654
	if($config['system']['webguiproto'] == "https") $https = "s";
655
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
656
	$newstring = str_replace("\$myurl", $myurl, $string);
657
	$string = $newstring;
658
	// fixup #2: $wanip
659
	$curwanip = get_interface_ip();
660
	$newstring = str_replace("\$wanip", $curwanip, $string);
661
	$string = $newstring;
662
	// fixup #3: $lanip
663
	$lancfg = $config['interfaces']['lan'];
664
	$lanip = $lancfg['ipaddr'];
665
	$newstring = str_replace("\$lanip", $lanip, $string);
666
	$string = $newstring;
667
	// fixup #4: fix'r'up here.
668
	return $newstring;
669
}
670

    
671
/*
672
 *  Parse templates if they are defined
673
 */
674
function parse_package_templates() {
675
	global $pkg, $config;
676
	$rows = 0;
677
	if($pkg['templates']['template'] <> "")
678
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
679
		$filename = $pkg_template_row['filename'];
680
		$template_text = $pkg_template_row['templatecontents'];
681
		$firstfield = "";
682
		/* calculate total row helpers count */
683
		foreach ($pkg['fields']['field'] as $fields) {
684
			if($fields['type'] == "rowhelper") {
685
				// save rowhelper items.
686
                                $row_helper_total_rows = 0;
687
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
688
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
689
						if($firstfield == "")  {
690
						  $firstfield = $rowhelperfield['fieldname'];
691
						} else {
692
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
693
						}
694
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
695
						$value = "";
696
						eval($comd);
697
						if($value <> "") {
698
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
699
						} else {
700
						    $row_helper_total_rows = $rows;
701
						    break;
702
						}
703
					}
704
				}
705
			}
706
		}
707

    
708
		/* replace $domain_total_rows with total rows */
709
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
710

    
711
		/* change fields defined as fieldname_fieldvalue to their value */
712
		foreach ($pkg['fields']['field'] as $fields) {
713
			if($fields['type'] == "rowhelper") {
714
				// save rowhelper items.
715
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
716
					$row_helper_data = "";
717
					$isfirst = 0;
718
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
719
						if($firstfield == "")  {
720
						  $firstfield = $rowhelperfield['fieldname'];
721
						} else {
722
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
723
						}
724
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
725
						eval($comd);
726
						if($value <> "") {
727
						    if($isfirst == 1) $row_helper_data .= "  " ;
728
						    $row_helper_data .= $value;
729
						    $isfirst = 1;
730
						}
731
						$sep = "";
732
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
733
						foreach ($sep as $se) $seperator = $se;
734
						if($seperator <> "") {
735
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
736
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
737
						}
738
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
739
					}
740
				}
741
			} else {
742
				$fieldname  = $fields['fieldname'];
743
				$fieldvalue = $_POST[$fieldname];
744
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
745
			}
746
		}
747

    
748
		/* replace cr's */
749
		$template_text = str_replace("\\n", "\n", $template_text);
750

    
751
		/* write out new template file */
752
		$fout = fopen($filename,"w");
753
		fwrite($fout, $template_text);
754
		fclose($fout);
755
	    }
756
}
757

    
758
?>
(107-107/206)