Project

General

Profile

Download (21.6 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
 <tabs>
31
 	<tab>
32
 		<text>Testing Tab</text>
33
 		<url>url to go to</url>
34
 	</tab>
35
 	<tab>
36
 		<text>Testing Tab 2</text>
37
 		<xml>filename of xml</xml>
38
 	</tab>
39
 </tabs>
40

    
41
*/
42

    
43
require("guiconfig.inc");
44
require("xmlparse_pkg.inc");
45

    
46
$pfSense_config = $config; // copy this since we will be parsing
47
                           // another xml file which will be clobbered.
48

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

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

    
58
if($xml == "") {
59
            $xml = "not_defined";
60
            print_info_box_np("ERROR:  Could not open " . $xml . ".");
61
            die;
62
} else {
63
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
64
}
65

    
66
$package_name = $pkg['menu'][0]['name'];
67
$section      = $pkg['menu'][0]['section'];
68
$config_path  = $pkg['configpath'];
69
$name         = $pkg['name'];
70
$title        = $section . ": " . $package_name;
71

    
72
$id = $_GET['id'];
73
if (isset($_POST['id']))
74
	$id = $_POST['id'];
75

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

    
79
// grab the installedpackages->package_name section.
80
$toeval = "\$a_pkg = &\$config['installedpackages']['" . $name . "']['config'];";
81
eval($toeval);
82

    
83
$toeval = "if (!is_array(\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'])) \$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'] = array();";
84
eval($toeval);
85

    
86
$toeval = "\$a_pkg = &\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'];";
87
eval($toeval);
88

    
89

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

    
93
if ($_POST) {
94
	if($_POST['act'] == "del") {
95
		if($pkg['custom_delete_php_command']) {
96
		    if($pkg['custom_php_command_before_form'] <> "")
97
			    eval($pkg['custom_php_command_before_form']);
98
		    eval($pkg['custom_delete_php_command']);
99
		}
100
		write_config();
101
		// resync the configuration file code if defined.
102
		if($pkg['custom_php_resync_config_command'] <> "") {
103
			if($pkg['custom_php_command_before_form'] <> "")
104
				eval($pkg['custom_php_command_before_form']);
105
			eval($pkg['custom_php_resync_config_command']);
106
		}
107
	} else {
108
		if($pkg['custom_add_php_command']) {
109
			if($pkg['donotsave'] <> "" or $pkg['preoutput'] <> "") {
110
				?>
111
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
112
<html>
113
<head>
114
<title><?=gentitle_pkg($title);?></title>
115
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
116
<link href="gui.css" rel="stylesheet" type="text/css">
117
</head>
118

    
119
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
120
<?php
121
include("fbegin.inc");
122

    
123
?>
124
<p class="pgtitle"><?=$title?></p>
125
				<?php
126
			}
127
			if($pkg['preoutput']) echo "<pre>";
128
			eval($pkg['custom_add_php_command']);
129
			if($pkg['preoutput']) echo "</pre>";
130
		}
131
	}
132

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

    
136
	$firstfield = "";
137
	$rows = 0;
138

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

    
168
				$fieldname  = $fields['fieldname'];
169
				$fieldvalue = $_POST[$fieldname];
170
				$toeval = "\$pkgarr['" . $fieldname . "'] 	= \"" . $fieldvalue . "\";";
171
				eval($toeval);
172
			}
173
		}
174

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

    
180
		write_config();
181

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

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

    
192
		parse_package_templates();
193

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

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

    
202
		if($pkg['aftersaveredirect'] <> "") {
203
		    header("Location:  " . $pkg['aftersaveredirect']);
204
		} else {
205
		    if(!$pkg['preoutput'])
206
			header("Location:  pkg.php?xml=" . $xml);
207
		}
208
		exit;
209
	}
210
}
211

    
212
?>
213
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
214
<html>
215
<head>
216
<?php
217
    if($pkg['title'] <> "") $title = $pkg['title'];
218
?>
219
<title><?=gentitle_pkg($title);?></title>
220
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
221
<link href="gui.css" rel="stylesheet" type="text/css">
222
</head>
223

    
224
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
225

    
226
<script type="text/javascript" language="javascript" src="row_helper_dynamic.js">
227
</script>
228

    
229
<?php
230
$config_tmp = $config;
231
$config = $pfSense_config;
232
include("fbegin.inc");
233
$config = $config_tmp;
234
?>
235
<p class="pgtitle"><?=$title?></p>
236
<form action="pkg_edit.php" method="post">
237
<input type="hidden" name="xml" value="<?= $xml ?>">
238
<?php if ($savemsg) print_info_box($savemsg); ?>
239

    
240
<?php
241
if ($pkg['tabs'] <> "") {
242
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
243
    echo "<tr><td>";
244
    echo "  <ul id=\"tabnav\">";
245
    foreach($pkg['tabs']['tab'] as $tab) {
246
	$active = "tabinact";
247
	if(isset($tab['active'])) $active = "tabact";
248

    
249
	$urltmp = "";
250
	$title = $tab['text'];
251
	if($tab['url'] <> "") $urltmp = $tab['url'];
252
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
253

    
254
	$toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
255
	eval($toeval);
256
	// eval url so that above $myurl item can be processed if need be.
257
	$toeval = "\$url = \"" . $urltmp . "\"; \n";
258
	eval($toeval);
259

    
260
	if($active == "tabinact") {
261
	    echo "<li class=\"{$active}\">";
262
	    echo "<a href=\"";
263
	    echo $url;
264
	    echo "\">";
265
	    echo $title;
266
	    echo "</a>";
267
	    echo "</li>";
268
	} else {
269
	    echo "<li class=\"{$active}\">";
270
	    echo $title;
271
	    echo "</li>";
272
	}
273
    }
274
    echo "  </ul>";
275
    echo "</td></tr>";
276
    echo "<tr>";
277
    echo "<td class=\"tabcont\">";
278
}
279
?>
280

    
281
<table width="100%" border="0" cellpadding="6" cellspacing="0">
282
  <?php
283
  $cols = 0;
284
  $savevalue = "Save";
285
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
286
  foreach ($pkg['fields']['field'] as $pkga) { ?>
287
	  
288
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
289

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

    
300
	  if(!$pkga['dontcombinecells'])
301
		echo "<td class=\"vtable\">";
302

    
303
		// if user is editing a record, load in the data.
304
		if (isset($id) && $a_pkg[$id]) {
305
			$fieldname = $pkga['fieldname'];
306
			$toeval = "\$value = \$a_pkg[" . $id . "]['" . $fieldname . "'];";
307
			echo "<!-- eval: " . $toeval . "-->\n";
308
			eval($toeval);
309
		}
310

    
311
	      if($pkga['type'] == "input") {
312
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
313
			echo "<input " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
314
			echo "<br>" . fixup_string($pkga['description']) . "\n";
315
	      } else if($pkga['type'] == "password") {
316
			echo "<input type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
317
			echo "<br>" . fixup_string($pkga['description']) . "\n";
318
	      } else if($pkga['type'] == "select") {
319
		  // XXX: TODO: set $selected
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
			foreach ($config['interfaces'] as $ifname => $iface) {
363
			  if ($iface['descr'])
364
				  $ifdescr = $iface['descr'];
365
			  else
366
				  $ifdescr = strtoupper($ifname);
367
			  $ifname = $iface['descr'];
368
			  $SELECTED = "";
369
			  if($value == $ifname) $SELECTED = " SELECTED";
370
			  echo "<option value='" . $ifdescr . "'" . $SELECTED . ">" . $ifdescr . "</option>\n";
371
			}
372
			echo "</select>\n";
373
			echo "<br>" . fixup_string($pkga['description']) . "\n";
374
	      } else if($pkga['type'] == "radio") {
375
			echo "<input type='radio' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
376
	      } else if($pkga['type'] == "rowhelper") {
377
		?>
378
			<script type="text/javascript" language='javascript'>
379
			<!--
380

    
381
			<?php
382
				$rowcounter = 0;
383
				$fieldcounter = 0;
384
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
385
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
386
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
387
					$fieldcounter++;
388
				}
389
			?>
390

    
391
			-->
392
			</script>
393

    
394
			<table name="maintable" id="maintable">
395
			<tr>
396
			<?php
397
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
398
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
399
				}
400
				echo "</tr>";
401
				echo "<tbody>";
402

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

    
407
				echo "<tr>\n";
408
				$rowcounter = 0;
409
				$trc = 0;
410
				if(isset($a_pkg[$id]['row'])) {
411
					foreach($a_pkg[$id]['row'] as $row) {
412
					/*
413
					 * loop through saved data for record if it exists, populating rowhelper
414
					 */
415
						foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
416
							if($rowhelper['value'] <> "") $value = $rowhelper['value'];
417
							$fieldname = $rowhelper['fieldname'];
418
							// if user is editing a record, load in the data.
419
							if (isset($id) && $a_pkg[$id]) {
420
								$toeval = "\$value = \$row['" . $fieldname . "'];";
421
								echo "<!-- eval: " . $toeval . "-->\n";
422
								eval($toeval);
423
								echo "<!-- value: " . $value . "-->\n";
424
							}
425
							$options = "";
426
							$type = $rowhelper['type'];
427
							$fieldname = $rowhelper['fieldname'];
428
							if($type == "option") $options = &$rowhelper['options']['option'];
429
							$size = "8";
430
							if($rowhelper['size'] <> "") $size = $rowhelper['size'];
431
							display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
432
							// javascript helpers for row_helper_dynamic.js
433
							echo "</td>\n";
434
							echo "<script language=\"JavaScript\">\n";
435
							echo "<!--\n";
436
							echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
437
							echo "-->\n";
438
							echo "</script>\n";
439
							$text = "";
440
							$trc++;
441
						}
442

    
443
						$rowcounter++;
444
						echo "<td>";
445
						echo "<input type=\"image\" src=\"/x.gif\" onclick=\"removeRow(this); return false;\" value=\"Delete\">";
446
						echo "</td>\n";
447
						echo "</tr>\n";
448
					}
449
				}
450
				if($trc == 0) {
451
					/*
452
					 *  no records loaded.
453
                                         *  just show a generic line non-populated with saved data
454
                                         */
455
                                        foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
456
						if($rowhelper['value'] <> "") $value = $rowhelper['value'];
457
						$fieldname = $rowhelper['fieldname'];
458
						$options = "";
459
						$type = $rowhelper['type'];
460
						$fieldname = $rowhelper['fieldname'];
461
						if($type == "option") $options = &$rowhelper['options']['option'];
462
						$size = "8";
463
						if($rowhelper['size'] <> "") $size = $rowhelper['size'];
464
						display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size);
465
						// javascript helpers for row_helper_dynamic.js
466
						echo "</td>\n";
467
						echo "<script language=\"JavaScript\">\n";
468
						echo "<!--\n";
469
						echo "newrow[" . $trc . "] = \"" . $text . "\";\n";
470
						echo "-->\n";
471
						echo "</script>\n";
472
						$text = "";
473
						$trc++;
474
					}
475

    
476
					$rowcounter++;
477
				}
478
			?>
479

    
480
			  </tbody>
481
			</table>
482

    
483
		<br><a onClick="javascript:addRowTo('maintable'); return false;" href="#"><img border="0" src="/plus.gif"></a>
484
		<script language="JavaScript">
485
		<!--
486
		field_counter_js = <?= $fieldcounter ?>;
487
		rows = <?= $rowcounter ?>;
488
		totalrows = <?php echo $rowcounter; ?>;
489
		loaded = <?php echo $rowcounter; ?>;
490
		//typesel_change();
491
		//-->
492
		</script>
493

    
494
		<?php
495
	      }
496
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
497
	     ?>
498

    
499
      <?php
500
	  if(!$pkga['combinefieldsbegin']) echo "</td></tr>";
501
      $i++;
502
  }
503
 ?>
504
  <tr>
505
	<td>&nbsp;</td>
506
  </tr>
507
  <tr>
508
    <td width="22%" valign="top">&nbsp;</td>
509
    <td width="78%">
510
      <input name="Submit" type="submit" class="formbtn" value="<?= $savevalue ?>"> <input class="formbtn" type="button" value="cancel" onclick="history.back()">
511
      <?php if (isset($id) && $a_pkg[$id]): ?>
512
      <input name="id" type="hidden" value="<?=$id;?>">
513
      <?php endif; ?>
514
    </td>
515
  </tr>
516
</table>
517

    
518
<?php
519
if ($pkg['tabs'] <> "") {
520
    echo "</td></tr></table>";
521
}
522
?>
523

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

    
526
</form>
527
<?php include("fend.inc"); ?>
528
</body>
529
</html>
530

    
531
<?php
532

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

    
557
function fixup_string($string) {
558
	global $config;
559
	// fixup #1: $myurl -> http[s]://ip_address:port/
560
	$https = "";
561
	$port = "";
562
	$urlport = "";
563
	$port = $config['system']['webguiport'];
564
	if($port <> "443" and $port <> "80") $urlport = ":" . $port;
565
	if($config['system']['webguiproto'] == "https") $https = "s";
566
	$myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlportport;
567
	$newstring = str_replace("\$myurl", $myurl, $string);
568
	$string = $newstring;
569
	// fixup #2: $wanip
570
	$curwanip = get_current_wan_address();
571
	$newstring = str_replace("\$wanip", $curwanip, $string);
572
	$string = $newstring;
573
	// fixup #3: $lanip
574
	$lancfg = $config['interfaces']['lan'];
575
	$lanip = $lancfg['ipaddr'];
576
	$newstring = str_replace("\$lanip", $lanip, $string);
577
	$string = $newstring;
578
	// fixup #4: fix'r'up here.
579
	return $newstring;
580
}
581

    
582
/*
583
 *  Parse templates if they are defined
584
 */
585
function parse_package_templates() {
586
	global $pkg, $config;
587
	if($pkg['templates']['template'] <> "")
588
	    foreach($pkg['templates']['template'] as $pkg_template_row) {
589
		$filename = $pkg_template_row['filename'];
590
		$template_text = $pkg_template_row['templatecontents'];
591

    
592
		/* calculate total row helpers count */
593
		foreach ($pkg['fields']['field'] as $fields) {
594
			if($fields['type'] == "rowhelper") {
595
				// save rowhelper items.
596
                                $row_helper_total_rows = 0;
597
				for($x=0; $x<99; $x++) { // XXX: this really should be passed from the form.
598
					foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) {
599
						if($firstfield == "")  {
600
						  $firstfield = $rowhelperfield['fieldname'];
601
						} else {
602
						  if($firstfield == $rowhelperfield['fieldname']) $rows++;
603
						}
604
						$comd = "\$value = \$_POST['" . $rowhelperfield['fieldname'] . $x . "'];";
605
						eval($comd);
606
						if($value <> "") {
607
						    //$template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text);
608
						} else {
609
						    $row_helper_total_rows = $rows;
610
						    break;
611
						}
612
					}
613
				}
614
			}
615
		}
616

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

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

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

    
659
		/* write out new template file */
660
		$fout = fopen($filename,"w");
661
		fwrite($fout, $template_text);
662
		fclose($fout);
663
	    }
664
}
665

    
666
?>
(59-59/113)