Project

General

Profile

Download (29.8 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
    echo '<tr><td>';
336
    $tab_array = array();
337
    foreach($pkg['tabs']['tab'] as $tab) {
338
	if(isset($tab['active'])) {
339
		$active = true;
340
	} else {
341
		$active = false;
342
	}
343
	$urltmp = "";
344
	if($tab['url'] <> "") $urltmp = $tab['url'];
345
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
346

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

    
360
	$tab_array[] = array(
361
				$tab['text'],
362
				$active,
363
				$url
364
			);
365
    }
366
    display_top_tabs($tab_array);
367
    echo '</td></tr>';
368
}
369
?>
370
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
371
  <?php
372
  $cols = 0;
373
  $savevalue = gettext("Save");
374
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
375
  foreach ($pkg['fields']['field'] as $pkga) { 
376
		if ($pkga['type'] == "sorting") 
377
			continue;
378

    
379
		if ($pkga['type'] == "listtopic") {
380
			echo "<td>&nbsp;</td>";
381
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
382
			continue;
383
	    }	
384
	
385
?>
386

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

    
389
	  <?php
390

    
391
	  $size = "";
392

    
393
	  if(!$pkga['dontdisplayname']) {
394
		unset($req);
395
		if (isset($pkga['required']))
396
			$req = 'req';
397
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
398
		echo fixup_string($pkga['fielddescr']);
399
		echo "</td>";
400
	  }
401

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

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

    
439
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
440
                  foreach ($pkga['options']['option'] as $opt) {
441
                      $selected = '';
442
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
443
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
444
                  }
445

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

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

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

    
549
				echo "<tr>";
550
				  // XXX: traverse saved fields, add back needed rows.
551
				echo "</tr>";
552

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

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

    
628
					$rowcounter++;
629
					echo "<td>";
630
					echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
631
					echo "</td>\n";
632
					echo "</tr>\n";
633
				}
634
			?>
635

    
636
			  <tbody></tbody>
637
			</table>
638

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

    
654
		<?php
655
	      }
656
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
657
	     ?>
658

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

    
686
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
687

    
688
<?php include("fend.inc"); ?>
689
</body>
690
</html>
691

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

    
744
function fixup_string($string) {
745
	global $config;
746
	// fixup #1: $myurl -> http[s]://ip_address:port/
747
	$https = "";
748
	$port = $config['system']['webguiport'];
749
	if($port <> "443" and $port <> "80")
750
		$urlport = ":" . $port;
751
	else
752
		$urlport = "";
753

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

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

    
808
		/* replace $domain_total_rows with total rows */
809
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
810

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

    
848
		/* replace cr's */
849
		$template_text = str_replace("\\n", "\n", $template_text);
850

    
851
		/* write out new template file */
852
		$fout = fopen($filename,"w");
853
		fwrite($fout, $template_text);
854
		fclose($fout);
855
	    }
856
}
857

    
858
?>
(117-117/222)