123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- #-
- # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- # SUCH DAMAGE.
- #
- #
- require_once 'Log.php';
- function cleanup() {
- global $logger, $dbh;
- $dbh->disconnect();
- $logger->close();
- }
- function get_sort_icon($curr_sort, $sort, $order, $extsort = 0) {
- if ($extsort == 1) return "";
- if ($curr_sort == $sort) {
- $icon = "up_selected.png";
- $alt = "up";
- if (!$order) {
- $icon = "down_selected.png";
- $alt = "down";
- }
- return "<img src=\"" . ATTRITION_IMAGES_PATH . "/$icon\" width=\"11\" height=\"9\" border=\"0\" alt=\"$alt\">";
- }
- return "";
- }
- function parse_csv_string($string) {
- $fields = array();
- $string = chop($string, "\r\n");
- if ($string == "") {
- return $fields;
- }
- $tmpfields = explode(",", $string);
- for ($i = 0; $i < count($tmpfields); $i++) {
- $field = $tmpfields[$i];
- if (preg_match("/^\"[^\"]+$/", $field)) {
- $field = preg_replace("/^\"/", "", $field);
- while ($i < count($tmpfields)) {
- $field .= "," . $tmpfields[++$i];
- if (preg_match("/\"$/", $field)) {
- break;
- }
- }
- if ($i == count($tmpfields) &&
- !preg_match("/\"$/", $field)) {
- return NULL;
- }
- $field = preg_replace("/\"$/", "", $field);
- }
- array_push($fields, $field);
- }
- return $fields;
- }
- function get_new_sort($curr_sort, $sort, $order) {
- if ($curr_sort == $sort)
- return ($order ^ true);
- return true;
- }
- function print_header($title) {
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title><?=$title?></title>
- <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
- <link rel="stylesheet" href="mini.css" type="text/css">
- <script type="text/javascript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/jquery-1.3.2.min.js"></script>
- <script type="text/javascript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/jquery.qtip-1.0.0-rc3.min.js"></script>
- <script language="JavaScript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/functions.js"></script>
- <script language="JavaScript" src="<?=get_base($_SERVER['SCRIPT_NAME'])?>/scw.js"></script>
- <style type="text/css">
- .style1 { font-family: "Trebuchet MS" }
- </style>
- <script type="text/javascript">
- <!--
- function MM_reloadPage(init) {
- if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
- document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
- else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
- }
- MM_reloadPage(true);
- //-->
- </script>
- </head>
- <?php
- }
- function normalize_string($string) {
- if ($string == "-") {
- return "N/A";
- }
- return $string;
- }
- function get_row_color($row) {
- return (($row % 2 != 0) ? "bgcolor=\"#f0f0f0\"" : "");
- }
- function get_base($script) {
- $base = dirname($script);
- if ($base == "/") {
- $base = "";
- }
- return $base;
- }
- function generate_port_profile($start_port, $end_port, $vlan, $pid) {
- global $VLANS, $PID_MAP;
- $profile = "interface range GigabitEthernet$start_port - $end_port\n";
- $zpid = $PID_MAP[$pid];
- if (file_exists(PORT_TMPL_DIR . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/' . $zpid . '/ports.tmpl');
- }
- if ($vlan == 'DYNAMIC') {
- if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/DYNAMIC/' . $zpid . '/ports.tmpl');
- }
- } else if ($vlan == 'TRUNK' || $vlan == 'UPLINK TRUNK') {
- if ($vlan == 'UPLINK TRUNK') {
- $profile .= " description STATIC: Uplink Trunk Port\n";
- } else {
- $profile .= " description STATIC: Trunk Port\n";
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/' . $zpid . '/ports.tmpl');
- }
- if ($vlan == 'UPLINK TRUNK') {
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/TRUNK/uplink/' . $zpid . '/ports.tmpl');
- }
- }
- } else {
- $vi = $VLANS[$vlan];
- $profile .= " description STATIC: $vlan Access Port\n";
- $profile .= " switchport access vlan $vi\n";
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $zpid . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/ports.tmpl');
- }
- if (file_exists(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl')) {
- $profile .= file_get_contents(PORT_TMPL_DIR . '/ACCESS/' . $vi . '/' . $zpid . '/ports.tmpl');
- }
- }
- return $profile;
- }
- function upload_strerror($errno) {
- switch ($errno) {
- case UPLOAD_ERR_OK:
- return "";
- case UPLOAD_ERR_INI_SIZE:
- return "File size greater than max allowed size of " .
- ini_get("upload_max_filesize");
- case UPLOAD_ERR_FROM_SIZE:
- return "File size greater than MAX_FILE_SIZE specified in the HTML form";
- case UPLOAD_ERR_PARTIAL:
- return "File only partially upload";
- case UPLOAD_ERR_NO_FILE:
- return "Filename was not specified";
- case UPLOAD_ERR_NO_TMP_DIR:
- return "Temporary directory does not exist";
- case UPLOAD_ERR_CATN_WRITE:
- return "Unable to write file to disk";
- default:
- return "Unknown upload error";
- }
- return "";
- }
- function csv_escape($string) {
- $string = str_replace("\"", "'", $string);
- if (strchr($string, ','))
- $string = "\"".$string."\"";
- $string = str_replace(array("\r", "\n"), "|", $string);
- return $string;
- }
- function get_PI_REST($url, $p = false, $decode = true, $timeout = 300) {
- $cookie = false;
- if (DEBUG >= 5) {
- print "<h3>calling $url with params:</h3>";
- var_dump($p);
- }
- $i = 0;
- $res = false;
- while ($i < 1) {
- $res = fetch_url($url, $cookie, $p, $timeout);
- if ($res) break;
- sleep(3);
- $i++;
- }
- if ($res) {
- if ($decode) {
- $json = json_decode($res, true);
- return $json;
- } else {
- return $res;
- }
- }
- return false;
- }
- function fetch_url($url, &$cookie, $p = false, $timeout = 300) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if (DEBUG > 5) {
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLINFO_HEADER_OUT, true);
- curl_setopt($ch, CURLOPT_VERBOSE, true);
- } else {
- curl_setopt($ch, CURLOPT_HEADER, false);
- }
- if (isset($p['get'])) {
- $url .= '?' . http_build_query($p['get']);
- }
- if (isset($p['post'])) {
- curl_setopt($ch, CURLOPT_POST, count($p['post']));
- curl_setopt($ch, CURLOPT_POSTFIELDS, $p['post']);
- }
- if (isset($p['put'])) {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- $p['header']['Content-Length'] = strlen($p['put']);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $p['put']);
- }
- if (isset($p['header'])) {
- foreach ($p['header'] as $name => $value) $header[] = $name . ': ' . $value;
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- if (isset($p['cookie'])) {
- curl_setopt($ch, CURLOPT_COOKIE, $p['cookie']);
- }
- if (isset($p['authentication'])) {
- curl_setopt($ch, CURLOPT_USERPWD, $p['authentication']['user'].':'.$p['authentication']['password']);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- $response = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $error = 'cURL error num: ' . curl_errno($ch) . ' => ' . curl_error($ch);
- if (DEBUG >= 5) {
- print "<h3>Request:</h3>";
- var_dump(curl_getinfo($ch));
- print "<h3>Response:</h3>";
- var_dump($response);
- print curl_error($ch) . "\n";
- }
- curl_close($ch);
- if (empty($response)) {
- if (DEBUG > 0) {
- echo "WARNING: empty response from URL<br/>\n";
- return false;
- }
- } else {
- if (empty($httpcode)) {
- if (DEBUG > 0) {
- echo "WARNING: no HTTP code was returned\n$response\n";
- return false;
- }
- } elseif ($httpcode != 200) {
- if (DEBUG > 0) {
- echo "ERROR: the server returned http code: $httpcode\n$response\n";
- }
- if (DEBUG > 1) {
- print "<h3>Request:</h3>";
- var_dump(curl_getinfo($ch));
- print "<h3>Response:</h3>";
- var_dump($response);
- }
- $cookie = false;
- return false;
- } else {
- return $response;
- }
- }
- }
- ?>
|