port_profiles.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2017 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. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  29. $options = [
  30. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  31. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  32. PDO::ATTR_EMULATE_PREPARES => false,
  33. ];
  34. try {
  35. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  36. } catch (PDOException $e) {
  37. die($e->getMessage());
  38. }
  39. $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Port Profiles');
  40. if ($logger === false) {
  41. die("Failed to open logfile.\n");
  42. }
  43. $mask = Log::MAX(LOG_LEVEL);
  44. $logger->setMask($mask);
  45. $base = get_base($_SERVER['SCRIPT_NAME']);
  46. $ruser = '<AUTHENTICATION NOT ENABLED>';
  47. if (isset($_SERVER['REMOTE_USER'])) {
  48. $ruser = $_SERVER['REMOTE_USER'];
  49. }
  50. $errors = array();
  51. if (isset($_REQUEST['submit'])) {
  52. $d = $_REQUEST['delete'];
  53. foreach ($d as $profile => $value) {
  54. if ($value == 1) {
  55. $sql = "DELETE FROM EXCEPTIONS WHERE name=:name";
  56. try {
  57. $sth = $dbh->prepare($sql);
  58. $sth->execute(['name' => $profile]);
  59. $sth->closeCursor();
  60. unlink(PORT_PROFILE_DIR . "/{$profile}.tmpl");
  61. $logger->info("User {$ruser} deleted port profile {$profile}");
  62. } catch (PDOException $e) {
  63. array_push($errors, "Failed to delete {$profile}: {$e->getMessage()}");
  64. }
  65. }
  66. }
  67. }
  68. $wc = '';
  69. $params = null;
  70. if (isset($_REQUEST['filter'])) {
  71. if ($_REQUEST['filter'] != 'All') {
  72. $wc = " WHERE pid = :pid";
  73. $params = ['pid' => $_REQUEST['filter']];
  74. }
  75. }
  76. $sql = 'SELECT EXCEPTIONS.* FROM EXCEPTIONS ' . $wc . ' ORDER BY name';
  77. if (isset($_REQUEST['profile_name'])) {
  78. $sql = "SELECT * FROM EXCEPTIONS WHERE name LIKE :name ORDER BY name";
  79. $params = ['name' => "%{$_REQUEST['profile_name']}%"];
  80. }
  81. $res = null;
  82. try {
  83. $res = $dbh->prepare($sql);
  84. $res->execute($params);
  85. } catch (PDOException $e) {
  86. echo "<p><font color=\"red\">Error querying for port profiles: {$e->getMessage()}</font></p>\r\n";
  87. exit(1);
  88. }
  89. $profiles = array();
  90. while ($row = $res->fetch()) {
  91. array_push($profiles, $row);
  92. }
  93. $filter = '';
  94. if (isset($_REQUEST['filter'])) {
  95. $filter = $_REQUEST['filter'];
  96. }
  97. print_header(TOOL_NAME . ': Port Profiles');
  98. ?>
  99. <body class="flex">
  100. <script language="javascript">
  101. var deletes = 0;
  102. $(document).ready(function () {
  103. $('#devtable').DataTable({
  104. "scrollY": "400px",
  105. "scrollCollapse": true,
  106. "paging": false
  107. });
  108. });
  109. </script>
  110. <div id="headswreg">
  111. <div class="apage">
  112. <div id="header">
  113. <h1>
  114. <?= TOOL_NAME ?>: Port Profiles
  115. </h1>
  116. </div>
  117. <br />
  118. </div>
  119. </div>
  120. <div class="apage">
  121. <form method="POST" name="search_form" action="<?= $_SERVER['PHP_SELF'] ?>">
  122. <table class="noborder" summary="Filter Table">
  123. <tr>
  124. <td class="left_act">Show Only Port Profiles For:
  125. <select name="filter" onChange="MM_jumpMenu('parent', this, 0)">
  126. <option value="<?= $_SERVER['PHP_SELF'] ?>?filter=All" <?= ($filter == 'All') ? 'selected' : '' ?>>All
  127. </option>
  128. <option value="<?= $_SERVER['PHP_SELF'] ?>?filter=__ANY__" <?= ($filter == '__ANY__') ? 'selected' : '' ?>>
  129. Any
  130. Switch</option>
  131. <?php
  132. foreach ($ZTP_PIDS as $spid) {
  133. $selected = ($filter == "$spid") ? 'selected' : ''; ?>
  134. <option value="<?= $_SERVER['PHP_SELF'] ?>?filter=<?= $spid ?>" <?= $selected ?>>
  135. <?= $spid ?>
  136. </option>
  137. <?php
  138. }
  139. ?>
  140. </select>
  141. </td>
  142. <td class="right">Profile Search:
  143. <input type="text" size="16" name="profile_name"
  144. value="<?= (isset($_REQUEST['profile_name'])) ? $_REQUEST['profile_name'] : '' ?>">
  145. <input type="image" value="Submit" name="search" src="/images/submit_button.png">
  146. </td>
  147. </tr>
  148. </table>
  149. </form>
  150. <br />
  151. <table class="noborder" summary="Control Table">
  152. <tr>
  153. <td class="left"><a href="<?= $base ?>/add_port_profile.php">Add Port Profile</a> |
  154. <a href="<?= $base ?>/logicsw.php">Logical Switches</a>
  155. </td>
  156. </tr>
  157. </table>
  158. <form name="mod_port_profile_form" method="POST" action="<?= $_SERVER['PHP_SELF'] ?>"
  159. onSubmit='if (deletes > 0) { return confirm("Are you sure you want to delete these port profiles?"); }'>
  160. <?php
  161. foreach (array_merge($_GET, $_POST) as $name => $value) {
  162. ?>
  163. <input type="hidden" name="<?= $name ?>" value="<?= $value ?>">
  164. <?php
  165. }
  166. ?>
  167. <div class="fbtable">
  168. <table summary="Button Table" width="100%" cellspacing="0">
  169. <tr>
  170. <td><input type="reset" value="Reset" name="reset">
  171. <input type="submit" value="Submit" name="submit">
  172. </td>
  173. </tr>
  174. </table>
  175. </div>
  176. <br />
  177. <div align="center">
  178. <?php
  179. foreach ($errors as $error) {
  180. ?>
  181. <p class="error">
  182. <?= $error ?>
  183. </p>
  184. <?php
  185. }
  186. ?>
  187. </div>
  188. <div class="fulltable">
  189. <table id="devtable" class="display compact" width="100%" cellspacing="0" summary="Port Profile Table">
  190. <thead>
  191. <tr>
  192. <th class="headlink">Row No.</th>
  193. <th class="headlink">Delete?</th>
  194. <th class="headlink">Name</th>
  195. <th class="headlink">Product ID</th>
  196. <th class="headlink">File Name</th>
  197. <th class="headlink">View</th>
  198. </tr>
  199. </thead>
  200. <tbody>
  201. <?php
  202. $i = 0;
  203. foreach ($profiles as $row) {
  204. $name = $row['name']; ?>
  205. <tr>
  206. <td>
  207. <?= $i + 1 ?>.
  208. </td>
  209. <td><input type="checkbox" name="delete[<?= $name ?>]" value="1"
  210. onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
  211. <td>
  212. <?= $name ?>
  213. </td>
  214. <td>
  215. <?= $row['pid'] ?>
  216. </td>
  217. <td>
  218. <?= $row['path'] ?>
  219. </td>
  220. <td><a href="#"
  221. onClick='window.open("<?= $base ?>/show_config.php?type=profile&cfg=<?= $row['path'] ?>&dname=<?= $row['name'] ?>", "Config for profile <?= $row['name'] ?>", "height=650,width=980"); return false;'>View</a>
  222. </td>
  223. </tr>
  224. <?php
  225. ++$i;
  226. }
  227. ?>
  228. </tbody>
  229. </table>
  230. </div>
  231. <br />
  232. <div class="fbtable">
  233. <table width="100%" cellspacing="0" summary="Button Table">
  234. <tr>
  235. <td><input type="reset" value="Reset" name="reset">
  236. <input type="submit" value="Submit" name="submit">
  237. </td>
  238. </tr>
  239. </table>
  240. </div>
  241. </form>
  242. </div>
  243. </body>
  244. </html>
  245. <?php
  246. cleanup();
  247. ?>