prototype.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. <?php
  2. /*
  3. * @version $Id: prototype.class.php 234 2019-12-12 14:34:31Z yllen $
  4. -------------------------------------------------------------------------
  5. LICENSE
  6. This file is part of Archires plugin for GLPI.
  7. Archires is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. Archires is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with Archires. If not, see <http://www.gnu.org/licenses/>.
  17. @package archires
  18. @author Nelly Mahu-Lasson, Xavier Caillaud
  19. @copyright Copyright (c) 2016-2018 Archires plugin team
  20. @license AGPL License 3.0 or (at your option) any later version
  21. http://www.gnu.org/licenses/agpl-3.0-standalone.html
  22. @link https://forge.glpi-project.org/projects/archires
  23. @since version 2.2
  24. --------------------------------------------------------------------------
  25. */
  26. if (!defined('GLPI_ROOT')) {
  27. die("Sorry. You can't access directly to this file");
  28. }
  29. class PluginArchiresPrototype extends CommonDBTM {
  30. static $rightname = "plugin_archires";
  31. private static function dotIt($engine, $graph, $format) {
  32. $out = '';
  33. $Path = realpath(GLPI_PLUGIN_DOC_DIR."/archires");
  34. $graph_name = tempnam($Path, "txt");
  35. $out_name = tempnam($Path, $format);
  36. if (file_put_contents($graph_name, $graph)) {
  37. $command = "$engine -T$format -o\"$out_name\" \"$graph_name\" ";
  38. $out = shell_exec($command);
  39. $out = file_get_contents($out_name);
  40. unlink($graph_name);
  41. unlink($out_name);
  42. }
  43. return $out;
  44. }
  45. function testGraphviz() {
  46. $graph = "graph G {
  47. a;
  48. b;
  49. c -- d;
  50. a -- c;}";
  51. return self::dotIt('dot', $graph, 'png');
  52. }
  53. function CleanField($string) {
  54. $string = str_replace(">", " - ", $string);
  55. $string = str_replace("&", " - ", $string);
  56. return $string;
  57. }
  58. static function displayTypeAndIP($PluginArchiresView,$itemtype,$device,$generation) {
  59. $graph = "";
  60. $PluginArchiresArchires = new PluginArchiresArchires();
  61. if (($PluginArchiresView->fields["display_ip"] != 0)
  62. && isset($device["ip"])) {
  63. if (($PluginArchiresView->fields["display_type"] != 0)
  64. && !empty($device["type"])) {
  65. $class = $itemtype."Type";
  66. $typeclass = new $class();
  67. $typeclass->getFromDB($device["type"]);
  68. if (!$generation) {
  69. $graph = $typeclass->fields["name"] . " " .$device["ip"];
  70. } else {
  71. $graph = " - ".$typeclass->fields["name"].
  72. "</td></tr><tr><td>".$device["ip"]."</td></tr>";
  73. }
  74. } else {
  75. if (!$generation) {
  76. $graph = $device["ip"];
  77. } else {
  78. $graph ="</td></tr><tr><td>".$device["ip"]."</td></tr>";
  79. }
  80. }
  81. } else {
  82. if (($PluginArchiresView->fields["display_type"] != 0)
  83. && !empty($device["type"])) {
  84. $class = $itemtype."Type";
  85. $typeclass = new $class();
  86. $typeclass->getFromDB($device["type"]);
  87. if (!$generation) {
  88. $graph =$typeclass->fields["name"];
  89. } else {
  90. $graph ="</td></tr><tr><td>".
  91. $typeclass->fields["name"]."</td></tr>";
  92. }
  93. } else {
  94. if (!$generation) {
  95. echo "";
  96. } else {
  97. $graph ="</td></tr>";
  98. }
  99. }
  100. }
  101. return $graph;
  102. }
  103. static function displayUsers($url,$device,$generation) {
  104. $graph ="";
  105. if ($device["users_id"]) {
  106. if ($generation) {
  107. $graph = "URL=\"".$url."\" tooltip=\"".getUserName($device["users_id"])."\"";
  108. } else {
  109. $graph = "<a href='".$url."'>".getUserName($device["users_id"])."</a>";
  110. }
  111. } else if (!$device["users_id"] && $device["groups_id"]) {
  112. if ($generation) {
  113. $graph = "URL=\"".$url."\" tooltip=\"".Dropdown::getDropdownName("glpi_groups",
  114. $device["groups_id"])."\"";
  115. } else {
  116. $graph = "<a href='".$url."'>".Dropdown::getDropdownName("glpi_groups",
  117. $device["groups_id"])."</a>";
  118. }
  119. } else if (!$device["users_id"]
  120. && !$device["groups_id"]
  121. && $device["contact"]) {
  122. if ($generation) {
  123. $graph = "URL=\"".$url."\" tooltip=\"".$device["contact"]."\"";
  124. } else {
  125. $graph = "<a href='".$url."'>".$device["contact"]."</a>";
  126. }
  127. } else {
  128. if ($generation) {
  129. $graph = "URL=\"".$url."\" tooltip=\"".$device["name"]."\"";
  130. } else {
  131. $graph = "<a href='".$url."'>".$device["name"]."</a>";
  132. }
  133. }
  134. return $graph;
  135. }
  136. static function test($item) {
  137. global $DB,$CFG_GLPI;
  138. $ID = $item->getID();
  139. $type = $item->getType();
  140. $plugin_archires_views_id = $item->fields["plugin_archires_views_id"];
  141. if (!$plugin_archires_views_id) {
  142. return false;
  143. }
  144. $plugin = new Plugin();
  145. $PluginArchiresView = new PluginArchiresView();
  146. if ($plugin->isActivated("appliances")) {
  147. $PluginArchiresApplianceQuery = new PluginArchiresApplianceQuery();
  148. }
  149. $PluginArchiresLocationQuery = new PluginArchiresLocationQuery();
  150. $PluginArchiresNetworkEquipmentQuery = new PluginArchiresNetworkEquipmentQuery();
  151. $PluginArchiresStateColor = new PluginArchiresStateColor();
  152. $PluginArchiresImageItem = new PluginArchiresImageItem();
  153. $PluginArchiresView->getFromDB($plugin_archires_views_id);
  154. $devices = [];
  155. $ports = [];
  156. echo "<br><div class='center'>";
  157. echo "<table class='tab_cadre_fixe'cellpadding='2' width='75%'>";
  158. echo "<tr><th colspan='6'>".__('Item')."</th></tr>";
  159. echo "<tr><th>".__('Graphviz name', 'archires')."</th>";
  160. echo "<th>".__('Associated picture', 'archires')."</th>";
  161. echo "<th>".__('Name of item', 'archires')."</th>";
  162. echo "<th>".sprintf(__('%1$s / %2$s'), __('Type'), __('IP'))."</th>";
  163. echo "<th>".__('Status')."</th>";
  164. echo "<th>".sprintf(__('%1$s / %2$s'), __('User'),
  165. sprintf(__('%1$s / %2$s'), __('Group'), __('Contact')))."</th></tr>";
  166. if ($type == 'PluginArchiresLocationQuery') {
  167. $devices = $PluginArchiresLocationQuery->Query($ID, $PluginArchiresView, true);
  168. $ports = $PluginArchiresLocationQuery->Query($ID, $PluginArchiresView, false);
  169. } else if ($type == 'PluginArchiresNetworkEquipmentQuery') {
  170. $devices = $PluginArchiresNetworkEquipmentQuery->Query($ID, $PluginArchiresView, true);
  171. $ports = $PluginArchiresNetworkEquipmentQuery->Query($ID, $PluginArchiresView, false);
  172. } else if ($type == 'PluginArchiresApplianceQuery') {
  173. $devices = $PluginArchiresApplianceQuery->Query($ID, $PluginArchiresView, true);
  174. $ports = $PluginArchiresApplianceQuery->Query($ID, $PluginArchiresView, false);
  175. }
  176. foreach ($devices as $itemtype => $typed_devices) {
  177. foreach ($typed_devices as $device_id => $device) {
  178. $device_unique_name = $itemtype . "_" . $device_id . "_";
  179. $device_unique_name .= $device["name"];
  180. $image_name = $PluginArchiresImageItem->displayItemImage($device["type"], $itemtype,
  181. true);
  182. $link = Toolbox::getItemTypeFormURL($itemtype);
  183. $url = $link."?id=".$device_id;
  184. echo "<tr class='tab_bg_1'>";
  185. echo "<td>$device_unique_name</td>";
  186. echo "<td class='center'><img src='$image_name' alt='$image_name'></td>";
  187. echo "<td>" . $device["name"]."</td>";
  188. echo "<td>";
  189. echo self::displayTypeAndIP($PluginArchiresView, $itemtype, $device, false);
  190. echo "</td>";
  191. echo "<td>";
  192. if ($PluginArchiresView->fields["display_state"]!=0 && isset($device["states_id"])) {
  193. echo $PluginArchiresStateColor->displayColorState($device);
  194. }
  195. echo "</td>";
  196. echo "<td>";
  197. echo self::displayUsers($url, $device, false);
  198. echo "</td>";
  199. echo "</tr>";
  200. }
  201. }
  202. echo "</table>";
  203. echo "<br><table class='tab_cadre_fixe' cellpadding='2' width='75%'>";
  204. echo "<tr><th colspan='6'>"._n('Link', 'Links', 2, 'archires')."</th></tr>";
  205. echo "<tr><th>".__('Graphviz links', 'archires')."</th>";
  206. echo "<th>".__('IP item 1', 'archires')."</th>";
  207. echo "<th>".__('Socket item 1', 'archires')."</th>";
  208. echo "<th>".__('Associated picture', 'archires')."</th>";
  209. echo "<th>".__('Socket item 2', 'archires')."</th>";
  210. echo "<th>".__('IP item 2', 'archires')."</th></tr>";
  211. $wires = [];
  212. $query = ['SELECT' => ['id', 'networkports_id_1', 'networkports_id_2'],
  213. 'FROM' => 'glpi_networkports_networkports'];
  214. if ($result = $DB->request($query)) {
  215. while ($data = $result->next()) {
  216. $wires[$data["id"]]["networkports_id_1"] = $data["networkports_id_1"];
  217. $wires[$data["id"]]["networkports_id_2"] = $data["networkports_id_2"];
  218. }
  219. }
  220. foreach ($wires as $wire) {
  221. if (isset($ports[$wire["networkports_id_1"]])
  222. && !empty($ports[$wire["networkports_id_1"]])
  223. && isset($ports[$wire["networkports_id_2"]])
  224. && !empty($ports[$wire["networkports_id_2"]])) {
  225. $items_id1 = $ports[$wire["networkports_id_1"]]["items_id"];
  226. $itemtype1 = $ports[$wire["networkports_id_1"]]["itemtype"];
  227. $logical_number1 = $ports[$wire["networkports_id_1"]]["logical_number"];
  228. $name1 = $ports[$wire["networkports_id_1"]]["namep"];
  229. $ID1 = $ports[$wire["networkports_id_1"]]["idp"];
  230. $ip1 = $ports[$wire["networkports_id_1"]]["ip"];
  231. $device_unique_name1 = $itemtype1 . "_" . $items_id1 . "_";
  232. $device_unique_name1 .= $devices[$itemtype1][$items_id1]["name"];
  233. $items_id2 = $ports[$wire["networkports_id_2"]]["items_id"];
  234. $itemtype2 = $ports[$wire["networkports_id_2"]]["itemtype"];
  235. $logical_number2 = $ports[$wire["networkports_id_2"]]["logical_number"];
  236. $name2 = $ports[$wire["networkports_id_2"]]["namep"];
  237. $ID2 = $ports[$wire["networkports_id_2"]]["idp"];
  238. $ip2 = $ports[$wire["networkports_id_2"]]["ip"];
  239. $device_unique_name2 = $itemtype2 . "_" . $items_id2 . "_";
  240. $device_unique_name2 .= $devices[$itemtype2][$items_id2]["name"];
  241. echo "<tr class='tab_bg_1'>";
  242. if ($PluginArchiresView->fields["display_ports"]!=0
  243. && $PluginArchiresView->fields["engine"]!=1) {
  244. $url_ports = $CFG_GLPI["root_doc"] . "/front/networkport.form.php?id=";
  245. echo "<td>".printf(__('%1$s - %2$s'), $device_unique_name1, $device_unique_name2).
  246. "</td>";
  247. if ($PluginArchiresView->fields["display_ip"]!=0) {
  248. echo "<td>".$ip1."</td>";
  249. } else {
  250. echo "<td></td>";
  251. }
  252. echo "<td><a href='".$url_ports.$ID1."'>".$name1."</a> - ".
  253. __('Socket', 'archires')." ".$logical_number1."</td>";
  254. echo "<td class='center'><img src= \"../pics/socket.png\" alt='../pics/socket.png' />";
  255. echo "</td><td><a href='".$url_ports.$ID2."'>".$name2."</a> - ".
  256. __('Socket', 'archires')." ".$logical_number2."</td>";
  257. if ($PluginArchiresView->fields["display_ip"]!=0) {
  258. echo "<td>".$ip2."</td>";
  259. } else {
  260. echo "<td></td>";
  261. }
  262. } else {
  263. echo "<td>".$device_unique_name1." -- ".$device_unique_name2 ."</td>";
  264. echo "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
  265. }
  266. }
  267. }
  268. echo "</tr></table>";
  269. echo "<br><table class='tab_cadre' cellpadding='2'>";
  270. echo "<tr><th>".__('Test Graphviz', 'archires')."</th></tr>";
  271. echo "<tr class='tab_bg_1'><td>";
  272. echo "<img src='./archires.test.php' alt=''>";
  273. echo "</td></tr>";
  274. echo "</table>";
  275. echo "</div>";
  276. }
  277. function graphItems($device,$device_id,$itemtype,$format,$image_name,$url,$PluginArchiresView) {
  278. global $DB;
  279. $PluginArchiresStateColor = new PluginArchiresStateColor();
  280. $device_unique_name = $itemtype . "_" . $device_id . "_";
  281. $device_unique_name .= $device["name"];
  282. $graph = "\"".$device_unique_name."\"[shape=plaintext, label=";
  283. //label
  284. $graph .= "<<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
  285. //img
  286. $graph .= "<tr><td><img src=\"".realpath(GLPI_ROOT)."/plugins/archires/".$image_name. "\"/>".
  287. "</td></tr>";
  288. $graph .= "<tr><td> </td></tr><tr><td>".$device["name"];
  289. //ip / type
  290. $graph .= self::displayTypeAndIP($PluginArchiresView, $itemtype, $device, true);
  291. //entity
  292. if (($PluginArchiresView->fields["display_entity"] != 0) && isset($device["entity"])) {
  293. $graph .= "<tr><td>".$this->CleanField(Dropdown::getDropdownName("glpi_entities",
  294. $device["entity"])).
  295. "</td></tr>";
  296. }
  297. //location
  298. if (($PluginArchiresView->fields["display_location"] != 0) && isset($device["locations_id"])) {
  299. $graph .= "<tr><td>".$this->CleanField(Dropdown::getDropdownName("glpi_locations",
  300. $device["locations_id"])).
  301. "</td></tr>";
  302. }
  303. //state
  304. if (($PluginArchiresView->fields["display_state"] !=0 ) && isset($device["states_id"])) {
  305. $graph .="<tr><td>".$PluginArchiresStateColor->displayColorState($device)."</td></tr>";
  306. }
  307. $graph .= "</table>>";
  308. //end label
  309. //link - users
  310. $graph .=self::displayUsers($url, $device, true);
  311. $graph .="];\n";
  312. return $graph;
  313. }
  314. function graphPorts($devices, $ports, $wire, $format, $PluginArchiresView) {
  315. global $DB, $CFG_GLPI;
  316. $PluginArchiresNetworkInterfaceColor = new PluginArchiresNetworkInterfaceColor();
  317. $PluginArchiresVlanColor = new PluginArchiresVlanColor();
  318. $items_id1 = $ports[$wire["networkports_id_1"]]["items_id"];
  319. $itemtype1 = $ports[$wire["networkports_id_1"]]["itemtype"];
  320. $logical_number1 = $ports[$wire["networkports_id_1"]]["logical_number"];
  321. $name1 = $ports[$wire["networkports_id_1"]]["namep"];
  322. $ID1 = $ports[$wire["networkports_id_1"]]["idp"];
  323. $ip1 = $ports[$wire["networkports_id_1"]]["ip"];
  324. $netmask1 = $ports[$wire["networkports_id_2"]]["netmask"];
  325. $device_unique_name1 = $itemtype1 . "_" . $items_id1 . "_";
  326. $device_unique_name1 .= $devices[$itemtype1][$items_id1]["name"];
  327. $items_id2 = $ports[$wire["networkports_id_2"]]["items_id"];
  328. $itemtype2 = $ports[$wire["networkports_id_2"]]["itemtype"];
  329. $logical_number2 = $ports[$wire["networkports_id_2"]]["logical_number"];
  330. $name2 = $ports[$wire["networkports_id_2"]]["namep"];
  331. $ID2 = $ports[$wire["networkports_id_2"]]["idp"];
  332. $ip2 = $ports[$wire["networkports_id_2"]]["ip"];
  333. $netmask2 = $ports[$wire["networkports_id_2"]]["netmask"];
  334. $device_unique_name2 = $itemtype2 . "_" . $items_id2 . "_";
  335. $device_unique_name2 .= $devices[$itemtype2][$items_id2]["name"];
  336. $graph = "";
  337. if ($PluginArchiresView->fields["color"] == PluginArchiresView::PLUGIN_ARCHIRES_NETWORK_COLOR ) {
  338. if (empty($networkinterfaces_id1) && empty($networkinterfaces_id2)) {
  339. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  340. } else if (!empty($networkinterfaces_id1)) {
  341. if ($PluginArchiresNetworkInterfaceColor->getFromDBbyNetworkInterface($networkinterfaces_id1)) {
  342. $graph .= "edge [color=".$PluginArchiresNetworkInterfaceColor->fields["color"].",
  343. fontname=\"Verdana\", fontsize=\"5\"];\n";
  344. } else {
  345. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  346. }
  347. } else {
  348. if ($PluginArchiresNetworkInterfaceColor->getFromDBbyNetworkInterface($networkinterfaces_id2)) {
  349. $graph .= "edge [color=".$PluginArchiresNetworkInterfaceColor->fields["color"].",
  350. fontname=\"Verdana\", fontsize=\"5\"];\n";
  351. } else {
  352. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  353. }
  354. }
  355. } else if ($PluginArchiresView->fields["color"] == PluginArchiresView::PLUGIN_ARCHIRES_VLAN_COLOR) {
  356. $vlan1 = $PluginArchiresVlanColor->getVlanbyNetworkPort($ID1);
  357. $vlan2 = $PluginArchiresVlanColor->getVlanbyNetworkPort($ID2);
  358. if (empty($vlan1) && empty($vlan2)) {
  359. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  360. } else if (!empty($vlan1)) {
  361. if ($PluginArchiresVlanColor->getFromDBbyVlan($vlan1)) {
  362. $graph .= "edge [color=".$PluginArchiresVlanColor->fields["color"].",
  363. fontname=\"Verdana\", fontsize=\"5\"];\n";
  364. } else {
  365. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  366. }
  367. } else {
  368. if ($PluginArchiresVlanColor->getFromDBbyVlan($vlan2)) {
  369. $graph .= "edge [color=".$PluginArchiresVlanColor->fields["color"].",
  370. fontname=\"Verdana\", fontsize=\"5\"];\n";
  371. } else {
  372. $graph .= "edge [color=black,arrowsize=1, fontname=\"Verdana\", fontsize=\"5\"];\n";
  373. }
  374. }
  375. }
  376. //Display Ports
  377. if (($PluginArchiresView->fields["display_ports"] != 0)
  378. && ($PluginArchiresView->fields["engine"] != 1)) {
  379. $url_ports = $CFG_GLPI["root_doc"] . "/front/networkport.form.php?id=";
  380. $graph .= "\"".$device_unique_name1."\"";
  381. $graph .= " -- \"".$device_unique_name2."\"[label=";
  382. $graph .= "<<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";
  383. //display ip ports
  384. if ($PluginArchiresView->fields["display_ip"] != 0) {
  385. if (!empty($ip1)) {
  386. $graph .= "<tr><td>".$ip1;
  387. if (!empty($netmask1)) {
  388. $graph = sprintf(__('%1$s / %2$s'), $graph, $netmask1);
  389. }
  390. $graph .= "</td></tr>";
  391. }
  392. }
  393. $graph .= "<tr><td HREF=\"".$url_ports.$ID1."\" tooltip=\"".$name1;
  394. if ($_SESSION["glpiis_ids_visible"] || empty($name1)) {
  395. $graph.= "_".$ID1."_";
  396. }
  397. $graph .= "\">";
  398. if ($PluginArchiresView->fields["display_ports"]==1) {
  399. $graph .= __('Socket', 'archires')." ".$logical_number1;
  400. } else if ($PluginArchiresView->fields["display_ports"]==2) {
  401. $graph .= $name1;
  402. if ($_SESSION["glpiis_ids_visible"] || empty($name1)) {
  403. $graph = sprintf(__('%1$s (%2$s)'), $graph, $ID1);
  404. }
  405. }
  406. $graph .= "</td></tr>";
  407. if ($format!='svg') {
  408. $graph .= "<tr><td><img src= '../pics/socket.png' /></td></tr>";
  409. } else {
  410. $graph .= "<tr><td><img src=\"".realpath(GLPI_ROOT)."/plugins/archires/pics/socket.png\"/>".
  411. "</td></tr>";
  412. }
  413. $graph .= "<tr><td HREF=\"".$url_ports.$ID2."\" tooltip=\"".$name2;
  414. if ($_SESSION["glpiis_ids_visible"] || empty($name2)) {
  415. $graph.= "_".$ID2."_";
  416. }
  417. $graph .= "\">";
  418. if ($PluginArchiresView->fields["display_ports"]==1) {
  419. $graph .= __('Socket', 'archires')." ".$logical_number2;
  420. } else if ($PluginArchiresView->fields["display_ports"]==2) {
  421. $graph .= $name2;
  422. if ($_SESSION["glpiis_ids_visible"] || empty($name2)) {
  423. $graph = sprintf(__('%1$s (%2$s)'), $graph, $ID2);
  424. }
  425. }
  426. $graph .= "</td></tr>";
  427. //display ip ports
  428. if ($PluginArchiresView->fields["display_ip"] != 0) {
  429. if (!empty($ip2)) {
  430. $graph .= "<tr><td>".$ip2;
  431. if (!empty($netmask2)) {
  432. $graph = sprintf(__('%1$s / %2$s'), $graph, $netmask2);
  433. }
  434. $graph .= "</td></tr>";
  435. }
  436. }
  437. $graph .= "</table>>];\n";
  438. } else {
  439. $graph .= "\"".$device_unique_name1."\"";
  440. $graph .= " -- \"".$device_unique_name2."\";\n";
  441. }
  442. return $graph;
  443. }
  444. static function displayGraph($item, $plugin_archires_views_id, $select=0) {
  445. global $DB,$CFG_GLPI;
  446. $querytype = $item->getType();
  447. $ID = $item->getID();
  448. $object_view = $item->fields["plugin_archires_views_id"];
  449. $graph = new self();
  450. if (!isset($plugin_archires_views_id)) {
  451. $plugin_archires_views_id = $object_view;
  452. }
  453. if (!$object_view && !$plugin_archires_views_id) {
  454. return false;
  455. }
  456. $PluginArchiresView = new PluginArchiresView();
  457. $PluginArchiresView->getFromDB($plugin_archires_views_id);
  458. $PluginArchiresNetworkInterfaceColor = new PluginArchiresNetworkInterfaceColor();
  459. $PluginArchiresVlanColor = new PluginArchiresVlanColor();
  460. $PluginArchiresStateColor = new PluginArchiresStateColor();
  461. echo "<div class='center'>";
  462. $PluginArchiresView->viewSelect($item, $plugin_archires_views_id, $select);
  463. echo "</div>";
  464. if (isset($ID) && !empty($ID)) {
  465. echo "<img src='".$CFG_GLPI["root_doc"]."/plugins/archires/front/archires.map.php?id=".$ID.
  466. "&amp;querytype=".$querytype."&amp;plugin_archires_views_id=".$plugin_archires_views_id.
  467. "' alt='' usemap='#G'>";
  468. echo $graph->createGraph("cmapx", $item, $plugin_archires_views_id);
  469. }
  470. //legend
  471. if (isset($ID) && !empty($ID)) {
  472. echo "<div class='center'>";
  473. echo "<table cellpadding='5'>";
  474. echo "<tr><td class='top'>";
  475. if ( $PluginArchiresView->fields["color"] == 0 ) {
  476. $PluginArchiresNetworkInterfaceColor->showConfigForm();
  477. } else if ($PluginArchiresView->fields["color"] == 1 ) {
  478. $PluginArchiresVlanColor->showConfigForm();
  479. }
  480. echo "</td><td>".$PluginArchiresStateColor->showConfigForm()."</td>";
  481. echo "</tr>";
  482. echo "</table>";
  483. echo "</div>";
  484. } else {
  485. echo "<div class='center'><br><br><img src=\"".$CFG_GLPI["root_doc"].
  486. "/pics/warning.png\" alt='warning'><br><br>";
  487. echo "<b>".__('No item found')."</b></div>";
  488. }
  489. }
  490. function createGraph($format,$obj,$plugin_archires_views_id) {
  491. global $DB,$CFG_GLPI;
  492. $type = get_class($obj);
  493. $ID = $obj->fields["id"];
  494. $object_view = $obj->fields["plugin_archires_views_id"];
  495. if (!isset($plugin_archires_views_id)) {
  496. $plugin_archires_views_id = $object_view;
  497. }
  498. $PluginArchiresView = new PluginArchiresView();
  499. $plugin = new Plugin();
  500. if ($plugin->isActivated("appliances")) {
  501. $PluginArchiresApplianceQuery = new PluginArchiresApplianceQuery();
  502. }
  503. $PluginArchiresLocationQuery = new PluginArchiresLocationQuery();
  504. $PluginArchiresNetworkEquipmentQuery = new PluginArchiresNetworkEquipmentQuery();
  505. $PluginArchiresImageItem = new PluginArchiresImageItem();
  506. $PluginArchiresView->getFromDB($plugin_archires_views_id);
  507. $devices = [];
  508. $ports = [];
  509. if ($type == 'PluginArchiresLocationQuery') {
  510. $devices = $PluginArchiresLocationQuery->Query($ID, $PluginArchiresView, true);
  511. $ports = $PluginArchiresLocationQuery->Query($ID, $PluginArchiresView, false);
  512. } else if ($type == 'PluginArchiresNetworkEquipmentQuery') {
  513. $devices = $PluginArchiresNetworkEquipmentQuery->Query($ID, $PluginArchiresView, true);
  514. $ports = $PluginArchiresNetworkEquipmentQuery->Query($ID, $PluginArchiresView, false);
  515. } else if ($type == 'PluginArchiresApplianceQuery') {
  516. $devices = $PluginArchiresApplianceQuery->Query($ID, $PluginArchiresView, true);
  517. $ports = $PluginArchiresApplianceQuery->Query($ID, $PluginArchiresView, false);
  518. }
  519. $wires = [];
  520. $query = ['SELECT' => ['id', 'networkports_id_1', 'networkports_id_2'],
  521. 'FROM' => 'glpi_networkports_networkports'];
  522. if ($result = $DB->request($query)) {
  523. while ($data = $result->next()) {
  524. $wires[$data["id"]]["networkports_id_1"] = $data["networkports_id_1"];
  525. $wires[$data["id"]]["networkports_id_2"] = $data["networkports_id_2"];
  526. }
  527. }
  528. $graph = "graph G {\n";
  529. $graph .= "overlap=false;\n";
  530. $graph .= "bgcolor=white;\n";
  531. //items
  532. $graph .= "node [shape=polygon, sides=6, fontname=\"Verdana\", fontsize=\"5\"];\n";
  533. foreach ($devices as $itemtype => $typed_devices) {
  534. foreach ($typed_devices as $device_id => $device) {
  535. $image_name = $PluginArchiresImageItem->displayItemImage($device["type"], $itemtype,
  536. false);
  537. $link = Toolbox::getItemTypeFormURL($itemtype);
  538. $url = $link."?id=".$device_id;
  539. $graph .= $this->graphItems($device, $device_id, $itemtype, $format, $image_name, $url,
  540. $PluginArchiresView);
  541. }
  542. }
  543. foreach ($wires as $wire) {
  544. if (!empty($ports[$wire["networkports_id_1"]])
  545. && !empty($ports[$wire["networkports_id_2"]])
  546. && isset($ports[$wire["networkports_id_1"]])
  547. && isset($ports[$wire["networkports_id_2"]]) ) {
  548. $graph .= $this->graphPorts($devices,$ports,$wire,$format,$PluginArchiresView);
  549. }
  550. }
  551. $graph .= "}\n";
  552. return $this->generateGraphviz($graph,$format,$PluginArchiresView);
  553. }
  554. function generateGraphviz($graph, $format, $PluginArchiresView) {
  555. if ($PluginArchiresView->fields["engine"] != 0) {
  556. $engine_archires = "neato";
  557. } else {
  558. $engine_archires = "dot";
  559. }
  560. return self::dotIt($engine_archires, $graph, $format);
  561. }
  562. static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
  563. switch ($item->getType()) {
  564. case 'PluginArchiresApplianceQuery' :
  565. case 'PluginArchiresLocationQuery' :
  566. case 'PluginArchiresNetworkEquipmentQuery' :
  567. switch ($tabnum) {
  568. case 1 :
  569. self::test($item);
  570. break;
  571. case 2 :
  572. $plugin_archires_views_id = $item->fields["plugin_archires_views_id"];
  573. self::displayGraph($item, $plugin_archires_views_id);
  574. PluginArchiresView::linkToAllViews($item);
  575. break;
  576. }
  577. break;
  578. }
  579. return true;
  580. }
  581. function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
  582. if (!$withtemplate && Session::haveRight("plugin_archires", READ)) {
  583. switch ($item->getType()) {
  584. case 'PluginArchiresApplianceQuery' :
  585. case 'PluginArchiresLocationQuery' :
  586. case 'PluginArchiresNetworkEquipmentQuery' :
  587. return ['1' => __('Test'),
  588. '2' => __('Generation', 'archires')];
  589. }
  590. }
  591. return '';
  592. }
  593. }