add_logic.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 Logical 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. $name = $_REQUEST['name'];
  51. $pid = $_REQUEST['pid'];
  52. $mdf = $_REQUEST['mdf'];
  53. $idf = $_REQUEST['idf'];
  54. $is_idf = $_REQUEST['is_idf'];
  55. $ports = $_REQUEST['ports'];
  56. for ($i = 0; $i < count($name); $i++) {
  57. if ($name[$i] === NULL || $name[$i] == "") {
  58. continue;
  59. }
  60. if ($pid[$i] == '__BOGUS__') {
  61. array_push($errors, 'Please select a Product ID on row ' . ($i + 1));
  62. }
  63. if ($ports[$i] == '__BOGUS__') {
  64. array_push($errors, 'Please select ports required on row ' . ($i + 1));
  65. }
  66. if ($mdf[$i] == '__BOGUS__') {
  67. array_push($errors, 'Please select an MDF for row ' . ($i + 1));
  68. }
  69. if ($idf[$i] == '__BOGUS__' && $is_idf[$i] == 0) {
  70. array_push($errors, 'Please select an IDF for row ' . ($i + 1));
  71. }
  72. $name[$i] = trim($name[$i]);
  73. if (count($errors) == 0) {
  74. $ip = NULL;
  75. if ($is_idf[$i] == 0) {
  76. $sql = 'SELECT ip_address FROM SWITCHES WHERE name = ?';
  77. $sth = $dbh->prepare($sql);
  78. if (PEAR::isError($sth)) {
  79. array_push($errors, "Failed to prepare query for row " . ($i + 1) . ": {$sth->getUserInfo()}");
  80. }
  81. $res = $sth->execute(array($idf[$i]));
  82. $sth->free();
  83. if (PEAR::isError($res)) {
  84. array_push($errors, "Failed to execute query for row " . ($i + 1) . ": {$res->getUserInfo()}");
  85. }
  86. $row = $res->fetchRow();
  87. preg_match("/^(\d+\.\d+\.\d+\.)/", $row['ip_address'], $matches);
  88. $sql = 'SELECT address FROM ADDRESSES WHERE location = ? AND address LIKE ? AND used = ? AND reserved = ? ORDER BY INET_ATON(address) LIMIT 1';
  89. $sth = $dbh->prepare($sql);
  90. if (PEAR::isError($sth)) {
  91. array_push($errors, "Failed to prepare query for row " . ($i + 1) . ": {$sth->getUserInfo()}");
  92. } else {
  93. $res = $sth->execute(array($mdf[$i], $matches[1].'%', '0', '0'));
  94. $sth->free();
  95. if (PEAR::isError($res)) {
  96. array_push($errors, "Failed to execute query for row " . ($i + 1) . ": {$res->getUserInfo()}");
  97. }
  98. $row = $res->fetchRow();
  99. $ip = $row['address'];
  100. }
  101. }
  102. if (count($errors) == 0) {
  103. $sql = 'REPLACE INTO SWITCHES (name, pid, is_idf, ip_address, ports_required) VALUES (?, ?, ?, ?, ?)';
  104. $sth = $dbh->prepare($sql);
  105. if (PEAR::isError($sth)) {
  106. array_push($errors, "Failed to prepare query for row " . ($i + 1) . ": {$sth->getUserInfo()}");
  107. } else {
  108. $res = $sth->execute(array($name[$i], $pid[$i], $is_idf[$i], $ip, $ports[$i]));
  109. $sth->free();
  110. if (PEAR::isError($res)) {
  111. array_push($errors, "Failed to execute query for row " . ($i + 1) . ": {$res->getUserInfo()}");
  112. } else {
  113. $logger->info("User {$_SERVER['REMOTE_USER']} added new logical switch: name={$name[$i]}, pid={$pid[$i]}, is_idf={$is_idf[$i]}, ip_adress={$ip}, ports_required={$ports[$i]}");
  114. }
  115. $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
  116. $sth = $dbh->prepare($sql);
  117. if (PEAR::isError($sth)) {
  118. array_push($errors, "Failed to prepare query for row " . ($i + 1) . ": {$sth->getUserInfo()}");
  119. } else {
  120. $res = $sth->execute(array('1', $ip));
  121. $sth->free();
  122. if (PEAR::isError($res)) {
  123. array_push($errors, "Failed to execute query for row " . ($i + 1) . ": {$res->getUserInfo()}");
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. if (count($errors) == 0) {
  131. cleanup();
  132. header("Location: $base/logicsw.php");
  133. exit;
  134. }
  135. }
  136. $idfs = array();
  137. foreach ($MDFS as $m) {
  138. $sql = 'SELECT s.name AS name FROM SWITCHES s, ADDRESSES a WHERE a.location = ? AND a.address = s.ip_address AND s.is_idf = ?';
  139. $sth = $dbh->prepare($sql);
  140. if (!PEAR::isError($sth)) {
  141. $res = $sth->execute(array($m, '1'));
  142. $sth->free();
  143. if (!PEAR::isError($res)) {
  144. $idfs[$m] = array();
  145. while ($row = $res->fetchRow()) {
  146. array_push($idfs[$m], $row['name']);
  147. }
  148. }
  149. }
  150. }
  151. print_header(TOOL_NAME . ": Add Logical Switch");
  152. ?>
  153. <script language="Javascript">
  154. var idfs = JSON.parse('<?=json_encode($idfs)?>');
  155. function onMDFChange(mdf, row) {
  156. var midfs = idfs[mdf];
  157. var sel = document.getElementById('idf['+row+']');
  158. deleteOptions(sel);
  159. createOption(sel, '--Please Select--', '__BOGUS__');
  160. for (mi in midfs) {
  161. createOption(sel, midfs[mi], midfs[mi]);
  162. }
  163. }
  164. </script>
  165. <body class="flex">
  166. <div id="headswreg">
  167. <div id="header">
  168. <h1><?=TOOL_NAME?>: Add Logical Switch</h1>
  169. </div>
  170. <br/>
  171. </div>
  172. <div id="page">
  173. <div id="itemhead">
  174. <h2 class="tabeltitle">Logical Switches</h2>
  175. </div>
  176. <div align="center">
  177. <?php
  178. foreach ($errors as $error) {
  179. ?>
  180. <p class="error"><?=$error?></p>
  181. <?php
  182. }
  183. ?>
  184. </div>
  185. <form name="logical_switch_add_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>">
  186. <table class="full" summary="New Logical Switch Table">
  187. <tr>
  188. <th>Row No.</th>
  189. <th>Hostname</th>
  190. <th>Product ID</th>
  191. <th>MDF</th>
  192. <th>Is IDF?</th>
  193. <th>IDF</th>
  194. <th>Ports Required</th>
  195. </tr>
  196. <?php
  197. for ($i = 0; $i < 10; $i++) {
  198. ?>
  199. <tr <?=get_row_color($i)?>>
  200. <td><?=$i + 1?>.</td>
  201. <td><input type="text" size="16" name="name[<?=$i?>]"></td>
  202. <td><select name="pid[<?=$i?>]">
  203. <option value="__BOGUS__" selected>--Please Select--</option>
  204. <?php
  205. foreach ($ZTP_PIDS as $p) {
  206. ?>
  207. <option value="<?=$p?>"><?=$p?></option>
  208. <?php
  209. }
  210. ?>
  211. </select></td>
  212. <td><select name="mdf[<?=$i?>]" onChange='onMDFChange(this.options[this.selectedIndex].value, <?=$i?>); return true;'>
  213. <option value="__BOGUS__" selected>--Please Select--</option>
  214. <?php
  215. foreach ($MDFS as $m) {
  216. ?>
  217. <option value="<?=$m?>"><?=$m?></option>
  218. <?php
  219. }
  220. ?>
  221. </select></td>
  222. <td><input type="checkbox" name="is_idf[<?=$i?>]" value="1" onClick="if (this.checked == true) { this.document.getElementById('idf[<?=$i?>]').disabled = true; } else { this.document.getElementById('idf[<?=$i?>]').disabled = false; }"></td>
  223. <td><select name="idf[<?=$i?>]" id="idf[<?=$i?>]">
  224. <option value="__BOGUS__" selected>--Please Select--</option>
  225. </select></td>
  226. <td><select name="ports[<?=$i?>]" id="ports[<?=$i?>]">
  227. <option value="__BOGUS__" selected>--Please Select--</option>
  228. <?php
  229. $nports = array_values($PID_PORTS);
  230. sort($nports);
  231. foreach ($nports as $n) {
  232. ?>
  233. <option value="<?=$n?>"><?=$n?></option>
  234. <?php
  235. }
  236. ?>
  237. </select></td>
  238. </tr>
  239. <?php
  240. }
  241. ?>
  242. </table>
  243. <br/>
  244. <div align="center">
  245. <input type="reset" name="reset" value="Reset">
  246. <input type="submit" name="submit" value="Add">
  247. </div>
  248. </form>
  249. <table class="noborder" summary="Return Table">
  250. <tr>
  251. <td class="left_act">
  252. <a href="<?=$base?>/logicsw.php">&lt;&lt;&lt;Return to Main Page</a>
  253. </td>
  254. </tr>
  255. </table>
  256. </div>
  257. </body>
  258. </html>
  259. <?php
  260. cleanup();
  261. ?>