PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/local/www/system_routes_edit.php

https://github.com/vongrippen/pfsense
PHP | 351 lines | 282 code | 29 blank | 40 comment | 43 complexity | 79ac54f16047ed74db9b61f401c198a4 MD5 | raw file
  1. <?php
  2. /*
  3. system_routes_edit.php
  4. part of m0n0wall (http://m0n0.ch/wall)
  5. Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
  6. Copyright (C) 2010 Scott Ullrich
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  18. AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. pfSense_MODULE: routing
  28. */
  29. ##|+PRIV
  30. ##|*IDENT=page-system-staticroutes-editroute
  31. ##|*NAME=System: Static Routes: Edit route page
  32. ##|*DESCR=Allow access to the 'System: Static Routes: Edit route' page.
  33. ##|*MATCH=system_routes_edit.php*
  34. ##|-PRIV
  35. function staticroutecmp($a, $b) {
  36. return strcmp($a['network'], $b['network']);
  37. }
  38. function staticroutes_sort() {
  39. global $g, $config;
  40. if (!is_array($config['staticroutes']['route']))
  41. return;
  42. usort($config['staticroutes']['route'], "staticroutecmp");
  43. }
  44. require("guiconfig.inc");
  45. if (!is_array($config['staticroutes']['route']))
  46. $config['staticroutes']['route'] = array();
  47. $a_routes = &$config['staticroutes']['route'];
  48. $a_gateways = return_gateways_array(true);
  49. $id = $_GET['id'];
  50. if (isset($_POST['id']))
  51. $id = $_POST['id'];
  52. if (isset($_GET['dup'])) {
  53. $id = $_GET['dup'];
  54. }
  55. if (isset($id) && $a_routes[$id]) {
  56. list($pconfig['network'],$pconfig['network_subnet']) =
  57. explode('/', $a_routes[$id]['network']);
  58. $pconfig['gateway'] = $a_routes[$id]['gateway'];
  59. $pconfig['descr'] = $a_routes[$id]['descr'];
  60. }
  61. if (isset($_GET['dup']))
  62. unset($id);
  63. if ($_POST) {
  64. unset($input_errors);
  65. $pconfig = $_POST;
  66. /* input validation */
  67. $reqdfields = explode(" ", "network network_subnet gateway");
  68. $reqdfieldsn = explode(",",
  69. gettext("Destination network") . "," .
  70. gettext("Destination network bit count") . "," .
  71. gettext("Gateway"));
  72. do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
  73. if (($_POST['network'] && !is_ipaddr($_POST['network']))) {
  74. $input_errors[] = gettext("A valid destination network must be specified.");
  75. }
  76. if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
  77. $input_errors[] = gettext("A valid destination network bit count must be specified.");
  78. }
  79. if ($_POST['gateway']) {
  80. if (!isset($a_gateways[$_POST['gateway']]))
  81. $input_errors[] = gettext("A valid gateway must be specified.");
  82. if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
  83. $input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
  84. }
  85. /* check for overlaps */
  86. if(is_ipaddrv6($_POST['network'])) {
  87. $osn = Net_IPv6::compress(gen_subnetv6($_POST['network'], $_POST['network_subnet'])) . "/" . $_POST['network_subnet'];
  88. }
  89. if(is_ipaddrv4($_POST['network'])) {
  90. if($_POST['network_subnet'] > 32)
  91. $input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
  92. else
  93. $osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
  94. }
  95. foreach ($a_routes as $route) {
  96. if (isset($id) && ($a_routes[$id]) && ($a_routes[$id] === $route))
  97. continue;
  98. if ($route['network'] == $osn) {
  99. $input_errors[] = gettext("A route to this destination network already exists.");
  100. break;
  101. }
  102. }
  103. if (!$input_errors) {
  104. $route = array();
  105. $route['network'] = $osn;
  106. $route['gateway'] = $_POST['gateway'];
  107. $route['descr'] = $_POST['descr'];
  108. if (!isset($id))
  109. $id = count($a_routes);
  110. if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
  111. $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
  112. else
  113. $toapplylist = array();
  114. $oroute = $a_routes[$id];
  115. $a_routes[$id] = $route;
  116. if (!empty($oroute)) {
  117. $osn = explode('/', $oroute['network']);
  118. $sn = explode('/', $route['network']);
  119. if ($oroute['network'] <> $route['network'])
  120. $toapplylist[] = "/sbin/route delete {$oroute['network']}";
  121. }
  122. file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
  123. staticroutes_sort();
  124. mark_subsystem_dirty('staticroutes');
  125. write_config();
  126. header("Location: system_routes.php");
  127. exit;
  128. }
  129. }
  130. $pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
  131. include("head.inc");
  132. ?>
  133. <body link="#0000CC" vlink="#0000CC" alink="#0000CC">
  134. <?php include("fbegin.inc"); ?>
  135. <?php if ($input_errors) print_input_errors($input_errors); ?>
  136. <form action="system_routes_edit.php" method="post" name="iform" id="iform">
  137. <table width="100%" border="0" cellpadding="6" cellspacing="0">
  138. <tr>
  139. <td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
  140. </tr>
  141. <tr>
  142. <td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
  143. <td width="78%" class="vtable">
  144. <input name="network" type="text" class="formfld unknown" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>">
  145. /
  146. <select name="network_subnet" class="formselect" id="network_subnet"
  147. <?php
  148. if(is_ipaddrv4($pconfig['network'])) {
  149. $size = 32;
  150. } else {
  151. $size = 128;
  152. }
  153. for ($i = $size; $i >= 1; $i--): ?>
  154. <option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected"; ?>>
  155. <?=$i;?>
  156. </option>
  157. <?php endfor; ?>
  158. </select>
  159. <br> <span class="vexpl"><?=gettext("Destination network for this static route"); ?></span></td>
  160. </tr>
  161. <tr>
  162. <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
  163. <td width="78%" class="vtable">
  164. <select name="gateway" id="gateway" class="formselect">
  165. <?php
  166. foreach ($a_gateways as $gateway) {
  167. echo "<option value='{$gateway['name']}' ";
  168. if ($gateway['name'] == $pconfig['gateway'])
  169. echo "selected";
  170. echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
  171. }
  172. ?>
  173. </select> <br />
  174. <div id='addgwbox'>
  175. <?=gettext("Choose which gateway this route applies to or"); ?> <a OnClick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
  176. </div>
  177. <div id='notebox'>
  178. </div>
  179. <div style="display:none" name ="status" id="status">
  180. </div>
  181. <div style="display:none" id="addgateway" name="addgateway">
  182. <p>
  183. <table border="1" style="background:#990000; border-style: none none none none; width:225px;"><tr><td>
  184. <table bgcolor="#990000" cellpadding="1" cellspacing="1">
  185. <tr><td>&nbsp;</td>
  186. <tr>
  187. <td colspan="2"><center><b><font color="white"><?=gettext("Add new gateway:"); ?></b></center></td>
  188. </tr>
  189. <tr><td>&nbsp;</td>
  190. <tr>
  191. <td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></td><td><input type="checkbox" id="defaultgw" name="defaultgw"></td>
  192. </tr>
  193. <tr>
  194. <td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></td>
  195. <td><select name="addinterfacegw" id="addinterfacegw">
  196. <?php $gwifs = get_configured_interface_with_descr();
  197. foreach($gwifs as $fif => $dif)
  198. echo "<option value=\"{$fif}\">{$dif}</option>\n";
  199. ?>
  200. </select></td>
  201. </tr>
  202. <tr>
  203. <td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></td><td><input id="name" name="name" value="GW"></td>
  204. </tr>
  205. <tr>
  206. <td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></td><td><input id="gatewayip" name="gatewayip"></td>
  207. </tr>
  208. <tr>
  209. <td align="right"><font color="white"><?=gettext("Description:"); ?></td><td><input id="gatewaydescr" name="gatewaydescr"></td>
  210. </tr>
  211. <tr><td>&nbsp;</td>
  212. <tr>
  213. <td colspan="2">
  214. <center>
  215. <div id='savebuttondiv'>
  216. <input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
  217. <input id="gwsave" type="Button" value="<?=gettext("Save Gateway"); ?>" onClick='hide_add_gatewaysave();'>
  218. <input id="gwcancel" type="Button" value="<?=gettext("Cancel"); ?>" onClick='hide_add_gateway();'>
  219. </div>
  220. </center>
  221. </td>
  222. </tr>
  223. <tr><td>&nbsp;</td>
  224. </table>
  225. </td></tr></table>
  226. <p/>
  227. </div>
  228. </tr>
  229. <tr>
  230. <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
  231. <td width="78%" class="vtable">
  232. <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
  233. <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span></td>
  234. </tr>
  235. <tr>
  236. <td width="22%" valign="top">&nbsp;</td>
  237. <td width="78%">
  238. <input id="save" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input id="cancel" type="button" value="<?=gettext("Cancel"); ?>" class="formbtn" onclick="history.back()">
  239. <?php if (isset($id) && $a_routes[$id]): ?>
  240. <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
  241. <?php endif; ?>
  242. </td>
  243. </tr>
  244. </table>
  245. </form>
  246. <script type="text/javascript">
  247. var gatewayip;
  248. var name;
  249. function show_add_gateway() {
  250. document.getElementById("addgateway").style.display = '';
  251. document.getElementById("addgwbox").style.display = 'none';
  252. document.getElementById("gateway").style.display = 'none';
  253. document.getElementById("save").style.display = 'none';
  254. document.getElementById("cancel").style.display = 'none';
  255. document.getElementById("gwsave").style.display = '';
  256. document.getElementById("gwcancel").style.display = '';
  257. $('notebox').innerHTML="";
  258. }
  259. function hide_add_gateway() {
  260. document.getElementById("addgateway").style.display = 'none';
  261. document.getElementById("addgwbox").style.display = '';
  262. document.getElementById("gateway").style.display = '';
  263. document.getElementById("save").style.display = '';
  264. document.getElementById("cancel").style.display = '';
  265. document.getElementById("gwsave").style.display = '';
  266. document.getElementById("gwcancel").style.display = '';
  267. }
  268. function hide_add_gatewaysave() {
  269. document.getElementById("addgateway").style.display = 'none';
  270. $('status').innerHTML = '<img src="/themes/metallic/images/misc/loader.gif"> <?=gettext("One moment please..."); ?>';
  271. var iface = $('addinterfacegw').getValue();
  272. name = $('name').getValue();
  273. var descr = $('gatewaydescr').getValue();
  274. gatewayip = $('gatewayip').getValue();
  275. addrtype = $('addrtype').getValue();
  276. var defaultgw = '';
  277. if ($('defaultgw').checked)
  278. defaultgw = 'yes';
  279. var url = "system_gateways_edit.php";
  280. var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
  281. var myAjax = new Ajax.Request(
  282. url,
  283. {
  284. method: 'post',
  285. parameters: pars,
  286. onFailure: report_failure,
  287. onComplete: save_callback
  288. });
  289. }
  290. function addOption(selectbox,text,value)
  291. {
  292. var optn = document.createElement("OPTION");
  293. optn.text = text;
  294. optn.value = value;
  295. selectbox.options.add(optn);
  296. selectbox.selectedIndex = (selectbox.options.length-1);
  297. $('notebox').innerHTML="<p/><strong><?=gettext("NOTE:");?></strong> <?php printf(gettext("You can manage Gateways %shere%s."), "<a target='_new' href='system_gateways.php'>", "</a>");?> </strong>";
  298. }
  299. function report_failure() {
  300. alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
  301. hide_add_gateway();
  302. }
  303. function save_callback(transport) {
  304. var response = transport.responseText;
  305. if (response) {
  306. document.getElementById("addgateway").style.display = 'none';
  307. hide_add_gateway();
  308. $('status').innerHTML = '';
  309. addOption($('gateway'), name, name);
  310. } else {
  311. report_failure();
  312. }
  313. }
  314. </script>
  315. <?php include("fend.inc"); ?>
  316. </body>
  317. </html>