add_phys.php 5.1 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. $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 . ' : Add Physical Switch');
  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. $errors = array();
  47. $ruser = '<AUTHENTICATION NOT ENABLED>';
  48. if (isset($_SERVER['REMOTE_USER'])) {
  49. $ruser = $_SERVER['REMOTE_USER'];
  50. }
  51. if (isset($_REQUEST['submit'])) {
  52. $sn = $_REQUEST['sn'];
  53. $pid = $_REQUEST['pid'];
  54. $ports = [];
  55. for ($i = 0; $i < count($sn); ++$i) {
  56. if ($sn[$i] === null || $sn[$i] == '') {
  57. continue;
  58. }
  59. foreach ($PID_PORTS as $pp => $np) {
  60. if ($pid[$i] == $pp) {
  61. $ports[$i] = $np;
  62. break;
  63. }
  64. }
  65. if ($pid[$i] == '__BOGUS__') {
  66. array_push($errors, 'Please select a Product ID on row ' . ($i + 1));
  67. }
  68. $sn[$i] = trim($sn[$i]);
  69. if (count($errors) == 0) {
  70. $sql = 'REPLACE INTO DEVICE_MAP (serial_number, pid, max_ports) VALUES (?, ?, ?)';
  71. try {
  72. $sth = $dbh->prepare($sql);
  73. $sth->execute(array($sn[$i], $pid[$i], $ports[$i]));
  74. $sth->closeCursor();
  75. $logger->info("User {$ruser} added physical switch: serial_number={$sn[$i]} and pid={$pid[$i]}");
  76. } catch (PDOException $e) {
  77. array_push($errors, 'Failed to add new physical switch at row ' . ($i + 1) . ": {$e->getMessage()}");
  78. }
  79. }
  80. }
  81. if (count($errors) == 0) {
  82. cleanup();
  83. header("Location: $base/index.php");
  84. exit;
  85. }
  86. }
  87. print_header(TOOL_NAME . ': Add Physical Switch');
  88. ?>
  89. <body class="flex">
  90. <div id="headswreg">
  91. <div id="header">
  92. <h1>
  93. <?= TOOL_NAME ?>: Add Physical Switch
  94. </h1>
  95. </div>
  96. <br />
  97. </div>
  98. <div id="page">
  99. <div id="itemhead">
  100. <h2 class="tabeltitle">Physical Switches</h2>
  101. </div>
  102. <div align="center">
  103. <?php
  104. foreach ($errors as $error) {
  105. ?>
  106. <p class="error">
  107. <?= $error ?>
  108. </p>
  109. <?php
  110. }
  111. ?>
  112. </div>
  113. <form name="phys_switch_add_form" method="POST" action="<?= $_SERVER['PHP_SELF'] ?>">
  114. <table class="full" summary="New Physical Switch Table">
  115. <tr>
  116. <th>Row No.</th>
  117. <th>Serial Number</th>
  118. <th>Product ID</th>
  119. </tr>
  120. <?php
  121. for ($i = 0; $i < 10; ++$i) {
  122. ?>
  123. <tr <?= get_row_color($i) ?>>
  124. <td>
  125. <?= $i + 1 ?>.
  126. </td>
  127. <td><input type="text" size="16" name="sn[<?= $i ?>]"></td>
  128. <td><select name="pid[<?= $i ?>]">
  129. <option value="__BOGUS__" selected>--Please Select--</option>
  130. <?php
  131. foreach ($ZTP_PIDS as $p) {
  132. ?>
  133. <option value="<?= $p ?>">
  134. <?= $p ?>
  135. </option>
  136. <?php
  137. } ?>
  138. </select></td>
  139. </tr>
  140. <?php
  141. }
  142. ?>
  143. </table>
  144. <br />
  145. <div align="center">
  146. <input type="reset" name="reset" value="Reset">
  147. <input type="submit" name="submit" value="Add">
  148. </div>
  149. </form>
  150. <table class="noborder" summary="Return Table">
  151. <tr>
  152. <td class="left_act">
  153. <a href="<?= $base ?>/index.php">&lt;&lt;&lt;Return to Main Page</a>
  154. </td>
  155. </tr>
  156. </table>
  157. </div>
  158. </body>
  159. </html>
  160. <?php
  161. cleanup();
  162. ?>