Project

General

Profile

Download (23.5 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
				if(is_array( $_POST[$fields['fieldname']] )) {
143
					$pkgarr[$fields['fieldname']]=array();
144
					foreach($_POST[$fields['fieldname']] as $v) {
145
						$pkgarr[$fields['fieldname']][] = $v;
146
						eval($comd);								
147
					}
148
					continue;
149
				}                                
150
				$fieldname  = $fields['fieldname'];
151
				$fieldvalue = $_POST[$fieldname];
152
				$pkgarr[$fieldname] = $fieldvalue;
153
			}
154
		}
155

    
156
		if (isset($id) && $a_pkg[$id])
157
			$a_pkg[$id] = $pkgarr;
158
		else
159
			$a_pkg[] = $pkgarr;
160

    
161
		write_config();
162

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

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

    
173
		parse_package_templates();
174

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

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

    
183
		if($pkg['aftersaveredirect'] <> "") {
184
		    header("Location:  " . $pkg['aftersaveredirect']);
185
		} elseif(!$pkg['adddeleteeditpagefields']) {
186
		    header("Location:  pkg_edit.php?xml={$xml}&id=0");
187
		} elseif(!$pkg['preoutput']) {
188
		    header("Location:  pkg.php?xml=" . $xml);
189
		}
190
		exit;
191
	}
192
}
193

    
194
if($pkg['title'] <> "")
195
	$title = $pkg['title'];
196
else
197
	$title = "Package Editor";
198

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

    
202
?>
203

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

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

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

    
293
	  <?php
294
	  
295
	  $size = "";
296
	  
297
	  if(!$pkga['dontdisplayname']) {
298
		echo "<td width=\"22%\" class=\"vncellreq\">";
299
		echo fixup_string($pkga['fielddescr']);
300
		echo "</td>";
301
	  }
302

    
303
	  if(!$pkga['dontcombinecells'])
304
		echo "<td class=\"vtable\">";
305

    
306
		// if user is editing a record, load in the data.
307
		if (isset($id) && $a_pkg[$id]) {
308
			$fieldname = $pkga['fieldname'];
309
			$value = $a_pkg[$id][$fieldname];
310
		}
311

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

    
405
			<?php
406
				$rowcounter = 0;
407
				$fieldcounter = 0;
408
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
409
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
410
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
411
					$fieldcounter++;
412
				}
413
			?>
414

    
415
			-->
416
			</script>
417

    
418
			<table name="maintable" id="maintable">
419
			<tr>
420
			<?php
421
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
422
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
423
				}
424
				echo "</tr>";
425
				echo "<tbody>";
426

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

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

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

    
497
					$rowcounter++;
498
				}
499
			?>
500

    
501
			  </tbody>
502
			</table>
503

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

    
515
		<?php
516
	      }
517
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
518
	     ?>
519

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

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

    
542
</form>
543
<?php include("fend.inc"); ?>
544
</body>
545
</html>
546

    
547
<?php
548

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

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

    
600
/*
601
 *  Parse templates if they are defined
602
 */
603
function parse_package_templates() {
604
	global $pkg, $config;
605
	if($pkg['templates']['template'] <> "")
606
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
607
		$filename = $pkg_template_row['filename'];
608
		$template_text = $pkg_template_row['templatecontents'];
609

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

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

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

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

    
677
		/* write out new template file */
678
		$fout = fopen($filename,"w");
679
		fwrite($fout, $template_text);
680
		fclose($fout);
681
	    }
682
}
683

    
684
?>
(79-79/146)