Project

General

Profile

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

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

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

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

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

    
30
require_once("guiconfig.inc");
31
require_once("pkg-utils.inc");
32

    
33
/* dummy stubs needed by some code that was MFC'd */
34
function gettext($text) { return $text; }
35
function pfSenseHeader($location) { header("Location: $location"); }
36

    
37
function gentitle_pkg($pgname) {
38
	global $pfSense_config;
39
	return $pfSense_config['system']['hostname'] . "." . $pfSense_config['system']['domain'] . " - " . $pgname;
40
}
41

    
42
$xml = htmlspecialchars($_GET['xml']);
43
if($_POST['xml']) $xml = htmlspecialchars($_POST['xml']);
44

    
45
if($xml == "") {
46
            print_info_box_np(gettext("ERROR: No package defined."));
47
            die;
48
} else {
49
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
50
}
51

    
52
if($pkg['include_file'] <> "") {
53
	require_once($pkg['include_file']);
54
}
55

    
56
if (!isset($pkg['adddeleteeditpagefields']))
57
	$only_edit = true;
58
else
59
	$only_edit = false;
60

    
61
$package_name = $pkg['menu'][0]['name'];
62
$section      = $pkg['menu'][0]['section'];
63
$config_path  = $pkg['configpath'];
64
$name         = $pkg['name'];
65
$title        = $pkg['title'];
66
$pgtitle      = $title;
67

    
68
$id = htmlspecialchars($_GET['id']);
69
if (isset($_POST['id']))
70
	$id = htmlspecialchars($_POST['id']);
71

    
72
// Not posting?  Then user is editing a record. There must be a valid id
73
// when editing a record.
74
if(!$id && !$_POST)
75
	$id = "0";
76

    
77
if($pkg['custom_php_global_functions'] <> "")
78
        eval($pkg['custom_php_global_functions']);
79

    
80
// grab the installedpackages->package_name section.
81
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
82
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
83

    
84
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
85

    
86
if($_GET['savemsg'] <> "")
87
	$savemsg = htmlspecialchars($_GET['savemsg']);
88

    
89
if($pkg['custom_php_command_before_form'] <> "")
90
	eval($pkg['custom_php_command_before_form']);
91

    
92
if ($_POST) {
93
	if($_POST['act'] == "del") {
94
		if($pkg['custom_delete_php_command']) {
95
		    if($pkg['custom_php_command_before_form'] <> "")
96
			    eval($pkg['custom_php_command_before_form']);
97
		    eval($pkg['custom_delete_php_command']);
98
		}
99
		write_config($pkg['delete_string']);
100
		// resync the configuration file code if defined.
101
		if($pkg['custom_php_resync_config_command'] <> "") {
102
			if($pkg['custom_php_command_before_form'] <> "")
103
				eval($pkg['custom_php_command_before_form']);
104
			eval($pkg['custom_php_resync_config_command']);
105
		}
106
	} else {
107
		if($pkg['custom_add_php_command']) {
108
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
109
			?>
110

    
111
<?php include("head.inc"); ?>
112
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
113
<?php include("fbegin.inc"); ?>
114
<?php
115
			}
116
			if($pkg['preoutput']) echo "<pre>";
117
			eval($pkg['custom_add_php_command']);
118
			if($pkg['preoutput']) echo "</pre>";
119
		}
120
	}
121

    
122
	// donotsave is enabled.  lets simply exit.
123
	if($pkg['donotsave'] <> "") exit;
124

    
125
	$firstfield = "";
126
	$rows = 0;
127

    
128
	$input_errors = array();
129
	$reqfields = array();
130
	$reqfieldsn = array();
131
	foreach ($pkg['fields']['field'] as $field) {
132
		if (($field['type'] == 'input') && isset($field['required'])) {
133
			$reqfields[] = $field['fieldname'];
134
			$reqfieldsn[] = $field['fielddescr'];
135
		}
136
	}
137
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);
138

    
139
	if ($pkg['custom_php_validation_command'])
140
		eval($pkg['custom_php_validation_command']);
141

    
142
	// store values in xml configration file.
143
	if (!$input_errors) {
144
		$pkgarr = array();
145
		foreach ($pkg['fields']['field'] as $fields) {
146
			if($fields['type'] == "listtopic")
147
				continue;
148
			if($fields['type'] == "rowhelper") {
149
				// save rowhelper items.
150
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
151
				                         // XXX: this really is not helping embedded platforms.
152
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
153
						if($firstfield == "")  {
154
						  $firstfield = $rowhelperfield['fieldname'];
155
						} else {
156
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
157
						}
158
						$fieldname = str_replace("\\", "", $rowhelperfield['fieldname']);
159
						$comd = "\$value = \$_POST['" . $fieldname . $x . "'];";
160
//						echo($comd . "<br>");
161
						eval($comd);
162
						if($value <> "") {
163
							$comd = "\$pkgarr['row'][" . $x . "]['" . $fieldname . "'] = \"" . $value . "\";";
164
//							echo($comd . "<br>");
165
							eval($comd);
166
						}
167
					}
168
				}
169
			} else {
170
				$fieldname  = $fields['fieldname'];
171
				$fieldvalue = $_POST[$fieldname];
172
				if (is_array($fieldvalue))
173
					$fieldvalue = implode(',', $fieldvalue);
174
				else {
175
					$fieldvalue = trim($fieldvalue);
176
					if ($fields['encoding'] == 'base64')
177
						$fieldvalue = base64_encode($fieldvalue);
178
				}
179
				if($fieldname)
180
					$pkgarr[$fieldname] = $fieldvalue;
181
			}
182
		}
183

    
184
		if (isset($id) && $a_pkg[$id])
185
			$a_pkg[$id] = $pkgarr;
186
		else
187
			$a_pkg[] = $pkgarr;
188
		write_config($pkg['addedit_string']);
189

    
190
		// late running code
191
		if($pkg['custom_add_php_command_late'] <> "") {
192
		    eval($pkg['custom_add_php_command_late']);
193
		}
194

    
195
		// resync the configuration file code if defined.
196
		if($pkg['custom_php_resync_config_command'] <> "") {
197
		    eval($pkg['custom_php_resync_config_command']);
198
		}
199

    
200
		parse_package_templates();
201

    
202
		/* if start_command is defined, restart w/ this */
203
		if($pkg['start_command'] <> "")
204
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
205

    
206
		/* if restart_command is defined, restart w/ this */
207
		if($pkg['restart_command'] <> "")
208
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
209

    
210
		if($pkg['aftersaveredirect'] <> "") {
211
		    pfSenseHeader($pkg['aftersaveredirect']);
212
		} elseif(!$pkg['adddeleteeditpagefields']) {
213
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
214
		} elseif(!$pkg['preoutput']) {
215
		    pfSenseHeader("pkg.php?xml=" . $xml);
216
		}
217
		exit;
218
	}
219
	else
220
		$get_from_post = true;
221
}
222

    
223
if($pkg['title'] <> "") {
224
	$edit = ($only_edit ? '' : ': Edit');
225
	$title = $pkg['title'] . $edit;
226
}
227
else
228
	$title = gettext("Package Editor");
229

    
230
$pgtitle = $title;
231
include("head.inc");
232

    
233
if ($pkg['custom_php_after_head_command'])
234
	eval($pkg['custom_php_after_head_command']);
235

    
236
?>
237

    
238
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
239
<?php if($pkg['fields']['field'] <> "") { ?>
240
<script language="JavaScript">
241
<!--
242
function enablechange() {
243
<?php
244
foreach ($pkg['fields']['field'] as $field) {
245
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
246
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
247

    
248
		if (isset($field['enablefields'])) {
249
			foreach (explode(',', $field['enablefields']) as $enablefield)
250
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
251
		}
252

    
253
		if (isset($field['checkenablefields'])) {
254
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield)
255
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
256
		}
257

    
258
		print("\t}\n\telse {\n");
259

    
260
		if (isset($field['enablefields'])) {
261
			foreach (explode(',', $field['enablefields']) as $enablefield)
262
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
263
		}
264

    
265
		if (isset($field['checkenablefields'])) {
266
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield)
267
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
268
		}
269

    
270
		print("\t}\n");
271
	}
272
}
273
?>
274
}
275
//-->
276
</script>
277
<?php } ?>
278
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
279
</script>
280

    
281
<?php include("fbegin.inc"); ?>
282
<p class="pgtitle"><?=$pgtitle?></p>
283
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
284
<form name="iform" action="pkg_edit.php" method="post">
285
<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
286
<script src="/javascript/scriptaculous/scriptaculous.js" type="text/javascript"></script>
287
<input type="hidden" name="xml" value="<?= $xml ?>">
288
<?php if ($savemsg) print_info_box($savemsg); ?>
289
<table width="100%" border="0" cellpadding="0" cellspacing="0">
290
<?php
291
if ($pkg['tabs'] <> "") {
292
    echo '<tr><td>';
293
    $tab_array = array();
294
    foreach($pkg['tabs']['tab'] as $tab) {
295
	if(isset($tab['active'])) {
296
		$active = true;
297
	} else {
298
		$active = false;
299
	}
300
	$urltmp = "";
301
	if($tab['url'] <> "") $urltmp = $tab['url'];
302
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
303

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

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

    
334
		if ($pkga['type'] == "listtopic") {
335
			echo "<td>&nbsp;</td>";
336
			echo "<tr><td colspan=\"2\" class=\"listtopic\">" . $pkga['name'] . "<br></td></tr>\n";
337
			continue;
338
	    }	
339

    
340
?>
341

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

    
344
	  <?php
345

    
346
	  $size = "";
347

    
348
	  if(!$pkga['dontdisplayname']) {
349
		unset($req);
350
		if (isset($pkga['required']))
351
			$req = 'req';
352
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
353
		echo fixup_string($pkga['fielddescr']);
354
		echo "</td>";
355
	  }
356

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

    
372
	      if($pkga['type'] == "input") {
373
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
374
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
375
			echo "<br>" . fixup_string($pkga['description']) . "\n";
376
	      } else if($pkga['type'] == "password") {
377
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
378
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
379
			echo "<br>" . fixup_string($pkga['description']) . "\n";
380
	      } else if($pkga['type'] == "select") {
381
                  $fieldname = $pkga['fieldname'];
382
                  if (isset($pkga['multiple'])) {
383
                    $multiple = 'multiple="multiple"';
384
                    $items = explode(',', $value);
385
                    $fieldname .= "[]";
386
                  }
387
                  else {
388
                    $multiple = '';
389
                    $items = array($value);
390
                  }
391
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
392
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
393

    
394
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
395
                  foreach ($pkga['options']['option'] as $opt) {
396
                      $selected = '';
397
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
398
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
399
                  }
400

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

    
469
			-->
470
			</script>
471

    
472
			<table name="maintable" id="maintable">
473
			<tr>
474
			<?php
475
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
476
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
477
				}
478
				echo "</tr>";
479

    
480
				echo "<tr>";
481
				  // XXX: traverse saved fields, add back needed rows.
482
				echo "</tr>";
483

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

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

    
552
					$rowcounter++;
553
				}
554
			?>
555

    
556
			  <tbody></tbody>
557
			</table>
558

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

    
570
		<?php
571
	      }
572
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
573
	     ?>
574

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

    
602
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
603

    
604
<?php include("fend.inc"); ?>
605
</body>
606
</html>
607

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

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

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

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

    
702
		/* replace $domain_total_rows with total rows */
703
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
704

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

    
742
		/* replace cr's */
743
		$template_text = str_replace("\\n", "\n", $template_text);
744

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

    
752
?>
(91-91/175)