Project

General

Profile

Download (26.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
						$comd = "\$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
		// late running code
199
		if($pkg['custom_add_php_command_late'] <> "") {
200
		    eval($pkg['custom_add_php_command_late']);
201
		}
202

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

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

    
211
		parse_package_templates();
212

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

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

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

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

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

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

    
247
?>
248

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

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

    
269
		if (isset($field['checkenablefields'])) {
270
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
271
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
272
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
273
				print("\t\t}else{\n");
274
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n");
275
				print("\t\t}\n");
276
			}
277
		}
278

    
279
		print("\t}\n\telse {\n");
280

    
281
		if (isset($field['enablefields'])) {
282
			foreach (explode(',', $field['enablefields']) as $enablefield) {
283
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
284
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
285
				print("\t\t}else{\n");
286
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n");
287
				print("\t\t}\n");
288
			}
289
		}
290

    
291
		if (isset($field['checkenablefields'])) {
292
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
293
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
294
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
295
				print("\t\t}else{\n");
296
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n");
297
				print("\t\t}\n");
298
			}
299
		}
300

    
301
		print("\t}\n");
302
	}
303
}
304
?>
305
}
306
//-->
307
</script>
308
<?php } ?>
309
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
310
</script>
311

    
312
<?php include("fbegin.inc"); ?>
313
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
314
<form name="iform" action="pkg_edit.php" method="post">
315
<input type="hidden" name="xml" value="<?= $xml ?>">
316
<?php if ($savemsg) print_info_box($savemsg); ?>
317
<table width="100%" border="0" cellpadding="0" cellspacing="0">
318
<?php
319
if ($pkg['tabs'] <> "") {
320
    echo '<tr><td>';
321
    $tab_array = array();
322
    foreach($pkg['tabs']['tab'] as $tab) {
323
	if(isset($tab['active'])) {
324
		$active = true;
325
	} else {
326
		$active = false;
327
	}
328
	$urltmp = "";
329
	if($tab['url'] <> "") $urltmp = $tab['url'];
330
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
331

    
332
 	$addresswithport = getenv("HTTP_HOST");
333
	$colonpos = strpos($addresswithport, ":");
334
	if ($colonpos !== False){
335
		//my url is actually just the IP address of the pfsense box
336
		$myurl = substr($addresswithport, 0, $colonpos);
337
	}
338
	else
339
	{
340
		$myurl = $addresswithport;
341
	}
342
	// eval url so that above $myurl item can be processed if need be.
343
	$url = str_replace('$myurl', $myurl, $urltmp);
344

    
345
	$tab_array[] = array(
346
				$tab['text'],
347
				$active,
348
				$url
349
			);
350
    }
351
    display_top_tabs($tab_array);
352
    echo '</td></tr>';
353
}
354
?>
355
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
356
  <?php
357
  $cols = 0;
358
  $savevalue = gettext("Save");
359
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
360
  foreach ($pkg['fields']['field'] as $pkga) { 
361

    
362
		if ($pkga['type'] == "listtopic") {
363
			echo "<td>&nbsp;</td>";
364
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
365
			continue;
366
	    }	
367
	
368
?>
369

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

    
372
	  <?php
373

    
374
	  $size = "";
375

    
376
	  if(!$pkga['dontdisplayname']) {
377
		unset($req);
378
		if (isset($pkga['required']))
379
			$req = 'req';
380
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
381
		echo fixup_string($pkga['fielddescr']);
382
		echo "</td>";
383
	  }
384

    
385
	  if(!$pkga['dontcombinecells'])
386
		echo "<td class=\"vtable\">";
387
		// if user is editing a record, load in the data.
388
		$fieldname = $pkga['fieldname'];
389
		if ($get_from_post) {
390
			$value = $_POST[$fieldname];
391
			if (is_array($value)) $value = implode(',', $value);
392
		}
393
		else {
394
			if (isset($id) && $a_pkg[$id])
395
				$value = $a_pkg[$id][$fieldname];
396
			else
397
				$value = $pkga['default_value'];
398
		}
399

    
400
	      if($pkga['type'] == "input") {
401
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
402
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
403
			echo "<br>" . fixup_string($pkga['description']) . "\n";
404
	      } else if($pkga['type'] == "password") {
405
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
406
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
407
			echo "<br>" . fixup_string($pkga['description']) . "\n";
408
	      } else if($pkga['type'] == "select") {
409
                  $fieldname = $pkga['fieldname'];
410
                  if (isset($pkga['multiple'])) {
411
                    $multiple = 'multiple="multiple"';
412
                    $items = explode(',', $value);
413
                    $fieldname .= "[]";
414
                  }
415
                  else {
416
                    $multiple = '';
417
                    $items = array($value);
418
                  }
419
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
420
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
421

    
422
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
423
                  foreach ($pkga['options']['option'] as $opt) {
424
                      $selected = '';
425
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
426
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
427
                  }
428

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

    
507
				echo "<tr>";
508
				  // XXX: traverse saved fields, add back needed rows.
509
				echo "</tr>";
510

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

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

    
579
					$rowcounter++;
580
				}
581
			?>
582

    
583
			  <tbody></tbody>
584
			</table>
585

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

    
597
		<?php
598
	      }
599
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
600
	     ?>
601

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

    
629
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
630

    
631
<?php include("fend.inc"); ?>
632
</body>
633
</html>
634

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

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

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

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

    
729
		/* replace $domain_total_rows with total rows */
730
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
731

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

    
769
		/* replace cr's */
770
		$template_text = str_replace("\\n", "\n", $template_text);
771

    
772
		/* write out new template file */
773
		$fout = fopen($filename,"w");
774
		fwrite($fout, $template_text);
775
		fclose($fout);
776
	    }
777
}
778

    
779
?>
(112-112/216)