123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- //-
- // Copyright (c) 2011-2016 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';
- require_once 'Log.php';
- require_once 'functions.php';
- $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Port Profile Preview');
- if ($logger === false) {
- die("Failed to open logfile.\n");
- }
- $mask = Log::MAX(LOG_LEVEL);
- $logger->setMask($mask);
- $base = get_base($_SERVER['SCRIPT_NAME']);
- $pname = $_REQUEST['pname'];
- $pid = $_REQUEST['pid'];
- if (!isset($pname) || !isset($pid) || $pname == '') {
- echo "<font color=\"red\">You must specify a name and a PID</font>\n";
- exit(1);
- }
- $vlans = $_REQUEST['vlan'];
- $start_ports = $_REQUEST['start_port'];
- $end_ports = $_REQUEST['end_port'];
- $profile = '';
- $errors = array();
- $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;
- }
- } elseif ($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');
- }
- }
- $profile = preg_replace('/"/', '\\"', $profile);
- $profile = preg_replace("/\r\n/", '\\n', $profile);
- $profile = preg_replace("/\n/", '\\\\n', $profile);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Port Profile Preview ::
- <?= $pname ?>
- </title>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <script src="codemirror.js"></script>
- <link rel="stylesheet" href="codemirror.css" />
- <link rel="stylesheet" href="midnight.css" />
- <script src="cisco-config.js"></script>
- </head>
- <body>
- <hr />
- <?php
- if (count($errors) > 0) {
- foreach ($errors as $error) {
- ?>
- <p style="color: red">
- <?= $error ?>
- </p>
- <?php
- }
- } else {
- ?>
- <script language="Javascript">
- var profileViewer = CodeMirror(document.body, {
- value: "<?= $profile ?>",
- mode: "cisco-config",
- lineNumbers: true,
- theme: "midnight",
- readOnly: true
- });
- profileViewer.setSize(null, 600);
- </script>
- <?php
- }
- ?>
- <hr />
- </body>
- </html>
- <?php
- cleanup();
- ?>
|