Project

General

Profile

Download (23.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_once("guiconfig.inc");
44
require_once("pkg-utils.inc");
45

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

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

    
55
if($xml == "") {
56
            print_info_box_np("ERROR: No package defined.");
57
            die;
58
} else {
59
            $pkg = parse_xml_config_pkg("/usr/local/pkg/" . $xml, "packagegui");
60
}
61
$package_name = $pkg['menu'][0]['name'];
62
$section      = $pkg['menu'][0]['section'];
63
$config_path  = $pkg['configpath'];
64
$name         = $pkg['name'];
65
$title        = $section . ": " . $package_name;
66
$pgtitle      = $title;
67

    
68
$id = $_GET['id'];
69
if (isset($_POST['id']))
70
	$id = $_POST['id'];
71

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

    
75
// grab the installedpackages->package_name section.
76
$toeval = "\$a_pkg = &\$config['installedpackages']['" . $name . "']['config'];";
77
eval($toeval);
78

    
79
$toeval = "if (!is_array(\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'])) \$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'] = array();";
80
eval($toeval);
81

    
82
$toeval = "\$a_pkg = &\$config['installedpackages']['" . xml_safe_fieldname($pkg['name']) . "']['config'];";
83
eval($toeval);
84

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

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

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

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

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

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

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

    
269
<?php
270
if ($pkg['tabs'] <> "") {
271
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
272
    echo "<tr><td>";
273
    echo "  <ul id=\"tabnav\">";
274
    foreach($pkg['tabs']['tab'] as $tab) {
275
	$active = "tabinact";
276
	if(isset($tab['active'])) $active = "tabact";
277

    
278
	$urltmp = "";
279
	$title = $tab['text'];
280
	if($tab['url'] <> "") $urltmp = $tab['url'];
281
	if($tab['xml'] <> "") $urltmp = "pkg_edit.php?xml=" . $tab['xml'];
282

    
283
	$toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
284
	eval($toeval);
285
	// eval url so that above $myurl item can be processed if need be.
286
	$toeval = "\$url = \"" . $urltmp . "\"; \n";
287
	eval($toeval);
288

    
289
	if($active == "tabinact") {
290
	    echo "<li class=\"{$active}\">";
291
	    echo "<a href=\"";
292
	    echo $url;
293
	    echo "\">";
294
	    echo $title;
295
	    echo "</a>";
296
	    echo "</li>";
297
	} else {
298
	    echo "<li class=\"{$active}\">";
299
	    echo $title;
300
	    echo "</li>";
301
	}
302
    }
303
    echo "  </ul>";
304
    echo "</td></tr>";
305
    echo "<tr>";
306
    echo "<td class=\"tabcont\">";
307
}
308
?>
309

    
310
<table width="100%" border="0" cellpadding="6" cellspacing="0">
311
  <?php
312
  $cols = 0;
313
  $savevalue = "Save";
314
  if($pkg['savetext'] <> "") $savevalue = $pkg['savetext'];
315
  foreach ($pkg['fields']['field'] as $pkga) { ?>
316
	  
317
	  <?php if(!$pkga['combinefieldsend']) echo "<tr valign=\"top\">"; ?>
318

    
319
	  <?php
320
	  
321
	  $size = "";
322
	  
323
	  if(!$pkga['dontdisplayname']) {
324
		echo "<td width=\"22%\" class=\"vncellreq\">";
325
		echo fixup_string($pkga['fielddescr']);
326
		echo "</td>";
327
	  }
328

    
329
	  if(!$pkga['dontcombinecells'])
330
		echo "<td class=\"vtable\">";
331

    
332
		// if user is editing a record, load in the data.
333
		if (isset($id) && $a_pkg[$id]) {
334
			$fieldname = $pkga['fieldname'];
335
			$toeval = "\$value = \$a_pkg[" . $id . "]['" . $fieldname . "'];";
336
			echo "<!-- eval: " . $toeval . "-->\n";
337
			eval($toeval);
338
		}
339

    
340
	      if($pkga['type'] == "input") {
341
			if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
342
			echo "<input " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
343
			echo "<br>" . fixup_string($pkga['description']) . "\n";
344
	      } else if($pkga['type'] == "password") {
345
			echo "<input type='password' " . $size . " name='" . $pkga['fieldname'] . "' value='" . $value . "'>\n";
346
			echo "<br>" . fixup_string($pkga['description']) . "\n";
347
	      } else if($pkga['type'] == "select") {
348
		  // XXX: TODO: set $selected
349
                  if($pkga['size']) $size = " size='" . $pkga['size'] . "' ";
350
		  if($pkga['multiple'] == "yes") $multiple = "MULTIPLE ";
351
		    echo "<select " . $multiple . $size . "id='" . $pkga['fieldname'] . "' name='" . $pkga['fieldname'] . "'>\n";
352
		    foreach ($pkga['options']['option'] as $opt) {
353
			  $selected = "";
354
			  if($opt['value'] == $value) $selected = " SELECTED";
355
			  echo "\t<option name='" . $opt['name'] . "' value='" . $opt['value'] . "'" . $selected . ">" . $opt['name'] . "</option>\n";
356
		    }
357
		    echo "</select>\n";
358
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
359
	      } else if($pkga['type'] == "vpn_selection") {
360
		    echo "<select name='" . $vpn['name'] . "'>\n";
361
		    foreach ($config['ipsec']['tunnel'] as $vpn) {
362
			echo "\t<option value=\"" . $vpn['descr'] . "\">" . $vpn['descr'] . "</option>\n";
363
		    }
364
		    echo "</select>\n";
365
		    echo "<br>" . fixup_string($pkga['description']) . "\n";
366
	      } else if($pkga['type'] == "checkbox") {
367
			$checkboxchecked = "";
368
			if($value == "on") $checkboxchecked = " CHECKED";
369
			echo "<input type='checkbox' name='" . $pkga['fieldname'] . "'" . $checkboxchecked . ">\n";
370
			echo "<br>" . fixup_string($pkga['description']) . "\n";
371
	      } else if($pkga['type'] == "textarea") {
372
		  if($pkga['rows']) $rows = " rows='" . $pkga['rows'] . "' ";
373
		  if($pkga['cols']) $cols = " cols='" . $pkga['cols'] . "' ";
374
			echo "<textarea " . $rows . $cols . " name='" . $pkga['fieldname'] . "'>" . $value . "</textarea>\n";
375
			echo "<br>" . fixup_string($pkga['description']) . "\n";
376
		  } else if($pkga['type'] == "interfaces_selection") {
377
			$size = "";
378
			$multiple = "";
379
			$fieldname = $pkga['fieldname'];
380
			if($pkga['size'] <> "") $size = " size=\"" . $pkga['size'] . "\"";
381
			if($pkga['multiple'] <> "" and $pkga['multiple'] <> "0") {
382
			  $multiple = " multiple=\"multiple\"";
383
			  $fieldname .= "[]";
384
			}
385
			echo "<select name='" . $fieldname . "'" . $size . $multiple . ">\n";
386
			if($pkga['add_to_interfaces_selection'] <> "") {
387
				$SELECTED = "";
388
				if($pkga['add_to_interfaces_selection'] == $value) $SELECTED = " SELECTED";
389
				echo "<option value='" . $pkga['add_to_interfaces_selection'] . "'" . $SELECTED . ">" . $pkga['add_to_interfaces_selection'] . "</option>\n";
390
			}			
391
			foreach ($config['interfaces'] as $ifname => $iface) {
392
			  if ($iface['descr'])
393
				  $ifdescr = $iface['descr'];
394
			  else
395
				  $ifdescr = strtoupper($ifname);
396
			  $ifname = $iface['descr'];
397
			  $SELECTED = "";
398
			  if($value == $ifdescr) $SELECTED = " SELECTED";
399
			  echo "<option value='" . $ifdescr . "'" . $SELECTED . ">" . $ifdescr . "</option>\n";
400
			  echo "<!-- {$value} -->";
401
			}
402
			echo "</select>\n";
403
			echo "<br>" . fixup_string($pkga['description']) . "\n";
404
	      } else if($pkga['type'] == "radio") {
405
			echo "<input type='radio' name='" . $pkga['fieldname'] . "' value='" . $value . "'>";
406
	      } else if($pkga['type'] == "rowhelper") {
407
		?>
408
			<script type="text/javascript" language='javascript'>
409
			<!--
410

    
411
			<?php
412
				$rowcounter = 0;
413
				$fieldcounter = 0;
414
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
415
					echo "rowname[" . $fieldcounter . "] = \"" . $rowhelper['fieldname'] . "\";\n";
416
					echo "rowtype[" . $fieldcounter . "] = \"" . $rowhelper['type'] . "\";\n";
417
					$fieldcounter++;
418
				}
419
			?>
420

    
421
			-->
422
			</script>
423

    
424
			<table name="maintable" id="maintable">
425
			<tr>
426
			<?php
427
				foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) {
428
				  echo "<td><b>" . fixup_string($rowhelper['fielddescr']) . "</td>\n";
429
				}
430
				echo "</tr>";
431
				echo "<tbody>";
432

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

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

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

    
506
					$rowcounter++;
507
				}
508
			?>
509

    
510
			  </tbody>
511
			</table>
512

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

    
524
		<?php
525
	      }
526
	      if($pkga['typehint']) echo " " . $pkga['typehint'];
527
	     ?>
528

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

    
548
<?php
549
if ($pkg['tabs'] <> "") {
550
    echo "</td></tr></table>";
551
}
552
?>
553

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

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

    
561
<?php
562

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

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

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

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

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

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

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

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

    
696
?>
(66-66/128)