add_phys.php 5.6 KB

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