Project

General

Profile

Download (29.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
    pkg_edit.php
5
    Copyright (C) 2004-2010 Scott Ullrich <sullrich@gmail.com>
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
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig
31
	pfSense_MODULE:	pkgs
32
*/
33

    
34
##|+PRIV
35
##|*IDENT=page-package-edit
36
##|*NAME=Package: Edit page
37
##|*DESCR=Allow access to the 'Package: Edit' page.
38
##|*MATCH=pkg_edit.php*
39
##|-PRIV
40

    
41
ini_set('max_execution_time', '0');
42

    
43
require("guiconfig.inc");
44
require_once("functions.inc");
45
require_once("filter.inc");
46
require_once("shaper.inc");
47
require_once("pkg-utils.inc");
48

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

    
52
function gentitle_pkg($pgname) {
53
	global $pfSense_config;
54
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
55
}
56

    
57
$xml = htmlspecialchars($_GET['xml']);
58
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
59

    
60
if($xml == "") {
61
            print_info_box_np(gettext("ERROR: No package defined."));
62
            die;
63
} else {
64
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
65
}
66

    
67
if($pkg['include_file'] <> "") {
68
	require_once($pkg['include_file']);
69
}
70

    
71
if (!isset($pkg['adddeleteeditpagefields']))
72
	$only_edit = true;
73
else
74
	$only_edit = false;
75

    
76
$package_name = $pkg['menu'][0]['name'];
77
$section      = $pkg['menu'][0]['section'];
78
$config_path  = $pkg['configpath'];
79
$name         = $pkg['name'];
80
$title        = $pkg['title'];
81
$pgtitle      = $title;
82

    
83
$id = $_GET['id'];
84
if (isset($_POST['id']))
85
	$id = htmlspecialchars($_POST['id']);
86
	
87
// Not posting?  Then user is editing a record. There must be a valid id
88
// when editing a record.
89
if(!$id && !$_POST)
90
	$id = "0";
91
	
92
if($pkg['custom_php_global_functions'] <> "")
93
        eval($pkg['custom_php_global_functions']);
94

    
95
// grab the installedpackages->package_name section.
96
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
97
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
98

    
99
// If the first entry in the array is an empty <config/> tag, kill it.
100
if ((count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) 
101
	&& ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == ""))
102
	array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']);
103

    
104
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
105

    
106
if($_GET['savemsg'] <> "")
107
	$savemsg = htmlspecialchars($_GET['savemsg']);
108

    
109
if($pkg['custom_php_command_before_form'] <> "")
110
	eval($pkg['custom_php_command_before_form']);
111

    
112
if ($_POST) {
113
	if($_POST['act'] == "del") {
114
		if($pkg['custom_delete_php_command']) {
115
		    if($pkg['custom_php_command_before_form'] <> "")
116
			    eval($pkg['custom_php_command_before_form']);
117
		    eval($pkg['custom_delete_php_command']);
118
		}
119
		write_config($pkg['delete_string']);
120
		// resync the configuration file code if defined.
121
		if($pkg['custom_php_resync_config_command'] <> "") {
122
			if($pkg['custom_php_command_before_form'] <> "")
123
				eval($pkg['custom_php_command_before_form']);
124
			eval($pkg['custom_php_resync_config_command']);
125
		}
126
	} else {
127
		if($pkg['custom_add_php_command']) {
128
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
129
			?>
130

    
131
<?php include("head.inc"); ?>
132
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
133
<?php include("fbegin.inc"); ?>
134
<?php
135
			}
136
			if($pkg['preoutput']) echo "<pre>";
137
			eval($pkg['custom_add_php_command']);
138
			if($pkg['preoutput']) echo "</pre>";
139
		}
140
	}
141

    
142
	// donotsave is enabled.  lets simply exit.
143
	if($pkg['donotsave'] <> "") exit;
144

    
145
	$firstfield = "";
146
	$rows = 0;
147

    
148
	$input_errors = array();
149
	$reqfields = array();
150
	$reqfieldsn = array();
151
	foreach ($pkg['fields']['field'] as $field) {
152
		if (($field['type'] == 'input') && isset($field['required'])) {
153
			if($field['fieldname'])
154
				$reqfields[] = $field['fieldname'];
155
			if($field['fielddescr'])	
156
				$reqfieldsn[] = $field['fielddescr'];
157
		}
158
	}
159
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
160

    
161
	if ($pkg['custom_php_validation_command'])
162
		eval($pkg['custom_php_validation_command']);
163

    
164
	// store values in xml configration file.
165
	if (!$input_errors) {
166
		$pkgarr = array();
167
		foreach ($pkg['fields']['field'] as $fields) {
168
			if($fields['type'] == "sorting")
169
				continue;
170
			if($fields['type'] == "listtopic")
171
				continue;
172
			if($fields['type'] == "rowhelper") {
173
				// save rowhelper items.
174
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
175
				                         // XXX: this really is not helping embedded platforms.
176
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
177
						if($firstfield == "")  {
178
						  $firstfield = $rowhelperfield['fieldname'];
179
						} else {
180
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
181
						}
182
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
183
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
184
						eval($comd);
185
						if($value <> "") {
186
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
187
							//echo($comd . "<br>");
188
							eval($comd);
189
						}
190
					}
191
				}
192
			} else {
193
				$fieldname  = $fields['fieldname'];
194
				$fieldvalue = $_POST[$fieldname];
195
				if (is_array($fieldvalue))
196
					$fieldvalue = implode(',', $fieldvalue);
197
				else {
198
					$fieldvalue = trim($fieldvalue);
199
					if ($fields['encoding'] == 'base64')
200
						$fieldvalue = base64_encode($fieldvalue);
201
				}
202
				if($fieldname)
203
					$pkgarr[$fieldname] = $fieldvalue;
204
			}
205
		}
206

    
207
		if (isset($id) && $a_pkg[$id])
208
			$a_pkg[$id] = $pkgarr;
209
		else
210
			$a_pkg[] = $pkgarr;
211

    
212
		write_config($pkg['addedit_string']);
213
		// late running code
214
		if($pkg['custom_add_php_command_late'] <> "") {
215
		    eval($pkg['custom_add_php_command_late']);
216
		}
217

    
218
		if (isset($pkg['filter_rules_needed'])) 
219
			filter_configure();
220

    
221
		// resync the configuration file code if defined.
222
		if($pkg['custom_php_resync_config_command'] <> "") {
223
		    eval($pkg['custom_php_resync_config_command']);
224
		}
225

    
226
		parse_package_templates();
227

    
228
		/* if start_command is defined, restart w/ this */
229
		if($pkg['start_command'] <> "")
230
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
231

    
232
		/* if restart_command is defined, restart w/ this */
233
		if($pkg['restart_command'] <> "")
234
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
235

    
236
		if($pkg['aftersaveredirect'] <> "") {
237
		    pfSenseHeader($pkg['aftersaveredirect']);
238
		} elseif(!$pkg['adddeleteeditpagefields']) {
239
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
240
		} elseif(!$pkg['preoutput']) {
241
		    pfSenseHeader("pkg.php?xml=" . $xml);
242
		}
243
		exit;
244
	}
245
	else
246
		$get_from_post = true;
247
}
248

    
249
if($pkg['title'] <> "") {
250
	$edit = ($only_edit ? '' : ": " .  gettext("Edit"));
251
	$title = $pkg['title'] . $edit;
252
}
253
else
254
	$title = gettext("Package Editor");
255

    
256
$pgtitle = $title;
257
include("head.inc");
258

    
259
if ($pkg['custom_php_after_head_command'])
260
	eval($pkg['custom_php_after_head_command']);
261

    
262
?>
263

    
264
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
265
<?php if($pkg['fields']['field'] <> "") { ?>
266
<script language="JavaScript">
267
<!--
268
function enablechange() {
269
<?php
270
foreach ($pkg['fields']['field'] as $field) {
271
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
272
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
273

    
274
		if (isset($field['enablefields'])) {
275
			foreach (explode(',', $field['enablefields']) as $enablefield) {
276
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
277
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
278
				print("\t\t}else{\n");
279
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 1;\n");
280
				print("\t\t}\n");
281
			}
282
		}
283

    
284
		if (isset($field['checkenablefields'])) {
285
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield) {
286
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
287
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
288
				print("\t\t}else{\n");
289
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 0;\n");
290
				print("\t\t}\n");
291
			}
292
		}
293

    
294
		print("\t}\n\telse {\n");
295

    
296
		if (isset($field['enablefields'])) {
297
			foreach (explode(',', $field['enablefields']) as $enablefield) {
298
				print("\t\tif(document.iform.elements[\"$enablefield\"]){\n");
299
				print("\t\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
300
				print("\t\t}else{\n");
301
				print("\t\t\tdocument.iform.elements[\"$enablefield".'[]'."\"].disabled = 0;\n");
302
				print("\t\t}\n");
303
			}
304
		}
305

    
306
		if (isset($field['checkenablefields'])) {
307
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield) {
308
				print("\t\tif(document.iform.elements[\"$checkenablefield\"]){\n");
309
				print("\t\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
310
				print("\t\t}else{\n");
311
				print("\t\t\tdocument.iform.elements[\"$checkenablefield".'[]'."\"].checked = 1;\n");
312
				print("\t\t}\n");
313
			}
314
		}
315

    
316
		print("\t}\n");
317
	}
318
}
319
?>
320
}
321
//-->
322
</script>
323
<?php } ?>
324
<script type="text/javascript" language="javascript" src="/javascript/row_helper_dynamic.js">
325
</script>
326

    
327
<?php include("fbegin.inc"); ?>
328
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
329
<form name="iform" action="pkg_edit.php" method="post">
330
<input type="hidden" name="xml" value="<?= $xml ?>">
331
<?php if ($savemsg) print_info_box($savemsg); ?>
332
<table width="100%" border="0" cellpadding="0" cellspacing="0">
333
<?php
334
if ($pkg['tabs'] <> "") {
335
    $tab_array = array();
336
    foreach($pkg['tabs']['tab'] as $tab) {
337
		if($tab['tab_level'])
338
			$tab_level = $tab['tab_level'];
339
		else
340
			$tab_level = 1;
341
		if(isset($tab['active'])) {
342
			$active = true;
343
		} else {
344
			$active = false;
345
		}
346
		$urltmp = "";
347
		if($tab['url'] <> "") $urltmp = $tab['url'];
348
		if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
349

    
350
 		$addresswithport = getenv("HTTP_HOST");
351
		$colonpos = strpos($addresswithport, ":");
352
		if ($colonpos !== False) {
353
			//my url is actually just the IP address of the pfsense box
354
			$myurl = substr($addresswithport, 0, $colonpos);
355
		} else {
356
			$myurl = $addresswithport;
357
		}
358
		// eval url so that above $myurl item can be processed if need be.
359
		$url = str_replace('$myurl', $myurl, $urltmp);
360

    
361
		$tab_array[$tab_level][] = array(
362
						$tab['text'],
363
						$active,
364
						$url
365
					);
366
    }
367

    
368
	ksort($tab_array);
369
	foreach($tab_array as $tab) {
370
   		echo '<tr><td>';
371
    	display_top_tabs($tab);
372
   		echo '</td></tr>';
373
	}
374
}
375
?>
376
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
377
  <?php
378
  $cols = 0;
379
  $savevalue = gettext("Save");
380
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
381
  foreach ($pkg['fields']['field'] as $pkga) { 
382
		if ($pkga['type'] == "sorting") 
383
			continue;
384

    
385
		if ($pkga['type'] == "listtopic") {
386
			echo "<td>&nbsp;</td>";
387
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
388
			continue;
389
	    }	
390
	
391
?>
392

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

    
395
	  <?php
396

    
397
	  $size = "";
398

    
399
	  if(!$pkga['dontdisplayname']) {
400
		unset($req);
401
		if (isset($pkga['required']))
402
			$req = 'req';
403
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
404
		echo fixup_string($pkga['fielddescr']);
405
		echo "</td>";
406
	  }
407

    
408
	  if(!$pkga['dontcombinecells'])
409
		echo "<td class=\"vtable\">";
410
		// if user is editing a record, load in the data.
411
		$fieldname = $pkga['fieldname'];
412
		if ($get_from_post) {
413
			$value = $_POST[$fieldname];
414
			if (is_array($value)) $value = implode(',', $value);
415
		}
416
		else {
417
			if (isset($id) && $a_pkg[$id])
418
				$value = $a_pkg[$id][$fieldname];
419
			else
420
				$value = $pkga['default_value'];
421
		}
422

    
423
	      if($pkga['type'] == "input") {
424
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
425
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' class='formfld unknown' value='" . $value . "'>\n";
426
			echo "<br>" . fixup_string($pkga['description']) . "\n";
427
	      } else if($pkga['type'] == "password") {
428
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";		
429
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' class='formfld pwd' value='" . $value . "'>\n";
430
			echo "<br>" . fixup_string($pkga['description']) . "\n";
431
	      } else if($pkga['type'] == "select") {
432
                  $fieldname = $pkga['fieldname'];
433
                  if (isset($pkga['multiple'])) {
434
                    $multiple = 'multiple="multiple"';
435
                    $items = explode(',', $value);
436
                    $fieldname .= "[]";
437
                  }
438
                  else {
439
                    $multiple = '';
440
                    $items = array($value);
441
                  }
442
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
443
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
444

    
445
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
446
                  foreach ($pkga['options']['option'] as $opt) {
447
                      $selected = '';
448
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
449
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
450
                  }
451

    
452
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
453
		  } else if($pkga['type'] == "select_source") {
454
                  $fieldname = $pkga['fieldname'];
455
                  if (isset($pkga['multiple'])) {
456
                    $multiple = 'multiple="multiple"';
457
                    $items = explode(',', $value);
458
                    $fieldname .= "[]";
459
                  }
460
                  else {
461
                    $multiple = '';
462
                    $items = array($value);
463
                  }
464
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
465
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
466

    
467
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
468
				  $source_url = $pkga['source'];
469
				  eval("\$pkg_source_txt = &$source_url;");
470
                  foreach ($pkg_source_txt as $opt) {
471
                      $selected = '';
472
					  if($pkga['source_name']) {
473
						$source_name = $opt[$pkga['source_name']];
474
					  } else {
475
						$source_name = $opt[$pkga['name']];
476
					  }
477
					  if($pkga['source_value']) {
478
						$source_value = $opt[$pkga['source_value']];
479
					  } else {
480
						$source_value = $opt[$pkga['value']];
481
					  }
482
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
483
                      	print("\t<option name=\"{$source_name}\" value=\"{$source_value}\" $selected>{$source_name}</option>\n");
484
                  }
485

    
486
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");		
487
	      } else if($pkga['type'] == "vpn_selection") {
488
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
489
		    foreach ($config['ipsec']['phase1'] as $vpn) {
490
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
491
		    }
492
		    echo "</select>\n";
493
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
494
	      } else if($pkga['type'] == "checkbox") {
495
			$checkboxchecked = "";
496
			if($value == "on") $checkboxchecked = " CHECKED";
497
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
498
				$onclick = ' onclick="javascript:enablechange();"';
499
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
500
			echo "<br>" . fixup_string($pkga['description']) . "\n";
501
	      } else if($pkga['type'] == "textarea") {
502
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
503
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
504
		  if($pkga['wrap'] == "off") { $wrap = 'wrap="off" style="white-space:nowrap;"'; } else { $wrap = ''; }		  
505
		  if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
506
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "' ".$wrap.">" . $value . "</textarea>\n";
507
			echo "<br>" . fixup_string($pkga['description']) . "\n";
508
		  } else if($pkga['type'] == "interfaces_selection") {
509
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
510
			$multiple = '';
511
			$fieldname = $pkga['fieldname'];
512
			if (isset($pkga['multiple'])) {
513
				$fieldname .= '[]';
514
				$multiple = 'multiple';
515
			}
516
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
517
			$ifaces = get_configured_interface_with_descr();
518
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
519
			if (!empty($additional_ifaces))
520
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
521
			if(is_array($value))
522
				$values = $value;
523
			else
524
				$values  =  explode(',',  $value);
525
			foreach($ifaces as $ifname => $iface) {
526
				$selected = (in_array($ifname, $values) ? 'selected' : '');
527
				print("<option value=\"$ifname\" $selected>$iface</option>\n");
528
			}
529
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
530
	      } else if($pkga['type'] == "radio") {
531
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
532
	      } else if($pkga['type'] == "rowhelper") {
533
		?>
534
			<script type="text/javascript" language='javascript'>
535
			<!--
536
			<?php
537
				$rowcounter = 0;
538
				$fieldcounter = 0;
539
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
540
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
541
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
542
					$fieldcounter++;
543
				}
544
			?>
545
			-->
546
			</script>
547
			<table name="maintable" id="maintable">
548
			<tr>
549
			<?php
550
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
551
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
552
				}
553
				echo "</tr>";
554

    
555
				echo "<tr>";
556
				  // XXX: traverse saved fields, add back needed rows.
557
				echo "</tr>";
558

    
559
				echo "<tr>\n";
560
				$rowcounter = 0;
561
				$trc = 0;
562
				if(isset($a_pkg[$id]['row'])) {
563
					foreach($a_pkg[$id]['row'] as $row) {
564
					/*
565
					 * loop through saved data for record if it exists, populating rowhelper
566
					 */
567
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
568
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
569
							$fieldname = $rowhelper['fieldname'];
570
							// if user is editing a record, load in the data.
571
							if (isset($id) && $a_pkg[$id]) {
572
								$value = $row[$fieldname];
573
							}
574
							$options = "";
575
							$type = $rowhelper['type'];
576
							$fieldname = $rowhelper['fieldname'];
577
							if($type == "option")
578
								$options = &$rowhelper['options']['option'];
579
							if($rowhelper['size']) 
580
								$size = $rowhelper['size'];
581
							else if ($pkga['size'])
582
								$size = $pkga['size'];
583
							else
584
								$size = "8";
585
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
586
							// javascript helpers for row_helper_dynamic.js
587
							echo "</td>\n";
588
							echo "<script language=\"JavaScript\">\n";
589
							echo "<!--\n";
590
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
591
							echo "-->\n";
592
							echo "</script>\n";
593
							$text = "";
594
							$trc++;
595
						}
596

    
597
						$rowcounter++;
598
						echo "<td>";
599
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
600
						echo "</td>\n";
601
						echo "</tr>\n";
602
					}
603
				}
604
				if($trc == 0) {
605
					/*
606
					 *  no records loaded.
607
                                         *  just show a generic line non-populated with saved data
608
                                         */
609
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
610
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
611
						$fieldname = $rowhelper['fieldname'];
612
						$options = "";
613
						$type = $rowhelper['type'];
614
						$fieldname = $rowhelper['fieldname'];
615
						if($type == "option") $options = &$rowhelper['options']['option'];
616
						if($rowhelper['size'] <> "")
617
							$size = $rowhelper['size'];
618
						else if ($pkga['size'])
619
							$size = $pkga['size'];
620
						else
621
							$size = 8;
622
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
623
						// javascript helpers for row_helper_dynamic.js
624
						echo "</td>\n";
625
						echo "<script language=\"JavaScript\">\n";
626
						echo "<!--\n";
627
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
628
						echo "-->\n";
629
						echo "</script>\n";
630
						$text = "";
631
						$trc++;
632
					}
633

    
634
					$rowcounter++;
635
					echo "<td>";
636
					echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
637
					echo "</td>\n";
638
					echo "</tr>\n";
639
				}
640
			?>
641

    
642
			  <tbody></tbody>
643
			</table>
644

    
645
		<?php if ($pkga['size']): ?>
646
			<br><a onClick="javascript:addRowTo('maintable', <?=$pkga['size'];?>); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
647
		<?php else: ?>
648
			<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
649
		<?php endif; ?>
650
		<script language="JavaScript">
651
		<!--
652
		field_counter_js = <?= $fieldcounter ?>;
653
		rows = <?= $rowcounter ?>;
654
		totalrows = <?php echo $rowcounter; ?>;
655
		loaded = <?php echo $rowcounter; ?>;
656
		//typesel_change();
657
		//-->
658
		</script>
659

    
660
		<?php
661
	      }
662
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
663
	     ?>
664

    
665
      <?php
666
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
667
      $i++;
668
  }
669
 ?>
670
  <tr>
671
	<td>&nbsp;</td>
672
  </tr>
673
  <tr>
674
    <td width="22%" valign="top">&nbsp;</td>
675
    <td width="78%">
676
<?php
677
if($pkg['note'] != "")
678
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
679
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
680
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
681
?>
682
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
683
<?php if (!$only_edit): ?>
684
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
685
<?php endif; ?>
686
    </td>
687
  </tr>
688
</table>
689
</div></tr></td></table>
690
</form>
691

    
692
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
693

    
694
<?php include("fend.inc"); ?>
695
</body>
696
</html>
697

    
698
<?php
699
/*
700
 * ROW Helpers function
701
 */
702
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
703
	global $text, $config;
704
	echo "<td>\n";
705
	if($type == "input") {
706
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
707
	} else if($type == "checkbox") {
708
		if($value)
709
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
710
		else
711
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
712
	} else if($type == "password") {
713
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' class='formfld pwd' value='" . $value . "'>\n";
714
	} else if($type == "textarea") {
715
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' class='formfld unknown' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
716
	} else if($type == "select") {
717
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
718
		foreach($rowhelper['options']['option'] as $rowopt) {
719
			$selected = "";
720
			if($rowopt['value'] == $value) $selected = " SELECTED";
721
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
722
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
723
		}
724
		echo "</select>\n";
725
	} else if($type == "select_source") {
726
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
727
	    $source_url = $rowhelper['source'];
728
	    eval("\$pkg_source_txt = &$source_url;");
729
		foreach($pkg_source_txt as $opt) {
730
			$selected = "";
731
		    if($rowhelper['source_name']) {
732
				$source_name = $opt[$rowhelper['source_name']];
733
		    } else {
734
				$source_name = $opt[$rowhelper['name']];
735
		    }
736
		  	if($rowhelper['source_value']) {
737
				$source_value = $opt[$rowhelper['source_value']];
738
		  	} else {
739
				$source_value = $opt[$rowhelper['value']];
740
		  	}
741
			if($source_value == $value) 
742
				$selected = " SELECTED";
743
			$text .= "<option value='" . $source_value . "'" . $selected . ">" . $source_name . "</option>";
744
			echo "<option value='" . $source_value . "'" . $selected . ">" . $source_name . "</option>\n";
745
		}
746
		echo "</select>\n";		
747
	}
748
}
749

    
750
function fixup_string($string) {
751
	global $config;
752
	// fixup #1: $myurl -> http[s]://ip_address:port/
753
	$https = "";
754
	$port = $config['system']['webguiport'];
755
	if($port <> "443" and $port <> "80")
756
		$urlport = ":" . $port;
757
	else
758
		$urlport = "";
759

    
760
	if($config['system']['webgui']['protocol'] == "https") $https = "s";
761
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
762
	$newstring = str_replace("\$myurl", $myurl, $string);
763
	$string = $newstring;
764
	// fixup #2: $wanip
765
	$curwanip = get_interface_ip();
766
	$newstring = str_replace("\$wanip", $curwanip, $string);
767
	$string = $newstring;
768
	// fixup #3: $lanip
769
	$lancfg = $config['interfaces']['lan'];
770
	$lanip = $lancfg['ipaddr'];
771
	$newstring = str_replace("\$lanip", $lanip, $string);
772
	$string = $newstring;
773
	// fixup #4: fix'r'up here.
774
	return $newstring;
775
}
776

    
777
/*
778
 *  Parse templates if they are defined
779
 */
780
function parse_package_templates() {
781
	global $pkg, $config;
782
	$rows = 0;
783
	if($pkg['templates']['template'] <> "")
784
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
785
		$filename = $pkg_template_row['filename'];
786
		$template_text = $pkg_template_row['templatecontents'];
787
		$firstfield = "";
788
		/* calculate total row helpers count */
789
		foreach ($pkg['fields']['field'] as $fields) {
790
			if($fields['type'] == "rowhelper") {
791
				// save rowhelper items.
792
                                $row_helper_total_rows = 0;
793
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
794
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
795
						if($firstfield == "")  {
796
						  $firstfield = $rowhelperfield['fieldname'];
797
						} else {
798
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
799
						}
800
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
801
						$value = "";
802
						eval($comd);
803
						if($value <> "") {
804
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
805
						} else {
806
						    $row_helper_total_rows = $rows;
807
						    break;
808
						}
809
					}
810
				}
811
			}
812
		}
813

    
814
		/* replace $domain_total_rows with total rows */
815
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
816

    
817
		/* change fields defined as fieldname_fieldvalue to their value */
818
		foreach ($pkg['fields']['field'] as $fields) {
819
			if($fields['type'] == "rowhelper") {
820
				// save rowhelper items.
821
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
822
					$row_helper_data = "";
823
					$isfirst = 0;
824
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
825
						if($firstfield == "")  {
826
						  $firstfield = $rowhelperfield['fieldname'];
827
						} else {
828
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
829
						}
830
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
831
						eval($comd);
832
						if($value <> "") {
833
						    if($isfirst == 1) $row_helper_data .= "  " ;
834
						    $row_helper_data .= $value;
835
						    $isfirst = 1;
836
						}
837
						$sep = "";
838
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
839
						foreach ($sep as $se) $seperator = $se;
840
						if($seperator <> "") {
841
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
842
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
843
						}
844
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
845
					}
846
				}
847
			} else {
848
				$fieldname  = $fields['fieldname'];
849
				$fieldvalue = $_POST[$fieldname];
850
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
851
			}
852
		}
853

    
854
		/* replace cr's */
855
		$template_text = str_replace("\\n", "\n", $template_text);
856

    
857
		/* write out new template file */
858
		$fout = fopen($filename,"w");
859
		fwrite($fout, $template_text);
860
		fclose($fout);
861
	    }
862
}
863

    
864
?>
(117-117/222)