Project

General

Profile

Download (29.7 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'] == "listtopic")
169
				continue;
170
			if($fields['type'] == "rowhelper") {
171
				// save rowhelper items.
172
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
173
				                         // XXX: this really is not helping embedded platforms.
174
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
175
						if($firstfield == "")  {
176
						  $firstfield = $rowhelperfield['fieldname'];
177
						} else {
178
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
179
						}
180
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
181
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
182
						eval($comd);
183
						if($value <> "") {
184
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
185
							//echo($comd . "<br>");
186
							eval($comd);
187
						}
188
					}
189
				}
190
			} else {
191
				$fieldname  = $fields['fieldname'];
192
				$fieldvalue = $_POST[$fieldname];
193
				if (is_array($fieldvalue))
194
					$fieldvalue = implode(',', $fieldvalue);
195
				else {
196
					$fieldvalue = trim($fieldvalue);
197
					if ($fields['encoding'] == 'base64')
198
						$fieldvalue = base64_encode($fieldvalue);
199
				}
200
				if($fieldname)
201
					$pkgarr[$fieldname] = $fieldvalue;
202
			}
203
		}
204

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

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

    
216
		if (isset($pkg['filter_rules_needed'])) 
217
			filter_configure();
218

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

    
224
		parse_package_templates();
225

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

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

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

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

    
254
$pgtitle = $title;
255
include("head.inc");
256

    
257
if ($pkg['custom_php_after_head_command'])
258
	eval($pkg['custom_php_after_head_command']);
259

    
260
?>
261

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

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

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

    
292
		print("\t}\n\telse {\n");
293

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

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

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

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

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

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

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

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

    
385
	  <?php
386

    
387
	  $size = "";
388

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

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

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

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

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

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

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

    
545
				echo "<tr>";
546
				  // XXX: traverse saved fields, add back needed rows.
547
				echo "</tr>";
548

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

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

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

    
632
			  <tbody></tbody>
633
			</table>
634

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

    
650
		<?php
651
	      }
652
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
653
	     ?>
654

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

    
682
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
683

    
684
<?php include("fend.inc"); ?>
685
</body>
686
</html>
687

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

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

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

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

    
804
		/* replace $domain_total_rows with total rows */
805
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
806

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

    
844
		/* replace cr's */
845
		$template_text = str_replace("\\n", "\n", $template_text);
846

    
847
		/* write out new template file */
848
		$fout = fopen($filename,"w");
849
		fwrite($fout, $template_text);
850
		fclose($fout);
851
	    }
852
}
853

    
854
?>
(116-116/221)