. * * @package LibreNMS * @link http://librenms.org * @copyright 2018 Tony Murray * @author Tony Murray */ namespace LibreNMS\Util; use App\Models\Device; use App\Models\Port; use Auth; use Carbon\Carbon; use LibreNMS\Config; class Url { /** * @param Device $device * @param string $text * @param array $vars * @param int $start * @param int $end * @param int $escape_text * @param int $overlib * @return string */ public static function deviceLink($device, $text = null, $vars = [], $start = 0, $end = 0, $escape_text = 1, $overlib = 1) { if (!$start) { $start = Carbon::now()->subDay(1)->timestamp; } if (!$end) { $end = Carbon::now()->timestamp; } if (!$text) { $text = $device->displayName(); } if ($escape_text) { $text = htmlentities($text); } $class = self::deviceLinkDisplayClass($device); $graphs = self::getOverviewGraphsForDevice($device); $url = Url::deviceUrl($device, $vars); // beginning of overlib box contains large hostname followed by hardware & OS details $contents = '
' . $device->displayName() . ''; if ($device->hardware) { $contents .= ' - ' . htmlentities($device->hardware); } if ($device->os) { $contents .= ' - ' . htmlentities(Config::getOsSetting($device->os, 'text')); } if ($device->version) { $contents .= ' ' . htmlentities($device->version); } if ($device->features) { $contents .= ' (' . htmlentities($device->features) . ')'; } if ($device->location_id) { $contents .= ' - ' . htmlentities($device->location); } $contents .= '
'; foreach ((array)$graphs as $entry) { $graph = isset($entry['graph']) ? $entry['graph'] : 'unknown'; $graphhead = isset($entry['text']) ? $entry['text'] : 'unknown'; $contents .= '
'; $contents .= '' . $graphhead . '
'; $contents .= Url::minigraphImage($device, $start, $end, $graph); $contents .= Url::minigraphImage($device, Carbon::now()->subWeek(1)->timestamp, $end, $graph); $contents .= '
'; } if ($overlib == 0) { $link = $contents; } else { $contents = self::escapeBothQuotes($contents); $link = Url::overlibLink($url, $text, $contents, $class); } if ($device->canAccess(Auth::user())) { return $link; } else { return $device->displayName(); } } /** * @param Port $port * @param string $text * @param string $type * @param boolean $overlib * @param boolean $single_graph * @return mixed|string */ public static function portLink($port, $text = null, $type = null, $overlib = true, $single_graph = false) { $label = Rewrite::normalizeIfName($port->getLabel()); if (!$text) { $text = $label; } $content = '
' . addslashes(htmlentities($port->device->displayName() . ' - ' . $label)) . '
'; if ($port->ifAlias) { $content .= addslashes(htmlentities($port->ifAlias)) . '
'; } $content .= "
"; $graph_array = [ 'type' => $type ?: 'port_bits', 'legend' => 'yes', 'height' => 100, 'width' => 340, 'to' => Carbon::now()->timestamp, 'from' => Carbon::now()->subDay()->timestamp, 'id' => $port->port_id, ]; $content .= self::graphTag($graph_array); if (!$single_graph) { $graph_array['from'] = Carbon::now()->subWeek()->timestamp; $content .= self::graphTag($graph_array); $graph_array['from'] = Carbon::now()->subMonth()->timestamp; $content .= self::graphTag($graph_array); $graph_array['from'] = Carbon::now()->subYear()->timestamp; $content .= self::graphTag($graph_array); } $content .= '
'; if (!$overlib) { return $content; } elseif ($port->canAccess(Auth::user())) { return self::overlibLink(self::portUrl($port), $text, $content, self::portLinkDisplayClass($port)); } return Rewrite::normalizeIfName($text); } public static function deviceUrl($device, $vars = []) { return self::generate(['page' => 'device', 'device' => $device->device_id], $vars); } public static function portUrl($port, $vars = []) { return self::generate(['page' => 'device', 'device' => $port->device_id, 'tab' => 'port', 'port' => $port->port_id], $vars); } /** * @param Port $port * @return string */ public static function portThumbnail($port) { $graph_array = [ 'port_id' => $port->port_id, 'graph_type' => 'port_bits', 'from' => Carbon::now()->subDay()->timestamp, 'to' => Carbon::now()->timestamp, 'width' => 150, 'height' => 21, ]; return self::portImage($graph_array); } public static function portImage($args) { if (empty($args['bg'])) { $args['bg'] = 'FFFFFF00'; } return ""; } public static function generate($vars, $new_vars = []) { $vars = array_merge($vars, $new_vars); $url = $vars['page'] . '/'; unset($vars['page']); foreach ($vars as $var => $value) { if ($value == '0' || $value != '' && !str_contains($var, 'opt') && !is_numeric($var)) { $url .= $var . '=' . urlencode($value) . '/'; } } return $url; } /** * @param array $args * @return string */ public static function graphTag($args) { $urlargs = []; foreach ($args as $key => $arg) { $urlargs[] = $key . '=' . urlencode($arg); } return ''; } public static function lazyGraphTag($args) { $urlargs = []; foreach ($args as $key => $arg) { $urlargs[] = $key . "=" . urlencode($arg); } if (Config::get('enable_lazy_load', true)) { return ''; } return ''; } public static function overlibLink($url, $text, $contents, $class = null) { $contents = "
" . $contents . '
'; $contents = str_replace('"', "\'", $contents); if ($class === null) { $output = '"; } else { $output .= '>'; } $output .= $text . ''; return $output; } public static function overlibContent($graph_array, $text) { $overlib_content = '
' . $text . '
'; $now = Carbon::now(); foreach ([1, 7, 30, 365] as $days) { $graph_array['from'] = $now->subDays($days)->timestamp; $overlib_content .= self::escapeBothQuotes(self::graphTag($graph_array)); } $overlib_content .= '
'; return $overlib_content; } /** * Generate minigraph image url * * @param Device $device * @param int $start * @param int $end * @param string $type * @param string $legend * @param int $width * @param int $height * @param string $sep * @param string $class * @param int $absolute_size * @return string */ public static function minigraphImage($device, $start, $end, $type, $legend = 'no', $width = 275, $height = 100, $sep = '&', $class = 'minigraph-image', $absolute_size = 0) { $vars = ['device=' . $device->device_id, "from=$start", "to=$end", "width=$width", "height=$height", "type=$type", "legend=$legend", "absolute=$absolute_size"]; return ''; } private static function getOverviewGraphsForDevice($device) { if ($device->snmp_disable) { return Config::getOsSetting('ping', 'over'); } if ($graphs = Config::getOsSetting($device->os, 'over')) { return $graphs; } if ($os_group = Config::getOsSetting($device->os, 'os_group')) { $name = key($os_group); if (isset($os_group[$name]['over'])) { return $os_group[$name]['over']; } } return Config::getOsSetting('default', 'over'); } /** * @param Device $device * @return string */ private static function deviceLinkDisplayClass($device) { if ($device->disabled) { return 'list-device-disabled'; } if ($device->ignore) { return $device->status ? 'list-device-ignored-up' : 'list-device-ignored'; } return $device->status ? 'list-device' : 'list-device-down'; } /** * Get html class for a port using ifAdminStatus and ifOperStatus * * @param Port $port * @return string */ public static function portLinkDisplayClass($port) { if ($port->ifAdminStatus == "down") { return "interface-admindown"; } if ($port->ifAdminStatus == "up" && $port->ifOperStatus == "down") { return "interface-updown"; } return "interface-upup"; } private static function escapeBothQuotes($string) { return str_replace(["'", '"'], "\'", $string); } }