Project

General

Profile

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

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

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

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

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

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

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

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

    
43
if($xml == "") {
44
            print_info_box_np("ERROR: No package defined.");
45
            die;
46
} else {
47
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
48
}
49
$package_name = $pkg['menu'][0]['name'];
50
$section      = $pkg['menu'][0]['section'];
51
$config_path  = $pkg['configpath'];
52
$name         = $pkg['name'];
53
$title        = $section . ": " . $package_name;
54
$pgtitle      = $title;
55

    
56
$id = $_GET['id'];
57
if (isset($_POST['id']))
58
	$id = $_POST['id'];
59

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

    
63
// grab the installedpackages->package_name section.
64
$a_pkg = &$config['installedpackages'][$name]['config'];
65

    
66
if(!is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']))
67
	$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array();
68

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

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

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

    
77
if ($_POST) {
78
	if($_POST['act'] == "del") {
79
		if($pkg['custom_delete_php_command']) {
80
		    if($pkg['custom_php_command_before_form'] <> "")
81
			    eval($pkg['custom_php_command_before_form']);
82
		    eval($pkg['custom_delete_php_command']);
83
		}
84
		write_config();
85
		// resync the configuration file code if defined.
86
		if($pkg['custom_php_resync_config_command'] <> "") {
87
			if($pkg['custom_php_command_before_form'] <> "")
88
				eval($pkg['custom_php_command_before_form']);
89
			eval($pkg['custom_php_resync_config_command']);
90
		}
91
	} else {
92
		if($pkg['custom_add_php_command']) {
93
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
94
			?>
95

    
96
<?
97
/* 
98
 *   if user has selected a custom template, use it.
99
 *   otherwise default to pfsense tempalte
100
 */
101
if($config['template'] <> "")
102
	$g['template'] = $config['template'];
103
else
104
	$g['template'] = "pfsense";
105

    
106
?>
107

    
108
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
109
<html>
110
<head>
111
	<title><?=$pgtitle;?></title>
112
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
113
	<link rel="stylesheet" href="/templates/<?php echo $g['template']; ?>/all.css" media="all" />
114
	<link rel="stylesheet" type="text/css" href="/niftycssprintCode.css" media="print" />
115
	<script type="text/javascript" src="/niftyjsCode.js"></script>
116
</head>
117

    
118
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
119
<?php include("fbegin.inc"); ?>
120
<p class="pgtitle"><?=$pgtitle?></p>
121
				<?php
122
			}
123
			if($pkg['preoutput']) echo "<pre>";
124
			eval($pkg['custom_add_php_command']);
125
			if($pkg['preoutput']) echo "</pre>";
126
		}
127
	}
128

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

    
132
	$firstfield = "";
133
	$rows = 0;
134

    
135
	// store values in xml configration file.
136
	if (!$input_errors) {
137
		$pkgarr = array();
138
		foreach ($pkg['fields']['field'] as $fields) {
139
			if($fields['type'] == "rowhelper") {
140
				// save rowhelper items.
141
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
142
				                         // XXX: this really is not helping embedded platforms.
143
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
144
						if($firstfield == "")  {
145
						  $firstfield = $rowhelperfield['fieldname'];
146
						} else {
147
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
148
						}
149
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
150
						//echo($comd . "<br>");
151
						eval($comd);
152
						if($value <> "") {
153
							$comd = "\$pkgarr['row'][" . $x . "]['" . $rowhelperfield['fieldname'] . "'] = \"" . $value . "\";";
154
							//echo($comd . "<br>");
155
							eval($comd);
156
						}
157
					}
158
				}
159
			} else {
160
				// simply loop through all field names looking for posted
161
				// values matching the fieldnames.  if found, save to package
162
				// configuration area.
163
				$fieldname  = $fields['fieldname'];
164
				$fieldvalue = $_POST[$fieldname];
165
				$pkgarr[$fieldname] = $fieldvalue;
166
			}
167
		}
168

    
169
		if (isset($id) && $a_pkg[$id])
170
			$a_pkg[$id] = $pkgarr;
171
		else
172
			$a_pkg[] = $pkgarr;
173

    
174
		write_config();
175

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

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

    
186
		parse_package_templates();
187

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

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

    
196
		if($pkg['aftersaveredirect'] <> "") {
197
		    header("Location:  " . $pkg['aftersaveredirect']);
198
		} else {
199
		    if(!$pkg['preoutput'])
200
			header("Location:  pkg.php?xml=" . $xml);
201
		}
202
		exit;
203
	}
204
}
205

    
206
if($pkg['title'] <> "")
207
	$title = $pkg['title'];
208
else
209
	$title = "Package Editor";
210

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

    
214
?>
215

    
216
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
217
<?php if($pkg['fields']['field'] <> "") { ?>
218
<script language="JavaScript">
219
<!--
220
function enablechange() {
221
<?php               
222
        foreach($pkg['fields']['field'] as $field) {
223
                if(isset($field['enablefields']) or isset($field['checkenablefields'])) {
224
                        print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
225
                        if(isset($field['enablefields'])) {
226
                                $enablefields = explode(',', $field['enablefields']);
227
                                foreach($enablefields as $enablefield) {
228
                                        $enablefield = strtolower($enablefield);
229
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 1;' . "\n";
230
                                }   
231
                        }
232
                        if(isset($field['checkenablefields'])) {
233
                                $checkenablefields = explode(',', $field['checkenablefields']);
234
                                foreach($checkenablefields as $checkenablefield) {
235
                                        $checkenablefield = strtolower($checkenablefield);
236
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 0;' . "\n";
237
                                }
238
                        }
239
                        print "\t" . '} else {' . "\n";
240
                        if(isset($field['enablefields'])) { 
241
                                foreach($enablefields as $enablefield) {
242
                                        $enablefield = strtolower($enablefield);
243
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 0;' . "\n";
244
                                }
245
                        }
246
                        if(isset($field['checkenablefields'])) {
247
                                foreach($checkenablefields as $checkenablefield) {
248
                                        $checkenablefield = strtolower($checkenablefield);
249
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 1;' . "\n";
250
                                }
251
                        }
252
                        print "\t" . '}' . "\n";
253
                }
254
        }
255
?>
256
}
257
//-->
258
</script>
259
<?php } ?>
260
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
261
</script>
262

    
263
<?php include("fbegin.inc"); ?>
264
<p class="pgtitle"><?=$pgtitle?></p>
265
<form action="pkg_edit.php" method="post">
266
<input type="hidden" name="xml" value="<?= $xml ?>">
267
<?php if ($savemsg) print_info_box($savemsg); ?>
268
<table width="100%" border="0" cellpadding="0" cellspacing="0">
269
<?php
270
if ($pkg['tabs'] <> "") {
271
    echo '<tr><td>';
272
    $tab_array = array();
273
    foreach($pkg['tabs']['tab'] as $tab) {
274
	if(isset($tab['active'])) {
275
		$active = true;
276
	} else {
277
		$active = false;
278
	}
279
	$urltmp = "";
280
	if($tab['url'] <> "") $urltmp = $tab['url'];
281
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
282

    
283
	$myurl = getenv("HTTP_HOST");
284
	// eval url so that above $myurl item can be processed if need be.
285
	$url = str_replace('$myurl', $myurl, $urltmp);
286
	$tab_array[] = array(
287
				$tab['text'],
288
				$active,
289
				$url
290
			);
291
    }
292
    display_top_tabs($tab_array);
293
    echo '</td></tr>';
294
}
295
?>
296
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
297
  <?php
298
  $cols = 0;
299
  $savevalue = "Save";
300
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
301
  foreach ($pkg['fields']['field'] as $pkga) { ?>
302
	  
303
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
304

    
305
	  <?php
306
	  
307
	  $size = "";
308
	  
309
	  if(!$pkga['dontdisplayname']) {
310
		echo "<td width=\"22%\" class=\"vncellreq\">";
311
		echo fixup_string($pkga['fielddescr']);
312
		echo "</td>";
313
	  }
314

    
315
	  if(!$pkga['dontcombinecells'])
316
		echo "<td class=\"vtable\">";
317

    
318
		// if user is editing a record, load in the data.
319
		if (isset($id) && $a_pkg[$id]) {
320
			$fieldname = $pkga['fieldname'];
321
			$value = $a_pkg[$id][$fieldname];
322
		}
323

    
324
	      if($pkga['type'] == "input") {
325
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
326
			echo "<input " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
327
			echo "<br>" . fixup_string($pkga['description']) . "\n";
328
	      } else if($pkga['type'] == "password") {
329
			echo "<input type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
330
			echo "<br>" . fixup_string($pkga['description']) . "\n";
331
	      } else if($pkga['type'] == "select") {
332
		  // XXX: TODO: set $selected
333
                  if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
334
		  if($pkga['multiple'] == "yes") $multiple = "MULTIPLE ";
335
		    echo "<select " . $multiple . $size . "id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "'>\n";
336
		    foreach ($pkga['options']['option'] as $opt) {
337
			  $selected = "";
338
			  if($opt['value'] == $value) $selected = " SELECTED";
339
			  echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
340
		    }
341
		    echo "</select>\n";
342
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
343
	      } else if($pkga['type'] == "vpn_selection") {
344
		    echo "<select name='" . $vpn['name'] . "'>\n";
345
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
346
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
347
		    }
348
		    echo "</select>\n";
349
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
350
	      } else if($pkga['type'] == "checkbox") {
351
			$checkboxchecked = "";
352
			if($value == "on") $checkboxchecked = " CHECKED";
353
			echo "<input type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . ">\n";
354
			echo "<br>" . fixup_string($pkga['description']) . "\n";
355
	      } else if($pkga['type'] == "textarea") {
356
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
357
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
358
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
359
			echo "<br>" . fixup_string($pkga['description']) . "\n";
360
		  } else if($pkga['type'] == "interfaces_selection") {
361
			$size = "";
362
			$multiple = "";
363
			$fieldname = $pkga['fieldname'];
364
			if($pkga['size'] <> "") $size = " size=\"" . $pkga['size'] . "\"";
365
			if($pkga['multiple'] <> "" and $pkga['multiple'] <> "0") {
366
			  $multiple = " multiple=\"multiple\"";
367
			  $fieldname .= "[]";
368
			}
369
			echo "<select name='" . $fieldname . "'" . $size . $multiple . ">\n";
370
			if($pkga['add_to_interfaces_selection'] <> "") {
371
				$SELECTED = "";
372
				if($pkga['add_to_interfaces_selection'] == $value) $SELECTED = " SELECTED";
373
				echo "<option value='" . $pkga['add_to_interfaces_selection'] . "'" . $SELECTED . ">" . $pkga['add_to_interfaces_selection'] . "</option>\n";
374
			}
375
			$interfaces = &$config['interfaces'];
376
			if($pkga['all_interfaces'] <> "") {
377
				$ints = split(" ", `/sbin/ifconfig -l`);
378
				$interfaces = array();
379
				foreach ($ints as $int) {
380
					$interfaces[]['descr'] = $int;
381
					$interfaces[] = $int;
382
				}
383
			}
384
			foreach ($interfaces as $ifname => $iface) {
385
			  if ($iface['descr'])
386
				  $ifdescr = $iface['descr'];
387
			  else
388
				  $ifdescr = strtoupper($ifname);
389
			  $ifname = $iface['descr'];
390
			  $ip = "";
391
			  if($pkga['all_interfaces'] <> "") {
392
				$ifdescr = $iface;
393
				$ip = " " . find_interface_ip($iface);
394
			  }
395
			  $SELECTED = "";
396
			  if($value == $ifdescr) $SELECTED = " SELECTED";
397
			  $to_echo =  "<option value='" . $ifdescr . "'" . $SELECTED . ">" . $ifdescr . $ip . "</option>\n";
398
			  $to_echo .= "<!-- {$value} -->";
399
			  $to_echo .= "<!-- {$iface} -->";
400
			  $canecho = 0;
401
			  if($pkga['interface_filter'] <> "") {
402
				if(stristr($iface, $pkga['interface_filter']) == true)
403
					$canecho = 1;
404
			  } else {
405
				$canecho = 1;
406
			  }
407
			  if($canecho == 1) 
408
				echo $to_echo;
409
			}
410
			echo "</select>\n<br>" . fixup_string($pkga['description']) . "\n";
411
	      } else if($pkga['type'] == "radio") {
412
			echo "<input type='radio' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
413
	      } else if($pkga['type'] == "rowhelper") {
414
		?>
415
			<script type="text/javascript" language='javascript'>
416
			<!--
417

    
418
			<?php
419
				$rowcounter = 0;
420
				$fieldcounter = 0;
421
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
422
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
423
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
424
					$fieldcounter++;
425
				}
426
			?>
427

    
428
			-->
429
			</script>
430

    
431
			<table name="maintable" id="maintable">
432
			<tr>
433
			<?php
434
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
435
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
436
				}
437
				echo "</tr>";
438
				echo "<tbody>";
439

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

    
444
				echo "<tr>\n";
445
				$rowcounter = 0;
446
				$trc = 0;
447
				if(isset($a_pkg[$id]['row'])) {
448
					foreach($a_pkg[$id]['row'] as $row) {
449
					/*
450
					 * loop through saved data for record if it exists, populating rowhelper
451
					 */
452
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
453
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
454
							$fieldname = $rowhelper['fieldname'];
455
							// if user is editing a record, load in the data.
456
							if (isset($id) && $a_pkg[$id]) {
457
								$value = $row[$fieldname];
458
							}
459
							$options = "";
460
							$type = $rowhelper['type'];
461
							$fieldname = $rowhelper['fieldname'];
462
							if($type == "option") $options = &$rowhelper['options']['option'];
463
							$size = "8";
464
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
465
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
466
							// javascript helpers for row_helper_dynamic.js
467
							echo "</td>\n";
468
							echo "<script language=\"JavaScript\">\n";
469
							echo "<!--\n";
470
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
471
							echo "-->\n";
472
							echo "</script>\n";
473
							$text = "";
474
							$trc++;
475
						}
476

    
477
						$rowcounter++;
478
						echo "<td>";
479
						echo "<input type=\"image\" src=\"/x.gif\" onclick=\"removeRow(this); return false;\" value=\"Delete\">";
480
						echo "</td>\n";
481
						echo "</tr>\n";
482
					}
483
				}
484
				if($trc == 0) {
485
					/*
486
					 *  no records loaded.
487
                                         *  just show a generic line non-populated with saved data
488
                                         */
489
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
490
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
491
						$fieldname = $rowhelper['fieldname'];
492
						$options = "";
493
						$type = $rowhelper['type'];
494
						$fieldname = $rowhelper['fieldname'];
495
						if($type == "option") $options = &$rowhelper['options']['option'];
496
						$size = "8";
497
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
498
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
499
						// javascript helpers for row_helper_dynamic.js
500
						echo "</td>\n";
501
						echo "<script language=\"JavaScript\">\n";
502
						echo "<!--\n";
503
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
504
						echo "-->\n";
505
						echo "</script>\n";
506
						$text = "";
507
						$trc++;
508
					}
509

    
510
					$rowcounter++;
511
				}
512
			?>
513

    
514
			  </tbody>
515
			</table>
516

    
517
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="/plus.gif"></a>
518
		<script language="JavaScript">
519
		<!--
520
		field_counter_js = <?= $fieldcounter ?>;
521
		rows = <?= $rowcounter ?>;
522
		totalrows = <?php echo $rowcounter; ?>;
523
		loaded = <?php echo $rowcounter; ?>;
524
		//typesel_change();
525
		//-->
526
		</script>
527

    
528
		<?php
529
	      }
530
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
531
	     ?>
532

    
533
      <?php
534
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
535
      $i++;
536
  }
537
 ?>
538
  <tr>
539
	<td>&nbsp;</td>
540
  </tr>
541
  <tr>
542
    <td width="22%" valign="top">&nbsp;</td>
543
    <td width="78%">
544
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>"> <input class="formbtn" type="button" value="cancel" onclick="history.back()">
545
      <?php if (isset($id) && $a_pkg[$id]): ?>
546
      <input name="id" type="hidden" value="<?=$id;?>">
547
      <?php endif; ?>
548
    </td>
549
  </tr>
550
</table>
551
</div></tr></td></table>
552

    
553
<?php if($pkga['note'] <> "") echo "<br><center>" . $pkga['note'] . "</center>"; ?>
554

    
555
</form>
556
<?php include("fend.inc"); ?>
557
</body>
558
</html>
559

    
560
<?php
561

    
562
/*
563
 * ROW Helpers function
564
 */
565
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
566
	global $text;
567
	echo "<td>\n";
568
	if($type == "input") {
569
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
570
	} else if($type == "password") {
571
		echo "<input size='" . $size . "' type='password' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
572
	} else if($type == "textarea") {
573
		echo "<textarea rows='2' cols='12' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
574
	} else if($type == "select") {
575
		echo "<select name='" . $fieldname . $trc . "'>\n";
576
		foreach($rowhelper['options']['option'] as $rowopt) {
577
			$selected = "";
578
			if($rowopt['value'] == $value) $selected = " SELECTED";
579
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
580
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
581
		}
582
		echo "</select>\n";
583
	}
584
}
585

    
586
function fixup_string($string) {
587
	global $config;
588
	// fixup #1: $myurl -> http[s]://ip_address:port/
589
	$https = "";
590
	$port = $config['system']['webguiport'];
591
	if($port <> "443" and $port <> "80")
592
		$urlport = ":" . $port;
593
	else
594
		$urlport = "";
595
		
596
	if($config['system']['webguiproto'] == "https") $https = "s";
597
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
598
	$newstring = str_replace("\$myurl", $myurl, $string);
599
	$string = $newstring;
600
	// fixup #2: $wanip
601
	$curwanip = get_current_wan_address();
602
	$newstring = str_replace("\$wanip", $curwanip, $string);
603
	$string = $newstring;
604
	// fixup #3: $lanip
605
	$lancfg = $config['interfaces']['lan'];
606
	$lanip = $lancfg['ipaddr'];
607
	$newstring = str_replace("\$lanip", $lanip, $string);
608
	$string = $newstring;
609
	// fixup #4: fix'r'up here.
610
	return $newstring;
611
}
612

    
613
/*
614
 *  Parse templates if they are defined
615
 */
616
function parse_package_templates() {
617
	global $pkg, $config;
618
	if($pkg['templates']['template'] <> "")
619
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
620
		$filename = $pkg_template_row['filename'];
621
		$template_text = $pkg_template_row['templatecontents'];
622

    
623
		/* calculate total row helpers count */
624
		foreach ($pkg['fields']['field'] as $fields) {
625
			if($fields['type'] == "rowhelper") {
626
				// save rowhelper items.
627
                                $row_helper_total_rows = 0;
628
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
629
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
630
						if($firstfield == "")  {
631
						  $firstfield = $rowhelperfield['fieldname'];
632
						} else {
633
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
634
						}
635
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
636
						eval($comd);
637
						if($value <> "") {
638
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
639
						} else {
640
						    $row_helper_total_rows = $rows;
641
						    break;
642
						}
643
					}
644
				}
645
			}
646
		}
647

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

    
651
		/* change fields defined as fieldname_fieldvalue to their value */
652
		foreach ($pkg['fields']['field'] as $fields) {
653
			if($fields['type'] == "rowhelper") {
654
				// save rowhelper items.
655
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
656
					$row_helper_data = "";
657
					$isfirst = 0;
658
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
659
						if($firstfield == "")  {
660
						  $firstfield = $rowhelperfield['fieldname'];
661
						} else {
662
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
663
						}
664
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
665
						eval($comd);
666
						if($value <> "") {
667
						    if($isfirst == 1) $row_helper_data .= "  " ;
668
						    $row_helper_data .= $value;
669
						    $isfirst = 1;
670
						}
671
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
672
						foreach ($sep as $se) $seperator = $se;
673
						if($seperator <> "") {
674
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
675
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
676
						}
677
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
678
					}
679
				}
680
			} else {
681
				$fieldname  = $fields['fieldname'];
682
				$fieldvalue = $_POST[$fieldname];
683
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
684
			}
685
		}
686

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

    
690
		/* write out new template file */
691
		$fout = fopen($filename,"w");
692
		fwrite($fout, $template_text);
693
		fclose($fout);
694
	    }
695
}
696

    
697
?>
(71-71/133)