summaryrefslogtreecommitdiffstats
path: root/usr/local/www/pkg_edit.php
blob: f7a5bc77e2fb45a87a63c3157fe104660c3aaa8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<?php
/* $Id$ */
/*
    pkg_edit.php
    Copyright (C) 2004 Scott Ullrich
    All rights reserved.

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

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

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

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

require_once("guiconfig.inc");
require_once("pkg-utils.inc");

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

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

// XXX: Make this input safe.
$xml = $_GET['xml'];
if($_POST['xml']) $xml = $_POST['xml'];

if($xml == "") {
            print_info_box_np(gettext("ERROR: No package defined."));
            die;
} else {
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
}

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

if (!isset($pkg['adddeleteeditpagefields']))
	$only_edit = true;
else
	$only_edit = false;

$package_name = $pkg['menu'][0]['name'];
$section      = $pkg['menu'][0]['section'];
$config_path  = $pkg['configpath'];
$name         = $pkg['name'];
$title        = $pkg['title'];
$pgtitle      = $title;

$id = $_GET['id'];
if (isset($_POST['id']))
	$id = $_POST['id'];

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

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

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

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

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

if ($_POST) {
	if($_POST['act'] == "del") {
		if($pkg['custom_delete_php_command']) {
		    if($pkg['custom_php_command_before_form'] <> "")
			    eval($pkg['custom_php_command_before_form']);
		    eval($pkg['custom_delete_php_command']);
		}
		write_config($pkg['delete_string']);
		// resync the configuration file code if defined.
		if($pkg['custom_php_resync_config_command'] <> "") {
			if($pkg['custom_php_command_before_form'] <> "")
				eval($pkg['custom_php_command_before_form']);
			eval($pkg['custom_php_resync_config_command']);
		}
	} else {
		if($pkg['custom_add_php_command']) {
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
			?>

<?php include("head.inc"); ?>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<?php
			}
			if($pkg['preoutput']) echo "<pre>";
			eval($pkg['custom_add_php_command']);
			if($pkg['preoutput']) echo "</pre>";
		}
	}

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

	$firstfield = "";
	$rows = 0;

	$input_errors = array();
	$reqfields = array();
	$reqfieldsn = array();
	foreach ($pkg['fields']['field'] as $field) {
		if (($field['type'] == 'input') && isset($field['required'])) {
			$reqfields[] = $field['fieldname'];
			$reqfieldsn[] = $field['fielddescr'];
		}
	}
	do_input_validation($_POST, $reqfields, $reqfieldsn, &$input_errors);

	if ($pkg['custom_php_validation_command'])
		eval($pkg['custom_php_validation_command']);

	// store values in xml configration file.
	if (!$input_errors) {
		$pkgarr = array();
		foreach ($pkg['fields']['field'] as $fields) {
			if($fields['type'] == "rowhelper") {
				// save rowhelper items.
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
				                         // XXX: this really is not helping embedded platforms.
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
						if($firstfield == "")  {
						  $firstfield = $rowhelperfield['fieldname'];
						} else {
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
						}
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
						eval($comd);
						if($value <> "") {
							$comd = "\$pkgarr['row'][" . $x . "]['" . $rowhelperfield['fieldname'] . "'] = \"" . $value . "\";";
							//echo($comd . "<br>");
							eval($comd);
						}
					}
				}
			} else {
				$fieldname  = $fields['fieldname'];
				$fieldvalue = $_POST[$fieldname];
				if (is_array($fieldvalue))
					$fieldvalue = implode(',', $fieldvalue);
				else {
					$fieldvalue = trim($fieldvalue);
					if ($fields['encoding'] == 'base64')
						$fieldvalue = base64_encode($fieldvalue);
				}
				$pkgarr[$fieldname] = $fieldvalue;
			}
		}

		if (isset($id) && $a_pkg[$id])
			$a_pkg[$id] = $pkgarr;
		else
			$a_pkg[] = $pkgarr;

		write_config($pkg['addedit_string']);

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

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

		parse_package_templates();

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

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

		if($pkg['aftersaveredirect'] <> "") {
		    pfSenseHeader($pkg['aftersaveredirect']);
		} elseif(!$pkg['adddeleteeditpagefields']) {
		    pfSenseHeader("pkg_edit.php?xml={$xml}&id=0");
		} elseif(!$pkg['preoutput']) {
		    pfSenseHeader("pkg.php?xml=" . $xml);
		}
		exit;
	}
	else
		$get_from_post = true;
}

if($pkg['title'] <> "") {
	$edit = ($only_edit ? '' : ': Edit');
	$title = $pkg['title'] . $edit;
}
else
	$title = gettext("Package Editor");

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

if ($pkg['custom_php_after_head_command'])
	eval($pkg['custom_php_after_head_command']);

?>

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

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

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

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

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

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

		print("\t}\n");
	}
}
?>
}
//-->
</script>
<?php } ?>
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
</script>

<?php include("fbegin.inc"); ?>
<?php if (!empty($input_errors)) print_input_errors($input_errors); ?>
<form name="iform" action="pkg_edit.php" method="post">
<script src="/javascript/scriptaculous/prototype.js" type="text/javascript"></script>
<script src="/javascript/scriptaculous/scriptaculous.js" type="text/javascript"></script>
<input type="hidden" name="xml" value="<?= $xml ?>">
<?php if ($savemsg) print_info_box($savemsg); ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php
if ($pkg['tabs'] <> "") {
    echo '<tr><td>';
    $tab_array = array();
    foreach($pkg['tabs']['tab'] as $tab) {
	if(isset($tab['active'])) {
		$active = true;
	} else {
		$active = false;
	}
	$urltmp = "";
	if($tab['url'] <> "") $urltmp = $tab['url'];
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];

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

	$tab_array[] = array(
				$tab['text'],
				$active,
				$url
			);
    }
    display_top_tabs($tab_array);
    echo '</td></tr>';
}
?>
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
  <?php
  $cols = 0;
  $savevalue = gettext("Save");
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
  foreach ($pkg['fields']['field'] as $pkga) { ?>

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

	  <?php

	  $size = "";

	  if(!$pkga['dontdisplayname']) {
		unset($req);
		if (isset($pkga['required']))
			$req = 'req';
		echo "<td width=\"22%\" class=\"vncell{$req}\">";
		echo fixup_string($pkga['fielddescr']);
		echo "</td>";
	  }

	  if(!$pkga['dontcombinecells'])
		echo "<td class=\"vtable\">";
		// if user is editing a record, load in the data.
		$fieldname = $pkga['fieldname'];
		if ($get_from_post) {
			$value = $_POST[$fieldname];
			if (is_array($value)) $value = implode(',', $value);
		}
		else {
			if (isset($id) && $a_pkg[$id])
				$value = $a_pkg[$id][$fieldname];
			else
				$value = $pkga['default_value'];
		}

	      if($pkga['type'] == "input") {
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
			echo "<input " . $size . " id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
			echo "<br>" . fixup_string($pkga['description']) . "\n";
	      } else if($pkga['type'] == "password") {
			echo "<input id='" . $pkga['fieldname'] . "' type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
			echo "<br>" . fixup_string($pkga['description']) . "\n";
	      } else if($pkga['type'] == "select") {
                  $fieldname = $pkga['fieldname'];
                  if (isset($pkga['multiple'])) {
                    $multiple = 'multiple="multiple"';
                    $items = explode(',', $value);
                    $fieldname .= "[]";
                  }
                  else {
                    $multiple = '';
                    $items = array($value);
                  }
                  $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : '');
                  $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : '');

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

                  print("</select>\n<br />\n" . fixup_string($pkga['description']) . "\n");
	      } else if($pkga['type'] == "vpn_selection") {
		    echo "<select id='" . $pkga['fieldname'] . "' name='" . $vpn['name'] . "'>\n";
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
		    }
		    echo "</select>\n";
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
	      } else if($pkga['type'] == "checkbox") {
			$checkboxchecked = "";
			if($value == "on") $checkboxchecked = " CHECKED";
			if (isset($pkga['enablefields']) || isset($pkga['checkenablefields']))
				$onclick = ' onclick="javascript:enablechange();"';
			echo "<input id='" . $pkga['fieldname'] . "' type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . $onclick . ">\n";
			echo "<br>" . fixup_string($pkga['description']) . "\n";
	      } else if($pkga['type'] == "textarea") {
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
                  if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value);
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
			echo "<br>" . fixup_string($pkga['description']) . "\n";
		  } else if($pkga['type'] == "interfaces_selection") {
			$size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : '');
			$multiple = '';
			$fieldname = $pkga['fieldname'];
			if (isset($pkga['multiple'])) {
				$fieldname .= '[]';
				$multiple = 'multiple';
			}
			print("<select id='" . $pkga['fieldname'] . "' name=\"$fieldname\" $size $multiple>\n");

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

			$additional_ifaces = $pkga['add_to_interfaces_selection'];
			if (!empty($additional_ifaces))
				$ifaces = array_merge($ifaces, explode(',', $additional_ifaces));
			if(is_array($value))
				$values = $value;
			else
				$values  =  explode(',',  $value);

			foreach($ifaces as $ifname => $iface) {
				if (isset($iface['descr']))
					$ifdescr = $iface['descr'];
				else
					$ifdescr = strtoupper($ifname);
				if ($ip = find_interface_ip($iface))
					$ip = " ($ip)";
				$selected = (in_array($ifname, $values) ? 'selected' : '');
				print("<option value=\"$ifname\" $selected>$ifdescr</option>\n");
			}

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

			<?php
				$rowcounter = 0;
				$fieldcounter = 0;
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
					$fieldcounter++;
				}
			?>

			-->
			</script>

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

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

				echo "<tr>\n";
				$rowcounter = 0;
				$trc = 0;
				if(isset($a_pkg[$id]['row'])) {
					foreach($a_pkg[$id]['row'] as $row) {
					/*
					 * loop through saved data for record if it exists, populating rowhelper
					 */
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
							$fieldname = $rowhelper['fieldname'];
							// if user is editing a record, load in the data.
							if (isset($id) && $a_pkg[$id]) {
								$value = $row[$fieldname];
							}
							$options = "";
							$type = $rowhelper['type'];
							$fieldname = $rowhelper['fieldname'];
							if($type == "option") $options = &$rowhelper['options']['option'];
							$size = "8";
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
							// javascript helpers for row_helper_dynamic.js
							echo "</td>\n";
							echo "<script language=\"JavaScript\">\n";
							echo "<!--\n";
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
							echo "-->\n";
							echo "</script>\n";
							$text = "";
							$trc++;
						}

						$rowcounter++;
						echo "<td>";
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"" . gettext("Delete") . "\">";
						echo "</td>\n";
						echo "</tr>\n";
					}
				}
				if($trc == 0) {
					/*
					 *  no records loaded.
                                         *  just show a generic line non-populated with saved data
                                         */
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
						$fieldname = $rowhelper['fieldname'];
						$options = "";
						$type = $rowhelper['type'];
						$fieldname = $rowhelper['fieldname'];
						if($type == "option") $options = &$rowhelper['options']['option'];
						$size = "8";
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
						// javascript helpers for row_helper_dynamic.js
						echo "</td>\n";
						echo "<script language=\"JavaScript\">\n";
						echo "<!--\n";
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
						echo "-->\n";
						echo "</script>\n";
						$text = "";
						$trc++;
					}

					$rowcounter++;
				}
			?>

			  <tbody></tbody>
			</table>

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

		<?php
	      }
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
	     ?>

      <?php
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
      $i++;
  }
 ?>
  <tr>
	<td>&nbsp;</td>
  </tr>
  <tr>
    <td width="22%" valign="top">&nbsp;</td>
    <td width="78%">
<?php
if($pkg['note'] != "")
	print("<p><span class=\"red\"><strong>" . gettext("Note") . ":</strong></span> {$pkg['note']}</p>");
//if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands
      print("<input name=\"id\" type=\"hidden\" value=\"$id\">");
?>
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>">
<?php if (!$only_edit): ?>
      <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
<?php endif; ?>
    </td>
  </tr>
</table>
</div></tr></td></table>
</form>

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

<?php include("fend.inc"); ?>
</body>
</html>

<?php
/*
 * ROW Helpers function
 */
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
	global $text;
	echo "<td>\n";
	if($type == "input") {
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' id='" . $fieldname . $trc . "' value='" . $value . "'>\n";
	} else if($type == "checkbox") {
		if($value)
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON' CHECKED>\n";
		else
			echo "<input size='" . $size . "' type='checkbox' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='ON'>\n";
	} else if($type == "password") {
		echo "<input size='" . $size . "' type='password' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
	} else if($type == "textarea") {
		echo "<textarea rows='2' cols='12' id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
	} else if($type == "select") {
		echo "<select id='" . $fieldname . $trc . "' name='" . $fieldname . $trc . "'>\n";
		foreach($rowhelper['options']['option'] as $rowopt) {
			$selected = "";
			if($rowopt['value'] == $value) $selected = " SELECTED";
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
		}
		echo "</select>\n";
	}
}

function fixup_string($string) {
	global $config;
	// fixup #1: $myurl -> http[s]://ip_address:port/
	$https = "";
	$port = $config['system']['webguiport'];
	if($port <> "443" and $port <> "80")
		$urlport = ":" . $port;
	else
		$urlport = "";

	if($config['system']['webguiproto'] == "https") $https = "s";
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
	$newstring = str_replace("\$myurl", $myurl, $string);
	$string = $newstring;
	// fixup #2: $wanip
	$curwanip = get_current_wan_address();
	$newstring = str_replace("\$wanip", $curwanip, $string);
	$string = $newstring;
	// fixup #3: $lanip
	$lancfg = $config['interfaces']['lan'];
	$lanip = $lancfg['ipaddr'];
	$newstring = str_replace("\$lanip", $lanip, $string);
	$string = $newstring;
	// fixup #4: fix'r'up here.
	return $newstring;
}

/*
 *  Parse templates if they are defined
 */
function parse_package_templates() {
	global $pkg, $config;
	$rows = 0;
	if($pkg['templates']['template'] <> "")
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
		$filename = $pkg_template_row['filename'];
		$template_text = $pkg_template_row['templatecontents'];
		$firstfield = "";
		/* calculate total row helpers count */
		foreach ($pkg['fields']['field'] as $fields) {
			if($fields['type'] == "rowhelper") {
				// save rowhelper items.
                                $row_helper_total_rows = 0;
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
						if($firstfield == "")  {
						  $firstfield = $rowhelperfield['fieldname'];
						} else {
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
						}
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
						$value = "";
						eval($comd);
						if($value <> "") {
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
						} else {
						    $row_helper_total_rows = $rows;
						    break;
						}
					}
				}
			}
		}

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

		/* change fields defined as fieldname_fieldvalue to their value */
		foreach ($pkg['fields']['field'] as $fields) {
			if($fields['type'] == "rowhelper") {
				// save rowhelper items.
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
					$row_helper_data = "";
					$isfirst = 0;
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
						if($firstfield == "")  {
						  $firstfield = $rowhelperfield['fieldname'];
						} else {
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
						}
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
						eval($comd);
						if($value <> "") {
						    if($isfirst == 1) $row_helper_data .= "  " ;
						    $row_helper_data .= $value;
						    $isfirst = 1;
						}
						$sep = "";
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
						foreach ($sep as $se) $seperator = $se;
						if($seperator <> "") {
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
						}
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
					}
				}
			} else {
				$fieldname  = $fields['fieldname'];
				$fieldvalue = $_POST[$fieldname];
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
			}
		}

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

		/* write out new template file */
		$fout = fopen($filename,"w");
		fwrite($fout, $template_text);
		fclose($fout);
	    }
}

?>
OpenPOWER on IntegriCloud