profile_preview.php 4.9 KB

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