123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- #-
- # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
- # 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 BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
- #
- #
- include_once 'db.inc.php';
- include_once 'swreg/swreg.inc.php';
- include_once 'swreg_web.inc.php';
- require_once 'MDB2.php';
- require_once 'Log.php';
- require_once 'functions.php';
- $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
- $options = array('result_buffering', false);
- $dbh = MDB2::factory($dsn, $options);
- if (PEAR::isError($dbh)) {
- die($dbh->getMessage());
- }
- $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
- $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Add Port Profile');
- if ($logger === FALSE) {
- die("Failed to open logfile.\n");
- }
- $mask = Log::UPTO(LOG_LEVEL);
- $logger->setMask($mask);
- $base = get_base($_SERVER['SCRIPT_NAME']);
- $errors = array();
- if (isset($_REQUEST['submit'])) {
- $pname = $_REQUEST['pname'];
- $pid = $_REQUEST['pid'];
- $vlans = $_REQUEST['vlan'];
- $start_ports = $_REQUEST['start_port'];
- $end_ports = $_REQUEST['end_port'];
- if (!isset($pname) || !isset($pid) || $pname == "") {
- array_push($errors, "You must specify a profile name and PID");
- } else {
- $profile = '';
- $seen_sports = array();
- $seen_eports = array();
- $max_port = 0;
- for ($i = 0; $i < count($start_ports); $i++) {
- $vlan = $vlans[$i];
- $start_port = $start_ports[$i];
- $end_port = $end_ports[$i];
- $row = $i + 1;
- if ($vlan == '__BOGUS__') {
- continue;
- }
- if ($start_port == '__BOGUS__' || $end_port == '__BOGUS__') {
- array_push($errors, "You must select a starting and ending port at row $row");
- continue;
- }
- if ($pid != "__ANY__") {
- preg_match("/\/(\d+)$/", $start_port, $sm);
- if ($sm[1] > $end_port) {
- array_push($errors, "Starting port must be less than equal to the ending port at row $row");
- continue;
- }
- } else if ($vlan == "DYNAMIC") {
- array_push($errors, "In order to make ports dynamic, you must choose an explicit PID at row $row");
- continue;
- }
- if (isset($seen_sports[$start_port])) {
- array_push($errors, "Starting port $start_port was already used at row {$seen_sports[$start_port]}");
- continue;
- }
- $seen_sports[$start_port] = $row;
- if (isset($seen_eports[$end_port])) {
- array_push($errors, "Ending port $end_port was already used at row {$seen_eports[$end_port]}");
- continue;
- }
- $seen_eports[$end_port] = $row;
- $profile .= generate_port_profile($start_port, $end_port, $vlan, $pid);
- $profile .= "!\n";
- if ($pid != "__ANY__" && $end_port > $max_port) {
- $max_port = $end_port;
- }
- }
- if ($profile == '') {
- array_push($errors, "No profile ranges specified");
- }
- if (!preg_match("/^[\w\d\-_]+$/", $pname)) {
- array_push($errors, "Profile name can only contain letters, numbers, hyphens, and underscores");
- }
- if ($pid != "__ANY__") {
- $ports = 8;
- foreach ($PID_PORTS as $pp => $np) {
- if ($pid == $pp) {
- $ports = $np;
- break;
- }
- }
- if ($max_port != $ports) {
- array_push($errors, "All ports must be specified for a port profile");
- }
- }
- }
- if (count($errors) == 0) {
- $fd = fopen(PORT_PROFILE_DIR . "/{$pname}.tmpl", "w");
- fwrite($fd, $profile);
- fclose($fd);
- $sql = "INSERT INTO EXCEPTIONS VALUES (?, ?, ?)";
- $sth = $dbh->prepare($sql);
- if (PEAR::isError($sth)) {
- array_push($errors, "Failed to prepare query $sql: {$sth->getUserInfo()}");
- } else {
- if (isset($PID_MAP[$pid])) {
- $pid = $PID_MAP[$pid];
- }
- $res = $sth->execute(array($pname, $pid, "{$pname}.tmpl"));
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to execute query $sql: {$res->getUserInfo()}");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} add port profile: name={$pname}, pid={$pid}, path={$pname}.tmpl");
- }
- }
- cleanup();
- if (count($errors) == 0) {
- header("Location: $base/port_profiles.php");
- exit;
- }
- }
- }
- $nports = array_values($PID_PORTS);
- rsort($nports);
- $mport = $nports[0];
- print_header(TOOL_NAME . ": Add Port Profile");
- ?>
- <script language="Javascript">
- var lastStart = 1;
- var PID_PORTS = [];
- var PORT_PATTERNS = [];
- <?php
- foreach ($PID_PORTS as $pp => $np) {
- ?>
- PID_PORTS["<?=$pp?>"] = <?=$np?>;
- <?php
- }
- foreach ($PORT_PATTERNS as $p => $pa) {
- ?>
- PORT_PATTERNS["<?=$p?>"] = "<?=$pa?>";
- <?php
- }
- ?>
- function setLastStart(port, increment) {
- var regex = /([0-9]+)$/;
- try {
- var matches = regex.exec(port);
- lastStart = matches[1];
- if (increment) {
- lastStart++;
- }
- } catch (err) { }
- }
- function purgePorts() {
- for (var i = 0; i < <?=$mport?>; i++) {
- var sSelect = document.getElementById("start_port[" + i + "]");
- var eSelect = document.getElementById("end_port[" + i + "]");
- deleteOptions(sSelect);
- deleteOptions(eSelect);
- createOption(sSelect, '--Please Select--', '__BOGUS__');
- createOption(eSelect, '--Please Select--', '__BOGUS__');
- }
- }
- function updatePorts(pid, sidx, eidx, send, eend) {
- if (typeof send === undefined) {
- send = <?=$mport?>;
- }
- if (typeof eend === undefined) {
- eend = <?=$mport?>;
- }
- for (var i = sidx; i < send; i++) {
- var sSelect = document.getElementById("start_port[" + i + "]");
- if (pid == "__ANY__") {
- createOption(sSelect, 'First Switch Port', '%%START_PORT%%');
- } else {
- endp = PID_PORTS[pid] + 1;
- ppat = PORT_PATTERNS[pid];
- for (var j = lastStart; j < endp; j++) {
- createOption(sSelect, ppat+j, ppat+j);
- }
- }
- }
- for (var i = eidx; i < eend; i++) {
- var eSelect = document.getElementById("end_port[" + i + "]");
- if (pid == "__ANY__") {
- createOption(eSelect, 'Last Switch Port', '%%MAX_PORT%%');
- } else {
- endp = PID_PORTS[pid] + 1;
- for (var j = lastStart; j < endp; j++) {
- createOption(eSelect, j, j);
- }
- }
- }
- }
- </script>
- <body class="flex" onLoad='purgePorts(); updatePorts("__ANY__", 0, 0, 1, 0); return true;'>
- <div id="headswreg">
- <div id="header">
- <h1><?=TOOL_NAME?>: Add Port Profile</h1>
- </div>
- <br/>
- </div>
- <div class="apage">
- <form name="port_profile_add_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>">
- <table class="noborder" summary="Profile Options">
- <tr>
- <td class="left_act">Profile Name:
- <input type="text" name="pname" size="16">
- </td>
- <td>Select PID:
- <select name="pid" id="pid" onChange='purgePorts(); updatePorts(this.options[this.selectedIndex].value, 0, 0, 1, 0); return true;'>
- <option value="__ANY__" selected>Any</option>
- <?php
- foreach ($PID_PORTS as $p => $n) {
- ?>
- <option value="<?=$p?>"><?=$p?></option>
- <?php
- }
- ?>
- </select></td>
- </tr>
- </table>
- </div>
- <div id="page">
- <div id="itemhead">
- <h2 class="tabeltitle">Port Profile</h2>
- </div>
- <div align="center">
- <?php
- foreach ($errors as $error) {
- ?>
- <p class="error"><?=$error?></p>
- <?php
- }
- ?>
- </div>
- <table class="full" summary="New Port Profile Table">
- <tr>
- <th>Row No.</th>
- <th>Starting Port</th>
- <th>Ending Port</th>
- <th>VLAN</th>
- </tr>
- <?php
- for ($i = 0; $i < $mport; $i++) {
- ?>
- <tr <?=get_row_color($i)?>>
- <td><?=$i + 1?>.</td>
- <td><select name="start_port[<?=$i?>]" id="start_port[<?=$i?>]" onChange='setLastStart(this.options[this.selectedIndex].value, false); pSelect = document.getElementById("pid"); updatePorts(pSelect.options[pSelect.selectedIndex].value, <?=$i+1?>, <?=$i?>, <?=$i+1?>, <?=$i+1?>); return true;'>
- </select></td>
- <td><select name="end_port[<?=$i?>]" id="end_port[<?=$i?>]" onChange='setLastStart(this.options[this.selectedIndex].value, true); pSelect = document.getElementById("pid"); updatePorts(pSelect.options[pSelect.selectedIndex].value, <?=$i+1?>, <?=$i+1?>, <?=$i+2?>, <?=$i+1?>); return true;'>
- </select></td>
- <td><select name="vlan[<?=$i?>]">
- <option value="__BOGUS__" selected>--Please Select--</option>
- <option value="DYNAMIC">DYNAMIC</option>
- <option value="TRUNK">TRUNK</option>
- <option value="UPLINK TRUNK">UPLINK TRUNK</option>
- <option disabled>─────────────</option>
- <?php
- ksort($VLANS);
- foreach ($VLANS as $vn => $vi) {
- ?>
- <option value="<?=$vn?>"><?=$vn?></option>
- <?php
- }
- ?>
- </select></td>
- </tr>
- <?php
- }
- ?>
- </table>
- <br/>
- <div align="center">
- <input type="reset" name="reset" value="Reset">
- <input type="submit" name="preview" value="Preview" onClick='this.form.action="<?=$base?>/profile_preview.php"; this.form.target="_blank";'>
- <input type="submit" name="submit" value="Add" onClick='this.form.action="<?=$_SERVER['PHP_SELF']?>"; this.form.target="_self";'>
- </div>
- </form>
- <table class="noborder" summary="Return Table">
- <tr>
- <td class="left_act">
- <a href="<?=$base?>/port_profiles.php"><<<Return to Main Page</a>
- </td>
- </tr>
- </table>
- </div>
- </body>
- </html>
- <?php
- cleanup();
- ?>
|