prototype.class.php 29 KB

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