profile_preview.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2016 Joe Clarke <jclarke@cisco.com>
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // 1. Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. // SUCH DAMAGE.
  24. include_once 'db.inc.php';
  25. include_once 'swreg/swreg.inc.php';
  26. require_once 'Log.php';
  27. require_once 'functions.php';
  28. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Port Profile Preview');
  29. if ($logger === false) {
  30. die("Failed to open logfile.\n");
  31. }
  32. $mask = Log::MAX(LOG_LEVEL);
  33. $logger->setMask($mask);
  34. $base = get_base($_SERVER['SCRIPT_NAME']);
  35. $pname = $_REQUEST['pname'];
  36. $pid = $_REQUEST['pid'];
  37. if (!isset($pname) || !isset($pid) || $pname == '') {
  38. echo "<font color=\"red\">You must specify a name and a PID</font>\n";
  39. exit(1);
  40. }
  41. $vlans = $_REQUEST['vlan'];
  42. $start_ports = $_REQUEST['start_port'];
  43. $end_ports = $_REQUEST['end_port'];
  44. $profile = '';
  45. $errors = array();
  46. $seen_sports = array();
  47. $seen_eports = array();
  48. $max_port = 0;
  49. for ($i = 0; $i < count($start_ports); ++$i) {
  50. $vlan = $vlans[$i];
  51. $start_port = $start_ports[$i];
  52. $end_port = $end_ports[$i];
  53. $row = $i + 1;
  54. if ($vlan == '__BOGUS__') {
  55. continue;
  56. }
  57. if ($start_port == '__BOGUS__' || $end_port == '__BOGUS__') {
  58. array_push($errors, "You must select a starting and ending port at row $row");
  59. continue;
  60. }
  61. if ($pid != '__ANY__') {
  62. preg_match("/\/(\d+)$/", $start_port, $sm);
  63. if ($sm[1] > $end_port) {
  64. array_push($errors, "Starting port must be less than equal to the ending port at row $row");
  65. continue;
  66. }
  67. } elseif ($vlan == 'DYNAMIC') {
  68. array_push($errors, "In order to make ports dynamic, you must choose an explicit PID at row $row");
  69. continue;
  70. }
  71. if (isset($seen_sports[$start_port])) {
  72. array_push($errors, "Starting port $start_port was already used at row {$seen_sports[$start_port]}");
  73. continue;
  74. }
  75. $seen_sports[$start_port] = $row;
  76. if (isset($seen_eports[$end_port])) {
  77. array_push($errors, "Ending port $end_port was already used at row {$seen_eports[$end_port]}");
  78. continue;
  79. }
  80. $seen_eports[$end_port] = $row;
  81. $profile .= generate_port_profile($start_port, $end_port, $vlan, $pid);
  82. $profile .= "!\n";
  83. if ($pid != '__ANY__' && $end_port > $max_port) {
  84. $max_port = $end_port;
  85. }
  86. }
  87. if ($profile == '') {
  88. array_push($errors, 'No profile ranges specified');
  89. }
  90. if (!preg_match("/^[\w\d\-_]+$/", $pname)) {
  91. array_push($errors, 'Profile name can only contain letters, numbers, hyphens, and underscores');
  92. }
  93. if ($pid != '__ANY__') {
  94. $ports = 8;
  95. foreach ($PID_PORTS as $pp => $np) {
  96. if ($pid == $pp) {
  97. $ports = $np;
  98. break;
  99. }
  100. }
  101. if ($max_port != $ports) {
  102. array_push($errors, 'All ports must be specified for a port profile');
  103. }
  104. }
  105. $profile = preg_replace('/"/', '\\"', $profile);
  106. $profile = preg_replace("/\r\n/", '\\n', $profile);
  107. $profile = preg_replace("/\n/", '\\\\n', $profile);
  108. ?>
  109. <!DOCTYPE html>
  110. <html>
  111. <head>
  112. <title>Port Profile Preview ::
  113. <?= $pname ?>
  114. </title>
  115. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  116. <script src="codemirror.js"></script>
  117. <link rel="stylesheet" href="codemirror.css" />
  118. <link rel="stylesheet" href="midnight.css" />
  119. <script src="cisco-config.js"></script>
  120. </head>
  121. <body>
  122. <hr />
  123. <?php
  124. if (count($errors) > 0) {
  125. foreach ($errors as $error) {
  126. ?>
  127. <p style="color: red">
  128. <?= $error ?>
  129. </p>
  130. <?php
  131. }
  132. } else {
  133. ?>
  134. <script language="Javascript">
  135. var profileViewer = CodeMirror(document.body, {
  136. value: "<?= $profile ?>",
  137. mode: "cisco-config",
  138. lineNumbers: true,
  139. theme: "midnight",
  140. readOnly: true
  141. });
  142. profileViewer.setSize(null, 600);
  143. </script>
  144. <?php
  145. }
  146. ?>
  147. <hr />
  148. </body>
  149. </html>
  150. <?php
  151. cleanup();
  152. ?>