Project

General

Profile

Download (25.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2004 Scott Ullrich
6
    All rights reserved.
7

    
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10

    
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13

    
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17

    
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29

    
30
##|+PRIV
31
##|*IDENT=page-package-edit
32
##|*NAME=Package: Edit page
33
##|*DESCR=Allow access to the 'Package: Edit' page.
34
##|*MATCH=pkg_edit.php*
35
##|-PRIV
36

    
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
151
	// store values in xml configration file.
152
	if (!$input_errors) {
153
		$pkgarr = array();
154
		foreach ($pkg['fields']['field'] as $fields) {
155
			if($fields['type'] == "listtopic")
156
				continue;
157
			if($fields['type'] == "rowhelper") {
158
				// save rowhelper items.
159
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
160
				                         // XXX: this really is not helping embedded platforms.
161
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
162
						if($firstfield == "")  {
163
						  $firstfield = $rowhelperfield['fieldname'];
164
						} else {
165
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
166
						}
167
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
168
						$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
		// resync the configuration file code if defined.
205
		if($pkg['custom_php_resync_config_command'] <> "") {
206
		    eval($pkg['custom_php_resync_config_command']);
207
		}
208

    
209
		parse_package_templates();
210

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

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

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

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

    
239
$pgtitle = $title;
240
include("head.inc");
241

    
242
if ($pkg['custom_php_after_head_command'])
243
	eval($pkg['custom_php_after_head_command']);
244

    
245
?>
246

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

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

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

    
267
		print("\t}\n\telse {\n");
268

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

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

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

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

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

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

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

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

    
350
	  <?php
351

    
352
	  $size = "";
353

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

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

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

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

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

    
485
				echo "<tr>";
486
				  // XXX: traverse saved fields, add back needed rows.
487
				echo "</tr>";
488

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

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

    
555
					$rowcounter++;
556
				}
557
			?>
558

    
559
			  <tbody></tbody>
560
			</table>
561

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

    
573
		<?php
574
	      }
575
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
576
	     ?>
577

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

    
605
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
606

    
607
<?php include("fend.inc"); ?>
608
</body>
609
</html>
610

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

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

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

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

    
705
		/* replace $domain_total_rows with total rows */
706
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
707

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

    
745
		/* replace cr's */
746
		$template_text = str_replace("\\n", "\n", $template_text);
747

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

    
755
?>
(111-111/217)