Project

General

Profile

Download (24.7 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
// XXX: Make this input safe.
43
$xml = $_GET['xml'];
44
if($_POST['xml']) $xml = $_POST['xml'];
45

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

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

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

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

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

    
73
if($pkg['custom_php_global_functions'] <> "")
74
        eval($pkg['custom_php_global_functions']);
75

    
76
// grab the installedpackages->package_name section.
77
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
78
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
79

    
80
$a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'];
81

    
82
if($_GET['savemsg'] <> "")
83
	$savemsg = $_GET['savemsg'];
84

    
85
if($pkg['custom_php_command_before_form'] <> "")
86
	eval($pkg['custom_php_command_before_form']);
87

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

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

    
118
	// donotsave is enabled.  lets simply exit.
119
	if($pkg['donotsave'] <> "") exit;
120

    
121
	$firstfield = "";
122
	$rows = 0;
123

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

    
135
	if ($pkg['custom_php_validation_command'])
136
		eval($pkg['custom_php_validation_command']);
137

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

    
175
		if (isset($id) && $a_pkg[$id])
176
			$a_pkg[$id] = $pkgarr;
177
		else
178
			$a_pkg[] = $pkgarr;
179

    
180
		write_config($pkg['addedit_string']);
181

    
182
		// late running code
183
		if($pkg['custom_add_php_command_late'] <> "") {
184
		    eval($pkg['custom_add_php_command_late']);
185
		}
186

    
187
		// resync the configuration file code if defined.
188
		if($pkg['custom_php_resync_config_command'] <> "") {
189
		    eval($pkg['custom_php_resync_config_command']);
190
		}
191

    
192
		parse_package_templates();
193

    
194
		/* if start_command is defined, restart w/ this */
195
		if($pkg['start_command'] <> "")
196
		    exec($pkg['start_command'] . ">/dev/null 2&>1");
197

    
198
		/* if restart_command is defined, restart w/ this */
199
		if($pkg['restart_command'] <> "")
200
		    exec($pkg['restart_command'] . ">/dev/null 2&>1");
201

    
202
		if($pkg['aftersaveredirect'] <> "") {
203
		    pfSenseHeader($pkg['aftersaveredirect']);
204
		} elseif(!$pkg['adddeleteeditpagefields']) {
205
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
206
		} elseif(!$pkg['preoutput']) {
207
		    pfSenseHeader("pkg.php?xml=" . $xml);
208
		}
209
		exit;
210
	}
211
	else
212
		$get_from_post = true;
213
}
214

    
215
if($pkg['title'] <> "") {
216
	$edit = ($only_edit ? '' : ': Edit');
217
	$title = $pkg['title'] . $edit;
218
}
219
else
220
	$title = gettext("Package Editor");
221

    
222
$pgtitle = $title;
223
include("head.inc");
224

    
225
if ($pkg['custom_php_after_head_command'])
226
	eval($pkg['custom_php_after_head_command']);
227

    
228
?>
229

    
230
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
231
<?php if($pkg['fields']['field'] <> "") { ?>
232
<script language="JavaScript">
233
<!--
234
function enablechange() {
235
<?php
236
foreach ($pkg['fields']['field'] as $field) {
237
	if (isset($field['enablefields']) or isset($field['checkenablefields'])) {
238
		print("\tif (document.iform.elements[\"{$field['fieldname']}\"].checked == false) {\n");
239

    
240
		if (isset($field['enablefields'])) {
241
			foreach (explode(',', $field['enablefields']) as $enablefield)
242
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 1;\n");
243
		}
244

    
245
		if (isset($field['checkenablefields'])) {
246
			foreach (explode(',', $field['checkenablefields']) as $checkenablefield)
247
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 0;\n");
248
		}
249

    
250
		print("\t}\n\telse {\n");
251

    
252
		if (isset($field['enablefields'])) {
253
			foreach (explode(',', $field['enablefields']) as $enablefield)
254
				print("\t\tdocument.iform.elements[\"$enablefield\"].disabled = 0;\n");
255
		}
256

    
257
		if (isset($field['checkenablefields'])) {
258
			foreach(explode(',', $field['checkenablefields']) as $checkenablefield)
259
				print("\t\tdocument.iform.elements[\"$checkenablefield\"].checked = 1;\n");
260
		}
261

    
262
		print("\t}\n");
263
	}
264
}
265
?>
266
}
267
//-->
268
</script>
269
<?php } ?>
270
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
271
</script>
272

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

    
296
 	$addresswithport = getenv("HTTP_HOST");
297
	$colonpos = strpos($addresswithport, ":");
298
	if ($colonpos !== False){
299
		//my url is actually just the IP address of the pfsense box
300
		$myurl = substr($addresswithport, 0, $colonpos);
301
	}
302
	else
303
	{
304
		$myurl = $addresswithport;
305
	}
306
	// eval url so that above $myurl item can be processed if need be.
307
	$url = str_replace('$myurl', $myurl, $urltmp);
308

    
309
	$tab_array[] = array(
310
				$tab['text'],
311
				$active,
312
				$url
313
			);
314
    }
315
    display_top_tabs($tab_array);
316
    echo '</td></tr>';
317
}
318
?>
319
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
320
  <?php
321
  $cols = 0;
322
  $savevalue = gettext("Save");
323
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
324
  foreach ($pkg['fields']['field'] as $pkga) { ?>
325

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

    
328
	  <?php
329

    
330
	  $size = "";
331

    
332
	  if(!$pkga['dontdisplayname']) {
333
		unset($req);
334
		if (isset($pkga['required']))
335
			$req = 'req';
336
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
337
		echo fixup_string($pkga['fielddescr']);
338
		echo "</td>";
339
	  }
340

    
341
	  if(!$pkga['dontcombinecells'])
342
		echo "<td class=\"vtable\">";
343
		// if user is editing a record, load in the data.
344
		$fieldname = $pkga['fieldname'];
345
		if ($get_from_post) {
346
			$value = $_POST[$fieldname];
347
			if (is_array($value)) $value = implode(',', $value);
348
		}
349
		else {
350
			if (isset($id) && $a_pkg[$id])
351
				$value = $a_pkg[$id][$fieldname];
352
			else
353
				$value = $pkga['default_value'];
354
		}
355

    
356
	      if($pkga['type'] == "input") {
357
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
358
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
359
			echo "<br>" . fixup_string($pkga['description']) . "\n";
360
	      } else if($pkga['type'] == "password") {
361
			echo "<input id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
362
			echo "<br>" . fixup_string($pkga['description']) . "\n";
363
	      } else if($pkga['type'] == "select") {
364
                  $fieldname = $pkga['fieldname'];
365
                  if (isset($pkga['multiple'])) {
366
                    $multiple = 'multiple="multiple"';
367
                    $items = explode(',', $value);
368
                    $fieldname .= "[]";
369
                  }
370
                  else {
371
                    $multiple = '';
372
                    $items = array($value);
373
                  }
374
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
375
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');
376

    
377
                  print("<select id='" . $pkga['fieldname'] . "' $multiple $size $onchange id=\"$fieldname\" name=\"$fieldname\">\n");
378
                  foreach ($pkga['options']['option'] as $opt) {
379
                      $selected = '';
380
                      if (in_array($opt['value'], $items)) $selected = 'selected="selected"';
381
                      print("\t<option name=\"{$opt['name']}\" value=\"{$opt['value']}\" $selected>{$opt['name']}</option>\n");
382
                  }
383

    
384
                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
385
	      } else if($pkga['type'] == "vpn_selection") {
386
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
387
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
388
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
389
		    }
390
		    echo "</select>\n";
391
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
392
	      } else if($pkga['type'] == "checkbox") {
393
			$checkboxchecked = "";
394
			if($value == "on") $checkboxchecked = " CHECKED";
395
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
396
				$onclick = ' onclick="javascript:enablechange();"';
397
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
398
			echo "<br>" . fixup_string($pkga['description']) . "\n";
399
	      } else if($pkga['type'] == "textarea") {
400
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
401
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
402
                  if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
403
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
404
			echo "<br>" . fixup_string($pkga['description']) . "\n";
405
		  } else if($pkga['type'] == "interfaces_selection") {
406
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
407
			$multiple = '';
408
			$fieldname = $pkga['fieldname'];
409
			if (isset($pkga['multiple'])) {
410
				$fieldname .= '[]';
411
				$multiple = 'multiple';
412
			}
413
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");
414

    
415
			if (isset($pkga['all_interfaces']))
416
				$ifaces = explode(' ', trim(shell_exec('ifconfig -l')));
417
			else
418
				$ifaces = $config['interfaces'];
419

    
420
			$additional_ifaces = $pkga['add_to_interfaces_selection'];
421
			if (!empty($additional_ifaces))
422
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
423
			if(is_array($value))
424
				$values = $value;
425
			else
426
				$values  =  explode(',',  $value);
427

    
428
			foreach($ifaces as $ifname => $iface) {
429
				if (isset($iface['descr']))
430
					$ifdescr = $iface['descr'];
431
				else
432
					$ifdescr = strtoupper($ifname);
433
				if ($ip = find_interface_ip($iface))
434
					$ip = " ($ip)";
435
				$selected = (in_array($ifname, $values) ? 'selected' : '');
436
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
437
			}
438

    
439
			print("</select>\n<br />" . fixup_string($pkga['description']) . "\n");
440
	      } else if($pkga['type'] == "radio") {
441
			echo "<input type='radio' id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
442
	      } else if($pkga['type'] == "rowhelper") {
443
		?>
444
			<script type="text/javascript" language='javascript'>
445
			<!--
446

    
447
			<?php
448
				$rowcounter = 0;
449
				$fieldcounter = 0;
450
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
451
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
452
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
453
					$fieldcounter++;
454
				}
455
			?>
456

    
457
			-->
458
			</script>
459

    
460
			<table name="maintable" id="maintable">
461
			<tr>
462
			<?php
463
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
464
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
465
				}
466
				echo "</tr>";
467

    
468
				echo "<tr>";
469
				  // XXX: traverse saved fields, add back needed rows.
470
				echo "</tr>";
471

    
472
				echo "<tr>\n";
473
				$rowcounter = 0;
474
				$trc = 0;
475
				if(isset($a_pkg[$id]['row'])) {
476
					foreach($a_pkg[$id]['row'] as $row) {
477
					/*
478
					 * loop through saved data for record if it exists, populating rowhelper
479
					 */
480
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
481
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
482
							$fieldname = $rowhelper['fieldname'];
483
							// if user is editing a record, load in the data.
484
							if (isset($id) && $a_pkg[$id]) {
485
								$value = $row[$fieldname];
486
							}
487
							$options = "";
488
							$type = $rowhelper['type'];
489
							$fieldname = $rowhelper['fieldname'];
490
							if($type == "option") $options = &$rowhelper['options']['option'];
491
							$size = "8";
492
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
493
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
494
							// javascript helpers for row_helper_dynamic.js
495
							echo "</td>\n";
496
							echo "<script language=\"JavaScript\">\n";
497
							echo "<!--\n";
498
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
499
							echo "-->\n";
500
							echo "</script>\n";
501
							$text = "";
502
							$trc++;
503
						}
504

    
505
						$rowcounter++;
506
						echo "<td>";
507
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
508
						echo "</td>\n";
509
						echo "</tr>\n";
510
					}
511
				}
512
				if($trc == 0) {
513
					/*
514
					 *  no records loaded.
515
                                         *  just show a generic line non-populated with saved data
516
                                         */
517
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
518
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
519
						$fieldname = $rowhelper['fieldname'];
520
						$options = "";
521
						$type = $rowhelper['type'];
522
						$fieldname = $rowhelper['fieldname'];
523
						if($type == "option") $options = &$rowhelper['options']['option'];
524
						$size = "8";
525
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
526
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
527
						// javascript helpers for row_helper_dynamic.js
528
						echo "</td>\n";
529
						echo "<script language=\"JavaScript\">\n";
530
						echo "<!--\n";
531
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
532
						echo "-->\n";
533
						echo "</script>\n";
534
						$text = "";
535
						$trc++;
536
					}
537

    
538
					$rowcounter++;
539
				}
540
			?>
541

    
542
			  <tbody></tbody>
543
			</table>
544

    
545
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif"></a>
546
		<script language="JavaScript">
547
		<!--
548
		field_counter_js = <?= $fieldcounter ?>;
549
		rows = <?= $rowcounter ?>;
550
		totalrows = <?php echo $rowcounter; ?>;
551
		loaded = <?php echo $rowcounter; ?>;
552
		//typesel_change();
553
		//-->
554
		</script>
555

    
556
		<?php
557
	      }
558
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
559
	     ?>
560

    
561
      <?php
562
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
563
      $i++;
564
  }
565
 ?>
566
  <tr>
567
	<td>&nbsp;</td>
568
  </tr>
569
  <tr>
570
    <td width="22%" valign="top">&nbsp;</td>
571
    <td width="78%">
572
<?php
573
if($pkg['note'] != "")
574
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
575
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
576
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
577
?>
578
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
579
<?php if (!$only_edit): ?>
580
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
581
<?php endif; ?>
582
    </td>
583
  </tr>
584
</table>
585
</div></tr></td></table>
586
</form>
587

    
588
<?php if ($pkg['custom_php_after_form_command']) eval($pkg['custom_php_after_form_command']); ?>
589

    
590
<?php include("fend.inc"); ?>
591
</body>
592
</html>
593

    
594
<?php
595
/*
596
 * ROW Helpers function
597
 */
598
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
599
	global $text;
600
	echo "<td>\n";
601
	if($type == "input") {
602
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
603
	} else if($type == "checkbox") {
604
		if($value)
605
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
606
		else
607
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
608
	} else if($type == "password") {
609
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
610
	} else if($type == "textarea") {
611
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
612
	} else if($type == "select") {
613
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
614
		foreach($rowhelper['options']['option'] as $rowopt) {
615
			$selected = "";
616
			if($rowopt['value'] == $value) $selected = " SELECTED";
617
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
618
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
619
		}
620
		echo "</select>\n";
621
	}
622
}
623

    
624
function fixup_string($string) {
625
	global $config;
626
	// fixup #1: $myurl -> http[s]://ip_address:port/
627
	$https = "";
628
	$port = $config['system']['webguiport'];
629
	if($port <> "443" and $port <> "80")
630
		$urlport = ":" . $port;
631
	else
632
		$urlport = "";
633

    
634
	if($config['system']['webguiproto'] == "https") $https = "s";
635
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
636
	$newstring = str_replace("\$myurl", $myurl, $string);
637
	$string = $newstring;
638
	// fixup #2: $wanip
639
	$curwanip = get_current_wan_address();
640
	$newstring = str_replace("\$wanip", $curwanip, $string);
641
	$string = $newstring;
642
	// fixup #3: $lanip
643
	$lancfg = $config['interfaces']['lan'];
644
	$lanip = $lancfg['ipaddr'];
645
	$newstring = str_replace("\$lanip", $lanip, $string);
646
	$string = $newstring;
647
	// fixup #4: fix'r'up here.
648
	return $newstring;
649
}
650

    
651
/*
652
 *  Parse templates if they are defined
653
 */
654
function parse_package_templates() {
655
	global $pkg, $config;
656
	$rows = 0;
657
	if($pkg['templates']['template'] <> "")
658
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
659
		$filename = $pkg_template_row['filename'];
660
		$template_text = $pkg_template_row['templatecontents'];
661
		$firstfield = "";
662
		/* calculate total row helpers count */
663
		foreach ($pkg['fields']['field'] as $fields) {
664
			if($fields['type'] == "rowhelper") {
665
				// save rowhelper items.
666
                                $row_helper_total_rows = 0;
667
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
668
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
669
						if($firstfield == "")  {
670
						  $firstfield = $rowhelperfield['fieldname'];
671
						} else {
672
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
673
						}
674
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
675
						$value = "";
676
						eval($comd);
677
						if($value <> "") {
678
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
679
						} else {
680
						    $row_helper_total_rows = $rows;
681
						    break;
682
						}
683
					}
684
				}
685
			}
686
		}
687

    
688
		/* replace $domain_total_rows with total rows */
689
		$template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text);
690

    
691
		/* change fields defined as fieldname_fieldvalue to their value */
692
		foreach ($pkg['fields']['field'] as $fields) {
693
			if($fields['type'] == "rowhelper") {
694
				// save rowhelper items.
695
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
696
					$row_helper_data = "";
697
					$isfirst = 0;
698
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
699
						if($firstfield == "")  {
700
						  $firstfield = $rowhelperfield['fieldname'];
701
						} else {
702
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
703
						}
704
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
705
						eval($comd);
706
						if($value <> "") {
707
						    if($isfirst == 1) $row_helper_data .= "  " ;
708
						    $row_helper_data .= $value;
709
						    $isfirst = 1;
710
						}
711
						$sep = "";
712
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
713
						foreach ($sep as $se) $seperator = $se;
714
						if($seperator <> "") {
715
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
716
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
717
						}
718
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
719
					}
720
				}
721
			} else {
722
				$fieldname  = $fields['fieldname'];
723
				$fieldvalue = $_POST[$fieldname];
724
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
725
			}
726
		}
727

    
728
		/* replace cr's */
729
		$template_text = str_replace("\\n", "\n", $template_text);
730

    
731
		/* write out new template file */
732
		$fout = fopen($filename,"w");
733
		fwrite($fout, $template_text);
734
		fclose($fout);
735
	    }
736
}
737

    
738
?>
(94-94/186)