/html/map.php

https://github.com/kyrisu/observernms_clean · PHP · 138 lines · 117 code · 21 blank · 0 comment · 30 complexity · 7254cc657fa6978fe2d4a44bd6cbc501 MD5 · raw file

  1. <?php
  2. include("../includes/defaults.inc.php");
  3. include("../config.php");
  4. include("../includes/functions.php");
  5. include("includes/authenticate.inc.php");
  6. if (isset($_GET['device'])) { $where = "WHERE device_id = ".$_GET['device']; } else { $where = ""; }
  7. $deviceresult = mysql_query("SELECT * from devices $where");
  8. if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format']))
  9. {
  10. $map = "digraph G { sep=0.01; size=\"12,5.5\"; pack=100; bgcolor=transparent; splines=true; overlap=scale; concentrate=0; epsilon=0.001; rankdir=0;
  11. node [ fontname=\"helvetica\", fontstyle=bold, style=filled, color=white, fillcolor=lightgrey, overlap=false;];
  12. edge [ bgcolor=white; fontname=\"helvetica\"; fontstyle=bold; arrowhead=dot; arrowtail=dot];
  13. graph [bgcolor=transparent;];\n\n";
  14. if(!$_SESSION['authenticated'])
  15. {
  16. $map .= "\"Not authenticated\" [fontsize=20 fillcolor=\"lightblue\" URL=\"/\" shape=box3d]\n";
  17. }
  18. else
  19. {
  20. while ($device = mysql_fetch_array($deviceresult))
  21. {
  22. if($device)
  23. {
  24. $sql = "SELECT * from ports AS I, links AS L WHERE I.device_id = ".$device['device_id']." AND L.local_interface_id = I.interface_id";
  25. $links = mysql_query($sql);
  26. if (mysql_num_rows($links))
  27. {
  28. $map .= "\"".$device['hostname']."\" [fontsize=20 fillcolor=\"lightblue\" URL=\"/device/".$device['device_id']."/map/\" shape=box3d]\n";
  29. }
  30. while($link = mysql_fetch_array($links))
  31. {
  32. $local_interface_id = $link['local_interface_id'];
  33. $remote_interface_id = $link['remote_interface_id'];
  34. $i = 0; $done = 0;
  35. while (isset($linkdone) && $i < count($linkdone))
  36. {
  37. $thislink = "$remote_interface_id $local_interface_id";
  38. if ($linkdone[$i] == $thislink) { $done = 1; }
  39. $i++;
  40. }
  41. if(!$done)
  42. {
  43. $linkdone[] = "$local_interface_id $remote_interface_id";
  44. if($link['ifSpeed'] >= "10000000000")
  45. {
  46. $info = "color=lightred style=\"setlinewidth(8)\"";
  47. } elseif ($link['ifSpeed'] >= "1000000000") {
  48. $info = "color=lightblue style=\"setlinewidth(4)\"";
  49. } elseif ($link['ifSpeed'] >= "100000000") {
  50. $info = "color=lightgrey style=\"setlinewidth(2)\"";
  51. } elseif ($link['ifSpeed'] >= "10000000") {
  52. $info = "style=\"setlinewidth(1)\"";
  53. } else {
  54. $info = "style=\"setlinewidth(1)\"";
  55. }
  56. $src = $device['hostname'];
  57. if($remote_interface_id) {
  58. $dst = mysql_result(mysql_query("SELECT `hostname` FROM `devices` AS D, `ports` AS I WHERE I.interface_id = '$remote_interface_id' AND D.device_id = I.device_id"),0);
  59. $dst_host = mysql_result(mysql_query("SELECT D.device_id FROM `devices` AS D, `ports` AS I WHERE I.interface_id = '$remote_interface_id' AND D.device_id = I.device_id"),0);
  60. } else {
  61. $dst_host = $link['remote_hostname'];
  62. $dst = $link['remote_hostname'];
  63. }
  64. $sif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM ports WHERE `interface_id`=" . $link['local_interface_id'])),$device);
  65. if($remote_interface_id) {
  66. $dif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM ports WHERE `interface_id`=" . $link['remote_interface_id'])));
  67. } else {
  68. $dif['label'] = $link['remote_port'];
  69. $dif['interface_id'] = $link['remote_hostname'] . $link['remote_port'];
  70. }
  71. $map .= "\"" . $sif['interface_id'] . "\" [label=\"" . $sif['label'] . "\", fontsize=12, fillcolor=lightblue URL=\"/device/".$device['device_id']."/interface/$local_interface_id/\"]\n";
  72. if (!$ifdone[$src][$sif['interface_id']])
  73. {
  74. $map .= "\"$src\" -> \"" . $sif['interface_id'] . "\" [weight=500000, arrowsize=0, len=0];\n";
  75. $ifdone[$src][$sif['interface_id']] = 1;
  76. }
  77. $map .= "\"$dst\" [URL=\"/device/$dst_host/map/\" fontsize=20 shape=box3d]\n";
  78. if($dst_host == $device['device_id']) {
  79. $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"/device/$dst_host/interface/$remote_interface_id/\"]\n";
  80. } else {
  81. $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . " \", fontsize=12, fillcolor=lightgray, URL=\"/device/$dst_host/interface/$remote_interface_id/\"]\n";
  82. }
  83. if (!$ifdone[$dst][$dif['interface_id']])
  84. {
  85. $map .= "\"" . $dif['interface_id'] . "\" -> \"$dst\" [weight=500000, arrowsize=0, len=0];\n";
  86. $ifdone[$dst][$dif['interface_id']] = 1;
  87. }
  88. $map .= "\"" . $sif['interface_id'] . "\" -> \"" . $dif['interface_id'] . "\" [weight=1, arrowhead=normal, arrowtail=normal, len=2, $info] \n";
  89. }
  90. }
  91. $done = 0;
  92. }
  93. }
  94. }
  95. $map .= "
  96. };";
  97. if ($_GET['debug'] == 1) { echo("<pre>$map</pre>");exit(); }
  98. $img = shell_exec("echo \"".addslashes($map)."\" | dot -T".$_GET['format']."");
  99. if($_GET['format'] == "png") {
  100. header("Content-type: image/".$_GET['format']);
  101. } elseif ($_GET['format'] == "svg") {
  102. header("Content-type: image/svg+xml");
  103. $img = str_replace("<a ", "<a target = \"_parent\" ", $img);
  104. }
  105. echo("$img");
  106. }
  107. else
  108. {
  109. if ($_SESSION['authenticated']) ## FIXME level 10 only?
  110. {
  111. echo '<center>
  112. <object data="'. $config['base_url'] . '/map.php?format=svg" type="image/svg+xml">
  113. </object>
  114. </center>';
  115. }
  116. }
  117. ?>