functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. require_once 'Log.php';
  29. function cleanup() {
  30. global $logger, $dbh;
  31. $dbh->disconnect();
  32. $logger->close();
  33. }
  34. function get_sort_icon($curr_sort, $sort, $order, $extsort = 0) {
  35. if ($extsort == 1) return "";
  36. if ($curr_sort == $sort) {
  37. $icon = "up_selected.png";
  38. $alt = "up";
  39. if (!$order) {
  40. $icon = "down_selected.png";
  41. $alt = "down";
  42. }
  43. return "<img src=\"" . ATTRITION_IMAGES_PATH . "/$icon\" width=\"11\" height=\"9\" border=\"0\" alt=\"$alt\">";
  44. }
  45. return "";
  46. }
  47. function parse_csv_string($string) {
  48. $fields = array();
  49. $string = chop($string, "\r\n");
  50. if ($string == "") {
  51. return $fields;
  52. }
  53. $tmpfields = explode(",", $string);
  54. for ($i = 0; $i < count($tmpfields); $i++) {
  55. $field = $tmpfields[$i];
  56. if (preg_match("/^\"[^\"]+$/", $field)) {
  57. $field = preg_replace("/^\"/", "", $field);
  58. while ($i < count($tmpfields)) {
  59. $field .= "," . $tmpfields[++$i];
  60. if (preg_match("/\"$/", $field)) {
  61. break;
  62. }
  63. }
  64. if ($i == count($tmpfields) &&
  65. !preg_match("/\"$/", $field)) {
  66. return NULL;
  67. }
  68. $field = preg_replace("/\"$/", "", $field);
  69. }
  70. array_push($fields, $field);
  71. }
  72. return $fields;
  73. }
  74. function get_new_sort($curr_sort, $sort, $order) {
  75. if ($curr_sort == $sort)
  76. return ($order ^ true);
  77. return true;
  78. }
  79. function print_header($title) {
  80. ?>
  81. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  82. <html>
  83. <head>
  84. <title><?=$title?></title>
  85. <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  86. <link rel="stylesheet" href="mini.css" type="text/css">
  87. <script type="text/javascript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/jquery-1.3.2.min.js"></script>
  88. <script type="text/javascript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/jquery.qtip-1.0.0-rc3.min.js"></script>
  89. <script language="JavaScript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/functions.js"></script>
  90. <script language="JavaScript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/scw.js"></script>
  91. <style type="text/css">
  92. .style1 { font-family: "Trebuchet MS" }
  93. </style>
  94. <script type="text/javascript">
  95. <!--
  96. function MM_reloadPage(init) {
  97. if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
  98. document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  99. else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
  100. }
  101. MM_reloadPage(true);
  102. //-->
  103. </script>
  104. </head>
  105. <?php
  106. }
  107. function normalize_string($string) {
  108. if ($string == "-") {
  109. return "N/A";
  110. }
  111. return $string;
  112. }
  113. function get_row_color($row) {
  114. return (($row % 2 != 0) ? "bgcolor=\"#f0f0f0\"" : "");
  115. }
  116. function get_base($script) {
  117. $base = dirname($script);
  118. if ($base == "/") {
  119. $base = "";
  120. }
  121. return $base;
  122. }
  123. function generate_port_profile($start_port, $end_port, $vlan, $pid) {
  124. global $VLANS, $PID_MAP;
  125. $profile = "interface range GigabitEthernet$start_port - $end_port\n";
  126. $zpid = $PID_MAP[$pid];
  127. if (file_exists(PORT_TMPL_DIR . '/ports.tmpl')) {
  128. $profile .= file_get_contents(PORT_TMPL_DIR . '/ports.tmpl');
  129. }
  130. if (file_exists(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl')) {
  131. $profile .= file_get_contents(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl');
  132. }
  133. if ($vlan == 'DYNAMIC') {
  134. if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl')) {
  135. $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl');
  136. }
  137. if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl')) {
  138. $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl');
  139. }
  140. } else if ($vlan == 'TRUNK' || $vlan == 'UPLINK TRUNK') {
  141. if ($vlan == 'UPLINK TRUNK') {
  142. $profile .= " description STATIC: Uplink Trunk Port\n";
  143. } else {
  144. $profile .= " description STATIC: Trunk Port\n";
  145. }
  146. if (file_exists(PORT_TMPL_DIR . '/TRUNK/ports.tmpl')) {
  147. $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/ports.tmpl');
  148. }
  149. if (file_exists(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl')) {
  150. $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl');
  151. }
  152. if ($vlan == 'UPLINK TRUNK') {
  153. if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl')) {
  154. $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl');
  155. }
  156. if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl')) {
  157. $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl');
  158. }
  159. }
  160. } else {
  161. $vi = $VLANS[$vlan];
  162. $profile .= " description STATIC: $vlan Access Port\n";
  163. $profile .= " switchport access vlan $vi\n";
  164. if (file_exists(PORT_TMPL_DIR . '/ACCESS/ports.tmpl')) {
  165. $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/ports.tmpl');
  166. }
  167. if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl')) {
  168. $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl');
  169. }
  170. if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl')) {
  171. $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl');
  172. }
  173. if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl')) {
  174. $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl');
  175. }
  176. }
  177. return $profile;
  178. }
  179. function upload_strerror($errno) {
  180. switch ($errno) {
  181. case UPLOAD_ERR_OK:
  182. return "";
  183. case UPLOAD_ERR_INI_SIZE:
  184. return "File size greater than max allowed size of " .
  185. ini_get("upload_max_filesize");
  186. case UPLOAD_ERR_FROM_SIZE:
  187. return "File size greater than MAX_FILE_SIZE specified in the HTML form";
  188. case UPLOAD_ERR_PARTIAL:
  189. return "File only partially upload";
  190. case UPLOAD_ERR_NO_FILE:
  191. return "Filename was not specified";
  192. case UPLOAD_ERR_NO_TMP_DIR:
  193. return "Temporary directory does not exist";
  194. case UPLOAD_ERR_CATN_WRITE:
  195. return "Unable to write file to disk";
  196. default:
  197. return "Unknown upload error";
  198. }
  199. return "";
  200. }
  201. function csv_escape($string) {
  202. $string = str_replace("\"", "'", $string);
  203. if (strchr($string, ','))
  204. $string = "\"".$string."\"";
  205. $string = str_replace(array("\r", "\n"), "|", $string);
  206. return $string;
  207. }
  208. function get_PI_REST($url, $p = false, $decode = true, $timeout = 300) {
  209. $cookie = false;
  210. if (DEBUG >= 5) {
  211. print "<h3>calling $url with params:</h3>";
  212. var_dump($p);
  213. }
  214. $i = 0;
  215. $res = false;
  216. while ($i < 1) {
  217. $res = fetch_url($url, $cookie, $p, $timeout);
  218. if ($res) break;
  219. sleep(3);
  220. $i++;
  221. }
  222. if ($res) {
  223. if ($decode) {
  224. $json = json_decode($res, true);
  225. return $json;
  226. } else {
  227. return $res;
  228. }
  229. }
  230. return false;
  231. }
  232. function fetch_url($url, &$cookie, $p = false, $timeout = 300) {
  233. $ch = curl_init();
  234. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  235. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  236. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  237. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  238. if (DEBUG > 5) {
  239. curl_setopt($ch, CURLOPT_HEADER, true);
  240. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  241. curl_setopt($ch, CURLOPT_VERBOSE, true);
  242. } else {
  243. curl_setopt($ch, CURLOPT_HEADER, false);
  244. }
  245. if (isset($p['get'])) {
  246. $url .= '?' . http_build_query($p['get']);
  247. }
  248. if (isset($p['post'])) {
  249. curl_setopt($ch, CURLOPT_POST, count($p['post']));
  250. curl_setopt($ch, CURLOPT_POSTFIELDS, $p['post']);
  251. }
  252. if (isset($p['put'])) {
  253. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  254. $p['header']['Content-Length'] = strlen($p['put']);
  255. curl_setopt($ch, CURLOPT_POSTFIELDS, $p['put']);
  256. }
  257. if (isset($p['header'])) {
  258. foreach ($p['header'] as $name => $value) $header[] = $name . ': ' . $value;
  259. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  260. }
  261. if (isset($p['cookie'])) {
  262. curl_setopt($ch, CURLOPT_COOKIE, $p['cookie']);
  263. }
  264. if (isset($p['authentication'])) {
  265. curl_setopt($ch, CURLOPT_USERPWD, $p['authentication']['user'].':'.$p['authentication']['password']);
  266. }
  267. curl_setopt($ch, CURLOPT_URL, $url);
  268. $response = curl_exec($ch);
  269. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  270. $error = 'cURL error num: ' . curl_errno($ch) . ' => ' . curl_error($ch);
  271. if (DEBUG >= 5) {
  272. print "<h3>Request:</h3>";
  273. var_dump(curl_getinfo($ch));
  274. print "<h3>Response:</h3>";
  275. var_dump($response);
  276. print curl_error($ch) . "\n";
  277. }
  278. curl_close($ch);
  279. if (empty($response)) {
  280. if (DEBUG > 0) {
  281. echo "WARNING: empty response from URL<br/>\n";
  282. return false;
  283. }
  284. } else {
  285. if (empty($httpcode)) {
  286. if (DEBUG > 0) {
  287. echo "WARNING: no HTTP code was returned\n$response\n";
  288. return false;
  289. }
  290. } elseif ($httpcode != 200) {
  291. if (DEBUG > 0) {
  292. echo "ERROR: the server returned http code: $httpcode\n$response\n";
  293. }
  294. if (DEBUG > 1) {
  295. print "<h3>Request:</h3>";
  296. var_dump(curl_getinfo($ch));
  297. print "<h3>Response:</h3>";
  298. var_dump($response);
  299. }
  300. $cookie = false;
  301. return false;
  302. } else {
  303. return $response;
  304. }
  305. }
  306. }
  307. ?>