Project

General

Profile

Download (23.3 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
<?php include("head.inc"); ?>
97
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
98
<?php include("fbegin.inc"); ?>
99
<p class="pgtitle"><?=$pgtitle?></p>
100
				<?php
101
			}
102
			if($pkg['preoutput']) echo "<pre>";
103
			eval($pkg['custom_add_php_command']);
104
			if($pkg['preoutput']) echo "</pre>";
105
		}
106
	}
107

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

    
111
	$firstfield = "";
112
	$rows = 0;
113

    
114
	// store values in xml configration file.
115
	if (!$input_errors) {
116
		$pkgarr = array();
117
		foreach ($pkg['fields']['field'] as $fields) {
118
			if($fields['type'] == "rowhelper") {
119
				// save rowhelper items.
120
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
121
				                         // XXX: this really is not helping embedded platforms.
122
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
123
						if($firstfield == "")  {
124
						  $firstfield = $rowhelperfield['fieldname'];
125
						} else {
126
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
127
						}
128
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
129
						//echo($comd . "<br>");
130
						eval($comd);
131
						if($value <> "") {
132
							$comd = "\$pkgarr['row'][" . $x . "]['" . $rowhelperfield['fieldname'] . "'] = \"" . $value . "\";";
133
							//echo($comd . "<br>");
134
							eval($comd);
135
						}
136
					}
137
				}
138
			} else {
139
				// simply loop through all field names looking for posted
140
				// values matching the fieldnames.  if found, save to package
141
				// configuration area.
142
				$fieldname  = $fields['fieldname'];
143
				$fieldvalue = $_POST[$fieldname];
144
				$pkgarr[$fieldname] = $fieldvalue;
145
			}
146
		}
147

    
148
		if (isset($id) && $a_pkg[$id])
149
			$a_pkg[$id] = $pkgarr;
150
		else
151
			$a_pkg[] = $pkgarr;
152

    
153
		write_config();
154

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

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

    
165
		parse_package_templates();
166

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

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

    
175
		if($pkg['aftersaveredirect'] <> "") {
176
		    header("Location:  " . $pkg['aftersaveredirect']);
177
		} elseif(!$pkg['adddeleteeditpagefields']) {
178
		    header("Location:  pkg_edit.php?xml={$xml}&id=0");
179
		} elseif(!$pkg['preoutput']) {
180
		    header("Location:  pkg.php?xml=" . $xml);
181
		}
182
		exit;
183
	}
184
}
185

    
186
if($pkg['title'] <> "")
187
	$title = $pkg['title'];
188
else
189
	$title = "Package Editor";
190

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

    
194
?>
195

    
196
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" onLoad="enablechange();">
197
<?php if($pkg['fields']['field'] <> "") { ?>
198
<script language="JavaScript">
199
<!--
200
function enablechange() {
201
<?php               
202
        foreach($pkg['fields']['field'] as $field) {
203
                if(isset($field['enablefields']) or isset($field['checkenablefields'])) {
204
                        print "\t" . 'if (document.iform.' . strtolower($field['name']) . '.checked == false) {' . "\n";
205
                        if(isset($field['enablefields'])) {
206
                                $enablefields = explode(',', $field['enablefields']);
207
                                foreach($enablefields as $enablefield) {
208
                                        $enablefield = strtolower($enablefield);
209
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 1;' . "\n";
210
                                }   
211
                        }
212
                        if(isset($field['checkenablefields'])) {
213
                                $checkenablefields = explode(',', $field['checkenablefields']);
214
                                foreach($checkenablefields as $checkenablefield) {
215
                                        $checkenablefield = strtolower($checkenablefield);
216
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 0;' . "\n";
217
                                }
218
                        }
219
                        print "\t" . '} else {' . "\n";
220
                        if(isset($field['enablefields'])) { 
221
                                foreach($enablefields as $enablefield) {
222
                                        $enablefield = strtolower($enablefield);
223
                                        print "\t\t" . 'document.iform.' . $enablefield . '.disabled = 0;' . "\n";
224
                                }
225
                        }
226
                        if(isset($field['checkenablefields'])) {
227
                                foreach($checkenablefields as $checkenablefield) {
228
                                        $checkenablefield = strtolower($checkenablefield);
229
                                        print "\t\t" . 'document.iform.' . $checkenablefield . '.checked = 1;' . "\n";
230
                                }
231
                        }
232
                        print "\t" . '}' . "\n";
233
                }
234
        }
235
?>
236
}
237
//-->
238
</script>
239
<?php } ?>
240
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
241
</script>
242

    
243
<?php include("fbegin.inc"); ?>
244
<p class="pgtitle"><?=$pgtitle?></p>
245
<form action="pkg_edit.php" method="post">
246
<input type="hidden" name="xml" value="<?= $xml ?>">
247
<?php if ($savemsg) print_info_box($savemsg); ?>
248
<table width="100%" border="0" cellpadding="0" cellspacing="0">
249
<?php
250
if ($pkg['tabs'] <> "") {
251
    echo '<tr><td>';
252
    $tab_array = array();
253
    foreach($pkg['tabs']['tab'] as $tab) {
254
	if(isset($tab['active'])) {
255
		$active = true;
256
	} else {
257
		$active = false;
258
	}
259
	$urltmp = "";
260
	if($tab['url'] <> "") $urltmp = $tab['url'];
261
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
262

    
263
	$myurl = getenv("HTTP_HOST");
264
	// eval url so that above $myurl item can be processed if need be.
265
	$url = str_replace('$myurl', $myurl, $urltmp);
266
	$tab_array[] = array(
267
				$tab['text'],
268
				$active,
269
				$url
270
			);
271
    }
272
    display_top_tabs($tab_array);
273
    echo '</td></tr>';
274
}
275
?>
276
<tr><td><div id="mainarea"><table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
277
  <?php
278
  $cols = 0;
279
  $savevalue = "Save";
280
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
281
  foreach ($pkg['fields']['field'] as $pkga) { ?>
282
	  
283
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
284

    
285
	  <?php
286
	  
287
	  $size = "";
288
	  
289
	  if(!$pkga['dontdisplayname']) {
290
		echo "<td width=\"22%\" class=\"vncellreq\">";
291
		echo fixup_string($pkga['fielddescr']);
292
		echo "</td>";
293
	  }
294

    
295
	  if(!$pkga['dontcombinecells'])
296
		echo "<td class=\"vtable\">";
297

    
298
		// if user is editing a record, load in the data.
299
		if (isset($id) && $a_pkg[$id]) {
300
			$fieldname = $pkga['fieldname'];
301
			$value = $a_pkg[$id][$fieldname];
302
		}
303

    
304
	      if($pkga['type'] == "input") {
305
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
306
			echo "<input " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
307
			echo "<br>" . fixup_string($pkga['description']) . "\n";
308
	      } else if($pkga['type'] == "password") {
309
			echo "<input type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
310
			echo "<br>" . fixup_string($pkga['description']) . "\n";
311
	      } else if($pkga['type'] == "select") {
312
                  if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
313
		  if($pkga['multiple'] == "yes") $multiple = "MULTIPLE ";
314
		    echo "<select " . $multiple . $size . "id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "'>\n";
315
		    foreach ($pkga['options']['option'] as $opt) {
316
			  $selected = "";
317
			  if($opt['value'] == $value) $selected = " SELECTED";
318
			  echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
319
		    }
320
		    echo "</select>\n";
321
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
322
	      } else if($pkga['type'] == "vpn_selection") {
323
		    echo "<select name='" . $vpn['name'] . "'>\n";
324
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
325
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
326
		    }
327
		    echo "</select>\n";
328
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
329
	      } else if($pkga['type'] == "checkbox") {
330
			$checkboxchecked = "";
331
			if($value == "on") $checkboxchecked = " CHECKED";
332
			echo "<input type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . ">\n";
333
			echo "<br>" . fixup_string($pkga['description']) . "\n";
334
	      } else if($pkga['type'] == "textarea") {
335
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
336
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
337
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
338
			echo "<br>" . fixup_string($pkga['description']) . "\n";
339
		  } else if($pkga['type'] == "interfaces_selection") {
340
			$size = "";
341
			$multiple = "";
342
			$fieldname = $pkga['fieldname'];
343
			if($pkga['size'] <> "") $size = " size=\"" . $pkga['size'] . "\"";
344
			if($pkga['multiple'] <> "" and $pkga['multiple'] <> "0") {
345
			  $multiple = " multiple=\"multiple\"";
346
			  $fieldname .= "[]";
347
			}
348
			echo "<select name='" . $fieldname . "'" . $size . $multiple . ">\n";
349
			if($pkga['add_to_interfaces_selection'] <> "") {
350
				$SELECTED = "";
351
				if($pkga['add_to_interfaces_selection'] == $value) $SELECTED = " SELECTED";
352
				echo "<option value='" . $pkga['add_to_interfaces_selection'] . "'" . $SELECTED . ">" . $pkga['add_to_interfaces_selection'] . "</option>\n";
353
			}
354
			$interfaces = &$config['interfaces'];
355
			if($pkga['all_interfaces'] <> "") {
356
				$ints = split(" ", `/sbin/ifconfig -l`);
357
				$interfaces = array();
358
				foreach ($ints as $int) {
359
					$interfaces[]['descr'] = $int;
360
					$interfaces[] = $int;
361
				}
362
			}
363
			foreach ($interfaces as $ifname => $iface) {
364
			  if ($iface['descr'])
365
				  $ifdescr = $iface['descr'];
366
			  else
367
				  $ifdescr = strtoupper($ifname);
368
			  $ifname = $iface['descr'];
369
			  $ip = "";
370
			  if($pkga['all_interfaces'] <> "") {
371
				$ifdescr = $iface;
372
				$ip = " " . find_interface_ip($iface);
373
			  }
374
			  $SELECTED = "";
375
			  if($value == $ifdescr) $SELECTED = " SELECTED";
376
			  $to_echo =  "<option value='" . $ifdescr . "'" . $SELECTED . ">" . $ifdescr . $ip . "</option>\n";
377
			  $to_echo .= "<!-- {$value} -->";
378
			  $to_echo .= "<!-- {$iface} -->";
379
			  $canecho = 0;
380
			  if($pkga['interface_filter'] <> "") {
381
				if(stristr($iface, $pkga['interface_filter']) == true)
382
					$canecho = 1;
383
			  } else {
384
				$canecho = 1;
385
			  }
386
			  if($canecho == 1) 
387
				echo $to_echo;
388
			}
389
			echo "</select>\n<br>" . fixup_string($pkga['description']) . "\n";
390
	      } else if($pkga['type'] == "radio") {
391
			echo "<input type='radio' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
392
	      } else if($pkga['type'] == "rowhelper") {
393
		?>
394
			<script type="text/javascript" language='javascript'>
395
			<!--
396

    
397
			<?php
398
				$rowcounter = 0;
399
				$fieldcounter = 0;
400
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
401
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
402
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
403
					$fieldcounter++;
404
				}
405
			?>
406

    
407
			-->
408
			</script>
409

    
410
			<table name="maintable" id="maintable">
411
			<tr>
412
			<?php
413
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
414
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
415
				}
416
				echo "</tr>";
417
				echo "<tbody>";
418

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

    
423
				echo "<tr>\n";
424
				$rowcounter = 0;
425
				$trc = 0;
426
				if(isset($a_pkg[$id]['row'])) {
427
					foreach($a_pkg[$id]['row'] as $row) {
428
					/*
429
					 * loop through saved data for record if it exists, populating rowhelper
430
					 */
431
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
432
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
433
							$fieldname = $rowhelper['fieldname'];
434
							// if user is editing a record, load in the data.
435
							if (isset($id) && $a_pkg[$id]) {
436
								$value = $row[$fieldname];
437
							}
438
							$options = "";
439
							$type = $rowhelper['type'];
440
							$fieldname = $rowhelper['fieldname'];
441
							if($type == "option") $options = &$rowhelper['options']['option'];
442
							$size = "8";
443
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
444
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
445
							// javascript helpers for row_helper_dynamic.js
446
							echo "</td>\n";
447
							echo "<script language=\"JavaScript\">\n";
448
							echo "<!--\n";
449
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
450
							echo "-->\n";
451
							echo "</script>\n";
452
							$text = "";
453
							$trc++;
454
						}
455

    
456
						$rowcounter++;
457
						echo "<td>";
458
						echo "<input type=\"image\" src=\"./themes/".$g['theme']."/images/icons/icon_x.gif\" onclick=\"removeRow(this); return false;\" value=\"Delete\">";
459
						echo "</td>\n";
460
						echo "</tr>\n";
461
					}
462
				}
463
				if($trc == 0) {
464
					/*
465
					 *  no records loaded.
466
                                         *  just show a generic line non-populated with saved data
467
                                         */
468
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
469
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
470
						$fieldname = $rowhelper['fieldname'];
471
						$options = "";
472
						$type = $rowhelper['type'];
473
						$fieldname = $rowhelper['fieldname'];
474
						if($type == "option") $options = &$rowhelper['options']['option'];
475
						$size = "8";
476
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
477
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
478
						// javascript helpers for row_helper_dynamic.js
479
						echo "</td>\n";
480
						echo "<script language=\"JavaScript\">\n";
481
						echo "<!--\n";
482
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
483
						echo "-->\n";
484
						echo "</script>\n";
485
						$text = "";
486
						$trc++;
487
					}
488

    
489
					$rowcounter++;
490
				}
491
			?>
492

    
493
			  </tbody>
494
			</table>
495

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

    
507
		<?php
508
	      }
509
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
510
	     ?>
511

    
512
      <?php
513
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
514
      $i++;
515
  }
516
 ?>
517
  <tr>
518
	<td>&nbsp;</td>
519
  </tr>
520
  <tr>
521
    <td width="22%" valign="top">&nbsp;</td>
522
    <td width="78%">
523
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>"> <input class="formbtn" type="button" value="cancel" onclick="history.back()">
524
      <?php if (isset($id) && $a_pkg[$id]): ?>
525
      <input name="id" type="hidden" value="<?=$id;?>">
526
      <?php endif; ?>
527
    </td>
528
  </tr>
529
</table>
530
</div></tr></td></table>
531

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

    
534
</form>
535
<?php include("fend.inc"); ?>
536
</body>
537
</html>
538

    
539
<?php
540

    
541
/*
542
 * ROW Helpers function
543
 */
544
function display_row($trc, $value, $fieldname, $type, $rowhelper, $size) {
545
	global $text;
546
	echo "<td>\n";
547
	if($type == "input") {
548
		echo "<input size='" . $size . "' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
549
	} else if($type == "password") {
550
		echo "<input size='" . $size . "' type='password' name='" . $fieldname . $trc . "' value='" . $value . "'>\n";
551
	} else if($type == "textarea") {
552
		echo "<textarea rows='2' cols='12' name='" . $fieldname . $trc . "'>" . $value . "</textarea>\n";
553
	} else if($type == "select") {
554
		echo "<select name='" . $fieldname . $trc . "'>\n";
555
		foreach($rowhelper['options']['option'] as $rowopt) {
556
			$selected = "";
557
			if($rowopt['value'] == $value) $selected = " SELECTED";
558
			$text .= "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>";
559
			echo "<option value='" . $rowopt['value'] . "'" . $selected . ">" . $rowopt['name'] . "</option>\n";
560
		}
561
		echo "</select>\n";
562
	}
563
}
564

    
565
function fixup_string($string) {
566
	global $config;
567
	// fixup #1: $myurl -> http[s]://ip_address:port/
568
	$https = "";
569
	$port = $config['system']['webguiport'];
570
	if($port <> "443" and $port <> "80")
571
		$urlport = ":" . $port;
572
	else
573
		$urlport = "";
574
		
575
	if($config['system']['webguiproto'] == "https") $https = "s";
576
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport;
577
	$newstring = str_replace("\$myurl", $myurl, $string);
578
	$string = $newstring;
579
	// fixup #2: $wanip
580
	$curwanip = get_current_wan_address();
581
	$newstring = str_replace("\$wanip", $curwanip, $string);
582
	$string = $newstring;
583
	// fixup #3: $lanip
584
	$lancfg = $config['interfaces']['lan'];
585
	$lanip = $lancfg['ipaddr'];
586
	$newstring = str_replace("\$lanip", $lanip, $string);
587
	$string = $newstring;
588
	// fixup #4: fix'r'up here.
589
	return $newstring;
590
}
591

    
592
/*
593
 *  Parse templates if they are defined
594
 */
595
function parse_package_templates() {
596
	global $pkg, $config;
597
	if($pkg['templates']['template'] <> "")
598
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
599
		$filename = $pkg_template_row['filename'];
600
		$template_text = $pkg_template_row['templatecontents'];
601

    
602
		/* calculate total row helpers count */
603
		foreach ($pkg['fields']['field'] as $fields) {
604
			if($fields['type'] == "rowhelper") {
605
				// save rowhelper items.
606
                                $row_helper_total_rows = 0;
607
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
608
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
609
						if($firstfield == "")  {
610
						  $firstfield = $rowhelperfield['fieldname'];
611
						} else {
612
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
613
						}
614
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
615
						eval($comd);
616
						if($value <> "") {
617
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
618
						} else {
619
						    $row_helper_total_rows = $rows;
620
						    break;
621
						}
622
					}
623
				}
624
			}
625
		}
626

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

    
630
		/* change fields defined as fieldname_fieldvalue to their value */
631
		foreach ($pkg['fields']['field'] as $fields) {
632
			if($fields['type'] == "rowhelper") {
633
				// save rowhelper items.
634
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
635
					$row_helper_data = "";
636
					$isfirst = 0;
637
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
638
						if($firstfield == "")  {
639
						  $firstfield = $rowhelperfield['fieldname'];
640
						} else {
641
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
642
						}
643
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
644
						eval($comd);
645
						if($value <> "") {
646
						    if($isfirst == 1) $row_helper_data .= "  " ;
647
						    $row_helper_data .= $value;
648
						    $isfirst = 1;
649
						}
650
						ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep);
651
						foreach ($sep as $se) $seperator = $se;
652
						if($seperator <> "") {
653
						    $row_helper_data = ereg_replace("  ", $seperator, $row_helper_data);
654
						    $template_text = ereg_replace("\[" . $seperator . "\]", "", $template_text);
655
						}
656
						$template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text);
657
					}
658
				}
659
			} else {
660
				$fieldname  = $fields['fieldname'];
661
				$fieldvalue = $_POST[$fieldname];
662
				$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
663
			}
664
		}
665

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

    
669
		/* write out new template file */
670
		$fout = fopen($filename,"w");
671
		fwrite($fout, $template_text);
672
		fclose($fout);
673
	    }
674
}
675

    
676
?>
(76-76/143)