* Copyright 2016 Nicolas Goaziou * * This file is part of CHD Gestion. * * CHD Gestion is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CHD Gestion is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CHD Gestion. If not, see . **/ include_once('inc/config.php'); $mysqli = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database']); if (mysqli_connect_errno()) { die(mysqli_connect_error()); } unset($db_config); $mysqli->set_charset("utf8") or die($mysqli->error); $opt_show_source=array_key_exists('s', $_GET); $opt_embed=array_key_exists('e', $_GET); $opt_ping=array_key_exists('p', $_GET); $arg_adt=(array_key_exists('a', $_GET) && is_numeric($_GET['a']))?(0+$_GET['a']):NULL; if (!$arg_adt) { die("Wrong args"); } if ($opt_ping) { ?> array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") ); $pipes=array(); $proc = proc_open('dot -Tsvg', $descriptorspec, $pipes, '/var/www/priv/api'); if (!is_resource($proc)) die("Pb ouverture dot"); $fd=$pipes[0]; } else { header('Content-Type: text/plain'); $fd=fopen("php://output","w"); } function graph_header() { return << $limit ) { $breakpos = strpos($adt_title, ' ', $limit); if ( $breakpos !== FALSE ) { $adt_title = substr($adt_title, 0, $breakpos) . '
' . substr($adt_title, $breakpos); } } } return << $adt
$adt_title >, URL="/gestion/adherents/view/$adherent_id"]; EOD; } // >]; function node_equipement($eid, $img, $label, $ip) { $label = htmlentities($label); $sub = (strlen($ip)>0)?$ip:"eid $eid"; return << $label
$sub >, URL="/gestion/equipements/view/$eid"]; EOD; } function edge_service($adherent_id, $routeur_eid, $service) { $service = htmlentities($service); return << "e$routeur_eid" [ label=<$service> ] EOD; } function node_relais($rid, $eid, $shape, $label) { $label = htmlentities($label); return << 
$label
rid $rid >, URL="/gestion/relais/view/$rid"]; "e$eid" -> "r$rid" [ dir=back, arrowtail=empty, style=dashed ]; EOD; } // >, URL="/gestion/relais/view/$rid"]; fwrite($fd, graph_header()); $sql = "SELECT v.adt, v.adt_title, v.service_id, v.service, v.relais_id, v.relais_degre, v.relais, v.routeur_equipement_id, v.antenne1_equipement_id, v.antenne2_equipement_id, e1.ipmgmt_id AS e1_ip, IF(LENGTH(e1.hostname)>0,e1.hostname,'Routeur') AS e1_title, CONCAT('emid', e1.equipement_modele_id, '.png') AS e1_img, e2.ipmgmt_id AS e2_ip, IF(LENGTH(e2.hostname)>0,e2.hostname,'Antenne1') AS e2_title, CONCAT('emid', e2.equipement_modele_id, '.png') AS e2_img, e3.ipmgmt_id AS e3_ip, IF(LENGTH(e3.hostname)>0,e3.hostname,'Antenne2') AS e3_title, CONCAT('emid', e3.equipement_modele_id, '.png') AS e3_img FROM v_relais_detail v LEFT JOIN equipements e1 ON (e1.id = v.routeur_equipement_id) LEFT JOIN equipements e2 ON (e2.id = v.antenne1_equipement_id) LEFT JOIN equipements e3 ON (e3.id = v.antenne2_equipement_id) WHERE adherent_id=" . $arg_adt; $res = $mysqli->query($sql); $empty=true; while ( $row = $res->fetch_assoc() ) { $empty=false; if ( isset($row['adt']) && isset($row['service_id']) && isset($row['routeur_equipement_id']) ) { fwrite($fd, node_adt($arg_adt, 'none', $row['adt'], $row['adt_title'])); //egg fwrite($fd, node_equipement($row['routeur_equipement_id'], $row['e1_img'], $row['e1_title'], $row['e1_ip'])); fwrite($fd, edge_service($arg_adt,$row['routeur_equipement_id'], $row['service'])); } if ( isset($row['antenne1_equipement_id']) ) { fwrite($fd, node_equipement($row['antenne1_equipement_id'], $row['e2_img'], $row['e2_title'], $row['e2_ip'])); fwrite($fd, "\te".$row['routeur_equipement_id']." -> e".$row['antenne1_equipement_id'].";\n"); } if ( isset($row['antenne2_equipement_id']) ) { fwrite($fd, node_equipement($row['antenne2_equipement_id'], $row['e3_img'], $row['e3_title'], $row['e3_ip'])); fwrite($fd, "\te".$row['antenne1_equipement_id']." -> e".$row['antenne2_equipement_id'].";\n"); } if ( isset($row['relais_id']) && isset($row['relais_degre']) ) { switch ($row['relais_degre']) { case 1: $e_node = $row['routeur_equipement_id']; break; case 2: $e_node = $row['antenne1_equipement_id']; break; case 3: $e_node = $row['antenne2_equipement_id']; break; default: continue; } fwrite($fd, node_relais($row['relais_id'], $e_node, 'none', $row['relais'])); } } if ($empty) { fwrite($fd, "\tnothing;\n"); } fwrite($fd, "}\n"); if (!$opt_show_source) { fclose($fd); if ($opt_embed) { // Skip and for ($i=0 ; $i<3 && !feof($pipes[1]); $i++) { fgets($pipes[1]); } } $image_data = stream_get_contents($pipes[1]); if ($opt_embed) { $image_data = str_replace('image xlink:href="icons', 'image xlink:href="/api/icons', $image_data); $image_data = str_replace('xlink:title="<TABLE>"', 'target="_blank"', $image_data); } fclose($pipes[1]); $err_data = stream_get_contents($pipes[2]); fclose($pipes[2]); $return_value = proc_close($proc); if ( $return_value !== 0 || strlen($err_data) !== 0 ) { header('Content-Type: text/plain'); echo $err_data; } else { header('Content-Type: image/svg+xml'); echo $image_data; } flush(); }