All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ini_set('max_execution_time', '0'); require_once("guiconfig.inc"); require_once("functions.inc"); require_once("filter.inc"); function get_pkg_interfaces_select_source($include_localhost=false) { $interfaces = get_configured_interface_with_descr(); $ssifs = array(); foreach ($interfaces as $iface => $ifacename) { $tmp["name"] = $ifacename; $tmp["value"] = $iface; $ssifs[] = $tmp; } if ($include_localhost) { $tmp["name"] = "Localhost"; $tmp["value"] = "lo0"; $ssifs[] = $tmp; } return $ssifs; } function domTT_title($title_msg){ if (!empty($title_msg)){ $title_msg=preg_replace("/\s+/"," ",$title_msg); $title_msg=preg_replace("/'/","\'",$title_msg); return "onmouseout=\"this.style.color = ''; domTT_mouseout(this, event);\" onmouseover=\"domTT_activate(this, event, 'content', '{$title_msg}', 'trail', true, 'delay', 0, 'fade', 'both', 'fadeMax', 93, 'delay',300,'styleClass', 'niceTitle');\""; } } global $listtags; $listtags = array_flip(array('build_port_path', 'depends_on_package', 'onetoone', 'queue', 'rule', 'servernat', 'alias', 'additional_files_needed', 'tab', 'template', 'menu', 'rowhelperfield', 'service', 'step', 'package', 'columnitem', 'option', 'item', 'field', 'package', 'file')); $pkg = parse_xml_config_raw('/usr/local/pkg/miniupnpd.xml', 'packagegui', false); require_once 'miniupnpd.inc'; $name = $pkg['name']; $title = $pkg['title']; $pgtitle = $title; $id = $_GET['id']; if (isset($_POST['id'])) $id = htmlspecialchars($_POST['id']); // Not posting? Then user is editing a record. There must be a valid id // when editing a record. if(!$id && !$_POST) $id = "0"; if(!is_numeric($id)) { header("Location: /"); exit; } if($config['installedpackages'] && !is_array($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'])) $config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'] = array(); if ($config['installedpackages'] && (count($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']) > 0) && ($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config'][0] == "")) array_shift($config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']); $a_pkg = &$config['installedpackages'][xml_safe_fieldname($pkg['name'])]['config']; before_form_miniupnpd($pkg); if ($_POST) { $firstfield = ""; $rows = 0; $input_errors = array(); $reqfields = array(); $reqfieldsn = array(); foreach ($pkg['fields']['field'] as $field) { if (($field['type'] == 'input') && isset($field['required'])) { if($field['fieldname']) $reqfields[] = $field['fieldname']; if($field['fielddescr']) $reqfieldsn[] = $field['fielddescr']; } } do_input_validation($_POST, $reqfields, $reqfieldsn, $input_errors); validate_form_miniupnpd($_POST, $input_errors); if($_POST['act'] == "del") { write_config($pkg['delete_string']); before_form_miniupnpd($pkg); sync_package_miniupnpd(); } // donotsave is enabled. lets simply exit. if(empty($pkg['donotsave'])) { // store values in xml configration file. if (!$input_errors) { $pkgarr = array(); foreach ($pkg['fields']['field'] as $fields) { switch($fields['type']){ case "rowhelper": // save rowhelper items. #$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row"); $rowhelpername="row"; foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) foreach($_POST as $key => $value){ if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches)) $pkgarr[$rowhelpername][$matches[1]][$rowhelperfield['fieldname']]=$value; } break; default: $fieldname = $fields['fieldname']; if ($fieldname == "interface_array") { $fieldvalue = $_POST[$fieldname]; } elseif (is_array($_POST[$fieldname])) { $fieldvalue = implode(',', $_POST[$fieldname]); } else { $fieldvalue = trim($_POST[$fieldname]); if ($fields['encoding'] == 'base64') $fieldvalue = base64_encode($fieldvalue); } if($fieldname) $pkgarr[$fieldname] = $fieldvalue; } } if (isset($id) && $a_pkg[$id]) $a_pkg[$id] = $pkgarr; else $a_pkg[] = $pkgarr; write_config($pkg['addedit_string']); sync_package_miniupnpd(); parse_package_templates(); /* if start_command is defined, restart w/ this */ if($pkg['start_command'] <> "") exec($pkg['start_command'] . ">/dev/null 2&>1"); /* if restart_command is defined, restart w/ this */ if($pkg['restart_command'] <> "") exec($pkg['restart_command'] . ">/dev/null 2&>1"); if($pkg['aftersaveredirect'] <> "") { redirectHeader($pkg['aftersaveredirect']); } elseif(!$pkg['adddeleteeditpagefields']) { redirectHeader("services_upnp.php?id=0"); } elseif(!$pkg['preoutput']) { redirectHeader("services_upnp.php"); } exit; } else { $get_from_post = true; } } elseif (!$input_errors) { exit; } } include("head.inc"); ?> "") { ?>
"; $input .= "\n"; echo $input; continue; } if($pkga['combinefields']=="begin"){ $input=""; echo $input; $size = ""; if (isset($pkga['dontdisplayname'])){ $input=""; if(!isset($pkga['combinefields'])) $input .= ""; if(isset($pkga['usecolspan2'])) $colspan="colspan='2'"; else $input .= ""; $advanced .= $input; $adv_filed_count++; } else echo $input; } else if (!isset($pkga['placeonbottom'])){ unset($req); if (isset($pkga['required'])) $req = 'req'; $input= ""; echo $input; } if($pkga['combinefields']=="begin"){ $input=""; } else{ $input= ""; if($pkga['usecolspan2']) $input.= "
"; } echo "{$input}\n"; #increment counter $i++; } ?>
 
{$pkga['name']}
 
"; $input .= fixup_string($pkga['fielddescr']); $input .= ""; echo $input; } $class=(isset($pkga['combinefields']) ? '' : 'class="vtable"'); if (!isset($pkga['placeonbottom'])){ $input="
"; echo $input; } // if user is editing a record, load in the data. $fieldname = $pkga['fieldname']; if ($get_from_post) { $value = $_POST[$fieldname]; if (is_array($value)) $value = implode(',', $value); } else { if (isset($id) && $a_pkg[$id]) $value = $a_pkg[$id][$fieldname]; else $value = $pkga['default_value']; } switch($pkga['type']){ case "input": $size = ($pkga['size'] ? " size='{$pkga['size']}' " : ""); $input = "\n"; $input .= "
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "password": $size = ($pkga['size'] ? " size='{$pkga['size']}' " : ""); $input = "\n"; $input .= "
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "info": $input = fixup_string($pkga['description']) . "\n"; echo $input; break; case "select": $fieldname = $pkga['fieldname']; if (isset($pkga['multiple'])) { $multiple = 'multiple="multiple"'; $items = explode(',', $value); $fieldname .= "[]"; } else { $multiple = ''; $items = array($value); } $size = ($pkga['size'] ? " size='{$pkga['size']}' " : ""); $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : ''); $input = "\n
\n" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "select_source": $fieldname = $pkga['fieldname']; if (isset($pkga['multiple'])) { $multiple = 'multiple="multiple"'; $items = explode(',', $value); $fieldname .= "[]"; } else { $multiple = ''; $items = array($value); } $size = (isset($pkga['size']) ? "size=\"{$pkga['size']}\"" : ''); $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : ''); $input = "\n
\n" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "vpn_selection" : $input = "\n"; $input .= "
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "checkbox": $checkboxchecked =($value == "on" ? " checked=\"checked\"" : ""); $onchange = (isset($pkga['onchange']) ? "onchange=\"{$pkga['onchange']}\"" : ''); if (isset($pkga['enablefields']) || isset($pkga['checkenablefields'])) $onclick = ' onclick="javascript:enablechange();"'; $input = "\n"; $input .= "
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "textarea": if($pkga['rows']) $rows = " rows='{$pkga['rows']}' "; if($pkga['cols']) $cols = " cols='{$pkga['cols']}' "; if (($pkga['encoding'] == 'base64') && !$get_from_post && !empty($value)) $value = base64_decode($value); $wrap =($pkga['wrap'] == "off" ? 'wrap="off" style="white-space:nowrap;"' : ''); $input = "\n"; $input .= "
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "aliases": // Use xml tag to filter type aliases $size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : ''); $fieldname = $pkga['fieldname']; $a_aliases = &$config['aliases']['alias']; $addrisfirst = 0; $aliasesaddr = ""; $value = "value='{$value}'"; if(isset($a_aliases)) { if(!empty($pkga['typealiases'])) { foreach($a_aliases as $alias) if($alias['type'] == $pkga['typealiases']) { if($addrisfirst == 1) $aliasesaddr .= ","; $aliasesaddr .= "'" . $alias['name'] . "'"; $addrisfirst = 1; } } else { foreach($a_aliases as $alias) { if($addrisfirst == 1) $aliasesaddr .= ","; $aliasesaddr .= "'" . $alias['name'] . "'"; $addrisfirst = 1; } } } $input = "\n
"; $input .= fixup_string($pkga['description']) . "\n"; $script = ""; echo $input; echo $script; break; case "interfaces_selection": $ips=array(); $interface_regex=(isset($pkga['hideinterfaceregex']) ? $pkga['hideinterfaceregex'] : "nointerfacestohide"); if (is_array($config['interfaces'])) foreach ($config['interfaces'] as $iface_key=>$iface_value){ if (isset($iface_value['enable']) && ! preg_match("/$interface_regex/",$iface_key)){ $iface_description=($iface_value['descr'] !="" ? strtoupper($iface_value['descr']) : strtoupper($iface_key)); if (isset($pkga['showips'])) $iface_description .= " address"; $ips[]=array('ip'=> $iface_key, 'description'=> $iface_description); } } if (is_array($config['virtualip']) && isset($pkga['showvirtualips'])) foreach ($config['virtualip']['vip'] as $vip){ if (! preg_match("/$interface_regex/",$vip['interface'])) $vip_description=($vip['descr'] !="" ? " ({$vip['descr']}) " : " "); switch ($vip['mode']){ case "ipalias": case "carp": $ips[]=array( 'ip'=> $vip['subnet'],'description' => "{$vip['subnet']} $vip_description"); break; case "proxyarp": if ($vip['type']=="network"){ $start = ip2long32(gen_subnet($vip['subnet'], $vip['subnet_bits'])); $end = ip2long32(gen_subnet_max($vip['subnet'], $vip['subnet_bits'])); $len = $end - $start; for ($i = 0; $i <= $len; $i++) $ips[]= array('ip'=>long2ip32($start+$i),'description'=> long2ip32($start+$i)." from {$vip['subnet']}/{$vip['subnet_bits']} {$vip_description}"); } else{ $ips[]= array('ip'=>$vip['subnet'],'description'=> "{$vip['subnet']} $vip_description"); } break; } } sort($ips); if (isset($pkga['showlistenall'])) array_unshift($ips,array('ip'=> 'All', 'description'=> 'Listen on All interfaces/ip addresses ')); if (! preg_match("/$interface_regex/","loopback")){ $iface_description=(isset($pkga['showips']) ? "127.0.0.1 (loopback)" : "loopback"); array_push($ips,array('ip'=> 'lo0', 'description'=> $iface_description)); } #show interfaces array on gui $size = ($pkga['size'] ? "size=\"{$pkga['size']}\"" : ''); $multiple = ''; $fieldname = $pkga['fieldname']; if (isset($pkga['multiple'])) { $fieldname .= '[]'; $multiple = 'multiple="multiple"'; } $input = "\n
" . fixup_string($pkga['description']) . "\n"; echo $input; break; case "radio": $input = ""; echo $input; break; case "button": $input = "\n"; if(isset($pkga['placeonbottom'])) $pkg_buttons .= $input; else echo $input ."\n
" . fixup_string($pkga['description']) . "\n"; break; case "rowhelper": #$rowhelpername=($fields['fieldname'] ? $fields['fieldname'] : "row"); $rowhelpername="row"; ?> '> " . fixup_string($rowhelper['fielddescr']) . "\n"; } $rowcounter = 0; $trc = 0; //Use assigned $a_pkg or create an empty array to enter loop if(isset($a_pkg[$id][$rowhelpername])) $saved_rows=$a_pkg[$id][$rowhelpername]; else $saved_rows[]=array(); foreach($saved_rows as $row) { echo "\n\n"; foreach($pkga['rowhelper']['rowhelperfield'] as $rowhelper) { unset($value); if($rowhelper['value'] <> "") $value = $rowhelper['value']; $fieldname = $rowhelper['fieldname']; // if user is editing a record, load in the data. if (isset($id) && $a_pkg[$id]) { $value = $row[$fieldname]; } $options = ""; $type = $rowhelper['type']; $description = $rowhelper['description']; $fieldname = $rowhelper['fieldname']; if($type == "option") $options = &$rowhelper['options']['option']; if($rowhelper['size']) $size = $rowhelper['size']; else if ($pkga['size']) $size = $pkga['size']; else $size = "8"; display_row($rowcounter, $value, $fieldname, $type, $rowhelper, $size); $text = ""; $trc++; } $rowcounter++; echo "\n"; } ?>
"; #echo "\"remove\""; echo "delete"; echo "

add
"; if ($pkga['combinefields']=="end") $input.="
 
 
" . gettext("Note") . ": {$pkg['note']}

"; } //if (isset($id) && $a_pkg[$id]) // We'll always have a valid ID in our hands echo ""; echo "\n{$pkg_buttons}\n"; ?>
\n"; switch($type){ case "input": echo "\n"; break; case "checkbox": echo "\n"; break; case "password": echo "\n"; break; case "textarea": echo "\n"; case "select": echo "\n"; break; case "interfaces_selection": $size = ($size ? "size=\"{$size}\"" : ''); $multiple = ''; if (isset($rowhelper['multiple'])) { $fieldname .= '[]'; $multiple = "multiple=\"multiple\""; } echo "\n"; break; case "select_source": echo "\n"; break; } echo "\n"; } function fixup_string($string) { global $config; // fixup #1: $myurl -> http[s]://ip_address:port/ $https = ""; $port = $config['system']['webguiport']; if($port <> "443" and $port <> "80") $urlport = ":" . $port; else $urlport = ""; if($config['system']['webgui']['protocol'] == "https") $https = "s"; $myurl = "http" . $https . "://" . getenv("HTTP_HOST") . $urlport; $newstring = str_replace("\$myurl", $myurl, $string); $string = $newstring; // fixup #2: $wanip $curwanip = get_interface_ip(); $newstring = str_replace("\$wanip", $curwanip, $string); $string = $newstring; // fixup #3: $lanip $lancfg = $config['interfaces']['lan']; $lanip = $lancfg['ipaddr']; $newstring = str_replace("\$lanip", $lanip, $string); $string = $newstring; // fixup #4: fix'r'up here. return $newstring; } /* * Parse templates if they are defined */ function parse_package_templates() { global $pkg, $config; $rows = 0; if($pkg['templates']['template'] <> "") foreach($pkg['templates']['template'] as $pkg_template_row) { $filename = $pkg_template_row['filename']; $template_text = $pkg_template_row['templatecontents']; $firstfield = ""; /* calculate total row helpers count and */ /* change fields defined as fieldname_fieldvalue to their value */ foreach ($pkg['fields']['field'] as $fields) { switch($fields['type']){ case "rowhelper": // save rowhelper items. $row_helper_total_rows = 0; $row_helper_data = ""; foreach($fields['rowhelper']['rowhelperfield'] as $rowhelperfield) foreach($_POST as $key => $value){ if (preg_match("/^{$rowhelperfield['fieldname']}(\d+)$/",$key,$matches)){ $row_helper_total_rows++; $row_helper_data .= $value; $sep = ""; ereg($rowhelperfield['fieldname'] . "_fieldvalue\[(.*)\]", $template_text, $sep); foreach ($sep as $se) $separator = $se; if($separator <> "") { $row_helper_data = ereg_replace(" ", $separator, $row_helper_data); $template_text = ereg_replace("\[{$separator}\]", "", $template_text); } $template_text = str_replace($rowhelperfield['fieldname'] . "_fieldvalue", $row_helper_data, $template_text); } } break; default: $fieldname = $fields['fieldname']; $fieldvalue = $_POST[$fieldname]; $template_text = str_replace($fieldname . "_fieldvalue", $fieldvalue, $template_text); } } /* replace $domain_total_rows with total rows */ $template_text = str_replace("$domain_total_rows", $row_helper_total_rows, $template_text); /* replace cr's */ $template_text = str_replace("\\n", "\n", $template_text); /* write out new template file */ $fout = fopen($filename,"w"); fwrite($fout, $template_text); fclose($fout); } } /* Return html div fields */ function display_advanced_field($fieldname) { $div = "
\n"; $div .= " - " . gettext("Show advanced option") ."
\n"; $div .= "